CodeGenFunction.h revision 3ea9e33ea25e0c2b12db56418ba3f994eb662c04
1bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar//===-- CodeGenFunction.h - Per-Function state for LLVM CodeGen -*- C++ -*-===//
25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//                     The LLVM Compiler Infrastructure
45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details.
75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
100dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump// This is the internal per-function state used for llvm translation.
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines#ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENFUNCTION_H
15176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines#define LLVM_CLANG_LIB_CODEGEN_CODEGENFUNCTION_H
165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1755fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "CGBuilder.h"
1855fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "CGDebugInfo.h"
196bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#include "CGLoopInfo.h"
2055fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "CGValue.h"
2155fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "CodeGenModule.h"
22651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#include "CodeGenPGO.h"
23651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#include "EHScopeStack.h"
2455fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/AST/CharUnits.h"
25e3a09e6ad09f8d1387ecaa008aaf85527909da0aArgyrios Kyrtzidis#include "clang/AST/ExprCXX.h"
265549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek#include "clang/AST/ExprObjC.h"
2755fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/AST/Type.h"
2814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne#include "clang/Basic/ABI.h"
29524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir#include "clang/Basic/CapturedStmt.h"
300e2c34f92f00628d48968dfea096d36381f494cbStephen Hines#include "clang/Basic/OpenMPKinds.h"
31481769b5dc102b0256b35581e787909ad5edfab5Chris Lattner#include "clang/Basic/TargetInfo.h"
3255fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Frontend/CodeGenOptions.h"
334c7d9f1507d0f102bd4133bba63348636facd469Jay Foad#include "llvm/ADT/ArrayRef.h"
34481769b5dc102b0256b35581e787909ad5edfab5Chris Lattner#include "llvm/ADT/DenseMap.h"
35481769b5dc102b0256b35581e787909ad5edfab5Chris Lattner#include "llvm/ADT/SmallVector.h"
36651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#include "llvm/IR/ValueHandle.h"
37c3287793626a778178a4dbadcc7b64e392c6e26fEric Christopher#include "llvm/Support/Debug.h"
388f2926b73ed635afecd020da787af6a837601a2bDaniel Dunbar
395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencernamespace llvm {
406bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesclass BasicBlock;
416bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesclass LLVMContext;
426bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesclass MDNode;
436bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesclass Module;
446bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesclass SwitchInst;
456bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesclass Twine;
466bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesclass Value;
476bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesclass CallSite;
485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencernamespace clang {
516bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesclass ASTContext;
526bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesclass BlockDecl;
536bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesclass CXXDestructorDecl;
546bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesclass CXXForRangeStmt;
556bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesclass CXXTryStmt;
566bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesclass Decl;
576bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesclass LabelDecl;
586bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesclass EnumConstantDecl;
596bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesclass FunctionDecl;
606bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesclass FunctionProtoType;
616bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesclass LabelStmt;
626bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesclass ObjCContainerDecl;
636bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesclass ObjCInterfaceDecl;
646bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesclass ObjCIvarDecl;
656bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesclass ObjCMethodDecl;
666bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesclass ObjCImplementationDecl;
676bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesclass ObjCPropertyImplDecl;
686bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesclass TargetInfo;
696bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesclass TargetCodeGenInfo;
706bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesclass VarDecl;
716bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesclass ObjCForCollectionStmt;
726bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesclass ObjCAtTryStmt;
736bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesclass ObjCAtThrowStmt;
746bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesclass ObjCAtSynchronizedStmt;
756bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesclass ObjCAutoreleasePoolStmt;
765549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek
775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencernamespace CodeGen {
786bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesclass CodeGenTypes;
796bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesclass CGFunctionInfo;
806bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesclass CGRecordLayout;
816bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesclass CGBlockInfo;
826bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesclass CGCXXABI;
836bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesclass BlockFlags;
846bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesclass BlockFieldFlags;
850dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
869d232c884ea9872d6555df0fd7359699819bc1f1John McCall/// The kind of evaluation to perform on values of a particular
879d232c884ea9872d6555df0fd7359699819bc1f1John McCall/// type.  Basically, is the code in CGExprScalar, CGExprComplex, or
889d232c884ea9872d6555df0fd7359699819bc1f1John McCall/// CGExprAgg?
899d232c884ea9872d6555df0fd7359699819bc1f1John McCall///
909d232c884ea9872d6555df0fd7359699819bc1f1John McCall/// TODO: should vectors maybe be split out into their own thing?
919d232c884ea9872d6555df0fd7359699819bc1f1John McCallenum TypeEvaluationKind {
929d232c884ea9872d6555df0fd7359699819bc1f1John McCall  TEK_Scalar,
939d232c884ea9872d6555df0fd7359699819bc1f1John McCall  TEK_Complex,
949d232c884ea9872d6555df0fd7359699819bc1f1John McCall  TEK_Aggregate
959d232c884ea9872d6555df0fd7359699819bc1f1John McCall};
969d232c884ea9872d6555df0fd7359699819bc1f1John McCall
975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// CodeGenFunction - This class organizes the per-function state that is used
985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// while generating LLVM code.
995936e33bf74dd6bf126ceee0f6169a2593d03a69John McCallclass CodeGenFunction : public CodeGenTypeCache {
1000e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  CodeGenFunction(const CodeGenFunction &) = delete;
1010e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  void operator=(const CodeGenFunction &) = delete;
1024c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
1034c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  friend class CGCXXABI;
104bfc0c1ae4b505b52b36f572a9641f99e76879cabChris Lattnerpublic:
105ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  /// A jump destination is an abstract label, branching to which may
106ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  /// require a jump out through normal cleanups.
107f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  struct JumpDest {
1086bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    JumpDest() : Block(nullptr), ScopeDepth(), Index(0) {}
109ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    JumpDest(llvm::BasicBlock *Block,
110ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall             EHScopeStack::stable_iterator Depth,
111ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall             unsigned Index)
112ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      : Block(Block), ScopeDepth(Depth), Index(Index) {}
113ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
1146bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    bool isValid() const { return Block != nullptr; }
115ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    llvm::BasicBlock *getBlock() const { return Block; }
116ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    EHScopeStack::stable_iterator getScopeDepth() const { return ScopeDepth; }
117ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    unsigned getDestIndex() const { return Index; }
1189cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
119495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem    // This should be used cautiously.
120495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem    void setScopeDepth(EHScopeStack::stable_iterator depth) {
121495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem      ScopeDepth = depth;
122495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem    }
123495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem
124ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  private:
125f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    llvm::BasicBlock *Block;
126f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    EHScopeStack::stable_iterator ScopeDepth;
127ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    unsigned Index;
128ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  };
129ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
1305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  CodeGenModule &CGM;  // Per-module state.
131444be7366d0a1e172c0290a1ea54c1cb16b5947cDaniel Dunbar  const TargetInfo &Target;
1320dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
13358dee10ed2eee34035f62d1c2d32b3639e9182f8Chris Lattner  typedef std::pair<llvm::Value *, llvm::Value *> ComplexPairTy;
1346bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  LoopInfoStack LoopStack;
13545d196b8387dcefc4df26cda114fa34c6528e928Daniel Dunbar  CGBuilderTy Builder;
1360dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
1376bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  /// \brief CGBuilder insert helper. This function is called after an
1386bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  /// instruction is created using Builder.
1396bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  void InsertHelper(llvm::Instruction *I, const llvm::Twine &Name,
1406bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                    llvm::BasicBlock *BB,
1416bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                    llvm::BasicBlock::iterator InsertPt) const;
1426bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
143f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall  /// CurFuncDecl - Holds the Decl for the current outermost
144f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall  /// non-closure context.
1454111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner  const Decl *CurFuncDecl;
146b5437d238752dc297e42410e98d38d5250fe0463Chris Lattner  /// CurCodeDecl - This is the inner-most code context, which includes blocks.
147b5437d238752dc297e42410e98d38d5250fe0463Chris Lattner  const Decl *CurCodeDecl;
14888b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar  const CGFunctionInfo *CurFnInfo;
149391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  QualType FnRetTy;
1505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::Function *CurFn;
1515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1526a1e0eb557d47e85185e09bdf8721f53f4bf9c9cMike Stump  /// CurGD - The GlobalDecl for the current function being compiled.
1536a1e0eb557d47e85185e09bdf8721f53f4bf9c9cMike Stump  GlobalDecl CurGD;
1546a1e0eb557d47e85185e09bdf8721f53f4bf9c9cMike Stump
155f85e193739c953358c865005855253af4f68a497John McCall  /// PrologueCleanupDepth - The cleanup depth enclosing all the
156f85e193739c953358c865005855253af4f68a497John McCall  /// cleanups associated with the parameters.
157f85e193739c953358c865005855253af4f68a497John McCall  EHScopeStack::stable_iterator PrologueCleanupDepth;
158f85e193739c953358c865005855253af4f68a497John McCall
1595ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar  /// ReturnBlock - Unified return block.
160f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  JumpDest ReturnBlock;
161f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1620dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// ReturnValue - The temporary alloca to hold the return value. This is null
163f3477c13eeaf11b32a41f181398fb5deffd0dd73Sylvestre Ledru  /// iff the function has no return value.
164b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman  llvm::Value *ReturnValue;
1650dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
1665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// AllocaInsertPoint - This is an instruction in the entry block before which
1675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// we prefer to insert allocas.
168481769b5dc102b0256b35581e787909ad5edfab5Chris Lattner  llvm::AssertingVH<llvm::Instruction> AllocaInsertPt;
1690ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar
170524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir  /// \brief API for captured statement code generation.
171524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir  class CGCapturedStmtInfo {
172524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir  public:
1730e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    explicit CGCapturedStmtInfo(CapturedRegionKind K = CR_Default)
1740e2c34f92f00628d48968dfea096d36381f494cbStephen Hines        : Kind(K), ThisValue(nullptr), CXXThisFieldDecl(nullptr) {}
175524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir    explicit CGCapturedStmtInfo(const CapturedStmt &S,
176524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir                                CapturedRegionKind K = CR_Default)
1776bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      : Kind(K), ThisValue(nullptr), CXXThisFieldDecl(nullptr) {
178524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir
179524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir      RecordDecl::field_iterator Field =
180524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir        S.getCapturedRecordDecl()->field_begin();
181524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir      for (CapturedStmt::const_capture_iterator I = S.capture_begin(),
182524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir                                                E = S.capture_end();
183524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir           I != E; ++I, ++Field) {
184524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir        if (I->capturesThis())
185524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir          CXXThisFieldDecl = *Field;
186176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        else if (I->capturesVariable())
187524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir          CaptureFields[I->getCapturedVar()] = *Field;
188524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir      }
189524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir    }
190524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir
191524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir    virtual ~CGCapturedStmtInfo();
192524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir
193524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir    CapturedRegionKind getKind() const { return Kind; }
194524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir
195524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir    void setContextValue(llvm::Value *V) { ThisValue = V; }
196524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir    // \brief Retrieve the value of the context parameter.
1970e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    virtual llvm::Value *getContextValue() const { return ThisValue; }
198524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir
199524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir    /// \brief Lookup the captured field decl for a variable.
2000e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    virtual const FieldDecl *lookup(const VarDecl *VD) const {
201524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir      return CaptureFields.lookup(VD);
202524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir    }
203524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir
2040e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    bool isCXXThisExprCaptured() const { return getThisFieldDecl() != nullptr; }
2050e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    virtual FieldDecl *getThisFieldDecl() const { return CXXThisFieldDecl; }
206524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir
207176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    static bool classof(const CGCapturedStmtInfo *) {
208176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      return true;
209176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    }
210176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
211524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir    /// \brief Emit the captured statement body.
2120e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    virtual void EmitBody(CodeGenFunction &CGF, const Stmt *S) {
2136bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      RegionCounter Cnt = CGF.getPGORegionCounter(S);
2146bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      Cnt.beginRegion(CGF.Builder);
215524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir      CGF.EmitStmt(S);
216524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir    }
217524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir
218524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir    /// \brief Get the name of the capture helper.
219524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir    virtual StringRef getHelperName() const { return "__captured_stmt"; }
220524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir
221524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir  private:
222524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir    /// \brief The kind of captured statement being generated.
223524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir    CapturedRegionKind Kind;
224524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir
225524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir    /// \brief Keep the map between VarDecl and FieldDecl.
226524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir    llvm::SmallDenseMap<const VarDecl *, FieldDecl *> CaptureFields;
227524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir
228524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir    /// \brief The base address of the captured record, passed in as the first
229524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir    /// argument of the parallel region function.
230524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir    llvm::Value *ThisValue;
231524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir
232524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir    /// \brief Captured 'this' type.
233524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir    FieldDecl *CXXThisFieldDecl;
234524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir  };
235524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir  CGCapturedStmtInfo *CapturedStmtInfo;
236524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir
237b3198a841e7f356f171f1e11faff014b2deb1eb8Nuno Lopes  /// BoundsChecking - Emit run-time bounds checks. Higher values mean
238b3198a841e7f356f171f1e11faff014b2deb1eb8Nuno Lopes  /// potentially higher performance penalties.
239b3198a841e7f356f171f1e11faff014b2deb1eb8Nuno Lopes  unsigned char BoundsChecking;
240b3198a841e7f356f171f1e11faff014b2deb1eb8Nuno Lopes
241176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  /// \brief Sanitizers enabled for this function.
242176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  SanitizerSet SanOpts;
243176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
244176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  /// \brief True if CodeGen currently emits code implementing sanitizer checks.
245176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  bool IsSanitizerScope;
246176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
247176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  /// \brief RAII object to set/unset CodeGenFunction::IsSanitizerScope.
248176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  class SanitizerScope {
249176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    CodeGenFunction *CGF;
250176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  public:
251176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    SanitizerScope(CodeGenFunction *CGF);
252176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    ~SanitizerScope();
253176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  };
254176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
255176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  /// In C++, whether we are code generating a thunk.  This controls whether we
256176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  /// should emit cleanups.
257176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  bool CurFuncIsThunk;
2584f45bc099f2665bc6e4bcbb169aa452390dbf3feWill Dietz
259f85e193739c953358c865005855253af4f68a497John McCall  /// In ARC, whether we should autorelease the return value.
260f85e193739c953358c865005855253af4f68a497John McCall  bool AutoreleaseResult;
261f85e193739c953358c865005855253af4f68a497John McCall
262176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  /// Whether we processed a Microsoft-style asm block during CodeGen. These can
263176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  /// potentially set the return value.
264176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  bool SawAsmBlock;
265176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
266d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall  const CodeGen::CGBlockInfo *BlockInfo;
267d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall  llvm::Value *BlockPointer;
268d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall
269cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman  llvm::DenseMap<const VarDecl *, FieldDecl *> LambdaCaptureFields;
270cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman  FieldDecl *LambdaThisCaptureField;
271cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman
2723d91bbcdab155181556969cad6ec97014405acedDouglas Gregor  /// \brief A mapping from NRVO variables to the flags used to indicate
2733d91bbcdab155181556969cad6ec97014405acedDouglas Gregor  /// when the NRVO has been applied to this variable.
2743d91bbcdab155181556969cad6ec97014405acedDouglas Gregor  llvm::DenseMap<const VarDecl *, llvm::Value *> NRVOFlags;
2759cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
276f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  EHScopeStack EHStack;
2778a07cd3f59a0ede54906bdc8ed21724815db27adRichard Smith  llvm::SmallVector<char, 256> LifetimeExtendedCleanupStack;
2780e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  llvm::SmallVector<const JumpDest *, 2> SEHTryEpilogueStack;
2798a07cd3f59a0ede54906bdc8ed21724815db27adRichard Smith
2808a07cd3f59a0ede54906bdc8ed21724815db27adRichard Smith  /// Header for data within LifetimeExtendedCleanupStack.
2818a07cd3f59a0ede54906bdc8ed21724815db27adRichard Smith  struct LifetimeExtendedCleanupHeader {
2828a07cd3f59a0ede54906bdc8ed21724815db27adRichard Smith    /// The size of the following cleanup object.
283176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    unsigned Size : 29;
2848a07cd3f59a0ede54906bdc8ed21724815db27adRichard Smith    /// The kind of cleanup to push: a value from the CleanupKind enumeration.
2858a07cd3f59a0ede54906bdc8ed21724815db27adRichard Smith    unsigned Kind : 3;
2868a07cd3f59a0ede54906bdc8ed21724815db27adRichard Smith
287176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    size_t getSize() const { return size_t(Size); }
2888a07cd3f59a0ede54906bdc8ed21724815db27adRichard Smith    CleanupKind getKind() const { return static_cast<CleanupKind>(Kind); }
2898a07cd3f59a0ede54906bdc8ed21724815db27adRichard Smith  };
2900dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
291ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  /// i32s containing the indexes of the cleanup destinations.
292ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  llvm::AllocaInst *NormalCleanupDest;
293ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
294ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  unsigned NextCleanupDestIndex;
295ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
2961a343ebbf413e8eae6b2737b2b2d79cbf5765571John McCall  /// FirstBlockInfo - The head of a singly-linked-list of block layouts.
2971a343ebbf413e8eae6b2737b2b2d79cbf5765571John McCall  CGBlockInfo *FirstBlockInfo;
2981a343ebbf413e8eae6b2737b2b2d79cbf5765571John McCall
299777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  /// EHResumeBlock - Unified block containing a call to llvm.eh.resume.
300777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  llvm::BasicBlock *EHResumeBlock;
301777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall
302285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling  /// The exception slot.  All landing pads write the current exception pointer
303285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling  /// into this alloca.
304f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  llvm::Value *ExceptionSlot;
3050dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
306285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling  /// The selector slot.  Under the MandatoryCleanup model, all landing pads
307285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling  /// write the current selector value into this alloca.
30893c332a8ba2c193c435b293966d343dab15f555bJohn McCall  llvm::AllocaInst *EHSelectorSlot;
30993c332a8ba2c193c435b293966d343dab15f555bJohn McCall
3100e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  llvm::AllocaInst *AbnormalTerminationSlot;
3110e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
3120e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  /// The implicit parameter to SEH filter functions of type
3130e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  /// 'EXCEPTION_POINTERS*'.
3140e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  ImplicitParamDecl *SEHPointersDecl;
3150e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
316f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// Emits a landing pad for the current EH stack.
317f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  llvm::BasicBlock *EmitLandingPad();
3180dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
319f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  llvm::BasicBlock *getInvokeDestImpl();
32099533834ba8f3658559f334e68a518ebb6388ceaMike Stump
321150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  template <class T>
322804b807ea918184d6de63bd745e1ff75a9bfc679John McCall  typename DominatingValue<T>::saved_type saveValueInCond(T value) {
323804b807ea918184d6de63bd745e1ff75a9bfc679John McCall    return DominatingValue<T>::save(*this, value);
324150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  }
325150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
326f1549f66a8216a78112286e3978cea2c29d6334cJohn McCallpublic:
327f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// ObjCEHValueStack - Stack of Objective-C exception values, used for
328f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// rethrows.
329686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  SmallVector<llvm::Value*, 8> ObjCEHValueStack;
330bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson
331d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall  /// A class controlling the emission of a finally block.
332d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall  class FinallyInfo {
333d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall    /// Where the catchall's edge through the cleanup should go.
334d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall    JumpDest RethrowDest;
335d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall
336d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall    /// A function to call to enter the catch.
337d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall    llvm::Constant *BeginCatchFn;
338d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall
339d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall    /// An i1 variable indicating whether or not the @finally is
340d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall    /// running for an exception.
341d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall    llvm::AllocaInst *ForEHVar;
342d88ea5687968640ada2bc5a10211cbeb68a671ecMike Stump
343d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall    /// An i8* variable into which the exception pointer to rethrow
344d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall    /// has been saved.
345d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall    llvm::AllocaInst *SavedExnVar;
346d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall
347d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall  public:
348d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall    void enter(CodeGenFunction &CGF, const Stmt *Finally,
349d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall               llvm::Constant *beginCatchFn, llvm::Constant *endCatchFn,
350d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall               llvm::Constant *rethrowFn);
351d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall    void exit(CodeGenFunction &CGF);
352d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall  };
353f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
3540e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  /// Cleanups can be emitted for two reasons: normal control leaving a region
3550e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  /// exceptional control flow leaving a region.
3560e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  struct SEHFinallyInfo {
3570e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    SEHFinallyInfo()
3580e2c34f92f00628d48968dfea096d36381f494cbStephen Hines        : FinallyBB(nullptr), ContBB(nullptr), ResumeBB(nullptr) {}
3590e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
3600e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    llvm::BasicBlock *FinallyBB;
3610e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    llvm::BasicBlock *ContBB;
3620e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    llvm::BasicBlock *ResumeBB;
3630e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  };
3640e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
3650e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  /// Returns true inside SEH __try blocks.
3660e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  bool isSEHTryScope() const { return !SEHTryEpilogueStack.empty(); }
3670e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
368150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  /// pushFullExprCleanup - Push a cleanup to be run at the end of the
369150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  /// current full-expression.  Safe against the possibility that
370150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  /// we're currently inside a conditionally-evaluated expression.
3713ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar  template <class T, class... As>
3723ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar  void pushFullExprCleanup(CleanupKind kind, As... A) {
3733ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall    // If we're not in a conditional branch, or if none of the
3743ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall    // arguments requires saving, then use the unconditional cleanup.
375c4a1a8450a3613ef256a71b9d8305b41f79eef50John McCall    if (!isInConditionalBranch())
3763ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar      return EHStack.pushCleanup<T>(kind, A...);
3773ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall
3783ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar    // Stash values in a tuple so we can guarantee the order of saves.
3793ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar    typedef std::tuple<typename DominatingValue<As>::saved_type...> SavedTuple;
3803ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar    SavedTuple Saved{saveValueInCond(A)...};
3813ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall
3823ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar    typedef EHScopeStack::ConditionalCleanup<T, As...> CleanupType;
3833ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar    EHStack.pushCleanupTuple<CleanupType>(kind, Saved);
3849928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall    initFullExprCleanup();
3859928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall  }
3869928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall
3878a07cd3f59a0ede54906bdc8ed21724815db27adRichard Smith  /// \brief Queue a cleanup to be pushed after finishing the current
3888a07cd3f59a0ede54906bdc8ed21724815db27adRichard Smith  /// full-expression.
3893ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar  template <class T, class... As>
3903ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar  void pushCleanupAfterFullExpr(CleanupKind Kind, As... A) {
3918a07cd3f59a0ede54906bdc8ed21724815db27adRichard Smith    assert(!isInConditionalBranch() && "can't defer conditional cleanup");
3928a07cd3f59a0ede54906bdc8ed21724815db27adRichard Smith
3938a07cd3f59a0ede54906bdc8ed21724815db27adRichard Smith    LifetimeExtendedCleanupHeader Header = { sizeof(T), Kind };
3948a07cd3f59a0ede54906bdc8ed21724815db27adRichard Smith
3958a07cd3f59a0ede54906bdc8ed21724815db27adRichard Smith    size_t OldSize = LifetimeExtendedCleanupStack.size();
3968a07cd3f59a0ede54906bdc8ed21724815db27adRichard Smith    LifetimeExtendedCleanupStack.resize(
3978a07cd3f59a0ede54906bdc8ed21724815db27adRichard Smith        LifetimeExtendedCleanupStack.size() + sizeof(Header) + Header.Size);
3988a07cd3f59a0ede54906bdc8ed21724815db27adRichard Smith
3998a07cd3f59a0ede54906bdc8ed21724815db27adRichard Smith    char *Buffer = &LifetimeExtendedCleanupStack[OldSize];
4008a07cd3f59a0ede54906bdc8ed21724815db27adRichard Smith    new (Buffer) LifetimeExtendedCleanupHeader(Header);
4013ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar    new (Buffer + sizeof(Header)) T(A...);
4028a07cd3f59a0ede54906bdc8ed21724815db27adRichard Smith  }
4038a07cd3f59a0ede54906bdc8ed21724815db27adRichard Smith
4046f103ba42cb69d50005a977c5ea583984ab63fc4John McCall  /// Set up the last cleaup that was pushed as a conditional
4056f103ba42cb69d50005a977c5ea583984ab63fc4John McCall  /// full-expression cleanup.
4066f103ba42cb69d50005a977c5ea583984ab63fc4John McCall  void initFullExprCleanup();
4076f103ba42cb69d50005a977c5ea583984ab63fc4John McCall
408f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// PushDestructorCleanup - Push a cleanup to call the
409f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// complete-object destructor of an object of the given type at the
410f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// given address.  Does nothing if T is not a C++ class type with a
411f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// non-trivial destructor.
412f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  void PushDestructorCleanup(QualType T, llvm::Value *Addr);
413f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
41481407d404a3fc8d24d0e2a052d85cae183033e2bJohn McCall  /// PushDestructorCleanup - Push a cleanup to call the
41581407d404a3fc8d24d0e2a052d85cae183033e2bJohn McCall  /// complete-object variant of the given destructor on the object at
41681407d404a3fc8d24d0e2a052d85cae183033e2bJohn McCall  /// the given address.
41781407d404a3fc8d24d0e2a052d85cae183033e2bJohn McCall  void PushDestructorCleanup(const CXXDestructorDecl *Dtor,
41881407d404a3fc8d24d0e2a052d85cae183033e2bJohn McCall                             llvm::Value *Addr);
41981407d404a3fc8d24d0e2a052d85cae183033e2bJohn McCall
420f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// PopCleanupBlock - Will pop the cleanup entry on the stack and
421f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// process all branch fixups.
4221c3db769249a633108dc320da87c48a432e2bcc1Adrian Prantl  void PopCleanupBlock(bool FallThroughIsBranchThrough = false);
423f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
4247d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall  /// DeactivateCleanupBlock - Deactivates the given cleanup block.
4257d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall  /// The block cannot be reactivated.  Pops it if it's the top of the
4267d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall  /// stack.
4276f103ba42cb69d50005a977c5ea583984ab63fc4John McCall  ///
4286f103ba42cb69d50005a977c5ea583984ab63fc4John McCall  /// \param DominatingIP - An instruction which is known to
4296f103ba42cb69d50005a977c5ea583984ab63fc4John McCall  ///   dominate the current IP (if set) and which lies along
4306f103ba42cb69d50005a977c5ea583984ab63fc4John McCall  ///   all paths of execution between the current IP and the
4316f103ba42cb69d50005a977c5ea583984ab63fc4John McCall  ///   the point at which the cleanup comes into scope.
4326f103ba42cb69d50005a977c5ea583984ab63fc4John McCall  void DeactivateCleanupBlock(EHScopeStack::stable_iterator Cleanup,
4336f103ba42cb69d50005a977c5ea583984ab63fc4John McCall                              llvm::Instruction *DominatingIP);
4347d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall
4357d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall  /// ActivateCleanupBlock - Activates an initially-inactive cleanup.
4367d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall  /// Cannot be used to resurrect a deactivated cleanup.
4376f103ba42cb69d50005a977c5ea583984ab63fc4John McCall  ///
4386f103ba42cb69d50005a977c5ea583984ab63fc4John McCall  /// \param DominatingIP - An instruction which is known to
4396f103ba42cb69d50005a977c5ea583984ab63fc4John McCall  ///   dominate the current IP (if set) and which lies along
4406f103ba42cb69d50005a977c5ea583984ab63fc4John McCall  ///   all paths of execution between the current IP and the
4416f103ba42cb69d50005a977c5ea583984ab63fc4John McCall  ///   the point at which the cleanup comes into scope.
4426f103ba42cb69d50005a977c5ea583984ab63fc4John McCall  void ActivateCleanupBlock(EHScopeStack::stable_iterator Cleanup,
4436f103ba42cb69d50005a977c5ea583984ab63fc4John McCall                            llvm::Instruction *DominatingIP);
444cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall
445f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// \brief Enters a new scope for capturing cleanups, all of which
446f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// will be executed once the scope is exited.
447f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  class RunCleanupsScope {
448f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    EHScopeStack::stable_iterator CleanupStackDepth;
4498a07cd3f59a0ede54906bdc8ed21724815db27adRichard Smith    size_t LifetimeExtendedCleanupStackSize;
45001234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor    bool OldDidCallStackSave;
451ed383135bcffdb8849418896b742fbca7fa2b787John McCall  protected:
4525656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor    bool PerformCleanup;
453ed383135bcffdb8849418896b742fbca7fa2b787John McCall  private:
45401234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor
4550e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    RunCleanupsScope(const RunCleanupsScope &) = delete;
4560e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    void operator=(const RunCleanupsScope &) = delete;
45701234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor
458c3287793626a778178a4dbadcc7b64e392c6e26fEric Christopher  protected:
459c3287793626a778178a4dbadcc7b64e392c6e26fEric Christopher    CodeGenFunction& CGF;
4604f45bc099f2665bc6e4bcbb169aa452390dbf3feWill Dietz
46101234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor  public:
46201234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor    /// \brief Enter a new cleanup scope.
4639cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer    explicit RunCleanupsScope(CodeGenFunction &CGF)
464c3287793626a778178a4dbadcc7b64e392c6e26fEric Christopher      : PerformCleanup(true), CGF(CGF)
4655656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor    {
466f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      CleanupStackDepth = CGF.EHStack.stable_begin();
4678a07cd3f59a0ede54906bdc8ed21724815db27adRichard Smith      LifetimeExtendedCleanupStackSize =
4688a07cd3f59a0ede54906bdc8ed21724815db27adRichard Smith          CGF.LifetimeExtendedCleanupStack.size();
46901234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor      OldDidCallStackSave = CGF.DidCallStackSave;
4704ada2ca7d82dab68d3646f3eb6dcdfee072e8ea4Argyrios Kyrtzidis      CGF.DidCallStackSave = false;
47101234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor    }
47201234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor
47301234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor    /// \brief Exit this cleanup scope, emitting any accumulated
47401234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor    /// cleanups.
475f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    ~RunCleanupsScope() {
4765656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor      if (PerformCleanup) {
4775656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor        CGF.DidCallStackSave = OldDidCallStackSave;
4788a07cd3f59a0ede54906bdc8ed21724815db27adRichard Smith        CGF.PopCleanupBlocks(CleanupStackDepth,
4798a07cd3f59a0ede54906bdc8ed21724815db27adRichard Smith                             LifetimeExtendedCleanupStackSize);
4805656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor      }
4815656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor    }
4825656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor
4835656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor    /// \brief Determine whether this scope requires any cleanups.
4845656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor    bool requiresCleanups() const {
485f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      return CGF.EHStack.stable_begin() != CleanupStackDepth;
4865656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor    }
4875656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor
4885656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor    /// \brief Force the emission of cleanups now, instead of waiting
4895656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor    /// until this object is destroyed.
4905656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor    void ForceCleanup() {
4915656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor      assert(PerformCleanup && "Already forced cleanup");
49201234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor      CGF.DidCallStackSave = OldDidCallStackSave;
4938a07cd3f59a0ede54906bdc8ed21724815db27adRichard Smith      CGF.PopCleanupBlocks(CleanupStackDepth,
4948a07cd3f59a0ede54906bdc8ed21724815db27adRichard Smith                           LifetimeExtendedCleanupStackSize);
4955656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor      PerformCleanup = false;
49601234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor    }
49701234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor  };
49801234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor
499176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  class LexicalScope : public RunCleanupsScope {
500c3287793626a778178a4dbadcc7b64e392c6e26fEric Christopher    SourceRange Range;
501495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem    SmallVector<const LabelDecl*, 4> Labels;
502495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem    LexicalScope *ParentScope;
503c3287793626a778178a4dbadcc7b64e392c6e26fEric Christopher
5040e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    LexicalScope(const LexicalScope &) = delete;
5050e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    void operator=(const LexicalScope &) = delete;
506c3287793626a778178a4dbadcc7b64e392c6e26fEric Christopher
507c3287793626a778178a4dbadcc7b64e392c6e26fEric Christopher  public:
508c3287793626a778178a4dbadcc7b64e392c6e26fEric Christopher    /// \brief Enter a new cleanup scope.
509c3287793626a778178a4dbadcc7b64e392c6e26fEric Christopher    explicit LexicalScope(CodeGenFunction &CGF, SourceRange Range)
510495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem      : RunCleanupsScope(CGF), Range(Range), ParentScope(CGF.CurLexicalScope) {
511495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem      CGF.CurLexicalScope = this;
512c3287793626a778178a4dbadcc7b64e392c6e26fEric Christopher      if (CGDebugInfo *DI = CGF.getDebugInfo())
513c3287793626a778178a4dbadcc7b64e392c6e26fEric Christopher        DI->EmitLexicalBlockStart(CGF.Builder, Range.getBegin());
514c3287793626a778178a4dbadcc7b64e392c6e26fEric Christopher    }
515c3287793626a778178a4dbadcc7b64e392c6e26fEric Christopher
516495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem    void addLabel(const LabelDecl *label) {
517495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem      assert(PerformCleanup && "adding label to dead scope?");
518495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem      Labels.push_back(label);
519495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem    }
520495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem
521c3287793626a778178a4dbadcc7b64e392c6e26fEric Christopher    /// \brief Exit this cleanup scope, emitting any accumulated
522c3287793626a778178a4dbadcc7b64e392c6e26fEric Christopher    /// cleanups.
523c3287793626a778178a4dbadcc7b64e392c6e26fEric Christopher    ~LexicalScope() {
524efb72adbae0c253fc2fd9127fb3e1c36cb1b8d93Adrian Prantl      if (CGDebugInfo *DI = CGF.getDebugInfo())
525efb72adbae0c253fc2fd9127fb3e1c36cb1b8d93Adrian Prantl        DI->EmitLexicalBlockEnd(CGF.Builder, Range.getEnd());
526efb72adbae0c253fc2fd9127fb3e1c36cb1b8d93Adrian Prantl
527495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem      // If we should perform a cleanup, force them now.  Note that
528495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem      // this ends the cleanup scope before rescoping any labels.
5290e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      if (PerformCleanup) {
5300e2c34f92f00628d48968dfea096d36381f494cbStephen Hines        ApplyDebugLocation DL(CGF, Range.getEnd());
5310e2c34f92f00628d48968dfea096d36381f494cbStephen Hines        ForceCleanup();
5320e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      }
533c3287793626a778178a4dbadcc7b64e392c6e26fEric Christopher    }
534c3287793626a778178a4dbadcc7b64e392c6e26fEric Christopher
535c3287793626a778178a4dbadcc7b64e392c6e26fEric Christopher    /// \brief Force the emission of cleanups now, instead of waiting
536c3287793626a778178a4dbadcc7b64e392c6e26fEric Christopher    /// until this object is destroyed.
537c3287793626a778178a4dbadcc7b64e392c6e26fEric Christopher    void ForceCleanup() {
538efb72adbae0c253fc2fd9127fb3e1c36cb1b8d93Adrian Prantl      CGF.CurLexicalScope = ParentScope;
539c3287793626a778178a4dbadcc7b64e392c6e26fEric Christopher      RunCleanupsScope::ForceCleanup();
540ed383135bcffdb8849418896b742fbca7fa2b787John McCall
541495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem      if (!Labels.empty())
542495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem        rescopeLabels();
543c3287793626a778178a4dbadcc7b64e392c6e26fEric Christopher    }
544495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem
545495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem    void rescopeLabels();
546c3287793626a778178a4dbadcc7b64e392c6e26fEric Christopher  };
547c3287793626a778178a4dbadcc7b64e392c6e26fEric Christopher
548176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  /// \brief The scope used to remap some variables as private in the OpenMP
549176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  /// loop body (or other captured region emitted without outlining), and to
550176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  /// restore old vars back on exit.
551176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  class OMPPrivateScope : public RunCleanupsScope {
552176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    typedef llvm::DenseMap<const VarDecl *, llvm::Value *> VarDeclMapTy;
553176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    VarDeclMapTy SavedLocals;
554176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    VarDeclMapTy SavedPrivates;
555176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
556176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  private:
5570e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    OMPPrivateScope(const OMPPrivateScope &) = delete;
5580e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    void operator=(const OMPPrivateScope &) = delete;
559176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
560176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  public:
561176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    /// \brief Enter a new OpenMP private scope.
562176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    explicit OMPPrivateScope(CodeGenFunction &CGF) : RunCleanupsScope(CGF) {}
563176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
564176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    /// \brief Registers \a LocalVD variable as a private and apply \a
565176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    /// PrivateGen function for it to generate corresponding private variable.
566176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    /// \a PrivateGen returns an address of the generated private variable.
567176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    /// \return true if the variable is registered as private, false if it has
568176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    /// been privatized already.
569176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    bool
570176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    addPrivate(const VarDecl *LocalVD,
571176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines               const std::function<llvm::Value *()> &PrivateGen) {
572176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      assert(PerformCleanup && "adding private to dead scope");
573176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      if (SavedLocals.count(LocalVD) > 0) return false;
574176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      SavedLocals[LocalVD] = CGF.LocalDeclMap.lookup(LocalVD);
575176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      CGF.LocalDeclMap.erase(LocalVD);
576176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      SavedPrivates[LocalVD] = PrivateGen();
577176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      CGF.LocalDeclMap[LocalVD] = SavedLocals[LocalVD];
578176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      return true;
579176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    }
580176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
581176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    /// \brief Privatizes local variables previously registered as private.
582176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    /// Registration is separate from the actual privatization to allow
583176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    /// initializers use values of the original variables, not the private one.
584176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    /// This is important, for example, if the private variable is a class
585176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    /// variable initialized by a constructor that references other private
586176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    /// variables. But at initialization original variables must be used, not
587176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    /// private copies.
588176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    /// \return true if at least one variable was privatized, false otherwise.
589176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    bool Privatize() {
590176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      for (auto VDPair : SavedPrivates) {
591176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        CGF.LocalDeclMap[VDPair.first] = VDPair.second;
592176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      }
593176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      SavedPrivates.clear();
594176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      return !SavedLocals.empty();
595176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    }
596176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
597176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    void ForceCleanup() {
598176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      RunCleanupsScope::ForceCleanup();
599176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      // Remap vars back to the original values.
600176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      for (auto I : SavedLocals) {
601176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        CGF.LocalDeclMap[I.first] = I.second;
602176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      }
603176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      SavedLocals.clear();
604176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    }
605176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
606176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    /// \brief Exit scope - all the mapped variables are restored.
6073ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar    ~OMPPrivateScope() {
6083ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar      if (PerformCleanup)
6093ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar        ForceCleanup();
6103ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar    }
611176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  };
61244ec82b4a1597802f5bf17721481b8c265bc8dc5Anders Carlsson
6138a07cd3f59a0ede54906bdc8ed21724815db27adRichard Smith  /// \brief Takes the old cleanup stack size and emits the cleanup blocks
6148a07cd3f59a0ede54906bdc8ed21724815db27adRichard Smith  /// that have been added.
6151c3db769249a633108dc320da87c48a432e2bcc1Adrian Prantl  void PopCleanupBlocks(EHScopeStack::stable_iterator OldCleanupStackSize);
61644ec82b4a1597802f5bf17721481b8c265bc8dc5Anders Carlsson
6178a07cd3f59a0ede54906bdc8ed21724815db27adRichard Smith  /// \brief Takes the old cleanup stack size and emits the cleanup blocks
6188a07cd3f59a0ede54906bdc8ed21724815db27adRichard Smith  /// that have been added, then adds all lifetime-extended cleanups from
6198a07cd3f59a0ede54906bdc8ed21724815db27adRichard Smith  /// the given position to the stack.
6208a07cd3f59a0ede54906bdc8ed21724815db27adRichard Smith  void PopCleanupBlocks(EHScopeStack::stable_iterator OldCleanupStackSize,
6218a07cd3f59a0ede54906bdc8ed21724815db27adRichard Smith                        size_t OldLifetimeExtendedStackSize);
6228a07cd3f59a0ede54906bdc8ed21724815db27adRichard Smith
623ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  void ResolveBranchFixups(llvm::BasicBlock *Target);
624ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
625f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// The given basic block lies in the current EH scope, but may be a
626f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// target of a potentially scope-crossing jump; get a stable handle
627f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// to which we can perform this jump later.
628ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  JumpDest getJumpDestInCurrentScope(llvm::BasicBlock *Target) {
629413e67778d593215d2f2161a4e712c8568f1ddd0John McCall    return JumpDest(Target,
630413e67778d593215d2f2161a4e712c8568f1ddd0John McCall                    EHStack.getInnermostNormalCleanup(),
631413e67778d593215d2f2161a4e712c8568f1ddd0John McCall                    NextCleanupDestIndex++);
632f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  }
633c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson
634f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// The given basic block lies in the current EH scope, but may be a
635f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// target of a potentially scope-crossing jump; get a stable handle
636f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// to which we can perform this jump later.
637686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  JumpDest getJumpDestInCurrentScope(StringRef Name = StringRef()) {
638ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    return getJumpDestInCurrentScope(createBasicBlock(Name));
639f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  }
640f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
641f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// EmitBranchThroughCleanup - Emit a branch from the current insert
642f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// block through the normal cleanup handling code (if any) and then
643f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// on to \arg Dest.
644f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  void EmitBranchThroughCleanup(JumpDest Dest);
645b11f9198111796ada02b57f62cdea92134fde9f7Chris Lattner
646b11f9198111796ada02b57f62cdea92134fde9f7Chris Lattner  /// isObviouslyBranchWithoutCleanups - Return true if a branch to the
647b11f9198111796ada02b57f62cdea92134fde9f7Chris Lattner  /// specified destination obviously has no cleanups to run.  'false' is always
648b11f9198111796ada02b57f62cdea92134fde9f7Chris Lattner  /// a conservatively correct answer for this method.
649b11f9198111796ada02b57f62cdea92134fde9f7Chris Lattner  bool isObviouslyBranchWithoutCleanups(JumpDest Dest) const;
650f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
651777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  /// popCatchScope - Pops the catch scope at the top of the EHScope
652777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  /// stack, emitting any required code (other than the catch handlers
653777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  /// themselves).
654777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  void popCatchScope();
655ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
656c686004145b1f4dbeb38173a0886ba7040ae0089David Chisnall  llvm::BasicBlock *getEHResumeBlock(bool isCleanup);
657777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  llvm::BasicBlock *getEHDispatchBlock(EHScopeStack::stable_iterator scope);
6580dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
659150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  /// An object to manage conditionally-evaluated expressions.
660150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  class ConditionalEvaluation {
661150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    llvm::BasicBlock *StartBB;
6621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
663150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  public:
664150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    ConditionalEvaluation(CodeGenFunction &CGF)
665150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      : StartBB(CGF.Builder.GetInsertBlock()) {}
6669cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
667150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    void begin(CodeGenFunction &CGF) {
668150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      assert(CGF.OutermostConditional != this);
669150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      if (!CGF.OutermostConditional)
670150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall        CGF.OutermostConditional = this;
671150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    }
672150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
673150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    void end(CodeGenFunction &CGF) {
6746bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      assert(CGF.OutermostConditional != nullptr);
675150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      if (CGF.OutermostConditional == this)
6766bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        CGF.OutermostConditional = nullptr;
677150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    }
678150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
679150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    /// Returns a block which will be executed prior to each
680150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    /// evaluation of the conditional code.
681150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    llvm::BasicBlock *getStartingBlock() const {
682150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      return StartBB;
683150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    }
684150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  };
6851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6863019c444c672938c57f5573840071ecd73425ee7John McCall  /// isInConditionalBranch - Return true if we're currently emitting
6873019c444c672938c57f5573840071ecd73425ee7John McCall  /// one branch or the other of a conditional expression.
6886bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  bool isInConditionalBranch() const { return OutermostConditional != nullptr; }
689150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
6906f103ba42cb69d50005a977c5ea583984ab63fc4John McCall  void setBeforeOutermostConditional(llvm::Value *value, llvm::Value *addr) {
6916f103ba42cb69d50005a977c5ea583984ab63fc4John McCall    assert(isInConditionalBranch());
6926f103ba42cb69d50005a977c5ea583984ab63fc4John McCall    llvm::BasicBlock *block = OutermostConditional->getStartingBlock();
6936f103ba42cb69d50005a977c5ea583984ab63fc4John McCall    new llvm::StoreInst(value, addr, &block->back());
6946f103ba42cb69d50005a977c5ea583984ab63fc4John McCall  }
6956f103ba42cb69d50005a977c5ea583984ab63fc4John McCall
696150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  /// An RAII object to record that we're evaluating a statement
697150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  /// expression.
698150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  class StmtExprEvaluation {
699150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    CodeGenFunction &CGF;
700150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
701150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    /// We have to save the outermost conditional: cleanups in a
702150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    /// statement expression aren't conditional just because the
703150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    /// StmtExpr is.
704150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    ConditionalEvaluation *SavedOutermostConditional;
705150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
706150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  public:
707150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    StmtExprEvaluation(CodeGenFunction &CGF)
708150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      : CGF(CGF), SavedOutermostConditional(CGF.OutermostConditional) {
7096bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      CGF.OutermostConditional = nullptr;
710150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    }
711150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
712150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    ~StmtExprEvaluation() {
713150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      CGF.OutermostConditional = SavedOutermostConditional;
714150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      CGF.EnsureInsertPoint();
715150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    }
716150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  };
717e996ffd240f20a1048179d7727a6ee3227261921John McCall
71856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  /// An object which temporarily prevents a value from being
71956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  /// destroyed by aggressive peephole optimizations that assume that
72056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  /// all uses of a value have been realized in the IR.
72156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  class PeepholeProtection {
72256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    llvm::Instruction *Inst;
72356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    friend class CodeGenFunction;
72456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
72556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  public:
7266bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    PeepholeProtection() : Inst(nullptr) {}
7274b9c2d235fb9449e249d74f48ecfec601650de93John McCall  };
72856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
7294b9c2d235fb9449e249d74f48ecfec601650de93John McCall  /// A non-RAII class containing all the information about a bound
7304b9c2d235fb9449e249d74f48ecfec601650de93John McCall  /// opaque value.  OpaqueValueMapping, below, is a RAII wrapper for
7314b9c2d235fb9449e249d74f48ecfec601650de93John McCall  /// this which makes individual mappings very simple; using this
7324b9c2d235fb9449e249d74f48ecfec601650de93John McCall  /// class directly is useful when you have a variable number of
7334b9c2d235fb9449e249d74f48ecfec601650de93John McCall  /// opaque values or don't want the RAII functionality for some
7344b9c2d235fb9449e249d74f48ecfec601650de93John McCall  /// reason.
7354b9c2d235fb9449e249d74f48ecfec601650de93John McCall  class OpaqueValueMappingData {
736e996ffd240f20a1048179d7727a6ee3227261921John McCall    const OpaqueValueExpr *OpaqueValue;
73756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    bool BoundLValue;
73856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    CodeGenFunction::PeepholeProtection Protection;
739e996ffd240f20a1048179d7727a6ee3227261921John McCall
7404b9c2d235fb9449e249d74f48ecfec601650de93John McCall    OpaqueValueMappingData(const OpaqueValueExpr *ov,
7414b9c2d235fb9449e249d74f48ecfec601650de93John McCall                           bool boundLValue)
7424b9c2d235fb9449e249d74f48ecfec601650de93John McCall      : OpaqueValue(ov), BoundLValue(boundLValue) {}
743e996ffd240f20a1048179d7727a6ee3227261921John McCall  public:
7446bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    OpaqueValueMappingData() : OpaqueValue(nullptr) {}
7454b9c2d235fb9449e249d74f48ecfec601650de93John McCall
74656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    static bool shouldBindAsLValue(const Expr *expr) {
747a5493f885095bd63d525a7035ad1c224de3ae5f2John McCall      // gl-values should be bound as l-values for obvious reasons.
748a5493f885095bd63d525a7035ad1c224de3ae5f2John McCall      // Records should be bound as l-values because IR generation
749a5493f885095bd63d525a7035ad1c224de3ae5f2John McCall      // always keeps them in memory.  Expressions of function type
750a5493f885095bd63d525a7035ad1c224de3ae5f2John McCall      // act exactly like l-values but are formally required to be
751a5493f885095bd63d525a7035ad1c224de3ae5f2John McCall      // r-values in C.
752a5493f885095bd63d525a7035ad1c224de3ae5f2John McCall      return expr->isGLValue() ||
753651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines             expr->getType()->isFunctionType() ||
754651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines             hasAggregateEvaluationKind(expr->getType());
75556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    }
75656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
7574b9c2d235fb9449e249d74f48ecfec601650de93John McCall    static OpaqueValueMappingData bind(CodeGenFunction &CGF,
7584b9c2d235fb9449e249d74f48ecfec601650de93John McCall                                       const OpaqueValueExpr *ov,
7594b9c2d235fb9449e249d74f48ecfec601650de93John McCall                                       const Expr *e) {
7604b9c2d235fb9449e249d74f48ecfec601650de93John McCall      if (shouldBindAsLValue(ov))
7614b9c2d235fb9449e249d74f48ecfec601650de93John McCall        return bind(CGF, ov, CGF.EmitLValue(e));
7624b9c2d235fb9449e249d74f48ecfec601650de93John McCall      return bind(CGF, ov, CGF.EmitAnyExpr(e));
7634b9c2d235fb9449e249d74f48ecfec601650de93John McCall    }
7644b9c2d235fb9449e249d74f48ecfec601650de93John McCall
7654b9c2d235fb9449e249d74f48ecfec601650de93John McCall    static OpaqueValueMappingData bind(CodeGenFunction &CGF,
7664b9c2d235fb9449e249d74f48ecfec601650de93John McCall                                       const OpaqueValueExpr *ov,
7674b9c2d235fb9449e249d74f48ecfec601650de93John McCall                                       const LValue &lv) {
7684b9c2d235fb9449e249d74f48ecfec601650de93John McCall      assert(shouldBindAsLValue(ov));
7694b9c2d235fb9449e249d74f48ecfec601650de93John McCall      CGF.OpaqueLValues.insert(std::make_pair(ov, lv));
7704b9c2d235fb9449e249d74f48ecfec601650de93John McCall      return OpaqueValueMappingData(ov, true);
7714b9c2d235fb9449e249d74f48ecfec601650de93John McCall    }
7724b9c2d235fb9449e249d74f48ecfec601650de93John McCall
7734b9c2d235fb9449e249d74f48ecfec601650de93John McCall    static OpaqueValueMappingData bind(CodeGenFunction &CGF,
7744b9c2d235fb9449e249d74f48ecfec601650de93John McCall                                       const OpaqueValueExpr *ov,
7754b9c2d235fb9449e249d74f48ecfec601650de93John McCall                                       const RValue &rv) {
7764b9c2d235fb9449e249d74f48ecfec601650de93John McCall      assert(!shouldBindAsLValue(ov));
7774b9c2d235fb9449e249d74f48ecfec601650de93John McCall      CGF.OpaqueRValues.insert(std::make_pair(ov, rv));
7784b9c2d235fb9449e249d74f48ecfec601650de93John McCall
7794b9c2d235fb9449e249d74f48ecfec601650de93John McCall      OpaqueValueMappingData data(ov, false);
7804b9c2d235fb9449e249d74f48ecfec601650de93John McCall
7814b9c2d235fb9449e249d74f48ecfec601650de93John McCall      // Work around an extremely aggressive peephole optimization in
7824b9c2d235fb9449e249d74f48ecfec601650de93John McCall      // EmitScalarConversion which assumes that all other uses of a
7834b9c2d235fb9449e249d74f48ecfec601650de93John McCall      // value are extant.
7844b9c2d235fb9449e249d74f48ecfec601650de93John McCall      data.Protection = CGF.protectFromPeepholes(rv);
7854b9c2d235fb9449e249d74f48ecfec601650de93John McCall
7864b9c2d235fb9449e249d74f48ecfec601650de93John McCall      return data;
7874b9c2d235fb9449e249d74f48ecfec601650de93John McCall    }
7884b9c2d235fb9449e249d74f48ecfec601650de93John McCall
7896bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    bool isValid() const { return OpaqueValue != nullptr; }
7906bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    void clear() { OpaqueValue = nullptr; }
7914b9c2d235fb9449e249d74f48ecfec601650de93John McCall
7924b9c2d235fb9449e249d74f48ecfec601650de93John McCall    void unbind(CodeGenFunction &CGF) {
7934b9c2d235fb9449e249d74f48ecfec601650de93John McCall      assert(OpaqueValue && "no data to unbind!");
7944b9c2d235fb9449e249d74f48ecfec601650de93John McCall
7954b9c2d235fb9449e249d74f48ecfec601650de93John McCall      if (BoundLValue) {
7964b9c2d235fb9449e249d74f48ecfec601650de93John McCall        CGF.OpaqueLValues.erase(OpaqueValue);
7974b9c2d235fb9449e249d74f48ecfec601650de93John McCall      } else {
7984b9c2d235fb9449e249d74f48ecfec601650de93John McCall        CGF.OpaqueRValues.erase(OpaqueValue);
7994b9c2d235fb9449e249d74f48ecfec601650de93John McCall        CGF.unprotectFromPeepholes(Protection);
8004b9c2d235fb9449e249d74f48ecfec601650de93John McCall      }
8014b9c2d235fb9449e249d74f48ecfec601650de93John McCall    }
8024b9c2d235fb9449e249d74f48ecfec601650de93John McCall  };
8034b9c2d235fb9449e249d74f48ecfec601650de93John McCall
8044b9c2d235fb9449e249d74f48ecfec601650de93John McCall  /// An RAII object to set (and then clear) a mapping for an OpaqueValueExpr.
8054b9c2d235fb9449e249d74f48ecfec601650de93John McCall  class OpaqueValueMapping {
8064b9c2d235fb9449e249d74f48ecfec601650de93John McCall    CodeGenFunction &CGF;
8074b9c2d235fb9449e249d74f48ecfec601650de93John McCall    OpaqueValueMappingData Data;
8084b9c2d235fb9449e249d74f48ecfec601650de93John McCall
8094b9c2d235fb9449e249d74f48ecfec601650de93John McCall  public:
8104b9c2d235fb9449e249d74f48ecfec601650de93John McCall    static bool shouldBindAsLValue(const Expr *expr) {
8114b9c2d235fb9449e249d74f48ecfec601650de93John McCall      return OpaqueValueMappingData::shouldBindAsLValue(expr);
8124b9c2d235fb9449e249d74f48ecfec601650de93John McCall    }
8134b9c2d235fb9449e249d74f48ecfec601650de93John McCall
81456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    /// Build the opaque value mapping for the given conditional
81556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    /// operator if it's the GNU ?: extension.  This is a common
81656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    /// enough pattern that the convenience operator is really
81756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    /// helpful.
81856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    ///
81956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    OpaqueValueMapping(CodeGenFunction &CGF,
82056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall                       const AbstractConditionalOperator *op) : CGF(CGF) {
8214b9c2d235fb9449e249d74f48ecfec601650de93John McCall      if (isa<ConditionalOperator>(op))
8224b9c2d235fb9449e249d74f48ecfec601650de93John McCall        // Leave Data empty.
82356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall        return;
82456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
82556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      const BinaryConditionalOperator *e = cast<BinaryConditionalOperator>(op);
8264b9c2d235fb9449e249d74f48ecfec601650de93John McCall      Data = OpaqueValueMappingData::bind(CGF, e->getOpaqueValue(),
8274b9c2d235fb9449e249d74f48ecfec601650de93John McCall                                          e->getCommon());
82856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    }
82956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
830e996ffd240f20a1048179d7727a6ee3227261921John McCall    OpaqueValueMapping(CodeGenFunction &CGF,
831e996ffd240f20a1048179d7727a6ee3227261921John McCall                       const OpaqueValueExpr *opaqueValue,
83256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall                       LValue lvalue)
8334b9c2d235fb9449e249d74f48ecfec601650de93John McCall      : CGF(CGF), Data(OpaqueValueMappingData::bind(CGF, opaqueValue, lvalue)) {
83456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    }
83556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
83656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    OpaqueValueMapping(CodeGenFunction &CGF,
83756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall                       const OpaqueValueExpr *opaqueValue,
83856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall                       RValue rvalue)
8394b9c2d235fb9449e249d74f48ecfec601650de93John McCall      : CGF(CGF), Data(OpaqueValueMappingData::bind(CGF, opaqueValue, rvalue)) {
840e996ffd240f20a1048179d7727a6ee3227261921John McCall    }
841e996ffd240f20a1048179d7727a6ee3227261921John McCall
842e996ffd240f20a1048179d7727a6ee3227261921John McCall    void pop() {
8434b9c2d235fb9449e249d74f48ecfec601650de93John McCall      Data.unbind(CGF);
8444b9c2d235fb9449e249d74f48ecfec601650de93John McCall      Data.clear();
845e996ffd240f20a1048179d7727a6ee3227261921John McCall    }
846e996ffd240f20a1048179d7727a6ee3227261921John McCall
847e996ffd240f20a1048179d7727a6ee3227261921John McCall    ~OpaqueValueMapping() {
8484b9c2d235fb9449e249d74f48ecfec601650de93John McCall      if (Data.isValid()) Data.unbind(CGF);
849e996ffd240f20a1048179d7727a6ee3227261921John McCall    }
850e996ffd240f20a1048179d7727a6ee3227261921John McCall  };
851e220455a059d926953befe72857b9525273717efFariborz Jahanian
852e220455a059d926953befe72857b9525273717efFariborz Jahanian  /// getByrefValueFieldNumber - Given a declaration, returns the LLVM field
853e220455a059d926953befe72857b9525273717efFariborz Jahanian  /// number that holds the value.
854e220455a059d926953befe72857b9525273717efFariborz Jahanian  unsigned getByRefValueLLVMField(const ValueDecl *VD) const;
85552a80e19ad688091723a52ad53337767bb0ac684Fariborz Jahanian
85652a80e19ad688091723a52ad53337767bb0ac684Fariborz Jahanian  /// BuildBlockByrefAddress - Computes address location of the
85752a80e19ad688091723a52ad53337767bb0ac684Fariborz Jahanian  /// variable which is declared as __block.
85852a80e19ad688091723a52ad53337767bb0ac684Fariborz Jahanian  llvm::Value *BuildBlockByrefAddress(llvm::Value *BaseAddr,
85952a80e19ad688091723a52ad53337767bb0ac684Fariborz Jahanian                                      const VarDecl *V);
8607f02f721d48772e4eee4fccd8af4f800be1bbc1eChris Lattnerprivate:
861d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  CGDebugInfo *DebugInfo;
862aa11289f754d220c9c155b68a4f84cdcfcefef6aDevang Patel  bool DisableDebugInfo;
86309429b962811f2c0a7a81a1b0a89f96f5878b20cMike Stump
86493c332a8ba2c193c435b293966d343dab15f555bJohn McCall  /// DidCallStackSave - Whether llvm.stacksave has been called. Used to avoid
86593c332a8ba2c193c435b293966d343dab15f555bJohn McCall  /// calling llvm.stacksave for multiple VLAs in the same scope.
86693c332a8ba2c193c435b293966d343dab15f555bJohn McCall  bool DidCallStackSave;
86793c332a8ba2c193c435b293966d343dab15f555bJohn McCall
868f71d232ce5354bd2f0c94ded30fb936c2a3d968bMike Stump  /// IndirectBranch - The first time an indirect goto is seen we create a block
869f71d232ce5354bd2f0c94ded30fb936c2a3d968bMike Stump  /// with an indirect branch.  Every time we see the address of a label taken,
870f71d232ce5354bd2f0c94ded30fb936c2a3d968bMike Stump  /// we add the label to the indirect goto.  Every subsequent indirect goto is
871f71d232ce5354bd2f0c94ded30fb936c2a3d968bMike Stump  /// codegen'd as a jump to the IndirectBranch's basic block.
872d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  llvm::IndirectBrInst *IndirectBranch;
8730ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar
8740dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// LocalDeclMap - This keeps track of the LLVM allocas or globals for local C
8750dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// decls.
8766b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  typedef llvm::DenseMap<const Decl*, llvm::Value*> DeclMapTy;
8776b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  DeclMapTy LocalDeclMap;
8785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// LabelMap - This keeps track of the LLVM basic block for each C label.
880ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  llvm::DenseMap<const LabelDecl*, JumpDest> LabelMap;
8810dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
8820dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  // BreakContinueStack - This keeps track of where break and continue
883e4b6d342c29d5cb9d311756100df1603810fa892Anders Carlsson  // statements should jump to.
884da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner  struct BreakContinue {
885f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    BreakContinue(JumpDest Break, JumpDest Continue)
886f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      : BreakBlock(Break), ContinueBlock(Continue) {}
8870dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
888f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    JumpDest BreakBlock;
889f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    JumpDest ContinueBlock;
8900dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  };
891686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  SmallVector<BreakContinue, 8> BreakContinueStack;
89218ccc7776ad6288c5f630dc7967fb99d099c9cd9Daniel Dunbar
893651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  CodeGenPGO PGO;
894651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
895651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinespublic:
896651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// Get a counter for instrumentation of the region associated with the given
897651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// statement.
898651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  RegionCounter getPGORegionCounter(const Stmt *S) {
899651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    return RegionCounter(PGO, S);
900651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  }
901651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesprivate:
902651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
903170fd49294b85ab10f820721d99ff9d109c7230dZhongxing Xu  /// SwitchInsn - This is nearest current switch instruction. It is null if
9040dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// current context is not in a switch.
90551b09f2c528c8460b5465c676173324e44176d62Devang Patel  llvm::SwitchInst *SwitchInsn;
906651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// The branch weights of SwitchInsn when doing instrumentation based PGO.
907651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  SmallVector<uint64_t, 16> *SwitchWeights;
90851b09f2c528c8460b5465c676173324e44176d62Devang Patel
9090dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// CaseRangeBlock - This block holds if condition check for last case
91080fd5f96e6805ac43aab99faabd5b4c8b19551b7Devang Patel  /// statement range in current switch instruction.
911c049e4f406a7f7179eba98659044a32508e53289Devang Patel  llvm::BasicBlock *CaseRangeBlock;
912c049e4f406a7f7179eba98659044a32508e53289Devang Patel
91356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  /// OpaqueLValues - Keeps track of the current set of opaque value
914e996ffd240f20a1048179d7727a6ee3227261921John McCall  /// expressions.
91556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  llvm::DenseMap<const OpaqueValueExpr *, LValue> OpaqueLValues;
91656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  llvm::DenseMap<const OpaqueValueExpr *, RValue> OpaqueRValues;
917e996ffd240f20a1048179d7727a6ee3227261921John McCall
9180dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  // VLASizeMap - This keeps track of the associated size for each VLA type.
919bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman  // We track this by the size expression rather than the type itself because
920bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman  // in certain situations, like a const qualifier applied to an VLA typedef,
921bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman  // multiple VLA types can share the same size expression.
9220dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  // FIXME: Maybe this could be a stack of maps that is pushed/popped as we
9230dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  // enter/leave scopes.
924bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman  llvm::DenseMap<const Expr*, llvm::Value*> VLASizeMap;
9250dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
926f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// A block containing a single 'unreachable' instruction.  Created
927f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// lazily by getUnreachableBlock().
928f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  llvm::BasicBlock *UnreachableBlock;
9290dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
930d072e59eb6b5e32f93be25f08f212b0ec21d6c5dAdrian Prantl  /// Counts of the number return expressions in the function.
931d072e59eb6b5e32f93be25f08f212b0ec21d6c5dAdrian Prantl  unsigned NumReturnExprs;
932fa6b079b1231366696f6a497c6a084c73a35c85dAdrian Prantl
933fa6b079b1231366696f6a497c6a084c73a35c85dAdrian Prantl  /// Count the number of simple (constant) return expressions in the function.
934fa6b079b1231366696f6a497c6a084c73a35c85dAdrian Prantl  unsigned NumSimpleReturnExprs;
935fa6b079b1231366696f6a497c6a084c73a35c85dAdrian Prantl
936d072e59eb6b5e32f93be25f08f212b0ec21d6c5dAdrian Prantl  /// The last regular (non-return) debug location (breakpoint) in the function.
937d072e59eb6b5e32f93be25f08f212b0ec21d6c5dAdrian Prantl  SourceLocation LastStopPoint;
938fa6b079b1231366696f6a497c6a084c73a35c85dAdrian Prantl
939c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smithpublic:
940c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith  /// A scope within which we are constructing the fields of an object which
941c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith  /// might use a CXXDefaultInitExpr. This stashes away a 'this' value to use
942c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith  /// if we need to evaluate a CXXDefaultInitExpr within the evaluation.
943c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith  class FieldConstructionScope {
944c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith  public:
945c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith    FieldConstructionScope(CodeGenFunction &CGF, llvm::Value *This)
946c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith        : CGF(CGF), OldCXXDefaultInitExprThis(CGF.CXXDefaultInitExprThis) {
947c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith      CGF.CXXDefaultInitExprThis = This;
948c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith    }
949c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith    ~FieldConstructionScope() {
950c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith      CGF.CXXDefaultInitExprThis = OldCXXDefaultInitExprThis;
951c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith    }
952c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith
953c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith  private:
954c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith    CodeGenFunction &CGF;
955c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith    llvm::Value *OldCXXDefaultInitExprThis;
956c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith  };
957c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith
958c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith  /// The scope of a CXXDefaultInitExpr. Within this scope, the value of 'this'
959c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith  /// is overridden to be the object under construction.
960c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith  class CXXDefaultInitExprScope {
961c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith  public:
962c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith    CXXDefaultInitExprScope(CodeGenFunction &CGF)
963c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith        : CGF(CGF), OldCXXThisValue(CGF.CXXThisValue) {
964c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith      CGF.CXXThisValue = CGF.CXXDefaultInitExprThis;
965c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith    }
966c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith    ~CXXDefaultInitExprScope() {
967c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith      CGF.CXXThisValue = OldCXXThisValue;
968c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith    }
969c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith
970c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith  public:
971c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith    CodeGenFunction &CGF;
972c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith    llvm::Value *OldCXXThisValue;
973c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith  };
974c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith
975c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smithprivate:
976f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson  /// CXXThisDecl - When generating code for a C++ member function,
977f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson  /// this will hold the implicit 'this' declaration.
978cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman  ImplicitParamDecl *CXXABIThisDecl;
979cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman  llvm::Value *CXXABIThisValue;
9802504941793b549323f9d29c62507cf21d865fadeJohn McCall  llvm::Value *CXXThisValue;
9811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
982c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith  /// The value of 'this' to use when evaluating CXXDefaultInitExprs within
983c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith  /// this expression.
984c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith  llvm::Value *CXXDefaultInitExprThis;
985c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith
98659660c21178b6af518bd4b564e032d5c9cc218cbTimur Iskhodzhanov  /// CXXStructorImplicitParamDecl - When generating code for a constructor or
98759660c21178b6af518bd4b564e032d5c9cc218cbTimur Iskhodzhanov  /// destructor, this will hold the implicit argument (e.g. VTT).
98859660c21178b6af518bd4b564e032d5c9cc218cbTimur Iskhodzhanov  ImplicitParamDecl *CXXStructorImplicitParamDecl;
98959660c21178b6af518bd4b564e032d5c9cc218cbTimur Iskhodzhanov  llvm::Value *CXXStructorImplicitParamValue;
9909cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
991150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  /// OutermostConditional - Points to the outermost active
992150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  /// conditional control.  This is used so that we know if a
993150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  /// temporary should be destroyed conditionally.
994150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  ConditionalEvaluation *OutermostConditional;
9951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
996495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem  /// The current lexical scope.
997495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem  LexicalScope *CurLexicalScope;
9987dfa4079f4b37507048b19bfc693d3b015d9dd11Anders Carlsson
9991c3db769249a633108dc320da87c48a432e2bcc1Adrian Prantl  /// The current source location that should be used for exception
10001c3db769249a633108dc320da87c48a432e2bcc1Adrian Prantl  /// handling code.
10011c3db769249a633108dc320da87c48a432e2bcc1Adrian Prantl  SourceLocation CurEHLocation;
10021c3db769249a633108dc320da87c48a432e2bcc1Adrian Prantl
10037dfa4079f4b37507048b19bfc693d3b015d9dd11Anders Carlsson  /// ByrefValueInfoMap - For each __block variable, contains a pair of the LLVM
10047dfa4079f4b37507048b19bfc693d3b015d9dd11Anders Carlsson  /// type as well as the field number that contains the actual data.
10052acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::DenseMap<const ValueDecl *, std::pair<llvm::Type *,
10067dfa4079f4b37507048b19bfc693d3b015d9dd11Anders Carlsson                                              unsigned> > ByRefValueInfo;
10079cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
1008f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  llvm::BasicBlock *TerminateLandingPad;
1009182f383db1782af752ecaf607fdff72a8542088bMike Stump  llvm::BasicBlock *TerminateHandler;
101083252dcfe61aaebcb6bc117e71dc12968729513fChris Lattner  llvm::BasicBlock *TrapBB;
1011940670512d7c3d93389bb38a426abcb7ef44271eEli Friedman
10120df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner  /// Add a kernel metadata node to the named metadata node 'opencl.kernels'.
10130df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner  /// In the kernel metadata node, reference the kernel function and metadata
10140df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner  /// nodes for its optional attribute qualifiers (OpenCL 1.1 6.7.2):
101537453b9580e293eef3bd60bd36047a93ac4515b1Joey Gouly  /// - A node for the vec_type_hint(<type>) qualifier contains string
101637453b9580e293eef3bd60bd36047a93ac4515b1Joey Gouly  ///   "vec_type_hint", an undefined value of the <type> data type,
101737453b9580e293eef3bd60bd36047a93ac4515b1Joey Gouly  ///   and a Boolean that is true if the <type> is integer and signed.
10180df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner  /// - A node for the work_group_size_hint(X,Y,Z) qualifier contains string
10190df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner  ///   "work_group_size_hint", and three 32-bit integers X, Y and Z.
10200df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner  /// - A node for the reqd_work_group_size(X,Y,Z) qualifier contains string
10210df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner  ///   "reqd_work_group_size", and three 32-bit integers X, Y and Z.
10220df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner  void EmitOpenCLKernelMetadata(const FunctionDecl *FD,
10230df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner                                llvm::Function *Fn);
10240df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner
10255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerpublic:
10264904bf4e84cfb48080270ebaa9005327f18ab0e5Fariborz Jahanian  CodeGenFunction(CodeGenModule &cgm, bool suppressNewContext=false);
10271a343ebbf413e8eae6b2737b2b2d79cbf5765571John McCall  ~CodeGenFunction();
10280dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
10291e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  CodeGenTypes &getTypes() const { return CGM.getTypes(); }
1030f2aac84709c418189e476ad591848dad50291885John McCall  ASTContext &getContext() const { return CGM.getContext(); }
1031aa11289f754d220c9c155b68a4f84cdcfcefef6aDevang Patel  CGDebugInfo *getDebugInfo() {
1032aa11289f754d220c9c155b68a4f84cdcfcefef6aDevang Patel    if (DisableDebugInfo)
10336bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      return nullptr;
1034aa11289f754d220c9c155b68a4f84cdcfcefef6aDevang Patel    return DebugInfo;
1035aa11289f754d220c9c155b68a4f84cdcfcefef6aDevang Patel  }
1036aa11289f754d220c9c155b68a4f84cdcfcefef6aDevang Patel  void disableDebugInfo() { DisableDebugInfo = true; }
1037aa11289f754d220c9c155b68a4f84cdcfcefef6aDevang Patel  void enableDebugInfo() { DisableDebugInfo = false; }
1038aa11289f754d220c9c155b68a4f84cdcfcefef6aDevang Patel
1039f85e193739c953358c865005855253af4f68a497John McCall  bool shouldUseFusedARCCalls() {
1040f85e193739c953358c865005855253af4f68a497John McCall    return CGM.getCodeGenOpts().OptimizationLevel == 0;
1041f85e193739c953358c865005855253af4f68a497John McCall  }
10425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
10434e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  const LangOptions &getLangOpts() const { return CGM.getLangOpts(); }
1044d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall
1045285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling  /// Returns a pointer to the function's exception object and selector slot,
1046285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling  /// which is assigned in every landing pad.
1047f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  llvm::Value *getExceptionSlot();
104893c332a8ba2c193c435b293966d343dab15f555bJohn McCall  llvm::Value *getEHSelectorSlot();
1049f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
10500e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  /// Stack slot that contains whether a __finally block is being executed as an
10510e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  /// EH cleanup or as a normal cleanup.
10520e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  llvm::Value *getAbnormalTerminationSlot();
10530e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
1054ae270598d5c7a9a283d4b3ddce53b151c6e2b182Bill Wendling  /// Returns the contents of the function's exception object and selector
1055ae270598d5c7a9a283d4b3ddce53b151c6e2b182Bill Wendling  /// slots.
1056ae270598d5c7a9a283d4b3ddce53b151c6e2b182Bill Wendling  llvm::Value *getExceptionFromSlot();
1057ae270598d5c7a9a283d4b3ddce53b151c6e2b182Bill Wendling  llvm::Value *getSelectorFromSlot();
1058ae270598d5c7a9a283d4b3ddce53b151c6e2b182Bill Wendling
1059ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  llvm::Value *getNormalCleanupDestSlot();
1060ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
1061f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  llvm::BasicBlock *getUnreachableBlock() {
1062f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    if (!UnreachableBlock) {
1063f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      UnreachableBlock = createBasicBlock("unreachable");
1064f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      new llvm::UnreachableInst(getLLVMContext(), UnreachableBlock);
1065f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    }
1066f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    return UnreachableBlock;
1067f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  }
1068f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1069f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  llvm::BasicBlock *getInvokeDest() {
10706bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    if (!EHStack.requiresLandingPad()) return nullptr;
1071f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    return getInvokeDestImpl();
1072f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  }
10739834ffbe54788239c8361d3cfe5826fd277ddfb2Daniel Dunbar
10740e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  bool currentFunctionUsesSEHTry() const {
10750e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    const auto *FD = dyn_cast_or_null<FunctionDecl>(CurCodeDecl);
10760e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    return FD && FD->usesSEHTry();
10770e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  }
10780e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
107964aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall  const TargetInfo &getTarget() const { return Target; }
1080d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall  llvm::LLVMContext &getLLVMContext() { return CGM.getLLVMContext(); }
108169243825cb5c91ec7207256aa57ae327cfaf8cb2Owen Anderson
10829834ffbe54788239c8361d3cfe5826fd277ddfb2Daniel Dunbar  //===--------------------------------------------------------------------===//
1083bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  //                                  Cleanups
1084bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  //===--------------------------------------------------------------------===//
1085bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
1086bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  typedef void Destroyer(CodeGenFunction &CGF, llvm::Value *addr, QualType ty);
1087bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
10882673c68aa58e277ebc755b71d81aca618cdedbf9John McCall  void pushIrregularPartialArrayCleanup(llvm::Value *arrayBegin,
10892673c68aa58e277ebc755b71d81aca618cdedbf9John McCall                                        llvm::Value *arrayEndPointer,
10902673c68aa58e277ebc755b71d81aca618cdedbf9John McCall                                        QualType elementType,
1091516bbd42e62d709013824d6fb8445a0cfda3129aPeter Collingbourne                                        Destroyer *destroyer);
10922673c68aa58e277ebc755b71d81aca618cdedbf9John McCall  void pushRegularPartialArrayCleanup(llvm::Value *arrayBegin,
10932673c68aa58e277ebc755b71d81aca618cdedbf9John McCall                                      llvm::Value *arrayEnd,
10942673c68aa58e277ebc755b71d81aca618cdedbf9John McCall                                      QualType elementType,
1095516bbd42e62d709013824d6fb8445a0cfda3129aPeter Collingbourne                                      Destroyer *destroyer);
1096bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
10979928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall  void pushDestroy(QualType::DestructionKind dtorKind,
10989928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall                   llvm::Value *addr, QualType type);
1099074cae0861a87bf96d8ea56d02e34839d9ccbd0aJohn McCall  void pushEHDestroy(QualType::DestructionKind dtorKind,
1100074cae0861a87bf96d8ea56d02e34839d9ccbd0aJohn McCall                     llvm::Value *addr, QualType type);
1101bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  void pushDestroy(CleanupKind kind, llvm::Value *addr, QualType type,
1102516bbd42e62d709013824d6fb8445a0cfda3129aPeter Collingbourne                   Destroyer *destroyer, bool useEHCleanupForArray);
11038a07cd3f59a0ede54906bdc8ed21724815db27adRichard Smith  void pushLifetimeExtendedDestroy(CleanupKind kind, llvm::Value *addr,
11048a07cd3f59a0ede54906bdc8ed21724815db27adRichard Smith                                   QualType type, Destroyer *destroyer,
11058a07cd3f59a0ede54906bdc8ed21724815db27adRichard Smith                                   bool useEHCleanupForArray);
1106176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  void pushCallObjectDeleteCleanup(const FunctionDecl *OperatorDelete,
1107176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                                   llvm::Value *CompletePtr,
1108176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                                   QualType ElementType);
1109651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void pushStackRestore(CleanupKind kind, llvm::Value *SPMem);
1110516bbd42e62d709013824d6fb8445a0cfda3129aPeter Collingbourne  void emitDestroy(llvm::Value *addr, QualType type, Destroyer *destroyer,
11112673c68aa58e277ebc755b71d81aca618cdedbf9John McCall                   bool useEHCleanupForArray);
1112c7971a9efdf9880448a69aabb5182c3c27eecf6dDavid Blaikie  llvm::Function *generateDestroyHelper(llvm::Constant *addr, QualType type,
1113516bbd42e62d709013824d6fb8445a0cfda3129aPeter Collingbourne                                        Destroyer *destroyer,
1114c7971a9efdf9880448a69aabb5182c3c27eecf6dDavid Blaikie                                        bool useEHCleanupForArray,
1115c7971a9efdf9880448a69aabb5182c3c27eecf6dDavid Blaikie                                        const VarDecl *VD);
1116bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  void emitArrayDestroy(llvm::Value *begin, llvm::Value *end,
1117516bbd42e62d709013824d6fb8445a0cfda3129aPeter Collingbourne                        QualType type, Destroyer *destroyer,
1118fbf780a9d1dbb191fc40c8af967c590e08724b74John McCall                        bool checkZeroLength, bool useEHCleanup);
1119bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
1120516bbd42e62d709013824d6fb8445a0cfda3129aPeter Collingbourne  Destroyer *getDestroyer(QualType::DestructionKind destructionKind);
11219928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall
1122bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  /// Determines whether an EH cleanup is required to destroy a type
1123bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  /// with the given destruction kind.
1124bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  bool needsEHCleanup(QualType::DestructionKind kind) {
1125bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    switch (kind) {
1126bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    case QualType::DK_none:
1127bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall      return false;
1128bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    case QualType::DK_cxx_destructor:
1129bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    case QualType::DK_objc_weak_lifetime:
11304e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      return getLangOpts().Exceptions;
1131bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    case QualType::DK_objc_strong_lifetime:
11324e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      return getLangOpts().Exceptions &&
1133bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall             CGM.getCodeGenOpts().ObjCAutoRefCountExceptions;
1134bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    }
1135bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    llvm_unreachable("bad destruction kind");
1136bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  }
1137bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
11389928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall  CleanupKind getCleanupKind(QualType::DestructionKind kind) {
11399928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall    return (needsEHCleanup(kind) ? NormalAndEHCleanup : NormalCleanup);
11409928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall  }
11419928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall
1142bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  //===--------------------------------------------------------------------===//
11439834ffbe54788239c8361d3cfe5826fd277ddfb2Daniel Dunbar  //                                  Objective-C
11449834ffbe54788239c8361d3cfe5826fd277ddfb2Daniel Dunbar  //===--------------------------------------------------------------------===//
11459834ffbe54788239c8361d3cfe5826fd277ddfb2Daniel Dunbar
1146391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  void GenerateObjCMethod(const ObjCMethodDecl *OMD);
1147af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
11480e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  void StartObjCMethod(const ObjCMethodDecl *MD, const ObjCContainerDecl *CD);
1149af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
11500dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// GenerateObjCGetter - Synthesize an Objective-C property getter function.
1151fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian  void GenerateObjCGetter(ObjCImplementationDecl *IMP,
1152fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian                          const ObjCPropertyImplDecl *PID);
11531e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall  void generateObjCGetterBody(const ObjCImplementationDecl *classImpl,
1154b6e5fe3cae9bf456f084c555c3ecc256f4181cc5Fariborz Jahanian                              const ObjCPropertyImplDecl *propImpl,
1155490a52b4947381879a47b4251db5fb81cdf5d02bFariborz Jahanian                              const ObjCMethodDecl *GetterMothodDecl,
1156b6e5fe3cae9bf456f084c555c3ecc256f4181cc5Fariborz Jahanian                              llvm::Constant *AtomicHelperFn);
11572846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian
1158109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian  void GenerateObjCCtorDtorMethod(ObjCImplementationDecl *IMP,
1159109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian                                  ObjCMethodDecl *MD, bool ctor);
1160af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
11610dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// GenerateObjCSetter - Synthesize an Objective-C property setter function
11620dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// for the given property.
1163fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian  void GenerateObjCSetter(ObjCImplementationDecl *IMP,
1164fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian                          const ObjCPropertyImplDecl *PID);
116571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall  void generateObjCSetterBody(const ObjCImplementationDecl *classImpl,
1166cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian                              const ObjCPropertyImplDecl *propImpl,
1167cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian                              llvm::Constant *AtomicHelperFn);
11680b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian  bool IndirectObjCSetterArg(const CGFunctionInfo &FI);
116915bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian  bool IvarTypeWithAggrGCObjects(QualType Ty);
1170af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
11714e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump  //===--------------------------------------------------------------------===//
11724e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump  //                                  Block Bits
11734e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump  //===--------------------------------------------------------------------===//
11744e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump
11756b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  llvm::Value *EmitBlockLiteral(const BlockExpr *);
11761a343ebbf413e8eae6b2737b2b2d79cbf5765571John McCall  llvm::Value *EmitBlockLiteral(const CGBlockInfo &Info);
11771a343ebbf413e8eae6b2737b2b2d79cbf5765571John McCall  static void destroyBlockInfos(CGBlockInfo *info);
11782a7eb28397148079cbc8e54e8a3871ef01c4f4bcBlaine Garst  llvm::Constant *BuildDescriptorBlockDecl(const BlockExpr *,
117989ecd41e0a6bfb3b0913dbe41c3c666340b308c7Fariborz Jahanian                                           const CGBlockInfo &Info,
11802acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner                                           llvm::StructType *,
1181d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall                                           llvm::Constant *BlockVarLayout);
11824e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump
1183564360be450b319aeafc26698be9811837bfb826Fariborz Jahanian  llvm::Function *GenerateBlockFunction(GlobalDecl GD,
11846b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall                                        const CGBlockInfo &Info,
118564bee65a3436e3f0c352fcfe2130676f3502cffeEli Friedman                                        const DeclMapTy &ldm,
118664bee65a3436e3f0c352fcfe2130676f3502cffeEli Friedman                                        bool IsLambdaConversionToBlock);
11874e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump
1188d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall  llvm::Constant *GenerateCopyHelperFunction(const CGBlockInfo &blockInfo);
1189d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall  llvm::Constant *GenerateDestroyHelperFunction(const CGBlockInfo &blockInfo);
119020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian  llvm::Constant *GenerateObjCAtomicSetterCopyHelperFunction(
119120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian                                             const ObjCPropertyImplDecl *PID);
119220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian  llvm::Constant *GenerateObjCAtomicGetterCopyHelperFunction(
119320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian                                             const ObjCPropertyImplDecl *PID);
1194cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman  llvm::Value *EmitBlockCopyAndAutorelease(llvm::Value *Block, QualType Ty);
1195d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall
1196d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall  void BuildBlockRelease(llvm::Value *DeclPtr, BlockFieldFlags flags);
1197d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall
11985af02db5a48476e0748f135369663080eae87c64John McCall  class AutoVarEmission;
11995af02db5a48476e0748f135369663080eae87c64John McCall
12005af02db5a48476e0748f135369663080eae87c64John McCall  void emitByrefStructureInit(const AutoVarEmission &emission);
12015af02db5a48476e0748f135369663080eae87c64John McCall  void enterByrefCleanup(const AutoVarEmission &emission);
12025af02db5a48476e0748f135369663080eae87c64John McCall
12036b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  llvm::Value *LoadBlockStruct() {
12046b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall    assert(BlockPointer && "no block pointer set!");
12056b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall    return BlockPointer;
12066b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  }
12074e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump
1208ea1471e0e967548c596a71469702f8846dbaf3c0John McCall  void AllocateBlockCXXThisPointer(const CXXThisExpr *E);
1209f4b88a45902af1802a1cb42ba48b1c474474f228John McCall  void AllocateBlockDecl(const DeclRefExpr *E);
12106b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  llvm::Value *GetAddrOfBlockDecl(const VarDecl *var, bool ByRef);
12112acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *BuildByRefType(const VarDecl *var);
1212dab514fc30242c7afd6c03956e46136c400fb0d3Mike Stump
1213d26bc76c98006609002d9930f8840490e88ac5b5John McCall  void GenerateCode(GlobalDecl GD, llvm::Function *Fn,
1214d26bc76c98006609002d9930f8840490e88ac5b5John McCall                    const CGFunctionInfo &FnInfo);
12156bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  /// \brief Emit code for the start of a function.
12166bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  /// \param Loc       The location to be associated with the function.
12176bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  /// \param StartLoc  The location of the function body.
1218f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall  void StartFunction(GlobalDecl GD,
1219f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall                     QualType RetTy,
12207c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar                     llvm::Function *Fn,
1221d26bc76c98006609002d9930f8840490e88ac5b5John McCall                     const CGFunctionInfo &FnInfo,
12222284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar                     const FunctionArgList &Args,
12236bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                     SourceLocation Loc = SourceLocation(),
12246bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                     SourceLocation StartLoc = SourceLocation());
1225a448fb2da03ece39978784793eea68760e8205a1Daniel Dunbar
12269fc6a7774643a810c8501dae2323e863fefb623eJohn McCall  void EmitConstructorBody(FunctionArgList &Args);
12279fc6a7774643a810c8501dae2323e863fefb623eJohn McCall  void EmitDestructorBody(FunctionArgList &Args);
122856c00c4868831c9a137ca7b0e16d063cf986d110Lang Hames  void emitImplicitAssignmentOperatorBody(FunctionArgList &Args);
12293cebc73895daccea85984d8881b5b45c8f8df9c6Richard Smith  void EmitFunctionBody(FunctionArgList &Args, const Stmt *Body);
1230651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void EmitBlockWithFallThrough(llvm::BasicBlock *BB, RegionCounter &Cnt);
1231a355e07454463b19829ac92ffd115a097faff0e0John McCall
1232d6992ab33b7113e1bd7af51c0c52d17c23706c01Faisal Vali  void EmitForwardingCallToLambda(const CXXMethodDecl *LambdaCallOperator,
123364bee65a3436e3f0c352fcfe2130676f3502cffeEli Friedman                                  CallArgList &CallArgs);
1234bd89f8c2caa9550e41daa1aa9bf30f0f1e0dfaf7Eli Friedman  void EmitLambdaToBlockPointerBody(FunctionArgList &Args);
123564bee65a3436e3f0c352fcfe2130676f3502cffeEli Friedman  void EmitLambdaBlockInvokeBody();
123627dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor  void EmitLambdaDelegatingInvokeBody(const CXXMethodDecl *MD);
123727dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor  void EmitLambdaStaticInvokeFunction(const CXXMethodDecl *MD);
1238176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  void EmitAsanPrologueOrEpilogue(bool Prologue);
1239bd89f8c2caa9550e41daa1aa9bf30f0f1e0dfaf7Eli Friedman
12400e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  /// \brief Emit the unified return block, trying to avoid its emission when
12410e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  /// possible.
12420e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  /// \return The debug location of the user written return statement if the
12430e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  /// return block is is avoided.
12440e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  llvm::DebugLoc EmitReturnBlock();
12451c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
12460dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// FinishFunction - Complete IR generation of the current function. It is
12470dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// legal to call this function even if there is no current insertion point.
1248af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar  void FinishFunction(SourceLocation EndLoc=SourceLocation());
124917b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar
12500e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  void StartThunk(llvm::Function *Fn, GlobalDecl GD,
12510e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                  const CGFunctionInfo &FnInfo);
125293b717ac956f5c82f7895ed8874cbd514c6d0a4eHans Wennborg
1253176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  void EmitCallAndReturnForThunk(llvm::Value *Callee, const ThunkInfo *Thunk);
1254176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
1255176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  /// Emit a musttail call for a thunk with a potentially adjusted this pointer.
1256176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  void EmitMustTailThunk(const CXXMethodDecl *MD, llvm::Value *AdjustedThisPtr,
1257176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                         llvm::Value *Callee);
125893b717ac956f5c82f7895ed8874cbd514c6d0a4eHans Wennborg
1259519c32880b99396034ecaedc0cd86db2e8485003Anders Carlsson  /// GenerateThunk - Generate a thunk for the given method.
1260d26bc76c98006609002d9930f8840490e88ac5b5John McCall  void GenerateThunk(llvm::Function *Fn, const CGFunctionInfo &FnInfo,
1261d26bc76c98006609002d9930f8840490e88ac5b5John McCall                     GlobalDecl GD, const ThunkInfo &Thunk);
12629cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
12637dcdf5ba9324a9577461eae302e88fdd52e310c5Eli Friedman  void GenerateVarArgsThunk(llvm::Function *Fn, const CGFunctionInfo &FnInfo,
12647dcdf5ba9324a9577461eae302e88fdd52e310c5Eli Friedman                            GlobalDecl GD, const ThunkInfo &Thunk);
12657dcdf5ba9324a9577461eae302e88fdd52e310c5Eli Friedman
1266fb8cc253420e93cee33d29df5a2bdae6aaf16e39Douglas Gregor  void EmitCtorPrologue(const CXXConstructorDecl *CD, CXXCtorType Type,
1267fb8cc253420e93cee33d29df5a2bdae6aaf16e39Douglas Gregor                        FunctionArgList &Args);
12681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1269b74ed087469db042325bad76fdf3ff6ed67dec09Eli Friedman  void EmitInitializerForField(FieldDecl *Field, LValue LHS, Expr *Init,
1270b74ed087469db042325bad76fdf3ff6ed67dec09Eli Friedman                               ArrayRef<VarDecl *> ArrayIndexes);
1271b74ed087469db042325bad76fdf3ff6ed67dec09Eli Friedman
1272d103f9f9b401b419e756f8c1849683cd77586067Anders Carlsson  /// InitializeVTablePointer - Initialize the vtable pointer of the given
1273d103f9f9b401b419e756f8c1849683cd77586067Anders Carlsson  /// subobject.
1274d103f9f9b401b419e756f8c1849683cd77586067Anders Carlsson  ///
12759cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer  void InitializeVTablePointer(BaseSubobject Base,
1276b3b772ea15a4cd54879e244629aa685ead9548bbAnders Carlsson                               const CXXRecordDecl *NearestVBase,
1277d6fb21fa1053e5d616af55b181bb03c50d4b0d24Ken Dyck                               CharUnits OffsetFromNearestVBase,
1278d103f9f9b401b419e756f8c1849683cd77586067Anders Carlsson                               const CXXRecordDecl *VTableClass);
1279d103f9f9b401b419e756f8c1849683cd77586067Anders Carlsson
1280d103f9f9b401b419e756f8c1849683cd77586067Anders Carlsson  typedef llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBasesSetTy;
12819cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer  void InitializeVTablePointers(BaseSubobject Base,
1282b3b772ea15a4cd54879e244629aa685ead9548bbAnders Carlsson                                const CXXRecordDecl *NearestVBase,
1283d6fb21fa1053e5d616af55b181bb03c50d4b0d24Ken Dyck                                CharUnits OffsetFromNearestVBase,
1284603d6d12cc4cba1ef219a3d55c698c7c87f87adfAnders Carlsson                                bool BaseIsNonVirtualPrimaryBase,
1285603d6d12cc4cba1ef219a3d55c698c7c87f87adfAnders Carlsson                                const CXXRecordDecl *VTableClass,
1286603d6d12cc4cba1ef219a3d55c698c7c87f87adfAnders Carlsson                                VisitedVirtualBasesSetTy& VBases);
128777a259c6dbf582949af8a243b472c0c7faa373a6Eli Friedman
1288603d6d12cc4cba1ef219a3d55c698c7c87f87adfAnders Carlsson  void InitializeVTablePointers(const CXXRecordDecl *ClassDecl);
1289d103f9f9b401b419e756f8c1849683cd77586067Anders Carlsson
1290043fb9a1fc0609285f60f0f87e5a18195408f34cDan Gohman  /// GetVTablePtr - Return the Value of the vtable pointer member pointed
1291043fb9a1fc0609285f60f0f87e5a18195408f34cDan Gohman  /// to by This.
12922acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Value *GetVTablePtr(llvm::Value *This, llvm::Type *Ty);
1293d103f9f9b401b419e756f8c1849683cd77586067Anders Carlsson
12943ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar  /// \brief Derived is the presumed address of an object of type T after a
12953ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar  /// cast. If T is a polymorphic class type, emit a check that the virtual
12963ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar  /// table for Derived belongs to a class derived from T.
12973ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar  void EmitVTablePtrCheckForCast(QualType T, llvm::Value *Derived,
12983ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar                                 bool MayBeNull);
12993ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar
13000e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  /// EmitVTablePtrCheckForCall - Virtual method MD is being called via VTable.
13010e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  /// If vptr CFI is enabled, emit a check that VTable is valid.
13020e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  void EmitVTablePtrCheckForCall(const CXXMethodDecl *MD, llvm::Value *VTable);
13039581ed07dee5376002620a0cfb363c6b9e5bdd3eBenjamin Kramer
13043ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar  /// EmitVTablePtrCheck - Emit a check that VTable is a valid virtual table for
13053ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar  /// RD using llvm.bitset.test.
13063ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar  void EmitVTablePtrCheck(const CXXRecordDecl *RD, llvm::Value *VTable);
13073ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar
13089581ed07dee5376002620a0cfb363c6b9e5bdd3eBenjamin Kramer  /// CanDevirtualizeMemberFunctionCalls - Checks whether virtual calls on given
13099581ed07dee5376002620a0cfb363c6b9e5bdd3eBenjamin Kramer  /// expr can be devirtualized.
13109581ed07dee5376002620a0cfb363c6b9e5bdd3eBenjamin Kramer  bool CanDevirtualizeMemberFunctionCall(const Expr *Base,
13119581ed07dee5376002620a0cfb363c6b9e5bdd3eBenjamin Kramer                                         const CXXMethodDecl *MD);
13129581ed07dee5376002620a0cfb363c6b9e5bdd3eBenjamin Kramer
131350da2cadcc6da86abff6772de65280ace2cabc94John McCall  /// EnterDtorCleanups - Enter the cleanups necessary to complete the
131450da2cadcc6da86abff6772de65280ace2cabc94John McCall  /// given phase of destruction for a destructor.  The end result
131550da2cadcc6da86abff6772de65280ace2cabc94John McCall  /// should call destructors on members and base classes in reverse
131650da2cadcc6da86abff6772de65280ace2cabc94John McCall  /// order of their construction.
131750da2cadcc6da86abff6772de65280ace2cabc94John McCall  void EnterDtorCleanups(const CXXDestructorDecl *Dtor, CXXDtorType Type);
13181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13197255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  /// ShouldInstrumentFunction - Return true if the current function should be
13207255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  /// instrumented with __cyg_profile_func_* calls
13217255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  bool ShouldInstrumentFunction();
13227255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner
13237255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  /// EmitFunctionInstrumentation - Emit LLVM code to call the specified
13247255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  /// instrumentation function with the current function and the call site, if
13257255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  /// function instrumentation is enabled.
13267255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  void EmitFunctionInstrumentation(const char *Fn);
13277255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner
1328be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky  /// EmitMCountInstrumentation - Emit call to .mcount.
1329be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky  void EmitMCountInstrumentation();
1330be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky
13310dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// EmitFunctionProlog - Emit the target specific LLVM code to load the
13320dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// arguments for the given function. This is also responsible for naming the
13330dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// LLVM function arguments.
133488b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar  void EmitFunctionProlog(const CGFunctionInfo &FI,
133588b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar                          llvm::Function *Fn,
133617b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar                          const FunctionArgList &Args);
133717b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar
13380dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// EmitFunctionEpilog - Emit the target specific LLVM code to return the
13390dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// given temporary.
13404ee7dc2369c1f0257a73b2e83a7d38fdebdd9176Nick Lewycky  void EmitFunctionEpilog(const CGFunctionInfo &FI, bool EmitRetDbgLoc,
13414ee7dc2369c1f0257a73b2e83a7d38fdebdd9176Nick Lewycky                          SourceLocation EndLoc);
134217b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar
1343cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump  /// EmitStartEHSpec - Emit the start of the exception spec.
1344cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump  void EmitStartEHSpec(const Decl *D);
1345cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump
1346cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump  /// EmitEndEHSpec - Emit the end of the exception spec.
1347cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump  void EmitEndEHSpec(const Decl *D);
1348cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump
1349f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// getTerminateLandingPad - Return a landing pad that just calls terminate.
1350f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  llvm::BasicBlock *getTerminateLandingPad();
1351f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1352f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// getTerminateHandler - Return a handler (not a landing pad, just
1353f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// a catch handler) that just calls terminate.  This is used when
1354f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// a terminate scope encloses a try.
13559b39c51ae3c547568ac42325f94b4197618f6b18Mike Stump  llvm::BasicBlock *getTerminateHandler();
13569b39c51ae3c547568ac42325f94b4197618f6b18Mike Stump
13579cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::Type *ConvertTypeForMem(QualType T);
13589cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::Type *ConvertType(QualType T);
13599cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::Type *ConvertType(const TypeDecl *T) {
1360bff225ecf77fb891596ecb1b27196310d268365eJohn McCall    return ConvertType(getContext().getTypeDeclType(T));
1361bff225ecf77fb891596ecb1b27196310d268365eJohn McCall  }
1362c8aa5f1f264fb230c38182adab944232bb160c2bChris Lattner
13630dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// LoadObjCSelf - Load the value of self. This function is only valid while
13640dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// generating code for an Objective-C method.
1365c8aa5f1f264fb230c38182adab944232bb160c2bChris Lattner  llvm::Value *LoadObjCSelf();
13660dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
13670dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// TypeOfSelfObject - Return type of object that this self represents.
136845012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz Jahanian  QualType TypeOfSelfObject();
13694111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner
13705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// hasAggregateLLVMType - Return true if the specified AST type will map into
13715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// an aggregate LLVM type or is void.
13729d232c884ea9872d6555df0fd7359699819bc1f1John McCall  static TypeEvaluationKind getEvaluationKind(QualType T);
13739d232c884ea9872d6555df0fd7359699819bc1f1John McCall
13749d232c884ea9872d6555df0fd7359699819bc1f1John McCall  static bool hasScalarEvaluationKind(QualType T) {
13759d232c884ea9872d6555df0fd7359699819bc1f1John McCall    return getEvaluationKind(T) == TEK_Scalar;
13769d232c884ea9872d6555df0fd7359699819bc1f1John McCall  }
13779d232c884ea9872d6555df0fd7359699819bc1f1John McCall
13789d232c884ea9872d6555df0fd7359699819bc1f1John McCall  static bool hasAggregateEvaluationKind(QualType T) {
13799d232c884ea9872d6555df0fd7359699819bc1f1John McCall    return getEvaluationKind(T) == TEK_Aggregate;
13809d232c884ea9872d6555df0fd7359699819bc1f1John McCall  }
138155e874299f2ad827646a4ca9ea38c402aaeb38c9Daniel Dunbar
138255e874299f2ad827646a4ca9ea38c402aaeb38c9Daniel Dunbar  /// createBasicBlock - Create an LLVM basic block.
13834def70d3040e73707c738f7c366737a986135edfRichard Smith  llvm::BasicBlock *createBasicBlock(const Twine &name = "",
13846bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                     llvm::Function *parent = nullptr,
13856bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                     llvm::BasicBlock *before = nullptr) {
138629ea6729155c759836be4b5453bee9ef8e1e97eeDaniel Dunbar#ifdef NDEBUG
1387d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall    return llvm::BasicBlock::Create(getLLVMContext(), "", parent, before);
138829ea6729155c759836be4b5453bee9ef8e1e97eeDaniel Dunbar#else
1389d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall    return llvm::BasicBlock::Create(getLLVMContext(), name, parent, before);
139029ea6729155c759836be4b5453bee9ef8e1e97eeDaniel Dunbar#endif
139155e874299f2ad827646a4ca9ea38c402aaeb38c9Daniel Dunbar  }
13920dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
13935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// getBasicBlockForLabel - Return the LLVM basicblock that the specified
13945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// label maps to.
1395ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  JumpDest getJumpDestForLabel(const LabelDecl *S);
13960dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
1397f71d232ce5354bd2f0c94ded30fb936c2a3d968bMike Stump  /// SimplifyForwardingBlocks - If the given basic block is only a branch to
1398f71d232ce5354bd2f0c94ded30fb936c2a3d968bMike Stump  /// another basic block, simplify it. This assumes that no other code could
1399f71d232ce5354bd2f0c94ded30fb936c2a3d968bMike Stump  /// potentially reference the basic block.
1400aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar  void SimplifyForwardingBlocks(llvm::BasicBlock *BB);
1401aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar
14020dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// EmitBlock - Emit the given block \arg BB and set it as the insert point,
14030dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// adding a fall-through branch from the current insert block if
14040dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// necessary. It is legal to call this function even if there is no current
14050dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// insertion point.
1406a0c21a8faa79e88ac432d116eca58f7a7217195dDaniel Dunbar  ///
14070dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// IsFinished - If true, indicates that the caller has finished emitting
14080dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// branches to the given block and does not expect to emit code into it. This
14090dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// means the block can be ignored if it is unreachable.
1410a0c21a8faa79e88ac432d116eca58f7a7217195dDaniel Dunbar  void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false);
1411824e3bd76b2e32db2ec6e4d2d113413e518d1c63Daniel Dunbar
1412777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  /// EmitBlockAfterUses - Emit the given block somewhere hopefully
1413777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  /// near its uses, and leave the insertion point in it.
1414777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  void EmitBlockAfterUses(llvm::BasicBlock *BB);
1415777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall
14160dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// EmitBranch - Emit a branch to the specified basic block from the current
14170dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// insert block, taking care to avoid creation of branches from dummy
14180dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// blocks. It is legal to call this function even if there is no current
14190dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// insertion point.
14205e08ad3cc62ab94649959ae227a9a411a729bf49Daniel Dunbar  ///
14210dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// This function clears the current insertion point. The caller should follow
14220dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// calls to this function with calls to Emit*Block prior to generation new
14230dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// code.
1424d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar  void EmitBranch(llvm::BasicBlock *Block);
1425d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar
14260dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// HaveInsertPoint - True if an insertion point is defined. If not, this
14270dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// indicates that the current code being emitted is unreachable.
14280dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  bool HaveInsertPoint() const {
14296bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return Builder.GetInsertBlock() != nullptr;
1430a448fb2da03ece39978784793eea68760e8205a1Daniel Dunbar  }
1431a448fb2da03ece39978784793eea68760e8205a1Daniel Dunbar
14320dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// EnsureInsertPoint - Ensure that an insertion point is defined so that
14330dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// emitted IR has a place to go. Note that by definition, if this function
14340dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// creates a block then that block is unreachable; callers may do better to
14350dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// detect when no insertion point is defined and simply skip IR generation.
1436a448fb2da03ece39978784793eea68760e8205a1Daniel Dunbar  void EnsureInsertPoint() {
1437a448fb2da03ece39978784793eea68760e8205a1Daniel Dunbar    if (!HaveInsertPoint())
1438a448fb2da03ece39978784793eea68760e8205a1Daniel Dunbar      EmitBlock(createBasicBlock());
1439a448fb2da03ece39978784793eea68760e8205a1Daniel Dunbar  }
14400dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
1441488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar  /// ErrorUnsupported - Print out an error that codegen doesn't support the
1442dc5e8268292046114ffe02e48773572a91a310f1Chris Lattner  /// specified stmt yet.
14430a1c862914664029e3b6693e134cf3ccbf423844David Blaikie  void ErrorUnsupported(const Stmt *S, const char *Type);
14445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //===--------------------------------------------------------------------===//
14465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //                                  Helpers
14475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //===--------------------------------------------------------------------===//
14480dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
14496da2c716017d5c8530ec99779524491ebc5dadb8Eli Friedman  LValue MakeAddrLValue(llvm::Value *V, QualType T,
14506da2c716017d5c8530ec99779524491ebc5dadb8Eli Friedman                        CharUnits Alignment = CharUnits()) {
14513d5aff5d3036b0ff09d114857cd2276134b3d8c9Dan Gohman    return LValue::MakeAddr(V, T, Alignment, getContext(),
14523d5aff5d3036b0ff09d114857cd2276134b3d8c9Dan Gohman                            CGM.getTBAAInfo(T));
14532f77b3d57031ded0de2b7b977fb63b4660f82624Eli Friedman  }
1454e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall
1455176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  LValue MakeNaturalAlignAddrLValue(llvm::Value *V, QualType T);
14565cf8bfec711116b3c4acc70a00717b2e119e7550Daniel Dunbar
14575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// CreateTempAlloca - This creates a alloca and inserts it into the entry
1458195337d2e5d4625ae9dc1328c7cdbc7115b0261bDaniel Dunbar  /// block. The caller is responsible for setting an appropriate alignment on
1459195337d2e5d4625ae9dc1328c7cdbc7115b0261bDaniel Dunbar  /// the alloca.
14602acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::AllocaInst *CreateTempAlloca(llvm::Type *Ty,
14618cc488fefb2fb04bc8d5398da29f0182f97934cfChris Lattner                                     const Twine &Name = "tmp");
14620dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
1463ac418162692a951ca3796d6830496a85a2d12493John McCall  /// InitTempAlloca - Provide an initial value for the given alloca.
1464ac418162692a951ca3796d6830496a85a2d12493John McCall  void InitTempAlloca(llvm::AllocaInst *Alloca, llvm::Value *Value);
1465ac418162692a951ca3796d6830496a85a2d12493John McCall
14669bd4da2cfa8b956b851200db8f9a37e175edbb15Daniel Dunbar  /// CreateIRTemp - Create a temporary IR object of the given type, with
14679bd4da2cfa8b956b851200db8f9a37e175edbb15Daniel Dunbar  /// appropriate alignment. This routine should only be used when an temporary
14689bd4da2cfa8b956b851200db8f9a37e175edbb15Daniel Dunbar  /// value needs to be stored into an alloca (for example, to avoid explicit
14699bd4da2cfa8b956b851200db8f9a37e175edbb15Daniel Dunbar  /// PHI construction), but the type is the IR type, not the type appropriate
14709bd4da2cfa8b956b851200db8f9a37e175edbb15Daniel Dunbar  /// for storing in memory.
14718cc488fefb2fb04bc8d5398da29f0182f97934cfChris Lattner  llvm::AllocaInst *CreateIRTemp(QualType T, const Twine &Name = "tmp");
14729bd4da2cfa8b956b851200db8f9a37e175edbb15Daniel Dunbar
1473195337d2e5d4625ae9dc1328c7cdbc7115b0261bDaniel Dunbar  /// CreateMemTemp - Create a temporary memory object of the given type, with
1474195337d2e5d4625ae9dc1328c7cdbc7115b0261bDaniel Dunbar  /// appropriate alignment.
14758cc488fefb2fb04bc8d5398da29f0182f97934cfChris Lattner  llvm::AllocaInst *CreateMemTemp(QualType T, const Twine &Name = "tmp");
1476195337d2e5d4625ae9dc1328c7cdbc7115b0261bDaniel Dunbar
1477558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  /// CreateAggTemp - Create a temporary memory object for the given
1478558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  /// aggregate type.
14798cc488fefb2fb04bc8d5398da29f0182f97934cfChris Lattner  AggValueSlot CreateAggTemp(QualType T, const Twine &Name = "tmp") {
1480d7722d9d76a851e7897f4127626616d3b1b8e530Eli Friedman    CharUnits Alignment = getContext().getTypeAlignInChars(T);
1481f394078fde147dcf27e9b6a7965517388d64dcb6Eli Friedman    return AggValueSlot::forAddr(CreateMemTemp(T, Name), Alignment,
1482f394078fde147dcf27e9b6a7965517388d64dcb6Eli Friedman                                 T.getQualifiers(),
14837c2349be2d11143a2e59a167fd43362a3bf4585eJohn McCall                                 AggValueSlot::IsNotDestructed,
1484410ffb2bc5f072d58a73c14560345bcf77dec1ccJohn McCall                                 AggValueSlot::DoesNotNeedGCBarriers,
1485649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier                                 AggValueSlot::IsNotAliased);
1486558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  }
1487558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall
1488651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// CreateInAllocaTmp - Create a temporary memory object for the given
1489651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// aggregate type.
1490651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  AggValueSlot CreateInAllocaTmp(QualType T, const Twine &Name = "inalloca");
1491651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
1492d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall  /// Emit a cast to void* in the appropriate address space.
1493d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall  llvm::Value *EmitCastToVoidPtr(llvm::Value *value);
1494d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall
14955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// EvaluateExprAsBool - Perform the usual unary conversions on the specified
14965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// expression and compare the result against zero, returning an Int1Ty value.
14975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::Value *EvaluateExprAsBool(const Expr *E);
14985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14992a41637a995affa1563f4d82a8b026e326a2faa0John McCall  /// EmitIgnoredExpr - Emit an expression in a context which ignores the result.
15002a41637a995affa1563f4d82a8b026e326a2faa0John McCall  void EmitIgnoredExpr(const Expr *E);
15012a41637a995affa1563f4d82a8b026e326a2faa0John McCall
15029b65551d0b387a7597fb39356a4d8ef10046445eChris Lattner  /// EmitAnyExpr - Emit code to compute the specified expression which can have
15039b65551d0b387a7597fb39356a4d8ef10046445eChris Lattner  /// any type.  The result is returned as an RValue struct.  If this is an
15049b65551d0b387a7597fb39356a4d8ef10046445eChris Lattner  /// aggregate expression, the aggloc/agglocvolatile arguments indicate where
15059b65551d0b387a7597fb39356a4d8ef10046445eChris Lattner  /// the result should be returned.
150649d1cd5a09ed3df353371fd7f206674a85e0fb45Mike Stump  ///
150770517ca5c07c4b41ff8662b94ee22047b0299f8cDmitri Gribenko  /// \param ignoreResult True if the resulting value isn't used.
1508558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  RValue EmitAnyExpr(const Expr *E,
1509e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall                     AggValueSlot aggSlot = AggValueSlot::ignored(),
1510e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall                     bool ignoreResult = false);
1511d9363c3a80168283b3da518b4e17f545a6246857Devang Patel
15120dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  // EmitVAListRef - Emit a "reference" to a va_list; this is either the address
15130dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  // or the value of the expression, depending on how va_list is defined.
15144fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman  llvm::Value *EmitVAListRef(const Expr *E);
15154fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman
15160dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// EmitAnyExprToTemp - Similary to EmitAnyExpr(), however, the result will
15170dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// always be accessible even if no aggregate location is provided.
1518558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  RValue EmitAnyExprToTemp(const Expr *E);
151946f45b9bec4a265ad8400a538e5ec3a5683617f1Daniel Dunbar
152060d3365b46eb826fba44483583c0051ac5c41fe3John McCall  /// EmitAnyExprToMem - Emits the code necessary to evaluate an
1521649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier  /// arbitrary expression into the given memory location.
15223d3ec1c099ec8bfac3aa1fb0126fe515b7c7fa05John McCall  void EmitAnyExprToMem(const Expr *E, llvm::Value *Location,
1523649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier                        Qualifiers Quals, bool IsInitializer);
15243d3ec1c099ec8bfac3aa1fb0126fe515b7c7fa05John McCall
15253ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar  void EmitAnyExprToExn(const Expr *E, llvm::Value *Addr);
15263ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar
152760d3365b46eb826fba44483583c0051ac5c41fe3John McCall  /// EmitExprAsInit - Emits the code necessary to initialize a
152860d3365b46eb826fba44483583c0051ac5c41fe3John McCall  /// location in memory with the given initializer.
15290e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  void EmitExprAsInit(const Expr *init, const ValueDecl *D, LValue lvalue,
15300e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                      bool capturedByInit);
153160d3365b46eb826fba44483583c0051ac5c41fe3John McCall
15323ac83d69c61238cd0d38e90fcdd03390530ab2fbFariborz Jahanian  /// hasVolatileMember - returns true if aggregate type has a volatile
15333ac83d69c61238cd0d38e90fcdd03390530ab2fbFariborz Jahanian  /// member.
15343ac83d69c61238cd0d38e90fcdd03390530ab2fbFariborz Jahanian  bool hasVolatileMember(QualType T) {
15353ac83d69c61238cd0d38e90fcdd03390530ab2fbFariborz Jahanian    if (const RecordType *RT = T->getAs<RecordType>()) {
15363ac83d69c61238cd0d38e90fcdd03390530ab2fbFariborz Jahanian      const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
15373ac83d69c61238cd0d38e90fcdd03390530ab2fbFariborz Jahanian      return RD->hasVolatileMember();
15383ac83d69c61238cd0d38e90fcdd03390530ab2fbFariborz Jahanian    }
15393ac83d69c61238cd0d38e90fcdd03390530ab2fbFariborz Jahanian    return false;
15403ac83d69c61238cd0d38e90fcdd03390530ab2fbFariborz Jahanian  }
15418fcbb8dfbc28b6279d22144e10ade401a6c03e9eArnaud A. de Grandmaison  /// EmitAggregateCopy - Emit an aggregate assignment.
15426cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer  ///
15436cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer  /// The difference to EmitAggregateCopy is that tail padding is not copied.
15446cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer  /// This is required for correctness when assigning non-POD structures in C++.
15456cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer  void EmitAggregateAssign(llvm::Value *DestPtr, llvm::Value *SrcPtr,
15463ac83d69c61238cd0d38e90fcdd03390530ab2fbFariborz Jahanian                           QualType EltTy) {
15473ac83d69c61238cd0d38e90fcdd03390530ab2fbFariborz Jahanian    bool IsVolatile = hasVolatileMember(EltTy);
15483ac83d69c61238cd0d38e90fcdd03390530ab2fbFariborz Jahanian    EmitAggregateCopy(DestPtr, SrcPtr, EltTy, IsVolatile, CharUnits::Zero(),
15493ac83d69c61238cd0d38e90fcdd03390530ab2fbFariborz Jahanian                      true);
15506cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer  }
15516cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer
15520e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  void EmitAggregateCopyCtor(llvm::Value *DestPtr, llvm::Value *SrcPtr,
15530e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                           QualType DestTy, QualType SrcTy) {
15540e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    CharUnits DestTypeAlign = getContext().getTypeAlignInChars(DestTy);
15550e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    CharUnits SrcTypeAlign = getContext().getTypeAlignInChars(SrcTy);
15560e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    EmitAggregateCopy(DestPtr, SrcPtr, SrcTy, /*IsVolatile=*/false,
15570e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                      std::min(DestTypeAlign, SrcTypeAlign),
15580e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                      /*IsAssignment=*/false);
15590e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  }
15600e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
15618fcbb8dfbc28b6279d22144e10ade401a6c03e9eArnaud A. de Grandmaison  /// EmitAggregateCopy - Emit an aggregate copy.
156227fe2e6c97a3782a0639d87b460741e8ba5d076dMike Stump  ///
1563f3477c13eeaf11b32a41f181398fb5deffd0dd73Sylvestre Ledru  /// \param isVolatile - True iff either the source or the destination is
156427fe2e6c97a3782a0639d87b460741e8ba5d076dMike Stump  /// volatile.
15656cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer  /// \param isAssignment - If false, allow padding to be copied.  This often
15666cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer  /// yields more efficient.
15677482d12c345c6391f8956850545e2d4aa7701ce6Daniel Dunbar  void EmitAggregateCopy(llvm::Value *DestPtr, llvm::Value *SrcPtr,
1568bd7d82878c1588afccbee6c68fa6e17bbbab7f2cEli Friedman                         QualType EltTy, bool isVolatile=false,
15696cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer                         CharUnits Alignment = CharUnits::Zero(),
15706cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer                         bool isAssignment = false);
15717482d12c345c6391f8956850545e2d4aa7701ce6Daniel Dunbar
157251b09f2c528c8460b5465c676173324e44176d62Devang Patel  /// StartBlock - Start new block named N. If insert block is a dummy block
157351b09f2c528c8460b5465c676173324e44176d62Devang Patel  /// then reuse it.
157451b09f2c528c8460b5465c676173324e44176d62Devang Patel  void StartBlock(const char *N);
157551b09f2c528c8460b5465c676173324e44176d62Devang Patel
1576dde0a94120915fa925d1ffcdb997c7b44dc9fa21Anders Carlsson  /// GetAddrOfLocalVar - Return the address of a local variable.
15774c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  llvm::Value *GetAddrOfLocalVar(const VarDecl *VD) {
15784c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    llvm::Value *Res = LocalDeclMap[VD];
15794c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    assert(Res && "Invalid argument to GetAddrOfLocalVar(), no decl!");
15804c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    return Res;
15814c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  }
15820dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
158356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  /// getOpaqueLValueMapping - Given an opaque value expression (which
158456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  /// must be mapped to an l-value), return its mapping.
158556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  const LValue &getOpaqueLValueMapping(const OpaqueValueExpr *e) {
158656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    assert(OpaqueValueMapping::shouldBindAsLValue(e));
158756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
158856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    llvm::DenseMap<const OpaqueValueExpr*,LValue>::iterator
158956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      it = OpaqueLValues.find(e);
159056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    assert(it != OpaqueLValues.end() && "no mapping for opaque value!");
159156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    return it->second;
159256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  }
159356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
159456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  /// getOpaqueRValueMapping - Given an opaque value expression (which
159556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  /// must be mapped to an r-value), return its mapping.
159656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  const RValue &getOpaqueRValueMapping(const OpaqueValueExpr *e) {
159756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    assert(!OpaqueValueMapping::shouldBindAsLValue(e));
159856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
159956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    llvm::DenseMap<const OpaqueValueExpr*,RValue>::iterator
160056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      it = OpaqueRValues.find(e);
160156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    assert(it != OpaqueRValues.end() && "no mapping for opaque value!");
1602e996ffd240f20a1048179d7727a6ee3227261921John McCall    return it->second;
1603e996ffd240f20a1048179d7727a6ee3227261921John McCall  }
1604e996ffd240f20a1048179d7727a6ee3227261921John McCall
16054f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman  /// getAccessedFieldNo - Given an encoded value and a result number, return
16064f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman  /// the input field number being accessed.
16074f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman  static unsigned getAccessedFieldNo(unsigned Idx, const llvm::Constant *Elts);
16084f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman
1609ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  llvm::BlockAddress *GetAddrOfLabel(const LabelDecl *L);
16103d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner  llvm::BasicBlock *GetIndirectGotoBlock();
16110ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar
16121884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlsson  /// EmitNullInitialization - Generate code to set a value of the given type to
16131884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlsson  /// null, If the type contains data member pointers, they will be initialized
16141884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlsson  /// to -1 in accordance with the Itanium C++ ABI.
16151884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlsson  void EmitNullInitialization(llvm::Value *DestPtr, QualType Ty);
1616ddf7cac45d85b73127adbbd91a2b28fc7291c57eAnders Carlsson
1617ddf7cac45d85b73127adbbd91a2b28fc7291c57eAnders Carlsson  // EmitVAArg - Generate code to get an argument from the passed in pointer
1618ddf7cac45d85b73127adbbd91a2b28fc7291c57eAnders Carlsson  // and update it accordingly. The return value is a pointer to the argument.
1619ddf7cac45d85b73127adbbd91a2b28fc7291c57eAnders Carlsson  // FIXME: We should be able to get rid of this method and use the va_arg
16200dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  // instruction in LLVM instead once it works well enough.
1621ddf7cac45d85b73127adbbd91a2b28fc7291c57eAnders Carlsson  llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty);
1622f666b7780d04186521adcaedb0e15dfa4d5e6933Anders Carlsson
1623bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  /// emitArrayLength - Compute the length of an array, even if it's a
1624bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  /// VLA, and drill down to the base element type.
1625bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  llvm::Value *emitArrayLength(const ArrayType *arrayType,
1626bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall                               QualType &baseType,
1627bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall                               llvm::Value *&addr);
1628bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
1629bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  /// EmitVLASize - Capture all the sizes for the VLA expressions in
1630bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  /// the given variably-modified type and store them in the VLASizeMap.
1631d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar  ///
1632d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar  /// This function can be called with a null (unreachable) insert point.
1633bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  void EmitVariablyModifiedType(QualType Ty);
1634bc8d40d85f3fa1e34569834916f18fecaa635152John McCall
1635bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  /// getVLASize - Returns an LLVM value that corresponds to the size,
1636bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  /// in non-variably-sized elements, of a variable length array type,
1637bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  /// plus that largest non-variably-sized element type.  Assumes that
1638bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  /// the type has already been emitted with EmitVariablyModifiedType.
1639bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  std::pair<llvm::Value*,QualType> getVLASize(const VariableArrayType *vla);
1640bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  std::pair<llvm::Value*,QualType> getVLASize(QualType vla);
1641dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlsson
16425f4307b7ba164b03c853c8d3eb4674d33f8967a6Anders Carlsson  /// LoadCXXThis - Load the value of 'this'. This function is only valid while
16435f4307b7ba164b03c853c8d3eb4674d33f8967a6Anders Carlsson  /// generating code for an C++ member function.
16442504941793b549323f9d29c62507cf21d865fadeJohn McCall  llvm::Value *LoadCXXThis() {
16452504941793b549323f9d29c62507cf21d865fadeJohn McCall    assert(CXXThisValue && "no 'this' value for this function");
16462504941793b549323f9d29c62507cf21d865fadeJohn McCall    return CXXThisValue;
16472504941793b549323f9d29c62507cf21d865fadeJohn McCall  }
16481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1649c997d4278d329e18891aac9698fb991b2d4622ebAnders Carlsson  /// LoadCXXVTT - Load the VTT parameter to base constructors/destructors have
1650c997d4278d329e18891aac9698fb991b2d4622ebAnders Carlsson  /// virtual bases.
165159660c21178b6af518bd4b564e032d5c9cc218cbTimur Iskhodzhanov  // FIXME: Every place that calls LoadCXXVTT is something
165259660c21178b6af518bd4b564e032d5c9cc218cbTimur Iskhodzhanov  // that needs to be abstracted properly.
16532504941793b549323f9d29c62507cf21d865fadeJohn McCall  llvm::Value *LoadCXXVTT() {
165459660c21178b6af518bd4b564e032d5c9cc218cbTimur Iskhodzhanov    assert(CXXStructorImplicitParamValue && "no VTT value for this function");
165559660c21178b6af518bd4b564e032d5c9cc218cbTimur Iskhodzhanov    return CXXStructorImplicitParamValue;
165659660c21178b6af518bd4b564e032d5c9cc218cbTimur Iskhodzhanov  }
165759660c21178b6af518bd4b564e032d5c9cc218cbTimur Iskhodzhanov
165859660c21178b6af518bd4b564e032d5c9cc218cbTimur Iskhodzhanov  /// LoadCXXStructorImplicitParam - Load the implicit parameter
165959660c21178b6af518bd4b564e032d5c9cc218cbTimur Iskhodzhanov  /// for a constructor/destructor.
166059660c21178b6af518bd4b564e032d5c9cc218cbTimur Iskhodzhanov  llvm::Value *LoadCXXStructorImplicitParam() {
166159660c21178b6af518bd4b564e032d5c9cc218cbTimur Iskhodzhanov    assert(CXXStructorImplicitParamValue &&
166259660c21178b6af518bd4b564e032d5c9cc218cbTimur Iskhodzhanov           "no implicit argument value for this function");
166359660c21178b6af518bd4b564e032d5c9cc218cbTimur Iskhodzhanov    return CXXStructorImplicitParamValue;
16642504941793b549323f9d29c62507cf21d865fadeJohn McCall  }
1665bff225ecf77fb891596ecb1b27196310d268365eJohn McCall
1666bff225ecf77fb891596ecb1b27196310d268365eJohn McCall  /// GetAddressOfBaseOfCompleteClass - Convert the given pointer to a
16678561a8666c70f924c8f0209c41b9b77bbbf90607Anders Carlsson  /// complete class to the given direct base.
16688561a8666c70f924c8f0209c41b9b77bbbf90607Anders Carlsson  llvm::Value *
16698561a8666c70f924c8f0209c41b9b77bbbf90607Anders Carlsson  GetAddressOfDirectBaseInCompleteClass(llvm::Value *Value,
16708561a8666c70f924c8f0209c41b9b77bbbf90607Anders Carlsson                                        const CXXRecordDecl *Derived,
16718561a8666c70f924c8f0209c41b9b77bbbf90607Anders Carlsson                                        const CXXRecordDecl *Base,
16728561a8666c70f924c8f0209c41b9b77bbbf90607Anders Carlsson                                        bool BaseIsVirtual);
1673a88ad5618fcb07c5374c79d6781a33234984b039Anders Carlsson
1674f71d232ce5354bd2f0c94ded30fb936c2a3d968bMike Stump  /// GetAddressOfBaseClass - This function will add the necessary delta to the
1675f71d232ce5354bd2f0c94ded30fb936c2a3d968bMike Stump  /// load of 'this' and returns address of the base class.
16769cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer  llvm::Value *GetAddressOfBaseClass(llvm::Value *Value,
16778561a8666c70f924c8f0209c41b9b77bbbf90607Anders Carlsson                                     const CXXRecordDecl *Derived,
1678f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                     CastExpr::path_const_iterator PathBegin,
1679f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                     CastExpr::path_const_iterator PathEnd,
1680176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                                     bool NullCheckValue, SourceLocation Loc);
168134a2d384c745ebc39cae45dc1c0c4a6a7012e09bAnders Carlsson
1682a3697c9c155bda93fd2802f37084b620f4738822Anders Carlsson  llvm::Value *GetAddressOfDerivedClass(llvm::Value *Value,
16838561a8666c70f924c8f0209c41b9b77bbbf90607Anders Carlsson                                        const CXXRecordDecl *Derived,
1684f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                        CastExpr::path_const_iterator PathBegin,
1685f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                        CastExpr::path_const_iterator PathEnd,
16865a0f49ebc83e7fe0da07b9964c44b0a7fae270cbAnders Carlsson                                        bool NullCheckValue);
1687a3697c9c155bda93fd2802f37084b620f4738822Anders Carlsson
16881d4fff5551c2347010b955b4337a2aa7d65a050eTimur Iskhodzhanov  /// GetVTTParameter - Return the VTT parameter that should be passed to a
16891d4fff5551c2347010b955b4337a2aa7d65a050eTimur Iskhodzhanov  /// base constructor/destructor with virtual bases.
16901d4fff5551c2347010b955b4337a2aa7d65a050eTimur Iskhodzhanov  /// FIXME: VTTs are Itanium ABI-specific, so the definition should move
16911d4fff5551c2347010b955b4337a2aa7d65a050eTimur Iskhodzhanov  /// to ItaniumCXXABI.cpp together with all the references to VTT.
16921d4fff5551c2347010b955b4337a2aa7d65a050eTimur Iskhodzhanov  llvm::Value *GetVTTParameter(GlobalDecl GD, bool ForVirtualBase,
16931d4fff5551c2347010b955b4337a2aa7d65a050eTimur Iskhodzhanov                               bool Delegating);
16941d4fff5551c2347010b955b4337a2aa7d65a050eTimur Iskhodzhanov
1695c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall  void EmitDelegateCXXConstructorCall(const CXXConstructorDecl *Ctor,
1696c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall                                      CXXCtorType CtorType,
16974ee7dc2369c1f0257a73b2e83a7d38fdebdd9176Nick Lewycky                                      const FunctionArgList &Args,
16984ee7dc2369c1f0257a73b2e83a7d38fdebdd9176Nick Lewycky                                      SourceLocation Loc);
1699059ce0d92eb5a7da900ae735dc0a2ea3d64f4b0bSean Hunt  // It's important not to confuse this and the previous function. Delegating
1700059ce0d92eb5a7da900ae735dc0a2ea3d64f4b0bSean Hunt  // constructors are the C++0x feature. The constructor delegate optimization
1701059ce0d92eb5a7da900ae735dc0a2ea3d64f4b0bSean Hunt  // is used to reduce duplication in the base and complete consturctors where
1702059ce0d92eb5a7da900ae735dc0a2ea3d64f4b0bSean Hunt  // they are substantially the same.
1703059ce0d92eb5a7da900ae735dc0a2ea3d64f4b0bSean Hunt  void EmitDelegatingCXXConstructorCall(const CXXConstructorDecl *Ctor,
1704059ce0d92eb5a7da900ae735dc0a2ea3d64f4b0bSean Hunt                                        const FunctionArgList &Args);
1705155ed4a23366f4514befb1c9f5f89d16f8b8b6dbAnders Carlsson  void EmitCXXConstructorCall(const CXXConstructorDecl *D, CXXCtorType Type,
1706378e1e739aed97e9b278beeb20e9f5bbe34c0232Douglas Gregor                              bool ForVirtualBase, bool Delegating,
1707176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                              llvm::Value *This, const CXXConstructExpr *E);
1708176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
170934999876e215b22febc240b1a6dc054215d12f9cFariborz Jahanian  void EmitSynthesizedCXXCopyCtorCall(const CXXConstructorDecl *D,
171034999876e215b22febc240b1a6dc054215d12f9cFariborz Jahanian                              llvm::Value *This, llvm::Value *Src,
1711176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                              const CXXConstructExpr *E);
17121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1713288dcaf329c49b01dacd5c1dd9f35609555feecdFariborz Jahanian  void EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
1714569c1f4a6c703eaa8b963bed7d5c2fd5d4569e93Anders Carlsson                                  const ConstantArrayType *ArrayTy,
17155d4d946ec2d88696fd8422aeb64dc29688e6a2c1Anders Carlsson                                  llvm::Value *ArrayPtr,
1716176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                                  const CXXConstructExpr *E,
171759174c0633fb5cde41735cfbff5744bdf837e8d9Douglas Gregor                                  bool ZeroInitialization = false);
17189cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
1719569c1f4a6c703eaa8b963bed7d5c2fd5d4569e93Anders Carlsson  void EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
1720569c1f4a6c703eaa8b963bed7d5c2fd5d4569e93Anders Carlsson                                  llvm::Value *NumElements,
17215d4d946ec2d88696fd8422aeb64dc29688e6a2c1Anders Carlsson                                  llvm::Value *ArrayPtr,
1722176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                                  const CXXConstructExpr *E,
172359174c0633fb5cde41735cfbff5744bdf837e8d9Douglas Gregor                                  bool ZeroInitialization = false);
1724b14095aa98c6fedd3625920c4ce834bcaf24d9f7Anders Carlsson
1725bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  static Destroyer destroyCXXObject;
1726bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
17277267c1693abe7875b0c57268be05005ae013c6c9Anders Carlsson  void EmitCXXDestructorCall(const CXXDestructorDecl *D, CXXDtorType Type,
1728378e1e739aed97e9b278beeb20e9f5bbe34c0232Douglas Gregor                             bool ForVirtualBase, bool Delegating,
1729378e1e739aed97e9b278beeb20e9f5bbe34c0232Douglas Gregor                             llvm::Value *This);
17309cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
1731197056726ed6412501130c2ef2ff69009772aa65John McCall  void EmitNewArrayInitializer(const CXXNewExpr *E, QualType elementType,
1732c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines                               llvm::Value *NewPtr, llvm::Value *NumElements,
1733c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines                               llvm::Value *AllocSizeWithoutCookie);
17341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
173586811609d9353e3aed198045d56e790eb3b6118cPeter Collingbourne  void EmitCXXTemporary(const CXXTemporary *Temporary, QualType TempType,
173686811609d9353e3aed198045d56e790eb3b6118cPeter Collingbourne                        llvm::Value *Ptr);
17371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1738a00703dccbdc6cc4a293db38477dea7db5538c7eAnders Carlsson  llvm::Value *EmitCXXNewExpr(const CXXNewExpr *E);
173960e282cc1e508be327b0481cecedc206873cb86aAnders Carlsson  void EmitCXXDeleteExpr(const CXXDeleteExpr *E);
17401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17414bf8152799ec41796f989ea1b94f6f1bac349117Eli Friedman  void EmitDeleteCall(const FunctionDecl *DeleteFD, llvm::Value *Ptr,
17424bf8152799ec41796f989ea1b94f6f1bac349117Eli Friedman                      QualType DeleteTy);
17434bf8152799ec41796f989ea1b94f6f1bac349117Eli Friedman
1744c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines  RValue EmitBuiltinNewDeleteCall(const FunctionProtoType *Type,
1745c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines                                  const Expr *Arg, bool IsDelete);
1746c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines
1747c2e84ae9a6d25cea3e583c768e576b4c981ec028Mike Stump  llvm::Value* EmitCXXTypeidExpr(const CXXTypeidExpr *E);
1748c849c052d6b4b70f2651c1969531861a5f230053Mike Stump  llvm::Value *EmitDynamicCast(llvm::Value *V, const CXXDynamicCastExpr *DCE);
1749c5f804636e367ef744fd24cf88f7c956a5af0434Nico Weber  llvm::Value* EmitCXXUuidofExpr(const CXXUuidofExpr *E);
1750c2e84ae9a6d25cea3e583c768e576b4c981ec028Mike Stump
17512c9f87ca5cccbfdaad82762368af5b2323320653Richard Smith  /// \brief Situations in which we might emit a check for the suitability of a
17522c9f87ca5cccbfdaad82762368af5b2323320653Richard Smith  ///        pointer or glvalue.
17537ac9ef1c82c779a5348ed11b3d10e01c58803b35Richard Smith  enum TypeCheckKind {
17542c9f87ca5cccbfdaad82762368af5b2323320653Richard Smith    /// Checking the operand of a load. Must be suitably sized and aligned.
17557ac9ef1c82c779a5348ed11b3d10e01c58803b35Richard Smith    TCK_Load,
17562c9f87ca5cccbfdaad82762368af5b2323320653Richard Smith    /// Checking the destination of a store. Must be suitably sized and aligned.
17577ac9ef1c82c779a5348ed11b3d10e01c58803b35Richard Smith    TCK_Store,
17582c9f87ca5cccbfdaad82762368af5b2323320653Richard Smith    /// Checking the bound value in a reference binding. Must be suitably sized
17592c9f87ca5cccbfdaad82762368af5b2323320653Richard Smith    /// and aligned, but is not required to refer to an object (until the
17602c9f87ca5cccbfdaad82762368af5b2323320653Richard Smith    /// reference is used), per core issue 453.
17617ac9ef1c82c779a5348ed11b3d10e01c58803b35Richard Smith    TCK_ReferenceBinding,
17622c9f87ca5cccbfdaad82762368af5b2323320653Richard Smith    /// Checking the object expression in a non-static data member access. Must
17632c9f87ca5cccbfdaad82762368af5b2323320653Richard Smith    /// be an object within its lifetime.
17647ac9ef1c82c779a5348ed11b3d10e01c58803b35Richard Smith    TCK_MemberAccess,
17652c9f87ca5cccbfdaad82762368af5b2323320653Richard Smith    /// Checking the 'this' pointer for a call to a non-static member function.
17662c9f87ca5cccbfdaad82762368af5b2323320653Richard Smith    /// Must be an object within its lifetime.
17678e1cee6f23e2552b96b81e5ef419ab3f69c5b5c2Richard Smith    TCK_MemberCall,
17688e1cee6f23e2552b96b81e5ef419ab3f69c5b5c2Richard Smith    /// Checking the 'this' pointer for a constructor call.
1769c764830bdb6de82baed068889096bd3e52d4cbdaRichard Smith    TCK_ConstructorCall,
1770c764830bdb6de82baed068889096bd3e52d4cbdaRichard Smith    /// Checking the operand of a static_cast to a derived pointer type. Must be
1771c764830bdb6de82baed068889096bd3e52d4cbdaRichard Smith    /// null or an object within its lifetime.
1772c764830bdb6de82baed068889096bd3e52d4cbdaRichard Smith    TCK_DowncastPointer,
1773c764830bdb6de82baed068889096bd3e52d4cbdaRichard Smith    /// Checking the operand of a static_cast to a derived reference type. Must
1774c764830bdb6de82baed068889096bd3e52d4cbdaRichard Smith    /// be an object within its lifetime.
1775176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    TCK_DowncastReference,
1776176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    /// Checking the operand of a cast to a base object. Must be suitably sized
1777176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    /// and aligned.
1778176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    TCK_Upcast,
1779176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    /// Checking the operand of a cast to a virtual base object. Must be an
1780176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    /// object within its lifetime.
1781176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    TCK_UpcastToVirtualBase
17822c9f87ca5cccbfdaad82762368af5b2323320653Richard Smith  };
17832c9f87ca5cccbfdaad82762368af5b2323320653Richard Smith
1784c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines  /// \brief Whether any type-checking sanitizers are enabled. If \c false,
1785c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines  /// calls to EmitTypeCheck can be skipped.
1786c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines  bool sanitizePerformTypeCheck() const;
1787c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines
17887ac9ef1c82c779a5348ed11b3d10e01c58803b35Richard Smith  /// \brief Emit a check that \p V is the address of storage of the
17892c9f87ca5cccbfdaad82762368af5b2323320653Richard Smith  /// appropriate size and alignment for an object of type \p Type.
17904def70d3040e73707c738f7c366737a986135edfRichard Smith  void EmitTypeCheck(TypeCheckKind TCK, SourceLocation Loc, llvm::Value *V,
1791176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                     QualType Type, CharUnits Alignment = CharUnits::Zero(),
1792176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                     bool SkipNullCheck = false);
1793b14e62d5aee7522b98c410ee65bd750c2cfe6f01Mike Stump
1794a0a628f839cf50844cc0f226bd9cb72485f98f91Richard Smith  /// \brief Emit a check that \p Base points into an array object, which
1795a0a628f839cf50844cc0f226bd9cb72485f98f91Richard Smith  /// we can access at index \p Index. \p Accessed should be \c false if we
1796a0a628f839cf50844cc0f226bd9cb72485f98f91Richard Smith  /// this expression is used as an lvalue, for instance in "&Arr[Idx]".
1797a0a628f839cf50844cc0f226bd9cb72485f98f91Richard Smith  void EmitBoundsCheck(const Expr *E, const Expr *Base, llvm::Value *Index,
1798a0a628f839cf50844cc0f226bd9cb72485f98f91Richard Smith                       QualType IndexType, bool Accessed);
1799a0a628f839cf50844cc0f226bd9cb72485f98f91Richard Smith
1800dd36d328730d8c02915d0884541b45ec6ca0049dChris Lattner  llvm::Value *EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV,
1801dd36d328730d8c02915d0884541b45ec6ca0049dChris Lattner                                       bool isInc, bool isPre);
1802dd36d328730d8c02915d0884541b45ec6ca0049dChris Lattner  ComplexPairTy EmitComplexPrePostIncDec(const UnaryOperator *E, LValue LV,
1803dd36d328730d8c02915d0884541b45ec6ca0049dChris Lattner                                         bool isInc, bool isPre);
1804176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
1805176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  void EmitAlignmentAssumption(llvm::Value *PtrValue, unsigned Alignment,
1806176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                               llvm::Value *OffsetValue = nullptr) {
1807176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    Builder.CreateAlignmentAssumption(CGM.getDataLayout(), PtrValue, Alignment,
1808176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                                      OffsetValue);
1809176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  }
1810176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
18115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //===--------------------------------------------------------------------===//
18125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //                            Declaration Emission
18135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //===--------------------------------------------------------------------===//
18140dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
1815d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar  /// EmitDecl - Emit a declaration.
1816d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar  ///
1817d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar  /// This function can be called with a null (unreachable) insert point.
18185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void EmitDecl(const Decl &D);
1819d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar
1820b6bbcc9995186799a60ce17d0c1acff31601653aJohn McCall  /// EmitVarDecl - Emit a local variable declaration.
1821d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar  ///
1822d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar  /// This function can be called with a null (unreachable) insert point.
1823b6bbcc9995186799a60ce17d0c1acff31601653aJohn McCall  void EmitVarDecl(const VarDecl &D);
1824d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar
18250e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  void EmitScalarInit(const Expr *init, const ValueDecl *D, LValue lvalue,
18260e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                      bool capturedByInit);
18277acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall  void EmitScalarInit(llvm::Value *init, LValue lvalue);
1828f85e193739c953358c865005855253af4f68a497John McCall
1829f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  typedef void SpecialInitFn(CodeGenFunction &Init, const VarDecl &D,
1830f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall                             llvm::Value *Address);
1831f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1832176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  /// \brief Determine whether the given initializer is trivial in the sense
1833176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  /// that it requires no code to be generated.
1834176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  bool isTrivialInitializer(const Expr *Init);
1835176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
1836b6bbcc9995186799a60ce17d0c1acff31601653aJohn McCall  /// EmitAutoVarDecl - Emit an auto variable declaration.
1837d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar  ///
1838d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar  /// This function can be called with a null (unreachable) insert point.
183934695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall  void EmitAutoVarDecl(const VarDecl &D);
184034695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall
184134695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall  class AutoVarEmission {
184234695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall    friend class CodeGenFunction;
184334695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall
184457b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall    const VarDecl *Variable;
184534695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall
184634695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall    /// The alignment of the variable.
184734695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall    CharUnits Alignment;
184834695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall
184934695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall    /// The address of the alloca.  Null if the variable was emitted
185034695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall    /// as a global constant.
185134695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall    llvm::Value *Address;
185234695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall
185334695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall    llvm::Value *NRVOFlag;
185434695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall
185534695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall    /// True if the variable is a __block variable.
185634695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall    bool IsByRef;
185734695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall
185834695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall    /// True if the variable is of aggregate type and has a constant
185934695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall    /// initializer.
186034695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall    bool IsConstantAggregate;
186134695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall
1862495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem    /// Non-null if we should use lifetime annotations.
1863495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem    llvm::Value *SizeForLifetimeMarkers;
1864495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem
186557b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall    struct Invalid {};
18666bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    AutoVarEmission(Invalid) : Variable(nullptr) {}
186757b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall
186834695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall    AutoVarEmission(const VarDecl &variable)
18696bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      : Variable(&variable), Address(nullptr), NRVOFlag(nullptr),
1870495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem        IsByRef(false), IsConstantAggregate(false),
18716bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        SizeForLifetimeMarkers(nullptr) {}
187234695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall
18736bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    bool wasEmittedAsGlobal() const { return Address == nullptr; }
187434695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall
187534695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall  public:
187657b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall    static AutoVarEmission invalid() { return AutoVarEmission(Invalid()); }
1877495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem
18786bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    bool useLifetimeMarkers() const {
18796bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      return SizeForLifetimeMarkers != nullptr;
18806bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    }
1881495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem    llvm::Value *getSizeForLifetimeMarkers() const {
1882495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem      assert(useLifetimeMarkers());
1883495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem      return SizeForLifetimeMarkers;
1884495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem    }
1885495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem
1886495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem    /// Returns the raw, allocated address, which is not necessarily
1887495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem    /// the address of the object itself.
1888495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem    llvm::Value *getAllocatedAddress() const {
1889495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem      return Address;
1890495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem    }
189157b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall
189234695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall    /// Returns the address of the object within this declaration.
189334695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall    /// Note that this does not chase the forwarding pointer for
189434695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall    /// __block decls.
189534695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall    llvm::Value *getObjectAddress(CodeGenFunction &CGF) const {
189634695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall      if (!IsByRef) return Address;
189734695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall
189834695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall      return CGF.Builder.CreateStructGEP(Address,
189957b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall                                         CGF.getByRefValueLLVMField(Variable),
190057b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall                                         Variable->getNameAsString());
190134695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall    }
190234695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall  };
190334695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall  AutoVarEmission EmitAutoVarAlloca(const VarDecl &var);
190434695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall  void EmitAutoVarInit(const AutoVarEmission &emission);
190534695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall  void EmitAutoVarCleanups(const AutoVarEmission &emission);
1906bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  void emitAutoVarTypeCleanup(const AutoVarEmission &emission,
1907bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall                              QualType::DestructionKind dtorKind);
1908d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar
1909b6bbcc9995186799a60ce17d0c1acff31601653aJohn McCall  void EmitStaticVarDecl(const VarDecl &D,
1910b6bbcc9995186799a60ce17d0c1acff31601653aJohn McCall                         llvm::GlobalValue::LinkageTypes Linkage);
1911b7ec246872b412f0e7bb9e93eacfd78cfa6adfb3Daniel Dunbar
1912b7ec246872b412f0e7bb9e93eacfd78cfa6adfb3Daniel Dunbar  /// EmitParmDecl - Emit a ParmVarDecl or an ImplicitParamDecl.
1913651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void EmitParmDecl(const VarDecl &D, llvm::Value *Arg, bool ArgIsPointer,
1914651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                    unsigned ArgNo);
19150dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
191656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  /// protectFromPeepholes - Protect a value that we're intending to
191756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  /// store to the side, but which will probably be used later, from
191856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  /// aggressive peepholing optimizations that might delete it.
191956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  ///
192056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  /// Pass the result to unprotectFromPeepholes to declare that
192156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  /// protection is no longer required.
192256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  ///
192356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  /// There's no particular reason why this shouldn't apply to
192456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  /// l-values, it's just that no existing peepholes work on pointers.
192556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  PeepholeProtection protectFromPeepholes(RValue rvalue);
192656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  void unprotectFromPeepholes(PeepholeProtection protection);
192756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
19285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //===--------------------------------------------------------------------===//
19295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //                             Statement Emission
19305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //===--------------------------------------------------------------------===//
19315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
19320dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// EmitStopPoint - Emit a debug stoppoint if we are emitting debug info.
19330912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  void EmitStopPoint(const Stmt *S);
19340912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar
19350dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// EmitStmt - Emit the code for the statement \arg S. It is legal to call
19360dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// this function even if there is no current insertion point.
19370dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  ///
19380dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// This function may clear the current insertion point; callers should use
19390dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// EnsureInsertPoint if they wish to subsequently generate code without first
19400dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// calling EmitBlock, EmitBranch, or EmitStmt.
19415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void EmitStmt(const Stmt *S);
1942a448fb2da03ece39978784793eea68760e8205a1Daniel Dunbar
19430912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  /// EmitSimpleStmt - Try to emit a "simple" statement which does not
19440dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// necessarily require an insertion point or debug information; typically
19450dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// because the statement amounts to a jump or a container of other
19460dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// statements.
19470912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  ///
19480912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  /// \return True if the statement was handled.
19490912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  bool EmitSimpleStmt(const Stmt *S);
19500912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar
19512ac2fa7726eb2f6fe445e098489e92931c31f7caEli Friedman  llvm::Value *EmitCompoundStmt(const CompoundStmt &S, bool GetLast = false,
19522ac2fa7726eb2f6fe445e098489e92931c31f7caEli Friedman                                AggValueSlot AVS = AggValueSlot::ignored());
19532ac2fa7726eb2f6fe445e098489e92931c31f7caEli Friedman  llvm::Value *EmitCompoundStmtWithoutScope(const CompoundStmt &S,
19542ac2fa7726eb2f6fe445e098489e92931c31f7caEli Friedman                                            bool GetLast = false,
19552ac2fa7726eb2f6fe445e098489e92931c31f7caEli Friedman                                            AggValueSlot AVS =
19562ac2fa7726eb2f6fe445e098489e92931c31f7caEli Friedman                                                AggValueSlot::ignored());
1957a448fb2da03ece39978784793eea68760e8205a1Daniel Dunbar
19580dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// EmitLabel - Emit the block for the given label. It is legal to call this
19590dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// function even if there is no current insertion point.
1960ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  void EmitLabel(const LabelDecl *D); // helper for EmitLabelStmt.
1961a448fb2da03ece39978784793eea68760e8205a1Daniel Dunbar
19625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void EmitLabelStmt(const LabelStmt &S);
1963534986f2b21e6050bf00163cd6423fd92155a6edRichard Smith  void EmitAttributedStmt(const AttributedStmt &S);
19645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void EmitGotoStmt(const GotoStmt &S);
19650ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar  void EmitIndirectGotoStmt(const IndirectGotoStmt &S);
19665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void EmitIfStmt(const IfStmt &S);
1967c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines
1968c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines  void EmitCondBrHints(llvm::LLVMContext &Context, llvm::BranchInst *CondBr,
1969176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                       ArrayRef<const Attr *> Attrs);
1970c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines  void EmitWhileStmt(const WhileStmt &S,
1971176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                     ArrayRef<const Attr *> Attrs = None);
1972176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  void EmitDoStmt(const DoStmt &S, ArrayRef<const Attr *> Attrs = None);
1973c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines  void EmitForStmt(const ForStmt &S,
1974176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                   ArrayRef<const Attr *> Attrs = None);
19755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void EmitReturnStmt(const ReturnStmt &S);
19765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void EmitDeclStmt(const DeclStmt &S);
19770912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  void EmitBreakStmt(const BreakStmt &S);
19780912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  void EmitContinueStmt(const ContinueStmt &S);
197951b09f2c528c8460b5465c676173324e44176d62Devang Patel  void EmitSwitchStmt(const SwitchStmt &S);
198051b09f2c528c8460b5465c676173324e44176d62Devang Patel  void EmitDefaultStmt(const DefaultStmt &S);
198151b09f2c528c8460b5465c676173324e44176d62Devang Patel  void EmitCaseStmt(const CaseStmt &S);
1982c049e4f406a7f7179eba98659044a32508e53289Devang Patel  void EmitCaseStmtRange(const CaseStmt &S);
1983a23b91d542e336be5051ac54f394e860fb756911Chad Rosier  void EmitAsmStmt(const AsmStmt &S);
19840dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
19853d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson  void EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S);
198664d5d6c5903157c521af496479d06dc26032d718Anders Carlsson  void EmitObjCAtTryStmt(const ObjCAtTryStmt &S);
198764d5d6c5903157c521af496479d06dc26032d718Anders Carlsson  void EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S);
198810cac6f7115b59a466bb8d2d51cdddeb38aadc37Chris Lattner  void EmitObjCAtSynchronizedStmt(const ObjCAtSynchronizedStmt &S);
1989f85e193739c953358c865005855253af4f68a497John McCall  void EmitObjCAutoreleasePoolStmt(const ObjCAutoreleasePoolStmt &S);
19900dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
199159a7000a79118e4c140885ccbb2ac6a686a73092John McCall  void EnterCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock = false);
199259a7000a79118e4c140885ccbb2ac6a686a73092John McCall  void ExitCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock = false);
19939fc6a7774643a810c8501dae2323e863fefb623eJohn McCall
19946815e941998659a55c20c147861b0f437928c3d8Anders Carlsson  void EmitCXXTryStmt(const CXXTryStmt &S);
199598592d9c4dff79480fdc25b83988de03f912b647Reid Kleckner  void EmitSEHTryStmt(const SEHTryStmt &S);
1996c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines  void EmitSEHLeaveStmt(const SEHLeaveStmt &S);
19970e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  void EnterSEHTryStmt(const SEHTryStmt &S, SEHFinallyInfo &FI);
19980e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  void ExitSEHTryStmt(const SEHTryStmt &S, SEHFinallyInfo &FI);
19990e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
20000e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  llvm::Function *GenerateSEHFilterFunction(CodeGenFunction &ParentCGF,
20010e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                                            const SEHExceptStmt &Except);
20020e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
20030e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  void EmitSEHExceptionCodeSave();
20040e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  llvm::Value *EmitSEHExceptionCode();
20050e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  llvm::Value *EmitSEHExceptionInfo();
20060e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  llvm::Value *EmitSEHAbnormalTermination();
20070e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
2008c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines  void EmitCXXForRangeStmt(const CXXForRangeStmt &S,
2009176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                           ArrayRef<const Attr *> Attrs = None);
20109cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
2011176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  LValue InitCapturedStruct(const CapturedStmt &S);
2012524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir  llvm::Function *EmitCapturedStmt(const CapturedStmt &S, CapturedRegionKind K);
2013176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  void GenerateCapturedStmtFunctionProlog(const CapturedStmt &S);
2014176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  llvm::Function *GenerateCapturedStmtFunctionEpilog(const CapturedStmt &S);
2015c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines  llvm::Function *GenerateCapturedStmtFunction(const CapturedStmt &S);
20166bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  llvm::Value *GenerateCapturedStmtArgument(const CapturedStmt &S);
2017176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  void EmitOMPAggregateAssign(LValue OriginalAddr, llvm::Value *PrivateAddr,
2018176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                              const Expr *AssignExpr, QualType Type,
2019176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                              const VarDecl *VDInit);
2020176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  void EmitOMPFirstprivateClause(const OMPExecutableDirective &D,
2021176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                                 OMPPrivateScope &PrivateScope);
2022176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  void EmitOMPPrivateClause(const OMPExecutableDirective &D,
2023176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                            OMPPrivateScope &PrivateScope);
20246bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
20256bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  void EmitOMPParallelDirective(const OMPParallelDirective &S);
20266bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  void EmitOMPSimdDirective(const OMPSimdDirective &S);
2027c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines  void EmitOMPForDirective(const OMPForDirective &S);
2028176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  void EmitOMPForSimdDirective(const OMPForSimdDirective &S);
2029c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines  void EmitOMPSectionsDirective(const OMPSectionsDirective &S);
2030c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines  void EmitOMPSectionDirective(const OMPSectionDirective &S);
2031c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines  void EmitOMPSingleDirective(const OMPSingleDirective &S);
2032176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  void EmitOMPMasterDirective(const OMPMasterDirective &S);
2033176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  void EmitOMPCriticalDirective(const OMPCriticalDirective &S);
2034c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines  void EmitOMPParallelForDirective(const OMPParallelForDirective &S);
2035176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  void EmitOMPParallelForSimdDirective(const OMPParallelForSimdDirective &S);
2036c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines  void EmitOMPParallelSectionsDirective(const OMPParallelSectionsDirective &S);
2037176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  void EmitOMPTaskDirective(const OMPTaskDirective &S);
2038176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  void EmitOMPTaskyieldDirective(const OMPTaskyieldDirective &S);
2039176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  void EmitOMPBarrierDirective(const OMPBarrierDirective &S);
2040176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  void EmitOMPTaskwaitDirective(const OMPTaskwaitDirective &S);
2041176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  void EmitOMPFlushDirective(const OMPFlushDirective &S);
2042176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  void EmitOMPOrderedDirective(const OMPOrderedDirective &S);
2043176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  void EmitOMPAtomicDirective(const OMPAtomicDirective &S);
2044176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  void EmitOMPTargetDirective(const OMPTargetDirective &S);
2045176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  void EmitOMPTeamsDirective(const OMPTeamsDirective &S);
2046176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
20470e2c34f92f00628d48968dfea096d36381f494cbStephen Hinesprivate:
20480e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
20490e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  /// Helpers for the OpenMP loop directives.
2050176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  void EmitOMPLoopBody(const OMPLoopDirective &Directive,
2051176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                       bool SeparateIter = false);
20523ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar  void EmitOMPInnerLoop(const Stmt &S, bool RequiresCleanup,
20533ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar                        const Expr *LoopCond, const Expr *IncExpr,
20543ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar                        const std::function<void()> &BodyGen);
2055176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  void EmitOMPSimdFinal(const OMPLoopDirective &S);
20560e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  void EmitOMPWorksharingLoop(const OMPLoopDirective &S);
20570e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  void EmitOMPForOuterLoop(OpenMPScheduleClauseKind ScheduleKind,
20580e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                           const OMPLoopDirective &S,
20590e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                           OMPPrivateScope &LoopScope, llvm::Value *LB,
20600e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                           llvm::Value *UB, llvm::Value *ST, llvm::Value *IL,
20610e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                           llvm::Value *Chunk);
20620e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
20630e2c34f92f00628d48968dfea096d36381f494cbStephen Hinespublic:
2064524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir
20655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //===--------------------------------------------------------------------===//
20665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //                         LValue Expression Emission
20675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //===--------------------------------------------------------------------===//
20685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
206913e81737a433b23f8c662d10d1d57356122a8caaDaniel Dunbar  /// GetUndefRValue - Get an appropriate 'undef' rvalue for the given type.
207013e81737a433b23f8c662d10d1d57356122a8caaDaniel Dunbar  RValue GetUndefRValue(QualType Ty);
207113e81737a433b23f8c662d10d1d57356122a8caaDaniel Dunbar
2072ce1d38b8163650e473d7084e0686ed5a7956057bDaniel Dunbar  /// EmitUnsupportedRValue - Emit a dummy r-value using the type of E
2073ce1d38b8163650e473d7084e0686ed5a7956057bDaniel Dunbar  /// and issue an ErrorUnsupported style diagnostic (using the
2074ce1d38b8163650e473d7084e0686ed5a7956057bDaniel Dunbar  /// provided Name).
2075ce1d38b8163650e473d7084e0686ed5a7956057bDaniel Dunbar  RValue EmitUnsupportedRValue(const Expr *E,
2076ce1d38b8163650e473d7084e0686ed5a7956057bDaniel Dunbar                               const char *Name);
2077ce1d38b8163650e473d7084e0686ed5a7956057bDaniel Dunbar
20780dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// EmitUnsupportedLValue - Emit a dummy l-value using the type of E and issue
20790dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// an ErrorUnsupported style diagnostic (using the provided Name).
20806ba82a4a4b295e54cf192c3e98b28f39990c6194Daniel Dunbar  LValue EmitUnsupportedLValue(const Expr *E,
20816ba82a4a4b295e54cf192c3e98b28f39990c6194Daniel Dunbar                               const char *Name);
20826ba82a4a4b295e54cf192c3e98b28f39990c6194Daniel Dunbar
20835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// EmitLValue - Emit code to compute a designator that specifies the location
20845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// of the expression.
20855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ///
20865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// This can return one of two things: a simple address or a bitfield
20875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// reference.  In either case, the LLVM Value* in the LValue structure is
20885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// guaranteed to be an LLVM pointer type.
20895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ///
20905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// If this returns a bitfield reference, nothing about the pointee type of
20915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// the LLVM value is known: For example, it may not be a pointer to an
20925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// integer.
20935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ///
20945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// If this returns a normal address, and if the lvalue's C type is fixed
20955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// size, this method guarantees that the returned pointer type will point to
20965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// an LLVM type of the same size of the lvalue's type.  If the lvalue has a
20975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// variable length type, this is not possible.
20985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ///
20995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  LValue EmitLValue(const Expr *E);
21000dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
21017ac9ef1c82c779a5348ed11b3d10e01c58803b35Richard Smith  /// \brief Same as EmitLValue but additionally we generate checking code to
21027ac9ef1c82c779a5348ed11b3d10e01c58803b35Richard Smith  /// guard against undefined behavior.  This is only suitable when we know
21037ac9ef1c82c779a5348ed11b3d10e01c58803b35Richard Smith  /// that the address will be used to access the object.
21047ac9ef1c82c779a5348ed11b3d10e01c58803b35Richard Smith  LValue EmitCheckedLValue(const Expr *E, TypeCheckKind TCK);
2105b14e62d5aee7522b98c410ee65bd750c2cfe6f01Mike Stump
21064ee7dc2369c1f0257a73b2e83a7d38fdebdd9176Nick Lewycky  RValue convertTempToRValue(llvm::Value *addr, QualType type,
21074ee7dc2369c1f0257a73b2e83a7d38fdebdd9176Nick Lewycky                             SourceLocation Loc);
21089d232c884ea9872d6555df0fd7359699819bc1f1John McCall
21099eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall  void EmitAtomicInit(Expr *E, LValue lvalue);
21109eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall
21110e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  bool LValueIsSuitableForInlineAtomic(LValue Src);
21120e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  bool typeIsSuitableForInlineAtomic(QualType Ty, bool IsVolatile) const;
21130e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
21140e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  RValue EmitAtomicLoad(LValue LV, SourceLocation SL,
21150e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                        AggValueSlot Slot = AggValueSlot::ignored());
21160e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
21174ee7dc2369c1f0257a73b2e83a7d38fdebdd9176Nick Lewycky  RValue EmitAtomicLoad(LValue lvalue, SourceLocation loc,
21180e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                        llvm::AtomicOrdering AO, bool IsVolatile = false,
21199eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall                        AggValueSlot slot = AggValueSlot::ignored());
21209eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall
21219eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall  void EmitAtomicStore(RValue rvalue, LValue lvalue, bool isInit);
21229eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall
21230e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  void EmitAtomicStore(RValue rvalue, LValue lvalue, llvm::AtomicOrdering AO,
21240e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                       bool IsVolatile, bool isInit);
21250e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
21260e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  std::pair<RValue, RValue> EmitAtomicCompareExchange(
21270e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      LValue Obj, RValue Expected, RValue Desired, SourceLocation Loc,
21280e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      llvm::AtomicOrdering Success = llvm::SequentiallyConsistent,
21290e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      llvm::AtomicOrdering Failure = llvm::SequentiallyConsistent,
21300e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      bool IsWeak = false, AggValueSlot Slot = AggValueSlot::ignored());
21310e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
213226815d97c5743481e317f17a8d53a6819d061862John McCall  /// EmitToMemory - Change a scalar value from its value
213326815d97c5743481e317f17a8d53a6819d061862John McCall  /// representation to its in-memory representation.
213426815d97c5743481e317f17a8d53a6819d061862John McCall  llvm::Value *EmitToMemory(llvm::Value *Value, QualType Ty);
213526815d97c5743481e317f17a8d53a6819d061862John McCall
213626815d97c5743481e317f17a8d53a6819d061862John McCall  /// EmitFromMemory - Change a scalar value from its memory
213726815d97c5743481e317f17a8d53a6819d061862John McCall  /// representation to its value representation.
213826815d97c5743481e317f17a8d53a6819d061862John McCall  llvm::Value *EmitFromMemory(llvm::Value *Value, QualType Ty);
213926815d97c5743481e317f17a8d53a6819d061862John McCall
21409d9cc874bcc168474757a8f90476684b4a93ed76Daniel Dunbar  /// EmitLoadOfScalar - Load a scalar value from an address, taking
21419d9cc874bcc168474757a8f90476684b4a93ed76Daniel Dunbar  /// care to appropriately convert from the memory representation to
21429d9cc874bcc168474757a8f90476684b4a93ed76Daniel Dunbar  /// the LLVM value representation.
214309429b962811f2c0a7a81a1b0a89f96f5878b20cMike Stump  llvm::Value *EmitLoadOfScalar(llvm::Value *Addr, bool Volatile,
21443d5aff5d3036b0ff09d114857cd2276134b3d8c9Dan Gohman                                unsigned Alignment, QualType Ty,
21454ee7dc2369c1f0257a73b2e83a7d38fdebdd9176Nick Lewycky                                SourceLocation Loc,
21466bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                llvm::MDNode *TBAAInfo = nullptr,
2147b37a73d5c6a0c8bb1f6e363d3b53980e4fa0ceadManman Ren                                QualType TBAABaseTy = QualType(),
2148b37a73d5c6a0c8bb1f6e363d3b53980e4fa0ceadManman Ren                                uint64_t TBAAOffset = 0);
2149545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall
2150545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall  /// EmitLoadOfScalar - Load a scalar value from an address, taking
2151545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall  /// care to appropriately convert from the memory representation to
2152545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall  /// the LLVM value representation.  The l-value must be a simple
2153545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall  /// l-value.
21544ee7dc2369c1f0257a73b2e83a7d38fdebdd9176Nick Lewycky  llvm::Value *EmitLoadOfScalar(LValue lvalue, SourceLocation Loc);
21559d9cc874bcc168474757a8f90476684b4a93ed76Daniel Dunbar
21569d9cc874bcc168474757a8f90476684b4a93ed76Daniel Dunbar  /// EmitStoreOfScalar - Store a scalar value to an address, taking
21579d9cc874bcc168474757a8f90476684b4a93ed76Daniel Dunbar  /// care to appropriately convert from the memory representation to
21589d9cc874bcc168474757a8f90476684b4a93ed76Daniel Dunbar  /// the LLVM value representation.
215909429b962811f2c0a7a81a1b0a89f96f5878b20cMike Stump  void EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr,
21603d5aff5d3036b0ff09d114857cd2276134b3d8c9Dan Gohman                         bool Volatile, unsigned Alignment, QualType Ty,
21616bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                         llvm::MDNode *TBAAInfo = nullptr, bool isInit = false,
2162b37a73d5c6a0c8bb1f6e363d3b53980e4fa0ceadManman Ren                         QualType TBAABaseTy = QualType(),
2163b37a73d5c6a0c8bb1f6e363d3b53980e4fa0ceadManman Ren                         uint64_t TBAAOffset = 0);
2164545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall
2165545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall  /// EmitStoreOfScalar - Store a scalar value to an address, taking
2166545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall  /// care to appropriately convert from the memory representation to
2167545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall  /// the LLVM value representation.  The l-value must be a simple
21687a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall  /// l-value.  The isInit flag indicates whether this is an initialization.
21697a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall  /// If so, atomic qualifiers are ignored and the store is always non-atomic.
21707a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall  void EmitStoreOfScalar(llvm::Value *value, LValue lvalue, bool isInit=false);
21719d9cc874bcc168474757a8f90476684b4a93ed76Daniel Dunbar
21725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// EmitLoadOfLValue - Given an expression that represents a value lvalue,
21735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// this method emits the address of the lvalue, then loads the result as an
21745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// rvalue, returning the rvalue.
21754ee7dc2369c1f0257a73b2e83a7d38fdebdd9176Nick Lewycky  RValue EmitLoadOfLValue(LValue V, SourceLocation Loc);
2176545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall  RValue EmitLoadOfExtVectorElementLValue(LValue V);
2177545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall  RValue EmitLoadOfBitfieldLValue(LValue LV);
21786bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  RValue EmitLoadOfGlobalRegLValue(LValue LV);
21790dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
21805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// EmitStoreThroughLValue - Store the specified rvalue into the specified
21815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// lvalue, where both are guaranteed to the have the same type, and that type
21825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// is 'Ty'.
21830e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  void EmitStoreThroughLValue(RValue Src, LValue Dst, bool isInit = false);
2184545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall  void EmitStoreThroughExtVectorComponentLValue(RValue Src, LValue Dst);
21856bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  void EmitStoreThroughGlobalRegLValue(RValue Src, LValue Dst);
2186ed3849b456d64d124bddc7ac044f3ce590bd9d69Daniel Dunbar
21874ee7dc2369c1f0257a73b2e83a7d38fdebdd9176Nick Lewycky  /// EmitStoreThroughBitfieldLValue - Store Src into Dst with same constraints
21884ee7dc2369c1f0257a73b2e83a7d38fdebdd9176Nick Lewycky  /// as EmitStoreThroughLValue.
2189ed3849b456d64d124bddc7ac044f3ce590bd9d69Daniel Dunbar  ///
21900dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// \param Result [out] - If non-null, this will be set to a Value* for the
21910dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// bit-field contents after the store, appropriate for use as the result of
21920dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// an assignment to the bit-field.
2193545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall  void EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst,
21946bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                      llvm::Value **Result=nullptr);
21950dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
219683ce9d4a552987d34cbd500e983db8d770232379John McCall  /// Emit an l-value for an assignment (simple or compound) of complex type.
219783ce9d4a552987d34cbd500e983db8d770232379John McCall  LValue EmitComplexAssignmentLValue(const BinaryOperator *E);
21982a41637a995affa1563f4d82a8b026e326a2faa0John McCall  LValue EmitComplexCompoundAssignmentLValue(const CompoundAssignOperator *E);
21990e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  LValue EmitScalarCompoundAssignWithComplex(const CompoundAssignOperator *E,
22000e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                                             llvm::Value *&Result);
220183ce9d4a552987d34cbd500e983db8d770232379John McCall
2202fc8f0e14ad142ed811e90fbd9a30e419e301c717Chris Lattner  // Note: only available for agg return types
220380e62c29fe21cd06c93eba6e72b7e32dca06fcf9Daniel Dunbar  LValue EmitBinaryOperatorLValue(const BinaryOperator *E);
22042a41637a995affa1563f4d82a8b026e326a2faa0John McCall  LValue EmitCompoundAssignmentLValue(const CompoundAssignOperator *E);
22055b5c9ef865607e179413462dcd71bcebb5b7daaeDaniel Dunbar  // Note: only available for agg return types
220622c940ee588a420cd2e6c8f68be2ac3cc80061ffChristopher Lamb  LValue EmitCallExprLValue(const CallExpr *E);
22075b5c9ef865607e179413462dcd71bcebb5b7daaeDaniel Dunbar  // Note: only available for agg return types
22085b5c9ef865607e179413462dcd71bcebb5b7daaeDaniel Dunbar  LValue EmitVAArgExprLValue(const VAArgExpr *E);
22095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  LValue EmitDeclRefLValue(const DeclRefExpr *E);
22106bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  LValue EmitReadRegister(const VarDecl *VD);
22115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  LValue EmitStringLiteralLValue(const StringLiteral *E);
2212eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner  LValue EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E);
2213d9f6910f4ef37c0e8eeee2a01287d9572c3176efChris Lattner  LValue EmitPredefinedLValue(const PredefinedExpr *E);
22145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  LValue EmitUnaryOpLValue(const UnaryOperator *E);
2215a0a628f839cf50844cc0f226bd9cb72485f98f91Richard Smith  LValue EmitArraySubscriptExpr(const ArraySubscriptExpr *E,
2216a0a628f839cf50844cc0f226bd9cb72485f98f91Richard Smith                                bool Accessed = false);
2217213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  LValue EmitExtVectorElementExpr(const ExtVectorElementExpr *E);
2218b84a06e68ffd71da22e3c75b6e4bbdba37816413Devang Patel  LValue EmitMemberExpr(const MemberExpr *E);
2219820bca41c3899374775d2a1dfc2ef2e22aaf1c7bFariborz Jahanian  LValue EmitObjCIsaExpr(const ObjCIsaExpr *E);
222006e863f2902b8ba55b056308875c19f7ba3dab25Eli Friedman  LValue EmitCompoundLiteralLValue(const CompoundLiteralExpr *E);
222113ec9100ca6bc03b5ce8832e4a0fcb724d47bcb1Richard Smith  LValue EmitInitListLValue(const InitListExpr *E);
222256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  LValue EmitConditionalOperatorLValue(const AbstractConditionalOperator *E);
222375dfedaf702822c9e51a4f3c5d3ecf2d2ad99272Chris Lattner  LValue EmitCastLValue(const CastExpr *E);
222403e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor  LValue EmitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E);
2225e996ffd240f20a1048179d7727a6ee3227261921John McCall  LValue EmitOpaqueValueLValue(const OpaqueValueExpr *e);
2226176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
2227176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  llvm::Value *EmitExtVectorElementLValue(LValue V);
22289cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
22294ee7dc2369c1f0257a73b2e83a7d38fdebdd9176Nick Lewycky  RValue EmitRValueForField(LValue LV, const FieldDecl *FD, SourceLocation Loc);
2230eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov
2231dd2ecee313b558a9b621ec003b45e0fbc83508d7John McCall  class ConstantEmission {
2232dd2ecee313b558a9b621ec003b45e0fbc83508d7John McCall    llvm::PointerIntPair<llvm::Constant*, 1, bool> ValueAndIsReference;
2233dd2ecee313b558a9b621ec003b45e0fbc83508d7John McCall    ConstantEmission(llvm::Constant *C, bool isReference)
2234dd2ecee313b558a9b621ec003b45e0fbc83508d7John McCall      : ValueAndIsReference(C, isReference) {}
2235dd2ecee313b558a9b621ec003b45e0fbc83508d7John McCall  public:
2236dd2ecee313b558a9b621ec003b45e0fbc83508d7John McCall    ConstantEmission() {}
2237dd2ecee313b558a9b621ec003b45e0fbc83508d7John McCall    static ConstantEmission forReference(llvm::Constant *C) {
2238dd2ecee313b558a9b621ec003b45e0fbc83508d7John McCall      return ConstantEmission(C, true);
2239dd2ecee313b558a9b621ec003b45e0fbc83508d7John McCall    }
2240dd2ecee313b558a9b621ec003b45e0fbc83508d7John McCall    static ConstantEmission forValue(llvm::Constant *C) {
2241dd2ecee313b558a9b621ec003b45e0fbc83508d7John McCall      return ConstantEmission(C, false);
2242dd2ecee313b558a9b621ec003b45e0fbc83508d7John McCall    }
2243dd2ecee313b558a9b621ec003b45e0fbc83508d7John McCall
22440e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    explicit operator bool() const {
22456bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      return ValueAndIsReference.getOpaqueValue() != nullptr;
22466bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    }
2247dd2ecee313b558a9b621ec003b45e0fbc83508d7John McCall
2248dd2ecee313b558a9b621ec003b45e0fbc83508d7John McCall    bool isReference() const { return ValueAndIsReference.getInt(); }
2249dd2ecee313b558a9b621ec003b45e0fbc83508d7John McCall    LValue getReferenceLValue(CodeGenFunction &CGF, Expr *refExpr) const {
2250dd2ecee313b558a9b621ec003b45e0fbc83508d7John McCall      assert(isReference());
2251dd2ecee313b558a9b621ec003b45e0fbc83508d7John McCall      return CGF.MakeNaturalAlignAddrLValue(ValueAndIsReference.getPointer(),
2252dd2ecee313b558a9b621ec003b45e0fbc83508d7John McCall                                            refExpr->getType());
2253dd2ecee313b558a9b621ec003b45e0fbc83508d7John McCall    }
2254dd2ecee313b558a9b621ec003b45e0fbc83508d7John McCall
2255dd2ecee313b558a9b621ec003b45e0fbc83508d7John McCall    llvm::Constant *getValue() const {
2256dd2ecee313b558a9b621ec003b45e0fbc83508d7John McCall      assert(!isReference());
2257dd2ecee313b558a9b621ec003b45e0fbc83508d7John McCall      return ValueAndIsReference.getPointer();
2258dd2ecee313b558a9b621ec003b45e0fbc83508d7John McCall    }
2259dd2ecee313b558a9b621ec003b45e0fbc83508d7John McCall  };
2260dd2ecee313b558a9b621ec003b45e0fbc83508d7John McCall
2261f4b88a45902af1802a1cb42ba48b1c474474f228John McCall  ConstantEmission tryEmitAsConstant(DeclRefExpr *refExpr);
2262dd2ecee313b558a9b621ec003b45e0fbc83508d7John McCall
22634b9c2d235fb9449e249d74f48ecfec601650de93John McCall  RValue EmitPseudoObjectRValue(const PseudoObjectExpr *e,
22644b9c2d235fb9449e249d74f48ecfec601650de93John McCall                                AggValueSlot slot = AggValueSlot::ignored());
22654b9c2d235fb9449e249d74f48ecfec601650de93John McCall  LValue EmitPseudoObjectLValue(const PseudoObjectExpr *e);
22664b9c2d235fb9449e249d74f48ecfec601650de93John McCall
22672a03192a02dbf4fdff438d1e658356bde871aba4Daniel Dunbar  llvm::Value *EmitIvarOffset(const ObjCInterfaceDecl *Interface,
226829e0bccf2bcce22b877f8b2ed173f564c116b97eDaniel Dunbar                              const ObjCIvarDecl *Ivar);
2269377ecc7996dce6803f7b7b6208cab5e197c9c5b8Eli Friedman  LValue EmitLValueForField(LValue Base, const FieldDecl* Field);
2270f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall  LValue EmitLValueForLambdaField(const FieldDecl *Field);
22719cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
227206a2970e9480c6d02b367b2f970baff29b9f9721Anders Carlsson  /// EmitLValueForFieldInitialization - Like EmitLValueForField, except that
227306a2970e9480c6d02b367b2f970baff29b9f9721Anders Carlsson  /// if the Field is a reference, this will return the address of the reference
227406a2970e9480c6d02b367b2f970baff29b9f9721Anders Carlsson  /// and not the address of the value stored in the reference.
2275377ecc7996dce6803f7b7b6208cab5e197c9c5b8Eli Friedman  LValue EmitLValueForFieldInitialization(LValue Base,
2276377ecc7996dce6803f7b7b6208cab5e197c9c5b8Eli Friedman                                          const FieldDecl* Field);
22779cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
227845012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz Jahanian  LValue EmitLValueForIvar(QualType ObjectTy,
227945012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz Jahanian                           llvm::Value* Base, const ObjCIvarDecl *Ivar,
228029e0bccf2bcce22b877f8b2ed173f564c116b97eDaniel Dunbar                           unsigned CVRQualifiers);
2281fd64bb635dc221baa19f81d5d2a084f7eb269f7fFariborz Jahanian
2282b58d017f2b9eeed33f2ab3ede968b89cf5296bf2Anders Carlsson  LValue EmitCXXConstructLValue(const CXXConstructExpr *E);
2283e61c9e80a108b5db7e5f0e702f0e455d737c6390Anders Carlsson  LValue EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E);
228431a3702618ab49578542cffee20ba0c8859b9d1eEli Friedman  LValue EmitLambdaLValue(const LambdaExpr *E);
2285c2e84ae9a6d25cea3e583c768e576b4c981ec028Mike Stump  LValue EmitCXXTypeidLValue(const CXXTypeidExpr *E);
2286c5f804636e367ef744fd24cf88f7c956a5af0434Nico Weber  LValue EmitCXXUuidofLValue(const CXXUuidofExpr *E);
22879cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
22880a04d77bde7e3a661c2b41b60630d125d09ed6efDaniel Dunbar  LValue EmitObjCMessageExprLValue(const ObjCMessageExpr *E);
2289391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  LValue EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E);
2290654599454c8e6cc83b1b9b3af43c49c2f66a26cbChris Lattner  LValue EmitStmtExprLValue(const StmtExpr *E);
22918bfd31f9dad09cd52225d868bbd92a9bebe87775Fariborz Jahanian  LValue EmitPointerToDataMemberBinaryExpr(const BinaryOperator *E);
229203b2960c14aede6ac82bdef32247094ebb72fa69Fariborz Jahanian  LValue EmitObjCSelectorLValue(const ObjCSelectorExpr *E);
22933b8e0b7f96164e8c49d2a833da26edd960f5da1fYunzhong Gao  void   EmitDeclRefExprDbgValue(const DeclRefExpr *E, llvm::Constant *Init);
229456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
22955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //===--------------------------------------------------------------------===//
2296883f6a7cc7dccb1d675e27121a82614d63492a8dChris Lattner  //                         Scalar Expression Emission
22975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //===--------------------------------------------------------------------===//
22985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
22990dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// EmitCall - Generate a call of the given function, expecting the given
23000dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// result type, and using the given argument list which specifies both the
23010dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// LLVM arguments and the types they were derived from.
2302c0ef9f59937c3971c48b6fed37cf5bd8985c024bDaniel Dunbar  ///
2303f71d232ce5354bd2f0c94ded30fb936c2a3d968bMike Stump  /// \param TargetDecl - If given, the decl of the function in a direct call;
2304f71d232ce5354bd2f0c94ded30fb936c2a3d968bMike Stump  /// used to set attributes on the call (noreturn, etc.).
230588b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar  RValue EmitCall(const CGFunctionInfo &FnInfo,
230688b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar                  llvm::Value *Callee,
2307f3c47c9525153aea2de0ec4bd615b9cf2d81c103Anders Carlsson                  ReturnValueSlot ReturnValue,
2308c0ef9f59937c3971c48b6fed37cf5bd8985c024bDaniel Dunbar                  const CallArgList &Args,
23096bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                  const Decl *TargetDecl = nullptr,
23106bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                  llvm::Instruction **callOrInvoke = nullptr);
23111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2312176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  RValue EmitCall(QualType FnType, llvm::Value *Callee, const CallExpr *E,
2313d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson                  ReturnValueSlot ReturnValue,
23140e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                  const Decl *TargetDecl = nullptr,
23150e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                  llvm::Value *Chain = nullptr);
23169cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer  RValue EmitCallExpr(const CallExpr *E,
2317d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson                      ReturnValueSlot ReturnValue = ReturnValueSlot());
23181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2319bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  llvm::CallInst *EmitRuntimeCall(llvm::Value *callee,
2320bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall                                  const Twine &name = "");
2321bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  llvm::CallInst *EmitRuntimeCall(llvm::Value *callee,
2322bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall                                  ArrayRef<llvm::Value*> args,
2323bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall                                  const Twine &name = "");
2324bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  llvm::CallInst *EmitNounwindRuntimeCall(llvm::Value *callee,
2325bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall                                          const Twine &name = "");
2326bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  llvm::CallInst *EmitNounwindRuntimeCall(llvm::Value *callee,
2327bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall                                          ArrayRef<llvm::Value*> args,
2328bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall                                          const Twine &name = "");
2329bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall
2330f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  llvm::CallSite EmitCallOrInvoke(llvm::Value *Callee,
23312d3ba4f5a923a90c3fc290ddfba5e36c2d0a9b46Chris Lattner                                  ArrayRef<llvm::Value *> Args,
23328cc488fefb2fb04bc8d5398da29f0182f97934cfChris Lattner                                  const Twine &Name = "");
23334c7d9f1507d0f102bd4133bba63348636facd469Jay Foad  llvm::CallSite EmitCallOrInvoke(llvm::Value *Callee,
23348cc488fefb2fb04bc8d5398da29f0182f97934cfChris Lattner                                  const Twine &Name = "");
2335bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  llvm::CallSite EmitRuntimeCallOrInvoke(llvm::Value *callee,
2336bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall                                         ArrayRef<llvm::Value*> args,
2337bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall                                         const Twine &name = "");
2338bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  llvm::CallSite EmitRuntimeCallOrInvoke(llvm::Value *callee,
2339bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall                                         const Twine &name = "");
2340bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  void EmitNoreturnRuntimeCallOrInvoke(llvm::Value *callee,
2341bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall                                       ArrayRef<llvm::Value*> args);
2342f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
23432726267f094a0c1f5ac5b501ec5a9898c58876bfFariborz Jahanian  llvm::Value *BuildAppleKextVirtualCall(const CXXMethodDecl *MD,
23442726267f094a0c1f5ac5b501ec5a9898c58876bfFariborz Jahanian                                         NestedNameSpecifier *Qual,
23452acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner                                         llvm::Type *Ty);
2346ccd5259d33cbbdd6f5fbf7ccab4cb4a2702309eaFariborz Jahanian
2347ccd5259d33cbbdd6f5fbf7ccab4cb4a2702309eaFariborz Jahanian  llvm::Value *BuildAppleKextVirtualDestructorCall(const CXXDestructorDecl *DD,
2348ccd5259d33cbbdd6f5fbf7ccab4cb4a2702309eaFariborz Jahanian                                                   CXXDtorType Type,
2349771c678c04f5f685b4f188ec6c2fd88ad0f7457fFariborz Jahanian                                                   const CXXRecordDecl *RD);
2350566abee1e9828a7700b51e4d17ea08234fde3bb4Anders Carlsson
2351176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  RValue
2352176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  EmitCXXMemberOrOperatorCall(const CXXMethodDecl *MD, llvm::Value *Callee,
2353176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                              ReturnValueSlot ReturnValue, llvm::Value *This,
2354176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                              llvm::Value *ImplicitParam,
2355176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                              QualType ImplicitParamTy, const CallExpr *E);
2356176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  RValue EmitCXXStructorCall(const CXXMethodDecl *MD, llvm::Value *Callee,
2357176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                             ReturnValueSlot ReturnValue, llvm::Value *This,
2358176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                             llvm::Value *ImplicitParam,
2359176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                             QualType ImplicitParamTy, const CallExpr *E,
2360176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                             StructorType Type);
2361a1736c0c750d4514a5d8fda36670addf1e4de54aAnders Carlsson  RValue EmitCXXMemberCallExpr(const CXXMemberCallExpr *E,
2362a1736c0c750d4514a5d8fda36670addf1e4de54aAnders Carlsson                               ReturnValueSlot ReturnValue);
23630e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  RValue EmitCXXMemberOrOperatorMemberCallExpr(const CallExpr *CE,
23640e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                                               const CXXMethodDecl *MD,
23650e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                                               ReturnValueSlot ReturnValue,
23660e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                                               bool HasQualifier,
23670e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                                               NestedNameSpecifier *Qualifier,
23680e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                                               bool IsArrow, const Expr *Base);
23690e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  // Compute the object pointer.
2370a1736c0c750d4514a5d8fda36670addf1e4de54aAnders Carlsson  RValue EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E,
2371a1736c0c750d4514a5d8fda36670addf1e4de54aAnders Carlsson                                      ReturnValueSlot ReturnValue);
23725549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek
23730f294632f36459174199b77699e339715244b5abAnders Carlsson  RValue EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E,
2374a1736c0c750d4514a5d8fda36670addf1e4de54aAnders Carlsson                                       const CXXMethodDecl *MD,
2375a1736c0c750d4514a5d8fda36670addf1e4de54aAnders Carlsson                                       ReturnValueSlot ReturnValue);
23761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23776c0aa5ff6e6253db0f993053599e2a52b5b93b2dPeter Collingbourne  RValue EmitCUDAKernelCallExpr(const CUDAKernelCallExpr *E,
23786c0aa5ff6e6253db0f993053599e2a52b5b93b2dPeter Collingbourne                                ReturnValueSlot ReturnValue);
23796c0aa5ff6e6253db0f993053599e2a52b5b93b2dPeter Collingbourne
23809cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
23811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  RValue EmitBuiltinExpr(const FunctionDecl *FD,
23820e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                         unsigned BuiltinID, const CallExpr *E,
23830e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                         ReturnValueSlot ReturnValue);
23845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2385a1736c0c750d4514a5d8fda36670addf1e4de54aAnders Carlsson  RValue EmitBlockCallExpr(const CallExpr *E, ReturnValueSlot ReturnValue);
238609429b962811f2c0a7a81a1b0a89f96f5878b20cMike Stump
23870dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// EmitTargetBuiltinExpr - Emit the given builtin call. Returns 0 if the call
23880dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// is unhandled by the current target.
2389f02e9ddf5efc75917af712b3c7f909581205f0a5Daniel Dunbar  llvm::Value *EmitTargetBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
2390f02e9ddf5efc75917af712b3c7f909581205f0a5Daniel Dunbar
23918137a607eebe799d95fd05226fb91119a5b054b0Kevin Qin  llvm::Value *EmitAArch64CompareBuiltinExpr(llvm::Value *Op, llvm::Type *Ty,
23928137a607eebe799d95fd05226fb91119a5b054b0Kevin Qin                                             const llvm::CmpInst::Predicate Fp,
23938137a607eebe799d95fd05226fb91119a5b054b0Kevin Qin                                             const llvm::CmpInst::Predicate Ip,
23948137a607eebe799d95fd05226fb91119a5b054b0Kevin Qin                                             const llvm::Twine &Name = "");
23952752c0137d95aa2f4ee1cdff4b564bac842e041bChris Lattner  llvm::Value *EmitARMBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
2396651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
2397651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  llvm::Value *EmitCommonNeonBuiltinExpr(unsigned BuiltinID,
2398651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                         unsigned LLVMIntrinsic,
2399651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                         unsigned AltLLVMIntrinsic,
2400651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                         const char *NameHint,
2401651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                         unsigned Modifier,
2402651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                         const CallExpr *E,
2403651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                         SmallVectorImpl<llvm::Value *> &Ops,
24046bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                         llvm::Value *Align = nullptr);
2405651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  llvm::Function *LookupNeonLLVMIntrinsic(unsigned IntrinsicID,
2406651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                          unsigned Modifier, llvm::Type *ArgTy,
2407651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                          const CallExpr *E);
24089cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer  llvm::Value *EmitNeonCall(llvm::Function *F,
2409686775deca8b8685eb90801495880e3abdd844c2Chris Lattner                            SmallVectorImpl<llvm::Value*> &O,
2410db3d4d036037f379f12643e067b229862d61e932Bob Wilson                            const char *name,
241161eecf5aea018ea65c9ab0bccacd2996b15c632dNate Begeman                            unsigned shift = 0, bool rightshift = false);
2412cf55652cf668c1402eee0b12edd2e5a1bc34d7a1Bob Wilson  llvm::Value *EmitNeonSplat(llvm::Value *V, llvm::Constant *Idx);
24132acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Value *EmitNeonShiftVector(llvm::Value *V, llvm::Type *Ty,
241461eecf5aea018ea65c9ab0bccacd2996b15c632dNate Begeman                                   bool negateForRightShift);
24157f0ff70c391afeb7811b7ddcb06bfbbd5c6cf8f9Amaury de la Vieuville  llvm::Value *EmitNeonRShiftImm(llvm::Value *Vec, llvm::Value *Amt,
24167f0ff70c391afeb7811b7ddcb06bfbbd5c6cf8f9Amaury de la Vieuville                                 llvm::Type *Ty, bool usgn, const char *name);
24176bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  // Helper functions for EmitAArch64BuiltinExpr.
2418651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  llvm::Value *vectorWrapScalar8(llvm::Value *Op);
2419651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  llvm::Value *vectorWrapScalar16(llvm::Value *Op);
2420651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  llvm::Value *emitVectorWrappedScalar8Intrinsic(
2421651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      unsigned Int, SmallVectorImpl<llvm::Value *> &Ops, const char *Name);
2422651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  llvm::Value *emitVectorWrappedScalar16Intrinsic(
2423651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      unsigned Int, SmallVectorImpl<llvm::Value *> &Ops, const char *Name);
24246bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  llvm::Value *EmitAArch64BuiltinExpr(unsigned BuiltinID, const CallExpr *E);
2425651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  llvm::Value *EmitNeon64Call(llvm::Function *F,
2426651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                              llvm::SmallVectorImpl<llvm::Value *> &O,
2427651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                              const char *name);
24289cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
2429795b10062c2eaffae9e04241fb1a73cdbcb24a37Bill Wendling  llvm::Value *BuildVector(ArrayRef<llvm::Value*> Ops);
2430564f1de67d7ba43646b8740db86d6269e3dfbe0bAnders Carlsson  llvm::Value *EmitX86BuiltinExpr(unsigned BuiltinID, const CallExpr *E);
2431564f1de67d7ba43646b8740db86d6269e3dfbe0bAnders Carlsson  llvm::Value *EmitPPCBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
2432c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines  llvm::Value *EmitR600BuiltinExpr(unsigned BuiltinID, const CallExpr *E);
24330dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
2434ed7c618f849e2541b1d0288c43154937652c5b15Daniel Dunbar  llvm::Value *EmitObjCProtocolExpr(const ObjCProtocolExpr *E);
24357f02f721d48772e4eee4fccd8af4f800be1bbc1eChris Lattner  llvm::Value *EmitObjCStringLiteral(const ObjCStringLiteral *E);
2436eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard  llvm::Value *EmitObjCBoxedExpr(const ObjCBoxedExpr *E);
2437ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  llvm::Value *EmitObjCArrayLiteral(const ObjCArrayLiteral *E);
2438ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  llvm::Value *EmitObjCDictionaryLiteral(const ObjCDictionaryLiteral *E);
2439ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  llvm::Value *EmitObjCCollectionLiteral(const Expr *E,
2440ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek                                const ObjCMethodDecl *MethodWithObjects);
24418fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner  llvm::Value *EmitObjCSelectorExpr(const ObjCSelectorExpr *E);
2442ef072fd2f3347cfd857d6eb787b245b950771430John McCall  RValue EmitObjCMessageExpr(const ObjCMessageExpr *E,
2443ef072fd2f3347cfd857d6eb787b245b950771430John McCall                             ReturnValueSlot Return = ReturnValueSlot());
24448fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner
2445f85e193739c953358c865005855253af4f68a497John McCall  /// Retrieves the default cleanup kind for an ARC cleanup.
2446f85e193739c953358c865005855253af4f68a497John McCall  /// Except under -fobjc-arc-eh, ARC cleanups are normal-only.
2447f85e193739c953358c865005855253af4f68a497John McCall  CleanupKind getARCCleanupKind() {
2448f85e193739c953358c865005855253af4f68a497John McCall    return CGM.getCodeGenOpts().ObjCAutoRefCountExceptions
2449f85e193739c953358c865005855253af4f68a497John McCall             ? NormalAndEHCleanup : NormalCleanup;
2450f85e193739c953358c865005855253af4f68a497John McCall  }
2451f85e193739c953358c865005855253af4f68a497John McCall
2452f85e193739c953358c865005855253af4f68a497John McCall  // ARC primitives.
2453f85e193739c953358c865005855253af4f68a497John McCall  void EmitARCInitWeak(llvm::Value *value, llvm::Value *addr);
2454f85e193739c953358c865005855253af4f68a497John McCall  void EmitARCDestroyWeak(llvm::Value *addr);
2455f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitARCLoadWeak(llvm::Value *addr);
2456f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitARCLoadWeakRetained(llvm::Value *addr);
2457f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitARCStoreWeak(llvm::Value *value, llvm::Value *addr,
2458f85e193739c953358c865005855253af4f68a497John McCall                                bool ignored);
2459f85e193739c953358c865005855253af4f68a497John McCall  void EmitARCCopyWeak(llvm::Value *dst, llvm::Value *src);
2460f85e193739c953358c865005855253af4f68a497John McCall  void EmitARCMoveWeak(llvm::Value *dst, llvm::Value *src);
2461f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitARCRetainAutorelease(QualType type, llvm::Value *value);
2462f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitARCRetainAutoreleaseNonBlock(llvm::Value *value);
2463545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall  llvm::Value *EmitARCStoreStrong(LValue lvalue, llvm::Value *value,
24645b07e8077a20b80fee90bd76c43c6150c676e4a8John McCall                                  bool resultIgnored);
2465f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitARCStoreStrongCall(llvm::Value *addr, llvm::Value *value,
24665b07e8077a20b80fee90bd76c43c6150c676e4a8John McCall                                      bool resultIgnored);
2467f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitARCRetain(QualType type, llvm::Value *value);
2468f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitARCRetainNonBlock(llvm::Value *value);
2469348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall  llvm::Value *EmitARCRetainBlock(llvm::Value *value, bool mandatory);
24705b07e8077a20b80fee90bd76c43c6150c676e4a8John McCall  void EmitARCDestroyStrong(llvm::Value *addr, ARCPreciseLifetime_t precise);
24715b07e8077a20b80fee90bd76c43c6150c676e4a8John McCall  void EmitARCRelease(llvm::Value *value, ARCPreciseLifetime_t precise);
2472f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitARCAutorelease(llvm::Value *value);
2473f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitARCAutoreleaseReturnValue(llvm::Value *value);
2474f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitARCRetainAutoreleaseReturnValue(llvm::Value *value);
2475f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitARCRetainAutoreleasedReturnValue(llvm::Value *value);
2476f85e193739c953358c865005855253af4f68a497John McCall
2477f85e193739c953358c865005855253af4f68a497John McCall  std::pair<LValue,llvm::Value*>
2478f85e193739c953358c865005855253af4f68a497John McCall  EmitARCStoreAutoreleasing(const BinaryOperator *e);
2479f85e193739c953358c865005855253af4f68a497John McCall  std::pair<LValue,llvm::Value*>
2480f85e193739c953358c865005855253af4f68a497John McCall  EmitARCStoreStrong(const BinaryOperator *e, bool ignored);
2481f85e193739c953358c865005855253af4f68a497John McCall
24822b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall  llvm::Value *EmitObjCThrowOperand(const Expr *expr);
24832b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall
2484f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitObjCProduceObject(QualType T, llvm::Value *Ptr);
2485f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitObjCConsumeObject(QualType T, llvm::Value *Ptr);
2486f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitObjCExtendObjectLifetime(QualType T, llvm::Value *Ptr);
2487f85e193739c953358c865005855253af4f68a497John McCall
2488348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall  llvm::Value *EmitARCExtendBlockObject(const Expr *expr);
2489f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitARCRetainScalarExpr(const Expr *expr);
2490f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitARCRetainAutoreleaseScalarExpr(const Expr *expr);
2491f85e193739c953358c865005855253af4f68a497John McCall
2492c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines  void EmitARCIntrinsicUse(ArrayRef<llvm::Value*> values);
2493b6a6079449a5275c283982e19b0c38e165833bb2John McCall
2494bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  static Destroyer destroyARCStrongImprecise;
2495bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  static Destroyer destroyARCStrongPrecise;
2496bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  static Destroyer destroyARCWeak;
2497bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
2498f85e193739c953358c865005855253af4f68a497John McCall  void EmitObjCAutoreleasePoolPop(llvm::Value *Ptr);
2499f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitObjCAutoreleasePoolPush();
2500f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitObjCMRRAutoreleasePoolPush();
2501f85e193739c953358c865005855253af4f68a497John McCall  void EmitObjCAutoreleasePoolCleanup(llvm::Value *Ptr);
2502f85e193739c953358c865005855253af4f68a497John McCall  void EmitObjCMRRAutoreleasePoolPop(llvm::Value *Ptr);
2503f85e193739c953358c865005855253af4f68a497John McCall
2504d4ec562b3aaf50ea9015f82c96ebfd05a35fc7efRichard Smith  /// \brief Emits a reference binding to the passed in expression.
2505d4ec562b3aaf50ea9015f82c96ebfd05a35fc7efRichard Smith  RValue EmitReferenceBindingToExpr(const Expr *E);
25063aba09376c5f49c4c8d176109ea4835bc2c528eeAnders Carlsson
2507883f6a7cc7dccb1d675e27121a82614d63492a8dChris Lattner  //===--------------------------------------------------------------------===//
2508bfc0c1ae4b505b52b36f572a9641f99e76879cabChris Lattner  //                           Expression Emission
2509883f6a7cc7dccb1d675e27121a82614d63492a8dChris Lattner  //===--------------------------------------------------------------------===//
2510bfc0c1ae4b505b52b36f572a9641f99e76879cabChris Lattner
2511bfc0c1ae4b505b52b36f572a9641f99e76879cabChris Lattner  // Expressions are broken into three classes: scalar, complex, aggregate.
25120dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
25130dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// EmitScalarExpr - Emit the computation of the specified expression of LLVM
25140dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// scalar type, returning the result.
251514c5cbf59cffee52275230922283a247de407712Anders Carlsson  llvm::Value *EmitScalarExpr(const Expr *E , bool IgnoreResultAssign = false);
25160dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
25173707b255f8993fb362904c9cff87b0e9bc6ca317Chris Lattner  /// EmitScalarConversion - Emit a conversion from the specified type to the
25183707b255f8993fb362904c9cff87b0e9bc6ca317Chris Lattner  /// specified destination type, both of which are LLVM scalar types.
25193707b255f8993fb362904c9cff87b0e9bc6ca317Chris Lattner  llvm::Value *EmitScalarConversion(llvm::Value *Src, QualType SrcTy,
25203707b255f8993fb362904c9cff87b0e9bc6ca317Chris Lattner                                    QualType DstTy);
25210dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
25224f1a7b380809b4ca19ad2daff61bc11cd1e979d8Chris Lattner  /// EmitComplexToScalarConversion - Emit a conversion from the specified
25230dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// complex type to the specified destination type, where the destination type
25240dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// is an LLVM scalar type.
25254f1a7b380809b4ca19ad2daff61bc11cd1e979d8Chris Lattner  llvm::Value *EmitComplexToScalarConversion(ComplexPairTy Src, QualType SrcTy,
25264f1a7b380809b4ca19ad2daff61bc11cd1e979d8Chris Lattner                                             QualType DstTy);
25270dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
25280dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
2529558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  /// EmitAggExpr - Emit the computation of the specified expression
2530558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  /// of aggregate type.  The result is computed into the given slot,
2531558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  /// which may be null to indicate that the value is not needed.
2532e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  void EmitAggExpr(const Expr *E, AggValueSlot AS);
25330dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
253418aba0dd518e486d8b50523e7dafb4b5657135d2Daniel Dunbar  /// EmitAggExprToLValue - Emit the computation of the specified expression of
253518aba0dd518e486d8b50523e7dafb4b5657135d2Daniel Dunbar  /// aggregate type into a temporary LValue.
253618aba0dd518e486d8b50523e7dafb4b5657135d2Daniel Dunbar  LValue EmitAggExprToLValue(const Expr *E);
253718aba0dd518e486d8b50523e7dafb4b5657135d2Daniel Dunbar
2538082b02e8403d3ee9d2ded969fbe0e5d472f04cd8Fariborz Jahanian  /// EmitGCMemmoveCollectable - Emit special API for structs with object
2539082b02e8403d3ee9d2ded969fbe0e5d472f04cd8Fariborz Jahanian  /// pointers.
2540082b02e8403d3ee9d2ded969fbe0e5d472f04cd8Fariborz Jahanian  void EmitGCMemmoveCollectable(llvm::Value *DestPtr, llvm::Value *SrcPtr,
254108c321380fff07d476a19daab6d29522c046cd49Fariborz Jahanian                                QualType Ty);
2542082b02e8403d3ee9d2ded969fbe0e5d472f04cd8Fariborz Jahanian
25430c24c808e4dce43e88abf2d5f98546b901bd4315John McCall  /// EmitExtendGCLifetime - Given a pointer to an Objective-C object,
25440c24c808e4dce43e88abf2d5f98546b901bd4315John McCall  /// make sure it survives garbage collection until this point.
25450c24c808e4dce43e88abf2d5f98546b901bd4315John McCall  void EmitExtendGCLifetime(llvm::Value *object);
25460c24c808e4dce43e88abf2d5f98546b901bd4315John McCall
2547b6ef18a2b06d6760459e1756a61f79ff496cee19Chris Lattner  /// EmitComplexExpr - Emit the computation of the specified expression of
254823b1cdb0b5e089468bb8475b8ec9287af67b4b59Chris Lattner  /// complex type, returning the result.
2549b418d74c11498b7e1044103131e2e3be4d63512eJohn McCall  ComplexPairTy EmitComplexExpr(const Expr *E,
2550b418d74c11498b7e1044103131e2e3be4d63512eJohn McCall                                bool IgnoreReal = false,
2551b418d74c11498b7e1044103131e2e3be4d63512eJohn McCall                                bool IgnoreImag = false);
25520dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
25539d232c884ea9872d6555df0fd7359699819bc1f1John McCall  /// EmitComplexExprIntoLValue - Emit the given expression of complex
25549d232c884ea9872d6555df0fd7359699819bc1f1John McCall  /// type and place its result into the specified l-value.
25559d232c884ea9872d6555df0fd7359699819bc1f1John McCall  void EmitComplexExprIntoLValue(const Expr *E, LValue dest, bool isInit);
25569d232c884ea9872d6555df0fd7359699819bc1f1John McCall
25579d232c884ea9872d6555df0fd7359699819bc1f1John McCall  /// EmitStoreOfComplex - Store a complex number into the specified l-value.
25589d232c884ea9872d6555df0fd7359699819bc1f1John McCall  void EmitStoreOfComplex(ComplexPairTy V, LValue dest, bool isInit);
25597f8ea5c5b3a6a4332a841eefdd86b0726722ea7bDaniel Dunbar
25609d232c884ea9872d6555df0fd7359699819bc1f1John McCall  /// EmitLoadOfComplex - Load a complex number from the specified l-value.
25614ee7dc2369c1f0257a73b2e83a7d38fdebdd9176Nick Lewycky  ComplexPairTy EmitLoadOfComplex(LValue src, SourceLocation loc);
25622621fd1d6d3c5eadcae246859f62738645df7540Chris Lattner
25630f30a12ce7b3d4d86c9ca9072f587da77c8eef34Chandler Carruth  /// AddInitializerToStaticVarDecl - Add the initializer for 'D' to the
25640f30a12ce7b3d4d86c9ca9072f587da77c8eef34Chandler Carruth  /// global variable that has already been created for it.  If the initializer
25650f30a12ce7b3d4d86c9ca9072f587da77c8eef34Chandler Carruth  /// has a different type than GV does, this may free GV and return a different
25660f30a12ce7b3d4d86c9ca9072f587da77c8eef34Chandler Carruth  /// one.  Otherwise it just returns GV.
25670f30a12ce7b3d4d86c9ca9072f587da77c8eef34Chandler Carruth  llvm::GlobalVariable *
25680f30a12ce7b3d4d86c9ca9072f587da77c8eef34Chandler Carruth  AddInitializerToStaticVarDecl(const VarDecl &D,
25690f30a12ce7b3d4d86c9ca9072f587da77c8eef34Chandler Carruth                                llvm::GlobalVariable *GV);
25709cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
25710096acf421c4609ce7f43e8b05f8c5ca866d4611Daniel Dunbar
25723b2e16b3d25f6b311dba2871e2a566c96238c3d2Anders Carlsson  /// EmitCXXGlobalVarDeclInit - Create the initializer for a C++
25733b2e16b3d25f6b311dba2871e2a566c96238c3d2Anders Carlsson  /// variable with global storage.
25747ca4850a3e3530fa6c93b64b740446e32c97f992Richard Smith  void EmitCXXGlobalVarDeclInit(const VarDecl &D, llvm::Constant *DeclPtr,
25757ca4850a3e3530fa6c93b64b740446e32c97f992Richard Smith                                bool PerformInit);
25763b2e16b3d25f6b311dba2871e2a566c96238c3d2Anders Carlsson
2577176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  llvm::Constant *createAtExitStub(const VarDecl &VD, llvm::Constant *Dtor,
2578176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                                   llvm::Constant *Addr);
2579176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
258020bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  /// Call atexit() with a function that passes the given argument to
258120bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  /// the given function.
2582c7971a9efdf9880448a69aabb5182c3c27eecf6dDavid Blaikie  void registerGlobalDtorWithAtExit(const VarDecl &D, llvm::Constant *fn,
2583c7971a9efdf9880448a69aabb5182c3c27eecf6dDavid Blaikie                                    llvm::Constant *addr);
25841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25853030eb82593097502469a8b3fc26112c79c75605John McCall  /// Emit code in this function to perform a guarded variable
25863030eb82593097502469a8b3fc26112c79c75605John McCall  /// initialization.  Guarded initializations are used when it's not
25873030eb82593097502469a8b3fc26112c79c75605John McCall  /// possible to prove that an initialization will be done exactly
25883030eb82593097502469a8b3fc26112c79c75605John McCall  /// once, e.g. with a static local variable or a static data member
25893030eb82593097502469a8b3fc26112c79c75605John McCall  /// of a class template.
25900f30a12ce7b3d4d86c9ca9072f587da77c8eef34Chandler Carruth  void EmitCXXGuardedInit(const VarDecl &D, llvm::GlobalVariable *DeclPtr,
25917ca4850a3e3530fa6c93b64b740446e32c97f992Richard Smith                          bool PerformInit);
25925cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
2593efb0fa9e11f75af51744a6159530ef7cc8efa24aDaniel Dunbar  /// GenerateCXXGlobalInitFunc - Generates code for initializing global
2594efb0fa9e11f75af51744a6159530ef7cc8efa24aDaniel Dunbar  /// variables.
2595efb0fa9e11f75af51744a6159530ef7cc8efa24aDaniel Dunbar  void GenerateCXXGlobalInitFunc(llvm::Function *Fn,
2596176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                                 ArrayRef<llvm::Function *> CXXThreadLocals,
25976bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                 llvm::GlobalVariable *Guard = nullptr);
2598efb0fa9e11f75af51744a6159530ef7cc8efa24aDaniel Dunbar
25993f88f686e32949ffe02ccb551f482f9cf810358bJohn McCall  /// GenerateCXXGlobalDtorsFunc - Generates code for destroying global
2600efb0fa9e11f75af51744a6159530ef7cc8efa24aDaniel Dunbar  /// variables.
26013f88f686e32949ffe02ccb551f482f9cf810358bJohn McCall  void GenerateCXXGlobalDtorsFunc(llvm::Function *Fn,
26023f88f686e32949ffe02ccb551f482f9cf810358bJohn McCall                                  const std::vector<std::pair<llvm::WeakVH,
26033f88f686e32949ffe02ccb551f482f9cf810358bJohn McCall                                  llvm::Constant*> > &DtorsAndObjects);
2604efb0fa9e11f75af51744a6159530ef7cc8efa24aDaniel Dunbar
2605d26bc76c98006609002d9930f8840490e88ac5b5John McCall  void GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn,
2606d26bc76c98006609002d9930f8840490e88ac5b5John McCall                                        const VarDecl *D,
26077ca4850a3e3530fa6c93b64b740446e32c97f992Richard Smith                                        llvm::GlobalVariable *Addr,
26087ca4850a3e3530fa6c93b64b740446e32c97f992Richard Smith                                        bool PerformInit);
2609efb0fa9e11f75af51744a6159530ef7cc8efa24aDaniel Dunbar
2610558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  void EmitCXXConstructExpr(const CXXConstructExpr *E, AggValueSlot Dest);
261134999876e215b22febc240b1a6dc054215d12f9cFariborz Jahanian
261234999876e215b22febc240b1a6dc054215d12f9cFariborz Jahanian  void EmitSynthesizedCXXCopyCtor(llvm::Value *Dest, llvm::Value *Src,
2613830937bc1100fba7682f7c32c40512085870f50cFariborz Jahanian                                  const Expr *Exp);
26141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26151a343ebbf413e8eae6b2737b2b2d79cbf5765571John McCall  void enterFullExpression(const ExprWithCleanups *E) {
26161a343ebbf413e8eae6b2737b2b2d79cbf5765571John McCall    if (E->getNumObjects() == 0) return;
26171a343ebbf413e8eae6b2737b2b2d79cbf5765571John McCall    enterNonTrivialFullExpression(E);
26181a343ebbf413e8eae6b2737b2b2d79cbf5765571John McCall  }
26191a343ebbf413e8eae6b2737b2b2d79cbf5765571John McCall  void enterNonTrivialFullExpression(const ExprWithCleanups *E);
26201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26214c71b8cded575b0cfc133c5da4502ca613982094Richard Smith  void EmitCXXThrowExpr(const CXXThrowExpr *E, bool KeepInsertionPoint = true);
26221eb2e59338c4b9c0429fc39ca98662adc9e7a3f2Douglas Gregor
26234c5d8afd100189b6cce4fd89bfb8aec5700acb50Eli Friedman  void EmitLambdaExpr(const LambdaExpr *E, AggValueSlot Dest);
26244c5d8afd100189b6cce4fd89bfb8aec5700acb50Eli Friedman
26256bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  RValue EmitAtomicExpr(AtomicExpr *E, llvm::Value *Dest = nullptr);
2626276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman
26270ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar  //===--------------------------------------------------------------------===//
262877f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  //                         Annotations Emission
262977f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  //===--------------------------------------------------------------------===//
263077f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge
263177f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  /// Emit an annotation call (intrinsic or builtin).
263277f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  llvm::Value *EmitAnnotationCall(llvm::Value *AnnotationFn,
263377f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge                                  llvm::Value *AnnotatedVal,
2634cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko                                  StringRef AnnotationStr,
263577f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge                                  SourceLocation Location);
263677f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge
263777f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  /// Emit local annotations for the local variable V, declared by D.
263877f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  void EmitVarAnnotations(const VarDecl *D, llvm::Value *V);
263977f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge
264077f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  /// Emit field annotations for the given field & value. Returns the
264177f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  /// annotation result.
264277f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  llvm::Value *EmitFieldAnnotations(const FieldDecl *D, llvm::Value *V);
264377f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge
264477f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  //===--------------------------------------------------------------------===//
26450ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar  //                             Internal Helpers
26460ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar  //===--------------------------------------------------------------------===//
26470dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
26480946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  /// ContainsLabel - Return true if the statement contains a label in it.  If
26490946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  /// this statement is not executed normally, it not containing a label means
26500946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  /// that we can just remove the code.
26510946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  static bool ContainsLabel(const Stmt *S, bool IgnoreCaseStmts = false);
26520dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
2653ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  /// containsBreak - Return true if the statement contains a break out of it.
2654ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  /// If the statement (recursively) contains a switch or loop with a break
2655ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  /// inside of it, this is fine.
2656ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  static bool containsBreak(const Stmt *S);
2657ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner
26584bc0455d826e9634b7a68c0d06146edd633c9453Daniel Dunbar  /// ConstantFoldsToSimpleInteger - If the specified expression does not fold
2659c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner  /// to a constant, or if it does but contains a label, return false.  If it
2660c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner  /// constant folds return true and set the boolean result in Result.
2661c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner  bool ConstantFoldsToSimpleInteger(const Expr *Cond, bool &Result);
26620dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
2663ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  /// ConstantFoldsToSimpleInteger - If the specified expression does not fold
2664ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  /// to a constant, or if it does but contains a label, return false.  If it
2665ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  /// constant folds return true and set the folded value.
2666e1ecdc168175719d74e112bcacd4aae5e12d4631Richard Trieu  bool ConstantFoldsToSimpleInteger(const Expr *Cond, llvm::APSInt &Result);
2667ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner
266831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  /// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an
266931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  /// if statement) to the specified blocks.  Based on the condition, this might
267031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  /// try to simplify the codegen of the conditional based on the branch.
2671651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// TrueCount should be the number of times we expect the condition to
2672651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// evaluate to true based on PGO data.
26739bc47e29dce8f095be7a6d07dbb02a5a7a112949Chris Lattner  void EmitBranchOnBoolExpr(const Expr *Cond, llvm::BasicBlock *TrueBlock,
2674651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                            llvm::BasicBlock *FalseBlock, uint64_t TrueCount);
2675be07f60131bc6f8d6696f4644df1ef667a1730d5Mike Stump
26764def70d3040e73707c738f7c366737a986135edfRichard Smith  /// \brief Emit a description of a type in a format suitable for passing to
26774def70d3040e73707c738f7c366737a986135edfRichard Smith  /// a runtime sanitizer handler.
26784def70d3040e73707c738f7c366737a986135edfRichard Smith  llvm::Constant *EmitCheckTypeDescriptor(QualType T);
26794def70d3040e73707c738f7c366737a986135edfRichard Smith
26804def70d3040e73707c738f7c366737a986135edfRichard Smith  /// \brief Convert a value into a format suitable for passing to a runtime
26814def70d3040e73707c738f7c366737a986135edfRichard Smith  /// sanitizer handler.
26824def70d3040e73707c738f7c366737a986135edfRichard Smith  llvm::Value *EmitCheckValue(llvm::Value *V);
26834def70d3040e73707c738f7c366737a986135edfRichard Smith
26844def70d3040e73707c738f7c366737a986135edfRichard Smith  /// \brief Emit a description of a source location in a format suitable for
26854def70d3040e73707c738f7c366737a986135edfRichard Smith  /// passing to a runtime sanitizer handler.
26864def70d3040e73707c738f7c366737a986135edfRichard Smith  llvm::Constant *EmitCheckSourceLocation(SourceLocation Loc);
26874def70d3040e73707c738f7c366737a986135edfRichard Smith
2688cc305617e952f8c3a06b63d9225d0dd7769341b7Richard Smith  /// \brief Create a basic block that will call a handler function in a
2689cc305617e952f8c3a06b63d9225d0dd7769341b7Richard Smith  /// sanitizer runtime with the provided arguments, and create a conditional
2690cc305617e952f8c3a06b63d9225d0dd7769341b7Richard Smith  /// branch to it.
2691176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  void EmitCheck(ArrayRef<std::pair<llvm::Value *, SanitizerKind>> Checked,
2692176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                 StringRef CheckName, ArrayRef<llvm::Constant *> StaticArgs,
2693176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                 ArrayRef<llvm::Value *> DynamicArgs);
26949cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
2695cc305617e952f8c3a06b63d9225d0dd7769341b7Richard Smith  /// \brief Create a basic block that will call the trap intrinsic, and emit a
2696cc305617e952f8c3a06b63d9225d0dd7769341b7Richard Smith  /// conditional branch to it, for the -ftrapv checks.
269778d85b1c5830a881c0a20a1b3fea99ee73149590Chad Rosier  void EmitTrapCheck(llvm::Value *Checked);
2698cc305617e952f8c3a06b63d9225d0dd7769341b7Richard Smith
269921c9ad9d29d08a287292c670e7c52bc522c7f8bbAnders Carlsson  /// EmitCallArg - Emit a single call argument.
2700413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall  void EmitCallArg(CallArgList &args, const Expr *E, QualType ArgType);
270121c9ad9d29d08a287292c670e7c52bc522c7f8bbAnders Carlsson
27022736071ea3a46f90e65c93418961611d96c10ab9John McCall  /// EmitDelegateCallArg - We are performing a delegate call; that
27032736071ea3a46f90e65c93418961611d96c10ab9John McCall  /// is, the current function is delegating to another one.  Produce
27042736071ea3a46f90e65c93418961611d96c10ab9John McCall  /// a r-value suitable for passing the given parameter.
27054ee7dc2369c1f0257a73b2e83a7d38fdebdd9176Nick Lewycky  void EmitDelegateCallArg(CallArgList &args, const VarDecl *param,
27064ee7dc2369c1f0257a73b2e83a7d38fdebdd9176Nick Lewycky                           SourceLocation loc);
27072736071ea3a46f90e65c93418961611d96c10ab9John McCall
2708c5096cbf7a42e0f9012945b00d9037a5b5a88d72Peter Collingbourne  /// SetFPAccuracy - Set the minimum required accuracy of the given floating
2709c5096cbf7a42e0f9012945b00d9037a5b5a88d72Peter Collingbourne  /// point operation, expressed as the maximum relative error in ulp.
27108250016fb9491fcfde7fce05230d66e267944484Duncan Sands  void SetFPAccuracy(llvm::Value *Val, float Accuracy);
2711c5096cbf7a42e0f9012945b00d9037a5b5a88d72Peter Collingbourne
271231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattnerprivate:
2713c3f8955d7da1019dbe16b0bdf3e49d2e08d988e9Rafael Espindola  llvm::MDNode *getRangeForLoadFromType(QualType Ty);
271429e0bccf2bcce22b877f8b2ed173f564c116b97eDaniel Dunbar  void EmitReturnOfRValue(RValue RV, QualType Ty);
271529e0bccf2bcce22b877f8b2ed173f564c116b97eDaniel Dunbar
2716651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void deferPlaceholderReplacement(llvm::Instruction *Old, llvm::Value *New);
2717651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
2718651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  llvm::SmallVector<std::pair<llvm::Instruction *, llvm::Value *>, 4>
2719651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  DeferredReplacements;
2720651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
27215627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  /// ExpandTypeFromArgs - Reconstruct a structure of type \arg Ty
27225627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  /// from function arguments into \arg Dst. See ABIArgInfo::Expand.
27235627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  ///
27245627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  /// \param AI - The first function argument of the expansion.
2725176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  void ExpandTypeFromArgs(QualType Ty, LValue Dst,
2726176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                          SmallVectorImpl<llvm::Argument *>::iterator &AI);
2727176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
2728176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  /// ExpandTypeToArgs - Expand an RValue \arg RV, with the LLVM type for \arg
2729176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  /// Ty, into individual arguments on the provided vector \arg IRCallArgs,
2730176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  /// starting at index \arg IRCallArgPos. See ABIArgInfo::Expand.
2731176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  void ExpandTypeToArgs(QualType Ty, RValue RV, llvm::FunctionType *IRFuncTy,
2732176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                        SmallVectorImpl<llvm::Value *> &IRCallArgs,
2733176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                        unsigned &IRCallArgPos);
2734c8c7b187adcd989e01a9fba1ebead6c2abd770c9Anders Carlsson
273542b60551eff3a424e191b293bfd606559dc96bffChad Rosier  llvm::Value* EmitAsmInput(const TargetInfo::ConstraintInfo &Info,
2736c8c7b187adcd989e01a9fba1ebead6c2abd770c9Anders Carlsson                            const Expr *InputExpr, std::string &ConstraintStr);
27370dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
273842b60551eff3a424e191b293bfd606559dc96bffChad Rosier  llvm::Value* EmitAsmInputLValue(const TargetInfo::ConstraintInfo &Info,
27396d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman                                  LValue InputValue, QualType InputType,
27404ee7dc2369c1f0257a73b2e83a7d38fdebdd9176Nick Lewycky                                  std::string &ConstraintStr,
27414ee7dc2369c1f0257a73b2e83a7d38fdebdd9176Nick Lewycky                                  SourceLocation Loc);
27426d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman
2743651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinespublic:
27440139bb96494b4c4ba0824617d5d2495dc7e44c76Anders Carlsson  /// EmitCallArgs - Emit call arguments for a function.
2745651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  template <typename T>
2746651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void EmitCallArgs(CallArgList &Args, const T *CallArgTypeInfo,
27470139bb96494b4c4ba0824617d5d2495dc7e44c76Anders Carlsson                    CallExpr::const_arg_iterator ArgBeg,
2748b0e603c63813c8378e35a75408ab4cefe71e93e2Adrian Prantl                    CallExpr::const_arg_iterator ArgEnd,
2749176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                    const FunctionDecl *CalleeDecl = nullptr,
27500e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                    unsigned ParamsToSkip = 0) {
2751651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    SmallVector<QualType, 16> ArgTypes;
2752b0e603c63813c8378e35a75408ab4cefe71e93e2Adrian Prantl    CallExpr::const_arg_iterator Arg = ArgBeg;
2753af23f69aa6247e6bd20a66b2e16babb91f192535Anders Carlsson
2754176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    assert((ParamsToSkip == 0 || CallArgTypeInfo) &&
2755176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines           "Can't skip parameters if type info is not provided");
2756176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    if (CallArgTypeInfo) {
2757176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      // First, use the argument types that the type info knows about
2758176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      for (auto I = CallArgTypeInfo->param_type_begin() + ParamsToSkip,
2759176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                E = CallArgTypeInfo->param_type_end();
2760176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines           I != E; ++I, ++Arg) {
2761176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        assert(Arg != ArgEnd && "Running over edge of argument list!");
27620e2c34f92f00628d48968dfea096d36381f494cbStephen Hines        assert(
27630e2c34f92f00628d48968dfea096d36381f494cbStephen Hines            ((*I)->isVariablyModifiedType() ||
27640e2c34f92f00628d48968dfea096d36381f494cbStephen Hines             getContext()
27650e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                     .getCanonicalType((*I).getNonReferenceType())
27660e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                     .getTypePtr() ==
27670e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                 getContext().getCanonicalType(Arg->getType()).getTypePtr()) &&
27680e2c34f92f00628d48968dfea096d36381f494cbStephen Hines            "type mismatch in call argument!");
2769176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        ArgTypes.push_back(*I);
2770176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      }
2771af23f69aa6247e6bd20a66b2e16babb91f192535Anders Carlsson    }
27721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2773651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    // Either we've emitted all the call args, or we have a call to variadic
2774176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    // function.
2775176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    assert(
2776176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        (Arg == ArgEnd || !CallArgTypeInfo || CallArgTypeInfo->isVariadic()) &&
2777176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        "Extra arguments in non-variadic function!");
2778651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
2779af23f69aa6247e6bd20a66b2e16babb91f192535Anders Carlsson    // If we still have any arguments, emit them using the type of the argument.
2780651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    for (; Arg != ArgEnd; ++Arg)
2781176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      ArgTypes.push_back(getVarArgType(*Arg));
2782b0e603c63813c8378e35a75408ab4cefe71e93e2Adrian Prantl
27830e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    EmitCallArgs(Args, ArgTypes, ArgBeg, ArgEnd, CalleeDecl, ParamsToSkip);
2784af23f69aa6247e6bd20a66b2e16babb91f192535Anders Carlsson  }
2785492c4f998d848673d3d6c9e6416115df4036a71dJohn McCall
2786651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void EmitCallArgs(CallArgList &Args, ArrayRef<QualType> ArgTypes,
2787651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                    CallExpr::const_arg_iterator ArgBeg,
2788c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines                    CallExpr::const_arg_iterator ArgEnd,
2789176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                    const FunctionDecl *CalleeDecl = nullptr,
27900e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                    unsigned ParamsToSkip = 0);
2791651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
2792651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesprivate:
2793176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  QualType getVarArgType(const Expr *Arg);
2794176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
2795492c4f998d848673d3d6c9e6416115df4036a71dJohn McCall  const TargetCodeGenInfo &getTargetHooks() const {
2796492c4f998d848673d3d6c9e6416115df4036a71dJohn McCall    return CGM.getTargetCodeGenInfo();
2797492c4f998d848673d3d6c9e6416115df4036a71dJohn McCall  }
2798744016dde06fcffd50931e94a98c850f8b12cd87John McCall
2799744016dde06fcffd50931e94a98c850f8b12cd87John McCall  void EmitDeclMetadata();
2800f0c11f7e6848f023ced6a5b51399ba787c7d4d0bJohn McCall
2801f0c11f7e6848f023ced6a5b51399ba787c7d4d0bJohn McCall  CodeGenModule::ByrefHelpers *
28022acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  buildByrefHelpers(llvm::StructType &byrefType,
2803f0c11f7e6848f023ced6a5b51399ba787c7d4d0bJohn McCall                    const AutoVarEmission &emission);
2804b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman
2805b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman  void AddObjCARCExceptionMetadata(llvm::Instruction *Inst);
2806f4c3db175101cbf94dad59419c3ed7aed51bf606Jay Foad
2807ea93e40785ffeadfac66b948c95f9490ec26207aEli Friedman  /// GetPointeeAlignment - Given an expression with a pointer type, emit the
2808ea93e40785ffeadfac66b948c95f9490ec26207aEli Friedman  /// value and compute our best estimate of the alignment of the pointee.
2809ea93e40785ffeadfac66b948c95f9490ec26207aEli Friedman  std::pair<llvm::Value*, unsigned> EmitPointerWithAlignment(const Expr *Addr);
28100e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
28110e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  llvm::Value *GetValueForARMHint(unsigned BuiltinID);
28125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer};
28131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2814150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall/// Helper class with most of the code for saving a value for a
2815150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall/// conditional expression cleanup.
2816804b807ea918184d6de63bd745e1ff75a9bfc679John McCallstruct DominatingLLVMValue {
2817150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  typedef llvm::PointerIntPair<llvm::Value*, 1, bool> saved_type;
2818150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
2819150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  /// Answer whether the given value needs extra work to be saved.
2820150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  static bool needsSaving(llvm::Value *value) {
2821150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    // If it's not an instruction, we don't need to save.
2822150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    if (!isa<llvm::Instruction>(value)) return false;
2823150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
2824150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    // If it's an instruction in the entry block, we don't need to save.
2825150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    llvm::BasicBlock *block = cast<llvm::Instruction>(value)->getParent();
2826150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    return (block != &block->getParent()->getEntryBlock());
2827150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  }
2828150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
2829150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  /// Try to save the given value.
2830150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  static saved_type save(CodeGenFunction &CGF, llvm::Value *value) {
2831150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    if (!needsSaving(value)) return saved_type(value, false);
2832150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
2833150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    // Otherwise we need an alloca.
2834150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    llvm::Value *alloca =
2835150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      CGF.CreateTempAlloca(value->getType(), "cond-cleanup.save");
2836150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    CGF.Builder.CreateStore(value, alloca);
2837150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
2838150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    return saved_type(alloca, true);
2839150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  }
2840150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
2841150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  static llvm::Value *restore(CodeGenFunction &CGF, saved_type value) {
2842150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    if (!value.getInt()) return value.getPointer();
2843150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    return CGF.Builder.CreateLoad(value.getPointer());
2844150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  }
2845150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall};
2846150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
2847804b807ea918184d6de63bd745e1ff75a9bfc679John McCall/// A partial specialization of DominatingValue for llvm::Values that
2848804b807ea918184d6de63bd745e1ff75a9bfc679John McCall/// might be llvm::Instructions.
2849804b807ea918184d6de63bd745e1ff75a9bfc679John McCalltemplate <class T> struct DominatingPointer<T,true> : DominatingLLVMValue {
2850804b807ea918184d6de63bd745e1ff75a9bfc679John McCall  typedef T *type;
2851804b807ea918184d6de63bd745e1ff75a9bfc679John McCall  static type restore(CodeGenFunction &CGF, saved_type value) {
2852804b807ea918184d6de63bd745e1ff75a9bfc679John McCall    return static_cast<T*>(DominatingLLVMValue::restore(CGF, value));
2853804b807ea918184d6de63bd745e1ff75a9bfc679John McCall  }
2854804b807ea918184d6de63bd745e1ff75a9bfc679John McCall};
2855804b807ea918184d6de63bd745e1ff75a9bfc679John McCall
2856804b807ea918184d6de63bd745e1ff75a9bfc679John McCall/// A specialization of DominatingValue for RValue.
2857804b807ea918184d6de63bd745e1ff75a9bfc679John McCalltemplate <> struct DominatingValue<RValue> {
2858804b807ea918184d6de63bd745e1ff75a9bfc679John McCall  typedef RValue type;
2859804b807ea918184d6de63bd745e1ff75a9bfc679John McCall  class saved_type {
2860804b807ea918184d6de63bd745e1ff75a9bfc679John McCall    enum Kind { ScalarLiteral, ScalarAddress, AggregateLiteral,
2861804b807ea918184d6de63bd745e1ff75a9bfc679John McCall                AggregateAddress, ComplexAddress };
2862804b807ea918184d6de63bd745e1ff75a9bfc679John McCall
2863804b807ea918184d6de63bd745e1ff75a9bfc679John McCall    llvm::Value *Value;
2864804b807ea918184d6de63bd745e1ff75a9bfc679John McCall    Kind K;
2865804b807ea918184d6de63bd745e1ff75a9bfc679John McCall    saved_type(llvm::Value *v, Kind k) : Value(v), K(k) {}
2866804b807ea918184d6de63bd745e1ff75a9bfc679John McCall
2867804b807ea918184d6de63bd745e1ff75a9bfc679John McCall  public:
2868804b807ea918184d6de63bd745e1ff75a9bfc679John McCall    static bool needsSaving(RValue value);
2869804b807ea918184d6de63bd745e1ff75a9bfc679John McCall    static saved_type save(CodeGenFunction &CGF, RValue value);
2870804b807ea918184d6de63bd745e1ff75a9bfc679John McCall    RValue restore(CodeGenFunction &CGF);
2871804b807ea918184d6de63bd745e1ff75a9bfc679John McCall
2872804b807ea918184d6de63bd745e1ff75a9bfc679John McCall    // implementations in CGExprCXX.cpp
2873804b807ea918184d6de63bd745e1ff75a9bfc679John McCall  };
2874804b807ea918184d6de63bd745e1ff75a9bfc679John McCall
2875804b807ea918184d6de63bd745e1ff75a9bfc679John McCall  static bool needsSaving(type value) {
2876804b807ea918184d6de63bd745e1ff75a9bfc679John McCall    return saved_type::needsSaving(value);
2877804b807ea918184d6de63bd745e1ff75a9bfc679John McCall  }
2878804b807ea918184d6de63bd745e1ff75a9bfc679John McCall  static saved_type save(CodeGenFunction &CGF, type value) {
2879804b807ea918184d6de63bd745e1ff75a9bfc679John McCall    return saved_type::save(CGF, value);
2880804b807ea918184d6de63bd745e1ff75a9bfc679John McCall  }
2881150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  static type restore(CodeGenFunction &CGF, saved_type value) {
2882804b807ea918184d6de63bd745e1ff75a9bfc679John McCall    return value.restore(CGF);
2883150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  }
2884150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall};
2885150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
28865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}  // end namespace CodeGen
28875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}  // end namespace clang
28884e1524babb095e70de1da882573eb6fbee98a857Fariborz Jahanian
28895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#endif
2890