15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- CodeGenFunction.cpp - Emit LLVM Code from ASTs for a Function ----===//
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//
105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// This coordinates the per-function state used while generating code.
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "CodeGenFunction.h"
15a4ae2294b6ebfb2554aacb6a6a0682fb5ed1f276Peter Collingbourne#include "CGCUDARuntime.h"
164c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall#include "CGCXXABI.h"
173f2af1002249c8acc9ce17f1fc50324864feb8e1Eli Friedman#include "CGDebugInfo.h"
186bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#include "CGOpenMPRuntime.h"
1955fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "CodeGenModule.h"
20651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#include "CodeGenPGO.h"
21b914e87377fd4d7642f544000a79f8648c6f06c9Peter Collingbourne#include "TargetInfo.h"
22de7fb8413b13651fd85b7125d08b3c9ac2816d9dDaniel Dunbar#include "clang/AST/ASTContext.h"
23c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel Dunbar#include "clang/AST/Decl.h"
242b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson#include "clang/AST/DeclCXX.h"
256a1e0eb557d47e85185e09bdf8721f53f4bf9c9cMike Stump#include "clang/AST/StmtCXX.h"
2655fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Basic/TargetInfo.h"
278b54999a831bb195c08541ca995ef0505c96193fMark Lacey#include "clang/CodeGen/CGFunctionInfo.h"
287255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner#include "clang/Frontend/CodeGenOptions.h"
293b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth#include "llvm/IR/DataLayout.h"
303b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth#include "llvm/IR/Intrinsics.h"
313b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth#include "llvm/IR/MDBuilder.h"
323b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth#include "llvm/IR/Operator.h"
335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace CodeGen;
355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
364904bf4e84cfb48080270ebaa9005327f18ab0e5Fariborz JahanianCodeGenFunction::CodeGenFunction(CodeGenModule &cgm, bool suppressNewContext)
37c3030bc285d90a139fb74629daadef5617283203David Blaikie    : CodeGenTypeCache(cgm), CGM(cgm), Target(cgm.getTarget()),
386bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      Builder(cgm.getModule().getContext(), llvm::ConstantFolder(),
396bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines              CGBuilderInserterTy(this)), CapturedStmtInfo(nullptr),
40ef8225444452a1486bd721f3285301fe84643b00Stephen Hines      SanOpts(&CGM.getLangOpts().Sanitize), AutoreleaseResult(false), BlockInfo(nullptr),
416bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      BlockPointer(nullptr), LambdaThisCaptureField(nullptr),
426bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      NormalCleanupDest(nullptr), NextCleanupDestIndex(1),
436bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      FirstBlockInfo(nullptr), EHResumeBlock(nullptr), ExceptionSlot(nullptr),
446bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      EHSelectorSlot(nullptr), DebugInfo(CGM.getModuleDebugInfo()),
456bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      DisableDebugInfo(false), DidCallStackSave(false), IndirectBranch(nullptr),
466bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      PGO(cgm), SwitchInsn(nullptr), SwitchWeights(nullptr),
476bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      CaseRangeBlock(nullptr), UnreachableBlock(nullptr), NumReturnExprs(0),
486bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      NumSimpleReturnExprs(0), CXXABIThisDecl(nullptr),
496bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      CXXABIThisValue(nullptr), CXXThisValue(nullptr),
506bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      CXXDefaultInitExprThis(nullptr), CXXStructorImplicitParamDecl(nullptr),
516bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      CXXStructorImplicitParamValue(nullptr), OutermostConditional(nullptr),
526bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      CurLexicalScope(nullptr), TerminateLandingPad(nullptr),
536bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      TerminateHandler(nullptr), TrapBB(nullptr) {
544904bf4e84cfb48080270ebaa9005327f18ab0e5Fariborz Jahanian  if (!suppressNewContext)
554904bf4e84cfb48080270ebaa9005327f18ab0e5Fariborz Jahanian    CGM.getCXXABI().getMangleContext().startNewFunction();
56cdeb7d57a4867c28e3e6a8c391cba6f13c781fa6Michael Ilseman
57cdeb7d57a4867c28e3e6a8c391cba6f13c781fa6Michael Ilseman  llvm::FastMathFlags FMF;
58cdeb7d57a4867c28e3e6a8c391cba6f13c781fa6Michael Ilseman  if (CGM.getLangOpts().FastMath)
5929573b0a825b888c233d7859a257e866ffc5276eBenjamin Kramer    FMF.setUnsafeAlgebra();
60cdeb7d57a4867c28e3e6a8c391cba6f13c781fa6Michael Ilseman  if (CGM.getLangOpts().FiniteMathOnly) {
6129573b0a825b888c233d7859a257e866ffc5276eBenjamin Kramer    FMF.setNoNaNs();
6229573b0a825b888c233d7859a257e866ffc5276eBenjamin Kramer    FMF.setNoInfs();
63cdeb7d57a4867c28e3e6a8c391cba6f13c781fa6Michael Ilseman  }
64cdeb7d57a4867c28e3e6a8c391cba6f13c781fa6Michael Ilseman  Builder.SetFastMathFlags(FMF);
654111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner}
665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
671a343ebbf413e8eae6b2737b2b2d79cbf5765571John McCallCodeGenFunction::~CodeGenFunction() {
688a07cd3f59a0ede54906bdc8ed21724815db27adRichard Smith  assert(LifetimeExtendedCleanupStack.empty() && "failed to emit a cleanup");
698a07cd3f59a0ede54906bdc8ed21724815db27adRichard Smith
701a343ebbf413e8eae6b2737b2b2d79cbf5765571John McCall  // If there are any unclaimed block infos, go ahead and destroy them
711a343ebbf413e8eae6b2737b2b2d79cbf5765571John McCall  // now.  This can happen if IR-gen gets clever and skips evaluating
721a343ebbf413e8eae6b2737b2b2d79cbf5765571John McCall  // something.
731a343ebbf413e8eae6b2737b2b2d79cbf5765571John McCall  if (FirstBlockInfo)
741a343ebbf413e8eae6b2737b2b2d79cbf5765571John McCall    destroyBlockInfos(FirstBlockInfo);
756bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
766bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  if (getLangOpts().OpenMP) {
776bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    CGM.getOpenMPRuntime().FunctionFinished(*this);
786bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  }
791a343ebbf413e8eae6b2737b2b2d79cbf5765571John McCall}
801a343ebbf413e8eae6b2737b2b2d79cbf5765571John McCall
815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
829cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnerllvm::Type *CodeGenFunction::ConvertTypeForMem(QualType T) {
838b1a343b6b360d63d5dc8a6beb841ce4414c1e00Daniel Dunbar  return CGM.getTypes().ConvertTypeForMem(T);
848b1a343b6b360d63d5dc8a6beb841ce4414c1e00Daniel Dunbar}
858b1a343b6b360d63d5dc8a6beb841ce4414c1e00Daniel Dunbar
869cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnerllvm::Type *CodeGenFunction::ConvertType(QualType T) {
875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return CGM.getTypes().ConvertType(T);
885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
909d232c884ea9872d6555df0fd7359699819bc1f1John McCallTypeEvaluationKind CodeGenFunction::getEvaluationKind(QualType type) {
919d232c884ea9872d6555df0fd7359699819bc1f1John McCall  type = type.getCanonicalType();
929d232c884ea9872d6555df0fd7359699819bc1f1John McCall  while (true) {
939d232c884ea9872d6555df0fd7359699819bc1f1John McCall    switch (type->getTypeClass()) {
94f2aac84709c418189e476ad591848dad50291885John McCall#define TYPE(name, parent)
95f2aac84709c418189e476ad591848dad50291885John McCall#define ABSTRACT_TYPE(name, parent)
96f2aac84709c418189e476ad591848dad50291885John McCall#define NON_CANONICAL_TYPE(name, parent) case Type::name:
97f2aac84709c418189e476ad591848dad50291885John McCall#define DEPENDENT_TYPE(name, parent) case Type::name:
98f2aac84709c418189e476ad591848dad50291885John McCall#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(name, parent) case Type::name:
99f2aac84709c418189e476ad591848dad50291885John McCall#include "clang/AST/TypeNodes.def"
1009d232c884ea9872d6555df0fd7359699819bc1f1John McCall      llvm_unreachable("non-canonical or dependent type in IR-generation");
1019d232c884ea9872d6555df0fd7359699819bc1f1John McCall
102dc7a4f5d7a7e3b60d4dc4a80338d7a2728540998Richard Smith    case Type::Auto:
103dc7a4f5d7a7e3b60d4dc4a80338d7a2728540998Richard Smith      llvm_unreachable("undeduced auto type in IR-generation");
104dc7a4f5d7a7e3b60d4dc4a80338d7a2728540998Richard Smith
1059d232c884ea9872d6555df0fd7359699819bc1f1John McCall    // Various scalar types.
1069d232c884ea9872d6555df0fd7359699819bc1f1John McCall    case Type::Builtin:
1079d232c884ea9872d6555df0fd7359699819bc1f1John McCall    case Type::Pointer:
1089d232c884ea9872d6555df0fd7359699819bc1f1John McCall    case Type::BlockPointer:
1099d232c884ea9872d6555df0fd7359699819bc1f1John McCall    case Type::LValueReference:
1109d232c884ea9872d6555df0fd7359699819bc1f1John McCall    case Type::RValueReference:
1119d232c884ea9872d6555df0fd7359699819bc1f1John McCall    case Type::MemberPointer:
1129d232c884ea9872d6555df0fd7359699819bc1f1John McCall    case Type::Vector:
1139d232c884ea9872d6555df0fd7359699819bc1f1John McCall    case Type::ExtVector:
1149d232c884ea9872d6555df0fd7359699819bc1f1John McCall    case Type::FunctionProto:
1159d232c884ea9872d6555df0fd7359699819bc1f1John McCall    case Type::FunctionNoProto:
1169d232c884ea9872d6555df0fd7359699819bc1f1John McCall    case Type::Enum:
1179d232c884ea9872d6555df0fd7359699819bc1f1John McCall    case Type::ObjCObjectPointer:
1189d232c884ea9872d6555df0fd7359699819bc1f1John McCall      return TEK_Scalar;
1199d232c884ea9872d6555df0fd7359699819bc1f1John McCall
1209d232c884ea9872d6555df0fd7359699819bc1f1John McCall    // Complexes.
1219d232c884ea9872d6555df0fd7359699819bc1f1John McCall    case Type::Complex:
1229d232c884ea9872d6555df0fd7359699819bc1f1John McCall      return TEK_Complex;
1239d232c884ea9872d6555df0fd7359699819bc1f1John McCall
1249d232c884ea9872d6555df0fd7359699819bc1f1John McCall    // Arrays, records, and Objective-C objects.
1259d232c884ea9872d6555df0fd7359699819bc1f1John McCall    case Type::ConstantArray:
1269d232c884ea9872d6555df0fd7359699819bc1f1John McCall    case Type::IncompleteArray:
1279d232c884ea9872d6555df0fd7359699819bc1f1John McCall    case Type::VariableArray:
1289d232c884ea9872d6555df0fd7359699819bc1f1John McCall    case Type::Record:
1299d232c884ea9872d6555df0fd7359699819bc1f1John McCall    case Type::ObjCObject:
1309d232c884ea9872d6555df0fd7359699819bc1f1John McCall    case Type::ObjCInterface:
1319d232c884ea9872d6555df0fd7359699819bc1f1John McCall      return TEK_Aggregate;
1329d232c884ea9872d6555df0fd7359699819bc1f1John McCall
1339d232c884ea9872d6555df0fd7359699819bc1f1John McCall    // We operate on atomic values according to their underlying type.
1349d232c884ea9872d6555df0fd7359699819bc1f1John McCall    case Type::Atomic:
1359d232c884ea9872d6555df0fd7359699819bc1f1John McCall      type = cast<AtomicType>(type)->getValueType();
1369d232c884ea9872d6555df0fd7359699819bc1f1John McCall      continue;
1379d232c884ea9872d6555df0fd7359699819bc1f1John McCall    }
1389d232c884ea9872d6555df0fd7359699819bc1f1John McCall    llvm_unreachable("unknown type kind!");
139f2aac84709c418189e476ad591848dad50291885John McCall  }
1404111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner}
141391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner
1420a0f93c90fd397a1aa9f97283c55f8ba0062bf55David Blaikievoid CodeGenFunction::EmitReturnBlock() {
1431c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // For cleanliness, we try to avoid emitting the return block for
1441c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // simple cases.
1451c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
1461c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
1471c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  if (CurBB) {
1481c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar    assert(!CurBB->getTerminator() && "Unexpected terminated block.");
1491c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
15096e18b05ea6b55aa92a1a576f29e9cee73a7e20bDaniel Dunbar    // We have a valid insert point, reuse it if it is empty or there are no
15196e18b05ea6b55aa92a1a576f29e9cee73a7e20bDaniel Dunbar    // explicit jumps to the return block.
152ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    if (CurBB->empty() || ReturnBlock.getBlock()->use_empty()) {
153ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      ReturnBlock.getBlock()->replaceAllUsesWith(CurBB);
154ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      delete ReturnBlock.getBlock();
15596e18b05ea6b55aa92a1a576f29e9cee73a7e20bDaniel Dunbar    } else
156ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      EmitBlock(ReturnBlock.getBlock());
1570a0f93c90fd397a1aa9f97283c55f8ba0062bf55David Blaikie    return;
1581c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  }
1591c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
1601c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // Otherwise, if the return block is the target of a single direct
1611c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // branch then we can just put the code in that block instead. This
1621c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // cleans up functions which started with a unified return block.
163ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  if (ReturnBlock.getBlock()->hasOneUse()) {
1641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    llvm::BranchInst *BI =
165651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      dyn_cast<llvm::BranchInst>(*ReturnBlock.getBlock()->user_begin());
166f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    if (BI && BI->isUnconditional() &&
167ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall        BI->getSuccessor(0) == ReturnBlock.getBlock()) {
168c5139a9f0c9d4f835e4ab0b51e78d2f74fb488d9David Blaikie      // Reset insertion point, including debug location, and delete the
169c5139a9f0c9d4f835e4ab0b51e78d2f74fb488d9David Blaikie      // branch.  This is really subtle and only works because the next change
170c5139a9f0c9d4f835e4ab0b51e78d2f74fb488d9David Blaikie      // in location will hit the caching in CGDebugInfo::EmitLocation and not
171c5139a9f0c9d4f835e4ab0b51e78d2f74fb488d9David Blaikie      // override this.
172acae01124151392a842bd6c37bd01b1ad56d6b4dEric Christopher      Builder.SetCurrentDebugLocation(BI->getDebugLoc());
1731c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      Builder.SetInsertPoint(BI->getParent());
1741c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      BI->eraseFromParent();
175ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      delete ReturnBlock.getBlock();
1760a0f93c90fd397a1aa9f97283c55f8ba0062bf55David Blaikie      return;
1771c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar    }
1781c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  }
1791c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
180f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // FIXME: We are at an unreachable point, there is no reason to emit the block
181f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // unless it has uses. However, we still need a place to put the debug
182f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // region.end for now.
1831c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
184ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  EmitBlock(ReturnBlock.getBlock());
185f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall}
186f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
187f1549f66a8216a78112286e3978cea2c29d6334cJohn McCallstatic void EmitIfUsed(CodeGenFunction &CGF, llvm::BasicBlock *BB) {
188f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  if (!BB) return;
189f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  if (!BB->use_empty())
190f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    return CGF.CurFn->getBasicBlockList().push_back(BB);
191f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  delete BB;
1921c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar}
1931c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
194af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbarvoid CodeGenFunction::FinishFunction(SourceLocation EndLoc) {
195391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  assert(BreakContinueStack.empty() &&
196391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner         "mismatched push/pop in break/continue stack!");
1971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
198d072e59eb6b5e32f93be25f08f212b0ec21d6c5dAdrian Prantl  bool OnlySimpleReturnStmts = NumSimpleReturnExprs > 0
1993b477597fa1184b223ad5ad9dfe1c870b8aa1ac5Adrian Prantl    && NumSimpleReturnExprs == NumReturnExprs
2003b477597fa1184b223ad5ad9dfe1c870b8aa1ac5Adrian Prantl    && ReturnBlock.getBlock()->use_empty();
2013b477597fa1184b223ad5ad9dfe1c870b8aa1ac5Adrian Prantl  // Usually the return expression is evaluated before the cleanup
2023b477597fa1184b223ad5ad9dfe1c870b8aa1ac5Adrian Prantl  // code.  If the function contains only a simple return statement,
2033b477597fa1184b223ad5ad9dfe1c870b8aa1ac5Adrian Prantl  // such as a constant, the location before the cleanup code becomes
2043b477597fa1184b223ad5ad9dfe1c870b8aa1ac5Adrian Prantl  // the last useful breakpoint in the function, because the simple
2053b477597fa1184b223ad5ad9dfe1c870b8aa1ac5Adrian Prantl  // return expression will be evaluated after the cleanup code. To be
2063b477597fa1184b223ad5ad9dfe1c870b8aa1ac5Adrian Prantl  // safe, set the debug location for cleanup code to the location of
2073b477597fa1184b223ad5ad9dfe1c870b8aa1ac5Adrian Prantl  // the return statement.  Otherwise the cleanup code should be at the
2083b477597fa1184b223ad5ad9dfe1c870b8aa1ac5Adrian Prantl  // end of the function's lexical scope.
2093b477597fa1184b223ad5ad9dfe1c870b8aa1ac5Adrian Prantl  //
2103b477597fa1184b223ad5ad9dfe1c870b8aa1ac5Adrian Prantl  // If there are multiple branches to the return block, the branch
2113b477597fa1184b223ad5ad9dfe1c870b8aa1ac5Adrian Prantl  // instructions will get the location of the return statements and
2123b477597fa1184b223ad5ad9dfe1c870b8aa1ac5Adrian Prantl  // all will be fine.
213fa6b079b1231366696f6a497c6a084c73a35c85dAdrian Prantl  if (CGDebugInfo *DI = getDebugInfo()) {
214d072e59eb6b5e32f93be25f08f212b0ec21d6c5dAdrian Prantl    if (OnlySimpleReturnStmts)
215ece7ebd2f2d03dc58fa1937a6d53e2a9f8c410f1Adrian Prantl      DI->EmitLocation(Builder, LastStopPoint);
216fa6b079b1231366696f6a497c6a084c73a35c85dAdrian Prantl    else
217fa6b079b1231366696f6a497c6a084c73a35c85dAdrian Prantl      DI->EmitLocation(Builder, EndLoc);
218fa6b079b1231366696f6a497c6a084c73a35c85dAdrian Prantl  }
2190a0f93c90fd397a1aa9f97283c55f8ba0062bf55David Blaikie
220f85e193739c953358c865005855253af4f68a497John McCall  // Pop any cleanups that might have been associated with the
221f85e193739c953358c865005855253af4f68a497John McCall  // parameters.  Do this in whatever block we're currently in; it's
222f85e193739c953358c865005855253af4f68a497John McCall  // important to do this before we enter the return block or return
223f85e193739c953358c865005855253af4f68a497John McCall  // edges will be *really* confused.
224fa6b079b1231366696f6a497c6a084c73a35c85dAdrian Prantl  bool EmitRetDbgLoc = true;
225fa6b079b1231366696f6a497c6a084c73a35c85dAdrian Prantl  if (EHStack.stable_begin() != PrologueCleanupDepth) {
2261c3db769249a633108dc320da87c48a432e2bcc1Adrian Prantl    PopCleanupBlocks(PrologueCleanupDepth);
227f85e193739c953358c865005855253af4f68a497John McCall
228fa6b079b1231366696f6a497c6a084c73a35c85dAdrian Prantl    // Make sure the line table doesn't jump back into the body for
229fa6b079b1231366696f6a497c6a084c73a35c85dAdrian Prantl    // the ret after it's been at EndLoc.
230fa6b079b1231366696f6a497c6a084c73a35c85dAdrian Prantl    EmitRetDbgLoc = false;
231fa6b079b1231366696f6a497c6a084c73a35c85dAdrian Prantl
232fa6b079b1231366696f6a497c6a084c73a35c85dAdrian Prantl    if (CGDebugInfo *DI = getDebugInfo())
233d072e59eb6b5e32f93be25f08f212b0ec21d6c5dAdrian Prantl      if (OnlySimpleReturnStmts)
234fa6b079b1231366696f6a497c6a084c73a35c85dAdrian Prantl        DI->EmitLocation(Builder, EndLoc);
235fa6b079b1231366696f6a497c6a084c73a35c85dAdrian Prantl  }
236fa6b079b1231366696f6a497c6a084c73a35c85dAdrian Prantl
2371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Emit function epilog (to return).
2380a0f93c90fd397a1aa9f97283c55f8ba0062bf55David Blaikie  EmitReturnBlock();
239f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar
240a18652fe1e8233fbf8b67484945c7f7b2bf272beDaniel Dunbar  if (ShouldInstrumentFunction())
241a18652fe1e8233fbf8b67484945c7f7b2bf272beDaniel Dunbar    EmitFunctionInstrumentation("__cyg_profile_func_exit");
2427255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner
243f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar  // Emit debug descriptor for function end.
244e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson  if (CGDebugInfo *DI = getDebugInfo()) {
2455a6fbcfd8c15a2296f94a0473a68ec09d429827fDevang Patel    DI->EmitFunctionEnd(Builder);
246f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar  }
247f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar
2484ee7dc2369c1f0257a73b2e83a7d38fdebdd9176Nick Lewycky  EmitFunctionEpilog(*CurFnInfo, EmitRetDbgLoc, EndLoc);
249cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump  EmitEndEHSpec(CurCodeDecl);
2505ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar
251f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  assert(EHStack.empty() &&
252f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall         "did not remove all scopes from cleanup stack!");
253f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
254d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // If someone did an indirect goto, emit the indirect goto block at the end of
255d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // the function.
256d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  if (IndirectBranch) {
257d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    EmitBlock(IndirectBranch->getParent());
258d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    Builder.ClearInsertionPoint();
259d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  }
2609dce4b3d9d14ba2be2aad5bd7fd8d381a0263c36Michael Ilseman
261391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  // Remove the AllocaInsertPt instruction, which is just a convenience for us.
262481769b5dc102b0256b35581e787909ad5edfab5Chris Lattner  llvm::Instruction *Ptr = AllocaInsertPt;
2636bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  AllocaInsertPt = nullptr;
264481769b5dc102b0256b35581e787909ad5edfab5Chris Lattner  Ptr->eraseFromParent();
2659dce4b3d9d14ba2be2aad5bd7fd8d381a0263c36Michael Ilseman
266d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // If someone took the address of a label but never did an indirect goto, we
267d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // made a zero entry PHI node, which is illegal, zap it now.
268d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  if (IndirectBranch) {
269d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    llvm::PHINode *PN = cast<llvm::PHINode>(IndirectBranch->getAddress());
270d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    if (PN->getNumIncomingValues() == 0) {
271d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner      PN->replaceAllUsesWith(llvm::UndefValue::get(PN->getType()));
272d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner      PN->eraseFromParent();
273d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    }
274d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  }
275f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
276777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  EmitIfUsed(*this, EHResumeBlock);
277f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  EmitIfUsed(*this, TerminateLandingPad);
278f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  EmitIfUsed(*this, TerminateHandler);
279f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  EmitIfUsed(*this, UnreachableBlock);
280744016dde06fcffd50931e94a98c850f8b12cd87John McCall
281744016dde06fcffd50931e94a98c850f8b12cd87John McCall  if (CGM.getCodeGenOpts().EmitDeclMetadata)
282744016dde06fcffd50931e94a98c850f8b12cd87John McCall    EmitDeclMetadata();
283651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
284651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  for (SmallVectorImpl<std::pair<llvm::Instruction *, llvm::Value *> >::iterator
285651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines           I = DeferredReplacements.begin(),
286651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines           E = DeferredReplacements.end();
287651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines       I != E; ++I) {
288651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    I->first->replaceAllUsesWith(I->second);
289651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    I->first->eraseFromParent();
290651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  }
291c8aa5f1f264fb230c38182adab944232bb160c2bChris Lattner}
292c8aa5f1f264fb230c38182adab944232bb160c2bChris Lattner
2937255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner/// ShouldInstrumentFunction - Return true if the current function should be
2947255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner/// instrumented with __cyg_profile_func_* calls
2957255a2d997b15beae82e627052fdb1b2474495c2Chris Lattnerbool CodeGenFunction::ShouldInstrumentFunction() {
2967255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  if (!CGM.getCodeGenOpts().InstrumentFunctions)
2977255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner    return false;
2987aa488a7fc5c3a8cd1a2b93476150e9737760713Ted Kremenek  if (!CurFuncDecl || CurFuncDecl->hasAttr<NoInstrumentFunctionAttr>())
2997255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner    return false;
3007255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  return true;
3017255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner}
3027255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner
3037255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner/// EmitFunctionInstrumentation - Emit LLVM code to call the specified
3047255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner/// instrumentation function with the current function and the call site, if
3057255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner/// function instrumentation is enabled.
3067255a2d997b15beae82e627052fdb1b2474495c2Chris Lattnervoid CodeGenFunction::EmitFunctionInstrumentation(const char *Fn) {
3078dab6571b2cab96f44d0a1d6e3edbfdb68b7ed6bChris Lattner  // void __cyg_profile_func_{enter,exit} (void *this_fn, void *call_site);
3089cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::PointerType *PointerTy = Int8PtrTy;
3099cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::Type *ProfileFuncArgs[] = { PointerTy, PointerTy };
3102acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::FunctionType *FunctionTy =
3118b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner    llvm::FunctionType::get(VoidTy, ProfileFuncArgs, false);
3127255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner
3137255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  llvm::Constant *F = CGM.CreateRuntimeFunction(FunctionTy, Fn);
3147255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  llvm::CallInst *CallSite = Builder.CreateCall(
3158dd55a3c3b28d195717c87bbc60e765951d408feBenjamin Kramer    CGM.getIntrinsic(llvm::Intrinsic::returnaddress),
31677b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner    llvm::ConstantInt::get(Int32Ty, 0),
3177255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner    "callsite");
3187255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner
319bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  llvm::Value *args[] = {
320bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall    llvm::ConstantExpr::getBitCast(CurFn, PointerTy),
321bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall    CallSite
322bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  };
323bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall
324bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  EmitNounwindRuntimeCall(F, args);
3257255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner}
3267255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner
327be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divackyvoid CodeGenFunction::EmitMCountInstrumentation() {
3288b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner  llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
329be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky
33064aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall  llvm::Constant *MCountFn =
33164aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall    CGM.CreateRuntimeFunction(FTy, getTarget().getMCountName());
332bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  EmitNounwindRuntimeCall(MCountFn);
333be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky}
334be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky
335198871cc90375246d8692680467ff6e810edac36Tanya Lattner// OpenCL v1.2 s5.6.4.6 allows the compiler to store kernel argument
336198871cc90375246d8692680467ff6e810edac36Tanya Lattner// information in the program executable. The argument information stored
337198871cc90375246d8692680467ff6e810edac36Tanya Lattner// includes the argument name, its type, the address and access qualifiers used.
338198871cc90375246d8692680467ff6e810edac36Tanya Lattnerstatic void GenOpenCLArgMetadata(const FunctionDecl *FD, llvm::Function *Fn,
339198871cc90375246d8692680467ff6e810edac36Tanya Lattner                                 CodeGenModule &CGM,llvm::LLVMContext &Context,
3401db7040604154f71c54cf1329ae384fbef196668Guy Benyei                                 SmallVector <llvm::Value*, 5> &kernelMDArgs,
3411db7040604154f71c54cf1329ae384fbef196668Guy Benyei                                 CGBuilderTy& Builder, ASTContext &ASTCtx) {
3421db7040604154f71c54cf1329ae384fbef196668Guy Benyei  // Create MDNodes that represent the kernel arg metadata.
343198871cc90375246d8692680467ff6e810edac36Tanya Lattner  // Each MDNode is a list in the form of "key", N number of values which is
344198871cc90375246d8692680467ff6e810edac36Tanya Lattner  // the same number of values as their are kernel arguments.
3459dce4b3d9d14ba2be2aad5bd7fd8d381a0263c36Michael Ilseman
3466bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  const PrintingPolicy &Policy = ASTCtx.getPrintingPolicy();
3476bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
3481db7040604154f71c54cf1329ae384fbef196668Guy Benyei  // MDNode for the kernel argument address space qualifiers.
3491db7040604154f71c54cf1329ae384fbef196668Guy Benyei  SmallVector<llvm::Value*, 8> addressQuals;
3501db7040604154f71c54cf1329ae384fbef196668Guy Benyei  addressQuals.push_back(llvm::MDString::get(Context, "kernel_arg_addr_space"));
3511db7040604154f71c54cf1329ae384fbef196668Guy Benyei
3521db7040604154f71c54cf1329ae384fbef196668Guy Benyei  // MDNode for the kernel argument access qualifiers (images only).
3531db7040604154f71c54cf1329ae384fbef196668Guy Benyei  SmallVector<llvm::Value*, 8> accessQuals;
3541db7040604154f71c54cf1329ae384fbef196668Guy Benyei  accessQuals.push_back(llvm::MDString::get(Context, "kernel_arg_access_qual"));
3551db7040604154f71c54cf1329ae384fbef196668Guy Benyei
3561db7040604154f71c54cf1329ae384fbef196668Guy Benyei  // MDNode for the kernel argument type names.
3571db7040604154f71c54cf1329ae384fbef196668Guy Benyei  SmallVector<llvm::Value*, 8> argTypeNames;
3581db7040604154f71c54cf1329ae384fbef196668Guy Benyei  argTypeNames.push_back(llvm::MDString::get(Context, "kernel_arg_type"));
3591db7040604154f71c54cf1329ae384fbef196668Guy Benyei
3601db7040604154f71c54cf1329ae384fbef196668Guy Benyei  // MDNode for the kernel argument type qualifiers.
3611db7040604154f71c54cf1329ae384fbef196668Guy Benyei  SmallVector<llvm::Value*, 8> argTypeQuals;
3621db7040604154f71c54cf1329ae384fbef196668Guy Benyei  argTypeQuals.push_back(llvm::MDString::get(Context, "kernel_arg_type_qual"));
3631db7040604154f71c54cf1329ae384fbef196668Guy Benyei
364198871cc90375246d8692680467ff6e810edac36Tanya Lattner  // MDNode for the kernel argument names.
365198871cc90375246d8692680467ff6e810edac36Tanya Lattner  SmallVector<llvm::Value*, 8> argNames;
366198871cc90375246d8692680467ff6e810edac36Tanya Lattner  argNames.push_back(llvm::MDString::get(Context, "kernel_arg_name"));
3679dce4b3d9d14ba2be2aad5bd7fd8d381a0263c36Michael Ilseman
368198871cc90375246d8692680467ff6e810edac36Tanya Lattner  for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
369198871cc90375246d8692680467ff6e810edac36Tanya Lattner    const ParmVarDecl *parm = FD->getParamDecl(i);
3701db7040604154f71c54cf1329ae384fbef196668Guy Benyei    QualType ty = parm->getType();
3711db7040604154f71c54cf1329ae384fbef196668Guy Benyei    std::string typeQuals;
3721db7040604154f71c54cf1329ae384fbef196668Guy Benyei
3731db7040604154f71c54cf1329ae384fbef196668Guy Benyei    if (ty->isPointerType()) {
3741db7040604154f71c54cf1329ae384fbef196668Guy Benyei      QualType pointeeTy = ty->getPointeeType();
3751db7040604154f71c54cf1329ae384fbef196668Guy Benyei
3761db7040604154f71c54cf1329ae384fbef196668Guy Benyei      // Get address qualifier.
3771db7040604154f71c54cf1329ae384fbef196668Guy Benyei      addressQuals.push_back(Builder.getInt32(ASTCtx.getTargetAddressSpace(
3781db7040604154f71c54cf1329ae384fbef196668Guy Benyei        pointeeTy.getAddressSpace())));
3791db7040604154f71c54cf1329ae384fbef196668Guy Benyei
3801db7040604154f71c54cf1329ae384fbef196668Guy Benyei      // Get argument type name.
3816bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      std::string typeName =
3826bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          pointeeTy.getUnqualifiedType().getAsString(Policy) + "*";
3831db7040604154f71c54cf1329ae384fbef196668Guy Benyei
3841db7040604154f71c54cf1329ae384fbef196668Guy Benyei      // Turn "unsigned type" to "utype"
3851db7040604154f71c54cf1329ae384fbef196668Guy Benyei      std::string::size_type pos = typeName.find("unsigned");
386f4492448a201c352be3d2e1e76220cf7cd499c55Benjamin Kramer      if (pos != std::string::npos)
387f4492448a201c352be3d2e1e76220cf7cd499c55Benjamin Kramer        typeName.erase(pos+1, 8);
3881db7040604154f71c54cf1329ae384fbef196668Guy Benyei
3891db7040604154f71c54cf1329ae384fbef196668Guy Benyei      argTypeNames.push_back(llvm::MDString::get(Context, typeName));
3901db7040604154f71c54cf1329ae384fbef196668Guy Benyei
3911db7040604154f71c54cf1329ae384fbef196668Guy Benyei      // Get argument type qualifiers:
3921db7040604154f71c54cf1329ae384fbef196668Guy Benyei      if (ty.isRestrictQualified())
3931db7040604154f71c54cf1329ae384fbef196668Guy Benyei        typeQuals = "restrict";
3941db7040604154f71c54cf1329ae384fbef196668Guy Benyei      if (pointeeTy.isConstQualified() ||
3951db7040604154f71c54cf1329ae384fbef196668Guy Benyei          (pointeeTy.getAddressSpace() == LangAS::opencl_constant))
396f4492448a201c352be3d2e1e76220cf7cd499c55Benjamin Kramer        typeQuals += typeQuals.empty() ? "const" : " const";
3971db7040604154f71c54cf1329ae384fbef196668Guy Benyei      if (pointeeTy.isVolatileQualified())
398f4492448a201c352be3d2e1e76220cf7cd499c55Benjamin Kramer        typeQuals += typeQuals.empty() ? "volatile" : " volatile";
3991db7040604154f71c54cf1329ae384fbef196668Guy Benyei    } else {
400651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      uint32_t AddrSpc = 0;
401651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      if (ty->isImageType())
402651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        AddrSpc =
403651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines          CGM.getContext().getTargetAddressSpace(LangAS::opencl_global);
404651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
405651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      addressQuals.push_back(Builder.getInt32(AddrSpc));
4061db7040604154f71c54cf1329ae384fbef196668Guy Benyei
4071db7040604154f71c54cf1329ae384fbef196668Guy Benyei      // Get argument type name.
4086bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      std::string typeName = ty.getUnqualifiedType().getAsString(Policy);
4091db7040604154f71c54cf1329ae384fbef196668Guy Benyei
4101db7040604154f71c54cf1329ae384fbef196668Guy Benyei      // Turn "unsigned type" to "utype"
4111db7040604154f71c54cf1329ae384fbef196668Guy Benyei      std::string::size_type pos = typeName.find("unsigned");
412f4492448a201c352be3d2e1e76220cf7cd499c55Benjamin Kramer      if (pos != std::string::npos)
413f4492448a201c352be3d2e1e76220cf7cd499c55Benjamin Kramer        typeName.erase(pos+1, 8);
4141db7040604154f71c54cf1329ae384fbef196668Guy Benyei
4151db7040604154f71c54cf1329ae384fbef196668Guy Benyei      argTypeNames.push_back(llvm::MDString::get(Context, typeName));
4161db7040604154f71c54cf1329ae384fbef196668Guy Benyei
4171db7040604154f71c54cf1329ae384fbef196668Guy Benyei      // Get argument type qualifiers:
4181db7040604154f71c54cf1329ae384fbef196668Guy Benyei      if (ty.isConstQualified())
4191db7040604154f71c54cf1329ae384fbef196668Guy Benyei        typeQuals = "const";
4201db7040604154f71c54cf1329ae384fbef196668Guy Benyei      if (ty.isVolatileQualified())
421f4492448a201c352be3d2e1e76220cf7cd499c55Benjamin Kramer        typeQuals += typeQuals.empty() ? "volatile" : " volatile";
4221db7040604154f71c54cf1329ae384fbef196668Guy Benyei    }
423651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
4241db7040604154f71c54cf1329ae384fbef196668Guy Benyei    argTypeQuals.push_back(llvm::MDString::get(Context, typeQuals));
4251db7040604154f71c54cf1329ae384fbef196668Guy Benyei
4261db7040604154f71c54cf1329ae384fbef196668Guy Benyei    // Get image access qualifier:
4271db7040604154f71c54cf1329ae384fbef196668Guy Benyei    if (ty->isImageType()) {
428651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      const OpenCLImageAccessAttr *A = parm->getAttr<OpenCLImageAccessAttr>();
429651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      if (A && A->isWriteOnly())
4301db7040604154f71c54cf1329ae384fbef196668Guy Benyei        accessQuals.push_back(llvm::MDString::get(Context, "write_only"));
4311db7040604154f71c54cf1329ae384fbef196668Guy Benyei      else
4321db7040604154f71c54cf1329ae384fbef196668Guy Benyei        accessQuals.push_back(llvm::MDString::get(Context, "read_only"));
433651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      // FIXME: what about read_write?
4341db7040604154f71c54cf1329ae384fbef196668Guy Benyei    } else
4351db7040604154f71c54cf1329ae384fbef196668Guy Benyei      accessQuals.push_back(llvm::MDString::get(Context, "none"));
4369dce4b3d9d14ba2be2aad5bd7fd8d381a0263c36Michael Ilseman
437198871cc90375246d8692680467ff6e810edac36Tanya Lattner    // Get argument name.
438198871cc90375246d8692680467ff6e810edac36Tanya Lattner    argNames.push_back(llvm::MDString::get(Context, parm->getName()));
439198871cc90375246d8692680467ff6e810edac36Tanya Lattner  }
4401db7040604154f71c54cf1329ae384fbef196668Guy Benyei
4411db7040604154f71c54cf1329ae384fbef196668Guy Benyei  kernelMDArgs.push_back(llvm::MDNode::get(Context, addressQuals));
4421db7040604154f71c54cf1329ae384fbef196668Guy Benyei  kernelMDArgs.push_back(llvm::MDNode::get(Context, accessQuals));
4431db7040604154f71c54cf1329ae384fbef196668Guy Benyei  kernelMDArgs.push_back(llvm::MDNode::get(Context, argTypeNames));
4441db7040604154f71c54cf1329ae384fbef196668Guy Benyei  kernelMDArgs.push_back(llvm::MDNode::get(Context, argTypeQuals));
445198871cc90375246d8692680467ff6e810edac36Tanya Lattner  kernelMDArgs.push_back(llvm::MDNode::get(Context, argNames));
446198871cc90375246d8692680467ff6e810edac36Tanya Lattner}
447198871cc90375246d8692680467ff6e810edac36Tanya Lattner
4489dce4b3d9d14ba2be2aad5bd7fd8d381a0263c36Michael Ilsemanvoid CodeGenFunction::EmitOpenCLKernelMetadata(const FunctionDecl *FD,
4490df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner                                               llvm::Function *Fn)
4500df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner{
4510df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner  if (!FD->hasAttr<OpenCLKernelAttr>())
4520df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner    return;
4530df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner
4540df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner  llvm::LLVMContext &Context = getLLVMContext();
4550df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner
456cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko  SmallVector <llvm::Value*, 5> kernelMDArgs;
4570df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner  kernelMDArgs.push_back(Fn);
4580df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner
459198871cc90375246d8692680467ff6e810edac36Tanya Lattner  if (CGM.getCodeGenOpts().EmitOpenCLArgMetadata)
4601db7040604154f71c54cf1329ae384fbef196668Guy Benyei    GenOpenCLArgMetadata(FD, Fn, CGM, Context, kernelMDArgs,
4611db7040604154f71c54cf1329ae384fbef196668Guy Benyei                         Builder, getContext());
4629dce4b3d9d14ba2be2aad5bd7fd8d381a0263c36Michael Ilseman
463651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (const VecTypeHintAttr *A = FD->getAttr<VecTypeHintAttr>()) {
464651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    QualType hintQTy = A->getTypeHint();
46537453b9580e293eef3bd60bd36047a93ac4515b1Joey Gouly    const ExtVectorType *hintEltQTy = hintQTy->getAs<ExtVectorType>();
46637453b9580e293eef3bd60bd36047a93ac4515b1Joey Gouly    bool isSignedInteger =
46737453b9580e293eef3bd60bd36047a93ac4515b1Joey Gouly        hintQTy->isSignedIntegerType() ||
46837453b9580e293eef3bd60bd36047a93ac4515b1Joey Gouly        (hintEltQTy && hintEltQTy->getElementType()->isSignedIntegerType());
46937453b9580e293eef3bd60bd36047a93ac4515b1Joey Gouly    llvm::Value *attrMDArgs[] = {
47037453b9580e293eef3bd60bd36047a93ac4515b1Joey Gouly      llvm::MDString::get(Context, "vec_type_hint"),
471651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      llvm::UndefValue::get(CGM.getTypes().ConvertType(A->getTypeHint())),
47237453b9580e293eef3bd60bd36047a93ac4515b1Joey Gouly      llvm::ConstantInt::get(
47337453b9580e293eef3bd60bd36047a93ac4515b1Joey Gouly          llvm::IntegerType::get(Context, 32),
47437453b9580e293eef3bd60bd36047a93ac4515b1Joey Gouly          llvm::APInt(32, (uint64_t)(isSignedInteger ? 1 : 0)))
47537453b9580e293eef3bd60bd36047a93ac4515b1Joey Gouly    };
47637453b9580e293eef3bd60bd36047a93ac4515b1Joey Gouly    kernelMDArgs.push_back(llvm::MDNode::get(Context, attrMDArgs));
47737453b9580e293eef3bd60bd36047a93ac4515b1Joey Gouly  }
47837453b9580e293eef3bd60bd36047a93ac4515b1Joey Gouly
479651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (const WorkGroupSizeHintAttr *A = FD->getAttr<WorkGroupSizeHintAttr>()) {
4808fd8ee9b718a7d6ebbcc382c1de39ed1260fe2d3Benjamin Kramer    llvm::Value *attrMDArgs[] = {
4818fd8ee9b718a7d6ebbcc382c1de39ed1260fe2d3Benjamin Kramer      llvm::MDString::get(Context, "work_group_size_hint"),
482651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      Builder.getInt32(A->getXDim()),
483651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      Builder.getInt32(A->getYDim()),
484651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      Builder.getInt32(A->getZDim())
4858fd8ee9b718a7d6ebbcc382c1de39ed1260fe2d3Benjamin Kramer    };
4860df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner    kernelMDArgs.push_back(llvm::MDNode::get(Context, attrMDArgs));
4870df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner  }
4880df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner
489651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (const ReqdWorkGroupSizeAttr *A = FD->getAttr<ReqdWorkGroupSizeAttr>()) {
4908fd8ee9b718a7d6ebbcc382c1de39ed1260fe2d3Benjamin Kramer    llvm::Value *attrMDArgs[] = {
4918fd8ee9b718a7d6ebbcc382c1de39ed1260fe2d3Benjamin Kramer      llvm::MDString::get(Context, "reqd_work_group_size"),
492651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      Builder.getInt32(A->getXDim()),
493651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      Builder.getInt32(A->getYDim()),
494651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      Builder.getInt32(A->getZDim())
4958fd8ee9b718a7d6ebbcc382c1de39ed1260fe2d3Benjamin Kramer    };
4960df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner    kernelMDArgs.push_back(llvm::MDNode::get(Context, attrMDArgs));
4970df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner  }
4980df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner
4990df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner  llvm::MDNode *kernelMDNode = llvm::MDNode::get(Context, kernelMDArgs);
5000df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner  llvm::NamedMDNode *OpenCLKernelMetadata =
5010df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner    CGM.getModule().getOrInsertNamedMetadata("opencl.kernels");
5020df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner  OpenCLKernelMetadata->addOperand(kernelMDNode);
5030df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner}
5040df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner
5056bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines/// Determine whether the function F ends with a return stmt.
5066bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesstatic bool endsWithReturn(const Decl* F) {
5076bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  const Stmt *Body = nullptr;
5086bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  if (auto *FD = dyn_cast_or_null<FunctionDecl>(F))
5096bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    Body = FD->getBody();
5106bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  else if (auto *OMD = dyn_cast_or_null<ObjCMethodDecl>(F))
5116bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    Body = OMD->getBody();
5126bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
5136bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  if (auto *CS = dyn_cast_or_null<CompoundStmt>(Body)) {
5146bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    auto LastStmt = CS->body_rbegin();
5156bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    if (LastStmt != CS->body_rend())
5166bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      return isa<ReturnStmt>(*LastStmt);
5176bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  }
5186bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  return false;
5196bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines}
5206bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
521f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCallvoid CodeGenFunction::StartFunction(GlobalDecl GD,
522f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall                                    QualType RetTy,
5237c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar                                    llvm::Function *Fn,
524d26bc76c98006609002d9930f8840490e88ac5b5John McCall                                    const CGFunctionInfo &FnInfo,
5252284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar                                    const FunctionArgList &Args,
5266bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                    SourceLocation Loc,
5279c6082fe89c61af697f017aa80937581cc2128d8Tilmann Scheller                                    SourceLocation StartLoc) {
5280ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlsson  const Decl *D = GD.getDecl();
5299dce4b3d9d14ba2be2aad5bd7fd8d381a0263c36Michael Ilseman
5304cc1a4703363ff940b6273aeef9d96a87edeb04bAnders Carlsson  DidCallStackSave = false;
531f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall  CurCodeDecl = D;
5326bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  CurFuncDecl = (D ? D->getNonClosureContext() : nullptr);
5337c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar  FnRetTy = RetTy;
534bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  CurFn = Fn;
535d26bc76c98006609002d9930f8840490e88ac5b5John McCall  CurFnInfo = &FnInfo;
5365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(CurFn->isDeclaration() && "Function already has body?");
537ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner
538ef8225444452a1486bd721f3285301fe84643b00Stephen Hines  if (CGM.getSanitizerBlacklist().isIn(*Fn))
5394f45bc099f2665bc6e4bcbb169aa452390dbf3feWill Dietz    SanOpts = &SanitizerOptions::Disabled;
5404f45bc099f2665bc6e4bcbb169aa452390dbf3feWill Dietz
541a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen  // Pass inline keyword to optimizer if it appears explicitly on any
542651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  // declaration. Also, in the case of -fno-inline attach NoInline
543651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  // attribute to all function that are not marked AlwaysInline.
544651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
545651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    if (!CGM.getCodeGenOpts().NoInline) {
546651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      for (auto RI : FD->redecls())
5478fbe3855db0c341964bb550e13659505efe06c43Chad Rosier        if (RI->isInlineSpecified()) {
54872390b39c545426023ec104afe8706395d732badBill Wendling          Fn->addFnAttr(llvm::Attribute::InlineHint);
5498fbe3855db0c341964bb550e13659505efe06c43Chad Rosier          break;
5508fbe3855db0c341964bb550e13659505efe06c43Chad Rosier        }
551651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    } else if (!FD->hasAttr<AlwaysInlineAttr>())
552651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      Fn->addFnAttr(llvm::Attribute::NoInline);
553651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  }
554a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen
5557edf9e38b91917b661277601c0e448eef0eb2b56Richard Smith  if (getLangOpts().OpenCL) {
556f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne    // Add metadata for a kernel function.
557f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne    if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
5580df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner      EmitOpenCLKernelMetadata(FD, Fn);
559f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne  }
560f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne
561b914e87377fd4d7642f544000a79f8648c6f06c9Peter Collingbourne  // If we are checking function types, emit a function type signature as
562b914e87377fd4d7642f544000a79f8648c6f06c9Peter Collingbourne  // prefix data.
563b914e87377fd4d7642f544000a79f8648c6f06c9Peter Collingbourne  if (getLangOpts().CPlusPlus && SanOpts->Function) {
564b914e87377fd4d7642f544000a79f8648c6f06c9Peter Collingbourne    if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
565b914e87377fd4d7642f544000a79f8648c6f06c9Peter Collingbourne      if (llvm::Constant *PrefixSig =
566b914e87377fd4d7642f544000a79f8648c6f06c9Peter Collingbourne              CGM.getTargetCodeGenInfo().getUBSanFunctionSignature(CGM)) {
567b914e87377fd4d7642f544000a79f8648c6f06c9Peter Collingbourne        llvm::Constant *FTRTTIConst =
568b914e87377fd4d7642f544000a79f8648c6f06c9Peter Collingbourne            CGM.GetAddrOfRTTIDescriptor(FD->getType(), /*ForEH=*/true);
569b914e87377fd4d7642f544000a79f8648c6f06c9Peter Collingbourne        llvm::Constant *PrefixStructElems[] = { PrefixSig, FTRTTIConst };
570b914e87377fd4d7642f544000a79f8648c6f06c9Peter Collingbourne        llvm::Constant *PrefixStructConst =
571b914e87377fd4d7642f544000a79f8648c6f06c9Peter Collingbourne            llvm::ConstantStruct::getAnon(PrefixStructElems, /*Packed=*/true);
572b914e87377fd4d7642f544000a79f8648c6f06c9Peter Collingbourne        Fn->setPrefixData(PrefixStructConst);
573b914e87377fd4d7642f544000a79f8648c6f06c9Peter Collingbourne      }
574b914e87377fd4d7642f544000a79f8648c6f06c9Peter Collingbourne    }
575b914e87377fd4d7642f544000a79f8648c6f06c9Peter Collingbourne  }
576b914e87377fd4d7642f544000a79f8648c6f06c9Peter Collingbourne
57755e874299f2ad827646a4ca9ea38c402aaeb38c9Daniel Dunbar  llvm::BasicBlock *EntryBB = createBasicBlock("entry", CurFn);
5785ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar
5795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Create a marker to make it easy to insert allocas into the entryblock
58055352a2d616cf9fbb621d10faf8b960b4b268bd8Chris Lattner  // later.  Don't create this with the builder, because we don't want it
58155352a2d616cf9fbb621d10faf8b960b4b268bd8Chris Lattner  // folded.
58277b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner  llvm::Value *Undef = llvm::UndefValue::get(Int32Ty);
58377b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner  AllocaInsertPt = new llvm::BitCastInst(Undef, Int32Ty, "", EntryBB);
584f1466848dce9c4c75d96a6cabdc8db560e26aac8Chris Lattner  if (Builder.isNamePreserving())
585f1466848dce9c4c75d96a6cabdc8db560e26aac8Chris Lattner    AllocaInsertPt->setName("allocapt");
5861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
587f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  ReturnBlock = getJumpDestInCurrentScope("return");
5881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
58955352a2d616cf9fbb621d10faf8b960b4b268bd8Chris Lattner  Builder.SetInsertPoint(EntryBB);
5901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
591af99417156c652a6f04dff643925036dc3241d60Sanjiv Gupta  // Emit subprogram debug descriptor.
592e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson  if (CGDebugInfo *DI = getDebugInfo()) {
593bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose    SmallVector<QualType, 16> ArgTypes;
59406253664315307d34ab57b892b6a0c6c5b3153bbEric Christopher    for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
59506253664315307d34ab57b892b6a0c6c5b3153bbEric Christopher	 i != e; ++i) {
596bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose      ArgTypes.push_back((*i)->getType());
59706253664315307d34ab57b892b6a0c6c5b3153bbEric Christopher    }
59806253664315307d34ab57b892b6a0c6c5b3153bbEric Christopher
599e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    QualType FnType =
600bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose      getContext().getFunctionType(RetTy, ArgTypes,
601e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall                                   FunctionProtoType::ExtProtoInfo());
6026bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    DI->EmitFunctionStart(GD, Loc, StartLoc, FnType, CurFn, Builder);
603af99417156c652a6f04dff643925036dc3241d60Sanjiv Gupta  }
604af99417156c652a6f04dff643925036dc3241d60Sanjiv Gupta
605a18652fe1e8233fbf8b67484945c7f7b2bf272beDaniel Dunbar  if (ShouldInstrumentFunction())
606a18652fe1e8233fbf8b67484945c7f7b2bf272beDaniel Dunbar    EmitFunctionInstrumentation("__cyg_profile_func_enter");
6077255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner
608be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky  if (CGM.getCodeGenOpts().InstrumentForProfiling)
609be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky    EmitMCountInstrumentation();
610be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky
611b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman  if (RetTy->isVoidType()) {
612b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman    // Void type; nothing to return.
6136bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    ReturnValue = nullptr;
6146bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
6156bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    // Count the implicit return.
6166bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    if (!endsWithReturn(D))
6176bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      ++NumReturnExprs;
618b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman  } else if (CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect &&
6199d232c884ea9872d6555df0fd7359699819bc1f1John McCall             !hasScalarEvaluationKind(CurFnInfo->getReturnType())) {
620b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman    // Indirect aggregate return; emit returned value directly into sret slot.
621647a1ec397fa13af176d07d9f5d071560a94c7a9Daniel Dunbar    // This reduces code size, and affects correctness in C++.
6226bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    auto AI = CurFn->arg_begin();
6236bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    if (CurFnInfo->getReturnInfo().isSRetAfterThis())
6246bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      ++AI;
6256bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    ReturnValue = AI;
626651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  } else if (CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::InAlloca &&
627651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines             !hasScalarEvaluationKind(CurFnInfo->getReturnType())) {
628651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    // Load the sret pointer from the argument struct and return into that.
629651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    unsigned Idx = CurFnInfo->getReturnInfo().getInAllocaFieldIndex();
630651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    llvm::Function::arg_iterator EI = CurFn->arg_end();
631651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    --EI;
632651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    llvm::Value *Addr = Builder.CreateStructGEP(EI, Idx);
633651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    ReturnValue = Builder.CreateLoad(Addr, "agg.result");
634b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman  } else {
635647a1ec397fa13af176d07d9f5d071560a94c7a9Daniel Dunbar    ReturnValue = CreateIRTemp(RetTy, "retval");
636f85e193739c953358c865005855253af4f68a497John McCall
637f85e193739c953358c865005855253af4f68a497John McCall    // Tell the epilog emitter to autorelease the result.  We do this
638f85e193739c953358c865005855253af4f68a497John McCall    // now so that various specialized functions can suppress it
639f85e193739c953358c865005855253af4f68a497John McCall    // during their IR-generation.
6404e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (getLangOpts().ObjCAutoRefCount &&
641f85e193739c953358c865005855253af4f68a497John McCall        !CurFnInfo->isReturnsRetained() &&
642f85e193739c953358c865005855253af4f68a497John McCall        RetTy->isObjCRetainableType())
643f85e193739c953358c865005855253af4f68a497John McCall      AutoreleaseResult = true;
644b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman  }
645b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman
646cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump  EmitStartEHSpec(CurCodeDecl);
647f85e193739c953358c865005855253af4f68a497John McCall
648f85e193739c953358c865005855253af4f68a497John McCall  PrologueCleanupDepth = EHStack.stable_begin();
64988b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar  EmitFunctionProlog(*CurFnInfo, CurFn, Args);
6501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
651cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman  if (D && isa<CXXMethodDecl>(D) && cast<CXXMethodDecl>(D)->isInstance()) {
6524c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    CGM.getCXXABI().EmitInstanceFunctionProlog(*this);
653cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman    const CXXMethodDecl *MD = cast<CXXMethodDecl>(D);
654cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman    if (MD->getParent()->isLambda() &&
655cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman        MD->getOverloadedOperator() == OO_Call) {
656cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman      // We're in a lambda; figure out the captures.
657cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman      MD->getParent()->getCaptureFields(LambdaCaptureFields,
658cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman                                        LambdaThisCaptureField);
659cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman      if (LambdaThisCaptureField) {
660cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman        // If this lambda captures this, load it.
661f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall        LValue ThisLValue = EmitLValueForLambdaField(LambdaThisCaptureField);
6624ee7dc2369c1f0257a73b2e83a7d38fdebdd9176Nick Lewycky        CXXThisValue = EmitLoadOfLValue(ThisLValue,
6634ee7dc2369c1f0257a73b2e83a7d38fdebdd9176Nick Lewycky                                        SourceLocation()).getScalarVal();
664cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman      }
665cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman    } else {
666cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman      // Not in a lambda; just use 'this' from the method.
667cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman      // FIXME: Should we generate a new load for each use of 'this'?  The
668cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman      // fast register allocator would be happier...
669cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman      CXXThisValue = CXXABIThisValue;
670cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman    }
671cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman  }
6722504941793b549323f9d29c62507cf21d865fadeJohn McCall
673751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson  // If any of the arguments have a variably modified type, make sure to
674751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson  // emit the type size.
675751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson  for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
676751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson       i != e; ++i) {
677f5bf912767ba19832bba7694fc679a5ec3c055ffEli Friedman    const VarDecl *VD = *i;
678f5bf912767ba19832bba7694fc679a5ec3c055ffEli Friedman
679f5bf912767ba19832bba7694fc679a5ec3c055ffEli Friedman    // Dig out the type as written from ParmVarDecls; it's unclear whether
680f5bf912767ba19832bba7694fc679a5ec3c055ffEli Friedman    // the standard (C99 6.9.1p10) requires this, but we're following the
681f5bf912767ba19832bba7694fc679a5ec3c055ffEli Friedman    // precedent set by gcc.
682f5bf912767ba19832bba7694fc679a5ec3c055ffEli Friedman    QualType Ty;
683f5bf912767ba19832bba7694fc679a5ec3c055ffEli Friedman    if (const ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(VD))
684f5bf912767ba19832bba7694fc679a5ec3c055ffEli Friedman      Ty = PVD->getOriginalType();
685f5bf912767ba19832bba7694fc679a5ec3c055ffEli Friedman    else
686f5bf912767ba19832bba7694fc679a5ec3c055ffEli Friedman      Ty = VD->getType();
687751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson
688751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson    if (Ty->isVariablyModifiedType())
689bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      EmitVariablyModifiedType(Ty);
690751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson  }
69173fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher  // Emit a location at the end of the prologue.
69273fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher  if (CGDebugInfo *DI = getDebugInfo())
69373fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher    DI->EmitLocation(Builder, StartLoc);
6947c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar}
695eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman
6963cebc73895daccea85984d8881b5b45c8f8df9c6Richard Smithvoid CodeGenFunction::EmitFunctionBody(FunctionArgList &Args,
6973cebc73895daccea85984d8881b5b45c8f8df9c6Richard Smith                                       const Stmt *Body) {
698651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  RegionCounter Cnt = getPGORegionCounter(Body);
699651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  Cnt.beginRegion(Builder);
7003cebc73895daccea85984d8881b5b45c8f8df9c6Richard Smith  if (const CompoundStmt *S = dyn_cast<CompoundStmt>(Body))
701a6504853d297c30cfa271f4710af5a3d5db59449David Blaikie    EmitCompoundStmtWithoutScope(*S);
702a6504853d297c30cfa271f4710af5a3d5db59449David Blaikie  else
7033cebc73895daccea85984d8881b5b45c8f8df9c6Richard Smith    EmitStmt(Body);
704a355e07454463b19829ac92ffd115a097faff0e0John McCall}
705a355e07454463b19829ac92ffd115a097faff0e0John McCall
706651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines/// When instrumenting to collect profile data, the counts for some blocks
707651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines/// such as switch cases need to not include the fall-through counts, so
708651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines/// emit a branch around the instrumentation code. When not instrumenting,
709651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines/// this just calls EmitBlock().
710651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesvoid CodeGenFunction::EmitBlockWithFallThrough(llvm::BasicBlock *BB,
711651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                               RegionCounter &Cnt) {
7126bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  llvm::BasicBlock *SkipCountBB = nullptr;
713651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (HaveInsertPoint() && CGM.getCodeGenOpts().ProfileInstrGenerate) {
714651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    // When instrumenting for profiling, the fallthrough to certain
715651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    // statements needs to skip over the instrumentation code so that we
716651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    // get an accurate count.
717651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    SkipCountBB = createBasicBlock("skipcount");
718651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    EmitBranch(SkipCountBB);
719651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  }
720651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  EmitBlock(BB);
721651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  Cnt.beginRegion(Builder, /*AddIncomingFallThrough=*/true);
722651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (SkipCountBB)
723651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    EmitBlock(SkipCountBB);
724651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines}
725651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
72639dad53772c42eb36ebec1c81c56ba99d038fb94John McCall/// Tries to mark the given function nounwind based on the
72739dad53772c42eb36ebec1c81c56ba99d038fb94John McCall/// non-existence of any throwing calls within it.  We believe this is
72839dad53772c42eb36ebec1c81c56ba99d038fb94John McCall/// lightweight enough to do at -O0.
72939dad53772c42eb36ebec1c81c56ba99d038fb94John McCallstatic void TryMarkNoThrow(llvm::Function *F) {
730b3a29f132794f67108bccc9c7cc3795365e8a965John McCall  // LLVM treats 'nounwind' on a function as part of the type, so we
731b3a29f132794f67108bccc9c7cc3795365e8a965John McCall  // can't do this on functions that can be overwritten.
732b3a29f132794f67108bccc9c7cc3795365e8a965John McCall  if (F->mayBeOverridden()) return;
733b3a29f132794f67108bccc9c7cc3795365e8a965John McCall
73439dad53772c42eb36ebec1c81c56ba99d038fb94John McCall  for (llvm::Function::iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI)
73539dad53772c42eb36ebec1c81c56ba99d038fb94John McCall    for (llvm::BasicBlock::iterator
73639dad53772c42eb36ebec1c81c56ba99d038fb94John McCall           BI = FI->begin(), BE = FI->end(); BI != BE; ++BI)
737285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling      if (llvm::CallInst *Call = dyn_cast<llvm::CallInst>(&*BI)) {
73839dad53772c42eb36ebec1c81c56ba99d038fb94John McCall        if (!Call->doesNotThrow())
73939dad53772c42eb36ebec1c81c56ba99d038fb94John McCall          return;
740285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling      } else if (isa<llvm::ResumeInst>(&*BI)) {
741285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling        return;
742285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling      }
743fac631052809b59c1b66f687c08a743de7fb50e9Bill Wendling  F->setDoesNotThrow();
74439dad53772c42eb36ebec1c81c56ba99d038fb94John McCall}
74539dad53772c42eb36ebec1c81c56ba99d038fb94John McCall
7463cebc73895daccea85984d8881b5b45c8f8df9c6Richard Smithstatic void EmitSizedDeallocationFunction(CodeGenFunction &CGF,
7473cebc73895daccea85984d8881b5b45c8f8df9c6Richard Smith                                          const FunctionDecl *UnsizedDealloc) {
7483cebc73895daccea85984d8881b5b45c8f8df9c6Richard Smith  // This is a weak discardable definition of the sized deallocation function.
7493cebc73895daccea85984d8881b5b45c8f8df9c6Richard Smith  CGF.CurFn->setLinkage(llvm::Function::LinkOnceAnyLinkage);
7503cebc73895daccea85984d8881b5b45c8f8df9c6Richard Smith
7513cebc73895daccea85984d8881b5b45c8f8df9c6Richard Smith  // Call the unsized deallocation function and forward the first argument
7523cebc73895daccea85984d8881b5b45c8f8df9c6Richard Smith  // unchanged.
7533cebc73895daccea85984d8881b5b45c8f8df9c6Richard Smith  llvm::Constant *Unsized = CGF.CGM.GetAddrOfFunction(UnsizedDealloc);
7543cebc73895daccea85984d8881b5b45c8f8df9c6Richard Smith  CGF.Builder.CreateCall(Unsized, &*CGF.CurFn->arg_begin());
7553cebc73895daccea85984d8881b5b45c8f8df9c6Richard Smith}
7563cebc73895daccea85984d8881b5b45c8f8df9c6Richard Smith
757d26bc76c98006609002d9930f8840490e88ac5b5John McCallvoid CodeGenFunction::GenerateCode(GlobalDecl GD, llvm::Function *Fn,
758d26bc76c98006609002d9930f8840490e88ac5b5John McCall                                   const CGFunctionInfo &FnInfo) {
7590ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlsson  const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
7609dce4b3d9d14ba2be2aad5bd7fd8d381a0263c36Michael Ilseman
761e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson  // Check if we should generate debug info for this function.
762c3030bc285d90a139fb74629daadef5617283203David Blaikie  if (FD->hasAttr<NoDebugAttr>())
7636bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    DebugInfo = nullptr; // disable debug info indefinitely for this function
7641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7657c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar  FunctionArgList Args;
766651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  QualType ResTy = FD->getReturnType();
7671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7686a1e0eb557d47e85185e09bdf8721f53f4bf9c9cMike Stump  CurGD = GD;
769651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD);
770651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (MD && MD->isInstance()) {
7713b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin    if (CGM.getCXXABI().HasThisReturn(GD))
7723b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin      ResTy = MD->getThisType(getContext());
773651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    CGM.getCXXABI().buildThisParam(*this, Args);
7743b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin  }
7751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7766e94f6c0f124c9a88b3dae0eea5e6b27957df183Chad Rosier  for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i)
7776e94f6c0f124c9a88b3dae0eea5e6b27957df183Chad Rosier    Args.push_back(FD->getParamDecl(i));
778af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
779651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (MD && (isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD)))
780651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    CGM.getCXXABI().addImplicitStructorParams(*this, ResTy, Args);
781651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
782a355e07454463b19829ac92ffd115a097faff0e0John McCall  SourceRange BodyRange;
783a355e07454463b19829ac92ffd115a097faff0e0John McCall  if (Stmt *Body = FD->getBody()) BodyRange = Body->getSourceRange();
7841c3db769249a633108dc320da87c48a432e2bcc1Adrian Prantl  CurEHLocation = BodyRange.getEnd();
7854365bba95be15342575b4f030c6583a770a5da3dAnders Carlsson
7866bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  // Use the location of the start of the function to determine where
7876bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  // the function definition is located. By default use the location
7886bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  // of the declaration as the location for the subprogram. A function
7896bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  // may lack a declaration in the source code if it is created by code
7906bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  // gen. (examples: _GLOBAL__I_a, __cxx_global_array_dtor, thunk).
791ef8225444452a1486bd721f3285301fe84643b00Stephen Hines  SourceLocation Loc = FD->getLocation();
792ef8225444452a1486bd721f3285301fe84643b00Stephen Hines
793ef8225444452a1486bd721f3285301fe84643b00Stephen Hines  // If this is a function specialization then use the pattern body
794ef8225444452a1486bd721f3285301fe84643b00Stephen Hines  // as the location for the function.
795ef8225444452a1486bd721f3285301fe84643b00Stephen Hines  if (const FunctionDecl *SpecDecl = FD->getTemplateInstantiationPattern())
796ef8225444452a1486bd721f3285301fe84643b00Stephen Hines    if (SpecDecl->hasBody(SpecDecl))
797ef8225444452a1486bd721f3285301fe84643b00Stephen Hines      Loc = SpecDecl->getLocation();
7986bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
799a355e07454463b19829ac92ffd115a097faff0e0John McCall  // Emit the standard function prologue.
8006bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  StartFunction(GD, ResTy, Fn, FnInfo, Args, Loc, BodyRange.getBegin());
8011851a12605bc6f1ea70d11974a315340ebaab6ebAnders Carlsson
802a355e07454463b19829ac92ffd115a097faff0e0John McCall  // Generate the body of the function.
803651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  PGO.assignRegionCounters(GD.getDecl(), CurFn);
8049fc6a7774643a810c8501dae2323e863fefb623eJohn McCall  if (isa<CXXDestructorDecl>(FD))
8059fc6a7774643a810c8501dae2323e863fefb623eJohn McCall    EmitDestructorBody(Args);
8069fc6a7774643a810c8501dae2323e863fefb623eJohn McCall  else if (isa<CXXConstructorDecl>(FD))
8079fc6a7774643a810c8501dae2323e863fefb623eJohn McCall    EmitConstructorBody(Args);
8087edf9e38b91917b661277601c0e448eef0eb2b56Richard Smith  else if (getLangOpts().CUDA &&
809a4ae2294b6ebfb2554aacb6a6a0682fb5ed1f276Peter Collingbourne           !CGM.getCodeGenOpts().CUDAIsDevice &&
810a4ae2294b6ebfb2554aacb6a6a0682fb5ed1f276Peter Collingbourne           FD->hasAttr<CUDAGlobalAttr>())
811a4ae2294b6ebfb2554aacb6a6a0682fb5ed1f276Peter Collingbourne    CGM.getCUDARuntime().EmitDeviceStubBody(*this, Args);
812bd89f8c2caa9550e41daa1aa9bf30f0f1e0dfaf7Eli Friedman  else if (isa<CXXConversionDecl>(FD) &&
81327dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor           cast<CXXConversionDecl>(FD)->isLambdaToBlockPointerConversion()) {
81427dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor    // The lambda conversion to block pointer is special; the semantics can't be
81527dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor    // expressed in the AST, so IRGen needs to special-case it.
81627dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor    EmitLambdaToBlockPointerBody(Args);
81727dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor  } else if (isa<CXXMethodDecl>(FD) &&
81827dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor             cast<CXXMethodDecl>(FD)->isLambdaStaticInvoker()) {
819fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali    // The lambda static invoker function is special, because it forwards or
82027dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor    // clones the body of the function call operator (but is actually static).
82127dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor    EmitLambdaStaticInvokeFunction(cast<CXXMethodDecl>(FD));
82256c00c4868831c9a137ca7b0e16d063cf986d110Lang Hames  } else if (FD->isDefaulted() && isa<CXXMethodDecl>(FD) &&
8237fda20777785ae0076aa9f1c49ff6639cb7299efNick Lewycky             (cast<CXXMethodDecl>(FD)->isCopyAssignmentOperator() ||
8247fda20777785ae0076aa9f1c49ff6639cb7299efNick Lewycky              cast<CXXMethodDecl>(FD)->isMoveAssignmentOperator())) {
82556c00c4868831c9a137ca7b0e16d063cf986d110Lang Hames    // Implicit copy-assignment gets the same special treatment as implicit
82656c00c4868831c9a137ca7b0e16d063cf986d110Lang Hames    // copy-constructors.
82756c00c4868831c9a137ca7b0e16d063cf986d110Lang Hames    emitImplicitAssignmentOperatorBody(Args);
8283cebc73895daccea85984d8881b5b45c8f8df9c6Richard Smith  } else if (Stmt *Body = FD->getBody()) {
8293cebc73895daccea85984d8881b5b45c8f8df9c6Richard Smith    EmitFunctionBody(Args, Body);
8303cebc73895daccea85984d8881b5b45c8f8df9c6Richard Smith  } else if (FunctionDecl *UnsizedDealloc =
8313cebc73895daccea85984d8881b5b45c8f8df9c6Richard Smith                 FD->getCorrespondingUnsizedGlobalDeallocationFunction()) {
8323cebc73895daccea85984d8881b5b45c8f8df9c6Richard Smith    // Global sized deallocation functions get an implicit weak definition if
8333cebc73895daccea85984d8881b5b45c8f8df9c6Richard Smith    // they don't have an explicit definition.
8343cebc73895daccea85984d8881b5b45c8f8df9c6Richard Smith    EmitSizedDeallocationFunction(*this, UnsizedDealloc);
8353cebc73895daccea85984d8881b5b45c8f8df9c6Richard Smith  } else
8363cebc73895daccea85984d8881b5b45c8f8df9c6Richard Smith    llvm_unreachable("no definition for emitted function");
837c33e4ba20304e222692e77f2c6ad26a5d8d32f83Anders Carlsson
83836ef0d54cfddf31cd48816e78ab4db73b31a6c1dRichard Smith  // C++11 [stmt.return]p2:
83936ef0d54cfddf31cd48816e78ab4db73b31a6c1dRichard Smith  //   Flowing off the end of a function [...] results in undefined behavior in
84036ef0d54cfddf31cd48816e78ab4db73b31a6c1dRichard Smith  //   a value-returning function.
84136ef0d54cfddf31cd48816e78ab4db73b31a6c1dRichard Smith  // C11 6.9.1p12:
84236ef0d54cfddf31cd48816e78ab4db73b31a6c1dRichard Smith  //   If the '}' that terminates a function is reached, and the value of the
84336ef0d54cfddf31cd48816e78ab4db73b31a6c1dRichard Smith  //   function call is used by the caller, the behavior is undefined.
8447edf9e38b91917b661277601c0e448eef0eb2b56Richard Smith  if (getLangOpts().CPlusPlus && !FD->hasImplicitReturnZero() &&
845651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      !FD->getReturnType()->isVoidType() && Builder.GetInsertBlock()) {
8464f45bc099f2665bc6e4bcbb169aa452390dbf3feWill Dietz    if (SanOpts->Return)
8474def70d3040e73707c738f7c366737a986135edfRichard Smith      EmitCheck(Builder.getFalse(), "missing_return",
8484def70d3040e73707c738f7c366737a986135edfRichard Smith                EmitCheckSourceLocation(FD->getLocation()),
849cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko                ArrayRef<llvm::Value *>(), CRK_Unrecoverable);
850802cd5b1974bab2038b9d3c969d9beb0ab34f435Richard Smith    else if (CGM.getCodeGenOpts().OptimizationLevel == 0)
851802cd5b1974bab2038b9d3c969d9beb0ab34f435Richard Smith      Builder.CreateCall(CGM.getIntrinsic(llvm::Intrinsic::trap));
85236ef0d54cfddf31cd48816e78ab4db73b31a6c1dRichard Smith    Builder.CreateUnreachable();
85336ef0d54cfddf31cd48816e78ab4db73b31a6c1dRichard Smith    Builder.ClearInsertionPoint();
85436ef0d54cfddf31cd48816e78ab4db73b31a6c1dRichard Smith  }
85536ef0d54cfddf31cd48816e78ab4db73b31a6c1dRichard Smith
856a355e07454463b19829ac92ffd115a097faff0e0John McCall  // Emit the standard function epilogue.
857a355e07454463b19829ac92ffd115a097faff0e0John McCall  FinishFunction(BodyRange.getEnd());
85839dad53772c42eb36ebec1c81c56ba99d038fb94John McCall
85939dad53772c42eb36ebec1c81c56ba99d038fb94John McCall  // If we haven't marked the function nothrow through other means, do
86039dad53772c42eb36ebec1c81c56ba99d038fb94John McCall  // a quick pass now to see if we can.
86139dad53772c42eb36ebec1c81c56ba99d038fb94John McCall  if (!CurFn->doesNotThrow())
86239dad53772c42eb36ebec1c81c56ba99d038fb94John McCall    TryMarkNoThrow(CurFn);
863651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
864651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  PGO.emitInstrumentationData();
865651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  PGO.destroyRegionCounters();
8665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
8675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8680946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner/// ContainsLabel - Return true if the statement contains a label in it.  If
8690946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner/// this statement is not executed normally, it not containing a label means
8700946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner/// that we can just remove the code.
8710946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattnerbool CodeGenFunction::ContainsLabel(const Stmt *S, bool IgnoreCaseStmts) {
8720946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // Null statement, not a label!
8736bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  if (!S) return false;
8741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8750946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // If this is a label, we have to emit the code, consider something like:
8760946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // if (0) {  ...  foo:  bar(); }  goto foo;
877ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  //
878ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  // TODO: If anyone cared, we could track __label__'s, since we know that you
879ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  // can't jump to one from outside their declared region.
8800946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  if (isa<LabelStmt>(S))
8810946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner    return true;
8829dce4b3d9d14ba2be2aad5bd7fd8d381a0263c36Michael Ilseman
8830946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // If this is a case/default statement, and we haven't seen a switch, we have
8840946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // to emit the code.
8850946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  if (isa<SwitchCase>(S) && !IgnoreCaseStmts)
8860946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner    return true;
8871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8880946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // If this is a switch statement, we want to ignore cases below it.
8890946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  if (isa<SwitchStmt>(S))
8900946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner    IgnoreCaseStmts = true;
8911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8920946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // Scan subexpressions for verboten labels.
8937502c1d3ce8bb97bcc4f7bebef507040bd93b26fJohn McCall  for (Stmt::const_child_range I = S->children(); I; ++I)
8940946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner    if (ContainsLabel(*I, IgnoreCaseStmts))
8950946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner      return true;
8961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8970946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  return false;
8980946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner}
8990946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner
900ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner/// containsBreak - Return true if the statement contains a break out of it.
901ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner/// If the statement (recursively) contains a switch or loop with a break
902ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner/// inside of it, this is fine.
903ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattnerbool CodeGenFunction::containsBreak(const Stmt *S) {
904ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  // Null statement, not a label!
9056bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  if (!S) return false;
906ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner
907ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  // If this is a switch or loop that defines its own break scope, then we can
908ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  // include it and anything inside of it.
909ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || isa<DoStmt>(S) ||
910ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner      isa<ForStmt>(S))
9112bef7f5499541e3b68f114cc4d7d197e9a902fe7Chris Lattner    return false;
9129dce4b3d9d14ba2be2aad5bd7fd8d381a0263c36Michael Ilseman
9132bef7f5499541e3b68f114cc4d7d197e9a902fe7Chris Lattner  if (isa<BreakStmt>(S))
914ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner    return true;
9159dce4b3d9d14ba2be2aad5bd7fd8d381a0263c36Michael Ilseman
916ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  // Scan subexpressions for verboten breaks.
917ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  for (Stmt::const_child_range I = S->children(); I; ++I)
918ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner    if (containsBreak(*I))
919ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner      return true;
9209dce4b3d9d14ba2be2aad5bd7fd8d381a0263c36Michael Ilseman
921ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  return false;
922ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner}
923ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner
92431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
925c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner/// ConstantFoldsToSimpleInteger - If the specified expression does not fold
926c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner/// to a constant, or if it does but contains a label, return false.  If it
927c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner/// constant folds return true and set the boolean result in Result.
928c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattnerbool CodeGenFunction::ConstantFoldsToSimpleInteger(const Expr *Cond,
929c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner                                                   bool &ResultBool) {
930e1ecdc168175719d74e112bcacd4aae5e12d4631Richard Trieu  llvm::APSInt ResultInt;
931ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  if (!ConstantFoldsToSimpleInteger(Cond, ResultInt))
932ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner    return false;
9339dce4b3d9d14ba2be2aad5bd7fd8d381a0263c36Michael Ilseman
934ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  ResultBool = ResultInt.getBoolValue();
935ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  return true;
936ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner}
937ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner
938ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner/// ConstantFoldsToSimpleInteger - If the specified expression does not fold
939ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner/// to a constant, or if it does but contains a label, return false.  If it
940ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner/// constant folds return true and set the folded value.
941ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattnerbool CodeGenFunction::
942e1ecdc168175719d74e112bcacd4aae5e12d4631Richard TrieuConstantFoldsToSimpleInteger(const Expr *Cond, llvm::APSInt &ResultInt) {
94336bc14c3a1cf63ee306df5687ac8e85f924f8639Daniel Dunbar  // FIXME: Rename and handle conversion of other evaluatable things
94436bc14c3a1cf63ee306df5687ac8e85f924f8639Daniel Dunbar  // to bool.
94580d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  llvm::APSInt Int;
94680d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  if (!Cond->EvaluateAsInt(Int, getContext()))
947c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner    return false;  // Not foldable, not integer or not fully evaluatable.
94880d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith
94931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  if (CodeGenFunction::ContainsLabel(Cond))
950c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner    return false;  // Contains a label.
95180d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith
95280d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  ResultInt = Int;
953c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner  return true;
95431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner}
95531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
95631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
957ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner
95831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an if
95931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// statement) to the specified blocks.  Based on the condition, this might try
96031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// to simplify the codegen of the conditional based on the branch.
96131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner///
96231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattnervoid CodeGenFunction::EmitBranchOnBoolExpr(const Expr *Cond,
96331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner                                           llvm::BasicBlock *TrueBlock,
964651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                           llvm::BasicBlock *FalseBlock,
965651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                           uint64_t TrueCount) {
966f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  Cond = Cond->IgnoreParens();
9671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
96831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  if (const BinaryOperator *CondBOp = dyn_cast<BinaryOperator>(Cond)) {
969651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
97031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner    // Handle X && Y in a condition.
9712de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    if (CondBOp->getOpcode() == BO_LAnd) {
972651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      RegionCounter Cnt = getPGORegionCounter(CondBOp);
973651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
97431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // If we have "1 && X", simplify the code.  "0 && X" would have constant
97531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // folded if the case was simple enough.
976e3eb83b93751544a5fab19b3824f56aeac454f82Bill Wendling      bool ConstantBool = false;
977c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner      if (ConstantFoldsToSimpleInteger(CondBOp->getLHS(), ConstantBool) &&
978c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner          ConstantBool) {
97931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        // br(1 && X) -> br(X).
980651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        Cnt.beginRegion(Builder);
981651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock,
982651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                    TrueCount);
98331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      }
9841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
98531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // If we have "X && 1", simplify the code to use an uncond branch.
98631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // "X && 0" would have been constant folded to 0.
987c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner      if (ConstantFoldsToSimpleInteger(CondBOp->getRHS(), ConstantBool) &&
988c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner          ConstantBool) {
98931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        // br(X && 1) -> br(X).
990651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock,
991651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                    TrueCount);
99231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      }
9931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
99431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // Emit the LHS as a conditional.  If the LHS conditional is false, we
99531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // want to jump to the FalseBlock.
9969615ecb44f549ae9fa2b4db6ff46bc78befbf62cDaniel Dunbar      llvm::BasicBlock *LHSTrue = createBasicBlock("land.lhs.true");
997651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      // The counter tells us how often we evaluate RHS, and all of TrueCount
998651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      // can be propagated to that branch.
999651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      uint64_t RHSCount = Cnt.getCount();
1000150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
1001150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      ConditionalEvaluation eval(*this);
1002651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      EmitBranchOnBoolExpr(CondBOp->getLHS(), LHSTrue, FalseBlock, RHSCount);
100331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBlock(LHSTrue);
10041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
100508e9e453f40aff95a59bd67db49b8f050765e1f0Anders Carlsson      // Any temporaries created here are conditional.
1006651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      Cnt.beginRegion(Builder);
1007150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      eval.begin(*this);
1008651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock, TrueCount);
1009150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      eval.end(*this);
101008e9e453f40aff95a59bd67db49b8f050765e1f0Anders Carlsson
101131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      return;
1012c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner    }
10139dce4b3d9d14ba2be2aad5bd7fd8d381a0263c36Michael Ilseman
1014c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner    if (CondBOp->getOpcode() == BO_LOr) {
1015651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      RegionCounter Cnt = getPGORegionCounter(CondBOp);
1016651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
101731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // If we have "0 || X", simplify the code.  "1 || X" would have constant
101831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // folded if the case was simple enough.
1019e3eb83b93751544a5fab19b3824f56aeac454f82Bill Wendling      bool ConstantBool = false;
1020c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner      if (ConstantFoldsToSimpleInteger(CondBOp->getLHS(), ConstantBool) &&
1021c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner          !ConstantBool) {
102231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        // br(0 || X) -> br(X).
1023651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        Cnt.beginRegion(Builder);
1024651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock,
1025651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                    TrueCount);
102631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      }
10271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
102831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // If we have "X || 0", simplify the code to use an uncond branch.
102931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // "X || 1" would have been constant folded to 1.
1030c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner      if (ConstantFoldsToSimpleInteger(CondBOp->getRHS(), ConstantBool) &&
1031c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner          !ConstantBool) {
103231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        // br(X || 0) -> br(X).
1033651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock,
1034651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                    TrueCount);
103531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      }
10361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
103731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // Emit the LHS as a conditional.  If the LHS conditional is true, we
103831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // want to jump to the TrueBlock.
10399615ecb44f549ae9fa2b4db6ff46bc78befbf62cDaniel Dunbar      llvm::BasicBlock *LHSFalse = createBasicBlock("lor.lhs.false");
1040651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      // We have the count for entry to the RHS and for the whole expression
1041651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      // being true, so we can divy up True count between the short circuit and
1042651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      // the RHS.
1043651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      uint64_t LHSCount = Cnt.getParentCount() - Cnt.getCount();
1044651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      uint64_t RHSCount = TrueCount - LHSCount;
1045150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
1046150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      ConditionalEvaluation eval(*this);
1047651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, LHSFalse, LHSCount);
104831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBlock(LHSFalse);
10491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
105008e9e453f40aff95a59bd67db49b8f050765e1f0Anders Carlsson      // Any temporaries created here are conditional.
1051651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      Cnt.beginRegion(Builder);
1052150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      eval.begin(*this);
1053651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock, RHSCount);
1054651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
1055150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      eval.end(*this);
105608e9e453f40aff95a59bd67db49b8f050765e1f0Anders Carlsson
105731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      return;
105831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner    }
1059552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner  }
10601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1061552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner  if (const UnaryOperator *CondUOp = dyn_cast<UnaryOperator>(Cond)) {
1062552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner    // br(!x, t, f) -> br(x, f, t)
1063651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    if (CondUOp->getOpcode() == UO_LNot) {
1064651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      // Negate the count.
1065651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      uint64_t FalseCount = PGO.getCurrentRegionCount() - TrueCount;
1066651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      // Negate the condition and swap the destination blocks.
1067651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      return EmitBranchOnBoolExpr(CondUOp->getSubExpr(), FalseBlock, TrueBlock,
1068651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                  FalseCount);
1069651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    }
107031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  }
10711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
107209b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar  if (const ConditionalOperator *CondOp = dyn_cast<ConditionalOperator>(Cond)) {
1073df33a35f6010fea92c786c0d8f85bfd7c73ebd3eEli Friedman    // br(c ? x : y, t, f) -> br(c, br(x, t, f), br(y, t, f))
1074df33a35f6010fea92c786c0d8f85bfd7c73ebd3eEli Friedman    llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true");
1075df33a35f6010fea92c786c0d8f85bfd7c73ebd3eEli Friedman    llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false");
107609b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar
1077651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    RegionCounter Cnt = getPGORegionCounter(CondOp);
1078df33a35f6010fea92c786c0d8f85bfd7c73ebd3eEli Friedman    ConditionalEvaluation cond(*this);
1079651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    EmitBranchOnBoolExpr(CondOp->getCond(), LHSBlock, RHSBlock, Cnt.getCount());
1080651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
1081651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    // When computing PGO branch weights, we only know the overall count for
1082651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    // the true block. This code is essentially doing tail duplication of the
1083651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    // naive code-gen, introducing new edges for which counts are not
1084651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    // available. Divide the counts proportionally between the LHS and RHS of
1085651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    // the conditional operator.
1086651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    uint64_t LHSScaledTrueCount = 0;
1087651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    if (TrueCount) {
1088651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      double LHSRatio = Cnt.getCount() / (double) Cnt.getParentCount();
1089651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      LHSScaledTrueCount = TrueCount * LHSRatio;
1090651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    }
1091150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
1092df33a35f6010fea92c786c0d8f85bfd7c73ebd3eEli Friedman    cond.begin(*this);
1093df33a35f6010fea92c786c0d8f85bfd7c73ebd3eEli Friedman    EmitBlock(LHSBlock);
1094651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    Cnt.beginRegion(Builder);
1095651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    EmitBranchOnBoolExpr(CondOp->getLHS(), TrueBlock, FalseBlock,
1096651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                         LHSScaledTrueCount);
1097df33a35f6010fea92c786c0d8f85bfd7c73ebd3eEli Friedman    cond.end(*this);
1098150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
1099df33a35f6010fea92c786c0d8f85bfd7c73ebd3eEli Friedman    cond.begin(*this);
1100df33a35f6010fea92c786c0d8f85bfd7c73ebd3eEli Friedman    EmitBlock(RHSBlock);
1101651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    EmitBranchOnBoolExpr(CondOp->getRHS(), TrueBlock, FalseBlock,
1102651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                         TrueCount - LHSScaledTrueCount);
1103df33a35f6010fea92c786c0d8f85bfd7c73ebd3eEli Friedman    cond.end(*this);
1104150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
1105df33a35f6010fea92c786c0d8f85bfd7c73ebd3eEli Friedman    return;
110609b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar  }
110709b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar
11084c71b8cded575b0cfc133c5da4502ca613982094Richard Smith  if (const CXXThrowExpr *Throw = dyn_cast<CXXThrowExpr>(Cond)) {
11094c71b8cded575b0cfc133c5da4502ca613982094Richard Smith    // Conditional operator handling can give us a throw expression as a
11104c71b8cded575b0cfc133c5da4502ca613982094Richard Smith    // condition for a case like:
11114c71b8cded575b0cfc133c5da4502ca613982094Richard Smith    //   br(c ? throw x : y, t, f) -> br(c, br(throw x, t, f), br(y, t, f)
11124c71b8cded575b0cfc133c5da4502ca613982094Richard Smith    // Fold this to:
11134c71b8cded575b0cfc133c5da4502ca613982094Richard Smith    //   br(c, throw x, br(y, t, f))
11144c71b8cded575b0cfc133c5da4502ca613982094Richard Smith    EmitCXXThrowExpr(Throw, /*KeepInsertionPoint*/false);
11154c71b8cded575b0cfc133c5da4502ca613982094Richard Smith    return;
11164c71b8cded575b0cfc133c5da4502ca613982094Richard Smith  }
11174c71b8cded575b0cfc133c5da4502ca613982094Richard Smith
1118651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  // Create branch weights based on the number of times we get here and the
1119651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  // number of times the condition should be true.
1120651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  uint64_t CurrentCount = std::max(PGO.getCurrentRegionCount(), TrueCount);
1121651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  llvm::MDNode *Weights = PGO.createBranchWeights(TrueCount,
1122651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                                  CurrentCount - TrueCount);
1123651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
112431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  // Emit the code with the fully general case.
112531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  llvm::Value *CondV = EvaluateExprAsBool(Cond);
1126651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  Builder.CreateCondBr(CondV, TrueBlock, FalseBlock, Weights);
112731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner}
112831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
1129488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar/// ErrorUnsupported - Print out an error that codegen doesn't support the
1130dc5e8268292046114ffe02e48773572a91a310f1Chris Lattner/// specified stmt yet.
11310a1c862914664029e3b6693e134cf3ccbf423844David Blaikievoid CodeGenFunction::ErrorUnsupported(const Stmt *S, const char *Type) {
11320a1c862914664029e3b6693e134cf3ccbf423844David Blaikie  CGM.ErrorUnsupported(S, Type);
1133dc5e8268292046114ffe02e48773572a91a310f1Chris Lattner}
1134dc5e8268292046114ffe02e48773572a91a310f1Chris Lattner
11357143325db76d6c3dabce82500f8cc7c93a941970John McCall/// emitNonZeroVLAInit - Emit the "zero" initialization of a
11367143325db76d6c3dabce82500f8cc7c93a941970John McCall/// variable-length array whose elements have a non-zero bit-pattern.
11377143325db76d6c3dabce82500f8cc7c93a941970John McCall///
11382ee5ba35febf830d366b65dd0dcbf8e291c41342James Dennett/// \param baseType the inner-most element type of the array
11397143325db76d6c3dabce82500f8cc7c93a941970John McCall/// \param src - a char* pointing to the bit-pattern for a single
11407143325db76d6c3dabce82500f8cc7c93a941970John McCall/// base element of the array
11417143325db76d6c3dabce82500f8cc7c93a941970John McCall/// \param sizeInChars - the total size of the VLA, in chars
11427143325db76d6c3dabce82500f8cc7c93a941970John McCallstatic void emitNonZeroVLAInit(CodeGenFunction &CGF, QualType baseType,
11439dce4b3d9d14ba2be2aad5bd7fd8d381a0263c36Michael Ilseman                               llvm::Value *dest, llvm::Value *src,
11447143325db76d6c3dabce82500f8cc7c93a941970John McCall                               llvm::Value *sizeInChars) {
11457143325db76d6c3dabce82500f8cc7c93a941970John McCall  std::pair<CharUnits,CharUnits> baseSizeAndAlign
11467143325db76d6c3dabce82500f8cc7c93a941970John McCall    = CGF.getContext().getTypeInfoInChars(baseType);
11477143325db76d6c3dabce82500f8cc7c93a941970John McCall
11487143325db76d6c3dabce82500f8cc7c93a941970John McCall  CGBuilderTy &Builder = CGF.Builder;
11497143325db76d6c3dabce82500f8cc7c93a941970John McCall
11507143325db76d6c3dabce82500f8cc7c93a941970John McCall  llvm::Value *baseSizeInChars
11517143325db76d6c3dabce82500f8cc7c93a941970John McCall    = llvm::ConstantInt::get(CGF.IntPtrTy, baseSizeAndAlign.first.getQuantity());
11527143325db76d6c3dabce82500f8cc7c93a941970John McCall
11532acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *i8p = Builder.getInt8PtrTy();
11547143325db76d6c3dabce82500f8cc7c93a941970John McCall
11557143325db76d6c3dabce82500f8cc7c93a941970John McCall  llvm::Value *begin = Builder.CreateBitCast(dest, i8p, "vla.begin");
11567143325db76d6c3dabce82500f8cc7c93a941970John McCall  llvm::Value *end = Builder.CreateInBoundsGEP(dest, sizeInChars, "vla.end");
11577143325db76d6c3dabce82500f8cc7c93a941970John McCall
11587143325db76d6c3dabce82500f8cc7c93a941970John McCall  llvm::BasicBlock *originBB = CGF.Builder.GetInsertBlock();
11597143325db76d6c3dabce82500f8cc7c93a941970John McCall  llvm::BasicBlock *loopBB = CGF.createBasicBlock("vla-init.loop");
11607143325db76d6c3dabce82500f8cc7c93a941970John McCall  llvm::BasicBlock *contBB = CGF.createBasicBlock("vla-init.cont");
11617143325db76d6c3dabce82500f8cc7c93a941970John McCall
11627143325db76d6c3dabce82500f8cc7c93a941970John McCall  // Make a loop over the VLA.  C99 guarantees that the VLA element
11637143325db76d6c3dabce82500f8cc7c93a941970John McCall  // count must be nonzero.
11647143325db76d6c3dabce82500f8cc7c93a941970John McCall  CGF.EmitBlock(loopBB);
11657143325db76d6c3dabce82500f8cc7c93a941970John McCall
1166bbf3bacb3e0c1ebb3e8a4a8b1330404a7e379315Jay Foad  llvm::PHINode *cur = Builder.CreatePHI(i8p, 2, "vla.cur");
11677143325db76d6c3dabce82500f8cc7c93a941970John McCall  cur->addIncoming(begin, originBB);
11687143325db76d6c3dabce82500f8cc7c93a941970John McCall
11697143325db76d6c3dabce82500f8cc7c93a941970John McCall  // memcpy the individual element bit-pattern.
11707143325db76d6c3dabce82500f8cc7c93a941970John McCall  Builder.CreateMemCpy(cur, src, baseSizeInChars,
11717143325db76d6c3dabce82500f8cc7c93a941970John McCall                       baseSizeAndAlign.second.getQuantity(),
11727143325db76d6c3dabce82500f8cc7c93a941970John McCall                       /*volatile*/ false);
11737143325db76d6c3dabce82500f8cc7c93a941970John McCall
11747143325db76d6c3dabce82500f8cc7c93a941970John McCall  // Go to the next element.
11757143325db76d6c3dabce82500f8cc7c93a941970John McCall  llvm::Value *next = Builder.CreateConstInBoundsGEP1_32(cur, 1, "vla.next");
11767143325db76d6c3dabce82500f8cc7c93a941970John McCall
11777143325db76d6c3dabce82500f8cc7c93a941970John McCall  // Leave if that's the end of the VLA.
11787143325db76d6c3dabce82500f8cc7c93a941970John McCall  llvm::Value *done = Builder.CreateICmpEQ(next, end, "vla-init.isdone");
11797143325db76d6c3dabce82500f8cc7c93a941970John McCall  Builder.CreateCondBr(done, contBB, loopBB);
11807143325db76d6c3dabce82500f8cc7c93a941970John McCall  cur->addIncoming(next, loopBB);
11817143325db76d6c3dabce82500f8cc7c93a941970John McCall
11827143325db76d6c3dabce82500f8cc7c93a941970John McCall  CGF.EmitBlock(contBB);
11839dce4b3d9d14ba2be2aad5bd7fd8d381a0263c36Michael Ilseman}
11847143325db76d6c3dabce82500f8cc7c93a941970John McCall
11851884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlssonvoid
11861884eb0b5c55edda4893ddec45e7dbad79758782Anders CarlssonCodeGenFunction::EmitNullInitialization(llvm::Value *DestPtr, QualType Ty) {
11870d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson  // Ignore empty classes in C++.
11887edf9e38b91917b661277601c0e448eef0eb2b56Richard Smith  if (getLangOpts().CPlusPlus) {
11890d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson    if (const RecordType *RT = Ty->getAs<RecordType>()) {
11900d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson      if (cast<CXXRecordDecl>(RT->getDecl())->isEmpty())
11910d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson        return;
11920d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson    }
11930d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson  }
11949021718882441dd391a1960084580d3cd19c423aJohn McCall
11959021718882441dd391a1960084580d3cd19c423aJohn McCall  // Cast the dest ptr to the appropriate i8 pointer type.
11969021718882441dd391a1960084580d3cd19c423aJohn McCall  unsigned DestAS =
11979021718882441dd391a1960084580d3cd19c423aJohn McCall    cast<llvm::PointerType>(DestPtr->getType())->getAddressSpace();
11982acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *BP = Builder.getInt8PtrTy(DestAS);
11993d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson  if (DestPtr->getType() != BP)
1200578faa837b552403e2002b97fdfbfde14f2448e5Benjamin Kramer    DestPtr = Builder.CreateBitCast(DestPtr, BP);
12013d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson
12023d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson  // Get size and alignment info for this aggregate.
12039dce4b3d9d14ba2be2aad5bd7fd8d381a0263c36Michael Ilseman  std::pair<CharUnits, CharUnits> TypeInfo =
120479be76c0e360d9e9c44285c9838af02adc43e55fKen Dyck    getContext().getTypeInfoInChars(Ty);
120579be76c0e360d9e9c44285c9838af02adc43e55fKen Dyck  CharUnits Size = TypeInfo.first;
120679be76c0e360d9e9c44285c9838af02adc43e55fKen Dyck  CharUnits Align = TypeInfo.second;
12073d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson
12085576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall  llvm::Value *SizeVal;
12097143325db76d6c3dabce82500f8cc7c93a941970John McCall  const VariableArrayType *vla;
12109021718882441dd391a1960084580d3cd19c423aJohn McCall
12115576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall  // Don't bother emitting a zero-byte memset.
121279be76c0e360d9e9c44285c9838af02adc43e55fKen Dyck  if (Size.isZero()) {
12135576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall    // But note that getTypeInfo returns 0 for a VLA.
12145576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall    if (const VariableArrayType *vlaType =
12155576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall          dyn_cast_or_null<VariableArrayType>(
12165576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall                                          getContext().getAsArrayType(Ty))) {
1217bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      QualType eltType;
1218bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      llvm::Value *numElts;
1219651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      std::tie(numElts, eltType) = getVLASize(vlaType);
1220bc8d40d85f3fa1e34569834916f18fecaa635152John McCall
1221bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      SizeVal = numElts;
1222bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      CharUnits eltSize = getContext().getTypeSizeInChars(eltType);
1223bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      if (!eltSize.isOne())
1224bc8d40d85f3fa1e34569834916f18fecaa635152John McCall        SizeVal = Builder.CreateNUWMul(SizeVal, CGM.getSize(eltSize));
12257143325db76d6c3dabce82500f8cc7c93a941970John McCall      vla = vlaType;
12265576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall    } else {
12275576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall      return;
12285576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall    }
12295576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall  } else {
1230bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    SizeVal = CGM.getSize(Size);
12316bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    vla = nullptr;
12325576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall  }
12339021718882441dd391a1960084580d3cd19c423aJohn McCall
12349021718882441dd391a1960084580d3cd19c423aJohn McCall  // If the type contains a pointer to data member we can't memset it to zero.
12359021718882441dd391a1960084580d3cd19c423aJohn McCall  // Instead, create a null constant and copy it to the destination.
12367143325db76d6c3dabce82500f8cc7c93a941970John McCall  // TODO: there are other patterns besides zero that we can usefully memset,
12377143325db76d6c3dabce82500f8cc7c93a941970John McCall  // like -1, which happens to be the pattern used by member-pointers.
1238f16aa103d3afd42fbca2ab346f191bf745cec092John McCall  if (!CGM.getTypes().isZeroInitializable(Ty)) {
12397143325db76d6c3dabce82500f8cc7c93a941970John McCall    // For a VLA, emit a single element, then splat that over the VLA.
12407143325db76d6c3dabce82500f8cc7c93a941970John McCall    if (vla) Ty = getContext().getBaseElementType(vla);
12415576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall
12429021718882441dd391a1960084580d3cd19c423aJohn McCall    llvm::Constant *NullConstant = CGM.EmitNullConstant(Ty);
12439021718882441dd391a1960084580d3cd19c423aJohn McCall
12449dce4b3d9d14ba2be2aad5bd7fd8d381a0263c36Michael Ilseman    llvm::GlobalVariable *NullVariable =
12459021718882441dd391a1960084580d3cd19c423aJohn McCall      new llvm::GlobalVariable(CGM.getModule(), NullConstant->getType(),
12469dce4b3d9d14ba2be2aad5bd7fd8d381a0263c36Michael Ilseman                               /*isConstant=*/true,
12479021718882441dd391a1960084580d3cd19c423aJohn McCall                               llvm::GlobalVariable::PrivateLinkage,
12485f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                               NullConstant, Twine());
12499021718882441dd391a1960084580d3cd19c423aJohn McCall    llvm::Value *SrcPtr =
12509021718882441dd391a1960084580d3cd19c423aJohn McCall      Builder.CreateBitCast(NullVariable, Builder.getInt8PtrTy());
12519021718882441dd391a1960084580d3cd19c423aJohn McCall
12527143325db76d6c3dabce82500f8cc7c93a941970John McCall    if (vla) return emitNonZeroVLAInit(*this, Ty, DestPtr, SrcPtr, SizeVal);
12537143325db76d6c3dabce82500f8cc7c93a941970John McCall
12549021718882441dd391a1960084580d3cd19c423aJohn McCall    // Get and call the appropriate llvm.memcpy overload.
125579be76c0e360d9e9c44285c9838af02adc43e55fKen Dyck    Builder.CreateMemCpy(DestPtr, SrcPtr, SizeVal, Align.getQuantity(), false);
125688207c9ca218486b93ae7df14e9764cd0c2c3383Chris Lattner    return;
12579dce4b3d9d14ba2be2aad5bd7fd8d381a0263c36Michael Ilseman  }
12589dce4b3d9d14ba2be2aad5bd7fd8d381a0263c36Michael Ilseman
12599021718882441dd391a1960084580d3cd19c423aJohn McCall  // Otherwise, just memset the whole thing to zero.  This is legal
12609021718882441dd391a1960084580d3cd19c423aJohn McCall  // because in LLVM, all default initializers (other than the ones we just
12619021718882441dd391a1960084580d3cd19c423aJohn McCall  // handled above) are guaranteed to have a bit pattern of all zeros.
12629dce4b3d9d14ba2be2aad5bd7fd8d381a0263c36Michael Ilseman  Builder.CreateMemSet(DestPtr, Builder.getInt8(0), SizeVal,
126379be76c0e360d9e9c44285c9838af02adc43e55fKen Dyck                       Align.getQuantity(), false);
12643d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson}
12653d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson
1266ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattnerllvm::BlockAddress *CodeGenFunction::GetAddrOfLabel(const LabelDecl *L) {
1267d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // Make sure that there is a block for the indirect goto.
12686bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  if (!IndirectBranch)
1269d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    GetIndirectGotoBlock();
12709dce4b3d9d14ba2be2aad5bd7fd8d381a0263c36Michael Ilseman
1271ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  llvm::BasicBlock *BB = getJumpDestForLabel(L).getBlock();
12729dce4b3d9d14ba2be2aad5bd7fd8d381a0263c36Michael Ilseman
1273d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // Make sure the indirect branch includes all of the address-taken blocks.
1274d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  IndirectBranch->addDestination(BB);
1275d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  return llvm::BlockAddress::get(CurFn, BB);
12763d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner}
12771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12783d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattnerllvm::BasicBlock *CodeGenFunction::GetIndirectGotoBlock() {
1279d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // If we already made the indirect branch for indirect goto, return its block.
1280d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  if (IndirectBranch) return IndirectBranch->getParent();
12819dce4b3d9d14ba2be2aad5bd7fd8d381a0263c36Michael Ilseman
1282d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  CGBuilderTy TmpBuilder(createBasicBlock("indirectgoto"));
12839dce4b3d9d14ba2be2aad5bd7fd8d381a0263c36Michael Ilseman
12843d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner  // Create the PHI node that indirect gotos will add entries to.
1285bbf3bacb3e0c1ebb3e8a4a8b1330404a7e379315Jay Foad  llvm::Value *DestVal = TmpBuilder.CreatePHI(Int8PtrTy, 0,
1286bbf3bacb3e0c1ebb3e8a4a8b1330404a7e379315Jay Foad                                              "indirect.goto.dest");
12879dce4b3d9d14ba2be2aad5bd7fd8d381a0263c36Michael Ilseman
1288d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // Create the indirect branch instruction.
1289d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  IndirectBranch = TmpBuilder.CreateIndirectBr(DestVal);
1290d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  return IndirectBranch->getParent();
12910ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar}
1292ddf7cac45d85b73127adbbd91a2b28fc7291c57eAnders Carlsson
1293bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall/// Computes the length of an array in elements, as well as the base
1294bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall/// element type and a properly-typed first element pointer.
1295bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCallllvm::Value *CodeGenFunction::emitArrayLength(const ArrayType *origArrayType,
1296bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall                                              QualType &baseType,
1297bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall                                              llvm::Value *&addr) {
1298bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  const ArrayType *arrayType = origArrayType;
1299bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
1300bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  // If it's a VLA, we have to load the stored size.  Note that
1301bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  // this is the size of the VLA in bytes, not its size in elements.
13026bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  llvm::Value *numVLAElements = nullptr;
1303bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  if (isa<VariableArrayType>(arrayType)) {
1304bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    numVLAElements = getVLASize(cast<VariableArrayType>(arrayType)).first;
1305bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
1306bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    // Walk into all VLAs.  This doesn't require changes to addr,
1307bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    // which has type T* where T is the first non-VLA element type.
1308bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    do {
1309bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall      QualType elementType = arrayType->getElementType();
1310bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall      arrayType = getContext().getAsArrayType(elementType);
1311bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
1312bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall      // If we only have VLA components, 'addr' requires no adjustment.
1313bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall      if (!arrayType) {
1314bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall        baseType = elementType;
1315bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall        return numVLAElements;
1316bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall      }
1317bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    } while (isa<VariableArrayType>(arrayType));
1318bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
1319bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    // We get out here only if we find a constant array type
1320bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    // inside the VLA.
1321bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  }
1322bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
1323bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  // We have some number of constant-length arrays, so addr should
1324bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  // have LLVM type [M x [N x [...]]]*.  Build a GEP that walks
1325bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  // down to the first element of addr.
13265f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<llvm::Value*, 8> gepIndices;
1327bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
1328bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  // GEP down to the array type.
1329bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  llvm::ConstantInt *zero = Builder.getInt32(0);
1330bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  gepIndices.push_back(zero);
1331bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
1332bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  uint64_t countFromCLAs = 1;
13331664d540d1524f0faffd2f839fccb56178975c60Richard Smith  QualType eltType;
1334bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
13352acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::ArrayType *llvmArrayType =
13361664d540d1524f0faffd2f839fccb56178975c60Richard Smith    dyn_cast<llvm::ArrayType>(
1337bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall      cast<llvm::PointerType>(addr->getType())->getElementType());
13381664d540d1524f0faffd2f839fccb56178975c60Richard Smith  while (llvmArrayType) {
1339bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    assert(isa<ConstantArrayType>(arrayType));
1340bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    assert(cast<ConstantArrayType>(arrayType)->getSize().getZExtValue()
1341bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall             == llvmArrayType->getNumElements());
1342bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
1343bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    gepIndices.push_back(zero);
1344bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    countFromCLAs *= llvmArrayType->getNumElements();
13451664d540d1524f0faffd2f839fccb56178975c60Richard Smith    eltType = arrayType->getElementType();
1346bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
1347bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    llvmArrayType =
1348bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall      dyn_cast<llvm::ArrayType>(llvmArrayType->getElementType());
1349bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    arrayType = getContext().getAsArrayType(arrayType->getElementType());
13501664d540d1524f0faffd2f839fccb56178975c60Richard Smith    assert((!llvmArrayType || arrayType) &&
13511664d540d1524f0faffd2f839fccb56178975c60Richard Smith           "LLVM and Clang types are out-of-synch");
1352bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  }
1353bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
13541664d540d1524f0faffd2f839fccb56178975c60Richard Smith  if (arrayType) {
13551664d540d1524f0faffd2f839fccb56178975c60Richard Smith    // From this point onwards, the Clang array type has been emitted
13561664d540d1524f0faffd2f839fccb56178975c60Richard Smith    // as some other type (probably a packed struct). Compute the array
13571664d540d1524f0faffd2f839fccb56178975c60Richard Smith    // size, and just emit the 'begin' expression as a bitcast.
13581664d540d1524f0faffd2f839fccb56178975c60Richard Smith    while (arrayType) {
13591664d540d1524f0faffd2f839fccb56178975c60Richard Smith      countFromCLAs *=
13601664d540d1524f0faffd2f839fccb56178975c60Richard Smith          cast<ConstantArrayType>(arrayType)->getSize().getZExtValue();
13611664d540d1524f0faffd2f839fccb56178975c60Richard Smith      eltType = arrayType->getElementType();
13621664d540d1524f0faffd2f839fccb56178975c60Richard Smith      arrayType = getContext().getAsArrayType(eltType);
13631664d540d1524f0faffd2f839fccb56178975c60Richard Smith    }
13641664d540d1524f0faffd2f839fccb56178975c60Richard Smith
1365956a5a17713deb1b5b27893303c4f308a1bd2a62Micah Villmow    unsigned AddressSpace = addr->getType()->getPointerAddressSpace();
13661664d540d1524f0faffd2f839fccb56178975c60Richard Smith    llvm::Type *BaseType = ConvertType(eltType)->getPointerTo(AddressSpace);
13671664d540d1524f0faffd2f839fccb56178975c60Richard Smith    addr = Builder.CreateBitCast(addr, BaseType, "array.begin");
13681664d540d1524f0faffd2f839fccb56178975c60Richard Smith  } else {
13691664d540d1524f0faffd2f839fccb56178975c60Richard Smith    // Create the actual GEP.
13701664d540d1524f0faffd2f839fccb56178975c60Richard Smith    addr = Builder.CreateInBoundsGEP(addr, gepIndices, "array.begin");
13711664d540d1524f0faffd2f839fccb56178975c60Richard Smith  }
1372bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
13731664d540d1524f0faffd2f839fccb56178975c60Richard Smith  baseType = eltType;
1374bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
1375bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  llvm::Value *numElements
1376bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    = llvm::ConstantInt::get(SizeTy, countFromCLAs);
1377bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
1378bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  // If we had any VLA dimensions, factor them in.
1379bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  if (numVLAElements)
1380bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    numElements = Builder.CreateNUWMul(numVLAElements, numElements);
1381bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
1382bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  return numElements;
1383bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall}
1384bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
1385bc8d40d85f3fa1e34569834916f18fecaa635152John McCallstd::pair<llvm::Value*, QualType>
1386bc8d40d85f3fa1e34569834916f18fecaa635152John McCallCodeGenFunction::getVLASize(QualType type) {
1387bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  const VariableArrayType *vla = getContext().getAsVariableArrayType(type);
1388bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  assert(vla && "type was not a variable array type!");
1389bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  return getVLASize(vla);
1390f666b7780d04186521adcaedb0e15dfa4d5e6933Anders Carlsson}
1391dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlsson
1392bc8d40d85f3fa1e34569834916f18fecaa635152John McCallstd::pair<llvm::Value*, QualType>
1393bc8d40d85f3fa1e34569834916f18fecaa635152John McCallCodeGenFunction::getVLASize(const VariableArrayType *type) {
1394bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  // The number of elements so far; always size_t.
13956bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  llvm::Value *numElements = nullptr;
13961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1397bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  QualType elementType;
1398bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  do {
1399bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    elementType = type->getElementType();
1400bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    llvm::Value *vlaSize = VLASizeMap[type->getSizeExpr()];
1401bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    assert(vlaSize && "no size for VLA!");
1402bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    assert(vlaSize->getType() == SizeTy);
14031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1404bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    if (!numElements) {
1405bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      numElements = vlaSize;
1406bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    } else {
1407bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      // It's undefined behavior if this wraps around, so mark it that way.
1408651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      // FIXME: Teach -fsanitize=undefined to trap this.
1409bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      numElements = Builder.CreateNUWMul(numElements, vlaSize);
1410bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    }
1411bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  } while ((type = getContext().getAsVariableArrayType(elementType)));
14121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1413bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  return std::pair<llvm::Value*,QualType>(numElements, elementType);
1414bc8d40d85f3fa1e34569834916f18fecaa635152John McCall}
14151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1416bc8d40d85f3fa1e34569834916f18fecaa635152John McCallvoid CodeGenFunction::EmitVariablyModifiedType(QualType type) {
1417bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  assert(type->isVariablyModifiedType() &&
1418bc8d40d85f3fa1e34569834916f18fecaa635152John McCall         "Must pass variably modified type to EmitVLASizes!");
14191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1420bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  EnsureInsertPoint();
14211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1422bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  // We're going to walk down into the type and look for VLA
1423bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  // expressions.
1424bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  do {
1425bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    assert(type->isVariablyModifiedType());
1426bc8d40d85f3fa1e34569834916f18fecaa635152John McCall
1427bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    const Type *ty = type.getTypePtr();
1428bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    switch (ty->getTypeClass()) {
142906284c1dc56caed19850bc3766c89f51763724c3Abramo Bagnara
1430bc8d40d85f3fa1e34569834916f18fecaa635152John McCall#define TYPE(Class, Base)
1431bc8d40d85f3fa1e34569834916f18fecaa635152John McCall#define ABSTRACT_TYPE(Class, Base)
143206284c1dc56caed19850bc3766c89f51763724c3Abramo Bagnara#define NON_CANONICAL_TYPE(Class, Base)
1433bc8d40d85f3fa1e34569834916f18fecaa635152John McCall#define DEPENDENT_TYPE(Class, Base) case Type::Class:
143406284c1dc56caed19850bc3766c89f51763724c3Abramo Bagnara#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base)
1435bc8d40d85f3fa1e34569834916f18fecaa635152John McCall#include "clang/AST/TypeNodes.def"
143606284c1dc56caed19850bc3766c89f51763724c3Abramo Bagnara      llvm_unreachable("unexpected dependent type!");
1437bc8d40d85f3fa1e34569834916f18fecaa635152John McCall
1438bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    // These types are never variably-modified.
1439bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::Builtin:
1440bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::Complex:
1441bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::Vector:
1442bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::ExtVector:
1443bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::Record:
1444bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::Enum:
14455ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara    case Type::Elaborated:
14465ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara    case Type::TemplateSpecialization:
1447bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::ObjCObject:
1448bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::ObjCInterface:
1449bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::ObjCObjectPointer:
1450bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      llvm_unreachable("type class is never variably-modified!");
1451bc8d40d85f3fa1e34569834916f18fecaa635152John McCall
1452651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    case Type::Adjusted:
1453651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      type = cast<AdjustedType>(ty)->getAdjustedType();
1454651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      break;
1455651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
145612df246d6dea2ee1f92c186f922f1afcf499647aReid Kleckner    case Type::Decayed:
145712df246d6dea2ee1f92c186f922f1afcf499647aReid Kleckner      type = cast<DecayedType>(ty)->getPointeeType();
145812df246d6dea2ee1f92c186f922f1afcf499647aReid Kleckner      break;
145912df246d6dea2ee1f92c186f922f1afcf499647aReid Kleckner
1460bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::Pointer:
1461bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      type = cast<PointerType>(ty)->getPointeeType();
1462bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      break;
1463bc8d40d85f3fa1e34569834916f18fecaa635152John McCall
1464bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::BlockPointer:
1465bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      type = cast<BlockPointerType>(ty)->getPointeeType();
1466bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      break;
1467bc8d40d85f3fa1e34569834916f18fecaa635152John McCall
1468bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::LValueReference:
1469bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::RValueReference:
1470bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      type = cast<ReferenceType>(ty)->getPointeeType();
1471bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      break;
1472bc8d40d85f3fa1e34569834916f18fecaa635152John McCall
1473bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::MemberPointer:
1474bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      type = cast<MemberPointerType>(ty)->getPointeeType();
1475bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      break;
1476bc8d40d85f3fa1e34569834916f18fecaa635152John McCall
1477bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::ConstantArray:
1478bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::IncompleteArray:
1479bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      // Losing element qualification here is fine.
1480bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      type = cast<ArrayType>(ty)->getElementType();
1481bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      break;
1482bc8d40d85f3fa1e34569834916f18fecaa635152John McCall
1483bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::VariableArray: {
1484bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      // Losing element qualification here is fine.
1485bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      const VariableArrayType *vat = cast<VariableArrayType>(ty);
1486bc8d40d85f3fa1e34569834916f18fecaa635152John McCall
1487bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      // Unknown size indication requires no size computation.
1488bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      // Otherwise, evaluate and record it.
1489bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      if (const Expr *size = vat->getSizeExpr()) {
1490bc8d40d85f3fa1e34569834916f18fecaa635152John McCall        // It's possible that we might have emitted this already,
1491bc8d40d85f3fa1e34569834916f18fecaa635152John McCall        // e.g. with a typedef and a pointer to it.
1492bc8d40d85f3fa1e34569834916f18fecaa635152John McCall        llvm::Value *&entry = VLASizeMap[size];
1493bc8d40d85f3fa1e34569834916f18fecaa635152John McCall        if (!entry) {
1494930c05c32eac171ef1a94fabf80aecb4e6e2c840Richard Smith          llvm::Value *Size = EmitScalarExpr(size);
1495930c05c32eac171ef1a94fabf80aecb4e6e2c840Richard Smith
1496930c05c32eac171ef1a94fabf80aecb4e6e2c840Richard Smith          // C11 6.7.6.2p5:
1497930c05c32eac171ef1a94fabf80aecb4e6e2c840Richard Smith          //   If the size is an expression that is not an integer constant
1498930c05c32eac171ef1a94fabf80aecb4e6e2c840Richard Smith          //   expression [...] each time it is evaluated it shall have a value
1499930c05c32eac171ef1a94fabf80aecb4e6e2c840Richard Smith          //   greater than zero.
15004f45bc099f2665bc6e4bcbb169aa452390dbf3feWill Dietz          if (SanOpts->VLABound &&
1501d6396a681c9acbe56bc41bbc2bed2db45755bcd7Richard Smith              size->getType()->isSignedIntegerType()) {
1502930c05c32eac171ef1a94fabf80aecb4e6e2c840Richard Smith            llvm::Value *Zero = llvm::Constant::getNullValue(Size->getType());
1503930c05c32eac171ef1a94fabf80aecb4e6e2c840Richard Smith            llvm::Constant *StaticArgs[] = {
1504930c05c32eac171ef1a94fabf80aecb4e6e2c840Richard Smith              EmitCheckSourceLocation(size->getLocStart()),
1505930c05c32eac171ef1a94fabf80aecb4e6e2c840Richard Smith              EmitCheckTypeDescriptor(size->getType())
1506930c05c32eac171ef1a94fabf80aecb4e6e2c840Richard Smith            };
1507930c05c32eac171ef1a94fabf80aecb4e6e2c840Richard Smith            EmitCheck(Builder.CreateICmpSGT(Size, Zero),
1508ad95481b2769ad61f23afc5e2c31f11c66090657Will Dietz                      "vla_bound_not_positive", StaticArgs, Size,
1509ad95481b2769ad61f23afc5e2c31f11c66090657Will Dietz                      CRK_Recoverable);
1510930c05c32eac171ef1a94fabf80aecb4e6e2c840Richard Smith          }
1511930c05c32eac171ef1a94fabf80aecb4e6e2c840Richard Smith
1512bc8d40d85f3fa1e34569834916f18fecaa635152John McCall          // Always zexting here would be wrong if it weren't
1513bc8d40d85f3fa1e34569834916f18fecaa635152John McCall          // undefined behavior to have a negative bound.
1514bf43f2fdfbd7c4f8cd1f1403765eca9e5dc6c8aeRichard Smith          entry = Builder.CreateIntCast(Size, SizeTy, /*signed*/ false);
1515bc8d40d85f3fa1e34569834916f18fecaa635152John McCall        }
1516bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      }
1517bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      type = vat->getElementType();
1518bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      break;
1519fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson    }
15201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
152106284c1dc56caed19850bc3766c89f51763724c3Abramo Bagnara    case Type::FunctionProto:
1522bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::FunctionNoProto:
1523651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      type = cast<FunctionType>(ty)->getReturnType();
1524bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      break;
1525b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
15265ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara    case Type::Paren:
15275ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara    case Type::TypeOf:
15285ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara    case Type::UnaryTransform:
15295ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara    case Type::Attributed:
15305ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara    case Type::SubstTemplateTypeParm:
1531226399ce18cdcbb1e83af7c5e644bdabb9d4f2f8David Blaikie    case Type::PackExpansion:
15325ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara      // Keep walking after single level desugaring.
15335ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara      type = type.getSingleStepDesugaredType(getContext());
15345ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara      break;
15355ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara
15365ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara    case Type::Typedef:
15375ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara    case Type::Decltype:
15385ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara    case Type::Auto:
15395ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara      // Stop walking: nothing to do.
15405ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara      return;
15415ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara
15425ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara    case Type::TypeOfExpr:
15435ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara      // Stop walking: emit typeof expression.
15445ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara      EmitIgnoredExpr(cast<TypeOfExprType>(ty)->getUnderlyingExpr());
15455ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara      return;
15465ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara
1547b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    case Type::Atomic:
1548b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      type = cast<AtomicType>(ty)->getValueType();
1549b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      break;
1550bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    }
1551bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  } while (type->isVariablyModifiedType());
1552dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlsson}
15534fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman
15544fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedmanllvm::Value* CodeGenFunction::EmitVAListRef(const Expr* E) {
1555bc07a55b68342a230aafd8875cc7a26450dd3f64Dan Gohman  if (getContext().getBuiltinVaListType()->isArrayType())
15564fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman    return EmitScalarExpr(E);
15574fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman  return EmitLValue(E).getAddress();
15584fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman}
15596ccc47698d0311ddabf32fa0f6db8e4f09ac96f8Anders Carlsson
15603b8e0b7f96164e8c49d2a833da26edd960f5da1fYunzhong Gaovoid CodeGenFunction::EmitDeclRefExprDbgValue(const DeclRefExpr *E,
15613b8e0b7f96164e8c49d2a833da26edd960f5da1fYunzhong Gao                                              llvm::Constant *Init) {
156225c2c8fb9309050612009a6571e2660e75531348Devang Patel  assert (Init && "Invalid DeclRefExpr initializer!");
15633b8e0b7f96164e8c49d2a833da26edd960f5da1fYunzhong Gao  if (CGDebugInfo *Dbg = getDebugInfo())
15643b8e0b7f96164e8c49d2a833da26edd960f5da1fYunzhong Gao    if (CGM.getCodeGenOpts().getDebugInfo() >= CodeGenOptions::LimitedDebugInfo)
15653b8e0b7f96164e8c49d2a833da26edd960f5da1fYunzhong Gao      Dbg->EmitGlobalVariable(E->getDecl(), Init);
15668d308384a220c7dc81755c47cdcbdd87dac25d5bDevang Patel}
156756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
156856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCallCodeGenFunction::PeepholeProtection
156956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCallCodeGenFunction::protectFromPeepholes(RValue rvalue) {
157056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  // At the moment, the only aggressive peephole we do in IR gen
157156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  // is trunc(zext) folding, but if we add more, we can easily
157256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  // extend this protection.
157356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
157456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  if (!rvalue.isScalar()) return PeepholeProtection();
157556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  llvm::Value *value = rvalue.getScalarVal();
157656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  if (!isa<llvm::ZExtInst>(value)) return PeepholeProtection();
157756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
157856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  // Just make an extra bitcast.
157956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  assert(HaveInsertPoint());
158056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  llvm::Instruction *inst = new llvm::BitCastInst(value, value->getType(), "",
158156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall                                                  Builder.GetInsertBlock());
158256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
158356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  PeepholeProtection protection;
158456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  protection.Inst = inst;
158556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  return protection;
158656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall}
158756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
158856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCallvoid CodeGenFunction::unprotectFromPeepholes(PeepholeProtection protection) {
158956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  if (!protection.Inst) return;
159056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
159156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  // In theory, we could try to duplicate the peepholes now, but whatever.
159256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  protection.Inst->eraseFromParent();
159356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall}
159477f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge
159577f68bb90af93b95045fb994e7cd68137adcc132Julien Lerougellvm::Value *CodeGenFunction::EmitAnnotationCall(llvm::Value *AnnotationFn,
159677f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge                                                 llvm::Value *AnnotatedVal,
1597cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko                                                 StringRef AnnotationStr,
159877f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge                                                 SourceLocation Location) {
159977f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  llvm::Value *Args[4] = {
160077f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge    AnnotatedVal,
160177f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge    Builder.CreateBitCast(CGM.EmitAnnotationString(AnnotationStr), Int8PtrTy),
160277f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge    Builder.CreateBitCast(CGM.EmitAnnotationUnit(Location), Int8PtrTy),
160377f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge    CGM.EmitAnnotationLineNo(Location)
160477f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  };
160577f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  return Builder.CreateCall(AnnotationFn, Args);
160677f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge}
160777f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge
160877f68bb90af93b95045fb994e7cd68137adcc132Julien Lerougevoid CodeGenFunction::EmitVarAnnotations(const VarDecl *D, llvm::Value *V) {
160977f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  assert(D->hasAttr<AnnotateAttr>() && "no annotate attribute");
161077f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  // FIXME We create a new bitcast for every annotation because that's what
161177f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  // llvm-gcc was doing.
1612651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  for (const auto *I : D->specific_attrs<AnnotateAttr>())
161377f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge    EmitAnnotationCall(CGM.getIntrinsic(llvm::Intrinsic::var_annotation),
161477f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge                       Builder.CreateBitCast(V, CGM.Int8PtrTy, V->getName()),
1615651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                       I->getAnnotation(), D->getLocation());
161677f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge}
161777f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge
161877f68bb90af93b95045fb994e7cd68137adcc132Julien Lerougellvm::Value *CodeGenFunction::EmitFieldAnnotations(const FieldDecl *D,
161977f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge                                                   llvm::Value *V) {
162077f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  assert(D->hasAttr<AnnotateAttr>() && "no annotate attribute");
162177f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  llvm::Type *VTy = V->getType();
162277f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::ptr_annotation,
162377f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge                                    CGM.Int8PtrTy);
162477f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge
1625651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  for (const auto *I : D->specific_attrs<AnnotateAttr>()) {
162677f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge    // FIXME Always emit the cast inst so we can differentiate between
162777f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge    // annotation on the first field of a struct and annotation on the struct
162877f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge    // itself.
162977f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge    if (VTy != CGM.Int8PtrTy)
163077f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge      V = Builder.Insert(new llvm::BitCastInst(V, CGM.Int8PtrTy));
1631651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    V = EmitAnnotationCall(F, V, I->getAnnotation(), D->getLocation());
163277f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge    V = Builder.CreateBitCast(V, VTy);
163377f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  }
163477f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge
163577f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  return V;
163677f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge}
1637524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir
1638524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben LangmuirCodeGenFunction::CGCapturedStmtInfo::~CGCapturedStmtInfo() { }
16396bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
16406bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesvoid CodeGenFunction::InsertHelper(llvm::Instruction *I,
16416bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                   const llvm::Twine &Name,
16426bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                   llvm::BasicBlock *BB,
16436bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                   llvm::BasicBlock::iterator InsertPt) const {
16446bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  LoopStack.InsertHelper(I);
16456bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines}
16466bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
16476bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinestemplate <bool PreserveNames>
16486bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesvoid CGBuilderInserter<PreserveNames>::InsertHelper(
16496bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    llvm::Instruction *I, const llvm::Twine &Name, llvm::BasicBlock *BB,
16506bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    llvm::BasicBlock::iterator InsertPt) const {
16516bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  llvm::IRBuilderDefaultInserter<PreserveNames>::InsertHelper(I, Name, BB,
16526bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                                              InsertPt);
16536bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  if (CGF)
16546bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    CGF->InsertHelper(I, Name, BB, InsertPt);
16556bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines}
16566bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
16576bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#ifdef NDEBUG
16586bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#define PreserveNames false
16596bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#else
16606bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#define PreserveNames true
16616bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#endif
16626bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinestemplate void CGBuilderInserter<PreserveNames>::InsertHelper(
16636bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    llvm::Instruction *I, const llvm::Twine &Name, llvm::BasicBlock *BB,
16646bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    llvm::BasicBlock::iterator InsertPt) const;
16656bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#undef PreserveNames
1666