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