1 //===-- CodeGenFunction.h - Per-Function state for LLVM CodeGen -*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This is the internal per-function state used for llvm translation.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENFUNCTION_H
14 #define LLVM_CLANG_LIB_CODEGEN_CODEGENFUNCTION_H
16 #include "CGBuilder.h"
17 #include "CGDebugInfo.h"
18 #include "CGLoopInfo.h"
20 #include "CodeGenModule.h"
21 #include "CodeGenPGO.h"
22 #include "EHScopeStack.h"
23 #include "VarBypassDetector.h"
24 #include "clang/AST/CharUnits.h"
25 #include "clang/AST/CurrentSourceLocExprScope.h"
26 #include "clang/AST/ExprCXX.h"
27 #include "clang/AST/ExprObjC.h"
28 #include "clang/AST/ExprOpenMP.h"
29 #include "clang/AST/StmtOpenMP.h"
30 #include "clang/AST/Type.h"
31 #include "clang/Basic/ABI.h"
32 #include "clang/Basic/CapturedStmt.h"
33 #include "clang/Basic/CodeGenOptions.h"
34 #include "clang/Basic/OpenMPKinds.h"
35 #include "clang/Basic/TargetInfo.h"
36 #include "llvm/ADT/ArrayRef.h"
37 #include "llvm/ADT/DenseMap.h"
38 #include "llvm/ADT/MapVector.h"
39 #include "llvm/ADT/SmallVector.h"
40 #include "llvm/Frontend/OpenMP/OMPIRBuilder.h"
41 #include "llvm/IR/ValueHandle.h"
42 #include "llvm/Support/Debug.h"
43 #include "llvm/Transforms/Utils/SanitizerStats.h"
58 class CXXDestructorDecl;
59 class CXXForRangeStmt;
63 class EnumConstantDecl;
65 class FunctionProtoType;
67 class ObjCContainerDecl;
68 class ObjCInterfaceDecl;
71 class ObjCImplementationDecl;
72 class ObjCPropertyImplDecl;
75 class ObjCForCollectionStmt;
77 class ObjCAtThrowStmt;
78 class ObjCAtSynchronizedStmt;
79 class ObjCAutoreleasePoolStmt;
80 class OMPUseDevicePtrClause;
81 class OMPUseDeviceAddrClause;
82 class ReturnsNonNullAttr;
84 class OMPExecutableDirective;
86 namespace analyze_os_log {
87 class OSLogBufferLayout;
97 class BlockByrefHelpers;
100 class BlockFieldFlags;
101 class RegionCodeGenTy;
102 class TargetCodeGenInfo;
103 struct OMPTaskDataTy;
106 /// The kind of evaluation to perform on values of a particular
107 /// type. Basically, is the code in CGExprScalar, CGExprComplex, or
110 /// TODO: should vectors maybe be split out into their own thing?
111 enum TypeEvaluationKind {
117 #define LIST_SANITIZER_CHECKS \
118 SANITIZER_CHECK(AddOverflow, add_overflow, 0) \
119 SANITIZER_CHECK(BuiltinUnreachable, builtin_unreachable, 0) \
120 SANITIZER_CHECK(CFICheckFail, cfi_check_fail, 0) \
121 SANITIZER_CHECK(DivremOverflow, divrem_overflow, 0) \
122 SANITIZER_CHECK(DynamicTypeCacheMiss, dynamic_type_cache_miss, 0) \
123 SANITIZER_CHECK(FloatCastOverflow, float_cast_overflow, 0) \
124 SANITIZER_CHECK(FunctionTypeMismatch, function_type_mismatch, 1) \
125 SANITIZER_CHECK(ImplicitConversion, implicit_conversion, 0) \
126 SANITIZER_CHECK(InvalidBuiltin, invalid_builtin, 0) \
127 SANITIZER_CHECK(InvalidObjCCast, invalid_objc_cast, 0) \
128 SANITIZER_CHECK(LoadInvalidValue, load_invalid_value, 0) \
129 SANITIZER_CHECK(MissingReturn, missing_return, 0) \
130 SANITIZER_CHECK(MulOverflow, mul_overflow, 0) \
131 SANITIZER_CHECK(NegateOverflow, negate_overflow, 0) \
132 SANITIZER_CHECK(NullabilityArg, nullability_arg, 0) \
133 SANITIZER_CHECK(NullabilityReturn, nullability_return, 1) \
134 SANITIZER_CHECK(NonnullArg, nonnull_arg, 0) \
135 SANITIZER_CHECK(NonnullReturn, nonnull_return, 1) \
136 SANITIZER_CHECK(OutOfBounds, out_of_bounds, 0) \
137 SANITIZER_CHECK(PointerOverflow, pointer_overflow, 0) \
138 SANITIZER_CHECK(ShiftOutOfBounds, shift_out_of_bounds, 0) \
139 SANITIZER_CHECK(SubOverflow, sub_overflow, 0) \
140 SANITIZER_CHECK(TypeMismatch, type_mismatch, 1) \
141 SANITIZER_CHECK(AlignmentAssumption, alignment_assumption, 0) \
142 SANITIZER_CHECK(VLABoundNotPositive, vla_bound_not_positive, 0)
144 enum SanitizerHandler {
145 #define SANITIZER_CHECK(Enum, Name, Version) Enum,
146 LIST_SANITIZER_CHECKS
147 #undef SANITIZER_CHECK
150 /// Helper class with most of the code for saving a value for a
151 /// conditional expression cleanup.
152 struct DominatingLLVMValue {
153 typedef llvm::PointerIntPair<llvm::Value*, 1, bool> saved_type;
155 /// Answer whether the given value needs extra work to be saved.
156 static bool needsSaving(llvm::Value *value) {
157 // If it's not an instruction, we don't need to save.
158 if (!isa<llvm::Instruction>(value)) return false;
160 // If it's an instruction in the entry block, we don't need to save.
161 llvm::BasicBlock *block = cast<llvm::Instruction>(value)->getParent();
162 return (block != &block->getParent()->getEntryBlock());
165 static saved_type save(CodeGenFunction &CGF, llvm::Value *value);
166 static llvm::Value *restore(CodeGenFunction &CGF, saved_type value);
169 /// A partial specialization of DominatingValue for llvm::Values that
170 /// might be llvm::Instructions.
171 template <class T> struct DominatingPointer<T,true> : DominatingLLVMValue {
173 static type restore(CodeGenFunction &CGF, saved_type value) {
174 return static_cast<T*>(DominatingLLVMValue::restore(CGF, value));
178 /// A specialization of DominatingValue for Address.
179 template <> struct DominatingValue<Address> {
180 typedef Address type;
183 DominatingLLVMValue::saved_type SavedValue;
187 static bool needsSaving(type value) {
188 return DominatingLLVMValue::needsSaving(value.getPointer());
190 static saved_type save(CodeGenFunction &CGF, type value) {
191 return { DominatingLLVMValue::save(CGF, value.getPointer()),
192 value.getAlignment() };
194 static type restore(CodeGenFunction &CGF, saved_type value) {
195 return Address(DominatingLLVMValue::restore(CGF, value.SavedValue),
200 /// A specialization of DominatingValue for RValue.
201 template <> struct DominatingValue<RValue> {
204 enum Kind { ScalarLiteral, ScalarAddress, AggregateLiteral,
205 AggregateAddress, ComplexAddress };
210 saved_type(llvm::Value *v, Kind k, unsigned a = 0)
211 : Value(v), K(k), Align(a) {}
214 static bool needsSaving(RValue value);
215 static saved_type save(CodeGenFunction &CGF, RValue value);
216 RValue restore(CodeGenFunction &CGF);
218 // implementations in CGCleanup.cpp
221 static bool needsSaving(type value) {
222 return saved_type::needsSaving(value);
224 static saved_type save(CodeGenFunction &CGF, type value) {
225 return saved_type::save(CGF, value);
227 static type restore(CodeGenFunction &CGF, saved_type value) {
228 return value.restore(CGF);
232 /// CodeGenFunction - This class organizes the per-function state that is used
233 /// while generating LLVM code.
234 class CodeGenFunction : public CodeGenTypeCache {
235 CodeGenFunction(const CodeGenFunction &) = delete;
236 void operator=(const CodeGenFunction &) = delete;
238 friend class CGCXXABI;
240 /// A jump destination is an abstract label, branching to which may
241 /// require a jump out through normal cleanups.
243 JumpDest() : Block(nullptr), ScopeDepth(), Index(0) {}
244 JumpDest(llvm::BasicBlock *Block,
245 EHScopeStack::stable_iterator Depth,
247 : Block(Block), ScopeDepth(Depth), Index(Index) {}
249 bool isValid() const { return Block != nullptr; }
250 llvm::BasicBlock *getBlock() const { return Block; }
251 EHScopeStack::stable_iterator getScopeDepth() const { return ScopeDepth; }
252 unsigned getDestIndex() const { return Index; }
254 // This should be used cautiously.
255 void setScopeDepth(EHScopeStack::stable_iterator depth) {
260 llvm::BasicBlock *Block;
261 EHScopeStack::stable_iterator ScopeDepth;
265 CodeGenModule &CGM; // Per-module state.
266 const TargetInfo &Target;
268 // For EH/SEH outlined funclets, this field points to parent's CGF
269 CodeGenFunction *ParentCGF = nullptr;
271 typedef std::pair<llvm::Value *, llvm::Value *> ComplexPairTy;
272 LoopInfoStack LoopStack;
275 // Stores variables for which we can't generate correct lifetime markers
277 VarBypassDetector Bypasses;
279 // CodeGen lambda for loops and support for ordered clause
280 typedef llvm::function_ref<void(CodeGenFunction &, const OMPLoopDirective &,
283 typedef llvm::function_ref<void(CodeGenFunction &, SourceLocation,
284 const unsigned, const bool)>
287 // Codegen lambda for loop bounds in worksharing loop constructs
288 typedef llvm::function_ref<std::pair<LValue, LValue>(
289 CodeGenFunction &, const OMPExecutableDirective &S)>
292 // Codegen lambda for loop bounds in dispatch-based loop implementation
293 typedef llvm::function_ref<std::pair<llvm::Value *, llvm::Value *>(
294 CodeGenFunction &, const OMPExecutableDirective &S, Address LB,
296 CodeGenDispatchBoundsTy;
298 /// CGBuilder insert helper. This function is called after an
299 /// instruction is created using Builder.
300 void InsertHelper(llvm::Instruction *I, const llvm::Twine &Name,
301 llvm::BasicBlock *BB,
302 llvm::BasicBlock::iterator InsertPt) const;
304 /// CurFuncDecl - Holds the Decl for the current outermost
305 /// non-closure context.
306 const Decl *CurFuncDecl;
307 /// CurCodeDecl - This is the inner-most code context, which includes blocks.
308 const Decl *CurCodeDecl;
309 const CGFunctionInfo *CurFnInfo;
311 llvm::Function *CurFn = nullptr;
313 // Holds coroutine data if the current function is a coroutine. We use a
314 // wrapper to manage its lifetime, so that we don't have to define CGCoroData
317 std::unique_ptr<CGCoroData> Data;
323 bool isCoroutine() const {
324 return CurCoro.Data != nullptr;
327 /// CurGD - The GlobalDecl for the current function being compiled.
330 /// PrologueCleanupDepth - The cleanup depth enclosing all the
331 /// cleanups associated with the parameters.
332 EHScopeStack::stable_iterator PrologueCleanupDepth;
334 /// ReturnBlock - Unified return block.
335 JumpDest ReturnBlock;
337 /// ReturnValue - The temporary alloca to hold the return
338 /// value. This is invalid iff the function has no return value.
339 Address ReturnValue = Address::invalid();
341 /// ReturnValuePointer - The temporary alloca to hold a pointer to sret.
342 /// This is invalid if sret is not in use.
343 Address ReturnValuePointer = Address::invalid();
345 /// If a return statement is being visited, this holds the return statment's
346 /// result expression.
347 const Expr *RetExpr = nullptr;
349 /// Return true if a label was seen in the current scope.
350 bool hasLabelBeenSeenInCurrentScope() const {
352 return CurLexicalScope->hasLabels();
353 return !LabelMap.empty();
356 /// AllocaInsertPoint - This is an instruction in the entry block before which
357 /// we prefer to insert allocas.
358 llvm::AssertingVH<llvm::Instruction> AllocaInsertPt;
360 /// API for captured statement code generation.
361 class CGCapturedStmtInfo {
363 explicit CGCapturedStmtInfo(CapturedRegionKind K = CR_Default)
364 : Kind(K), ThisValue(nullptr), CXXThisFieldDecl(nullptr) {}
365 explicit CGCapturedStmtInfo(const CapturedStmt &S,
366 CapturedRegionKind K = CR_Default)
367 : Kind(K), ThisValue(nullptr), CXXThisFieldDecl(nullptr) {
369 RecordDecl::field_iterator Field =
370 S.getCapturedRecordDecl()->field_begin();
371 for (CapturedStmt::const_capture_iterator I = S.capture_begin(),
373 I != E; ++I, ++Field) {
374 if (I->capturesThis())
375 CXXThisFieldDecl = *Field;
376 else if (I->capturesVariable())
377 CaptureFields[I->getCapturedVar()->getCanonicalDecl()] = *Field;
378 else if (I->capturesVariableByCopy())
379 CaptureFields[I->getCapturedVar()->getCanonicalDecl()] = *Field;
383 virtual ~CGCapturedStmtInfo();
385 CapturedRegionKind getKind() const { return Kind; }
387 virtual void setContextValue(llvm::Value *V) { ThisValue = V; }
388 // Retrieve the value of the context parameter.
389 virtual llvm::Value *getContextValue() const { return ThisValue; }
391 /// Lookup the captured field decl for a variable.
392 virtual const FieldDecl *lookup(const VarDecl *VD) const {
393 return CaptureFields.lookup(VD->getCanonicalDecl());
396 bool isCXXThisExprCaptured() const { return getThisFieldDecl() != nullptr; }
397 virtual FieldDecl *getThisFieldDecl() const { return CXXThisFieldDecl; }
399 static bool classof(const CGCapturedStmtInfo *) {
403 /// Emit the captured statement body.
404 virtual void EmitBody(CodeGenFunction &CGF, const Stmt *S) {
405 CGF.incrementProfileCounter(S);
409 /// Get the name of the capture helper.
410 virtual StringRef getHelperName() const { return "__captured_stmt"; }
413 /// The kind of captured statement being generated.
414 CapturedRegionKind Kind;
416 /// Keep the map between VarDecl and FieldDecl.
417 llvm::SmallDenseMap<const VarDecl *, FieldDecl *> CaptureFields;
419 /// The base address of the captured record, passed in as the first
420 /// argument of the parallel region function.
421 llvm::Value *ThisValue;
423 /// Captured 'this' type.
424 FieldDecl *CXXThisFieldDecl;
426 CGCapturedStmtInfo *CapturedStmtInfo = nullptr;
428 /// RAII for correct setting/restoring of CapturedStmtInfo.
429 class CGCapturedStmtRAII {
431 CodeGenFunction &CGF;
432 CGCapturedStmtInfo *PrevCapturedStmtInfo;
434 CGCapturedStmtRAII(CodeGenFunction &CGF,
435 CGCapturedStmtInfo *NewCapturedStmtInfo)
436 : CGF(CGF), PrevCapturedStmtInfo(CGF.CapturedStmtInfo) {
437 CGF.CapturedStmtInfo = NewCapturedStmtInfo;
439 ~CGCapturedStmtRAII() { CGF.CapturedStmtInfo = PrevCapturedStmtInfo; }
442 /// An abstract representation of regular/ObjC call/message targets.
443 class AbstractCallee {
444 /// The function declaration of the callee.
445 const Decl *CalleeDecl;
448 AbstractCallee() : CalleeDecl(nullptr) {}
449 AbstractCallee(const FunctionDecl *FD) : CalleeDecl(FD) {}
450 AbstractCallee(const ObjCMethodDecl *OMD) : CalleeDecl(OMD) {}
451 bool hasFunctionDecl() const {
452 return dyn_cast_or_null<FunctionDecl>(CalleeDecl);
454 const Decl *getDecl() const { return CalleeDecl; }
455 unsigned getNumParams() const {
456 if (const auto *FD = dyn_cast<FunctionDecl>(CalleeDecl))
457 return FD->getNumParams();
458 return cast<ObjCMethodDecl>(CalleeDecl)->param_size();
460 const ParmVarDecl *getParamDecl(unsigned I) const {
461 if (const auto *FD = dyn_cast<FunctionDecl>(CalleeDecl))
462 return FD->getParamDecl(I);
463 return *(cast<ObjCMethodDecl>(CalleeDecl)->param_begin() + I);
467 /// Sanitizers enabled for this function.
468 SanitizerSet SanOpts;
470 /// True if CodeGen currently emits code implementing sanitizer checks.
471 bool IsSanitizerScope = false;
473 /// RAII object to set/unset CodeGenFunction::IsSanitizerScope.
474 class SanitizerScope {
475 CodeGenFunction *CGF;
477 SanitizerScope(CodeGenFunction *CGF);
481 /// In C++, whether we are code generating a thunk. This controls whether we
482 /// should emit cleanups.
483 bool CurFuncIsThunk = false;
485 /// In ARC, whether we should autorelease the return value.
486 bool AutoreleaseResult = false;
488 /// Whether we processed a Microsoft-style asm block during CodeGen. These can
489 /// potentially set the return value.
490 bool SawAsmBlock = false;
492 const NamedDecl *CurSEHParent = nullptr;
494 /// True if the current function is an outlined SEH helper. This can be a
495 /// finally block or filter expression.
496 bool IsOutlinedSEHHelper = false;
498 /// True if CodeGen currently emits code inside presereved access index
500 bool IsInPreservedAIRegion = false;
502 /// True if the current statement has nomerge attribute.
503 bool InNoMergeAttributedStmt = false;
505 /// True if the current function should be marked mustprogress.
506 bool FnIsMustProgress = false;
508 /// True if the C++ Standard Requires Progress.
509 bool CPlusPlusWithProgress() {
510 return getLangOpts().CPlusPlus11 || getLangOpts().CPlusPlus14 ||
511 getLangOpts().CPlusPlus17 || getLangOpts().CPlusPlus20;
514 /// True if the C Standard Requires Progress.
515 bool CWithProgress() {
516 return getLangOpts().C11 || getLangOpts().C17 || getLangOpts().C2x;
519 /// True if the language standard requires progress in functions or
520 /// in infinite loops with non-constant conditionals.
521 bool LanguageRequiresProgress() {
522 return CWithProgress() || CPlusPlusWithProgress();
525 const CodeGen::CGBlockInfo *BlockInfo = nullptr;
526 llvm::Value *BlockPointer = nullptr;
528 llvm::DenseMap<const VarDecl *, FieldDecl *> LambdaCaptureFields;
529 FieldDecl *LambdaThisCaptureField = nullptr;
531 /// A mapping from NRVO variables to the flags used to indicate
532 /// when the NRVO has been applied to this variable.
533 llvm::DenseMap<const VarDecl *, llvm::Value *> NRVOFlags;
535 EHScopeStack EHStack;
536 llvm::SmallVector<char, 256> LifetimeExtendedCleanupStack;
537 llvm::SmallVector<const JumpDest *, 2> SEHTryEpilogueStack;
539 llvm::Instruction *CurrentFuncletPad = nullptr;
541 class CallLifetimeEnd final : public EHScopeStack::Cleanup {
546 CallLifetimeEnd(Address addr, llvm::Value *size)
547 : Addr(addr.getPointer()), Size(size) {}
549 void Emit(CodeGenFunction &CGF, Flags flags) override {
550 CGF.EmitLifetimeEnd(Size, Addr);
554 /// Header for data within LifetimeExtendedCleanupStack.
555 struct LifetimeExtendedCleanupHeader {
556 /// The size of the following cleanup object.
558 /// The kind of cleanup to push: a value from the CleanupKind enumeration.
560 /// Whether this is a conditional cleanup.
561 unsigned IsConditional : 1;
563 size_t getSize() const { return Size; }
564 CleanupKind getKind() const { return (CleanupKind)Kind; }
565 bool isConditional() const { return IsConditional; }
568 /// i32s containing the indexes of the cleanup destinations.
569 Address NormalCleanupDest = Address::invalid();
571 unsigned NextCleanupDestIndex = 1;
573 /// EHResumeBlock - Unified block containing a call to llvm.eh.resume.
574 llvm::BasicBlock *EHResumeBlock = nullptr;
576 /// The exception slot. All landing pads write the current exception pointer
577 /// into this alloca.
578 llvm::Value *ExceptionSlot = nullptr;
580 /// The selector slot. Under the MandatoryCleanup model, all landing pads
581 /// write the current selector value into this alloca.
582 llvm::AllocaInst *EHSelectorSlot = nullptr;
584 /// A stack of exception code slots. Entering an __except block pushes a slot
585 /// on the stack and leaving pops one. The __exception_code() intrinsic loads
586 /// a value from the top of the stack.
587 SmallVector<Address, 1> SEHCodeSlotStack;
589 /// Value returned by __exception_info intrinsic.
590 llvm::Value *SEHInfo = nullptr;
592 /// Emits a landing pad for the current EH stack.
593 llvm::BasicBlock *EmitLandingPad();
595 llvm::BasicBlock *getInvokeDestImpl();
597 /// Parent loop-based directive for scan directive.
598 const OMPExecutableDirective *OMPParentLoopDirectiveForScan = nullptr;
599 llvm::BasicBlock *OMPBeforeScanBlock = nullptr;
600 llvm::BasicBlock *OMPAfterScanBlock = nullptr;
601 llvm::BasicBlock *OMPScanExitBlock = nullptr;
602 llvm::BasicBlock *OMPScanDispatch = nullptr;
603 bool OMPFirstScanLoop = false;
605 /// Manages parent directive for scan directives.
606 class ParentLoopDirectiveForScanRegion {
607 CodeGenFunction &CGF;
608 const OMPExecutableDirective *ParentLoopDirectiveForScan;
611 ParentLoopDirectiveForScanRegion(
612 CodeGenFunction &CGF,
613 const OMPExecutableDirective &ParentLoopDirectiveForScan)
615 ParentLoopDirectiveForScan(CGF.OMPParentLoopDirectiveForScan) {
616 CGF.OMPParentLoopDirectiveForScan = &ParentLoopDirectiveForScan;
618 ~ParentLoopDirectiveForScanRegion() {
619 CGF.OMPParentLoopDirectiveForScan = ParentLoopDirectiveForScan;
624 typename DominatingValue<T>::saved_type saveValueInCond(T value) {
625 return DominatingValue<T>::save(*this, value);
628 class CGFPOptionsRAII {
630 CGFPOptionsRAII(CodeGenFunction &CGF, FPOptions FPFeatures);
631 CGFPOptionsRAII(CodeGenFunction &CGF, const Expr *E);
635 void ConstructorHelper(FPOptions FPFeatures);
636 CodeGenFunction &CGF;
637 FPOptions OldFPFeatures;
638 llvm::fp::ExceptionBehavior OldExcept;
639 llvm::RoundingMode OldRounding;
640 Optional<CGBuilderTy::FastMathFlagGuard> FMFGuard;
642 FPOptions CurFPFeatures;
645 /// ObjCEHValueStack - Stack of Objective-C exception values, used for
647 SmallVector<llvm::Value*, 8> ObjCEHValueStack;
649 /// A class controlling the emission of a finally block.
651 /// Where the catchall's edge through the cleanup should go.
652 JumpDest RethrowDest;
654 /// A function to call to enter the catch.
655 llvm::FunctionCallee BeginCatchFn;
657 /// An i1 variable indicating whether or not the @finally is
658 /// running for an exception.
659 llvm::AllocaInst *ForEHVar;
661 /// An i8* variable into which the exception pointer to rethrow
663 llvm::AllocaInst *SavedExnVar;
666 void enter(CodeGenFunction &CGF, const Stmt *Finally,
667 llvm::FunctionCallee beginCatchFn,
668 llvm::FunctionCallee endCatchFn, llvm::FunctionCallee rethrowFn);
669 void exit(CodeGenFunction &CGF);
672 /// Returns true inside SEH __try blocks.
673 bool isSEHTryScope() const { return !SEHTryEpilogueStack.empty(); }
675 /// Returns true while emitting a cleanuppad.
676 bool isCleanupPadScope() const {
677 return CurrentFuncletPad && isa<llvm::CleanupPadInst>(CurrentFuncletPad);
680 /// pushFullExprCleanup - Push a cleanup to be run at the end of the
681 /// current full-expression. Safe against the possibility that
682 /// we're currently inside a conditionally-evaluated expression.
683 template <class T, class... As>
684 void pushFullExprCleanup(CleanupKind kind, As... A) {
685 // If we're not in a conditional branch, or if none of the
686 // arguments requires saving, then use the unconditional cleanup.
687 if (!isInConditionalBranch())
688 return EHStack.pushCleanup<T>(kind, A...);
690 // Stash values in a tuple so we can guarantee the order of saves.
691 typedef std::tuple<typename DominatingValue<As>::saved_type...> SavedTuple;
692 SavedTuple Saved{saveValueInCond(A)...};
694 typedef EHScopeStack::ConditionalCleanup<T, As...> CleanupType;
695 EHStack.pushCleanupTuple<CleanupType>(kind, Saved);
696 initFullExprCleanup();
699 /// Queue a cleanup to be pushed after finishing the current full-expression,
700 /// potentially with an active flag.
701 template <class T, class... As>
702 void pushCleanupAfterFullExpr(CleanupKind Kind, As... A) {
703 if (!isInConditionalBranch())
704 return pushCleanupAfterFullExprWithActiveFlag<T>(Kind, Address::invalid(),
707 Address ActiveFlag = createCleanupActiveFlag();
708 assert(!DominatingValue<Address>::needsSaving(ActiveFlag) &&
709 "cleanup active flag should never need saving");
711 typedef std::tuple<typename DominatingValue<As>::saved_type...> SavedTuple;
712 SavedTuple Saved{saveValueInCond(A)...};
714 typedef EHScopeStack::ConditionalCleanup<T, As...> CleanupType;
715 pushCleanupAfterFullExprWithActiveFlag<CleanupType>(Kind, ActiveFlag, Saved);
718 template <class T, class... As>
719 void pushCleanupAfterFullExprWithActiveFlag(CleanupKind Kind,
720 Address ActiveFlag, As... A) {
721 LifetimeExtendedCleanupHeader Header = {sizeof(T), Kind,
722 ActiveFlag.isValid()};
724 size_t OldSize = LifetimeExtendedCleanupStack.size();
725 LifetimeExtendedCleanupStack.resize(
726 LifetimeExtendedCleanupStack.size() + sizeof(Header) + Header.Size +
727 (Header.IsConditional ? sizeof(ActiveFlag) : 0));
729 static_assert(sizeof(Header) % alignof(T) == 0,
730 "Cleanup will be allocated on misaligned address");
731 char *Buffer = &LifetimeExtendedCleanupStack[OldSize];
732 new (Buffer) LifetimeExtendedCleanupHeader(Header);
733 new (Buffer + sizeof(Header)) T(A...);
734 if (Header.IsConditional)
735 new (Buffer + sizeof(Header) + sizeof(T)) Address(ActiveFlag);
738 /// Set up the last cleanup that was pushed as a conditional
739 /// full-expression cleanup.
740 void initFullExprCleanup() {
741 initFullExprCleanupWithFlag(createCleanupActiveFlag());
744 void initFullExprCleanupWithFlag(Address ActiveFlag);
745 Address createCleanupActiveFlag();
747 /// PushDestructorCleanup - Push a cleanup to call the
748 /// complete-object destructor of an object of the given type at the
749 /// given address. Does nothing if T is not a C++ class type with a
750 /// non-trivial destructor.
751 void PushDestructorCleanup(QualType T, Address Addr);
753 /// PushDestructorCleanup - Push a cleanup to call the
754 /// complete-object variant of the given destructor on the object at
755 /// the given address.
756 void PushDestructorCleanup(const CXXDestructorDecl *Dtor, QualType T,
759 /// PopCleanupBlock - Will pop the cleanup entry on the stack and
760 /// process all branch fixups.
761 void PopCleanupBlock(bool FallThroughIsBranchThrough = false);
763 /// DeactivateCleanupBlock - Deactivates the given cleanup block.
764 /// The block cannot be reactivated. Pops it if it's the top of the
767 /// \param DominatingIP - An instruction which is known to
768 /// dominate the current IP (if set) and which lies along
769 /// all paths of execution between the current IP and the
770 /// the point at which the cleanup comes into scope.
771 void DeactivateCleanupBlock(EHScopeStack::stable_iterator Cleanup,
772 llvm::Instruction *DominatingIP);
774 /// ActivateCleanupBlock - Activates an initially-inactive cleanup.
775 /// Cannot be used to resurrect a deactivated cleanup.
777 /// \param DominatingIP - An instruction which is known to
778 /// dominate the current IP (if set) and which lies along
779 /// all paths of execution between the current IP and the
780 /// the point at which the cleanup comes into scope.
781 void ActivateCleanupBlock(EHScopeStack::stable_iterator Cleanup,
782 llvm::Instruction *DominatingIP);
784 /// Enters a new scope for capturing cleanups, all of which
785 /// will be executed once the scope is exited.
786 class RunCleanupsScope {
787 EHScopeStack::stable_iterator CleanupStackDepth, OldCleanupScopeDepth;
788 size_t LifetimeExtendedCleanupStackSize;
789 bool OldDidCallStackSave;
794 RunCleanupsScope(const RunCleanupsScope &) = delete;
795 void operator=(const RunCleanupsScope &) = delete;
798 CodeGenFunction& CGF;
801 /// Enter a new cleanup scope.
802 explicit RunCleanupsScope(CodeGenFunction &CGF)
803 : PerformCleanup(true), CGF(CGF)
805 CleanupStackDepth = CGF.EHStack.stable_begin();
806 LifetimeExtendedCleanupStackSize =
807 CGF.LifetimeExtendedCleanupStack.size();
808 OldDidCallStackSave = CGF.DidCallStackSave;
809 CGF.DidCallStackSave = false;
810 OldCleanupScopeDepth = CGF.CurrentCleanupScopeDepth;
811 CGF.CurrentCleanupScopeDepth = CleanupStackDepth;
814 /// Exit this cleanup scope, emitting any accumulated cleanups.
815 ~RunCleanupsScope() {
820 /// Determine whether this scope requires any cleanups.
821 bool requiresCleanups() const {
822 return CGF.EHStack.stable_begin() != CleanupStackDepth;
825 /// Force the emission of cleanups now, instead of waiting
826 /// until this object is destroyed.
827 /// \param ValuesToReload - A list of values that need to be available at
828 /// the insertion point after cleanup emission. If cleanup emission created
829 /// a shared cleanup block, these value pointers will be rewritten.
830 /// Otherwise, they not will be modified.
831 void ForceCleanup(std::initializer_list<llvm::Value**> ValuesToReload = {}) {
832 assert(PerformCleanup && "Already forced cleanup");
833 CGF.DidCallStackSave = OldDidCallStackSave;
834 CGF.PopCleanupBlocks(CleanupStackDepth, LifetimeExtendedCleanupStackSize,
836 PerformCleanup = false;
837 CGF.CurrentCleanupScopeDepth = OldCleanupScopeDepth;
841 // Cleanup stack depth of the RunCleanupsScope that was pushed most recently.
842 EHScopeStack::stable_iterator CurrentCleanupScopeDepth =
843 EHScopeStack::stable_end();
845 class LexicalScope : public RunCleanupsScope {
847 SmallVector<const LabelDecl*, 4> Labels;
848 LexicalScope *ParentScope;
850 LexicalScope(const LexicalScope &) = delete;
851 void operator=(const LexicalScope &) = delete;
854 /// Enter a new cleanup scope.
855 explicit LexicalScope(CodeGenFunction &CGF, SourceRange Range)
856 : RunCleanupsScope(CGF), Range(Range), ParentScope(CGF.CurLexicalScope) {
857 CGF.CurLexicalScope = this;
858 if (CGDebugInfo *DI = CGF.getDebugInfo())
859 DI->EmitLexicalBlockStart(CGF.Builder, Range.getBegin());
862 void addLabel(const LabelDecl *label) {
863 assert(PerformCleanup && "adding label to dead scope?");
864 Labels.push_back(label);
867 /// Exit this cleanup scope, emitting any accumulated
870 if (CGDebugInfo *DI = CGF.getDebugInfo())
871 DI->EmitLexicalBlockEnd(CGF.Builder, Range.getEnd());
873 // If we should perform a cleanup, force them now. Note that
874 // this ends the cleanup scope before rescoping any labels.
875 if (PerformCleanup) {
876 ApplyDebugLocation DL(CGF, Range.getEnd());
881 /// Force the emission of cleanups now, instead of waiting
882 /// until this object is destroyed.
883 void ForceCleanup() {
884 CGF.CurLexicalScope = ParentScope;
885 RunCleanupsScope::ForceCleanup();
891 bool hasLabels() const {
892 return !Labels.empty();
895 void rescopeLabels();
898 typedef llvm::DenseMap<const Decl *, Address> DeclMapTy;
900 /// The class used to assign some variables some temporarily addresses.
902 DeclMapTy SavedLocals;
903 DeclMapTy SavedTempAddresses;
904 OMPMapVars(const OMPMapVars &) = delete;
905 void operator=(const OMPMapVars &) = delete;
908 explicit OMPMapVars() = default;
910 assert(SavedLocals.empty() && "Did not restored original addresses.");
913 /// Sets the address of the variable \p LocalVD to be \p TempAddr in
915 /// \return true if at least one variable was set already, false otherwise.
916 bool setVarAddr(CodeGenFunction &CGF, const VarDecl *LocalVD,
918 LocalVD = LocalVD->getCanonicalDecl();
919 // Only save it once.
920 if (SavedLocals.count(LocalVD)) return false;
922 // Copy the existing local entry to SavedLocals.
923 auto it = CGF.LocalDeclMap.find(LocalVD);
924 if (it != CGF.LocalDeclMap.end())
925 SavedLocals.try_emplace(LocalVD, it->second);
927 SavedLocals.try_emplace(LocalVD, Address::invalid());
929 // Generate the private entry.
930 QualType VarTy = LocalVD->getType();
931 if (VarTy->isReferenceType()) {
932 Address Temp = CGF.CreateMemTemp(VarTy);
933 CGF.Builder.CreateStore(TempAddr.getPointer(), Temp);
936 SavedTempAddresses.try_emplace(LocalVD, TempAddr);
941 /// Applies new addresses to the list of the variables.
942 /// \return true if at least one variable is using new address, false
944 bool apply(CodeGenFunction &CGF) {
945 copyInto(SavedTempAddresses, CGF.LocalDeclMap);
946 SavedTempAddresses.clear();
947 return !SavedLocals.empty();
950 /// Restores original addresses of the variables.
951 void restore(CodeGenFunction &CGF) {
952 if (!SavedLocals.empty()) {
953 copyInto(SavedLocals, CGF.LocalDeclMap);
959 /// Copy all the entries in the source map over the corresponding
960 /// entries in the destination, which must exist.
961 static void copyInto(const DeclMapTy &Src, DeclMapTy &Dest) {
962 for (auto &Pair : Src) {
963 if (!Pair.second.isValid()) {
964 Dest.erase(Pair.first);
968 auto I = Dest.find(Pair.first);
970 I->second = Pair.second;
977 /// The scope used to remap some variables as private in the OpenMP loop body
978 /// (or other captured region emitted without outlining), and to restore old
979 /// vars back on exit.
980 class OMPPrivateScope : public RunCleanupsScope {
981 OMPMapVars MappedVars;
982 OMPPrivateScope(const OMPPrivateScope &) = delete;
983 void operator=(const OMPPrivateScope &) = delete;
986 /// Enter a new OpenMP private scope.
987 explicit OMPPrivateScope(CodeGenFunction &CGF) : RunCleanupsScope(CGF) {}
989 /// Registers \p LocalVD variable as a private and apply \p PrivateGen
990 /// function for it to generate corresponding private variable. \p
991 /// PrivateGen returns an address of the generated private variable.
992 /// \return true if the variable is registered as private, false if it has
993 /// been privatized already.
994 bool addPrivate(const VarDecl *LocalVD,
995 const llvm::function_ref<Address()> PrivateGen) {
996 assert(PerformCleanup && "adding private to dead scope");
997 return MappedVars.setVarAddr(CGF, LocalVD, PrivateGen());
1000 /// Privatizes local variables previously registered as private.
1001 /// Registration is separate from the actual privatization to allow
1002 /// initializers use values of the original variables, not the private one.
1003 /// This is important, for example, if the private variable is a class
1004 /// variable initialized by a constructor that references other private
1005 /// variables. But at initialization original variables must be used, not
1007 /// \return true if at least one variable was privatized, false otherwise.
1008 bool Privatize() { return MappedVars.apply(CGF); }
1010 void ForceCleanup() {
1011 RunCleanupsScope::ForceCleanup();
1012 MappedVars.restore(CGF);
1015 /// Exit scope - all the mapped variables are restored.
1016 ~OMPPrivateScope() {
1021 /// Checks if the global variable is captured in current function.
1022 bool isGlobalVarCaptured(const VarDecl *VD) const {
1023 VD = VD->getCanonicalDecl();
1024 return !VD->isLocalVarDeclOrParm() && CGF.LocalDeclMap.count(VD) > 0;
1028 /// Save/restore original map of previously emitted local vars in case when we
1029 /// need to duplicate emission of the same code several times in the same
1030 /// function for OpenMP code.
1031 class OMPLocalDeclMapRAII {
1032 CodeGenFunction &CGF;
1036 OMPLocalDeclMapRAII(CodeGenFunction &CGF)
1037 : CGF(CGF), SavedMap(CGF.LocalDeclMap) {}
1038 ~OMPLocalDeclMapRAII() { SavedMap.swap(CGF.LocalDeclMap); }
1041 /// Takes the old cleanup stack size and emits the cleanup blocks
1042 /// that have been added.
1044 PopCleanupBlocks(EHScopeStack::stable_iterator OldCleanupStackSize,
1045 std::initializer_list<llvm::Value **> ValuesToReload = {});
1047 /// Takes the old cleanup stack size and emits the cleanup blocks
1048 /// that have been added, then adds all lifetime-extended cleanups from
1049 /// the given position to the stack.
1051 PopCleanupBlocks(EHScopeStack::stable_iterator OldCleanupStackSize,
1052 size_t OldLifetimeExtendedStackSize,
1053 std::initializer_list<llvm::Value **> ValuesToReload = {});
1055 void ResolveBranchFixups(llvm::BasicBlock *Target);
1057 /// The given basic block lies in the current EH scope, but may be a
1058 /// target of a potentially scope-crossing jump; get a stable handle
1059 /// to which we can perform this jump later.
1060 JumpDest getJumpDestInCurrentScope(llvm::BasicBlock *Target) {
1061 return JumpDest(Target,
1062 EHStack.getInnermostNormalCleanup(),
1063 NextCleanupDestIndex++);
1066 /// The given basic block lies in the current EH scope, but may be a
1067 /// target of a potentially scope-crossing jump; get a stable handle
1068 /// to which we can perform this jump later.
1069 JumpDest getJumpDestInCurrentScope(StringRef Name = StringRef()) {
1070 return getJumpDestInCurrentScope(createBasicBlock(Name));
1073 /// EmitBranchThroughCleanup - Emit a branch from the current insert
1074 /// block through the normal cleanup handling code (if any) and then
1075 /// on to \arg Dest.
1076 void EmitBranchThroughCleanup(JumpDest Dest);
1078 /// isObviouslyBranchWithoutCleanups - Return true if a branch to the
1079 /// specified destination obviously has no cleanups to run. 'false' is always
1080 /// a conservatively correct answer for this method.
1081 bool isObviouslyBranchWithoutCleanups(JumpDest Dest) const;
1083 /// popCatchScope - Pops the catch scope at the top of the EHScope
1084 /// stack, emitting any required code (other than the catch handlers
1086 void popCatchScope();
1088 llvm::BasicBlock *getEHResumeBlock(bool isCleanup);
1089 llvm::BasicBlock *getEHDispatchBlock(EHScopeStack::stable_iterator scope);
1091 getFuncletEHDispatchBlock(EHScopeStack::stable_iterator scope);
1093 /// An object to manage conditionally-evaluated expressions.
1094 class ConditionalEvaluation {
1095 llvm::BasicBlock *StartBB;
1098 ConditionalEvaluation(CodeGenFunction &CGF)
1099 : StartBB(CGF.Builder.GetInsertBlock()) {}
1101 void begin(CodeGenFunction &CGF) {
1102 assert(CGF.OutermostConditional != this);
1103 if (!CGF.OutermostConditional)
1104 CGF.OutermostConditional = this;
1107 void end(CodeGenFunction &CGF) {
1108 assert(CGF.OutermostConditional != nullptr);
1109 if (CGF.OutermostConditional == this)
1110 CGF.OutermostConditional = nullptr;
1113 /// Returns a block which will be executed prior to each
1114 /// evaluation of the conditional code.
1115 llvm::BasicBlock *getStartingBlock() const {
1120 /// isInConditionalBranch - Return true if we're currently emitting
1121 /// one branch or the other of a conditional expression.
1122 bool isInConditionalBranch() const { return OutermostConditional != nullptr; }
1124 void setBeforeOutermostConditional(llvm::Value *value, Address addr) {
1125 assert(isInConditionalBranch());
1126 llvm::BasicBlock *block = OutermostConditional->getStartingBlock();
1127 auto store = new llvm::StoreInst(value, addr.getPointer(), &block->back());
1128 store->setAlignment(addr.getAlignment().getAsAlign());
1131 /// An RAII object to record that we're evaluating a statement
1133 class StmtExprEvaluation {
1134 CodeGenFunction &CGF;
1136 /// We have to save the outermost conditional: cleanups in a
1137 /// statement expression aren't conditional just because the
1139 ConditionalEvaluation *SavedOutermostConditional;
1142 StmtExprEvaluation(CodeGenFunction &CGF)
1143 : CGF(CGF), SavedOutermostConditional(CGF.OutermostConditional) {
1144 CGF.OutermostConditional = nullptr;
1147 ~StmtExprEvaluation() {
1148 CGF.OutermostConditional = SavedOutermostConditional;
1149 CGF.EnsureInsertPoint();
1153 /// An object which temporarily prevents a value from being
1154 /// destroyed by aggressive peephole optimizations that assume that
1155 /// all uses of a value have been realized in the IR.
1156 class PeepholeProtection {
1157 llvm::Instruction *Inst;
1158 friend class CodeGenFunction;
1161 PeepholeProtection() : Inst(nullptr) {}
1164 /// A non-RAII class containing all the information about a bound
1165 /// opaque value. OpaqueValueMapping, below, is a RAII wrapper for
1166 /// this which makes individual mappings very simple; using this
1167 /// class directly is useful when you have a variable number of
1168 /// opaque values or don't want the RAII functionality for some
1170 class OpaqueValueMappingData {
1171 const OpaqueValueExpr *OpaqueValue;
1173 CodeGenFunction::PeepholeProtection Protection;
1175 OpaqueValueMappingData(const OpaqueValueExpr *ov,
1177 : OpaqueValue(ov), BoundLValue(boundLValue) {}
1179 OpaqueValueMappingData() : OpaqueValue(nullptr) {}
1181 static bool shouldBindAsLValue(const Expr *expr) {
1182 // gl-values should be bound as l-values for obvious reasons.
1183 // Records should be bound as l-values because IR generation
1184 // always keeps them in memory. Expressions of function type
1185 // act exactly like l-values but are formally required to be
1187 return expr->isGLValue() ||
1188 expr->getType()->isFunctionType() ||
1189 hasAggregateEvaluationKind(expr->getType());
1192 static OpaqueValueMappingData bind(CodeGenFunction &CGF,
1193 const OpaqueValueExpr *ov,
1195 if (shouldBindAsLValue(ov))
1196 return bind(CGF, ov, CGF.EmitLValue(e));
1197 return bind(CGF, ov, CGF.EmitAnyExpr(e));
1200 static OpaqueValueMappingData bind(CodeGenFunction &CGF,
1201 const OpaqueValueExpr *ov,
1203 assert(shouldBindAsLValue(ov));
1204 CGF.OpaqueLValues.insert(std::make_pair(ov, lv));
1205 return OpaqueValueMappingData(ov, true);
1208 static OpaqueValueMappingData bind(CodeGenFunction &CGF,
1209 const OpaqueValueExpr *ov,
1211 assert(!shouldBindAsLValue(ov));
1212 CGF.OpaqueRValues.insert(std::make_pair(ov, rv));
1214 OpaqueValueMappingData data(ov, false);
1216 // Work around an extremely aggressive peephole optimization in
1217 // EmitScalarConversion which assumes that all other uses of a
1218 // value are extant.
1219 data.Protection = CGF.protectFromPeepholes(rv);
1224 bool isValid() const { return OpaqueValue != nullptr; }
1225 void clear() { OpaqueValue = nullptr; }
1227 void unbind(CodeGenFunction &CGF) {
1228 assert(OpaqueValue && "no data to unbind!");
1231 CGF.OpaqueLValues.erase(OpaqueValue);
1233 CGF.OpaqueRValues.erase(OpaqueValue);
1234 CGF.unprotectFromPeepholes(Protection);
1239 /// An RAII object to set (and then clear) a mapping for an OpaqueValueExpr.
1240 class OpaqueValueMapping {
1241 CodeGenFunction &CGF;
1242 OpaqueValueMappingData Data;
1245 static bool shouldBindAsLValue(const Expr *expr) {
1246 return OpaqueValueMappingData::shouldBindAsLValue(expr);
1249 /// Build the opaque value mapping for the given conditional
1250 /// operator if it's the GNU ?: extension. This is a common
1251 /// enough pattern that the convenience operator is really
1254 OpaqueValueMapping(CodeGenFunction &CGF,
1255 const AbstractConditionalOperator *op) : CGF(CGF) {
1256 if (isa<ConditionalOperator>(op))
1257 // Leave Data empty.
1260 const BinaryConditionalOperator *e = cast<BinaryConditionalOperator>(op);
1261 Data = OpaqueValueMappingData::bind(CGF, e->getOpaqueValue(),
1265 /// Build the opaque value mapping for an OpaqueValueExpr whose source
1266 /// expression is set to the expression the OVE represents.
1267 OpaqueValueMapping(CodeGenFunction &CGF, const OpaqueValueExpr *OV)
1270 assert(OV->getSourceExpr() && "wrong form of OpaqueValueMapping used "
1271 "for OVE with no source expression");
1272 Data = OpaqueValueMappingData::bind(CGF, OV, OV->getSourceExpr());
1276 OpaqueValueMapping(CodeGenFunction &CGF,
1277 const OpaqueValueExpr *opaqueValue,
1279 : CGF(CGF), Data(OpaqueValueMappingData::bind(CGF, opaqueValue, lvalue)) {
1282 OpaqueValueMapping(CodeGenFunction &CGF,
1283 const OpaqueValueExpr *opaqueValue,
1285 : CGF(CGF), Data(OpaqueValueMappingData::bind(CGF, opaqueValue, rvalue)) {
1293 ~OpaqueValueMapping() {
1294 if (Data.isValid()) Data.unbind(CGF);
1299 CGDebugInfo *DebugInfo;
1300 /// Used to create unique names for artificial VLA size debug info variables.
1301 unsigned VLAExprCounter = 0;
1302 bool DisableDebugInfo = false;
1304 /// DidCallStackSave - Whether llvm.stacksave has been called. Used to avoid
1305 /// calling llvm.stacksave for multiple VLAs in the same scope.
1306 bool DidCallStackSave = false;
1308 /// IndirectBranch - The first time an indirect goto is seen we create a block
1309 /// with an indirect branch. Every time we see the address of a label taken,
1310 /// we add the label to the indirect goto. Every subsequent indirect goto is
1311 /// codegen'd as a jump to the IndirectBranch's basic block.
1312 llvm::IndirectBrInst *IndirectBranch = nullptr;
1314 /// LocalDeclMap - This keeps track of the LLVM allocas or globals for local C
1316 DeclMapTy LocalDeclMap;
1318 // Keep track of the cleanups for callee-destructed parameters pushed to the
1319 // cleanup stack so that they can be deactivated later.
1320 llvm::DenseMap<const ParmVarDecl *, EHScopeStack::stable_iterator>
1321 CalleeDestructedParamCleanups;
1323 /// SizeArguments - If a ParmVarDecl had the pass_object_size attribute, this
1324 /// will contain a mapping from said ParmVarDecl to its implicit "object_size"
1326 llvm::SmallDenseMap<const ParmVarDecl *, const ImplicitParamDecl *, 2>
1329 /// Track escaped local variables with auto storage. Used during SEH
1330 /// outlining to produce a call to llvm.localescape.
1331 llvm::DenseMap<llvm::AllocaInst *, int> EscapedLocals;
1333 /// LabelMap - This keeps track of the LLVM basic block for each C label.
1334 llvm::DenseMap<const LabelDecl*, JumpDest> LabelMap;
1336 // BreakContinueStack - This keeps track of where break and continue
1337 // statements should jump to.
1338 struct BreakContinue {
1339 BreakContinue(JumpDest Break, JumpDest Continue)
1340 : BreakBlock(Break), ContinueBlock(Continue) {}
1342 JumpDest BreakBlock;
1343 JumpDest ContinueBlock;
1345 SmallVector<BreakContinue, 8> BreakContinueStack;
1347 /// Handles cancellation exit points in OpenMP-related constructs.
1348 class OpenMPCancelExitStack {
1349 /// Tracks cancellation exit point and join point for cancel-related exit
1350 /// and normal exit.
1352 CancelExit() = default;
1353 CancelExit(OpenMPDirectiveKind Kind, JumpDest ExitBlock,
1355 : Kind(Kind), ExitBlock(ExitBlock), ContBlock(ContBlock) {}
1356 OpenMPDirectiveKind Kind = llvm::omp::OMPD_unknown;
1357 /// true if the exit block has been emitted already by the special
1358 /// emitExit() call, false if the default codegen is used.
1359 bool HasBeenEmitted = false;
1364 SmallVector<CancelExit, 8> Stack;
1367 OpenMPCancelExitStack() : Stack(1) {}
1368 ~OpenMPCancelExitStack() = default;
1369 /// Fetches the exit block for the current OpenMP construct.
1370 JumpDest getExitBlock() const { return Stack.back().ExitBlock; }
1371 /// Emits exit block with special codegen procedure specific for the related
1372 /// OpenMP construct + emits code for normal construct cleanup.
1373 void emitExit(CodeGenFunction &CGF, OpenMPDirectiveKind Kind,
1374 const llvm::function_ref<void(CodeGenFunction &)> CodeGen) {
1375 if (Stack.back().Kind == Kind && getExitBlock().isValid()) {
1376 assert(CGF.getOMPCancelDestination(Kind).isValid());
1377 assert(CGF.HaveInsertPoint());
1378 assert(!Stack.back().HasBeenEmitted);
1379 auto IP = CGF.Builder.saveAndClearIP();
1380 CGF.EmitBlock(Stack.back().ExitBlock.getBlock());
1382 CGF.EmitBranch(Stack.back().ContBlock.getBlock());
1383 CGF.Builder.restoreIP(IP);
1384 Stack.back().HasBeenEmitted = true;
1388 /// Enter the cancel supporting \a Kind construct.
1389 /// \param Kind OpenMP directive that supports cancel constructs.
1390 /// \param HasCancel true, if the construct has inner cancel directive,
1391 /// false otherwise.
1392 void enter(CodeGenFunction &CGF, OpenMPDirectiveKind Kind, bool HasCancel) {
1393 Stack.push_back({Kind,
1394 HasCancel ? CGF.getJumpDestInCurrentScope("cancel.exit")
1396 HasCancel ? CGF.getJumpDestInCurrentScope("cancel.cont")
1399 /// Emits default exit point for the cancel construct (if the special one
1400 /// has not be used) + join point for cancel/normal exits.
1401 void exit(CodeGenFunction &CGF) {
1402 if (getExitBlock().isValid()) {
1403 assert(CGF.getOMPCancelDestination(Stack.back().Kind).isValid());
1404 bool HaveIP = CGF.HaveInsertPoint();
1405 if (!Stack.back().HasBeenEmitted) {
1407 CGF.EmitBranchThroughCleanup(Stack.back().ContBlock);
1408 CGF.EmitBlock(Stack.back().ExitBlock.getBlock());
1409 CGF.EmitBranchThroughCleanup(Stack.back().ContBlock);
1411 CGF.EmitBlock(Stack.back().ContBlock.getBlock());
1413 CGF.Builder.CreateUnreachable();
1414 CGF.Builder.ClearInsertionPoint();
1420 OpenMPCancelExitStack OMPCancelStack;
1422 /// Calculate branch weights for the likelihood attribute
1423 llvm::MDNode *createBranchWeights(Stmt::Likelihood LH) const;
1427 /// Calculate branch weights appropriate for PGO data
1428 llvm::MDNode *createProfileWeights(uint64_t TrueCount,
1429 uint64_t FalseCount) const;
1430 llvm::MDNode *createProfileWeights(ArrayRef<uint64_t> Weights) const;
1431 llvm::MDNode *createProfileWeightsForLoop(const Stmt *Cond,
1432 uint64_t LoopCount) const;
1434 /// Calculate the branch weight for PGO data or the likelihood attribute.
1435 /// The function tries to get the weight of \ref createProfileWeightsForLoop.
1436 /// If that fails it gets the weight of \ref createBranchWeights.
1437 llvm::MDNode *createProfileOrBranchWeightsForLoop(const Stmt *Cond,
1439 const Stmt *Body) const;
1442 /// Increment the profiler's counter for the given statement by \p StepV.
1443 /// If \p StepV is null, the default increment is 1.
1444 void incrementProfileCounter(const Stmt *S, llvm::Value *StepV = nullptr) {
1445 if (CGM.getCodeGenOpts().hasProfileClangInstr())
1446 PGO.emitCounterIncrement(Builder, S, StepV);
1447 PGO.setCurrentStmt(S);
1450 /// Get the profiler's count for the given statement.
1451 uint64_t getProfileCount(const Stmt *S) {
1452 Optional<uint64_t> Count = PGO.getStmtCount(S);
1453 if (!Count.hasValue())
1458 /// Set the profiler's current count.
1459 void setCurrentProfileCount(uint64_t Count) {
1460 PGO.setCurrentRegionCount(Count);
1463 /// Get the profiler's current count. This is generally the count for the most
1464 /// recently incremented counter.
1465 uint64_t getCurrentProfileCount() {
1466 return PGO.getCurrentRegionCount();
1471 /// SwitchInsn - This is nearest current switch instruction. It is null if
1472 /// current context is not in a switch.
1473 llvm::SwitchInst *SwitchInsn = nullptr;
1474 /// The branch weights of SwitchInsn when doing instrumentation based PGO.
1475 SmallVector<uint64_t, 16> *SwitchWeights = nullptr;
1477 /// The likelihood attributes of the SwitchCase.
1478 SmallVector<Stmt::Likelihood, 16> *SwitchLikelihood = nullptr;
1480 /// CaseRangeBlock - This block holds if condition check for last case
1481 /// statement range in current switch instruction.
1482 llvm::BasicBlock *CaseRangeBlock = nullptr;
1484 /// OpaqueLValues - Keeps track of the current set of opaque value
1486 llvm::DenseMap<const OpaqueValueExpr *, LValue> OpaqueLValues;
1487 llvm::DenseMap<const OpaqueValueExpr *, RValue> OpaqueRValues;
1489 // VLASizeMap - This keeps track of the associated size for each VLA type.
1490 // We track this by the size expression rather than the type itself because
1491 // in certain situations, like a const qualifier applied to an VLA typedef,
1492 // multiple VLA types can share the same size expression.
1493 // FIXME: Maybe this could be a stack of maps that is pushed/popped as we
1494 // enter/leave scopes.
1495 llvm::DenseMap<const Expr*, llvm::Value*> VLASizeMap;
1497 /// A block containing a single 'unreachable' instruction. Created
1498 /// lazily by getUnreachableBlock().
1499 llvm::BasicBlock *UnreachableBlock = nullptr;
1501 /// Counts of the number return expressions in the function.
1502 unsigned NumReturnExprs = 0;
1504 /// Count the number of simple (constant) return expressions in the function.
1505 unsigned NumSimpleReturnExprs = 0;
1507 /// The last regular (non-return) debug location (breakpoint) in the function.
1508 SourceLocation LastStopPoint;
1511 /// Source location information about the default argument or member
1512 /// initializer expression we're evaluating, if any.
1513 CurrentSourceLocExprScope CurSourceLocExprScope;
1514 using SourceLocExprScopeGuard =
1515 CurrentSourceLocExprScope::SourceLocExprScopeGuard;
1517 /// A scope within which we are constructing the fields of an object which
1518 /// might use a CXXDefaultInitExpr. This stashes away a 'this' value to use
1519 /// if we need to evaluate a CXXDefaultInitExpr within the evaluation.
1520 class FieldConstructionScope {
1522 FieldConstructionScope(CodeGenFunction &CGF, Address This)
1523 : CGF(CGF), OldCXXDefaultInitExprThis(CGF.CXXDefaultInitExprThis) {
1524 CGF.CXXDefaultInitExprThis = This;
1526 ~FieldConstructionScope() {
1527 CGF.CXXDefaultInitExprThis = OldCXXDefaultInitExprThis;
1531 CodeGenFunction &CGF;
1532 Address OldCXXDefaultInitExprThis;
1535 /// The scope of a CXXDefaultInitExpr. Within this scope, the value of 'this'
1536 /// is overridden to be the object under construction.
1537 class CXXDefaultInitExprScope {
1539 CXXDefaultInitExprScope(CodeGenFunction &CGF, const CXXDefaultInitExpr *E)
1540 : CGF(CGF), OldCXXThisValue(CGF.CXXThisValue),
1541 OldCXXThisAlignment(CGF.CXXThisAlignment),
1542 SourceLocScope(E, CGF.CurSourceLocExprScope) {
1543 CGF.CXXThisValue = CGF.CXXDefaultInitExprThis.getPointer();
1544 CGF.CXXThisAlignment = CGF.CXXDefaultInitExprThis.getAlignment();
1546 ~CXXDefaultInitExprScope() {
1547 CGF.CXXThisValue = OldCXXThisValue;
1548 CGF.CXXThisAlignment = OldCXXThisAlignment;
1552 CodeGenFunction &CGF;
1553 llvm::Value *OldCXXThisValue;
1554 CharUnits OldCXXThisAlignment;
1555 SourceLocExprScopeGuard SourceLocScope;
1558 struct CXXDefaultArgExprScope : SourceLocExprScopeGuard {
1559 CXXDefaultArgExprScope(CodeGenFunction &CGF, const CXXDefaultArgExpr *E)
1560 : SourceLocExprScopeGuard(E, CGF.CurSourceLocExprScope) {}
1563 /// The scope of an ArrayInitLoopExpr. Within this scope, the value of the
1564 /// current loop index is overridden.
1565 class ArrayInitLoopExprScope {
1567 ArrayInitLoopExprScope(CodeGenFunction &CGF, llvm::Value *Index)
1568 : CGF(CGF), OldArrayInitIndex(CGF.ArrayInitIndex) {
1569 CGF.ArrayInitIndex = Index;
1571 ~ArrayInitLoopExprScope() {
1572 CGF.ArrayInitIndex = OldArrayInitIndex;
1576 CodeGenFunction &CGF;
1577 llvm::Value *OldArrayInitIndex;
1580 class InlinedInheritingConstructorScope {
1582 InlinedInheritingConstructorScope(CodeGenFunction &CGF, GlobalDecl GD)
1583 : CGF(CGF), OldCurGD(CGF.CurGD), OldCurFuncDecl(CGF.CurFuncDecl),
1584 OldCurCodeDecl(CGF.CurCodeDecl),
1585 OldCXXABIThisDecl(CGF.CXXABIThisDecl),
1586 OldCXXABIThisValue(CGF.CXXABIThisValue),
1587 OldCXXThisValue(CGF.CXXThisValue),
1588 OldCXXABIThisAlignment(CGF.CXXABIThisAlignment),
1589 OldCXXThisAlignment(CGF.CXXThisAlignment),
1590 OldReturnValue(CGF.ReturnValue), OldFnRetTy(CGF.FnRetTy),
1591 OldCXXInheritedCtorInitExprArgs(
1592 std::move(CGF.CXXInheritedCtorInitExprArgs)) {
1594 CGF.CurFuncDecl = CGF.CurCodeDecl =
1595 cast<CXXConstructorDecl>(GD.getDecl());
1596 CGF.CXXABIThisDecl = nullptr;
1597 CGF.CXXABIThisValue = nullptr;
1598 CGF.CXXThisValue = nullptr;
1599 CGF.CXXABIThisAlignment = CharUnits();
1600 CGF.CXXThisAlignment = CharUnits();
1601 CGF.ReturnValue = Address::invalid();
1602 CGF.FnRetTy = QualType();
1603 CGF.CXXInheritedCtorInitExprArgs.clear();
1605 ~InlinedInheritingConstructorScope() {
1606 CGF.CurGD = OldCurGD;
1607 CGF.CurFuncDecl = OldCurFuncDecl;
1608 CGF.CurCodeDecl = OldCurCodeDecl;
1609 CGF.CXXABIThisDecl = OldCXXABIThisDecl;
1610 CGF.CXXABIThisValue = OldCXXABIThisValue;
1611 CGF.CXXThisValue = OldCXXThisValue;
1612 CGF.CXXABIThisAlignment = OldCXXABIThisAlignment;
1613 CGF.CXXThisAlignment = OldCXXThisAlignment;
1614 CGF.ReturnValue = OldReturnValue;
1615 CGF.FnRetTy = OldFnRetTy;
1616 CGF.CXXInheritedCtorInitExprArgs =
1617 std::move(OldCXXInheritedCtorInitExprArgs);
1621 CodeGenFunction &CGF;
1622 GlobalDecl OldCurGD;
1623 const Decl *OldCurFuncDecl;
1624 const Decl *OldCurCodeDecl;
1625 ImplicitParamDecl *OldCXXABIThisDecl;
1626 llvm::Value *OldCXXABIThisValue;
1627 llvm::Value *OldCXXThisValue;
1628 CharUnits OldCXXABIThisAlignment;
1629 CharUnits OldCXXThisAlignment;
1630 Address OldReturnValue;
1631 QualType OldFnRetTy;
1632 CallArgList OldCXXInheritedCtorInitExprArgs;
1635 // Helper class for the OpenMP IR Builder. Allows reusability of code used for
1636 // region body, and finalization codegen callbacks. This will class will also
1637 // contain privatization functions used by the privatization call backs
1639 // TODO: this is temporary class for things that are being moved out of
1640 // CGOpenMPRuntime, new versions of current CodeGenFunction methods, or
1641 // utility function for use with the OMPBuilder. Once that move to use the
1642 // OMPBuilder is done, everything here will either become part of CodeGenFunc.
1643 // directly, or a new helper class that will contain functions used by both
1644 // this and the OMPBuilder
1646 struct OMPBuilderCBHelpers {
1648 OMPBuilderCBHelpers() = delete;
1649 OMPBuilderCBHelpers(const OMPBuilderCBHelpers &) = delete;
1650 OMPBuilderCBHelpers &operator=(const OMPBuilderCBHelpers &) = delete;
1652 using InsertPointTy = llvm::OpenMPIRBuilder::InsertPointTy;
1654 /// Cleanup action for allocate support.
1655 class OMPAllocateCleanupTy final : public EHScopeStack::Cleanup {
1658 llvm::CallInst *RTLFnCI;
1661 OMPAllocateCleanupTy(llvm::CallInst *RLFnCI) : RTLFnCI(RLFnCI) {
1662 RLFnCI->removeFromParent();
1665 void Emit(CodeGenFunction &CGF, Flags /*flags*/) override {
1666 if (!CGF.HaveInsertPoint())
1668 CGF.Builder.Insert(RTLFnCI);
1672 /// Returns address of the threadprivate variable for the current
1673 /// thread. This Also create any necessary OMP runtime calls.
1675 /// \param VD VarDecl for Threadprivate variable.
1676 /// \param VDAddr Address of the Vardecl
1677 /// \param Loc The location where the barrier directive was encountered
1678 static Address getAddrOfThreadPrivate(CodeGenFunction &CGF,
1679 const VarDecl *VD, Address VDAddr,
1680 SourceLocation Loc);
1682 /// Gets the OpenMP-specific address of the local variable /p VD.
1683 static Address getAddressOfLocalVariable(CodeGenFunction &CGF,
1685 /// Get the platform-specific name separator.
1686 /// \param Parts different parts of the final name that needs separation
1687 /// \param FirstSeparator First separator used between the initial two
1688 /// parts of the name.
1689 /// \param Separator separator used between all of the rest consecutinve
1690 /// parts of the name
1691 static std::string getNameWithSeparators(ArrayRef<StringRef> Parts,
1692 StringRef FirstSeparator = ".",
1693 StringRef Separator = ".");
1694 /// Emit the Finalization for an OMP region
1695 /// \param CGF The Codegen function this belongs to
1696 /// \param IP Insertion point for generating the finalization code.
1697 static void FinalizeOMPRegion(CodeGenFunction &CGF, InsertPointTy IP) {
1698 CGBuilderTy::InsertPointGuard IPG(CGF.Builder);
1699 assert(IP.getBlock()->end() != IP.getPoint() &&
1700 "OpenMP IR Builder should cause terminated block!");
1702 llvm::BasicBlock *IPBB = IP.getBlock();
1703 llvm::BasicBlock *DestBB = IPBB->getUniqueSuccessor();
1704 assert(DestBB && "Finalization block should have one successor!");
1706 // erase and replace with cleanup branch.
1707 IPBB->getTerminator()->eraseFromParent();
1708 CGF.Builder.SetInsertPoint(IPBB);
1709 CodeGenFunction::JumpDest Dest = CGF.getJumpDestInCurrentScope(DestBB);
1710 CGF.EmitBranchThroughCleanup(Dest);
1713 /// Emit the body of an OMP region
1714 /// \param CGF The Codegen function this belongs to
1715 /// \param RegionBodyStmt The body statement for the OpenMP region being
1717 /// \param CodeGenIP Insertion point for generating the body code.
1718 /// \param FiniBB The finalization basic block
1719 static void EmitOMPRegionBody(CodeGenFunction &CGF,
1720 const Stmt *RegionBodyStmt,
1721 InsertPointTy CodeGenIP,
1722 llvm::BasicBlock &FiniBB) {
1723 llvm::BasicBlock *CodeGenIPBB = CodeGenIP.getBlock();
1724 if (llvm::Instruction *CodeGenIPBBTI = CodeGenIPBB->getTerminator())
1725 CodeGenIPBBTI->eraseFromParent();
1727 CGF.Builder.SetInsertPoint(CodeGenIPBB);
1729 CGF.EmitStmt(RegionBodyStmt);
1731 if (CGF.Builder.saveIP().isSet())
1732 CGF.Builder.CreateBr(&FiniBB);
1735 /// RAII for preserving necessary info during Outlined region body codegen.
1736 class OutlinedRegionBodyRAII {
1738 llvm::AssertingVH<llvm::Instruction> OldAllocaIP;
1739 CodeGenFunction::JumpDest OldReturnBlock;
1740 CGBuilderTy::InsertPoint IP;
1741 CodeGenFunction &CGF;
1744 OutlinedRegionBodyRAII(CodeGenFunction &cgf, InsertPointTy &AllocaIP,
1745 llvm::BasicBlock &RetBB)
1747 assert(AllocaIP.isSet() &&
1748 "Must specify Insertion point for allocas of outlined function");
1749 OldAllocaIP = CGF.AllocaInsertPt;
1750 CGF.AllocaInsertPt = &*AllocaIP.getPoint();
1751 IP = CGF.Builder.saveIP();
1753 OldReturnBlock = CGF.ReturnBlock;
1754 CGF.ReturnBlock = CGF.getJumpDestInCurrentScope(&RetBB);
1757 ~OutlinedRegionBodyRAII() {
1758 CGF.AllocaInsertPt = OldAllocaIP;
1759 CGF.ReturnBlock = OldReturnBlock;
1760 CGF.Builder.restoreIP(IP);
1764 /// RAII for preserving necessary info during inlined region body codegen.
1765 class InlinedRegionBodyRAII {
1767 llvm::AssertingVH<llvm::Instruction> OldAllocaIP;
1768 CodeGenFunction &CGF;
1771 InlinedRegionBodyRAII(CodeGenFunction &cgf, InsertPointTy &AllocaIP,
1772 llvm::BasicBlock &FiniBB)
1774 // Alloca insertion block should be in the entry block of the containing
1775 // function so it expects an empty AllocaIP in which case will reuse the
1776 // old alloca insertion point, or a new AllocaIP in the same block as
1778 assert((!AllocaIP.isSet() ||
1779 CGF.AllocaInsertPt->getParent() == AllocaIP.getBlock()) &&
1780 "Insertion point should be in the entry block of containing "
1782 OldAllocaIP = CGF.AllocaInsertPt;
1783 if (AllocaIP.isSet())
1784 CGF.AllocaInsertPt = &*AllocaIP.getPoint();
1786 // TODO: Remove the call, after making sure the counter is not used by
1788 // Since this is an inlined region, it should not modify the
1789 // ReturnBlock, and should reuse the one for the enclosing outlined
1790 // region. So, the JumpDest being return by the function is discarded
1791 (void)CGF.getJumpDestInCurrentScope(&FiniBB);
1794 ~InlinedRegionBodyRAII() { CGF.AllocaInsertPt = OldAllocaIP; }
1799 /// CXXThisDecl - When generating code for a C++ member function,
1800 /// this will hold the implicit 'this' declaration.
1801 ImplicitParamDecl *CXXABIThisDecl = nullptr;
1802 llvm::Value *CXXABIThisValue = nullptr;
1803 llvm::Value *CXXThisValue = nullptr;
1804 CharUnits CXXABIThisAlignment;
1805 CharUnits CXXThisAlignment;
1807 /// The value of 'this' to use when evaluating CXXDefaultInitExprs within
1808 /// this expression.
1809 Address CXXDefaultInitExprThis = Address::invalid();
1811 /// The current array initialization index when evaluating an
1812 /// ArrayInitIndexExpr within an ArrayInitLoopExpr.
1813 llvm::Value *ArrayInitIndex = nullptr;
1815 /// The values of function arguments to use when evaluating
1816 /// CXXInheritedCtorInitExprs within this context.
1817 CallArgList CXXInheritedCtorInitExprArgs;
1819 /// CXXStructorImplicitParamDecl - When generating code for a constructor or
1820 /// destructor, this will hold the implicit argument (e.g. VTT).
1821 ImplicitParamDecl *CXXStructorImplicitParamDecl = nullptr;
1822 llvm::Value *CXXStructorImplicitParamValue = nullptr;
1824 /// OutermostConditional - Points to the outermost active
1825 /// conditional control. This is used so that we know if a
1826 /// temporary should be destroyed conditionally.
1827 ConditionalEvaluation *OutermostConditional = nullptr;
1829 /// The current lexical scope.
1830 LexicalScope *CurLexicalScope = nullptr;
1832 /// The current source location that should be used for exception
1834 SourceLocation CurEHLocation;
1836 /// BlockByrefInfos - For each __block variable, contains
1837 /// information about the layout of the variable.
1838 llvm::DenseMap<const ValueDecl *, BlockByrefInfo> BlockByrefInfos;
1840 /// Used by -fsanitize=nullability-return to determine whether the return
1841 /// value can be checked.
1842 llvm::Value *RetValNullabilityPrecondition = nullptr;
1844 /// Check if -fsanitize=nullability-return instrumentation is required for
1846 bool requiresReturnValueNullabilityCheck() const {
1847 return RetValNullabilityPrecondition;
1850 /// Used to store precise source locations for return statements by the
1851 /// runtime return value checks.
1852 Address ReturnLocation = Address::invalid();
1854 /// Check if the return value of this function requires sanitization.
1855 bool requiresReturnValueCheck() const;
1857 llvm::BasicBlock *TerminateLandingPad = nullptr;
1858 llvm::BasicBlock *TerminateHandler = nullptr;
1859 llvm::SmallVector<llvm::BasicBlock *, 2> TrapBBs;
1861 /// Terminate funclets keyed by parent funclet pad.
1862 llvm::MapVector<llvm::Value *, llvm::BasicBlock *> TerminateFunclets;
1864 /// Largest vector width used in ths function. Will be used to create a
1865 /// function attribute.
1866 unsigned LargestVectorWidth = 0;
1868 /// True if we need emit the life-time markers.
1869 const bool ShouldEmitLifetimeMarkers;
1871 /// Add OpenCL kernel arg metadata and the kernel attribute metadata to
1872 /// the function metadata.
1873 void EmitOpenCLKernelMetadata(const FunctionDecl *FD,
1874 llvm::Function *Fn);
1877 CodeGenFunction(CodeGenModule &cgm, bool suppressNewContext=false);
1880 CodeGenTypes &getTypes() const { return CGM.getTypes(); }
1881 ASTContext &getContext() const { return CGM.getContext(); }
1882 CGDebugInfo *getDebugInfo() {
1883 if (DisableDebugInfo)
1887 void disableDebugInfo() { DisableDebugInfo = true; }
1888 void enableDebugInfo() { DisableDebugInfo = false; }
1890 bool shouldUseFusedARCCalls() {
1891 return CGM.getCodeGenOpts().OptimizationLevel == 0;
1894 const LangOptions &getLangOpts() const { return CGM.getLangOpts(); }
1896 /// Returns a pointer to the function's exception object and selector slot,
1897 /// which is assigned in every landing pad.
1898 Address getExceptionSlot();
1899 Address getEHSelectorSlot();
1901 /// Returns the contents of the function's exception object and selector
1903 llvm::Value *getExceptionFromSlot();
1904 llvm::Value *getSelectorFromSlot();
1906 Address getNormalCleanupDestSlot();
1908 llvm::BasicBlock *getUnreachableBlock() {
1909 if (!UnreachableBlock) {
1910 UnreachableBlock = createBasicBlock("unreachable");
1911 new llvm::UnreachableInst(getLLVMContext(), UnreachableBlock);
1913 return UnreachableBlock;
1916 llvm::BasicBlock *getInvokeDest() {
1917 if (!EHStack.requiresLandingPad()) return nullptr;
1918 return getInvokeDestImpl();
1921 bool currentFunctionUsesSEHTry() const { return CurSEHParent != nullptr; }
1923 const TargetInfo &getTarget() const { return Target; }
1924 llvm::LLVMContext &getLLVMContext() { return CGM.getLLVMContext(); }
1925 const TargetCodeGenInfo &getTargetHooks() const {
1926 return CGM.getTargetCodeGenInfo();
1929 //===--------------------------------------------------------------------===//
1931 //===--------------------------------------------------------------------===//
1933 typedef void Destroyer(CodeGenFunction &CGF, Address addr, QualType ty);
1935 void pushIrregularPartialArrayCleanup(llvm::Value *arrayBegin,
1936 Address arrayEndPointer,
1937 QualType elementType,
1938 CharUnits elementAlignment,
1939 Destroyer *destroyer);
1940 void pushRegularPartialArrayCleanup(llvm::Value *arrayBegin,
1941 llvm::Value *arrayEnd,
1942 QualType elementType,
1943 CharUnits elementAlignment,
1944 Destroyer *destroyer);
1946 void pushDestroy(QualType::DestructionKind dtorKind,
1947 Address addr, QualType type);
1948 void pushEHDestroy(QualType::DestructionKind dtorKind,
1949 Address addr, QualType type);
1950 void pushDestroy(CleanupKind kind, Address addr, QualType type,
1951 Destroyer *destroyer, bool useEHCleanupForArray);
1952 void pushLifetimeExtendedDestroy(CleanupKind kind, Address addr,
1953 QualType type, Destroyer *destroyer,
1954 bool useEHCleanupForArray);
1955 void pushCallObjectDeleteCleanup(const FunctionDecl *OperatorDelete,
1956 llvm::Value *CompletePtr,
1957 QualType ElementType);
1958 void pushStackRestore(CleanupKind kind, Address SPMem);
1959 void emitDestroy(Address addr, QualType type, Destroyer *destroyer,
1960 bool useEHCleanupForArray);
1961 llvm::Function *generateDestroyHelper(Address addr, QualType type,
1962 Destroyer *destroyer,
1963 bool useEHCleanupForArray,
1965 void emitArrayDestroy(llvm::Value *begin, llvm::Value *end,
1966 QualType elementType, CharUnits elementAlign,
1967 Destroyer *destroyer,
1968 bool checkZeroLength, bool useEHCleanup);
1970 Destroyer *getDestroyer(QualType::DestructionKind destructionKind);
1972 /// Determines whether an EH cleanup is required to destroy a type
1973 /// with the given destruction kind.
1974 bool needsEHCleanup(QualType::DestructionKind kind) {
1976 case QualType::DK_none:
1978 case QualType::DK_cxx_destructor:
1979 case QualType::DK_objc_weak_lifetime:
1980 case QualType::DK_nontrivial_c_struct:
1981 return getLangOpts().Exceptions;
1982 case QualType::DK_objc_strong_lifetime:
1983 return getLangOpts().Exceptions &&
1984 CGM.getCodeGenOpts().ObjCAutoRefCountExceptions;
1986 llvm_unreachable("bad destruction kind");
1989 CleanupKind getCleanupKind(QualType::DestructionKind kind) {
1990 return (needsEHCleanup(kind) ? NormalAndEHCleanup : NormalCleanup);
1993 //===--------------------------------------------------------------------===//
1995 //===--------------------------------------------------------------------===//
1997 void GenerateObjCMethod(const ObjCMethodDecl *OMD);
1999 void StartObjCMethod(const ObjCMethodDecl *MD, const ObjCContainerDecl *CD);
2001 /// GenerateObjCGetter - Synthesize an Objective-C property getter function.
2002 void GenerateObjCGetter(ObjCImplementationDecl *IMP,
2003 const ObjCPropertyImplDecl *PID);
2004 void generateObjCGetterBody(const ObjCImplementationDecl *classImpl,
2005 const ObjCPropertyImplDecl *propImpl,
2006 const ObjCMethodDecl *GetterMothodDecl,
2007 llvm::Constant *AtomicHelperFn);
2009 void GenerateObjCCtorDtorMethod(ObjCImplementationDecl *IMP,
2010 ObjCMethodDecl *MD, bool ctor);
2012 /// GenerateObjCSetter - Synthesize an Objective-C property setter function
2013 /// for the given property.
2014 void GenerateObjCSetter(ObjCImplementationDecl *IMP,
2015 const ObjCPropertyImplDecl *PID);
2016 void generateObjCSetterBody(const ObjCImplementationDecl *classImpl,
2017 const ObjCPropertyImplDecl *propImpl,
2018 llvm::Constant *AtomicHelperFn);
2020 //===--------------------------------------------------------------------===//
2022 //===--------------------------------------------------------------------===//
2024 /// Emit block literal.
2025 /// \return an LLVM value which is a pointer to a struct which contains
2026 /// information about the block, including the block invoke function, the
2027 /// captured variables, etc.
2028 llvm::Value *EmitBlockLiteral(const BlockExpr *);
2030 llvm::Function *GenerateBlockFunction(GlobalDecl GD,
2031 const CGBlockInfo &Info,
2032 const DeclMapTy &ldm,
2033 bool IsLambdaConversionToBlock,
2034 bool BuildGlobalBlock);
2036 /// Check if \p T is a C++ class that has a destructor that can throw.
2037 static bool cxxDestructorCanThrow(QualType T);
2039 llvm::Constant *GenerateCopyHelperFunction(const CGBlockInfo &blockInfo);
2040 llvm::Constant *GenerateDestroyHelperFunction(const CGBlockInfo &blockInfo);
2041 llvm::Constant *GenerateObjCAtomicSetterCopyHelperFunction(
2042 const ObjCPropertyImplDecl *PID);
2043 llvm::Constant *GenerateObjCAtomicGetterCopyHelperFunction(
2044 const ObjCPropertyImplDecl *PID);
2045 llvm::Value *EmitBlockCopyAndAutorelease(llvm::Value *Block, QualType Ty);
2047 void BuildBlockRelease(llvm::Value *DeclPtr, BlockFieldFlags flags,
2050 class AutoVarEmission;
2052 void emitByrefStructureInit(const AutoVarEmission &emission);
2054 /// Enter a cleanup to destroy a __block variable. Note that this
2055 /// cleanup should be a no-op if the variable hasn't left the stack
2056 /// yet; if a cleanup is required for the variable itself, that needs
2057 /// to be done externally.
2059 /// \param Kind Cleanup kind.
2061 /// \param Addr When \p LoadBlockVarAddr is false, the address of the __block
2062 /// structure that will be passed to _Block_object_dispose. When
2063 /// \p LoadBlockVarAddr is true, the address of the field of the block
2064 /// structure that holds the address of the __block structure.
2066 /// \param Flags The flag that will be passed to _Block_object_dispose.
2068 /// \param LoadBlockVarAddr Indicates whether we need to emit a load from
2069 /// \p Addr to get the address of the __block structure.
2070 void enterByrefCleanup(CleanupKind Kind, Address Addr, BlockFieldFlags Flags,
2071 bool LoadBlockVarAddr, bool CanThrow);
2073 void setBlockContextParameter(const ImplicitParamDecl *D, unsigned argNum,
2076 Address LoadBlockStruct();
2077 Address GetAddrOfBlockDecl(const VarDecl *var);
2079 /// BuildBlockByrefAddress - Computes the location of the
2080 /// data in a variable which is declared as __block.
2081 Address emitBlockByrefAddress(Address baseAddr, const VarDecl *V,
2082 bool followForward = true);
2083 Address emitBlockByrefAddress(Address baseAddr,
2084 const BlockByrefInfo &info,
2086 const llvm::Twine &name);
2088 const BlockByrefInfo &getBlockByrefInfo(const VarDecl *var);
2090 QualType BuildFunctionArgList(GlobalDecl GD, FunctionArgList &Args);
2092 void GenerateCode(GlobalDecl GD, llvm::Function *Fn,
2093 const CGFunctionInfo &FnInfo);
2095 /// Annotate the function with an attribute that disables TSan checking at
2097 void markAsIgnoreThreadCheckingAtRuntime(llvm::Function *Fn);
2099 /// Emit code for the start of a function.
2100 /// \param Loc The location to be associated with the function.
2101 /// \param StartLoc The location of the function body.
2102 void StartFunction(GlobalDecl GD,
2105 const CGFunctionInfo &FnInfo,
2106 const FunctionArgList &Args,
2107 SourceLocation Loc = SourceLocation(),
2108 SourceLocation StartLoc = SourceLocation());
2110 static bool IsConstructorDelegationValid(const CXXConstructorDecl *Ctor);
2112 void EmitConstructorBody(FunctionArgList &Args);
2113 void EmitDestructorBody(FunctionArgList &Args);
2114 void emitImplicitAssignmentOperatorBody(FunctionArgList &Args);
2115 void EmitFunctionBody(const Stmt *Body);
2116 void EmitBlockWithFallThrough(llvm::BasicBlock *BB, const Stmt *S);
2118 void EmitForwardingCallToLambda(const CXXMethodDecl *LambdaCallOperator,
2119 CallArgList &CallArgs);
2120 void EmitLambdaBlockInvokeBody();
2121 void EmitLambdaDelegatingInvokeBody(const CXXMethodDecl *MD);
2122 void EmitLambdaStaticInvokeBody(const CXXMethodDecl *MD);
2123 void EmitLambdaVLACapture(const VariableArrayType *VAT, LValue LV) {
2124 EmitStoreThroughLValue(RValue::get(VLASizeMap[VAT->getSizeExpr()]), LV);
2126 void EmitAsanPrologueOrEpilogue(bool Prologue);
2128 /// Emit the unified return block, trying to avoid its emission when
2130 /// \return The debug location of the user written return statement if the
2131 /// return block is is avoided.
2132 llvm::DebugLoc EmitReturnBlock();
2134 /// FinishFunction - Complete IR generation of the current function. It is
2135 /// legal to call this function even if there is no current insertion point.
2136 void FinishFunction(SourceLocation EndLoc=SourceLocation());
2138 void StartThunk(llvm::Function *Fn, GlobalDecl GD,
2139 const CGFunctionInfo &FnInfo, bool IsUnprototyped);
2141 void EmitCallAndReturnForThunk(llvm::FunctionCallee Callee,
2142 const ThunkInfo *Thunk, bool IsUnprototyped);
2146 /// Emit a musttail call for a thunk with a potentially adjusted this pointer.
2147 void EmitMustTailThunk(GlobalDecl GD, llvm::Value *AdjustedThisPtr,
2148 llvm::FunctionCallee Callee);
2150 /// Generate a thunk for the given method.
2151 void generateThunk(llvm::Function *Fn, const CGFunctionInfo &FnInfo,
2152 GlobalDecl GD, const ThunkInfo &Thunk,
2153 bool IsUnprototyped);
2155 llvm::Function *GenerateVarArgsThunk(llvm::Function *Fn,
2156 const CGFunctionInfo &FnInfo,
2157 GlobalDecl GD, const ThunkInfo &Thunk);
2159 void EmitCtorPrologue(const CXXConstructorDecl *CD, CXXCtorType Type,
2160 FunctionArgList &Args);
2162 void EmitInitializerForField(FieldDecl *Field, LValue LHS, Expr *Init);
2164 /// Struct with all information about dynamic [sub]class needed to set vptr.
2167 const CXXRecordDecl *NearestVBase;
2168 CharUnits OffsetFromNearestVBase;
2169 const CXXRecordDecl *VTableClass;
2172 /// Initialize the vtable pointer of the given subobject.
2173 void InitializeVTablePointer(const VPtr &vptr);
2175 typedef llvm::SmallVector<VPtr, 4> VPtrsVector;
2177 typedef llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBasesSetTy;
2178 VPtrsVector getVTablePointers(const CXXRecordDecl *VTableClass);
2180 void getVTablePointers(BaseSubobject Base, const CXXRecordDecl *NearestVBase,
2181 CharUnits OffsetFromNearestVBase,
2182 bool BaseIsNonVirtualPrimaryBase,
2183 const CXXRecordDecl *VTableClass,
2184 VisitedVirtualBasesSetTy &VBases, VPtrsVector &vptrs);
2186 void InitializeVTablePointers(const CXXRecordDecl *ClassDecl);
2188 /// GetVTablePtr - Return the Value of the vtable pointer member pointed
2190 llvm::Value *GetVTablePtr(Address This, llvm::Type *VTableTy,
2191 const CXXRecordDecl *VTableClass);
2193 enum CFITypeCheckKind {
2197 CFITCK_UnrelatedCast,
2203 /// Derived is the presumed address of an object of type T after a
2204 /// cast. If T is a polymorphic class type, emit a check that the virtual
2205 /// table for Derived belongs to a class derived from T.
2206 void EmitVTablePtrCheckForCast(QualType T, llvm::Value *Derived,
2207 bool MayBeNull, CFITypeCheckKind TCK,
2208 SourceLocation Loc);
2210 /// EmitVTablePtrCheckForCall - Virtual method MD is being called via VTable.
2211 /// If vptr CFI is enabled, emit a check that VTable is valid.
2212 void EmitVTablePtrCheckForCall(const CXXRecordDecl *RD, llvm::Value *VTable,
2213 CFITypeCheckKind TCK, SourceLocation Loc);
2215 /// EmitVTablePtrCheck - Emit a check that VTable is a valid virtual table for
2216 /// RD using llvm.type.test.
2217 void EmitVTablePtrCheck(const CXXRecordDecl *RD, llvm::Value *VTable,
2218 CFITypeCheckKind TCK, SourceLocation Loc);
2220 /// If whole-program virtual table optimization is enabled, emit an assumption
2221 /// that VTable is a member of RD's type identifier. Or, if vptr CFI is
2222 /// enabled, emit a check that VTable is a member of RD's type identifier.
2223 void EmitTypeMetadataCodeForVCall(const CXXRecordDecl *RD,
2224 llvm::Value *VTable, SourceLocation Loc);
2226 /// Returns whether we should perform a type checked load when loading a
2227 /// virtual function for virtual calls to members of RD. This is generally
2228 /// true when both vcall CFI and whole-program-vtables are enabled.
2229 bool ShouldEmitVTableTypeCheckedLoad(const CXXRecordDecl *RD);
2231 /// Emit a type checked load from the given vtable.
2232 llvm::Value *EmitVTableTypeCheckedLoad(const CXXRecordDecl *RD, llvm::Value *VTable,
2233 uint64_t VTableByteOffset);
2235 /// EnterDtorCleanups - Enter the cleanups necessary to complete the
2236 /// given phase of destruction for a destructor. The end result
2237 /// should call destructors on members and base classes in reverse
2238 /// order of their construction.
2239 void EnterDtorCleanups(const CXXDestructorDecl *Dtor, CXXDtorType Type);
2241 /// ShouldInstrumentFunction - Return true if the current function should be
2242 /// instrumented with __cyg_profile_func_* calls
2243 bool ShouldInstrumentFunction();
2245 /// ShouldXRayInstrument - Return true if the current function should be
2246 /// instrumented with XRay nop sleds.
2247 bool ShouldXRayInstrumentFunction() const;
2249 /// AlwaysEmitXRayCustomEvents - Return true if we must unconditionally emit
2250 /// XRay custom event handling calls.
2251 bool AlwaysEmitXRayCustomEvents() const;
2253 /// AlwaysEmitXRayTypedEvents - Return true if clang must unconditionally emit
2254 /// XRay typed event handling calls.
2255 bool AlwaysEmitXRayTypedEvents() const;
2257 /// Encode an address into a form suitable for use in a function prologue.
2258 llvm::Constant *EncodeAddrForUseInPrologue(llvm::Function *F,
2259 llvm::Constant *Addr);
2261 /// Decode an address used in a function prologue, encoded by \c
2262 /// EncodeAddrForUseInPrologue.
2263 llvm::Value *DecodeAddrUsedInPrologue(llvm::Value *F,
2264 llvm::Value *EncodedAddr);
2266 /// EmitFunctionProlog - Emit the target specific LLVM code to load the
2267 /// arguments for the given function. This is also responsible for naming the
2268 /// LLVM function arguments.
2269 void EmitFunctionProlog(const CGFunctionInfo &FI,
2271 const FunctionArgList &Args);
2273 /// EmitFunctionEpilog - Emit the target specific LLVM code to return the
2274 /// given temporary.
2275 void EmitFunctionEpilog(const CGFunctionInfo &FI, bool EmitRetDbgLoc,
2276 SourceLocation EndLoc);
2278 /// Emit a test that checks if the return value \p RV is nonnull.
2279 void EmitReturnValueCheck(llvm::Value *RV);
2281 /// EmitStartEHSpec - Emit the start of the exception spec.
2282 void EmitStartEHSpec(const Decl *D);
2284 /// EmitEndEHSpec - Emit the end of the exception spec.
2285 void EmitEndEHSpec(const Decl *D);
2287 /// getTerminateLandingPad - Return a landing pad that just calls terminate.
2288 llvm::BasicBlock *getTerminateLandingPad();
2290 /// getTerminateLandingPad - Return a cleanup funclet that just calls
2292 llvm::BasicBlock *getTerminateFunclet();
2294 /// getTerminateHandler - Return a handler (not a landing pad, just
2295 /// a catch handler) that just calls terminate. This is used when
2296 /// a terminate scope encloses a try.
2297 llvm::BasicBlock *getTerminateHandler();
2299 llvm::Type *ConvertTypeForMem(QualType T);
2300 llvm::Type *ConvertType(QualType T);
2301 llvm::Type *ConvertType(const TypeDecl *T) {
2302 return ConvertType(getContext().getTypeDeclType(T));
2305 /// LoadObjCSelf - Load the value of self. This function is only valid while
2306 /// generating code for an Objective-C method.
2307 llvm::Value *LoadObjCSelf();
2309 /// TypeOfSelfObject - Return type of object that this self represents.
2310 QualType TypeOfSelfObject();
2312 /// getEvaluationKind - Return the TypeEvaluationKind of QualType \c T.
2313 static TypeEvaluationKind getEvaluationKind(QualType T);
2315 static bool hasScalarEvaluationKind(QualType T) {
2316 return getEvaluationKind(T) == TEK_Scalar;
2319 static bool hasAggregateEvaluationKind(QualType T) {
2320 return getEvaluationKind(T) == TEK_Aggregate;
2323 /// createBasicBlock - Create an LLVM basic block.
2324 llvm::BasicBlock *createBasicBlock(const Twine &name = "",
2325 llvm::Function *parent = nullptr,
2326 llvm::BasicBlock *before = nullptr) {
2327 return llvm::BasicBlock::Create(getLLVMContext(), name, parent, before);
2330 /// getBasicBlockForLabel - Return the LLVM basicblock that the specified
2332 JumpDest getJumpDestForLabel(const LabelDecl *S);
2334 /// SimplifyForwardingBlocks - If the given basic block is only a branch to
2335 /// another basic block, simplify it. This assumes that no other code could
2336 /// potentially reference the basic block.
2337 void SimplifyForwardingBlocks(llvm::BasicBlock *BB);
2339 /// EmitBlock - Emit the given block \arg BB and set it as the insert point,
2340 /// adding a fall-through branch from the current insert block if
2341 /// necessary. It is legal to call this function even if there is no current
2342 /// insertion point.
2344 /// IsFinished - If true, indicates that the caller has finished emitting
2345 /// branches to the given block and does not expect to emit code into it. This
2346 /// means the block can be ignored if it is unreachable.
2347 void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false);
2349 /// EmitBlockAfterUses - Emit the given block somewhere hopefully
2350 /// near its uses, and leave the insertion point in it.
2351 void EmitBlockAfterUses(llvm::BasicBlock *BB);
2353 /// EmitBranch - Emit a branch to the specified basic block from the current
2354 /// insert block, taking care to avoid creation of branches from dummy
2355 /// blocks. It is legal to call this function even if there is no current
2356 /// insertion point.
2358 /// This function clears the current insertion point. The caller should follow
2359 /// calls to this function with calls to Emit*Block prior to generation new
2361 void EmitBranch(llvm::BasicBlock *Block);
2363 /// HaveInsertPoint - True if an insertion point is defined. If not, this
2364 /// indicates that the current code being emitted is unreachable.
2365 bool HaveInsertPoint() const {
2366 return Builder.GetInsertBlock() != nullptr;
2369 /// EnsureInsertPoint - Ensure that an insertion point is defined so that
2370 /// emitted IR has a place to go. Note that by definition, if this function
2371 /// creates a block then that block is unreachable; callers may do better to
2372 /// detect when no insertion point is defined and simply skip IR generation.
2373 void EnsureInsertPoint() {
2374 if (!HaveInsertPoint())
2375 EmitBlock(createBasicBlock());
2378 /// ErrorUnsupported - Print out an error that codegen doesn't support the
2379 /// specified stmt yet.
2380 void ErrorUnsupported(const Stmt *S, const char *Type);
2382 //===--------------------------------------------------------------------===//
2384 //===--------------------------------------------------------------------===//
2386 LValue MakeAddrLValue(Address Addr, QualType T,
2387 AlignmentSource Source = AlignmentSource::Type) {
2388 return LValue::MakeAddr(Addr, T, getContext(), LValueBaseInfo(Source),
2389 CGM.getTBAAAccessInfo(T));
2392 LValue MakeAddrLValue(Address Addr, QualType T, LValueBaseInfo BaseInfo,
2393 TBAAAccessInfo TBAAInfo) {
2394 return LValue::MakeAddr(Addr, T, getContext(), BaseInfo, TBAAInfo);
2397 LValue MakeAddrLValue(llvm::Value *V, QualType T, CharUnits Alignment,
2398 AlignmentSource Source = AlignmentSource::Type) {
2399 return LValue::MakeAddr(Address(V, Alignment), T, getContext(),
2400 LValueBaseInfo(Source), CGM.getTBAAAccessInfo(T));
2403 LValue MakeAddrLValue(llvm::Value *V, QualType T, CharUnits Alignment,
2404 LValueBaseInfo BaseInfo, TBAAAccessInfo TBAAInfo) {
2405 return LValue::MakeAddr(Address(V, Alignment), T, getContext(),
2406 BaseInfo, TBAAInfo);
2409 LValue MakeNaturalAlignPointeeAddrLValue(llvm::Value *V, QualType T);
2410 LValue MakeNaturalAlignAddrLValue(llvm::Value *V, QualType T);
2412 Address EmitLoadOfReference(LValue RefLVal,
2413 LValueBaseInfo *PointeeBaseInfo = nullptr,
2414 TBAAAccessInfo *PointeeTBAAInfo = nullptr);
2415 LValue EmitLoadOfReferenceLValue(LValue RefLVal);
2416 LValue EmitLoadOfReferenceLValue(Address RefAddr, QualType RefTy,
2417 AlignmentSource Source =
2418 AlignmentSource::Type) {
2419 LValue RefLVal = MakeAddrLValue(RefAddr, RefTy, LValueBaseInfo(Source),
2420 CGM.getTBAAAccessInfo(RefTy));
2421 return EmitLoadOfReferenceLValue(RefLVal);
2424 Address EmitLoadOfPointer(Address Ptr, const PointerType *PtrTy,
2425 LValueBaseInfo *BaseInfo = nullptr,
2426 TBAAAccessInfo *TBAAInfo = nullptr);
2427 LValue EmitLoadOfPointerLValue(Address Ptr, const PointerType *PtrTy);
2429 /// CreateTempAlloca - This creates an alloca and inserts it into the entry
2430 /// block if \p ArraySize is nullptr, otherwise inserts it at the current
2431 /// insertion point of the builder. The caller is responsible for setting an
2432 /// appropriate alignment on
2435 /// \p ArraySize is the number of array elements to be allocated if it
2438 /// LangAS::Default is the address space of pointers to local variables and
2439 /// temporaries, as exposed in the source language. In certain
2440 /// configurations, this is not the same as the alloca address space, and a
2441 /// cast is needed to lift the pointer from the alloca AS into
2442 /// LangAS::Default. This can happen when the target uses a restricted
2443 /// address space for the stack but the source language requires
2444 /// LangAS::Default to be a generic address space. The latter condition is
2445 /// common for most programming languages; OpenCL is an exception in that
2446 /// LangAS::Default is the private address space, which naturally maps
2449 /// Because the address of a temporary is often exposed to the program in
2450 /// various ways, this function will perform the cast. The original alloca
2451 /// instruction is returned through \p Alloca if it is not nullptr.
2453 /// The cast is not performaed in CreateTempAllocaWithoutCast. This is
2454 /// more efficient if the caller knows that the address will not be exposed.
2455 llvm::AllocaInst *CreateTempAlloca(llvm::Type *Ty, const Twine &Name = "tmp",
2456 llvm::Value *ArraySize = nullptr);
2457 Address CreateTempAlloca(llvm::Type *Ty, CharUnits align,
2458 const Twine &Name = "tmp",
2459 llvm::Value *ArraySize = nullptr,
2460 Address *Alloca = nullptr);
2461 Address CreateTempAllocaWithoutCast(llvm::Type *Ty, CharUnits align,
2462 const Twine &Name = "tmp",
2463 llvm::Value *ArraySize = nullptr);
2465 /// CreateDefaultAlignedTempAlloca - This creates an alloca with the
2466 /// default ABI alignment of the given LLVM type.
2468 /// IMPORTANT NOTE: This is *not* generally the right alignment for
2469 /// any given AST type that happens to have been lowered to the
2470 /// given IR type. This should only ever be used for function-local,
2471 /// IR-driven manipulations like saving and restoring a value. Do
2472 /// not hand this address off to arbitrary IRGen routines, and especially
2473 /// do not pass it as an argument to a function that might expect a
2474 /// properly ABI-aligned value.
2475 Address CreateDefaultAlignTempAlloca(llvm::Type *Ty,
2476 const Twine &Name = "tmp");
2478 /// InitTempAlloca - Provide an initial value for the given alloca which
2479 /// will be observable at all locations in the function.
2481 /// The address should be something that was returned from one of
2482 /// the CreateTempAlloca or CreateMemTemp routines, and the
2483 /// initializer must be valid in the entry block (i.e. it must
2484 /// either be a constant or an argument value).
2485 void InitTempAlloca(Address Alloca, llvm::Value *Value);
2487 /// CreateIRTemp - Create a temporary IR object of the given type, with
2488 /// appropriate alignment. This routine should only be used when an temporary
2489 /// value needs to be stored into an alloca (for example, to avoid explicit
2490 /// PHI construction), but the type is the IR type, not the type appropriate
2491 /// for storing in memory.
2493 /// That is, this is exactly equivalent to CreateMemTemp, but calling
2494 /// ConvertType instead of ConvertTypeForMem.
2495 Address CreateIRTemp(QualType T, const Twine &Name = "tmp");
2497 /// CreateMemTemp - Create a temporary memory object of the given type, with
2498 /// appropriate alignmen and cast it to the default address space. Returns
2499 /// the original alloca instruction by \p Alloca if it is not nullptr.
2500 Address CreateMemTemp(QualType T, const Twine &Name = "tmp",
2501 Address *Alloca = nullptr);
2502 Address CreateMemTemp(QualType T, CharUnits Align, const Twine &Name = "tmp",
2503 Address *Alloca = nullptr);
2505 /// CreateMemTemp - Create a temporary memory object of the given type, with
2506 /// appropriate alignmen without casting it to the default address space.
2507 Address CreateMemTempWithoutCast(QualType T, const Twine &Name = "tmp");
2508 Address CreateMemTempWithoutCast(QualType T, CharUnits Align,
2509 const Twine &Name = "tmp");
2511 /// CreateAggTemp - Create a temporary memory object for the given
2513 AggValueSlot CreateAggTemp(QualType T, const Twine &Name = "tmp",
2514 Address *Alloca = nullptr) {
2515 return AggValueSlot::forAddr(CreateMemTemp(T, Name, Alloca),
2517 AggValueSlot::IsNotDestructed,
2518 AggValueSlot::DoesNotNeedGCBarriers,
2519 AggValueSlot::IsNotAliased,
2520 AggValueSlot::DoesNotOverlap);
2523 /// Emit a cast to void* in the appropriate address space.
2524 llvm::Value *EmitCastToVoidPtr(llvm::Value *value);
2526 /// EvaluateExprAsBool - Perform the usual unary conversions on the specified
2527 /// expression and compare the result against zero, returning an Int1Ty value.
2528 llvm::Value *EvaluateExprAsBool(const Expr *E);
2530 /// EmitIgnoredExpr - Emit an expression in a context which ignores the result.
2531 void EmitIgnoredExpr(const Expr *E);
2533 /// EmitAnyExpr - Emit code to compute the specified expression which can have
2534 /// any type. The result is returned as an RValue struct. If this is an
2535 /// aggregate expression, the aggloc/agglocvolatile arguments indicate where
2536 /// the result should be returned.
2538 /// \param ignoreResult True if the resulting value isn't used.
2539 RValue EmitAnyExpr(const Expr *E,
2540 AggValueSlot aggSlot = AggValueSlot::ignored(),
2541 bool ignoreResult = false);
2543 // EmitVAListRef - Emit a "reference" to a va_list; this is either the address
2544 // or the value of the expression, depending on how va_list is defined.
2545 Address EmitVAListRef(const Expr *E);
2547 /// Emit a "reference" to a __builtin_ms_va_list; this is
2548 /// always the value of the expression, because a __builtin_ms_va_list is a
2549 /// pointer to a char.
2550 Address EmitMSVAListRef(const Expr *E);
2552 /// EmitAnyExprToTemp - Similarly to EmitAnyExpr(), however, the result will
2553 /// always be accessible even if no aggregate location is provided.
2554 RValue EmitAnyExprToTemp(const Expr *E);
2556 /// EmitAnyExprToMem - Emits the code necessary to evaluate an
2557 /// arbitrary expression into the given memory location.
2558 void EmitAnyExprToMem(const Expr *E, Address Location,
2559 Qualifiers Quals, bool IsInitializer);
2561 void EmitAnyExprToExn(const Expr *E, Address Addr);
2563 /// EmitExprAsInit - Emits the code necessary to initialize a
2564 /// location in memory with the given initializer.
2565 void EmitExprAsInit(const Expr *init, const ValueDecl *D, LValue lvalue,
2566 bool capturedByInit);
2568 /// hasVolatileMember - returns true if aggregate type has a volatile
2570 bool hasVolatileMember(QualType T) {
2571 if (const RecordType *RT = T->getAs<RecordType>()) {
2572 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
2573 return RD->hasVolatileMember();
2578 /// Determine whether a return value slot may overlap some other object.
2579 AggValueSlot::Overlap_t getOverlapForReturnValue() {
2580 // FIXME: Assuming no overlap here breaks guaranteed copy elision for base
2581 // class subobjects. These cases may need to be revisited depending on the
2582 // resolution of the relevant core issue.
2583 return AggValueSlot::DoesNotOverlap;
2586 /// Determine whether a field initialization may overlap some other object.
2587 AggValueSlot::Overlap_t getOverlapForFieldInit(const FieldDecl *FD);
2589 /// Determine whether a base class initialization may overlap some other
2591 AggValueSlot::Overlap_t getOverlapForBaseInit(const CXXRecordDecl *RD,
2592 const CXXRecordDecl *BaseRD,
2595 /// Emit an aggregate assignment.
2596 void EmitAggregateAssign(LValue Dest, LValue Src, QualType EltTy) {
2597 bool IsVolatile = hasVolatileMember(EltTy);
2598 EmitAggregateCopy(Dest, Src, EltTy, AggValueSlot::MayOverlap, IsVolatile);
2601 void EmitAggregateCopyCtor(LValue Dest, LValue Src,
2602 AggValueSlot::Overlap_t MayOverlap) {
2603 EmitAggregateCopy(Dest, Src, Src.getType(), MayOverlap);
2606 /// EmitAggregateCopy - Emit an aggregate copy.
2608 /// \param isVolatile \c true iff either the source or the destination is
2610 /// \param MayOverlap Whether the tail padding of the destination might be
2611 /// occupied by some other object. More efficient code can often be
2612 /// generated if not.
2613 void EmitAggregateCopy(LValue Dest, LValue Src, QualType EltTy,
2614 AggValueSlot::Overlap_t MayOverlap,
2615 bool isVolatile = false);
2617 /// GetAddrOfLocalVar - Return the address of a local variable.
2618 Address GetAddrOfLocalVar(const VarDecl *VD) {
2619 auto it = LocalDeclMap.find(VD);
2620 assert(it != LocalDeclMap.end() &&
2621 "Invalid argument to GetAddrOfLocalVar(), no decl!");
2625 /// Given an opaque value expression, return its LValue mapping if it exists,
2626 /// otherwise create one.
2627 LValue getOrCreateOpaqueLValueMapping(const OpaqueValueExpr *e);
2629 /// Given an opaque value expression, return its RValue mapping if it exists,
2630 /// otherwise create one.
2631 RValue getOrCreateOpaqueRValueMapping(const OpaqueValueExpr *e);
2633 /// Get the index of the current ArrayInitLoopExpr, if any.
2634 llvm::Value *getArrayInitIndex() { return ArrayInitIndex; }
2636 /// getAccessedFieldNo - Given an encoded value and a result number, return
2637 /// the input field number being accessed.
2638 static unsigned getAccessedFieldNo(unsigned Idx, const llvm::Constant *Elts);
2640 llvm::BlockAddress *GetAddrOfLabel(const LabelDecl *L);
2641 llvm::BasicBlock *GetIndirectGotoBlock();
2643 /// Check if \p E is a C++ "this" pointer wrapped in value-preserving casts.
2644 static bool IsWrappedCXXThis(const Expr *E);
2646 /// EmitNullInitialization - Generate code to set a value of the given type to
2647 /// null, If the type contains data member pointers, they will be initialized
2648 /// to -1 in accordance with the Itanium C++ ABI.
2649 void EmitNullInitialization(Address DestPtr, QualType Ty);
2651 /// Emits a call to an LLVM variable-argument intrinsic, either
2652 /// \c llvm.va_start or \c llvm.va_end.
2653 /// \param ArgValue A reference to the \c va_list as emitted by either
2654 /// \c EmitVAListRef or \c EmitMSVAListRef.
2655 /// \param IsStart If \c true, emits a call to \c llvm.va_start; otherwise,
2656 /// calls \c llvm.va_end.
2657 llvm::Value *EmitVAStartEnd(llvm::Value *ArgValue, bool IsStart);
2659 /// Generate code to get an argument from the passed in pointer
2660 /// and update it accordingly.
2661 /// \param VE The \c VAArgExpr for which to generate code.
2662 /// \param VAListAddr Receives a reference to the \c va_list as emitted by
2663 /// either \c EmitVAListRef or \c EmitMSVAListRef.
2664 /// \returns A pointer to the argument.
2665 // FIXME: We should be able to get rid of this method and use the va_arg
2666 // instruction in LLVM instead once it works well enough.
2667 Address EmitVAArg(VAArgExpr *VE, Address &VAListAddr);
2669 /// emitArrayLength - Compute the length of an array, even if it's a
2670 /// VLA, and drill down to the base element type.
2671 llvm::Value *emitArrayLength(const ArrayType *arrayType,
2675 /// EmitVLASize - Capture all the sizes for the VLA expressions in
2676 /// the given variably-modified type and store them in the VLASizeMap.
2678 /// This function can be called with a null (unreachable) insert point.
2679 void EmitVariablyModifiedType(QualType Ty);
2681 struct VlaSizePair {
2682 llvm::Value *NumElts;
2685 VlaSizePair(llvm::Value *NE, QualType T) : NumElts(NE), Type(T) {}
2688 /// Return the number of elements for a single dimension
2689 /// for the given array type.
2690 VlaSizePair getVLAElements1D(const VariableArrayType *vla);
2691 VlaSizePair getVLAElements1D(QualType vla);
2693 /// Returns an LLVM value that corresponds to the size,
2694 /// in non-variably-sized elements, of a variable length array type,
2695 /// plus that largest non-variably-sized element type. Assumes that
2696 /// the type has already been emitted with EmitVariablyModifiedType.
2697 VlaSizePair getVLASize(const VariableArrayType *vla);
2698 VlaSizePair getVLASize(QualType vla);
2700 /// LoadCXXThis - Load the value of 'this'. This function is only valid while
2701 /// generating code for an C++ member function.
2702 llvm::Value *LoadCXXThis() {
2703 assert(CXXThisValue && "no 'this' value for this function");
2704 return CXXThisValue;
2706 Address LoadCXXThisAddress();
2708 /// LoadCXXVTT - Load the VTT parameter to base constructors/destructors have
2710 // FIXME: Every place that calls LoadCXXVTT is something
2711 // that needs to be abstracted properly.
2712 llvm::Value *LoadCXXVTT() {
2713 assert(CXXStructorImplicitParamValue && "no VTT value for this function");
2714 return CXXStructorImplicitParamValue;
2717 /// GetAddressOfBaseOfCompleteClass - Convert the given pointer to a
2718 /// complete class to the given direct base.
2720 GetAddressOfDirectBaseInCompleteClass(Address Value,
2721 const CXXRecordDecl *Derived,
2722 const CXXRecordDecl *Base,
2723 bool BaseIsVirtual);
2725 static bool ShouldNullCheckClassCastValue(const CastExpr *Cast);
2727 /// GetAddressOfBaseClass - This function will add the necessary delta to the
2728 /// load of 'this' and returns address of the base class.
2729 Address GetAddressOfBaseClass(Address Value,
2730 const CXXRecordDecl *Derived,
2731 CastExpr::path_const_iterator PathBegin,
2732 CastExpr::path_const_iterator PathEnd,
2733 bool NullCheckValue, SourceLocation Loc);
2735 Address GetAddressOfDerivedClass(Address Value,
2736 const CXXRecordDecl *Derived,
2737 CastExpr::path_const_iterator PathBegin,
2738 CastExpr::path_const_iterator PathEnd,
2739 bool NullCheckValue);
2741 /// GetVTTParameter - Return the VTT parameter that should be passed to a
2742 /// base constructor/destructor with virtual bases.
2743 /// FIXME: VTTs are Itanium ABI-specific, so the definition should move
2744 /// to ItaniumCXXABI.cpp together with all the references to VTT.
2745 llvm::Value *GetVTTParameter(GlobalDecl GD, bool ForVirtualBase,
2748 void EmitDelegateCXXConstructorCall(const CXXConstructorDecl *Ctor,
2749 CXXCtorType CtorType,
2750 const FunctionArgList &Args,
2751 SourceLocation Loc);
2752 // It's important not to confuse this and the previous function. Delegating
2753 // constructors are the C++0x feature. The constructor delegate optimization
2754 // is used to reduce duplication in the base and complete consturctors where
2755 // they are substantially the same.
2756 void EmitDelegatingCXXConstructorCall(const CXXConstructorDecl *Ctor,
2757 const FunctionArgList &Args);
2759 /// Emit a call to an inheriting constructor (that is, one that invokes a
2760 /// constructor inherited from a base class) by inlining its definition. This
2761 /// is necessary if the ABI does not support forwarding the arguments to the
2762 /// base class constructor (because they're variadic or similar).
2763 void EmitInlinedInheritingCXXConstructorCall(const CXXConstructorDecl *Ctor,
2764 CXXCtorType CtorType,
2765 bool ForVirtualBase,
2769 /// Emit a call to a constructor inherited from a base class, passing the
2770 /// current constructor's arguments along unmodified (without even making
2772 void EmitInheritedCXXConstructorCall(const CXXConstructorDecl *D,
2773 bool ForVirtualBase, Address This,
2774 bool InheritedFromVBase,
2775 const CXXInheritedCtorInitExpr *E);
2777 void EmitCXXConstructorCall(const CXXConstructorDecl *D, CXXCtorType Type,
2778 bool ForVirtualBase, bool Delegating,
2779 AggValueSlot ThisAVS, const CXXConstructExpr *E);
2781 void EmitCXXConstructorCall(const CXXConstructorDecl *D, CXXCtorType Type,
2782 bool ForVirtualBase, bool Delegating,
2783 Address This, CallArgList &Args,
2784 AggValueSlot::Overlap_t Overlap,
2785 SourceLocation Loc, bool NewPointerIsChecked);
2787 /// Emit assumption load for all bases. Requires to be be called only on
2788 /// most-derived class and not under construction of the object.
2789 void EmitVTableAssumptionLoads(const CXXRecordDecl *ClassDecl, Address This);
2791 /// Emit assumption that vptr load == global vtable.
2792 void EmitVTableAssumptionLoad(const VPtr &vptr, Address This);
2794 void EmitSynthesizedCXXCopyCtorCall(const CXXConstructorDecl *D,
2795 Address This, Address Src,
2796 const CXXConstructExpr *E);
2798 void EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
2799 const ArrayType *ArrayTy,
2801 const CXXConstructExpr *E,
2802 bool NewPointerIsChecked,
2803 bool ZeroInitialization = false);
2805 void EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
2806 llvm::Value *NumElements,
2808 const CXXConstructExpr *E,
2809 bool NewPointerIsChecked,
2810 bool ZeroInitialization = false);
2812 static Destroyer destroyCXXObject;
2814 void EmitCXXDestructorCall(const CXXDestructorDecl *D, CXXDtorType Type,
2815 bool ForVirtualBase, bool Delegating, Address This,
2818 void EmitNewArrayInitializer(const CXXNewExpr *E, QualType elementType,
2819 llvm::Type *ElementTy, Address NewPtr,
2820 llvm::Value *NumElements,
2821 llvm::Value *AllocSizeWithoutCookie);
2823 void EmitCXXTemporary(const CXXTemporary *Temporary, QualType TempType,
2826 llvm::Value *EmitLifetimeStart(uint64_t Size, llvm::Value *Addr);
2827 void EmitLifetimeEnd(llvm::Value *Size, llvm::Value *Addr);
2829 llvm::Value *EmitCXXNewExpr(const CXXNewExpr *E);
2830 void EmitCXXDeleteExpr(const CXXDeleteExpr *E);
2832 void EmitDeleteCall(const FunctionDecl *DeleteFD, llvm::Value *Ptr,
2833 QualType DeleteTy, llvm::Value *NumElements = nullptr,
2834 CharUnits CookieSize = CharUnits());
2836 RValue EmitBuiltinNewDeleteCall(const FunctionProtoType *Type,
2837 const CallExpr *TheCallExpr, bool IsDelete);
2839 llvm::Value *EmitCXXTypeidExpr(const CXXTypeidExpr *E);
2840 llvm::Value *EmitDynamicCast(Address V, const CXXDynamicCastExpr *DCE);
2841 Address EmitCXXUuidofExpr(const CXXUuidofExpr *E);
2843 /// Situations in which we might emit a check for the suitability of a
2844 /// pointer or glvalue. Needs to be kept in sync with ubsan_handlers.cpp in
2846 enum TypeCheckKind {
2847 /// Checking the operand of a load. Must be suitably sized and aligned.
2849 /// Checking the destination of a store. Must be suitably sized and aligned.
2851 /// Checking the bound value in a reference binding. Must be suitably sized
2852 /// and aligned, but is not required to refer to an object (until the
2853 /// reference is used), per core issue 453.
2854 TCK_ReferenceBinding,
2855 /// Checking the object expression in a non-static data member access. Must
2856 /// be an object within its lifetime.
2858 /// Checking the 'this' pointer for a call to a non-static member function.
2859 /// Must be an object within its lifetime.
2861 /// Checking the 'this' pointer for a constructor call.
2862 TCK_ConstructorCall,
2863 /// Checking the operand of a static_cast to a derived pointer type. Must be
2864 /// null or an object within its lifetime.
2865 TCK_DowncastPointer,
2866 /// Checking the operand of a static_cast to a derived reference type. Must
2867 /// be an object within its lifetime.
2868 TCK_DowncastReference,
2869 /// Checking the operand of a cast to a base object. Must be suitably sized
2872 /// Checking the operand of a cast to a virtual base object. Must be an
2873 /// object within its lifetime.
2874 TCK_UpcastToVirtualBase,
2875 /// Checking the value assigned to a _Nonnull pointer. Must not be null.
2877 /// Checking the operand of a dynamic_cast or a typeid expression. Must be
2878 /// null or an object within its lifetime.
2879 TCK_DynamicOperation
2882 /// Determine whether the pointer type check \p TCK permits null pointers.
2883 static bool isNullPointerAllowed(TypeCheckKind TCK);
2885 /// Determine whether the pointer type check \p TCK requires a vptr check.
2886 static bool isVptrCheckRequired(TypeCheckKind TCK, QualType Ty);
2888 /// Whether any type-checking sanitizers are enabled. If \c false,
2889 /// calls to EmitTypeCheck can be skipped.
2890 bool sanitizePerformTypeCheck() const;
2892 /// Emit a check that \p V is the address of storage of the
2893 /// appropriate size and alignment for an object of type \p Type
2894 /// (or if ArraySize is provided, for an array of that bound).
2895 void EmitTypeCheck(TypeCheckKind TCK, SourceLocation Loc, llvm::Value *V,
2896 QualType Type, CharUnits Alignment = CharUnits::Zero(),
2897 SanitizerSet SkippedChecks = SanitizerSet(),
2898 llvm::Value *ArraySize = nullptr);
2900 /// Emit a check that \p Base points into an array object, which
2901 /// we can access at index \p Index. \p Accessed should be \c false if we
2902 /// this expression is used as an lvalue, for instance in "&Arr[Idx]".
2903 void EmitBoundsCheck(const Expr *E, const Expr *Base, llvm::Value *Index,
2904 QualType IndexType, bool Accessed);
2906 llvm::Value *EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV,
2907 bool isInc, bool isPre);
2908 ComplexPairTy EmitComplexPrePostIncDec(const UnaryOperator *E, LValue LV,
2909 bool isInc, bool isPre);
2911 /// Converts Location to a DebugLoc, if debug information is enabled.
2912 llvm::DebugLoc SourceLocToDebugLoc(SourceLocation Location);
2914 /// Get the record field index as represented in debug info.
2915 unsigned getDebugInfoFIndex(const RecordDecl *Rec, unsigned FieldIndex);
2918 //===--------------------------------------------------------------------===//
2919 // Declaration Emission
2920 //===--------------------------------------------------------------------===//
2922 /// EmitDecl - Emit a declaration.
2924 /// This function can be called with a null (unreachable) insert point.
2925 void EmitDecl(const Decl &D);
2927 /// EmitVarDecl - Emit a local variable declaration.
2929 /// This function can be called with a null (unreachable) insert point.
2930 void EmitVarDecl(const VarDecl &D);
2932 void EmitScalarInit(const Expr *init, const ValueDecl *D, LValue lvalue,
2933 bool capturedByInit);
2935 typedef void SpecialInitFn(CodeGenFunction &Init, const VarDecl &D,
2936 llvm::Value *Address);
2938 /// Determine whether the given initializer is trivial in the sense
2939 /// that it requires no code to be generated.
2940 bool isTrivialInitializer(const Expr *Init);
2942 /// EmitAutoVarDecl - Emit an auto variable declaration.
2944 /// This function can be called with a null (unreachable) insert point.
2945 void EmitAutoVarDecl(const VarDecl &D);
2947 class AutoVarEmission {
2948 friend class CodeGenFunction;
2950 const VarDecl *Variable;
2952 /// The address of the alloca for languages with explicit address space
2953 /// (e.g. OpenCL) or alloca casted to generic pointer for address space
2954 /// agnostic languages (e.g. C++). Invalid if the variable was emitted
2955 /// as a global constant.
2958 llvm::Value *NRVOFlag;
2960 /// True if the variable is a __block variable that is captured by an
2962 bool IsEscapingByRef;
2964 /// True if the variable is of aggregate type and has a constant
2966 bool IsConstantAggregate;
2968 /// Non-null if we should use lifetime annotations.
2969 llvm::Value *SizeForLifetimeMarkers;
2971 /// Address with original alloca instruction. Invalid if the variable was
2972 /// emitted as a global constant.
2976 AutoVarEmission(Invalid)
2977 : Variable(nullptr), Addr(Address::invalid()),
2978 AllocaAddr(Address::invalid()) {}
2980 AutoVarEmission(const VarDecl &variable)
2981 : Variable(&variable), Addr(Address::invalid()), NRVOFlag(nullptr),
2982 IsEscapingByRef(false), IsConstantAggregate(false),
2983 SizeForLifetimeMarkers(nullptr), AllocaAddr(Address::invalid()) {}
2985 bool wasEmittedAsGlobal() const { return !Addr.isValid(); }
2988 static AutoVarEmission invalid() { return AutoVarEmission(Invalid()); }
2990 bool useLifetimeMarkers() const {
2991 return SizeForLifetimeMarkers != nullptr;
2993 llvm::Value *getSizeForLifetimeMarkers() const {
2994 assert(useLifetimeMarkers());
2995 return SizeForLifetimeMarkers;
2998 /// Returns the raw, allocated address, which is not necessarily
2999 /// the address of the object itself. It is casted to default
3000 /// address space for address space agnostic languages.
3001 Address getAllocatedAddress() const {
3005 /// Returns the address for the original alloca instruction.
3006 Address getOriginalAllocatedAddress() const { return AllocaAddr; }
3008 /// Returns the address of the object within this declaration.
3009 /// Note that this does not chase the forwarding pointer for
3011 Address getObjectAddress(CodeGenFunction &CGF) const {
3012 if (!IsEscapingByRef) return Addr;
3014 return CGF.emitBlockByrefAddress(Addr, Variable, /*forward*/ false);
3017 AutoVarEmission EmitAutoVarAlloca(const VarDecl &var);
3018 void EmitAutoVarInit(const AutoVarEmission &emission);
3019 void EmitAutoVarCleanups(const AutoVarEmission &emission);
3020 void emitAutoVarTypeCleanup(const AutoVarEmission &emission,
3021 QualType::DestructionKind dtorKind);
3023 /// Emits the alloca and debug information for the size expressions for each
3024 /// dimension of an array. It registers the association of its (1-dimensional)
3025 /// QualTypes and size expression's debug node, so that CGDebugInfo can
3026 /// reference this node when creating the DISubrange object to describe the
3028 void EmitAndRegisterVariableArrayDimensions(CGDebugInfo *DI,
3030 bool EmitDebugInfo);
3032 void EmitStaticVarDecl(const VarDecl &D,
3033 llvm::GlobalValue::LinkageTypes Linkage);
3038 ParamValue(llvm::Value *V, unsigned A) : Value(V), Alignment(A) {}
3040 static ParamValue forDirect(llvm::Value *value) {
3041 return ParamValue(value, 0);
3043 static ParamValue forIndirect(Address addr) {
3044 assert(!addr.getAlignment().isZero());
3045 return ParamValue(addr.getPointer(), addr.getAlignment().getQuantity());
3048 bool isIndirect() const { return Alignment != 0; }
3049 llvm::Value *getAnyValue() const { return Value; }
3051 llvm::Value *getDirectValue() const {
3052 assert(!isIndirect());
3056 Address getIndirectAddress() const {
3057 assert(isIndirect());
3058 return Address(Value, CharUnits::fromQuantity(Alignment));
3062 /// EmitParmDecl - Emit a ParmVarDecl or an ImplicitParamDecl.
3063 void EmitParmDecl(const VarDecl &D, ParamValue Arg, unsigned ArgNo);
3065 /// protectFromPeepholes - Protect a value that we're intending to
3066 /// store to the side, but which will probably be used later, from
3067 /// aggressive peepholing optimizations that might delete it.
3069 /// Pass the result to unprotectFromPeepholes to declare that
3070 /// protection is no longer required.
3072 /// There's no particular reason why this shouldn't apply to
3073 /// l-values, it's just that no existing peepholes work on pointers.
3074 PeepholeProtection protectFromPeepholes(RValue rvalue);
3075 void unprotectFromPeepholes(PeepholeProtection protection);
3077 void emitAlignmentAssumptionCheck(llvm::Value *Ptr, QualType Ty,
3079 SourceLocation AssumptionLoc,
3080 llvm::Value *Alignment,
3081 llvm::Value *OffsetValue,
3082 llvm::Value *TheCheck,
3083 llvm::Instruction *Assumption);
3085 void emitAlignmentAssumption(llvm::Value *PtrValue, QualType Ty,
3086 SourceLocation Loc, SourceLocation AssumptionLoc,
3087 llvm::Value *Alignment,
3088 llvm::Value *OffsetValue = nullptr);
3090 void emitAlignmentAssumption(llvm::Value *PtrValue, const Expr *E,
3091 SourceLocation AssumptionLoc,
3092 llvm::Value *Alignment,
3093 llvm::Value *OffsetValue = nullptr);
3095 //===--------------------------------------------------------------------===//
3096 // Statement Emission
3097 //===--------------------------------------------------------------------===//
3099 /// EmitStopPoint - Emit a debug stoppoint if we are emitting debug info.
3100 void EmitStopPoint(const Stmt *S);
3102 /// EmitStmt - Emit the code for the statement \arg S. It is legal to call
3103 /// this function even if there is no current insertion point.
3105 /// This function may clear the current insertion point; callers should use
3106 /// EnsureInsertPoint if they wish to subsequently generate code without first
3107 /// calling EmitBlock, EmitBranch, or EmitStmt.
3108 void EmitStmt(const Stmt *S, ArrayRef<const Attr *> Attrs = None);
3110 /// EmitSimpleStmt - Try to emit a "simple" statement which does not
3111 /// necessarily require an insertion point or debug information; typically
3112 /// because the statement amounts to a jump or a container of other
3115 /// \return True if the statement was handled.
3116 bool EmitSimpleStmt(const Stmt *S, ArrayRef<const Attr *> Attrs);
3118 Address EmitCompoundStmt(const CompoundStmt &S, bool GetLast = false,
3119 AggValueSlot AVS = AggValueSlot::ignored());
3120 Address EmitCompoundStmtWithoutScope(const CompoundStmt &S,
3121 bool GetLast = false,
3123 AggValueSlot::ignored());
3125 /// EmitLabel - Emit the block for the given label. It is legal to call this
3126 /// function even if there is no current insertion point.
3127 void EmitLabel(const LabelDecl *D); // helper for EmitLabelStmt.
3129 void EmitLabelStmt(const LabelStmt &S);
3130 void EmitAttributedStmt(const AttributedStmt &S);
3131 void EmitGotoStmt(const GotoStmt &S);
3132 void EmitIndirectGotoStmt(const IndirectGotoStmt &S);
3133 void EmitIfStmt(const IfStmt &S);
3135 void EmitWhileStmt(const WhileStmt &S,
3136 ArrayRef<const Attr *> Attrs = None);
3137 void EmitDoStmt(const DoStmt &S, ArrayRef<const Attr *> Attrs = None);
3138 void EmitForStmt(const ForStmt &S,
3139 ArrayRef<const Attr *> Attrs = None);
3140 void EmitReturnStmt(const ReturnStmt &S);
3141 void EmitDeclStmt(const DeclStmt &S);
3142 void EmitBreakStmt(const BreakStmt &S);
3143 void EmitContinueStmt(const ContinueStmt &S);
3144 void EmitSwitchStmt(const SwitchStmt &S);
3145 void EmitDefaultStmt(const DefaultStmt &S, ArrayRef<const Attr *> Attrs);
3146 void EmitCaseStmt(const CaseStmt &S, ArrayRef<const Attr *> Attrs);
3147 void EmitCaseStmtRange(const CaseStmt &S, ArrayRef<const Attr *> Attrs);
3148 void EmitAsmStmt(const AsmStmt &S);
3150 void EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S);
3151 void EmitObjCAtTryStmt(const ObjCAtTryStmt &S);
3152 void EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S);
3153 void EmitObjCAtSynchronizedStmt(const ObjCAtSynchronizedStmt &S);
3154 void EmitObjCAutoreleasePoolStmt(const ObjCAutoreleasePoolStmt &S);
3156 void EmitCoroutineBody(const CoroutineBodyStmt &S);
3157 void EmitCoreturnStmt(const CoreturnStmt &S);
3158 RValue EmitCoawaitExpr(const CoawaitExpr &E,
3159 AggValueSlot aggSlot = AggValueSlot::ignored(),
3160 bool ignoreResult = false);
3161 LValue EmitCoawaitLValue(const CoawaitExpr *E);
3162 RValue EmitCoyieldExpr(const CoyieldExpr &E,
3163 AggValueSlot aggSlot = AggValueSlot::ignored(),
3164 bool ignoreResult = false);
3165 LValue EmitCoyieldLValue(const CoyieldExpr *E);
3166 RValue EmitCoroutineIntrinsic(const CallExpr *E, unsigned int IID);
3168 void EnterCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock = false);
3169 void ExitCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock = false);
3171 void EmitCXXTryStmt(const CXXTryStmt &S);
3172 void EmitSEHTryStmt(const SEHTryStmt &S);
3173 void EmitSEHLeaveStmt(const SEHLeaveStmt &S);
3174 void EnterSEHTryStmt(const SEHTryStmt &S);
3175 void ExitSEHTryStmt(const SEHTryStmt &S);
3177 void pushSEHCleanup(CleanupKind kind,
3178 llvm::Function *FinallyFunc);
3179 void startOutlinedSEHHelper(CodeGenFunction &ParentCGF, bool IsFilter,
3180 const Stmt *OutlinedStmt);
3182 llvm::Function *GenerateSEHFilterFunction(CodeGenFunction &ParentCGF,
3183 const SEHExceptStmt &Except);
3185 llvm::Function *GenerateSEHFinallyFunction(CodeGenFunction &ParentCGF,
3186 const SEHFinallyStmt &Finally);
3188 void EmitSEHExceptionCodeSave(CodeGenFunction &ParentCGF,
3189 llvm::Value *ParentFP,
3190 llvm::Value *EntryEBP);
3191 llvm::Value *EmitSEHExceptionCode();
3192 llvm::Value *EmitSEHExceptionInfo();
3193 llvm::Value *EmitSEHAbnormalTermination();
3195 /// Emit simple code for OpenMP directives in Simd-only mode.
3196 void EmitSimpleOMPExecutableDirective(const OMPExecutableDirective &D);
3198 /// Scan the outlined statement for captures from the parent function. For
3199 /// each capture, mark the capture as escaped and emit a call to
3200 /// llvm.localrecover. Insert the localrecover result into the LocalDeclMap.
3201 void EmitCapturedLocals(CodeGenFunction &ParentCGF, const Stmt *OutlinedStmt,
3204 /// Recovers the address of a local in a parent function. ParentVar is the
3205 /// address of the variable used in the immediate parent function. It can
3206 /// either be an alloca or a call to llvm.localrecover if there are nested
3207 /// outlined functions. ParentFP is the frame pointer of the outermost parent
3209 Address recoverAddrOfEscapedLocal(CodeGenFunction &ParentCGF,
3211 llvm::Value *ParentFP);
3213 void EmitCXXForRangeStmt(const CXXForRangeStmt &S,
3214 ArrayRef<const Attr *> Attrs = None);
3216 /// Controls insertion of cancellation exit blocks in worksharing constructs.
3217 class OMPCancelStackRAII {
3218 CodeGenFunction &CGF;
3221 OMPCancelStackRAII(CodeGenFunction &CGF, OpenMPDirectiveKind Kind,
3224 CGF.OMPCancelStack.enter(CGF, Kind, HasCancel);
3226 ~OMPCancelStackRAII() { CGF.OMPCancelStack.exit(CGF); }
3229 /// Returns calculated size of the specified type.
3230 llvm::Value *getTypeSize(QualType Ty);
3231 LValue InitCapturedStruct(const CapturedStmt &S);
3232 llvm::Function *EmitCapturedStmt(const CapturedStmt &S, CapturedRegionKind K);
3233 llvm::Function *GenerateCapturedStmtFunction(const CapturedStmt &S);
3234 Address GenerateCapturedStmtArgument(const CapturedStmt &S);
3235 llvm::Function *GenerateOpenMPCapturedStmtFunction(const CapturedStmt &S,
3236 SourceLocation Loc);
3237 void GenerateOpenMPCapturedVars(const CapturedStmt &S,
3238 SmallVectorImpl<llvm::Value *> &CapturedVars);
3239 void emitOMPSimpleStore(LValue LVal, RValue RVal, QualType RValTy,
3240 SourceLocation Loc);
3241 /// Perform element by element copying of arrays with type \a
3242 /// OriginalType from \a SrcAddr to \a DestAddr using copying procedure
3243 /// generated by \a CopyGen.
3245 /// \param DestAddr Address of the destination array.
3246 /// \param SrcAddr Address of the source array.
3247 /// \param OriginalType Type of destination and source arrays.
3248 /// \param CopyGen Copying procedure that copies value of single array element
3249 /// to another single array element.
3250 void EmitOMPAggregateAssign(
3251 Address DestAddr, Address SrcAddr, QualType OriginalType,
3252 const llvm::function_ref<void(Address, Address)> CopyGen);
3253 /// Emit proper copying of data from one variable to another.
3255 /// \param OriginalType Original type of the copied variables.
3256 /// \param DestAddr Destination address.
3257 /// \param SrcAddr Source address.
3258 /// \param DestVD Destination variable used in \a CopyExpr (for arrays, has
3259 /// type of the base array element).
3260 /// \param SrcVD Source variable used in \a CopyExpr (for arrays, has type of
3261 /// the base array element).
3262 /// \param Copy Actual copygin expression for copying data from \a SrcVD to \a
3264 void EmitOMPCopy(QualType OriginalType,
3265 Address DestAddr, Address SrcAddr,
3266 const VarDecl *DestVD, const VarDecl *SrcVD,
3268 /// Emit atomic update code for constructs: \a X = \a X \a BO \a E or
3269 /// \a X = \a E \a BO \a E.
3271 /// \param X Value to be updated.
3272 /// \param E Update value.
3273 /// \param BO Binary operation for update operation.
3274 /// \param IsXLHSInRHSPart true if \a X is LHS in RHS part of the update
3275 /// expression, false otherwise.
3276 /// \param AO Atomic ordering of the generated atomic instructions.
3277 /// \param CommonGen Code generator for complex expressions that cannot be
3278 /// expressed through atomicrmw instruction.
3279 /// \returns <true, OldAtomicValue> if simple 'atomicrmw' instruction was
3280 /// generated, <false, RValue::get(nullptr)> otherwise.
3281 std::pair<bool, RValue> EmitOMPAtomicSimpleUpdateExpr(
3282 LValue X, RValue E, BinaryOperatorKind BO, bool IsXLHSInRHSPart,
3283 llvm::AtomicOrdering AO, SourceLocation Loc,
3284 const llvm::function_ref<RValue(RValue)> CommonGen);
3285 bool EmitOMPFirstprivateClause(const OMPExecutableDirective &D,
3286 OMPPrivateScope &PrivateScope);
3287 void EmitOMPPrivateClause(const OMPExecutableDirective &D,
3288 OMPPrivateScope &PrivateScope);
3289 void EmitOMPUseDevicePtrClause(
3290 const OMPUseDevicePtrClause &C, OMPPrivateScope &PrivateScope,
3291 const llvm::DenseMap<const ValueDecl *, Address> &CaptureDeviceAddrMap);
3292 void EmitOMPUseDeviceAddrClause(
3293 const OMPUseDeviceAddrClause &C, OMPPrivateScope &PrivateScope,
3294 const llvm::DenseMap<const ValueDecl *, Address> &CaptureDeviceAddrMap);
3295 /// Emit code for copyin clause in \a D directive. The next code is
3296 /// generated at the start of outlined functions for directives:
3298 /// threadprivate_var1 = master_threadprivate_var1;
3299 /// operator=(threadprivate_var2, master_threadprivate_var2);
3301 /// __kmpc_barrier(&loc, global_tid);
3304 /// \param D OpenMP directive possibly with 'copyin' clause(s).
3305 /// \returns true if at least one copyin variable is found, false otherwise.
3306 bool EmitOMPCopyinClause(const OMPExecutableDirective &D);
3307 /// Emit initial code for lastprivate variables. If some variable is
3308 /// not also firstprivate, then the default initialization is used. Otherwise
3309 /// initialization of this variable is performed by EmitOMPFirstprivateClause
3312 /// \param D Directive that may have 'lastprivate' directives.
3313 /// \param PrivateScope Private scope for capturing lastprivate variables for
3314 /// proper codegen in internal captured statement.
3316 /// \returns true if there is at least one lastprivate variable, false
3318 bool EmitOMPLastprivateClauseInit(const OMPExecutableDirective &D,
3319 OMPPrivateScope &PrivateScope);
3320 /// Emit final copying of lastprivate values to original variables at
3321 /// the end of the worksharing or simd directive.
3323 /// \param D Directive that has at least one 'lastprivate' directives.
3324 /// \param IsLastIterCond Boolean condition that must be set to 'i1 true' if
3325 /// it is the last iteration of the loop code in associated directive, or to
3326 /// 'i1 false' otherwise. If this item is nullptr, no final check is required.
3327 void EmitOMPLastprivateClauseFinal(const OMPExecutableDirective &D,
3329 llvm::Value *IsLastIterCond = nullptr);
3330 /// Emit initial code for linear clauses.
3331 void EmitOMPLinearClause(const OMPLoopDirective &D,
3332 CodeGenFunction::OMPPrivateScope &PrivateScope);
3333 /// Emit final code for linear clauses.
3334 /// \param CondGen Optional conditional code for final part of codegen for
3336 void EmitOMPLinearClauseFinal(
3337 const OMPLoopDirective &D,
3338 const llvm::function_ref<llvm::Value *(CodeGenFunction &)> CondGen);
3339 /// Emit initial code for reduction variables. Creates reduction copies
3340 /// and initializes them with the values according to OpenMP standard.
3342 /// \param D Directive (possibly) with the 'reduction' clause.
3343 /// \param PrivateScope Private scope for capturing reduction variables for
3344 /// proper codegen in internal captured statement.
3346 void EmitOMPReductionClauseInit(const OMPExecutableDirective &D,
3347 OMPPrivateScope &PrivateScope,
3348 bool ForInscan = false);
3349 /// Emit final update of reduction values to original variables at
3350 /// the end of the directive.
3352 /// \param D Directive that has at least one 'reduction' directives.
3353 /// \param ReductionKind The kind of reduction to perform.
3354 void EmitOMPReductionClauseFinal(const OMPExecutableDirective &D,
3355 const OpenMPDirectiveKind ReductionKind);
3356 /// Emit initial code for linear variables. Creates private copies
3357 /// and initializes them with the values according to OpenMP standard.
3359 /// \param D Directive (possibly) with the 'linear' clause.
3360 /// \return true if at least one linear variable is found that should be
3361 /// initialized with the value of the original variable, false otherwise.
3362 bool EmitOMPLinearClauseInit(const OMPLoopDirective &D);
3364 typedef const llvm::function_ref<void(CodeGenFunction & /*CGF*/,
3365 llvm::Function * /*OutlinedFn*/,
3366 const OMPTaskDataTy & /*Data*/)>
3368 void EmitOMPTaskBasedDirective(const OMPExecutableDirective &S,
3369 const OpenMPDirectiveKind CapturedRegion,
3370 const RegionCodeGenTy &BodyGen,
3371 const TaskGenTy &TaskGen, OMPTaskDataTy &Data);
3372 struct OMPTargetDataInfo {
3373 Address BasePointersArray = Address::invalid();
3374 Address PointersArray = Address::invalid();
3375 Address SizesArray = Address::invalid();
3376 Address MappersArray = Address::invalid();
3377 unsigned NumberOfTargetItems = 0;
3378 explicit OMPTargetDataInfo() = default;
3379 OMPTargetDataInfo(Address BasePointersArray, Address PointersArray,
3380 Address SizesArray, Address MappersArray,
3381 unsigned NumberOfTargetItems)
3382 : BasePointersArray(BasePointersArray), PointersArray(PointersArray),
3383 SizesArray(SizesArray), MappersArray(MappersArray),
3384 NumberOfTargetItems(NumberOfTargetItems) {}
3386 void EmitOMPTargetTaskBasedDirective(const OMPExecutableDirective &S,
3387 const RegionCodeGenTy &BodyGen,
3388 OMPTargetDataInfo &InputInfo);
3390 void EmitOMPParallelDirective(const OMPParallelDirective &S);
3391 void EmitOMPSimdDirective(const OMPSimdDirective &S);
3392 void EmitOMPForDirective(const OMPForDirective &S);
3393 void EmitOMPForSimdDirective(const OMPForSimdDirective &S);
3394 void EmitOMPSectionsDirective(const OMPSectionsDirective &S);
3395 void EmitOMPSectionDirective(const OMPSectionDirective &S);
3396 void EmitOMPSingleDirective(const OMPSingleDirective &S);
3397 void EmitOMPMasterDirective(const OMPMasterDirective &S);
3398 void EmitOMPCriticalDirective(const OMPCriticalDirective &S);
3399 void EmitOMPParallelForDirective(const OMPParallelForDirective &S);
3400 void EmitOMPParallelForSimdDirective(const OMPParallelForSimdDirective &S);
3401 void EmitOMPParallelSectionsDirective(const OMPParallelSectionsDirective &S);
3402 void EmitOMPParallelMasterDirective(const OMPParallelMasterDirective &S);
3403 void EmitOMPTaskDirective(const OMPTaskDirective &S);
3404 void EmitOMPTaskyieldDirective(const OMPTaskyieldDirective &S);
3405 void EmitOMPBarrierDirective(const OMPBarrierDirective &S);
3406 void EmitOMPTaskwaitDirective(const OMPTaskwaitDirective &S);
3407 void EmitOMPTaskgroupDirective(const OMPTaskgroupDirective &S);
3408 void EmitOMPFlushDirective(const OMPFlushDirective &S);
3409 void EmitOMPDepobjDirective(const OMPDepobjDirective &S);
3410 void EmitOMPScanDirective(const OMPScanDirective &S);
3411 void EmitOMPOrderedDirective(const OMPOrderedDirective &S);
3412 void EmitOMPAtomicDirective(const OMPAtomicDirective &S);
3413 void EmitOMPTargetDirective(const OMPTargetDirective &S);
3414 void EmitOMPTargetDataDirective(const OMPTargetDataDirective &S);
3415 void EmitOMPTargetEnterDataDirective(const OMPTargetEnterDataDirective &S);
3416 void EmitOMPTargetExitDataDirective(const OMPTargetExitDataDirective &S);
3417 void EmitOMPTargetUpdateDirective(const OMPTargetUpdateDirective &S);
3418 void EmitOMPTargetParallelDirective(const OMPTargetParallelDirective &S);
3420 EmitOMPTargetParallelForDirective(const OMPTargetParallelForDirective &S);
3421 void EmitOMPTeamsDirective(const OMPTeamsDirective &S);
3423 EmitOMPCancellationPointDirective(const OMPCancellationPointDirective &S);
3424 void EmitOMPCancelDirective(const OMPCancelDirective &S);
3425 void EmitOMPTaskLoopBasedDirective(const OMPLoopDirective &S);
3426 void EmitOMPTaskLoopDirective(const OMPTaskLoopDirective &S);