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