CodeGenFunction.cpp revision 8a07cd3f59a0ede54906bdc8ed21724815db27ad
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"
1855fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "CodeGenModule.h"
19de7fb8413b13651fd85b7125d08b3c9ac2816d9dDaniel Dunbar#include "clang/AST/ASTContext.h"
20c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel Dunbar#include "clang/AST/Decl.h"
212b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson#include "clang/AST/DeclCXX.h"
226a1e0eb557d47e85185e09bdf8721f53f4bf9c9cMike Stump#include "clang/AST/StmtCXX.h"
231db7040604154f71c54cf1329ae384fbef196668Guy Benyei#include "clang/Basic/OpenCL.h"
2455fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Basic/TargetInfo.h"
257255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner#include "clang/Frontend/CodeGenOptions.h"
263b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth#include "llvm/IR/DataLayout.h"
273b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth#include "llvm/IR/Intrinsics.h"
283b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth#include "llvm/IR/MDBuilder.h"
293b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth#include "llvm/IR/Operator.h"
305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace CodeGen;
325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
334904bf4e84cfb48080270ebaa9005327f18ab0e5Fariborz JahanianCodeGenFunction::CodeGenFunction(CodeGenModule &cgm, bool suppressNewContext)
3464aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall  : CodeGenTypeCache(cgm), CGM(cgm), Target(cgm.getTarget()),
35cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman    Builder(cgm.getModule().getContext()),
36524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir    CapturedStmtInfo(0),
374f45bc099f2665bc6e4bcbb169aa452390dbf3feWill Dietz    SanitizePerformTypeCheck(CGM.getSanOpts().Null |
384f45bc099f2665bc6e4bcbb169aa452390dbf3feWill Dietz                             CGM.getSanOpts().Alignment |
394f45bc099f2665bc6e4bcbb169aa452390dbf3feWill Dietz                             CGM.getSanOpts().ObjectSize |
404f45bc099f2665bc6e4bcbb169aa452390dbf3feWill Dietz                             CGM.getSanOpts().Vptr),
414f45bc099f2665bc6e4bcbb169aa452390dbf3feWill Dietz    SanOpts(&CGM.getSanOpts()),
42f85e193739c953358c865005855253af4f68a497John McCall    AutoreleaseResult(false), BlockInfo(0), BlockPointer(0),
4323f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman    LambdaThisCaptureField(0), NormalCleanupDest(0), NextCleanupDestIndex(1),
44cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman    FirstBlockInfo(0), EHResumeBlock(0), ExceptionSlot(0), EHSelectorSlot(0),
4594d46b55103bf7ba0dc5425baf1eb7e4adcaa7eaManman Ren    DebugInfo(0), DisableDebugInfo(false), CalleeWithThisReturn(0),
4694d46b55103bf7ba0dc5425baf1eb7e4adcaa7eaManman Ren    DidCallStackSave(false),
4793c332a8ba2c193c435b293966d343dab15f555bJohn McCall    IndirectBranch(0), SwitchInsn(0), CaseRangeBlock(0), UnreachableBlock(0),
48d072e59eb6b5e32f93be25f08f212b0ec21d6c5dAdrian Prantl    NumReturnExprs(0), NumSimpleReturnExprs(0),
4959660c21178b6af518bd4b564e032d5c9cc218cbTimur Iskhodzhanov    CXXABIThisDecl(0), CXXABIThisValue(0), CXXThisValue(0),
50c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith    CXXDefaultInitExprThis(0),
5159660c21178b6af518bd4b564e032d5c9cc218cbTimur Iskhodzhanov    CXXStructorImplicitParamDecl(0), CXXStructorImplicitParamValue(0),
52495cfa46300979642acde8d93a1f21c9291dac98Nadav Rotem    OutermostConditional(0), CurLexicalScope(0), TerminateLandingPad(0),
53cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman    TerminateHandler(0), TrapBB(0) {
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);
751a343ebbf413e8eae6b2737b2b2d79cbf5765571John McCall}
761a343ebbf413e8eae6b2737b2b2d79cbf5765571John McCall
775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
789cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnerllvm::Type *CodeGenFunction::ConvertTypeForMem(QualType T) {
798b1a343b6b360d63d5dc8a6beb841ce4414c1e00Daniel Dunbar  return CGM.getTypes().ConvertTypeForMem(T);
808b1a343b6b360d63d5dc8a6beb841ce4414c1e00Daniel Dunbar}
818b1a343b6b360d63d5dc8a6beb841ce4414c1e00Daniel Dunbar
829cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnerllvm::Type *CodeGenFunction::ConvertType(QualType T) {
835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return CGM.getTypes().ConvertType(T);
845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
869d232c884ea9872d6555df0fd7359699819bc1f1John McCallTypeEvaluationKind CodeGenFunction::getEvaluationKind(QualType type) {
879d232c884ea9872d6555df0fd7359699819bc1f1John McCall  type = type.getCanonicalType();
889d232c884ea9872d6555df0fd7359699819bc1f1John McCall  while (true) {
899d232c884ea9872d6555df0fd7359699819bc1f1John McCall    switch (type->getTypeClass()) {
90f2aac84709c418189e476ad591848dad50291885John McCall#define TYPE(name, parent)
91f2aac84709c418189e476ad591848dad50291885John McCall#define ABSTRACT_TYPE(name, parent)
92f2aac84709c418189e476ad591848dad50291885John McCall#define NON_CANONICAL_TYPE(name, parent) case Type::name:
93f2aac84709c418189e476ad591848dad50291885John McCall#define DEPENDENT_TYPE(name, parent) case Type::name:
94f2aac84709c418189e476ad591848dad50291885John McCall#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(name, parent) case Type::name:
95f2aac84709c418189e476ad591848dad50291885John McCall#include "clang/AST/TypeNodes.def"
969d232c884ea9872d6555df0fd7359699819bc1f1John McCall      llvm_unreachable("non-canonical or dependent type in IR-generation");
979d232c884ea9872d6555df0fd7359699819bc1f1John McCall
98dc7a4f5d7a7e3b60d4dc4a80338d7a2728540998Richard Smith    case Type::Auto:
99dc7a4f5d7a7e3b60d4dc4a80338d7a2728540998Richard Smith      llvm_unreachable("undeduced auto type in IR-generation");
100dc7a4f5d7a7e3b60d4dc4a80338d7a2728540998Richard Smith
1019d232c884ea9872d6555df0fd7359699819bc1f1John McCall    // Various scalar types.
1029d232c884ea9872d6555df0fd7359699819bc1f1John McCall    case Type::Builtin:
1039d232c884ea9872d6555df0fd7359699819bc1f1John McCall    case Type::Pointer:
1049d232c884ea9872d6555df0fd7359699819bc1f1John McCall    case Type::BlockPointer:
1059d232c884ea9872d6555df0fd7359699819bc1f1John McCall    case Type::LValueReference:
1069d232c884ea9872d6555df0fd7359699819bc1f1John McCall    case Type::RValueReference:
1079d232c884ea9872d6555df0fd7359699819bc1f1John McCall    case Type::MemberPointer:
1089d232c884ea9872d6555df0fd7359699819bc1f1John McCall    case Type::Vector:
1099d232c884ea9872d6555df0fd7359699819bc1f1John McCall    case Type::ExtVector:
1109d232c884ea9872d6555df0fd7359699819bc1f1John McCall    case Type::FunctionProto:
1119d232c884ea9872d6555df0fd7359699819bc1f1John McCall    case Type::FunctionNoProto:
1129d232c884ea9872d6555df0fd7359699819bc1f1John McCall    case Type::Enum:
1139d232c884ea9872d6555df0fd7359699819bc1f1John McCall    case Type::ObjCObjectPointer:
1149d232c884ea9872d6555df0fd7359699819bc1f1John McCall      return TEK_Scalar;
1159d232c884ea9872d6555df0fd7359699819bc1f1John McCall
1169d232c884ea9872d6555df0fd7359699819bc1f1John McCall    // Complexes.
1179d232c884ea9872d6555df0fd7359699819bc1f1John McCall    case Type::Complex:
1189d232c884ea9872d6555df0fd7359699819bc1f1John McCall      return TEK_Complex;
1199d232c884ea9872d6555df0fd7359699819bc1f1John McCall
1209d232c884ea9872d6555df0fd7359699819bc1f1John McCall    // Arrays, records, and Objective-C objects.
1219d232c884ea9872d6555df0fd7359699819bc1f1John McCall    case Type::ConstantArray:
1229d232c884ea9872d6555df0fd7359699819bc1f1John McCall    case Type::IncompleteArray:
1239d232c884ea9872d6555df0fd7359699819bc1f1John McCall    case Type::VariableArray:
1249d232c884ea9872d6555df0fd7359699819bc1f1John McCall    case Type::Record:
1259d232c884ea9872d6555df0fd7359699819bc1f1John McCall    case Type::ObjCObject:
1269d232c884ea9872d6555df0fd7359699819bc1f1John McCall    case Type::ObjCInterface:
1279d232c884ea9872d6555df0fd7359699819bc1f1John McCall      return TEK_Aggregate;
1289d232c884ea9872d6555df0fd7359699819bc1f1John McCall
1299d232c884ea9872d6555df0fd7359699819bc1f1John McCall    // We operate on atomic values according to their underlying type.
1309d232c884ea9872d6555df0fd7359699819bc1f1John McCall    case Type::Atomic:
1319d232c884ea9872d6555df0fd7359699819bc1f1John McCall      type = cast<AtomicType>(type)->getValueType();
1329d232c884ea9872d6555df0fd7359699819bc1f1John McCall      continue;
1339d232c884ea9872d6555df0fd7359699819bc1f1John McCall    }
1349d232c884ea9872d6555df0fd7359699819bc1f1John McCall    llvm_unreachable("unknown type kind!");
135f2aac84709c418189e476ad591848dad50291885John McCall  }
1364111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner}
137391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner
1380a0f93c90fd397a1aa9f97283c55f8ba0062bf55David Blaikievoid CodeGenFunction::EmitReturnBlock() {
1391c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // For cleanliness, we try to avoid emitting the return block for
1401c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // simple cases.
1411c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
1421c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
1431c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  if (CurBB) {
1441c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar    assert(!CurBB->getTerminator() && "Unexpected terminated block.");
1451c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
14696e18b05ea6b55aa92a1a576f29e9cee73a7e20bDaniel Dunbar    // We have a valid insert point, reuse it if it is empty or there are no
14796e18b05ea6b55aa92a1a576f29e9cee73a7e20bDaniel Dunbar    // explicit jumps to the return block.
148ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    if (CurBB->empty() || ReturnBlock.getBlock()->use_empty()) {
149ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      ReturnBlock.getBlock()->replaceAllUsesWith(CurBB);
150ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      delete ReturnBlock.getBlock();
15196e18b05ea6b55aa92a1a576f29e9cee73a7e20bDaniel Dunbar    } else
152ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      EmitBlock(ReturnBlock.getBlock());
1530a0f93c90fd397a1aa9f97283c55f8ba0062bf55David Blaikie    return;
1541c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  }
1551c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
1561c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // Otherwise, if the return block is the target of a single direct
1571c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // branch then we can just put the code in that block instead. This
1581c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // cleans up functions which started with a unified return block.
159ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  if (ReturnBlock.getBlock()->hasOneUse()) {
1601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    llvm::BranchInst *BI =
161ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      dyn_cast<llvm::BranchInst>(*ReturnBlock.getBlock()->use_begin());
162f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    if (BI && BI->isUnconditional() &&
163ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall        BI->getSuccessor(0) == ReturnBlock.getBlock()) {
164c5139a9f0c9d4f835e4ab0b51e78d2f74fb488d9David Blaikie      // Reset insertion point, including debug location, and delete the
165c5139a9f0c9d4f835e4ab0b51e78d2f74fb488d9David Blaikie      // branch.  This is really subtle and only works because the next change
166c5139a9f0c9d4f835e4ab0b51e78d2f74fb488d9David Blaikie      // in location will hit the caching in CGDebugInfo::EmitLocation and not
167c5139a9f0c9d4f835e4ab0b51e78d2f74fb488d9David Blaikie      // override this.
168acae01124151392a842bd6c37bd01b1ad56d6b4dEric Christopher      Builder.SetCurrentDebugLocation(BI->getDebugLoc());
1691c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      Builder.SetInsertPoint(BI->getParent());
1701c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      BI->eraseFromParent();
171ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      delete ReturnBlock.getBlock();
1720a0f93c90fd397a1aa9f97283c55f8ba0062bf55David Blaikie      return;
1731c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar    }
1741c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  }
1751c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
176f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // FIXME: We are at an unreachable point, there is no reason to emit the block
177f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // unless it has uses. However, we still need a place to put the debug
178f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // region.end for now.
1791c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
180ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  EmitBlock(ReturnBlock.getBlock());
181f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall}
182f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
183f1549f66a8216a78112286e3978cea2c29d6334cJohn McCallstatic void EmitIfUsed(CodeGenFunction &CGF, llvm::BasicBlock *BB) {
184f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  if (!BB) return;
185f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  if (!BB->use_empty())
186f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    return CGF.CurFn->getBasicBlockList().push_back(BB);
187f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  delete BB;
1881c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar}
1891c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
190af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbarvoid CodeGenFunction::FinishFunction(SourceLocation EndLoc) {
191391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  assert(BreakContinueStack.empty() &&
192391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner         "mismatched push/pop in break/continue stack!");
1931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
194d072e59eb6b5e32f93be25f08f212b0ec21d6c5dAdrian Prantl  bool OnlySimpleReturnStmts = NumSimpleReturnExprs > 0
195d072e59eb6b5e32f93be25f08f212b0ec21d6c5dAdrian Prantl    && NumSimpleReturnExprs == NumReturnExprs;
196d072e59eb6b5e32f93be25f08f212b0ec21d6c5dAdrian Prantl  // If the function contains only a simple return statement, the
1973081c6fe13fbefc398e685a34fe7ba644c209ecdAdrian Prantl  // location before the cleanup code becomes the last useful
1983081c6fe13fbefc398e685a34fe7ba644c209ecdAdrian Prantl  // breakpoint in the function, because the simple return expression
1993081c6fe13fbefc398e685a34fe7ba644c209ecdAdrian Prantl  // will be evaluated after the cleanup code. To be safe, set the
2003081c6fe13fbefc398e685a34fe7ba644c209ecdAdrian Prantl  // debug location for cleanup code to the location of the return
2013081c6fe13fbefc398e685a34fe7ba644c209ecdAdrian Prantl  // statement. Otherwise the cleanup code should be at the end of the
2023081c6fe13fbefc398e685a34fe7ba644c209ecdAdrian Prantl  // function's lexical scope.
203fa6b079b1231366696f6a497c6a084c73a35c85dAdrian Prantl  if (CGDebugInfo *DI = getDebugInfo()) {
204d072e59eb6b5e32f93be25f08f212b0ec21d6c5dAdrian Prantl    if (OnlySimpleReturnStmts)
205ece7ebd2f2d03dc58fa1937a6d53e2a9f8c410f1Adrian Prantl      DI->EmitLocation(Builder, LastStopPoint);
206fa6b079b1231366696f6a497c6a084c73a35c85dAdrian Prantl    else
207fa6b079b1231366696f6a497c6a084c73a35c85dAdrian Prantl      DI->EmitLocation(Builder, EndLoc);
208fa6b079b1231366696f6a497c6a084c73a35c85dAdrian Prantl  }
2090a0f93c90fd397a1aa9f97283c55f8ba0062bf55David Blaikie
210f85e193739c953358c865005855253af4f68a497John McCall  // Pop any cleanups that might have been associated with the
211f85e193739c953358c865005855253af4f68a497John McCall  // parameters.  Do this in whatever block we're currently in; it's
212f85e193739c953358c865005855253af4f68a497John McCall  // important to do this before we enter the return block or return
213f85e193739c953358c865005855253af4f68a497John McCall  // edges will be *really* confused.
214fa6b079b1231366696f6a497c6a084c73a35c85dAdrian Prantl  bool EmitRetDbgLoc = true;
215fa6b079b1231366696f6a497c6a084c73a35c85dAdrian Prantl  if (EHStack.stable_begin() != PrologueCleanupDepth) {
2161c3db769249a633108dc320da87c48a432e2bcc1Adrian Prantl    PopCleanupBlocks(PrologueCleanupDepth);
217f85e193739c953358c865005855253af4f68a497John McCall
218fa6b079b1231366696f6a497c6a084c73a35c85dAdrian Prantl    // Make sure the line table doesn't jump back into the body for
219fa6b079b1231366696f6a497c6a084c73a35c85dAdrian Prantl    // the ret after it's been at EndLoc.
220fa6b079b1231366696f6a497c6a084c73a35c85dAdrian Prantl    EmitRetDbgLoc = false;
221fa6b079b1231366696f6a497c6a084c73a35c85dAdrian Prantl
222fa6b079b1231366696f6a497c6a084c73a35c85dAdrian Prantl    if (CGDebugInfo *DI = getDebugInfo())
223d072e59eb6b5e32f93be25f08f212b0ec21d6c5dAdrian Prantl      if (OnlySimpleReturnStmts)
224fa6b079b1231366696f6a497c6a084c73a35c85dAdrian Prantl        DI->EmitLocation(Builder, EndLoc);
225fa6b079b1231366696f6a497c6a084c73a35c85dAdrian Prantl  }
226fa6b079b1231366696f6a497c6a084c73a35c85dAdrian Prantl
2271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Emit function epilog (to return).
2280a0f93c90fd397a1aa9f97283c55f8ba0062bf55David Blaikie  EmitReturnBlock();
229f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar
230a18652fe1e8233fbf8b67484945c7f7b2bf272beDaniel Dunbar  if (ShouldInstrumentFunction())
231a18652fe1e8233fbf8b67484945c7f7b2bf272beDaniel Dunbar    EmitFunctionInstrumentation("__cyg_profile_func_exit");
2327255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner
233f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar  // Emit debug descriptor for function end.
234e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson  if (CGDebugInfo *DI = getDebugInfo()) {
2355a6fbcfd8c15a2296f94a0473a68ec09d429827fDevang Patel    DI->EmitFunctionEnd(Builder);
236f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar  }
237f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar
238fa6b079b1231366696f6a497c6a084c73a35c85dAdrian Prantl  EmitFunctionEpilog(*CurFnInfo, EmitRetDbgLoc);
239cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump  EmitEndEHSpec(CurCodeDecl);
2405ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar
241f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  assert(EHStack.empty() &&
242f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall         "did not remove all scopes from cleanup stack!");
243f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
244d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // If someone did an indirect goto, emit the indirect goto block at the end of
245d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // the function.
246d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  if (IndirectBranch) {
247d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    EmitBlock(IndirectBranch->getParent());
248d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    Builder.ClearInsertionPoint();
249d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  }
2509dce4b3d9d14ba2be2aad5bd7fd8d381a0263c36Michael Ilseman
251391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  // Remove the AllocaInsertPt instruction, which is just a convenience for us.
252481769b5dc102b0256b35581e787909ad5edfab5Chris Lattner  llvm::Instruction *Ptr = AllocaInsertPt;
253391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  AllocaInsertPt = 0;
254481769b5dc102b0256b35581e787909ad5edfab5Chris Lattner  Ptr->eraseFromParent();
2559dce4b3d9d14ba2be2aad5bd7fd8d381a0263c36Michael Ilseman
256d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // If someone took the address of a label but never did an indirect goto, we
257d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // made a zero entry PHI node, which is illegal, zap it now.
258d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  if (IndirectBranch) {
259d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    llvm::PHINode *PN = cast<llvm::PHINode>(IndirectBranch->getAddress());
260d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    if (PN->getNumIncomingValues() == 0) {
261d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner      PN->replaceAllUsesWith(llvm::UndefValue::get(PN->getType()));
262d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner      PN->eraseFromParent();
263d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    }
264d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  }
265f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
266777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  EmitIfUsed(*this, EHResumeBlock);
267f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  EmitIfUsed(*this, TerminateLandingPad);
268f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  EmitIfUsed(*this, TerminateHandler);
269f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  EmitIfUsed(*this, UnreachableBlock);
270744016dde06fcffd50931e94a98c850f8b12cd87John McCall
271744016dde06fcffd50931e94a98c850f8b12cd87John McCall  if (CGM.getCodeGenOpts().EmitDeclMetadata)
272744016dde06fcffd50931e94a98c850f8b12cd87John McCall    EmitDeclMetadata();
273c8aa5f1f264fb230c38182adab944232bb160c2bChris Lattner}
274c8aa5f1f264fb230c38182adab944232bb160c2bChris Lattner
2757255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner/// ShouldInstrumentFunction - Return true if the current function should be
2767255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner/// instrumented with __cyg_profile_func_* calls
2777255a2d997b15beae82e627052fdb1b2474495c2Chris Lattnerbool CodeGenFunction::ShouldInstrumentFunction() {
2787255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  if (!CGM.getCodeGenOpts().InstrumentFunctions)
2797255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner    return false;
2807aa488a7fc5c3a8cd1a2b93476150e9737760713Ted Kremenek  if (!CurFuncDecl || CurFuncDecl->hasAttr<NoInstrumentFunctionAttr>())
2817255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner    return false;
2827255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  return true;
2837255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner}
2847255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner
2857255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner/// EmitFunctionInstrumentation - Emit LLVM code to call the specified
2867255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner/// instrumentation function with the current function and the call site, if
2877255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner/// function instrumentation is enabled.
2887255a2d997b15beae82e627052fdb1b2474495c2Chris Lattnervoid CodeGenFunction::EmitFunctionInstrumentation(const char *Fn) {
2898dab6571b2cab96f44d0a1d6e3edbfdb68b7ed6bChris Lattner  // void __cyg_profile_func_{enter,exit} (void *this_fn, void *call_site);
2909cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::PointerType *PointerTy = Int8PtrTy;
2919cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::Type *ProfileFuncArgs[] = { PointerTy, PointerTy };
2922acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::FunctionType *FunctionTy =
2938b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner    llvm::FunctionType::get(VoidTy, ProfileFuncArgs, false);
2947255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner
2957255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  llvm::Constant *F = CGM.CreateRuntimeFunction(FunctionTy, Fn);
2967255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  llvm::CallInst *CallSite = Builder.CreateCall(
2978dd55a3c3b28d195717c87bbc60e765951d408feBenjamin Kramer    CGM.getIntrinsic(llvm::Intrinsic::returnaddress),
29877b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner    llvm::ConstantInt::get(Int32Ty, 0),
2997255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner    "callsite");
3007255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner
301bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  llvm::Value *args[] = {
302bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall    llvm::ConstantExpr::getBitCast(CurFn, PointerTy),
303bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall    CallSite
304bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  };
305bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall
306bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  EmitNounwindRuntimeCall(F, args);
3077255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner}
3087255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner
309be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divackyvoid CodeGenFunction::EmitMCountInstrumentation() {
3108b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner  llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
311be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky
31264aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall  llvm::Constant *MCountFn =
31364aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall    CGM.CreateRuntimeFunction(FTy, getTarget().getMCountName());
314bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  EmitNounwindRuntimeCall(MCountFn);
315be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky}
316be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky
317198871cc90375246d8692680467ff6e810edac36Tanya Lattner// OpenCL v1.2 s5.6.4.6 allows the compiler to store kernel argument
318198871cc90375246d8692680467ff6e810edac36Tanya Lattner// information in the program executable. The argument information stored
319198871cc90375246d8692680467ff6e810edac36Tanya Lattner// includes the argument name, its type, the address and access qualifiers used.
320198871cc90375246d8692680467ff6e810edac36Tanya Lattnerstatic void GenOpenCLArgMetadata(const FunctionDecl *FD, llvm::Function *Fn,
321198871cc90375246d8692680467ff6e810edac36Tanya Lattner                                 CodeGenModule &CGM,llvm::LLVMContext &Context,
3221db7040604154f71c54cf1329ae384fbef196668Guy Benyei                                 SmallVector <llvm::Value*, 5> &kernelMDArgs,
3231db7040604154f71c54cf1329ae384fbef196668Guy Benyei                                 CGBuilderTy& Builder, ASTContext &ASTCtx) {
3241db7040604154f71c54cf1329ae384fbef196668Guy Benyei  // Create MDNodes that represent the kernel arg metadata.
325198871cc90375246d8692680467ff6e810edac36Tanya Lattner  // Each MDNode is a list in the form of "key", N number of values which is
326198871cc90375246d8692680467ff6e810edac36Tanya Lattner  // the same number of values as their are kernel arguments.
3279dce4b3d9d14ba2be2aad5bd7fd8d381a0263c36Michael Ilseman
3281db7040604154f71c54cf1329ae384fbef196668Guy Benyei  // MDNode for the kernel argument address space qualifiers.
3291db7040604154f71c54cf1329ae384fbef196668Guy Benyei  SmallVector<llvm::Value*, 8> addressQuals;
3301db7040604154f71c54cf1329ae384fbef196668Guy Benyei  addressQuals.push_back(llvm::MDString::get(Context, "kernel_arg_addr_space"));
3311db7040604154f71c54cf1329ae384fbef196668Guy Benyei
3321db7040604154f71c54cf1329ae384fbef196668Guy Benyei  // MDNode for the kernel argument access qualifiers (images only).
3331db7040604154f71c54cf1329ae384fbef196668Guy Benyei  SmallVector<llvm::Value*, 8> accessQuals;
3341db7040604154f71c54cf1329ae384fbef196668Guy Benyei  accessQuals.push_back(llvm::MDString::get(Context, "kernel_arg_access_qual"));
3351db7040604154f71c54cf1329ae384fbef196668Guy Benyei
3361db7040604154f71c54cf1329ae384fbef196668Guy Benyei  // MDNode for the kernel argument type names.
3371db7040604154f71c54cf1329ae384fbef196668Guy Benyei  SmallVector<llvm::Value*, 8> argTypeNames;
3381db7040604154f71c54cf1329ae384fbef196668Guy Benyei  argTypeNames.push_back(llvm::MDString::get(Context, "kernel_arg_type"));
3391db7040604154f71c54cf1329ae384fbef196668Guy Benyei
3401db7040604154f71c54cf1329ae384fbef196668Guy Benyei  // MDNode for the kernel argument type qualifiers.
3411db7040604154f71c54cf1329ae384fbef196668Guy Benyei  SmallVector<llvm::Value*, 8> argTypeQuals;
3421db7040604154f71c54cf1329ae384fbef196668Guy Benyei  argTypeQuals.push_back(llvm::MDString::get(Context, "kernel_arg_type_qual"));
3431db7040604154f71c54cf1329ae384fbef196668Guy Benyei
344198871cc90375246d8692680467ff6e810edac36Tanya Lattner  // MDNode for the kernel argument names.
345198871cc90375246d8692680467ff6e810edac36Tanya Lattner  SmallVector<llvm::Value*, 8> argNames;
346198871cc90375246d8692680467ff6e810edac36Tanya Lattner  argNames.push_back(llvm::MDString::get(Context, "kernel_arg_name"));
3479dce4b3d9d14ba2be2aad5bd7fd8d381a0263c36Michael Ilseman
348198871cc90375246d8692680467ff6e810edac36Tanya Lattner  for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
349198871cc90375246d8692680467ff6e810edac36Tanya Lattner    const ParmVarDecl *parm = FD->getParamDecl(i);
3501db7040604154f71c54cf1329ae384fbef196668Guy Benyei    QualType ty = parm->getType();
3511db7040604154f71c54cf1329ae384fbef196668Guy Benyei    std::string typeQuals;
3521db7040604154f71c54cf1329ae384fbef196668Guy Benyei
3531db7040604154f71c54cf1329ae384fbef196668Guy Benyei    if (ty->isPointerType()) {
3541db7040604154f71c54cf1329ae384fbef196668Guy Benyei      QualType pointeeTy = ty->getPointeeType();
3551db7040604154f71c54cf1329ae384fbef196668Guy Benyei
3561db7040604154f71c54cf1329ae384fbef196668Guy Benyei      // Get address qualifier.
3571db7040604154f71c54cf1329ae384fbef196668Guy Benyei      addressQuals.push_back(Builder.getInt32(ASTCtx.getTargetAddressSpace(
3581db7040604154f71c54cf1329ae384fbef196668Guy Benyei        pointeeTy.getAddressSpace())));
3591db7040604154f71c54cf1329ae384fbef196668Guy Benyei
3601db7040604154f71c54cf1329ae384fbef196668Guy Benyei      // Get argument type name.
3611db7040604154f71c54cf1329ae384fbef196668Guy Benyei      std::string typeName = pointeeTy.getUnqualifiedType().getAsString() + "*";
3621db7040604154f71c54cf1329ae384fbef196668Guy Benyei
3631db7040604154f71c54cf1329ae384fbef196668Guy Benyei      // Turn "unsigned type" to "utype"
3641db7040604154f71c54cf1329ae384fbef196668Guy Benyei      std::string::size_type pos = typeName.find("unsigned");
365f4492448a201c352be3d2e1e76220cf7cd499c55Benjamin Kramer      if (pos != std::string::npos)
366f4492448a201c352be3d2e1e76220cf7cd499c55Benjamin Kramer        typeName.erase(pos+1, 8);
3671db7040604154f71c54cf1329ae384fbef196668Guy Benyei
3681db7040604154f71c54cf1329ae384fbef196668Guy Benyei      argTypeNames.push_back(llvm::MDString::get(Context, typeName));
3691db7040604154f71c54cf1329ae384fbef196668Guy Benyei
3701db7040604154f71c54cf1329ae384fbef196668Guy Benyei      // Get argument type qualifiers:
3711db7040604154f71c54cf1329ae384fbef196668Guy Benyei      if (ty.isRestrictQualified())
3721db7040604154f71c54cf1329ae384fbef196668Guy Benyei        typeQuals = "restrict";
3731db7040604154f71c54cf1329ae384fbef196668Guy Benyei      if (pointeeTy.isConstQualified() ||
3741db7040604154f71c54cf1329ae384fbef196668Guy Benyei          (pointeeTy.getAddressSpace() == LangAS::opencl_constant))
375f4492448a201c352be3d2e1e76220cf7cd499c55Benjamin Kramer        typeQuals += typeQuals.empty() ? "const" : " const";
3761db7040604154f71c54cf1329ae384fbef196668Guy Benyei      if (pointeeTy.isVolatileQualified())
377f4492448a201c352be3d2e1e76220cf7cd499c55Benjamin Kramer        typeQuals += typeQuals.empty() ? "volatile" : " volatile";
3781db7040604154f71c54cf1329ae384fbef196668Guy Benyei    } else {
3791db7040604154f71c54cf1329ae384fbef196668Guy Benyei      addressQuals.push_back(Builder.getInt32(0));
3801db7040604154f71c54cf1329ae384fbef196668Guy Benyei
3811db7040604154f71c54cf1329ae384fbef196668Guy Benyei      // Get argument type name.
3821db7040604154f71c54cf1329ae384fbef196668Guy Benyei      std::string typeName = ty.getUnqualifiedType().getAsString();
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.isConstQualified())
3931db7040604154f71c54cf1329ae384fbef196668Guy Benyei        typeQuals = "const";
3941db7040604154f71c54cf1329ae384fbef196668Guy Benyei      if (ty.isVolatileQualified())
395f4492448a201c352be3d2e1e76220cf7cd499c55Benjamin Kramer        typeQuals += typeQuals.empty() ? "volatile" : " volatile";
3961db7040604154f71c54cf1329ae384fbef196668Guy Benyei    }
3971db7040604154f71c54cf1329ae384fbef196668Guy Benyei
3981db7040604154f71c54cf1329ae384fbef196668Guy Benyei    argTypeQuals.push_back(llvm::MDString::get(Context, typeQuals));
3991db7040604154f71c54cf1329ae384fbef196668Guy Benyei
4001db7040604154f71c54cf1329ae384fbef196668Guy Benyei    // Get image access qualifier:
4011db7040604154f71c54cf1329ae384fbef196668Guy Benyei    if (ty->isImageType()) {
4021db7040604154f71c54cf1329ae384fbef196668Guy Benyei      if (parm->hasAttr<OpenCLImageAccessAttr>() &&
4031db7040604154f71c54cf1329ae384fbef196668Guy Benyei          parm->getAttr<OpenCLImageAccessAttr>()->getAccess() == CLIA_write_only)
4041db7040604154f71c54cf1329ae384fbef196668Guy Benyei        accessQuals.push_back(llvm::MDString::get(Context, "write_only"));
4051db7040604154f71c54cf1329ae384fbef196668Guy Benyei      else
4061db7040604154f71c54cf1329ae384fbef196668Guy Benyei        accessQuals.push_back(llvm::MDString::get(Context, "read_only"));
4071db7040604154f71c54cf1329ae384fbef196668Guy Benyei    } else
4081db7040604154f71c54cf1329ae384fbef196668Guy Benyei      accessQuals.push_back(llvm::MDString::get(Context, "none"));
4099dce4b3d9d14ba2be2aad5bd7fd8d381a0263c36Michael Ilseman
410198871cc90375246d8692680467ff6e810edac36Tanya Lattner    // Get argument name.
411198871cc90375246d8692680467ff6e810edac36Tanya Lattner    argNames.push_back(llvm::MDString::get(Context, parm->getName()));
412198871cc90375246d8692680467ff6e810edac36Tanya Lattner  }
4131db7040604154f71c54cf1329ae384fbef196668Guy Benyei
4141db7040604154f71c54cf1329ae384fbef196668Guy Benyei  kernelMDArgs.push_back(llvm::MDNode::get(Context, addressQuals));
4151db7040604154f71c54cf1329ae384fbef196668Guy Benyei  kernelMDArgs.push_back(llvm::MDNode::get(Context, accessQuals));
4161db7040604154f71c54cf1329ae384fbef196668Guy Benyei  kernelMDArgs.push_back(llvm::MDNode::get(Context, argTypeNames));
4171db7040604154f71c54cf1329ae384fbef196668Guy Benyei  kernelMDArgs.push_back(llvm::MDNode::get(Context, argTypeQuals));
418198871cc90375246d8692680467ff6e810edac36Tanya Lattner  kernelMDArgs.push_back(llvm::MDNode::get(Context, argNames));
419198871cc90375246d8692680467ff6e810edac36Tanya Lattner}
420198871cc90375246d8692680467ff6e810edac36Tanya Lattner
4219dce4b3d9d14ba2be2aad5bd7fd8d381a0263c36Michael Ilsemanvoid CodeGenFunction::EmitOpenCLKernelMetadata(const FunctionDecl *FD,
4220df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner                                               llvm::Function *Fn)
4230df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner{
4240df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner  if (!FD->hasAttr<OpenCLKernelAttr>())
4250df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner    return;
4260df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner
4270df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner  llvm::LLVMContext &Context = getLLVMContext();
4280df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner
429cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko  SmallVector <llvm::Value*, 5> kernelMDArgs;
4300df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner  kernelMDArgs.push_back(Fn);
4310df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner
432198871cc90375246d8692680467ff6e810edac36Tanya Lattner  if (CGM.getCodeGenOpts().EmitOpenCLArgMetadata)
4331db7040604154f71c54cf1329ae384fbef196668Guy Benyei    GenOpenCLArgMetadata(FD, Fn, CGM, Context, kernelMDArgs,
4341db7040604154f71c54cf1329ae384fbef196668Guy Benyei                         Builder, getContext());
4359dce4b3d9d14ba2be2aad5bd7fd8d381a0263c36Michael Ilseman
43637453b9580e293eef3bd60bd36047a93ac4515b1Joey Gouly  if (FD->hasAttr<VecTypeHintAttr>()) {
43737453b9580e293eef3bd60bd36047a93ac4515b1Joey Gouly    VecTypeHintAttr *attr = FD->getAttr<VecTypeHintAttr>();
43837453b9580e293eef3bd60bd36047a93ac4515b1Joey Gouly    QualType hintQTy = attr->getTypeHint();
43937453b9580e293eef3bd60bd36047a93ac4515b1Joey Gouly    const ExtVectorType *hintEltQTy = hintQTy->getAs<ExtVectorType>();
44037453b9580e293eef3bd60bd36047a93ac4515b1Joey Gouly    bool isSignedInteger =
44137453b9580e293eef3bd60bd36047a93ac4515b1Joey Gouly        hintQTy->isSignedIntegerType() ||
44237453b9580e293eef3bd60bd36047a93ac4515b1Joey Gouly        (hintEltQTy && hintEltQTy->getElementType()->isSignedIntegerType());
44337453b9580e293eef3bd60bd36047a93ac4515b1Joey Gouly    llvm::Value *attrMDArgs[] = {
44437453b9580e293eef3bd60bd36047a93ac4515b1Joey Gouly      llvm::MDString::get(Context, "vec_type_hint"),
44537453b9580e293eef3bd60bd36047a93ac4515b1Joey Gouly      llvm::UndefValue::get(CGM.getTypes().ConvertType(attr->getTypeHint())),
44637453b9580e293eef3bd60bd36047a93ac4515b1Joey Gouly      llvm::ConstantInt::get(
44737453b9580e293eef3bd60bd36047a93ac4515b1Joey Gouly          llvm::IntegerType::get(Context, 32),
44837453b9580e293eef3bd60bd36047a93ac4515b1Joey Gouly          llvm::APInt(32, (uint64_t)(isSignedInteger ? 1 : 0)))
44937453b9580e293eef3bd60bd36047a93ac4515b1Joey Gouly    };
45037453b9580e293eef3bd60bd36047a93ac4515b1Joey Gouly    kernelMDArgs.push_back(llvm::MDNode::get(Context, attrMDArgs));
45137453b9580e293eef3bd60bd36047a93ac4515b1Joey Gouly  }
45237453b9580e293eef3bd60bd36047a93ac4515b1Joey Gouly
4530df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner  if (FD->hasAttr<WorkGroupSizeHintAttr>()) {
4540df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner    WorkGroupSizeHintAttr *attr = FD->getAttr<WorkGroupSizeHintAttr>();
4558fd8ee9b718a7d6ebbcc382c1de39ed1260fe2d3Benjamin Kramer    llvm::Value *attrMDArgs[] = {
4568fd8ee9b718a7d6ebbcc382c1de39ed1260fe2d3Benjamin Kramer      llvm::MDString::get(Context, "work_group_size_hint"),
4578fd8ee9b718a7d6ebbcc382c1de39ed1260fe2d3Benjamin Kramer      Builder.getInt32(attr->getXDim()),
4588fd8ee9b718a7d6ebbcc382c1de39ed1260fe2d3Benjamin Kramer      Builder.getInt32(attr->getYDim()),
4598fd8ee9b718a7d6ebbcc382c1de39ed1260fe2d3Benjamin Kramer      Builder.getInt32(attr->getZDim())
4608fd8ee9b718a7d6ebbcc382c1de39ed1260fe2d3Benjamin Kramer    };
4610df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner    kernelMDArgs.push_back(llvm::MDNode::get(Context, attrMDArgs));
4620df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner  }
4630df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner
4640df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner  if (FD->hasAttr<ReqdWorkGroupSizeAttr>()) {
4650df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner    ReqdWorkGroupSizeAttr *attr = FD->getAttr<ReqdWorkGroupSizeAttr>();
4668fd8ee9b718a7d6ebbcc382c1de39ed1260fe2d3Benjamin Kramer    llvm::Value *attrMDArgs[] = {
4678fd8ee9b718a7d6ebbcc382c1de39ed1260fe2d3Benjamin Kramer      llvm::MDString::get(Context, "reqd_work_group_size"),
4688fd8ee9b718a7d6ebbcc382c1de39ed1260fe2d3Benjamin Kramer      Builder.getInt32(attr->getXDim()),
4698fd8ee9b718a7d6ebbcc382c1de39ed1260fe2d3Benjamin Kramer      Builder.getInt32(attr->getYDim()),
4708fd8ee9b718a7d6ebbcc382c1de39ed1260fe2d3Benjamin Kramer      Builder.getInt32(attr->getZDim())
4718fd8ee9b718a7d6ebbcc382c1de39ed1260fe2d3Benjamin Kramer    };
4720df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner    kernelMDArgs.push_back(llvm::MDNode::get(Context, attrMDArgs));
4730df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner  }
4740df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner
4750df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner  llvm::MDNode *kernelMDNode = llvm::MDNode::get(Context, kernelMDArgs);
4760df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner  llvm::NamedMDNode *OpenCLKernelMetadata =
4770df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner    CGM.getModule().getOrInsertNamedMetadata("opencl.kernels");
4780df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner  OpenCLKernelMetadata->addOperand(kernelMDNode);
4790df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner}
4800df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner
481f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCallvoid CodeGenFunction::StartFunction(GlobalDecl GD,
482f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall                                    QualType RetTy,
4837c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar                                    llvm::Function *Fn,
484d26bc76c98006609002d9930f8840490e88ac5b5John McCall                                    const CGFunctionInfo &FnInfo,
4852284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar                                    const FunctionArgList &Args,
4869c6082fe89c61af697f017aa80937581cc2128d8Tilmann Scheller                                    SourceLocation StartLoc) {
4870ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlsson  const Decl *D = GD.getDecl();
4889dce4b3d9d14ba2be2aad5bd7fd8d381a0263c36Michael Ilseman
4894cc1a4703363ff940b6273aeef9d96a87edeb04bAnders Carlsson  DidCallStackSave = false;
490f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall  CurCodeDecl = D;
491f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall  CurFuncDecl = (D ? D->getNonClosureContext() : 0);
4927c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar  FnRetTy = RetTy;
493bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  CurFn = Fn;
494d26bc76c98006609002d9930f8840490e88ac5b5John McCall  CurFnInfo = &FnInfo;
4955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(CurFn->isDeclaration() && "Function already has body?");
496ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner
4974f45bc099f2665bc6e4bcbb169aa452390dbf3feWill Dietz  if (CGM.getSanitizerBlacklist().isIn(*Fn)) {
4984f45bc099f2665bc6e4bcbb169aa452390dbf3feWill Dietz    SanOpts = &SanitizerOptions::Disabled;
4994f45bc099f2665bc6e4bcbb169aa452390dbf3feWill Dietz    SanitizePerformTypeCheck = false;
5004f45bc099f2665bc6e4bcbb169aa452390dbf3feWill Dietz  }
5014f45bc099f2665bc6e4bcbb169aa452390dbf3feWill Dietz
502a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen  // Pass inline keyword to optimizer if it appears explicitly on any
503a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen  // declaration.
5049dce4b3d9d14ba2be2aad5bd7fd8d381a0263c36Michael Ilseman  if (!CGM.getCodeGenOpts().NoInline)
5058fbe3855db0c341964bb550e13659505efe06c43Chad Rosier    if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
5068fbe3855db0c341964bb550e13659505efe06c43Chad Rosier      for (FunctionDecl::redecl_iterator RI = FD->redecls_begin(),
5078fbe3855db0c341964bb550e13659505efe06c43Chad Rosier             RE = FD->redecls_end(); RI != RE; ++RI)
5088fbe3855db0c341964bb550e13659505efe06c43Chad Rosier        if (RI->isInlineSpecified()) {
50972390b39c545426023ec104afe8706395d732badBill Wendling          Fn->addFnAttr(llvm::Attribute::InlineHint);
5108fbe3855db0c341964bb550e13659505efe06c43Chad Rosier          break;
5118fbe3855db0c341964bb550e13659505efe06c43Chad Rosier        }
512a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen
5137edf9e38b91917b661277601c0e448eef0eb2b56Richard Smith  if (getLangOpts().OpenCL) {
514f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne    // Add metadata for a kernel function.
515f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne    if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
5160df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner      EmitOpenCLKernelMetadata(FD, Fn);
517f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne  }
518f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne
51955e874299f2ad827646a4ca9ea38c402aaeb38c9Daniel Dunbar  llvm::BasicBlock *EntryBB = createBasicBlock("entry", CurFn);
5205ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar
5215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Create a marker to make it easy to insert allocas into the entryblock
52255352a2d616cf9fbb621d10faf8b960b4b268bd8Chris Lattner  // later.  Don't create this with the builder, because we don't want it
52355352a2d616cf9fbb621d10faf8b960b4b268bd8Chris Lattner  // folded.
52477b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner  llvm::Value *Undef = llvm::UndefValue::get(Int32Ty);
52577b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner  AllocaInsertPt = new llvm::BitCastInst(Undef, Int32Ty, "", EntryBB);
526f1466848dce9c4c75d96a6cabdc8db560e26aac8Chris Lattner  if (Builder.isNamePreserving())
527f1466848dce9c4c75d96a6cabdc8db560e26aac8Chris Lattner    AllocaInsertPt->setName("allocapt");
5281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
529f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  ReturnBlock = getJumpDestInCurrentScope("return");
5301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
53155352a2d616cf9fbb621d10faf8b960b4b268bd8Chris Lattner  Builder.SetInsertPoint(EntryBB);
5321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
533af99417156c652a6f04dff643925036dc3241d60Sanjiv Gupta  // Emit subprogram debug descriptor.
534e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson  if (CGDebugInfo *DI = getDebugInfo()) {
535bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose    SmallVector<QualType, 16> ArgTypes;
53606253664315307d34ab57b892b6a0c6c5b3153bbEric Christopher    for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
53706253664315307d34ab57b892b6a0c6c5b3153bbEric Christopher	 i != e; ++i) {
538bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose      ArgTypes.push_back((*i)->getType());
53906253664315307d34ab57b892b6a0c6c5b3153bbEric Christopher    }
54006253664315307d34ab57b892b6a0c6c5b3153bbEric Christopher
541e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    QualType FnType =
542bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose      getContext().getFunctionType(RetTy, ArgTypes,
543e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall                                   FunctionProtoType::ExtProtoInfo());
544e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
5452284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar    DI->setLocation(StartLoc);
5469c6c3a0e3ae09626d2d4b04e4ffa42c3d7cab32bDevang Patel    DI->EmitFunctionStart(GD, FnType, CurFn, Builder);
547af99417156c652a6f04dff643925036dc3241d60Sanjiv Gupta  }
548af99417156c652a6f04dff643925036dc3241d60Sanjiv Gupta
549a18652fe1e8233fbf8b67484945c7f7b2bf272beDaniel Dunbar  if (ShouldInstrumentFunction())
550a18652fe1e8233fbf8b67484945c7f7b2bf272beDaniel Dunbar    EmitFunctionInstrumentation("__cyg_profile_func_enter");
5517255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner
552be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky  if (CGM.getCodeGenOpts().InstrumentForProfiling)
553be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky    EmitMCountInstrumentation();
554be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky
555b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman  if (RetTy->isVoidType()) {
556b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman    // Void type; nothing to return.
557b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman    ReturnValue = 0;
558b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman  } else if (CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect &&
5599d232c884ea9872d6555df0fd7359699819bc1f1John McCall             !hasScalarEvaluationKind(CurFnInfo->getReturnType())) {
560b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman    // Indirect aggregate return; emit returned value directly into sret slot.
561647a1ec397fa13af176d07d9f5d071560a94c7a9Daniel Dunbar    // This reduces code size, and affects correctness in C++.
562b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman    ReturnValue = CurFn->arg_begin();
563b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman  } else {
564647a1ec397fa13af176d07d9f5d071560a94c7a9Daniel Dunbar    ReturnValue = CreateIRTemp(RetTy, "retval");
565f85e193739c953358c865005855253af4f68a497John McCall
566f85e193739c953358c865005855253af4f68a497John McCall    // Tell the epilog emitter to autorelease the result.  We do this
567f85e193739c953358c865005855253af4f68a497John McCall    // now so that various specialized functions can suppress it
568f85e193739c953358c865005855253af4f68a497John McCall    // during their IR-generation.
5694e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (getLangOpts().ObjCAutoRefCount &&
570f85e193739c953358c865005855253af4f68a497John McCall        !CurFnInfo->isReturnsRetained() &&
571f85e193739c953358c865005855253af4f68a497John McCall        RetTy->isObjCRetainableType())
572f85e193739c953358c865005855253af4f68a497John McCall      AutoreleaseResult = true;
573b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman  }
574b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman
575cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump  EmitStartEHSpec(CurCodeDecl);
576f85e193739c953358c865005855253af4f68a497John McCall
577f85e193739c953358c865005855253af4f68a497John McCall  PrologueCleanupDepth = EHStack.stable_begin();
57888b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar  EmitFunctionProlog(*CurFnInfo, CurFn, Args);
5791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
580cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman  if (D && isa<CXXMethodDecl>(D) && cast<CXXMethodDecl>(D)->isInstance()) {
5814c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    CGM.getCXXABI().EmitInstanceFunctionProlog(*this);
582cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman    const CXXMethodDecl *MD = cast<CXXMethodDecl>(D);
583cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman    if (MD->getParent()->isLambda() &&
584cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman        MD->getOverloadedOperator() == OO_Call) {
585cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman      // We're in a lambda; figure out the captures.
586cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman      MD->getParent()->getCaptureFields(LambdaCaptureFields,
587cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman                                        LambdaThisCaptureField);
588cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman      if (LambdaThisCaptureField) {
589cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman        // If this lambda captures this, load it.
590f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall        LValue ThisLValue = EmitLValueForLambdaField(LambdaThisCaptureField);
591cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman        CXXThisValue = EmitLoadOfLValue(ThisLValue).getScalarVal();
592cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman      }
593cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman    } else {
594cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman      // Not in a lambda; just use 'this' from the method.
595cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman      // FIXME: Should we generate a new load for each use of 'this'?  The
596cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman      // fast register allocator would be happier...
597cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman      CXXThisValue = CXXABIThisValue;
598cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman    }
599cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman  }
6002504941793b549323f9d29c62507cf21d865fadeJohn McCall
601751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson  // If any of the arguments have a variably modified type, make sure to
602751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson  // emit the type size.
603751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson  for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
604751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson       i != e; ++i) {
605f5bf912767ba19832bba7694fc679a5ec3c055ffEli Friedman    const VarDecl *VD = *i;
606f5bf912767ba19832bba7694fc679a5ec3c055ffEli Friedman
607f5bf912767ba19832bba7694fc679a5ec3c055ffEli Friedman    // Dig out the type as written from ParmVarDecls; it's unclear whether
608f5bf912767ba19832bba7694fc679a5ec3c055ffEli Friedman    // the standard (C99 6.9.1p10) requires this, but we're following the
609f5bf912767ba19832bba7694fc679a5ec3c055ffEli Friedman    // precedent set by gcc.
610f5bf912767ba19832bba7694fc679a5ec3c055ffEli Friedman    QualType Ty;
611f5bf912767ba19832bba7694fc679a5ec3c055ffEli Friedman    if (const ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(VD))
612f5bf912767ba19832bba7694fc679a5ec3c055ffEli Friedman      Ty = PVD->getOriginalType();
613f5bf912767ba19832bba7694fc679a5ec3c055ffEli Friedman    else
614f5bf912767ba19832bba7694fc679a5ec3c055ffEli Friedman      Ty = VD->getType();
615751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson
616751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson    if (Ty->isVariablyModifiedType())
617bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      EmitVariablyModifiedType(Ty);
618751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson  }
61973fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher  // Emit a location at the end of the prologue.
62073fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher  if (CGDebugInfo *DI = getDebugInfo())
62173fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher    DI->EmitLocation(Builder, StartLoc);
6227c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar}
623eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman
6249fc6a7774643a810c8501dae2323e863fefb623eJohn McCallvoid CodeGenFunction::EmitFunctionBody(FunctionArgList &Args) {
6259fc6a7774643a810c8501dae2323e863fefb623eJohn McCall  const FunctionDecl *FD = cast<FunctionDecl>(CurGD.getDecl());
62606a9f3680d22529a2fcf20c52d71cf221d99d910Douglas Gregor  assert(FD->getBody());
627a6504853d297c30cfa271f4710af5a3d5db59449David Blaikie  if (const CompoundStmt *S = dyn_cast<CompoundStmt>(FD->getBody()))
628a6504853d297c30cfa271f4710af5a3d5db59449David Blaikie    EmitCompoundStmtWithoutScope(*S);
629a6504853d297c30cfa271f4710af5a3d5db59449David Blaikie  else
630a6504853d297c30cfa271f4710af5a3d5db59449David Blaikie    EmitStmt(FD->getBody());
631a355e07454463b19829ac92ffd115a097faff0e0John McCall}
632a355e07454463b19829ac92ffd115a097faff0e0John McCall
63339dad53772c42eb36ebec1c81c56ba99d038fb94John McCall/// Tries to mark the given function nounwind based on the
63439dad53772c42eb36ebec1c81c56ba99d038fb94John McCall/// non-existence of any throwing calls within it.  We believe this is
63539dad53772c42eb36ebec1c81c56ba99d038fb94John McCall/// lightweight enough to do at -O0.
63639dad53772c42eb36ebec1c81c56ba99d038fb94John McCallstatic void TryMarkNoThrow(llvm::Function *F) {
637b3a29f132794f67108bccc9c7cc3795365e8a965John McCall  // LLVM treats 'nounwind' on a function as part of the type, so we
638b3a29f132794f67108bccc9c7cc3795365e8a965John McCall  // can't do this on functions that can be overwritten.
639b3a29f132794f67108bccc9c7cc3795365e8a965John McCall  if (F->mayBeOverridden()) return;
640b3a29f132794f67108bccc9c7cc3795365e8a965John McCall
64139dad53772c42eb36ebec1c81c56ba99d038fb94John McCall  for (llvm::Function::iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI)
64239dad53772c42eb36ebec1c81c56ba99d038fb94John McCall    for (llvm::BasicBlock::iterator
64339dad53772c42eb36ebec1c81c56ba99d038fb94John McCall           BI = FI->begin(), BE = FI->end(); BI != BE; ++BI)
644285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling      if (llvm::CallInst *Call = dyn_cast<llvm::CallInst>(&*BI)) {
64539dad53772c42eb36ebec1c81c56ba99d038fb94John McCall        if (!Call->doesNotThrow())
64639dad53772c42eb36ebec1c81c56ba99d038fb94John McCall          return;
647285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling      } else if (isa<llvm::ResumeInst>(&*BI)) {
648285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling        return;
649285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling      }
650fac631052809b59c1b66f687c08a743de7fb50e9Bill Wendling  F->setDoesNotThrow();
65139dad53772c42eb36ebec1c81c56ba99d038fb94John McCall}
65239dad53772c42eb36ebec1c81c56ba99d038fb94John McCall
653d26bc76c98006609002d9930f8840490e88ac5b5John McCallvoid CodeGenFunction::GenerateCode(GlobalDecl GD, llvm::Function *Fn,
654d26bc76c98006609002d9930f8840490e88ac5b5John McCall                                   const CGFunctionInfo &FnInfo) {
6550ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlsson  const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
6569dce4b3d9d14ba2be2aad5bd7fd8d381a0263c36Michael Ilseman
657e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson  // Check if we should generate debug info for this function.
658a240df2ec1b374b3e9e7f760875ffb17cd64506fAlexey Samsonov  if (!FD->hasAttr<NoDebugAttr>())
659a240df2ec1b374b3e9e7f760875ffb17cd64506fAlexey Samsonov    maybeInitializeDebugInfo();
6601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6617c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar  FunctionArgList Args;
6624c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  QualType ResTy = FD->getResultType();
6631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6646a1e0eb557d47e85185e09bdf8721f53f4bf9c9cMike Stump  CurGD = GD;
6654c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  if (isa<CXXMethodDecl>(FD) && cast<CXXMethodDecl>(FD)->isInstance())
6664c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    CGM.getCXXABI().BuildInstanceFunctionParams(*this, ResTy, Args);
6671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6686e94f6c0f124c9a88b3dae0eea5e6b27957df183Chad Rosier  for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i)
6696e94f6c0f124c9a88b3dae0eea5e6b27957df183Chad Rosier    Args.push_back(FD->getParamDecl(i));
670af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
671a355e07454463b19829ac92ffd115a097faff0e0John McCall  SourceRange BodyRange;
672a355e07454463b19829ac92ffd115a097faff0e0John McCall  if (Stmt *Body = FD->getBody()) BodyRange = Body->getSourceRange();
6731c3db769249a633108dc320da87c48a432e2bcc1Adrian Prantl  CurEHLocation = BodyRange.getEnd();
6744365bba95be15342575b4f030c6583a770a5da3dAnders Carlsson
67563fd408a61ae9b94e8d8a986832f526f7cdbfa84Manman Ren  // CalleeWithThisReturn keeps track of the last callee inside this function
67663fd408a61ae9b94e8d8a986832f526f7cdbfa84Manman Ren  // that returns 'this'. Before starting the function, we set it to null.
67763fd408a61ae9b94e8d8a986832f526f7cdbfa84Manman Ren  CalleeWithThisReturn = 0;
67863fd408a61ae9b94e8d8a986832f526f7cdbfa84Manman Ren
679a355e07454463b19829ac92ffd115a097faff0e0John McCall  // Emit the standard function prologue.
680d26bc76c98006609002d9930f8840490e88ac5b5John McCall  StartFunction(GD, ResTy, Fn, FnInfo, Args, BodyRange.getBegin());
6811851a12605bc6f1ea70d11974a315340ebaab6ebAnders Carlsson
682a355e07454463b19829ac92ffd115a097faff0e0John McCall  // Generate the body of the function.
6839fc6a7774643a810c8501dae2323e863fefb623eJohn McCall  if (isa<CXXDestructorDecl>(FD))
6849fc6a7774643a810c8501dae2323e863fefb623eJohn McCall    EmitDestructorBody(Args);
6859fc6a7774643a810c8501dae2323e863fefb623eJohn McCall  else if (isa<CXXConstructorDecl>(FD))
6869fc6a7774643a810c8501dae2323e863fefb623eJohn McCall    EmitConstructorBody(Args);
6877edf9e38b91917b661277601c0e448eef0eb2b56Richard Smith  else if (getLangOpts().CUDA &&
688a4ae2294b6ebfb2554aacb6a6a0682fb5ed1f276Peter Collingbourne           !CGM.getCodeGenOpts().CUDAIsDevice &&
689a4ae2294b6ebfb2554aacb6a6a0682fb5ed1f276Peter Collingbourne           FD->hasAttr<CUDAGlobalAttr>())
690a4ae2294b6ebfb2554aacb6a6a0682fb5ed1f276Peter Collingbourne    CGM.getCUDARuntime().EmitDeviceStubBody(*this, Args);
691bd89f8c2caa9550e41daa1aa9bf30f0f1e0dfaf7Eli Friedman  else if (isa<CXXConversionDecl>(FD) &&
69227dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor           cast<CXXConversionDecl>(FD)->isLambdaToBlockPointerConversion()) {
69327dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor    // The lambda conversion to block pointer is special; the semantics can't be
69427dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor    // expressed in the AST, so IRGen needs to special-case it.
69527dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor    EmitLambdaToBlockPointerBody(Args);
69627dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor  } else if (isa<CXXMethodDecl>(FD) &&
69727dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor             cast<CXXMethodDecl>(FD)->isLambdaStaticInvoker()) {
69827dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor    // The lambda "__invoke" function is special, because it forwards or
69927dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor    // clones the body of the function call operator (but is actually static).
70027dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor    EmitLambdaStaticInvokeFunction(cast<CXXMethodDecl>(FD));
70156c00c4868831c9a137ca7b0e16d063cf986d110Lang Hames  } else if (FD->isDefaulted() && isa<CXXMethodDecl>(FD) &&
70256c00c4868831c9a137ca7b0e16d063cf986d110Lang Hames             cast<CXXMethodDecl>(FD)->isCopyAssignmentOperator()) {
70356c00c4868831c9a137ca7b0e16d063cf986d110Lang Hames    // Implicit copy-assignment gets the same special treatment as implicit
70456c00c4868831c9a137ca7b0e16d063cf986d110Lang Hames    // copy-constructors.
70556c00c4868831c9a137ca7b0e16d063cf986d110Lang Hames    emitImplicitAssignmentOperatorBody(Args);
706bd89f8c2caa9550e41daa1aa9bf30f0f1e0dfaf7Eli Friedman  }
7079fc6a7774643a810c8501dae2323e863fefb623eJohn McCall  else
7089fc6a7774643a810c8501dae2323e863fefb623eJohn McCall    EmitFunctionBody(Args);
709c33e4ba20304e222692e77f2c6ad26a5d8d32f83Anders Carlsson
71036ef0d54cfddf31cd48816e78ab4db73b31a6c1dRichard Smith  // C++11 [stmt.return]p2:
71136ef0d54cfddf31cd48816e78ab4db73b31a6c1dRichard Smith  //   Flowing off the end of a function [...] results in undefined behavior in
71236ef0d54cfddf31cd48816e78ab4db73b31a6c1dRichard Smith  //   a value-returning function.
71336ef0d54cfddf31cd48816e78ab4db73b31a6c1dRichard Smith  // C11 6.9.1p12:
71436ef0d54cfddf31cd48816e78ab4db73b31a6c1dRichard Smith  //   If the '}' that terminates a function is reached, and the value of the
71536ef0d54cfddf31cd48816e78ab4db73b31a6c1dRichard Smith  //   function call is used by the caller, the behavior is undefined.
7167edf9e38b91917b661277601c0e448eef0eb2b56Richard Smith  if (getLangOpts().CPlusPlus && !FD->hasImplicitReturnZero() &&
71736ef0d54cfddf31cd48816e78ab4db73b31a6c1dRichard Smith      !FD->getResultType()->isVoidType() && Builder.GetInsertBlock()) {
7184f45bc099f2665bc6e4bcbb169aa452390dbf3feWill Dietz    if (SanOpts->Return)
7194def70d3040e73707c738f7c366737a986135edfRichard Smith      EmitCheck(Builder.getFalse(), "missing_return",
7204def70d3040e73707c738f7c366737a986135edfRichard Smith                EmitCheckSourceLocation(FD->getLocation()),
721cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko                ArrayRef<llvm::Value *>(), CRK_Unrecoverable);
722802cd5b1974bab2038b9d3c969d9beb0ab34f435Richard Smith    else if (CGM.getCodeGenOpts().OptimizationLevel == 0)
723802cd5b1974bab2038b9d3c969d9beb0ab34f435Richard Smith      Builder.CreateCall(CGM.getIntrinsic(llvm::Intrinsic::trap));
72436ef0d54cfddf31cd48816e78ab4db73b31a6c1dRichard Smith    Builder.CreateUnreachable();
72536ef0d54cfddf31cd48816e78ab4db73b31a6c1dRichard Smith    Builder.ClearInsertionPoint();
72636ef0d54cfddf31cd48816e78ab4db73b31a6c1dRichard Smith  }
72736ef0d54cfddf31cd48816e78ab4db73b31a6c1dRichard Smith
728a355e07454463b19829ac92ffd115a097faff0e0John McCall  // Emit the standard function epilogue.
729a355e07454463b19829ac92ffd115a097faff0e0John McCall  FinishFunction(BodyRange.getEnd());
73063fd408a61ae9b94e8d8a986832f526f7cdbfa84Manman Ren  // CalleeWithThisReturn keeps track of the last callee inside this function
73163fd408a61ae9b94e8d8a986832f526f7cdbfa84Manman Ren  // that returns 'this'. After finishing the function, we set it to null.
73263fd408a61ae9b94e8d8a986832f526f7cdbfa84Manman Ren  CalleeWithThisReturn = 0;
73339dad53772c42eb36ebec1c81c56ba99d038fb94John McCall
73439dad53772c42eb36ebec1c81c56ba99d038fb94John McCall  // If we haven't marked the function nothrow through other means, do
73539dad53772c42eb36ebec1c81c56ba99d038fb94John McCall  // a quick pass now to see if we can.
73639dad53772c42eb36ebec1c81c56ba99d038fb94John McCall  if (!CurFn->doesNotThrow())
73739dad53772c42eb36ebec1c81c56ba99d038fb94John McCall    TryMarkNoThrow(CurFn);
7385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
7395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7400946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner/// ContainsLabel - Return true if the statement contains a label in it.  If
7410946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner/// this statement is not executed normally, it not containing a label means
7420946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner/// that we can just remove the code.
7430946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattnerbool CodeGenFunction::ContainsLabel(const Stmt *S, bool IgnoreCaseStmts) {
7440946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // Null statement, not a label!
7450946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  if (S == 0) return false;
7461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7470946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // If this is a label, we have to emit the code, consider something like:
7480946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // if (0) {  ...  foo:  bar(); }  goto foo;
749ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  //
750ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  // TODO: If anyone cared, we could track __label__'s, since we know that you
751ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  // can't jump to one from outside their declared region.
7520946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  if (isa<LabelStmt>(S))
7530946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner    return true;
7549dce4b3d9d14ba2be2aad5bd7fd8d381a0263c36Michael Ilseman
7550946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // If this is a case/default statement, and we haven't seen a switch, we have
7560946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // to emit the code.
7570946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  if (isa<SwitchCase>(S) && !IgnoreCaseStmts)
7580946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner    return true;
7591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7600946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // If this is a switch statement, we want to ignore cases below it.
7610946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  if (isa<SwitchStmt>(S))
7620946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner    IgnoreCaseStmts = true;
7631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7640946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // Scan subexpressions for verboten labels.
7657502c1d3ce8bb97bcc4f7bebef507040bd93b26fJohn McCall  for (Stmt::const_child_range I = S->children(); I; ++I)
7660946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner    if (ContainsLabel(*I, IgnoreCaseStmts))
7670946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner      return true;
7681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7690946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  return false;
7700946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner}
7710946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner
772ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner/// containsBreak - Return true if the statement contains a break out of it.
773ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner/// If the statement (recursively) contains a switch or loop with a break
774ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner/// inside of it, this is fine.
775ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattnerbool CodeGenFunction::containsBreak(const Stmt *S) {
776ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  // Null statement, not a label!
777ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  if (S == 0) return false;
778ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner
779ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  // If this is a switch or loop that defines its own break scope, then we can
780ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  // include it and anything inside of it.
781ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || isa<DoStmt>(S) ||
782ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner      isa<ForStmt>(S))
7832bef7f5499541e3b68f114cc4d7d197e9a902fe7Chris Lattner    return false;
7849dce4b3d9d14ba2be2aad5bd7fd8d381a0263c36Michael Ilseman
7852bef7f5499541e3b68f114cc4d7d197e9a902fe7Chris Lattner  if (isa<BreakStmt>(S))
786ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner    return true;
7879dce4b3d9d14ba2be2aad5bd7fd8d381a0263c36Michael Ilseman
788ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  // Scan subexpressions for verboten breaks.
789ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  for (Stmt::const_child_range I = S->children(); I; ++I)
790ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner    if (containsBreak(*I))
791ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner      return true;
7929dce4b3d9d14ba2be2aad5bd7fd8d381a0263c36Michael Ilseman
793ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  return false;
794ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner}
795ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner
79631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
797c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner/// ConstantFoldsToSimpleInteger - If the specified expression does not fold
798c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner/// to a constant, or if it does but contains a label, return false.  If it
799c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner/// constant folds return true and set the boolean result in Result.
800c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattnerbool CodeGenFunction::ConstantFoldsToSimpleInteger(const Expr *Cond,
801c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner                                                   bool &ResultBool) {
802e1ecdc168175719d74e112bcacd4aae5e12d4631Richard Trieu  llvm::APSInt ResultInt;
803ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  if (!ConstantFoldsToSimpleInteger(Cond, ResultInt))
804ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner    return false;
8059dce4b3d9d14ba2be2aad5bd7fd8d381a0263c36Michael Ilseman
806ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  ResultBool = ResultInt.getBoolValue();
807ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  return true;
808ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner}
809ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner
810ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner/// ConstantFoldsToSimpleInteger - If the specified expression does not fold
811ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner/// to a constant, or if it does but contains a label, return false.  If it
812ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner/// constant folds return true and set the folded value.
813ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattnerbool CodeGenFunction::
814e1ecdc168175719d74e112bcacd4aae5e12d4631Richard TrieuConstantFoldsToSimpleInteger(const Expr *Cond, llvm::APSInt &ResultInt) {
81536bc14c3a1cf63ee306df5687ac8e85f924f8639Daniel Dunbar  // FIXME: Rename and handle conversion of other evaluatable things
81636bc14c3a1cf63ee306df5687ac8e85f924f8639Daniel Dunbar  // to bool.
81780d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  llvm::APSInt Int;
81880d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  if (!Cond->EvaluateAsInt(Int, getContext()))
819c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner    return false;  // Not foldable, not integer or not fully evaluatable.
82080d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith
82131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  if (CodeGenFunction::ContainsLabel(Cond))
822c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner    return false;  // Contains a label.
82380d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith
82480d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  ResultInt = Int;
825c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner  return true;
82631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner}
82731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
82831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
829ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner
83031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an if
83131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// statement) to the specified blocks.  Based on the condition, this might try
83231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// to simplify the codegen of the conditional based on the branch.
83331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner///
83431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattnervoid CodeGenFunction::EmitBranchOnBoolExpr(const Expr *Cond,
83531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner                                           llvm::BasicBlock *TrueBlock,
83631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner                                           llvm::BasicBlock *FalseBlock) {
837f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  Cond = Cond->IgnoreParens();
8381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
83931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  if (const BinaryOperator *CondBOp = dyn_cast<BinaryOperator>(Cond)) {
84031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner    // Handle X && Y in a condition.
8412de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    if (CondBOp->getOpcode() == BO_LAnd) {
84231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // If we have "1 && X", simplify the code.  "0 && X" would have constant
84331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // folded if the case was simple enough.
844e3eb83b93751544a5fab19b3824f56aeac454f82Bill Wendling      bool ConstantBool = false;
845c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner      if (ConstantFoldsToSimpleInteger(CondBOp->getLHS(), ConstantBool) &&
846c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner          ConstantBool) {
84731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        // br(1 && X) -> br(X).
84831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
84931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      }
8501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
85131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // If we have "X && 1", simplify the code to use an uncond branch.
85231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // "X && 0" would have been constant folded to 0.
853c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner      if (ConstantFoldsToSimpleInteger(CondBOp->getRHS(), ConstantBool) &&
854c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner          ConstantBool) {
85531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        // br(X && 1) -> br(X).
85631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock);
85731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      }
8581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
85931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // Emit the LHS as a conditional.  If the LHS conditional is false, we
86031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // want to jump to the FalseBlock.
8619615ecb44f549ae9fa2b4db6ff46bc78befbf62cDaniel Dunbar      llvm::BasicBlock *LHSTrue = createBasicBlock("land.lhs.true");
862150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
863150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      ConditionalEvaluation eval(*this);
86431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBranchOnBoolExpr(CondBOp->getLHS(), LHSTrue, FalseBlock);
86531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBlock(LHSTrue);
8661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
86708e9e453f40aff95a59bd67db49b8f050765e1f0Anders Carlsson      // Any temporaries created here are conditional.
868150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      eval.begin(*this);
86931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
870150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      eval.end(*this);
87108e9e453f40aff95a59bd67db49b8f050765e1f0Anders Carlsson
87231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      return;
873c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner    }
8749dce4b3d9d14ba2be2aad5bd7fd8d381a0263c36Michael Ilseman
875c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner    if (CondBOp->getOpcode() == BO_LOr) {
87631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // If we have "0 || X", simplify the code.  "1 || X" would have constant
87731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // folded if the case was simple enough.
878e3eb83b93751544a5fab19b3824f56aeac454f82Bill Wendling      bool ConstantBool = false;
879c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner      if (ConstantFoldsToSimpleInteger(CondBOp->getLHS(), ConstantBool) &&
880c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner          !ConstantBool) {
88131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        // br(0 || X) -> br(X).
88231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
88331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      }
8841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
88531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // If we have "X || 0", simplify the code to use an uncond branch.
88631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // "X || 1" would have been constant folded to 1.
887c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner      if (ConstantFoldsToSimpleInteger(CondBOp->getRHS(), ConstantBool) &&
888c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner          !ConstantBool) {
88931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        // br(X || 0) -> br(X).
89031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock);
89131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      }
8921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
89331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // Emit the LHS as a conditional.  If the LHS conditional is true, we
89431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // want to jump to the TrueBlock.
8959615ecb44f549ae9fa2b4db6ff46bc78befbf62cDaniel Dunbar      llvm::BasicBlock *LHSFalse = createBasicBlock("lor.lhs.false");
896150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
897150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      ConditionalEvaluation eval(*this);
89831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, LHSFalse);
89931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBlock(LHSFalse);
9001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
90108e9e453f40aff95a59bd67db49b8f050765e1f0Anders Carlsson      // Any temporaries created here are conditional.
902150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      eval.begin(*this);
90331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
904150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      eval.end(*this);
90508e9e453f40aff95a59bd67db49b8f050765e1f0Anders Carlsson
90631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      return;
90731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner    }
908552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner  }
9091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
910552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner  if (const UnaryOperator *CondUOp = dyn_cast<UnaryOperator>(Cond)) {
911552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner    // br(!x, t, f) -> br(x, f, t)
9122de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    if (CondUOp->getOpcode() == UO_LNot)
913552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner      return EmitBranchOnBoolExpr(CondUOp->getSubExpr(), FalseBlock, TrueBlock);
91431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  }
9151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
91609b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar  if (const ConditionalOperator *CondOp = dyn_cast<ConditionalOperator>(Cond)) {
917df33a35f6010fea92c786c0d8f85bfd7c73ebd3eEli Friedman    // br(c ? x : y, t, f) -> br(c, br(x, t, f), br(y, t, f))
918df33a35f6010fea92c786c0d8f85bfd7c73ebd3eEli Friedman    llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true");
919df33a35f6010fea92c786c0d8f85bfd7c73ebd3eEli Friedman    llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false");
92009b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar
921df33a35f6010fea92c786c0d8f85bfd7c73ebd3eEli Friedman    ConditionalEvaluation cond(*this);
922df33a35f6010fea92c786c0d8f85bfd7c73ebd3eEli Friedman    EmitBranchOnBoolExpr(CondOp->getCond(), LHSBlock, RHSBlock);
923150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
924df33a35f6010fea92c786c0d8f85bfd7c73ebd3eEli Friedman    cond.begin(*this);
925df33a35f6010fea92c786c0d8f85bfd7c73ebd3eEli Friedman    EmitBlock(LHSBlock);
926df33a35f6010fea92c786c0d8f85bfd7c73ebd3eEli Friedman    EmitBranchOnBoolExpr(CondOp->getLHS(), TrueBlock, FalseBlock);
927df33a35f6010fea92c786c0d8f85bfd7c73ebd3eEli Friedman    cond.end(*this);
928150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
929df33a35f6010fea92c786c0d8f85bfd7c73ebd3eEli Friedman    cond.begin(*this);
930df33a35f6010fea92c786c0d8f85bfd7c73ebd3eEli Friedman    EmitBlock(RHSBlock);
931df33a35f6010fea92c786c0d8f85bfd7c73ebd3eEli Friedman    EmitBranchOnBoolExpr(CondOp->getRHS(), TrueBlock, FalseBlock);
932df33a35f6010fea92c786c0d8f85bfd7c73ebd3eEli Friedman    cond.end(*this);
933150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
934df33a35f6010fea92c786c0d8f85bfd7c73ebd3eEli Friedman    return;
93509b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar  }
93609b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar
9374c71b8cded575b0cfc133c5da4502ca613982094Richard Smith  if (const CXXThrowExpr *Throw = dyn_cast<CXXThrowExpr>(Cond)) {
9384c71b8cded575b0cfc133c5da4502ca613982094Richard Smith    // Conditional operator handling can give us a throw expression as a
9394c71b8cded575b0cfc133c5da4502ca613982094Richard Smith    // condition for a case like:
9404c71b8cded575b0cfc133c5da4502ca613982094Richard Smith    //   br(c ? throw x : y, t, f) -> br(c, br(throw x, t, f), br(y, t, f)
9414c71b8cded575b0cfc133c5da4502ca613982094Richard Smith    // Fold this to:
9424c71b8cded575b0cfc133c5da4502ca613982094Richard Smith    //   br(c, throw x, br(y, t, f))
9434c71b8cded575b0cfc133c5da4502ca613982094Richard Smith    EmitCXXThrowExpr(Throw, /*KeepInsertionPoint*/false);
9444c71b8cded575b0cfc133c5da4502ca613982094Richard Smith    return;
9454c71b8cded575b0cfc133c5da4502ca613982094Richard Smith  }
9464c71b8cded575b0cfc133c5da4502ca613982094Richard Smith
94731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  // Emit the code with the fully general case.
94831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  llvm::Value *CondV = EvaluateExprAsBool(Cond);
94931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  Builder.CreateCondBr(CondV, TrueBlock, FalseBlock);
95031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner}
95131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
952488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar/// ErrorUnsupported - Print out an error that codegen doesn't support the
953dc5e8268292046114ffe02e48773572a91a310f1Chris Lattner/// specified stmt yet.
95490df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbarvoid CodeGenFunction::ErrorUnsupported(const Stmt *S, const char *Type,
95590df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar                                       bool OmitOnError) {
95690df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar  CGM.ErrorUnsupported(S, Type, OmitOnError);
957dc5e8268292046114ffe02e48773572a91a310f1Chris Lattner}
958dc5e8268292046114ffe02e48773572a91a310f1Chris Lattner
9597143325db76d6c3dabce82500f8cc7c93a941970John McCall/// emitNonZeroVLAInit - Emit the "zero" initialization of a
9607143325db76d6c3dabce82500f8cc7c93a941970John McCall/// variable-length array whose elements have a non-zero bit-pattern.
9617143325db76d6c3dabce82500f8cc7c93a941970John McCall///
9622ee5ba35febf830d366b65dd0dcbf8e291c41342James Dennett/// \param baseType the inner-most element type of the array
9637143325db76d6c3dabce82500f8cc7c93a941970John McCall/// \param src - a char* pointing to the bit-pattern for a single
9647143325db76d6c3dabce82500f8cc7c93a941970John McCall/// base element of the array
9657143325db76d6c3dabce82500f8cc7c93a941970John McCall/// \param sizeInChars - the total size of the VLA, in chars
9667143325db76d6c3dabce82500f8cc7c93a941970John McCallstatic void emitNonZeroVLAInit(CodeGenFunction &CGF, QualType baseType,
9679dce4b3d9d14ba2be2aad5bd7fd8d381a0263c36Michael Ilseman                               llvm::Value *dest, llvm::Value *src,
9687143325db76d6c3dabce82500f8cc7c93a941970John McCall                               llvm::Value *sizeInChars) {
9697143325db76d6c3dabce82500f8cc7c93a941970John McCall  std::pair<CharUnits,CharUnits> baseSizeAndAlign
9707143325db76d6c3dabce82500f8cc7c93a941970John McCall    = CGF.getContext().getTypeInfoInChars(baseType);
9717143325db76d6c3dabce82500f8cc7c93a941970John McCall
9727143325db76d6c3dabce82500f8cc7c93a941970John McCall  CGBuilderTy &Builder = CGF.Builder;
9737143325db76d6c3dabce82500f8cc7c93a941970John McCall
9747143325db76d6c3dabce82500f8cc7c93a941970John McCall  llvm::Value *baseSizeInChars
9757143325db76d6c3dabce82500f8cc7c93a941970John McCall    = llvm::ConstantInt::get(CGF.IntPtrTy, baseSizeAndAlign.first.getQuantity());
9767143325db76d6c3dabce82500f8cc7c93a941970John McCall
9772acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *i8p = Builder.getInt8PtrTy();
9787143325db76d6c3dabce82500f8cc7c93a941970John McCall
9797143325db76d6c3dabce82500f8cc7c93a941970John McCall  llvm::Value *begin = Builder.CreateBitCast(dest, i8p, "vla.begin");
9807143325db76d6c3dabce82500f8cc7c93a941970John McCall  llvm::Value *end = Builder.CreateInBoundsGEP(dest, sizeInChars, "vla.end");
9817143325db76d6c3dabce82500f8cc7c93a941970John McCall
9827143325db76d6c3dabce82500f8cc7c93a941970John McCall  llvm::BasicBlock *originBB = CGF.Builder.GetInsertBlock();
9837143325db76d6c3dabce82500f8cc7c93a941970John McCall  llvm::BasicBlock *loopBB = CGF.createBasicBlock("vla-init.loop");
9847143325db76d6c3dabce82500f8cc7c93a941970John McCall  llvm::BasicBlock *contBB = CGF.createBasicBlock("vla-init.cont");
9857143325db76d6c3dabce82500f8cc7c93a941970John McCall
9867143325db76d6c3dabce82500f8cc7c93a941970John McCall  // Make a loop over the VLA.  C99 guarantees that the VLA element
9877143325db76d6c3dabce82500f8cc7c93a941970John McCall  // count must be nonzero.
9887143325db76d6c3dabce82500f8cc7c93a941970John McCall  CGF.EmitBlock(loopBB);
9897143325db76d6c3dabce82500f8cc7c93a941970John McCall
990bbf3bacb3e0c1ebb3e8a4a8b1330404a7e379315Jay Foad  llvm::PHINode *cur = Builder.CreatePHI(i8p, 2, "vla.cur");
9917143325db76d6c3dabce82500f8cc7c93a941970John McCall  cur->addIncoming(begin, originBB);
9927143325db76d6c3dabce82500f8cc7c93a941970John McCall
9937143325db76d6c3dabce82500f8cc7c93a941970John McCall  // memcpy the individual element bit-pattern.
9947143325db76d6c3dabce82500f8cc7c93a941970John McCall  Builder.CreateMemCpy(cur, src, baseSizeInChars,
9957143325db76d6c3dabce82500f8cc7c93a941970John McCall                       baseSizeAndAlign.second.getQuantity(),
9967143325db76d6c3dabce82500f8cc7c93a941970John McCall                       /*volatile*/ false);
9977143325db76d6c3dabce82500f8cc7c93a941970John McCall
9987143325db76d6c3dabce82500f8cc7c93a941970John McCall  // Go to the next element.
9997143325db76d6c3dabce82500f8cc7c93a941970John McCall  llvm::Value *next = Builder.CreateConstInBoundsGEP1_32(cur, 1, "vla.next");
10007143325db76d6c3dabce82500f8cc7c93a941970John McCall
10017143325db76d6c3dabce82500f8cc7c93a941970John McCall  // Leave if that's the end of the VLA.
10027143325db76d6c3dabce82500f8cc7c93a941970John McCall  llvm::Value *done = Builder.CreateICmpEQ(next, end, "vla-init.isdone");
10037143325db76d6c3dabce82500f8cc7c93a941970John McCall  Builder.CreateCondBr(done, contBB, loopBB);
10047143325db76d6c3dabce82500f8cc7c93a941970John McCall  cur->addIncoming(next, loopBB);
10057143325db76d6c3dabce82500f8cc7c93a941970John McCall
10067143325db76d6c3dabce82500f8cc7c93a941970John McCall  CGF.EmitBlock(contBB);
10079dce4b3d9d14ba2be2aad5bd7fd8d381a0263c36Michael Ilseman}
10087143325db76d6c3dabce82500f8cc7c93a941970John McCall
10091884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlssonvoid
10101884eb0b5c55edda4893ddec45e7dbad79758782Anders CarlssonCodeGenFunction::EmitNullInitialization(llvm::Value *DestPtr, QualType Ty) {
10110d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson  // Ignore empty classes in C++.
10127edf9e38b91917b661277601c0e448eef0eb2b56Richard Smith  if (getLangOpts().CPlusPlus) {
10130d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson    if (const RecordType *RT = Ty->getAs<RecordType>()) {
10140d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson      if (cast<CXXRecordDecl>(RT->getDecl())->isEmpty())
10150d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson        return;
10160d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson    }
10170d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson  }
10189021718882441dd391a1960084580d3cd19c423aJohn McCall
10199021718882441dd391a1960084580d3cd19c423aJohn McCall  // Cast the dest ptr to the appropriate i8 pointer type.
10209021718882441dd391a1960084580d3cd19c423aJohn McCall  unsigned DestAS =
10219021718882441dd391a1960084580d3cd19c423aJohn McCall    cast<llvm::PointerType>(DestPtr->getType())->getAddressSpace();
10222acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *BP = Builder.getInt8PtrTy(DestAS);
10233d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson  if (DestPtr->getType() != BP)
1024578faa837b552403e2002b97fdfbfde14f2448e5Benjamin Kramer    DestPtr = Builder.CreateBitCast(DestPtr, BP);
10253d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson
10263d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson  // Get size and alignment info for this aggregate.
10279dce4b3d9d14ba2be2aad5bd7fd8d381a0263c36Michael Ilseman  std::pair<CharUnits, CharUnits> TypeInfo =
102879be76c0e360d9e9c44285c9838af02adc43e55fKen Dyck    getContext().getTypeInfoInChars(Ty);
102979be76c0e360d9e9c44285c9838af02adc43e55fKen Dyck  CharUnits Size = TypeInfo.first;
103079be76c0e360d9e9c44285c9838af02adc43e55fKen Dyck  CharUnits Align = TypeInfo.second;
10313d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson
10325576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall  llvm::Value *SizeVal;
10337143325db76d6c3dabce82500f8cc7c93a941970John McCall  const VariableArrayType *vla;
10349021718882441dd391a1960084580d3cd19c423aJohn McCall
10355576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall  // Don't bother emitting a zero-byte memset.
103679be76c0e360d9e9c44285c9838af02adc43e55fKen Dyck  if (Size.isZero()) {
10375576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall    // But note that getTypeInfo returns 0 for a VLA.
10385576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall    if (const VariableArrayType *vlaType =
10395576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall          dyn_cast_or_null<VariableArrayType>(
10405576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall                                          getContext().getAsArrayType(Ty))) {
1041bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      QualType eltType;
1042bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      llvm::Value *numElts;
1043bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      llvm::tie(numElts, eltType) = getVLASize(vlaType);
1044bc8d40d85f3fa1e34569834916f18fecaa635152John McCall
1045bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      SizeVal = numElts;
1046bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      CharUnits eltSize = getContext().getTypeSizeInChars(eltType);
1047bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      if (!eltSize.isOne())
1048bc8d40d85f3fa1e34569834916f18fecaa635152John McCall        SizeVal = Builder.CreateNUWMul(SizeVal, CGM.getSize(eltSize));
10497143325db76d6c3dabce82500f8cc7c93a941970John McCall      vla = vlaType;
10505576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall    } else {
10515576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall      return;
10525576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall    }
10535576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall  } else {
1054bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    SizeVal = CGM.getSize(Size);
10557143325db76d6c3dabce82500f8cc7c93a941970John McCall    vla = 0;
10565576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall  }
10579021718882441dd391a1960084580d3cd19c423aJohn McCall
10589021718882441dd391a1960084580d3cd19c423aJohn McCall  // If the type contains a pointer to data member we can't memset it to zero.
10599021718882441dd391a1960084580d3cd19c423aJohn McCall  // Instead, create a null constant and copy it to the destination.
10607143325db76d6c3dabce82500f8cc7c93a941970John McCall  // TODO: there are other patterns besides zero that we can usefully memset,
10617143325db76d6c3dabce82500f8cc7c93a941970John McCall  // like -1, which happens to be the pattern used by member-pointers.
1062f16aa103d3afd42fbca2ab346f191bf745cec092John McCall  if (!CGM.getTypes().isZeroInitializable(Ty)) {
10637143325db76d6c3dabce82500f8cc7c93a941970John McCall    // For a VLA, emit a single element, then splat that over the VLA.
10647143325db76d6c3dabce82500f8cc7c93a941970John McCall    if (vla) Ty = getContext().getBaseElementType(vla);
10655576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall
10669021718882441dd391a1960084580d3cd19c423aJohn McCall    llvm::Constant *NullConstant = CGM.EmitNullConstant(Ty);
10679021718882441dd391a1960084580d3cd19c423aJohn McCall
10689dce4b3d9d14ba2be2aad5bd7fd8d381a0263c36Michael Ilseman    llvm::GlobalVariable *NullVariable =
10699021718882441dd391a1960084580d3cd19c423aJohn McCall      new llvm::GlobalVariable(CGM.getModule(), NullConstant->getType(),
10709dce4b3d9d14ba2be2aad5bd7fd8d381a0263c36Michael Ilseman                               /*isConstant=*/true,
10719021718882441dd391a1960084580d3cd19c423aJohn McCall                               llvm::GlobalVariable::PrivateLinkage,
10725f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                               NullConstant, Twine());
10739021718882441dd391a1960084580d3cd19c423aJohn McCall    llvm::Value *SrcPtr =
10749021718882441dd391a1960084580d3cd19c423aJohn McCall      Builder.CreateBitCast(NullVariable, Builder.getInt8PtrTy());
10759021718882441dd391a1960084580d3cd19c423aJohn McCall
10767143325db76d6c3dabce82500f8cc7c93a941970John McCall    if (vla) return emitNonZeroVLAInit(*this, Ty, DestPtr, SrcPtr, SizeVal);
10777143325db76d6c3dabce82500f8cc7c93a941970John McCall
10789021718882441dd391a1960084580d3cd19c423aJohn McCall    // Get and call the appropriate llvm.memcpy overload.
107979be76c0e360d9e9c44285c9838af02adc43e55fKen Dyck    Builder.CreateMemCpy(DestPtr, SrcPtr, SizeVal, Align.getQuantity(), false);
108088207c9ca218486b93ae7df14e9764cd0c2c3383Chris Lattner    return;
10819dce4b3d9d14ba2be2aad5bd7fd8d381a0263c36Michael Ilseman  }
10829dce4b3d9d14ba2be2aad5bd7fd8d381a0263c36Michael Ilseman
10839021718882441dd391a1960084580d3cd19c423aJohn McCall  // Otherwise, just memset the whole thing to zero.  This is legal
10849021718882441dd391a1960084580d3cd19c423aJohn McCall  // because in LLVM, all default initializers (other than the ones we just
10859021718882441dd391a1960084580d3cd19c423aJohn McCall  // handled above) are guaranteed to have a bit pattern of all zeros.
10869dce4b3d9d14ba2be2aad5bd7fd8d381a0263c36Michael Ilseman  Builder.CreateMemSet(DestPtr, Builder.getInt8(0), SizeVal,
108779be76c0e360d9e9c44285c9838af02adc43e55fKen Dyck                       Align.getQuantity(), false);
10883d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson}
10893d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson
1090ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattnerllvm::BlockAddress *CodeGenFunction::GetAddrOfLabel(const LabelDecl *L) {
1091d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // Make sure that there is a block for the indirect goto.
1092d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  if (IndirectBranch == 0)
1093d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    GetIndirectGotoBlock();
10949dce4b3d9d14ba2be2aad5bd7fd8d381a0263c36Michael Ilseman
1095ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  llvm::BasicBlock *BB = getJumpDestForLabel(L).getBlock();
10969dce4b3d9d14ba2be2aad5bd7fd8d381a0263c36Michael Ilseman
1097d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // Make sure the indirect branch includes all of the address-taken blocks.
1098d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  IndirectBranch->addDestination(BB);
1099d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  return llvm::BlockAddress::get(CurFn, BB);
11003d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner}
11011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11023d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattnerllvm::BasicBlock *CodeGenFunction::GetIndirectGotoBlock() {
1103d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // If we already made the indirect branch for indirect goto, return its block.
1104d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  if (IndirectBranch) return IndirectBranch->getParent();
11059dce4b3d9d14ba2be2aad5bd7fd8d381a0263c36Michael Ilseman
1106d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  CGBuilderTy TmpBuilder(createBasicBlock("indirectgoto"));
11079dce4b3d9d14ba2be2aad5bd7fd8d381a0263c36Michael Ilseman
11083d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner  // Create the PHI node that indirect gotos will add entries to.
1109bbf3bacb3e0c1ebb3e8a4a8b1330404a7e379315Jay Foad  llvm::Value *DestVal = TmpBuilder.CreatePHI(Int8PtrTy, 0,
1110bbf3bacb3e0c1ebb3e8a4a8b1330404a7e379315Jay Foad                                              "indirect.goto.dest");
11119dce4b3d9d14ba2be2aad5bd7fd8d381a0263c36Michael Ilseman
1112d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // Create the indirect branch instruction.
1113d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  IndirectBranch = TmpBuilder.CreateIndirectBr(DestVal);
1114d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  return IndirectBranch->getParent();
11150ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar}
1116ddf7cac45d85b73127adbbd91a2b28fc7291c57eAnders Carlsson
1117bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall/// Computes the length of an array in elements, as well as the base
1118bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall/// element type and a properly-typed first element pointer.
1119bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCallllvm::Value *CodeGenFunction::emitArrayLength(const ArrayType *origArrayType,
1120bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall                                              QualType &baseType,
1121bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall                                              llvm::Value *&addr) {
1122bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  const ArrayType *arrayType = origArrayType;
1123bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
1124bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  // If it's a VLA, we have to load the stored size.  Note that
1125bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  // this is the size of the VLA in bytes, not its size in elements.
1126bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  llvm::Value *numVLAElements = 0;
1127bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  if (isa<VariableArrayType>(arrayType)) {
1128bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    numVLAElements = getVLASize(cast<VariableArrayType>(arrayType)).first;
1129bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
1130bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    // Walk into all VLAs.  This doesn't require changes to addr,
1131bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    // which has type T* where T is the first non-VLA element type.
1132bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    do {
1133bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall      QualType elementType = arrayType->getElementType();
1134bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall      arrayType = getContext().getAsArrayType(elementType);
1135bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
1136bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall      // If we only have VLA components, 'addr' requires no adjustment.
1137bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall      if (!arrayType) {
1138bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall        baseType = elementType;
1139bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall        return numVLAElements;
1140bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall      }
1141bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    } while (isa<VariableArrayType>(arrayType));
1142bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
1143bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    // We get out here only if we find a constant array type
1144bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    // inside the VLA.
1145bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  }
1146bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
1147bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  // We have some number of constant-length arrays, so addr should
1148bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  // have LLVM type [M x [N x [...]]]*.  Build a GEP that walks
1149bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  // down to the first element of addr.
11505f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<llvm::Value*, 8> gepIndices;
1151bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
1152bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  // GEP down to the array type.
1153bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  llvm::ConstantInt *zero = Builder.getInt32(0);
1154bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  gepIndices.push_back(zero);
1155bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
1156bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  uint64_t countFromCLAs = 1;
11571664d540d1524f0faffd2f839fccb56178975c60Richard Smith  QualType eltType;
1158bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
11592acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::ArrayType *llvmArrayType =
11601664d540d1524f0faffd2f839fccb56178975c60Richard Smith    dyn_cast<llvm::ArrayType>(
1161bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall      cast<llvm::PointerType>(addr->getType())->getElementType());
11621664d540d1524f0faffd2f839fccb56178975c60Richard Smith  while (llvmArrayType) {
1163bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    assert(isa<ConstantArrayType>(arrayType));
1164bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    assert(cast<ConstantArrayType>(arrayType)->getSize().getZExtValue()
1165bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall             == llvmArrayType->getNumElements());
1166bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
1167bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    gepIndices.push_back(zero);
1168bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    countFromCLAs *= llvmArrayType->getNumElements();
11691664d540d1524f0faffd2f839fccb56178975c60Richard Smith    eltType = arrayType->getElementType();
1170bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
1171bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    llvmArrayType =
1172bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall      dyn_cast<llvm::ArrayType>(llvmArrayType->getElementType());
1173bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    arrayType = getContext().getAsArrayType(arrayType->getElementType());
11741664d540d1524f0faffd2f839fccb56178975c60Richard Smith    assert((!llvmArrayType || arrayType) &&
11751664d540d1524f0faffd2f839fccb56178975c60Richard Smith           "LLVM and Clang types are out-of-synch");
1176bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  }
1177bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
11781664d540d1524f0faffd2f839fccb56178975c60Richard Smith  if (arrayType) {
11791664d540d1524f0faffd2f839fccb56178975c60Richard Smith    // From this point onwards, the Clang array type has been emitted
11801664d540d1524f0faffd2f839fccb56178975c60Richard Smith    // as some other type (probably a packed struct). Compute the array
11811664d540d1524f0faffd2f839fccb56178975c60Richard Smith    // size, and just emit the 'begin' expression as a bitcast.
11821664d540d1524f0faffd2f839fccb56178975c60Richard Smith    while (arrayType) {
11831664d540d1524f0faffd2f839fccb56178975c60Richard Smith      countFromCLAs *=
11841664d540d1524f0faffd2f839fccb56178975c60Richard Smith          cast<ConstantArrayType>(arrayType)->getSize().getZExtValue();
11851664d540d1524f0faffd2f839fccb56178975c60Richard Smith      eltType = arrayType->getElementType();
11861664d540d1524f0faffd2f839fccb56178975c60Richard Smith      arrayType = getContext().getAsArrayType(eltType);
11871664d540d1524f0faffd2f839fccb56178975c60Richard Smith    }
11881664d540d1524f0faffd2f839fccb56178975c60Richard Smith
1189956a5a17713deb1b5b27893303c4f308a1bd2a62Micah Villmow    unsigned AddressSpace = addr->getType()->getPointerAddressSpace();
11901664d540d1524f0faffd2f839fccb56178975c60Richard Smith    llvm::Type *BaseType = ConvertType(eltType)->getPointerTo(AddressSpace);
11911664d540d1524f0faffd2f839fccb56178975c60Richard Smith    addr = Builder.CreateBitCast(addr, BaseType, "array.begin");
11921664d540d1524f0faffd2f839fccb56178975c60Richard Smith  } else {
11931664d540d1524f0faffd2f839fccb56178975c60Richard Smith    // Create the actual GEP.
11941664d540d1524f0faffd2f839fccb56178975c60Richard Smith    addr = Builder.CreateInBoundsGEP(addr, gepIndices, "array.begin");
11951664d540d1524f0faffd2f839fccb56178975c60Richard Smith  }
1196bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
11971664d540d1524f0faffd2f839fccb56178975c60Richard Smith  baseType = eltType;
1198bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
1199bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  llvm::Value *numElements
1200bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    = llvm::ConstantInt::get(SizeTy, countFromCLAs);
1201bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
1202bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  // If we had any VLA dimensions, factor them in.
1203bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  if (numVLAElements)
1204bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    numElements = Builder.CreateNUWMul(numVLAElements, numElements);
1205bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
1206bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  return numElements;
1207bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall}
1208bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
1209bc8d40d85f3fa1e34569834916f18fecaa635152John McCallstd::pair<llvm::Value*, QualType>
1210bc8d40d85f3fa1e34569834916f18fecaa635152John McCallCodeGenFunction::getVLASize(QualType type) {
1211bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  const VariableArrayType *vla = getContext().getAsVariableArrayType(type);
1212bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  assert(vla && "type was not a variable array type!");
1213bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  return getVLASize(vla);
1214f666b7780d04186521adcaedb0e15dfa4d5e6933Anders Carlsson}
1215dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlsson
1216bc8d40d85f3fa1e34569834916f18fecaa635152John McCallstd::pair<llvm::Value*, QualType>
1217bc8d40d85f3fa1e34569834916f18fecaa635152John McCallCodeGenFunction::getVLASize(const VariableArrayType *type) {
1218bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  // The number of elements so far; always size_t.
1219bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  llvm::Value *numElements = 0;
12201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1221bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  QualType elementType;
1222bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  do {
1223bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    elementType = type->getElementType();
1224bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    llvm::Value *vlaSize = VLASizeMap[type->getSizeExpr()];
1225bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    assert(vlaSize && "no size for VLA!");
1226bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    assert(vlaSize->getType() == SizeTy);
12271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1228bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    if (!numElements) {
1229bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      numElements = vlaSize;
1230bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    } else {
1231bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      // It's undefined behavior if this wraps around, so mark it that way.
1232930c05c32eac171ef1a94fabf80aecb4e6e2c840Richard Smith      // FIXME: Teach -fcatch-undefined-behavior to trap this.
1233bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      numElements = Builder.CreateNUWMul(numElements, vlaSize);
1234bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    }
1235bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  } while ((type = getContext().getAsVariableArrayType(elementType)));
12361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1237bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  return std::pair<llvm::Value*,QualType>(numElements, elementType);
1238bc8d40d85f3fa1e34569834916f18fecaa635152John McCall}
12391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1240bc8d40d85f3fa1e34569834916f18fecaa635152John McCallvoid CodeGenFunction::EmitVariablyModifiedType(QualType type) {
1241bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  assert(type->isVariablyModifiedType() &&
1242bc8d40d85f3fa1e34569834916f18fecaa635152John McCall         "Must pass variably modified type to EmitVLASizes!");
12431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1244bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  EnsureInsertPoint();
12451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1246bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  // We're going to walk down into the type and look for VLA
1247bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  // expressions.
1248bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  do {
1249bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    assert(type->isVariablyModifiedType());
1250bc8d40d85f3fa1e34569834916f18fecaa635152John McCall
1251bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    const Type *ty = type.getTypePtr();
1252bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    switch (ty->getTypeClass()) {
125306284c1dc56caed19850bc3766c89f51763724c3Abramo Bagnara
1254bc8d40d85f3fa1e34569834916f18fecaa635152John McCall#define TYPE(Class, Base)
1255bc8d40d85f3fa1e34569834916f18fecaa635152John McCall#define ABSTRACT_TYPE(Class, Base)
125606284c1dc56caed19850bc3766c89f51763724c3Abramo Bagnara#define NON_CANONICAL_TYPE(Class, Base)
1257bc8d40d85f3fa1e34569834916f18fecaa635152John McCall#define DEPENDENT_TYPE(Class, Base) case Type::Class:
125806284c1dc56caed19850bc3766c89f51763724c3Abramo Bagnara#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base)
1259bc8d40d85f3fa1e34569834916f18fecaa635152John McCall#include "clang/AST/TypeNodes.def"
126006284c1dc56caed19850bc3766c89f51763724c3Abramo Bagnara      llvm_unreachable("unexpected dependent type!");
1261bc8d40d85f3fa1e34569834916f18fecaa635152John McCall
1262bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    // These types are never variably-modified.
1263bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::Builtin:
1264bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::Complex:
1265bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::Vector:
1266bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::ExtVector:
1267bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::Record:
1268bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::Enum:
12695ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara    case Type::Elaborated:
12705ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara    case Type::TemplateSpecialization:
1271bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::ObjCObject:
1272bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::ObjCInterface:
1273bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::ObjCObjectPointer:
1274bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      llvm_unreachable("type class is never variably-modified!");
1275bc8d40d85f3fa1e34569834916f18fecaa635152John McCall
1276bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::Pointer:
1277bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      type = cast<PointerType>(ty)->getPointeeType();
1278bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      break;
1279bc8d40d85f3fa1e34569834916f18fecaa635152John McCall
1280bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::BlockPointer:
1281bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      type = cast<BlockPointerType>(ty)->getPointeeType();
1282bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      break;
1283bc8d40d85f3fa1e34569834916f18fecaa635152John McCall
1284bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::LValueReference:
1285bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::RValueReference:
1286bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      type = cast<ReferenceType>(ty)->getPointeeType();
1287bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      break;
1288bc8d40d85f3fa1e34569834916f18fecaa635152John McCall
1289bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::MemberPointer:
1290bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      type = cast<MemberPointerType>(ty)->getPointeeType();
1291bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      break;
1292bc8d40d85f3fa1e34569834916f18fecaa635152John McCall
1293bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::ConstantArray:
1294bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::IncompleteArray:
1295bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      // Losing element qualification here is fine.
1296bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      type = cast<ArrayType>(ty)->getElementType();
1297bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      break;
1298bc8d40d85f3fa1e34569834916f18fecaa635152John McCall
1299bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::VariableArray: {
1300bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      // Losing element qualification here is fine.
1301bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      const VariableArrayType *vat = cast<VariableArrayType>(ty);
1302bc8d40d85f3fa1e34569834916f18fecaa635152John McCall
1303bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      // Unknown size indication requires no size computation.
1304bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      // Otherwise, evaluate and record it.
1305bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      if (const Expr *size = vat->getSizeExpr()) {
1306bc8d40d85f3fa1e34569834916f18fecaa635152John McCall        // It's possible that we might have emitted this already,
1307bc8d40d85f3fa1e34569834916f18fecaa635152John McCall        // e.g. with a typedef and a pointer to it.
1308bc8d40d85f3fa1e34569834916f18fecaa635152John McCall        llvm::Value *&entry = VLASizeMap[size];
1309bc8d40d85f3fa1e34569834916f18fecaa635152John McCall        if (!entry) {
1310930c05c32eac171ef1a94fabf80aecb4e6e2c840Richard Smith          llvm::Value *Size = EmitScalarExpr(size);
1311930c05c32eac171ef1a94fabf80aecb4e6e2c840Richard Smith
1312930c05c32eac171ef1a94fabf80aecb4e6e2c840Richard Smith          // C11 6.7.6.2p5:
1313930c05c32eac171ef1a94fabf80aecb4e6e2c840Richard Smith          //   If the size is an expression that is not an integer constant
1314930c05c32eac171ef1a94fabf80aecb4e6e2c840Richard Smith          //   expression [...] each time it is evaluated it shall have a value
1315930c05c32eac171ef1a94fabf80aecb4e6e2c840Richard Smith          //   greater than zero.
13164f45bc099f2665bc6e4bcbb169aa452390dbf3feWill Dietz          if (SanOpts->VLABound &&
1317d6396a681c9acbe56bc41bbc2bed2db45755bcd7Richard Smith              size->getType()->isSignedIntegerType()) {
1318930c05c32eac171ef1a94fabf80aecb4e6e2c840Richard Smith            llvm::Value *Zero = llvm::Constant::getNullValue(Size->getType());
1319930c05c32eac171ef1a94fabf80aecb4e6e2c840Richard Smith            llvm::Constant *StaticArgs[] = {
1320930c05c32eac171ef1a94fabf80aecb4e6e2c840Richard Smith              EmitCheckSourceLocation(size->getLocStart()),
1321930c05c32eac171ef1a94fabf80aecb4e6e2c840Richard Smith              EmitCheckTypeDescriptor(size->getType())
1322930c05c32eac171ef1a94fabf80aecb4e6e2c840Richard Smith            };
1323930c05c32eac171ef1a94fabf80aecb4e6e2c840Richard Smith            EmitCheck(Builder.CreateICmpSGT(Size, Zero),
1324ad95481b2769ad61f23afc5e2c31f11c66090657Will Dietz                      "vla_bound_not_positive", StaticArgs, Size,
1325ad95481b2769ad61f23afc5e2c31f11c66090657Will Dietz                      CRK_Recoverable);
1326930c05c32eac171ef1a94fabf80aecb4e6e2c840Richard Smith          }
1327930c05c32eac171ef1a94fabf80aecb4e6e2c840Richard Smith
1328bc8d40d85f3fa1e34569834916f18fecaa635152John McCall          // Always zexting here would be wrong if it weren't
1329bc8d40d85f3fa1e34569834916f18fecaa635152John McCall          // undefined behavior to have a negative bound.
1330bf43f2fdfbd7c4f8cd1f1403765eca9e5dc6c8aeRichard Smith          entry = Builder.CreateIntCast(Size, SizeTy, /*signed*/ false);
1331bc8d40d85f3fa1e34569834916f18fecaa635152John McCall        }
1332bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      }
1333bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      type = vat->getElementType();
1334bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      break;
1335fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson    }
13361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
133706284c1dc56caed19850bc3766c89f51763724c3Abramo Bagnara    case Type::FunctionProto:
1338bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::FunctionNoProto:
1339bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      type = cast<FunctionType>(ty)->getResultType();
1340bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      break;
1341b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
13425ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara    case Type::Paren:
13435ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara    case Type::TypeOf:
13445ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara    case Type::UnaryTransform:
13455ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara    case Type::Attributed:
13465ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara    case Type::SubstTemplateTypeParm:
13475ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara      // Keep walking after single level desugaring.
13485ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara      type = type.getSingleStepDesugaredType(getContext());
13495ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara      break;
13505ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara
13515ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara    case Type::Typedef:
13525ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara    case Type::Decltype:
13535ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara    case Type::Auto:
13545ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara      // Stop walking: nothing to do.
13555ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara      return;
13565ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara
13575ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara    case Type::TypeOfExpr:
13585ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara      // Stop walking: emit typeof expression.
13595ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara      EmitIgnoredExpr(cast<TypeOfExprType>(ty)->getUnderlyingExpr());
13605ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara      return;
13615ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara
1362b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    case Type::Atomic:
1363b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      type = cast<AtomicType>(ty)->getValueType();
1364b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      break;
1365bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    }
1366bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  } while (type->isVariablyModifiedType());
1367dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlsson}
13684fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman
13694fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedmanllvm::Value* CodeGenFunction::EmitVAListRef(const Expr* E) {
1370bc07a55b68342a230aafd8875cc7a26450dd3f64Dan Gohman  if (getContext().getBuiltinVaListType()->isArrayType())
13714fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman    return EmitScalarExpr(E);
13724fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman  return EmitLValue(E).getAddress();
13734fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman}
13746ccc47698d0311ddabf32fa0f6db8e4f09ac96f8Anders Carlsson
13759dce4b3d9d14ba2be2aad5bd7fd8d381a0263c36Michael Ilsemanvoid CodeGenFunction::EmitDeclRefExprDbgValue(const DeclRefExpr *E,
1376189d6ef40eff11b83b2cda941d5ed89a5cef09b2John McCall                                              llvm::Constant *Init) {
137725c2c8fb9309050612009a6571e2660e75531348Devang Patel  assert (Init && "Invalid DeclRefExpr initializer!");
137825c2c8fb9309050612009a6571e2660e75531348Devang Patel  if (CGDebugInfo *Dbg = getDebugInfo())
13794cdad3151bfb2075c6bdbfe89fbb08f31a90a45bDouglas Gregor    if (CGM.getCodeGenOpts().getDebugInfo() >= CodeGenOptions::LimitedDebugInfo)
1380fd00eecad6fa5400cf37269d84361a0551d0e6d3Alexey Samsonov      Dbg->EmitGlobalVariable(E->getDecl(), Init);
13818d308384a220c7dc81755c47cdcbdd87dac25d5bDevang Patel}
138256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
138356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCallCodeGenFunction::PeepholeProtection
138456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCallCodeGenFunction::protectFromPeepholes(RValue rvalue) {
138556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  // At the moment, the only aggressive peephole we do in IR gen
138656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  // is trunc(zext) folding, but if we add more, we can easily
138756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  // extend this protection.
138856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
138956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  if (!rvalue.isScalar()) return PeepholeProtection();
139056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  llvm::Value *value = rvalue.getScalarVal();
139156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  if (!isa<llvm::ZExtInst>(value)) return PeepholeProtection();
139256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
139356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  // Just make an extra bitcast.
139456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  assert(HaveInsertPoint());
139556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  llvm::Instruction *inst = new llvm::BitCastInst(value, value->getType(), "",
139656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall                                                  Builder.GetInsertBlock());
139756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
139856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  PeepholeProtection protection;
139956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  protection.Inst = inst;
140056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  return protection;
140156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall}
140256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
140356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCallvoid CodeGenFunction::unprotectFromPeepholes(PeepholeProtection protection) {
140456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  if (!protection.Inst) return;
140556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
140656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  // In theory, we could try to duplicate the peepholes now, but whatever.
140756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  protection.Inst->eraseFromParent();
140856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall}
140977f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge
141077f68bb90af93b95045fb994e7cd68137adcc132Julien Lerougellvm::Value *CodeGenFunction::EmitAnnotationCall(llvm::Value *AnnotationFn,
141177f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge                                                 llvm::Value *AnnotatedVal,
1412cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko                                                 StringRef AnnotationStr,
141377f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge                                                 SourceLocation Location) {
141477f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  llvm::Value *Args[4] = {
141577f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge    AnnotatedVal,
141677f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge    Builder.CreateBitCast(CGM.EmitAnnotationString(AnnotationStr), Int8PtrTy),
141777f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge    Builder.CreateBitCast(CGM.EmitAnnotationUnit(Location), Int8PtrTy),
141877f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge    CGM.EmitAnnotationLineNo(Location)
141977f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  };
142077f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  return Builder.CreateCall(AnnotationFn, Args);
142177f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge}
142277f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge
142377f68bb90af93b95045fb994e7cd68137adcc132Julien Lerougevoid CodeGenFunction::EmitVarAnnotations(const VarDecl *D, llvm::Value *V) {
142477f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  assert(D->hasAttr<AnnotateAttr>() && "no annotate attribute");
142577f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  // FIXME We create a new bitcast for every annotation because that's what
142677f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  // llvm-gcc was doing.
142777f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  for (specific_attr_iterator<AnnotateAttr>
142877f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge       ai = D->specific_attr_begin<AnnotateAttr>(),
142977f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge       ae = D->specific_attr_end<AnnotateAttr>(); ai != ae; ++ai)
143077f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge    EmitAnnotationCall(CGM.getIntrinsic(llvm::Intrinsic::var_annotation),
143177f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge                       Builder.CreateBitCast(V, CGM.Int8PtrTy, V->getName()),
143277f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge                       (*ai)->getAnnotation(), D->getLocation());
143377f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge}
143477f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge
143577f68bb90af93b95045fb994e7cd68137adcc132Julien Lerougellvm::Value *CodeGenFunction::EmitFieldAnnotations(const FieldDecl *D,
143677f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge                                                   llvm::Value *V) {
143777f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  assert(D->hasAttr<AnnotateAttr>() && "no annotate attribute");
143877f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  llvm::Type *VTy = V->getType();
143977f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::ptr_annotation,
144077f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge                                    CGM.Int8PtrTy);
144177f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge
144277f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  for (specific_attr_iterator<AnnotateAttr>
144377f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge       ai = D->specific_attr_begin<AnnotateAttr>(),
144477f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge       ae = D->specific_attr_end<AnnotateAttr>(); ai != ae; ++ai) {
144577f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge    // FIXME Always emit the cast inst so we can differentiate between
144677f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge    // annotation on the first field of a struct and annotation on the struct
144777f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge    // itself.
144877f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge    if (VTy != CGM.Int8PtrTy)
144977f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge      V = Builder.Insert(new llvm::BitCastInst(V, CGM.Int8PtrTy));
145077f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge    V = EmitAnnotationCall(F, V, (*ai)->getAnnotation(), D->getLocation());
145177f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge    V = Builder.CreateBitCast(V, VTy);
145277f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  }
145377f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge
145477f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  return V;
145577f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge}
1456524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben Langmuir
1457524387ae3dfc0c4cf2b095f83f9e47aa549b7e55Ben LangmuirCodeGenFunction::CGCapturedStmtInfo::~CGCapturedStmtInfo() { }
1458