1//===-- CodeGenFunction.h - Per-Function state for LLVM CodeGen -*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This is the internal per-function state used for llvm translation.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENFUNCTION_H
14#define LLVM_CLANG_LIB_CODEGEN_CODEGENFUNCTION_H
15
16#include "CGBuilder.h"
17#include "CGDebugInfo.h"
18#include "CGLoopInfo.h"
19#include "CGValue.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"
44#include <optional>
45
46namespace llvm {
47class BasicBlock;
48class LLVMContext;
49class MDNode;
50class SwitchInst;
51class Twine;
52class Value;
53class CanonicalLoopInfo;
54}
55
56namespace clang {
57class ASTContext;
58class CXXDestructorDecl;
59class CXXForRangeStmt;
60class CXXTryStmt;
61class Decl;
62class LabelDecl;
63class FunctionDecl;
64class FunctionProtoType;
65class LabelStmt;
66class ObjCContainerDecl;
67class ObjCInterfaceDecl;
68class ObjCIvarDecl;
69class ObjCMethodDecl;
70class ObjCImplementationDecl;
71class ObjCPropertyImplDecl;
72class TargetInfo;
73class VarDecl;
74class ObjCForCollectionStmt;
75class ObjCAtTryStmt;
76class ObjCAtThrowStmt;
77class ObjCAtSynchronizedStmt;
78class ObjCAutoreleasePoolStmt;
79class OMPUseDevicePtrClause;
80class OMPUseDeviceAddrClause;
81class SVETypeFlags;
82class OMPExecutableDirective;
83
84namespace analyze_os_log {
85class OSLogBufferLayout;
86}
87
88namespace CodeGen {
89class CodeGenTypes;
90class CGCallee;
91class CGFunctionInfo;
92class CGBlockInfo;
93class CGCXXABI;
94class BlockByrefHelpers;
95class BlockByrefInfo;
96class BlockFieldFlags;
97class RegionCodeGenTy;
98class TargetCodeGenInfo;
99struct OMPTaskDataTy;
100struct CGCoroData;
101
102/// The kind of evaluation to perform on values of a particular
103/// type.  Basically, is the code in CGExprScalar, CGExprComplex, or
104/// CGExprAgg?
105///
106/// TODO: should vectors maybe be split out into their own thing?
107enum TypeEvaluationKind {
108  TEK_Scalar,
109  TEK_Complex,
110  TEK_Aggregate
111};
112
113#define LIST_SANITIZER_CHECKS                                                  \
114  SANITIZER_CHECK(AddOverflow, add_overflow, 0)                                \
115  SANITIZER_CHECK(BuiltinUnreachable, builtin_unreachable, 0)                  \
116  SANITIZER_CHECK(CFICheckFail, cfi_check_fail, 0)                             \
117  SANITIZER_CHECK(DivremOverflow, divrem_overflow, 0)                          \
118  SANITIZER_CHECK(DynamicTypeCacheMiss, dynamic_type_cache_miss, 0)            \
119  SANITIZER_CHECK(FloatCastOverflow, float_cast_overflow, 0)                   \
120  SANITIZER_CHECK(FunctionTypeMismatch, function_type_mismatch, 1)             \
121  SANITIZER_CHECK(ImplicitConversion, implicit_conversion, 0)                  \
122  SANITIZER_CHECK(InvalidBuiltin, invalid_builtin, 0)                          \
123  SANITIZER_CHECK(InvalidObjCCast, invalid_objc_cast, 0)                       \
124  SANITIZER_CHECK(LoadInvalidValue, load_invalid_value, 0)                     \
125  SANITIZER_CHECK(MissingReturn, missing_return, 0)                            \
126  SANITIZER_CHECK(MulOverflow, mul_overflow, 0)                                \
127  SANITIZER_CHECK(NegateOverflow, negate_overflow, 0)                          \
128  SANITIZER_CHECK(NullabilityArg, nullability_arg, 0)                          \
129  SANITIZER_CHECK(NullabilityReturn, nullability_return, 1)                    \
130  SANITIZER_CHECK(NonnullArg, nonnull_arg, 0)                                  \
131  SANITIZER_CHECK(NonnullReturn, nonnull_return, 1)                            \
132  SANITIZER_CHECK(OutOfBounds, out_of_bounds, 0)                               \
133  SANITIZER_CHECK(PointerOverflow, pointer_overflow, 0)                        \
134  SANITIZER_CHECK(ShiftOutOfBounds, shift_out_of_bounds, 0)                    \
135  SANITIZER_CHECK(SubOverflow, sub_overflow, 0)                                \
136  SANITIZER_CHECK(TypeMismatch, type_mismatch, 1)                              \
137  SANITIZER_CHECK(AlignmentAssumption, alignment_assumption, 0)                \
138  SANITIZER_CHECK(VLABoundNotPositive, vla_bound_not_positive, 0)
139
140enum SanitizerHandler {
141#define SANITIZER_CHECK(Enum, Name, Version) Enum,
142  LIST_SANITIZER_CHECKS
143#undef SANITIZER_CHECK
144};
145
146/// Helper class with most of the code for saving a value for a
147/// conditional expression cleanup.
148struct DominatingLLVMValue {
149  typedef llvm::PointerIntPair<llvm::Value*, 1, bool> saved_type;
150
151  /// Answer whether the given value needs extra work to be saved.
152  static bool needsSaving(llvm::Value *value) {
153    // If it's not an instruction, we don't need to save.
154    if (!isa<llvm::Instruction>(value)) return false;
155
156    // If it's an instruction in the entry block, we don't need to save.
157    llvm::BasicBlock *block = cast<llvm::Instruction>(value)->getParent();
158    return (block != &block->getParent()->getEntryBlock());
159  }
160
161  static saved_type save(CodeGenFunction &CGF, llvm::Value *value);
162  static llvm::Value *restore(CodeGenFunction &CGF, saved_type value);
163};
164
165/// A partial specialization of DominatingValue for llvm::Values that
166/// might be llvm::Instructions.
167template <class T> struct DominatingPointer<T,true> : DominatingLLVMValue {
168  typedef T *type;
169  static type restore(CodeGenFunction &CGF, saved_type value) {
170    return static_cast<T*>(DominatingLLVMValue::restore(CGF, value));
171  }
172};
173
174/// A specialization of DominatingValue for Address.
175template <> struct DominatingValue<Address> {
176  typedef Address type;
177
178  struct saved_type {
179    DominatingLLVMValue::saved_type SavedValue;
180    llvm::Type *ElementType;
181    CharUnits Alignment;
182  };
183
184  static bool needsSaving(type value) {
185    return DominatingLLVMValue::needsSaving(value.getPointer());
186  }
187  static saved_type save(CodeGenFunction &CGF, type value) {
188    return { DominatingLLVMValue::save(CGF, value.getPointer()),
189             value.getElementType(), value.getAlignment() };
190  }
191  static type restore(CodeGenFunction &CGF, saved_type value) {
192    return Address(DominatingLLVMValue::restore(CGF, value.SavedValue),
193                   value.ElementType, value.Alignment);
194  }
195};
196
197/// A specialization of DominatingValue for RValue.
198template <> struct DominatingValue<RValue> {
199  typedef RValue type;
200  class saved_type {
201    enum Kind { ScalarLiteral, ScalarAddress, AggregateLiteral,
202                AggregateAddress, ComplexAddress };
203
204    llvm::Value *Value;
205    llvm::Type *ElementType;
206    unsigned K : 3;
207    unsigned Align : 29;
208    saved_type(llvm::Value *v, llvm::Type *e, Kind k, unsigned a = 0)
209      : Value(v), ElementType(e), K(k), Align(a) {}
210
211  public:
212    static bool needsSaving(RValue value);
213    static saved_type save(CodeGenFunction &CGF, RValue value);
214    RValue restore(CodeGenFunction &CGF);
215
216    // implementations in CGCleanup.cpp
217  };
218
219  static bool needsSaving(type value) {
220    return saved_type::needsSaving(value);
221  }
222  static saved_type save(CodeGenFunction &CGF, type value) {
223    return saved_type::save(CGF, value);
224  }
225  static type restore(CodeGenFunction &CGF, saved_type value) {
226    return value.restore(CGF);
227  }
228};
229
230/// CodeGenFunction - This class organizes the per-function state that is used
231/// while generating LLVM code.
232class CodeGenFunction : public CodeGenTypeCache {
233  CodeGenFunction(const CodeGenFunction &) = delete;
234  void operator=(const CodeGenFunction &) = delete;
235
236  friend class CGCXXABI;
237public:
238  /// A jump destination is an abstract label, branching to which may
239  /// require a jump out through normal cleanups.
240  struct JumpDest {
241    JumpDest() : Block(nullptr), Index(0) {}
242    JumpDest(llvm::BasicBlock *Block, EHScopeStack::stable_iterator Depth,
243             unsigned Index)
244        : Block(Block), ScopeDepth(Depth), Index(Index) {}
245
246    bool isValid() const { return Block != nullptr; }
247    llvm::BasicBlock *getBlock() const { return Block; }
248    EHScopeStack::stable_iterator getScopeDepth() const { return ScopeDepth; }
249    unsigned getDestIndex() const { return Index; }
250
251    // This should be used cautiously.
252    void setScopeDepth(EHScopeStack::stable_iterator depth) {
253      ScopeDepth = depth;
254    }
255
256  private:
257    llvm::BasicBlock *Block;
258    EHScopeStack::stable_iterator ScopeDepth;
259    unsigned Index;
260  };
261
262  CodeGenModule &CGM;  // Per-module state.
263  const TargetInfo &Target;
264
265  // For EH/SEH outlined funclets, this field points to parent's CGF
266  CodeGenFunction *ParentCGF = nullptr;
267
268  typedef std::pair<llvm::Value *, llvm::Value *> ComplexPairTy;
269  LoopInfoStack LoopStack;
270  CGBuilderTy Builder;
271
272  // Stores variables for which we can't generate correct lifetime markers
273  // because of jumps.
274  VarBypassDetector Bypasses;
275
276  /// List of recently emitted OMPCanonicalLoops.
277  ///
278  /// Since OMPCanonicalLoops are nested inside other statements (in particular
279  /// CapturedStmt generated by OMPExecutableDirective and non-perfectly nested
280  /// loops), we cannot directly call OMPEmitOMPCanonicalLoop and receive its
281  /// llvm::CanonicalLoopInfo. Instead, we call EmitStmt and any
282  /// OMPEmitOMPCanonicalLoop called by it will add its CanonicalLoopInfo to
283  /// this stack when done. Entering a new loop requires clearing this list; it
284  /// either means we start parsing a new loop nest (in which case the previous
285  /// loop nest goes out of scope) or a second loop in the same level in which
286  /// case it would be ambiguous into which of the two (or more) loops the loop
287  /// nest would extend.
288  SmallVector<llvm::CanonicalLoopInfo *, 4> OMPLoopNestStack;
289
290  /// Number of nested loop to be consumed by the last surrounding
291  /// loop-associated directive.
292  int ExpectedOMPLoopDepth = 0;
293
294  // CodeGen lambda for loops and support for ordered clause
295  typedef llvm::function_ref<void(CodeGenFunction &, const OMPLoopDirective &,
296                                  JumpDest)>
297      CodeGenLoopTy;
298  typedef llvm::function_ref<void(CodeGenFunction &, SourceLocation,
299                                  const unsigned, const bool)>
300      CodeGenOrderedTy;
301
302  // Codegen lambda for loop bounds in worksharing loop constructs
303  typedef llvm::function_ref<std::pair<LValue, LValue>(
304      CodeGenFunction &, const OMPExecutableDirective &S)>
305      CodeGenLoopBoundsTy;
306
307  // Codegen lambda for loop bounds in dispatch-based loop implementation
308  typedef llvm::function_ref<std::pair<llvm::Value *, llvm::Value *>(
309      CodeGenFunction &, const OMPExecutableDirective &S, Address LB,
310      Address UB)>
311      CodeGenDispatchBoundsTy;
312
313  /// CGBuilder insert helper. This function is called after an
314  /// instruction is created using Builder.
315  void InsertHelper(llvm::Instruction *I, const llvm::Twine &Name,
316                    llvm::BasicBlock *BB,
317                    llvm::BasicBlock::iterator InsertPt) const;
318
319  /// CurFuncDecl - Holds the Decl for the current outermost
320  /// non-closure context.
321  const Decl *CurFuncDecl;
322  /// CurCodeDecl - This is the inner-most code context, which includes blocks.
323  const Decl *CurCodeDecl;
324  const CGFunctionInfo *CurFnInfo;
325  QualType FnRetTy;
326  llvm::Function *CurFn = nullptr;
327
328  /// Save Parameter Decl for coroutine.
329  llvm::SmallVector<const ParmVarDecl *, 4> FnArgs;
330
331  // Holds coroutine data if the current function is a coroutine. We use a
332  // wrapper to manage its lifetime, so that we don't have to define CGCoroData
333  // in this header.
334  struct CGCoroInfo {
335    std::unique_ptr<CGCoroData> Data;
336    CGCoroInfo();
337    ~CGCoroInfo();
338  };
339  CGCoroInfo CurCoro;
340
341  bool isCoroutine() const {
342    return CurCoro.Data != nullptr;
343  }
344
345  /// CurGD - The GlobalDecl for the current function being compiled.
346  GlobalDecl CurGD;
347
348  /// PrologueCleanupDepth - The cleanup depth enclosing all the
349  /// cleanups associated with the parameters.
350  EHScopeStack::stable_iterator PrologueCleanupDepth;
351
352  /// ReturnBlock - Unified return block.
353  JumpDest ReturnBlock;
354
355  /// ReturnValue - The temporary alloca to hold the return
356  /// value. This is invalid iff the function has no return value.
357  Address ReturnValue = Address::invalid();
358
359  /// ReturnValuePointer - The temporary alloca to hold a pointer to sret.
360  /// This is invalid if sret is not in use.
361  Address ReturnValuePointer = Address::invalid();
362
363  /// If a return statement is being visited, this holds the return statment's
364  /// result expression.
365  const Expr *RetExpr = nullptr;
366
367  /// Return true if a label was seen in the current scope.
368  bool hasLabelBeenSeenInCurrentScope() const {
369    if (CurLexicalScope)
370      return CurLexicalScope->hasLabels();
371    return !LabelMap.empty();
372  }
373
374  /// AllocaInsertPoint - This is an instruction in the entry block before which
375  /// we prefer to insert allocas.
376  llvm::AssertingVH<llvm::Instruction> AllocaInsertPt;
377
378private:
379  /// PostAllocaInsertPt - This is a place in the prologue where code can be
380  /// inserted that will be dominated by all the static allocas. This helps
381  /// achieve two things:
382  ///   1. Contiguity of all static allocas (within the prologue) is maintained.
383  ///   2. All other prologue code (which are dominated by static allocas) do
384  ///      appear in the source order immediately after all static allocas.
385  ///
386  /// PostAllocaInsertPt will be lazily created when it is *really* required.
387  llvm::AssertingVH<llvm::Instruction> PostAllocaInsertPt = nullptr;
388
389public:
390  /// Return PostAllocaInsertPt. If it is not yet created, then insert it
391  /// immediately after AllocaInsertPt.
392  llvm::Instruction *getPostAllocaInsertPoint() {
393    if (!PostAllocaInsertPt) {
394      assert(AllocaInsertPt &&
395             "Expected static alloca insertion point at function prologue");
396      assert(AllocaInsertPt->getParent()->isEntryBlock() &&
397             "EBB should be entry block of the current code gen function");
398      PostAllocaInsertPt = AllocaInsertPt->clone();
399      PostAllocaInsertPt->setName("postallocapt");
400      PostAllocaInsertPt->insertAfter(AllocaInsertPt);
401    }
402
403    return PostAllocaInsertPt;
404  }
405
406  /// API for captured statement code generation.
407  class CGCapturedStmtInfo {
408  public:
409    explicit CGCapturedStmtInfo(CapturedRegionKind K = CR_Default)
410        : Kind(K), ThisValue(nullptr), CXXThisFieldDecl(nullptr) {}
411    explicit CGCapturedStmtInfo(const CapturedStmt &S,
412                                CapturedRegionKind K = CR_Default)
413      : Kind(K), ThisValue(nullptr), CXXThisFieldDecl(nullptr) {
414
415      RecordDecl::field_iterator Field =
416        S.getCapturedRecordDecl()->field_begin();
417      for (CapturedStmt::const_capture_iterator I = S.capture_begin(),
418                                                E = S.capture_end();
419           I != E; ++I, ++Field) {
420        if (I->capturesThis())
421          CXXThisFieldDecl = *Field;
422        else if (I->capturesVariable())
423          CaptureFields[I->getCapturedVar()->getCanonicalDecl()] = *Field;
424        else if (I->capturesVariableByCopy())
425          CaptureFields[I->getCapturedVar()->getCanonicalDecl()] = *Field;
426      }
427    }
428
429    virtual ~CGCapturedStmtInfo();
430
431    CapturedRegionKind getKind() const { return Kind; }
432
433    virtual void setContextValue(llvm::Value *V) { ThisValue = V; }
434    // Retrieve the value of the context parameter.
435    virtual llvm::Value *getContextValue() const { return ThisValue; }
436
437    /// Lookup the captured field decl for a variable.
438    virtual const FieldDecl *lookup(const VarDecl *VD) const {
439      return CaptureFields.lookup(VD->getCanonicalDecl());
440    }
441
442    bool isCXXThisExprCaptured() const { return getThisFieldDecl() != nullptr; }
443    virtual FieldDecl *getThisFieldDecl() const { return CXXThisFieldDecl; }
444
445    static bool classof(const CGCapturedStmtInfo *) {
446      return true;
447    }
448
449    /// Emit the captured statement body.
450    virtual void EmitBody(CodeGenFunction &CGF, const Stmt *S) {
451      CGF.incrementProfileCounter(S);
452      CGF.EmitStmt(S);
453    }
454
455    /// Get the name of the capture helper.
456    virtual StringRef getHelperName() const { return "__captured_stmt"; }
457
458    /// Get the CaptureFields
459    llvm::SmallDenseMap<const VarDecl *, FieldDecl *> getCaptureFields() {
460      return CaptureFields;
461    }
462
463  private:
464    /// The kind of captured statement being generated.
465    CapturedRegionKind Kind;
466
467    /// Keep the map between VarDecl and FieldDecl.
468    llvm::SmallDenseMap<const VarDecl *, FieldDecl *> CaptureFields;
469
470    /// The base address of the captured record, passed in as the first
471    /// argument of the parallel region function.
472    llvm::Value *ThisValue;
473
474    /// Captured 'this' type.
475    FieldDecl *CXXThisFieldDecl;
476  };
477  CGCapturedStmtInfo *CapturedStmtInfo = nullptr;
478
479  /// RAII for correct setting/restoring of CapturedStmtInfo.
480  class CGCapturedStmtRAII {
481  private:
482    CodeGenFunction &CGF;
483    CGCapturedStmtInfo *PrevCapturedStmtInfo;
484  public:
485    CGCapturedStmtRAII(CodeGenFunction &CGF,
486                       CGCapturedStmtInfo *NewCapturedStmtInfo)
487        : CGF(CGF), PrevCapturedStmtInfo(CGF.CapturedStmtInfo) {
488      CGF.CapturedStmtInfo = NewCapturedStmtInfo;
489    }
490    ~CGCapturedStmtRAII() { CGF.CapturedStmtInfo = PrevCapturedStmtInfo; }
491  };
492
493  /// An abstract representation of regular/ObjC call/message targets.
494  class AbstractCallee {
495    /// The function declaration of the callee.
496    const Decl *CalleeDecl;
497
498  public:
499    AbstractCallee() : CalleeDecl(nullptr) {}
500    AbstractCallee(const FunctionDecl *FD) : CalleeDecl(FD) {}
501    AbstractCallee(const ObjCMethodDecl *OMD) : CalleeDecl(OMD) {}
502    bool hasFunctionDecl() const {
503      return isa_and_nonnull<FunctionDecl>(CalleeDecl);
504    }
505    const Decl *getDecl() const { return CalleeDecl; }
506    unsigned getNumParams() const {
507      if (const auto *FD = dyn_cast<FunctionDecl>(CalleeDecl))
508        return FD->getNumParams();
509      return cast<ObjCMethodDecl>(CalleeDecl)->param_size();
510    }
511    const ParmVarDecl *getParamDecl(unsigned I) const {
512      if (const auto *FD = dyn_cast<FunctionDecl>(CalleeDecl))
513        return FD->getParamDecl(I);
514      return *(cast<ObjCMethodDecl>(CalleeDecl)->param_begin() + I);
515    }
516  };
517
518  /// Sanitizers enabled for this function.
519  SanitizerSet SanOpts;
520
521  /// True if CodeGen currently emits code implementing sanitizer checks.
522  bool IsSanitizerScope = false;
523
524  /// RAII object to set/unset CodeGenFunction::IsSanitizerScope.
525  class SanitizerScope {
526    CodeGenFunction *CGF;
527  public:
528    SanitizerScope(CodeGenFunction *CGF);
529    ~SanitizerScope();
530  };
531
532  /// In C++, whether we are code generating a thunk.  This controls whether we
533  /// should emit cleanups.
534  bool CurFuncIsThunk = false;
535
536  /// In ARC, whether we should autorelease the return value.
537  bool AutoreleaseResult = false;
538
539  /// Whether we processed a Microsoft-style asm block during CodeGen. These can
540  /// potentially set the return value.
541  bool SawAsmBlock = false;
542
543  GlobalDecl CurSEHParent;
544
545  /// True if the current function is an outlined SEH helper. This can be a
546  /// finally block or filter expression.
547  bool IsOutlinedSEHHelper = false;
548
549  /// True if CodeGen currently emits code inside presereved access index
550  /// region.
551  bool IsInPreservedAIRegion = false;
552
553  /// True if the current statement has nomerge attribute.
554  bool InNoMergeAttributedStmt = false;
555
556  /// True if the current statement has noinline attribute.
557  bool InNoInlineAttributedStmt = false;
558
559  /// True if the current statement has always_inline attribute.
560  bool InAlwaysInlineAttributedStmt = false;
561
562  // The CallExpr within the current statement that the musttail attribute
563  // applies to.  nullptr if there is no 'musttail' on the current statement.
564  const CallExpr *MustTailCall = nullptr;
565
566  /// Returns true if a function must make progress, which means the
567  /// mustprogress attribute can be added.
568  bool checkIfFunctionMustProgress() {
569    if (CGM.getCodeGenOpts().getFiniteLoops() ==
570        CodeGenOptions::FiniteLoopsKind::Never)
571      return false;
572
573    // C++11 and later guarantees that a thread eventually will do one of the
574    // following (C++11 [intro.multithread]p24 and C++17 [intro.progress]p1):
575    // - terminate,
576    //  - make a call to a library I/O function,
577    //  - perform an access through a volatile glvalue, or
578    //  - perform a synchronization operation or an atomic operation.
579    //
580    // Hence each function is 'mustprogress' in C++11 or later.
581    return getLangOpts().CPlusPlus11;
582  }
583
584  /// Returns true if a loop must make progress, which means the mustprogress
585  /// attribute can be added. \p HasConstantCond indicates whether the branch
586  /// condition is a known constant.
587  bool checkIfLoopMustProgress(bool HasConstantCond) {
588    if (CGM.getCodeGenOpts().getFiniteLoops() ==
589        CodeGenOptions::FiniteLoopsKind::Always)
590      return true;
591    if (CGM.getCodeGenOpts().getFiniteLoops() ==
592        CodeGenOptions::FiniteLoopsKind::Never)
593      return false;
594
595    // If the containing function must make progress, loops also must make
596    // progress (as in C++11 and later).
597    if (checkIfFunctionMustProgress())
598      return true;
599
600    // Now apply rules for plain C (see  6.8.5.6 in C11).
601    // Loops with constant conditions do not have to make progress in any C
602    // version.
603    if (HasConstantCond)
604      return false;
605
606    // Loops with non-constant conditions must make progress in C11 and later.
607    return getLangOpts().C11;
608  }
609
610  const CodeGen::CGBlockInfo *BlockInfo = nullptr;
611  llvm::Value *BlockPointer = nullptr;
612
613  llvm::DenseMap<const ValueDecl *, FieldDecl *> LambdaCaptureFields;
614  FieldDecl *LambdaThisCaptureField = nullptr;
615
616  /// A mapping from NRVO variables to the flags used to indicate
617  /// when the NRVO has been applied to this variable.
618  llvm::DenseMap<const VarDecl *, llvm::Value *> NRVOFlags;
619
620  EHScopeStack EHStack;
621  llvm::SmallVector<char, 256> LifetimeExtendedCleanupStack;
622  llvm::SmallVector<const JumpDest *, 2> SEHTryEpilogueStack;
623
624  llvm::Instruction *CurrentFuncletPad = nullptr;
625
626  class CallLifetimeEnd final : public EHScopeStack::Cleanup {
627    bool isRedundantBeforeReturn() override { return true; }
628
629    llvm::Value *Addr;
630    llvm::Value *Size;
631
632  public:
633    CallLifetimeEnd(Address addr, llvm::Value *size)
634        : Addr(addr.getPointer()), Size(size) {}
635
636    void Emit(CodeGenFunction &CGF, Flags flags) override {
637      CGF.EmitLifetimeEnd(Size, Addr);
638    }
639  };
640
641  /// Header for data within LifetimeExtendedCleanupStack.
642  struct LifetimeExtendedCleanupHeader {
643    /// The size of the following cleanup object.
644    unsigned Size;
645    /// The kind of cleanup to push: a value from the CleanupKind enumeration.
646    unsigned Kind : 31;
647    /// Whether this is a conditional cleanup.
648    unsigned IsConditional : 1;
649
650    size_t getSize() const { return Size; }
651    CleanupKind getKind() const { return (CleanupKind)Kind; }
652    bool isConditional() const { return IsConditional; }
653  };
654
655  /// i32s containing the indexes of the cleanup destinations.
656  Address NormalCleanupDest = Address::invalid();
657
658  unsigned NextCleanupDestIndex = 1;
659
660  /// EHResumeBlock - Unified block containing a call to llvm.eh.resume.
661  llvm::BasicBlock *EHResumeBlock = nullptr;
662
663  /// The exception slot.  All landing pads write the current exception pointer
664  /// into this alloca.
665  llvm::Value *ExceptionSlot = nullptr;
666
667  /// The selector slot.  Under the MandatoryCleanup model, all landing pads
668  /// write the current selector value into this alloca.
669  llvm::AllocaInst *EHSelectorSlot = nullptr;
670
671  /// A stack of exception code slots. Entering an __except block pushes a slot
672  /// on the stack and leaving pops one. The __exception_code() intrinsic loads
673  /// a value from the top of the stack.
674  SmallVector<Address, 1> SEHCodeSlotStack;
675
676  /// Value returned by __exception_info intrinsic.
677  llvm::Value *SEHInfo = nullptr;
678
679  /// Emits a landing pad for the current EH stack.
680  llvm::BasicBlock *EmitLandingPad();
681
682  llvm::BasicBlock *getInvokeDestImpl();
683
684  /// Parent loop-based directive for scan directive.
685  const OMPExecutableDirective *OMPParentLoopDirectiveForScan = nullptr;
686  llvm::BasicBlock *OMPBeforeScanBlock = nullptr;
687  llvm::BasicBlock *OMPAfterScanBlock = nullptr;
688  llvm::BasicBlock *OMPScanExitBlock = nullptr;
689  llvm::BasicBlock *OMPScanDispatch = nullptr;
690  bool OMPFirstScanLoop = false;
691
692  /// Manages parent directive for scan directives.
693  class ParentLoopDirectiveForScanRegion {
694    CodeGenFunction &CGF;
695    const OMPExecutableDirective *ParentLoopDirectiveForScan;
696
697  public:
698    ParentLoopDirectiveForScanRegion(
699        CodeGenFunction &CGF,
700        const OMPExecutableDirective &ParentLoopDirectiveForScan)
701        : CGF(CGF),
702          ParentLoopDirectiveForScan(CGF.OMPParentLoopDirectiveForScan) {
703      CGF.OMPParentLoopDirectiveForScan = &ParentLoopDirectiveForScan;
704    }
705    ~ParentLoopDirectiveForScanRegion() {
706      CGF.OMPParentLoopDirectiveForScan = ParentLoopDirectiveForScan;
707    }
708  };
709
710  template <class T>
711  typename DominatingValue<T>::saved_type saveValueInCond(T value) {
712    return DominatingValue<T>::save(*this, value);
713  }
714
715  class CGFPOptionsRAII {
716  public:
717    CGFPOptionsRAII(CodeGenFunction &CGF, FPOptions FPFeatures);
718    CGFPOptionsRAII(CodeGenFunction &CGF, const Expr *E);
719    ~CGFPOptionsRAII();
720
721  private:
722    void ConstructorHelper(FPOptions FPFeatures);
723    CodeGenFunction &CGF;
724    FPOptions OldFPFeatures;
725    llvm::fp::ExceptionBehavior OldExcept;
726    llvm::RoundingMode OldRounding;
727    std::optional<CGBuilderTy::FastMathFlagGuard> FMFGuard;
728  };
729  FPOptions CurFPFeatures;
730
731public:
732  /// ObjCEHValueStack - Stack of Objective-C exception values, used for
733  /// rethrows.
734  SmallVector<llvm::Value*, 8> ObjCEHValueStack;
735
736  /// A class controlling the emission of a finally block.
737  class FinallyInfo {
738    /// Where the catchall's edge through the cleanup should go.
739    JumpDest RethrowDest;
740
741    /// A function to call to enter the catch.
742    llvm::FunctionCallee BeginCatchFn;
743
744    /// An i1 variable indicating whether or not the @finally is
745    /// running for an exception.
746    llvm::AllocaInst *ForEHVar;
747
748    /// An i8* variable into which the exception pointer to rethrow
749    /// has been saved.
750    llvm::AllocaInst *SavedExnVar;
751
752  public:
753    void enter(CodeGenFunction &CGF, const Stmt *Finally,
754               llvm::FunctionCallee beginCatchFn,
755               llvm::FunctionCallee endCatchFn, llvm::FunctionCallee rethrowFn);
756    void exit(CodeGenFunction &CGF);
757  };
758
759  /// Returns true inside SEH __try blocks.
760  bool isSEHTryScope() const { return !SEHTryEpilogueStack.empty(); }
761
762  /// Returns true while emitting a cleanuppad.
763  bool isCleanupPadScope() const {
764    return CurrentFuncletPad && isa<llvm::CleanupPadInst>(CurrentFuncletPad);
765  }
766
767  /// pushFullExprCleanup - Push a cleanup to be run at the end of the
768  /// current full-expression.  Safe against the possibility that
769  /// we're currently inside a conditionally-evaluated expression.
770  template <class T, class... As>
771  void pushFullExprCleanup(CleanupKind kind, As... A) {
772    // If we're not in a conditional branch, or if none of the
773    // arguments requires saving, then use the unconditional cleanup.
774    if (!isInConditionalBranch())
775      return EHStack.pushCleanup<T>(kind, A...);
776
777    // Stash values in a tuple so we can guarantee the order of saves.
778    typedef std::tuple<typename DominatingValue<As>::saved_type...> SavedTuple;
779    SavedTuple Saved{saveValueInCond(A)...};
780
781    typedef EHScopeStack::ConditionalCleanup<T, As...> CleanupType;
782    EHStack.pushCleanupTuple<CleanupType>(kind, Saved);
783    initFullExprCleanup();
784  }
785
786  /// Queue a cleanup to be pushed after finishing the current full-expression,
787  /// potentially with an active flag.
788  template <class T, class... As>
789  void pushCleanupAfterFullExpr(CleanupKind Kind, As... A) {
790    if (!isInConditionalBranch())
791      return pushCleanupAfterFullExprWithActiveFlag<T>(Kind, Address::invalid(),
792                                                       A...);
793
794    Address ActiveFlag = createCleanupActiveFlag();
795    assert(!DominatingValue<Address>::needsSaving(ActiveFlag) &&
796           "cleanup active flag should never need saving");
797
798    typedef std::tuple<typename DominatingValue<As>::saved_type...> SavedTuple;
799    SavedTuple Saved{saveValueInCond(A)...};
800
801    typedef EHScopeStack::ConditionalCleanup<T, As...> CleanupType;
802    pushCleanupAfterFullExprWithActiveFlag<CleanupType>(Kind, ActiveFlag, Saved);
803  }
804
805  template <class T, class... As>
806  void pushCleanupAfterFullExprWithActiveFlag(CleanupKind Kind,
807                                              Address ActiveFlag, As... A) {
808    LifetimeExtendedCleanupHeader Header = {sizeof(T), Kind,
809                                            ActiveFlag.isValid()};
810
811    size_t OldSize = LifetimeExtendedCleanupStack.size();
812    LifetimeExtendedCleanupStack.resize(
813        LifetimeExtendedCleanupStack.size() + sizeof(Header) + Header.Size +
814        (Header.IsConditional ? sizeof(ActiveFlag) : 0));
815
816    static_assert(sizeof(Header) % alignof(T) == 0,
817                  "Cleanup will be allocated on misaligned address");
818    char *Buffer = &LifetimeExtendedCleanupStack[OldSize];
819    new (Buffer) LifetimeExtendedCleanupHeader(Header);
820    new (Buffer + sizeof(Header)) T(A...);
821    if (Header.IsConditional)
822      new (Buffer + sizeof(Header) + sizeof(T)) Address(ActiveFlag);
823  }
824
825  /// Set up the last cleanup that was pushed as a conditional
826  /// full-expression cleanup.
827  void initFullExprCleanup() {
828    initFullExprCleanupWithFlag(createCleanupActiveFlag());
829  }
830
831  void initFullExprCleanupWithFlag(Address ActiveFlag);
832  Address createCleanupActiveFlag();
833
834  /// PushDestructorCleanup - Push a cleanup to call the
835  /// complete-object destructor of an object of the given type at the
836  /// given address.  Does nothing if T is not a C++ class type with a
837  /// non-trivial destructor.
838  void PushDestructorCleanup(QualType T, Address Addr);
839
840  /// PushDestructorCleanup - Push a cleanup to call the
841  /// complete-object variant of the given destructor on the object at
842  /// the given address.
843  void PushDestructorCleanup(const CXXDestructorDecl *Dtor, QualType T,
844                             Address Addr);
845
846  /// PopCleanupBlock - Will pop the cleanup entry on the stack and
847  /// process all branch fixups.
848  void PopCleanupBlock(bool FallThroughIsBranchThrough = false);
849
850  /// DeactivateCleanupBlock - Deactivates the given cleanup block.
851  /// The block cannot be reactivated.  Pops it if it's the top of the
852  /// stack.
853  ///
854  /// \param DominatingIP - An instruction which is known to
855  ///   dominate the current IP (if set) and which lies along
856  ///   all paths of execution between the current IP and the
857  ///   the point at which the cleanup comes into scope.
858  void DeactivateCleanupBlock(EHScopeStack::stable_iterator Cleanup,
859                              llvm::Instruction *DominatingIP);
860
861  /// ActivateCleanupBlock - Activates an initially-inactive cleanup.
862  /// Cannot be used to resurrect a deactivated cleanup.
863  ///
864  /// \param DominatingIP - An instruction which is known to
865  ///   dominate the current IP (if set) and which lies along
866  ///   all paths of execution between the current IP and the
867  ///   the point at which the cleanup comes into scope.
868  void ActivateCleanupBlock(EHScopeStack::stable_iterator Cleanup,
869                            llvm::Instruction *DominatingIP);
870
871  /// Enters a new scope for capturing cleanups, all of which
872  /// will be executed once the scope is exited.
873  class RunCleanupsScope {
874    EHScopeStack::stable_iterator CleanupStackDepth, OldCleanupScopeDepth;
875    size_t LifetimeExtendedCleanupStackSize;
876    bool OldDidCallStackSave;
877  protected:
878    bool PerformCleanup;
879  private:
880
881    RunCleanupsScope(const RunCleanupsScope &) = delete;
882    void operator=(const RunCleanupsScope &) = delete;
883
884  protected:
885    CodeGenFunction& CGF;
886
887  public:
888    /// Enter a new cleanup scope.
889    explicit RunCleanupsScope(CodeGenFunction &CGF)
890      : PerformCleanup(true), CGF(CGF)
891    {
892      CleanupStackDepth = CGF.EHStack.stable_begin();
893      LifetimeExtendedCleanupStackSize =
894          CGF.LifetimeExtendedCleanupStack.size();
895      OldDidCallStackSave = CGF.DidCallStackSave;
896      CGF.DidCallStackSave = false;
897      OldCleanupScopeDepth = CGF.CurrentCleanupScopeDepth;
898      CGF.CurrentCleanupScopeDepth = CleanupStackDepth;
899    }
900
901    /// Exit this cleanup scope, emitting any accumulated cleanups.
902    ~RunCleanupsScope() {
903      if (PerformCleanup)
904        ForceCleanup();
905    }
906
907    /// Determine whether this scope requires any cleanups.
908    bool requiresCleanups() const {
909      return CGF.EHStack.stable_begin() != CleanupStackDepth;
910    }
911
912    /// Force the emission of cleanups now, instead of waiting
913    /// until this object is destroyed.
914    /// \param ValuesToReload - A list of values that need to be available at
915    /// the insertion point after cleanup emission. If cleanup emission created
916    /// a shared cleanup block, these value pointers will be rewritten.
917    /// Otherwise, they not will be modified.
918    void ForceCleanup(std::initializer_list<llvm::Value**> ValuesToReload = {}) {
919      assert(PerformCleanup && "Already forced cleanup");
920      CGF.DidCallStackSave = OldDidCallStackSave;
921      CGF.PopCleanupBlocks(CleanupStackDepth, LifetimeExtendedCleanupStackSize,
922                           ValuesToReload);
923      PerformCleanup = false;
924      CGF.CurrentCleanupScopeDepth = OldCleanupScopeDepth;
925    }
926  };
927
928  // Cleanup stack depth of the RunCleanupsScope that was pushed most recently.
929  EHScopeStack::stable_iterator CurrentCleanupScopeDepth =
930      EHScopeStack::stable_end();
931
932  class LexicalScope : public RunCleanupsScope {
933    SourceRange Range;
934    SmallVector<const LabelDecl*, 4> Labels;
935    LexicalScope *ParentScope;
936
937    LexicalScope(const LexicalScope &) = delete;
938    void operator=(const LexicalScope &) = delete;
939
940  public:
941    /// Enter a new cleanup scope.
942    explicit LexicalScope(CodeGenFunction &CGF, SourceRange Range)
943      : RunCleanupsScope(CGF), Range(Range), ParentScope(CGF.CurLexicalScope) {
944      CGF.CurLexicalScope = this;
945      if (CGDebugInfo *DI = CGF.getDebugInfo())
946        DI->EmitLexicalBlockStart(CGF.Builder, Range.getBegin());
947    }
948
949    void addLabel(const LabelDecl *label) {
950      assert(PerformCleanup && "adding label to dead scope?");
951      Labels.push_back(label);
952    }
953
954    /// Exit this cleanup scope, emitting any accumulated
955    /// cleanups.
956    ~LexicalScope() {
957      if (CGDebugInfo *DI = CGF.getDebugInfo())
958        DI->EmitLexicalBlockEnd(CGF.Builder, Range.getEnd());
959
960      // If we should perform a cleanup, force them now.  Note that
961      // this ends the cleanup scope before rescoping any labels.
962      if (PerformCleanup) {
963        ApplyDebugLocation DL(CGF, Range.getEnd());
964        ForceCleanup();
965      }
966    }
967
968    /// Force the emission of cleanups now, instead of waiting
969    /// until this object is destroyed.
970    void ForceCleanup() {
971      CGF.CurLexicalScope = ParentScope;
972      RunCleanupsScope::ForceCleanup();
973
974      if (!Labels.empty())
975        rescopeLabels();
976    }
977
978    bool hasLabels() const {
979      return !Labels.empty();
980    }
981
982    void rescopeLabels();
983  };
984
985  typedef llvm::DenseMap<const Decl *, Address> DeclMapTy;
986
987  /// The class used to assign some variables some temporarily addresses.
988  class OMPMapVars {
989    DeclMapTy SavedLocals;
990    DeclMapTy SavedTempAddresses;
991    OMPMapVars(const OMPMapVars &) = delete;
992    void operator=(const OMPMapVars &) = delete;
993
994  public:
995    explicit OMPMapVars() = default;
996    ~OMPMapVars() {
997      assert(SavedLocals.empty() && "Did not restored original addresses.");
998    };
999
1000    /// Sets the address of the variable \p LocalVD to be \p TempAddr in
1001    /// function \p CGF.
1002    /// \return true if at least one variable was set already, false otherwise.
1003    bool setVarAddr(CodeGenFunction &CGF, const VarDecl *LocalVD,
1004                    Address TempAddr) {
1005      LocalVD = LocalVD->getCanonicalDecl();
1006      // Only save it once.
1007      if (SavedLocals.count(LocalVD)) return false;
1008
1009      // Copy the existing local entry to SavedLocals.
1010      auto it = CGF.LocalDeclMap.find(LocalVD);
1011      if (it != CGF.LocalDeclMap.end())
1012        SavedLocals.try_emplace(LocalVD, it->second);
1013      else
1014        SavedLocals.try_emplace(LocalVD, Address::invalid());
1015
1016      // Generate the private entry.
1017      QualType VarTy = LocalVD->getType();
1018      if (VarTy->isReferenceType()) {
1019        Address Temp = CGF.CreateMemTemp(VarTy);
1020        CGF.Builder.CreateStore(TempAddr.getPointer(), Temp);
1021        TempAddr = Temp;
1022      }
1023      SavedTempAddresses.try_emplace(LocalVD, TempAddr);
1024
1025      return true;
1026    }
1027
1028    /// Applies new addresses to the list of the variables.
1029    /// \return true if at least one variable is using new address, false
1030    /// otherwise.
1031    bool apply(CodeGenFunction &CGF) {
1032      copyInto(SavedTempAddresses, CGF.LocalDeclMap);
1033      SavedTempAddresses.clear();
1034      return !SavedLocals.empty();
1035    }
1036
1037    /// Restores original addresses of the variables.
1038    void restore(CodeGenFunction &CGF) {
1039      if (!SavedLocals.empty()) {
1040        copyInto(SavedLocals, CGF.LocalDeclMap);
1041        SavedLocals.clear();
1042      }
1043    }
1044
1045  private:
1046    /// Copy all the entries in the source map over the corresponding
1047    /// entries in the destination, which must exist.
1048    static void copyInto(const DeclMapTy &Src, DeclMapTy &Dest) {
1049      for (auto &Pair : Src) {
1050        if (!Pair.second.isValid()) {
1051          Dest.erase(Pair.first);
1052          continue;
1053        }
1054
1055        auto I = Dest.find(Pair.first);
1056        if (I != Dest.end())
1057          I->second = Pair.second;
1058        else
1059          Dest.insert(Pair);
1060      }
1061    }
1062  };
1063
1064  /// The scope used to remap some variables as private in the OpenMP loop body
1065  /// (or other captured region emitted without outlining), and to restore old
1066  /// vars back on exit.
1067  class OMPPrivateScope : public RunCleanupsScope {
1068    OMPMapVars MappedVars;
1069    OMPPrivateScope(const OMPPrivateScope &) = delete;
1070    void operator=(const OMPPrivateScope &) = delete;
1071
1072  public:
1073    /// Enter a new OpenMP private scope.
1074    explicit OMPPrivateScope(CodeGenFunction &CGF) : RunCleanupsScope(CGF) {}
1075
1076    /// Registers \p LocalVD variable as a private with \p Addr as the address
1077    /// of the corresponding private variable. \p
1078    /// PrivateGen is the address of the generated private variable.
1079    /// \return true if the variable is registered as private, false if it has
1080    /// been privatized already.
1081    bool addPrivate(const VarDecl *LocalVD, Address Addr) {
1082      assert(PerformCleanup && "adding private to dead scope");
1083      return MappedVars.setVarAddr(CGF, LocalVD, Addr);
1084    }
1085
1086    /// Privatizes local variables previously registered as private.
1087    /// Registration is separate from the actual privatization to allow
1088    /// initializers use values of the original variables, not the private one.
1089    /// This is important, for example, if the private variable is a class
1090    /// variable initialized by a constructor that references other private
1091    /// variables. But at initialization original variables must be used, not
1092    /// private copies.
1093    /// \return true if at least one variable was privatized, false otherwise.
1094    bool Privatize() { return MappedVars.apply(CGF); }
1095
1096    void ForceCleanup() {
1097      RunCleanupsScope::ForceCleanup();
1098      restoreMap();
1099    }
1100
1101    /// Exit scope - all the mapped variables are restored.
1102    ~OMPPrivateScope() {
1103      if (PerformCleanup)
1104        ForceCleanup();
1105    }
1106
1107    /// Checks if the global variable is captured in current function.
1108    bool isGlobalVarCaptured(const VarDecl *VD) const {
1109      VD = VD->getCanonicalDecl();
1110      return !VD->isLocalVarDeclOrParm() && CGF.LocalDeclMap.count(VD) > 0;
1111    }
1112
1113    /// Restore all mapped variables w/o clean up. This is usefully when we want
1114    /// to reference the original variables but don't want the clean up because
1115    /// that could emit lifetime end too early, causing backend issue #56913.
1116    void restoreMap() { MappedVars.restore(CGF); }
1117  };
1118
1119  /// Save/restore original map of previously emitted local vars in case when we
1120  /// need to duplicate emission of the same code several times in the same
1121  /// function for OpenMP code.
1122  class OMPLocalDeclMapRAII {
1123    CodeGenFunction &CGF;
1124    DeclMapTy SavedMap;
1125
1126  public:
1127    OMPLocalDeclMapRAII(CodeGenFunction &CGF)
1128        : CGF(CGF), SavedMap(CGF.LocalDeclMap) {}
1129    ~OMPLocalDeclMapRAII() { SavedMap.swap(CGF.LocalDeclMap); }
1130  };
1131
1132  /// Takes the old cleanup stack size and emits the cleanup blocks
1133  /// that have been added.
1134  void
1135  PopCleanupBlocks(EHScopeStack::stable_iterator OldCleanupStackSize,
1136                   std::initializer_list<llvm::Value **> ValuesToReload = {});
1137
1138  /// Takes the old cleanup stack size and emits the cleanup blocks
1139  /// that have been added, then adds all lifetime-extended cleanups from
1140  /// the given position to the stack.
1141  void
1142  PopCleanupBlocks(EHScopeStack::stable_iterator OldCleanupStackSize,
1143                   size_t OldLifetimeExtendedStackSize,
1144                   std::initializer_list<llvm::Value **> ValuesToReload = {});
1145
1146  void ResolveBranchFixups(llvm::BasicBlock *Target);
1147
1148  /// The given basic block lies in the current EH scope, but may be a
1149  /// target of a potentially scope-crossing jump; get a stable handle
1150  /// to which we can perform this jump later.
1151  JumpDest getJumpDestInCurrentScope(llvm::BasicBlock *Target) {
1152    return JumpDest(Target,
1153                    EHStack.getInnermostNormalCleanup(),
1154                    NextCleanupDestIndex++);
1155  }
1156
1157  /// The given basic block lies in the current EH scope, but may be a
1158  /// target of a potentially scope-crossing jump; get a stable handle
1159  /// to which we can perform this jump later.
1160  JumpDest getJumpDestInCurrentScope(StringRef Name = StringRef()) {
1161    return getJumpDestInCurrentScope(createBasicBlock(Name));
1162  }
1163
1164  /// EmitBranchThroughCleanup - Emit a branch from the current insert
1165  /// block through the normal cleanup handling code (if any) and then
1166  /// on to \arg Dest.
1167  void EmitBranchThroughCleanup(JumpDest Dest);
1168
1169  /// isObviouslyBranchWithoutCleanups - Return true if a branch to the
1170  /// specified destination obviously has no cleanups to run.  'false' is always
1171  /// a conservatively correct answer for this method.
1172  bool isObviouslyBranchWithoutCleanups(JumpDest Dest) const;
1173
1174  /// popCatchScope - Pops the catch scope at the top of the EHScope
1175  /// stack, emitting any required code (other than the catch handlers
1176  /// themselves).
1177  void popCatchScope();
1178
1179  llvm::BasicBlock *getEHResumeBlock(bool isCleanup);
1180  llvm::BasicBlock *getEHDispatchBlock(EHScopeStack::stable_iterator scope);
1181  llvm::BasicBlock *
1182  getFuncletEHDispatchBlock(EHScopeStack::stable_iterator scope);
1183
1184  /// An object to manage conditionally-evaluated expressions.
1185  class ConditionalEvaluation {
1186    llvm::BasicBlock *StartBB;
1187
1188  public:
1189    ConditionalEvaluation(CodeGenFunction &CGF)
1190      : StartBB(CGF.Builder.GetInsertBlock()) {}
1191
1192    void begin(CodeGenFunction &CGF) {
1193      assert(CGF.OutermostConditional != this);
1194      if (!CGF.OutermostConditional)
1195        CGF.OutermostConditional = this;
1196    }
1197
1198    void end(CodeGenFunction &CGF) {
1199      assert(CGF.OutermostConditional != nullptr);
1200      if (CGF.OutermostConditional == this)
1201        CGF.OutermostConditional = nullptr;
1202    }
1203
1204    /// Returns a block which will be executed prior to each
1205    /// evaluation of the conditional code.
1206    llvm::BasicBlock *getStartingBlock() const {
1207      return StartBB;
1208    }
1209  };
1210
1211  /// isInConditionalBranch - Return true if we're currently emitting
1212  /// one branch or the other of a conditional expression.
1213  bool isInConditionalBranch() const { return OutermostConditional != nullptr; }
1214
1215  void setBeforeOutermostConditional(llvm::Value *value, Address addr) {
1216    assert(isInConditionalBranch());
1217    llvm::BasicBlock *block = OutermostConditional->getStartingBlock();
1218    auto store = new llvm::StoreInst(value, addr.getPointer(), &block->back());
1219    store->setAlignment(addr.getAlignment().getAsAlign());
1220  }
1221
1222  /// An RAII object to record that we're evaluating a statement
1223  /// expression.
1224  class StmtExprEvaluation {
1225    CodeGenFunction &CGF;
1226
1227    /// We have to save the outermost conditional: cleanups in a
1228    /// statement expression aren't conditional just because the
1229    /// StmtExpr is.
1230    ConditionalEvaluation *SavedOutermostConditional;
1231
1232  public:
1233    StmtExprEvaluation(CodeGenFunction &CGF)
1234      : CGF(CGF), SavedOutermostConditional(CGF.OutermostConditional) {
1235      CGF.OutermostConditional = nullptr;
1236    }
1237
1238    ~StmtExprEvaluation() {
1239      CGF.OutermostConditional = SavedOutermostConditional;
1240      CGF.EnsureInsertPoint();
1241    }
1242  };
1243
1244  /// An object which temporarily prevents a value from being
1245  /// destroyed by aggressive peephole optimizations that assume that
1246  /// all uses of a value have been realized in the IR.
1247  class PeepholeProtection {
1248    llvm::Instruction *Inst;
1249    friend class CodeGenFunction;
1250
1251  public:
1252    PeepholeProtection() : Inst(nullptr) {}
1253  };
1254
1255  /// A non-RAII class containing all the information about a bound
1256  /// opaque value.  OpaqueValueMapping, below, is a RAII wrapper for
1257  /// this which makes individual mappings very simple; using this
1258  /// class directly is useful when you have a variable number of
1259  /// opaque values or don't want the RAII functionality for some
1260  /// reason.
1261  class OpaqueValueMappingData {
1262    const OpaqueValueExpr *OpaqueValue;
1263    bool BoundLValue;
1264    CodeGenFunction::PeepholeProtection Protection;
1265
1266    OpaqueValueMappingData(const OpaqueValueExpr *ov,
1267                           bool boundLValue)
1268      : OpaqueValue(ov), BoundLValue(boundLValue) {}
1269  public:
1270    OpaqueValueMappingData() : OpaqueValue(nullptr) {}
1271
1272    static bool shouldBindAsLValue(const Expr *expr) {
1273      // gl-values should be bound as l-values for obvious reasons.
1274      // Records should be bound as l-values because IR generation
1275      // always keeps them in memory.  Expressions of function type
1276      // act exactly like l-values but are formally required to be
1277      // r-values in C.
1278      return expr->isGLValue() ||
1279             expr->getType()->isFunctionType() ||
1280             hasAggregateEvaluationKind(expr->getType());
1281    }
1282
1283    static OpaqueValueMappingData bind(CodeGenFunction &CGF,
1284                                       const OpaqueValueExpr *ov,
1285                                       const Expr *e) {
1286      if (shouldBindAsLValue(ov))
1287        return bind(CGF, ov, CGF.EmitLValue(e));
1288      return bind(CGF, ov, CGF.EmitAnyExpr(e));
1289    }
1290
1291    static OpaqueValueMappingData bind(CodeGenFunction &CGF,
1292                                       const OpaqueValueExpr *ov,
1293                                       const LValue &lv) {
1294      assert(shouldBindAsLValue(ov));
1295      CGF.OpaqueLValues.insert(std::make_pair(ov, lv));
1296      return OpaqueValueMappingData(ov, true);
1297    }
1298
1299    static OpaqueValueMappingData bind(CodeGenFunction &CGF,
1300                                       const OpaqueValueExpr *ov,
1301                                       const RValue &rv) {
1302      assert(!shouldBindAsLValue(ov));
1303      CGF.OpaqueRValues.insert(std::make_pair(ov, rv));
1304
1305      OpaqueValueMappingData data(ov, false);
1306
1307      // Work around an extremely aggressive peephole optimization in
1308      // EmitScalarConversion which assumes that all other uses of a
1309      // value are extant.
1310      data.Protection = CGF.protectFromPeepholes(rv);
1311
1312      return data;
1313    }
1314
1315    bool isValid() const { return OpaqueValue != nullptr; }
1316    void clear() { OpaqueValue = nullptr; }
1317
1318    void unbind(CodeGenFunction &CGF) {
1319      assert(OpaqueValue && "no data to unbind!");
1320
1321      if (BoundLValue) {
1322        CGF.OpaqueLValues.erase(OpaqueValue);
1323      } else {
1324        CGF.OpaqueRValues.erase(OpaqueValue);
1325        CGF.unprotectFromPeepholes(Protection);
1326      }
1327    }
1328  };
1329
1330  /// An RAII object to set (and then clear) a mapping for an OpaqueValueExpr.
1331  class OpaqueValueMapping {
1332    CodeGenFunction &CGF;
1333    OpaqueValueMappingData Data;
1334
1335  public:
1336    static bool shouldBindAsLValue(const Expr *expr) {
1337      return OpaqueValueMappingData::shouldBindAsLValue(expr);
1338    }
1339
1340    /// Build the opaque value mapping for the given conditional
1341    /// operator if it's the GNU ?: extension.  This is a common
1342    /// enough pattern that the convenience operator is really
1343    /// helpful.
1344    ///
1345    OpaqueValueMapping(CodeGenFunction &CGF,
1346                       const AbstractConditionalOperator *op) : CGF(CGF) {
1347      if (isa<ConditionalOperator>(op))
1348        // Leave Data empty.
1349        return;
1350
1351      const BinaryConditionalOperator *e = cast<BinaryConditionalOperator>(op);
1352      Data = OpaqueValueMappingData::bind(CGF, e->getOpaqueValue(),
1353                                          e->getCommon());
1354    }
1355
1356    /// Build the opaque value mapping for an OpaqueValueExpr whose source
1357    /// expression is set to the expression the OVE represents.
1358    OpaqueValueMapping(CodeGenFunction &CGF, const OpaqueValueExpr *OV)
1359        : CGF(CGF) {
1360      if (OV) {
1361        assert(OV->getSourceExpr() && "wrong form of OpaqueValueMapping used "
1362                                      "for OVE with no source expression");
1363        Data = OpaqueValueMappingData::bind(CGF, OV, OV->getSourceExpr());
1364      }
1365    }
1366
1367    OpaqueValueMapping(CodeGenFunction &CGF,
1368                       const OpaqueValueExpr *opaqueValue,
1369                       LValue lvalue)
1370      : CGF(CGF), Data(OpaqueValueMappingData::bind(CGF, opaqueValue, lvalue)) {
1371    }
1372
1373    OpaqueValueMapping(CodeGenFunction &CGF,
1374                       const OpaqueValueExpr *opaqueValue,
1375                       RValue rvalue)
1376      : CGF(CGF), Data(OpaqueValueMappingData::bind(CGF, opaqueValue, rvalue)) {
1377    }
1378
1379    void pop() {
1380      Data.unbind(CGF);
1381      Data.clear();
1382    }
1383
1384    ~OpaqueValueMapping() {
1385      if (Data.isValid()) Data.unbind(CGF);
1386    }
1387  };
1388
1389private:
1390  CGDebugInfo *DebugInfo;
1391  /// Used to create unique names for artificial VLA size debug info variables.
1392  unsigned VLAExprCounter = 0;
1393  bool DisableDebugInfo = false;
1394
1395  /// DidCallStackSave - Whether llvm.stacksave has been called. Used to avoid
1396  /// calling llvm.stacksave for multiple VLAs in the same scope.
1397  bool DidCallStackSave = false;
1398
1399  /// IndirectBranch - The first time an indirect goto is seen we create a block
1400  /// with an indirect branch.  Every time we see the address of a label taken,
1401  /// we add the label to the indirect goto.  Every subsequent indirect goto is
1402  /// codegen'd as a jump to the IndirectBranch's basic block.
1403  llvm::IndirectBrInst *IndirectBranch = nullptr;
1404
1405  /// LocalDeclMap - This keeps track of the LLVM allocas or globals for local C
1406  /// decls.
1407  DeclMapTy LocalDeclMap;
1408
1409  // Keep track of the cleanups for callee-destructed parameters pushed to the
1410  // cleanup stack so that they can be deactivated later.
1411  llvm::DenseMap<const ParmVarDecl *, EHScopeStack::stable_iterator>
1412      CalleeDestructedParamCleanups;
1413
1414  /// SizeArguments - If a ParmVarDecl had the pass_object_size attribute, this
1415  /// will contain a mapping from said ParmVarDecl to its implicit "object_size"
1416  /// parameter.
1417  llvm::SmallDenseMap<const ParmVarDecl *, const ImplicitParamDecl *, 2>
1418      SizeArguments;
1419
1420  /// Track escaped local variables with auto storage. Used during SEH
1421  /// outlining to produce a call to llvm.localescape.
1422  llvm::DenseMap<llvm::AllocaInst *, int> EscapedLocals;
1423
1424  /// LabelMap - This keeps track of the LLVM basic block for each C label.
1425  llvm::DenseMap<const LabelDecl*, JumpDest> LabelMap;
1426
1427  // BreakContinueStack - This keeps track of where break and continue
1428  // statements should jump to.
1429  struct BreakContinue {
1430    BreakContinue(JumpDest Break, JumpDest Continue)
1431      : BreakBlock(Break), ContinueBlock(Continue) {}
1432
1433    JumpDest BreakBlock;
1434    JumpDest ContinueBlock;
1435  };
1436  SmallVector<BreakContinue, 8> BreakContinueStack;
1437
1438  /// Handles cancellation exit points in OpenMP-related constructs.
1439  class OpenMPCancelExitStack {
1440    /// Tracks cancellation exit point and join point for cancel-related exit
1441    /// and normal exit.
1442    struct CancelExit {
1443      CancelExit() = default;
1444      CancelExit(OpenMPDirectiveKind Kind, JumpDest ExitBlock,
1445                 JumpDest ContBlock)
1446          : Kind(Kind), ExitBlock(ExitBlock), ContBlock(ContBlock) {}
1447      OpenMPDirectiveKind Kind = llvm::omp::OMPD_unknown;
1448      /// true if the exit block has been emitted already by the special
1449      /// emitExit() call, false if the default codegen is used.
1450      bool HasBeenEmitted = false;
1451      JumpDest ExitBlock;
1452      JumpDest ContBlock;
1453    };
1454
1455    SmallVector<CancelExit, 8> Stack;
1456
1457  public:
1458    OpenMPCancelExitStack() : Stack(1) {}
1459    ~OpenMPCancelExitStack() = default;
1460    /// Fetches the exit block for the current OpenMP construct.
1461    JumpDest getExitBlock() const { return Stack.back().ExitBlock; }
1462    /// Emits exit block with special codegen procedure specific for the related
1463    /// OpenMP construct + emits code for normal construct cleanup.
1464    void emitExit(CodeGenFunction &CGF, OpenMPDirectiveKind Kind,
1465                  const llvm::function_ref<void(CodeGenFunction &)> CodeGen) {
1466      if (Stack.back().Kind == Kind && getExitBlock().isValid()) {
1467        assert(CGF.getOMPCancelDestination(Kind).isValid());
1468        assert(CGF.HaveInsertPoint());
1469        assert(!Stack.back().HasBeenEmitted);
1470        auto IP = CGF.Builder.saveAndClearIP();
1471        CGF.EmitBlock(Stack.back().ExitBlock.getBlock());
1472        CodeGen(CGF);
1473        CGF.EmitBranch(Stack.back().ContBlock.getBlock());
1474        CGF.Builder.restoreIP(IP);
1475        Stack.back().HasBeenEmitted = true;
1476      }
1477      CodeGen(CGF);
1478    }
1479    /// Enter the cancel supporting \a Kind construct.
1480    /// \param Kind OpenMP directive that supports cancel constructs.
1481    /// \param HasCancel true, if the construct has inner cancel directive,
1482    /// false otherwise.
1483    void enter(CodeGenFunction &CGF, OpenMPDirectiveKind Kind, bool HasCancel) {
1484      Stack.push_back({Kind,
1485                       HasCancel ? CGF.getJumpDestInCurrentScope("cancel.exit")
1486                                 : JumpDest(),
1487                       HasCancel ? CGF.getJumpDestInCurrentScope("cancel.cont")
1488                                 : JumpDest()});
1489    }
1490    /// Emits default exit point for the cancel construct (if the special one
1491    /// has not be used) + join point for cancel/normal exits.
1492    void exit(CodeGenFunction &CGF) {
1493      if (getExitBlock().isValid()) {
1494        assert(CGF.getOMPCancelDestination(Stack.back().Kind).isValid());
1495        bool HaveIP = CGF.HaveInsertPoint();
1496        if (!Stack.back().HasBeenEmitted) {
1497          if (HaveIP)
1498            CGF.EmitBranchThroughCleanup(Stack.back().ContBlock);
1499          CGF.EmitBlock(Stack.back().ExitBlock.getBlock());
1500          CGF.EmitBranchThroughCleanup(Stack.back().ContBlock);
1501        }
1502        CGF.EmitBlock(Stack.back().ContBlock.getBlock());
1503        if (!HaveIP) {
1504          CGF.Builder.CreateUnreachable();
1505          CGF.Builder.ClearInsertionPoint();
1506        }
1507      }
1508      Stack.pop_back();
1509    }
1510  };
1511  OpenMPCancelExitStack OMPCancelStack;
1512
1513  /// Lower the Likelihood knowledge about the \p Cond via llvm.expect intrin.
1514  llvm::Value *emitCondLikelihoodViaExpectIntrinsic(llvm::Value *Cond,
1515                                                    Stmt::Likelihood LH);
1516
1517  CodeGenPGO PGO;
1518
1519  /// Calculate branch weights appropriate for PGO data
1520  llvm::MDNode *createProfileWeights(uint64_t TrueCount,
1521                                     uint64_t FalseCount) const;
1522  llvm::MDNode *createProfileWeights(ArrayRef<uint64_t> Weights) const;
1523  llvm::MDNode *createProfileWeightsForLoop(const Stmt *Cond,
1524                                            uint64_t LoopCount) const;
1525
1526public:
1527  /// Increment the profiler's counter for the given statement by \p StepV.
1528  /// If \p StepV is null, the default increment is 1.
1529  void incrementProfileCounter(const Stmt *S, llvm::Value *StepV = nullptr) {
1530    if (CGM.getCodeGenOpts().hasProfileClangInstr() &&
1531        !CurFn->hasFnAttribute(llvm::Attribute::NoProfile) &&
1532        !CurFn->hasFnAttribute(llvm::Attribute::SkipProfile))
1533      PGO.emitCounterIncrement(Builder, S, StepV);
1534    PGO.setCurrentStmt(S);
1535  }
1536
1537  /// Get the profiler's count for the given statement.
1538  uint64_t getProfileCount(const Stmt *S) {
1539    return PGO.getStmtCount(S).value_or(0);
1540  }
1541
1542  /// Set the profiler's current count.
1543  void setCurrentProfileCount(uint64_t Count) {
1544    PGO.setCurrentRegionCount(Count);
1545  }
1546
1547  /// Get the profiler's current count. This is generally the count for the most
1548  /// recently incremented counter.
1549  uint64_t getCurrentProfileCount() {
1550    return PGO.getCurrentRegionCount();
1551  }
1552
1553private:
1554
1555  /// SwitchInsn - This is nearest current switch instruction. It is null if
1556  /// current context is not in a switch.
1557  llvm::SwitchInst *SwitchInsn = nullptr;
1558  /// The branch weights of SwitchInsn when doing instrumentation based PGO.
1559  SmallVector<uint64_t, 16> *SwitchWeights = nullptr;
1560
1561  /// The likelihood attributes of the SwitchCase.
1562  SmallVector<Stmt::Likelihood, 16> *SwitchLikelihood = nullptr;
1563
1564  /// CaseRangeBlock - This block holds if condition check for last case
1565  /// statement range in current switch instruction.
1566  llvm::BasicBlock *CaseRangeBlock = nullptr;
1567
1568  /// OpaqueLValues - Keeps track of the current set of opaque value
1569  /// expressions.
1570  llvm::DenseMap<const OpaqueValueExpr *, LValue> OpaqueLValues;
1571  llvm::DenseMap<const OpaqueValueExpr *, RValue> OpaqueRValues;
1572
1573  // VLASizeMap - This keeps track of the associated size for each VLA type.
1574  // We track this by the size expression rather than the type itself because
1575  // in certain situations, like a const qualifier applied to an VLA typedef,
1576  // multiple VLA types can share the same size expression.
1577  // FIXME: Maybe this could be a stack of maps that is pushed/popped as we
1578  // enter/leave scopes.
1579  llvm::DenseMap<const Expr*, llvm::Value*> VLASizeMap;
1580
1581  /// A block containing a single 'unreachable' instruction.  Created
1582  /// lazily by getUnreachableBlock().
1583  llvm::BasicBlock *UnreachableBlock = nullptr;
1584
1585  /// Counts of the number return expressions in the function.
1586  unsigned NumReturnExprs = 0;
1587
1588  /// Count the number of simple (constant) return expressions in the function.
1589  unsigned NumSimpleReturnExprs = 0;
1590
1591  /// The last regular (non-return) debug location (breakpoint) in the function.
1592  SourceLocation LastStopPoint;
1593
1594public:
1595  /// Source location information about the default argument or member
1596  /// initializer expression we're evaluating, if any.
1597  CurrentSourceLocExprScope CurSourceLocExprScope;
1598  using SourceLocExprScopeGuard =
1599      CurrentSourceLocExprScope::SourceLocExprScopeGuard;
1600
1601  /// A scope within which we are constructing the fields of an object which
1602  /// might use a CXXDefaultInitExpr. This stashes away a 'this' value to use
1603  /// if we need to evaluate a CXXDefaultInitExpr within the evaluation.
1604  class FieldConstructionScope {
1605  public:
1606    FieldConstructionScope(CodeGenFunction &CGF, Address This)
1607        : CGF(CGF), OldCXXDefaultInitExprThis(CGF.CXXDefaultInitExprThis) {
1608      CGF.CXXDefaultInitExprThis = This;
1609    }
1610    ~FieldConstructionScope() {
1611      CGF.CXXDefaultInitExprThis = OldCXXDefaultInitExprThis;
1612    }
1613
1614  private:
1615    CodeGenFunction &CGF;
1616    Address OldCXXDefaultInitExprThis;
1617  };
1618
1619  /// The scope of a CXXDefaultInitExpr. Within this scope, the value of 'this'
1620  /// is overridden to be the object under construction.
1621  class CXXDefaultInitExprScope  {
1622  public:
1623    CXXDefaultInitExprScope(CodeGenFunction &CGF, const CXXDefaultInitExpr *E)
1624        : CGF(CGF), OldCXXThisValue(CGF.CXXThisValue),
1625          OldCXXThisAlignment(CGF.CXXThisAlignment),
1626          SourceLocScope(E, CGF.CurSourceLocExprScope) {
1627      CGF.CXXThisValue = CGF.CXXDefaultInitExprThis.getPointer();
1628      CGF.CXXThisAlignment = CGF.CXXDefaultInitExprThis.getAlignment();
1629    }
1630    ~CXXDefaultInitExprScope() {
1631      CGF.CXXThisValue = OldCXXThisValue;
1632      CGF.CXXThisAlignment = OldCXXThisAlignment;
1633    }
1634
1635  public:
1636    CodeGenFunction &CGF;
1637    llvm::Value *OldCXXThisValue;
1638    CharUnits OldCXXThisAlignment;
1639    SourceLocExprScopeGuard SourceLocScope;
1640  };
1641
1642  struct CXXDefaultArgExprScope : SourceLocExprScopeGuard {
1643    CXXDefaultArgExprScope(CodeGenFunction &CGF, const CXXDefaultArgExpr *E)
1644        : SourceLocExprScopeGuard(E, CGF.CurSourceLocExprScope) {}
1645  };
1646
1647  /// The scope of an ArrayInitLoopExpr. Within this scope, the value of the
1648  /// current loop index is overridden.
1649  class ArrayInitLoopExprScope {
1650  public:
1651    ArrayInitLoopExprScope(CodeGenFunction &CGF, llvm::Value *Index)
1652      : CGF(CGF), OldArrayInitIndex(CGF.ArrayInitIndex) {
1653      CGF.ArrayInitIndex = Index;
1654    }
1655    ~ArrayInitLoopExprScope() {
1656      CGF.ArrayInitIndex = OldArrayInitIndex;
1657    }
1658
1659  private:
1660    CodeGenFunction &CGF;
1661    llvm::Value *OldArrayInitIndex;
1662  };
1663
1664  class InlinedInheritingConstructorScope {
1665  public:
1666    InlinedInheritingConstructorScope(CodeGenFunction &CGF, GlobalDecl GD)
1667        : CGF(CGF), OldCurGD(CGF.CurGD), OldCurFuncDecl(CGF.CurFuncDecl),
1668          OldCurCodeDecl(CGF.CurCodeDecl),
1669          OldCXXABIThisDecl(CGF.CXXABIThisDecl),
1670          OldCXXABIThisValue(CGF.CXXABIThisValue),
1671          OldCXXThisValue(CGF.CXXThisValue),
1672          OldCXXABIThisAlignment(CGF.CXXABIThisAlignment),
1673          OldCXXThisAlignment(CGF.CXXThisAlignment),
1674          OldReturnValue(CGF.ReturnValue), OldFnRetTy(CGF.FnRetTy),
1675          OldCXXInheritedCtorInitExprArgs(
1676              std::move(CGF.CXXInheritedCtorInitExprArgs)) {
1677      CGF.CurGD = GD;
1678      CGF.CurFuncDecl = CGF.CurCodeDecl =
1679          cast<CXXConstructorDecl>(GD.getDecl());
1680      CGF.CXXABIThisDecl = nullptr;
1681      CGF.CXXABIThisValue = nullptr;
1682      CGF.CXXThisValue = nullptr;
1683      CGF.CXXABIThisAlignment = CharUnits();
1684      CGF.CXXThisAlignment = CharUnits();
1685      CGF.ReturnValue = Address::invalid();
1686      CGF.FnRetTy = QualType();
1687      CGF.CXXInheritedCtorInitExprArgs.clear();
1688    }
1689    ~InlinedInheritingConstructorScope() {
1690      CGF.CurGD = OldCurGD;
1691      CGF.CurFuncDecl = OldCurFuncDecl;
1692      CGF.CurCodeDecl = OldCurCodeDecl;
1693      CGF.CXXABIThisDecl = OldCXXABIThisDecl;
1694      CGF.CXXABIThisValue = OldCXXABIThisValue;
1695      CGF.CXXThisValue = OldCXXThisValue;
1696      CGF.CXXABIThisAlignment = OldCXXABIThisAlignment;
1697      CGF.CXXThisAlignment = OldCXXThisAlignment;
1698      CGF.ReturnValue = OldReturnValue;
1699      CGF.FnRetTy = OldFnRetTy;
1700      CGF.CXXInheritedCtorInitExprArgs =
1701          std::move(OldCXXInheritedCtorInitExprArgs);
1702    }
1703
1704  private:
1705    CodeGenFunction &CGF;
1706    GlobalDecl OldCurGD;
1707    const Decl *OldCurFuncDecl;
1708    const Decl *OldCurCodeDecl;
1709    ImplicitParamDecl *OldCXXABIThisDecl;
1710    llvm::Value *OldCXXABIThisValue;
1711    llvm::Value *OldCXXThisValue;
1712    CharUnits OldCXXABIThisAlignment;
1713    CharUnits OldCXXThisAlignment;
1714    Address OldReturnValue;
1715    QualType OldFnRetTy;
1716    CallArgList OldCXXInheritedCtorInitExprArgs;
1717  };
1718
1719  // Helper class for the OpenMP IR Builder. Allows reusability of code used for
1720  // region body, and finalization codegen callbacks. This will class will also
1721  // contain privatization functions used by the privatization call backs
1722  //
1723  // TODO: this is temporary class for things that are being moved out of
1724  // CGOpenMPRuntime, new versions of current CodeGenFunction methods, or
1725  // utility function for use with the OMPBuilder. Once that move to use the
1726  // OMPBuilder is done, everything here will either become part of CodeGenFunc.
1727  // directly, or a new helper class that will contain functions used by both
1728  // this and the OMPBuilder
1729
1730  struct OMPBuilderCBHelpers {
1731
1732    OMPBuilderCBHelpers() = delete;
1733    OMPBuilderCBHelpers(const OMPBuilderCBHelpers &) = delete;
1734    OMPBuilderCBHelpers &operator=(const OMPBuilderCBHelpers &) = delete;
1735
1736    using InsertPointTy = llvm::OpenMPIRBuilder::InsertPointTy;
1737
1738    /// Cleanup action for allocate support.
1739    class OMPAllocateCleanupTy final : public EHScopeStack::Cleanup {
1740
1741    private:
1742      llvm::CallInst *RTLFnCI;
1743
1744    public:
1745      OMPAllocateCleanupTy(llvm::CallInst *RLFnCI) : RTLFnCI(RLFnCI) {
1746        RLFnCI->removeFromParent();
1747      }
1748
1749      void Emit(CodeGenFunction &CGF, Flags /*flags*/) override {
1750        if (!CGF.HaveInsertPoint())
1751          return;
1752        CGF.Builder.Insert(RTLFnCI);
1753      }
1754    };
1755
1756    /// Returns address of the threadprivate variable for the current
1757    /// thread. This Also create any necessary OMP runtime calls.
1758    ///
1759    /// \param VD VarDecl for Threadprivate variable.
1760    /// \param VDAddr Address of the Vardecl
1761    /// \param Loc  The location where the barrier directive was encountered
1762    static Address getAddrOfThreadPrivate(CodeGenFunction &CGF,
1763                                          const VarDecl *VD, Address VDAddr,
1764                                          SourceLocation Loc);
1765
1766    /// Gets the OpenMP-specific address of the local variable /p VD.
1767    static Address getAddressOfLocalVariable(CodeGenFunction &CGF,
1768                                             const VarDecl *VD);
1769    /// Get the platform-specific name separator.
1770    /// \param Parts different parts of the final name that needs separation
1771    /// \param FirstSeparator First separator used between the initial two
1772    ///        parts of the name.
1773    /// \param Separator separator used between all of the rest consecutinve
1774    ///        parts of the name
1775    static std::string getNameWithSeparators(ArrayRef<StringRef> Parts,
1776                                             StringRef FirstSeparator = ".",
1777                                             StringRef Separator = ".");
1778    /// Emit the Finalization for an OMP region
1779    /// \param CGF	The Codegen function this belongs to
1780    /// \param IP	Insertion point for generating the finalization code.
1781    static void FinalizeOMPRegion(CodeGenFunction &CGF, InsertPointTy IP) {
1782      CGBuilderTy::InsertPointGuard IPG(CGF.Builder);
1783      assert(IP.getBlock()->end() != IP.getPoint() &&
1784             "OpenMP IR Builder should cause terminated block!");
1785
1786      llvm::BasicBlock *IPBB = IP.getBlock();
1787      llvm::BasicBlock *DestBB = IPBB->getUniqueSuccessor();
1788      assert(DestBB && "Finalization block should have one successor!");
1789
1790      // erase and replace with cleanup branch.
1791      IPBB->getTerminator()->eraseFromParent();
1792      CGF.Builder.SetInsertPoint(IPBB);
1793      CodeGenFunction::JumpDest Dest = CGF.getJumpDestInCurrentScope(DestBB);
1794      CGF.EmitBranchThroughCleanup(Dest);
1795    }
1796
1797    /// Emit the body of an OMP region
1798    /// \param CGF	          The Codegen function this belongs to
1799    /// \param RegionBodyStmt The body statement for the OpenMP region being
1800    ///                       generated
1801    /// \param AllocaIP       Where to insert alloca instructions
1802    /// \param CodeGenIP      Where to insert the region code
1803    /// \param RegionName     Name to be used for new blocks
1804    static void EmitOMPInlinedRegionBody(CodeGenFunction &CGF,
1805                                         const Stmt *RegionBodyStmt,
1806                                         InsertPointTy AllocaIP,
1807                                         InsertPointTy CodeGenIP,
1808                                         Twine RegionName);
1809
1810    static void EmitCaptureStmt(CodeGenFunction &CGF, InsertPointTy CodeGenIP,
1811                                llvm::BasicBlock &FiniBB, llvm::Function *Fn,
1812                                ArrayRef<llvm::Value *> Args) {
1813      llvm::BasicBlock *CodeGenIPBB = CodeGenIP.getBlock();
1814      if (llvm::Instruction *CodeGenIPBBTI = CodeGenIPBB->getTerminator())
1815        CodeGenIPBBTI->eraseFromParent();
1816
1817      CGF.Builder.SetInsertPoint(CodeGenIPBB);
1818
1819      if (Fn->doesNotThrow())
1820        CGF.EmitNounwindRuntimeCall(Fn, Args);
1821      else
1822        CGF.EmitRuntimeCall(Fn, Args);
1823
1824      if (CGF.Builder.saveIP().isSet())
1825        CGF.Builder.CreateBr(&FiniBB);
1826    }
1827
1828    /// Emit the body of an OMP region that will be outlined in
1829    /// OpenMPIRBuilder::finalize().
1830    /// \param CGF	          The Codegen function this belongs to
1831    /// \param RegionBodyStmt The body statement for the OpenMP region being
1832    ///                       generated
1833    /// \param AllocaIP       Where to insert alloca instructions
1834    /// \param CodeGenIP      Where to insert the region code
1835    /// \param RegionName     Name to be used for new blocks
1836    static void EmitOMPOutlinedRegionBody(CodeGenFunction &CGF,
1837                                          const Stmt *RegionBodyStmt,
1838                                          InsertPointTy AllocaIP,
1839                                          InsertPointTy CodeGenIP,
1840                                          Twine RegionName);
1841
1842    /// RAII for preserving necessary info during Outlined region body codegen.
1843    class OutlinedRegionBodyRAII {
1844
1845      llvm::AssertingVH<llvm::Instruction> OldAllocaIP;
1846      CodeGenFunction::JumpDest OldReturnBlock;
1847      CodeGenFunction &CGF;
1848
1849    public:
1850      OutlinedRegionBodyRAII(CodeGenFunction &cgf, InsertPointTy &AllocaIP,
1851                             llvm::BasicBlock &RetBB)
1852          : CGF(cgf) {
1853        assert(AllocaIP.isSet() &&
1854               "Must specify Insertion point for allocas of outlined function");
1855        OldAllocaIP = CGF.AllocaInsertPt;
1856        CGF.AllocaInsertPt = &*AllocaIP.getPoint();
1857
1858        OldReturnBlock = CGF.ReturnBlock;
1859        CGF.ReturnBlock = CGF.getJumpDestInCurrentScope(&RetBB);
1860      }
1861
1862      ~OutlinedRegionBodyRAII() {
1863        CGF.AllocaInsertPt = OldAllocaIP;
1864        CGF.ReturnBlock = OldReturnBlock;
1865      }
1866    };
1867
1868    /// RAII for preserving necessary info during inlined region body codegen.
1869    class InlinedRegionBodyRAII {
1870
1871      llvm::AssertingVH<llvm::Instruction> OldAllocaIP;
1872      CodeGenFunction &CGF;
1873
1874    public:
1875      InlinedRegionBodyRAII(CodeGenFunction &cgf, InsertPointTy &AllocaIP,
1876                            llvm::BasicBlock &FiniBB)
1877          : CGF(cgf) {
1878        // Alloca insertion block should be in the entry block of the containing
1879        // function so it expects an empty AllocaIP in which case will reuse the
1880        // old alloca insertion point, or a new AllocaIP in the same block as
1881        // the old one
1882        assert((!AllocaIP.isSet() ||
1883                CGF.AllocaInsertPt->getParent() == AllocaIP.getBlock()) &&
1884               "Insertion point should be in the entry block of containing "
1885               "function!");
1886        OldAllocaIP = CGF.AllocaInsertPt;
1887        if (AllocaIP.isSet())
1888          CGF.AllocaInsertPt = &*AllocaIP.getPoint();
1889
1890        // TODO: Remove the call, after making sure the counter is not used by
1891        //       the EHStack.
1892        // Since this is an inlined region, it should not modify the
1893        // ReturnBlock, and should reuse the one for the enclosing outlined
1894        // region. So, the JumpDest being return by the function is discarded
1895        (void)CGF.getJumpDestInCurrentScope(&FiniBB);
1896      }
1897
1898      ~InlinedRegionBodyRAII() { CGF.AllocaInsertPt = OldAllocaIP; }
1899    };
1900  };
1901
1902private:
1903  /// CXXThisDecl - When generating code for a C++ member function,
1904  /// this will hold the implicit 'this' declaration.
1905  ImplicitParamDecl *CXXABIThisDecl = nullptr;
1906  llvm::Value *CXXABIThisValue = nullptr;
1907  llvm::Value *CXXThisValue = nullptr;
1908  CharUnits CXXABIThisAlignment;
1909  CharUnits CXXThisAlignment;
1910
1911  /// The value of 'this' to use when evaluating CXXDefaultInitExprs within
1912  /// this expression.
1913  Address CXXDefaultInitExprThis = Address::invalid();
1914
1915  /// The current array initialization index when evaluating an
1916  /// ArrayInitIndexExpr within an ArrayInitLoopExpr.
1917  llvm::Value *ArrayInitIndex = nullptr;
1918
1919  /// The values of function arguments to use when evaluating
1920  /// CXXInheritedCtorInitExprs within this context.
1921  CallArgList CXXInheritedCtorInitExprArgs;
1922
1923  /// CXXStructorImplicitParamDecl - When generating code for a constructor or
1924  /// destructor, this will hold the implicit argument (e.g. VTT).
1925  ImplicitParamDecl *CXXStructorImplicitParamDecl = nullptr;
1926  llvm::Value *CXXStructorImplicitParamValue = nullptr;
1927
1928  /// OutermostConditional - Points to the outermost active
1929  /// conditional control.  This is used so that we know if a
1930  /// temporary should be destroyed conditionally.
1931  ConditionalEvaluation *OutermostConditional = nullptr;
1932
1933  /// The current lexical scope.
1934  LexicalScope *CurLexicalScope = nullptr;
1935
1936  /// The current source location that should be used for exception
1937  /// handling code.
1938  SourceLocation CurEHLocation;
1939
1940  /// BlockByrefInfos - For each __block variable, contains
1941  /// information about the layout of the variable.
1942  llvm::DenseMap<const ValueDecl *, BlockByrefInfo> BlockByrefInfos;
1943
1944  /// Used by -fsanitize=nullability-return to determine whether the return
1945  /// value can be checked.
1946  llvm::Value *RetValNullabilityPrecondition = nullptr;
1947
1948  /// Check if -fsanitize=nullability-return instrumentation is required for
1949  /// this function.
1950  bool requiresReturnValueNullabilityCheck() const {
1951    return RetValNullabilityPrecondition;
1952  }
1953
1954  /// Used to store precise source locations for return statements by the
1955  /// runtime return value checks.
1956  Address ReturnLocation = Address::invalid();
1957
1958  /// Check if the return value of this function requires sanitization.
1959  bool requiresReturnValueCheck() const;
1960
1961  llvm::BasicBlock *TerminateLandingPad = nullptr;
1962  llvm::BasicBlock *TerminateHandler = nullptr;
1963  llvm::SmallVector<llvm::BasicBlock *, 2> TrapBBs;
1964
1965  /// Terminate funclets keyed by parent funclet pad.
1966  llvm::MapVector<llvm::Value *, llvm::BasicBlock *> TerminateFunclets;
1967
1968  /// Largest vector width used in ths function. Will be used to create a
1969  /// function attribute.
1970  unsigned LargestVectorWidth = 0;
1971
1972  /// True if we need emit the life-time markers. This is initially set in
1973  /// the constructor, but could be overwritten to true if this is a coroutine.
1974  bool ShouldEmitLifetimeMarkers;
1975
1976  /// Add OpenCL kernel arg metadata and the kernel attribute metadata to
1977  /// the function metadata.
1978  void EmitKernelMetadata(const FunctionDecl *FD, llvm::Function *Fn);
1979
1980public:
1981  CodeGenFunction(CodeGenModule &cgm, bool suppressNewContext=false);
1982  ~CodeGenFunction();
1983
1984  CodeGenTypes &getTypes() const { return CGM.getTypes(); }
1985  ASTContext &getContext() const { return CGM.getContext(); }
1986  CGDebugInfo *getDebugInfo() {
1987    if (DisableDebugInfo)
1988      return nullptr;
1989    return DebugInfo;
1990  }
1991  void disableDebugInfo() { DisableDebugInfo = true; }
1992  void enableDebugInfo() { DisableDebugInfo = false; }
1993
1994  bool shouldUseFusedARCCalls() {
1995    return CGM.getCodeGenOpts().OptimizationLevel == 0;
1996  }
1997
1998  const LangOptions &getLangOpts() const { return CGM.getLangOpts(); }
1999
2000  /// Returns a pointer to the function's exception object and selector slot,
2001  /// which is assigned in every landing pad.
2002  Address getExceptionSlot();
2003  Address getEHSelectorSlot();
2004
2005  /// Returns the contents of the function's exception object and selector
2006  /// slots.
2007  llvm::Value *getExceptionFromSlot();
2008  llvm::Value *getSelectorFromSlot();
2009
2010  Address getNormalCleanupDestSlot();
2011
2012  llvm::BasicBlock *getUnreachableBlock() {
2013    if (!UnreachableBlock) {
2014      UnreachableBlock = createBasicBlock("unreachable");
2015      new llvm::UnreachableInst(getLLVMContext(), UnreachableBlock);
2016    }
2017    return UnreachableBlock;
2018  }
2019
2020  llvm::BasicBlock *getInvokeDest() {
2021    if (!EHStack.requiresLandingPad()) return nullptr;
2022    return getInvokeDestImpl();
2023  }
2024
2025  bool currentFunctionUsesSEHTry() const { return !!CurSEHParent; }
2026
2027  const TargetInfo &getTarget() const { return Target; }
2028  llvm::LLVMContext &getLLVMContext() { return CGM.getLLVMContext(); }
2029  const TargetCodeGenInfo &getTargetHooks() const {
2030    return CGM.getTargetCodeGenInfo();
2031  }
2032
2033  //===--------------------------------------------------------------------===//
2034  //                                  Cleanups
2035  //===--------------------------------------------------------------------===//
2036
2037  typedef void Destroyer(CodeGenFunction &CGF, Address addr, QualType ty);
2038
2039  void pushIrregularPartialArrayCleanup(llvm::Value *arrayBegin,
2040                                        Address arrayEndPointer,
2041                                        QualType elementType,
2042                                        CharUnits elementAlignment,
2043                                        Destroyer *destroyer);
2044  void pushRegularPartialArrayCleanup(llvm::Value *arrayBegin,
2045                                      llvm::Value *arrayEnd,
2046                                      QualType elementType,
2047                                      CharUnits elementAlignment,
2048                                      Destroyer *destroyer);
2049
2050  void pushDestroy(QualType::DestructionKind dtorKind,
2051                   Address addr, QualType type);
2052  void pushEHDestroy(QualType::DestructionKind dtorKind,
2053                     Address addr, QualType type);
2054  void pushDestroy(CleanupKind kind, Address addr, QualType type,
2055                   Destroyer *destroyer, bool useEHCleanupForArray);
2056  void pushLifetimeExtendedDestroy(CleanupKind kind, Address addr,
2057                                   QualType type, Destroyer *destroyer,
2058                                   bool useEHCleanupForArray);
2059  void pushCallObjectDeleteCleanup(const FunctionDecl *OperatorDelete,
2060                                   llvm::Value *CompletePtr,
2061                                   QualType ElementType);
2062  void pushStackRestore(CleanupKind kind, Address SPMem);
2063  void emitDestroy(Address addr, QualType type, Destroyer *destroyer,
2064                   bool useEHCleanupForArray);
2065  llvm::Function *generateDestroyHelper(Address addr, QualType type,
2066                                        Destroyer *destroyer,
2067                                        bool useEHCleanupForArray,
2068                                        const VarDecl *VD);
2069  void emitArrayDestroy(llvm::Value *begin, llvm::Value *end,
2070                        QualType elementType, CharUnits elementAlign,
2071                        Destroyer *destroyer,
2072                        bool checkZeroLength, bool useEHCleanup);
2073
2074  Destroyer *getDestroyer(QualType::DestructionKind destructionKind);
2075
2076  /// Determines whether an EH cleanup is required to destroy a type
2077  /// with the given destruction kind.
2078  bool needsEHCleanup(QualType::DestructionKind kind) {
2079    switch (kind) {
2080    case QualType::DK_none:
2081      return false;
2082    case QualType::DK_cxx_destructor:
2083    case QualType::DK_objc_weak_lifetime:
2084    case QualType::DK_nontrivial_c_struct:
2085      return getLangOpts().Exceptions;
2086    case QualType::DK_objc_strong_lifetime:
2087      return getLangOpts().Exceptions &&
2088             CGM.getCodeGenOpts().ObjCAutoRefCountExceptions;
2089    }
2090    llvm_unreachable("bad destruction kind");
2091  }
2092
2093  CleanupKind getCleanupKind(QualType::DestructionKind kind) {
2094    return (needsEHCleanup(kind) ? NormalAndEHCleanup : NormalCleanup);
2095  }
2096
2097  //===--------------------------------------------------------------------===//
2098  //                                  Objective-C
2099  //===--------------------------------------------------------------------===//
2100
2101  void GenerateObjCMethod(const ObjCMethodDecl *OMD);
2102
2103  void StartObjCMethod(const ObjCMethodDecl *MD, const ObjCContainerDecl *CD);
2104
2105  /// GenerateObjCGetter - Synthesize an Objective-C property getter function.
2106  void GenerateObjCGetter(ObjCImplementationDecl *IMP,
2107                          const ObjCPropertyImplDecl *PID);
2108  void generateObjCGetterBody(const ObjCImplementationDecl *classImpl,
2109                              const ObjCPropertyImplDecl *propImpl,
2110                              const ObjCMethodDecl *GetterMothodDecl,
2111                              llvm::Constant *AtomicHelperFn);
2112
2113  void GenerateObjCCtorDtorMethod(ObjCImplementationDecl *IMP,
2114                                  ObjCMethodDecl *MD, bool ctor);
2115
2116  /// GenerateObjCSetter - Synthesize an Objective-C property setter function
2117  /// for the given property.
2118  void GenerateObjCSetter(ObjCImplementationDecl *IMP,
2119                          const ObjCPropertyImplDecl *PID);
2120  void generateObjCSetterBody(const ObjCImplementationDecl *classImpl,
2121                              const ObjCPropertyImplDecl *propImpl,
2122                              llvm::Constant *AtomicHelperFn);
2123
2124  //===--------------------------------------------------------------------===//
2125  //                                  Block Bits
2126  //===--------------------------------------------------------------------===//
2127
2128  /// Emit block literal.
2129  /// \return an LLVM value which is a pointer to a struct which contains
2130  /// information about the block, including the block invoke function, the
2131  /// captured variables, etc.
2132  llvm::Value *EmitBlockLiteral(const BlockExpr *);
2133
2134  llvm::Function *GenerateBlockFunction(GlobalDecl GD,
2135                                        const CGBlockInfo &Info,
2136                                        const DeclMapTy &ldm,
2137                                        bool IsLambdaConversionToBlock,
2138                                        bool BuildGlobalBlock);
2139
2140  /// Check if \p T is a C++ class that has a destructor that can throw.
2141  static bool cxxDestructorCanThrow(QualType T);
2142
2143  llvm::Constant *GenerateCopyHelperFunction(const CGBlockInfo &blockInfo);
2144  llvm::Constant *GenerateDestroyHelperFunction(const CGBlockInfo &blockInfo);
2145  llvm::Constant *GenerateObjCAtomicSetterCopyHelperFunction(
2146                                             const ObjCPropertyImplDecl *PID);
2147  llvm::Constant *GenerateObjCAtomicGetterCopyHelperFunction(
2148                                             const ObjCPropertyImplDecl *PID);
2149  llvm::Value *EmitBlockCopyAndAutorelease(llvm::Value *Block, QualType Ty);
2150
2151  void BuildBlockRelease(llvm::Value *DeclPtr, BlockFieldFlags flags,
2152                         bool CanThrow);
2153
2154  class AutoVarEmission;
2155
2156  void emitByrefStructureInit(const AutoVarEmission &emission);
2157
2158  /// Enter a cleanup to destroy a __block variable.  Note that this
2159  /// cleanup should be a no-op if the variable hasn't left the stack
2160  /// yet; if a cleanup is required for the variable itself, that needs
2161  /// to be done externally.
2162  ///
2163  /// \param Kind Cleanup kind.
2164  ///
2165  /// \param Addr When \p LoadBlockVarAddr is false, the address of the __block
2166  /// structure that will be passed to _Block_object_dispose. When
2167  /// \p LoadBlockVarAddr is true, the address of the field of the block
2168  /// structure that holds the address of the __block structure.
2169  ///
2170  /// \param Flags The flag that will be passed to _Block_object_dispose.
2171  ///
2172  /// \param LoadBlockVarAddr Indicates whether we need to emit a load from
2173  /// \p Addr to get the address of the __block structure.
2174  void enterByrefCleanup(CleanupKind Kind, Address Addr, BlockFieldFlags Flags,
2175                         bool LoadBlockVarAddr, bool CanThrow);
2176
2177  void setBlockContextParameter(const ImplicitParamDecl *D, unsigned argNum,
2178                                llvm::Value *ptr);
2179
2180  Address LoadBlockStruct();
2181  Address GetAddrOfBlockDecl(const VarDecl *var);
2182
2183  /// BuildBlockByrefAddress - Computes the location of the
2184  /// data in a variable which is declared as __block.
2185  Address emitBlockByrefAddress(Address baseAddr, const VarDecl *V,
2186                                bool followForward = true);
2187  Address emitBlockByrefAddress(Address baseAddr,
2188                                const BlockByrefInfo &info,
2189                                bool followForward,
2190                                const llvm::Twine &name);
2191
2192  const BlockByrefInfo &getBlockByrefInfo(const VarDecl *var);
2193
2194  QualType BuildFunctionArgList(GlobalDecl GD, FunctionArgList &Args);
2195
2196  void GenerateCode(GlobalDecl GD, llvm::Function *Fn,
2197                    const CGFunctionInfo &FnInfo);
2198
2199  /// Annotate the function with an attribute that disables TSan checking at
2200  /// runtime.
2201  void markAsIgnoreThreadCheckingAtRuntime(llvm::Function *Fn);
2202
2203  /// Emit code for the start of a function.
2204  /// \param Loc       The location to be associated with the function.
2205  /// \param StartLoc  The location of the function body.
2206  void StartFunction(GlobalDecl GD,
2207                     QualType RetTy,
2208                     llvm::Function *Fn,
2209                     const CGFunctionInfo &FnInfo,
2210                     const FunctionArgList &Args,
2211                     SourceLocation Loc = SourceLocation(),
2212                     SourceLocation StartLoc = SourceLocation());
2213
2214  static bool IsConstructorDelegationValid(const CXXConstructorDecl *Ctor);
2215
2216  void EmitConstructorBody(FunctionArgList &Args);
2217  void EmitDestructorBody(FunctionArgList &Args);
2218  void emitImplicitAssignmentOperatorBody(FunctionArgList &Args);
2219  void EmitFunctionBody(const Stmt *Body);
2220  void EmitBlockWithFallThrough(llvm::BasicBlock *BB, const Stmt *S);
2221
2222  void EmitForwardingCallToLambda(const CXXMethodDecl *LambdaCallOperator,
2223                                  CallArgList &CallArgs);
2224  void EmitLambdaBlockInvokeBody();
2225  void EmitLambdaDelegatingInvokeBody(const CXXMethodDecl *MD);
2226  void EmitLambdaStaticInvokeBody(const CXXMethodDecl *MD);
2227  void EmitLambdaVLACapture(const VariableArrayType *VAT, LValue LV) {
2228    EmitStoreThroughLValue(RValue::get(VLASizeMap[VAT->getSizeExpr()]), LV);
2229  }
2230  void EmitAsanPrologueOrEpilogue(bool Prologue);
2231
2232  /// Emit the unified return block, trying to avoid its emission when
2233  /// possible.
2234  /// \return The debug location of the user written return statement if the
2235  /// return block is avoided.
2236  llvm::DebugLoc EmitReturnBlock();
2237
2238  /// FinishFunction - Complete IR generation of the current function. It is
2239  /// legal to call this function even if there is no current insertion point.
2240  void FinishFunction(SourceLocation EndLoc=SourceLocation());
2241
2242  void StartThunk(llvm::Function *Fn, GlobalDecl GD,
2243                  const CGFunctionInfo &FnInfo, bool IsUnprototyped);
2244
2245  void EmitCallAndReturnForThunk(llvm::FunctionCallee Callee,
2246                                 const ThunkInfo *Thunk, bool IsUnprototyped);
2247
2248  void FinishThunk();
2249
2250  /// Emit a musttail call for a thunk with a potentially adjusted this pointer.
2251  void EmitMustTailThunk(GlobalDecl GD, llvm::Value *AdjustedThisPtr,
2252                         llvm::FunctionCallee Callee);
2253
2254  /// Generate a thunk for the given method.
2255  void generateThunk(llvm::Function *Fn, const CGFunctionInfo &FnInfo,
2256                     GlobalDecl GD, const ThunkInfo &Thunk,
2257                     bool IsUnprototyped);
2258
2259  llvm::Function *GenerateVarArgsThunk(llvm::Function *Fn,
2260                                       const CGFunctionInfo &FnInfo,
2261                                       GlobalDecl GD, const ThunkInfo &Thunk);
2262
2263  void EmitCtorPrologue(const CXXConstructorDecl *CD, CXXCtorType Type,
2264                        FunctionArgList &Args);
2265
2266  void EmitInitializerForField(FieldDecl *Field, LValue LHS, Expr *Init);
2267
2268  /// Struct with all information about dynamic [sub]class needed to set vptr.
2269  struct VPtr {
2270    BaseSubobject Base;
2271    const CXXRecordDecl *NearestVBase;
2272    CharUnits OffsetFromNearestVBase;
2273    const CXXRecordDecl *VTableClass;
2274  };
2275
2276  /// Initialize the vtable pointer of the given subobject.
2277  void InitializeVTablePointer(const VPtr &vptr);
2278
2279  typedef llvm::SmallVector<VPtr, 4> VPtrsVector;
2280
2281  typedef llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBasesSetTy;
2282  VPtrsVector getVTablePointers(const CXXRecordDecl *VTableClass);
2283
2284  void getVTablePointers(BaseSubobject Base, const CXXRecordDecl *NearestVBase,
2285                         CharUnits OffsetFromNearestVBase,
2286                         bool BaseIsNonVirtualPrimaryBase,
2287                         const CXXRecordDecl *VTableClass,
2288                         VisitedVirtualBasesSetTy &VBases, VPtrsVector &vptrs);
2289
2290  void InitializeVTablePointers(const CXXRecordDecl *ClassDecl);
2291
2292  /// GetVTablePtr - Return the Value of the vtable pointer member pointed
2293  /// to by This.
2294  llvm::Value *GetVTablePtr(Address This, llvm::Type *VTableTy,
2295                            const CXXRecordDecl *VTableClass);
2296
2297  enum CFITypeCheckKind {
2298    CFITCK_VCall,
2299    CFITCK_NVCall,
2300    CFITCK_DerivedCast,
2301    CFITCK_UnrelatedCast,
2302    CFITCK_ICall,
2303    CFITCK_NVMFCall,
2304    CFITCK_VMFCall,
2305  };
2306
2307  /// Derived is the presumed address of an object of type T after a
2308  /// cast. If T is a polymorphic class type, emit a check that the virtual
2309  /// table for Derived belongs to a class derived from T.
2310  void EmitVTablePtrCheckForCast(QualType T, Address Derived, bool MayBeNull,
2311                                 CFITypeCheckKind TCK, SourceLocation Loc);
2312
2313  /// EmitVTablePtrCheckForCall - Virtual method MD is being called via VTable.
2314  /// If vptr CFI is enabled, emit a check that VTable is valid.
2315  void EmitVTablePtrCheckForCall(const CXXRecordDecl *RD, llvm::Value *VTable,
2316                                 CFITypeCheckKind TCK, SourceLocation Loc);
2317
2318  /// EmitVTablePtrCheck - Emit a check that VTable is a valid virtual table for
2319  /// RD using llvm.type.test.
2320  void EmitVTablePtrCheck(const CXXRecordDecl *RD, llvm::Value *VTable,
2321                          CFITypeCheckKind TCK, SourceLocation Loc);
2322
2323  /// If whole-program virtual table optimization is enabled, emit an assumption
2324  /// that VTable is a member of RD's type identifier. Or, if vptr CFI is
2325  /// enabled, emit a check that VTable is a member of RD's type identifier.
2326  void EmitTypeMetadataCodeForVCall(const CXXRecordDecl *RD,
2327                                    llvm::Value *VTable, SourceLocation Loc);
2328
2329  /// Returns whether we should perform a type checked load when loading a
2330  /// virtual function for virtual calls to members of RD. This is generally
2331  /// true when both vcall CFI and whole-program-vtables are enabled.
2332  bool ShouldEmitVTableTypeCheckedLoad(const CXXRecordDecl *RD);
2333
2334  /// Emit a type checked load from the given vtable.
2335  llvm::Value *EmitVTableTypeCheckedLoad(const CXXRecordDecl *RD,
2336                                         llvm::Value *VTable,
2337                                         llvm::Type *VTableTy,
2338                                         uint64_t VTableByteOffset);
2339
2340  /// EnterDtorCleanups - Enter the cleanups necessary to complete the
2341  /// given phase of destruction for a destructor.  The end result
2342  /// should call destructors on members and base classes in reverse
2343  /// order of their construction.
2344  void EnterDtorCleanups(const CXXDestructorDecl *Dtor, CXXDtorType Type);
2345
2346  /// ShouldInstrumentFunction - Return true if the current function should be
2347  /// instrumented with __cyg_profile_func_* calls
2348  bool ShouldInstrumentFunction();
2349
2350  /// ShouldSkipSanitizerInstrumentation - Return true if the current function
2351  /// should not be instrumented with sanitizers.
2352  bool ShouldSkipSanitizerInstrumentation();
2353
2354  /// ShouldXRayInstrument - Return true if the current function should be
2355  /// instrumented with XRay nop sleds.
2356  bool ShouldXRayInstrumentFunction() const;
2357
2358  /// AlwaysEmitXRayCustomEvents - Return true if we must unconditionally emit
2359  /// XRay custom event handling calls.
2360  bool AlwaysEmitXRayCustomEvents() const;
2361
2362  /// AlwaysEmitXRayTypedEvents - Return true if clang must unconditionally emit
2363  /// XRay typed event handling calls.
2364  bool AlwaysEmitXRayTypedEvents() const;
2365
2366  /// Decode an address used in a function prologue, encoded by \c
2367  /// EncodeAddrForUseInPrologue.
2368  llvm::Value *DecodeAddrUsedInPrologue(llvm::Value *F,
2369                                        llvm::Value *EncodedAddr);
2370
2371  /// EmitFunctionProlog - Emit the target specific LLVM code to load the
2372  /// arguments for the given function. This is also responsible for naming the
2373  /// LLVM function arguments.
2374  void EmitFunctionProlog(const CGFunctionInfo &FI,
2375                          llvm::Function *Fn,
2376                          const FunctionArgList &Args);
2377
2378  /// EmitFunctionEpilog - Emit the target specific LLVM code to return the
2379  /// given temporary.
2380  void EmitFunctionEpilog(const CGFunctionInfo &FI, bool EmitRetDbgLoc,
2381                          SourceLocation EndLoc);
2382
2383  /// Emit a test that checks if the return value \p RV is nonnull.
2384  void EmitReturnValueCheck(llvm::Value *RV);
2385
2386  /// EmitStartEHSpec - Emit the start of the exception spec.
2387  void EmitStartEHSpec(const Decl *D);
2388
2389  /// EmitEndEHSpec - Emit the end of the exception spec.
2390  void EmitEndEHSpec(const Decl *D);
2391
2392  /// getTerminateLandingPad - Return a landing pad that just calls terminate.
2393  llvm::BasicBlock *getTerminateLandingPad();
2394
2395  /// getTerminateLandingPad - Return a cleanup funclet that just calls
2396  /// terminate.
2397  llvm::BasicBlock *getTerminateFunclet();
2398
2399  /// getTerminateHandler - Return a handler (not a landing pad, just
2400  /// a catch handler) that just calls terminate.  This is used when
2401  /// a terminate scope encloses a try.
2402  llvm::BasicBlock *getTerminateHandler();
2403
2404  llvm::Type *ConvertTypeForMem(QualType T);
2405  llvm::Type *ConvertType(QualType T);
2406  llvm::Type *ConvertType(const TypeDecl *T) {
2407    return ConvertType(getContext().getTypeDeclType(T));
2408  }
2409
2410  /// LoadObjCSelf - Load the value of self. This function is only valid while
2411  /// generating code for an Objective-C method.
2412  llvm::Value *LoadObjCSelf();
2413
2414  /// TypeOfSelfObject - Return type of object that this self represents.
2415  QualType TypeOfSelfObject();
2416
2417  /// getEvaluationKind - Return the TypeEvaluationKind of QualType \c T.
2418  static TypeEvaluationKind getEvaluationKind(QualType T);
2419
2420  static bool hasScalarEvaluationKind(QualType T) {
2421    return getEvaluationKind(T) == TEK_Scalar;
2422  }
2423
2424  static bool hasAggregateEvaluationKind(QualType T) {
2425    return getEvaluationKind(T) == TEK_Aggregate;
2426  }
2427
2428  /// createBasicBlock - Create an LLVM basic block.
2429  llvm::BasicBlock *createBasicBlock(const Twine &name = "",
2430                                     llvm::Function *parent = nullptr,
2431                                     llvm::BasicBlock *before = nullptr) {
2432    return llvm::BasicBlock::Create(getLLVMContext(), name, parent, before);
2433  }
2434
2435  /// getBasicBlockForLabel - Return the LLVM basicblock that the specified
2436  /// label maps to.
2437  JumpDest getJumpDestForLabel(const LabelDecl *S);
2438
2439  /// SimplifyForwardingBlocks - If the given basic block is only a branch to
2440  /// another basic block, simplify it. This assumes that no other code could
2441  /// potentially reference the basic block.
2442  void SimplifyForwardingBlocks(llvm::BasicBlock *BB);
2443
2444  /// EmitBlock - Emit the given block \arg BB and set it as the insert point,
2445  /// adding a fall-through branch from the current insert block if
2446  /// necessary. It is legal to call this function even if there is no current
2447  /// insertion point.
2448  ///
2449  /// IsFinished - If true, indicates that the caller has finished emitting
2450  /// branches to the given block and does not expect to emit code into it. This
2451  /// means the block can be ignored if it is unreachable.
2452  void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false);
2453
2454  /// EmitBlockAfterUses - Emit the given block somewhere hopefully
2455  /// near its uses, and leave the insertion point in it.
2456  void EmitBlockAfterUses(llvm::BasicBlock *BB);
2457
2458  /// EmitBranch - Emit a branch to the specified basic block from the current
2459  /// insert block, taking care to avoid creation of branches from dummy
2460  /// blocks. It is legal to call this function even if there is no current
2461  /// insertion point.
2462  ///
2463  /// This function clears the current insertion point. The caller should follow
2464  /// calls to this function with calls to Emit*Block prior to generation new
2465  /// code.
2466  void EmitBranch(llvm::BasicBlock *Block);
2467
2468  /// HaveInsertPoint - True if an insertion point is defined. If not, this
2469  /// indicates that the current code being emitted is unreachable.
2470  bool HaveInsertPoint() const {
2471    return Builder.GetInsertBlock() != nullptr;
2472  }
2473
2474  /// EnsureInsertPoint - Ensure that an insertion point is defined so that
2475  /// emitted IR has a place to go. Note that by definition, if this function
2476  /// creates a block then that block is unreachable; callers may do better to
2477  /// detect when no insertion point is defined and simply skip IR generation.
2478  void EnsureInsertPoint() {
2479    if (!HaveInsertPoint())
2480      EmitBlock(createBasicBlock());
2481  }
2482
2483  /// ErrorUnsupported - Print out an error that codegen doesn't support the
2484  /// specified stmt yet.
2485  void ErrorUnsupported(const Stmt *S, const char *Type);
2486
2487  //===--------------------------------------------------------------------===//
2488  //                                  Helpers
2489  //===--------------------------------------------------------------------===//
2490
2491  LValue MakeAddrLValue(Address Addr, QualType T,
2492                        AlignmentSource Source = AlignmentSource::Type) {
2493    return LValue::MakeAddr(Addr, T, getContext(), LValueBaseInfo(Source),
2494                            CGM.getTBAAAccessInfo(T));
2495  }
2496
2497  LValue MakeAddrLValue(Address Addr, QualType T, LValueBaseInfo BaseInfo,
2498                        TBAAAccessInfo TBAAInfo) {
2499    return LValue::MakeAddr(Addr, T, getContext(), BaseInfo, TBAAInfo);
2500  }
2501
2502  LValue MakeAddrLValue(llvm::Value *V, QualType T, CharUnits Alignment,
2503                        AlignmentSource Source = AlignmentSource::Type) {
2504    Address Addr(V, ConvertTypeForMem(T), Alignment);
2505    return LValue::MakeAddr(Addr, T, getContext(), LValueBaseInfo(Source),
2506                            CGM.getTBAAAccessInfo(T));
2507  }
2508
2509  LValue
2510  MakeAddrLValueWithoutTBAA(Address Addr, QualType T,
2511                            AlignmentSource Source = AlignmentSource::Type) {
2512    return LValue::MakeAddr(Addr, T, getContext(), LValueBaseInfo(Source),
2513                            TBAAAccessInfo());
2514  }
2515
2516  LValue MakeNaturalAlignPointeeAddrLValue(llvm::Value *V, QualType T);
2517  LValue MakeNaturalAlignAddrLValue(llvm::Value *V, QualType T);
2518
2519  Address EmitLoadOfReference(LValue RefLVal,
2520                              LValueBaseInfo *PointeeBaseInfo = nullptr,
2521                              TBAAAccessInfo *PointeeTBAAInfo = nullptr);
2522  LValue EmitLoadOfReferenceLValue(LValue RefLVal);
2523  LValue EmitLoadOfReferenceLValue(Address RefAddr, QualType RefTy,
2524                                   AlignmentSource Source =
2525                                       AlignmentSource::Type) {
2526    LValue RefLVal = MakeAddrLValue(RefAddr, RefTy, LValueBaseInfo(Source),
2527                                    CGM.getTBAAAccessInfo(RefTy));
2528    return EmitLoadOfReferenceLValue(RefLVal);
2529  }
2530
2531  /// Load a pointer with type \p PtrTy stored at address \p Ptr.
2532  /// Note that \p PtrTy is the type of the loaded pointer, not the addresses
2533  /// it is loaded from.
2534  Address EmitLoadOfPointer(Address Ptr, const PointerType *PtrTy,
2535                            LValueBaseInfo *BaseInfo = nullptr,
2536                            TBAAAccessInfo *TBAAInfo = nullptr);
2537  LValue EmitLoadOfPointerLValue(Address Ptr, const PointerType *PtrTy);
2538
2539  /// CreateTempAlloca - This creates an alloca and inserts it into the entry
2540  /// block if \p ArraySize is nullptr, otherwise inserts it at the current
2541  /// insertion point of the builder. The caller is responsible for setting an
2542  /// appropriate alignment on
2543  /// the alloca.
2544  ///
2545  /// \p ArraySize is the number of array elements to be allocated if it
2546  ///    is not nullptr.
2547  ///
2548  /// LangAS::Default is the address space of pointers to local variables and
2549  /// temporaries, as exposed in the source language. In certain
2550  /// configurations, this is not the same as the alloca address space, and a
2551  /// cast is needed to lift the pointer from the alloca AS into
2552  /// LangAS::Default. This can happen when the target uses a restricted
2553  /// address space for the stack but the source language requires
2554  /// LangAS::Default to be a generic address space. The latter condition is
2555  /// common for most programming languages; OpenCL is an exception in that
2556  /// LangAS::Default is the private address space, which naturally maps
2557  /// to the stack.
2558  ///
2559  /// Because the address of a temporary is often exposed to the program in
2560  /// various ways, this function will perform the cast. The original alloca
2561  /// instruction is returned through \p Alloca if it is not nullptr.
2562  ///
2563  /// The cast is not performaed in CreateTempAllocaWithoutCast. This is
2564  /// more efficient if the caller knows that the address will not be exposed.
2565  llvm::AllocaInst *CreateTempAlloca(llvm::Type *Ty, const Twine &Name = "tmp",
2566                                     llvm::Value *ArraySize = nullptr);
2567  Address CreateTempAlloca(llvm::Type *Ty, CharUnits align,
2568                           const Twine &Name = "tmp",
2569                           llvm::Value *ArraySize = nullptr,
2570                           Address *Alloca = nullptr);
2571  Address CreateTempAllocaWithoutCast(llvm::Type *Ty, CharUnits align,
2572                                      const Twine &Name = "tmp",
2573                                      llvm::Value *ArraySize = nullptr);
2574
2575  /// CreateDefaultAlignedTempAlloca - This creates an alloca with the
2576  /// default ABI alignment of the given LLVM type.
2577  ///
2578  /// IMPORTANT NOTE: This is *not* generally the right alignment for
2579  /// any given AST type that happens to have been lowered to the
2580  /// given IR type.  This should only ever be used for function-local,
2581  /// IR-driven manipulations like saving and restoring a value.  Do
2582  /// not hand this address off to arbitrary IRGen routines, and especially
2583  /// do not pass it as an argument to a function that might expect a
2584  /// properly ABI-aligned value.
2585  Address CreateDefaultAlignTempAlloca(llvm::Type *Ty,
2586                                       const Twine &Name = "tmp");
2587
2588  /// CreateIRTemp - Create a temporary IR object of the given type, with
2589  /// appropriate alignment. This routine should only be used when an temporary
2590  /// value needs to be stored into an alloca (for example, to avoid explicit
2591  /// PHI construction), but the type is the IR type, not the type appropriate
2592  /// for storing in memory.
2593  ///
2594  /// That is, this is exactly equivalent to CreateMemTemp, but calling
2595  /// ConvertType instead of ConvertTypeForMem.
2596  Address CreateIRTemp(QualType T, const Twine &Name = "tmp");
2597
2598  /// CreateMemTemp - Create a temporary memory object of the given type, with
2599  /// appropriate alignmen and cast it to the default address space. Returns
2600  /// the original alloca instruction by \p Alloca if it is not nullptr.
2601  Address CreateMemTemp(QualType T, const Twine &Name = "tmp",
2602                        Address *Alloca = nullptr);
2603  Address CreateMemTemp(QualType T, CharUnits Align, const Twine &Name = "tmp",
2604                        Address *Alloca = nullptr);
2605
2606  /// CreateMemTemp - Create a temporary memory object of the given type, with
2607  /// appropriate alignmen without casting it to the default address space.
2608  Address CreateMemTempWithoutCast(QualType T, const Twine &Name = "tmp");
2609  Address CreateMemTempWithoutCast(QualType T, CharUnits Align,
2610                                   const Twine &Name = "tmp");
2611
2612  /// CreateAggTemp - Create a temporary memory object for the given
2613  /// aggregate type.
2614  AggValueSlot CreateAggTemp(QualType T, const Twine &Name = "tmp",
2615                             Address *Alloca = nullptr) {
2616    return AggValueSlot::forAddr(CreateMemTemp(T, Name, Alloca),
2617                                 T.getQualifiers(),
2618                                 AggValueSlot::IsNotDestructed,
2619                                 AggValueSlot::DoesNotNeedGCBarriers,
2620                                 AggValueSlot::IsNotAliased,
2621                                 AggValueSlot::DoesNotOverlap);
2622  }
2623
2624  /// Emit a cast to void* in the appropriate address space.
2625  llvm::Value *EmitCastToVoidPtr(llvm::Value *value);
2626
2627  /// EvaluateExprAsBool - Perform the usual unary conversions on the specified
2628  /// expression and compare the result against zero, returning an Int1Ty value.
2629  llvm::Value *EvaluateExprAsBool(const Expr *E);
2630
2631  /// EmitIgnoredExpr - Emit an expression in a context which ignores the result.
2632  void EmitIgnoredExpr(const Expr *E);
2633
2634  /// EmitAnyExpr - Emit code to compute the specified expression which can have
2635  /// any type.  The result is returned as an RValue struct.  If this is an
2636  /// aggregate expression, the aggloc/agglocvolatile arguments indicate where
2637  /// the result should be returned.
2638  ///
2639  /// \param ignoreResult True if the resulting value isn't used.
2640  RValue EmitAnyExpr(const Expr *E,
2641                     AggValueSlot aggSlot = AggValueSlot::ignored(),
2642                     bool ignoreResult = false);
2643
2644  // EmitVAListRef - Emit a "reference" to a va_list; this is either the address
2645  // or the value of the expression, depending on how va_list is defined.
2646  Address EmitVAListRef(const Expr *E);
2647
2648  /// Emit a "reference" to a __builtin_ms_va_list; this is
2649  /// always the value of the expression, because a __builtin_ms_va_list is a
2650  /// pointer to a char.
2651  Address EmitMSVAListRef(const Expr *E);
2652
2653  /// EmitAnyExprToTemp - Similarly to EmitAnyExpr(), however, the result will
2654  /// always be accessible even if no aggregate location is provided.
2655  RValue EmitAnyExprToTemp(const Expr *E);
2656
2657  /// EmitAnyExprToMem - Emits the code necessary to evaluate an
2658  /// arbitrary expression into the given memory location.
2659  void EmitAnyExprToMem(const Expr *E, Address Location,
2660                        Qualifiers Quals, bool IsInitializer);
2661
2662  void EmitAnyExprToExn(const Expr *E, Address Addr);
2663
2664  /// EmitExprAsInit - Emits the code necessary to initialize a
2665  /// location in memory with the given initializer.
2666  void EmitExprAsInit(const Expr *init, const ValueDecl *D, LValue lvalue,
2667                      bool capturedByInit);
2668
2669  /// hasVolatileMember - returns true if aggregate type has a volatile
2670  /// member.
2671  bool hasVolatileMember(QualType T) {
2672    if (const RecordType *RT = T->getAs<RecordType>()) {
2673      const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
2674      return RD->hasVolatileMember();
2675    }
2676    return false;
2677  }
2678
2679  /// Determine whether a return value slot may overlap some other object.
2680  AggValueSlot::Overlap_t getOverlapForReturnValue() {
2681    // FIXME: Assuming no overlap here breaks guaranteed copy elision for base
2682    // class subobjects. These cases may need to be revisited depending on the
2683    // resolution of the relevant core issue.
2684    return AggValueSlot::DoesNotOverlap;
2685  }
2686
2687  /// Determine whether a field initialization may overlap some other object.
2688  AggValueSlot::Overlap_t getOverlapForFieldInit(const FieldDecl *FD);
2689
2690  /// Determine whether a base class initialization may overlap some other
2691  /// object.
2692  AggValueSlot::Overlap_t getOverlapForBaseInit(const CXXRecordDecl *RD,
2693                                                const CXXRecordDecl *BaseRD,
2694                                                bool IsVirtual);
2695
2696  /// Emit an aggregate assignment.
2697  void EmitAggregateAssign(LValue Dest, LValue Src, QualType EltTy) {
2698    bool IsVolatile = hasVolatileMember(EltTy);
2699    EmitAggregateCopy(Dest, Src, EltTy, AggValueSlot::MayOverlap, IsVolatile);
2700  }
2701
2702  void EmitAggregateCopyCtor(LValue Dest, LValue Src,
2703                             AggValueSlot::Overlap_t MayOverlap) {
2704    EmitAggregateCopy(Dest, Src, Src.getType(), MayOverlap);
2705  }
2706
2707  /// EmitAggregateCopy - Emit an aggregate copy.
2708  ///
2709  /// \param isVolatile \c true iff either the source or the destination is
2710  ///        volatile.
2711  /// \param MayOverlap Whether the tail padding of the destination might be
2712  ///        occupied by some other object. More efficient code can often be
2713  ///        generated if not.
2714  void EmitAggregateCopy(LValue Dest, LValue Src, QualType EltTy,
2715                         AggValueSlot::Overlap_t MayOverlap,
2716                         bool isVolatile = false);
2717
2718  /// GetAddrOfLocalVar - Return the address of a local variable.
2719  Address GetAddrOfLocalVar(const VarDecl *VD) {
2720    auto it = LocalDeclMap.find(VD);
2721    assert(it != LocalDeclMap.end() &&
2722           "Invalid argument to GetAddrOfLocalVar(), no decl!");
2723    return it->second;
2724  }
2725
2726  /// Given an opaque value expression, return its LValue mapping if it exists,
2727  /// otherwise create one.
2728  LValue getOrCreateOpaqueLValueMapping(const OpaqueValueExpr *e);
2729
2730  /// Given an opaque value expression, return its RValue mapping if it exists,
2731  /// otherwise create one.
2732  RValue getOrCreateOpaqueRValueMapping(const OpaqueValueExpr *e);
2733
2734  /// Get the index of the current ArrayInitLoopExpr, if any.
2735  llvm::Value *getArrayInitIndex() { return ArrayInitIndex; }
2736
2737  /// getAccessedFieldNo - Given an encoded value and a result number, return
2738  /// the input field number being accessed.
2739  static unsigned getAccessedFieldNo(unsigned Idx, const llvm::Constant *Elts);
2740
2741  llvm::BlockAddress *GetAddrOfLabel(const LabelDecl *L);
2742  llvm::BasicBlock *GetIndirectGotoBlock();
2743
2744  /// Check if \p E is a C++ "this" pointer wrapped in value-preserving casts.
2745  static bool IsWrappedCXXThis(const Expr *E);
2746
2747  /// EmitNullInitialization - Generate code to set a value of the given type to
2748  /// null, If the type contains data member pointers, they will be initialized
2749  /// to -1 in accordance with the Itanium C++ ABI.
2750  void EmitNullInitialization(Address DestPtr, QualType Ty);
2751
2752  /// Emits a call to an LLVM variable-argument intrinsic, either
2753  /// \c llvm.va_start or \c llvm.va_end.
2754  /// \param ArgValue A reference to the \c va_list as emitted by either
2755  /// \c EmitVAListRef or \c EmitMSVAListRef.
2756  /// \param IsStart If \c true, emits a call to \c llvm.va_start; otherwise,
2757  /// calls \c llvm.va_end.
2758  llvm::Value *EmitVAStartEnd(llvm::Value *ArgValue, bool IsStart);
2759
2760  /// Generate code to get an argument from the passed in pointer
2761  /// and update it accordingly.
2762  /// \param VE The \c VAArgExpr for which to generate code.
2763  /// \param VAListAddr Receives a reference to the \c va_list as emitted by
2764  /// either \c EmitVAListRef or \c EmitMSVAListRef.
2765  /// \returns A pointer to the argument.
2766  // FIXME: We should be able to get rid of this method and use the va_arg
2767  // instruction in LLVM instead once it works well enough.
2768  Address EmitVAArg(VAArgExpr *VE, Address &VAListAddr);
2769
2770  /// emitArrayLength - Compute the length of an array, even if it's a
2771  /// VLA, and drill down to the base element type.
2772  llvm::Value *emitArrayLength(const ArrayType *arrayType,
2773                               QualType &baseType,
2774                               Address &addr);
2775
2776  /// EmitVLASize - Capture all the sizes for the VLA expressions in
2777  /// the given variably-modified type and store them in the VLASizeMap.
2778  ///
2779  /// This function can be called with a null (unreachable) insert point.
2780  void EmitVariablyModifiedType(QualType Ty);
2781
2782  struct VlaSizePair {
2783    llvm::Value *NumElts;
2784    QualType Type;
2785
2786    VlaSizePair(llvm::Value *NE, QualType T) : NumElts(NE), Type(T) {}
2787  };
2788
2789  /// Return the number of elements for a single dimension
2790  /// for the given array type.
2791  VlaSizePair getVLAElements1D(const VariableArrayType *vla);
2792  VlaSizePair getVLAElements1D(QualType vla);
2793
2794  /// Returns an LLVM value that corresponds to the size,
2795  /// in non-variably-sized elements, of a variable length array type,
2796  /// plus that largest non-variably-sized element type.  Assumes that
2797  /// the type has already been emitted with EmitVariablyModifiedType.
2798  VlaSizePair getVLASize(const VariableArrayType *vla);
2799  VlaSizePair getVLASize(QualType vla);
2800
2801  /// LoadCXXThis - Load the value of 'this'. This function is only valid while
2802  /// generating code for an C++ member function.
2803  llvm::Value *LoadCXXThis() {
2804    assert(CXXThisValue && "no 'this' value for this function");
2805    return CXXThisValue;
2806  }
2807  Address LoadCXXThisAddress();
2808
2809  /// LoadCXXVTT - Load the VTT parameter to base constructors/destructors have
2810  /// virtual bases.
2811  // FIXME: Every place that calls LoadCXXVTT is something
2812  // that needs to be abstracted properly.
2813  llvm::Value *LoadCXXVTT() {
2814    assert(CXXStructorImplicitParamValue && "no VTT value for this function");
2815    return CXXStructorImplicitParamValue;
2816  }
2817
2818  /// GetAddressOfBaseOfCompleteClass - Convert the given pointer to a
2819  /// complete class to the given direct base.
2820  Address
2821  GetAddressOfDirectBaseInCompleteClass(Address Value,
2822                                        const CXXRecordDecl *Derived,
2823                                        const CXXRecordDecl *Base,
2824                                        bool BaseIsVirtual);
2825
2826  static bool ShouldNullCheckClassCastValue(const CastExpr *Cast);
2827
2828  /// GetAddressOfBaseClass - This function will add the necessary delta to the
2829  /// load of 'this' and returns address of the base class.
2830  Address GetAddressOfBaseClass(Address Value,
2831                                const CXXRecordDecl *Derived,
2832                                CastExpr::path_const_iterator PathBegin,
2833                                CastExpr::path_const_iterator PathEnd,
2834                                bool NullCheckValue, SourceLocation Loc);
2835
2836  Address GetAddressOfDerivedClass(Address Value,
2837                                   const CXXRecordDecl *Derived,
2838                                   CastExpr::path_const_iterator PathBegin,
2839                                   CastExpr::path_const_iterator PathEnd,
2840                                   bool NullCheckValue);
2841
2842  /// GetVTTParameter - Return the VTT parameter that should be passed to a
2843  /// base constructor/destructor with virtual bases.
2844  /// FIXME: VTTs are Itanium ABI-specific, so the definition should move
2845  /// to ItaniumCXXABI.cpp together with all the references to VTT.
2846  llvm::Value *GetVTTParameter(GlobalDecl GD, bool ForVirtualBase,
2847                               bool Delegating);
2848
2849  void EmitDelegateCXXConstructorCall(const CXXConstructorDecl *Ctor,
2850                                      CXXCtorType CtorType,
2851                                      const FunctionArgList &Args,
2852                                      SourceLocation Loc);
2853  // It's important not to confuse this and the previous function. Delegating
2854  // constructors are the C++0x feature. The constructor delegate optimization
2855  // is used to reduce duplication in the base and complete consturctors where
2856  // they are substantially the same.
2857  void EmitDelegatingCXXConstructorCall(const CXXConstructorDecl *Ctor,
2858                                        const FunctionArgList &Args);
2859
2860  /// Emit a call to an inheriting constructor (that is, one that invokes a
2861  /// constructor inherited from a base class) by inlining its definition. This
2862  /// is necessary if the ABI does not support forwarding the arguments to the
2863  /// base class constructor (because they're variadic or similar).
2864  void EmitInlinedInheritingCXXConstructorCall(const CXXConstructorDecl *Ctor,
2865                                               CXXCtorType CtorType,
2866                                               bool ForVirtualBase,
2867                                               bool Delegating,
2868                                               CallArgList &Args);
2869
2870  /// Emit a call to a constructor inherited from a base class, passing the
2871  /// current constructor's arguments along unmodified (without even making
2872  /// a copy).
2873  void EmitInheritedCXXConstructorCall(const CXXConstructorDecl *D,
2874                                       bool ForVirtualBase, Address This,
2875                                       bool InheritedFromVBase,
2876                                       const CXXInheritedCtorInitExpr *E);
2877
2878  void EmitCXXConstructorCall(const CXXConstructorDecl *D, CXXCtorType Type,
2879                              bool ForVirtualBase, bool Delegating,
2880                              AggValueSlot ThisAVS, const CXXConstructExpr *E);
2881
2882  void EmitCXXConstructorCall(const CXXConstructorDecl *D, CXXCtorType Type,
2883                              bool ForVirtualBase, bool Delegating,
2884                              Address This, CallArgList &Args,
2885                              AggValueSlot::Overlap_t Overlap,
2886                              SourceLocation Loc, bool NewPointerIsChecked);
2887
2888  /// Emit assumption load for all bases. Requires to be called only on
2889  /// most-derived class and not under construction of the object.
2890  void EmitVTableAssumptionLoads(const CXXRecordDecl *ClassDecl, Address This);
2891
2892  /// Emit assumption that vptr load == global vtable.
2893  void EmitVTableAssumptionLoad(const VPtr &vptr, Address This);
2894
2895  void EmitSynthesizedCXXCopyCtorCall(const CXXConstructorDecl *D,
2896                                      Address This, Address Src,
2897                                      const CXXConstructExpr *E);
2898
2899  void EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
2900                                  const ArrayType *ArrayTy,
2901                                  Address ArrayPtr,
2902                                  const CXXConstructExpr *E,
2903                                  bool NewPointerIsChecked,
2904                                  bool ZeroInitialization = false);
2905
2906  void EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
2907                                  llvm::Value *NumElements,
2908                                  Address ArrayPtr,
2909                                  const CXXConstructExpr *E,
2910                                  bool NewPointerIsChecked,
2911                                  bool ZeroInitialization = false);
2912
2913  static Destroyer destroyCXXObject;
2914
2915  void EmitCXXDestructorCall(const CXXDestructorDecl *D, CXXDtorType Type,
2916                             bool ForVirtualBase, bool Delegating, Address This,
2917                             QualType ThisTy);
2918
2919  void EmitNewArrayInitializer(const CXXNewExpr *E, QualType elementType,
2920                               llvm::Type *ElementTy, Address NewPtr,
2921                               llvm::Value *NumElements,
2922                               llvm::Value *AllocSizeWithoutCookie);
2923
2924  void EmitCXXTemporary(const CXXTemporary *Temporary, QualType TempType,
2925                        Address Ptr);
2926
2927  void EmitSehCppScopeBegin();
2928  void EmitSehCppScopeEnd();
2929  void EmitSehTryScopeBegin();
2930  void EmitSehTryScopeEnd();
2931
2932  llvm::Value *EmitLifetimeStart(llvm::TypeSize Size, llvm::Value *Addr);
2933  void EmitLifetimeEnd(llvm::Value *Size, llvm::Value *Addr);
2934
2935  llvm::Value *EmitCXXNewExpr(const CXXNewExpr *E);
2936  void EmitCXXDeleteExpr(const CXXDeleteExpr *E);
2937
2938  void EmitDeleteCall(const FunctionDecl *DeleteFD, llvm::Value *Ptr,
2939                      QualType DeleteTy, llvm::Value *NumElements = nullptr,
2940                      CharUnits CookieSize = CharUnits());
2941
2942  RValue EmitBuiltinNewDeleteCall(const FunctionProtoType *Type,
2943                                  const CallExpr *TheCallExpr, bool IsDelete);
2944
2945  llvm::Value *EmitCXXTypeidExpr(const CXXTypeidExpr *E);
2946  llvm::Value *EmitDynamicCast(Address V, const CXXDynamicCastExpr *DCE);
2947  Address EmitCXXUuidofExpr(const CXXUuidofExpr *E);
2948
2949  /// Situations in which we might emit a check for the suitability of a
2950  /// pointer or glvalue. Needs to be kept in sync with ubsan_handlers.cpp in
2951  /// compiler-rt.
2952  enum TypeCheckKind {
2953    /// Checking the operand of a load. Must be suitably sized and aligned.
2954    TCK_Load,
2955    /// Checking the destination of a store. Must be suitably sized and aligned.
2956    TCK_Store,
2957    /// Checking the bound value in a reference binding. Must be suitably sized
2958    /// and aligned, but is not required to refer to an object (until the
2959    /// reference is used), per core issue 453.
2960    TCK_ReferenceBinding,
2961    /// Checking the object expression in a non-static data member access. Must
2962    /// be an object within its lifetime.
2963    TCK_MemberAccess,
2964    /// Checking the 'this' pointer for a call to a non-static member function.
2965    /// Must be an object within its lifetime.
2966    TCK_MemberCall,
2967    /// Checking the 'this' pointer for a constructor call.
2968    TCK_ConstructorCall,
2969    /// Checking the operand of a static_cast to a derived pointer type. Must be
2970    /// null or an object within its lifetime.
2971    TCK_DowncastPointer,
2972    /// Checking the operand of a static_cast to a derived reference type. Must
2973    /// be an object within its lifetime.
2974    TCK_DowncastReference,
2975    /// Checking the operand of a cast to a base object. Must be suitably sized
2976    /// and aligned.
2977    TCK_Upcast,
2978    /// Checking the operand of a cast to a virtual base object. Must be an
2979    /// object within its lifetime.
2980    TCK_UpcastToVirtualBase,
2981    /// Checking the value assigned to a _Nonnull pointer. Must not be null.
2982    TCK_NonnullAssign,
2983    /// Checking the operand of a dynamic_cast or a typeid expression.  Must be
2984    /// null or an object within its lifetime.
2985    TCK_DynamicOperation
2986  };
2987
2988  /// Determine whether the pointer type check \p TCK permits null pointers.
2989  static bool isNullPointerAllowed(TypeCheckKind TCK);
2990
2991  /// Determine whether the pointer type check \p TCK requires a vptr check.
2992  static bool isVptrCheckRequired(TypeCheckKind TCK, QualType Ty);
2993
2994  /// Whether any type-checking sanitizers are enabled. If \c false,
2995  /// calls to EmitTypeCheck can be skipped.
2996  bool sanitizePerformTypeCheck() const;
2997
2998  /// Emit a check that \p V is the address of storage of the
2999  /// appropriate size and alignment for an object of type \p Type
3000  /// (or if ArraySize is provided, for an array of that bound).
3001  void EmitTypeCheck(TypeCheckKind TCK, SourceLocation Loc, llvm::Value *V,
3002                     QualType Type, CharUnits Alignment = CharUnits::Zero(),
3003                     SanitizerSet SkippedChecks = SanitizerSet(),
3004                     llvm::Value *ArraySize = nullptr);
3005
3006  /// Emit a check that \p Base points into an array object, which
3007  /// we can access at index \p Index. \p Accessed should be \c false if we
3008  /// this expression is used as an lvalue, for instance in "&Arr[Idx]".
3009  void EmitBoundsCheck(const Expr *E, const Expr *Base, llvm::Value *Index,
3010                       QualType IndexType, bool Accessed);
3011
3012  llvm::Value *EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV,
3013                                       bool isInc, bool isPre);
3014  ComplexPairTy EmitComplexPrePostIncDec(const UnaryOperator *E, LValue LV,
3015                                         bool isInc, bool isPre);
3016
3017  /// Converts Location to a DebugLoc, if debug information is enabled.
3018  llvm::DebugLoc SourceLocToDebugLoc(SourceLocation Location);
3019
3020  /// Get the record field index as represented in debug info.
3021  unsigned getDebugInfoFIndex(const RecordDecl *Rec, unsigned FieldIndex);
3022
3023
3024  //===--------------------------------------------------------------------===//
3025  //                            Declaration Emission
3026  //===--------------------------------------------------------------------===//
3027
3028  /// EmitDecl - Emit a declaration.
3029  ///
3030  /// This function can be called with a null (unreachable) insert point.
3031  void EmitDecl(const Decl &D);
3032
3033  /// EmitVarDecl - Emit a local variable declaration.
3034  ///
3035  /// This function can be called with a null (unreachable) insert point.
3036  void EmitVarDecl(const VarDecl &D);
3037
3038  void EmitScalarInit(const Expr *init, const ValueDecl *D, LValue lvalue,
3039                      bool capturedByInit);
3040
3041  typedef void SpecialInitFn(CodeGenFunction &Init, const VarDecl &D,
3042                             llvm::Value *Address);
3043
3044  /// Determine whether the given initializer is trivial in the sense
3045  /// that it requires no code to be generated.
3046  bool isTrivialInitializer(const Expr *Init);
3047
3048  /// EmitAutoVarDecl - Emit an auto variable declaration.
3049  ///
3050  /// This function can be called with a null (unreachable) insert point.
3051  void EmitAutoVarDecl(const VarDecl &D);
3052
3053  class AutoVarEmission {
3054    friend class CodeGenFunction;
3055
3056    const VarDecl *Variable;
3057
3058    /// The address of the alloca for languages with explicit address space
3059    /// (e.g. OpenCL) or alloca casted to generic pointer for address space
3060    /// agnostic languages (e.g. C++). Invalid if the variable was emitted
3061    /// as a global constant.
3062    Address Addr;
3063
3064    llvm::Value *NRVOFlag;
3065
3066    /// True if the variable is a __block variable that is captured by an
3067    /// escaping block.
3068    bool IsEscapingByRef;
3069
3070    /// True if the variable is of aggregate type and has a constant
3071    /// initializer.
3072    bool IsConstantAggregate;
3073
3074    /// Non-null if we should use lifetime annotations.
3075    llvm::Value *SizeForLifetimeMarkers;
3076
3077    /// Address with original alloca instruction. Invalid if the variable was
3078    /// emitted as a global constant.
3079    Address AllocaAddr;
3080
3081    struct Invalid {};
3082    AutoVarEmission(Invalid)
3083        : Variable(nullptr), Addr(Address::invalid()),
3084          AllocaAddr(Address::invalid()) {}
3085
3086    AutoVarEmission(const VarDecl &variable)
3087        : Variable(&variable), Addr(Address::invalid()), NRVOFlag(nullptr),
3088          IsEscapingByRef(false), IsConstantAggregate(false),
3089          SizeForLifetimeMarkers(nullptr), AllocaAddr(Address::invalid()) {}
3090
3091    bool wasEmittedAsGlobal() const { return !Addr.isValid(); }
3092
3093  public:
3094    static AutoVarEmission invalid() { return AutoVarEmission(Invalid()); }
3095
3096    bool useLifetimeMarkers() const {
3097      return SizeForLifetimeMarkers != nullptr;
3098    }
3099    llvm::Value *getSizeForLifetimeMarkers() const {
3100      assert(useLifetimeMarkers());
3101      return SizeForLifetimeMarkers;
3102    }
3103
3104    /// Returns the raw, allocated address, which is not necessarily
3105    /// the address of the object itself. It is casted to default
3106    /// address space for address space agnostic languages.
3107    Address getAllocatedAddress() const {
3108      return Addr;
3109    }
3110
3111    /// Returns the address for the original alloca instruction.
3112    Address getOriginalAllocatedAddress() const { return AllocaAddr; }
3113
3114    /// Returns the address of the object within this declaration.
3115    /// Note that this does not chase the forwarding pointer for
3116    /// __block decls.
3117    Address getObjectAddress(CodeGenFunction &CGF) const {
3118      if (!IsEscapingByRef) return Addr;
3119
3120      return CGF.emitBlockByrefAddress(Addr, Variable, /*forward*/ false);
3121    }
3122  };
3123  AutoVarEmission EmitAutoVarAlloca(const VarDecl &var);
3124  void EmitAutoVarInit(const AutoVarEmission &emission);
3125  void EmitAutoVarCleanups(const AutoVarEmission &emission);
3126  void emitAutoVarTypeCleanup(const AutoVarEmission &emission,
3127                              QualType::DestructionKind dtorKind);
3128
3129  /// Emits the alloca and debug information for the size expressions for each
3130  /// dimension of an array. It registers the association of its (1-dimensional)
3131  /// QualTypes and size expression's debug node, so that CGDebugInfo can
3132  /// reference this node when creating the DISubrange object to describe the
3133  /// array types.
3134  void EmitAndRegisterVariableArrayDimensions(CGDebugInfo *DI,
3135                                              const VarDecl &D,
3136                                              bool EmitDebugInfo);
3137
3138  void EmitStaticVarDecl(const VarDecl &D,
3139                         llvm::GlobalValue::LinkageTypes Linkage);
3140
3141  class ParamValue {
3142    llvm::Value *Value;
3143    llvm::Type *ElementType;
3144    unsigned Alignment;
3145    ParamValue(llvm::Value *V, llvm::Type *T, unsigned A)
3146        : Value(V), ElementType(T), Alignment(A) {}
3147  public:
3148    static ParamValue forDirect(llvm::Value *value) {
3149      return ParamValue(value, nullptr, 0);
3150    }
3151    static ParamValue forIndirect(Address addr) {
3152      assert(!addr.getAlignment().isZero());
3153      return ParamValue(addr.getPointer(), addr.getElementType(),
3154                        addr.getAlignment().getQuantity());
3155    }
3156
3157    bool isIndirect() const { return Alignment != 0; }
3158    llvm::Value *getAnyValue() const { return Value; }
3159
3160    llvm::Value *getDirectValue() const {
3161      assert(!isIndirect());
3162      return Value;
3163    }
3164
3165    Address getIndirectAddress() const {
3166      assert(isIndirect());
3167      return Address(Value, ElementType, CharUnits::fromQuantity(Alignment));
3168    }
3169  };
3170
3171  /// EmitParmDecl - Emit a ParmVarDecl or an ImplicitParamDecl.
3172  void EmitParmDecl(const VarDecl &D, ParamValue Arg, unsigned ArgNo);
3173
3174  /// protectFromPeepholes - Protect a value that we're intending to
3175  /// store to the side, but which will probably be used later, from
3176  /// aggressive peepholing optimizations that might delete it.
3177  ///
3178  /// Pass the result to unprotectFromPeepholes to declare that
3179  /// protection is no longer required.
3180  ///
3181  /// There's no particular reason why this shouldn't apply to
3182  /// l-values, it's just that no existing peepholes work on pointers.
3183  PeepholeProtection protectFromPeepholes(RValue rvalue);
3184  void unprotectFromPeepholes(PeepholeProtection protection);
3185
3186  void emitAlignmentAssumptionCheck(llvm::Value *Ptr, QualType Ty,
3187                                    SourceLocation Loc,
3188                                    SourceLocation AssumptionLoc,
3189                                    llvm::Value *Alignment,
3190                                    llvm::Value *OffsetValue,
3191                                    llvm::Value *TheCheck,
3192                                    llvm::Instruction *Assumption);
3193
3194  void emitAlignmentAssumption(llvm::Value *PtrValue, QualType Ty,
3195                               SourceLocation Loc, SourceLocation AssumptionLoc,
3196                               llvm::Value *Alignment,
3197                               llvm::Value *OffsetValue = nullptr);
3198
3199  void emitAlignmentAssumption(llvm::Value *PtrValue, const Expr *E,
3200                               SourceLocation AssumptionLoc,
3201                               llvm::Value *Alignment,
3202                               llvm::Value *OffsetValue = nullptr);
3203
3204  //===--------------------------------------------------------------------===//
3205  //                             Statement Emission
3206  //===--------------------------------------------------------------------===//
3207
3208  /// EmitStopPoint - Emit a debug stoppoint if we are emitting debug info.
3209  void EmitStopPoint(const Stmt *S);
3210
3211  /// EmitStmt - Emit the code for the statement \arg S. It is legal to call
3212  /// this function even if there is no current insertion point.
3213  ///
3214  /// This function may clear the current insertion point; callers should use
3215  /// EnsureInsertPoint if they wish to subsequently generate code without first
3216  /// calling EmitBlock, EmitBranch, or EmitStmt.
3217  void EmitStmt(const Stmt *S, ArrayRef<const Attr *> Attrs = std::nullopt);
3218
3219  /// EmitSimpleStmt - Try to emit a "simple" statement which does not
3220  /// necessarily require an insertion point or debug information; typically
3221  /// because the statement amounts to a jump or a container of other
3222  /// statements.
3223  ///
3224  /// \return True if the statement was handled.
3225  bool EmitSimpleStmt(const Stmt *S, ArrayRef<const Attr *> Attrs);
3226
3227  Address EmitCompoundStmt(const CompoundStmt &S, bool GetLast = false,
3228                           AggValueSlot AVS = AggValueSlot::ignored());
3229  Address EmitCompoundStmtWithoutScope(const CompoundStmt &S,
3230                                       bool GetLast = false,
3231                                       AggValueSlot AVS =
3232                                                AggValueSlot::ignored());
3233
3234  /// EmitLabel - Emit the block for the given label. It is legal to call this
3235  /// function even if there is no current insertion point.
3236  void EmitLabel(const LabelDecl *D); // helper for EmitLabelStmt.
3237
3238  void EmitLabelStmt(const LabelStmt &S);
3239  void EmitAttributedStmt(const AttributedStmt &S);
3240  void EmitGotoStmt(const GotoStmt &S);
3241  void EmitIndirectGotoStmt(const IndirectGotoStmt &S);
3242  void EmitIfStmt(const IfStmt &S);
3243
3244  void EmitWhileStmt(const WhileStmt &S,
3245                     ArrayRef<const Attr *> Attrs = std::nullopt);
3246  void EmitDoStmt(const DoStmt &S, ArrayRef<const Attr *> Attrs = std::nullopt);
3247  void EmitForStmt(const ForStmt &S,
3248                   ArrayRef<const Attr *> Attrs = std::nullopt);
3249  void EmitReturnStmt(const ReturnStmt &S);
3250  void EmitDeclStmt(const DeclStmt &S);
3251  void EmitBreakStmt(const BreakStmt &S);
3252  void EmitContinueStmt(const ContinueStmt &S);
3253  void EmitSwitchStmt(const SwitchStmt &S);
3254  void EmitDefaultStmt(const DefaultStmt &S, ArrayRef<const Attr *> Attrs);
3255  void EmitCaseStmt(const CaseStmt &S, ArrayRef<const Attr *> Attrs);
3256  void EmitCaseStmtRange(const CaseStmt &S, ArrayRef<const Attr *> Attrs);
3257  void EmitAsmStmt(const AsmStmt &S);
3258
3259  void EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S);
3260  void EmitObjCAtTryStmt(const ObjCAtTryStmt &S);
3261  void EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S);
3262  void EmitObjCAtSynchronizedStmt(const ObjCAtSynchronizedStmt &S);
3263  void EmitObjCAutoreleasePoolStmt(const ObjCAutoreleasePoolStmt &S);
3264
3265  void EmitCoroutineBody(const CoroutineBodyStmt &S);
3266  void EmitCoreturnStmt(const CoreturnStmt &S);
3267  RValue EmitCoawaitExpr(const CoawaitExpr &E,
3268                         AggValueSlot aggSlot = AggValueSlot::ignored(),
3269                         bool ignoreResult = false);
3270  LValue EmitCoawaitLValue(const CoawaitExpr *E);
3271  RValue EmitCoyieldExpr(const CoyieldExpr &E,
3272                         AggValueSlot aggSlot = AggValueSlot::ignored(),
3273                         bool ignoreResult = false);
3274  LValue EmitCoyieldLValue(const CoyieldExpr *E);
3275  RValue EmitCoroutineIntrinsic(const CallExpr *E, unsigned int IID);
3276
3277  void EnterCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock = false);
3278  void ExitCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock = false);
3279
3280  void EmitCXXTryStmt(const CXXTryStmt &S);
3281  void EmitSEHTryStmt(const SEHTryStmt &S);
3282  void EmitSEHLeaveStmt(const SEHLeaveStmt &S);
3283  void EnterSEHTryStmt(const SEHTryStmt &S);
3284  void ExitSEHTryStmt(const SEHTryStmt &S);
3285  void VolatilizeTryBlocks(llvm::BasicBlock *BB,
3286                           llvm::SmallPtrSet<llvm::BasicBlock *, 10> &V);
3287
3288  void pushSEHCleanup(CleanupKind kind,
3289                      llvm::Function *FinallyFunc);
3290  void startOutlinedSEHHelper(CodeGenFunction &ParentCGF, bool IsFilter,
3291                              const Stmt *OutlinedStmt);
3292
3293  llvm::Function *GenerateSEHFilterFunction(CodeGenFunction &ParentCGF,
3294                                            const SEHExceptStmt &Except);
3295
3296  llvm::Function *GenerateSEHFinallyFunction(CodeGenFunction &ParentCGF,
3297                                             const SEHFinallyStmt &Finally);
3298
3299  void EmitSEHExceptionCodeSave(CodeGenFunction &ParentCGF,
3300                                llvm::Value *ParentFP,
3301                                llvm::Value *EntryEBP);
3302  llvm::Value *EmitSEHExceptionCode();
3303  llvm::Value *EmitSEHExceptionInfo();
3304  llvm::Value *EmitSEHAbnormalTermination();
3305
3306  /// Emit simple code for OpenMP directives in Simd-only mode.
3307  void EmitSimpleOMPExecutableDirective(const OMPExecutableDirective &D);
3308
3309  /// Scan the outlined statement for captures from the parent function. For
3310  /// each capture, mark the capture as escaped and emit a call to
3311  /// llvm.localrecover. Insert the localrecover result into the LocalDeclMap.
3312  void EmitCapturedLocals(CodeGenFunction &ParentCGF, const Stmt *OutlinedStmt,
3313                          bool IsFilter);
3314
3315  /// Recovers the address of a local in a parent function. ParentVar is the
3316  /// address of the variable used in the immediate parent function. It can
3317  /// either be an alloca or a call to llvm.localrecover if there are nested
3318  /// outlined functions. ParentFP is the frame pointer of the outermost parent
3319  /// frame.
3320  Address recoverAddrOfEscapedLocal(CodeGenFunction &ParentCGF,
3321                                    Address ParentVar,
3322                                    llvm::Value *ParentFP);
3323
3324  void EmitCXXForRangeStmt(const CXXForRangeStmt &S,
3325                           ArrayRef<const Attr *> Attrs = std::nullopt);
3326
3327  /// Controls insertion of cancellation exit blocks in worksharing constructs.
3328  class OMPCancelStackRAII {
3329    CodeGenFunction &CGF;
3330
3331  public:
3332    OMPCancelStackRAII(CodeGenFunction &CGF, OpenMPDirectiveKind Kind,
3333                       bool HasCancel)
3334        : CGF(CGF) {
3335      CGF.OMPCancelStack.enter(CGF, Kind, HasCancel);
3336    }
3337    ~OMPCancelStackRAII() { CGF.OMPCancelStack.exit(CGF); }
3338  };
3339
3340  /// Returns calculated size of the specified type.
3341  llvm::Value *getTypeSize(QualType Ty);
3342  LValue InitCapturedStruct(const CapturedStmt &S);
3343  llvm::Function *EmitCapturedStmt(const CapturedStmt &S, CapturedRegionKind K);
3344  llvm::Function *GenerateCapturedStmtFunction(const CapturedStmt &S);
3345  Address GenerateCapturedStmtArgument(const CapturedStmt &S);
3346  llvm::Function *GenerateOpenMPCapturedStmtFunction(const CapturedStmt &S,
3347                                                     SourceLocation Loc);
3348  void GenerateOpenMPCapturedVars(const CapturedStmt &S,
3349                                  SmallVectorImpl<llvm::Value *> &CapturedVars);
3350  void emitOMPSimpleStore(LValue LVal, RValue RVal, QualType RValTy,
3351                          SourceLocation Loc);
3352  /// Perform element by element copying of arrays with type \a
3353  /// OriginalType from \a SrcAddr to \a DestAddr using copying procedure
3354  /// generated by \a CopyGen.
3355  ///
3356  /// \param DestAddr Address of the destination array.
3357  /// \param SrcAddr Address of the source array.
3358  /// \param OriginalType Type of destination and source arrays.
3359  /// \param CopyGen Copying procedure that copies value of single array element
3360  /// to another single array element.
3361  void EmitOMPAggregateAssign(
3362      Address DestAddr, Address SrcAddr, QualType OriginalType,
3363      const llvm::function_ref<void(Address, Address)> CopyGen);
3364  /// Emit proper copying of data from one variable to another.
3365  ///
3366  /// \param OriginalType Original type of the copied variables.
3367  /// \param DestAddr Destination address.
3368  /// \param SrcAddr Source address.
3369  /// \param DestVD Destination variable used in \a CopyExpr (for arrays, has
3370  /// type of the base array element).
3371  /// \param SrcVD Source variable used in \a CopyExpr (for arrays, has type of
3372  /// the base array element).
3373  /// \param Copy Actual copygin expression for copying data from \a SrcVD to \a
3374  /// DestVD.
3375  void EmitOMPCopy(QualType OriginalType,
3376                   Address DestAddr, Address SrcAddr,
3377                   const VarDecl *DestVD, const VarDecl *SrcVD,
3378                   const Expr *Copy);
3379  /// Emit atomic update code for constructs: \a X = \a X \a BO \a E or
3380  /// \a X = \a E \a BO \a E.
3381  ///
3382  /// \param X Value to be updated.
3383  /// \param E Update value.
3384  /// \param BO Binary operation for update operation.
3385  /// \param IsXLHSInRHSPart true if \a X is LHS in RHS part of the update
3386  /// expression, false otherwise.
3387  /// \param AO Atomic ordering of the generated atomic instructions.
3388  /// \param CommonGen Code generator for complex expressions that cannot be
3389  /// expressed through atomicrmw instruction.
3390  /// \returns <true, OldAtomicValue> if simple 'atomicrmw' instruction was
3391  /// generated, <false, RValue::get(nullptr)> otherwise.
3392  std::pair<bool, RValue> EmitOMPAtomicSimpleUpdateExpr(
3393      LValue X, RValue E, BinaryOperatorKind BO, bool IsXLHSInRHSPart,
3394      llvm::AtomicOrdering AO, SourceLocation Loc,
3395      const llvm::function_ref<RValue(RValue)> CommonGen);
3396  bool EmitOMPFirstprivateClause(const OMPExecutableDirective &D,
3397                                 OMPPrivateScope &PrivateScope);
3398  void EmitOMPPrivateClause(const OMPExecutableDirective &D,
3399                            OMPPrivateScope &PrivateScope);
3400  void EmitOMPUseDevicePtrClause(
3401      const OMPUseDevicePtrClause &C, OMPPrivateScope &PrivateScope,
3402      const llvm::DenseMap<const ValueDecl *, Address> &CaptureDeviceAddrMap);
3403  void EmitOMPUseDeviceAddrClause(
3404      const OMPUseDeviceAddrClause &C, OMPPrivateScope &PrivateScope,
3405      const llvm::DenseMap<const ValueDecl *, Address> &CaptureDeviceAddrMap);
3406  /// Emit code for copyin clause in \a D directive. The next code is
3407  /// generated at the start of outlined functions for directives:
3408  /// \code
3409  /// threadprivate_var1 = master_threadprivate_var1;
3410  /// operator=(threadprivate_var2, master_threadprivate_var2);
3411  /// ...
3412  /// __kmpc_barrier(&loc, global_tid);
3413  /// \endcode
3414  ///
3415  /// \param D OpenMP directive possibly with 'copyin' clause(s).
3416  /// \returns true if at least one copyin variable is found, false otherwise.
3417  bool EmitOMPCopyinClause(const OMPExecutableDirective &D);
3418  /// Emit initial code for lastprivate variables. If some variable is
3419  /// not also firstprivate, then the default initialization is used. Otherwise
3420  /// initialization of this variable is performed by EmitOMPFirstprivateClause
3421  /// method.
3422  ///
3423  /// \param D Directive that may have 'lastprivate' directives.
3424  /// \param PrivateScope Private scope for capturing lastprivate variables for
3425  /// proper codegen in internal captured statement.
3426  ///
3427  /// \returns true if there is at least one lastprivate variable, false
3428  /// otherwise.
3429  bool EmitOMPLastprivateClauseInit(const OMPExecutableDirective &D,
3430                                    OMPPrivateScope &PrivateScope);
3431  /// Emit final copying of lastprivate values to original variables at
3432  /// the end of the worksharing or simd directive.
3433  ///
3434  /// \param D Directive that has at least one 'lastprivate' directives.
3435  /// \param IsLastIterCond Boolean condition that must be set to 'i1 true' if
3436  /// it is the last iteration of the loop code in associated directive, or to
3437  /// 'i1 false' otherwise. If this item is nullptr, no final check is required.
3438  void EmitOMPLastprivateClauseFinal(const OMPExecutableDirective &D,
3439                                     bool NoFinals,
3440                                     llvm::Value *IsLastIterCond = nullptr);
3441  /// Emit initial code for linear clauses.
3442  void EmitOMPLinearClause(const OMPLoopDirective &D,
3443                           CodeGenFunction::OMPPrivateScope &PrivateScope);
3444  /// Emit final code for linear clauses.
3445  /// \param CondGen Optional conditional code for final part of codegen for
3446  /// linear clause.
3447  void EmitOMPLinearClauseFinal(
3448      const OMPLoopDirective &D,
3449      const llvm::function_ref<llvm::Value *(CodeGenFunction &)> CondGen);
3450  /// Emit initial code for reduction variables. Creates reduction copies
3451  /// and initializes them with the values according to OpenMP standard.
3452  ///
3453  /// \param D Directive (possibly) with the 'reduction' clause.
3454  /// \param PrivateScope Private scope for capturing reduction variables for
3455  /// proper codegen in internal captured statement.
3456  ///
3457  void EmitOMPReductionClauseInit(const OMPExecutableDirective &D,
3458                                  OMPPrivateScope &PrivateScope,
3459                                  bool ForInscan = false);
3460  /// Emit final update of reduction values to original variables at
3461  /// the end of the directive.
3462  ///
3463  /// \param D Directive that has at least one 'reduction' directives.
3464  /// \param ReductionKind The kind of reduction to perform.
3465  void EmitOMPReductionClauseFinal(const OMPExecutableDirective &D,
3466                                   const OpenMPDirectiveKind ReductionKind);
3467  /// Emit initial code for linear variables. Creates private copies
3468  /// and initializes them with the values according to OpenMP standard.
3469  ///
3470  /// \param D Directive (possibly) with the 'linear' clause.
3471  /// \return true if at least one linear variable is found that should be
3472  /// initialized with the value of the original variable, false otherwise.
3473  bool EmitOMPLinearClauseInit(const OMPLoopDirective &D);
3474
3475  typedef const llvm::function_ref<void(CodeGenFunction & /*CGF*/,
3476                                        llvm::Function * /*OutlinedFn*/,
3477                                        const OMPTaskDataTy & /*Data*/)>
3478      TaskGenTy;
3479  void EmitOMPTaskBasedDirective(const OMPExecutableDirective &S,
3480                                 const OpenMPDirectiveKind CapturedRegion,
3481                                 const RegionCodeGenTy &BodyGen,
3482                                 const TaskGenTy &TaskGen, OMPTaskDataTy &Data);
3483  struct OMPTargetDataInfo {
3484    Address BasePointersArray = Address::invalid();
3485    Address PointersArray = Address::invalid();
3486    Address SizesArray = Address::invalid();
3487    Address MappersArray = Address::invalid();
3488    unsigned NumberOfTargetItems = 0;
3489    explicit OMPTargetDataInfo() = default;
3490    OMPTargetDataInfo(Address BasePointersArray, Address PointersArray,
3491                      Address SizesArray, Address MappersArray,
3492                      unsigned NumberOfTargetItems)
3493        : BasePointersArray(BasePointersArray), PointersArray(PointersArray),
3494          SizesArray(SizesArray), MappersArray(MappersArray),
3495          NumberOfTargetItems(NumberOfTargetItems) {}
3496  };
3497  void EmitOMPTargetTaskBasedDirective(const OMPExecutableDirective &S,
3498                                       const RegionCodeGenTy &BodyGen,
3499                                       OMPTargetDataInfo &InputInfo);
3500  void processInReduction(const OMPExecutableDirective &S,
3501                          OMPTaskDataTy &Data,
3502                          CodeGenFunction &CGF,
3503                          const CapturedStmt *CS,
3504                          OMPPrivateScope &Scope);
3505  void EmitOMPMetaDirective(const OMPMetaDirective &S);
3506  void EmitOMPParallelDirective(const OMPParallelDirective &S);
3507  void EmitOMPSimdDirective(const OMPSimdDirective &S);
3508  void EmitOMPTileDirective(const OMPTileDirective &S);
3509  void EmitOMPUnrollDirective(const OMPUnrollDirective &S);
3510  void EmitOMPForDirective(const OMPForDirective &S);
3511  void EmitOMPForSimdDirective(const OMPForSimdDirective &S);
3512  void EmitOMPSectionsDirective(const OMPSectionsDirective &S);
3513  void EmitOMPSectionDirective(const OMPSectionDirective &S);
3514  void EmitOMPSingleDirective(const OMPSingleDirective &S);
3515  void EmitOMPMasterDirective(const OMPMasterDirective &S);
3516  void EmitOMPMaskedDirective(const OMPMaskedDirective &S);
3517  void EmitOMPCriticalDirective(const OMPCriticalDirective &S);
3518  void EmitOMPParallelForDirective(const OMPParallelForDirective &S);
3519  void EmitOMPParallelForSimdDirective(const OMPParallelForSimdDirective &S);
3520  void EmitOMPParallelSectionsDirective(const OMPParallelSectionsDirective &S);
3521  void EmitOMPParallelMasterDirective(const OMPParallelMasterDirective &S);
3522  void EmitOMPTaskDirective(const OMPTaskDirective &S);
3523  void EmitOMPTaskyieldDirective(const OMPTaskyieldDirective &S);
3524  void EmitOMPErrorDirective(const OMPErrorDirective &S);
3525  void EmitOMPBarrierDirective(const OMPBarrierDirective &S);
3526  void EmitOMPTaskwaitDirective(const OMPTaskwaitDirective &S);
3527  void EmitOMPTaskgroupDirective(const OMPTaskgroupDirective &S);
3528  void EmitOMPFlushDirective(const OMPFlushDirective &S);
3529  void EmitOMPDepobjDirective(const OMPDepobjDirective &S);
3530  void EmitOMPScanDirective(const OMPScanDirective &S);
3531  void EmitOMPOrderedDirective(const OMPOrderedDirective &S);
3532  void EmitOMPAtomicDirective(const OMPAtomicDirective &S);
3533  void EmitOMPTargetDirective(const OMPTargetDirective &S);
3534  void EmitOMPTargetDataDirective(const OMPTargetDataDirective &S);
3535  void EmitOMPTargetEnterDataDirective(const OMPTargetEnterDataDirective &S);
3536  void EmitOMPTargetExitDataDirective(const OMPTargetExitDataDirective &S);
3537  void EmitOMPTargetUpdateDirective(const OMPTargetUpdateDirective &S);
3538  void EmitOMPTargetParallelDirective(const OMPTargetParallelDirective &S);
3539  void
3540  EmitOMPTargetParallelForDirective(const OMPTargetParallelForDirective &S);
3541  void EmitOMPTeamsDirective(const OMPTeamsDirective &S);
3542  void
3543  EmitOMPCancellationPointDirective(const OMPCancellationPointDirective &S);
3544  void EmitOMPCancelDirective(const OMPCancelDirective &S);
3545  void EmitOMPTaskLoopBasedDirective(const OMPLoopDirective &S);
3546  void EmitOMPTaskLoopDirective(const OMPTaskLoopDirective &S);
3547  void EmitOMPTaskLoopSimdDirective(const OMPTaskLoopSimdDirective &S);
3548  void EmitOMPMasterTaskLoopDirective(const OMPMasterTaskLoopDirective &S);
3549  void
3550  EmitOMPMasterTaskLoopSimdDirective(const OMPMasterTaskLoopSimdDirective &S);
3551  void EmitOMPParallelMasterTaskLoopDirective(
3552      const OMPParallelMasterTaskLoopDirective &S);
3553  void EmitOMPParallelMasterTaskLoopSimdDirective(
3554      const OMPParallelMasterTaskLoopSimdDirective &S);
3555  void EmitOMPDistributeDirective(const OMPDistributeDirective &S);
3556  void EmitOMPDistributeParallelForDirective(
3557      const OMPDistributeParallelForDirective &S);
3558  void EmitOMPDistributeParallelForSimdDirective(
3559      const OMPDistributeParallelForSimdDirective &S);
3560  void EmitOMPDistributeSimdDirective(const OMPDistributeSimdDirective &S);
3561  void EmitOMPTargetParallelForSimdDirective(
3562      const OMPTargetParallelForSimdDirective &S);
3563  void EmitOMPTargetSimdDirective(const OMPTargetSimdDirective &S);
3564  void EmitOMPTeamsDistributeDirective(const OMPTeamsDistributeDirective &S);
3565  void
3566  EmitOMPTeamsDistributeSimdDirective(const OMPTeamsDistributeSimdDirective &S);
3567  void EmitOMPTeamsDistributeParallelForSimdDirective(
3568      const OMPTeamsDistributeParallelForSimdDirective &S);
3569  void EmitOMPTeamsDistributeParallelForDirective(
3570      const OMPTeamsDistributeParallelForDirective &S);
3571  void EmitOMPTargetTeamsDirective(const OMPTargetTeamsDirective &S);
3572  void EmitOMPTargetTeamsDistributeDirective(
3573      const OMPTargetTeamsDistributeDirective &S);
3574  void EmitOMPTargetTeamsDistributeParallelForDirective(
3575      const OMPTargetTeamsDistributeParallelForDirective &S);
3576  void EmitOMPTargetTeamsDistributeParallelForSimdDirective(
3577      const OMPTargetTeamsDistributeParallelForSimdDirective &S);
3578  void EmitOMPTargetTeamsDistributeSimdDirective(
3579      const OMPTargetTeamsDistributeSimdDirective &S);
3580  void EmitOMPGenericLoopDirective(const OMPGenericLoopDirective &S);
3581  void EmitOMPInteropDirective(const OMPInteropDirective &S);
3582
3583  /// Emit device code for the target directive.
3584  static void EmitOMPTargetDeviceFunction(CodeGenModule &CGM,
3585                                          StringRef ParentName,
3586                                          const OMPTargetDirective &S);
3587  static void
3588  EmitOMPTargetParallelDeviceFunction(CodeGenModule &CGM, StringRef ParentName,
3589                                      const OMPTargetParallelDirective &S);
3590  /// Emit device code for the target parallel for directive.
3591  static void EmitOMPTargetParallelForDeviceFunction(
3592      CodeGenModule &CGM, StringRef ParentName,
3593      const OMPTargetParallelForDirective &S);
3594  /// Emit device code for the target parallel for simd directive.
3595  static void EmitOMPTargetParallelForSimdDeviceFunction(
3596      CodeGenModule &CGM, StringRef ParentName,
3597      const OMPTargetParallelForSimdDirective &S);
3598  /// Emit device code for the target teams directive.
3599  static void
3600  EmitOMPTargetTeamsDeviceFunction(CodeGenModule &CGM, StringRef ParentName,
3601                                   const OMPTargetTeamsDirective &S);
3602  /// Emit device code for the target teams distribute directive.
3603  static void EmitOMPTargetTeamsDistributeDeviceFunction(
3604      CodeGenModule &CGM, StringRef ParentName,
3605      const OMPTargetTeamsDistributeDirective &S);
3606  /// Emit device code for the target teams distribute simd directive.
3607  static void EmitOMPTargetTeamsDistributeSimdDeviceFunction(
3608      CodeGenModule &CGM, StringRef ParentName,
3609      const OMPTargetTeamsDistributeSimdDirective &S);
3610  /// Emit device code for the target simd directive.
3611  static void EmitOMPTargetSimdDeviceFunction(CodeGenModule &CGM,
3612                                              StringRef ParentName,
3613                                              const OMPTargetSimdDirective &S);
3614  /// Emit device code for the target teams distribute parallel for simd
3615  /// directive.
3616  static void EmitOMPTargetTeamsDistributeParallelForSimdDeviceFunction(
3617      CodeGenModule &CGM, StringRef ParentName,
3618      const OMPTargetTeamsDistributeParallelForSimdDirective &S);
3619
3620  static void EmitOMPTargetTeamsDistributeParallelForDeviceFunction(
3621      CodeGenModule &CGM, StringRef ParentName,
3622      const OMPTargetTeamsDistributeParallelForDirective &S);
3623
3624  /// Emit the Stmt \p S and return its topmost canonical loop, if any.
3625  /// TODO: The \p Depth paramter is not yet implemented and must be 1. In the
3626  /// future it is meant to be the number of loops expected in the loop nests
3627  /// (usually specified by the "collapse" clause) that are collapsed to a
3628  /// single loop by this function.
3629  llvm::CanonicalLoopInfo *EmitOMPCollapsedCanonicalLoopNest(const Stmt *S,
3630                                                             int Depth);
3631
3632  /// Emit an OMPCanonicalLoop using the OpenMPIRBuilder.
3633  void EmitOMPCanonicalLoop(const OMPCanonicalLoop *S);
3634
3635  /// Emit inner loop of the worksharing/simd construct.
3636  ///
3637  /// \param S Directive, for which the inner loop must be emitted.
3638  /// \param RequiresCleanup true, if directive has some associated private
3639  /// variables.
3640  /// \param LoopCond Bollean condition for loop continuation.
3641  /// \param IncExpr Increment expression for loop control variable.
3642  /// \param BodyGen Generator for the inner body of the inner loop.
3643  /// \param PostIncGen Genrator for post-increment code (required for ordered
3644  /// loop directvies).
3645  void EmitOMPInnerLoop(
3646      const OMPExecutableDirective &S, bool RequiresCleanup,
3647      const Expr *LoopCond, const Expr *IncExpr,
3648      const llvm::function_ref<void(CodeGenFunction &)> BodyGen,
3649      const llvm::function_ref<void(CodeGenFunction &)> PostIncGen);
3650
3651  JumpDest getOMPCancelDestination(OpenMPDirectiveKind Kind);
3652  /// Emit initial code for loop counters of loop-based directives.
3653  void EmitOMPPrivateLoopCounters(const OMPLoopDirective &S,
3654                                  OMPPrivateScope &LoopScope);
3655
3656  /// Helper for the OpenMP loop directives.
3657  void EmitOMPLoopBody(const OMPLoopDirective &D, JumpDest LoopExit);
3658
3659  /// Emit code for the worksharing loop-based directive.
3660  /// \return true, if this construct has any lastprivate clause, false -
3661  /// otherwise.
3662  bool EmitOMPWorksharingLoop(const OMPLoopDirective &S, Expr *EUB,
3663                              const CodeGenLoopBoundsTy &CodeGenLoopBounds,
3664                              const CodeGenDispatchBoundsTy &CGDispatchBounds);
3665
3666  /// Emit code for the distribute loop-based directive.
3667  void EmitOMPDistributeLoop(const OMPLoopDirective &S,
3668                             const CodeGenLoopTy &CodeGenLoop, Expr *IncExpr);
3669
3670  /// Helpers for the OpenMP loop directives.
3671  void EmitOMPSimdInit(const OMPLoopDirective &D);
3672  void EmitOMPSimdFinal(
3673      const OMPLoopDirective &D,
3674      const llvm::function_ref<llvm::Value *(CodeGenFunction &)> CondGen);
3675
3676  /// Emits the lvalue for the expression with possibly captured variable.
3677  LValue EmitOMPSharedLValue(const Expr *E);
3678
3679private:
3680  /// Helpers for blocks.
3681  llvm::Value *EmitBlockLiteral(const CGBlockInfo &Info);
3682
3683  /// struct with the values to be passed to the OpenMP loop-related functions
3684  struct OMPLoopArguments {
3685    /// loop lower bound
3686    Address LB = Address::invalid();
3687    /// loop upper bound
3688    Address UB = Address::invalid();
3689    /// loop stride
3690    Address ST = Address::invalid();
3691    /// isLastIteration argument for runtime functions
3692    Address IL = Address::invalid();
3693    /// Chunk value generated by sema
3694    llvm::Value *Chunk = nullptr;
3695    /// EnsureUpperBound
3696    Expr *EUB = nullptr;
3697    /// IncrementExpression
3698    Expr *IncExpr = nullptr;
3699    /// Loop initialization
3700    Expr *Init = nullptr;
3701    /// Loop exit condition
3702    Expr *Cond = nullptr;
3703    /// Update of LB after a whole chunk has been executed
3704    Expr *NextLB = nullptr;
3705    /// Update of UB after a whole chunk has been executed
3706    Expr *NextUB = nullptr;
3707    OMPLoopArguments() = default;
3708    OMPLoopArguments(Address LB, Address UB, Address ST, Address IL,
3709                     llvm::Value *Chunk = nullptr, Expr *EUB = nullptr,
3710                     Expr *IncExpr = nullptr, Expr *Init = nullptr,
3711                     Expr *Cond = nullptr, Expr *NextLB = nullptr,
3712                     Expr *NextUB = nullptr)
3713        : LB(LB), UB(UB), ST(ST), IL(IL), Chunk(Chunk), EUB(EUB),
3714          IncExpr(IncExpr), Init(Init), Cond(Cond), NextLB(NextLB),
3715          NextUB(NextUB) {}
3716  };
3717  void EmitOMPOuterLoop(bool DynamicOrOrdered, bool IsMonotonic,
3718                        const OMPLoopDirective &S, OMPPrivateScope &LoopScope,
3719                        const OMPLoopArguments &LoopArgs,
3720                        const CodeGenLoopTy &CodeGenLoop,
3721                        const CodeGenOrderedTy &CodeGenOrdered);
3722  void EmitOMPForOuterLoop(const OpenMPScheduleTy &ScheduleKind,
3723                           bool IsMonotonic, const OMPLoopDirective &S,
3724                           OMPPrivateScope &LoopScope, bool Ordered,
3725                           const OMPLoopArguments &LoopArgs,
3726                           const CodeGenDispatchBoundsTy &CGDispatchBounds);
3727  void EmitOMPDistributeOuterLoop(OpenMPDistScheduleClauseKind ScheduleKind,
3728                                  const OMPLoopDirective &S,
3729                                  OMPPrivateScope &LoopScope,
3730                                  const OMPLoopArguments &LoopArgs,
3731                                  const CodeGenLoopTy &CodeGenLoopContent);
3732  /// Emit code for sections directive.
3733  void EmitSections(const OMPExecutableDirective &S);
3734
3735public:
3736
3737  //===--------------------------------------------------------------------===//
3738  //                         LValue Expression Emission
3739  //===--------------------------------------------------------------------===//
3740
3741  /// Create a check that a scalar RValue is non-null.
3742  llvm::Value *EmitNonNullRValueCheck(RValue RV, QualType T);
3743
3744  /// GetUndefRValue - Get an appropriate 'undef' rvalue for the given type.
3745  RValue GetUndefRValue(QualType Ty);
3746
3747  /// EmitUnsupportedRValue - Emit a dummy r-value using the type of E
3748  /// and issue an ErrorUnsupported style diagnostic (using the
3749  /// provided Name).
3750  RValue EmitUnsupportedRValue(const Expr *E,
3751                               const char *Name);
3752
3753  /// EmitUnsupportedLValue - Emit a dummy l-value using the type of E and issue
3754  /// an ErrorUnsupported style diagnostic (using the provided Name).
3755  LValue EmitUnsupportedLValue(const Expr *E,
3756                               const char *Name);
3757
3758  /// EmitLValue - Emit code to compute a designator that specifies the location
3759  /// of the expression.
3760  ///
3761  /// This can return one of two things: a simple address or a bitfield
3762  /// reference.  In either case, the LLVM Value* in the LValue structure is
3763  /// guaranteed to be an LLVM pointer type.
3764  ///
3765  /// If this returns a bitfield reference, nothing about the pointee type of
3766  /// the LLVM value is known: For example, it may not be a pointer to an
3767  /// integer.
3768  ///
3769  /// If this returns a normal address, and if the lvalue's C type is fixed
3770  /// size, this method guarantees that the returned pointer type will point to
3771  /// an LLVM type of the same size of the lvalue's type.  If the lvalue has a
3772  /// variable length type, this is not possible.
3773  ///
3774  LValue EmitLValue(const Expr *E);
3775
3776  /// Same as EmitLValue but additionally we generate checking code to
3777  /// guard against undefined behavior.  This is only suitable when we know
3778  /// that the address will be used to access the object.
3779  LValue EmitCheckedLValue(const Expr *E, TypeCheckKind TCK);
3780
3781  RValue convertTempToRValue(Address addr, QualType type,
3782                             SourceLocation Loc);
3783
3784  void EmitAtomicInit(Expr *E, LValue lvalue);
3785
3786  bool LValueIsSuitableForInlineAtomic(LValue Src);
3787
3788  RValue EmitAtomicLoad(LValue LV, SourceLocation SL,
3789                        AggValueSlot Slot = AggValueSlot::ignored());
3790
3791  RValue EmitAtomicLoad(LValue lvalue, SourceLocation loc,
3792                        llvm::AtomicOrdering AO, bool IsVolatile = false,
3793                        AggValueSlot slot = AggValueSlot::ignored());
3794
3795  void EmitAtomicStore(RValue rvalue, LValue lvalue, bool isInit);
3796
3797  void EmitAtomicStore(RValue rvalue, LValue lvalue, llvm::AtomicOrdering AO,
3798                       bool IsVolatile, bool isInit);
3799
3800  std::pair<RValue, llvm::Value *> EmitAtomicCompareExchange(
3801      LValue Obj, RValue Expected, RValue Desired, SourceLocation Loc,
3802      llvm::AtomicOrdering Success =
3803          llvm::AtomicOrdering::SequentiallyConsistent,
3804      llvm::AtomicOrdering Failure =
3805          llvm::AtomicOrdering::SequentiallyConsistent,
3806      bool IsWeak = false, AggValueSlot Slot = AggValueSlot::ignored());
3807
3808  void EmitAtomicUpdate(LValue LVal, llvm::AtomicOrdering AO,
3809                        const llvm::function_ref<RValue(RValue)> &UpdateOp,
3810                        bool IsVolatile);
3811
3812  /// EmitToMemory - Change a scalar value from its value
3813  /// representation to its in-memory representation.
3814  llvm::Value *EmitToMemory(llvm::Value *Value, QualType Ty);
3815
3816  /// EmitFromMemory - Change a scalar value from its memory
3817  /// representation to its value representation.
3818  llvm::Value *EmitFromMemory(llvm::Value *Value, QualType Ty);
3819
3820  /// Check if the scalar \p Value is within the valid range for the given
3821  /// type \p Ty.
3822  ///
3823  /// Returns true if a check is needed (even if the range is unknown).
3824  bool EmitScalarRangeCheck(llvm::Value *Value, QualType Ty,
3825                            SourceLocation Loc);
3826
3827  /// EmitLoadOfScalar - Load a scalar value from an address, taking
3828  /// care to appropriately convert from the memory representation to
3829  /// the LLVM value representation.
3830  llvm::Value *EmitLoadOfScalar(Address Addr, bool Volatile, QualType Ty,
3831                                SourceLocation Loc,
3832                                AlignmentSource Source = AlignmentSource::Type,
3833                                bool isNontemporal = false) {
3834    return EmitLoadOfScalar(Addr, Volatile, Ty, Loc, LValueBaseInfo(Source),
3835                            CGM.getTBAAAccessInfo(Ty), isNontemporal);
3836  }
3837
3838  llvm::Value *EmitLoadOfScalar(Address Addr, bool Volatile, QualType Ty,
3839                                SourceLocation Loc, LValueBaseInfo BaseInfo,
3840                                TBAAAccessInfo TBAAInfo,
3841                                bool isNontemporal = false);
3842
3843  /// EmitLoadOfScalar - Load a scalar value from an address, taking
3844  /// care to appropriately convert from the memory representation to
3845  /// the LLVM value representation.  The l-value must be a simple
3846  /// l-value.
3847  llvm::Value *EmitLoadOfScalar(LValue lvalue, SourceLocation Loc);
3848
3849  /// EmitStoreOfScalar - Store a scalar value to an address, taking
3850  /// care to appropriately convert from the memory representation to
3851  /// the LLVM value representation.
3852  void EmitStoreOfScalar(llvm::Value *Value, Address Addr,
3853                         bool Volatile, QualType Ty,
3854                         AlignmentSource Source = AlignmentSource::Type,
3855                         bool isInit = false, bool isNontemporal = false) {
3856    EmitStoreOfScalar(Value, Addr, Volatile, Ty, LValueBaseInfo(Source),
3857                      CGM.getTBAAAccessInfo(Ty), isInit, isNontemporal);
3858  }
3859
3860  void EmitStoreOfScalar(llvm::Value *Value, Address Addr,
3861                         bool Volatile, QualType Ty,
3862                         LValueBaseInfo BaseInfo, TBAAAccessInfo TBAAInfo,
3863                         bool isInit = false, bool isNontemporal = false);
3864
3865  /// EmitStoreOfScalar - Store a scalar value to an address, taking
3866  /// care to appropriately convert from the memory representation to
3867  /// the LLVM value representation.  The l-value must be a simple
3868  /// l-value.  The isInit flag indicates whether this is an initialization.
3869  /// If so, atomic qualifiers are ignored and the store is always non-atomic.
3870  void EmitStoreOfScalar(llvm::Value *value, LValue lvalue, bool isInit=false);
3871
3872  /// EmitLoadOfLValue - Given an expression that represents a value lvalue,
3873  /// this method emits the address of the lvalue, then loads the result as an
3874  /// rvalue, returning the rvalue.
3875  RValue EmitLoadOfLValue(LValue V, SourceLocation Loc);
3876  RValue EmitLoadOfExtVectorElementLValue(LValue V);
3877  RValue EmitLoadOfBitfieldLValue(LValue LV, SourceLocation Loc);
3878  RValue EmitLoadOfGlobalRegLValue(LValue LV);
3879
3880  /// EmitStoreThroughLValue - Store the specified rvalue into the specified
3881  /// lvalue, where both are guaranteed to the have the same type, and that type
3882  /// is 'Ty'.
3883  void EmitStoreThroughLValue(RValue Src, LValue Dst, bool isInit = false);
3884  void EmitStoreThroughExtVectorComponentLValue(RValue Src, LValue Dst);
3885  void EmitStoreThroughGlobalRegLValue(RValue Src, LValue Dst);
3886
3887  /// EmitStoreThroughBitfieldLValue - Store Src into Dst with same constraints
3888  /// as EmitStoreThroughLValue.
3889  ///
3890  /// \param Result [out] - If non-null, this will be set to a Value* for the
3891  /// bit-field contents after the store, appropriate for use as the result of
3892  /// an assignment to the bit-field.
3893  void EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst,
3894                                      llvm::Value **Result=nullptr);
3895
3896  /// Emit an l-value for an assignment (simple or compound) of complex type.
3897  LValue EmitComplexAssignmentLValue(const BinaryOperator *E);
3898  LValue EmitComplexCompoundAssignmentLValue(const CompoundAssignOperator *E);
3899  LValue EmitScalarCompoundAssignWithComplex(const CompoundAssignOperator *E,
3900                                             llvm::Value *&Result);
3901
3902  // Note: only available for agg return types
3903  LValue EmitBinaryOperatorLValue(const BinaryOperator *E);
3904  LValue EmitCompoundAssignmentLValue(const CompoundAssignOperator *E);
3905  // Note: only available for agg return types
3906  LValue EmitCallExprLValue(const CallExpr *E);
3907  // Note: only available for agg return types
3908  LValue EmitVAArgExprLValue(const VAArgExpr *E);
3909  LValue EmitDeclRefLValue(const DeclRefExpr *E);
3910  LValue EmitStringLiteralLValue(const StringLiteral *E);
3911  LValue EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E);
3912  LValue EmitPredefinedLValue(const PredefinedExpr *E);
3913  LValue EmitUnaryOpLValue(const UnaryOperator *E);
3914  LValue EmitArraySubscriptExpr(const ArraySubscriptExpr *E,
3915                                bool Accessed = false);
3916  LValue EmitMatrixSubscriptExpr(const MatrixSubscriptExpr *E);
3917  LValue EmitOMPArraySectionExpr(const OMPArraySectionExpr *E,
3918                                 bool IsLowerBound = true);
3919  LValue EmitExtVectorElementExpr(const ExtVectorElementExpr *E);
3920  LValue EmitMemberExpr(const MemberExpr *E);
3921  LValue EmitObjCIsaExpr(const ObjCIsaExpr *E);
3922  LValue EmitCompoundLiteralLValue(const CompoundLiteralExpr *E);
3923  LValue EmitInitListLValue(const InitListExpr *E);
3924  void EmitIgnoredConditionalOperator(const AbstractConditionalOperator *E);
3925  LValue EmitConditionalOperatorLValue(const AbstractConditionalOperator *E);
3926  LValue EmitCastLValue(const CastExpr *E);
3927  LValue EmitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E);
3928  LValue EmitOpaqueValueLValue(const OpaqueValueExpr *e);
3929
3930  Address EmitExtVectorElementLValue(LValue V);
3931
3932  RValue EmitRValueForField(LValue LV, const FieldDecl *FD, SourceLocation Loc);
3933
3934  Address EmitArrayToPointerDecay(const Expr *Array,
3935                                  LValueBaseInfo *BaseInfo = nullptr,
3936                                  TBAAAccessInfo *TBAAInfo = nullptr);
3937
3938  class ConstantEmission {
3939    llvm::PointerIntPair<llvm::Constant*, 1, bool> ValueAndIsReference;
3940    ConstantEmission(llvm::Constant *C, bool isReference)
3941      : ValueAndIsReference(C, isReference) {}
3942  public:
3943    ConstantEmission() {}
3944    static ConstantEmission forReference(llvm::Constant *C) {
3945      return ConstantEmission(C, true);
3946    }
3947    static ConstantEmission forValue(llvm::Constant *C) {
3948      return ConstantEmission(C, false);
3949    }
3950
3951    explicit operator bool() const {
3952      return ValueAndIsReference.getOpaqueValue() != nullptr;
3953    }
3954
3955    bool isReference() const { return ValueAndIsReference.getInt(); }
3956    LValue getReferenceLValue(CodeGenFunction &CGF, Expr *refExpr) const {
3957      assert(isReference());
3958      return CGF.MakeNaturalAlignAddrLValue(ValueAndIsReference.getPointer(),
3959                                            refExpr->getType());
3960    }
3961
3962    llvm::Constant *getValue() const {
3963      assert(!isReference());
3964      return ValueAndIsReference.getPointer();
3965    }
3966  };
3967
3968  ConstantEmission tryEmitAsConstant(DeclRefExpr *refExpr);
3969  ConstantEmission tryEmitAsConstant(const MemberExpr *ME);
3970  llvm::Value *emitScalarConstant(const ConstantEmission &Constant, Expr *E);
3971
3972  RValue EmitPseudoObjectRValue(const PseudoObjectExpr *e,
3973                                AggValueSlot slot = AggValueSlot::ignored());
3974  LValue EmitPseudoObjectLValue(const PseudoObjectExpr *e);
3975
3976  llvm::Value *EmitIvarOffset(const ObjCInterfaceDecl *Interface,
3977                              const ObjCIvarDecl *Ivar);
3978  llvm::Value *EmitIvarOffsetAsPointerDiff(const ObjCInterfaceDecl *Interface,
3979                                           const ObjCIvarDecl *Ivar);
3980  LValue EmitLValueForField(LValue Base, const FieldDecl* Field);
3981  LValue EmitLValueForLambdaField(const FieldDecl *Field);
3982
3983  /// EmitLValueForFieldInitialization - Like EmitLValueForField, except that
3984  /// if the Field is a reference, this will return the address of the reference
3985  /// and not the address of the value stored in the reference.
3986  LValue EmitLValueForFieldInitialization(LValue Base,
3987                                          const FieldDecl* Field);
3988
3989  LValue EmitLValueForIvar(QualType ObjectTy,
3990                           llvm::Value* Base, const ObjCIvarDecl *Ivar,
3991                           unsigned CVRQualifiers);
3992
3993  LValue EmitCXXConstructLValue(const CXXConstructExpr *E);
3994  LValue EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E);
3995  LValue EmitCXXTypeidLValue(const CXXTypeidExpr *E);
3996  LValue EmitCXXUuidofLValue(const CXXUuidofExpr *E);
3997
3998  LValue EmitObjCMessageExprLValue(const ObjCMessageExpr *E);
3999  LValue EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E);
4000  LValue EmitStmtExprLValue(const StmtExpr *E);
4001  LValue EmitPointerToDataMemberBinaryExpr(const BinaryOperator *E);
4002  LValue EmitObjCSelectorLValue(const ObjCSelectorExpr *E);
4003  void   EmitDeclRefExprDbgValue(const DeclRefExpr *E, const APValue &Init);
4004
4005  //===--------------------------------------------------------------------===//
4006  //                         Scalar Expression Emission
4007  //===--------------------------------------------------------------------===//
4008
4009  /// EmitCall - Generate a call of the given function, expecting the given
4010  /// result type, and using the given argument list which specifies both the
4011  /// LLVM arguments and the types they were derived from.
4012  RValue EmitCall(const CGFunctionInfo &CallInfo, const CGCallee &Callee,
4013                  ReturnValueSlot ReturnValue, const CallArgList &Args,
4014                  llvm::CallBase **callOrInvoke, bool IsMustTail,
4015                  SourceLocation Loc);
4016  RValue EmitCall(const CGFunctionInfo &CallInfo, const CGCallee &Callee,
4017                  ReturnValueSlot ReturnValue, const CallArgList &Args,
4018                  llvm::CallBase **callOrInvoke = nullptr,
4019                  bool IsMustTail = false) {
4020    return EmitCall(CallInfo, Callee, ReturnValue, Args, callOrInvoke,
4021                    IsMustTail, SourceLocation());
4022  }
4023  RValue EmitCall(QualType FnType, const CGCallee &Callee, const CallExpr *E,
4024                  ReturnValueSlot ReturnValue, llvm::Value *Chain = nullptr);
4025  RValue EmitCallExpr(const CallExpr *E,
4026                      ReturnValueSlot ReturnValue = ReturnValueSlot());
4027  RValue EmitSimpleCallExpr(const CallExpr *E, ReturnValueSlot ReturnValue);
4028  CGCallee EmitCallee(const Expr *E);
4029
4030  void checkTargetFeatures(const CallExpr *E, const FunctionDecl *TargetDecl);
4031  void checkTargetFeatures(SourceLocation Loc, const FunctionDecl *TargetDecl);
4032
4033  llvm::CallInst *EmitRuntimeCall(llvm::FunctionCallee callee,
4034                                  const Twine &name = "");
4035  llvm::CallInst *EmitRuntimeCall(llvm::FunctionCallee callee,
4036                                  ArrayRef<llvm::Value *> args,
4037                                  const Twine &name = "");
4038  llvm::CallInst *EmitNounwindRuntimeCall(llvm::FunctionCallee callee,
4039                                          const Twine &name = "");
4040  llvm::CallInst *EmitNounwindRuntimeCall(llvm::FunctionCallee callee,
4041                                          ArrayRef<llvm::Value *> args,
4042                                          const Twine &name = "");
4043
4044  SmallVector<llvm::OperandBundleDef, 1>
4045  getBundlesForFunclet(llvm::Value *Callee);
4046
4047  llvm::CallBase *EmitCallOrInvoke(llvm::FunctionCallee Callee,
4048                                   ArrayRef<llvm::Value *> Args,
4049                                   const Twine &Name = "");
4050  llvm::CallBase *EmitRuntimeCallOrInvoke(llvm::FunctionCallee callee,
4051                                          ArrayRef<llvm::Value *> args,
4052                                          const Twine &name = "");
4053  llvm::CallBase *EmitRuntimeCallOrInvoke(llvm::FunctionCallee callee,
4054                                          const Twine &name = "");
4055  void EmitNoreturnRuntimeCallOrInvoke(llvm::FunctionCallee callee,
4056                                       ArrayRef<llvm::Value *> args);
4057
4058  CGCallee BuildAppleKextVirtualCall(const CXXMethodDecl *MD,
4059                                     NestedNameSpecifier *Qual,
4060                                     llvm::Type *Ty);
4061
4062  CGCallee BuildAppleKextVirtualDestructorCall(const CXXDestructorDecl *DD,
4063                                               CXXDtorType Type,
4064                                               const CXXRecordDecl *RD);
4065
4066  // Return the copy constructor name with the prefix "__copy_constructor_"
4067  // removed.
4068  static std::string getNonTrivialCopyConstructorStr(QualType QT,
4069                                                     CharUnits Alignment,
4070                                                     bool IsVolatile,
4071                                                     ASTContext &Ctx);
4072
4073  // Return the destructor name with the prefix "__destructor_" removed.
4074  static std::string getNonTrivialDestructorStr(QualType QT,
4075                                                CharUnits Alignment,
4076                                                bool IsVolatile,
4077                                                ASTContext &Ctx);
4078
4079  // These functions emit calls to the special functions of non-trivial C
4080  // structs.
4081  void defaultInitNonTrivialCStructVar(LValue Dst);
4082  void callCStructDefaultConstructor(LValue Dst);
4083  void callCStructDestructor(LValue Dst);
4084  void callCStructCopyConstructor(LValue Dst, LValue Src);
4085  void callCStructMoveConstructor(LValue Dst, LValue Src);
4086  void callCStructCopyAssignmentOperator(LValue Dst, LValue Src);
4087  void callCStructMoveAssignmentOperator(LValue Dst, LValue Src);
4088
4089  RValue
4090  EmitCXXMemberOrOperatorCall(const CXXMethodDecl *Method,
4091                              const CGCallee &Callee,
4092                              ReturnValueSlot ReturnValue, llvm::Value *This,
4093                              llvm::Value *ImplicitParam,
4094                              QualType ImplicitParamTy, const CallExpr *E,
4095                              CallArgList *RtlArgs);
4096  RValue EmitCXXDestructorCall(GlobalDecl Dtor, const CGCallee &Callee,
4097                               llvm::Value *This, QualType ThisTy,
4098                               llvm::Value *ImplicitParam,
4099                               QualType ImplicitParamTy, const CallExpr *E);
4100  RValue EmitCXXMemberCallExpr(const CXXMemberCallExpr *E,
4101                               ReturnValueSlot ReturnValue);
4102  RValue EmitCXXMemberOrOperatorMemberCallExpr(const CallExpr *CE,
4103                                               const CXXMethodDecl *MD,
4104                                               ReturnValueSlot ReturnValue,
4105                                               bool HasQualifier,
4106                                               NestedNameSpecifier *Qualifier,
4107                                               bool IsArrow, const Expr *Base);
4108  // Compute the object pointer.
4109  Address EmitCXXMemberDataPointerAddress(const Expr *E, Address base,
4110                                          llvm::Value *memberPtr,
4111                                          const MemberPointerType *memberPtrType,
4112                                          LValueBaseInfo *BaseInfo = nullptr,
4113                                          TBAAAccessInfo *TBAAInfo = nullptr);
4114  RValue EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E,
4115                                      ReturnValueSlot ReturnValue);
4116
4117  RValue EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E,
4118                                       const CXXMethodDecl *MD,
4119                                       ReturnValueSlot ReturnValue);
4120  RValue EmitCXXPseudoDestructorExpr(const CXXPseudoDestructorExpr *E);
4121
4122  RValue EmitCUDAKernelCallExpr(const CUDAKernelCallExpr *E,
4123                                ReturnValueSlot ReturnValue);
4124
4125  RValue EmitNVPTXDevicePrintfCallExpr(const CallExpr *E);
4126  RValue EmitAMDGPUDevicePrintfCallExpr(const CallExpr *E);
4127  RValue EmitOpenMPDevicePrintfCallExpr(const CallExpr *E);
4128
4129  RValue EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
4130                         const CallExpr *E, ReturnValueSlot ReturnValue);
4131
4132  RValue emitRotate(const CallExpr *E, bool IsRotateRight);
4133
4134  /// Emit IR for __builtin_os_log_format.
4135  RValue emitBuiltinOSLogFormat(const CallExpr &E);
4136
4137  /// Emit IR for __builtin_is_aligned.
4138  RValue EmitBuiltinIsAligned(const CallExpr *E);
4139  /// Emit IR for __builtin_align_up/__builtin_align_down.
4140  RValue EmitBuiltinAlignTo(const CallExpr *E, bool AlignUp);
4141
4142  llvm::Function *generateBuiltinOSLogHelperFunction(
4143      const analyze_os_log::OSLogBufferLayout &Layout,
4144      CharUnits BufferAlignment);
4145
4146  RValue EmitBlockCallExpr(const CallExpr *E, ReturnValueSlot ReturnValue);
4147
4148  /// EmitTargetBuiltinExpr - Emit the given builtin call. Returns 0 if the call
4149  /// is unhandled by the current target.
4150  llvm::Value *EmitTargetBuiltinExpr(unsigned BuiltinID, const CallExpr *E,
4151                                     ReturnValueSlot ReturnValue);
4152
4153  llvm::Value *EmitAArch64CompareBuiltinExpr(llvm::Value *Op, llvm::Type *Ty,
4154                                             const llvm::CmpInst::Predicate Fp,
4155                                             const llvm::CmpInst::Predicate Ip,
4156                                             const llvm::Twine &Name = "");
4157  llvm::Value *EmitARMBuiltinExpr(unsigned BuiltinID, const CallExpr *E,
4158                                  ReturnValueSlot ReturnValue,
4159                                  llvm::Triple::ArchType Arch);
4160  llvm::Value *EmitARMMVEBuiltinExpr(unsigned BuiltinID, const CallExpr *E,
4161                                     ReturnValueSlot ReturnValue,
4162                                     llvm::Triple::ArchType Arch);
4163  llvm::Value *EmitARMCDEBuiltinExpr(unsigned BuiltinID, const CallExpr *E,
4164                                     ReturnValueSlot ReturnValue,
4165                                     llvm::Triple::ArchType Arch);
4166  llvm::Value *EmitCMSEClearRecord(llvm::Value *V, llvm::IntegerType *ITy,
4167                                   QualType RTy);
4168  llvm::Value *EmitCMSEClearRecord(llvm::Value *V, llvm::ArrayType *ATy,
4169                                   QualType RTy);
4170
4171  llvm::Value *EmitCommonNeonBuiltinExpr(unsigned BuiltinID,
4172                                         unsigned LLVMIntrinsic,
4173                                         unsigned AltLLVMIntrinsic,
4174                                         const char *NameHint,
4175                                         unsigned Modifier,
4176                                         const CallExpr *E,
4177                                         SmallVectorImpl<llvm::Value *> &Ops,
4178                                         Address PtrOp0, Address PtrOp1,
4179                                         llvm::Triple::ArchType Arch);
4180
4181  llvm::Function *LookupNeonLLVMIntrinsic(unsigned IntrinsicID,
4182                                          unsigned Modifier, llvm::Type *ArgTy,
4183                                          const CallExpr *E);
4184  llvm::Value *EmitNeonCall(llvm::Function *F,
4185                            SmallVectorImpl<llvm::Value*> &O,
4186                            const char *name,
4187                            unsigned shift = 0, bool rightshift = false);
4188  llvm::Value *EmitNeonSplat(llvm::Value *V, llvm::Constant *Idx,
4189                             const llvm::ElementCount &Count);
4190  llvm::Value *EmitNeonSplat(llvm::Value *V, llvm::Constant *Idx);
4191  llvm::Value *EmitNeonShiftVector(llvm::Value *V, llvm::Type *Ty,
4192                                   bool negateForRightShift);
4193  llvm::Value *EmitNeonRShiftImm(llvm::Value *Vec, llvm::Value *Amt,
4194                                 llvm::Type *Ty, bool usgn, const char *name);
4195  llvm::Value *vectorWrapScalar16(llvm::Value *Op);
4196  /// SVEBuiltinMemEltTy - Returns the memory element type for this memory
4197  /// access builtin.  Only required if it can't be inferred from the base
4198  /// pointer operand.
4199  llvm::Type *SVEBuiltinMemEltTy(const SVETypeFlags &TypeFlags);
4200
4201  SmallVector<llvm::Type *, 2>
4202  getSVEOverloadTypes(const SVETypeFlags &TypeFlags, llvm::Type *ReturnType,
4203                      ArrayRef<llvm::Value *> Ops);
4204  llvm::Type *getEltType(const SVETypeFlags &TypeFlags);
4205  llvm::ScalableVectorType *getSVEType(const SVETypeFlags &TypeFlags);
4206  llvm::ScalableVectorType *getSVEPredType(const SVETypeFlags &TypeFlags);
4207  llvm::Value *EmitSVETupleSetOrGet(const SVETypeFlags &TypeFlags,
4208                                    llvm::Type *ReturnType,
4209                                    ArrayRef<llvm::Value *> Ops);
4210  llvm::Value *EmitSVETupleCreate(const SVETypeFlags &TypeFlags,
4211                                  llvm::Type *ReturnType,
4212                                  ArrayRef<llvm::Value *> Ops);
4213  llvm::Value *EmitSVEAllTruePred(const SVETypeFlags &TypeFlags);
4214  llvm::Value *EmitSVEDupX(llvm::Value *Scalar);
4215  llvm::Value *EmitSVEDupX(llvm::Value *Scalar, llvm::Type *Ty);
4216  llvm::Value *EmitSVEReinterpret(llvm::Value *Val, llvm::Type *Ty);
4217  llvm::Value *EmitSVEPMull(const SVETypeFlags &TypeFlags,
4218                            llvm::SmallVectorImpl<llvm::Value *> &Ops,
4219                            unsigned BuiltinID);
4220  llvm::Value *EmitSVEMovl(const SVETypeFlags &TypeFlags,
4221                           llvm::ArrayRef<llvm::Value *> Ops,
4222                           unsigned BuiltinID);
4223  llvm::Value *EmitSVEPredicateCast(llvm::Value *Pred,
4224                                    llvm::ScalableVectorType *VTy);
4225  llvm::Value *EmitSVEGatherLoad(const SVETypeFlags &TypeFlags,
4226                                 llvm::SmallVectorImpl<llvm::Value *> &Ops,
4227                                 unsigned IntID);
4228  llvm::Value *EmitSVEScatterStore(const SVETypeFlags &TypeFlags,
4229                                   llvm::SmallVectorImpl<llvm::Value *> &Ops,
4230                                   unsigned IntID);
4231  llvm::Value *EmitSVEMaskedLoad(const CallExpr *, llvm::Type *ReturnTy,
4232                                 SmallVectorImpl<llvm::Value *> &Ops,
4233                                 unsigned BuiltinID, bool IsZExtReturn);
4234  llvm::Value *EmitSVEMaskedStore(const CallExpr *,
4235                                  SmallVectorImpl<llvm::Value *> &Ops,
4236                                  unsigned BuiltinID);
4237  llvm::Value *EmitSVEPrefetchLoad(const SVETypeFlags &TypeFlags,
4238                                   SmallVectorImpl<llvm::Value *> &Ops,
4239                                   unsigned BuiltinID);
4240  llvm::Value *EmitSVEGatherPrefetch(const SVETypeFlags &TypeFlags,
4241                                     SmallVectorImpl<llvm::Value *> &Ops,
4242                                     unsigned IntID);
4243  llvm::Value *EmitSVEStructLoad(const SVETypeFlags &TypeFlags,
4244                                 SmallVectorImpl<llvm::Value *> &Ops,
4245                                 unsigned IntID);
4246  llvm::Value *EmitSVEStructStore(const SVETypeFlags &TypeFlags,
4247                                  SmallVectorImpl<llvm::Value *> &Ops,
4248                                  unsigned IntID);
4249  llvm::Value *EmitAArch64SVEBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
4250
4251  llvm::Value *EmitAArch64BuiltinExpr(unsigned BuiltinID, const CallExpr *E,
4252                                      llvm::Triple::ArchType Arch);
4253  llvm::Value *EmitBPFBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
4254
4255  llvm::Value *BuildVector(ArrayRef<llvm::Value*> Ops);
4256  llvm::Value *EmitX86BuiltinExpr(unsigned BuiltinID, const CallExpr *E);
4257  llvm::Value *EmitPPCBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
4258  llvm::Value *EmitAMDGPUBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
4259  llvm::Value *EmitSystemZBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
4260  llvm::Value *EmitNVPTXBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
4261  llvm::Value *EmitWebAssemblyBuiltinExpr(unsigned BuiltinID,
4262                                          const CallExpr *E);
4263  llvm::Value *EmitHexagonBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
4264  llvm::Value *EmitRISCVBuiltinExpr(unsigned BuiltinID, const CallExpr *E,
4265                                    ReturnValueSlot ReturnValue);
4266  llvm::Value *EmitLoongArchBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
4267  void ProcessOrderScopeAMDGCN(llvm::Value *Order, llvm::Value *Scope,
4268                               llvm::AtomicOrdering &AO,
4269                               llvm::SyncScope::ID &SSID);
4270
4271  enum class MSVCIntrin;
4272  llvm::Value *EmitMSVCBuiltinExpr(MSVCIntrin BuiltinID, const CallExpr *E);
4273
4274  llvm::Value *EmitBuiltinAvailable(const VersionTuple &Version);
4275
4276  llvm::Value *EmitObjCProtocolExpr(const ObjCProtocolExpr *E);
4277  llvm::Value *EmitObjCStringLiteral(const ObjCStringLiteral *E);
4278  llvm::Value *EmitObjCBoxedExpr(const ObjCBoxedExpr *E);
4279  llvm::Value *EmitObjCArrayLiteral(const ObjCArrayLiteral *E);
4280  llvm::Value *EmitObjCDictionaryLiteral(const ObjCDictionaryLiteral *E);
4281  llvm::Value *EmitObjCCollectionLiteral(const Expr *E,
4282                                const ObjCMethodDecl *MethodWithObjects);
4283  llvm::Value *EmitObjCSelectorExpr(const ObjCSelectorExpr *E);
4284  RValue EmitObjCMessageExpr(const ObjCMessageExpr *E,
4285                             ReturnValueSlot Return = ReturnValueSlot());
4286
4287  /// Retrieves the default cleanup kind for an ARC cleanup.
4288  /// Except under -fobjc-arc-eh, ARC cleanups are normal-only.
4289  CleanupKind getARCCleanupKind() {
4290    return CGM.getCodeGenOpts().ObjCAutoRefCountExceptions
4291             ? NormalAndEHCleanup : NormalCleanup;
4292  }
4293
4294  // ARC primitives.
4295  void EmitARCInitWeak(Address addr, llvm::Value *value);
4296  void EmitARCDestroyWeak(Address addr);
4297  llvm::Value *EmitARCLoadWeak(Address addr);
4298  llvm::Value *EmitARCLoadWeakRetained(Address addr);
4299  llvm::Value *EmitARCStoreWeak(Address addr, llvm::Value *value, bool ignored);
4300  void emitARCCopyAssignWeak(QualType Ty, Address DstAddr, Address SrcAddr);
4301  void emitARCMoveAssignWeak(QualType Ty, Address DstAddr, Address SrcAddr);
4302  void EmitARCCopyWeak(Address dst, Address src);
4303  void EmitARCMoveWeak(Address dst, Address src);
4304  llvm::Value *EmitARCRetainAutorelease(QualType type, llvm::Value *value);
4305  llvm::Value *EmitARCRetainAutoreleaseNonBlock(llvm::Value *value);
4306  llvm::Value *EmitARCStoreStrong(LValue lvalue, llvm::Value *value,
4307                                  bool resultIgnored);
4308  llvm::Value *EmitARCStoreStrongCall(Address addr, llvm::Value *value,
4309                                      bool resultIgnored);
4310  llvm::Value *EmitARCRetain(QualType type, llvm::Value *value);
4311  llvm::Value *EmitARCRetainNonBlock(llvm::Value *value);
4312  llvm::Value *EmitARCRetainBlock(llvm::Value *value, bool mandatory);
4313  void EmitARCDestroyStrong(Address addr, ARCPreciseLifetime_t precise);
4314  void EmitARCRelease(llvm::Value *value, ARCPreciseLifetime_t precise);
4315  llvm::Value *EmitARCAutorelease(llvm::Value *value);
4316  llvm::Value *EmitARCAutoreleaseReturnValue(llvm::Value *value);
4317  llvm::Value *EmitARCRetainAutoreleaseReturnValue(llvm::Value *value);
4318  llvm::Value *EmitARCRetainAutoreleasedReturnValue(llvm::Value *value);
4319  llvm::Value *EmitARCUnsafeClaimAutoreleasedReturnValue(llvm::Value *value);
4320
4321  llvm::Value *EmitObjCAutorelease(llvm::Value *value, llvm::Type *returnType);
4322  llvm::Value *EmitObjCRetainNonBlock(llvm::Value *value,
4323                                      llvm::Type *returnType);
4324  void EmitObjCRelease(llvm::Value *value, ARCPreciseLifetime_t precise);
4325
4326  std::pair<LValue,llvm::Value*>
4327  EmitARCStoreAutoreleasing(const BinaryOperator *e);
4328  std::pair<LValue,llvm::Value*>
4329  EmitARCStoreStrong(const BinaryOperator *e, bool ignored);
4330  std::pair<LValue,llvm::Value*>
4331  EmitARCStoreUnsafeUnretained(const BinaryOperator *e, bool ignored);
4332
4333  llvm::Value *EmitObjCAlloc(llvm::Value *value,
4334                             llvm::Type *returnType);
4335  llvm::Value *EmitObjCAllocWithZone(llvm::Value *value,
4336                                     llvm::Type *returnType);
4337  llvm::Value *EmitObjCAllocInit(llvm::Value *value, llvm::Type *resultType);
4338
4339  llvm::Value *EmitObjCThrowOperand(const Expr *expr);
4340  llvm::Value *EmitObjCConsumeObject(QualType T, llvm::Value *Ptr);
4341  llvm::Value *EmitObjCExtendObjectLifetime(QualType T, llvm::Value *Ptr);
4342
4343  llvm::Value *EmitARCExtendBlockObject(const Expr *expr);
4344  llvm::Value *EmitARCReclaimReturnedObject(const Expr *e,
4345                                            bool allowUnsafeClaim);
4346  llvm::Value *EmitARCRetainScalarExpr(const Expr *expr);
4347  llvm::Value *EmitARCRetainAutoreleaseScalarExpr(const Expr *expr);
4348  llvm::Value *EmitARCUnsafeUnretainedScalarExpr(const Expr *expr);
4349
4350  void EmitARCIntrinsicUse(ArrayRef<llvm::Value*> values);
4351
4352  void EmitARCNoopIntrinsicUse(ArrayRef<llvm::Value *> values);
4353
4354  static Destroyer destroyARCStrongImprecise;
4355  static Destroyer destroyARCStrongPrecise;
4356  static Destroyer destroyARCWeak;
4357  static Destroyer emitARCIntrinsicUse;
4358  static Destroyer destroyNonTrivialCStruct;
4359
4360  void EmitObjCAutoreleasePoolPop(llvm::Value *Ptr);
4361  llvm::Value *EmitObjCAutoreleasePoolPush();
4362  llvm::Value *EmitObjCMRRAutoreleasePoolPush();
4363  void EmitObjCAutoreleasePoolCleanup(llvm::Value *Ptr);
4364  void EmitObjCMRRAutoreleasePoolPop(llvm::Value *Ptr);
4365
4366  /// Emits a reference binding to the passed in expression.
4367  RValue EmitReferenceBindingToExpr(const Expr *E);
4368
4369  //===--------------------------------------------------------------------===//
4370  //                           Expression Emission
4371  //===--------------------------------------------------------------------===//
4372
4373  // Expressions are broken into three classes: scalar, complex, aggregate.
4374
4375  /// EmitScalarExpr - Emit the computation of the specified expression of LLVM
4376  /// scalar type, returning the result.
4377  llvm::Value *EmitScalarExpr(const Expr *E , bool IgnoreResultAssign = false);
4378
4379  /// Emit a conversion from the specified type to the specified destination
4380  /// type, both of which are LLVM scalar types.
4381  llvm::Value *EmitScalarConversion(llvm::Value *Src, QualType SrcTy,
4382                                    QualType DstTy, SourceLocation Loc);
4383
4384  /// Emit a conversion from the specified complex type to the specified
4385  /// destination type, where the destination type is an LLVM scalar type.
4386  llvm::Value *EmitComplexToScalarConversion(ComplexPairTy Src, QualType SrcTy,
4387                                             QualType DstTy,
4388                                             SourceLocation Loc);
4389
4390  /// EmitAggExpr - Emit the computation of the specified expression
4391  /// of aggregate type.  The result is computed into the given slot,
4392  /// which may be null to indicate that the value is not needed.
4393  void EmitAggExpr(const Expr *E, AggValueSlot AS);
4394
4395  /// EmitAggExprToLValue - Emit the computation of the specified expression of
4396  /// aggregate type into a temporary LValue.
4397  LValue EmitAggExprToLValue(const Expr *E);
4398
4399  /// Build all the stores needed to initialize an aggregate at Dest with the
4400  /// value Val.
4401  void EmitAggregateStore(llvm::Value *Val, Address Dest, bool DestIsVolatile);
4402
4403  /// EmitExtendGCLifetime - Given a pointer to an Objective-C object,
4404  /// make sure it survives garbage collection until this point.
4405  void EmitExtendGCLifetime(llvm::Value *object);
4406
4407  /// EmitComplexExpr - Emit the computation of the specified expression of
4408  /// complex type, returning the result.
4409  ComplexPairTy EmitComplexExpr(const Expr *E,
4410                                bool IgnoreReal = false,
4411                                bool IgnoreImag = false);
4412
4413  /// EmitComplexExprIntoLValue - Emit the given expression of complex
4414  /// type and place its result into the specified l-value.
4415  void EmitComplexExprIntoLValue(const Expr *E, LValue dest, bool isInit);
4416
4417  /// EmitStoreOfComplex - Store a complex number into the specified l-value.
4418  void EmitStoreOfComplex(ComplexPairTy V, LValue dest, bool isInit);
4419
4420  /// EmitLoadOfComplex - Load a complex number from the specified l-value.
4421  ComplexPairTy EmitLoadOfComplex(LValue src, SourceLocation loc);
4422
4423  ComplexPairTy EmitPromotedComplexExpr(const Expr *E, QualType PromotionType);
4424  llvm::Value *EmitPromotedScalarExpr(const Expr *E, QualType PromotionType);
4425  ComplexPairTy EmitPromotedValue(ComplexPairTy result, QualType PromotionType);
4426  ComplexPairTy EmitUnPromotedValue(ComplexPairTy result, QualType PromotionType);
4427
4428  Address emitAddrOfRealComponent(Address complex, QualType complexType);
4429  Address emitAddrOfImagComponent(Address complex, QualType complexType);
4430
4431  /// AddInitializerToStaticVarDecl - Add the initializer for 'D' to the
4432  /// global variable that has already been created for it.  If the initializer
4433  /// has a different type than GV does, this may free GV and return a different
4434  /// one.  Otherwise it just returns GV.
4435  llvm::GlobalVariable *
4436  AddInitializerToStaticVarDecl(const VarDecl &D,
4437                                llvm::GlobalVariable *GV);
4438
4439  // Emit an @llvm.invariant.start call for the given memory region.
4440  void EmitInvariantStart(llvm::Constant *Addr, CharUnits Size);
4441
4442  /// EmitCXXGlobalVarDeclInit - Create the initializer for a C++
4443  /// variable with global storage.
4444  void EmitCXXGlobalVarDeclInit(const VarDecl &D, llvm::GlobalVariable *GV,
4445                                bool PerformInit);
4446
4447  llvm::Function *createAtExitStub(const VarDecl &VD, llvm::FunctionCallee Dtor,
4448                                   llvm::Constant *Addr);
4449
4450  llvm::Function *createTLSAtExitStub(const VarDecl &VD,
4451                                      llvm::FunctionCallee Dtor,
4452                                      llvm::Constant *Addr,
4453                                      llvm::FunctionCallee &AtExit);
4454
4455  /// Call atexit() with a function that passes the given argument to
4456  /// the given function.
4457  void registerGlobalDtorWithAtExit(const VarDecl &D, llvm::FunctionCallee fn,
4458                                    llvm::Constant *addr);
4459
4460  /// Call atexit() with function dtorStub.
4461  void registerGlobalDtorWithAtExit(llvm::Constant *dtorStub);
4462
4463  /// Call unatexit() with function dtorStub.
4464  llvm::Value *unregisterGlobalDtorWithUnAtExit(llvm::Constant *dtorStub);
4465
4466  /// Emit code in this function to perform a guarded variable
4467  /// initialization.  Guarded initializations are used when it's not
4468  /// possible to prove that an initialization will be done exactly
4469  /// once, e.g. with a static local variable or a static data member
4470  /// of a class template.
4471  void EmitCXXGuardedInit(const VarDecl &D, llvm::GlobalVariable *DeclPtr,
4472                          bool PerformInit);
4473
4474  enum class GuardKind { VariableGuard, TlsGuard };
4475
4476  /// Emit a branch to select whether or not to perform guarded initialization.
4477  void EmitCXXGuardedInitBranch(llvm::Value *NeedsInit,
4478                                llvm::BasicBlock *InitBlock,
4479                                llvm::BasicBlock *NoInitBlock,
4480                                GuardKind Kind, const VarDecl *D);
4481
4482  /// GenerateCXXGlobalInitFunc - Generates code for initializing global
4483  /// variables.
4484  void
4485  GenerateCXXGlobalInitFunc(llvm::Function *Fn,
4486                            ArrayRef<llvm::Function *> CXXThreadLocals,
4487                            ConstantAddress Guard = ConstantAddress::invalid());
4488
4489  /// GenerateCXXGlobalCleanUpFunc - Generates code for cleaning up global
4490  /// variables.
4491  void GenerateCXXGlobalCleanUpFunc(
4492      llvm::Function *Fn,
4493      ArrayRef<std::tuple<llvm::FunctionType *, llvm::WeakTrackingVH,
4494                          llvm::Constant *>>
4495          DtorsOrStermFinalizers);
4496
4497  void GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn,
4498                                        const VarDecl *D,
4499                                        llvm::GlobalVariable *Addr,
4500                                        bool PerformInit);
4501
4502  void EmitCXXConstructExpr(const CXXConstructExpr *E, AggValueSlot Dest);
4503
4504  void EmitSynthesizedCXXCopyCtor(Address Dest, Address Src, const Expr *Exp);
4505
4506  void EmitCXXThrowExpr(const CXXThrowExpr *E, bool KeepInsertionPoint = true);
4507
4508  RValue EmitAtomicExpr(AtomicExpr *E);
4509
4510  //===--------------------------------------------------------------------===//
4511  //                         Annotations Emission
4512  //===--------------------------------------------------------------------===//
4513
4514  /// Emit an annotation call (intrinsic).
4515  llvm::Value *EmitAnnotationCall(llvm::Function *AnnotationFn,
4516                                  llvm::Value *AnnotatedVal,
4517                                  StringRef AnnotationStr,
4518                                  SourceLocation Location,
4519                                  const AnnotateAttr *Attr);
4520
4521  /// Emit local annotations for the local variable V, declared by D.
4522  void EmitVarAnnotations(const VarDecl *D, llvm::Value *V);
4523
4524  /// Emit field annotations for the given field & value. Returns the
4525  /// annotation result.
4526  Address EmitFieldAnnotations(const FieldDecl *D, Address V);
4527
4528  //===--------------------------------------------------------------------===//
4529  //                             Internal Helpers
4530  //===--------------------------------------------------------------------===//
4531
4532  /// ContainsLabel - Return true if the statement contains a label in it.  If
4533  /// this statement is not executed normally, it not containing a label means
4534  /// that we can just remove the code.
4535  static bool ContainsLabel(const Stmt *S, bool IgnoreCaseStmts = false);
4536
4537  /// containsBreak - Return true if the statement contains a break out of it.
4538  /// If the statement (recursively) contains a switch or loop with a break
4539  /// inside of it, this is fine.
4540  static bool containsBreak(const Stmt *S);
4541
4542  /// Determine if the given statement might introduce a declaration into the
4543  /// current scope, by being a (possibly-labelled) DeclStmt.
4544  static bool mightAddDeclToScope(const Stmt *S);
4545
4546  /// ConstantFoldsToSimpleInteger - If the specified expression does not fold
4547  /// to a constant, or if it does but contains a label, return false.  If it
4548  /// constant folds return true and set the boolean result in Result.
4549  bool ConstantFoldsToSimpleInteger(const Expr *Cond, bool &Result,
4550                                    bool AllowLabels = false);
4551
4552  /// ConstantFoldsToSimpleInteger - If the specified expression does not fold
4553  /// to a constant, or if it does but contains a label, return false.  If it
4554  /// constant folds return true and set the folded value.
4555  bool ConstantFoldsToSimpleInteger(const Expr *Cond, llvm::APSInt &Result,
4556                                    bool AllowLabels = false);
4557
4558  /// isInstrumentedCondition - Determine whether the given condition is an
4559  /// instrumentable condition (i.e. no "&&" or "||").
4560  static bool isInstrumentedCondition(const Expr *C);
4561
4562  /// EmitBranchToCounterBlock - Emit a conditional branch to a new block that
4563  /// increments a profile counter based on the semantics of the given logical
4564  /// operator opcode.  This is used to instrument branch condition coverage
4565  /// for logical operators.
4566  void EmitBranchToCounterBlock(const Expr *Cond, BinaryOperator::Opcode LOp,
4567                                llvm::BasicBlock *TrueBlock,
4568                                llvm::BasicBlock *FalseBlock,
4569                                uint64_t TrueCount = 0,
4570                                Stmt::Likelihood LH = Stmt::LH_None,
4571                                const Expr *CntrIdx = nullptr);
4572
4573  /// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an
4574  /// if statement) to the specified blocks.  Based on the condition, this might
4575  /// try to simplify the codegen of the conditional based on the branch.
4576  /// TrueCount should be the number of times we expect the condition to
4577  /// evaluate to true based on PGO data.
4578  void EmitBranchOnBoolExpr(const Expr *Cond, llvm::BasicBlock *TrueBlock,
4579                            llvm::BasicBlock *FalseBlock, uint64_t TrueCount,
4580                            Stmt::Likelihood LH = Stmt::LH_None);
4581
4582  /// Given an assignment `*LHS = RHS`, emit a test that checks if \p RHS is
4583  /// nonnull, if \p LHS is marked _Nonnull.
4584  void EmitNullabilityCheck(LValue LHS, llvm::Value *RHS, SourceLocation Loc);
4585
4586  /// An enumeration which makes it easier to specify whether or not an
4587  /// operation is a subtraction.
4588  enum { NotSubtraction = false, IsSubtraction = true };
4589
4590  /// Same as IRBuilder::CreateInBoundsGEP, but additionally emits a check to
4591  /// detect undefined behavior when the pointer overflow sanitizer is enabled.
4592  /// \p SignedIndices indicates whether any of the GEP indices are signed.
4593  /// \p IsSubtraction indicates whether the expression used to form the GEP
4594  /// is a subtraction.
4595  llvm::Value *EmitCheckedInBoundsGEP(llvm::Type *ElemTy, llvm::Value *Ptr,
4596                                      ArrayRef<llvm::Value *> IdxList,
4597                                      bool SignedIndices,
4598                                      bool IsSubtraction,
4599                                      SourceLocation Loc,
4600                                      const Twine &Name = "");
4601
4602  /// Specifies which type of sanitizer check to apply when handling a
4603  /// particular builtin.
4604  enum BuiltinCheckKind {
4605    BCK_CTZPassedZero,
4606    BCK_CLZPassedZero,
4607  };
4608
4609  /// Emits an argument for a call to a builtin. If the builtin sanitizer is
4610  /// enabled, a runtime check specified by \p Kind is also emitted.
4611  llvm::Value *EmitCheckedArgForBuiltin(const Expr *E, BuiltinCheckKind Kind);
4612
4613  /// Emit a description of a type in a format suitable for passing to
4614  /// a runtime sanitizer handler.
4615  llvm::Constant *EmitCheckTypeDescriptor(QualType T);
4616
4617  /// Convert a value into a format suitable for passing to a runtime
4618  /// sanitizer handler.
4619  llvm::Value *EmitCheckValue(llvm::Value *V);
4620
4621  /// Emit a description of a source location in a format suitable for
4622  /// passing to a runtime sanitizer handler.
4623  llvm::Constant *EmitCheckSourceLocation(SourceLocation Loc);
4624
4625  void EmitKCFIOperandBundle(const CGCallee &Callee,
4626                             SmallVectorImpl<llvm::OperandBundleDef> &Bundles);
4627
4628  /// Create a basic block that will either trap or call a handler function in
4629  /// the UBSan runtime with the provided arguments, and create a conditional
4630  /// branch to it.
4631  void EmitCheck(ArrayRef<std::pair<llvm::Value *, SanitizerMask>> Checked,
4632                 SanitizerHandler Check, ArrayRef<llvm::Constant *> StaticArgs,
4633                 ArrayRef<llvm::Value *> DynamicArgs);
4634
4635  /// Emit a slow path cross-DSO CFI check which calls __cfi_slowpath
4636  /// if Cond if false.
4637  void EmitCfiSlowPathCheck(SanitizerMask Kind, llvm::Value *Cond,
4638                            llvm::ConstantInt *TypeId, llvm::Value *Ptr,
4639                            ArrayRef<llvm::Constant *> StaticArgs);
4640
4641  /// Emit a reached-unreachable diagnostic if \p Loc is valid and runtime
4642  /// checking is enabled. Otherwise, just emit an unreachable instruction.
4643  void EmitUnreachable(SourceLocation Loc);
4644
4645  /// Create a basic block that will call the trap intrinsic, and emit a
4646  /// conditional branch to it, for the -ftrapv checks.
4647  void EmitTrapCheck(llvm::Value *Checked, SanitizerHandler CheckHandlerID);
4648
4649  /// Emit a call to trap or debugtrap and attach function attribute
4650  /// "trap-func-name" if specified.
4651  llvm::CallInst *EmitTrapCall(llvm::Intrinsic::ID IntrID);
4652
4653  /// Emit a stub for the cross-DSO CFI check function.
4654  void EmitCfiCheckStub();
4655
4656  /// Emit a cross-DSO CFI failure handling function.
4657  void EmitCfiCheckFail();
4658
4659  /// Create a check for a function parameter that may potentially be
4660  /// declared as non-null.
4661  void EmitNonNullArgCheck(RValue RV, QualType ArgType, SourceLocation ArgLoc,
4662                           AbstractCallee AC, unsigned ParmNum);
4663
4664  /// EmitCallArg - Emit a single call argument.
4665  void EmitCallArg(CallArgList &args, const Expr *E, QualType ArgType);
4666
4667  /// EmitDelegateCallArg - We are performing a delegate call; that
4668  /// is, the current function is delegating to another one.  Produce
4669  /// a r-value suitable for passing the given parameter.
4670  void EmitDelegateCallArg(CallArgList &args, const VarDecl *param,
4671                           SourceLocation loc);
4672
4673  /// SetFPAccuracy - Set the minimum required accuracy of the given floating
4674  /// point operation, expressed as the maximum relative error in ulp.
4675  void SetFPAccuracy(llvm::Value *Val, float Accuracy);
4676
4677  /// Set the codegen fast-math flags.
4678  void SetFastMathFlags(FPOptions FPFeatures);
4679
4680  // Truncate or extend a boolean vector to the requested number of elements.
4681  llvm::Value *emitBoolVecConversion(llvm::Value *SrcVec,
4682                                     unsigned NumElementsDst,
4683                                     const llvm::Twine &Name = "");
4684
4685private:
4686  llvm::MDNode *getRangeForLoadFromType(QualType Ty);
4687  void EmitReturnOfRValue(RValue RV, QualType Ty);
4688
4689  void deferPlaceholderReplacement(llvm::Instruction *Old, llvm::Value *New);
4690
4691  llvm::SmallVector<std::pair<llvm::WeakTrackingVH, llvm::Value *>, 4>
4692      DeferredReplacements;
4693
4694  /// Set the address of a local variable.
4695  void setAddrOfLocalVar(const VarDecl *VD, Address Addr) {
4696    assert(!LocalDeclMap.count(VD) && "Decl already exists in LocalDeclMap!");
4697    LocalDeclMap.insert({VD, Addr});
4698  }
4699
4700  /// ExpandTypeFromArgs - Reconstruct a structure of type \arg Ty
4701  /// from function arguments into \arg Dst. See ABIArgInfo::Expand.
4702  ///
4703  /// \param AI - The first function argument of the expansion.
4704  void ExpandTypeFromArgs(QualType Ty, LValue Dst,
4705                          llvm::Function::arg_iterator &AI);
4706
4707  /// ExpandTypeToArgs - Expand an CallArg \arg Arg, with the LLVM type for \arg
4708  /// Ty, into individual arguments on the provided vector \arg IRCallArgs,
4709  /// starting at index \arg IRCallArgPos. See ABIArgInfo::Expand.
4710  void ExpandTypeToArgs(QualType Ty, CallArg Arg, llvm::FunctionType *IRFuncTy,
4711                        SmallVectorImpl<llvm::Value *> &IRCallArgs,
4712                        unsigned &IRCallArgPos);
4713
4714  std::pair<llvm::Value *, llvm::Type *>
4715  EmitAsmInput(const TargetInfo::ConstraintInfo &Info, const Expr *InputExpr,
4716               std::string &ConstraintStr);
4717
4718  std::pair<llvm::Value *, llvm::Type *>
4719  EmitAsmInputLValue(const TargetInfo::ConstraintInfo &Info, LValue InputValue,
4720                     QualType InputType, std::string &ConstraintStr,
4721                     SourceLocation Loc);
4722
4723  /// Attempts to statically evaluate the object size of E. If that
4724  /// fails, emits code to figure the size of E out for us. This is
4725  /// pass_object_size aware.
4726  ///
4727  /// If EmittedExpr is non-null, this will use that instead of re-emitting E.
4728  llvm::Value *evaluateOrEmitBuiltinObjectSize(const Expr *E, unsigned Type,
4729                                               llvm::IntegerType *ResType,
4730                                               llvm::Value *EmittedE,
4731                                               bool IsDynamic);
4732
4733  /// Emits the size of E, as required by __builtin_object_size. This
4734  /// function is aware of pass_object_size parameters, and will act accordingly
4735  /// if E is a parameter with the pass_object_size attribute.
4736  llvm::Value *emitBuiltinObjectSize(const Expr *E, unsigned Type,
4737                                     llvm::IntegerType *ResType,
4738                                     llvm::Value *EmittedE,
4739                                     bool IsDynamic);
4740
4741  void emitZeroOrPatternForAutoVarInit(QualType type, const VarDecl &D,
4742                                       Address Loc);
4743
4744public:
4745  enum class EvaluationOrder {
4746    ///! No language constraints on evaluation order.
4747    Default,
4748    ///! Language semantics require left-to-right evaluation.
4749    ForceLeftToRight,
4750    ///! Language semantics require right-to-left evaluation.
4751    ForceRightToLeft
4752  };
4753
4754  // Wrapper for function prototype sources. Wraps either a FunctionProtoType or
4755  // an ObjCMethodDecl.
4756  struct PrototypeWrapper {
4757    llvm::PointerUnion<const FunctionProtoType *, const ObjCMethodDecl *> P;
4758
4759    PrototypeWrapper(const FunctionProtoType *FT) : P(FT) {}
4760    PrototypeWrapper(const ObjCMethodDecl *MD) : P(MD) {}
4761  };
4762
4763  void EmitCallArgs(CallArgList &Args, PrototypeWrapper Prototype,
4764                    llvm::iterator_range<CallExpr::const_arg_iterator> ArgRange,
4765                    AbstractCallee AC = AbstractCallee(),
4766                    unsigned ParamsToSkip = 0,
4767                    EvaluationOrder Order = EvaluationOrder::Default);
4768
4769  /// EmitPointerWithAlignment - Given an expression with a pointer type,
4770  /// emit the value and compute our best estimate of the alignment of the
4771  /// pointee.
4772  ///
4773  /// \param BaseInfo - If non-null, this will be initialized with
4774  /// information about the source of the alignment and the may-alias
4775  /// attribute.  Note that this function will conservatively fall back on
4776  /// the type when it doesn't recognize the expression and may-alias will
4777  /// be set to false.
4778  ///
4779  /// One reasonable way to use this information is when there's a language
4780  /// guarantee that the pointer must be aligned to some stricter value, and
4781  /// we're simply trying to ensure that sufficiently obvious uses of under-
4782  /// aligned objects don't get miscompiled; for example, a placement new
4783  /// into the address of a local variable.  In such a case, it's quite
4784  /// reasonable to just ignore the returned alignment when it isn't from an
4785  /// explicit source.
4786  Address EmitPointerWithAlignment(const Expr *Addr,
4787                                   LValueBaseInfo *BaseInfo = nullptr,
4788                                   TBAAAccessInfo *TBAAInfo = nullptr);
4789
4790  /// If \p E references a parameter with pass_object_size info or a constant
4791  /// array size modifier, emit the object size divided by the size of \p EltTy.
4792  /// Otherwise return null.
4793  llvm::Value *LoadPassedObjectSize(const Expr *E, QualType EltTy);
4794
4795  void EmitSanitizerStatReport(llvm::SanitizerStatKind SSK);
4796
4797  struct MultiVersionResolverOption {
4798    llvm::Function *Function;
4799    struct Conds {
4800      StringRef Architecture;
4801      llvm::SmallVector<StringRef, 8> Features;
4802
4803      Conds(StringRef Arch, ArrayRef<StringRef> Feats)
4804          : Architecture(Arch), Features(Feats.begin(), Feats.end()) {}
4805    } Conditions;
4806
4807    MultiVersionResolverOption(llvm::Function *F, StringRef Arch,
4808                               ArrayRef<StringRef> Feats)
4809        : Function(F), Conditions(Arch, Feats) {}
4810  };
4811
4812  // Emits the body of a multiversion function's resolver. Assumes that the
4813  // options are already sorted in the proper order, with the 'default' option
4814  // last (if it exists).
4815  void EmitMultiVersionResolver(llvm::Function *Resolver,
4816                                ArrayRef<MultiVersionResolverOption> Options);
4817  void
4818  EmitX86MultiVersionResolver(llvm::Function *Resolver,
4819                              ArrayRef<MultiVersionResolverOption> Options);
4820  void
4821  EmitAArch64MultiVersionResolver(llvm::Function *Resolver,
4822                                  ArrayRef<MultiVersionResolverOption> Options);
4823
4824private:
4825  QualType getVarArgType(const Expr *Arg);
4826
4827  void EmitDeclMetadata();
4828
4829  BlockByrefHelpers *buildByrefHelpers(llvm::StructType &byrefType,
4830                                  const AutoVarEmission &emission);
4831
4832  void AddObjCARCExceptionMetadata(llvm::Instruction *Inst);
4833
4834  llvm::Value *GetValueForARMHint(unsigned BuiltinID);
4835  llvm::Value *EmitX86CpuIs(const CallExpr *E);
4836  llvm::Value *EmitX86CpuIs(StringRef CPUStr);
4837  llvm::Value *EmitX86CpuSupports(const CallExpr *E);
4838  llvm::Value *EmitX86CpuSupports(ArrayRef<StringRef> FeatureStrs);
4839  llvm::Value *EmitX86CpuSupports(uint64_t Mask);
4840  llvm::Value *EmitX86CpuInit();
4841  llvm::Value *FormX86ResolverCondition(const MultiVersionResolverOption &RO);
4842  llvm::Value *EmitAArch64CpuInit();
4843  llvm::Value *
4844  FormAArch64ResolverCondition(const MultiVersionResolverOption &RO);
4845  llvm::Value *EmitAArch64CpuSupports(ArrayRef<StringRef> FeatureStrs);
4846};
4847
4848
4849inline DominatingLLVMValue::saved_type
4850DominatingLLVMValue::save(CodeGenFunction &CGF, llvm::Value *value) {
4851  if (!needsSaving(value)) return saved_type(value, false);
4852
4853  // Otherwise, we need an alloca.
4854  auto align = CharUnits::fromQuantity(
4855      CGF.CGM.getDataLayout().getPrefTypeAlign(value->getType()));
4856  Address alloca =
4857      CGF.CreateTempAlloca(value->getType(), align, "cond-cleanup.save");
4858  CGF.Builder.CreateStore(value, alloca);
4859
4860  return saved_type(alloca.getPointer(), true);
4861}
4862
4863inline llvm::Value *DominatingLLVMValue::restore(CodeGenFunction &CGF,
4864                                                 saved_type value) {
4865  // If the value says it wasn't saved, trust that it's still dominating.
4866  if (!value.getInt()) return value.getPointer();
4867
4868  // Otherwise, it should be an alloca instruction, as set up in save().
4869  auto alloca = cast<llvm::AllocaInst>(value.getPointer());
4870  return CGF.Builder.CreateAlignedLoad(alloca->getAllocatedType(), alloca,
4871                                       alloca->getAlign());
4872}
4873
4874}  // end namespace CodeGen
4875
4876// Map the LangOption for floating point exception behavior into
4877// the corresponding enum in the IR.
4878llvm::fp::ExceptionBehavior
4879ToConstrainedExceptMD(LangOptions::FPExceptionModeKind Kind);
4880}  // end namespace clang
4881
4882#endif
4883