blob: 0472a85f50da2ebd0b40c39dff2824d0ba53e8c0 [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"
David Majnemer7e5937b2016-02-17 18:37:11 +000018#include "llvm/ADT/PostOrderIterator.h"
19#include "llvm/Analysis/CFG.h"
David Majnemer70497c62015-12-02 23:06:39 +000020#include "llvm/Analysis/EHPersonalities.h"
Reid Klecknerfe4d4912015-05-28 22:00:24 +000021#include "llvm/CodeGen/MachineModuleInfo.h"
Reid Kleckner0738a9c2015-05-05 17:44:16 +000022#include "llvm/CodeGen/WinEHFuncInfo.h"
David Majnemer7e5937b2016-02-17 18:37:11 +000023#include "llvm/IR/CallSite.h"
24#include "llvm/IR/Function.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000025#include "llvm/IR/IRBuilder.h"
Reid Kleckner0738a9c2015-05-05 17:44:16 +000026#include "llvm/IR/Instructions.h"
27#include "llvm/IR/IntrinsicInst.h"
28#include "llvm/IR/Module.h"
Reid Kleckner0738a9c2015-05-05 17:44:16 +000029#include "llvm/Pass.h"
David Majnemer7e5937b2016-02-17 18:37:11 +000030#include "llvm/Support/Debug.h"
31#include <deque>
Reid Kleckner0738a9c2015-05-05 17:44:16 +000032
33using namespace llvm;
Reid Kleckner0738a9c2015-05-05 17:44:16 +000034
35#define DEBUG_TYPE "winehstate"
36
David Majnemerefb41742016-02-01 04:28:59 +000037namespace llvm {
38void initializeWinEHStatePassPass(PassRegistry &);
39}
David Majnemer0ad363e2015-08-18 19:07:12 +000040
Reid Kleckner0738a9c2015-05-05 17:44:16 +000041namespace {
David Majnemer7e5937b2016-02-17 18:37:11 +000042const int OverdefinedState = INT_MIN;
43
Reid Kleckner0738a9c2015-05-05 17:44:16 +000044class WinEHStatePass : public FunctionPass {
45public:
46 static char ID; // Pass identification, replacement for typeid.
47
David Majnemer0ad363e2015-08-18 19:07:12 +000048 WinEHStatePass() : FunctionPass(ID) {
49 initializeWinEHStatePassPass(*PassRegistry::getPassRegistry());
50 }
Reid Kleckner0738a9c2015-05-05 17:44:16 +000051
52 bool runOnFunction(Function &Fn) override;
53
54 bool doInitialization(Module &M) override;
55
56 bool doFinalization(Module &M) override;
57
58 void getAnalysisUsage(AnalysisUsage &AU) const override;
59
Mehdi Amini117296c2016-10-01 02:56:57 +000060 StringRef getPassName() const override {
Reid Kleckner0738a9c2015-05-05 17:44:16 +000061 return "Windows 32-bit x86 EH state insertion";
62 }
63
64private:
65 void emitExceptionRegistrationRecord(Function *F);
66
Reid Kleckner2bc93ca2015-06-10 01:02:30 +000067 void linkExceptionRegistration(IRBuilder<> &Builder, Function *Handler);
Reid Klecknerfe4d4912015-05-28 22:00:24 +000068 void unlinkExceptionRegistration(IRBuilder<> &Builder);
Reid Klecknerc20276d2015-11-17 21:10:25 +000069 void addStateStores(Function &F, WinEHFuncInfo &FuncInfo);
David Majnemerefb41742016-02-01 04:28:59 +000070 void insertStateNumberStore(Instruction *IP, int State);
Reid Kleckner0738a9c2015-05-05 17:44:16 +000071
Reid Kleckner2632f0d2015-05-20 23:08:04 +000072 Value *emitEHLSDA(IRBuilder<> &Builder, Function *F);
73
74 Function *generateLSDAInEAXThunk(Function *ParentFunc);
75
David Majnemere60ee3b2016-02-29 19:16:03 +000076 bool isStateStoreNeeded(EHPersonality Personality, CallSite CS);
77 void rewriteSetJmpCallSite(IRBuilder<> &Builder, Function &F, CallSite CS,
78 Value *State);
79 int getBaseStateForBB(DenseMap<BasicBlock *, ColorVector> &BlockColors,
80 WinEHFuncInfo &FuncInfo, BasicBlock *BB);
81 int getStateForCallSite(DenseMap<BasicBlock *, ColorVector> &BlockColors,
82 WinEHFuncInfo &FuncInfo, CallSite CS);
83
Reid Kleckner0738a9c2015-05-05 17:44:16 +000084 // Module-level type getters.
Reid Klecknere6531a552015-05-29 22:57:46 +000085 Type *getEHLinkRegistrationType();
86 Type *getSEHRegistrationType();
87 Type *getCXXEHRegistrationType();
Reid Kleckner0738a9c2015-05-05 17:44:16 +000088
89 // Per-module data.
90 Module *TheModule = nullptr;
Reid Klecknere6531a552015-05-29 22:57:46 +000091 StructType *EHLinkRegistrationTy = nullptr;
92 StructType *CXXEHRegistrationTy = nullptr;
93 StructType *SEHRegistrationTy = nullptr;
David Majnemere60ee3b2016-02-29 19:16:03 +000094 Constant *SetJmp3 = nullptr;
95 Constant *CxxLongjmpUnwind = nullptr;
Reid Kleckner0738a9c2015-05-05 17:44:16 +000096
97 // Per-function state
98 EHPersonality Personality = EHPersonality::Unknown;
99 Function *PersonalityFn = nullptr;
David Majnemer7e5937b2016-02-17 18:37:11 +0000100 bool UseStackGuard = false;
101 int ParentBaseState;
David Majnemere60ee3b2016-02-29 19:16:03 +0000102 Constant *SehLongjmpUnwind = nullptr;
103 Constant *Cookie = nullptr;
Reid Klecknerfe4d4912015-05-28 22:00:24 +0000104
105 /// The stack allocation containing all EH data, including the link in the
106 /// fs:00 chain and the current state.
107 AllocaInst *RegNode = nullptr;
108
Etienne Bergeronf6be62f2016-06-21 15:58:55 +0000109 // The allocation containing the EH security guard.
110 AllocaInst *EHGuardNode = nullptr;
111
Reid Klecknerfe4d4912015-05-28 22:00:24 +0000112 /// The index of the state field of RegNode.
113 int StateFieldIndex = ~0U;
114
115 /// The linked list node subobject inside of RegNode.
116 Value *Link = nullptr;
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000117};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000118}
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000119
120FunctionPass *llvm::createX86WinEHStatePass() { return new WinEHStatePass(); }
121
122char WinEHStatePass::ID = 0;
123
David Majnemer0ad363e2015-08-18 19:07:12 +0000124INITIALIZE_PASS(WinEHStatePass, "x86-winehstate",
125 "Insert stores for EH state numbers", false, false)
126
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000127bool WinEHStatePass::doInitialization(Module &M) {
128 TheModule = &M;
129 return false;
130}
131
132bool WinEHStatePass::doFinalization(Module &M) {
133 assert(TheModule == &M);
134 TheModule = nullptr;
Reid Klecknere6531a552015-05-29 22:57:46 +0000135 EHLinkRegistrationTy = nullptr;
136 CXXEHRegistrationTy = nullptr;
137 SEHRegistrationTy = nullptr;
David Majnemere60ee3b2016-02-29 19:16:03 +0000138 SetJmp3 = nullptr;
139 CxxLongjmpUnwind = nullptr;
140 SehLongjmpUnwind = nullptr;
141 Cookie = nullptr;
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000142 return false;
143}
144
145void WinEHStatePass::getAnalysisUsage(AnalysisUsage &AU) const {
146 // This pass should only insert a stack allocation, memory accesses, and
Reid Kleckner60381792015-07-07 22:25:32 +0000147 // localrecovers.
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000148 AU.setPreservesCFG();
149}
150
151bool WinEHStatePass::runOnFunction(Function &F) {
Joseph Tremoulet2afea542015-10-06 20:28:16 +0000152 // Check the personality. Do nothing if this personality doesn't use funclets.
David Majnemer7fddecc2015-06-17 20:52:32 +0000153 if (!F.hasPersonalityFn())
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000154 return false;
155 PersonalityFn =
David Majnemer7fddecc2015-06-17 20:52:32 +0000156 dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts());
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000157 if (!PersonalityFn)
158 return false;
159 Personality = classifyEHPersonality(PersonalityFn);
Joseph Tremoulet2afea542015-10-06 20:28:16 +0000160 if (!isFuncletEHPersonality(Personality))
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000161 return false;
162
Reid Kleckner84ebff42015-09-16 17:19:44 +0000163 // Skip this function if there are no EH pads and we aren't using IR-level
164 // outlining.
David Majnemerbfa5b982015-10-10 00:04:29 +0000165 bool HasPads = false;
166 for (BasicBlock &BB : F) {
167 if (BB.isEHPad()) {
168 HasPads = true;
169 break;
Reid Kleckner84ebff42015-09-16 17:19:44 +0000170 }
Reid Kleckner84ebff42015-09-16 17:19:44 +0000171 }
David Majnemerbfa5b982015-10-10 00:04:29 +0000172 if (!HasPads)
173 return false;
Reid Kleckner84ebff42015-09-16 17:19:44 +0000174
David Majnemere60ee3b2016-02-29 19:16:03 +0000175 Type *Int8PtrType = Type::getInt8PtrTy(TheModule->getContext());
176 SetJmp3 = TheModule->getOrInsertFunction(
177 "_setjmp3", FunctionType::get(
178 Type::getInt32Ty(TheModule->getContext()),
179 {Int8PtrType, Type::getInt32Ty(TheModule->getContext())},
180 /*isVarArg=*/true));
David Majnemer862c5ba32016-02-20 07:34:21 +0000181
Reid Kleckner173a7252015-05-29 21:58:11 +0000182 // Disable frame pointer elimination in this function.
183 // FIXME: Do the nested handlers need to keep the parent ebp in ebp, or can we
184 // use an arbitrary register?
185 F.addFnAttr("no-frame-pointer-elim", "true");
186
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000187 emitExceptionRegistrationRecord(&F);
Reid Klecknerfe4d4912015-05-28 22:00:24 +0000188
Reid Klecknerc20276d2015-11-17 21:10:25 +0000189 // The state numbers calculated here in IR must agree with what we calculate
190 // later on for the MachineFunction. In particular, if an IR pass deletes an
191 // unreachable EH pad after this point before machine CFG construction, we
192 // will be in trouble. If this assumption is ever broken, we should turn the
193 // numbers into an immutable analysis pass.
194 WinEHFuncInfo FuncInfo;
195 addStateStores(F, FuncInfo);
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000196
197 // Reset per-function state.
198 PersonalityFn = nullptr;
199 Personality = EHPersonality::Unknown;
David Majnemer7e5937b2016-02-17 18:37:11 +0000200 UseStackGuard = false;
Etienne Bergeronf6be62f2016-06-21 15:58:55 +0000201 RegNode = nullptr;
202 EHGuardNode = nullptr;
203
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000204 return true;
205}
206
207/// Get the common EH registration subobject:
Reid Kleckner2632f0d2015-05-20 23:08:04 +0000208/// typedef _EXCEPTION_DISPOSITION (*PEXCEPTION_ROUTINE)(
209/// _EXCEPTION_RECORD *, void *, _CONTEXT *, void *);
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000210/// struct EHRegistrationNode {
211/// EHRegistrationNode *Next;
Reid Kleckner2632f0d2015-05-20 23:08:04 +0000212/// PEXCEPTION_ROUTINE Handler;
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000213/// };
Reid Klecknere6531a552015-05-29 22:57:46 +0000214Type *WinEHStatePass::getEHLinkRegistrationType() {
215 if (EHLinkRegistrationTy)
216 return EHLinkRegistrationTy;
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000217 LLVMContext &Context = TheModule->getContext();
Reid Klecknere6531a552015-05-29 22:57:46 +0000218 EHLinkRegistrationTy = StructType::create(Context, "EHRegistrationNode");
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000219 Type *FieldTys[] = {
Reid Klecknere6531a552015-05-29 22:57:46 +0000220 EHLinkRegistrationTy->getPointerTo(0), // EHRegistrationNode *Next
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000221 Type::getInt8PtrTy(Context) // EXCEPTION_DISPOSITION (*Handler)(...)
222 };
Reid Klecknere6531a552015-05-29 22:57:46 +0000223 EHLinkRegistrationTy->setBody(FieldTys, false);
224 return EHLinkRegistrationTy;
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000225}
226
227/// The __CxxFrameHandler3 registration node:
228/// struct CXXExceptionRegistration {
229/// void *SavedESP;
230/// EHRegistrationNode SubRecord;
231/// int32_t TryLevel;
232/// };
Reid Klecknere6531a552015-05-29 22:57:46 +0000233Type *WinEHStatePass::getCXXEHRegistrationType() {
234 if (CXXEHRegistrationTy)
235 return CXXEHRegistrationTy;
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000236 LLVMContext &Context = TheModule->getContext();
237 Type *FieldTys[] = {
238 Type::getInt8PtrTy(Context), // void *SavedESP
Reid Klecknere6531a552015-05-29 22:57:46 +0000239 getEHLinkRegistrationType(), // EHRegistrationNode SubRecord
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000240 Type::getInt32Ty(Context) // int32_t TryLevel
241 };
Reid Klecknere6531a552015-05-29 22:57:46 +0000242 CXXEHRegistrationTy =
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000243 StructType::create(FieldTys, "CXXExceptionRegistration");
Reid Klecknere6531a552015-05-29 22:57:46 +0000244 return CXXEHRegistrationTy;
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000245}
246
Reid Klecknere6531a552015-05-29 22:57:46 +0000247/// The _except_handler3/4 registration node:
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000248/// struct EH4ExceptionRegistration {
249/// void *SavedESP;
250/// _EXCEPTION_POINTERS *ExceptionPointers;
251/// EHRegistrationNode SubRecord;
252/// int32_t EncodedScopeTable;
253/// int32_t TryLevel;
254/// };
Reid Klecknere6531a552015-05-29 22:57:46 +0000255Type *WinEHStatePass::getSEHRegistrationType() {
256 if (SEHRegistrationTy)
257 return SEHRegistrationTy;
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000258 LLVMContext &Context = TheModule->getContext();
259 Type *FieldTys[] = {
260 Type::getInt8PtrTy(Context), // void *SavedESP
261 Type::getInt8PtrTy(Context), // void *ExceptionPointers
Reid Klecknere6531a552015-05-29 22:57:46 +0000262 getEHLinkRegistrationType(), // EHRegistrationNode SubRecord
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000263 Type::getInt32Ty(Context), // int32_t EncodedScopeTable
264 Type::getInt32Ty(Context) // int32_t TryLevel
265 };
Reid Klecknere6531a552015-05-29 22:57:46 +0000266 SEHRegistrationTy = StructType::create(FieldTys, "SEHExceptionRegistration");
267 return SEHRegistrationTy;
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000268}
269
270// Emit an exception registration record. These are stack allocations with the
271// common subobject of two pointers: the previous registration record (the old
272// fs:00) and the personality function for the current frame. The data before
273// and after that is personality function specific.
274void WinEHStatePass::emitExceptionRegistrationRecord(Function *F) {
275 assert(Personality == EHPersonality::MSVC_CXX ||
276 Personality == EHPersonality::MSVC_X86SEH);
277
David Majnemerefb41742016-02-01 04:28:59 +0000278 // Struct type of RegNode. Used for GEPing.
279 Type *RegNodeTy;
280
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000281 IRBuilder<> Builder(&F->getEntryBlock(), F->getEntryBlock().begin());
282 Type *Int8PtrType = Builder.getInt8PtrTy();
Etienne Bergeronf6be62f2016-06-21 15:58:55 +0000283 Type *Int32Ty = Builder.getInt32Ty();
284 Type *VoidTy = Builder.getVoidTy();
285
Reid Klecknere6531a552015-05-29 22:57:46 +0000286 if (Personality == EHPersonality::MSVC_CXX) {
287 RegNodeTy = getCXXEHRegistrationType();
Reid Klecknerfe4d4912015-05-28 22:00:24 +0000288 RegNode = Builder.CreateAlloca(RegNodeTy);
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000289 // SavedESP = llvm.stacksave()
290 Value *SP = Builder.CreateCall(
David Blaikieff6409d2015-05-18 22:13:54 +0000291 Intrinsic::getDeclaration(TheModule, Intrinsic::stacksave), {});
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000292 Builder.CreateStore(SP, Builder.CreateStructGEP(RegNodeTy, RegNode, 0));
293 // TryLevel = -1
Reid Klecknerfe4d4912015-05-28 22:00:24 +0000294 StateFieldIndex = 2;
David Majnemer7e5937b2016-02-17 18:37:11 +0000295 ParentBaseState = -1;
296 insertStateNumberStore(&*Builder.GetInsertPoint(), ParentBaseState);
Reid Kleckner2632f0d2015-05-20 23:08:04 +0000297 // Handler = __ehhandler$F
298 Function *Trampoline = generateLSDAInEAXThunk(F);
Reid Klecknerfe4d4912015-05-28 22:00:24 +0000299 Link = Builder.CreateStructGEP(RegNodeTy, RegNode, 1);
300 linkExceptionRegistration(Builder, Trampoline);
David Majnemere60ee3b2016-02-29 19:16:03 +0000301
302 CxxLongjmpUnwind = TheModule->getOrInsertFunction(
303 "__CxxLongjmpUnwind",
Etienne Bergeronf6be62f2016-06-21 15:58:55 +0000304 FunctionType::get(VoidTy, Int8PtrType, /*isVarArg=*/false));
David Majnemere60ee3b2016-02-29 19:16:03 +0000305 cast<Function>(CxxLongjmpUnwind->stripPointerCasts())
306 ->setCallingConv(CallingConv::X86_StdCall);
Reid Klecknere6531a552015-05-29 22:57:46 +0000307 } else if (Personality == EHPersonality::MSVC_X86SEH) {
308 // If _except_handler4 is in use, some additional guard checks and prologue
309 // stuff is required.
Etienne Bergeronf6be62f2016-06-21 15:58:55 +0000310 StringRef PersonalityName = PersonalityFn->getName();
311 UseStackGuard = (PersonalityName == "_except_handler4");
312
313 // Allocate local structures.
Reid Klecknere6531a552015-05-29 22:57:46 +0000314 RegNodeTy = getSEHRegistrationType();
Reid Klecknerfe4d4912015-05-28 22:00:24 +0000315 RegNode = Builder.CreateAlloca(RegNodeTy);
Etienne Bergeronf6be62f2016-06-21 15:58:55 +0000316 if (UseStackGuard)
317 EHGuardNode = Builder.CreateAlloca(Int32Ty);
318
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000319 // SavedESP = llvm.stacksave()
320 Value *SP = Builder.CreateCall(
David Blaikieff6409d2015-05-18 22:13:54 +0000321 Intrinsic::getDeclaration(TheModule, Intrinsic::stacksave), {});
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000322 Builder.CreateStore(SP, Builder.CreateStructGEP(RegNodeTy, RegNode, 0));
Reid Klecknere6531a552015-05-29 22:57:46 +0000323 // TryLevel = -2 / -1
Reid Klecknerfe4d4912015-05-28 22:00:24 +0000324 StateFieldIndex = 4;
David Majnemer7e5937b2016-02-17 18:37:11 +0000325 ParentBaseState = UseStackGuard ? -2 : -1;
326 insertStateNumberStore(&*Builder.GetInsertPoint(), ParentBaseState);
Reid Kleckner2632f0d2015-05-20 23:08:04 +0000327 // ScopeTable = llvm.x86.seh.lsda(F)
David Majnemere60ee3b2016-02-29 19:16:03 +0000328 Value *LSDA = emitEHLSDA(Builder, F);
Reid Klecknere6531a552015-05-29 22:57:46 +0000329 LSDA = Builder.CreatePtrToInt(LSDA, Int32Ty);
330 // If using _except_handler4, xor the address of the table with
331 // __security_cookie.
332 if (UseStackGuard) {
David Majnemere60ee3b2016-02-29 19:16:03 +0000333 Cookie = TheModule->getOrInsertGlobal("__security_cookie", Int32Ty);
Etienne Bergeronf6be62f2016-06-21 15:58:55 +0000334 Value *Val = Builder.CreateLoad(Int32Ty, Cookie, "cookie");
Reid Klecknere6531a552015-05-29 22:57:46 +0000335 LSDA = Builder.CreateXor(LSDA, Val);
336 }
337 Builder.CreateStore(LSDA, Builder.CreateStructGEP(RegNodeTy, RegNode, 3));
Etienne Bergeronf6be62f2016-06-21 15:58:55 +0000338
339 // If using _except_handler4, the EHGuard contains: FramePtr xor Cookie.
340 if (UseStackGuard) {
341 Value *Val = Builder.CreateLoad(Int32Ty, Cookie);
342 Value *FrameAddr = Builder.CreateCall(
343 Intrinsic::getDeclaration(TheModule, Intrinsic::frameaddress),
344 Builder.getInt32(0), "frameaddr");
345 Value *FrameAddrI32 = Builder.CreatePtrToInt(FrameAddr, Int32Ty);
346 FrameAddrI32 = Builder.CreateXor(FrameAddrI32, Val);
347 Builder.CreateStore(FrameAddrI32, EHGuardNode);
348 }
349
350 // Register the exception handler.
Reid Klecknerfe4d4912015-05-28 22:00:24 +0000351 Link = Builder.CreateStructGEP(RegNodeTy, RegNode, 2);
352 linkExceptionRegistration(Builder, PersonalityFn);
David Majnemere60ee3b2016-02-29 19:16:03 +0000353
354 SehLongjmpUnwind = TheModule->getOrInsertFunction(
355 UseStackGuard ? "_seh_longjmp_unwind4" : "_seh_longjmp_unwind",
356 FunctionType::get(Type::getVoidTy(TheModule->getContext()), Int8PtrType,
357 /*isVarArg=*/false));
358 cast<Function>(SehLongjmpUnwind->stripPointerCasts())
359 ->setCallingConv(CallingConv::X86_StdCall);
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000360 } else {
361 llvm_unreachable("unexpected personality function");
362 }
363
Reid Klecknerfe4d4912015-05-28 22:00:24 +0000364 // Insert an unlink before all returns.
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000365 for (BasicBlock &BB : *F) {
366 TerminatorInst *T = BB.getTerminator();
367 if (!isa<ReturnInst>(T))
368 continue;
369 Builder.SetInsertPoint(T);
Reid Klecknerfe4d4912015-05-28 22:00:24 +0000370 unlinkExceptionRegistration(Builder);
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000371 }
372}
373
Reid Kleckner2632f0d2015-05-20 23:08:04 +0000374Value *WinEHStatePass::emitEHLSDA(IRBuilder<> &Builder, Function *F) {
375 Value *FI8 = Builder.CreateBitCast(F, Type::getInt8PtrTy(F->getContext()));
376 return Builder.CreateCall(
377 Intrinsic::getDeclaration(TheModule, Intrinsic::x86_seh_lsda), FI8);
378}
379
380/// Generate a thunk that puts the LSDA of ParentFunc in EAX and then calls
381/// PersonalityFn, forwarding the parameters passed to PEXCEPTION_ROUTINE:
382/// typedef _EXCEPTION_DISPOSITION (*PEXCEPTION_ROUTINE)(
383/// _EXCEPTION_RECORD *, void *, _CONTEXT *, void *);
384/// We essentially want this code:
385/// movl $lsda, %eax
386/// jmpl ___CxxFrameHandler3
387Function *WinEHStatePass::generateLSDAInEAXThunk(Function *ParentFunc) {
388 LLVMContext &Context = ParentFunc->getContext();
389 Type *Int32Ty = Type::getInt32Ty(Context);
390 Type *Int8PtrType = Type::getInt8PtrTy(Context);
391 Type *ArgTys[5] = {Int8PtrType, Int8PtrType, Int8PtrType, Int8PtrType,
392 Int8PtrType};
393 FunctionType *TrampolineTy =
394 FunctionType::get(Int32Ty, makeArrayRef(&ArgTys[0], 4),
395 /*isVarArg=*/false);
396 FunctionType *TargetFuncTy =
397 FunctionType::get(Int32Ty, makeArrayRef(&ArgTys[0], 5),
398 /*isVarArg=*/false);
Reid Kleckner5f4dd922015-07-13 17:55:14 +0000399 Function *Trampoline =
400 Function::Create(TrampolineTy, GlobalValue::InternalLinkage,
Peter Collingbourne6f0ecca2017-05-16 00:39:01 +0000401 Twine("__ehhandler$") + GlobalValue::dropLLVMManglingEscape(
Reid Kleckner5f4dd922015-07-13 17:55:14 +0000402 ParentFunc->getName()),
403 TheModule);
Dave Leef9b72322017-10-20 17:04:43 +0000404 if (auto *C = ParentFunc->getComdat())
405 Trampoline->setComdat(C);
Reid Kleckner2632f0d2015-05-20 23:08:04 +0000406 BasicBlock *EntryBB = BasicBlock::Create(Context, "entry", Trampoline);
407 IRBuilder<> Builder(EntryBB);
408 Value *LSDA = emitEHLSDA(Builder, ParentFunc);
409 Value *CastPersonality =
410 Builder.CreateBitCast(PersonalityFn, TargetFuncTy->getPointerTo());
411 auto AI = Trampoline->arg_begin();
Duncan P. N. Exon Smithd77de642015-10-19 21:48:29 +0000412 Value *Args[5] = {LSDA, &*AI++, &*AI++, &*AI++, &*AI++};
Reid Kleckner2632f0d2015-05-20 23:08:04 +0000413 CallInst *Call = Builder.CreateCall(CastPersonality, Args);
414 // Can't use musttail due to prototype mismatch, but we can use tail.
415 Call->setTailCall(true);
416 // Set inreg so we pass it in EAX.
Reid Klecknera0b45f42017-05-03 18:17:31 +0000417 Call->addParamAttr(0, Attribute::InReg);
Reid Kleckner2632f0d2015-05-20 23:08:04 +0000418 Builder.CreateRet(Call);
419 return Trampoline;
420}
421
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000422void WinEHStatePass::linkExceptionRegistration(IRBuilder<> &Builder,
Reid Kleckner2bc93ca2015-06-10 01:02:30 +0000423 Function *Handler) {
424 // Emit the .safeseh directive for this function.
425 Handler->addFnAttr("safeseh");
426
Reid Klecknere6531a552015-05-29 22:57:46 +0000427 Type *LinkTy = getEHLinkRegistrationType();
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000428 // Handler = Handler
Reid Kleckner2bc93ca2015-06-10 01:02:30 +0000429 Value *HandlerI8 = Builder.CreateBitCast(Handler, Builder.getInt8PtrTy());
430 Builder.CreateStore(HandlerI8, Builder.CreateStructGEP(LinkTy, Link, 1));
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000431 // Next = [fs:00]
432 Constant *FSZero =
Reid Klecknerfe4d4912015-05-28 22:00:24 +0000433 Constant::getNullValue(LinkTy->getPointerTo()->getPointerTo(257));
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000434 Value *Next = Builder.CreateLoad(FSZero);
Reid Klecknerfe4d4912015-05-28 22:00:24 +0000435 Builder.CreateStore(Next, Builder.CreateStructGEP(LinkTy, Link, 0));
436 // [fs:00] = Link
437 Builder.CreateStore(Link, FSZero);
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000438}
439
Reid Klecknerfe4d4912015-05-28 22:00:24 +0000440void WinEHStatePass::unlinkExceptionRegistration(IRBuilder<> &Builder) {
441 // Clone Link into the current BB for better address mode folding.
442 if (auto *GEP = dyn_cast<GetElementPtrInst>(Link)) {
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000443 GEP = cast<GetElementPtrInst>(GEP->clone());
444 Builder.Insert(GEP);
Reid Klecknerfe4d4912015-05-28 22:00:24 +0000445 Link = GEP;
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000446 }
Reid Klecknere6531a552015-05-29 22:57:46 +0000447 Type *LinkTy = getEHLinkRegistrationType();
Reid Klecknerfe4d4912015-05-28 22:00:24 +0000448 // [fs:00] = Link->Next
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000449 Value *Next =
Reid Klecknerfe4d4912015-05-28 22:00:24 +0000450 Builder.CreateLoad(Builder.CreateStructGEP(LinkTy, Link, 0));
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000451 Constant *FSZero =
Reid Klecknerfe4d4912015-05-28 22:00:24 +0000452 Constant::getNullValue(LinkTy->getPointerTo()->getPointerTo(257));
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000453 Builder.CreateStore(Next, FSZero);
454}
Reid Klecknerfe4d4912015-05-28 22:00:24 +0000455
David Majnemere60ee3b2016-02-29 19:16:03 +0000456// Calls to setjmp(p) are lowered to _setjmp3(p, 0) by the frontend.
457// The idea behind _setjmp3 is that it takes an optional number of personality
458// specific parameters to indicate how to restore the personality-specific frame
459// state when longjmp is initiated. Typically, the current TryLevel is saved.
460void WinEHStatePass::rewriteSetJmpCallSite(IRBuilder<> &Builder, Function &F,
461 CallSite CS, Value *State) {
462 // Don't rewrite calls with a weird number of arguments.
463 if (CS.getNumArgOperands() != 2)
464 return;
465
466 Instruction *Inst = CS.getInstruction();
467
468 SmallVector<OperandBundleDef, 1> OpBundles;
469 CS.getOperandBundlesAsDefs(OpBundles);
470
471 SmallVector<Value *, 3> OptionalArgs;
472 if (Personality == EHPersonality::MSVC_CXX) {
473 OptionalArgs.push_back(CxxLongjmpUnwind);
474 OptionalArgs.push_back(State);
475 OptionalArgs.push_back(emitEHLSDA(Builder, &F));
476 } else if (Personality == EHPersonality::MSVC_X86SEH) {
477 OptionalArgs.push_back(SehLongjmpUnwind);
478 OptionalArgs.push_back(State);
479 if (UseStackGuard)
480 OptionalArgs.push_back(Cookie);
481 } else {
482 llvm_unreachable("unhandled personality!");
483 }
484
485 SmallVector<Value *, 5> Args;
486 Args.push_back(
487 Builder.CreateBitCast(CS.getArgOperand(0), Builder.getInt8PtrTy()));
488 Args.push_back(Builder.getInt32(OptionalArgs.size()));
489 Args.append(OptionalArgs.begin(), OptionalArgs.end());
490
491 CallSite NewCS;
492 if (CS.isCall()) {
493 auto *CI = cast<CallInst>(Inst);
494 CallInst *NewCI = Builder.CreateCall(SetJmp3, Args, OpBundles);
495 NewCI->setTailCallKind(CI->getTailCallKind());
496 NewCS = NewCI;
497 } else {
498 auto *II = cast<InvokeInst>(Inst);
499 NewCS = Builder.CreateInvoke(
500 SetJmp3, II->getNormalDest(), II->getUnwindDest(), Args, OpBundles);
501 }
502 NewCS.setCallingConv(CS.getCallingConv());
503 NewCS.setAttributes(CS.getAttributes());
504 NewCS->setDebugLoc(CS->getDebugLoc());
505
506 Instruction *NewInst = NewCS.getInstruction();
507 NewInst->takeName(Inst);
508 Inst->replaceAllUsesWith(NewInst);
509 Inst->eraseFromParent();
510}
511
David Majnemer7e5937b2016-02-17 18:37:11 +0000512// Figure out what state we should assign calls in this block.
David Majnemere60ee3b2016-02-29 19:16:03 +0000513int WinEHStatePass::getBaseStateForBB(
514 DenseMap<BasicBlock *, ColorVector> &BlockColors, WinEHFuncInfo &FuncInfo,
515 BasicBlock *BB) {
516 int BaseState = ParentBaseState;
David Majnemer7e5937b2016-02-17 18:37:11 +0000517 auto &BBColors = BlockColors[BB];
518
519 assert(BBColors.size() == 1 && "multi-color BB not removed by preparation");
520 BasicBlock *FuncletEntryBB = BBColors.front();
521 if (auto *FuncletPad =
522 dyn_cast<FuncletPadInst>(FuncletEntryBB->getFirstNonPHI())) {
523 auto BaseStateI = FuncInfo.FuncletBaseStateMap.find(FuncletPad);
524 if (BaseStateI != FuncInfo.FuncletBaseStateMap.end())
525 BaseState = BaseStateI->second;
526 }
527
528 return BaseState;
529}
530
531// Calculate the state a call-site is in.
David Majnemere60ee3b2016-02-29 19:16:03 +0000532int WinEHStatePass::getStateForCallSite(
533 DenseMap<BasicBlock *, ColorVector> &BlockColors, WinEHFuncInfo &FuncInfo,
534 CallSite CS) {
David Majnemer7e5937b2016-02-17 18:37:11 +0000535 if (auto *II = dyn_cast<InvokeInst>(CS.getInstruction())) {
536 // Look up the state number of the EH pad this unwinds to.
537 assert(FuncInfo.InvokeStateMap.count(II) && "invoke has no state!");
538 return FuncInfo.InvokeStateMap[II];
539 }
540 // Possibly throwing call instructions have no actions to take after
541 // an unwind. Ensure they are in the -1 state.
542 return getBaseStateForBB(BlockColors, FuncInfo, CS.getParent());
543}
544
545// Calculate the intersection of all the FinalStates for a BasicBlock's
David Majnemera822c882016-02-18 21:13:35 +0000546// predecessors.
David Majnemer7e5937b2016-02-17 18:37:11 +0000547static int getPredState(DenseMap<BasicBlock *, int> &FinalStates, Function &F,
548 int ParentBaseState, BasicBlock *BB) {
549 // The entry block has no predecessors but we know that the prologue always
550 // sets us up with a fixed state.
551 if (&F.getEntryBlock() == BB)
552 return ParentBaseState;
553
554 // This is an EH Pad, conservatively report this basic block as overdefined.
555 if (BB->isEHPad())
556 return OverdefinedState;
557
558 int CommonState = OverdefinedState;
559 for (BasicBlock *PredBB : predecessors(BB)) {
560 // We didn't manage to get a state for one of these predecessors,
561 // conservatively report this basic block as overdefined.
562 auto PredEndState = FinalStates.find(PredBB);
563 if (PredEndState == FinalStates.end())
564 return OverdefinedState;
565
566 // This code is reachable via exceptional control flow,
567 // conservatively report this basic block as overdefined.
568 if (isa<CatchReturnInst>(PredBB->getTerminator()))
569 return OverdefinedState;
570
571 int PredState = PredEndState->second;
572 assert(PredState != OverdefinedState &&
573 "overdefined BBs shouldn't be in FinalStates");
574 if (CommonState == OverdefinedState)
575 CommonState = PredState;
576
577 // At least two predecessors have different FinalStates,
578 // conservatively report this basic block as overdefined.
579 if (CommonState != PredState)
580 return OverdefinedState;
581 }
582
583 return CommonState;
Nico Weber32ac2732016-02-17 18:48:08 +0000584}
David Majnemer7e5937b2016-02-17 18:37:11 +0000585
David Majnemera822c882016-02-18 21:13:35 +0000586// Calculate the intersection of all the InitialStates for a BasicBlock's
587// successors.
588static int getSuccState(DenseMap<BasicBlock *, int> &InitialStates, Function &F,
589 int ParentBaseState, BasicBlock *BB) {
590 // This block rejoins normal control flow,
591 // conservatively report this basic block as overdefined.
592 if (isa<CatchReturnInst>(BB->getTerminator()))
593 return OverdefinedState;
594
595 int CommonState = OverdefinedState;
596 for (BasicBlock *SuccBB : successors(BB)) {
597 // We didn't manage to get a state for one of these predecessors,
598 // conservatively report this basic block as overdefined.
599 auto SuccStartState = InitialStates.find(SuccBB);
600 if (SuccStartState == InitialStates.end())
601 return OverdefinedState;
602
603 // This is an EH Pad, conservatively report this basic block as overdefined.
604 if (SuccBB->isEHPad())
605 return OverdefinedState;
606
607 int SuccState = SuccStartState->second;
608 assert(SuccState != OverdefinedState &&
609 "overdefined BBs shouldn't be in FinalStates");
610 if (CommonState == OverdefinedState)
611 CommonState = SuccState;
612
613 // At least two successors have different InitialStates,
614 // conservatively report this basic block as overdefined.
615 if (CommonState != SuccState)
616 return OverdefinedState;
617 }
618
619 return CommonState;
620}
621
David Majnemere60ee3b2016-02-29 19:16:03 +0000622bool WinEHStatePass::isStateStoreNeeded(EHPersonality Personality,
623 CallSite CS) {
David Majnemer7e5937b2016-02-17 18:37:11 +0000624 if (!CS)
625 return false;
626
David Majnemere60ee3b2016-02-29 19:16:03 +0000627 // If the function touches memory, it needs a state store.
David Majnemer7e5937b2016-02-17 18:37:11 +0000628 if (isAsynchronousEHPersonality(Personality))
629 return !CS.doesNotAccessMemory();
630
David Majnemere60ee3b2016-02-29 19:16:03 +0000631 // If the function throws, it needs a state store.
David Majnemer7e5937b2016-02-17 18:37:11 +0000632 return !CS.doesNotThrow();
633}
634
Reid Klecknerc20276d2015-11-17 21:10:25 +0000635void WinEHStatePass::addStateStores(Function &F, WinEHFuncInfo &FuncInfo) {
636 // Mark the registration node. The backend needs to know which alloca it is so
637 // that it can recover the original frame pointer.
Etienne Bergeronf6be62f2016-06-21 15:58:55 +0000638 IRBuilder<> Builder(RegNode->getNextNode());
Reid Klecknerc20276d2015-11-17 21:10:25 +0000639 Value *RegNodeI8 = Builder.CreateBitCast(RegNode, Builder.getInt8PtrTy());
640 Builder.CreateCall(
641 Intrinsic::getDeclaration(TheModule, Intrinsic::x86_seh_ehregnode),
642 {RegNodeI8});
Reid Kleckner14e77352015-10-09 23:34:53 +0000643
Etienne Bergeronf6be62f2016-06-21 15:58:55 +0000644 if (EHGuardNode) {
645 IRBuilder<> Builder(EHGuardNode->getNextNode());
646 Value *EHGuardNodeI8 =
647 Builder.CreateBitCast(EHGuardNode, Builder.getInt8PtrTy());
648 Builder.CreateCall(
649 Intrinsic::getDeclaration(TheModule, Intrinsic::x86_seh_ehguard),
650 {EHGuardNodeI8});
651 }
652
Reid Klecknerc20276d2015-11-17 21:10:25 +0000653 // Calculate state numbers.
654 if (isAsynchronousEHPersonality(Personality))
655 calculateSEHStateNumbers(&F, FuncInfo);
656 else
657 calculateWinCXXEHStateNumbers(&F, FuncInfo);
Reid Kleckner14e77352015-10-09 23:34:53 +0000658
Reid Klecknerfe4d4912015-05-28 22:00:24 +0000659 // Iterate all the instructions and emit state number stores.
David Majnemer8a1c45d2015-12-12 05:38:55 +0000660 DenseMap<BasicBlock *, ColorVector> BlockColors = colorEHFunclets(F);
David Majnemer7e5937b2016-02-17 18:37:11 +0000661 ReversePostOrderTraversal<Function *> RPOT(&F);
David Majnemer8a1c45d2015-12-12 05:38:55 +0000662
David Majnemer7e5937b2016-02-17 18:37:11 +0000663 // InitialStates yields the state of the first call-site for a BasicBlock.
664 DenseMap<BasicBlock *, int> InitialStates;
665 // FinalStates yields the state of the last call-site for a BasicBlock.
666 DenseMap<BasicBlock *, int> FinalStates;
667 // Worklist used to revisit BasicBlocks with indeterminate
668 // Initial/Final-States.
669 std::deque<BasicBlock *> Worklist;
670 // Fill in InitialStates and FinalStates for BasicBlocks with call-sites.
671 for (BasicBlock *BB : RPOT) {
672 int InitialState = OverdefinedState;
673 int FinalState;
674 if (&F.getEntryBlock() == BB)
675 InitialState = FinalState = ParentBaseState;
676 for (Instruction &I : *BB) {
677 CallSite CS(&I);
678 if (!isStateStoreNeeded(Personality, CS))
David Majnemerf2bb7102016-01-29 05:33:15 +0000679 continue;
680
David Majnemer7e5937b2016-02-17 18:37:11 +0000681 int State = getStateForCallSite(BlockColors, FuncInfo, CS);
682 if (InitialState == OverdefinedState)
683 InitialState = State;
684 FinalState = State;
David Majnemer8a1c45d2015-12-12 05:38:55 +0000685 }
David Majnemer7e5937b2016-02-17 18:37:11 +0000686 // No call-sites in this basic block? That's OK, we will come back to these
687 // in a later pass.
688 if (InitialState == OverdefinedState) {
689 Worklist.push_back(BB);
690 continue;
691 }
692 DEBUG(dbgs() << "X86WinEHState: " << BB->getName()
693 << " InitialState=" << InitialState << '\n');
694 DEBUG(dbgs() << "X86WinEHState: " << BB->getName()
695 << " FinalState=" << FinalState << '\n');
696 InitialStates.insert({BB, InitialState});
697 FinalStates.insert({BB, FinalState});
698 }
David Majnemer8a1c45d2015-12-12 05:38:55 +0000699
David Majnemer7e5937b2016-02-17 18:37:11 +0000700 // Try to fill-in InitialStates and FinalStates which have no call-sites.
701 while (!Worklist.empty()) {
702 BasicBlock *BB = Worklist.front();
703 Worklist.pop_front();
704 // This BasicBlock has already been figured out, nothing more we can do.
705 if (InitialStates.count(BB) != 0)
706 continue;
707
708 int PredState = getPredState(FinalStates, F, ParentBaseState, BB);
709 if (PredState == OverdefinedState)
710 continue;
711
712 // We successfully inferred this BasicBlock's state via it's predecessors;
713 // enqueue it's successors to see if we can infer their states.
714 InitialStates.insert({BB, PredState});
715 FinalStates.insert({BB, PredState});
716 for (BasicBlock *SuccBB : successors(BB))
717 Worklist.push_back(SuccBB);
718 }
719
David Majnemera822c882016-02-18 21:13:35 +0000720 // Try to hoist stores from successors.
721 for (BasicBlock *BB : RPOT) {
722 int SuccState = getSuccState(InitialStates, F, ParentBaseState, BB);
723 if (SuccState == OverdefinedState)
724 continue;
725
726 // Update our FinalState to reflect the common InitialState of our
727 // successors.
728 FinalStates.insert({BB, SuccState});
729 }
730
David Majnemer7e5937b2016-02-17 18:37:11 +0000731 // Finally, insert state stores before call-sites which transition us to a new
732 // state.
733 for (BasicBlock *BB : RPOT) {
734 auto &BBColors = BlockColors[BB];
735 BasicBlock *FuncletEntryBB = BBColors.front();
736 if (isa<CleanupPadInst>(FuncletEntryBB->getFirstNonPHI()))
737 continue;
738
739 int PrevState = getPredState(FinalStates, F, ParentBaseState, BB);
740 DEBUG(dbgs() << "X86WinEHState: " << BB->getName()
741 << " PrevState=" << PrevState << '\n');
742
743 for (Instruction &I : *BB) {
744 CallSite CS(&I);
745 if (!isStateStoreNeeded(Personality, CS))
746 continue;
747
748 int State = getStateForCallSite(BlockColors, FuncInfo, CS);
749 if (State != PrevState)
750 insertStateNumberStore(&I, State);
751 PrevState = State;
Reid Klecknerfe4d4912015-05-28 22:00:24 +0000752 }
David Majnemera822c882016-02-18 21:13:35 +0000753
754 // We might have hoisted a state store into this block, emit it now.
755 auto EndState = FinalStates.find(BB);
756 if (EndState != FinalStates.end())
757 if (EndState->second != PrevState)
758 insertStateNumberStore(BB->getTerminator(), EndState->second);
Reid Klecknerfe4d4912015-05-28 22:00:24 +0000759 }
David Majnemere60ee3b2016-02-29 19:16:03 +0000760
761 SmallVector<CallSite, 1> SetJmp3CallSites;
762 for (BasicBlock *BB : RPOT) {
763 for (Instruction &I : *BB) {
764 CallSite CS(&I);
765 if (!CS)
766 continue;
767 if (CS.getCalledValue()->stripPointerCasts() !=
768 SetJmp3->stripPointerCasts())
769 continue;
770
771 SetJmp3CallSites.push_back(CS);
772 }
773 }
774
775 for (CallSite CS : SetJmp3CallSites) {
776 auto &BBColors = BlockColors[CS->getParent()];
777 BasicBlock *FuncletEntryBB = BBColors.front();
778 bool InCleanup = isa<CleanupPadInst>(FuncletEntryBB->getFirstNonPHI());
779
780 IRBuilder<> Builder(CS.getInstruction());
781 Value *State;
782 if (InCleanup) {
783 Value *StateField =
784 Builder.CreateStructGEP(nullptr, RegNode, StateFieldIndex);
785 State = Builder.CreateLoad(StateField);
786 } else {
787 State = Builder.getInt32(getStateForCallSite(BlockColors, FuncInfo, CS));
788 }
789 rewriteSetJmpCallSite(Builder, F, CS, State);
790 }
Reid Klecknerfe4d4912015-05-28 22:00:24 +0000791}
792
David Majnemerefb41742016-02-01 04:28:59 +0000793void WinEHStatePass::insertStateNumberStore(Instruction *IP, int State) {
Reid Klecknerfe4d4912015-05-28 22:00:24 +0000794 IRBuilder<> Builder(IP);
795 Value *StateField =
David Majnemerefb41742016-02-01 04:28:59 +0000796 Builder.CreateStructGEP(nullptr, RegNode, StateFieldIndex);
Reid Klecknerfe4d4912015-05-28 22:00:24 +0000797 Builder.CreateStore(Builder.getInt32(State), StateField);
798}