1 //===-- CodeGenFunction.h - Per-Function state for LLVM CodeGen -*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This is the internal per-function state used for llvm translation.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENFUNCTION_H
15 #define LLVM_CLANG_LIB_CODEGEN_CODEGENFUNCTION_H
16 
17 #include "CGBuilder.h"
18 #include "CGDebugInfo.h"
19 #include "CGLoopInfo.h"
20 #include "CGValue.h"
21 #include "CodeGenModule.h"
22 #include "CodeGenPGO.h"
23 #include "EHScopeStack.h"
24 #include "clang/AST/CharUnits.h"
25 #include "clang/AST/ExprCXX.h"
26 #include "clang/AST/ExprObjC.h"
27 #include "clang/AST/Type.h"
28 #include "clang/Basic/ABI.h"
29 #include "clang/Basic/CapturedStmt.h"
30 #include "clang/Basic/OpenMPKinds.h"
31 #include "clang/Basic/TargetInfo.h"
32 #include "clang/Frontend/CodeGenOptions.h"
33 #include "llvm/ADT/ArrayRef.h"
34 #include "llvm/ADT/DenseMap.h"
35 #include "llvm/ADT/SmallVector.h"
36 #include "llvm/IR/ValueHandle.h"
37 #include "llvm/Support/Debug.h"
38 
39 namespace llvm {
40 class BasicBlock;
41 class LLVMContext;
42 class MDNode;
43 class Module;
44 class SwitchInst;
45 class Twine;
46 class Value;
47 class CallSite;
48 }
49 
50 namespace clang {
51 class ASTContext;
52 class BlockDecl;
53 class CXXDestructorDecl;
54 class CXXForRangeStmt;
55 class CXXTryStmt;
56 class Decl;
57 class LabelDecl;
58 class EnumConstantDecl;
59 class FunctionDecl;
60 class FunctionProtoType;
61 class LabelStmt;
62 class ObjCContainerDecl;
63 class ObjCInterfaceDecl;
64 class ObjCIvarDecl;
65 class ObjCMethodDecl;
66 class ObjCImplementationDecl;
67 class ObjCPropertyImplDecl;
68 class TargetInfo;
69 class TargetCodeGenInfo;
70 class VarDecl;
71 class ObjCForCollectionStmt;
72 class ObjCAtTryStmt;
73 class ObjCAtThrowStmt;
74 class ObjCAtSynchronizedStmt;
75 class ObjCAutoreleasePoolStmt;
76 
77 namespace CodeGen {
78 class CodeGenTypes;
79 class CGFunctionInfo;
80 class CGRecordLayout;
81 class CGBlockInfo;
82 class CGCXXABI;
83 class BlockFlags;
84 class BlockFieldFlags;
85 
86 /// The kind of evaluation to perform on values of a particular
87 /// type.  Basically, is the code in CGExprScalar, CGExprComplex, or
88 /// CGExprAgg?
89 ///
90 /// TODO: should vectors maybe be split out into their own thing?
91 enum TypeEvaluationKind {
92   TEK_Scalar,
93   TEK_Complex,
94   TEK_Aggregate
95 };
96 
97 /// CodeGenFunction - This class organizes the per-function state that is used
98 /// while generating LLVM code.
99 class CodeGenFunction : public CodeGenTypeCache {
100   CodeGenFunction(const CodeGenFunction &) = delete;
101   void operator=(const CodeGenFunction &) = delete;
102 
103   friend class CGCXXABI;
104 public:
105   /// A jump destination is an abstract label, branching to which may
106   /// require a jump out through normal cleanups.
107   struct JumpDest {
108     JumpDest() : Block(nullptr), ScopeDepth(), Index(0) {}
109     JumpDest(llvm::BasicBlock *Block,
110              EHScopeStack::stable_iterator Depth,
111              unsigned Index)
112       : Block(Block), ScopeDepth(Depth), Index(Index) {}
113 
114     bool isValid() const { return Block != nullptr; }
115     llvm::BasicBlock *getBlock() const { return Block; }
116     EHScopeStack::stable_iterator getScopeDepth() const { return ScopeDepth; }
117     unsigned getDestIndex() const { return Index; }
118 
119     // This should be used cautiously.
120     void setScopeDepth(EHScopeStack::stable_iterator depth) {
121       ScopeDepth = depth;
122     }
123 
124   private:
125     llvm::BasicBlock *Block;
126     EHScopeStack::stable_iterator ScopeDepth;
127     unsigned Index;
128   };
129 
130   CodeGenModule &CGM;  // Per-module state.
131   const TargetInfo &Target;
132 
133   typedef std::pair<llvm::Value *, llvm::Value *> ComplexPairTy;
134   LoopInfoStack LoopStack;
135   CGBuilderTy Builder;
136 
137   /// \brief CGBuilder insert helper. This function is called after an
138   /// instruction is created using Builder.
139   void InsertHelper(llvm::Instruction *I, const llvm::Twine &Name,
140                     llvm::BasicBlock *BB,
141                     llvm::BasicBlock::iterator InsertPt) const;
142 
143   /// CurFuncDecl - Holds the Decl for the current outermost
144   /// non-closure context.
145   const Decl *CurFuncDecl;
146   /// CurCodeDecl - This is the inner-most code context, which includes blocks.
147   const Decl *CurCodeDecl;
148   const CGFunctionInfo *CurFnInfo;
149   QualType FnRetTy;
150   llvm::Function *CurFn;
151 
152   /// CurGD - The GlobalDecl for the current function being compiled.
153   GlobalDecl CurGD;
154 
155   /// PrologueCleanupDepth - The cleanup depth enclosing all the
156   /// cleanups associated with the parameters.
157   EHScopeStack::stable_iterator PrologueCleanupDepth;
158 
159   /// ReturnBlock - Unified return block.
160   JumpDest ReturnBlock;
161 
162   /// ReturnValue - The temporary alloca to hold the return value. This is null
163   /// iff the function has no return value.
164   llvm::Value *ReturnValue;
165 
166   /// AllocaInsertPoint - This is an instruction in the entry block before which
167   /// we prefer to insert allocas.
168   llvm::AssertingVH<llvm::Instruction> AllocaInsertPt;
169 
170   /// \brief API for captured statement code generation.
171   class CGCapturedStmtInfo {
172   public:
173     explicit CGCapturedStmtInfo(CapturedRegionKind K = CR_Default)
174         : Kind(K), ThisValue(nullptr), CXXThisFieldDecl(nullptr) {}
175     explicit CGCapturedStmtInfo(const CapturedStmt &S,
176                                 CapturedRegionKind K = CR_Default)
177       : Kind(K), ThisValue(nullptr), CXXThisFieldDecl(nullptr) {
178 
179       RecordDecl::field_iterator Field =
180         S.getCapturedRecordDecl()->field_begin();
181       for (CapturedStmt::const_capture_iterator I = S.capture_begin(),
182                                                 E = S.capture_end();
183            I != E; ++I, ++Field) {
184         if (I->capturesThis())
185           CXXThisFieldDecl = *Field;
186         else if (I->capturesVariable())
187           CaptureFields[I->getCapturedVar()] = *Field;
188       }
189     }
190 
191     virtual ~CGCapturedStmtInfo();
192 
193     CapturedRegionKind getKind() const { return Kind; }
194 
195     virtual void setContextValue(llvm::Value *V) { ThisValue = V; }
196     // \brief Retrieve the value of the context parameter.
197     virtual llvm::Value *getContextValue() const { return ThisValue; }
198 
199     /// \brief Lookup the captured field decl for a variable.
200     virtual const FieldDecl *lookup(const VarDecl *VD) const {
201       return CaptureFields.lookup(VD);
202     }
203 
204     bool isCXXThisExprCaptured() const { return getThisFieldDecl() != nullptr; }
205     virtual FieldDecl *getThisFieldDecl() const { return CXXThisFieldDecl; }
206 
207     static bool classof(const CGCapturedStmtInfo *) {
208       return true;
209     }
210 
211     /// \brief Emit the captured statement body.
212     virtual void EmitBody(CodeGenFunction &CGF, const Stmt *S) {
213       CGF.incrementProfileCounter(S);
214       CGF.EmitStmt(S);
215     }
216 
217     /// \brief Get the name of the capture helper.
218     virtual StringRef getHelperName() const { return "__captured_stmt"; }
219 
220   private:
221     /// \brief The kind of captured statement being generated.
222     CapturedRegionKind Kind;
223 
224     /// \brief Keep the map between VarDecl and FieldDecl.
225     llvm::SmallDenseMap<const VarDecl *, FieldDecl *> CaptureFields;
226 
227     /// \brief The base address of the captured record, passed in as the first
228     /// argument of the parallel region function.
229     llvm::Value *ThisValue;
230 
231     /// \brief Captured 'this' type.
232     FieldDecl *CXXThisFieldDecl;
233   };
234   CGCapturedStmtInfo *CapturedStmtInfo;
235 
236   /// \brief RAII for correct setting/restoring of CapturedStmtInfo.
237   class CGCapturedStmtRAII {
238   private:
239     CodeGenFunction &CGF;
240     CGCapturedStmtInfo *PrevCapturedStmtInfo;
241   public:
242     CGCapturedStmtRAII(CodeGenFunction &CGF,
243                        CGCapturedStmtInfo *NewCapturedStmtInfo)
244         : CGF(CGF), PrevCapturedStmtInfo(CGF.CapturedStmtInfo) {
245       CGF.CapturedStmtInfo = NewCapturedStmtInfo;
246     }
247     ~CGCapturedStmtRAII() { CGF.CapturedStmtInfo = PrevCapturedStmtInfo; }
248   };
249 
250   /// BoundsChecking - Emit run-time bounds checks. Higher values mean
251   /// potentially higher performance penalties.
252   unsigned char BoundsChecking;
253 
254   /// \brief Sanitizers enabled for this function.
255   SanitizerSet SanOpts;
256 
257   /// \brief True if CodeGen currently emits code implementing sanitizer checks.
258   bool IsSanitizerScope;
259 
260   /// \brief RAII object to set/unset CodeGenFunction::IsSanitizerScope.
261   class SanitizerScope {
262     CodeGenFunction *CGF;
263   public:
264     SanitizerScope(CodeGenFunction *CGF);
265     ~SanitizerScope();
266   };
267 
268   /// In C++, whether we are code generating a thunk.  This controls whether we
269   /// should emit cleanups.
270   bool CurFuncIsThunk;
271 
272   /// In ARC, whether we should autorelease the return value.
273   bool AutoreleaseResult;
274 
275   /// Whether we processed a Microsoft-style asm block during CodeGen. These can
276   /// potentially set the return value.
277   bool SawAsmBlock;
278 
279   /// True if the current function is an outlined SEH helper. This can be a
280   /// finally block or filter expression.
281   bool IsOutlinedSEHHelper;
282 
283   const CodeGen::CGBlockInfo *BlockInfo;
284   llvm::Value *BlockPointer;
285 
286   llvm::DenseMap<const VarDecl *, FieldDecl *> LambdaCaptureFields;
287   FieldDecl *LambdaThisCaptureField;
288 
289   /// \brief A mapping from NRVO variables to the flags used to indicate
290   /// when the NRVO has been applied to this variable.
291   llvm::DenseMap<const VarDecl *, llvm::Value *> NRVOFlags;
292 
293   EHScopeStack EHStack;
294   llvm::SmallVector<char, 256> LifetimeExtendedCleanupStack;
295   llvm::SmallVector<const JumpDest *, 2> SEHTryEpilogueStack;
296 
297   /// Header for data within LifetimeExtendedCleanupStack.
298   struct LifetimeExtendedCleanupHeader {
299     /// The size of the following cleanup object.
300     unsigned Size;
301     /// The kind of cleanup to push: a value from the CleanupKind enumeration.
302     CleanupKind Kind;
303 
304     size_t getSize() const { return Size; }
305     CleanupKind getKind() const { return Kind; }
306   };
307 
308   /// i32s containing the indexes of the cleanup destinations.
309   llvm::AllocaInst *NormalCleanupDest;
310 
311   unsigned NextCleanupDestIndex;
312 
313   /// FirstBlockInfo - The head of a singly-linked-list of block layouts.
314   CGBlockInfo *FirstBlockInfo;
315 
316   /// EHResumeBlock - Unified block containing a call to llvm.eh.resume.
317   llvm::BasicBlock *EHResumeBlock;
318 
319   /// The exception slot.  All landing pads write the current exception pointer
320   /// into this alloca.
321   llvm::Value *ExceptionSlot;
322 
323   /// The selector slot.  Under the MandatoryCleanup model, all landing pads
324   /// write the current selector value into this alloca.
325   llvm::AllocaInst *EHSelectorSlot;
326 
327   /// A stack of exception code slots. Entering an __except block pushes a slot
328   /// on the stack and leaving pops one. The __exception_code() intrinsic loads
329   /// a value from the top of the stack.
330   SmallVector<llvm::Value *, 1> SEHCodeSlotStack;
331 
332   /// Value returned by __exception_info intrinsic.
333   llvm::Value *SEHInfo = nullptr;
334 
335   /// Emits a landing pad for the current EH stack.
336   llvm::BasicBlock *EmitLandingPad();
337 
338   llvm::BasicBlock *getInvokeDestImpl();
339 
340   template <class T>
341   typename DominatingValue<T>::saved_type saveValueInCond(T value) {
342     return DominatingValue<T>::save(*this, value);
343   }
344 
345 public:
346   /// ObjCEHValueStack - Stack of Objective-C exception values, used for
347   /// rethrows.
348   SmallVector<llvm::Value*, 8> ObjCEHValueStack;
349 
350   /// A class controlling the emission of a finally block.
351   class FinallyInfo {
352     /// Where the catchall's edge through the cleanup should go.
353     JumpDest RethrowDest;
354 
355     /// A function to call to enter the catch.
356     llvm::Constant *BeginCatchFn;
357 
358     /// An i1 variable indicating whether or not the @finally is
359     /// running for an exception.
360     llvm::AllocaInst *ForEHVar;
361 
362     /// An i8* variable into which the exception pointer to rethrow
363     /// has been saved.
364     llvm::AllocaInst *SavedExnVar;
365 
366   public:
367     void enter(CodeGenFunction &CGF, const Stmt *Finally,
368                llvm::Constant *beginCatchFn, llvm::Constant *endCatchFn,
369                llvm::Constant *rethrowFn);
370     void exit(CodeGenFunction &CGF);
371   };
372 
373   /// Returns true inside SEH __try blocks.
374   bool isSEHTryScope() const { return !SEHTryEpilogueStack.empty(); }
375 
376   /// pushFullExprCleanup - Push a cleanup to be run at the end of the
377   /// current full-expression.  Safe against the possibility that
378   /// we're currently inside a conditionally-evaluated expression.
379   template <class T, class... As>
380   void pushFullExprCleanup(CleanupKind kind, As... A) {
381     // If we're not in a conditional branch, or if none of the
382     // arguments requires saving, then use the unconditional cleanup.
383     if (!isInConditionalBranch())
384       return EHStack.pushCleanup<T>(kind, A...);
385 
386     // Stash values in a tuple so we can guarantee the order of saves.
387     typedef std::tuple<typename DominatingValue<As>::saved_type...> SavedTuple;
388     SavedTuple Saved{saveValueInCond(A)...};
389 
390     typedef EHScopeStack::ConditionalCleanup<T, As...> CleanupType;
391     EHStack.pushCleanupTuple<CleanupType>(kind, Saved);
392     initFullExprCleanup();
393   }
394 
395   /// \brief Queue a cleanup to be pushed after finishing the current
396   /// full-expression.
397   template <class T, class... As>
398   void pushCleanupAfterFullExpr(CleanupKind Kind, As... A) {
399     assert(!isInConditionalBranch() && "can't defer conditional cleanup");
400 
401     LifetimeExtendedCleanupHeader Header = { sizeof(T), Kind };
402 
403     size_t OldSize = LifetimeExtendedCleanupStack.size();
404     LifetimeExtendedCleanupStack.resize(
405         LifetimeExtendedCleanupStack.size() + sizeof(Header) + Header.Size);
406 
407     static_assert(sizeof(Header) % llvm::AlignOf<T>::Alignment == 0,
408                   "Cleanup will be allocated on misaligned address");
409     char *Buffer = &LifetimeExtendedCleanupStack[OldSize];
410     new (Buffer) LifetimeExtendedCleanupHeader(Header);
411     new (Buffer + sizeof(Header)) T(A...);
412   }
413 
414   /// Set up the last cleaup that was pushed as a conditional
415   /// full-expression cleanup.
416   void initFullExprCleanup();
417 
418   /// PushDestructorCleanup - Push a cleanup to call the
419   /// complete-object destructor of an object of the given type at the
420   /// given address.  Does nothing if T is not a C++ class type with a
421   /// non-trivial destructor.
422   void PushDestructorCleanup(QualType T, llvm::Value *Addr);
423 
424   /// PushDestructorCleanup - Push a cleanup to call the
425   /// complete-object variant of the given destructor on the object at
426   /// the given address.
427   void PushDestructorCleanup(const CXXDestructorDecl *Dtor,
428                              llvm::Value *Addr);
429 
430   /// PopCleanupBlock - Will pop the cleanup entry on the stack and
431   /// process all branch fixups.
432   void PopCleanupBlock(bool FallThroughIsBranchThrough = false);
433 
434   /// DeactivateCleanupBlock - Deactivates the given cleanup block.
435   /// The block cannot be reactivated.  Pops it if it's the top of the
436   /// stack.
437   ///
438   /// \param DominatingIP - An instruction which is known to
439   ///   dominate the current IP (if set) and which lies along
440   ///   all paths of execution between the current IP and the
441   ///   the point at which the cleanup comes into scope.
442   void DeactivateCleanupBlock(EHScopeStack::stable_iterator Cleanup,
443                               llvm::Instruction *DominatingIP);
444 
445   /// ActivateCleanupBlock - Activates an initially-inactive cleanup.
446   /// Cannot be used to resurrect a deactivated cleanup.
447   ///
448   /// \param DominatingIP - An instruction which is known to
449   ///   dominate the current IP (if set) and which lies along
450   ///   all paths of execution between the current IP and the
451   ///   the point at which the cleanup comes into scope.
452   void ActivateCleanupBlock(EHScopeStack::stable_iterator Cleanup,
453                             llvm::Instruction *DominatingIP);
454 
455   /// \brief Enters a new scope for capturing cleanups, all of which
456   /// will be executed once the scope is exited.
457   class RunCleanupsScope {
458     EHScopeStack::stable_iterator CleanupStackDepth;
459     size_t LifetimeExtendedCleanupStackSize;
460     bool OldDidCallStackSave;
461   protected:
462     bool PerformCleanup;
463   private:
464 
465     RunCleanupsScope(const RunCleanupsScope &) = delete;
466     void operator=(const RunCleanupsScope &) = delete;
467 
468   protected:
469     CodeGenFunction& CGF;
470 
471   public:
472     /// \brief Enter a new cleanup scope.
473     explicit RunCleanupsScope(CodeGenFunction &CGF)
474       : PerformCleanup(true), CGF(CGF)
475     {
476       CleanupStackDepth = CGF.EHStack.stable_begin();
477       LifetimeExtendedCleanupStackSize =
478           CGF.LifetimeExtendedCleanupStack.size();
479       OldDidCallStackSave = CGF.DidCallStackSave;
480       CGF.DidCallStackSave = false;
481     }
482 
483     /// \brief Exit this cleanup scope, emitting any accumulated
484     /// cleanups.
485     ~RunCleanupsScope() {
486       if (PerformCleanup) {
487         CGF.DidCallStackSave = OldDidCallStackSave;
488         CGF.PopCleanupBlocks(CleanupStackDepth,
489                              LifetimeExtendedCleanupStackSize);
490       }
491     }
492 
493     /// \brief Determine whether this scope requires any cleanups.
494     bool requiresCleanups() const {
495       return CGF.EHStack.stable_begin() != CleanupStackDepth;
496     }
497 
498     /// \brief Force the emission of cleanups now, instead of waiting
499     /// until this object is destroyed.
500     void ForceCleanup() {
501       assert(PerformCleanup && "Already forced cleanup");
502       CGF.DidCallStackSave = OldDidCallStackSave;
503       CGF.PopCleanupBlocks(CleanupStackDepth,
504                            LifetimeExtendedCleanupStackSize);
505       PerformCleanup = false;
506     }
507   };
508 
509   class LexicalScope : public RunCleanupsScope {
510     SourceRange Range;
511     SmallVector<const LabelDecl*, 4> Labels;
512     LexicalScope *ParentScope;
513 
514     LexicalScope(const LexicalScope &) = delete;
515     void operator=(const LexicalScope &) = delete;
516 
517   public:
518     /// \brief Enter a new cleanup scope.
519     explicit LexicalScope(CodeGenFunction &CGF, SourceRange Range)
520       : RunCleanupsScope(CGF), Range(Range), ParentScope(CGF.CurLexicalScope) {
521       CGF.CurLexicalScope = this;
522       if (CGDebugInfo *DI = CGF.getDebugInfo())
523         DI->EmitLexicalBlockStart(CGF.Builder, Range.getBegin());
524     }
525 
526     void addLabel(const LabelDecl *label) {
527       assert(PerformCleanup && "adding label to dead scope?");
528       Labels.push_back(label);
529     }
530 
531     /// \brief Exit this cleanup scope, emitting any accumulated
532     /// cleanups.
533     ~LexicalScope() {
534       if (CGDebugInfo *DI = CGF.getDebugInfo())
535         DI->EmitLexicalBlockEnd(CGF.Builder, Range.getEnd());
536 
537       // If we should perform a cleanup, force them now.  Note that
538       // this ends the cleanup scope before rescoping any labels.
539       if (PerformCleanup) {
540         ApplyDebugLocation DL(CGF, Range.getEnd());
541         ForceCleanup();
542       }
543     }
544 
545     /// \brief Force the emission of cleanups now, instead of waiting
546     /// until this object is destroyed.
547     void ForceCleanup() {
548       CGF.CurLexicalScope = ParentScope;
549       RunCleanupsScope::ForceCleanup();
550 
551       if (!Labels.empty())
552         rescopeLabels();
553     }
554 
555     void rescopeLabels();
556   };
557 
558   /// \brief The scope used to remap some variables as private in the OpenMP
559   /// loop body (or other captured region emitted without outlining), and to
560   /// restore old vars back on exit.
561   class OMPPrivateScope : public RunCleanupsScope {
562     typedef llvm::DenseMap<const VarDecl *, llvm::Value *> VarDeclMapTy;
563     VarDeclMapTy SavedLocals;
564     VarDeclMapTy SavedPrivates;
565 
566   private:
567     OMPPrivateScope(const OMPPrivateScope &) = delete;
568     void operator=(const OMPPrivateScope &) = delete;
569 
570   public:
571     /// \brief Enter a new OpenMP private scope.
572     explicit OMPPrivateScope(CodeGenFunction &CGF) : RunCleanupsScope(CGF) {}
573 
574     /// \brief Registers \a LocalVD variable as a private and apply \a
575     /// PrivateGen function for it to generate corresponding private variable.
576     /// \a PrivateGen returns an address of the generated private variable.
577     /// \return true if the variable is registered as private, false if it has
578     /// been privatized already.
579     bool
580     addPrivate(const VarDecl *LocalVD,
581                const std::function<llvm::Value *()> &PrivateGen) {
582       assert(PerformCleanup && "adding private to dead scope");
583       if (SavedLocals.count(LocalVD) > 0) return false;
584       SavedLocals[LocalVD] = CGF.LocalDeclMap.lookup(LocalVD);
585       CGF.LocalDeclMap.erase(LocalVD);
586       SavedPrivates[LocalVD] = PrivateGen();
587       CGF.LocalDeclMap[LocalVD] = SavedLocals[LocalVD];
588       return true;
589     }
590 
591     /// \brief Privatizes local variables previously registered as private.
592     /// Registration is separate from the actual privatization to allow
593     /// initializers use values of the original variables, not the private one.
594     /// This is important, for example, if the private variable is a class
595     /// variable initialized by a constructor that references other private
596     /// variables. But at initialization original variables must be used, not
597     /// private copies.
598     /// \return true if at least one variable was privatized, false otherwise.
599     bool Privatize() {
600       for (auto VDPair : SavedPrivates) {
601         CGF.LocalDeclMap[VDPair.first] = VDPair.second;
602       }
603       SavedPrivates.clear();
604       return !SavedLocals.empty();
605     }
606 
607     void ForceCleanup() {
608       RunCleanupsScope::ForceCleanup();
609       // Remap vars back to the original values.
610       for (auto I : SavedLocals) {
611         CGF.LocalDeclMap[I.first] = I.second;
612       }
613       SavedLocals.clear();
614     }
615 
616     /// \brief Exit scope - all the mapped variables are restored.
617     ~OMPPrivateScope() {
618       if (PerformCleanup)
619         ForceCleanup();
620     }
621   };
622 
623   /// \brief Takes the old cleanup stack size and emits the cleanup blocks
624   /// that have been added.
625   void PopCleanupBlocks(EHScopeStack::stable_iterator OldCleanupStackSize);
626 
627   /// \brief Takes the old cleanup stack size and emits the cleanup blocks
628   /// that have been added, then adds all lifetime-extended cleanups from
629   /// the given position to the stack.
630   void PopCleanupBlocks(EHScopeStack::stable_iterator OldCleanupStackSize,
631                         size_t OldLifetimeExtendedStackSize);
632 
633   void ResolveBranchFixups(llvm::BasicBlock *Target);
634 
635   /// The given basic block lies in the current EH scope, but may be a
636   /// target of a potentially scope-crossing jump; get a stable handle
637   /// to which we can perform this jump later.
638   JumpDest getJumpDestInCurrentScope(llvm::BasicBlock *Target) {
639     return JumpDest(Target,
640                     EHStack.getInnermostNormalCleanup(),
641                     NextCleanupDestIndex++);
642   }
643 
644   /// The given basic block lies in the current EH scope, but may be a
645   /// target of a potentially scope-crossing jump; get a stable handle
646   /// to which we can perform this jump later.
647   JumpDest getJumpDestInCurrentScope(StringRef Name = StringRef()) {
648     return getJumpDestInCurrentScope(createBasicBlock(Name));
649   }
650 
651   /// EmitBranchThroughCleanup - Emit a branch from the current insert
652   /// block through the normal cleanup handling code (if any) and then
653   /// on to \arg Dest.
654   void EmitBranchThroughCleanup(JumpDest Dest);
655 
656   /// isObviouslyBranchWithoutCleanups - Return true if a branch to the
657   /// specified destination obviously has no cleanups to run.  'false' is always
658   /// a conservatively correct answer for this method.
659   bool isObviouslyBranchWithoutCleanups(JumpDest Dest) const;
660 
661   /// popCatchScope - Pops the catch scope at the top of the EHScope
662   /// stack, emitting any required code (other than the catch handlers
663   /// themselves).
664   void popCatchScope();
665 
666   llvm::BasicBlock *getEHResumeBlock(bool isCleanup);
667   llvm::BasicBlock *getEHDispatchBlock(EHScopeStack::stable_iterator scope);
668 
669   /// An object to manage conditionally-evaluated expressions.
670   class ConditionalEvaluation {
671     llvm::BasicBlock *StartBB;
672 
673   public:
674     ConditionalEvaluation(CodeGenFunction &CGF)
675       : StartBB(CGF.Builder.GetInsertBlock()) {}
676 
677     void begin(CodeGenFunction &CGF) {
678       assert(CGF.OutermostConditional != this);
679       if (!CGF.OutermostConditional)
680         CGF.OutermostConditional = this;
681     }
682 
683     void end(CodeGenFunction &CGF) {
684       assert(CGF.OutermostConditional != nullptr);
685       if (CGF.OutermostConditional == this)
686         CGF.OutermostConditional = nullptr;
687     }
688 
689     /// Returns a block which will be executed prior to each
690     /// evaluation of the conditional code.
691     llvm::BasicBlock *getStartingBlock() const {
692       return StartBB;
693     }
694   };
695 
696   /// isInConditionalBranch - Return true if we're currently emitting
697   /// one branch or the other of a conditional expression.
698   bool isInConditionalBranch() const { return OutermostConditional != nullptr; }
699 
700   void setBeforeOutermostConditional(llvm::Value *value, llvm::Value *addr) {
701     assert(isInConditionalBranch());
702     llvm::BasicBlock *block = OutermostConditional->getStartingBlock();
703     new llvm::StoreInst(value, addr, &block->back());
704   }
705 
706   /// An RAII object to record that we're evaluating a statement
707   /// expression.
708   class StmtExprEvaluation {
709     CodeGenFunction &CGF;
710 
711     /// We have to save the outermost conditional: cleanups in a
712     /// statement expression aren't conditional just because the
713     /// StmtExpr is.
714     ConditionalEvaluation *SavedOutermostConditional;
715 
716   public:
717     StmtExprEvaluation(CodeGenFunction &CGF)
718       : CGF(CGF), SavedOutermostConditional(CGF.OutermostConditional) {
719       CGF.OutermostConditional = nullptr;
720     }
721 
722     ~StmtExprEvaluation() {
723       CGF.OutermostConditional = SavedOutermostConditional;
724       CGF.EnsureInsertPoint();
725     }
726   };
727 
728   /// An object which temporarily prevents a value from being
729   /// destroyed by aggressive peephole optimizations that assume that
730   /// all uses of a value have been realized in the IR.
731   class PeepholeProtection {
732     llvm::Instruction *Inst;
733     friend class CodeGenFunction;
734 
735   public:
736     PeepholeProtection() : Inst(nullptr) {}
737   };
738 
739   /// A non-RAII class containing all the information about a bound
740   /// opaque value.  OpaqueValueMapping, below, is a RAII wrapper for
741   /// this which makes individual mappings very simple; using this
742   /// class directly is useful when you have a variable number of
743   /// opaque values or don't want the RAII functionality for some
744   /// reason.
745   class OpaqueValueMappingData {
746     const OpaqueValueExpr *OpaqueValue;
747     bool BoundLValue;
748     CodeGenFunction::PeepholeProtection Protection;
749 
750     OpaqueValueMappingData(const OpaqueValueExpr *ov,
751                            bool boundLValue)
752       : OpaqueValue(ov), BoundLValue(boundLValue) {}
753   public:
754     OpaqueValueMappingData() : OpaqueValue(nullptr) {}
755 
756     static bool shouldBindAsLValue(const Expr *expr) {
757       // gl-values should be bound as l-values for obvious reasons.
758       // Records should be bound as l-values because IR generation
759       // always keeps them in memory.  Expressions of function type
760       // act exactly like l-values but are formally required to be
761       // r-values in C.
762       return expr->isGLValue() ||
763              expr->getType()->isFunctionType() ||
764              hasAggregateEvaluationKind(expr->getType());
765     }
766 
767     static OpaqueValueMappingData bind(CodeGenFunction &CGF,
768                                        const OpaqueValueExpr *ov,
769                                        const Expr *e) {
770       if (shouldBindAsLValue(ov))
771         return bind(CGF, ov, CGF.EmitLValue(e));
772       return bind(CGF, ov, CGF.EmitAnyExpr(e));
773     }
774 
775     static OpaqueValueMappingData bind(CodeGenFunction &CGF,
776                                        const OpaqueValueExpr *ov,
777                                        const LValue &lv) {
778       assert(shouldBindAsLValue(ov));
779       CGF.OpaqueLValues.insert(std::make_pair(ov, lv));
780       return OpaqueValueMappingData(ov, true);
781     }
782 
783     static OpaqueValueMappingData bind(CodeGenFunction &CGF,
784                                        const OpaqueValueExpr *ov,
785                                        const RValue &rv) {
786       assert(!shouldBindAsLValue(ov));
787       CGF.OpaqueRValues.insert(std::make_pair(ov, rv));
788 
789       OpaqueValueMappingData data(ov, false);
790 
791       // Work around an extremely aggressive peephole optimization in
792       // EmitScalarConversion which assumes that all other uses of a
793       // value are extant.
794       data.Protection = CGF.protectFromPeepholes(rv);
795 
796       return data;
797     }
798 
799     bool isValid() const { return OpaqueValue != nullptr; }
800     void clear() { OpaqueValue = nullptr; }
801 
802     void unbind(CodeGenFunction &CGF) {
803       assert(OpaqueValue && "no data to unbind!");
804 
805       if (BoundLValue) {
806         CGF.OpaqueLValues.erase(OpaqueValue);
807       } else {
808         CGF.OpaqueRValues.erase(OpaqueValue);
809         CGF.unprotectFromPeepholes(Protection);
810       }
811     }
812   };
813 
814   /// An RAII object to set (and then clear) a mapping for an OpaqueValueExpr.
815   class OpaqueValueMapping {
816     CodeGenFunction &CGF;
817     OpaqueValueMappingData Data;
818 
819   public:
820     static bool shouldBindAsLValue(const Expr *expr) {
821       return OpaqueValueMappingData::shouldBindAsLValue(expr);
822     }
823 
824     /// Build the opaque value mapping for the given conditional
825     /// operator if it's the GNU ?: extension.  This is a common
826     /// enough pattern that the convenience operator is really
827     /// helpful.
828     ///
829     OpaqueValueMapping(CodeGenFunction &CGF,
830                        const AbstractConditionalOperator *op) : CGF(CGF) {
831       if (isa<ConditionalOperator>(op))
832         // Leave Data empty.
833         return;
834 
835       const BinaryConditionalOperator *e = cast<BinaryConditionalOperator>(op);
836       Data = OpaqueValueMappingData::bind(CGF, e->getOpaqueValue(),
837                                           e->getCommon());
838     }
839 
840     OpaqueValueMapping(CodeGenFunction &CGF,
841                        const OpaqueValueExpr *opaqueValue,
842                        LValue lvalue)
843       : CGF(CGF), Data(OpaqueValueMappingData::bind(CGF, opaqueValue, lvalue)) {
844     }
845 
846     OpaqueValueMapping(CodeGenFunction &CGF,
847                        const OpaqueValueExpr *opaqueValue,
848                        RValue rvalue)
849       : CGF(CGF), Data(OpaqueValueMappingData::bind(CGF, opaqueValue, rvalue)) {
850     }
851 
852     void pop() {
853       Data.unbind(CGF);
854       Data.clear();
855     }
856 
857     ~OpaqueValueMapping() {
858       if (Data.isValid()) Data.unbind(CGF);
859     }
860   };
861 
862   /// getByrefValueFieldNumber - Given a declaration, returns the LLVM field
863   /// number that holds the value.
864   std::pair<llvm::Type *, unsigned>
865   getByRefValueLLVMField(const ValueDecl *VD) const;
866 
867   /// BuildBlockByrefAddress - Computes address location of the
868   /// variable which is declared as __block.
869   llvm::Value *BuildBlockByrefAddress(llvm::Value *BaseAddr,
870                                       const VarDecl *V);
871 private:
872   CGDebugInfo *DebugInfo;
873   bool DisableDebugInfo;
874 
875   /// DidCallStackSave - Whether llvm.stacksave has been called. Used to avoid
876   /// calling llvm.stacksave for multiple VLAs in the same scope.
877   bool DidCallStackSave;
878 
879   /// IndirectBranch - The first time an indirect goto is seen we create a block
880   /// with an indirect branch.  Every time we see the address of a label taken,
881   /// we add the label to the indirect goto.  Every subsequent indirect goto is
882   /// codegen'd as a jump to the IndirectBranch's basic block.
883   llvm::IndirectBrInst *IndirectBranch;
884 
885   /// LocalDeclMap - This keeps track of the LLVM allocas or globals for local C
886   /// decls.
887   typedef llvm::DenseMap<const Decl*, llvm::Value*> DeclMapTy;
888   DeclMapTy LocalDeclMap;
889 
890   /// Track escaped local variables with auto storage. Used during SEH
891   /// outlining to produce a call to llvm.localescape.
892   llvm::DenseMap<llvm::AllocaInst *, int> EscapedLocals;
893 
894   /// LabelMap - This keeps track of the LLVM basic block for each C label.
895   llvm::DenseMap<const LabelDecl*, JumpDest> LabelMap;
896 
897   // BreakContinueStack - This keeps track of where break and continue
898   // statements should jump to.
899   struct BreakContinue {
900     BreakContinue(JumpDest Break, JumpDest Continue)
901       : BreakBlock(Break), ContinueBlock(Continue) {}
902 
903     JumpDest BreakBlock;
904     JumpDest ContinueBlock;
905   };
906   SmallVector<BreakContinue, 8> BreakContinueStack;
907 
908   CodeGenPGO PGO;
909 
910   /// Calculate branch weights appropriate for PGO data
911   llvm::MDNode *createProfileWeights(uint64_t TrueCount, uint64_t FalseCount);
912   llvm::MDNode *createProfileWeights(ArrayRef<uint64_t> Weights);
913   llvm::MDNode *createProfileWeightsForLoop(const Stmt *Cond,
914                                             uint64_t LoopCount);
915 
916 public:
917   /// Increment the profiler's counter for the given statement.
918   void incrementProfileCounter(const Stmt *S) {
919     if (CGM.getCodeGenOpts().ProfileInstrGenerate)
920       PGO.emitCounterIncrement(Builder, S);
921     PGO.setCurrentStmt(S);
922   }
923 
924   /// Get the profiler's count for the given statement.
925   uint64_t getProfileCount(const Stmt *S) {
926     Optional<uint64_t> Count = PGO.getStmtCount(S);
927     if (!Count.hasValue())
928       return 0;
929     return *Count;
930   }
931 
932   /// Set the profiler's current count.
933   void setCurrentProfileCount(uint64_t Count) {
934     PGO.setCurrentRegionCount(Count);
935   }
936 
937   /// Get the profiler's current count. This is generally the count for the most
938   /// recently incremented counter.
939   uint64_t getCurrentProfileCount() {
940     return PGO.getCurrentRegionCount();
941   }
942 
943 private:
944 
945   /// SwitchInsn - This is nearest current switch instruction. It is null if
946   /// current context is not in a switch.
947   llvm::SwitchInst *SwitchInsn;
948   /// The branch weights of SwitchInsn when doing instrumentation based PGO.
949   SmallVector<uint64_t, 16> *SwitchWeights;
950 
951   /// CaseRangeBlock - This block holds if condition check for last case
952   /// statement range in current switch instruction.
953   llvm::BasicBlock *CaseRangeBlock;
954 
955   /// OpaqueLValues - Keeps track of the current set of opaque value
956   /// expressions.
957   llvm::DenseMap<const OpaqueValueExpr *, LValue> OpaqueLValues;
958   llvm::DenseMap<const OpaqueValueExpr *, RValue> OpaqueRValues;
959 
960   // VLASizeMap - This keeps track of the associated size for each VLA type.
961   // We track this by the size expression rather than the type itself because
962   // in certain situations, like a const qualifier applied to an VLA typedef,
963   // multiple VLA types can share the same size expression.
964   // FIXME: Maybe this could be a stack of maps that is pushed/popped as we
965   // enter/leave scopes.
966   llvm::DenseMap<const Expr*, llvm::Value*> VLASizeMap;
967 
968   /// A block containing a single 'unreachable' instruction.  Created
969   /// lazily by getUnreachableBlock().
970   llvm::BasicBlock *UnreachableBlock;
971 
972   /// Counts of the number return expressions in the function.
973   unsigned NumReturnExprs;
974 
975   /// Count the number of simple (constant) return expressions in the function.
976   unsigned NumSimpleReturnExprs;
977 
978   /// The last regular (non-return) debug location (breakpoint) in the function.
979   SourceLocation LastStopPoint;
980 
981 public:
982   /// A scope within which we are constructing the fields of an object which
983   /// might use a CXXDefaultInitExpr. This stashes away a 'this' value to use
984   /// if we need to evaluate a CXXDefaultInitExpr within the evaluation.
985   class FieldConstructionScope {
986   public:
987     FieldConstructionScope(CodeGenFunction &CGF, llvm::Value *This)
988         : CGF(CGF), OldCXXDefaultInitExprThis(CGF.CXXDefaultInitExprThis) {
989       CGF.CXXDefaultInitExprThis = This;
990     }
991     ~FieldConstructionScope() {
992       CGF.CXXDefaultInitExprThis = OldCXXDefaultInitExprThis;
993     }
994 
995   private:
996     CodeGenFunction &CGF;
997     llvm::Value *OldCXXDefaultInitExprThis;
998   };
999 
1000   /// The scope of a CXXDefaultInitExpr. Within this scope, the value of 'this'
1001   /// is overridden to be the object under construction.
1002   class CXXDefaultInitExprScope {
1003   public:
1004     CXXDefaultInitExprScope(CodeGenFunction &CGF)
1005         : CGF(CGF), OldCXXThisValue(CGF.CXXThisValue) {
1006       CGF.CXXThisValue = CGF.CXXDefaultInitExprThis;
1007     }
1008     ~CXXDefaultInitExprScope() {
1009       CGF.CXXThisValue = OldCXXThisValue;
1010     }
1011 
1012   public:
1013     CodeGenFunction &CGF;
1014     llvm::Value *OldCXXThisValue;
1015   };
1016 
1017 private:
1018   /// CXXThisDecl - When generating code for a C++ member function,
1019   /// this will hold the implicit 'this' declaration.
1020   ImplicitParamDecl *CXXABIThisDecl;
1021   llvm::Value *CXXABIThisValue;
1022   llvm::Value *CXXThisValue;
1023 
1024   /// The value of 'this' to use when evaluating CXXDefaultInitExprs within
1025   /// this expression.
1026   llvm::Value *CXXDefaultInitExprThis;
1027 
1028   /// CXXStructorImplicitParamDecl - When generating code for a constructor or
1029   /// destructor, this will hold the implicit argument (e.g. VTT).
1030   ImplicitParamDecl *CXXStructorImplicitParamDecl;
1031   llvm::Value *CXXStructorImplicitParamValue;
1032 
1033   /// OutermostConditional - Points to the outermost active
1034   /// conditional control.  This is used so that we know if a
1035   /// temporary should be destroyed conditionally.
1036   ConditionalEvaluation *OutermostConditional;
1037 
1038   /// The current lexical scope.
1039   LexicalScope *CurLexicalScope;
1040 
1041   /// The current source location that should be used for exception
1042   /// handling code.
1043   SourceLocation CurEHLocation;
1044 
1045   /// ByrefValueInfoMap - For each __block variable, contains a pair of the LLVM
1046   /// type as well as the field number that contains the actual data.
1047   llvm::DenseMap<const ValueDecl *, std::pair<llvm::Type *,
1048                                               unsigned> > ByRefValueInfo;
1049 
1050   llvm::BasicBlock *TerminateLandingPad;
1051   llvm::BasicBlock *TerminateHandler;
1052   llvm::BasicBlock *TrapBB;
1053 
1054   /// Add a kernel metadata node to the named metadata node 'opencl.kernels'.
1055   /// In the kernel metadata node, reference the kernel function and metadata
1056   /// nodes for its optional attribute qualifiers (OpenCL 1.1 6.7.2):
1057   /// - A node for the vec_type_hint(<type>) qualifier contains string
1058   ///   "vec_type_hint", an undefined value of the <type> data type,
1059   ///   and a Boolean that is true if the <type> is integer and signed.
1060   /// - A node for the work_group_size_hint(X,Y,Z) qualifier contains string
1061   ///   "work_group_size_hint", and three 32-bit integers X, Y and Z.
1062   /// - A node for the reqd_work_group_size(X,Y,Z) qualifier contains string
1063   ///   "reqd_work_group_size", and three 32-bit integers X, Y and Z.
1064   void EmitOpenCLKernelMetadata(const FunctionDecl *FD,
1065                                 llvm::Function *Fn);
1066 
1067 public:
1068   CodeGenFunction(CodeGenModule &cgm, bool suppressNewContext=false);
1069   ~CodeGenFunction();
1070 
1071   CodeGenTypes &getTypes() const { return CGM.getTypes(); }
1072   ASTContext &getContext() const { return CGM.getContext(); }
1073   CGDebugInfo *getDebugInfo() {
1074     if (DisableDebugInfo)
1075       return nullptr;
1076     return DebugInfo;
1077   }
1078   void disableDebugInfo() { DisableDebugInfo = true; }
1079   void enableDebugInfo() { DisableDebugInfo = false; }
1080 
1081   bool shouldUseFusedARCCalls() {
1082     return CGM.getCodeGenOpts().OptimizationLevel == 0;
1083   }
1084 
1085   const LangOptions &getLangOpts() const { return CGM.getLangOpts(); }
1086 
1087   /// Returns a pointer to the function's exception object and selector slot,
1088   /// which is assigned in every landing pad.
1089   llvm::Value *getExceptionSlot();
1090   llvm::Value *getEHSelectorSlot();
1091 
1092   /// Returns the contents of the function's exception object and selector
1093   /// slots.
1094   llvm::Value *getExceptionFromSlot();
1095   llvm::Value *getSelectorFromSlot();
1096 
1097   llvm::Value *getNormalCleanupDestSlot();
1098 
1099   llvm::BasicBlock *getUnreachableBlock() {
1100     if (!UnreachableBlock) {
1101       UnreachableBlock = createBasicBlock("unreachable");
1102       new llvm::UnreachableInst(getLLVMContext(), UnreachableBlock);
1103     }
1104     return UnreachableBlock;
1105   }
1106 
1107   llvm::BasicBlock *getInvokeDest() {
1108     if (!EHStack.requiresLandingPad()) return nullptr;
1109     return getInvokeDestImpl();
1110   }
1111 
1112   bool currentFunctionUsesSEHTry() const {
1113     const auto *FD = dyn_cast_or_null<FunctionDecl>(CurCodeDecl);
1114     return FD && FD->usesSEHTry();
1115   }
1116 
1117   const TargetInfo &getTarget() const { return Target; }
1118   llvm::LLVMContext &getLLVMContext() { return CGM.getLLVMContext(); }
1119 
1120   //===--------------------------------------------------------------------===//
1121   //                                  Cleanups
1122   //===--------------------------------------------------------------------===//
1123 
1124   typedef void Destroyer(CodeGenFunction &CGF, llvm::Value *addr, QualType ty);
1125 
1126   void pushIrregularPartialArrayCleanup(llvm::Value *arrayBegin,
1127                                         llvm::Value *arrayEndPointer,
1128                                         QualType elementType,
1129                                         Destroyer *destroyer);
1130   void pushRegularPartialArrayCleanup(llvm::Value *arrayBegin,
1131                                       llvm::Value *arrayEnd,
1132                                       QualType elementType,
1133                                       Destroyer *destroyer);
1134 
1135   void pushDestroy(QualType::DestructionKind dtorKind,
1136                    llvm::Value *addr, QualType type);
1137   void pushEHDestroy(QualType::DestructionKind dtorKind,
1138                      llvm::Value *addr, QualType type);
1139   void pushDestroy(CleanupKind kind, llvm::Value *addr, QualType type,
1140                    Destroyer *destroyer, bool useEHCleanupForArray);
1141   void pushLifetimeExtendedDestroy(CleanupKind kind, llvm::Value *addr,
1142                                    QualType type, Destroyer *destroyer,
1143                                    bool useEHCleanupForArray);
1144   void pushCallObjectDeleteCleanup(const FunctionDecl *OperatorDelete,
1145                                    llvm::Value *CompletePtr,
1146                                    QualType ElementType);
1147   void pushStackRestore(CleanupKind kind, llvm::Value *SPMem);
1148   void emitDestroy(llvm::Value *addr, QualType type, Destroyer *destroyer,
1149                    bool useEHCleanupForArray);
1150   llvm::Function *generateDestroyHelper(llvm::Constant *addr, QualType type,
1151                                         Destroyer *destroyer,
1152                                         bool useEHCleanupForArray,
1153                                         const VarDecl *VD);
1154   void emitArrayDestroy(llvm::Value *begin, llvm::Value *end,
1155                         QualType type, Destroyer *destroyer,
1156                         bool checkZeroLength, bool useEHCleanup);
1157 
1158   Destroyer *getDestroyer(QualType::DestructionKind destructionKind);
1159 
1160   /// Determines whether an EH cleanup is required to destroy a type
1161   /// with the given destruction kind.
1162   bool needsEHCleanup(QualType::DestructionKind kind) {
1163     switch (kind) {
1164     case QualType::DK_none:
1165       return false;
1166     case QualType::DK_cxx_destructor:
1167     case QualType::DK_objc_weak_lifetime:
1168       return getLangOpts().Exceptions;
1169     case QualType::DK_objc_strong_lifetime:
1170       return getLangOpts().Exceptions &&
1171              CGM.getCodeGenOpts().ObjCAutoRefCountExceptions;
1172     }
1173     llvm_unreachable("bad destruction kind");
1174   }
1175 
1176   CleanupKind getCleanupKind(QualType::DestructionKind kind) {
1177     return (needsEHCleanup(kind) ? NormalAndEHCleanup : NormalCleanup);
1178   }
1179 
1180   //===--------------------------------------------------------------------===//
1181   //                                  Objective-C
1182   //===--------------------------------------------------------------------===//
1183 
1184   void GenerateObjCMethod(const ObjCMethodDecl *OMD);
1185 
1186   void StartObjCMethod(const ObjCMethodDecl *MD, const ObjCContainerDecl *CD);
1187 
1188   /// GenerateObjCGetter - Synthesize an Objective-C property getter function.
1189   void GenerateObjCGetter(ObjCImplementationDecl *IMP,
1190                           const ObjCPropertyImplDecl *PID);
1191   void generateObjCGetterBody(const ObjCImplementationDecl *classImpl,
1192                               const ObjCPropertyImplDecl *propImpl,
1193                               const ObjCMethodDecl *GetterMothodDecl,
1194                               llvm::Constant *AtomicHelperFn);
1195 
1196   void GenerateObjCCtorDtorMethod(ObjCImplementationDecl *IMP,
1197                                   ObjCMethodDecl *MD, bool ctor);
1198 
1199   /// GenerateObjCSetter - Synthesize an Objective-C property setter function
1200   /// for the given property.
1201   void GenerateObjCSetter(ObjCImplementationDecl *IMP,
1202                           const ObjCPropertyImplDecl *PID);
1203   void generateObjCSetterBody(const ObjCImplementationDecl *classImpl,
1204                               const ObjCPropertyImplDecl *propImpl,
1205                               llvm::Constant *AtomicHelperFn);
1206   bool IndirectObjCSetterArg(const CGFunctionInfo &FI);
1207   bool IvarTypeWithAggrGCObjects(QualType Ty);
1208 
1209   //===--------------------------------------------------------------------===//
1210   //                                  Block Bits
1211   //===--------------------------------------------------------------------===//
1212 
1213   llvm::Value *EmitBlockLiteral(const BlockExpr *);
1214   llvm::Value *EmitBlockLiteral(const CGBlockInfo &Info);
1215   static void destroyBlockInfos(CGBlockInfo *info);
1216   llvm::Constant *BuildDescriptorBlockDecl(const BlockExpr *,
1217                                            const CGBlockInfo &Info,
1218                                            llvm::StructType *,
1219                                            llvm::Constant *BlockVarLayout);
1220 
1221   llvm::Function *GenerateBlockFunction(GlobalDecl GD,
1222                                         const CGBlockInfo &Info,
1223                                         const DeclMapTy &ldm,
1224                                         bool IsLambdaConversionToBlock);
1225 
1226   llvm::Constant *GenerateCopyHelperFunction(const CGBlockInfo &blockInfo);
1227   llvm::Constant *GenerateDestroyHelperFunction(const CGBlockInfo &blockInfo);
1228   llvm::Constant *GenerateObjCAtomicSetterCopyHelperFunction(
1229                                              const ObjCPropertyImplDecl *PID);
1230   llvm::Constant *GenerateObjCAtomicGetterCopyHelperFunction(
1231                                              const ObjCPropertyImplDecl *PID);
1232   llvm::Value *EmitBlockCopyAndAutorelease(llvm::Value *Block, QualType Ty);
1233 
1234   void BuildBlockRelease(llvm::Value *DeclPtr, BlockFieldFlags flags);
1235 
1236   class AutoVarEmission;
1237 
1238   void emitByrefStructureInit(const AutoVarEmission &emission);
1239   void enterByrefCleanup(const AutoVarEmission &emission);
1240 
1241   llvm::Value *LoadBlockStruct() {
1242     assert(BlockPointer && "no block pointer set!");
1243     return BlockPointer;
1244   }
1245 
1246   void AllocateBlockCXXThisPointer(const CXXThisExpr *E);
1247   void AllocateBlockDecl(const DeclRefExpr *E);
1248   llvm::Value *GetAddrOfBlockDecl(const VarDecl *var, bool ByRef);
1249   llvm::Type *BuildByRefType(const VarDecl *var);
1250 
1251   void GenerateCode(GlobalDecl GD, llvm::Function *Fn,
1252                     const CGFunctionInfo &FnInfo);
1253   /// \brief Emit code for the start of a function.
1254   /// \param Loc       The location to be associated with the function.
1255   /// \param StartLoc  The location of the function body.
1256   void StartFunction(GlobalDecl GD,
1257                      QualType RetTy,
1258                      llvm::Function *Fn,
1259                      const CGFunctionInfo &FnInfo,
1260                      const FunctionArgList &Args,
1261                      SourceLocation Loc = SourceLocation(),
1262                      SourceLocation StartLoc = SourceLocation());
1263 
1264   void EmitConstructorBody(FunctionArgList &Args);
1265   void EmitDestructorBody(FunctionArgList &Args);
1266   void emitImplicitAssignmentOperatorBody(FunctionArgList &Args);
1267   void EmitFunctionBody(FunctionArgList &Args, const Stmt *Body);
1268   void EmitBlockWithFallThrough(llvm::BasicBlock *BB, const Stmt *S);
1269 
1270   void EmitForwardingCallToLambda(const CXXMethodDecl *LambdaCallOperator,
1271                                   CallArgList &CallArgs);
1272   void EmitLambdaToBlockPointerBody(FunctionArgList &Args);
1273   void EmitLambdaBlockInvokeBody();
1274   void EmitLambdaDelegatingInvokeBody(const CXXMethodDecl *MD);
1275   void EmitLambdaStaticInvokeFunction(const CXXMethodDecl *MD);
1276   void EmitAsanPrologueOrEpilogue(bool Prologue);
1277 
1278   /// \brief Emit the unified return block, trying to avoid its emission when
1279   /// possible.
1280   /// \return The debug location of the user written return statement if the
1281   /// return block is is avoided.
1282   llvm::DebugLoc EmitReturnBlock();
1283 
1284   /// FinishFunction - Complete IR generation of the current function. It is
1285   /// legal to call this function even if there is no current insertion point.
1286   void FinishFunction(SourceLocation EndLoc=SourceLocation());
1287 
1288   void StartThunk(llvm::Function *Fn, GlobalDecl GD,
1289                   const CGFunctionInfo &FnInfo);
1290 
1291   void EmitCallAndReturnForThunk(llvm::Value *Callee, const ThunkInfo *Thunk);
1292 
1293   /// Emit a musttail call for a thunk with a potentially adjusted this pointer.
1294   void EmitMustTailThunk(const CXXMethodDecl *MD, llvm::Value *AdjustedThisPtr,
1295                          llvm::Value *Callee);
1296 
1297   /// Generate a thunk for the given method.
1298   void generateThunk(llvm::Function *Fn, const CGFunctionInfo &FnInfo,
1299                      GlobalDecl GD, const ThunkInfo &Thunk);
1300 
1301   llvm::Function *GenerateVarArgsThunk(llvm::Function *Fn,
1302                                        const CGFunctionInfo &FnInfo,
1303                                        GlobalDecl GD, const ThunkInfo &Thunk);
1304 
1305   void EmitCtorPrologue(const CXXConstructorDecl *CD, CXXCtorType Type,
1306                         FunctionArgList &Args);
1307 
1308   void EmitInitializerForField(FieldDecl *Field, LValue LHS, Expr *Init,
1309                                ArrayRef<VarDecl *> ArrayIndexes);
1310 
1311   /// InitializeVTablePointer - Initialize the vtable pointer of the given
1312   /// subobject.
1313   ///
1314   void InitializeVTablePointer(BaseSubobject Base,
1315                                const CXXRecordDecl *NearestVBase,
1316                                CharUnits OffsetFromNearestVBase,
1317                                const CXXRecordDecl *VTableClass);
1318 
1319   typedef llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBasesSetTy;
1320   void InitializeVTablePointers(BaseSubobject Base,
1321                                 const CXXRecordDecl *NearestVBase,
1322                                 CharUnits OffsetFromNearestVBase,
1323                                 bool BaseIsNonVirtualPrimaryBase,
1324                                 const CXXRecordDecl *VTableClass,
1325                                 VisitedVirtualBasesSetTy& VBases);
1326 
1327   void InitializeVTablePointers(const CXXRecordDecl *ClassDecl);
1328 
1329   /// GetVTablePtr - Return the Value of the vtable pointer member pointed
1330   /// to by This.
1331   llvm::Value *GetVTablePtr(llvm::Value *This, llvm::Type *Ty);
1332 
1333   enum CFITypeCheckKind {
1334     CFITCK_VCall,
1335     CFITCK_NVCall,
1336     CFITCK_DerivedCast,
1337     CFITCK_UnrelatedCast,
1338   };
1339 
1340   /// \brief Derived is the presumed address of an object of type T after a
1341   /// cast. If T is a polymorphic class type, emit a check that the virtual
1342   /// table for Derived belongs to a class derived from T.
1343   void EmitVTablePtrCheckForCast(QualType T, llvm::Value *Derived,
1344                                  bool MayBeNull, CFITypeCheckKind TCK,
1345                                  SourceLocation Loc);
1346 
1347   /// EmitVTablePtrCheckForCall - Virtual method MD is being called via VTable.
1348   /// If vptr CFI is enabled, emit a check that VTable is valid.
1349   void EmitVTablePtrCheckForCall(const CXXMethodDecl *MD, llvm::Value *VTable,
1350                                  CFITypeCheckKind TCK, SourceLocation Loc);
1351 
1352   /// EmitVTablePtrCheck - Emit a check that VTable is a valid virtual table for
1353   /// RD using llvm.bitset.test.
1354   void EmitVTablePtrCheck(const CXXRecordDecl *RD, llvm::Value *VTable,
1355                           CFITypeCheckKind TCK, SourceLocation Loc);
1356 
1357   /// CanDevirtualizeMemberFunctionCalls - Checks whether virtual calls on given
1358   /// expr can be devirtualized.
1359   bool CanDevirtualizeMemberFunctionCall(const Expr *Base,
1360                                          const CXXMethodDecl *MD);
1361 
1362   /// EnterDtorCleanups - Enter the cleanups necessary to complete the
1363   /// given phase of destruction for a destructor.  The end result
1364   /// should call destructors on members and base classes in reverse
1365   /// order of their construction.
1366   void EnterDtorCleanups(const CXXDestructorDecl *Dtor, CXXDtorType Type);
1367 
1368   /// ShouldInstrumentFunction - Return true if the current function should be
1369   /// instrumented with __cyg_profile_func_* calls
1370   bool ShouldInstrumentFunction();
1371 
1372   /// EmitFunctionInstrumentation - Emit LLVM code to call the specified
1373   /// instrumentation function with the current function and the call site, if
1374   /// function instrumentation is enabled.
1375   void EmitFunctionInstrumentation(const char *Fn);
1376 
1377   /// EmitMCountInstrumentation - Emit call to .mcount.
1378   void EmitMCountInstrumentation();
1379 
1380   /// EmitFunctionProlog - Emit the target specific LLVM code to load the
1381   /// arguments for the given function. This is also responsible for naming the
1382   /// LLVM function arguments.
1383   void EmitFunctionProlog(const CGFunctionInfo &FI,
1384                           llvm::Function *Fn,
1385                           const FunctionArgList &Args);
1386 
1387   /// EmitFunctionEpilog - Emit the target specific LLVM code to return the
1388   /// given temporary.
1389   void EmitFunctionEpilog(const CGFunctionInfo &FI, bool EmitRetDbgLoc,
1390                           SourceLocation EndLoc);
1391 
1392   /// EmitStartEHSpec - Emit the start of the exception spec.
1393   void EmitStartEHSpec(const Decl *D);
1394 
1395   /// EmitEndEHSpec - Emit the end of the exception spec.
1396   void EmitEndEHSpec(const Decl *D);
1397 
1398   /// getTerminateLandingPad - Return a landing pad that just calls terminate.
1399   llvm::BasicBlock *getTerminateLandingPad();
1400 
1401   /// getTerminateHandler - Return a handler (not a landing pad, just
1402   /// a catch handler) that just calls terminate.  This is used when
1403   /// a terminate scope encloses a try.
1404   llvm::BasicBlock *getTerminateHandler();
1405 
1406   llvm::Type *ConvertTypeForMem(QualType T);
1407   llvm::Type *ConvertType(QualType T);
1408   llvm::Type *ConvertType(const TypeDecl *T) {
1409     return ConvertType(getContext().getTypeDeclType(T));
1410   }
1411 
1412   /// LoadObjCSelf - Load the value of self. This function is only valid while
1413   /// generating code for an Objective-C method.
1414   llvm::Value *LoadObjCSelf();
1415 
1416   /// TypeOfSelfObject - Return type of object that this self represents.
1417   QualType TypeOfSelfObject();
1418 
1419   /// hasAggregateLLVMType - Return true if the specified AST type will map into
1420   /// an aggregate LLVM type or is void.
1421   static TypeEvaluationKind getEvaluationKind(QualType T);
1422 
1423   static bool hasScalarEvaluationKind(QualType T) {
1424     return getEvaluationKind(T) == TEK_Scalar;
1425   }
1426 
1427   static bool hasAggregateEvaluationKind(QualType T) {
1428     return getEvaluationKind(T) == TEK_Aggregate;
1429   }
1430 
1431   /// createBasicBlock - Create an LLVM basic block.
1432   llvm::BasicBlock *createBasicBlock(const Twine &name = "",
1433                                      llvm::Function *parent = nullptr,
1434                                      llvm::BasicBlock *before = nullptr) {
1435 #ifdef NDEBUG
1436     return llvm::BasicBlock::Create(getLLVMContext(), "", parent, before);
1437 #else
1438     return llvm::BasicBlock::Create(getLLVMContext(), name, parent, before);
1439 #endif
1440   }
1441 
1442   /// getBasicBlockForLabel - Return the LLVM basicblock that the specified
1443   /// label maps to.
1444   JumpDest getJumpDestForLabel(const LabelDecl *S);
1445 
1446   /// SimplifyForwardingBlocks - If the given basic block is only a branch to
1447   /// another basic block, simplify it. This assumes that no other code could
1448   /// potentially reference the basic block.
1449   void SimplifyForwardingBlocks(llvm::BasicBlock *BB);
1450 
1451   /// EmitBlock - Emit the given block \arg BB and set it as the insert point,
1452   /// adding a fall-through branch from the current insert block if
1453   /// necessary. It is legal to call this function even if there is no current
1454   /// insertion point.
1455   ///
1456   /// IsFinished - If true, indicates that the caller has finished emitting
1457   /// branches to the given block and does not expect to emit code into it. This
1458   /// means the block can be ignored if it is unreachable.
1459   void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false);
1460 
1461   /// EmitBlockAfterUses - Emit the given block somewhere hopefully
1462   /// near its uses, and leave the insertion point in it.
1463   void EmitBlockAfterUses(llvm::BasicBlock *BB);
1464 
1465   /// EmitBranch - Emit a branch to the specified basic block from the current
1466   /// insert block, taking care to avoid creation of branches from dummy
1467   /// blocks. It is legal to call this function even if there is no current
1468   /// insertion point.
1469   ///
1470   /// This function clears the current insertion point. The caller should follow
1471   /// calls to this function with calls to Emit*Block prior to generation new
1472   /// code.
1473   void EmitBranch(llvm::BasicBlock *Block);
1474 
1475   /// HaveInsertPoint - True if an insertion point is defined. If not, this
1476   /// indicates that the current code being emitted is unreachable.
1477   bool HaveInsertPoint() const {
1478     return Builder.GetInsertBlock() != nullptr;
1479   }
1480 
1481   /// EnsureInsertPoint - Ensure that an insertion point is defined so that
1482   /// emitted IR has a place to go. Note that by definition, if this function
1483   /// creates a block then that block is unreachable; callers may do better to
1484   /// detect when no insertion point is defined and simply skip IR generation.
1485   void EnsureInsertPoint() {
1486     if (!HaveInsertPoint())
1487       EmitBlock(createBasicBlock());
1488   }
1489 
1490   /// ErrorUnsupported - Print out an error that codegen doesn't support the
1491   /// specified stmt yet.
1492   void ErrorUnsupported(const Stmt *S, const char *Type);
1493 
1494   //===--------------------------------------------------------------------===//
1495   //                                  Helpers
1496   //===--------------------------------------------------------------------===//
1497 
1498   LValue MakeAddrLValue(llvm::Value *V, QualType T,
1499                         CharUnits Alignment = CharUnits()) {
1500     return LValue::MakeAddr(V, T, Alignment, getContext(),
1501                             CGM.getTBAAInfo(T));
1502   }
1503 
1504   LValue MakeNaturalAlignAddrLValue(llvm::Value *V, QualType T);
1505 
1506   /// CreateTempAlloca - This creates a alloca and inserts it into the entry
1507   /// block. The caller is responsible for setting an appropriate alignment on
1508   /// the alloca.
1509   llvm::AllocaInst *CreateTempAlloca(llvm::Type *Ty,
1510                                      const Twine &Name = "tmp");
1511 
1512   /// InitTempAlloca - Provide an initial value for the given alloca.
1513   void InitTempAlloca(llvm::AllocaInst *Alloca, llvm::Value *Value);
1514 
1515   /// CreateIRTemp - Create a temporary IR object of the given type, with
1516   /// appropriate alignment. This routine should only be used when an temporary
1517   /// value needs to be stored into an alloca (for example, to avoid explicit
1518   /// PHI construction), but the type is the IR type, not the type appropriate
1519   /// for storing in memory.
1520   llvm::AllocaInst *CreateIRTemp(QualType T, const Twine &Name = "tmp");
1521 
1522   /// CreateMemTemp - Create a temporary memory object of the given type, with
1523   /// appropriate alignment.
1524   llvm::AllocaInst *CreateMemTemp(QualType T, const Twine &Name = "tmp");
1525 
1526   /// CreateAggTemp - Create a temporary memory object for the given
1527   /// aggregate type.
1528   AggValueSlot CreateAggTemp(QualType T, const Twine &Name = "tmp") {
1529     CharUnits Alignment = getContext().getTypeAlignInChars(T);
1530     return AggValueSlot::forAddr(CreateMemTemp(T, Name), Alignment,
1531                                  T.getQualifiers(),
1532                                  AggValueSlot::IsNotDestructed,
1533                                  AggValueSlot::DoesNotNeedGCBarriers,
1534                                  AggValueSlot::IsNotAliased);
1535   }
1536 
1537   /// CreateInAllocaTmp - Create a temporary memory object for the given
1538   /// aggregate type.
1539   AggValueSlot CreateInAllocaTmp(QualType T, const Twine &Name = "inalloca");
1540 
1541   /// Emit a cast to void* in the appropriate address space.
1542   llvm::Value *EmitCastToVoidPtr(llvm::Value *value);
1543 
1544   /// EvaluateExprAsBool - Perform the usual unary conversions on the specified
1545   /// expression and compare the result against zero, returning an Int1Ty value.
1546   llvm::Value *EvaluateExprAsBool(const Expr *E);
1547 
1548   /// EmitIgnoredExpr - Emit an expression in a context which ignores the result.
1549   void EmitIgnoredExpr(const Expr *E);
1550 
1551   /// EmitAnyExpr - Emit code to compute the specified expression which can have
1552   /// any type.  The result is returned as an RValue struct.  If this is an
1553   /// aggregate expression, the aggloc/agglocvolatile arguments indicate where
1554   /// the result should be returned.
1555   ///
1556   /// \param ignoreResult True if the resulting value isn't used.
1557   RValue EmitAnyExpr(const Expr *E,
1558                      AggValueSlot aggSlot = AggValueSlot::ignored(),
1559                      bool ignoreResult = false);
1560 
1561   // EmitVAListRef - Emit a "reference" to a va_list; this is either the address
1562   // or the value of the expression, depending on how va_list is defined.
1563   llvm::Value *EmitVAListRef(const Expr *E);
1564 
1565   /// EmitAnyExprToTemp - Similary to EmitAnyExpr(), however, the result will
1566   /// always be accessible even if no aggregate location is provided.
1567   RValue EmitAnyExprToTemp(const Expr *E);
1568 
1569   /// EmitAnyExprToMem - Emits the code necessary to evaluate an
1570   /// arbitrary expression into the given memory location.
1571   void EmitAnyExprToMem(const Expr *E, llvm::Value *Location,
1572                         Qualifiers Quals, bool IsInitializer);
1573 
1574   void EmitAnyExprToExn(const Expr *E, llvm::Value *Addr);
1575 
1576   /// EmitExprAsInit - Emits the code necessary to initialize a
1577   /// location in memory with the given initializer.
1578   void EmitExprAsInit(const Expr *init, const ValueDecl *D, LValue lvalue,
1579                       bool capturedByInit);
1580 
1581   /// hasVolatileMember - returns true if aggregate type has a volatile
1582   /// member.
1583   bool hasVolatileMember(QualType T) {
1584     if (const RecordType *RT = T->getAs<RecordType>()) {
1585       const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
1586       return RD->hasVolatileMember();
1587     }
1588     return false;
1589   }
1590   /// EmitAggregateCopy - Emit an aggregate assignment.
1591   ///
1592   /// The difference to EmitAggregateCopy is that tail padding is not copied.
1593   /// This is required for correctness when assigning non-POD structures in C++.
1594   void EmitAggregateAssign(llvm::Value *DestPtr, llvm::Value *SrcPtr,
1595                            QualType EltTy) {
1596     bool IsVolatile = hasVolatileMember(EltTy);
1597     EmitAggregateCopy(DestPtr, SrcPtr, EltTy, IsVolatile, CharUnits::Zero(),
1598                       true);
1599   }
1600 
1601   void EmitAggregateCopyCtor(llvm::Value *DestPtr, llvm::Value *SrcPtr,
1602                            QualType DestTy, QualType SrcTy) {
1603     CharUnits DestTypeAlign = getContext().getTypeAlignInChars(DestTy);
1604     CharUnits SrcTypeAlign = getContext().getTypeAlignInChars(SrcTy);
1605     EmitAggregateCopy(DestPtr, SrcPtr, SrcTy, /*IsVolatile=*/false,
1606                       std::min(DestTypeAlign, SrcTypeAlign),
1607                       /*IsAssignment=*/false);
1608   }
1609 
1610   /// EmitAggregateCopy - Emit an aggregate copy.
1611   ///
1612   /// \param isVolatile - True iff either the source or the destination is
1613   /// volatile.
1614   /// \param isAssignment - If false, allow padding to be copied.  This often
1615   /// yields more efficient.
1616   void EmitAggregateCopy(llvm::Value *DestPtr, llvm::Value *SrcPtr,
1617                          QualType EltTy, bool isVolatile=false,
1618                          CharUnits Alignment = CharUnits::Zero(),
1619                          bool isAssignment = false);
1620 
1621   /// StartBlock - Start new block named N. If insert block is a dummy block
1622   /// then reuse it.
1623   void StartBlock(const char *N);
1624 
1625   /// GetAddrOfLocalVar - Return the address of a local variable.
1626   llvm::Value *GetAddrOfLocalVar(const VarDecl *VD) {
1627     llvm::Value *Res = LocalDeclMap[VD];
1628     assert(Res && "Invalid argument to GetAddrOfLocalVar(), no decl!");
1629     return Res;
1630   }
1631 
1632   /// getOpaqueLValueMapping - Given an opaque value expression (which
1633   /// must be mapped to an l-value), return its mapping.
1634   const LValue &getOpaqueLValueMapping(const OpaqueValueExpr *e) {
1635     assert(OpaqueValueMapping::shouldBindAsLValue(e));
1636 
1637     llvm::DenseMap<const OpaqueValueExpr*,LValue>::iterator
1638       it = OpaqueLValues.find(e);
1639     assert(it != OpaqueLValues.end() && "no mapping for opaque value!");
1640     return it->second;
1641   }
1642 
1643   /// getOpaqueRValueMapping - Given an opaque value expression (which
1644   /// must be mapped to an r-value), return its mapping.
1645   const RValue &getOpaqueRValueMapping(const OpaqueValueExpr *e) {
1646     assert(!OpaqueValueMapping::shouldBindAsLValue(e));
1647 
1648     llvm::DenseMap<const OpaqueValueExpr*,RValue>::iterator
1649       it = OpaqueRValues.find(e);
1650     assert(it != OpaqueRValues.end() && "no mapping for opaque value!");
1651     return it->second;
1652   }
1653 
1654   /// getAccessedFieldNo - Given an encoded value and a result number, return
1655   /// the input field number being accessed.
1656   static unsigned getAccessedFieldNo(unsigned Idx, const llvm::Constant *Elts);
1657 
1658   llvm::BlockAddress *GetAddrOfLabel(const LabelDecl *L);
1659   llvm::BasicBlock *GetIndirectGotoBlock();
1660 
1661   /// EmitNullInitialization - Generate code to set a value of the given type to
1662   /// null, If the type contains data member pointers, they will be initialized
1663   /// to -1 in accordance with the Itanium C++ ABI.
1664   void EmitNullInitialization(llvm::Value *DestPtr, QualType Ty);
1665 
1666   // EmitVAArg - Generate code to get an argument from the passed in pointer
1667   // and update it accordingly. The return value is a pointer to the argument.
1668   // FIXME: We should be able to get rid of this method and use the va_arg
1669   // instruction in LLVM instead once it works well enough.
1670   llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty);
1671 
1672   /// emitArrayLength - Compute the length of an array, even if it's a
1673   /// VLA, and drill down to the base element type.
1674   llvm::Value *emitArrayLength(const ArrayType *arrayType,
1675                                QualType &baseType,
1676                                llvm::Value *&addr);
1677 
1678   /// EmitVLASize - Capture all the sizes for the VLA expressions in
1679   /// the given variably-modified type and store them in the VLASizeMap.
1680   ///
1681   /// This function can be called with a null (unreachable) insert point.
1682   void EmitVariablyModifiedType(QualType Ty);
1683 
1684   /// getVLASize - Returns an LLVM value that corresponds to the size,
1685   /// in non-variably-sized elements, of a variable length array type,
1686   /// plus that largest non-variably-sized element type.  Assumes that
1687   /// the type has already been emitted with EmitVariablyModifiedType.
1688   std::pair<llvm::Value*,QualType> getVLASize(const VariableArrayType *vla);
1689   std::pair<llvm::Value*,QualType> getVLASize(QualType vla);
1690 
1691   /// LoadCXXThis - Load the value of 'this'. This function is only valid while
1692   /// generating code for an C++ member function.
1693   llvm::Value *LoadCXXThis() {
1694     assert(CXXThisValue && "no 'this' value for this function");
1695     return CXXThisValue;
1696   }
1697 
1698   /// LoadCXXVTT - Load the VTT parameter to base constructors/destructors have
1699   /// virtual bases.
1700   // FIXME: Every place that calls LoadCXXVTT is something
1701   // that needs to be abstracted properly.
1702   llvm::Value *LoadCXXVTT() {
1703     assert(CXXStructorImplicitParamValue && "no VTT value for this function");
1704     return CXXStructorImplicitParamValue;
1705   }
1706 
1707   /// LoadCXXStructorImplicitParam - Load the implicit parameter
1708   /// for a constructor/destructor.
1709   llvm::Value *LoadCXXStructorImplicitParam() {
1710     assert(CXXStructorImplicitParamValue &&
1711            "no implicit argument value for this function");
1712     return CXXStructorImplicitParamValue;
1713   }
1714 
1715   /// GetAddressOfBaseOfCompleteClass - Convert the given pointer to a
1716   /// complete class to the given direct base.
1717   llvm::Value *
1718   GetAddressOfDirectBaseInCompleteClass(llvm::Value *Value,
1719                                         const CXXRecordDecl *Derived,
1720                                         const CXXRecordDecl *Base,
1721                                         bool BaseIsVirtual);
1722 
1723   /// GetAddressOfBaseClass - This function will add the necessary delta to the
1724   /// load of 'this' and returns address of the base class.
1725   llvm::Value *GetAddressOfBaseClass(llvm::Value *Value,
1726                                      const CXXRecordDecl *Derived,
1727                                      CastExpr::path_const_iterator PathBegin,
1728                                      CastExpr::path_const_iterator PathEnd,
1729                                      bool NullCheckValue, SourceLocation Loc);
1730 
1731   llvm::Value *GetAddressOfDerivedClass(llvm::Value *Value,
1732                                         const CXXRecordDecl *Derived,
1733                                         CastExpr::path_const_iterator PathBegin,
1734                                         CastExpr::path_const_iterator PathEnd,
1735                                         bool NullCheckValue);
1736 
1737   /// GetVTTParameter - Return the VTT parameter that should be passed to a
1738   /// base constructor/destructor with virtual bases.
1739   /// FIXME: VTTs are Itanium ABI-specific, so the definition should move
1740   /// to ItaniumCXXABI.cpp together with all the references to VTT.
1741   llvm::Value *GetVTTParameter(GlobalDecl GD, bool ForVirtualBase,
1742                                bool Delegating);
1743 
1744   void EmitDelegateCXXConstructorCall(const CXXConstructorDecl *Ctor,
1745                                       CXXCtorType CtorType,
1746                                       const FunctionArgList &Args,
1747                                       SourceLocation Loc);
1748   // It's important not to confuse this and the previous function. Delegating
1749   // constructors are the C++0x feature. The constructor delegate optimization
1750   // is used to reduce duplication in the base and complete consturctors where
1751   // they are substantially the same.
1752   void EmitDelegatingCXXConstructorCall(const CXXConstructorDecl *Ctor,
1753                                         const FunctionArgList &Args);
1754   void EmitCXXConstructorCall(const CXXConstructorDecl *D, CXXCtorType Type,
1755                               bool ForVirtualBase, bool Delegating,
1756                               llvm::Value *This, const CXXConstructExpr *E);
1757 
1758   void EmitSynthesizedCXXCopyCtorCall(const CXXConstructorDecl *D,
1759                               llvm::Value *This, llvm::Value *Src,
1760                               const CXXConstructExpr *E);
1761 
1762   void EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
1763                                   const ConstantArrayType *ArrayTy,
1764                                   llvm::Value *ArrayPtr,
1765                                   const CXXConstructExpr *E,
1766                                   bool ZeroInitialization = false);
1767 
1768   void EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
1769                                   llvm::Value *NumElements,
1770                                   llvm::Value *ArrayPtr,
1771                                   const CXXConstructExpr *E,
1772                                   bool ZeroInitialization = false);
1773 
1774   static Destroyer destroyCXXObject;
1775 
1776   void EmitCXXDestructorCall(const CXXDestructorDecl *D, CXXDtorType Type,
1777                              bool ForVirtualBase, bool Delegating,
1778                              llvm::Value *This);
1779 
1780   void EmitNewArrayInitializer(const CXXNewExpr *E, QualType elementType,
1781                                llvm::Type *ElementTy, llvm::Value *NewPtr,
1782                                llvm::Value *NumElements,
1783                                llvm::Value *AllocSizeWithoutCookie);
1784 
1785   void EmitCXXTemporary(const CXXTemporary *Temporary, QualType TempType,
1786                         llvm::Value *Ptr);
1787 
1788   llvm::Value *EmitLifetimeStart(uint64_t Size, llvm::Value *Addr);
1789   void EmitLifetimeEnd(llvm::Value *Size, llvm::Value *Addr);
1790 
1791   llvm::Value *EmitCXXNewExpr(const CXXNewExpr *E);
1792   void EmitCXXDeleteExpr(const CXXDeleteExpr *E);
1793 
1794   void EmitDeleteCall(const FunctionDecl *DeleteFD, llvm::Value *Ptr,
1795                       QualType DeleteTy);
1796 
1797   RValue EmitBuiltinNewDeleteCall(const FunctionProtoType *Type,
1798                                   const Expr *Arg, bool IsDelete);
1799 
1800   llvm::Value* EmitCXXTypeidExpr(const CXXTypeidExpr *E);
1801   llvm::Value *EmitDynamicCast(llvm::Value *V, const CXXDynamicCastExpr *DCE);
1802   llvm::Value* EmitCXXUuidofExpr(const CXXUuidofExpr *E);
1803 
1804   /// \brief Situations in which we might emit a check for the suitability of a
1805   ///        pointer or glvalue.
1806   enum TypeCheckKind {
1807     /// Checking the operand of a load. Must be suitably sized and aligned.
1808     TCK_Load,
1809     /// Checking the destination of a store. Must be suitably sized and aligned.
1810     TCK_Store,
1811     /// Checking the bound value in a reference binding. Must be suitably sized
1812     /// and aligned, but is not required to refer to an object (until the
1813     /// reference is used), per core issue 453.
1814     TCK_ReferenceBinding,
1815     /// Checking the object expression in a non-static data member access. Must
1816     /// be an object within its lifetime.
1817     TCK_MemberAccess,
1818     /// Checking the 'this' pointer for a call to a non-static member function.
1819     /// Must be an object within its lifetime.
1820     TCK_MemberCall,
1821     /// Checking the 'this' pointer for a constructor call.
1822     TCK_ConstructorCall,
1823     /// Checking the operand of a static_cast to a derived pointer type. Must be
1824     /// null or an object within its lifetime.
1825     TCK_DowncastPointer,
1826     /// Checking the operand of a static_cast to a derived reference type. Must
1827     /// be an object within its lifetime.
1828     TCK_DowncastReference,
1829     /// Checking the operand of a cast to a base object. Must be suitably sized
1830     /// and aligned.
1831     TCK_Upcast,
1832     /// Checking the operand of a cast to a virtual base object. Must be an
1833     /// object within its lifetime.
1834     TCK_UpcastToVirtualBase
1835   };
1836 
1837   /// \brief Whether any type-checking sanitizers are enabled. If \c false,
1838   /// calls to EmitTypeCheck can be skipped.
1839   bool sanitizePerformTypeCheck() const;
1840 
1841   /// \brief Emit a check that \p V is the address of storage of the
1842   /// appropriate size and alignment for an object of type \p Type.
1843   void EmitTypeCheck(TypeCheckKind TCK, SourceLocation Loc, llvm::Value *V,
1844                      QualType Type, CharUnits Alignment = CharUnits::Zero(),
1845                      bool SkipNullCheck = false);
1846 
1847   /// \brief Emit a check that \p Base points into an array object, which
1848   /// we can access at index \p Index. \p Accessed should be \c false if we
1849   /// this expression is used as an lvalue, for instance in "&Arr[Idx]".
1850   void EmitBoundsCheck(const Expr *E, const Expr *Base, llvm::Value *Index,
1851                        QualType IndexType, bool Accessed);
1852 
1853   llvm::Value *EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV,
1854                                        bool isInc, bool isPre);
1855   ComplexPairTy EmitComplexPrePostIncDec(const UnaryOperator *E, LValue LV,
1856                                          bool isInc, bool isPre);
1857 
1858   void EmitAlignmentAssumption(llvm::Value *PtrValue, unsigned Alignment,
1859                                llvm::Value *OffsetValue = nullptr) {
1860     Builder.CreateAlignmentAssumption(CGM.getDataLayout(), PtrValue, Alignment,
1861                                       OffsetValue);
1862   }
1863 
1864   //===--------------------------------------------------------------------===//
1865   //                            Declaration Emission
1866   //===--------------------------------------------------------------------===//
1867 
1868   /// EmitDecl - Emit a declaration.
1869   ///
1870   /// This function can be called with a null (unreachable) insert point.
1871   void EmitDecl(const Decl &D);
1872 
1873   /// EmitVarDecl - Emit a local variable declaration.
1874   ///
1875   /// This function can be called with a null (unreachable) insert point.
1876   void EmitVarDecl(const VarDecl &D);
1877 
1878   void EmitScalarInit(const Expr *init, const ValueDecl *D, LValue lvalue,
1879                       bool capturedByInit);
1880   void EmitScalarInit(llvm::Value *init, LValue lvalue);
1881 
1882   typedef void SpecialInitFn(CodeGenFunction &Init, const VarDecl &D,
1883                              llvm::Value *Address);
1884 
1885   /// \brief Determine whether the given initializer is trivial in the sense
1886   /// that it requires no code to be generated.
1887   bool isTrivialInitializer(const Expr *Init);
1888 
1889   /// EmitAutoVarDecl - Emit an auto variable declaration.
1890   ///
1891   /// This function can be called with a null (unreachable) insert point.
1892   void EmitAutoVarDecl(const VarDecl &D);
1893 
1894   class AutoVarEmission {
1895     friend class CodeGenFunction;
1896 
1897     const VarDecl *Variable;
1898 
1899     /// The alignment of the variable.
1900     CharUnits Alignment;
1901 
1902     /// The address of the alloca.  Null if the variable was emitted
1903     /// as a global constant.
1904     llvm::Value *Address;
1905 
1906     llvm::Value *NRVOFlag;
1907 
1908     /// True if the variable is a __block variable.
1909     bool IsByRef;
1910 
1911     /// True if the variable is of aggregate type and has a constant
1912     /// initializer.
1913     bool IsConstantAggregate;
1914 
1915     /// Non-null if we should use lifetime annotations.
1916     llvm::Value *SizeForLifetimeMarkers;
1917 
1918     struct Invalid {};
1919     AutoVarEmission(Invalid) : Variable(nullptr) {}
1920 
1921     AutoVarEmission(const VarDecl &variable)
1922       : Variable(&variable), Address(nullptr), NRVOFlag(nullptr),
1923         IsByRef(false), IsConstantAggregate(false),
1924         SizeForLifetimeMarkers(nullptr) {}
1925 
1926     bool wasEmittedAsGlobal() const { return Address == nullptr; }
1927 
1928   public:
1929     static AutoVarEmission invalid() { return AutoVarEmission(Invalid()); }
1930 
1931     bool useLifetimeMarkers() const {
1932       return SizeForLifetimeMarkers != nullptr;
1933     }
1934     llvm::Value *getSizeForLifetimeMarkers() const {
1935       assert(useLifetimeMarkers());
1936       return SizeForLifetimeMarkers;
1937     }
1938 
1939     /// Returns the raw, allocated address, which is not necessarily
1940     /// the address of the object itself.
1941     llvm::Value *getAllocatedAddress() const {
1942       return Address;
1943     }
1944 
1945     /// Returns the address of the object within this declaration.
1946     /// Note that this does not chase the forwarding pointer for
1947     /// __block decls.
1948     llvm::Value *getObjectAddress(CodeGenFunction &CGF) const {
1949       if (!IsByRef) return Address;
1950 
1951       auto F = CGF.getByRefValueLLVMField(Variable);
1952       return CGF.Builder.CreateStructGEP(F.first, Address, F.second,
1953                                          Variable->getNameAsString());
1954     }
1955   };
1956   AutoVarEmission EmitAutoVarAlloca(const VarDecl &var);
1957   void EmitAutoVarInit(const AutoVarEmission &emission);
1958   void EmitAutoVarCleanups(const AutoVarEmission &emission);
1959   void emitAutoVarTypeCleanup(const AutoVarEmission &emission,
1960                               QualType::DestructionKind dtorKind);
1961 
1962   void EmitStaticVarDecl(const VarDecl &D,
1963                          llvm::GlobalValue::LinkageTypes Linkage);
1964 
1965   /// EmitParmDecl - Emit a ParmVarDecl or an ImplicitParamDecl.
1966   void EmitParmDecl(const VarDecl &D, llvm::Value *Arg, bool ArgIsPointer,
1967                     unsigned ArgNo);
1968 
1969   /// protectFromPeepholes - Protect a value that we're intending to
1970   /// store to the side, but which will probably be used later, from
1971   /// aggressive peepholing optimizations that might delete it.
1972   ///
1973   /// Pass the result to unprotectFromPeepholes to declare that
1974   /// protection is no longer required.
1975   ///
1976   /// There's no particular reason why this shouldn't apply to
1977   /// l-values, it's just that no existing peepholes work on pointers.
1978   PeepholeProtection protectFromPeepholes(RValue rvalue);
1979   void unprotectFromPeepholes(PeepholeProtection protection);
1980 
1981   //===--------------------------------------------------------------------===//
1982   //                             Statement Emission
1983   //===--------------------------------------------------------------------===//
1984 
1985   /// EmitStopPoint - Emit a debug stoppoint if we are emitting debug info.
1986   void EmitStopPoint(const Stmt *S);
1987 
1988   /// EmitStmt - Emit the code for the statement \arg S. It is legal to call
1989   /// this function even if there is no current insertion point.
1990   ///
1991   /// This function may clear the current insertion point; callers should use
1992   /// EnsureInsertPoint if they wish to subsequently generate code without first
1993   /// calling EmitBlock, EmitBranch, or EmitStmt.
1994   void EmitStmt(const Stmt *S);
1995 
1996   /// EmitSimpleStmt - Try to emit a "simple" statement which does not
1997   /// necessarily require an insertion point or debug information; typically
1998   /// because the statement amounts to a jump or a container of other
1999   /// statements.
2000   ///
2001   /// \return True if the statement was handled.
2002   bool EmitSimpleStmt(const Stmt *S);
2003 
2004   llvm::Value *EmitCompoundStmt(const CompoundStmt &S, bool GetLast = false,
2005                                 AggValueSlot AVS = AggValueSlot::ignored());
2006   llvm::Value *EmitCompoundStmtWithoutScope(const CompoundStmt &S,
2007                                             bool GetLast = false,
2008                                             AggValueSlot AVS =
2009                                                 AggValueSlot::ignored());
2010 
2011   /// EmitLabel - Emit the block for the given label. It is legal to call this
2012   /// function even if there is no current insertion point.
2013   void EmitLabel(const LabelDecl *D); // helper for EmitLabelStmt.
2014 
2015   void EmitLabelStmt(const LabelStmt &S);
2016   void EmitAttributedStmt(const AttributedStmt &S);
2017   void EmitGotoStmt(const GotoStmt &S);
2018   void EmitIndirectGotoStmt(const IndirectGotoStmt &S);
2019   void EmitIfStmt(const IfStmt &S);
2020 
2021   void EmitWhileStmt(const WhileStmt &S,
2022                      ArrayRef<const Attr *> Attrs = None);
2023   void EmitDoStmt(const DoStmt &S, ArrayRef<const Attr *> Attrs = None);
2024   void EmitForStmt(const ForStmt &S,
2025                    ArrayRef<const Attr *> Attrs = None);
2026   void EmitReturnStmt(const ReturnStmt &S);
2027   void EmitDeclStmt(const DeclStmt &S);
2028   void EmitBreakStmt(const BreakStmt &S);
2029   void EmitContinueStmt(const ContinueStmt &S);
2030   void EmitSwitchStmt(const SwitchStmt &S);
2031   void EmitDefaultStmt(const DefaultStmt &S);
2032   void EmitCaseStmt(const CaseStmt &S);
2033   void EmitCaseStmtRange(const CaseStmt &S);
2034   void EmitAsmStmt(const AsmStmt &S);
2035 
2036   void EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S);
2037   void EmitObjCAtTryStmt(const ObjCAtTryStmt &S);
2038   void EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S);
2039   void EmitObjCAtSynchronizedStmt(const ObjCAtSynchronizedStmt &S);
2040   void EmitObjCAutoreleasePoolStmt(const ObjCAutoreleasePoolStmt &S);
2041 
2042   void EnterCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock = false);
2043   void ExitCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock = false);
2044 
2045   void EmitCXXTryStmt(const CXXTryStmt &S);
2046   void EmitSEHTryStmt(const SEHTryStmt &S);
2047   void EmitSEHLeaveStmt(const SEHLeaveStmt &S);
2048   void EnterSEHTryStmt(const SEHTryStmt &S);
2049   void ExitSEHTryStmt(const SEHTryStmt &S);
2050 
2051   void startOutlinedSEHHelper(CodeGenFunction &ParentCGF, bool IsFilter,
2052                               const Stmt *OutlinedStmt);
2053 
2054   llvm::Function *GenerateSEHFilterFunction(CodeGenFunction &ParentCGF,
2055                                             const SEHExceptStmt &Except);
2056 
2057   llvm::Function *GenerateSEHFinallyFunction(CodeGenFunction &ParentCGF,
2058                                              const SEHFinallyStmt &Finally);
2059 
2060   void EmitSEHExceptionCodeSave(CodeGenFunction &ParentCGF,
2061                                 llvm::Value *ParentFP,
2062                                 llvm::Value *EntryEBP);
2063   llvm::Value *EmitSEHExceptionCode();
2064   llvm::Value *EmitSEHExceptionInfo();
2065   llvm::Value *EmitSEHAbnormalTermination();
2066 
2067   /// Scan the outlined statement for captures from the parent function. For
2068   /// each capture, mark the capture as escaped and emit a call to
2069   /// llvm.localrecover. Insert the localrecover result into the LocalDeclMap.
2070   void EmitCapturedLocals(CodeGenFunction &ParentCGF, const Stmt *OutlinedStmt,
2071                           bool IsFilter);
2072 
2073   /// Recovers the address of a local in a parent function. ParentVar is the
2074   /// address of the variable used in the immediate parent function. It can
2075   /// either be an alloca or a call to llvm.localrecover if there are nested
2076   /// outlined functions. ParentFP is the frame pointer of the outermost parent
2077   /// frame.
2078   llvm::Value *recoverAddrOfEscapedLocal(CodeGenFunction &ParentCGF,
2079                                          llvm::Value *ParentVar,
2080                                          llvm::Value *ParentFP);
2081 
2082   void EmitCXXForRangeStmt(const CXXForRangeStmt &S,
2083                            ArrayRef<const Attr *> Attrs = None);
2084 
2085   LValue InitCapturedStruct(const CapturedStmt &S);
2086   llvm::Function *EmitCapturedStmt(const CapturedStmt &S, CapturedRegionKind K);
2087   void GenerateCapturedStmtFunctionProlog(const CapturedStmt &S);
2088   llvm::Function *GenerateCapturedStmtFunctionEpilog(const CapturedStmt &S);
2089   llvm::Function *GenerateCapturedStmtFunction(const CapturedStmt &S);
2090   llvm::Value *GenerateCapturedStmtArgument(const CapturedStmt &S);
2091   /// \brief Perform element by element copying of arrays with type \a
2092   /// OriginalType from \a SrcAddr to \a DestAddr using copying procedure
2093   /// generated by \a CopyGen.
2094   ///
2095   /// \param DestAddr Address of the destination array.
2096   /// \param SrcAddr Address of the source array.
2097   /// \param OriginalType Type of destination and source arrays.
2098   /// \param CopyGen Copying procedure that copies value of single array element
2099   /// to another single array element.
2100   void EmitOMPAggregateAssign(
2101       llvm::Value *DestAddr, llvm::Value *SrcAddr, QualType OriginalType,
2102       const llvm::function_ref<void(llvm::Value *, llvm::Value *)> &CopyGen);
2103   /// \brief Emit proper copying of data from one variable to another.
2104   ///
2105   /// \param OriginalType Original type of the copied variables.
2106   /// \param DestAddr Destination address.
2107   /// \param SrcAddr Source address.
2108   /// \param DestVD Destination variable used in \a CopyExpr (for arrays, has
2109   /// type of the base array element).
2110   /// \param SrcVD Source variable used in \a CopyExpr (for arrays, has type of
2111   /// the base array element).
2112   /// \param Copy Actual copygin expression for copying data from \a SrcVD to \a
2113   /// DestVD.
2114   void EmitOMPCopy(CodeGenFunction &CGF, QualType OriginalType,
2115                    llvm::Value *DestAddr, llvm::Value *SrcAddr,
2116                    const VarDecl *DestVD, const VarDecl *SrcVD,
2117                    const Expr *Copy);
2118   /// \brief Emit atomic update code for constructs: \a X = \a X \a BO \a E or
2119   /// \a X = \a E \a BO \a E.
2120   ///
2121   /// \param X Value to be updated.
2122   /// \param E Update value.
2123   /// \param BO Binary operation for update operation.
2124   /// \param IsXLHSInRHSPart true if \a X is LHS in RHS part of the update
2125   /// expression, false otherwise.
2126   /// \param AO Atomic ordering of the generated atomic instructions.
2127   /// \param CommonGen Code generator for complex expressions that cannot be
2128   /// expressed through atomicrmw instruction.
2129   /// \returns <true, OldAtomicValue> if simple 'atomicrmw' instruction was
2130   /// generated, <false, RValue::get(nullptr)> otherwise.
2131   std::pair<bool, RValue> EmitOMPAtomicSimpleUpdateExpr(
2132       LValue X, RValue E, BinaryOperatorKind BO, bool IsXLHSInRHSPart,
2133       llvm::AtomicOrdering AO, SourceLocation Loc,
2134       const llvm::function_ref<RValue(RValue)> &CommonGen);
2135   bool EmitOMPFirstprivateClause(const OMPExecutableDirective &D,
2136                                  OMPPrivateScope &PrivateScope);
2137   void EmitOMPPrivateClause(const OMPExecutableDirective &D,
2138                             OMPPrivateScope &PrivateScope);
2139   /// \brief Emit code for copyin clause in \a D directive. The next code is
2140   /// generated at the start of outlined functions for directives:
2141   /// \code
2142   /// threadprivate_var1 = master_threadprivate_var1;
2143   /// operator=(threadprivate_var2, master_threadprivate_var2);
2144   /// ...
2145   /// __kmpc_barrier(&loc, global_tid);
2146   /// \endcode
2147   ///
2148   /// \param D OpenMP directive possibly with 'copyin' clause(s).
2149   /// \returns true if at least one copyin variable is found, false otherwise.
2150   bool EmitOMPCopyinClause(const OMPExecutableDirective &D);
2151   /// \brief Emit initial code for lastprivate variables. If some variable is
2152   /// not also firstprivate, then the default initialization is used. Otherwise
2153   /// initialization of this variable is performed by EmitOMPFirstprivateClause
2154   /// method.
2155   ///
2156   /// \param D Directive that may have 'lastprivate' directives.
2157   /// \param PrivateScope Private scope for capturing lastprivate variables for
2158   /// proper codegen in internal captured statement.
2159   ///
2160   /// \returns true if there is at least one lastprivate variable, false
2161   /// otherwise.
2162   bool EmitOMPLastprivateClauseInit(const OMPExecutableDirective &D,
2163                                     OMPPrivateScope &PrivateScope);
2164   /// \brief Emit final copying of lastprivate values to original variables at
2165   /// the end of the worksharing or simd directive.
2166   ///
2167   /// \param D Directive that has at least one 'lastprivate' directives.
2168   /// \param IsLastIterCond Boolean condition that must be set to 'i1 true' if
2169   /// it is the last iteration of the loop code in associated directive, or to
2170   /// 'i1 false' otherwise. If this item is nullptr, no final check is required.
2171   void EmitOMPLastprivateClauseFinal(const OMPExecutableDirective &D,
2172                                      llvm::Value *IsLastIterCond = nullptr);
2173   /// \brief Emit initial code for reduction variables. Creates reduction copies
2174   /// and initializes them with the values according to OpenMP standard.
2175   ///
2176   /// \param D Directive (possibly) with the 'reduction' clause.
2177   /// \param PrivateScope Private scope for capturing reduction variables for
2178   /// proper codegen in internal captured statement.
2179   ///
2180   void EmitOMPReductionClauseInit(const OMPExecutableDirective &D,
2181                                   OMPPrivateScope &PrivateScope);
2182   /// \brief Emit final update of reduction values to original variables at
2183   /// the end of the directive.
2184   ///
2185   /// \param D Directive that has at least one 'reduction' directives.
2186   void EmitOMPReductionClauseFinal(const OMPExecutableDirective &D);
2187   /// \brief Emit initial code for linear variables. Creates private copies
2188   /// and initializes them with the values according to OpenMP standard.
2189   ///
2190   /// \param D Directive (possibly) with the 'linear' clause.
2191   void EmitOMPLinearClauseInit(const OMPLoopDirective &D);
2192 
2193   void EmitOMPParallelDirective(const OMPParallelDirective &S);
2194   void EmitOMPSimdDirective(const OMPSimdDirective &S);
2195   void EmitOMPForDirective(const OMPForDirective &S);
2196   void EmitOMPForSimdDirective(const OMPForSimdDirective &S);
2197   void EmitOMPSectionsDirective(const OMPSectionsDirective &S);
2198   void EmitOMPSectionDirective(const OMPSectionDirective &S);
2199   void EmitOMPSingleDirective(const OMPSingleDirective &S);
2200   void EmitOMPMasterDirective(const OMPMasterDirective &S);
2201   void EmitOMPCriticalDirective(const OMPCriticalDirective &S);
2202   void EmitOMPParallelForDirective(const OMPParallelForDirective &S);
2203   void EmitOMPParallelForSimdDirective(const OMPParallelForSimdDirective &S);
2204   void EmitOMPParallelSectionsDirective(const OMPParallelSectionsDirective &S);
2205   void EmitOMPTaskDirective(const OMPTaskDirective &S);
2206   void EmitOMPTaskyieldDirective(const OMPTaskyieldDirective &S);
2207   void EmitOMPBarrierDirective(const OMPBarrierDirective &S);
2208   void EmitOMPTaskwaitDirective(const OMPTaskwaitDirective &S);
2209   void EmitOMPTaskgroupDirective(const OMPTaskgroupDirective &S);
2210   void EmitOMPFlushDirective(const OMPFlushDirective &S);
2211   void EmitOMPOrderedDirective(const OMPOrderedDirective &S);
2212   void EmitOMPAtomicDirective(const OMPAtomicDirective &S);
2213   void EmitOMPTargetDirective(const OMPTargetDirective &S);
2214   void EmitOMPTargetDataDirective(const OMPTargetDataDirective &S);
2215   void EmitOMPTeamsDirective(const OMPTeamsDirective &S);
2216   void
2217   EmitOMPCancellationPointDirective(const OMPCancellationPointDirective &S);
2218   void EmitOMPCancelDirective(const OMPCancelDirective &S);
2219 
2220   /// \brief Emit inner loop of the worksharing/simd construct.
2221   ///
2222   /// \param S Directive, for which the inner loop must be emitted.
2223   /// \param RequiresCleanup true, if directive has some associated private
2224   /// variables.
2225   /// \param LoopCond Bollean condition for loop continuation.
2226   /// \param IncExpr Increment expression for loop control variable.
2227   /// \param BodyGen Generator for the inner body of the inner loop.
2228   /// \param PostIncGen Genrator for post-increment code (required for ordered
2229   /// loop directvies).
2230   void EmitOMPInnerLoop(
2231       const Stmt &S, bool RequiresCleanup, const Expr *LoopCond,
2232       const Expr *IncExpr,
2233       const llvm::function_ref<void(CodeGenFunction &)> &BodyGen,
2234       const llvm::function_ref<void(CodeGenFunction &)> &PostIncGen);
2235 
2236   JumpDest getOMPCancelDestination(OpenMPDirectiveKind Kind);
2237 
2238 private:
2239 
2240   /// Helpers for the OpenMP loop directives.
2241   void EmitOMPLoopBody(const OMPLoopDirective &D, JumpDest LoopExit);
2242   void EmitOMPSimdInit(const OMPLoopDirective &D);
2243   void EmitOMPSimdFinal(const OMPLoopDirective &D);
2244   /// \brief Emit code for the worksharing loop-based directive.
2245   /// \return true, if this construct has any lastprivate clause, false -
2246   /// otherwise.
2247   bool EmitOMPWorksharingLoop(const OMPLoopDirective &S);
2248   void EmitOMPForOuterLoop(OpenMPScheduleClauseKind ScheduleKind,
2249                            const OMPLoopDirective &S,
2250                            OMPPrivateScope &LoopScope, bool Ordered,
2251                            llvm::Value *LB, llvm::Value *UB, llvm::Value *ST,
2252                            llvm::Value *IL, llvm::Value *Chunk);
2253   /// \brief Emit code for sections directive.
2254   OpenMPDirectiveKind EmitSections(const OMPExecutableDirective &S);
2255 
2256 public:
2257 
2258   //===--------------------------------------------------------------------===//
2259   //                         LValue Expression Emission
2260   //===--------------------------------------------------------------------===//
2261 
2262   /// GetUndefRValue - Get an appropriate 'undef' rvalue for the given type.
2263   RValue GetUndefRValue(QualType Ty);
2264 
2265   /// EmitUnsupportedRValue - Emit a dummy r-value using the type of E
2266   /// and issue an ErrorUnsupported style diagnostic (using the
2267   /// provided Name).
2268   RValue EmitUnsupportedRValue(const Expr *E,
2269                                const char *Name);
2270 
2271   /// EmitUnsupportedLValue - Emit a dummy l-value using the type of E and issue
2272   /// an ErrorUnsupported style diagnostic (using the provided Name).
2273   LValue EmitUnsupportedLValue(const Expr *E,
2274                                const char *Name);
2275 
2276   /// EmitLValue - Emit code to compute a designator that specifies the location
2277   /// of the expression.
2278   ///
2279   /// This can return one of two things: a simple address or a bitfield
2280   /// reference.  In either case, the LLVM Value* in the LValue structure is
2281   /// guaranteed to be an LLVM pointer type.
2282   ///
2283   /// If this returns a bitfield reference, nothing about the pointee type of
2284   /// the LLVM value is known: For example, it may not be a pointer to an
2285   /// integer.
2286   ///
2287   /// If this returns a normal address, and if the lvalue's C type is fixed
2288   /// size, this method guarantees that the returned pointer type will point to
2289   /// an LLVM type of the same size of the lvalue's type.  If the lvalue has a
2290   /// variable length type, this is not possible.
2291   ///
2292   LValue EmitLValue(const Expr *E);
2293 
2294   /// \brief Same as EmitLValue but additionally we generate checking code to
2295   /// guard against undefined behavior.  This is only suitable when we know
2296   /// that the address will be used to access the object.
2297   LValue EmitCheckedLValue(const Expr *E, TypeCheckKind TCK);
2298 
2299   RValue convertTempToRValue(llvm::Value *addr, QualType type,
2300                              SourceLocation Loc);
2301 
2302   void EmitAtomicInit(Expr *E, LValue lvalue);
2303 
2304   bool LValueIsSuitableForInlineAtomic(LValue Src);
2305   bool typeIsSuitableForInlineAtomic(QualType Ty, bool IsVolatile) const;
2306 
2307   RValue EmitAtomicLoad(LValue LV, SourceLocation SL,
2308                         AggValueSlot Slot = AggValueSlot::ignored());
2309 
2310   RValue EmitAtomicLoad(LValue lvalue, SourceLocation loc,
2311                         llvm::AtomicOrdering AO, bool IsVolatile = false,
2312                         AggValueSlot slot = AggValueSlot::ignored());
2313 
2314   void EmitAtomicStore(RValue rvalue, LValue lvalue, bool isInit);
2315 
2316   void EmitAtomicStore(RValue rvalue, LValue lvalue, llvm::AtomicOrdering AO,
2317                        bool IsVolatile, bool isInit);
2318 
2319   std::pair<RValue, llvm::Value *> EmitAtomicCompareExchange(
2320       LValue Obj, RValue Expected, RValue Desired, SourceLocation Loc,
2321       llvm::AtomicOrdering Success = llvm::SequentiallyConsistent,
2322       llvm::AtomicOrdering Failure = llvm::SequentiallyConsistent,
2323       bool IsWeak = false, AggValueSlot Slot = AggValueSlot::ignored());
2324 
2325   void EmitAtomicUpdate(LValue LVal, llvm::AtomicOrdering AO,
2326                         const llvm::function_ref<RValue(RValue)> &UpdateOp,
2327                         bool IsVolatile);
2328 
2329   /// EmitToMemory - Change a scalar value from its value
2330   /// representation to its in-memory representation.
2331   llvm::Value *EmitToMemory(llvm::Value *Value, QualType Ty);
2332 
2333   /// EmitFromMemory - Change a scalar value from its memory
2334   /// representation to its value representation.
2335   llvm::Value *EmitFromMemory(llvm::Value *Value, QualType Ty);
2336 
2337   /// EmitLoadOfScalar - Load a scalar value from an address, taking
2338   /// care to appropriately convert from the memory representation to
2339   /// the LLVM value representation.
2340   llvm::Value *EmitLoadOfScalar(llvm::Value *Addr, bool Volatile,
2341                                 unsigned Alignment, QualType Ty,
2342                                 SourceLocation Loc,
2343                                 llvm::MDNode *TBAAInfo = nullptr,
2344                                 QualType TBAABaseTy = QualType(),
2345                                 uint64_t TBAAOffset = 0);
2346 
2347   /// EmitLoadOfScalar - Load a scalar value from an address, taking
2348   /// care to appropriately convert from the memory representation to
2349   /// the LLVM value representation.  The l-value must be a simple
2350   /// l-value.
2351   llvm::Value *EmitLoadOfScalar(LValue lvalue, SourceLocation Loc);
2352 
2353   /// EmitStoreOfScalar - Store a scalar value to an address, taking
2354   /// care to appropriately convert from the memory representation to
2355   /// the LLVM value representation.
2356   void EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr,
2357                          bool Volatile, unsigned Alignment, QualType Ty,
2358                          llvm::MDNode *TBAAInfo = nullptr, bool isInit = false,
2359                          QualType TBAABaseTy = QualType(),
2360                          uint64_t TBAAOffset = 0);
2361 
2362   /// EmitStoreOfScalar - Store a scalar value to an address, taking
2363   /// care to appropriately convert from the memory representation to
2364   /// the LLVM value representation.  The l-value must be a simple
2365   /// l-value.  The isInit flag indicates whether this is an initialization.
2366   /// If so, atomic qualifiers are ignored and the store is always non-atomic.
2367   void EmitStoreOfScalar(llvm::Value *value, LValue lvalue, bool isInit=false);
2368 
2369   /// EmitLoadOfLValue - Given an expression that represents a value lvalue,
2370   /// this method emits the address of the lvalue, then loads the result as an
2371   /// rvalue, returning the rvalue.
2372   RValue EmitLoadOfLValue(LValue V, SourceLocation Loc);
2373   RValue EmitLoadOfExtVectorElementLValue(LValue V);
2374   RValue EmitLoadOfBitfieldLValue(LValue LV);
2375   RValue EmitLoadOfGlobalRegLValue(LValue LV);
2376 
2377   /// EmitStoreThroughLValue - Store the specified rvalue into the specified
2378   /// lvalue, where both are guaranteed to the have the same type, and that type
2379   /// is 'Ty'.
2380   void EmitStoreThroughLValue(RValue Src, LValue Dst, bool isInit = false);
2381   void EmitStoreThroughExtVectorComponentLValue(RValue Src, LValue Dst);
2382   void EmitStoreThroughGlobalRegLValue(RValue Src, LValue Dst);
2383 
2384   /// EmitStoreThroughBitfieldLValue - Store Src into Dst with same constraints
2385   /// as EmitStoreThroughLValue.
2386   ///
2387   /// \param Result [out] - If non-null, this will be set to a Value* for the
2388   /// bit-field contents after the store, appropriate for use as the result of
2389   /// an assignment to the bit-field.
2390   void EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst,
2391                                       llvm::Value **Result=nullptr);
2392 
2393   /// Emit an l-value for an assignment (simple or compound) of complex type.
2394   LValue EmitComplexAssignmentLValue(const BinaryOperator *E);
2395   LValue EmitComplexCompoundAssignmentLValue(const CompoundAssignOperator *E);
2396   LValue EmitScalarCompoundAssignWithComplex(const CompoundAssignOperator *E,
2397                                              llvm::Value *&Result);
2398 
2399   // Note: only available for agg return types
2400   LValue EmitBinaryOperatorLValue(const BinaryOperator *E);
2401   LValue EmitCompoundAssignmentLValue(const CompoundAssignOperator *E);
2402   // Note: only available for agg return types
2403   LValue EmitCallExprLValue(const CallExpr *E);
2404   // Note: only available for agg return types
2405   LValue EmitVAArgExprLValue(const VAArgExpr *E);
2406   LValue EmitDeclRefLValue(const DeclRefExpr *E);
2407   LValue EmitReadRegister(const VarDecl *VD);
2408   LValue EmitStringLiteralLValue(const StringLiteral *E);
2409   LValue EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E);
2410   LValue EmitPredefinedLValue(const PredefinedExpr *E);
2411   LValue EmitUnaryOpLValue(const UnaryOperator *E);
2412   LValue EmitArraySubscriptExpr(const ArraySubscriptExpr *E,
2413                                 bool Accessed = false);
2414   LValue EmitExtVectorElementExpr(const ExtVectorElementExpr *E);
2415   LValue EmitMemberExpr(const MemberExpr *E);
2416   LValue EmitObjCIsaExpr(const ObjCIsaExpr *E);
2417   LValue EmitCompoundLiteralLValue(const CompoundLiteralExpr *E);
2418   LValue EmitInitListLValue(const InitListExpr *E);
2419   LValue EmitConditionalOperatorLValue(const AbstractConditionalOperator *E);
2420   LValue EmitCastLValue(const CastExpr *E);
2421   LValue EmitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E);
2422   LValue EmitOpaqueValueLValue(const OpaqueValueExpr *e);
2423 
2424   llvm::Value *EmitExtVectorElementLValue(LValue V);
2425 
2426   RValue EmitRValueForField(LValue LV, const FieldDecl *FD, SourceLocation Loc);
2427 
2428   class ConstantEmission {
2429     llvm::PointerIntPair<llvm::Constant*, 1, bool> ValueAndIsReference;
2430     ConstantEmission(llvm::Constant *C, bool isReference)
2431       : ValueAndIsReference(C, isReference) {}
2432   public:
2433     ConstantEmission() {}
2434     static ConstantEmission forReference(llvm::Constant *C) {
2435       return ConstantEmission(C, true);
2436     }
2437     static ConstantEmission forValue(llvm::Constant *C) {
2438       return ConstantEmission(C, false);
2439     }
2440 
2441     explicit operator bool() const {
2442       return ValueAndIsReference.getOpaqueValue() != nullptr;
2443     }
2444 
2445     bool isReference() const { return ValueAndIsReference.getInt(); }
2446     LValue getReferenceLValue(CodeGenFunction &CGF, Expr *refExpr) const {
2447       assert(isReference());
2448       return CGF.MakeNaturalAlignAddrLValue(ValueAndIsReference.getPointer(),
2449                                             refExpr->getType());
2450     }
2451 
2452     llvm::Constant *getValue() const {
2453       assert(!isReference());
2454       return ValueAndIsReference.getPointer();
2455     }
2456   };
2457 
2458   ConstantEmission tryEmitAsConstant(DeclRefExpr *refExpr);
2459 
2460   RValue EmitPseudoObjectRValue(const PseudoObjectExpr *e,
2461                                 AggValueSlot slot = AggValueSlot::ignored());
2462   LValue EmitPseudoObjectLValue(const PseudoObjectExpr *e);
2463 
2464   llvm::Value *EmitIvarOffset(const ObjCInterfaceDecl *Interface,
2465                               const ObjCIvarDecl *Ivar);
2466   LValue EmitLValueForField(LValue Base, const FieldDecl* Field);
2467   LValue EmitLValueForLambdaField(const FieldDecl *Field);
2468 
2469   /// EmitLValueForFieldInitialization - Like EmitLValueForField, except that
2470   /// if the Field is a reference, this will return the address of the reference
2471   /// and not the address of the value stored in the reference.
2472   LValue EmitLValueForFieldInitialization(LValue Base,
2473                                           const FieldDecl* Field);
2474 
2475   LValue EmitLValueForIvar(QualType ObjectTy,
2476                            llvm::Value* Base, const ObjCIvarDecl *Ivar,
2477                            unsigned CVRQualifiers);
2478 
2479   LValue EmitCXXConstructLValue(const CXXConstructExpr *E);
2480   LValue EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E);
2481   LValue EmitLambdaLValue(const LambdaExpr *E);
2482   LValue EmitCXXTypeidLValue(const CXXTypeidExpr *E);
2483   LValue EmitCXXUuidofLValue(const CXXUuidofExpr *E);
2484 
2485   LValue EmitObjCMessageExprLValue(const ObjCMessageExpr *E);
2486   LValue EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E);
2487   LValue EmitStmtExprLValue(const StmtExpr *E);
2488   LValue EmitPointerToDataMemberBinaryExpr(const BinaryOperator *E);
2489   LValue EmitObjCSelectorLValue(const ObjCSelectorExpr *E);
2490   void   EmitDeclRefExprDbgValue(const DeclRefExpr *E, llvm::Constant *Init);
2491 
2492   //===--------------------------------------------------------------------===//
2493   //                         Scalar Expression Emission
2494   //===--------------------------------------------------------------------===//
2495 
2496   /// EmitCall - Generate a call of the given function, expecting the given
2497   /// result type, and using the given argument list which specifies both the
2498   /// LLVM arguments and the types they were derived from.
2499   ///
2500   /// \param TargetDecl - If given, the decl of the function in a direct call;
2501   /// used to set attributes on the call (noreturn, etc.).
2502   RValue EmitCall(const CGFunctionInfo &FnInfo,
2503                   llvm::Value *Callee,
2504                   ReturnValueSlot ReturnValue,
2505                   const CallArgList &Args,
2506                   const Decl *TargetDecl = nullptr,
2507                   llvm::Instruction **callOrInvoke = nullptr);
2508 
2509   RValue EmitCall(QualType FnType, llvm::Value *Callee, const CallExpr *E,
2510                   ReturnValueSlot ReturnValue,
2511                   const Decl *TargetDecl = nullptr,
2512                   llvm::Value *Chain = nullptr);
2513   RValue EmitCallExpr(const CallExpr *E,
2514                       ReturnValueSlot ReturnValue = ReturnValueSlot());
2515 
2516   llvm::CallInst *EmitRuntimeCall(llvm::Value *callee,
2517                                   const Twine &name = "");
2518   llvm::CallInst *EmitRuntimeCall(llvm::Value *callee,
2519                                   ArrayRef<llvm::Value*> args,
2520                                   const Twine &name = "");
2521   llvm::CallInst *EmitNounwindRuntimeCall(llvm::Value *callee,
2522                                           const Twine &name = "");
2523   llvm::CallInst *EmitNounwindRuntimeCall(llvm::Value *callee,
2524                                           ArrayRef<llvm::Value*> args,
2525                                           const Twine &name = "");
2526 
2527   llvm::CallSite EmitCallOrInvoke(llvm::Value *Callee,
2528                                   ArrayRef<llvm::Value *> Args,
2529                                   const Twine &Name = "");
2530   llvm::CallSite EmitCallOrInvoke(llvm::Value *Callee,
2531                                   const Twine &Name = "");
2532   llvm::CallSite EmitRuntimeCallOrInvoke(llvm::Value *callee,
2533                                          ArrayRef<llvm::Value*> args,
2534                                          const Twine &name = "");
2535   llvm::CallSite EmitRuntimeCallOrInvoke(llvm::Value *callee,
2536                                          const Twine &name = "");
2537   void EmitNoreturnRuntimeCallOrInvoke(llvm::Value *callee,
2538                                        ArrayRef<llvm::Value*> args);
2539 
2540   llvm::Value *BuildAppleKextVirtualCall(const CXXMethodDecl *MD,
2541                                          NestedNameSpecifier *Qual,
2542                                          llvm::Type *Ty);
2543 
2544   llvm::Value *BuildAppleKextVirtualDestructorCall(const CXXDestructorDecl *DD,
2545                                                    CXXDtorType Type,
2546                                                    const CXXRecordDecl *RD);
2547 
2548   RValue
2549   EmitCXXMemberOrOperatorCall(const CXXMethodDecl *MD, llvm::Value *Callee,
2550                               ReturnValueSlot ReturnValue, llvm::Value *This,
2551                               llvm::Value *ImplicitParam,
2552                               QualType ImplicitParamTy, const CallExpr *E);
2553   RValue EmitCXXStructorCall(const CXXMethodDecl *MD, llvm::Value *Callee,
2554                              ReturnValueSlot ReturnValue, llvm::Value *This,
2555                              llvm::Value *ImplicitParam,
2556                              QualType ImplicitParamTy, const CallExpr *E,
2557                              StructorType Type);
2558   RValue EmitCXXMemberCallExpr(const CXXMemberCallExpr *E,
2559                                ReturnValueSlot ReturnValue);
2560   RValue EmitCXXMemberOrOperatorMemberCallExpr(const CallExpr *CE,
2561                                                const CXXMethodDecl *MD,
2562                                                ReturnValueSlot ReturnValue,
2563                                                bool HasQualifier,
2564                                                NestedNameSpecifier *Qualifier,
2565                                                bool IsArrow, const Expr *Base);
2566   // Compute the object pointer.
2567   RValue EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E,
2568                                       ReturnValueSlot ReturnValue);
2569 
2570   RValue EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E,
2571                                        const CXXMethodDecl *MD,
2572                                        ReturnValueSlot ReturnValue);
2573 
2574   RValue EmitCUDAKernelCallExpr(const CUDAKernelCallExpr *E,
2575                                 ReturnValueSlot ReturnValue);
2576 
2577 
2578   RValue EmitBuiltinExpr(const FunctionDecl *FD,
2579                          unsigned BuiltinID, const CallExpr *E,
2580                          ReturnValueSlot ReturnValue);
2581 
2582   RValue EmitBlockCallExpr(const CallExpr *E, ReturnValueSlot ReturnValue);
2583 
2584   /// EmitTargetBuiltinExpr - Emit the given builtin call. Returns 0 if the call
2585   /// is unhandled by the current target.
2586   llvm::Value *EmitTargetBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
2587 
2588   llvm::Value *EmitAArch64CompareBuiltinExpr(llvm::Value *Op, llvm::Type *Ty,
2589                                              const llvm::CmpInst::Predicate Fp,
2590                                              const llvm::CmpInst::Predicate Ip,
2591                                              const llvm::Twine &Name = "");
2592   llvm::Value *EmitARMBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
2593 
2594   llvm::Value *EmitCommonNeonBuiltinExpr(unsigned BuiltinID,
2595                                          unsigned LLVMIntrinsic,
2596                                          unsigned AltLLVMIntrinsic,
2597                                          const char *NameHint,
2598                                          unsigned Modifier,
2599                                          const CallExpr *E,
2600                                          SmallVectorImpl<llvm::Value *> &Ops,
2601                                          llvm::Value *Align = nullptr);
2602   llvm::Function *LookupNeonLLVMIntrinsic(unsigned IntrinsicID,
2603                                           unsigned Modifier, llvm::Type *ArgTy,
2604                                           const CallExpr *E);
2605   llvm::Value *EmitNeonCall(llvm::Function *F,
2606                             SmallVectorImpl<llvm::Value*> &O,
2607                             const char *name,
2608                             unsigned shift = 0, bool rightshift = false);
2609   llvm::Value *EmitNeonSplat(llvm::Value *V, llvm::Constant *Idx);
2610   llvm::Value *EmitNeonShiftVector(llvm::Value *V, llvm::Type *Ty,
2611                                    bool negateForRightShift);
2612   llvm::Value *EmitNeonRShiftImm(llvm::Value *Vec, llvm::Value *Amt,
2613                                  llvm::Type *Ty, bool usgn, const char *name);
2614   // Helper functions for EmitAArch64BuiltinExpr.
2615   llvm::Value *vectorWrapScalar8(llvm::Value *Op);
2616   llvm::Value *vectorWrapScalar16(llvm::Value *Op);
2617   llvm::Value *EmitAArch64BuiltinExpr(unsigned BuiltinID, const CallExpr *E);
2618 
2619   llvm::Value *BuildVector(ArrayRef<llvm::Value*> Ops);
2620   llvm::Value *EmitX86BuiltinExpr(unsigned BuiltinID, const CallExpr *E);
2621   llvm::Value *EmitPPCBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
2622   llvm::Value *EmitAMDGPUBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
2623   llvm::Value *EmitSystemZBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
2624   llvm::Value *EmitNVPTXBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
2625 
2626   llvm::Value *EmitObjCProtocolExpr(const ObjCProtocolExpr *E);
2627   llvm::Value *EmitObjCStringLiteral(const ObjCStringLiteral *E);
2628   llvm::Value *EmitObjCBoxedExpr(const ObjCBoxedExpr *E);
2629   llvm::Value *EmitObjCArrayLiteral(const ObjCArrayLiteral *E);
2630   llvm::Value *EmitObjCDictionaryLiteral(const ObjCDictionaryLiteral *E);
2631   llvm::Value *EmitObjCCollectionLiteral(const Expr *E,
2632                                 const ObjCMethodDecl *MethodWithObjects);
2633   llvm::Value *EmitObjCSelectorExpr(const ObjCSelectorExpr *E);
2634   RValue EmitObjCMessageExpr(const ObjCMessageExpr *E,
2635                              ReturnValueSlot Return = ReturnValueSlot());
2636 
2637   /// Retrieves the default cleanup kind for an ARC cleanup.
2638   /// Except under -fobjc-arc-eh, ARC cleanups are normal-only.
2639   CleanupKind getARCCleanupKind() {
2640     return CGM.getCodeGenOpts().ObjCAutoRefCountExceptions
2641              ? NormalAndEHCleanup : NormalCleanup;
2642   }
2643 
2644   // ARC primitives.
2645   void EmitARCInitWeak(llvm::Value *value, llvm::Value *addr);
2646   void EmitARCDestroyWeak(llvm::Value *addr);
2647   llvm::Value *EmitARCLoadWeak(llvm::Value *addr);
2648   llvm::Value *EmitARCLoadWeakRetained(llvm::Value *addr);
2649   llvm::Value *EmitARCStoreWeak(llvm::Value *value, llvm::Value *addr,
2650                                 bool ignored);
2651   void EmitARCCopyWeak(llvm::Value *dst, llvm::Value *src);
2652   void EmitARCMoveWeak(llvm::Value *dst, llvm::Value *src);
2653   llvm::Value *EmitARCRetainAutorelease(QualType type, llvm::Value *value);
2654   llvm::Value *EmitARCRetainAutoreleaseNonBlock(llvm::Value *value);
2655   llvm::Value *EmitARCStoreStrong(LValue lvalue, llvm::Value *value,
2656                                   bool resultIgnored);
2657   llvm::Value *EmitARCStoreStrongCall(llvm::Value *addr, llvm::Value *value,
2658                                       bool resultIgnored);
2659   llvm::Value *EmitARCRetain(QualType type, llvm::Value *value);
2660   llvm::Value *EmitARCRetainNonBlock(llvm::Value *value);
2661   llvm::Value *EmitARCRetainBlock(llvm::Value *value, bool mandatory);
2662   void EmitARCDestroyStrong(llvm::Value *addr, ARCPreciseLifetime_t precise);
2663   void EmitARCRelease(llvm::Value *value, ARCPreciseLifetime_t precise);
2664   llvm::Value *EmitARCAutorelease(llvm::Value *value);
2665   llvm::Value *EmitARCAutoreleaseReturnValue(llvm::Value *value);
2666   llvm::Value *EmitARCRetainAutoreleaseReturnValue(llvm::Value *value);
2667   llvm::Value *EmitARCRetainAutoreleasedReturnValue(llvm::Value *value);
2668 
2669   std::pair<LValue,llvm::Value*>
2670   EmitARCStoreAutoreleasing(const BinaryOperator *e);
2671   std::pair<LValue,llvm::Value*>
2672   EmitARCStoreStrong(const BinaryOperator *e, bool ignored);
2673 
2674   llvm::Value *EmitObjCThrowOperand(const Expr *expr);
2675 
2676   llvm::Value *EmitObjCProduceObject(QualType T, llvm::Value *Ptr);
2677   llvm::Value *EmitObjCConsumeObject(QualType T, llvm::Value *Ptr);
2678   llvm::Value *EmitObjCExtendObjectLifetime(QualType T, llvm::Value *Ptr);
2679 
2680   llvm::Value *EmitARCExtendBlockObject(const Expr *expr);
2681   llvm::Value *EmitARCRetainScalarExpr(const Expr *expr);
2682   llvm::Value *EmitARCRetainAutoreleaseScalarExpr(const Expr *expr);
2683 
2684   void EmitARCIntrinsicUse(ArrayRef<llvm::Value*> values);
2685 
2686   static Destroyer destroyARCStrongImprecise;
2687   static Destroyer destroyARCStrongPrecise;
2688   static Destroyer destroyARCWeak;
2689 
2690   void EmitObjCAutoreleasePoolPop(llvm::Value *Ptr);
2691   llvm::Value *EmitObjCAutoreleasePoolPush();
2692   llvm::Value *EmitObjCMRRAutoreleasePoolPush();
2693   void EmitObjCAutoreleasePoolCleanup(llvm::Value *Ptr);
2694   void EmitObjCMRRAutoreleasePoolPop(llvm::Value *Ptr);
2695 
2696   /// \brief Emits a reference binding to the passed in expression.
2697   RValue EmitReferenceBindingToExpr(const Expr *E);
2698 
2699   //===--------------------------------------------------------------------===//
2700   //                           Expression Emission
2701   //===--------------------------------------------------------------------===//
2702 
2703   // Expressions are broken into three classes: scalar, complex, aggregate.
2704 
2705   /// EmitScalarExpr - Emit the computation of the specified expression of LLVM
2706   /// scalar type, returning the result.
2707   llvm::Value *EmitScalarExpr(const Expr *E , bool IgnoreResultAssign = false);
2708 
2709   /// EmitScalarConversion - Emit a conversion from the specified type to the
2710   /// specified destination type, both of which are LLVM scalar types.
2711   llvm::Value *EmitScalarConversion(llvm::Value *Src, QualType SrcTy,
2712                                     QualType DstTy);
2713 
2714   /// EmitComplexToScalarConversion - Emit a conversion from the specified
2715   /// complex type to the specified destination type, where the destination type
2716   /// is an LLVM scalar type.
2717   llvm::Value *EmitComplexToScalarConversion(ComplexPairTy Src, QualType SrcTy,
2718                                              QualType DstTy);
2719 
2720 
2721   /// EmitAggExpr - Emit the computation of the specified expression
2722   /// of aggregate type.  The result is computed into the given slot,
2723   /// which may be null to indicate that the value is not needed.
2724   void EmitAggExpr(const Expr *E, AggValueSlot AS);
2725 
2726   /// EmitAggExprToLValue - Emit the computation of the specified expression of
2727   /// aggregate type into a temporary LValue.
2728   LValue EmitAggExprToLValue(const Expr *E);
2729 
2730   /// EmitGCMemmoveCollectable - Emit special API for structs with object
2731   /// pointers.
2732   void EmitGCMemmoveCollectable(llvm::Value *DestPtr, llvm::Value *SrcPtr,
2733                                 QualType Ty);
2734 
2735   /// EmitExtendGCLifetime - Given a pointer to an Objective-C object,
2736   /// make sure it survives garbage collection until this point.
2737   void EmitExtendGCLifetime(llvm::Value *object);
2738 
2739   /// EmitComplexExpr - Emit the computation of the specified expression of
2740   /// complex type, returning the result.
2741   ComplexPairTy EmitComplexExpr(const Expr *E,
2742                                 bool IgnoreReal = false,
2743                                 bool IgnoreImag = false);
2744 
2745   /// EmitComplexExprIntoLValue - Emit the given expression of complex
2746   /// type and place its result into the specified l-value.
2747   void EmitComplexExprIntoLValue(const Expr *E, LValue dest, bool isInit);
2748 
2749   /// EmitStoreOfComplex - Store a complex number into the specified l-value.
2750   void EmitStoreOfComplex(ComplexPairTy V, LValue dest, bool isInit);
2751 
2752   /// EmitLoadOfComplex - Load a complex number from the specified l-value.
2753   ComplexPairTy EmitLoadOfComplex(LValue src, SourceLocation loc);
2754 
2755   /// AddInitializerToStaticVarDecl - Add the initializer for 'D' to the
2756   /// global variable that has already been created for it.  If the initializer
2757   /// has a different type than GV does, this may free GV and return a different
2758   /// one.  Otherwise it just returns GV.
2759   llvm::GlobalVariable *
2760   AddInitializerToStaticVarDecl(const VarDecl &D,
2761                                 llvm::GlobalVariable *GV);
2762 
2763 
2764   /// EmitCXXGlobalVarDeclInit - Create the initializer for a C++
2765   /// variable with global storage.
2766   void EmitCXXGlobalVarDeclInit(const VarDecl &D, llvm::Constant *DeclPtr,
2767                                 bool PerformInit);
2768 
2769   llvm::Constant *createAtExitStub(const VarDecl &VD, llvm::Constant *Dtor,
2770                                    llvm::Constant *Addr);
2771 
2772   /// Call atexit() with a function that passes the given argument to
2773   /// the given function.
2774   void registerGlobalDtorWithAtExit(const VarDecl &D, llvm::Constant *fn,
2775                                     llvm::Constant *addr);
2776 
2777   /// Emit code in this function to perform a guarded variable
2778   /// initialization.  Guarded initializations are used when it's not
2779   /// possible to prove that an initialization will be done exactly
2780   /// once, e.g. with a static local variable or a static data member
2781   /// of a class template.
2782   void EmitCXXGuardedInit(const VarDecl &D, llvm::GlobalVariable *DeclPtr,
2783                           bool PerformInit);
2784 
2785   /// GenerateCXXGlobalInitFunc - Generates code for initializing global
2786   /// variables.
2787   void GenerateCXXGlobalInitFunc(llvm::Function *Fn,
2788                                  ArrayRef<llvm::Function *> CXXThreadLocals,
2789                                  llvm::GlobalVariable *Guard = nullptr);
2790 
2791   /// GenerateCXXGlobalDtorsFunc - Generates code for destroying global
2792   /// variables.
2793   void GenerateCXXGlobalDtorsFunc(llvm::Function *Fn,
2794                                   const std::vector<std::pair<llvm::WeakVH,
2795                                   llvm::Constant*> > &DtorsAndObjects);
2796 
2797   void GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn,
2798                                         const VarDecl *D,
2799                                         llvm::GlobalVariable *Addr,
2800                                         bool PerformInit);
2801 
2802   void EmitCXXConstructExpr(const CXXConstructExpr *E, AggValueSlot Dest);
2803 
2804   void EmitSynthesizedCXXCopyCtor(llvm::Value *Dest, llvm::Value *Src,
2805                                   const Expr *Exp);
2806 
2807   void enterFullExpression(const ExprWithCleanups *E) {
2808     if (E->getNumObjects() == 0) return;
2809     enterNonTrivialFullExpression(E);
2810   }
2811   void enterNonTrivialFullExpression(const ExprWithCleanups *E);
2812 
2813   void EmitCXXThrowExpr(const CXXThrowExpr *E, bool KeepInsertionPoint = true);
2814 
2815   void EmitLambdaExpr(const LambdaExpr *E, AggValueSlot Dest);
2816 
2817   RValue EmitAtomicExpr(AtomicExpr *E, llvm::Value *Dest = nullptr);
2818 
2819   //===--------------------------------------------------------------------===//
2820   //                         Annotations Emission
2821   //===--------------------------------------------------------------------===//
2822 
2823   /// Emit an annotation call (intrinsic or builtin).
2824   llvm::Value *EmitAnnotationCall(llvm::Value *AnnotationFn,
2825                                   llvm::Value *AnnotatedVal,
2826                                   StringRef AnnotationStr,
2827                                   SourceLocation Location);
2828 
2829   /// Emit local annotations for the local variable V, declared by D.
2830   void EmitVarAnnotations(const VarDecl *D, llvm::Value *V);
2831 
2832   /// Emit field annotations for the given field & value. Returns the
2833   /// annotation result.
2834   llvm::Value *EmitFieldAnnotations(const FieldDecl *D, llvm::Value *V);
2835 
2836   //===--------------------------------------------------------------------===//
2837   //                             Internal Helpers
2838   //===--------------------------------------------------------------------===//
2839 
2840   /// ContainsLabel - Return true if the statement contains a label in it.  If
2841   /// this statement is not executed normally, it not containing a label means
2842   /// that we can just remove the code.
2843   static bool ContainsLabel(const Stmt *S, bool IgnoreCaseStmts = false);
2844 
2845   /// containsBreak - Return true if the statement contains a break out of it.
2846   /// If the statement (recursively) contains a switch or loop with a break
2847   /// inside of it, this is fine.
2848   static bool containsBreak(const Stmt *S);
2849 
2850   /// ConstantFoldsToSimpleInteger - If the specified expression does not fold
2851   /// to a constant, or if it does but contains a label, return false.  If it
2852   /// constant folds return true and set the boolean result in Result.
2853   bool ConstantFoldsToSimpleInteger(const Expr *Cond, bool &Result);
2854 
2855   /// ConstantFoldsToSimpleInteger - If the specified expression does not fold
2856   /// to a constant, or if it does but contains a label, return false.  If it
2857   /// constant folds return true and set the folded value.
2858   bool ConstantFoldsToSimpleInteger(const Expr *Cond, llvm::APSInt &Result);
2859 
2860   /// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an
2861   /// if statement) to the specified blocks.  Based on the condition, this might
2862   /// try to simplify the codegen of the conditional based on the branch.
2863   /// TrueCount should be the number of times we expect the condition to
2864   /// evaluate to true based on PGO data.
2865   void EmitBranchOnBoolExpr(const Expr *Cond, llvm::BasicBlock *TrueBlock,
2866                             llvm::BasicBlock *FalseBlock, uint64_t TrueCount);
2867 
2868   /// \brief Emit a description of a type in a format suitable for passing to
2869   /// a runtime sanitizer handler.
2870   llvm::Constant *EmitCheckTypeDescriptor(QualType T);
2871 
2872   /// \brief Convert a value into a format suitable for passing to a runtime
2873   /// sanitizer handler.
2874   llvm::Value *EmitCheckValue(llvm::Value *V);
2875 
2876   /// \brief Emit a description of a source location in a format suitable for
2877   /// passing to a runtime sanitizer handler.
2878   llvm::Constant *EmitCheckSourceLocation(SourceLocation Loc);
2879 
2880   /// \brief Create a basic block that will call a handler function in a
2881   /// sanitizer runtime with the provided arguments, and create a conditional
2882   /// branch to it.
2883   void EmitCheck(ArrayRef<std::pair<llvm::Value *, SanitizerMask>> Checked,
2884                  StringRef CheckName, ArrayRef<llvm::Constant *> StaticArgs,
2885                  ArrayRef<llvm::Value *> DynamicArgs);
2886 
2887   /// \brief Create a basic block that will call the trap intrinsic, and emit a
2888   /// conditional branch to it, for the -ftrapv checks.
2889   void EmitTrapCheck(llvm::Value *Checked);
2890 
2891   /// \brief Emit a call to trap or debugtrap and attach function attribute
2892   /// "trap-func-name" if specified.
2893   llvm::CallInst *EmitTrapCall(llvm::Intrinsic::ID IntrID);
2894 
2895   /// \brief Create a check for a function parameter that may potentially be
2896   /// declared as non-null.
2897   void EmitNonNullArgCheck(RValue RV, QualType ArgType, SourceLocation ArgLoc,
2898                            const FunctionDecl *FD, unsigned ParmNum);
2899 
2900   /// EmitCallArg - Emit a single call argument.
2901   void EmitCallArg(CallArgList &args, const Expr *E, QualType ArgType);
2902 
2903   /// EmitDelegateCallArg - We are performing a delegate call; that
2904   /// is, the current function is delegating to another one.  Produce
2905   /// a r-value suitable for passing the given parameter.
2906   void EmitDelegateCallArg(CallArgList &args, const VarDecl *param,
2907                            SourceLocation loc);
2908 
2909   /// SetFPAccuracy - Set the minimum required accuracy of the given floating
2910   /// point operation, expressed as the maximum relative error in ulp.
2911   void SetFPAccuracy(llvm::Value *Val, float Accuracy);
2912 
2913 private:
2914   llvm::MDNode *getRangeForLoadFromType(QualType Ty);
2915   void EmitReturnOfRValue(RValue RV, QualType Ty);
2916 
2917   void deferPlaceholderReplacement(llvm::Instruction *Old, llvm::Value *New);
2918 
2919   llvm::SmallVector<std::pair<llvm::Instruction *, llvm::Value *>, 4>
2920   DeferredReplacements;
2921 
2922   /// ExpandTypeFromArgs - Reconstruct a structure of type \arg Ty
2923   /// from function arguments into \arg Dst. See ABIArgInfo::Expand.
2924   ///
2925   /// \param AI - The first function argument of the expansion.
2926   void ExpandTypeFromArgs(QualType Ty, LValue Dst,
2927                           SmallVectorImpl<llvm::Argument *>::iterator &AI);
2928 
2929   /// ExpandTypeToArgs - Expand an RValue \arg RV, with the LLVM type for \arg
2930   /// Ty, into individual arguments on the provided vector \arg IRCallArgs,
2931   /// starting at index \arg IRCallArgPos. See ABIArgInfo::Expand.
2932   void ExpandTypeToArgs(QualType Ty, RValue RV, llvm::FunctionType *IRFuncTy,
2933                         SmallVectorImpl<llvm::Value *> &IRCallArgs,
2934                         unsigned &IRCallArgPos);
2935 
2936   llvm::Value* EmitAsmInput(const TargetInfo::ConstraintInfo &Info,
2937                             const Expr *InputExpr, std::string &ConstraintStr);
2938 
2939   llvm::Value* EmitAsmInputLValue(const TargetInfo::ConstraintInfo &Info,
2940                                   LValue InputValue, QualType InputType,
2941                                   std::string &ConstraintStr,
2942                                   SourceLocation Loc);
2943 
2944 public:
2945 #ifndef NDEBUG
2946   // Determine whether the given argument is an Objective-C method
2947   // that may have type parameters in its signature.
2948   static bool isObjCMethodWithTypeParams(const ObjCMethodDecl *method) {
2949     const DeclContext *dc = method->getDeclContext();
2950     if (const ObjCInterfaceDecl *classDecl= dyn_cast<ObjCInterfaceDecl>(dc)) {
2951       return classDecl->getTypeParamListAsWritten();
2952     }
2953 
2954     if (const ObjCCategoryDecl *catDecl = dyn_cast<ObjCCategoryDecl>(dc)) {
2955       return catDecl->getTypeParamList();
2956     }
2957 
2958     return false;
2959   }
2960 
2961   template<typename T>
2962   static bool isObjCMethodWithTypeParams(const T *) { return false; }
2963 #endif
2964 
2965   /// EmitCallArgs - Emit call arguments for a function.
2966   template <typename T>
2967   void EmitCallArgs(CallArgList &Args, const T *CallArgTypeInfo,
2968                     llvm::iterator_range<CallExpr::const_arg_iterator> ArgRange,
2969                     const FunctionDecl *CalleeDecl = nullptr,
2970                     unsigned ParamsToSkip = 0) {
2971     SmallVector<QualType, 16> ArgTypes;
2972     CallExpr::const_arg_iterator Arg = ArgRange.begin();
2973 
2974     assert((ParamsToSkip == 0 || CallArgTypeInfo) &&
2975            "Can't skip parameters if type info is not provided");
2976     if (CallArgTypeInfo) {
2977 #ifndef NDEBUG
2978       bool isGenericMethod = isObjCMethodWithTypeParams(CallArgTypeInfo);
2979 #endif
2980 
2981       // First, use the argument types that the type info knows about
2982       for (auto I = CallArgTypeInfo->param_type_begin() + ParamsToSkip,
2983                 E = CallArgTypeInfo->param_type_end();
2984            I != E; ++I, ++Arg) {
2985         assert(Arg != ArgRange.end() && "Running over edge of argument list!");
2986         assert((isGenericMethod ||
2987                 ((*I)->isVariablyModifiedType() ||
2988                  (*I).getNonReferenceType()->isObjCRetainableType() ||
2989                  getContext()
2990                          .getCanonicalType((*I).getNonReferenceType())
2991                          .getTypePtr() ==
2992                      getContext()
2993                          .getCanonicalType((*Arg)->getType())
2994                          .getTypePtr())) &&
2995                "type mismatch in call argument!");
2996         ArgTypes.push_back(*I);
2997       }
2998     }
2999 
3000     // Either we've emitted all the call args, or we have a call to variadic
3001     // function.
3002     assert((Arg == ArgRange.end() || !CallArgTypeInfo ||
3003             CallArgTypeInfo->isVariadic()) &&
3004            "Extra arguments in non-variadic function!");
3005 
3006     // If we still have any arguments, emit them using the type of the argument.
3007     for (auto *A : llvm::make_range(Arg, ArgRange.end()))
3008       ArgTypes.push_back(getVarArgType(A));
3009 
3010     EmitCallArgs(Args, ArgTypes, ArgRange, CalleeDecl, ParamsToSkip);
3011   }
3012 
3013   void EmitCallArgs(CallArgList &Args, ArrayRef<QualType> ArgTypes,
3014                     llvm::iterator_range<CallExpr::const_arg_iterator> ArgRange,
3015                     const FunctionDecl *CalleeDecl = nullptr,
3016                     unsigned ParamsToSkip = 0);
3017 
3018 private:
3019   QualType getVarArgType(const Expr *Arg);
3020 
3021   const TargetCodeGenInfo &getTargetHooks() const {
3022     return CGM.getTargetCodeGenInfo();
3023   }
3024 
3025   void EmitDeclMetadata();
3026 
3027   CodeGenModule::ByrefHelpers *
3028   buildByrefHelpers(llvm::StructType &byrefType,
3029                     const AutoVarEmission &emission);
3030 
3031   void AddObjCARCExceptionMetadata(llvm::Instruction *Inst);
3032 
3033   /// GetPointeeAlignment - Given an expression with a pointer type, emit the
3034   /// value and compute our best estimate of the alignment of the pointee.
3035   std::pair<llvm::Value*, unsigned> EmitPointerWithAlignment(const Expr *Addr);
3036 
3037   llvm::Value *GetValueForARMHint(unsigned BuiltinID);
3038 };
3039 
3040 /// Helper class with most of the code for saving a value for a
3041 /// conditional expression cleanup.
3042 struct DominatingLLVMValue {
3043   typedef llvm::PointerIntPair<llvm::Value*, 1, bool> saved_type;
3044 
3045   /// Answer whether the given value needs extra work to be saved.
3046   static bool needsSaving(llvm::Value *value) {
3047     // If it's not an instruction, we don't need to save.
3048     if (!isa<llvm::Instruction>(value)) return false;
3049 
3050     // If it's an instruction in the entry block, we don't need to save.
3051     llvm::BasicBlock *block = cast<llvm::Instruction>(value)->getParent();
3052     return (block != &block->getParent()->getEntryBlock());
3053   }
3054 
3055   /// Try to save the given value.
3056   static saved_type save(CodeGenFunction &CGF, llvm::Value *value) {
3057     if (!needsSaving(value)) return saved_type(value, false);
3058 
3059     // Otherwise we need an alloca.
3060     llvm::Value *alloca =
3061       CGF.CreateTempAlloca(value->getType(), "cond-cleanup.save");
3062     CGF.Builder.CreateStore(value, alloca);
3063 
3064     return saved_type(alloca, true);
3065   }
3066 
3067   static llvm::Value *restore(CodeGenFunction &CGF, saved_type value) {
3068     if (!value.getInt()) return value.getPointer();
3069     return CGF.Builder.CreateLoad(value.getPointer());
3070   }
3071 };
3072 
3073 /// A partial specialization of DominatingValue for llvm::Values that
3074 /// might be llvm::Instructions.
3075 template <class T> struct DominatingPointer<T,true> : DominatingLLVMValue {
3076   typedef T *type;
3077   static type restore(CodeGenFunction &CGF, saved_type value) {
3078     return static_cast<T*>(DominatingLLVMValue::restore(CGF, value));
3079   }
3080 };
3081 
3082 /// A specialization of DominatingValue for RValue.
3083 template <> struct DominatingValue<RValue> {
3084   typedef RValue type;
3085   class saved_type {
3086     enum Kind { ScalarLiteral, ScalarAddress, AggregateLiteral,
3087                 AggregateAddress, ComplexAddress };
3088 
3089     llvm::Value *Value;
3090     Kind K;
3091     saved_type(llvm::Value *v, Kind k) : Value(v), K(k) {}
3092 
3093   public:
3094     static bool needsSaving(RValue value);
3095     static saved_type save(CodeGenFunction &CGF, RValue value);
3096     RValue restore(CodeGenFunction &CGF);
3097 
3098     // implementations in CGExprCXX.cpp
3099   };
3100 
3101   static bool needsSaving(type value) {
3102     return saved_type::needsSaving(value);
3103   }
3104   static saved_type save(CodeGenFunction &CGF, type value) {
3105     return saved_type::save(CGF, value);
3106   }
3107   static type restore(CodeGenFunction &CGF, saved_type value) {
3108     return value.restore(CGF);
3109   }
3110 };
3111 
3112 }  // end namespace CodeGen
3113 }  // end namespace clang
3114 
3115 #endif
3116