blob: ce69ea721993ffd30a608d7263a7ab7fe31ea95c [file] [log] [blame]
Reid Kleckner0738a9c2015-05-05 17:44:16 +00001//===-- X86WinEHState - Insert EH state updates for win32 exceptions ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// All functions using an MSVC EH personality use an explicitly updated state
11// number stored in an exception registration stack object. The registration
12// object is linked into a thread-local chain of registrations stored at fs:00.
13// This pass adds the registration object and EH state updates.
14//
15//===----------------------------------------------------------------------===//
16
17#include "X86.h"
18#include "llvm/Analysis/LibCallSemantics.h"
Reid Klecknerfe4d4912015-05-28 22:00:24 +000019#include "llvm/CodeGen/MachineModuleInfo.h"
Reid Kleckner0738a9c2015-05-05 17:44:16 +000020#include "llvm/CodeGen/Passes.h"
21#include "llvm/CodeGen/WinEHFuncInfo.h"
22#include "llvm/IR/Dominators.h"
23#include "llvm/IR/Function.h"
24#include "llvm/IR/IRBuilder.h"
25#include "llvm/IR/Instructions.h"
26#include "llvm/IR/IntrinsicInst.h"
27#include "llvm/IR/Module.h"
28#include "llvm/IR/PatternMatch.h"
29#include "llvm/Pass.h"
30#include "llvm/Support/Debug.h"
31#include "llvm/Support/raw_ostream.h"
32#include "llvm/Transforms/Utils/BasicBlockUtils.h"
33#include "llvm/Transforms/Utils/Cloning.h"
34#include "llvm/Transforms/Utils/Local.h"
35
36using namespace llvm;
37using namespace llvm::PatternMatch;
38
39#define DEBUG_TYPE "winehstate"
40
41namespace {
42class WinEHStatePass : public FunctionPass {
43public:
44 static char ID; // Pass identification, replacement for typeid.
45
46 WinEHStatePass() : FunctionPass(ID) {}
47
48 bool runOnFunction(Function &Fn) override;
49
50 bool doInitialization(Module &M) override;
51
52 bool doFinalization(Module &M) override;
53
54 void getAnalysisUsage(AnalysisUsage &AU) const override;
55
56 const char *getPassName() const override {
57 return "Windows 32-bit x86 EH state insertion";
58 }
59
60private:
61 void emitExceptionRegistrationRecord(Function *F);
62
Reid Klecknerfe4d4912015-05-28 22:00:24 +000063 void linkExceptionRegistration(IRBuilder<> &Builder, Value *Handler);
64 void unlinkExceptionRegistration(IRBuilder<> &Builder);
65 void addCXXStateStores(Function &F, MachineModuleInfo &MMI);
66 void addCXXStateStoresToFunclet(Value *ParentRegNode, WinEHFuncInfo &FuncInfo,
67 Function &F, int BaseState);
68 void insertStateNumberStore(Value *ParentRegNode, Instruction *IP, int State);
Reid Kleckner0738a9c2015-05-05 17:44:16 +000069
Reid Kleckner2632f0d2015-05-20 23:08:04 +000070 Value *emitEHLSDA(IRBuilder<> &Builder, Function *F);
71
72 Function *generateLSDAInEAXThunk(Function *ParentFunc);
73
Reid Klecknerfe4d4912015-05-28 22:00:24 +000074 int escapeRegNode(Function &F);
75
Reid Kleckner0738a9c2015-05-05 17:44:16 +000076 // Module-level type getters.
Reid Klecknere6531a552015-05-29 22:57:46 +000077 Type *getEHLinkRegistrationType();
78 Type *getSEHRegistrationType();
79 Type *getCXXEHRegistrationType();
Reid Kleckner0738a9c2015-05-05 17:44:16 +000080
81 // Per-module data.
82 Module *TheModule = nullptr;
Reid Klecknere6531a552015-05-29 22:57:46 +000083 StructType *EHLinkRegistrationTy = nullptr;
84 StructType *CXXEHRegistrationTy = nullptr;
85 StructType *SEHRegistrationTy = nullptr;
Reid Klecknerb7403332015-06-08 22:43:32 +000086 Function *FrameRecover = nullptr;
87 Function *FrameAddress = nullptr;
88 Function *FrameEscape = nullptr;
Reid Kleckner0738a9c2015-05-05 17:44:16 +000089
90 // Per-function state
91 EHPersonality Personality = EHPersonality::Unknown;
92 Function *PersonalityFn = nullptr;
Reid Klecknerfe4d4912015-05-28 22:00:24 +000093
94 /// The stack allocation containing all EH data, including the link in the
95 /// fs:00 chain and the current state.
96 AllocaInst *RegNode = nullptr;
97
98 /// Struct type of RegNode. Used for GEPing.
99 Type *RegNodeTy = nullptr;
100
101 /// The index of the state field of RegNode.
102 int StateFieldIndex = ~0U;
103
104 /// The linked list node subobject inside of RegNode.
105 Value *Link = nullptr;
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000106};
107}
108
109FunctionPass *llvm::createX86WinEHStatePass() { return new WinEHStatePass(); }
110
111char WinEHStatePass::ID = 0;
112
113bool WinEHStatePass::doInitialization(Module &M) {
114 TheModule = &M;
Reid Klecknerb7403332015-06-08 22:43:32 +0000115 FrameEscape = Intrinsic::getDeclaration(TheModule, Intrinsic::frameescape);
116 FrameRecover = Intrinsic::getDeclaration(TheModule, Intrinsic::framerecover);
117 FrameAddress = Intrinsic::getDeclaration(TheModule, Intrinsic::frameaddress);
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000118 return false;
119}
120
121bool WinEHStatePass::doFinalization(Module &M) {
122 assert(TheModule == &M);
123 TheModule = nullptr;
Reid Klecknere6531a552015-05-29 22:57:46 +0000124 EHLinkRegistrationTy = nullptr;
125 CXXEHRegistrationTy = nullptr;
126 SEHRegistrationTy = nullptr;
Reid Klecknerb7403332015-06-08 22:43:32 +0000127 FrameEscape = nullptr;
128 FrameRecover = nullptr;
129 FrameAddress = nullptr;
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000130 return false;
131}
132
133void WinEHStatePass::getAnalysisUsage(AnalysisUsage &AU) const {
134 // This pass should only insert a stack allocation, memory accesses, and
135 // framerecovers.
136 AU.setPreservesCFG();
137}
138
139bool WinEHStatePass::runOnFunction(Function &F) {
Reid Kleckner2632f0d2015-05-20 23:08:04 +0000140 // If this is an outlined handler, don't do anything. We'll do state insertion
141 // for it in the parent.
142 StringRef WinEHParentName =
143 F.getFnAttribute("wineh-parent").getValueAsString();
144 if (WinEHParentName != F.getName() && !WinEHParentName.empty())
145 return false;
146
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000147 // Check the personality. Do nothing if this is not an MSVC personality.
148 LandingPadInst *LP = nullptr;
149 for (BasicBlock &BB : F) {
150 LP = BB.getLandingPadInst();
151 if (LP)
152 break;
153 }
154 if (!LP)
155 return false;
156 PersonalityFn =
157 dyn_cast<Function>(LP->getPersonalityFn()->stripPointerCasts());
158 if (!PersonalityFn)
159 return false;
160 Personality = classifyEHPersonality(PersonalityFn);
161 if (!isMSVCEHPersonality(Personality))
162 return false;
163
Reid Kleckner173a7252015-05-29 21:58:11 +0000164 // Disable frame pointer elimination in this function.
165 // FIXME: Do the nested handlers need to keep the parent ebp in ebp, or can we
166 // use an arbitrary register?
167 F.addFnAttr("no-frame-pointer-elim", "true");
168
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000169 emitExceptionRegistrationRecord(&F);
Reid Klecknerfe4d4912015-05-28 22:00:24 +0000170
171 auto *MMIPtr = getAnalysisIfAvailable<MachineModuleInfo>();
172 assert(MMIPtr && "MachineModuleInfo should always be available");
173 MachineModuleInfo &MMI = *MMIPtr;
174 if (Personality == EHPersonality::MSVC_CXX) {
175 addCXXStateStores(F, MMI);
176 }
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000177
178 // Reset per-function state.
179 PersonalityFn = nullptr;
180 Personality = EHPersonality::Unknown;
181 return true;
182}
183
184/// Get the common EH registration subobject:
Reid Kleckner2632f0d2015-05-20 23:08:04 +0000185/// typedef _EXCEPTION_DISPOSITION (*PEXCEPTION_ROUTINE)(
186/// _EXCEPTION_RECORD *, void *, _CONTEXT *, void *);
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000187/// struct EHRegistrationNode {
188/// EHRegistrationNode *Next;
Reid Kleckner2632f0d2015-05-20 23:08:04 +0000189/// PEXCEPTION_ROUTINE Handler;
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000190/// };
Reid Klecknere6531a552015-05-29 22:57:46 +0000191Type *WinEHStatePass::getEHLinkRegistrationType() {
192 if (EHLinkRegistrationTy)
193 return EHLinkRegistrationTy;
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000194 LLVMContext &Context = TheModule->getContext();
Reid Klecknere6531a552015-05-29 22:57:46 +0000195 EHLinkRegistrationTy = StructType::create(Context, "EHRegistrationNode");
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000196 Type *FieldTys[] = {
Reid Klecknere6531a552015-05-29 22:57:46 +0000197 EHLinkRegistrationTy->getPointerTo(0), // EHRegistrationNode *Next
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000198 Type::getInt8PtrTy(Context) // EXCEPTION_DISPOSITION (*Handler)(...)
199 };
Reid Klecknere6531a552015-05-29 22:57:46 +0000200 EHLinkRegistrationTy->setBody(FieldTys, false);
201 return EHLinkRegistrationTy;
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000202}
203
204/// The __CxxFrameHandler3 registration node:
205/// struct CXXExceptionRegistration {
206/// void *SavedESP;
207/// EHRegistrationNode SubRecord;
208/// int32_t TryLevel;
209/// };
Reid Klecknere6531a552015-05-29 22:57:46 +0000210Type *WinEHStatePass::getCXXEHRegistrationType() {
211 if (CXXEHRegistrationTy)
212 return CXXEHRegistrationTy;
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000213 LLVMContext &Context = TheModule->getContext();
214 Type *FieldTys[] = {
215 Type::getInt8PtrTy(Context), // void *SavedESP
Reid Klecknere6531a552015-05-29 22:57:46 +0000216 getEHLinkRegistrationType(), // EHRegistrationNode SubRecord
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000217 Type::getInt32Ty(Context) // int32_t TryLevel
218 };
Reid Klecknere6531a552015-05-29 22:57:46 +0000219 CXXEHRegistrationTy =
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000220 StructType::create(FieldTys, "CXXExceptionRegistration");
Reid Klecknere6531a552015-05-29 22:57:46 +0000221 return CXXEHRegistrationTy;
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000222}
223
Reid Klecknere6531a552015-05-29 22:57:46 +0000224/// The _except_handler3/4 registration node:
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000225/// struct EH4ExceptionRegistration {
226/// void *SavedESP;
227/// _EXCEPTION_POINTERS *ExceptionPointers;
228/// EHRegistrationNode SubRecord;
229/// int32_t EncodedScopeTable;
230/// int32_t TryLevel;
231/// };
Reid Klecknere6531a552015-05-29 22:57:46 +0000232Type *WinEHStatePass::getSEHRegistrationType() {
233 if (SEHRegistrationTy)
234 return SEHRegistrationTy;
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000235 LLVMContext &Context = TheModule->getContext();
236 Type *FieldTys[] = {
237 Type::getInt8PtrTy(Context), // void *SavedESP
238 Type::getInt8PtrTy(Context), // void *ExceptionPointers
Reid Klecknere6531a552015-05-29 22:57:46 +0000239 getEHLinkRegistrationType(), // EHRegistrationNode SubRecord
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000240 Type::getInt32Ty(Context), // int32_t EncodedScopeTable
241 Type::getInt32Ty(Context) // int32_t TryLevel
242 };
Reid Klecknere6531a552015-05-29 22:57:46 +0000243 SEHRegistrationTy = StructType::create(FieldTys, "SEHExceptionRegistration");
244 return SEHRegistrationTy;
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000245}
246
247// Emit an exception registration record. These are stack allocations with the
248// common subobject of two pointers: the previous registration record (the old
249// fs:00) and the personality function for the current frame. The data before
250// and after that is personality function specific.
251void WinEHStatePass::emitExceptionRegistrationRecord(Function *F) {
252 assert(Personality == EHPersonality::MSVC_CXX ||
253 Personality == EHPersonality::MSVC_X86SEH);
254
255 StringRef PersonalityName = PersonalityFn->getName();
256 IRBuilder<> Builder(&F->getEntryBlock(), F->getEntryBlock().begin());
257 Type *Int8PtrType = Builder.getInt8PtrTy();
Reid Klecknere6531a552015-05-29 22:57:46 +0000258 if (Personality == EHPersonality::MSVC_CXX) {
259 RegNodeTy = getCXXEHRegistrationType();
Reid Klecknerfe4d4912015-05-28 22:00:24 +0000260 RegNode = Builder.CreateAlloca(RegNodeTy);
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000261 // FIXME: We can skip this in -GS- mode, when we figure that out.
262 // SavedESP = llvm.stacksave()
263 Value *SP = Builder.CreateCall(
David Blaikieff6409d2015-05-18 22:13:54 +0000264 Intrinsic::getDeclaration(TheModule, Intrinsic::stacksave), {});
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000265 Builder.CreateStore(SP, Builder.CreateStructGEP(RegNodeTy, RegNode, 0));
266 // TryLevel = -1
Reid Klecknerfe4d4912015-05-28 22:00:24 +0000267 StateFieldIndex = 2;
268 insertStateNumberStore(RegNode, Builder.GetInsertPoint(), -1);
Reid Kleckner2632f0d2015-05-20 23:08:04 +0000269 // Handler = __ehhandler$F
270 Function *Trampoline = generateLSDAInEAXThunk(F);
Reid Klecknerfe4d4912015-05-28 22:00:24 +0000271 Link = Builder.CreateStructGEP(RegNodeTy, RegNode, 1);
272 linkExceptionRegistration(Builder, Trampoline);
Reid Klecknere6531a552015-05-29 22:57:46 +0000273 } else if (Personality == EHPersonality::MSVC_X86SEH) {
274 // If _except_handler4 is in use, some additional guard checks and prologue
275 // stuff is required.
276 bool UseStackGuard = (PersonalityName == "_except_handler4");
277 RegNodeTy = getSEHRegistrationType();
Reid Klecknerfe4d4912015-05-28 22:00:24 +0000278 RegNode = Builder.CreateAlloca(RegNodeTy);
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000279 // SavedESP = llvm.stacksave()
280 Value *SP = Builder.CreateCall(
David Blaikieff6409d2015-05-18 22:13:54 +0000281 Intrinsic::getDeclaration(TheModule, Intrinsic::stacksave), {});
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000282 Builder.CreateStore(SP, Builder.CreateStructGEP(RegNodeTy, RegNode, 0));
Reid Klecknere6531a552015-05-29 22:57:46 +0000283 // TryLevel = -2 / -1
Reid Klecknerfe4d4912015-05-28 22:00:24 +0000284 StateFieldIndex = 4;
Reid Klecknere6531a552015-05-29 22:57:46 +0000285 insertStateNumberStore(RegNode, Builder.GetInsertPoint(),
286 UseStackGuard ? -2 : -1);
Reid Kleckner2632f0d2015-05-20 23:08:04 +0000287 // ScopeTable = llvm.x86.seh.lsda(F)
288 Value *FI8 = Builder.CreateBitCast(F, Int8PtrType);
289 Value *LSDA = Builder.CreateCall(
290 Intrinsic::getDeclaration(TheModule, Intrinsic::x86_seh_lsda), FI8);
Reid Klecknere6531a552015-05-29 22:57:46 +0000291 Type *Int32Ty = Type::getInt32Ty(TheModule->getContext());
292 LSDA = Builder.CreatePtrToInt(LSDA, Int32Ty);
293 // If using _except_handler4, xor the address of the table with
294 // __security_cookie.
295 if (UseStackGuard) {
296 Value *Cookie =
297 TheModule->getOrInsertGlobal("__security_cookie", Int32Ty);
298 Value *Val = Builder.CreateLoad(Int32Ty, Cookie);
299 LSDA = Builder.CreateXor(LSDA, Val);
300 }
301 Builder.CreateStore(LSDA, Builder.CreateStructGEP(RegNodeTy, RegNode, 3));
Reid Klecknerfe4d4912015-05-28 22:00:24 +0000302 Link = Builder.CreateStructGEP(RegNodeTy, RegNode, 2);
303 linkExceptionRegistration(Builder, PersonalityFn);
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000304 } else {
305 llvm_unreachable("unexpected personality function");
306 }
307
Reid Klecknerfe4d4912015-05-28 22:00:24 +0000308 // Insert an unlink before all returns.
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000309 for (BasicBlock &BB : *F) {
310 TerminatorInst *T = BB.getTerminator();
311 if (!isa<ReturnInst>(T))
312 continue;
313 Builder.SetInsertPoint(T);
Reid Klecknerfe4d4912015-05-28 22:00:24 +0000314 unlinkExceptionRegistration(Builder);
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000315 }
316}
317
Reid Kleckner2632f0d2015-05-20 23:08:04 +0000318Value *WinEHStatePass::emitEHLSDA(IRBuilder<> &Builder, Function *F) {
319 Value *FI8 = Builder.CreateBitCast(F, Type::getInt8PtrTy(F->getContext()));
320 return Builder.CreateCall(
321 Intrinsic::getDeclaration(TheModule, Intrinsic::x86_seh_lsda), FI8);
322}
323
324/// Generate a thunk that puts the LSDA of ParentFunc in EAX and then calls
325/// PersonalityFn, forwarding the parameters passed to PEXCEPTION_ROUTINE:
326/// typedef _EXCEPTION_DISPOSITION (*PEXCEPTION_ROUTINE)(
327/// _EXCEPTION_RECORD *, void *, _CONTEXT *, void *);
328/// We essentially want this code:
329/// movl $lsda, %eax
330/// jmpl ___CxxFrameHandler3
331Function *WinEHStatePass::generateLSDAInEAXThunk(Function *ParentFunc) {
332 LLVMContext &Context = ParentFunc->getContext();
333 Type *Int32Ty = Type::getInt32Ty(Context);
334 Type *Int8PtrType = Type::getInt8PtrTy(Context);
335 Type *ArgTys[5] = {Int8PtrType, Int8PtrType, Int8PtrType, Int8PtrType,
336 Int8PtrType};
337 FunctionType *TrampolineTy =
338 FunctionType::get(Int32Ty, makeArrayRef(&ArgTys[0], 4),
339 /*isVarArg=*/false);
340 FunctionType *TargetFuncTy =
341 FunctionType::get(Int32Ty, makeArrayRef(&ArgTys[0], 5),
342 /*isVarArg=*/false);
343 Function *Trampoline = Function::Create(
344 TrampolineTy, GlobalValue::InternalLinkage,
345 Twine("__ehhandler$") + ParentFunc->getName(), TheModule);
346 BasicBlock *EntryBB = BasicBlock::Create(Context, "entry", Trampoline);
347 IRBuilder<> Builder(EntryBB);
348 Value *LSDA = emitEHLSDA(Builder, ParentFunc);
349 Value *CastPersonality =
350 Builder.CreateBitCast(PersonalityFn, TargetFuncTy->getPointerTo());
351 auto AI = Trampoline->arg_begin();
352 Value *Args[5] = {LSDA, AI++, AI++, AI++, AI++};
353 CallInst *Call = Builder.CreateCall(CastPersonality, Args);
354 // Can't use musttail due to prototype mismatch, but we can use tail.
355 Call->setTailCall(true);
356 // Set inreg so we pass it in EAX.
357 Call->addAttribute(1, Attribute::InReg);
358 Builder.CreateRet(Call);
359 return Trampoline;
360}
361
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000362void WinEHStatePass::linkExceptionRegistration(IRBuilder<> &Builder,
Reid Klecknerfe4d4912015-05-28 22:00:24 +0000363 Value *Handler) {
Reid Klecknere6531a552015-05-29 22:57:46 +0000364 Type *LinkTy = getEHLinkRegistrationType();
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000365 // Handler = Handler
366 Handler = Builder.CreateBitCast(Handler, Builder.getInt8PtrTy());
Reid Klecknerfe4d4912015-05-28 22:00:24 +0000367 Builder.CreateStore(Handler, Builder.CreateStructGEP(LinkTy, Link, 1));
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000368 // Next = [fs:00]
369 Constant *FSZero =
Reid Klecknerfe4d4912015-05-28 22:00:24 +0000370 Constant::getNullValue(LinkTy->getPointerTo()->getPointerTo(257));
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000371 Value *Next = Builder.CreateLoad(FSZero);
Reid Klecknerfe4d4912015-05-28 22:00:24 +0000372 Builder.CreateStore(Next, Builder.CreateStructGEP(LinkTy, Link, 0));
373 // [fs:00] = Link
374 Builder.CreateStore(Link, FSZero);
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000375}
376
Reid Klecknerfe4d4912015-05-28 22:00:24 +0000377void WinEHStatePass::unlinkExceptionRegistration(IRBuilder<> &Builder) {
378 // Clone Link into the current BB for better address mode folding.
379 if (auto *GEP = dyn_cast<GetElementPtrInst>(Link)) {
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000380 GEP = cast<GetElementPtrInst>(GEP->clone());
381 Builder.Insert(GEP);
Reid Klecknerfe4d4912015-05-28 22:00:24 +0000382 Link = GEP;
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000383 }
Reid Klecknere6531a552015-05-29 22:57:46 +0000384 Type *LinkTy = getEHLinkRegistrationType();
Reid Klecknerfe4d4912015-05-28 22:00:24 +0000385 // [fs:00] = Link->Next
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000386 Value *Next =
Reid Klecknerfe4d4912015-05-28 22:00:24 +0000387 Builder.CreateLoad(Builder.CreateStructGEP(LinkTy, Link, 0));
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000388 Constant *FSZero =
Reid Klecknerfe4d4912015-05-28 22:00:24 +0000389 Constant::getNullValue(LinkTy->getPointerTo()->getPointerTo(257));
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000390 Builder.CreateStore(Next, FSZero);
391}
Reid Klecknerfe4d4912015-05-28 22:00:24 +0000392
393void WinEHStatePass::addCXXStateStores(Function &F, MachineModuleInfo &MMI) {
394 WinEHFuncInfo &FuncInfo = MMI.getWinEHFuncInfo(&F);
395 calculateWinCXXEHStateNumbers(&F, FuncInfo);
396
397 // The base state for the parent is -1.
398 addCXXStateStoresToFunclet(RegNode, FuncInfo, F, -1);
399
400 // Set up RegNodeEscapeIndex
401 int RegNodeEscapeIndex = escapeRegNode(F);
402
403 // Only insert stores in catch handlers.
Reid Klecknerfe4d4912015-05-28 22:00:24 +0000404 Constant *FI8 =
405 ConstantExpr::getBitCast(&F, Type::getInt8PtrTy(TheModule->getContext()));
406 for (auto P : FuncInfo.HandlerBaseState) {
407 Function *Handler = const_cast<Function *>(P.first);
408 int BaseState = P.second;
409 IRBuilder<> Builder(&Handler->getEntryBlock(),
410 Handler->getEntryBlock().begin());
411 // FIXME: Find and reuse such a call if present.
412 Value *ParentFP = Builder.CreateCall(FrameAddress, {Builder.getInt32(1)});
413 Value *RecoveredRegNode = Builder.CreateCall(
414 FrameRecover, {FI8, ParentFP, Builder.getInt32(RegNodeEscapeIndex)});
415 RecoveredRegNode =
416 Builder.CreateBitCast(RecoveredRegNode, RegNodeTy->getPointerTo(0));
417 addCXXStateStoresToFunclet(RecoveredRegNode, FuncInfo, *Handler, BaseState);
418 }
419}
420
421/// Escape RegNode so that we can access it from child handlers. Find the call
422/// to frameescape, if any, in the entry block and append RegNode to the list
423/// of arguments.
424int WinEHStatePass::escapeRegNode(Function &F) {
425 // Find the call to frameescape and extract its arguments.
426 IntrinsicInst *EscapeCall = nullptr;
427 for (Instruction &I : F.getEntryBlock()) {
428 IntrinsicInst *II = dyn_cast<IntrinsicInst>(&I);
429 if (II && II->getIntrinsicID() == Intrinsic::frameescape) {
430 EscapeCall = II;
431 break;
432 }
433 }
434 SmallVector<Value *, 8> Args;
435 if (EscapeCall) {
436 auto Ops = EscapeCall->arg_operands();
437 Args.append(Ops.begin(), Ops.end());
438 }
439 Args.push_back(RegNode);
440
441 // Replace the call (if it exists) with new one. Otherwise, insert at the end
442 // of the entry block.
443 IRBuilder<> Builder(&F.getEntryBlock(),
444 EscapeCall ? EscapeCall : F.getEntryBlock().end());
Reid Klecknerb7403332015-06-08 22:43:32 +0000445 Builder.CreateCall(FrameEscape, Args);
Reid Klecknerfe4d4912015-05-28 22:00:24 +0000446 if (EscapeCall)
447 EscapeCall->eraseFromParent();
448 return Args.size() - 1;
449}
450
451void WinEHStatePass::addCXXStateStoresToFunclet(Value *ParentRegNode,
452 WinEHFuncInfo &FuncInfo,
453 Function &F, int BaseState) {
454 // Iterate all the instructions and emit state number stores.
455 for (BasicBlock &BB : F) {
456 for (Instruction &I : BB) {
457 if (auto *CI = dyn_cast<CallInst>(&I)) {
458 // Possibly throwing call instructions have no actions to take after
459 // an unwind. Ensure they are in the -1 state.
460 if (CI->doesNotThrow())
461 continue;
462 insertStateNumberStore(ParentRegNode, CI, BaseState);
463 } else if (auto *II = dyn_cast<InvokeInst>(&I)) {
464 // Look up the state number of the landingpad this unwinds to.
465 LandingPadInst *LPI = II->getUnwindDest()->getLandingPadInst();
466 // FIXME: Why does this assertion fail?
467 //assert(FuncInfo.LandingPadStateMap.count(LPI) && "LP has no state!");
468 int State = FuncInfo.LandingPadStateMap[LPI];
469 insertStateNumberStore(ParentRegNode, II, State);
470 }
471 }
472 }
473}
474
475void WinEHStatePass::insertStateNumberStore(Value *ParentRegNode,
476 Instruction *IP, int State) {
477 IRBuilder<> Builder(IP);
478 Value *StateField =
479 Builder.CreateStructGEP(RegNodeTy, ParentRegNode, StateFieldIndex);
480 Builder.CreateStore(Builder.getInt32(State), StateField);
481}