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