clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name CGException.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model static -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/gnu/usr.bin/clang/libclangCodeGen/obj -resource-dir /usr/local/lib/clang/13.0.0 -I /usr/src/gnu/usr.bin/clang/libclangCodeGen/../../../llvm/clang/include -I /usr/src/gnu/usr.bin/clang/libclangCodeGen/../../../llvm/llvm/include -I /usr/src/gnu/usr.bin/clang/libclangCodeGen/../include -I /usr/src/gnu/usr.bin/clang/libclangCodeGen/obj -I /usr/src/gnu/usr.bin/clang/libclangCodeGen/obj/../include -D NDEBUG -D __STDC_LIMIT_MACROS -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D LLVM_PREFIX="/usr" -internal-isystem /usr/include/c++/v1 -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/usr/src/gnu/usr.bin/clang/libclangCodeGen/obj -ferror-limit 19 -fvisibility-inlines-hidden -fwrapv -stack-protector 2 -fno-rtti -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c++ /usr/src/gnu/usr.bin/clang/libclangCodeGen/../../../llvm/clang/lib/CodeGen/CGException.cpp
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | #include "CGCXXABI.h" |
14 | #include "CGCleanup.h" |
15 | #include "CGObjCRuntime.h" |
16 | #include "CodeGenFunction.h" |
17 | #include "ConstantEmitter.h" |
18 | #include "TargetInfo.h" |
19 | #include "clang/AST/Mangle.h" |
20 | #include "clang/AST/StmtCXX.h" |
21 | #include "clang/AST/StmtObjC.h" |
22 | #include "clang/AST/StmtVisitor.h" |
23 | #include "clang/Basic/DiagnosticSema.h" |
24 | #include "clang/Basic/TargetBuiltins.h" |
25 | #include "llvm/IR/IntrinsicInst.h" |
26 | #include "llvm/IR/Intrinsics.h" |
27 | #include "llvm/IR/IntrinsicsWebAssembly.h" |
28 | #include "llvm/Support/SaveAndRestore.h" |
29 | |
30 | using namespace clang; |
31 | using namespace CodeGen; |
32 | |
33 | static llvm::FunctionCallee getFreeExceptionFn(CodeGenModule &CGM) { |
34 | |
35 | |
36 | llvm::FunctionType *FTy = |
37 | llvm::FunctionType::get(CGM.VoidTy, CGM.Int8PtrTy, false); |
38 | |
39 | return CGM.CreateRuntimeFunction(FTy, "__cxa_free_exception"); |
40 | } |
41 | |
42 | static llvm::FunctionCallee getSehTryBeginFn(CodeGenModule &CGM) { |
43 | llvm::FunctionType *FTy = |
44 | llvm::FunctionType::get(CGM.VoidTy, false); |
45 | return CGM.CreateRuntimeFunction(FTy, "llvm.seh.try.begin"); |
46 | } |
47 | |
48 | static llvm::FunctionCallee getSehTryEndFn(CodeGenModule &CGM) { |
49 | llvm::FunctionType *FTy = |
50 | llvm::FunctionType::get(CGM.VoidTy, false); |
51 | return CGM.CreateRuntimeFunction(FTy, "llvm.seh.try.end"); |
52 | } |
53 | |
54 | static llvm::FunctionCallee getUnexpectedFn(CodeGenModule &CGM) { |
55 | |
56 | |
57 | llvm::FunctionType *FTy = |
58 | llvm::FunctionType::get(CGM.VoidTy, CGM.Int8PtrTy, false); |
59 | |
60 | return CGM.CreateRuntimeFunction(FTy, "__cxa_call_unexpected"); |
61 | } |
62 | |
63 | llvm::FunctionCallee CodeGenModule::getTerminateFn() { |
64 | |
65 | |
66 | llvm::FunctionType *FTy = |
67 | llvm::FunctionType::get(VoidTy, false); |
68 | |
69 | StringRef name; |
70 | |
71 | |
72 | if (getLangOpts().CPlusPlus && |
73 | getTarget().getCXXABI().isItaniumFamily()) { |
74 | name = "_ZSt9terminatev"; |
75 | } else if (getLangOpts().CPlusPlus && |
76 | getTarget().getCXXABI().isMicrosoft()) { |
77 | if (getLangOpts().isCompatibleWithMSVC(LangOptions::MSVC2015)) |
78 | name = "__std_terminate"; |
79 | else |
80 | name = "?terminate@@YAXXZ"; |
81 | } else if (getLangOpts().ObjC && |
82 | getLangOpts().ObjCRuntime.hasTerminate()) |
83 | name = "objc_terminate"; |
84 | else |
85 | name = "abort"; |
86 | return CreateRuntimeFunction(FTy, name); |
87 | } |
88 | |
89 | static llvm::FunctionCallee getCatchallRethrowFn(CodeGenModule &CGM, |
90 | StringRef Name) { |
91 | llvm::FunctionType *FTy = |
92 | llvm::FunctionType::get(CGM.VoidTy, CGM.Int8PtrTy, false); |
93 | |
94 | return CGM.CreateRuntimeFunction(FTy, Name); |
95 | } |
96 | |
97 | const EHPersonality EHPersonality::GNU_C = { "__gcc_personality_v0", nullptr }; |
98 | const EHPersonality |
99 | EHPersonality::GNU_C_SJLJ = { "__gcc_personality_sj0", nullptr }; |
100 | const EHPersonality |
101 | EHPersonality::GNU_C_SEH = { "__gcc_personality_seh0", nullptr }; |
102 | const EHPersonality |
103 | EHPersonality::NeXT_ObjC = { "__objc_personality_v0", nullptr }; |
104 | const EHPersonality |
105 | EHPersonality::GNU_CPlusPlus = { "__gxx_personality_v0", nullptr }; |
106 | const EHPersonality |
107 | EHPersonality::GNU_CPlusPlus_SJLJ = { "__gxx_personality_sj0", nullptr }; |
108 | const EHPersonality |
109 | EHPersonality::GNU_CPlusPlus_SEH = { "__gxx_personality_seh0", nullptr }; |
110 | const EHPersonality |
111 | EHPersonality::GNU_ObjC = {"__gnu_objc_personality_v0", "objc_exception_throw"}; |
112 | const EHPersonality |
113 | EHPersonality::GNU_ObjC_SJLJ = {"__gnu_objc_personality_sj0", "objc_exception_throw"}; |
114 | const EHPersonality |
115 | EHPersonality::GNU_ObjC_SEH = {"__gnu_objc_personality_seh0", "objc_exception_throw"}; |
116 | const EHPersonality |
117 | EHPersonality::GNU_ObjCXX = { "__gnustep_objcxx_personality_v0", nullptr }; |
118 | const EHPersonality |
119 | EHPersonality::GNUstep_ObjC = { "__gnustep_objc_personality_v0", nullptr }; |
120 | const EHPersonality |
121 | EHPersonality::MSVC_except_handler = { "_except_handler3", nullptr }; |
122 | const EHPersonality |
123 | EHPersonality::MSVC_C_specific_handler = { "__C_specific_handler", nullptr }; |
124 | const EHPersonality |
125 | EHPersonality::MSVC_CxxFrameHandler3 = { "__CxxFrameHandler3", nullptr }; |
126 | const EHPersonality |
127 | EHPersonality::GNU_Wasm_CPlusPlus = { "__gxx_wasm_personality_v0", nullptr }; |
128 | const EHPersonality EHPersonality::XL_CPlusPlus = {"__xlcxx_personality_v1", |
129 | nullptr}; |
130 | |
131 | static const EHPersonality &getCPersonality(const TargetInfo &Target, |
132 | const LangOptions &L) { |
133 | const llvm::Triple &T = Target.getTriple(); |
134 | if (T.isWindowsMSVCEnvironment()) |
135 | return EHPersonality::MSVC_CxxFrameHandler3; |
136 | if (L.hasSjLjExceptions()) |
137 | return EHPersonality::GNU_C_SJLJ; |
138 | if (L.hasDWARFExceptions()) |
139 | return EHPersonality::GNU_C; |
140 | if (L.hasSEHExceptions()) |
141 | return EHPersonality::GNU_C_SEH; |
142 | return EHPersonality::GNU_C; |
143 | } |
144 | |
145 | static const EHPersonality &getObjCPersonality(const TargetInfo &Target, |
146 | const LangOptions &L) { |
147 | const llvm::Triple &T = Target.getTriple(); |
148 | if (T.isWindowsMSVCEnvironment()) |
149 | return EHPersonality::MSVC_CxxFrameHandler3; |
150 | |
151 | switch (L.ObjCRuntime.getKind()) { |
152 | case ObjCRuntime::FragileMacOSX: |
153 | return getCPersonality(Target, L); |
154 | case ObjCRuntime::MacOSX: |
155 | case ObjCRuntime::iOS: |
156 | case ObjCRuntime::WatchOS: |
157 | return EHPersonality::NeXT_ObjC; |
158 | case ObjCRuntime::GNUstep: |
159 | if (L.ObjCRuntime.getVersion() >= VersionTuple(1, 7)) |
160 | return EHPersonality::GNUstep_ObjC; |
161 | LLVM_FALLTHROUGH; |
162 | case ObjCRuntime::GCC: |
163 | case ObjCRuntime::ObjFW: |
164 | if (L.hasSjLjExceptions()) |
165 | return EHPersonality::GNU_ObjC_SJLJ; |
166 | if (L.hasSEHExceptions()) |
167 | return EHPersonality::GNU_ObjC_SEH; |
168 | return EHPersonality::GNU_ObjC; |
169 | } |
170 | llvm_unreachable("bad runtime kind"); |
171 | } |
172 | |
173 | static const EHPersonality &getCXXPersonality(const TargetInfo &Target, |
174 | const LangOptions &L) { |
175 | const llvm::Triple &T = Target.getTriple(); |
176 | if (T.isWindowsMSVCEnvironment()) |
177 | return EHPersonality::MSVC_CxxFrameHandler3; |
178 | if (T.isOSAIX()) |
179 | return EHPersonality::XL_CPlusPlus; |
180 | if (L.hasSjLjExceptions()) |
181 | return EHPersonality::GNU_CPlusPlus_SJLJ; |
182 | if (L.hasDWARFExceptions()) |
183 | return EHPersonality::GNU_CPlusPlus; |
184 | if (L.hasSEHExceptions()) |
185 | return EHPersonality::GNU_CPlusPlus_SEH; |
186 | if (L.hasWasmExceptions()) |
187 | return EHPersonality::GNU_Wasm_CPlusPlus; |
188 | return EHPersonality::GNU_CPlusPlus; |
189 | } |
190 | |
191 | |
192 | |
193 | static const EHPersonality &getObjCXXPersonality(const TargetInfo &Target, |
194 | const LangOptions &L) { |
195 | if (Target.getTriple().isWindowsMSVCEnvironment()) |
196 | return EHPersonality::MSVC_CxxFrameHandler3; |
197 | |
198 | switch (L.ObjCRuntime.getKind()) { |
199 | |
200 | |
201 | case ObjCRuntime::FragileMacOSX: |
202 | return getCXXPersonality(Target, L); |
203 | |
204 | |
205 | |
206 | |
207 | case ObjCRuntime::MacOSX: |
208 | case ObjCRuntime::iOS: |
209 | case ObjCRuntime::WatchOS: |
210 | return getObjCPersonality(Target, L); |
211 | |
212 | case ObjCRuntime::GNUstep: |
213 | return EHPersonality::GNU_ObjCXX; |
214 | |
215 | |
216 | |
217 | case ObjCRuntime::GCC: |
218 | case ObjCRuntime::ObjFW: |
219 | return getObjCPersonality(Target, L); |
220 | } |
221 | llvm_unreachable("bad runtime kind"); |
222 | } |
223 | |
224 | static const EHPersonality &getSEHPersonalityMSVC(const llvm::Triple &T) { |
225 | if (T.getArch() == llvm::Triple::x86) |
226 | return EHPersonality::MSVC_except_handler; |
227 | return EHPersonality::MSVC_C_specific_handler; |
228 | } |
229 | |
230 | const EHPersonality &EHPersonality::get(CodeGenModule &CGM, |
231 | const FunctionDecl *FD) { |
232 | const llvm::Triple &T = CGM.getTarget().getTriple(); |
233 | const LangOptions &L = CGM.getLangOpts(); |
234 | const TargetInfo &Target = CGM.getTarget(); |
235 | |
236 | |
237 | if (FD && FD->usesSEHTry()) |
238 | return getSEHPersonalityMSVC(T); |
239 | |
240 | if (L.ObjC) |
241 | return L.CPlusPlus ? getObjCXXPersonality(Target, L) |
242 | : getObjCPersonality(Target, L); |
243 | return L.CPlusPlus ? getCXXPersonality(Target, L) |
244 | : getCPersonality(Target, L); |
245 | } |
246 | |
247 | const EHPersonality &EHPersonality::get(CodeGenFunction &CGF) { |
248 | const auto *FD = CGF.CurCodeDecl; |
249 | |
250 | |
251 | |
252 | FD = FD ? FD : CGF.CurSEHParent; |
253 | return get(CGF.CGM, dyn_cast_or_null<FunctionDecl>(FD)); |
254 | } |
255 | |
256 | static llvm::FunctionCallee getPersonalityFn(CodeGenModule &CGM, |
257 | const EHPersonality &Personality) { |
258 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.Int32Ty, true), |
259 | Personality.PersonalityFn, |
260 | llvm::AttributeList(), true); |
261 | } |
262 | |
263 | static llvm::Constant *getOpaquePersonalityFn(CodeGenModule &CGM, |
264 | const EHPersonality &Personality) { |
265 | llvm::FunctionCallee Fn = getPersonalityFn(CGM, Personality); |
266 | llvm::PointerType* Int8PtrTy = llvm::PointerType::get( |
267 | llvm::Type::getInt8Ty(CGM.getLLVMContext()), |
268 | CGM.getDataLayout().getProgramAddressSpace()); |
269 | |
270 | return llvm::ConstantExpr::getBitCast(cast<llvm::Constant>(Fn.getCallee()), |
271 | Int8PtrTy); |
272 | } |
273 | |
274 | |
275 | static bool LandingPadHasOnlyCXXUses(llvm::LandingPadInst *LPI) { |
276 | for (unsigned I = 0, E = LPI->getNumClauses(); I != E; ++I) { |
277 | |
278 | |
279 | llvm::Value *Val = LPI->getClause(I)->stripPointerCasts(); |
280 | if (LPI->isCatch(I)) { |
281 | |
282 | if (llvm::GlobalVariable *GV = dyn_cast<llvm::GlobalVariable>(Val)) |
283 | |
284 | |
285 | if (GV->getName().startswith("OBJC_EHTYPE")) |
286 | return false; |
287 | } else { |
288 | |
289 | llvm::Constant *CVal = cast<llvm::Constant>(Val); |
290 | for (llvm::User::op_iterator |
291 | II = CVal->op_begin(), IE = CVal->op_end(); II != IE; ++II) { |
292 | if (llvm::GlobalVariable *GV = |
293 | cast<llvm::GlobalVariable>((*II)->stripPointerCasts())) |
294 | |
295 | |
296 | if (GV->getName().startswith("OBJC_EHTYPE")) |
297 | return false; |
298 | } |
299 | } |
300 | } |
301 | return true; |
302 | } |
303 | |
304 | |
305 | |
306 | static bool PersonalityHasOnlyCXXUses(llvm::Constant *Fn) { |
307 | for (llvm::User *U : Fn->users()) { |
308 | |
309 | if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(U)) { |
310 | if (CE->getOpcode() != llvm::Instruction::BitCast) return false; |
311 | if (!PersonalityHasOnlyCXXUses(CE)) |
312 | return false; |
313 | continue; |
314 | } |
315 | |
316 | |
317 | llvm::Function *F = dyn_cast<llvm::Function>(U); |
318 | if (!F) return false; |
319 | |
320 | for (auto BB = F->begin(), E = F->end(); BB != E; ++BB) { |
321 | if (BB->isLandingPad()) |
322 | if (!LandingPadHasOnlyCXXUses(BB->getLandingPadInst())) |
323 | return false; |
324 | } |
325 | } |
326 | |
327 | return true; |
328 | } |
329 | |
330 | |
331 | |
332 | |
333 | |
334 | void CodeGenModule::SimplifyPersonality() { |
335 | |
336 | if (!LangOpts.CPlusPlus || !LangOpts.ObjC || !LangOpts.Exceptions) |
337 | return; |
338 | |
339 | |
340 | |
341 | if (!LangOpts.ObjCRuntime.isNeXTFamily()) |
342 | return; |
343 | |
344 | const EHPersonality &ObjCXX = EHPersonality::get(*this, nullptr); |
345 | const EHPersonality &CXX = getCXXPersonality(getTarget(), LangOpts); |
346 | if (&ObjCXX == &CXX) |
347 | return; |
348 | |
349 | assert(std::strcmp(ObjCXX.PersonalityFn, CXX.PersonalityFn) != 0 && |
350 | "Different EHPersonalities using the same personality function."); |
351 | |
352 | llvm::Function *Fn = getModule().getFunction(ObjCXX.PersonalityFn); |
353 | |
354 | |
355 | if (!Fn || Fn->use_empty()) return; |
356 | |
357 | |
358 | if (!PersonalityHasOnlyCXXUses(Fn)) return; |
359 | |
360 | |
361 | |
362 | llvm::FunctionCallee CXXFn = getPersonalityFn(*this, CXX); |
363 | |
364 | |
365 | if (Fn->getType() != CXXFn.getCallee()->getType()) |
366 | return; |
367 | |
368 | Fn->replaceAllUsesWith(CXXFn.getCallee()); |
369 | Fn->eraseFromParent(); |
370 | } |
371 | |
372 | |
373 | |
374 | static llvm::Constant *getCatchAllValue(CodeGenFunction &CGF) { |
375 | |
376 | return llvm::ConstantPointerNull::get(CGF.Int8PtrTy); |
377 | } |
378 | |
379 | namespace { |
380 | |
381 | |
382 | struct FreeException final : EHScopeStack::Cleanup { |
383 | llvm::Value *exn; |
384 | FreeException(llvm::Value *exn) : exn(exn) {} |
385 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
386 | CGF.EmitNounwindRuntimeCall(getFreeExceptionFn(CGF.CGM), exn); |
387 | } |
388 | }; |
389 | } |
390 | |
391 | |
392 | |
393 | |
394 | |
395 | void CodeGenFunction::EmitAnyExprToExn(const Expr *e, Address addr) { |
396 | |
397 | |
398 | pushFullExprCleanup<FreeException>(EHCleanup, addr.getPointer()); |
399 | EHScopeStack::stable_iterator cleanup = EHStack.stable_begin(); |
400 | |
401 | |
402 | |
403 | llvm::Type *ty = ConvertTypeForMem(e->getType())->getPointerTo(); |
404 | Address typedAddr = Builder.CreateBitCast(addr, ty); |
405 | |
406 | |
407 | |
408 | |
409 | |
410 | |
411 | |
412 | |
413 | EmitAnyExprToMem(e, typedAddr, e->getType().getQualifiers(), |
414 | true); |
415 | |
416 | |
417 | DeactivateCleanupBlock(cleanup, |
418 | cast<llvm::Instruction>(typedAddr.getPointer())); |
419 | } |
420 | |
421 | Address CodeGenFunction::getExceptionSlot() { |
422 | if (!ExceptionSlot) |
423 | ExceptionSlot = CreateTempAlloca(Int8PtrTy, "exn.slot"); |
424 | return Address(ExceptionSlot, getPointerAlign()); |
425 | } |
426 | |
427 | Address CodeGenFunction::getEHSelectorSlot() { |
428 | if (!EHSelectorSlot) |
429 | EHSelectorSlot = CreateTempAlloca(Int32Ty, "ehselector.slot"); |
430 | return Address(EHSelectorSlot, CharUnits::fromQuantity(4)); |
431 | } |
432 | |
433 | llvm::Value *CodeGenFunction::getExceptionFromSlot() { |
434 | return Builder.CreateLoad(getExceptionSlot(), "exn"); |
435 | } |
436 | |
437 | llvm::Value *CodeGenFunction::getSelectorFromSlot() { |
438 | return Builder.CreateLoad(getEHSelectorSlot(), "sel"); |
439 | } |
440 | |
441 | void CodeGenFunction::EmitCXXThrowExpr(const CXXThrowExpr *E, |
442 | bool KeepInsertionPoint) { |
443 | if (const Expr *SubExpr = E->getSubExpr()) { |
444 | QualType ThrowType = SubExpr->getType(); |
445 | if (ThrowType->isObjCObjectPointerType()) { |
446 | const Stmt *ThrowStmt = E->getSubExpr(); |
447 | const ObjCAtThrowStmt S(E->getExprLoc(), const_cast<Stmt *>(ThrowStmt)); |
448 | CGM.getObjCRuntime().EmitThrowStmt(*this, S, false); |
449 | } else { |
450 | CGM.getCXXABI().emitThrow(*this, E); |
451 | } |
452 | } else { |
453 | CGM.getCXXABI().emitRethrow(*this, true); |
454 | } |
455 | |
456 | |
457 | |
458 | if (KeepInsertionPoint) |
459 | EmitBlock(createBasicBlock("throw.cont")); |
460 | } |
461 | |
462 | void CodeGenFunction::EmitStartEHSpec(const Decl *D) { |
463 | if (!CGM.getLangOpts().CXXExceptions) |
464 | return; |
465 | |
466 | const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D); |
467 | if (!FD) { |
468 | |
469 | if (const CapturedDecl* CD = dyn_cast_or_null<CapturedDecl>(D)) { |
470 | if (CD->isNothrow()) |
471 | EHStack.pushTerminate(); |
472 | } |
473 | return; |
474 | } |
475 | const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>(); |
476 | if (!Proto) |
477 | return; |
478 | |
479 | ExceptionSpecificationType EST = Proto->getExceptionSpecType(); |
480 | if (isNoexceptExceptionSpec(EST) && Proto->canThrow() == CT_Cannot) { |
481 | |
482 | if (!getLangOpts().EHAsynch) |
483 | EHStack.pushTerminate(); |
484 | } else if (EST == EST_Dynamic || EST == EST_DynamicNone) { |
485 | |
486 | |
487 | if (getTarget().getCXXABI().isMicrosoft()) |
488 | return; |
489 | |
490 | |
491 | |
492 | if (CGM.getLangOpts().hasWasmExceptions()) { |
493 | if (EST == EST_DynamicNone) |
494 | EHStack.pushTerminate(); |
495 | else |
496 | CGM.getDiags().Report(D->getLocation(), |
497 | diag::warn_wasm_dynamic_exception_spec_ignored) |
498 | << FD->getExceptionSpecSourceRange(); |
499 | return; |
500 | } |
501 | |
502 | |
503 | |
504 | |
505 | |
506 | if (getTarget().getCXXABI() == TargetCXXABI::WebAssembly && |
507 | CGM.getLangOpts().getExceptionHandling() == |
508 | LangOptions::ExceptionHandlingKind::None && |
509 | EST == EST_Dynamic) |
510 | CGM.getDiags().Report(D->getLocation(), |
511 | diag::warn_wasm_dynamic_exception_spec_ignored) |
512 | << FD->getExceptionSpecSourceRange(); |
513 | |
514 | unsigned NumExceptions = Proto->getNumExceptions(); |
515 | EHFilterScope *Filter = EHStack.pushFilter(NumExceptions); |
516 | |
517 | for (unsigned I = 0; I != NumExceptions; ++I) { |
518 | QualType Ty = Proto->getExceptionType(I); |
519 | QualType ExceptType = Ty.getNonReferenceType().getUnqualifiedType(); |
520 | llvm::Value *EHType = CGM.GetAddrOfRTTIDescriptor(ExceptType, |
521 | true); |
522 | Filter->setFilter(I, EHType); |
523 | } |
524 | } |
525 | } |
526 | |
527 | |
528 | static void emitFilterDispatchBlock(CodeGenFunction &CGF, |
529 | EHFilterScope &filterScope) { |
530 | llvm::BasicBlock *dispatchBlock = filterScope.getCachedEHDispatchBlock(); |
531 | if (!dispatchBlock) return; |
532 | if (dispatchBlock->use_empty()) { |
533 | delete dispatchBlock; |
534 | return; |
535 | } |
536 | |
537 | CGF.EmitBlockAfterUses(dispatchBlock); |
538 | |
539 | |
540 | |
541 | if (filterScope.getNumFilters()) { |
542 | |
543 | llvm::Value *selector = CGF.getSelectorFromSlot(); |
544 | llvm::BasicBlock *unexpectedBB = CGF.createBasicBlock("ehspec.unexpected"); |
545 | |
546 | llvm::Value *zero = CGF.Builder.getInt32(0); |
547 | llvm::Value *failsFilter = |
548 | CGF.Builder.CreateICmpSLT(selector, zero, "ehspec.fails"); |
549 | CGF.Builder.CreateCondBr(failsFilter, unexpectedBB, |
550 | CGF.getEHResumeBlock(false)); |
551 | |
552 | CGF.EmitBlock(unexpectedBB); |
553 | } |
554 | |
555 | |
556 | |
557 | |
558 | |
559 | llvm::Value *exn = CGF.getExceptionFromSlot(); |
560 | CGF.EmitRuntimeCall(getUnexpectedFn(CGF.CGM), exn) |
561 | ->setDoesNotReturn(); |
562 | CGF.Builder.CreateUnreachable(); |
563 | } |
564 | |
565 | void CodeGenFunction::EmitEndEHSpec(const Decl *D) { |
566 | if (!CGM.getLangOpts().CXXExceptions) |
567 | return; |
568 | |
569 | const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D); |
570 | if (!FD) { |
571 | |
572 | if (const CapturedDecl* CD = dyn_cast_or_null<CapturedDecl>(D)) { |
573 | if (CD->isNothrow() && !EHStack.empty()) |
574 | EHStack.popTerminate(); |
575 | } |
576 | return; |
577 | } |
578 | const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>(); |
579 | if (!Proto) |
580 | return; |
581 | |
582 | ExceptionSpecificationType EST = Proto->getExceptionSpecType(); |
583 | if (isNoexceptExceptionSpec(EST) && Proto->canThrow() == CT_Cannot && |
584 | !EHStack.empty() ) { |
585 | EHStack.popTerminate(); |
586 | } else if (EST == EST_Dynamic || EST == EST_DynamicNone) { |
587 | |
588 | |
589 | if (getTarget().getCXXABI().isMicrosoft()) |
590 | return; |
591 | |
592 | |
593 | |
594 | if (CGM.getLangOpts().hasWasmExceptions()) { |
595 | if (EST == EST_DynamicNone) |
596 | EHStack.popTerminate(); |
597 | return; |
598 | } |
599 | EHFilterScope &filterScope = cast<EHFilterScope>(*EHStack.begin()); |
600 | emitFilterDispatchBlock(*this, filterScope); |
601 | EHStack.popFilter(); |
602 | } |
603 | } |
604 | |
605 | void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) { |
606 | EnterCXXTryStmt(S); |
607 | EmitStmt(S.getTryBlock()); |
608 | ExitCXXTryStmt(S); |
| 1 | Calling 'CodeGenFunction::ExitCXXTryStmt' | |
|
609 | } |
610 | |
611 | void CodeGenFunction::EnterCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock) { |
612 | unsigned NumHandlers = S.getNumHandlers(); |
613 | EHCatchScope *CatchScope = EHStack.pushCatch(NumHandlers); |
614 | |
615 | for (unsigned I = 0; I != NumHandlers; ++I) { |
616 | const CXXCatchStmt *C = S.getHandler(I); |
617 | |
618 | llvm::BasicBlock *Handler = createBasicBlock("catch"); |
619 | if (C->getExceptionDecl()) { |
620 | |
621 | |
622 | |
623 | |
624 | |
625 | |
626 | Qualifiers CaughtTypeQuals; |
627 | QualType CaughtType = CGM.getContext().getUnqualifiedArrayType( |
628 | C->getCaughtType().getNonReferenceType(), CaughtTypeQuals); |
629 | |
630 | CatchTypeInfo TypeInfo{nullptr, 0}; |
631 | if (CaughtType->isObjCObjectPointerType()) |
632 | TypeInfo.RTTI = CGM.getObjCRuntime().GetEHType(CaughtType); |
633 | else |
634 | TypeInfo = CGM.getCXXABI().getAddrOfCXXCatchHandlerType( |
635 | CaughtType, C->getCaughtType()); |
636 | CatchScope->setHandler(I, TypeInfo, Handler); |
637 | } else { |
638 | |
639 | CatchScope->setHandler(I, CGM.getCXXABI().getCatchAllTypeInfo(), Handler); |
640 | |
641 | |
642 | if (getLangOpts().EHAsynch) |
643 | EmitRuntimeCallOrInvoke(getSehTryBeginFn(CGM)); |
644 | } |
645 | } |
646 | } |
647 | |
648 | llvm::BasicBlock * |
649 | CodeGenFunction::getEHDispatchBlock(EHScopeStack::stable_iterator si) { |
650 | if (EHPersonality::get(*this).usesFuncletPads()) |
651 | return getFuncletEHDispatchBlock(si); |
652 | |
653 | |
654 | |
655 | if (si == EHStack.stable_end()) |
656 | return getEHResumeBlock(true); |
657 | |
658 | |
659 | EHScope &scope = *EHStack.find(si); |
660 | |
661 | llvm::BasicBlock *dispatchBlock = scope.getCachedEHDispatchBlock(); |
662 | if (!dispatchBlock) { |
663 | switch (scope.getKind()) { |
664 | case EHScope::Catch: { |
665 | |
666 | EHCatchScope &catchScope = cast<EHCatchScope>(scope); |
667 | if (catchScope.getNumHandlers() == 1 && |
668 | catchScope.getHandler(0).isCatchAll()) { |
669 | dispatchBlock = catchScope.getHandler(0).Block; |
670 | |
671 | |
672 | } else { |
673 | dispatchBlock = createBasicBlock("catch.dispatch"); |
674 | } |
675 | break; |
676 | } |
677 | |
678 | case EHScope::Cleanup: |
679 | dispatchBlock = createBasicBlock("ehcleanup"); |
680 | break; |
681 | |
682 | case EHScope::Filter: |
683 | dispatchBlock = createBasicBlock("filter.dispatch"); |
684 | break; |
685 | |
686 | case EHScope::Terminate: |
687 | dispatchBlock = getTerminateHandler(); |
688 | break; |
689 | } |
690 | scope.setCachedEHDispatchBlock(dispatchBlock); |
691 | } |
692 | return dispatchBlock; |
693 | } |
694 | |
695 | llvm::BasicBlock * |
696 | CodeGenFunction::getFuncletEHDispatchBlock(EHScopeStack::stable_iterator SI) { |
697 | |
698 | |
699 | if (SI == EHStack.stable_end()) |
700 | return nullptr; |
701 | |
702 | |
703 | EHScope &EHS = *EHStack.find(SI); |
704 | |
705 | llvm::BasicBlock *DispatchBlock = EHS.getCachedEHDispatchBlock(); |
706 | if (DispatchBlock) |
707 | return DispatchBlock; |
708 | |
709 | if (EHS.getKind() == EHScope::Terminate) |
710 | DispatchBlock = getTerminateFunclet(); |
711 | else |
712 | DispatchBlock = createBasicBlock(); |
713 | CGBuilderTy Builder(*this, DispatchBlock); |
714 | |
715 | switch (EHS.getKind()) { |
716 | case EHScope::Catch: |
717 | DispatchBlock->setName("catch.dispatch"); |
718 | break; |
719 | |
720 | case EHScope::Cleanup: |
721 | DispatchBlock->setName("ehcleanup"); |
722 | break; |
723 | |
724 | case EHScope::Filter: |
725 | llvm_unreachable("exception specifications not handled yet!"); |
726 | |
727 | case EHScope::Terminate: |
728 | DispatchBlock->setName("terminate"); |
729 | break; |
730 | } |
731 | EHS.setCachedEHDispatchBlock(DispatchBlock); |
732 | return DispatchBlock; |
733 | } |
734 | |
735 | |
736 | |
737 | |
738 | static bool isNonEHScope(const EHScope &S) { |
739 | switch (S.getKind()) { |
740 | case EHScope::Cleanup: |
741 | return !cast<EHCleanupScope>(S).isEHCleanup(); |
742 | case EHScope::Filter: |
743 | case EHScope::Catch: |
744 | case EHScope::Terminate: |
745 | return false; |
746 | } |
747 | |
748 | llvm_unreachable("Invalid EHScope Kind!"); |
749 | } |
750 | |
751 | llvm::BasicBlock *CodeGenFunction::getInvokeDestImpl() { |
752 | assert(EHStack.requiresLandingPad()); |
753 | assert(!EHStack.empty()); |
754 | |
755 | |
756 | |
757 | |
758 | |
759 | const LangOptions &LO = CGM.getLangOpts(); |
760 | if (!LO.Exceptions || LO.IgnoreExceptions) { |
761 | if (!LO.Borland && !LO.MicrosoftExt) |
762 | return nullptr; |
763 | if (!currentFunctionUsesSEHTry()) |
764 | return nullptr; |
765 | } |
766 | |
767 | |
768 | if (LO.CUDA && LO.CUDAIsDevice) |
769 | return nullptr; |
770 | |
771 | |
772 | |
773 | llvm::BasicBlock *LP = EHStack.begin()->getCachedLandingPad(); |
774 | if (LP) return LP; |
775 | |
776 | const EHPersonality &Personality = EHPersonality::get(*this); |
777 | |
778 | if (!CurFn->hasPersonalityFn()) |
779 | CurFn->setPersonalityFn(getOpaquePersonalityFn(CGM, Personality)); |
780 | |
781 | if (Personality.usesFuncletPads()) { |
782 | |
783 | LP = getEHDispatchBlock(EHStack.getInnermostEHScope()); |
784 | } else { |
785 | |
786 | LP = EmitLandingPad(); |
787 | } |
788 | |
789 | assert(LP); |
790 | |
791 | |
792 | |
793 | for (EHScopeStack::iterator ir = EHStack.begin(); true; ++ir) { |
794 | ir->setCachedLandingPad(LP); |
795 | if (!isNonEHScope(*ir)) break; |
796 | } |
797 | |
798 | return LP; |
799 | } |
800 | |
801 | llvm::BasicBlock *CodeGenFunction::EmitLandingPad() { |
802 | assert(EHStack.requiresLandingPad()); |
803 | assert(!CGM.getLangOpts().IgnoreExceptions && |
804 | "LandingPad should not be emitted when -fignore-exceptions are in " |
805 | "effect."); |
806 | EHScope &innermostEHScope = *EHStack.find(EHStack.getInnermostEHScope()); |
807 | switch (innermostEHScope.getKind()) { |
808 | case EHScope::Terminate: |
809 | return getTerminateLandingPad(); |
810 | |
811 | case EHScope::Catch: |
812 | case EHScope::Cleanup: |
813 | case EHScope::Filter: |
814 | if (llvm::BasicBlock *lpad = innermostEHScope.getCachedLandingPad()) |
815 | return lpad; |
816 | } |
817 | |
818 | |
819 | CGBuilderTy::InsertPoint savedIP = Builder.saveAndClearIP(); |
820 | auto DL = ApplyDebugLocation::CreateDefaultArtificial(*this, CurEHLocation); |
821 | |
822 | |
823 | llvm::BasicBlock *lpad = createBasicBlock("lpad"); |
824 | EmitBlock(lpad); |
825 | |
826 | llvm::LandingPadInst *LPadInst = |
827 | Builder.CreateLandingPad(llvm::StructType::get(Int8PtrTy, Int32Ty), 0); |
828 | |
829 | llvm::Value *LPadExn = Builder.CreateExtractValue(LPadInst, 0); |
830 | Builder.CreateStore(LPadExn, getExceptionSlot()); |
831 | llvm::Value *LPadSel = Builder.CreateExtractValue(LPadInst, 1); |
832 | Builder.CreateStore(LPadSel, getEHSelectorSlot()); |
833 | |
834 | |
835 | |
836 | |
837 | |
838 | |
839 | |
840 | bool hasCatchAll = false; |
841 | bool hasCleanup = false; |
842 | bool hasFilter = false; |
843 | SmallVector<llvm::Value*, 4> filterTypes; |
844 | llvm::SmallPtrSet<llvm::Value*, 4> catchTypes; |
845 | for (EHScopeStack::iterator I = EHStack.begin(), E = EHStack.end(); I != E; |
846 | ++I) { |
847 | |
848 | switch (I->getKind()) { |
849 | case EHScope::Cleanup: |
850 | |
851 | hasCleanup = (hasCleanup || cast<EHCleanupScope>(*I).isEHCleanup()); |
852 | continue; |
853 | |
854 | case EHScope::Filter: { |
855 | assert(I.next() == EHStack.end() && "EH filter is not end of EH stack"); |
856 | assert(!hasCatchAll && "EH filter reached after catch-all"); |
857 | |
858 | |
859 | EHFilterScope &filter = cast<EHFilterScope>(*I); |
860 | hasFilter = true; |
861 | |
862 | |
863 | for (unsigned i = 0, e = filter.getNumFilters(); i != e; ++i) |
864 | filterTypes.push_back(filter.getFilter(i)); |
865 | goto done; |
866 | } |
867 | |
868 | case EHScope::Terminate: |
869 | |
870 | assert(!hasCatchAll); |
871 | hasCatchAll = true; |
872 | goto done; |
873 | |
874 | case EHScope::Catch: |
875 | break; |
876 | } |
877 | |
878 | EHCatchScope &catchScope = cast<EHCatchScope>(*I); |
879 | for (unsigned hi = 0, he = catchScope.getNumHandlers(); hi != he; ++hi) { |
880 | EHCatchScope::Handler handler = catchScope.getHandler(hi); |
881 | assert(handler.Type.Flags == 0 && |
882 | "landingpads do not support catch handler flags"); |
883 | |
884 | |
885 | if (!handler.Type.RTTI) { |
886 | assert(!hasCatchAll); |
887 | hasCatchAll = true; |
888 | goto done; |
889 | } |
890 | |
891 | |
892 | if (catchTypes.insert(handler.Type.RTTI).second) |
893 | |
894 | LPadInst->addClause(handler.Type.RTTI); |
895 | } |
896 | } |
897 | |
898 | done: |
899 | |
900 | assert(!(hasCatchAll && hasFilter)); |
901 | if (hasCatchAll) { |
902 | LPadInst->addClause(getCatchAllValue(*this)); |
903 | |
904 | |
905 | |
906 | } else if (hasFilter) { |
907 | |
908 | |
909 | |
910 | SmallVector<llvm::Constant*, 8> Filters; |
911 | llvm::ArrayType *AType = |
912 | llvm::ArrayType::get(!filterTypes.empty() ? |
913 | filterTypes[0]->getType() : Int8PtrTy, |
914 | filterTypes.size()); |
915 | |
916 | for (unsigned i = 0, e = filterTypes.size(); i != e; ++i) |
917 | Filters.push_back(cast<llvm::Constant>(filterTypes[i])); |
918 | llvm::Constant *FilterArray = llvm::ConstantArray::get(AType, Filters); |
919 | LPadInst->addClause(FilterArray); |
920 | |
921 | |
922 | if (hasCleanup) |
923 | LPadInst->setCleanup(true); |
924 | |
925 | |
926 | } else if (hasCleanup) { |
927 | LPadInst->setCleanup(true); |
928 | } |
929 | |
930 | assert((LPadInst->getNumClauses() > 0 || LPadInst->isCleanup()) && |
931 | "landingpad instruction has no clauses!"); |
932 | |
933 | |
934 | Builder.CreateBr(getEHDispatchBlock(EHStack.getInnermostEHScope())); |
935 | |
936 | |
937 | Builder.restoreIP(savedIP); |
938 | |
939 | return lpad; |
940 | } |
941 | |
942 | static void emitCatchPadBlock(CodeGenFunction &CGF, EHCatchScope &CatchScope) { |
943 | llvm::BasicBlock *DispatchBlock = CatchScope.getCachedEHDispatchBlock(); |
944 | assert(DispatchBlock); |
945 | |
946 | CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveIP(); |
947 | CGF.EmitBlockAfterUses(DispatchBlock); |
948 | |
949 | llvm::Value *ParentPad = CGF.CurrentFuncletPad; |
950 | if (!ParentPad) |
951 | ParentPad = llvm::ConstantTokenNone::get(CGF.getLLVMContext()); |
952 | llvm::BasicBlock *UnwindBB = |
953 | CGF.getEHDispatchBlock(CatchScope.getEnclosingEHScope()); |
954 | |
955 | unsigned NumHandlers = CatchScope.getNumHandlers(); |
956 | llvm::CatchSwitchInst *CatchSwitch = |
957 | CGF.Builder.CreateCatchSwitch(ParentPad, UnwindBB, NumHandlers); |
958 | |
959 | |
960 | for (unsigned I = 0; I < NumHandlers; ++I) { |
961 | const EHCatchScope::Handler &Handler = CatchScope.getHandler(I); |
962 | |
963 | CatchTypeInfo TypeInfo = Handler.Type; |
964 | if (!TypeInfo.RTTI) |
965 | TypeInfo.RTTI = llvm::Constant::getNullValue(CGF.VoidPtrTy); |
966 | |
967 | CGF.Builder.SetInsertPoint(Handler.Block); |
968 | |
969 | if (EHPersonality::get(CGF).isMSVCXXPersonality()) { |
970 | CGF.Builder.CreateCatchPad( |
971 | CatchSwitch, {TypeInfo.RTTI, CGF.Builder.getInt32(TypeInfo.Flags), |
972 | llvm::Constant::getNullValue(CGF.VoidPtrTy)}); |
973 | } else { |
974 | CGF.Builder.CreateCatchPad(CatchSwitch, {TypeInfo.RTTI}); |
975 | } |
976 | |
977 | CatchSwitch->addHandler(Handler.Block); |
978 | } |
979 | CGF.Builder.restoreIP(SavedIP); |
980 | } |
981 | |
982 | |
983 | |
984 | |
985 | static void emitWasmCatchPadBlock(CodeGenFunction &CGF, |
986 | EHCatchScope &CatchScope) { |
987 | llvm::BasicBlock *DispatchBlock = CatchScope.getCachedEHDispatchBlock(); |
988 | assert(DispatchBlock); |
989 | |
990 | CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveIP(); |
991 | CGF.EmitBlockAfterUses(DispatchBlock); |
992 | |
993 | llvm::Value *ParentPad = CGF.CurrentFuncletPad; |
994 | if (!ParentPad) |
995 | ParentPad = llvm::ConstantTokenNone::get(CGF.getLLVMContext()); |
996 | llvm::BasicBlock *UnwindBB = |
997 | CGF.getEHDispatchBlock(CatchScope.getEnclosingEHScope()); |
998 | |
999 | unsigned NumHandlers = CatchScope.getNumHandlers(); |
1000 | llvm::CatchSwitchInst *CatchSwitch = |
1001 | CGF.Builder.CreateCatchSwitch(ParentPad, UnwindBB, NumHandlers); |
1002 | |
1003 | |
1004 | |
1005 | llvm::BasicBlock *WasmCatchStartBlock = CGF.createBasicBlock("catch.start"); |
1006 | CatchSwitch->addHandler(WasmCatchStartBlock); |
1007 | CGF.EmitBlockAfterUses(WasmCatchStartBlock); |
1008 | |
1009 | |
1010 | SmallVector<llvm::Value *, 4> CatchTypes; |
1011 | for (unsigned I = 0, E = NumHandlers; I < E; ++I) { |
1012 | const EHCatchScope::Handler &Handler = CatchScope.getHandler(I); |
1013 | CatchTypeInfo TypeInfo = Handler.Type; |
1014 | if (!TypeInfo.RTTI) |
1015 | TypeInfo.RTTI = llvm::Constant::getNullValue(CGF.VoidPtrTy); |
1016 | CatchTypes.push_back(TypeInfo.RTTI); |
1017 | } |
1018 | auto *CPI = CGF.Builder.CreateCatchPad(CatchSwitch, CatchTypes); |
1019 | |
1020 | |
1021 | |
1022 | |
1023 | llvm::Function *GetExnFn = |
1024 | CGF.CGM.getIntrinsic(llvm::Intrinsic::wasm_get_exception); |
1025 | llvm::Function *GetSelectorFn = |
1026 | CGF.CGM.getIntrinsic(llvm::Intrinsic::wasm_get_ehselector); |
1027 | llvm::CallInst *Exn = CGF.Builder.CreateCall(GetExnFn, CPI); |
1028 | CGF.Builder.CreateStore(Exn, CGF.getExceptionSlot()); |
1029 | llvm::CallInst *Selector = CGF.Builder.CreateCall(GetSelectorFn, CPI); |
1030 | |
1031 | llvm::Function *TypeIDFn = CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for); |
1032 | |
1033 | |
1034 | if (CatchScope.getNumHandlers() == 1 && |
1035 | CatchScope.getHandler(0).isCatchAll()) { |
1036 | CGF.Builder.CreateBr(CatchScope.getHandler(0).Block); |
1037 | CGF.Builder.restoreIP(SavedIP); |
1038 | return; |
1039 | } |
1040 | |
1041 | |
1042 | for (unsigned I = 0, E = NumHandlers;; ++I) { |
1043 | assert(I < E && "ran off end of handlers!"); |
1044 | const EHCatchScope::Handler &Handler = CatchScope.getHandler(I); |
1045 | CatchTypeInfo TypeInfo = Handler.Type; |
1046 | if (!TypeInfo.RTTI) |
1047 | TypeInfo.RTTI = llvm::Constant::getNullValue(CGF.VoidPtrTy); |
1048 | |
1049 | |
1050 | llvm::BasicBlock *NextBlock; |
1051 | |
1052 | bool EmitNextBlock = false, NextIsEnd = false; |
1053 | |
1054 | |
1055 | |
1056 | |
1057 | if (I + 1 == E) { |
1058 | NextBlock = CGF.createBasicBlock("rethrow"); |
1059 | EmitNextBlock = true; |
1060 | NextIsEnd = true; |
1061 | |
1062 | |
1063 | |
1064 | } else if (CatchScope.getHandler(I + 1).isCatchAll()) { |
1065 | NextBlock = CatchScope.getHandler(I + 1).Block; |
1066 | NextIsEnd = true; |
1067 | |
1068 | |
1069 | } else { |
1070 | NextBlock = CGF.createBasicBlock("catch.fallthrough"); |
1071 | EmitNextBlock = true; |
1072 | } |
1073 | |
1074 | |
1075 | llvm::CallInst *TypeIndex = CGF.Builder.CreateCall(TypeIDFn, TypeInfo.RTTI); |
1076 | TypeIndex->setDoesNotThrow(); |
1077 | |
1078 | llvm::Value *MatchesTypeIndex = |
1079 | CGF.Builder.CreateICmpEQ(Selector, TypeIndex, "matches"); |
1080 | CGF.Builder.CreateCondBr(MatchesTypeIndex, Handler.Block, NextBlock); |
1081 | |
1082 | if (EmitNextBlock) |
1083 | CGF.EmitBlock(NextBlock); |
1084 | if (NextIsEnd) |
1085 | break; |
1086 | } |
1087 | |
1088 | CGF.Builder.restoreIP(SavedIP); |
1089 | } |
1090 | |
1091 | |
1092 | |
1093 | static void emitCatchDispatchBlock(CodeGenFunction &CGF, |
1094 | EHCatchScope &catchScope) { |
1095 | if (EHPersonality::get(CGF).isWasmPersonality()) |
1096 | return emitWasmCatchPadBlock(CGF, catchScope); |
1097 | if (EHPersonality::get(CGF).usesFuncletPads()) |
1098 | return emitCatchPadBlock(CGF, catchScope); |
1099 | |
1100 | llvm::BasicBlock *dispatchBlock = catchScope.getCachedEHDispatchBlock(); |
1101 | assert(dispatchBlock); |
1102 | |
1103 | |
1104 | |
1105 | if (catchScope.getNumHandlers() == 1 && |
1106 | catchScope.getHandler(0).isCatchAll()) { |
1107 | assert(dispatchBlock == catchScope.getHandler(0).Block); |
1108 | return; |
1109 | } |
1110 | |
1111 | CGBuilderTy::InsertPoint savedIP = CGF.Builder.saveIP(); |
1112 | CGF.EmitBlockAfterUses(dispatchBlock); |
1113 | |
1114 | |
1115 | llvm::Function *llvm_eh_typeid_for = |
1116 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for); |
1117 | |
1118 | |
1119 | llvm::Value *selector = CGF.getSelectorFromSlot(); |
1120 | |
1121 | |
1122 | for (unsigned i = 0, e = catchScope.getNumHandlers(); ; ++i) { |
1123 | assert(i < e && "ran off end of handlers!"); |
1124 | const EHCatchScope::Handler &handler = catchScope.getHandler(i); |
1125 | |
1126 | llvm::Value *typeValue = handler.Type.RTTI; |
1127 | assert(handler.Type.Flags == 0 && |
1128 | "landingpads do not support catch handler flags"); |
1129 | assert(typeValue && "fell into catch-all case!"); |
1130 | typeValue = CGF.Builder.CreateBitCast(typeValue, CGF.Int8PtrTy); |
1131 | |
1132 | |
1133 | bool nextIsEnd; |
1134 | llvm::BasicBlock *nextBlock; |
1135 | |
1136 | |
1137 | |
1138 | if (i + 1 == e) { |
1139 | nextBlock = CGF.getEHDispatchBlock(catchScope.getEnclosingEHScope()); |
1140 | nextIsEnd = true; |
1141 | |
1142 | |
1143 | |
1144 | } else if (catchScope.getHandler(i+1).isCatchAll()) { |
1145 | nextBlock = catchScope.getHandler(i+1).Block; |
1146 | nextIsEnd = true; |
1147 | |
1148 | |
1149 | } else { |
1150 | nextBlock = CGF.createBasicBlock("catch.fallthrough"); |
1151 | nextIsEnd = false; |
1152 | } |
1153 | |
1154 | |
1155 | llvm::CallInst *typeIndex = |
1156 | CGF.Builder.CreateCall(llvm_eh_typeid_for, typeValue); |
1157 | typeIndex->setDoesNotThrow(); |
1158 | |
1159 | llvm::Value *matchesTypeIndex = |
1160 | CGF.Builder.CreateICmpEQ(selector, typeIndex, "matches"); |
1161 | CGF.Builder.CreateCondBr(matchesTypeIndex, handler.Block, nextBlock); |
1162 | |
1163 | |
1164 | if (nextIsEnd) { |
1165 | CGF.Builder.restoreIP(savedIP); |
1166 | return; |
1167 | } |
1168 | |
1169 | CGF.EmitBlock(nextBlock); |
1170 | } |
1171 | } |
1172 | |
1173 | void CodeGenFunction::popCatchScope() { |
1174 | EHCatchScope &catchScope = cast<EHCatchScope>(*EHStack.begin()); |
1175 | if (catchScope.hasEHBranches()) |
1176 | emitCatchDispatchBlock(*this, catchScope); |
1177 | EHStack.popCatch(); |
1178 | } |
1179 | |
1180 | void CodeGenFunction::ExitCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock) { |
1181 | unsigned NumHandlers = S.getNumHandlers(); |
1182 | EHCatchScope &CatchScope = cast<EHCatchScope>(*EHStack.begin()); |
| 2 | | The object is a 'EHCatchScope' | |
|
1183 | assert(CatchScope.getNumHandlers() == NumHandlers); |
1184 | llvm::BasicBlock *DispatchBlock = CatchScope.getCachedEHDispatchBlock(); |
1185 | |
1186 | |
1187 | if (!CatchScope.hasEHBranches()) { |
| 3 | | Calling 'EHScope::hasEHBranches' | |
|
| 11 | | Returning from 'EHScope::hasEHBranches' | |
|
| |
1188 | CatchScope.clearHandlerBlocks(); |
1189 | EHStack.popCatch(); |
1190 | return; |
1191 | } |
1192 | |
1193 | |
1194 | emitCatchDispatchBlock(*this, CatchScope); |
1195 | |
1196 | |
1197 | |
1198 | SmallVector<EHCatchScope::Handler, 8> Handlers( |
1199 | CatchScope.begin(), CatchScope.begin() + NumHandlers); |
1200 | |
1201 | EHStack.popCatch(); |
1202 | |
1203 | |
1204 | llvm::BasicBlock *ContBB = createBasicBlock("try.cont"); |
1205 | |
1206 | |
1207 | if (HaveInsertPoint()) |
| |
1208 | Builder.CreateBr(ContBB); |
1209 | |
1210 | |
1211 | |
1212 | bool doImplicitRethrow = false; |
1213 | if (IsFnTryBlock) |
| |
1214 | doImplicitRethrow = isa<CXXDestructorDecl>(CurCodeDecl) || |
1215 | isa<CXXConstructorDecl>(CurCodeDecl); |
1216 | |
1217 | |
1218 | |
1219 | |
1220 | SaveAndRestore<llvm::Instruction *> RestoreCurrentFuncletPad( |
1221 | CurrentFuncletPad); |
1222 | llvm::BasicBlock *WasmCatchStartBlock = nullptr; |
| 15 | | 'WasmCatchStartBlock' initialized to a null pointer value | |
|
1223 | if (EHPersonality::get(*this).isWasmPersonality()) { |
| 16 | | Assuming the condition is false | |
|
| |
1224 | auto *CatchSwitch = |
1225 | cast<llvm::CatchSwitchInst>(DispatchBlock->getFirstNonPHI()); |
1226 | WasmCatchStartBlock = CatchSwitch->hasUnwindDest() |
1227 | ? CatchSwitch->getSuccessor(1) |
1228 | : CatchSwitch->getSuccessor(0); |
1229 | auto *CPI = cast<llvm::CatchPadInst>(WasmCatchStartBlock->getFirstNonPHI()); |
1230 | CurrentFuncletPad = CPI; |
1231 | } |
1232 | |
1233 | |
1234 | |
1235 | |
1236 | |
1237 | |
1238 | |
1239 | |
1240 | bool HasCatchAll = false; |
1241 | for (unsigned I = NumHandlers; I != 0; --I) { |
| 18 | | Loop condition is false. Execution continues on line 1291 | |
|
1242 | HasCatchAll |= Handlers[I - 1].isCatchAll(); |
1243 | llvm::BasicBlock *CatchBlock = Handlers[I-1].Block; |
1244 | EmitBlockAfterUses(CatchBlock); |
1245 | |
1246 | |
1247 | const CXXCatchStmt *C = S.getHandler(I-1); |
1248 | |
1249 | |
1250 | |
1251 | RunCleanupsScope CatchScope(*this); |
1252 | |
1253 | |
1254 | SaveAndRestore<llvm::Instruction *> RestoreCurrentFuncletPad( |
1255 | CurrentFuncletPad); |
1256 | CGM.getCXXABI().emitBeginCatch(*this, C); |
1257 | |
1258 | |
1259 | incrementProfileCounter(C); |
1260 | |
1261 | |
1262 | EmitStmt(C->getHandlerBlock()); |
1263 | |
1264 | |
1265 | |
1266 | |
1267 | |
1268 | |
1269 | |
1270 | |
1271 | |
1272 | |
1273 | if (doImplicitRethrow && HaveInsertPoint()) { |
1274 | CGM.getCXXABI().emitRethrow(*this, false); |
1275 | Builder.CreateUnreachable(); |
1276 | Builder.ClearInsertionPoint(); |
1277 | } |
1278 | |
1279 | |
1280 | CatchScope.ForceCleanup(); |
1281 | |
1282 | |
1283 | if (HaveInsertPoint()) |
1284 | Builder.CreateBr(ContBB); |
1285 | } |
1286 | |
1287 | |
1288 | |
1289 | |
1290 | |
1291 | if (EHPersonality::get(*this).isWasmPersonality() && !HasCatchAll) { |
| 19 | | Assuming the condition is true | |
|
| |
1292 | assert(WasmCatchStartBlock); |
1293 | |
1294 | |
1295 | |
1296 | |
1297 | llvm::BasicBlock *RethrowBlock = WasmCatchStartBlock; |
| 21 | | 'RethrowBlock' initialized to a null pointer value | |
|
1298 | while (llvm::Instruction *TI = RethrowBlock->getTerminator()) { |
| 22 | | Called C++ object pointer is null |
|
1299 | auto *BI = cast<llvm::BranchInst>(TI); |
1300 | assert(BI->isConditional()); |
1301 | RethrowBlock = BI->getSuccessor(1); |
1302 | } |
1303 | assert(RethrowBlock != WasmCatchStartBlock && RethrowBlock->empty()); |
1304 | Builder.SetInsertPoint(RethrowBlock); |
1305 | llvm::Function *RethrowInCatchFn = |
1306 | CGM.getIntrinsic(llvm::Intrinsic::wasm_rethrow); |
1307 | EmitNoreturnRuntimeCallOrInvoke(RethrowInCatchFn, {}); |
1308 | } |
1309 | |
1310 | EmitBlock(ContBB); |
1311 | incrementProfileCounter(&S); |
1312 | } |
1313 | |
1314 | namespace { |
1315 | struct CallEndCatchForFinally final : EHScopeStack::Cleanup { |
1316 | llvm::Value *ForEHVar; |
1317 | llvm::FunctionCallee EndCatchFn; |
1318 | CallEndCatchForFinally(llvm::Value *ForEHVar, |
1319 | llvm::FunctionCallee EndCatchFn) |
1320 | : ForEHVar(ForEHVar), EndCatchFn(EndCatchFn) {} |
1321 | |
1322 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
1323 | llvm::BasicBlock *EndCatchBB = CGF.createBasicBlock("finally.endcatch"); |
1324 | llvm::BasicBlock *CleanupContBB = |
1325 | CGF.createBasicBlock("finally.cleanup.cont"); |
1326 | |
1327 | llvm::Value *ShouldEndCatch = |
1328 | CGF.Builder.CreateFlagLoad(ForEHVar, "finally.endcatch"); |
1329 | CGF.Builder.CreateCondBr(ShouldEndCatch, EndCatchBB, CleanupContBB); |
1330 | CGF.EmitBlock(EndCatchBB); |
1331 | CGF.EmitRuntimeCallOrInvoke(EndCatchFn); |
1332 | CGF.EmitBlock(CleanupContBB); |
1333 | } |
1334 | }; |
1335 | |
1336 | struct PerformFinally final : EHScopeStack::Cleanup { |
1337 | const Stmt *Body; |
1338 | llvm::Value *ForEHVar; |
1339 | llvm::FunctionCallee EndCatchFn; |
1340 | llvm::FunctionCallee RethrowFn; |
1341 | llvm::Value *SavedExnVar; |
1342 | |
1343 | PerformFinally(const Stmt *Body, llvm::Value *ForEHVar, |
1344 | llvm::FunctionCallee EndCatchFn, |
1345 | llvm::FunctionCallee RethrowFn, llvm::Value *SavedExnVar) |
1346 | : Body(Body), ForEHVar(ForEHVar), EndCatchFn(EndCatchFn), |
1347 | RethrowFn(RethrowFn), SavedExnVar(SavedExnVar) {} |
1348 | |
1349 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
1350 | |
1351 | if (EndCatchFn) |
1352 | CGF.EHStack.pushCleanup<CallEndCatchForFinally>(NormalAndEHCleanup, |
1353 | ForEHVar, EndCatchFn); |
1354 | |
1355 | |
1356 | |
1357 | llvm::Value *SavedCleanupDest = |
1358 | CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot(), |
1359 | "cleanup.dest.saved"); |
1360 | |
1361 | |
1362 | CGF.EmitStmt(Body); |
1363 | |
1364 | |
1365 | |
1366 | if (CGF.HaveInsertPoint()) { |
1367 | llvm::BasicBlock *RethrowBB = CGF.createBasicBlock("finally.rethrow"); |
1368 | llvm::BasicBlock *ContBB = CGF.createBasicBlock("finally.cont"); |
1369 | |
1370 | llvm::Value *ShouldRethrow = |
1371 | CGF.Builder.CreateFlagLoad(ForEHVar, "finally.shouldthrow"); |
1372 | CGF.Builder.CreateCondBr(ShouldRethrow, RethrowBB, ContBB); |
1373 | |
1374 | CGF.EmitBlock(RethrowBB); |
1375 | if (SavedExnVar) { |
1376 | CGF.EmitRuntimeCallOrInvoke(RethrowFn, |
1377 | CGF.Builder.CreateAlignedLoad(CGF.Int8PtrTy, SavedExnVar, |
1378 | CGF.getPointerAlign())); |
1379 | } else { |
1380 | CGF.EmitRuntimeCallOrInvoke(RethrowFn); |
1381 | } |
1382 | CGF.Builder.CreateUnreachable(); |
1383 | |
1384 | CGF.EmitBlock(ContBB); |
1385 | |
1386 | |
1387 | CGF.Builder.CreateStore(SavedCleanupDest, |
1388 | CGF.getNormalCleanupDestSlot()); |
1389 | } |
1390 | |
1391 | |
1392 | |
1393 | |
1394 | if (EndCatchFn) { |
1395 | CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP(); |
1396 | CGF.PopCleanupBlock(); |
1397 | CGF.Builder.restoreIP(SavedIP); |
1398 | } |
1399 | |
1400 | |
1401 | |
1402 | CGF.EnsureInsertPoint(); |
1403 | } |
1404 | }; |
1405 | } |
1406 | |
1407 | |
1408 | |
1409 | |
1410 | void CodeGenFunction::FinallyInfo::enter(CodeGenFunction &CGF, const Stmt *body, |
1411 | llvm::FunctionCallee beginCatchFn, |
1412 | llvm::FunctionCallee endCatchFn, |
1413 | llvm::FunctionCallee rethrowFn) { |
1414 | assert((!!beginCatchFn) == (!!endCatchFn) && |
1415 | "begin/end catch functions not paired"); |
1416 | assert(rethrowFn && "rethrow function is required"); |
1417 | |
1418 | BeginCatchFn = beginCatchFn; |
1419 | |
1420 | |
1421 | |
1422 | |
1423 | |
1424 | |
1425 | |
1426 | llvm::FunctionType *rethrowFnTy = rethrowFn.getFunctionType(); |
1427 | SavedExnVar = nullptr; |
1428 | if (rethrowFnTy->getNumParams()) |
1429 | SavedExnVar = CGF.CreateTempAlloca(CGF.Int8PtrTy, "finally.exn"); |
1430 | |
1431 | |
1432 | |
1433 | |
1434 | |
1435 | |
1436 | |
1437 | |
1438 | |
1439 | |
1440 | |
1441 | |
1442 | |
1443 | |
1444 | |
1445 | |
1446 | RethrowDest = CGF.getJumpDestInCurrentScope(CGF.getUnreachableBlock()); |
1447 | |
1448 | |
1449 | ForEHVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(), "finally.for-eh"); |
1450 | CGF.Builder.CreateFlagStore(false, ForEHVar); |
1451 | |
1452 | |
1453 | CGF.EHStack.pushCleanup<PerformFinally>(NormalCleanup, body, |
1454 | ForEHVar, endCatchFn, |
1455 | rethrowFn, SavedExnVar); |
1456 | |
1457 | |
1458 | llvm::BasicBlock *catchBB = CGF.createBasicBlock("finally.catchall"); |
1459 | EHCatchScope *catchScope = CGF.EHStack.pushCatch(1); |
1460 | catchScope->setCatchAllHandler(0, catchBB); |
1461 | } |
1462 | |
1463 | void CodeGenFunction::FinallyInfo::exit(CodeGenFunction &CGF) { |
1464 | |
1465 | EHCatchScope &catchScope = cast<EHCatchScope>(*CGF.EHStack.begin()); |
1466 | llvm::BasicBlock *catchBB = catchScope.getHandler(0).Block; |
1467 | |
1468 | CGF.popCatchScope(); |
1469 | |
1470 | |
1471 | if (catchBB->use_empty()) { |
1472 | delete catchBB; |
1473 | } else { |
1474 | CGBuilderTy::InsertPoint savedIP = CGF.Builder.saveAndClearIP(); |
1475 | CGF.EmitBlock(catchBB); |
1476 | |
1477 | llvm::Value *exn = nullptr; |
1478 | |
1479 | |
1480 | if (BeginCatchFn) { |
1481 | exn = CGF.getExceptionFromSlot(); |
1482 | CGF.EmitNounwindRuntimeCall(BeginCatchFn, exn); |
1483 | } |
1484 | |
1485 | |
1486 | if (SavedExnVar) { |
1487 | if (!exn) exn = CGF.getExceptionFromSlot(); |
1488 | CGF.Builder.CreateAlignedStore(exn, SavedExnVar, CGF.getPointerAlign()); |
1489 | } |
1490 | |
1491 | |
1492 | CGF.Builder.CreateFlagStore(true, ForEHVar); |
1493 | |
1494 | |
1495 | CGF.EmitBranchThroughCleanup(RethrowDest); |
1496 | |
1497 | CGF.Builder.restoreIP(savedIP); |
1498 | } |
1499 | |
1500 | |
1501 | CGF.PopCleanupBlock(); |
1502 | } |
1503 | |
1504 | llvm::BasicBlock *CodeGenFunction::getTerminateLandingPad() { |
1505 | if (TerminateLandingPad) |
1506 | return TerminateLandingPad; |
1507 | |
1508 | CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP(); |
1509 | |
1510 | |
1511 | TerminateLandingPad = createBasicBlock("terminate.lpad"); |
1512 | Builder.SetInsertPoint(TerminateLandingPad); |
1513 | |
1514 | |
1515 | const EHPersonality &Personality = EHPersonality::get(*this); |
1516 | |
1517 | if (!CurFn->hasPersonalityFn()) |
1518 | CurFn->setPersonalityFn(getOpaquePersonalityFn(CGM, Personality)); |
1519 | |
1520 | llvm::LandingPadInst *LPadInst = |
1521 | Builder.CreateLandingPad(llvm::StructType::get(Int8PtrTy, Int32Ty), 0); |
1522 | LPadInst->addClause(getCatchAllValue(*this)); |
1523 | |
1524 | llvm::Value *Exn = nullptr; |
1525 | if (getLangOpts().CPlusPlus) |
1526 | Exn = Builder.CreateExtractValue(LPadInst, 0); |
1527 | llvm::CallInst *terminateCall = |
1528 | CGM.getCXXABI().emitTerminateForUnexpectedException(*this, Exn); |
1529 | terminateCall->setDoesNotReturn(); |
1530 | Builder.CreateUnreachable(); |
1531 | |
1532 | |
1533 | Builder.restoreIP(SavedIP); |
1534 | |
1535 | return TerminateLandingPad; |
1536 | } |
1537 | |
1538 | llvm::BasicBlock *CodeGenFunction::getTerminateHandler() { |
1539 | if (TerminateHandler) |
1540 | return TerminateHandler; |
1541 | |
1542 | |
1543 | |
1544 | TerminateHandler = createBasicBlock("terminate.handler"); |
1545 | CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP(); |
1546 | Builder.SetInsertPoint(TerminateHandler); |
1547 | |
1548 | llvm::Value *Exn = nullptr; |
1549 | if (getLangOpts().CPlusPlus) |
1550 | Exn = getExceptionFromSlot(); |
1551 | llvm::CallInst *terminateCall = |
1552 | CGM.getCXXABI().emitTerminateForUnexpectedException(*this, Exn); |
1553 | terminateCall->setDoesNotReturn(); |
1554 | Builder.CreateUnreachable(); |
1555 | |
1556 | |
1557 | Builder.restoreIP(SavedIP); |
1558 | |
1559 | return TerminateHandler; |
1560 | } |
1561 | |
1562 | llvm::BasicBlock *CodeGenFunction::getTerminateFunclet() { |
1563 | assert(EHPersonality::get(*this).usesFuncletPads() && |
1564 | "use getTerminateLandingPad for non-funclet EH"); |
1565 | |
1566 | llvm::BasicBlock *&TerminateFunclet = TerminateFunclets[CurrentFuncletPad]; |
1567 | if (TerminateFunclet) |
1568 | return TerminateFunclet; |
1569 | |
1570 | CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP(); |
1571 | |
1572 | |
1573 | |
1574 | TerminateFunclet = createBasicBlock("terminate.handler"); |
1575 | Builder.SetInsertPoint(TerminateFunclet); |
1576 | |
1577 | |
1578 | |
1579 | SaveAndRestore<llvm::Instruction *> RestoreCurrentFuncletPad( |
1580 | CurrentFuncletPad); |
1581 | llvm::Value *ParentPad = CurrentFuncletPad; |
1582 | if (!ParentPad) |
1583 | ParentPad = llvm::ConstantTokenNone::get(CGM.getLLVMContext()); |
1584 | CurrentFuncletPad = Builder.CreateCleanupPad(ParentPad); |
1585 | |
1586 | |
1587 | llvm::CallInst *terminateCall = |
1588 | CGM.getCXXABI().emitTerminateForUnexpectedException(*this, nullptr); |
1589 | terminateCall->setDoesNotReturn(); |
1590 | Builder.CreateUnreachable(); |
1591 | |
1592 | |
1593 | Builder.restoreIP(SavedIP); |
1594 | |
1595 | return TerminateFunclet; |
1596 | } |
1597 | |
1598 | llvm::BasicBlock *CodeGenFunction::getEHResumeBlock(bool isCleanup) { |
1599 | if (EHResumeBlock) return EHResumeBlock; |
1600 | |
1601 | CGBuilderTy::InsertPoint SavedIP = Builder.saveIP(); |
1602 | |
1603 | |
1604 | EHResumeBlock = createBasicBlock("eh.resume"); |
1605 | Builder.SetInsertPoint(EHResumeBlock); |
1606 | |
1607 | const EHPersonality &Personality = EHPersonality::get(*this); |
1608 | |
1609 | |
1610 | |
1611 | const char *RethrowName = Personality.CatchallRethrowFn; |
1612 | if (RethrowName != nullptr && !isCleanup) { |
1613 | EmitRuntimeCall(getCatchallRethrowFn(CGM, RethrowName), |
1614 | getExceptionFromSlot())->setDoesNotReturn(); |
1615 | Builder.CreateUnreachable(); |
1616 | Builder.restoreIP(SavedIP); |
1617 | return EHResumeBlock; |
1618 | } |
1619 | |
1620 | |
1621 | llvm::Value *Exn = getExceptionFromSlot(); |
1622 | llvm::Value *Sel = getSelectorFromSlot(); |
1623 | |
1624 | llvm::Type *LPadType = llvm::StructType::get(Exn->getType(), Sel->getType()); |
1625 | llvm::Value *LPadVal = llvm::UndefValue::get(LPadType); |
1626 | LPadVal = Builder.CreateInsertValue(LPadVal, Exn, 0, "lpad.val"); |
1627 | LPadVal = Builder.CreateInsertValue(LPadVal, Sel, 1, "lpad.val"); |
1628 | |
1629 | Builder.CreateResume(LPadVal); |
1630 | Builder.restoreIP(SavedIP); |
1631 | return EHResumeBlock; |
1632 | } |
1633 | |
1634 | void CodeGenFunction::EmitSEHTryStmt(const SEHTryStmt &S) { |
1635 | EnterSEHTryStmt(S); |
1636 | { |
1637 | JumpDest TryExit = getJumpDestInCurrentScope("__try.__leave"); |
1638 | |
1639 | SEHTryEpilogueStack.push_back(&TryExit); |
1640 | |
1641 | llvm::BasicBlock *TryBB = nullptr; |
1642 | |
1643 | if (getLangOpts().EHAsynch) { |
1644 | EmitRuntimeCallOrInvoke(getSehTryBeginFn(CGM)); |
1645 | if (SEHTryEpilogueStack.size() == 1) |
1646 | TryBB = Builder.GetInsertBlock(); |
1647 | } |
1648 | |
1649 | EmitStmt(S.getTryBlock()); |
1650 | |
1651 | |
1652 | if (TryBB) { |
1653 | llvm::SmallPtrSet<llvm::BasicBlock *, 10> Visited; |
1654 | VolatilizeTryBlocks(TryBB, Visited); |
1655 | } |
1656 | |
1657 | SEHTryEpilogueStack.pop_back(); |
1658 | |
1659 | if (!TryExit.getBlock()->use_empty()) |
1660 | EmitBlock(TryExit.getBlock(), true); |
1661 | else |
1662 | delete TryExit.getBlock(); |
1663 | } |
1664 | ExitSEHTryStmt(S); |
1665 | } |
1666 | |
1667 | |
1668 | |
1669 | void CodeGenFunction::VolatilizeTryBlocks( |
1670 | llvm::BasicBlock *BB, llvm::SmallPtrSet<llvm::BasicBlock *, 10> &V) { |
1671 | if (BB == SEHTryEpilogueStack.back()->getBlock() || |
1672 | !V.insert(BB).second || |
1673 | !BB->getParent() || BB->empty()) |
1674 | return; |
1675 | |
1676 | if (!BB->isEHPad()) { |
1677 | for (llvm::BasicBlock::iterator J = BB->begin(), JE = BB->end(); J != JE; |
1678 | ++J) { |
1679 | if (auto LI = dyn_cast<llvm::LoadInst>(J)) { |
1680 | LI->setVolatile(true); |
1681 | } else if (auto SI = dyn_cast<llvm::StoreInst>(J)) { |
1682 | SI->setVolatile(true); |
1683 | } else if (auto* MCI = dyn_cast<llvm::MemIntrinsic>(J)) { |
1684 | MCI->setVolatile(llvm::ConstantInt::get(Builder.getInt1Ty(), 1)); |
1685 | } |
1686 | } |
1687 | } |
1688 | const llvm::Instruction *TI = BB->getTerminator(); |
1689 | if (TI) { |
1690 | unsigned N = TI->getNumSuccessors(); |
1691 | for (unsigned I = 0; I < N; I++) |
1692 | VolatilizeTryBlocks(TI->getSuccessor(I), V); |
1693 | } |
1694 | } |
1695 | |
1696 | namespace { |
1697 | struct PerformSEHFinally final : EHScopeStack::Cleanup { |
1698 | llvm::Function *OutlinedFinally; |
1699 | PerformSEHFinally(llvm::Function *OutlinedFinally) |
1700 | : OutlinedFinally(OutlinedFinally) {} |
1701 | |
1702 | void Emit(CodeGenFunction &CGF, Flags F) override { |
1703 | ASTContext &Context = CGF.getContext(); |
1704 | CodeGenModule &CGM = CGF.CGM; |
1705 | |
1706 | CallArgList Args; |
1707 | |
1708 | |
1709 | QualType ArgTys[2] = {Context.UnsignedCharTy, Context.VoidPtrTy}; |
1710 | llvm::Value *FP = nullptr; |
1711 | |
1712 | if (CGF.IsOutlinedSEHHelper) { |
1713 | FP = &CGF.CurFn->arg_begin()[1]; |
1714 | } else { |
1715 | llvm::Function *LocalAddrFn = |
1716 | CGM.getIntrinsic(llvm::Intrinsic::localaddress); |
1717 | FP = CGF.Builder.CreateCall(LocalAddrFn); |
1718 | } |
1719 | |
1720 | llvm::Value *IsForEH = |
1721 | llvm::ConstantInt::get(CGF.ConvertType(ArgTys[0]), F.isForEHCleanup()); |
1722 | |
1723 | |
1724 | |
1725 | |
1726 | |
1727 | |
1728 | if (!F.isForEHCleanup() && F.hasExitSwitch()) { |
1729 | Address Addr = CGF.getNormalCleanupDestSlot(); |
1730 | llvm::Value *Load = CGF.Builder.CreateLoad(Addr, "cleanup.dest"); |
1731 | llvm::Value *Zero = llvm::Constant::getNullValue(CGM.Int32Ty); |
1732 | IsForEH = CGF.Builder.CreateICmpNE(Load, Zero); |
1733 | } |
1734 | |
1735 | Args.add(RValue::get(IsForEH), ArgTys[0]); |
1736 | Args.add(RValue::get(FP), ArgTys[1]); |
1737 | |
1738 | |
1739 | const CGFunctionInfo &FnInfo = |
1740 | CGM.getTypes().arrangeBuiltinFunctionCall(Context.VoidTy, Args); |
1741 | |
1742 | auto Callee = CGCallee::forDirect(OutlinedFinally); |
1743 | CGF.EmitCall(FnInfo, Callee, ReturnValueSlot(), Args); |
1744 | } |
1745 | }; |
1746 | } |
1747 | |
1748 | namespace { |
1749 | |
1750 | struct CaptureFinder : ConstStmtVisitor<CaptureFinder> { |
1751 | CodeGenFunction &ParentCGF; |
1752 | const VarDecl *ParentThis; |
1753 | llvm::SmallSetVector<const VarDecl *, 4> Captures; |
1754 | Address SEHCodeSlot = Address::invalid(); |
1755 | CaptureFinder(CodeGenFunction &ParentCGF, const VarDecl *ParentThis) |
1756 | : ParentCGF(ParentCGF), ParentThis(ParentThis) {} |
1757 | |
1758 | |
1759 | bool foundCaptures() { |
1760 | return !Captures.empty() || SEHCodeSlot.isValid(); |
1761 | } |
1762 | |
1763 | void Visit(const Stmt *S) { |
1764 | |
1765 | ConstStmtVisitor<CaptureFinder>::Visit(S); |
1766 | for (const Stmt *Child : S->children()) |
1767 | if (Child) |
1768 | Visit(Child); |
1769 | } |
1770 | |
1771 | void VisitDeclRefExpr(const DeclRefExpr *E) { |
1772 | |
1773 | if (E->refersToEnclosingVariableOrCapture()) |
1774 | Captures.insert(ParentThis); |
1775 | |
1776 | const auto *D = dyn_cast<VarDecl>(E->getDecl()); |
1777 | if (D && D->isLocalVarDeclOrParm() && D->hasLocalStorage()) |
1778 | Captures.insert(D); |
1779 | } |
1780 | |
1781 | void VisitCXXThisExpr(const CXXThisExpr *E) { |
1782 | Captures.insert(ParentThis); |
1783 | } |
1784 | |
1785 | void VisitCallExpr(const CallExpr *E) { |
1786 | |
1787 | if (ParentCGF.getTarget().getTriple().getArch() != llvm::Triple::x86) |
1788 | return; |
1789 | |
1790 | unsigned ID = E->getBuiltinCallee(); |
1791 | switch (ID) { |
1792 | case Builtin::BI__exception_code: |
1793 | case Builtin::BI_exception_code: |
1794 | |
1795 | |
1796 | |
1797 | if (!SEHCodeSlot.isValid()) |
1798 | SEHCodeSlot = ParentCGF.SEHCodeSlotStack.back(); |
1799 | break; |
1800 | } |
1801 | } |
1802 | }; |
1803 | } |
1804 | |
1805 | Address CodeGenFunction::recoverAddrOfEscapedLocal(CodeGenFunction &ParentCGF, |
1806 | Address ParentVar, |
1807 | llvm::Value *ParentFP) { |
1808 | llvm::CallInst *RecoverCall = nullptr; |
1809 | CGBuilderTy Builder(*this, AllocaInsertPt); |
1810 | if (auto *ParentAlloca = dyn_cast<llvm::AllocaInst>(ParentVar.getPointer())) { |
1811 | |
1812 | |
1813 | auto InsertPair = ParentCGF.EscapedLocals.insert( |
1814 | std::make_pair(ParentAlloca, ParentCGF.EscapedLocals.size())); |
1815 | int FrameEscapeIdx = InsertPair.first->second; |
1816 | |
1817 | llvm::Function *FrameRecoverFn = llvm::Intrinsic::getDeclaration( |
1818 | &CGM.getModule(), llvm::Intrinsic::localrecover); |
1819 | llvm::Constant *ParentI8Fn = |
1820 | llvm::ConstantExpr::getBitCast(ParentCGF.CurFn, Int8PtrTy); |
1821 | RecoverCall = Builder.CreateCall( |
1822 | FrameRecoverFn, {ParentI8Fn, ParentFP, |
1823 | llvm::ConstantInt::get(Int32Ty, FrameEscapeIdx)}); |
1824 | |
1825 | } else { |
1826 | |
1827 | |
1828 | |
1829 | auto *ParentRecover = |
1830 | cast<llvm::IntrinsicInst>(ParentVar.getPointer()->stripPointerCasts()); |
1831 | assert(ParentRecover->getIntrinsicID() == llvm::Intrinsic::localrecover && |
1832 | "expected alloca or localrecover in parent LocalDeclMap"); |
1833 | RecoverCall = cast<llvm::CallInst>(ParentRecover->clone()); |
1834 | RecoverCall->setArgOperand(1, ParentFP); |
1835 | RecoverCall->insertBefore(AllocaInsertPt); |
1836 | } |
1837 | |
1838 | |
1839 | llvm::Value *ChildVar = |
1840 | Builder.CreateBitCast(RecoverCall, ParentVar.getType()); |
1841 | ChildVar->setName(ParentVar.getName()); |
1842 | return Address(ChildVar, ParentVar.getAlignment()); |
1843 | } |
1844 | |
1845 | void CodeGenFunction::EmitCapturedLocals(CodeGenFunction &ParentCGF, |
1846 | const Stmt *OutlinedStmt, |
1847 | bool IsFilter) { |
1848 | |
1849 | CaptureFinder Finder(ParentCGF, ParentCGF.CXXABIThisDecl); |
1850 | Finder.Visit(OutlinedStmt); |
1851 | |
1852 | |
1853 | |
1854 | if (!Finder.foundCaptures() && |
1855 | CGM.getTarget().getTriple().getArch() != llvm::Triple::x86) { |
1856 | if (IsFilter) |
1857 | EmitSEHExceptionCodeSave(ParentCGF, nullptr, nullptr); |
1858 | return; |
1859 | } |
1860 | |
1861 | llvm::Value *EntryFP = nullptr; |
1862 | CGBuilderTy Builder(CGM, AllocaInsertPt); |
1863 | if (IsFilter && CGM.getTarget().getTriple().getArch() == llvm::Triple::x86) { |
1864 | |
1865 | |
1866 | |
1867 | EntryFP = Builder.CreateCall( |
1868 | CGM.getIntrinsic(llvm::Intrinsic::frameaddress, AllocaInt8PtrTy), |
1869 | {Builder.getInt32(1)}); |
1870 | } else { |
1871 | |
1872 | |
1873 | auto AI = CurFn->arg_begin(); |
1874 | ++AI; |
1875 | EntryFP = &*AI; |
1876 | } |
1877 | |
1878 | llvm::Value *ParentFP = EntryFP; |
1879 | if (IsFilter) { |
1880 | |
1881 | |
1882 | |
1883 | llvm::Function *RecoverFPIntrin = |
1884 | CGM.getIntrinsic(llvm::Intrinsic::eh_recoverfp); |
1885 | llvm::Constant *ParentI8Fn = |
1886 | llvm::ConstantExpr::getBitCast(ParentCGF.CurFn, Int8PtrTy); |
1887 | ParentFP = Builder.CreateCall(RecoverFPIntrin, {ParentI8Fn, EntryFP}); |
1888 | |
1889 | |
1890 | |
1891 | |
1892 | |
1893 | |
1894 | |
1895 | if (ParentCGF.ParentCGF != nullptr) { |
1896 | |
1897 | |
1898 | |
1899 | llvm::AllocaInst *FramePtrAddrAlloca = nullptr; |
1900 | for (auto &I : ParentCGF.LocalDeclMap) { |
1901 | const VarDecl *D = cast<VarDecl>(I.first); |
1902 | if (isa<ImplicitParamDecl>(D) && |
1903 | D->getType() == getContext().VoidPtrTy) { |
1904 | assert(D->getName().startswith("frame_pointer")); |
1905 | FramePtrAddrAlloca = cast<llvm::AllocaInst>(I.second.getPointer()); |
1906 | break; |
1907 | } |
1908 | } |
1909 | assert(FramePtrAddrAlloca); |
1910 | auto InsertPair = ParentCGF.EscapedLocals.insert( |
1911 | std::make_pair(FramePtrAddrAlloca, ParentCGF.EscapedLocals.size())); |
1912 | int FrameEscapeIdx = InsertPair.first->second; |
1913 | |
1914 | |
1915 | |
1916 | |
1917 | |
1918 | |
1919 | |
1920 | llvm::Function *FrameRecoverFn = llvm::Intrinsic::getDeclaration( |
1921 | &CGM.getModule(), llvm::Intrinsic::localrecover); |
1922 | llvm::Constant *ParentI8Fn = |
1923 | llvm::ConstantExpr::getBitCast(ParentCGF.CurFn, Int8PtrTy); |
1924 | ParentFP = Builder.CreateCall( |
1925 | FrameRecoverFn, {ParentI8Fn, ParentFP, |
1926 | llvm::ConstantInt::get(Int32Ty, FrameEscapeIdx)}); |
1927 | ParentFP = Builder.CreateBitCast(ParentFP, CGM.VoidPtrPtrTy); |
1928 | ParentFP = Builder.CreateLoad(Address(ParentFP, getPointerAlign())); |
1929 | } |
1930 | } |
1931 | |
1932 | |
1933 | for (const VarDecl *VD : Finder.Captures) { |
1934 | if (VD->getType()->isVariablyModifiedType()) { |
1935 | CGM.ErrorUnsupported(VD, "VLA captured by SEH"); |
1936 | continue; |
1937 | } |
1938 | assert((isa<ImplicitParamDecl>(VD) || VD->isLocalVarDeclOrParm()) && |
1939 | "captured non-local variable"); |
1940 | |
1941 | auto L = ParentCGF.LambdaCaptureFields.find(VD); |
1942 | if (L != ParentCGF.LambdaCaptureFields.end()) { |
1943 | LambdaCaptureFields[VD] = L->second; |
1944 | continue; |
1945 | } |
1946 | |
1947 | |
1948 | |
1949 | auto I = ParentCGF.LocalDeclMap.find(VD); |
1950 | if (I == ParentCGF.LocalDeclMap.end()) |
1951 | continue; |
1952 | |
1953 | Address ParentVar = I->second; |
1954 | Address Recovered = |
1955 | recoverAddrOfEscapedLocal(ParentCGF, ParentVar, ParentFP); |
1956 | setAddrOfLocalVar(VD, Recovered); |
1957 | |
1958 | if (isa<ImplicitParamDecl>(VD)) { |
1959 | CXXABIThisAlignment = ParentCGF.CXXABIThisAlignment; |
1960 | CXXThisAlignment = ParentCGF.CXXThisAlignment; |
1961 | CXXABIThisValue = Builder.CreateLoad(Recovered, "this"); |
1962 | if (ParentCGF.LambdaThisCaptureField) { |
1963 | LambdaThisCaptureField = ParentCGF.LambdaThisCaptureField; |
1964 | |
1965 | |
1966 | LValue ThisFieldLValue = |
1967 | EmitLValueForLambdaField(LambdaThisCaptureField); |
1968 | if (!LambdaThisCaptureField->getType()->isPointerType()) { |
1969 | CXXThisValue = ThisFieldLValue.getAddress(*this).getPointer(); |
1970 | } else { |
1971 | CXXThisValue = EmitLoadOfLValue(ThisFieldLValue, SourceLocation()) |
1972 | .getScalarVal(); |
1973 | } |
1974 | } else { |
1975 | CXXThisValue = CXXABIThisValue; |
1976 | } |
1977 | } |
1978 | } |
1979 | |
1980 | if (Finder.SEHCodeSlot.isValid()) { |
1981 | SEHCodeSlotStack.push_back( |
1982 | recoverAddrOfEscapedLocal(ParentCGF, Finder.SEHCodeSlot, ParentFP)); |
1983 | } |
1984 | |
1985 | if (IsFilter) |
1986 | EmitSEHExceptionCodeSave(ParentCGF, ParentFP, EntryFP); |
1987 | } |
1988 | |
1989 | |
1990 | |
1991 | |
1992 | void CodeGenFunction::startOutlinedSEHHelper(CodeGenFunction &ParentCGF, |
1993 | bool IsFilter, |
1994 | const Stmt *OutlinedStmt) { |
1995 | SourceLocation StartLoc = OutlinedStmt->getBeginLoc(); |
1996 | |
1997 | |
1998 | SmallString<128> Name; |
1999 | { |
2000 | llvm::raw_svector_ostream OS(Name); |
2001 | const NamedDecl *ParentSEHFn = ParentCGF.CurSEHParent; |
2002 | assert(ParentSEHFn && "No CurSEHParent!"); |
2003 | MangleContext &Mangler = CGM.getCXXABI().getMangleContext(); |
2004 | if (IsFilter) |
2005 | Mangler.mangleSEHFilterExpression(ParentSEHFn, OS); |
2006 | else |
2007 | Mangler.mangleSEHFinallyBlock(ParentSEHFn, OS); |
2008 | } |
2009 | |
2010 | FunctionArgList Args; |
2011 | if (CGM.getTarget().getTriple().getArch() != llvm::Triple::x86 || !IsFilter) { |
2012 | |
2013 | |
2014 | if (IsFilter) { |
2015 | Args.push_back(ImplicitParamDecl::Create( |
2016 | getContext(), nullptr, StartLoc, |
2017 | &getContext().Idents.get("exception_pointers"), |
2018 | getContext().VoidPtrTy, ImplicitParamDecl::Other)); |
2019 | } else { |
2020 | Args.push_back(ImplicitParamDecl::Create( |
2021 | getContext(), nullptr, StartLoc, |
2022 | &getContext().Idents.get("abnormal_termination"), |
2023 | getContext().UnsignedCharTy, ImplicitParamDecl::Other)); |
2024 | } |
2025 | Args.push_back(ImplicitParamDecl::Create( |
2026 | getContext(), nullptr, StartLoc, |
2027 | &getContext().Idents.get("frame_pointer"), getContext().VoidPtrTy, |
2028 | ImplicitParamDecl::Other)); |
2029 | } |
2030 | |
2031 | QualType RetTy = IsFilter ? getContext().LongTy : getContext().VoidTy; |
2032 | |
2033 | const CGFunctionInfo &FnInfo = |
2034 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(RetTy, Args); |
2035 | |
2036 | llvm::FunctionType *FnTy = CGM.getTypes().GetFunctionType(FnInfo); |
2037 | llvm::Function *Fn = llvm::Function::Create( |
2038 | FnTy, llvm::GlobalValue::InternalLinkage, Name.str(), &CGM.getModule()); |
2039 | |
2040 | IsOutlinedSEHHelper = true; |
2041 | |
2042 | StartFunction(GlobalDecl(), RetTy, Fn, FnInfo, Args, |
2043 | OutlinedStmt->getBeginLoc(), OutlinedStmt->getBeginLoc()); |
2044 | CurSEHParent = ParentCGF.CurSEHParent; |
2045 | |
2046 | CGM.SetInternalFunctionAttributes(GlobalDecl(), CurFn, FnInfo); |
2047 | EmitCapturedLocals(ParentCGF, OutlinedStmt, IsFilter); |
2048 | } |
2049 | |
2050 | |
2051 | |
2052 | |
2053 | llvm::Function * |
2054 | CodeGenFunction::GenerateSEHFilterFunction(CodeGenFunction &ParentCGF, |
2055 | const SEHExceptStmt &Except) { |
2056 | const Expr *FilterExpr = Except.getFilterExpr(); |
2057 | startOutlinedSEHHelper(ParentCGF, true, FilterExpr); |
2058 | |
2059 | |
2060 | llvm::Value *R = EmitScalarExpr(FilterExpr); |
2061 | R = Builder.CreateIntCast(R, ConvertType(getContext().LongTy), |
2062 | FilterExpr->getType()->isSignedIntegerType()); |
2063 | Builder.CreateStore(R, ReturnValue); |
2064 | |
2065 | FinishFunction(FilterExpr->getEndLoc()); |
2066 | |
2067 | return CurFn; |
2068 | } |
2069 | |
2070 | llvm::Function * |
2071 | CodeGenFunction::GenerateSEHFinallyFunction(CodeGenFunction &ParentCGF, |
2072 | const SEHFinallyStmt &Finally) { |
2073 | const Stmt *FinallyBlock = Finally.getBlock(); |
2074 | startOutlinedSEHHelper(ParentCGF, false, FinallyBlock); |
2075 | |
2076 | |
2077 | EmitStmt(FinallyBlock); |
2078 | |
2079 | FinishFunction(FinallyBlock->getEndLoc()); |
2080 | |
2081 | return CurFn; |
2082 | } |
2083 | |
2084 | void CodeGenFunction::EmitSEHExceptionCodeSave(CodeGenFunction &ParentCGF, |
2085 | llvm::Value *ParentFP, |
2086 | llvm::Value *EntryFP) { |
2087 | |
2088 | |
2089 | if (CGM.getTarget().getTriple().getArch() != llvm::Triple::x86) { |
2090 | |
2091 | SEHInfo = &*CurFn->arg_begin(); |
2092 | SEHCodeSlotStack.push_back( |
2093 | CreateMemTemp(getContext().IntTy, "__exception_code")); |
2094 | } else { |
2095 | |
2096 | |
2097 | |
2098 | |
2099 | SEHInfo = Builder.CreateConstInBoundsGEP1_32(Int8Ty, EntryFP, -20); |
2100 | SEHInfo = Builder.CreateBitCast(SEHInfo, Int8PtrTy->getPointerTo()); |
2101 | SEHInfo = Builder.CreateAlignedLoad(Int8PtrTy, SEHInfo, getPointerAlign()); |
2102 | SEHCodeSlotStack.push_back(recoverAddrOfEscapedLocal( |
2103 | ParentCGF, ParentCGF.SEHCodeSlotStack.back(), ParentFP)); |
2104 | } |
2105 | |
2106 | |
2107 | |
2108 | |
2109 | |
2110 | |
2111 | |
2112 | |
2113 | llvm::Type *RecordTy = CGM.Int32Ty->getPointerTo(); |
2114 | llvm::Type *PtrsTy = llvm::StructType::get(RecordTy, CGM.VoidPtrTy); |
2115 | llvm::Value *Ptrs = Builder.CreateBitCast(SEHInfo, PtrsTy->getPointerTo()); |
2116 | llvm::Value *Rec = Builder.CreateStructGEP(PtrsTy, Ptrs, 0); |
2117 | Rec = Builder.CreateAlignedLoad(RecordTy, Rec, getPointerAlign()); |
2118 | llvm::Value *Code = Builder.CreateAlignedLoad(Int32Ty, Rec, getIntAlign()); |
2119 | assert(!SEHCodeSlotStack.empty() && "emitting EH code outside of __except"); |
2120 | Builder.CreateStore(Code, SEHCodeSlotStack.back()); |
2121 | } |
2122 | |
2123 | llvm::Value *CodeGenFunction::EmitSEHExceptionInfo() { |
2124 | |
2125 | |
2126 | if (!SEHInfo) |
2127 | return llvm::UndefValue::get(Int8PtrTy); |
2128 | assert(SEHInfo->getType() == Int8PtrTy); |
2129 | return SEHInfo; |
2130 | } |
2131 | |
2132 | llvm::Value *CodeGenFunction::EmitSEHExceptionCode() { |
2133 | assert(!SEHCodeSlotStack.empty() && "emitting EH code outside of __except"); |
2134 | return Builder.CreateLoad(SEHCodeSlotStack.back()); |
2135 | } |
2136 | |
2137 | llvm::Value *CodeGenFunction::EmitSEHAbnormalTermination() { |
2138 | |
2139 | |
2140 | auto AI = CurFn->arg_begin(); |
2141 | return Builder.CreateZExt(&*AI, Int32Ty); |
2142 | } |
2143 | |
2144 | void CodeGenFunction::pushSEHCleanup(CleanupKind Kind, |
2145 | llvm::Function *FinallyFunc) { |
2146 | EHStack.pushCleanup<PerformSEHFinally>(Kind, FinallyFunc); |
2147 | } |
2148 | |
2149 | void CodeGenFunction::EnterSEHTryStmt(const SEHTryStmt &S) { |
2150 | CodeGenFunction HelperCGF(CGM, true); |
2151 | HelperCGF.ParentCGF = this; |
2152 | if (const SEHFinallyStmt *Finally = S.getFinallyHandler()) { |
2153 | |
2154 | llvm::Function *FinallyFunc = |
2155 | HelperCGF.GenerateSEHFinallyFunction(*this, *Finally); |
2156 | |
2157 | |
2158 | EHStack.pushCleanup<PerformSEHFinally>(NormalAndEHCleanup, FinallyFunc); |
2159 | return; |
2160 | } |
2161 | |
2162 | |
2163 | const SEHExceptStmt *Except = S.getExceptHandler(); |
2164 | assert(Except); |
2165 | EHCatchScope *CatchScope = EHStack.pushCatch(1); |
2166 | SEHCodeSlotStack.push_back( |
2167 | CreateMemTemp(getContext().IntTy, "__exception_code")); |
2168 | |
2169 | |
2170 | |
2171 | |
2172 | llvm::Constant *C = |
2173 | ConstantEmitter(*this).tryEmitAbstract(Except->getFilterExpr(), |
2174 | getContext().IntTy); |
2175 | if (CGM.getTarget().getTriple().getArch() != llvm::Triple::x86 && C && |
2176 | C->isOneValue()) { |
2177 | CatchScope->setCatchAllHandler(0, createBasicBlock("__except")); |
2178 | return; |
2179 | } |
2180 | |
2181 | |
2182 | |
2183 | llvm::Function *FilterFunc = |
2184 | HelperCGF.GenerateSEHFilterFunction(*this, *Except); |
2185 | llvm::Constant *OpaqueFunc = |
2186 | llvm::ConstantExpr::getBitCast(FilterFunc, Int8PtrTy); |
2187 | CatchScope->setHandler(0, OpaqueFunc, createBasicBlock("__except.ret")); |
2188 | } |
2189 | |
2190 | void CodeGenFunction::ExitSEHTryStmt(const SEHTryStmt &S) { |
2191 | |
2192 | if (S.getFinallyHandler()) { |
2193 | PopCleanupBlock(); |
2194 | return; |
2195 | } |
2196 | |
2197 | |
2198 | if (getLangOpts().EHAsynch && Builder.GetInsertBlock()) { |
2199 | llvm::FunctionCallee SehTryEnd = getSehTryEndFn(CGM); |
2200 | EmitRuntimeCallOrInvoke(SehTryEnd); |
2201 | } |
2202 | |
2203 | |
2204 | const SEHExceptStmt *Except = S.getExceptHandler(); |
2205 | assert(Except && "__try must have __finally xor __except"); |
2206 | EHCatchScope &CatchScope = cast<EHCatchScope>(*EHStack.begin()); |
2207 | |
2208 | |
2209 | |
2210 | |
2211 | if (!CatchScope.hasEHBranches()) { |
2212 | CatchScope.clearHandlerBlocks(); |
2213 | EHStack.popCatch(); |
2214 | SEHCodeSlotStack.pop_back(); |
2215 | return; |
2216 | } |
2217 | |
2218 | |
2219 | llvm::BasicBlock *ContBB = createBasicBlock("__try.cont"); |
2220 | |
2221 | |
2222 | if (HaveInsertPoint()) |
2223 | Builder.CreateBr(ContBB); |
2224 | |
2225 | |
2226 | emitCatchDispatchBlock(*this, CatchScope); |
2227 | |
2228 | |
2229 | llvm::BasicBlock *CatchPadBB = CatchScope.getHandler(0).Block; |
2230 | EHStack.popCatch(); |
2231 | |
2232 | EmitBlockAfterUses(CatchPadBB); |
2233 | |
2234 | |
2235 | |
2236 | llvm::CatchPadInst *CPI = |
2237 | cast<llvm::CatchPadInst>(CatchPadBB->getFirstNonPHI()); |
2238 | llvm::BasicBlock *ExceptBB = createBasicBlock("__except"); |
2239 | Builder.CreateCatchRet(CPI, ExceptBB); |
2240 | EmitBlock(ExceptBB); |
2241 | |
2242 | |
2243 | if (CGM.getTarget().getTriple().getArch() != llvm::Triple::x86) { |
2244 | llvm::Function *SEHCodeIntrin = |
2245 | CGM.getIntrinsic(llvm::Intrinsic::eh_exceptioncode); |
2246 | llvm::Value *Code = Builder.CreateCall(SEHCodeIntrin, {CPI}); |
2247 | Builder.CreateStore(Code, SEHCodeSlotStack.back()); |
2248 | } |
2249 | |
2250 | |
2251 | EmitStmt(Except->getBlock()); |
2252 | |
2253 | |
2254 | SEHCodeSlotStack.pop_back(); |
2255 | |
2256 | if (HaveInsertPoint()) |
2257 | Builder.CreateBr(ContBB); |
2258 | |
2259 | EmitBlock(ContBB); |
2260 | } |
2261 | |
2262 | void CodeGenFunction::EmitSEHLeaveStmt(const SEHLeaveStmt &S) { |
2263 | |
2264 | |
2265 | |
2266 | if (HaveInsertPoint()) |
2267 | EmitStopPoint(&S); |
2268 | |
2269 | |
2270 | |
2271 | if (!isSEHTryScope()) { |
2272 | Builder.CreateUnreachable(); |
2273 | Builder.ClearInsertionPoint(); |
2274 | return; |
2275 | } |
2276 | |
2277 | EmitBranchThroughCleanup(*SEHTryEpilogueStack.back()); |
2278 | } |
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | #ifndef LLVM_CLANG_LIB_CODEGEN_CGCLEANUP_H |
14 | #define LLVM_CLANG_LIB_CODEGEN_CGCLEANUP_H |
15 | |
16 | #include "EHScopeStack.h" |
17 | |
18 | #include "Address.h" |
19 | #include "llvm/ADT/SmallPtrSet.h" |
20 | #include "llvm/ADT/SmallVector.h" |
21 | |
22 | namespace llvm { |
23 | class BasicBlock; |
24 | class Value; |
25 | class ConstantInt; |
26 | class AllocaInst; |
27 | } |
28 | |
29 | namespace clang { |
30 | class FunctionDecl; |
31 | namespace CodeGen { |
32 | class CodeGenModule; |
33 | class CodeGenFunction; |
34 | |
35 | |
36 | |
37 | struct CatchTypeInfo { |
38 | llvm::Constant *RTTI; |
39 | unsigned Flags; |
40 | }; |
41 | |
42 | |
43 | class EHScope { |
44 | llvm::BasicBlock *CachedLandingPad; |
45 | llvm::BasicBlock *CachedEHDispatchBlock; |
46 | |
47 | EHScopeStack::stable_iterator EnclosingEHScope; |
48 | |
49 | class CommonBitFields { |
50 | friend class EHScope; |
51 | unsigned Kind : 3; |
52 | }; |
53 | enum { NumCommonBits = 3 }; |
54 | |
55 | protected: |
56 | class CatchBitFields { |
57 | friend class EHCatchScope; |
58 | unsigned : NumCommonBits; |
59 | |
60 | unsigned NumHandlers : 32 - NumCommonBits; |
61 | }; |
62 | |
63 | class CleanupBitFields { |
64 | friend class EHCleanupScope; |
65 | unsigned : NumCommonBits; |
66 | |
67 | |
68 | unsigned IsNormalCleanup : 1; |
69 | |
70 | |
71 | unsigned IsEHCleanup : 1; |
72 | |
73 | |
74 | unsigned IsActive : 1; |
75 | |
76 | |
77 | unsigned IsLifetimeMarker : 1; |
78 | |
79 | |
80 | unsigned TestFlagInNormalCleanup : 1; |
81 | |
82 | |
83 | unsigned TestFlagInEHCleanup : 1; |
84 | |
85 | |
86 | |
87 | unsigned CleanupSize : 12; |
88 | }; |
89 | |
90 | class FilterBitFields { |
91 | friend class EHFilterScope; |
92 | unsigned : NumCommonBits; |
93 | |
94 | unsigned NumFilters : 32 - NumCommonBits; |
95 | }; |
96 | |
97 | union { |
98 | CommonBitFields CommonBits; |
99 | CatchBitFields CatchBits; |
100 | CleanupBitFields CleanupBits; |
101 | FilterBitFields FilterBits; |
102 | }; |
103 | |
104 | public: |
105 | enum Kind { Cleanup, Catch, Terminate, Filter }; |
106 | |
107 | EHScope(Kind kind, EHScopeStack::stable_iterator enclosingEHScope) |
108 | : CachedLandingPad(nullptr), CachedEHDispatchBlock(nullptr), |
109 | EnclosingEHScope(enclosingEHScope) { |
110 | CommonBits.Kind = kind; |
111 | } |
112 | |
113 | Kind getKind() const { return static_cast<Kind>(CommonBits.Kind); } |
114 | |
115 | llvm::BasicBlock *getCachedLandingPad() const { |
116 | return CachedLandingPad; |
117 | } |
118 | |
119 | void setCachedLandingPad(llvm::BasicBlock *block) { |
120 | CachedLandingPad = block; |
121 | } |
122 | |
123 | llvm::BasicBlock *getCachedEHDispatchBlock() const { |
124 | return CachedEHDispatchBlock; |
125 | } |
126 | |
127 | void setCachedEHDispatchBlock(llvm::BasicBlock *block) { |
128 | CachedEHDispatchBlock = block; |
129 | } |
130 | |
131 | bool hasEHBranches() const { |
132 | if (llvm::BasicBlock *block = getCachedEHDispatchBlock()) |
| 4 | | Assuming 'block' is non-null | |
|
| |
133 | return !block->use_empty(); |
| 6 | | Calling 'Value::use_empty' | |
|
| 9 | | Returning from 'Value::use_empty' | |
|
| 10 | | Returning the value 1, which participates in a condition later | |
|
134 | return false; |
135 | } |
136 | |
137 | EHScopeStack::stable_iterator getEnclosingEHScope() const { |
138 | return EnclosingEHScope; |
139 | } |
140 | }; |
141 | |
142 | |
143 | |
144 | |
145 | |
146 | |
147 | class EHCatchScope : public EHScope { |
148 | |
149 | |
150 | |
151 | |
152 | |
153 | public: |
154 | struct Handler { |
155 | |
156 | |
157 | CatchTypeInfo Type; |
158 | |
159 | |
160 | llvm::BasicBlock *Block; |
161 | |
162 | bool isCatchAll() const { return Type.RTTI == nullptr; } |
163 | }; |
164 | |
165 | private: |
166 | friend class EHScopeStack; |
167 | |
168 | Handler *getHandlers() { |
169 | return reinterpret_cast<Handler*>(this+1); |
170 | } |
171 | |
172 | const Handler *getHandlers() const { |
173 | return reinterpret_cast<const Handler*>(this+1); |
174 | } |
175 | |
176 | public: |
177 | static size_t getSizeForNumHandlers(unsigned N) { |
178 | return sizeof(EHCatchScope) + N * sizeof(Handler); |
179 | } |
180 | |
181 | EHCatchScope(unsigned numHandlers, |
182 | EHScopeStack::stable_iterator enclosingEHScope) |
183 | : EHScope(Catch, enclosingEHScope) { |
184 | CatchBits.NumHandlers = numHandlers; |
185 | assert(CatchBits.NumHandlers == numHandlers && "NumHandlers overflow?"); |
186 | } |
187 | |
188 | unsigned getNumHandlers() const { |
189 | return CatchBits.NumHandlers; |
190 | } |
191 | |
192 | void setCatchAllHandler(unsigned I, llvm::BasicBlock *Block) { |
193 | setHandler(I, CatchTypeInfo{nullptr, 0}, Block); |
194 | } |
195 | |
196 | void setHandler(unsigned I, llvm::Constant *Type, llvm::BasicBlock *Block) { |
197 | assert(I < getNumHandlers()); |
198 | getHandlers()[I].Type = CatchTypeInfo{Type, 0}; |
199 | getHandlers()[I].Block = Block; |
200 | } |
201 | |
202 | void setHandler(unsigned I, CatchTypeInfo Type, llvm::BasicBlock *Block) { |
203 | assert(I < getNumHandlers()); |
204 | getHandlers()[I].Type = Type; |
205 | getHandlers()[I].Block = Block; |
206 | } |
207 | |
208 | const Handler &getHandler(unsigned I) const { |
209 | assert(I < getNumHandlers()); |
210 | return getHandlers()[I]; |
211 | } |
212 | |
213 | |
214 | |
215 | |
216 | |
217 | void clearHandlerBlocks() { |
218 | for (unsigned I = 0, N = getNumHandlers(); I != N; ++I) |
219 | delete getHandler(I).Block; |
220 | } |
221 | |
222 | typedef const Handler *iterator; |
223 | iterator begin() const { return getHandlers(); } |
224 | iterator end() const { return getHandlers() + getNumHandlers(); } |
225 | |
226 | static bool classof(const EHScope *Scope) { |
227 | return Scope->getKind() == Catch; |
228 | } |
229 | }; |
230 | |
231 | |
232 | class alignas(8) EHCleanupScope : public EHScope { |
233 | |
234 | EHScopeStack::stable_iterator EnclosingNormal; |
235 | |
236 | |
237 | EHScopeStack::stable_iterator EnclosingEH; |
238 | |
239 | |
240 | |
241 | llvm::BasicBlock *NormalBlock; |
242 | |
243 | |
244 | |
245 | llvm::AllocaInst *ActiveFlag; |
246 | |
247 | |
248 | |
249 | |
250 | |
251 | struct ExtInfo { |
252 | |
253 | llvm::SmallPtrSet<llvm::BasicBlock*, 4> Branches; |
254 | |
255 | |
256 | SmallVector<std::pair<llvm::BasicBlock*,llvm::ConstantInt*>, 4> |
257 | BranchAfters; |
258 | }; |
259 | mutable struct ExtInfo *ExtInfo; |
260 | |
261 | |
262 | |
263 | |
264 | unsigned FixupDepth; |
265 | |
266 | struct ExtInfo &getExtInfo() { |
267 | if (!ExtInfo) ExtInfo = new struct ExtInfo(); |
268 | return *ExtInfo; |
269 | } |
270 | |
271 | const struct ExtInfo &getExtInfo() const { |
272 | if (!ExtInfo) ExtInfo = new struct ExtInfo(); |
273 | return *ExtInfo; |
274 | } |
275 | |
276 | public: |
277 | |
278 | |
279 | static size_t getSizeForCleanupSize(size_t Size) { |
280 | return sizeof(EHCleanupScope) + Size; |
281 | } |
282 | |
283 | size_t getAllocatedSize() const { |
284 | return sizeof(EHCleanupScope) + CleanupBits.CleanupSize; |
285 | } |
286 | |
287 | EHCleanupScope(bool isNormal, bool isEH, unsigned cleanupSize, |
288 | unsigned fixupDepth, |
289 | EHScopeStack::stable_iterator enclosingNormal, |
290 | EHScopeStack::stable_iterator enclosingEH) |
291 | : EHScope(EHScope::Cleanup, enclosingEH), |
292 | EnclosingNormal(enclosingNormal), NormalBlock(nullptr), |
293 | ActiveFlag(nullptr), ExtInfo(nullptr), FixupDepth(fixupDepth) { |
294 | CleanupBits.IsNormalCleanup = isNormal; |
295 | CleanupBits.IsEHCleanup = isEH; |
296 | CleanupBits.IsActive = true; |
297 | CleanupBits.IsLifetimeMarker = false; |
298 | CleanupBits.TestFlagInNormalCleanup = false; |
299 | CleanupBits.TestFlagInEHCleanup = false; |
300 | CleanupBits.CleanupSize = cleanupSize; |
301 | |
302 | assert(CleanupBits.CleanupSize == cleanupSize && "cleanup size overflow"); |
303 | } |
304 | |
305 | void Destroy() { |
306 | delete ExtInfo; |
307 | } |
308 | |
309 | ~EHCleanupScope() = delete; |
310 | |
311 | bool isNormalCleanup() const { return CleanupBits.IsNormalCleanup; } |
312 | llvm::BasicBlock *getNormalBlock() const { return NormalBlock; } |
313 | void setNormalBlock(llvm::BasicBlock *BB) { NormalBlock = BB; } |
314 | |
315 | bool isEHCleanup() const { return CleanupBits.IsEHCleanup; } |
316 | |
317 | bool isActive() const { return CleanupBits.IsActive; } |
318 | void setActive(bool A) { CleanupBits.IsActive = A; } |
319 | |
320 | bool isLifetimeMarker() const { return CleanupBits.IsLifetimeMarker; } |
321 | void setLifetimeMarker() { CleanupBits.IsLifetimeMarker = true; } |
322 | |
323 | bool hasActiveFlag() const { return ActiveFlag != nullptr; } |
324 | Address getActiveFlag() const { |
325 | return Address(ActiveFlag, CharUnits::One()); |
326 | } |
327 | void setActiveFlag(Address Var) { |
328 | assert(Var.getAlignment().isOne()); |
329 | ActiveFlag = cast<llvm::AllocaInst>(Var.getPointer()); |
330 | } |
331 | |
332 | void setTestFlagInNormalCleanup() { |
333 | CleanupBits.TestFlagInNormalCleanup = true; |
334 | } |
335 | bool shouldTestFlagInNormalCleanup() const { |
336 | return CleanupBits.TestFlagInNormalCleanup; |
337 | } |
338 | |
339 | void setTestFlagInEHCleanup() { |
340 | CleanupBits.TestFlagInEHCleanup = true; |
341 | } |
342 | bool shouldTestFlagInEHCleanup() const { |
343 | return CleanupBits.TestFlagInEHCleanup; |
344 | } |
345 | |
346 | unsigned getFixupDepth() const { return FixupDepth; } |
347 | EHScopeStack::stable_iterator getEnclosingNormalCleanup() const { |
348 | return EnclosingNormal; |
349 | } |
350 | |
351 | size_t getCleanupSize() const { return CleanupBits.CleanupSize; } |
352 | void *getCleanupBuffer() { return this + 1; } |
353 | |
354 | EHScopeStack::Cleanup *getCleanup() { |
355 | return reinterpret_cast<EHScopeStack::Cleanup*>(getCleanupBuffer()); |
356 | } |
357 | |
358 | |
359 | bool hasBranches() const { return ExtInfo && !ExtInfo->Branches.empty(); } |
360 | |
361 | |
362 | |
363 | |
364 | |
365 | |
366 | |
367 | |
368 | |
369 | |
370 | |
371 | |
372 | void addBranchAfter(llvm::ConstantInt *Index, |
373 | llvm::BasicBlock *Block) { |
374 | struct ExtInfo &ExtInfo = getExtInfo(); |
375 | if (ExtInfo.Branches.insert(Block).second) |
376 | ExtInfo.BranchAfters.push_back(std::make_pair(Block, Index)); |
377 | } |
378 | |
379 | |
380 | unsigned getNumBranchAfters() const { |
381 | return ExtInfo ? ExtInfo->BranchAfters.size() : 0; |
382 | } |
383 | |
384 | llvm::BasicBlock *getBranchAfterBlock(unsigned I) const { |
385 | assert(I < getNumBranchAfters()); |
386 | return ExtInfo->BranchAfters[I].first; |
387 | } |
388 | |
389 | llvm::ConstantInt *getBranchAfterIndex(unsigned I) const { |
390 | assert(I < getNumBranchAfters()); |
391 | return ExtInfo->BranchAfters[I].second; |
392 | } |
393 | |
394 | |
395 | |
396 | |
397 | |
398 | |
399 | |
400 | |
401 | |
402 | |
403 | |
404 | |
405 | |
406 | |
407 | |
408 | |
409 | bool addBranchThrough(llvm::BasicBlock *Block) { |
410 | return getExtInfo().Branches.insert(Block).second; |
411 | } |
412 | |
413 | |
414 | bool hasBranchThroughs() const { |
415 | if (!ExtInfo) return false; |
416 | return (ExtInfo->BranchAfters.size() != ExtInfo->Branches.size()); |
417 | } |
418 | |
419 | static bool classof(const EHScope *Scope) { |
420 | return (Scope->getKind() == Cleanup); |
421 | } |
422 | }; |
423 | |
424 | |
425 | |
426 | |
427 | |
428 | |
429 | static_assert(alignof(EHCleanupScope) == EHScopeStack::ScopeStackAlignment, |
430 | "EHCleanupScope expected alignment"); |
431 | |
432 | |
433 | |
434 | |
435 | |
436 | |
437 | class EHFilterScope : public EHScope { |
438 | |
439 | |
440 | |
441 | llvm::Value **getFilters() { |
442 | return reinterpret_cast<llvm::Value**>(this+1); |
443 | } |
444 | |
445 | llvm::Value * const *getFilters() const { |
446 | return reinterpret_cast<llvm::Value* const *>(this+1); |
447 | } |
448 | |
449 | public: |
450 | EHFilterScope(unsigned numFilters) |
451 | : EHScope(Filter, EHScopeStack::stable_end()) { |
452 | FilterBits.NumFilters = numFilters; |
453 | assert(FilterBits.NumFilters == numFilters && "NumFilters overflow"); |
454 | } |
455 | |
456 | static size_t getSizeForNumFilters(unsigned numFilters) { |
457 | return sizeof(EHFilterScope) + numFilters * sizeof(llvm::Value*); |
458 | } |
459 | |
460 | unsigned getNumFilters() const { return FilterBits.NumFilters; } |
461 | |
462 | void setFilter(unsigned i, llvm::Value *filterValue) { |
463 | assert(i < getNumFilters()); |
464 | getFilters()[i] = filterValue; |
465 | } |
466 | |
467 | llvm::Value *getFilter(unsigned i) const { |
468 | assert(i < getNumFilters()); |
469 | return getFilters()[i]; |
470 | } |
471 | |
472 | static bool classof(const EHScope *scope) { |
473 | return scope->getKind() == Filter; |
474 | } |
475 | }; |
476 | |
477 | |
478 | |
479 | class EHTerminateScope : public EHScope { |
480 | public: |
481 | EHTerminateScope(EHScopeStack::stable_iterator enclosingEHScope) |
482 | : EHScope(Terminate, enclosingEHScope) {} |
483 | static size_t getSize() { return sizeof(EHTerminateScope); } |
484 | |
485 | static bool classof(const EHScope *scope) { |
486 | return scope->getKind() == Terminate; |
487 | } |
488 | }; |
489 | |
490 | |
491 | class EHScopeStack::iterator { |
492 | char *Ptr; |
493 | |
494 | friend class EHScopeStack; |
495 | explicit iterator(char *Ptr) : Ptr(Ptr) {} |
496 | |
497 | public: |
498 | iterator() : Ptr(nullptr) {} |
499 | |
500 | EHScope *get() const { |
501 | return reinterpret_cast<EHScope*>(Ptr); |
502 | } |
503 | |
504 | EHScope *operator->() const { return get(); } |
505 | EHScope &operator*() const { return *get(); } |
506 | |
507 | iterator &operator++() { |
508 | size_t Size; |
509 | switch (get()->getKind()) { |
510 | case EHScope::Catch: |
511 | Size = EHCatchScope::getSizeForNumHandlers( |
512 | static_cast<const EHCatchScope *>(get())->getNumHandlers()); |
513 | break; |
514 | |
515 | case EHScope::Filter: |
516 | Size = EHFilterScope::getSizeForNumFilters( |
517 | static_cast<const EHFilterScope *>(get())->getNumFilters()); |
518 | break; |
519 | |
520 | case EHScope::Cleanup: |
521 | Size = static_cast<const EHCleanupScope *>(get())->getAllocatedSize(); |
522 | break; |
523 | |
524 | case EHScope::Terminate: |
525 | Size = EHTerminateScope::getSize(); |
526 | break; |
527 | } |
528 | Ptr += llvm::alignTo(Size, ScopeStackAlignment); |
529 | return *this; |
530 | } |
531 | |
532 | iterator next() { |
533 | iterator copy = *this; |
534 | ++copy; |
535 | return copy; |
536 | } |
537 | |
538 | iterator operator++(int) { |
539 | iterator copy = *this; |
540 | operator++(); |
541 | return copy; |
542 | } |
543 | |
544 | bool encloses(iterator other) const { return Ptr >= other.Ptr; } |
545 | bool strictlyEncloses(iterator other) const { return Ptr > other.Ptr; } |
546 | |
547 | bool operator==(iterator other) const { return Ptr == other.Ptr; } |
548 | bool operator!=(iterator other) const { return Ptr != other.Ptr; } |
549 | }; |
550 | |
551 | inline EHScopeStack::iterator EHScopeStack::begin() const { |
552 | return iterator(StartOfData); |
553 | } |
554 | |
555 | inline EHScopeStack::iterator EHScopeStack::end() const { |
556 | return iterator(EndOfBuffer); |
557 | } |
558 | |
559 | inline void EHScopeStack::popCatch() { |
560 | assert(!empty() && "popping exception stack when not empty"); |
561 | |
562 | EHCatchScope &scope = cast<EHCatchScope>(*begin()); |
563 | InnermostEHScope = scope.getEnclosingEHScope(); |
564 | deallocate(EHCatchScope::getSizeForNumHandlers(scope.getNumHandlers())); |
565 | } |
566 | |
567 | inline void EHScopeStack::popTerminate() { |
568 | assert(!empty() && "popping exception stack when not empty"); |
569 | |
570 | EHTerminateScope &scope = cast<EHTerminateScope>(*begin()); |
571 | InnermostEHScope = scope.getEnclosingEHScope(); |
572 | deallocate(EHTerminateScope::getSize()); |
573 | } |
574 | |
575 | inline EHScopeStack::iterator EHScopeStack::find(stable_iterator sp) const { |
576 | assert(sp.isValid() && "finding invalid savepoint"); |
577 | assert(sp.Size <= stable_begin().Size && "finding savepoint after pop"); |
578 | return iterator(EndOfBuffer - sp.Size); |
579 | } |
580 | |
581 | inline EHScopeStack::stable_iterator |
582 | EHScopeStack::stabilize(iterator ir) const { |
583 | assert(StartOfData <= ir.Ptr && ir.Ptr <= EndOfBuffer); |
584 | return stable_iterator(EndOfBuffer - ir.Ptr); |
585 | } |
586 | |
587 | |
588 | struct EHPersonality { |
589 | const char *PersonalityFn; |
590 | |
591 | |
592 | |
593 | |
594 | const char *CatchallRethrowFn; |
595 | |
596 | static const EHPersonality &get(CodeGenModule &CGM, const FunctionDecl *FD); |
597 | static const EHPersonality &get(CodeGenFunction &CGF); |
598 | |
599 | static const EHPersonality GNU_C; |
600 | static const EHPersonality GNU_C_SJLJ; |
601 | static const EHPersonality GNU_C_SEH; |
602 | static const EHPersonality GNU_ObjC; |
603 | static const EHPersonality GNU_ObjC_SJLJ; |
604 | static const EHPersonality GNU_ObjC_SEH; |
605 | static const EHPersonality GNUstep_ObjC; |
606 | static const EHPersonality GNU_ObjCXX; |
607 | static const EHPersonality NeXT_ObjC; |
608 | static const EHPersonality GNU_CPlusPlus; |
609 | static const EHPersonality GNU_CPlusPlus_SJLJ; |
610 | static const EHPersonality GNU_CPlusPlus_SEH; |
611 | static const EHPersonality MSVC_except_handler; |
612 | static const EHPersonality MSVC_C_specific_handler; |
613 | static const EHPersonality MSVC_CxxFrameHandler3; |
614 | static const EHPersonality GNU_Wasm_CPlusPlus; |
615 | static const EHPersonality XL_CPlusPlus; |
616 | |
617 | |
618 | |
619 | bool usesFuncletPads() const { |
620 | return isMSVCPersonality() || isWasmPersonality(); |
621 | } |
622 | |
623 | bool isMSVCPersonality() const { |
624 | return this == &MSVC_except_handler || this == &MSVC_C_specific_handler || |
625 | this == &MSVC_CxxFrameHandler3; |
626 | } |
627 | |
628 | bool isWasmPersonality() const { return this == &GNU_Wasm_CPlusPlus; } |
629 | |
630 | bool isMSVCXXPersonality() const { return this == &MSVC_CxxFrameHandler3; } |
631 | }; |
632 | } |
633 | } |
634 | |
635 | #endif |
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | #ifndef LLVM_IR_VALUE_H |
14 | #define LLVM_IR_VALUE_H |
15 | |
16 | #include "llvm-c/Types.h" |
17 | #include "llvm/ADT/STLExtras.h" |
18 | #include "llvm/ADT/StringRef.h" |
19 | #include "llvm/ADT/iterator_range.h" |
20 | #include "llvm/IR/Use.h" |
21 | #include "llvm/Support/Alignment.h" |
22 | #include "llvm/Support/CBindingWrapping.h" |
23 | #include "llvm/Support/Casting.h" |
24 | #include <cassert> |
25 | #include <iterator> |
26 | #include <memory> |
27 | |
28 | namespace llvm { |
29 | |
30 | class APInt; |
31 | class Argument; |
32 | class BasicBlock; |
33 | class Constant; |
34 | class ConstantData; |
35 | class ConstantAggregate; |
36 | class DataLayout; |
37 | class Function; |
38 | class GlobalAlias; |
39 | class GlobalIFunc; |
40 | class GlobalIndirectSymbol; |
41 | class GlobalObject; |
42 | class GlobalValue; |
43 | class GlobalVariable; |
44 | class InlineAsm; |
45 | class Instruction; |
46 | class LLVMContext; |
47 | class MDNode; |
48 | class Module; |
49 | class ModuleSlotTracker; |
50 | class raw_ostream; |
51 | template<typename ValueTy> class StringMapEntry; |
52 | class Twine; |
53 | class Type; |
54 | class User; |
55 | |
56 | using ValueName = StringMapEntry<Value *>; |
57 | |
58 | |
59 | |
60 | |
61 | |
62 | |
63 | |
64 | |
65 | |
66 | |
67 | |
68 | |
69 | |
70 | |
71 | |
72 | |
73 | |
74 | |
75 | class Value { |
76 | Type *VTy; |
77 | Use *UseList; |
78 | |
79 | friend class ValueAsMetadata; |
80 | friend class ValueHandleBase; |
81 | |
82 | const unsigned char SubclassID; |
83 | unsigned char HasValueHandle : 1; |
84 | |
85 | protected: |
86 | |
87 | |
88 | |
89 | |
90 | |
91 | unsigned char SubclassOptionalData : 7; |
92 | |
93 | private: |
94 | |
95 | |
96 | |
97 | |
98 | |
99 | unsigned short SubclassData; |
100 | |
101 | protected: |
102 | |
103 | |
104 | |
105 | |
106 | |
107 | |
108 | |
109 | |
110 | |
111 | |
112 | |
113 | |
114 | enum : unsigned { NumUserOperandsBits = 27 }; |
115 | unsigned NumUserOperands : NumUserOperandsBits; |
116 | |
117 | |
118 | unsigned IsUsedByMD : 1; |
119 | unsigned HasName : 1; |
120 | unsigned HasMetadata : 1; |
121 | unsigned HasHungOffUses : 1; |
122 | unsigned HasDescriptor : 1; |
123 | |
124 | private: |
125 | template <typename UseT> |
126 | class use_iterator_impl { |
127 | friend class Value; |
128 | |
129 | UseT *U; |
130 | |
131 | explicit use_iterator_impl(UseT *u) : U(u) {} |
132 | |
133 | public: |
134 | using iterator_category = std::forward_iterator_tag; |
135 | using value_type = UseT *; |
136 | using difference_type = std::ptrdiff_t; |
137 | using pointer = value_type *; |
138 | using reference = value_type &; |
139 | |
140 | use_iterator_impl() : U() {} |
141 | |
142 | bool operator==(const use_iterator_impl &x) const { return U == x.U; } |
143 | bool operator!=(const use_iterator_impl &x) const { return !operator==(x); } |
144 | |
145 | use_iterator_impl &operator++() { |
146 | assert(U && "Cannot increment end iterator!"); |
147 | U = U->getNext(); |
148 | return *this; |
149 | } |
150 | |
151 | use_iterator_impl operator++(int) { |
152 | auto tmp = *this; |
153 | ++*this; |
154 | return tmp; |
155 | } |
156 | |
157 | UseT &operator*() const { |
158 | assert(U && "Cannot dereference end iterator!"); |
159 | return *U; |
160 | } |
161 | |
162 | UseT *operator->() const { return &operator*(); } |
163 | |
164 | operator use_iterator_impl<const UseT>() const { |
165 | return use_iterator_impl<const UseT>(U); |
166 | } |
167 | }; |
168 | |
169 | template <typename UserTy> |
170 | class user_iterator_impl { |
171 | use_iterator_impl<Use> UI; |
172 | explicit user_iterator_impl(Use *U) : UI(U) {} |
173 | friend class Value; |
174 | |
175 | public: |
176 | using iterator_category = std::forward_iterator_tag; |
177 | using value_type = UserTy *; |
178 | using difference_type = std::ptrdiff_t; |
179 | using pointer = value_type *; |
180 | using reference = value_type &; |
181 | |
182 | user_iterator_impl() = default; |
183 | |
184 | bool operator==(const user_iterator_impl &x) const { return UI == x.UI; } |
185 | bool operator!=(const user_iterator_impl &x) const { return !operator==(x); } |
186 | |
187 | |
188 | bool atEnd() const { return *this == user_iterator_impl(); } |
189 | |
190 | user_iterator_impl &operator++() { |
191 | ++UI; |
192 | return *this; |
193 | } |
194 | |
195 | user_iterator_impl operator++(int) { |
196 | auto tmp = *this; |
197 | ++*this; |
198 | return tmp; |
199 | } |
200 | |
201 | |
202 | UserTy *operator*() const { |
203 | return UI->getUser(); |
204 | } |
205 | |
206 | UserTy *operator->() const { return operator*(); } |
207 | |
208 | operator user_iterator_impl<const UserTy>() const { |
209 | return user_iterator_impl<const UserTy>(*UI); |
210 | } |
211 | |
212 | Use &getUse() const { return *UI; } |
213 | }; |
214 | |
215 | protected: |
216 | Value(Type *Ty, unsigned scid); |
217 | |
218 | |
219 | |
220 | |
221 | |
222 | |
223 | ~Value(); |
224 | |
225 | public: |
226 | Value(const Value &) = delete; |
227 | Value &operator=(const Value &) = delete; |
228 | |
229 | |
230 | void deleteValue(); |
231 | |
232 | |
233 | void dump() const; |
234 | |
235 | |
236 | |
237 | void print(raw_ostream &O, bool IsForDebug = false) const; |
238 | void print(raw_ostream &O, ModuleSlotTracker &MST, |
239 | bool IsForDebug = false) const; |
240 | |
241 | |
242 | |
243 | |
244 | |
245 | |
246 | |
247 | |
248 | |
249 | void printAsOperand(raw_ostream &O, bool PrintType = true, |
250 | const Module *M = nullptr) const; |
251 | void printAsOperand(raw_ostream &O, bool PrintType, |
252 | ModuleSlotTracker &MST) const; |
253 | |
254 | |
255 | |
256 | Type *getType() const { return VTy; } |
257 | |
258 | |
259 | LLVMContext &getContext() const; |
260 | |
261 | |
262 | bool hasName() const { return HasName; } |
263 | ValueName *getValueName() const; |
264 | void setValueName(ValueName *VN); |
265 | |
266 | private: |
267 | void destroyValueName(); |
268 | enum class ReplaceMetadataUses { No, Yes }; |
269 | void doRAUW(Value *New, ReplaceMetadataUses); |
270 | void setNameImpl(const Twine &Name); |
271 | |
272 | public: |
273 | |
274 | |
275 | |
276 | |
277 | |
278 | StringRef getName() const; |
279 | |
280 | |
281 | |
282 | |
283 | |
284 | |
285 | void setName(const Twine &Name); |
286 | |
287 | |
288 | |
289 | |
290 | |
291 | |
292 | void takeName(Value *V); |
293 | |
294 | #ifndef NDEBUG |
295 | std::string getNameOrAsOperand() const; |
296 | #endif |
297 | |
298 | |
299 | |
300 | |
301 | |
302 | |
303 | void replaceAllUsesWith(Value *V); |
304 | |
305 | |
306 | |
307 | |
308 | |
309 | void replaceNonMetadataUsesWith(Value *V); |
310 | |
311 | |
312 | |
313 | |
314 | |
315 | void replaceUsesWithIf(Value *New, |
316 | llvm::function_ref<bool(Use &U)> ShouldReplace); |
317 | |
318 | |
319 | |
320 | |
321 | |
322 | |
323 | void replaceUsesOutsideBlock(Value *V, BasicBlock *BB); |
324 | |
325 | |
326 | |
327 | |
328 | |
329 | |
330 | |
331 | |
332 | |
333 | |
334 | |
335 | void assertModuleIsMaterializedImpl() const; |
336 | |
337 | |
338 | |
339 | void assertModuleIsMaterialized() const { |
340 | #ifndef NDEBUG |
341 | assertModuleIsMaterializedImpl(); |
342 | #endif |
343 | } |
344 | |
345 | bool use_empty() const { |
346 | assertModuleIsMaterialized(); |
347 | return UseList == nullptr; |
| 7 | | Assuming the condition is false | |
|
| 8 | | Returning zero, which participates in a condition later | |
|
348 | } |
349 | |
350 | bool materialized_use_empty() const { |
351 | return UseList == nullptr; |
352 | } |
353 | |
354 | using use_iterator = use_iterator_impl<Use>; |
355 | using const_use_iterator = use_iterator_impl<const Use>; |
356 | |
357 | use_iterator materialized_use_begin() { return use_iterator(UseList); } |
358 | const_use_iterator materialized_use_begin() const { |
359 | return const_use_iterator(UseList); |
360 | } |
361 | use_iterator use_begin() { |
362 | assertModuleIsMaterialized(); |
363 | return materialized_use_begin(); |
364 | } |
365 | const_use_iterator use_begin() const { |
366 | assertModuleIsMaterialized(); |
367 | return materialized_use_begin(); |
368 | } |
369 | use_iterator use_end() { return use_iterator(); } |
370 | const_use_iterator use_end() const { return const_use_iterator(); } |
371 | iterator_range<use_iterator> materialized_uses() { |
372 | return make_range(materialized_use_begin(), use_end()); |
373 | } |
374 | iterator_range<const_use_iterator> materialized_uses() const { |
375 | return make_range(materialized_use_begin(), use_end()); |
376 | } |
377 | iterator_range<use_iterator> uses() { |
378 | assertModuleIsMaterialized(); |
379 | return materialized_uses(); |
380 | } |
381 | iterator_range<const_use_iterator> uses() const { |
382 | assertModuleIsMaterialized(); |
383 | return materialized_uses(); |
384 | } |
385 | |
386 | bool user_empty() const { |
387 | assertModuleIsMaterialized(); |
388 | return UseList == nullptr; |
389 | } |
390 | |
391 | using user_iterator = user_iterator_impl<User>; |
392 | using const_user_iterator = user_iterator_impl<const User>; |
393 | |
394 | user_iterator materialized_user_begin() { return user_iterator(UseList); } |
395 | const_user_iterator materialized_user_begin() const { |
396 | return const_user_iterator(UseList); |
397 | } |
398 | user_iterator user_begin() { |
399 | assertModuleIsMaterialized(); |
400 | return materialized_user_begin(); |
401 | } |
402 | const_user_iterator user_begin() const { |
403 | assertModuleIsMaterialized(); |
404 | return materialized_user_begin(); |
405 | } |
406 | user_iterator user_end() { return user_iterator(); } |
407 | const_user_iterator user_end() const { return const_user_iterator(); } |
408 | User *user_back() { |
409 | assertModuleIsMaterialized(); |
410 | return *materialized_user_begin(); |
411 | } |
412 | const User *user_back() const { |
413 | assertModuleIsMaterialized(); |
414 | return *materialized_user_begin(); |
415 | } |
416 | iterator_range<user_iterator> materialized_users() { |
417 | return make_range(materialized_user_begin(), user_end()); |
418 | } |
419 | iterator_range<const_user_iterator> materialized_users() const { |
420 | return make_range(materialized_user_begin(), user_end()); |
421 | } |
422 | iterator_range<user_iterator> users() { |
423 | assertModuleIsMaterialized(); |
424 | return materialized_users(); |
425 | } |
426 | iterator_range<const_user_iterator> users() const { |
427 | assertModuleIsMaterialized(); |
428 | return materialized_users(); |
429 | } |
430 | |
431 | |
432 | |
433 | |
434 | |
435 | bool hasOneUse() const { return hasSingleElement(uses()); } |
436 | |
437 | |
438 | bool hasNUses(unsigned N) const; |
439 | |
440 | |
441 | |
442 | |
443 | bool hasNUsesOrMore(unsigned N) const; |
444 | |
445 | |
446 | |
447 | |
448 | |
449 | |
450 | |
451 | |
452 | |
453 | bool hasOneUser() const; |
454 | |
455 | |
456 | |
457 | |
458 | |
459 | |
460 | Use *getSingleUndroppableUse(); |
461 | const Use *getSingleUndroppableUse() const { |
462 | return const_cast<Value *>(this)->getSingleUndroppableUse(); |
463 | } |
464 | |
465 | |
466 | |
467 | |
468 | |
469 | bool hasNUndroppableUses(unsigned N) const; |
470 | |
471 | |
472 | |
473 | |
474 | bool hasNUndroppableUsesOrMore(unsigned N) const; |
475 | |
476 | |
477 | |
478 | |
479 | |
480 | |
481 | |
482 | |
483 | void dropDroppableUses(llvm::function_ref<bool(const Use *)> ShouldDrop = |
484 | [](const Use *) { return true; }); |
485 | |
486 | |
487 | void dropDroppableUsesIn(User &Usr); |
488 | |
489 | |
490 | static void dropDroppableUse(Use &U); |
491 | |
492 | |
493 | bool isUsedInBasicBlock(const BasicBlock *BB) const; |
494 | |
495 | |
496 | |
497 | |
498 | |
499 | unsigned getNumUses() const; |
500 | |
501 | |
502 | void addUse(Use &U) { U.addToList(&UseList); } |
503 | |
504 | |
505 | |
506 | |
507 | |
508 | |
509 | |
510 | enum ValueTy { |
511 | #define HANDLE_VALUE(Name) Name##Val, |
512 | #include "llvm/IR/Value.def" |
513 | |
514 | |
515 | #define HANDLE_CONSTANT_MARKER(Marker, Constant) Marker = Constant##Val, |
516 | #include "llvm/IR/Value.def" |
517 | }; |
518 | |
519 | |
520 | |
521 | |
522 | |
523 | |
524 | |
525 | |
526 | |
527 | |
528 | |
529 | unsigned getValueID() const { |
530 | return SubclassID; |
531 | } |
532 | |
533 | |
534 | |
535 | |
536 | unsigned getRawSubclassOptionalData() const { |
537 | return SubclassOptionalData; |
538 | } |
539 | |
540 | |
541 | void clearSubclassOptionalData() { |
542 | SubclassOptionalData = 0; |
543 | } |
544 | |
545 | |
546 | bool hasSameSubclassOptionalData(const Value *V) const { |
547 | return SubclassOptionalData == V->SubclassOptionalData; |
548 | } |
549 | |
550 | |
551 | bool hasValueHandle() const { return HasValueHandle; } |
552 | |
553 | |
554 | bool isUsedByMetadata() const { return IsUsedByMD; } |
555 | |
556 | |
557 | bool isTransitiveUsedByMetadataOnly() const; |
558 | |
559 | protected: |
560 | |
561 | |
562 | |
563 | |
564 | |
565 | MDNode *getMetadata(unsigned KindID) const; |
566 | MDNode *getMetadata(StringRef Kind) const; |
567 | |
568 | |
569 | |
570 | |
571 | |
572 | |
573 | void getMetadata(unsigned KindID, SmallVectorImpl<MDNode *> &MDs) const; |
574 | void getMetadata(StringRef Kind, SmallVectorImpl<MDNode *> &MDs) const; |
575 | |
576 | |
577 | |
578 | |
579 | |
580 | |
581 | void |
582 | getAllMetadata(SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs) const; |
583 | |
584 | |
585 | bool hasMetadata() const { return (bool)HasMetadata; } |
586 | |
587 | |
588 | |
589 | bool hasMetadata(unsigned KindID) const { |
590 | return getMetadata(KindID) != nullptr; |
591 | } |
592 | bool hasMetadata(StringRef Kind) const { |
593 | return getMetadata(Kind) != nullptr; |
594 | } |
595 | |
596 | |
597 | |
598 | |
599 | |
600 | |
601 | |
602 | void setMetadata(unsigned KindID, MDNode *Node); |
603 | void setMetadata(StringRef Kind, MDNode *Node); |
604 | |
605 | |
606 | |
607 | |
608 | void addMetadata(unsigned KindID, MDNode &MD); |
609 | void addMetadata(StringRef Kind, MDNode &MD); |
610 | |
611 | |
612 | |
613 | |
614 | |
615 | bool eraseMetadata(unsigned KindID); |
616 | |
617 | |
618 | void clearMetadata(); |
619 | |
620 | public: |
621 | |
622 | |
623 | |
624 | |
625 | bool isSwiftError() const; |
626 | |
627 | |
628 | |
629 | |
630 | |
631 | const Value *stripPointerCasts() const; |
632 | Value *stripPointerCasts() { |
633 | return const_cast<Value *>( |
634 | static_cast<const Value *>(this)->stripPointerCasts()); |
635 | } |
636 | |
637 | |
638 | |
639 | |
640 | |
641 | const Value *stripPointerCastsAndAliases() const; |
642 | Value *stripPointerCastsAndAliases() { |
643 | return const_cast<Value *>( |
644 | static_cast<const Value *>(this)->stripPointerCastsAndAliases()); |
645 | } |
646 | |
647 | |
648 | |
649 | |
650 | |
651 | |
652 | const Value *stripPointerCastsSameRepresentation() const; |
653 | Value *stripPointerCastsSameRepresentation() { |
654 | return const_cast<Value *>(static_cast<const Value *>(this) |
655 | ->stripPointerCastsSameRepresentation()); |
656 | } |
657 | |
658 | |
659 | |
660 | |
661 | |
662 | |
663 | |
664 | const Value *stripPointerCastsForAliasAnalysis() const; |
665 | Value *stripPointerCastsForAliasAnalysis() { |
666 | return const_cast<Value *>(static_cast<const Value *>(this) |
667 | ->stripPointerCastsForAliasAnalysis()); |
668 | } |
669 | |
670 | |
671 | |
672 | |
673 | |
674 | const Value *stripInBoundsConstantOffsets() const; |
675 | Value *stripInBoundsConstantOffsets() { |
676 | return const_cast<Value *>( |
677 | static_cast<const Value *>(this)->stripInBoundsConstantOffsets()); |
678 | } |
679 | |
680 | |
681 | |
682 | |
683 | |
684 | |
685 | |
686 | |
687 | |
688 | |
689 | |
690 | |
691 | |
692 | |
693 | |
694 | |
695 | |
696 | |
697 | |
698 | |
699 | |
700 | |
701 | |
702 | |
703 | |
704 | |
705 | |
706 | const Value *stripAndAccumulateConstantOffsets( |
707 | const DataLayout &DL, APInt &Offset, bool AllowNonInbounds, |
708 | function_ref<bool(Value &Value, APInt &Offset)> ExternalAnalysis = |
709 | nullptr) const; |
710 | Value *stripAndAccumulateConstantOffsets(const DataLayout &DL, APInt &Offset, |
711 | bool AllowNonInbounds) { |
712 | return const_cast<Value *>( |
713 | static_cast<const Value *>(this)->stripAndAccumulateConstantOffsets( |
714 | DL, Offset, AllowNonInbounds)); |
715 | } |
716 | |
717 | |
718 | |
719 | const Value *stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL, |
720 | APInt &Offset) const { |
721 | return stripAndAccumulateConstantOffsets(DL, Offset, |
722 | false); |
723 | } |
724 | Value *stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL, |
725 | APInt &Offset) { |
726 | return stripAndAccumulateConstantOffsets(DL, Offset, |
727 | false); |
728 | } |
729 | |
730 | |
731 | |
732 | |
733 | |
734 | const Value *stripInBoundsOffsets(function_ref<void(const Value *)> Func = |
735 | [](const Value *) {}) const; |
736 | inline Value *stripInBoundsOffsets(function_ref<void(const Value *)> Func = |
737 | [](const Value *) {}) { |
738 | return const_cast<Value *>( |
739 | static_cast<const Value *>(this)->stripInBoundsOffsets(Func)); |
740 | } |
741 | |
742 | |
743 | |
744 | |
745 | |
746 | bool canBeFreed() const; |
747 | |
748 | |
749 | |
750 | |
751 | |
752 | |
753 | |
754 | |
755 | |
756 | |
757 | uint64_t getPointerDereferenceableBytes(const DataLayout &DL, |
758 | bool &CanBeNull, |
759 | bool &CanBeFreed) const; |
760 | |
761 | |
762 | |
763 | |
764 | |
765 | Align getPointerAlignment(const DataLayout &DL) const; |
766 | |
767 | |
768 | |
769 | |
770 | |
771 | |
772 | |
773 | const Value *DoPHITranslation(const BasicBlock *CurBB, |
774 | const BasicBlock *PredBB) const; |
775 | Value *DoPHITranslation(const BasicBlock *CurBB, const BasicBlock *PredBB) { |
776 | return const_cast<Value *>( |
777 | static_cast<const Value *>(this)->DoPHITranslation(CurBB, PredBB)); |
778 | } |
779 | |
780 | |
781 | |
782 | |
783 | |
784 | static const unsigned MaxAlignmentExponent = 29; |
785 | static const unsigned MaximumAlignment = 1u << MaxAlignmentExponent; |
786 | |
787 | |
788 | |
789 | |
790 | |
791 | |
792 | |
793 | void mutateType(Type *Ty) { |
794 | VTy = Ty; |
795 | } |
796 | |
797 | |
798 | |
799 | |
800 | |
801 | template <class Compare> void sortUseList(Compare Cmp); |
802 | |
803 | |
804 | void reverseUseList(); |
805 | |
806 | private: |
807 | |
808 | |
809 | |
810 | |
811 | |
812 | |
813 | |
814 | |
815 | template <class Compare> |
816 | static Use *mergeUseLists(Use *L, Use *R, Compare Cmp) { |
817 | Use *Merged; |
818 | Use **Next = &Merged; |
819 | |
820 | while (true) { |
821 | if (!L) { |
822 | *Next = R; |
823 | break; |
824 | } |
825 | if (!R) { |
826 | *Next = L; |
827 | break; |
828 | } |
829 | if (Cmp(*R, *L)) { |
830 | *Next = R; |
831 | Next = &R->Next; |
832 | R = R->Next; |
833 | } else { |
834 | *Next = L; |
835 | Next = &L->Next; |
836 | L = L->Next; |
837 | } |
838 | } |
839 | |
840 | return Merged; |
841 | } |
842 | |
843 | protected: |
844 | unsigned short getSubclassDataFromValue() const { return SubclassData; } |
845 | void setValueSubclassData(unsigned short D) { SubclassData = D; } |
846 | }; |
847 | |
848 | struct ValueDeleter { void operator()(Value *V) { V->deleteValue(); } }; |
849 | |
850 | |
851 | |
852 | |
853 | using unique_value = std::unique_ptr<Value, ValueDeleter>; |
854 | |
855 | inline raw_ostream &operator<<(raw_ostream &OS, const Value &V) { |
856 | V.print(OS); |
857 | return OS; |
858 | } |
859 | |
860 | void Use::set(Value *V) { |
861 | if (Val) removeFromList(); |
862 | Val = V; |
863 | if (V) V->addUse(*this); |
864 | } |
865 | |
866 | Value *Use::operator=(Value *RHS) { |
867 | set(RHS); |
868 | return RHS; |
869 | } |
870 | |
871 | const Use &Use::operator=(const Use &RHS) { |
872 | set(RHS.Val); |
873 | return *this; |
874 | } |
875 | |
876 | template <class Compare> void Value::sortUseList(Compare Cmp) { |
877 | if (!UseList || !UseList->Next) |
878 | |
879 | return; |
880 | |
881 | |
882 | |
883 | |
884 | |
885 | |
886 | const unsigned MaxSlots = 32; |
887 | Use *Slots[MaxSlots]; |
888 | |
889 | |
890 | Use *Next = UseList->Next; |
891 | UseList->Next = nullptr; |
892 | unsigned NumSlots = 1; |
893 | Slots[0] = UseList; |
894 | |
895 | |
896 | while (Next->Next) { |
897 | Use *Current = Next; |
898 | Next = Current->Next; |
899 | |
900 | |
901 | Current->Next = nullptr; |
902 | |
903 | |
904 | unsigned I; |
905 | for (I = 0; I < NumSlots; ++I) { |
906 | if (!Slots[I]) |
907 | break; |
908 | |
909 | |
910 | |
911 | |
912 | |
913 | Current = mergeUseLists(Slots[I], Current, Cmp); |
914 | Slots[I] = nullptr; |
915 | } |
916 | |
917 | if (I == NumSlots) { |
918 | ++NumSlots; |
919 | assert(NumSlots <= MaxSlots && "Use list bigger than 2^32"); |
920 | } |
921 | |
922 | |
923 | Slots[I] = Current; |
924 | } |
925 | |
926 | |
927 | assert(Next && "Expected one more Use"); |
928 | assert(!Next->Next && "Expected only one Use"); |
929 | UseList = Next; |
930 | for (unsigned I = 0; I < NumSlots; ++I) |
931 | if (Slots[I]) |
932 | |
933 | |
934 | UseList = mergeUseLists(Slots[I], UseList, Cmp); |
935 | |
936 | |
937 | for (Use *I = UseList, **Prev = &UseList; I; I = I->Next) { |
938 | I->Prev = Prev; |
939 | Prev = &I->Next; |
940 | } |
941 | } |
942 | |
943 | |
944 | |
945 | |
946 | template <> struct isa_impl<Constant, Value> { |
947 | static inline bool doit(const Value &Val) { |
948 | static_assert(Value::ConstantFirstVal == 0, "Val.getValueID() >= Value::ConstantFirstVal"); |
949 | return Val.getValueID() <= Value::ConstantLastVal; |
950 | } |
951 | }; |
952 | |
953 | template <> struct isa_impl<ConstantData, Value> { |
954 | static inline bool doit(const Value &Val) { |
955 | return Val.getValueID() >= Value::ConstantDataFirstVal && |
956 | Val.getValueID() <= Value::ConstantDataLastVal; |
957 | } |
958 | }; |
959 | |
960 | template <> struct isa_impl<ConstantAggregate, Value> { |
961 | static inline bool doit(const Value &Val) { |
962 | return Val.getValueID() >= Value::ConstantAggregateFirstVal && |
963 | Val.getValueID() <= Value::ConstantAggregateLastVal; |
964 | } |
965 | }; |
966 | |
967 | template <> struct isa_impl<Argument, Value> { |
968 | static inline bool doit (const Value &Val) { |
969 | return Val.getValueID() == Value::ArgumentVal; |
970 | } |
971 | }; |
972 | |
973 | template <> struct isa_impl<InlineAsm, Value> { |
974 | static inline bool doit(const Value &Val) { |
975 | return Val.getValueID() == Value::InlineAsmVal; |
976 | } |
977 | }; |
978 | |
979 | template <> struct isa_impl<Instruction, Value> { |
980 | static inline bool doit(const Value &Val) { |
981 | return Val.getValueID() >= Value::InstructionVal; |
982 | } |
983 | }; |
984 | |
985 | template <> struct isa_impl<BasicBlock, Value> { |
986 | static inline bool doit(const Value &Val) { |
987 | return Val.getValueID() == Value::BasicBlockVal; |
988 | } |
989 | }; |
990 | |
991 | template <> struct isa_impl<Function, Value> { |
992 | static inline bool doit(const Value &Val) { |
993 | return Val.getValueID() == Value::FunctionVal; |
994 | } |
995 | }; |
996 | |
997 | template <> struct isa_impl<GlobalVariable, Value> { |
998 | static inline bool doit(const Value &Val) { |
999 | return Val.getValueID() == Value::GlobalVariableVal; |
1000 | } |
1001 | }; |
1002 | |
1003 | template <> struct isa_impl<GlobalAlias, Value> { |
1004 | static inline bool doit(const Value &Val) { |
1005 | return Val.getValueID() == Value::GlobalAliasVal; |
1006 | } |
1007 | }; |
1008 | |
1009 | template <> struct isa_impl<GlobalIFunc, Value> { |
1010 | static inline bool doit(const Value &Val) { |
1011 | return Val.getValueID() == Value::GlobalIFuncVal; |
1012 | } |
1013 | }; |
1014 | |
1015 | template <> struct isa_impl<GlobalIndirectSymbol, Value> { |
1016 | static inline bool doit(const Value &Val) { |
1017 | return isa<GlobalAlias>(Val) || isa<GlobalIFunc>(Val); |
1018 | } |
1019 | }; |
1020 | |
1021 | template <> struct isa_impl<GlobalValue, Value> { |
1022 | static inline bool doit(const Value &Val) { |
1023 | return isa<GlobalObject>(Val) || isa<GlobalIndirectSymbol>(Val); |
1024 | } |
1025 | }; |
1026 | |
1027 | template <> struct isa_impl<GlobalObject, Value> { |
1028 | static inline bool doit(const Value &Val) { |
1029 | return isa<GlobalVariable>(Val) || isa<Function>(Val); |
1030 | } |
1031 | }; |
1032 | |
1033 | |
1034 | DEFINE_ISA_CONVERSION_FUNCTIONS(Value, LLVMValueRef) |
1035 | |
1036 | |
1037 | inline Value **unwrap(LLVMValueRef *Vals) { |
1038 | return reinterpret_cast<Value**>(Vals); |
1039 | } |
1040 | |
1041 | template<typename T> |
1042 | inline T **unwrap(LLVMValueRef *Vals, unsigned Length) { |
1043 | #ifndef NDEBUG |
1044 | for (LLVMValueRef *I = Vals, *E = Vals + Length; I != E; ++I) |
1045 | unwrap<T>(*I); |
1046 | #endif |
1047 | (void)Length; |
1048 | return reinterpret_cast<T**>(Vals); |
1049 | } |
1050 | |
1051 | inline LLVMValueRef *wrap(const Value **Vals) { |
1052 | return reinterpret_cast<LLVMValueRef*>(const_cast<Value**>(Vals)); |
1053 | } |
1054 | |
1055 | } |
1056 | |
1057 | #endif // LLVM_IR_VALUE_H |