LLVM 20.0.0git
X86WinEHState.cpp
Go to the documentation of this file.
1//===-- X86WinEHState - Insert EH state updates for win32 exceptions ------===//
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// 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.
13//
14//===----------------------------------------------------------------------===//
15
16#include "X86.h"
18#include "llvm/Analysis/CFG.h"
21#include "llvm/IR/CFG.h"
23#include "llvm/IR/Function.h"
24#include "llvm/IR/IRBuilder.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"
31#include <deque>
32
33using namespace llvm;
34
35#define DEBUG_TYPE "winehstate"
36
37namespace {
38const int OverdefinedState = INT_MIN;
39
40class WinEHStatePass : public FunctionPass {
41public:
42 static char ID; // Pass identification, replacement for typeid.
43
44 WinEHStatePass() : FunctionPass(ID) { }
45
46 bool runOnFunction(Function &Fn) override;
47
48 bool doInitialization(Module &M) override;
49
50 bool doFinalization(Module &M) override;
51
52 void getAnalysisUsage(AnalysisUsage &AU) const override;
53
54 StringRef getPassName() const override {
55 return "Windows 32-bit x86 EH state insertion";
56 }
57
58private:
59 void emitExceptionRegistrationRecord(Function *F);
60
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);
65
66 Value *emitEHLSDA(IRBuilder<> &Builder, Function *F);
67
68 Function *generateLSDAInEAXThunk(Function *ParentFunc);
69
70 bool isStateStoreNeeded(EHPersonality Personality, CallBase &Call);
71 void rewriteSetJmpCall(IRBuilder<> &Builder, Function &F, CallBase &Call,
72 Value *State);
73 int getBaseStateForBB(DenseMap<BasicBlock *, ColorVector> &BlockColors,
74 WinEHFuncInfo &FuncInfo, BasicBlock *BB);
75 int getStateForCall(DenseMap<BasicBlock *, ColorVector> &BlockColors,
76 WinEHFuncInfo &FuncInfo, CallBase &Call);
77
78 // Module-level type getters.
79 Type *getEHLinkRegistrationType();
80 Type *getSEHRegistrationType();
81 Type *getCXXEHRegistrationType();
82
83 // Per-module data.
84 Module *TheModule = nullptr;
85 StructType *EHLinkRegistrationTy = nullptr;
86 StructType *CXXEHRegistrationTy = nullptr;
87 StructType *SEHRegistrationTy = nullptr;
88 FunctionCallee SetJmp3 = nullptr;
89 FunctionCallee CxxLongjmpUnwind = nullptr;
90
91 // Per-function state
93 Function *PersonalityFn = nullptr;
94 bool UseStackGuard = false;
95 int ParentBaseState = 0;
96 FunctionCallee SehLongjmpUnwind = nullptr;
97 Constant *Cookie = nullptr;
98
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;
102
103 // The allocation containing the EH security guard.
104 AllocaInst *EHGuardNode = nullptr;
105
106 /// The index of the state field of RegNode.
107 int StateFieldIndex = ~0U;
108
109 /// The linked list node subobject inside of RegNode.
110 Value *Link = nullptr;
111};
112} // namespace
113
114FunctionPass *llvm::createX86WinEHStatePass() { return new WinEHStatePass(); }
115
116char WinEHStatePass::ID = 0;
117
118INITIALIZE_PASS(WinEHStatePass, "x86-winehstate",
119 "Insert stores for EH state numbers", false, false)
120
121bool WinEHStatePass::doInitialization(Module &M) {
122 TheModule = &M;
123 return false;
124}
125
126bool WinEHStatePass::doFinalization(Module &M) {
127 assert(TheModule == &M);
128 TheModule = nullptr;
129 EHLinkRegistrationTy = nullptr;
130 CXXEHRegistrationTy = nullptr;
131 SEHRegistrationTy = nullptr;
132 SetJmp3 = nullptr;
133 CxxLongjmpUnwind = nullptr;
134 SehLongjmpUnwind = nullptr;
135 Cookie = nullptr;
136 return false;
137}
138
139void WinEHStatePass::getAnalysisUsage(AnalysisUsage &AU) const {
140 // This pass should only insert a stack allocation, memory accesses, and
141 // localrecovers.
142 AU.setPreservesCFG();
143}
144
145bool 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())
150 return false;
151
152 // Check the personality. Do nothing if this personality doesn't use funclets.
153 if (!F.hasPersonalityFn())
154 return false;
155 PersonalityFn =
156 dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts());
157 if (!PersonalityFn)
158 return false;
159 Personality = classifyEHPersonality(PersonalityFn);
160 if (!isFuncletEHPersonality(Personality))
161 return false;
162
163 // Skip this function if there are no EH pads and we aren't using IR-level
164 // outlining.
165 bool HasPads = false;
166 for (BasicBlock &BB : F) {
167 if (BB.isEHPad()) {
168 HasPads = true;
169 break;
170 }
171 }
172 if (!HasPads)
173 return false;
174
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())},
180 /*isVarArg=*/true));
181
182 emitExceptionRegistrationRecord(&F);
183
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);
191
192 // Reset per-function state.
193 PersonalityFn = nullptr;
194 Personality = EHPersonality::Unknown;
195 UseStackGuard = false;
196 RegNode = nullptr;
197 EHGuardNode = nullptr;
198
199 return true;
200}
201
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;
208/// };
209Type *WinEHStatePass::getEHLinkRegistrationType() {
210 if (EHLinkRegistrationTy)
211 return EHLinkRegistrationTy;
212 LLVMContext &Context = TheModule->getContext();
213 EHLinkRegistrationTy = StructType::create(Context, "EHRegistrationNode");
214 Type *FieldTys[] = {
215 PointerType::getUnqual(
216 EHLinkRegistrationTy->getContext()), // EHRegistrationNode *Next
217 PointerType::getUnqual(Context) // EXCEPTION_DISPOSITION (*Handler)(...)
218 };
219 EHLinkRegistrationTy->setBody(FieldTys, false);
220 return EHLinkRegistrationTy;
221}
222
223/// The __CxxFrameHandler3 registration node:
224/// struct CXXExceptionRegistration {
225/// void *SavedESP;
226/// EHRegistrationNode SubRecord;
227/// int32_t TryLevel;
228/// };
229Type *WinEHStatePass::getCXXEHRegistrationType() {
230 if (CXXEHRegistrationTy)
231 return CXXEHRegistrationTy;
232 LLVMContext &Context = TheModule->getContext();
233 Type *FieldTys[] = {
234 PointerType::getUnqual(Context), // void *SavedESP
235 getEHLinkRegistrationType(), // EHRegistrationNode SubRecord
236 Type::getInt32Ty(Context) // int32_t TryLevel
237 };
238 CXXEHRegistrationTy =
239 StructType::create(FieldTys, "CXXExceptionRegistration");
240 return CXXEHRegistrationTy;
241}
242
243/// The _except_handler3/4 registration node:
244/// struct EH4ExceptionRegistration {
245/// void *SavedESP;
246/// _EXCEPTION_POINTERS *ExceptionPointers;
247/// EHRegistrationNode SubRecord;
248/// int32_t EncodedScopeTable;
249/// int32_t TryLevel;
250/// };
251Type *WinEHStatePass::getSEHRegistrationType() {
252 if (SEHRegistrationTy)
253 return SEHRegistrationTy;
254 LLVMContext &Context = TheModule->getContext();
255 Type *FieldTys[] = {
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
261 };
262 SEHRegistrationTy = StructType::create(FieldTys, "SEHExceptionRegistration");
263 return SEHRegistrationTy;
264}
265
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.
270void WinEHStatePass::emitExceptionRegistrationRecord(Function *F) {
271 assert(Personality == EHPersonality::MSVC_CXX ||
272 Personality == EHPersonality::MSVC_X86SEH);
273
274 // Struct type of RegNode. Used for GEPing.
275 Type *RegNodeTy;
276
277 IRBuilder<> Builder(&F->getEntryBlock(), F->getEntryBlock().begin());
278 Type *Int8PtrType = Builder.getPtrTy();
279 Type *Int32Ty = Builder.getInt32Ty();
280 Type *VoidTy = Builder.getVoidTy();
281
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));
288 // TryLevel = -1
289 StateFieldIndex = 2;
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);
296
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");
307
308 // Allocate local structures.
309 RegNodeTy = getSEHRegistrationType();
310 RegNode = Builder.CreateAlloca(RegNodeTy);
311 if (UseStackGuard)
312 EHGuardNode = Builder.CreateAlloca(Int32Ty);
313
314 // SavedESP = llvm.stacksave()
315 Value *SP = Builder.CreateStackSave();
316 Builder.CreateStore(SP, Builder.CreateStructGEP(RegNodeTy, RegNode, 0));
317 // TryLevel = -2 / -1
318 StateFieldIndex = 4;
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.
326 if (UseStackGuard) {
327 Cookie = TheModule->getOrInsertGlobal("__security_cookie", Int32Ty);
328 Value *Val = Builder.CreateLoad(Int32Ty, Cookie, "cookie");
329 LSDA = Builder.CreateXor(LSDA, Val);
330 }
331 Builder.CreateStore(LSDA, Builder.CreateStructGEP(RegNodeTy, RegNode, 3));
332
333 // If using _except_handler4, the EHGuard contains: FramePtr xor Cookie.
334 if (UseStackGuard) {
335 Value *Val = Builder.CreateLoad(Int32Ty, Cookie);
336 Value *FrameAddr = Builder.CreateCall(
338 TheModule, Intrinsic::frameaddress,
339 Builder.getPtrTy(
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);
345 }
346
347 // Register the exception handler.
348 Link = Builder.CreateStructGEP(RegNodeTy, RegNode, 2);
349 linkExceptionRegistration(Builder, PersonalityFn);
350
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);
357 } else {
358 llvm_unreachable("unexpected personality function");
359 }
360
361 // Insert an unlink before all returns.
362 for (BasicBlock &BB : *F) {
363 Instruction *T = BB.getTerminator();
364 if (!isa<ReturnInst>(T))
365 continue;
366 Builder.SetInsertPoint(T);
367 unlinkExceptionRegistration(Builder);
368 }
369}
370
371Value *WinEHStatePass::emitEHLSDA(IRBuilder<> &Builder, Function *F) {
372 return Builder.CreateCall(
373 Intrinsic::getDeclaration(TheModule, Intrinsic::x86_seh_lsda), F);
374}
375
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:
381/// movl $lsda, %eax
382/// jmpl ___CxxFrameHandler3
383Function *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,
388 Int8PtrType};
389 FunctionType *TrampolineTy =
390 FunctionType::get(Int32Ty, ArrayRef(&ArgTys[0], 4),
391 /*isVarArg=*/false);
392 FunctionType *TargetFuncTy =
393 FunctionType::get(Int32Ty, ArrayRef(&ArgTys[0], 5),
394 /*isVarArg=*/false);
395 Function *Trampoline =
398 ParentFunc->getName()),
399 TheModule);
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);
413 return Trampoline;
414}
415
416void WinEHStatePass::linkExceptionRegistration(IRBuilder<> &Builder,
417 Function *Handler) {
418 // Emit the .safeseh directive for this function.
419 Handler->addFnAttr("safeseh");
420
421 LLVMContext &C = Builder.getContext();
422 Type *LinkTy = getEHLinkRegistrationType();
423 // Handler = Handler
424 Builder.CreateStore(Handler, Builder.CreateStructGEP(LinkTy, Link, 1));
425 // Next = [fs:00]
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));
429 // [fs:00] = Link
430 Builder.CreateStore(Link, FSZero);
431}
432
433void 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());
437 Builder.Insert(GEP);
438 Link = GEP;
439 }
440
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);
448}
449
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.
454void 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)
458 return;
459
461 Call.getOperandBundlesAsDefs(OpBundles);
462
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);
471 if (UseStackGuard)
472 OptionalArgs.push_back(Cookie);
473 } else {
474 llvm_unreachable("unhandled personality!");
475 }
476
478 Args.push_back(
479 Builder.CreateBitCast(Call.getArgOperand(0), Builder.getPtrTy()));
480 Args.push_back(Builder.getInt32(OptionalArgs.size()));
481 Args.append(OptionalArgs.begin(), OptionalArgs.end());
482
483 CallBase *NewCall;
484 if (auto *CI = dyn_cast<CallInst>(&Call)) {
485 CallInst *NewCI = Builder.CreateCall(SetJmp3, Args, OpBundles);
486 NewCI->setTailCallKind(CI->getTailCallKind());
487 NewCall = NewCI;
488 } else {
489 auto *II = cast<InvokeInst>(&Call);
490 NewCall = Builder.CreateInvoke(
491 SetJmp3, II->getNormalDest(), II->getUnwindDest(), Args, OpBundles);
492 }
493 NewCall->setCallingConv(Call.getCallingConv());
494 NewCall->setAttributes(Call.getAttributes());
495 NewCall->setDebugLoc(Call.getDebugLoc());
496
497 NewCall->takeName(&Call);
498 Call.replaceAllUsesWith(NewCall);
499 Call.eraseFromParent();
500}
501
502// Figure out what state we should assign calls in this block.
503int WinEHStatePass::getBaseStateForBB(
505 BasicBlock *BB) {
506 int BaseState = ParentBaseState;
507 auto &BBColors = BlockColors[BB];
508
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;
516 }
517
518 return BaseState;
519}
520
521// Calculate the state a call-site is in.
522int WinEHStatePass::getStateForCall(
524 CallBase &Call) {
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];
529 }
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());
533}
534
535// Calculate the intersection of all the FinalStates for a BasicBlock's
536// predecessors.
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;
543
544 // This is an EH Pad, conservatively report this basic block as overdefined.
545 if (BB->isEHPad())
546 return OverdefinedState;
547
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;
555
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;
560
561 int PredState = PredEndState->second;
562 assert(PredState != OverdefinedState &&
563 "overdefined BBs shouldn't be in FinalStates");
564 if (CommonState == OverdefinedState)
565 CommonState = PredState;
566
567 // At least two predecessors have different FinalStates,
568 // conservatively report this basic block as overdefined.
569 if (CommonState != PredState)
570 return OverdefinedState;
571 }
572
573 return CommonState;
574}
575
576// Calculate the intersection of all the InitialStates for a BasicBlock's
577// successors.
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;
584
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;
592
593 // This is an EH Pad, conservatively report this basic block as overdefined.
594 if (SuccBB->isEHPad())
595 return OverdefinedState;
596
597 int SuccState = SuccStartState->second;
598 assert(SuccState != OverdefinedState &&
599 "overdefined BBs shouldn't be in FinalStates");
600 if (CommonState == OverdefinedState)
601 CommonState = SuccState;
602
603 // At least two successors have different InitialStates,
604 // conservatively report this basic block as overdefined.
605 if (CommonState != SuccState)
606 return OverdefinedState;
607 }
608
609 return CommonState;
610}
611
612bool WinEHStatePass::isStateStoreNeeded(EHPersonality Personality,
613 CallBase &Call) {
614 // If the function touches memory, it needs a state store.
615 if (isAsynchronousEHPersonality(Personality))
616 return !Call.doesNotAccessMemory();
617
618 // If the function throws, it needs a state store.
619 return !Call.doesNotThrow();
620}
621
622void 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());
627 Builder.CreateCall(
628 Intrinsic::getDeclaration(TheModule, Intrinsic::x86_seh_ehregnode),
629 {RegNodeI8});
630
631 if (EHGuardNode) {
632 IRBuilder<> Builder(EHGuardNode->getNextNode());
633 Value *EHGuardNodeI8 =
634 Builder.CreateBitCast(EHGuardNode, Builder.getPtrTy());
635 Builder.CreateCall(
636 Intrinsic::getDeclaration(TheModule, Intrinsic::x86_seh_ehguard),
637 {EHGuardNodeI8});
638 }
639
640 // Calculate state numbers.
641 if (isAsynchronousEHPersonality(Personality))
642 calculateSEHStateNumbers(&F, FuncInfo);
643 else
645
646 // Iterate all the instructions and emit state number stores.
649
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;
660 int FinalState;
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))
666 continue;
667
668 int State = getStateForCall(BlockColors, FuncInfo, *Call);
669 if (InitialState == OverdefinedState)
670 InitialState = State;
671 FinalState = State;
672 }
673 // No call-sites in this basic block? That's OK, we will come back to these
674 // in a later pass.
675 if (InitialState == OverdefinedState) {
676 Worklist.push_back(BB);
677 continue;
678 }
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});
685 }
686
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)
693 continue;
694
695 int PredState = getPredState(FinalStates, F, ParentBaseState, BB);
696 if (PredState == OverdefinedState)
697 continue;
698
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);
705 }
706
707 // Try to hoist stores from successors.
708 for (BasicBlock *BB : RPOT) {
709 int SuccState = getSuccState(InitialStates, F, ParentBaseState, BB);
710 if (SuccState == OverdefinedState)
711 continue;
712
713 // Update our FinalState to reflect the common InitialState of our
714 // successors.
715 FinalStates.insert({BB, SuccState});
716 }
717
718 // Finally, insert state stores before call-sites which transition us to a new
719 // state.
720 for (BasicBlock *BB : RPOT) {
721 auto &BBColors = BlockColors[BB];
722 BasicBlock *FuncletEntryBB = BBColors.front();
723 if (isa<CleanupPadInst>(FuncletEntryBB->getFirstNonPHI()))
724 continue;
725
726 int PrevState = getPredState(FinalStates, F, ParentBaseState, BB);
727 LLVM_DEBUG(dbgs() << "X86WinEHState: " << BB->getName()
728 << " PrevState=" << PrevState << '\n');
729
730 for (Instruction &I : *BB) {
731 auto *Call = dyn_cast<CallBase>(&I);
732 if (!Call || !isStateStoreNeeded(Personality, *Call))
733 continue;
734
735 int State = getStateForCall(BlockColors, FuncInfo, *Call);
736 if (State != PrevState)
737 insertStateNumberStore(&I, State);
738 PrevState = State;
739 }
740
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);
746 }
747
748 SmallVector<CallBase *, 1> SetJmp3Calls;
749 for (BasicBlock *BB : RPOT) {
750 for (Instruction &I : *BB) {
751 auto *Call = dyn_cast<CallBase>(&I);
752 if (!Call)
753 continue;
754 if (Call->getCalledOperand()->stripPointerCasts() !=
755 SetJmp3.getCallee()->stripPointerCasts())
756 continue;
757
758 SetJmp3Calls.push_back(Call);
759 }
760 }
761
762 for (CallBase *Call : SetJmp3Calls) {
763 auto &BBColors = BlockColors[Call->getParent()];
764 BasicBlock *FuncletEntryBB = BBColors.front();
765 bool InCleanup = isa<CleanupPadInst>(FuncletEntryBB->getFirstNonPHI());
766
767 IRBuilder<> Builder(Call);
768 Value *State;
769 if (InCleanup) {
770 Value *StateField = Builder.CreateStructGEP(RegNode->getAllocatedType(),
771 RegNode, StateFieldIndex);
772 State = Builder.CreateLoad(Builder.getInt32Ty(), StateField);
773 } else {
774 State = Builder.getInt32(getStateForCall(BlockColors, FuncInfo, *Call));
775 }
776 rewriteSetJmpCall(Builder, F, *Call, State);
777 }
778}
779
780void 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);
785}
#define LLVM_DEBUG(X)
Definition: Debug.h:101
Hexagon Common GEP
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
Module.h This file contains the declarations for the Module class.
uint64_t IntrinsicInst * II
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
This file builds on the ADT/GraphTraits.h file to build a generic graph post order iterator.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static int getPredState(DenseMap< BasicBlock *, int > &FinalStates, Function &F, int ParentBaseState, BasicBlock *BB)
static int getSuccState(DenseMap< BasicBlock *, int > &InitialStates, Function &F, int ParentBaseState, BasicBlock *BB)
an instruction to allocate memory on the stack
Definition: Instructions.h:61
Represent the analysis usage information of a pass.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:256
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
LLVM Basic Block Representation.
Definition: BasicBlock.h:61
const Instruction * getFirstNonPHI() const
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
Definition: BasicBlock.cpp:367
const Instruction & front() const
Definition: BasicBlock.h:471
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition: BasicBlock.h:212
bool isEHPad() const
Return true if this basic block is an exception handling block.
Definition: BasicBlock.h:675
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.h:239
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1236
void setCallingConv(CallingConv::ID CC)
Definition: InstrTypes.h:1527
void setAttributes(AttributeList A)
Set the parameter attributes for this call.
Definition: InstrTypes.h:1546
This class represents a function call, abstracting a target machine's calling convention.
void setTailCallKind(TailCallKind TCK)
This is an important base class in LLVM.
Definition: Constant.h:42
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Definition: Constants.cpp:370
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:155
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
Definition: DenseMap.h:151
iterator end()
Definition: DenseMap.h:84
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:211
A handy container for a FunctionType+Callee-pointer pair, which can be passed around as a single enti...
Definition: DerivedTypes.h:168
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:310
virtual bool runOnFunction(Function &F)=0
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
void addFnAttr(Attribute::AttrKind Kind)
Add function attributes to this function.
Definition: Function.cpp:653
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)
Definition: Function.h:172
arg_iterator arg_begin()
Definition: Function.h:866
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition: Function.cpp:380
void setComdat(Comdat *C)
Definition: Globals.cpp:206
const Comdat * getComdat() const
Definition: GlobalObject.h:129
static StringRef dropLLVMManglingEscape(StringRef Name)
If the given string begins with the GlobalValue name mangling escape character '\1',...
Definition: GlobalValue.h:567
@ InternalLinkage
Rename collisions when linking (static functions).
Definition: GlobalValue.h:59
InvokeInst * CreateInvoke(FunctionType *Ty, Value *Callee, BasicBlock *NormalDest, BasicBlock *UnwindDest, ArrayRef< Value * > Args, ArrayRef< OperandBundleDef > OpBundles, const Twine &Name="")
Create an invoke instruction.
Definition: IRBuilder.h:1175
Value * CreateStructGEP(Type *Ty, Value *Ptr, unsigned Idx, const Twine &Name="")
Definition: IRBuilder.h:1989
IntegerType * getInt32Ty()
Fetch the type representing a 32-bit integer.
Definition: IRBuilder.h:523
ReturnInst * CreateRet(Value *V)
Create a 'ret <val>' instruction.
Definition: IRBuilder.h:1112
ConstantInt * getInt32(uint32_t C)
Get a constant 32-bit value.
Definition: IRBuilder.h:483
InstTy * Insert(InstTy *I, const Twine &Name="") const
Insert and return the specified instruction.
Definition: IRBuilder.h:142
Value * CreateBitCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:2147
LoadInst * CreateLoad(Type *Ty, Value *Ptr, const char *Name)
Provided to resolve 'CreateLoad(Ty, Ptr, "...")' correctly, instead of converting the string to 'bool...
Definition: IRBuilder.h:1807
LLVMContext & getContext() const
Definition: IRBuilder.h:173
StoreInst * CreateStore(Value *Val, Value *Ptr, bool isVolatile=false)
Definition: IRBuilder.h:1820
PointerType * getPtrTy(unsigned AddrSpace=0)
Fetch the type representing a pointer.
Definition: IRBuilder.h:566
CallInst * CreateCall(FunctionType *FTy, Value *Callee, ArrayRef< Value * > Args=std::nullopt, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:2432
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2686
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
Definition: Instruction.h:463
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: Pass.cpp:98
virtual bool doInitialization(Module &)
doInitialization - Virtual method overridden by subclasses to do any necessary initialization before ...
Definition: Pass.h:119
virtual bool doFinalization(Module &)
doFinalization - Virtual method overriden by subclasses to do any necessary clean up after all passes...
Definition: Pass.h:123
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
size_t size() const
Definition: SmallVector.h:91
void push_back(const T &Elt)
Definition: SmallVector.h:426
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Class to represent struct types.
Definition: DerivedTypes.h:216
static StructType * create(LLVMContext &Context, StringRef Name)
This creates an identified struct.
Definition: Type.cpp:501
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
static Type * getVoidTy(LLVMContext &C)
static IntegerType * getInt32Ty(LLVMContext &C)
LLVM Value Representation.
Definition: Value.h:74
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:309
void takeName(Value *V)
Transfer the name from V to this value.
Definition: Value.cpp:383
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
@ X86_StdCall
stdcall is mostly used by the Win32 API.
Definition: CallingConv.h:99
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
Function * getDeclaration(Module *M, ID id, ArrayRef< Type * > Tys=std::nullopt)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
Definition: Function.cpp:1539
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
FunctionPass * createX86WinEHStatePass()
Return an IR pass that inserts EH registration stack objects and explicit EH state updates.
auto successors(const MachineBasicBlock *BB)
DenseMap< BasicBlock *, ColorVector > colorEHFunclets(Function &F)
If an EH funclet personality is in use (see isFuncletEHPersonality), this will recompute which blocks...
void calculateWinCXXEHStateNumbers(const Function *ParentFn, WinEHFuncInfo &FuncInfo)
Analyze the IR in ParentFn and it's handlers to build WinEHFuncInfo, which describes the state number...
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
bool isFuncletEHPersonality(EHPersonality Pers)
Returns true if this is a personality function that invokes handler funclets (which must return to it...
void calculateSEHStateNumbers(const Function *ParentFn, WinEHFuncInfo &FuncInfo)
bool isAsynchronousEHPersonality(EHPersonality Pers)
Returns true if this personality function catches asynchronous exceptions.
auto predecessors(const MachineBasicBlock *BB)
DenseMap< const FuncletPadInst *, int > FuncletBaseStateMap
Definition: WinEHFuncInfo.h:92
DenseMap< const InvokeInst *, int > InvokeStateMap
Definition: WinEHFuncInfo.h:93