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