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