CodeGenFunction.cpp revision a240df2ec1b374b3e9e7f760875ffb17cd64506f
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"
155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "CodeGenModule.h"
16a4ae2294b6ebfb2554aacb6a6a0682fb5ed1f276Peter Collingbourne#include "CGCUDARuntime.h"
174c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall#include "CGCXXABI.h"
183f2af1002249c8acc9ce17f1fc50324864feb8e1Eli Friedman#include "CGDebugInfo.h"
195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Basic/TargetInfo.h"
20de7fb8413b13651fd85b7125d08b3c9ac2816d9dDaniel Dunbar#include "clang/AST/ASTContext.h"
21c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel Dunbar#include "clang/AST/Decl.h"
222b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson#include "clang/AST/DeclCXX.h"
236a1e0eb557d47e85185e09bdf8721f53f4bf9c9cMike Stump#include "clang/AST/StmtCXX.h"
247255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner#include "clang/Frontend/CodeGenOptions.h"
257255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner#include "llvm/Intrinsics.h"
266bebe5aca3c45c741303dd0ae12d0225bddbe2e6Chandler Carruth#include "llvm/MDBuilder.h"
2725a6a84cf5067b32c271e3ba078676dee838798dMicah Villmow#include "llvm/DataLayout.h"
285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace CodeGen;
305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
314904bf4e84cfb48080270ebaa9005327f18ab0e5Fariborz JahanianCodeGenFunction::CodeGenFunction(CodeGenModule &cgm, bool suppressNewContext)
325936e33bf74dd6bf126ceee0f6169a2593d03a69John McCall  : CodeGenTypeCache(cgm), CGM(cgm),
33cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman    Target(CGM.getContext().getTargetInfo()),
34cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman    Builder(cgm.getModule().getContext()),
35f85e193739c953358c865005855253af4f68a497John McCall    AutoreleaseResult(false), BlockInfo(0), BlockPointer(0),
3623f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman    LambdaThisCaptureField(0), NormalCleanupDest(0), NextCleanupDestIndex(1),
37cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman    FirstBlockInfo(0), EHResumeBlock(0), ExceptionSlot(0), EHSelectorSlot(0),
3893c332a8ba2c193c435b293966d343dab15f555bJohn McCall    DebugInfo(0), DisableDebugInfo(false), DidCallStackSave(false),
3993c332a8ba2c193c435b293966d343dab15f555bJohn McCall    IndirectBranch(0), SwitchInsn(0), CaseRangeBlock(0), UnreachableBlock(0),
40cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman    CXXABIThisDecl(0), CXXABIThisValue(0), CXXThisValue(0), CXXVTTDecl(0),
41cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman    CXXVTTValue(0), OutermostConditional(0), TerminateLandingPad(0),
42cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman    TerminateHandler(0), TrapBB(0) {
43c1cfdf8647a499b6b3024f4bd14a236cddb23988Anders Carlsson
444e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  CatchUndefined = getContext().getLangOpts().CatchUndefined;
454904bf4e84cfb48080270ebaa9005327f18ab0e5Fariborz Jahanian  if (!suppressNewContext)
464904bf4e84cfb48080270ebaa9005327f18ab0e5Fariborz Jahanian    CGM.getCXXABI().getMangleContext().startNewFunction();
474111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner}
485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
491a343ebbf413e8eae6b2737b2b2d79cbf5765571John McCallCodeGenFunction::~CodeGenFunction() {
501a343ebbf413e8eae6b2737b2b2d79cbf5765571John McCall  // If there are any unclaimed block infos, go ahead and destroy them
511a343ebbf413e8eae6b2737b2b2d79cbf5765571John McCall  // now.  This can happen if IR-gen gets clever and skips evaluating
521a343ebbf413e8eae6b2737b2b2d79cbf5765571John McCall  // something.
531a343ebbf413e8eae6b2737b2b2d79cbf5765571John McCall  if (FirstBlockInfo)
541a343ebbf413e8eae6b2737b2b2d79cbf5765571John McCall    destroyBlockInfos(FirstBlockInfo);
551a343ebbf413e8eae6b2737b2b2d79cbf5765571John McCall}
561a343ebbf413e8eae6b2737b2b2d79cbf5765571John McCall
575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
589cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnerllvm::Type *CodeGenFunction::ConvertTypeForMem(QualType T) {
598b1a343b6b360d63d5dc8a6beb841ce4414c1e00Daniel Dunbar  return CGM.getTypes().ConvertTypeForMem(T);
608b1a343b6b360d63d5dc8a6beb841ce4414c1e00Daniel Dunbar}
618b1a343b6b360d63d5dc8a6beb841ce4414c1e00Daniel Dunbar
629cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnerllvm::Type *CodeGenFunction::ConvertType(QualType T) {
635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return CGM.getTypes().ConvertType(T);
645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
66f2aac84709c418189e476ad591848dad50291885John McCallbool CodeGenFunction::hasAggregateLLVMType(QualType type) {
67f2aac84709c418189e476ad591848dad50291885John McCall  switch (type.getCanonicalType()->getTypeClass()) {
68f2aac84709c418189e476ad591848dad50291885John McCall#define TYPE(name, parent)
69f2aac84709c418189e476ad591848dad50291885John McCall#define ABSTRACT_TYPE(name, parent)
70f2aac84709c418189e476ad591848dad50291885John McCall#define NON_CANONICAL_TYPE(name, parent) case Type::name:
71f2aac84709c418189e476ad591848dad50291885John McCall#define DEPENDENT_TYPE(name, parent) case Type::name:
72f2aac84709c418189e476ad591848dad50291885John McCall#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(name, parent) case Type::name:
73f2aac84709c418189e476ad591848dad50291885John McCall#include "clang/AST/TypeNodes.def"
74f2aac84709c418189e476ad591848dad50291885John McCall    llvm_unreachable("non-canonical or dependent type in IR-generation");
75f2aac84709c418189e476ad591848dad50291885John McCall
76f2aac84709c418189e476ad591848dad50291885John McCall  case Type::Builtin:
77f2aac84709c418189e476ad591848dad50291885John McCall  case Type::Pointer:
78f2aac84709c418189e476ad591848dad50291885John McCall  case Type::BlockPointer:
79f2aac84709c418189e476ad591848dad50291885John McCall  case Type::LValueReference:
80f2aac84709c418189e476ad591848dad50291885John McCall  case Type::RValueReference:
81f2aac84709c418189e476ad591848dad50291885John McCall  case Type::MemberPointer:
82f2aac84709c418189e476ad591848dad50291885John McCall  case Type::Vector:
83f2aac84709c418189e476ad591848dad50291885John McCall  case Type::ExtVector:
84f2aac84709c418189e476ad591848dad50291885John McCall  case Type::FunctionProto:
85f2aac84709c418189e476ad591848dad50291885John McCall  case Type::FunctionNoProto:
86f2aac84709c418189e476ad591848dad50291885John McCall  case Type::Enum:
87f2aac84709c418189e476ad591848dad50291885John McCall  case Type::ObjCObjectPointer:
88f2aac84709c418189e476ad591848dad50291885John McCall    return false;
89f2aac84709c418189e476ad591848dad50291885John McCall
90f2aac84709c418189e476ad591848dad50291885John McCall  // Complexes, arrays, records, and Objective-C objects.
91f2aac84709c418189e476ad591848dad50291885John McCall  case Type::Complex:
92f2aac84709c418189e476ad591848dad50291885John McCall  case Type::ConstantArray:
93f2aac84709c418189e476ad591848dad50291885John McCall  case Type::IncompleteArray:
94f2aac84709c418189e476ad591848dad50291885John McCall  case Type::VariableArray:
95f2aac84709c418189e476ad591848dad50291885John McCall  case Type::Record:
96f2aac84709c418189e476ad591848dad50291885John McCall  case Type::ObjCObject:
97f2aac84709c418189e476ad591848dad50291885John McCall  case Type::ObjCInterface:
98f2aac84709c418189e476ad591848dad50291885John McCall    return true;
99b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
100b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  // In IRGen, atomic types are just the underlying type
101b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  case Type::Atomic:
102b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    return hasAggregateLLVMType(type->getAs<AtomicType>()->getValueType());
103f2aac84709c418189e476ad591848dad50291885John McCall  }
104f2aac84709c418189e476ad591848dad50291885John McCall  llvm_unreachable("unknown type kind!");
1054111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner}
106391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner
1071c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbarvoid CodeGenFunction::EmitReturnBlock() {
1081c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // For cleanliness, we try to avoid emitting the return block for
1091c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // simple cases.
1101c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
1111c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
1121c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  if (CurBB) {
1131c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar    assert(!CurBB->getTerminator() && "Unexpected terminated block.");
1141c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
11596e18b05ea6b55aa92a1a576f29e9cee73a7e20bDaniel Dunbar    // We have a valid insert point, reuse it if it is empty or there are no
11696e18b05ea6b55aa92a1a576f29e9cee73a7e20bDaniel Dunbar    // explicit jumps to the return block.
117ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    if (CurBB->empty() || ReturnBlock.getBlock()->use_empty()) {
118ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      ReturnBlock.getBlock()->replaceAllUsesWith(CurBB);
119ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      delete ReturnBlock.getBlock();
12096e18b05ea6b55aa92a1a576f29e9cee73a7e20bDaniel Dunbar    } else
121ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      EmitBlock(ReturnBlock.getBlock());
1221c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar    return;
1231c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  }
1241c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
1251c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // Otherwise, if the return block is the target of a single direct
1261c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // branch then we can just put the code in that block instead. This
1271c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // cleans up functions which started with a unified return block.
128ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  if (ReturnBlock.getBlock()->hasOneUse()) {
1291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    llvm::BranchInst *BI =
130ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      dyn_cast<llvm::BranchInst>(*ReturnBlock.getBlock()->use_begin());
131f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    if (BI && BI->isUnconditional() &&
132ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall        BI->getSuccessor(0) == ReturnBlock.getBlock()) {
133acae01124151392a842bd6c37bd01b1ad56d6b4dEric Christopher      // Reset insertion point, including debug location, and delete the branch.
134acae01124151392a842bd6c37bd01b1ad56d6b4dEric Christopher      Builder.SetCurrentDebugLocation(BI->getDebugLoc());
1351c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      Builder.SetInsertPoint(BI->getParent());
1361c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      BI->eraseFromParent();
137ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      delete ReturnBlock.getBlock();
1381c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      return;
1391c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar    }
1401c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  }
1411c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
142f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // FIXME: We are at an unreachable point, there is no reason to emit the block
143f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // unless it has uses. However, we still need a place to put the debug
144f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // region.end for now.
1451c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
146ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  EmitBlock(ReturnBlock.getBlock());
147f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall}
148f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
149f1549f66a8216a78112286e3978cea2c29d6334cJohn McCallstatic void EmitIfUsed(CodeGenFunction &CGF, llvm::BasicBlock *BB) {
150f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  if (!BB) return;
151f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  if (!BB->use_empty())
152f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    return CGF.CurFn->getBasicBlockList().push_back(BB);
153f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  delete BB;
1541c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar}
1551c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
156af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbarvoid CodeGenFunction::FinishFunction(SourceLocation EndLoc) {
157391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  assert(BreakContinueStack.empty() &&
158391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner         "mismatched push/pop in break/continue stack!");
1591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
160f85e193739c953358c865005855253af4f68a497John McCall  // Pop any cleanups that might have been associated with the
161f85e193739c953358c865005855253af4f68a497John McCall  // parameters.  Do this in whatever block we're currently in; it's
162f85e193739c953358c865005855253af4f68a497John McCall  // important to do this before we enter the return block or return
163f85e193739c953358c865005855253af4f68a497John McCall  // edges will be *really* confused.
164f85e193739c953358c865005855253af4f68a497John McCall  if (EHStack.stable_begin() != PrologueCleanupDepth)
165f85e193739c953358c865005855253af4f68a497John McCall    PopCleanupBlocks(PrologueCleanupDepth);
166f85e193739c953358c865005855253af4f68a497John McCall
1671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Emit function epilog (to return).
1681c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  EmitReturnBlock();
169f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar
170a18652fe1e8233fbf8b67484945c7f7b2bf272beDaniel Dunbar  if (ShouldInstrumentFunction())
171a18652fe1e8233fbf8b67484945c7f7b2bf272beDaniel Dunbar    EmitFunctionInstrumentation("__cyg_profile_func_exit");
1727255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner
173f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar  // Emit debug descriptor for function end.
174e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson  if (CGDebugInfo *DI = getDebugInfo()) {
175f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar    DI->setLocation(EndLoc);
1765a6fbcfd8c15a2296f94a0473a68ec09d429827fDevang Patel    DI->EmitFunctionEnd(Builder);
177f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar  }
178f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar
17935b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner  EmitFunctionEpilog(*CurFnInfo);
180cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump  EmitEndEHSpec(CurCodeDecl);
1815ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar
182f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  assert(EHStack.empty() &&
183f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall         "did not remove all scopes from cleanup stack!");
184f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
185d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // If someone did an indirect goto, emit the indirect goto block at the end of
186d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // the function.
187d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  if (IndirectBranch) {
188d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    EmitBlock(IndirectBranch->getParent());
189d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    Builder.ClearInsertionPoint();
190d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  }
191d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner
192391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  // Remove the AllocaInsertPt instruction, which is just a convenience for us.
193481769b5dc102b0256b35581e787909ad5edfab5Chris Lattner  llvm::Instruction *Ptr = AllocaInsertPt;
194391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  AllocaInsertPt = 0;
195481769b5dc102b0256b35581e787909ad5edfab5Chris Lattner  Ptr->eraseFromParent();
196d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner
197d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // If someone took the address of a label but never did an indirect goto, we
198d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // made a zero entry PHI node, which is illegal, zap it now.
199d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  if (IndirectBranch) {
200d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    llvm::PHINode *PN = cast<llvm::PHINode>(IndirectBranch->getAddress());
201d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    if (PN->getNumIncomingValues() == 0) {
202d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner      PN->replaceAllUsesWith(llvm::UndefValue::get(PN->getType()));
203d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner      PN->eraseFromParent();
204d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    }
205d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  }
206f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
207777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  EmitIfUsed(*this, EHResumeBlock);
208f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  EmitIfUsed(*this, TerminateLandingPad);
209f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  EmitIfUsed(*this, TerminateHandler);
210f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  EmitIfUsed(*this, UnreachableBlock);
211744016dde06fcffd50931e94a98c850f8b12cd87John McCall
212744016dde06fcffd50931e94a98c850f8b12cd87John McCall  if (CGM.getCodeGenOpts().EmitDeclMetadata)
213744016dde06fcffd50931e94a98c850f8b12cd87John McCall    EmitDeclMetadata();
214c8aa5f1f264fb230c38182adab944232bb160c2bChris Lattner}
215c8aa5f1f264fb230c38182adab944232bb160c2bChris Lattner
2167255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner/// ShouldInstrumentFunction - Return true if the current function should be
2177255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner/// instrumented with __cyg_profile_func_* calls
2187255a2d997b15beae82e627052fdb1b2474495c2Chris Lattnerbool CodeGenFunction::ShouldInstrumentFunction() {
2197255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  if (!CGM.getCodeGenOpts().InstrumentFunctions)
2207255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner    return false;
2217aa488a7fc5c3a8cd1a2b93476150e9737760713Ted Kremenek  if (!CurFuncDecl || CurFuncDecl->hasAttr<NoInstrumentFunctionAttr>())
2227255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner    return false;
2237255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  return true;
2247255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner}
2257255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner
2267255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner/// EmitFunctionInstrumentation - Emit LLVM code to call the specified
2277255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner/// instrumentation function with the current function and the call site, if
2287255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner/// function instrumentation is enabled.
2297255a2d997b15beae82e627052fdb1b2474495c2Chris Lattnervoid CodeGenFunction::EmitFunctionInstrumentation(const char *Fn) {
2308dab6571b2cab96f44d0a1d6e3edbfdb68b7ed6bChris Lattner  // void __cyg_profile_func_{enter,exit} (void *this_fn, void *call_site);
2319cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::PointerType *PointerTy = Int8PtrTy;
2329cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::Type *ProfileFuncArgs[] = { PointerTy, PointerTy };
2332acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::FunctionType *FunctionTy =
2348b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner    llvm::FunctionType::get(VoidTy, ProfileFuncArgs, false);
2357255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner
2367255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  llvm::Constant *F = CGM.CreateRuntimeFunction(FunctionTy, Fn);
2377255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  llvm::CallInst *CallSite = Builder.CreateCall(
2388dd55a3c3b28d195717c87bbc60e765951d408feBenjamin Kramer    CGM.getIntrinsic(llvm::Intrinsic::returnaddress),
23977b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner    llvm::ConstantInt::get(Int32Ty, 0),
2407255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner    "callsite");
2417255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner
2428dab6571b2cab96f44d0a1d6e3edbfdb68b7ed6bChris Lattner  Builder.CreateCall2(F,
2438dab6571b2cab96f44d0a1d6e3edbfdb68b7ed6bChris Lattner                      llvm::ConstantExpr::getBitCast(CurFn, PointerTy),
2448dab6571b2cab96f44d0a1d6e3edbfdb68b7ed6bChris Lattner                      CallSite);
2457255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner}
2467255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner
247be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divackyvoid CodeGenFunction::EmitMCountInstrumentation() {
2488b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner  llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
249be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky
250be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky  llvm::Constant *MCountFn = CGM.CreateRuntimeFunction(FTy,
251be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky                                                       Target.getMCountName());
252be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky  Builder.CreateCall(MCountFn);
253be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky}
254be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky
255198871cc90375246d8692680467ff6e810edac36Tanya Lattner// OpenCL v1.2 s5.6.4.6 allows the compiler to store kernel argument
256198871cc90375246d8692680467ff6e810edac36Tanya Lattner// information in the program executable. The argument information stored
257198871cc90375246d8692680467ff6e810edac36Tanya Lattner// includes the argument name, its type, the address and access qualifiers used.
258198871cc90375246d8692680467ff6e810edac36Tanya Lattner// FIXME: Add type, address, and access qualifiers.
259198871cc90375246d8692680467ff6e810edac36Tanya Lattnerstatic void GenOpenCLArgMetadata(const FunctionDecl *FD, llvm::Function *Fn,
260198871cc90375246d8692680467ff6e810edac36Tanya Lattner                                 CodeGenModule &CGM,llvm::LLVMContext &Context,
261198871cc90375246d8692680467ff6e810edac36Tanya Lattner                                 llvm::SmallVector <llvm::Value*, 5> &kernelMDArgs) {
262198871cc90375246d8692680467ff6e810edac36Tanya Lattner
263198871cc90375246d8692680467ff6e810edac36Tanya Lattner  // Create MDNodes that represents the kernel arg metadata.
264198871cc90375246d8692680467ff6e810edac36Tanya Lattner  // Each MDNode is a list in the form of "key", N number of values which is
265198871cc90375246d8692680467ff6e810edac36Tanya Lattner  // the same number of values as their are kernel arguments.
266198871cc90375246d8692680467ff6e810edac36Tanya Lattner
267198871cc90375246d8692680467ff6e810edac36Tanya Lattner  // MDNode for the kernel argument names.
268198871cc90375246d8692680467ff6e810edac36Tanya Lattner  SmallVector<llvm::Value*, 8> argNames;
269198871cc90375246d8692680467ff6e810edac36Tanya Lattner  argNames.push_back(llvm::MDString::get(Context, "kernel_arg_name"));
270198871cc90375246d8692680467ff6e810edac36Tanya Lattner
271198871cc90375246d8692680467ff6e810edac36Tanya Lattner  for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
272198871cc90375246d8692680467ff6e810edac36Tanya Lattner    const ParmVarDecl *parm = FD->getParamDecl(i);
273198871cc90375246d8692680467ff6e810edac36Tanya Lattner
274198871cc90375246d8692680467ff6e810edac36Tanya Lattner    // Get argument name.
275198871cc90375246d8692680467ff6e810edac36Tanya Lattner    argNames.push_back(llvm::MDString::get(Context, parm->getName()));
276198871cc90375246d8692680467ff6e810edac36Tanya Lattner
277198871cc90375246d8692680467ff6e810edac36Tanya Lattner  }
278198871cc90375246d8692680467ff6e810edac36Tanya Lattner  // Add MDNode to the list of all metadata.
279198871cc90375246d8692680467ff6e810edac36Tanya Lattner  kernelMDArgs.push_back(llvm::MDNode::get(Context, argNames));
280198871cc90375246d8692680467ff6e810edac36Tanya Lattner}
281198871cc90375246d8692680467ff6e810edac36Tanya Lattner
2820df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattnervoid CodeGenFunction::EmitOpenCLKernelMetadata(const FunctionDecl *FD,
2830df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner                                               llvm::Function *Fn)
2840df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner{
2850df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner  if (!FD->hasAttr<OpenCLKernelAttr>())
2860df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner    return;
2870df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner
2880df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner  llvm::LLVMContext &Context = getLLVMContext();
2890df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner
2900df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner  llvm::SmallVector <llvm::Value*, 5> kernelMDArgs;
2910df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner  kernelMDArgs.push_back(Fn);
2920df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner
293198871cc90375246d8692680467ff6e810edac36Tanya Lattner  if (CGM.getCodeGenOpts().EmitOpenCLArgMetadata)
294198871cc90375246d8692680467ff6e810edac36Tanya Lattner    GenOpenCLArgMetadata(FD, Fn, CGM, Context, kernelMDArgs);
295198871cc90375246d8692680467ff6e810edac36Tanya Lattner
2960df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner  if (FD->hasAttr<WorkGroupSizeHintAttr>()) {
2970df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner    llvm::SmallVector <llvm::Value*, 5> attrMDArgs;
2980df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner    attrMDArgs.push_back(llvm::MDString::get(Context, "work_group_size_hint"));
2990df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner    WorkGroupSizeHintAttr *attr = FD->getAttr<WorkGroupSizeHintAttr>();
3000df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner    llvm::Type *iTy = llvm::IntegerType::get(Context, 32);
3010df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner    attrMDArgs.push_back(llvm::ConstantInt::get(iTy,
3020df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner       llvm::APInt(32, (uint64_t)attr->getXDim())));
3030df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner    attrMDArgs.push_back(llvm::ConstantInt::get(iTy,
3040df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner       llvm::APInt(32, (uint64_t)attr->getYDim())));
3050df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner    attrMDArgs.push_back(llvm::ConstantInt::get(iTy,
3060df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner       llvm::APInt(32, (uint64_t)attr->getZDim())));
3070df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner    kernelMDArgs.push_back(llvm::MDNode::get(Context, attrMDArgs));
3080df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner  }
3090df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner
3100df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner  if (FD->hasAttr<ReqdWorkGroupSizeAttr>()) {
3110df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner    llvm::SmallVector <llvm::Value*, 5> attrMDArgs;
3120df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner    attrMDArgs.push_back(llvm::MDString::get(Context, "reqd_work_group_size"));
3130df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner    ReqdWorkGroupSizeAttr *attr = FD->getAttr<ReqdWorkGroupSizeAttr>();
3140df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner    llvm::Type *iTy = llvm::IntegerType::get(Context, 32);
3150df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner    attrMDArgs.push_back(llvm::ConstantInt::get(iTy,
3160df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner       llvm::APInt(32, (uint64_t)attr->getXDim())));
3170df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner    attrMDArgs.push_back(llvm::ConstantInt::get(iTy,
3180df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner       llvm::APInt(32, (uint64_t)attr->getYDim())));
3190df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner    attrMDArgs.push_back(llvm::ConstantInt::get(iTy,
3200df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner       llvm::APInt(32, (uint64_t)attr->getZDim())));
3210df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner    kernelMDArgs.push_back(llvm::MDNode::get(Context, attrMDArgs));
3220df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner  }
3230df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner
3240df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner  llvm::MDNode *kernelMDNode = llvm::MDNode::get(Context, kernelMDArgs);
3250df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner  llvm::NamedMDNode *OpenCLKernelMetadata =
3260df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner    CGM.getModule().getOrInsertNamedMetadata("opencl.kernels");
3270df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner  OpenCLKernelMetadata->addOperand(kernelMDNode);
3280df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner}
3290df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner
3300ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlssonvoid CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy,
3317c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar                                    llvm::Function *Fn,
332d26bc76c98006609002d9930f8840490e88ac5b5John McCall                                    const CGFunctionInfo &FnInfo,
3332284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar                                    const FunctionArgList &Args,
3349c6082fe89c61af697f017aa80937581cc2128d8Tilmann Scheller                                    SourceLocation StartLoc) {
3350ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlsson  const Decl *D = GD.getDecl();
3360ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlsson
3374cc1a4703363ff940b6273aeef9d96a87edeb04bAnders Carlsson  DidCallStackSave = false;
338b5437d238752dc297e42410e98d38d5250fe0463Chris Lattner  CurCodeDecl = CurFuncDecl = D;
3397c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar  FnRetTy = RetTy;
340bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  CurFn = Fn;
341d26bc76c98006609002d9930f8840490e88ac5b5John McCall  CurFnInfo = &FnInfo;
3425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(CurFn->isDeclaration() && "Function already has body?");
343ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner
344a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen  // Pass inline keyword to optimizer if it appears explicitly on any
345a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen  // declaration.
3468fbe3855db0c341964bb550e13659505efe06c43Chad Rosier  if (!CGM.getCodeGenOpts().NoInline)
3478fbe3855db0c341964bb550e13659505efe06c43Chad Rosier    if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
3488fbe3855db0c341964bb550e13659505efe06c43Chad Rosier      for (FunctionDecl::redecl_iterator RI = FD->redecls_begin(),
3498fbe3855db0c341964bb550e13659505efe06c43Chad Rosier             RE = FD->redecls_end(); RI != RE; ++RI)
3508fbe3855db0c341964bb550e13659505efe06c43Chad Rosier        if (RI->isInlineSpecified()) {
351fac631052809b59c1b66f687c08a743de7fb50e9Bill Wendling          Fn->addFnAttr(llvm::Attributes::InlineHint);
3528fbe3855db0c341964bb550e13659505efe06c43Chad Rosier          break;
3538fbe3855db0c341964bb550e13659505efe06c43Chad Rosier        }
354a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen
3554e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getContext().getLangOpts().OpenCL) {
356f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne    // Add metadata for a kernel function.
357f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne    if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
3580df579ec000ffe52e0cddf1e7ee5e50a55256835Tanya Lattner      EmitOpenCLKernelMetadata(FD, Fn);
359f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne  }
360f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne
36155e874299f2ad827646a4ca9ea38c402aaeb38c9Daniel Dunbar  llvm::BasicBlock *EntryBB = createBasicBlock("entry", CurFn);
3625ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar
3635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Create a marker to make it easy to insert allocas into the entryblock
36455352a2d616cf9fbb621d10faf8b960b4b268bd8Chris Lattner  // later.  Don't create this with the builder, because we don't want it
36555352a2d616cf9fbb621d10faf8b960b4b268bd8Chris Lattner  // folded.
36677b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner  llvm::Value *Undef = llvm::UndefValue::get(Int32Ty);
36777b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner  AllocaInsertPt = new llvm::BitCastInst(Undef, Int32Ty, "", EntryBB);
368f1466848dce9c4c75d96a6cabdc8db560e26aac8Chris Lattner  if (Builder.isNamePreserving())
369f1466848dce9c4c75d96a6cabdc8db560e26aac8Chris Lattner    AllocaInsertPt->setName("allocapt");
3701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
371f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  ReturnBlock = getJumpDestInCurrentScope("return");
3721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
37355352a2d616cf9fbb621d10faf8b960b4b268bd8Chris Lattner  Builder.SetInsertPoint(EntryBB);
3741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
375af99417156c652a6f04dff643925036dc3241d60Sanjiv Gupta  // Emit subprogram debug descriptor.
376e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson  if (CGDebugInfo *DI = getDebugInfo()) {
37706253664315307d34ab57b892b6a0c6c5b3153bbEric Christopher    unsigned NumArgs = 0;
37806253664315307d34ab57b892b6a0c6c5b3153bbEric Christopher    QualType *ArgsArray = new QualType[Args.size()];
37906253664315307d34ab57b892b6a0c6c5b3153bbEric Christopher    for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
38006253664315307d34ab57b892b6a0c6c5b3153bbEric Christopher	 i != e; ++i) {
38106253664315307d34ab57b892b6a0c6c5b3153bbEric Christopher      ArgsArray[NumArgs++] = (*i)->getType();
38206253664315307d34ab57b892b6a0c6c5b3153bbEric Christopher    }
38306253664315307d34ab57b892b6a0c6c5b3153bbEric Christopher
384e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    QualType FnType =
38506253664315307d34ab57b892b6a0c6c5b3153bbEric Christopher      getContext().getFunctionType(RetTy, ArgsArray, NumArgs,
386e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall                                   FunctionProtoType::ExtProtoInfo());
387e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
3882a04f1cf72d57cf5d74b24e785c04f7a3fc3398fBenjamin Kramer    delete[] ArgsArray;
38906253664315307d34ab57b892b6a0c6c5b3153bbEric Christopher
3902284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar    DI->setLocation(StartLoc);
3919c6c3a0e3ae09626d2d4b04e4ffa42c3d7cab32bDevang Patel    DI->EmitFunctionStart(GD, FnType, CurFn, Builder);
392af99417156c652a6f04dff643925036dc3241d60Sanjiv Gupta  }
393af99417156c652a6f04dff643925036dc3241d60Sanjiv Gupta
394a18652fe1e8233fbf8b67484945c7f7b2bf272beDaniel Dunbar  if (ShouldInstrumentFunction())
395a18652fe1e8233fbf8b67484945c7f7b2bf272beDaniel Dunbar    EmitFunctionInstrumentation("__cyg_profile_func_enter");
3967255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner
397be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky  if (CGM.getCodeGenOpts().InstrumentForProfiling)
398be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky    EmitMCountInstrumentation();
399be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky
400b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman  if (RetTy->isVoidType()) {
401b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman    // Void type; nothing to return.
402b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman    ReturnValue = 0;
403b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman  } else if (CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect &&
404b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman             hasAggregateLLVMType(CurFnInfo->getReturnType())) {
405b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman    // Indirect aggregate return; emit returned value directly into sret slot.
406647a1ec397fa13af176d07d9f5d071560a94c7a9Daniel Dunbar    // This reduces code size, and affects correctness in C++.
407b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman    ReturnValue = CurFn->arg_begin();
408b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman  } else {
409647a1ec397fa13af176d07d9f5d071560a94c7a9Daniel Dunbar    ReturnValue = CreateIRTemp(RetTy, "retval");
410f85e193739c953358c865005855253af4f68a497John McCall
411f85e193739c953358c865005855253af4f68a497John McCall    // Tell the epilog emitter to autorelease the result.  We do this
412f85e193739c953358c865005855253af4f68a497John McCall    // now so that various specialized functions can suppress it
413f85e193739c953358c865005855253af4f68a497John McCall    // during their IR-generation.
4144e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (getLangOpts().ObjCAutoRefCount &&
415f85e193739c953358c865005855253af4f68a497John McCall        !CurFnInfo->isReturnsRetained() &&
416f85e193739c953358c865005855253af4f68a497John McCall        RetTy->isObjCRetainableType())
417f85e193739c953358c865005855253af4f68a497John McCall      AutoreleaseResult = true;
418b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman  }
419b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman
420cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump  EmitStartEHSpec(CurCodeDecl);
421f85e193739c953358c865005855253af4f68a497John McCall
422f85e193739c953358c865005855253af4f68a497John McCall  PrologueCleanupDepth = EHStack.stable_begin();
42388b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar  EmitFunctionProlog(*CurFnInfo, CurFn, Args);
4241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
425cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman  if (D && isa<CXXMethodDecl>(D) && cast<CXXMethodDecl>(D)->isInstance()) {
4264c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    CGM.getCXXABI().EmitInstanceFunctionProlog(*this);
427cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman    const CXXMethodDecl *MD = cast<CXXMethodDecl>(D);
428cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman    if (MD->getParent()->isLambda() &&
429cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman        MD->getOverloadedOperator() == OO_Call) {
430cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman      // We're in a lambda; figure out the captures.
431cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman      MD->getParent()->getCaptureFields(LambdaCaptureFields,
432cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman                                        LambdaThisCaptureField);
433cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman      if (LambdaThisCaptureField) {
434cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman        // If this lambda captures this, load it.
435377ecc7996dce6803f7b7b6208cab5e197c9c5b8Eli Friedman        QualType LambdaTagType =
436377ecc7996dce6803f7b7b6208cab5e197c9c5b8Eli Friedman            getContext().getTagDeclType(LambdaThisCaptureField->getParent());
437377ecc7996dce6803f7b7b6208cab5e197c9c5b8Eli Friedman        LValue LambdaLV = MakeNaturalAlignAddrLValue(CXXABIThisValue,
438377ecc7996dce6803f7b7b6208cab5e197c9c5b8Eli Friedman                                                     LambdaTagType);
439377ecc7996dce6803f7b7b6208cab5e197c9c5b8Eli Friedman        LValue ThisLValue = EmitLValueForField(LambdaLV,
440377ecc7996dce6803f7b7b6208cab5e197c9c5b8Eli Friedman                                               LambdaThisCaptureField);
441cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman        CXXThisValue = EmitLoadOfLValue(ThisLValue).getScalarVal();
442cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman      }
443cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman    } else {
444cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman      // Not in a lambda; just use 'this' from the method.
445cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman      // FIXME: Should we generate a new load for each use of 'this'?  The
446cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman      // fast register allocator would be happier...
447cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman      CXXThisValue = CXXABIThisValue;
448cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman    }
449cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman  }
4502504941793b549323f9d29c62507cf21d865fadeJohn McCall
451751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson  // If any of the arguments have a variably modified type, make sure to
452751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson  // emit the type size.
453751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson  for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
454751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson       i != e; ++i) {
455d26bc76c98006609002d9930f8840490e88ac5b5John McCall    QualType Ty = (*i)->getType();
456751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson
457751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson    if (Ty->isVariablyModifiedType())
458bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      EmitVariablyModifiedType(Ty);
459751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson  }
46073fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher  // Emit a location at the end of the prologue.
46173fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher  if (CGDebugInfo *DI = getDebugInfo())
46273fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher    DI->EmitLocation(Builder, StartLoc);
4637c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar}
464eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman
4659fc6a7774643a810c8501dae2323e863fefb623eJohn McCallvoid CodeGenFunction::EmitFunctionBody(FunctionArgList &Args) {
4669fc6a7774643a810c8501dae2323e863fefb623eJohn McCall  const FunctionDecl *FD = cast<FunctionDecl>(CurGD.getDecl());
46706a9f3680d22529a2fcf20c52d71cf221d99d910Douglas Gregor  assert(FD->getBody());
46806a9f3680d22529a2fcf20c52d71cf221d99d910Douglas Gregor  EmitStmt(FD->getBody());
469a355e07454463b19829ac92ffd115a097faff0e0John McCall}
470a355e07454463b19829ac92ffd115a097faff0e0John McCall
47139dad53772c42eb36ebec1c81c56ba99d038fb94John McCall/// Tries to mark the given function nounwind based on the
47239dad53772c42eb36ebec1c81c56ba99d038fb94John McCall/// non-existence of any throwing calls within it.  We believe this is
47339dad53772c42eb36ebec1c81c56ba99d038fb94John McCall/// lightweight enough to do at -O0.
47439dad53772c42eb36ebec1c81c56ba99d038fb94John McCallstatic void TryMarkNoThrow(llvm::Function *F) {
475b3a29f132794f67108bccc9c7cc3795365e8a965John McCall  // LLVM treats 'nounwind' on a function as part of the type, so we
476b3a29f132794f67108bccc9c7cc3795365e8a965John McCall  // can't do this on functions that can be overwritten.
477b3a29f132794f67108bccc9c7cc3795365e8a965John McCall  if (F->mayBeOverridden()) return;
478b3a29f132794f67108bccc9c7cc3795365e8a965John McCall
47939dad53772c42eb36ebec1c81c56ba99d038fb94John McCall  for (llvm::Function::iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI)
48039dad53772c42eb36ebec1c81c56ba99d038fb94John McCall    for (llvm::BasicBlock::iterator
48139dad53772c42eb36ebec1c81c56ba99d038fb94John McCall           BI = FI->begin(), BE = FI->end(); BI != BE; ++BI)
482285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling      if (llvm::CallInst *Call = dyn_cast<llvm::CallInst>(&*BI)) {
48339dad53772c42eb36ebec1c81c56ba99d038fb94John McCall        if (!Call->doesNotThrow())
48439dad53772c42eb36ebec1c81c56ba99d038fb94John McCall          return;
485285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling      } else if (isa<llvm::ResumeInst>(&*BI)) {
486285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling        return;
487285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling      }
488fac631052809b59c1b66f687c08a743de7fb50e9Bill Wendling  F->setDoesNotThrow();
48939dad53772c42eb36ebec1c81c56ba99d038fb94John McCall}
49039dad53772c42eb36ebec1c81c56ba99d038fb94John McCall
491d26bc76c98006609002d9930f8840490e88ac5b5John McCallvoid CodeGenFunction::GenerateCode(GlobalDecl GD, llvm::Function *Fn,
492d26bc76c98006609002d9930f8840490e88ac5b5John McCall                                   const CGFunctionInfo &FnInfo) {
4930ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlsson  const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
4940ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlsson
495e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson  // Check if we should generate debug info for this function.
496a240df2ec1b374b3e9e7f760875ffb17cd64506fAlexey Samsonov  if (!FD->hasAttr<NoDebugAttr>())
497a240df2ec1b374b3e9e7f760875ffb17cd64506fAlexey Samsonov    maybeInitializeDebugInfo();
4981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4997c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar  FunctionArgList Args;
5004c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  QualType ResTy = FD->getResultType();
5011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5026a1e0eb557d47e85185e09bdf8721f53f4bf9c9cMike Stump  CurGD = GD;
5034c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  if (isa<CXXMethodDecl>(FD) && cast<CXXMethodDecl>(FD)->isInstance())
5044c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    CGM.getCXXABI().BuildInstanceFunctionParams(*this, ResTy, Args);
5051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5066e94f6c0f124c9a88b3dae0eea5e6b27957df183Chad Rosier  for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i)
5076e94f6c0f124c9a88b3dae0eea5e6b27957df183Chad Rosier    Args.push_back(FD->getParamDecl(i));
508af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
509a355e07454463b19829ac92ffd115a097faff0e0John McCall  SourceRange BodyRange;
510a355e07454463b19829ac92ffd115a097faff0e0John McCall  if (Stmt *Body = FD->getBody()) BodyRange = Body->getSourceRange();
5114365bba95be15342575b4f030c6583a770a5da3dAnders Carlsson
512a355e07454463b19829ac92ffd115a097faff0e0John McCall  // Emit the standard function prologue.
513d26bc76c98006609002d9930f8840490e88ac5b5John McCall  StartFunction(GD, ResTy, Fn, FnInfo, Args, BodyRange.getBegin());
5141851a12605bc6f1ea70d11974a315340ebaab6ebAnders Carlsson
515a355e07454463b19829ac92ffd115a097faff0e0John McCall  // Generate the body of the function.
5169fc6a7774643a810c8501dae2323e863fefb623eJohn McCall  if (isa<CXXDestructorDecl>(FD))
5179fc6a7774643a810c8501dae2323e863fefb623eJohn McCall    EmitDestructorBody(Args);
5189fc6a7774643a810c8501dae2323e863fefb623eJohn McCall  else if (isa<CXXConstructorDecl>(FD))
5199fc6a7774643a810c8501dae2323e863fefb623eJohn McCall    EmitConstructorBody(Args);
5204e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  else if (getContext().getLangOpts().CUDA &&
521a4ae2294b6ebfb2554aacb6a6a0682fb5ed1f276Peter Collingbourne           !CGM.getCodeGenOpts().CUDAIsDevice &&
522a4ae2294b6ebfb2554aacb6a6a0682fb5ed1f276Peter Collingbourne           FD->hasAttr<CUDAGlobalAttr>())
523a4ae2294b6ebfb2554aacb6a6a0682fb5ed1f276Peter Collingbourne    CGM.getCUDARuntime().EmitDeviceStubBody(*this, Args);
524bd89f8c2caa9550e41daa1aa9bf30f0f1e0dfaf7Eli Friedman  else if (isa<CXXConversionDecl>(FD) &&
52527dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor           cast<CXXConversionDecl>(FD)->isLambdaToBlockPointerConversion()) {
52627dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor    // The lambda conversion to block pointer is special; the semantics can't be
52727dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor    // expressed in the AST, so IRGen needs to special-case it.
52827dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor    EmitLambdaToBlockPointerBody(Args);
52927dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor  } else if (isa<CXXMethodDecl>(FD) &&
53027dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor             cast<CXXMethodDecl>(FD)->isLambdaStaticInvoker()) {
53127dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor    // The lambda "__invoke" function is special, because it forwards or
53227dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor    // clones the body of the function call operator (but is actually static).
53327dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor    EmitLambdaStaticInvokeFunction(cast<CXXMethodDecl>(FD));
534bd89f8c2caa9550e41daa1aa9bf30f0f1e0dfaf7Eli Friedman  }
5359fc6a7774643a810c8501dae2323e863fefb623eJohn McCall  else
5369fc6a7774643a810c8501dae2323e863fefb623eJohn McCall    EmitFunctionBody(Args);
537c33e4ba20304e222692e77f2c6ad26a5d8d32f83Anders Carlsson
53836ef0d54cfddf31cd48816e78ab4db73b31a6c1dRichard Smith  // C++11 [stmt.return]p2:
53936ef0d54cfddf31cd48816e78ab4db73b31a6c1dRichard Smith  //   Flowing off the end of a function [...] results in undefined behavior in
54036ef0d54cfddf31cd48816e78ab4db73b31a6c1dRichard Smith  //   a value-returning function.
54136ef0d54cfddf31cd48816e78ab4db73b31a6c1dRichard Smith  // C11 6.9.1p12:
54236ef0d54cfddf31cd48816e78ab4db73b31a6c1dRichard Smith  //   If the '}' that terminates a function is reached, and the value of the
54336ef0d54cfddf31cd48816e78ab4db73b31a6c1dRichard Smith  //   function call is used by the caller, the behavior is undefined.
54436ef0d54cfddf31cd48816e78ab4db73b31a6c1dRichard Smith  if (getContext().getLangOpts().CPlusPlus && !FD->hasImplicitReturnZero() &&
54536ef0d54cfddf31cd48816e78ab4db73b31a6c1dRichard Smith      !FD->getResultType()->isVoidType() && Builder.GetInsertBlock()) {
54636ef0d54cfddf31cd48816e78ab4db73b31a6c1dRichard Smith    if (CatchUndefined)
5474def70d3040e73707c738f7c366737a986135edfRichard Smith      EmitCheck(Builder.getFalse(), "missing_return",
5484def70d3040e73707c738f7c366737a986135edfRichard Smith                EmitCheckSourceLocation(FD->getLocation()),
5494def70d3040e73707c738f7c366737a986135edfRichard Smith                llvm::ArrayRef<llvm::Value*>());
550802cd5b1974bab2038b9d3c969d9beb0ab34f435Richard Smith    else if (CGM.getCodeGenOpts().OptimizationLevel == 0)
551802cd5b1974bab2038b9d3c969d9beb0ab34f435Richard Smith      Builder.CreateCall(CGM.getIntrinsic(llvm::Intrinsic::trap));
55236ef0d54cfddf31cd48816e78ab4db73b31a6c1dRichard Smith    Builder.CreateUnreachable();
55336ef0d54cfddf31cd48816e78ab4db73b31a6c1dRichard Smith    Builder.ClearInsertionPoint();
55436ef0d54cfddf31cd48816e78ab4db73b31a6c1dRichard Smith  }
55536ef0d54cfddf31cd48816e78ab4db73b31a6c1dRichard Smith
556a355e07454463b19829ac92ffd115a097faff0e0John McCall  // Emit the standard function epilogue.
557a355e07454463b19829ac92ffd115a097faff0e0John McCall  FinishFunction(BodyRange.getEnd());
55839dad53772c42eb36ebec1c81c56ba99d038fb94John McCall
55939dad53772c42eb36ebec1c81c56ba99d038fb94John McCall  // If we haven't marked the function nothrow through other means, do
56039dad53772c42eb36ebec1c81c56ba99d038fb94John McCall  // a quick pass now to see if we can.
56139dad53772c42eb36ebec1c81c56ba99d038fb94John McCall  if (!CurFn->doesNotThrow())
56239dad53772c42eb36ebec1c81c56ba99d038fb94John McCall    TryMarkNoThrow(CurFn);
5635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
5645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5650946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner/// ContainsLabel - Return true if the statement contains a label in it.  If
5660946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner/// this statement is not executed normally, it not containing a label means
5670946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner/// that we can just remove the code.
5680946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattnerbool CodeGenFunction::ContainsLabel(const Stmt *S, bool IgnoreCaseStmts) {
5690946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // Null statement, not a label!
5700946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  if (S == 0) return false;
5711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5720946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // If this is a label, we have to emit the code, consider something like:
5730946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // if (0) {  ...  foo:  bar(); }  goto foo;
574ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  //
575ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  // TODO: If anyone cared, we could track __label__'s, since we know that you
576ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  // can't jump to one from outside their declared region.
5770946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  if (isa<LabelStmt>(S))
5780946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner    return true;
579ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner
5800946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // If this is a case/default statement, and we haven't seen a switch, we have
5810946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // to emit the code.
5820946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  if (isa<SwitchCase>(S) && !IgnoreCaseStmts)
5830946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner    return true;
5841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5850946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // If this is a switch statement, we want to ignore cases below it.
5860946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  if (isa<SwitchStmt>(S))
5870946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner    IgnoreCaseStmts = true;
5881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5890946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // Scan subexpressions for verboten labels.
5907502c1d3ce8bb97bcc4f7bebef507040bd93b26fJohn McCall  for (Stmt::const_child_range I = S->children(); I; ++I)
5910946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner    if (ContainsLabel(*I, IgnoreCaseStmts))
5920946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner      return true;
5931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5940946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  return false;
5950946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner}
5960946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner
597ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner/// containsBreak - Return true if the statement contains a break out of it.
598ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner/// If the statement (recursively) contains a switch or loop with a break
599ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner/// inside of it, this is fine.
600ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattnerbool CodeGenFunction::containsBreak(const Stmt *S) {
601ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  // Null statement, not a label!
602ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  if (S == 0) return false;
603ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner
604ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  // If this is a switch or loop that defines its own break scope, then we can
605ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  // include it and anything inside of it.
606ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || isa<DoStmt>(S) ||
607ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner      isa<ForStmt>(S))
6082bef7f5499541e3b68f114cc4d7d197e9a902fe7Chris Lattner    return false;
6092bef7f5499541e3b68f114cc4d7d197e9a902fe7Chris Lattner
6102bef7f5499541e3b68f114cc4d7d197e9a902fe7Chris Lattner  if (isa<BreakStmt>(S))
611ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner    return true;
612ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner
613ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  // Scan subexpressions for verboten breaks.
614ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  for (Stmt::const_child_range I = S->children(); I; ++I)
615ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner    if (containsBreak(*I))
616ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner      return true;
617ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner
618ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  return false;
619ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner}
620ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner
62131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
622c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner/// ConstantFoldsToSimpleInteger - If the specified expression does not fold
623c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner/// to a constant, or if it does but contains a label, return false.  If it
624c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner/// constant folds return true and set the boolean result in Result.
625c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattnerbool CodeGenFunction::ConstantFoldsToSimpleInteger(const Expr *Cond,
626c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner                                                   bool &ResultBool) {
627e1ecdc168175719d74e112bcacd4aae5e12d4631Richard Trieu  llvm::APSInt ResultInt;
628ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  if (!ConstantFoldsToSimpleInteger(Cond, ResultInt))
629ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner    return false;
630ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner
631ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  ResultBool = ResultInt.getBoolValue();
632ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  return true;
633ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner}
634ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner
635ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner/// ConstantFoldsToSimpleInteger - If the specified expression does not fold
636ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner/// to a constant, or if it does but contains a label, return false.  If it
637ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner/// constant folds return true and set the folded value.
638ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattnerbool CodeGenFunction::
639e1ecdc168175719d74e112bcacd4aae5e12d4631Richard TrieuConstantFoldsToSimpleInteger(const Expr *Cond, llvm::APSInt &ResultInt) {
64036bc14c3a1cf63ee306df5687ac8e85f924f8639Daniel Dunbar  // FIXME: Rename and handle conversion of other evaluatable things
64136bc14c3a1cf63ee306df5687ac8e85f924f8639Daniel Dunbar  // to bool.
64280d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  llvm::APSInt Int;
64380d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  if (!Cond->EvaluateAsInt(Int, getContext()))
644c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner    return false;  // Not foldable, not integer or not fully evaluatable.
64580d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith
64631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  if (CodeGenFunction::ContainsLabel(Cond))
647c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner    return false;  // Contains a label.
64880d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith
64980d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  ResultInt = Int;
650c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner  return true;
65131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner}
65231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
65331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
654ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner
65531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an if
65631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// statement) to the specified blocks.  Based on the condition, this might try
65731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// to simplify the codegen of the conditional based on the branch.
65831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner///
65931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattnervoid CodeGenFunction::EmitBranchOnBoolExpr(const Expr *Cond,
66031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner                                           llvm::BasicBlock *TrueBlock,
66131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner                                           llvm::BasicBlock *FalseBlock) {
662f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  Cond = Cond->IgnoreParens();
6631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
66431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  if (const BinaryOperator *CondBOp = dyn_cast<BinaryOperator>(Cond)) {
66531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner    // Handle X && Y in a condition.
6662de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    if (CondBOp->getOpcode() == BO_LAnd) {
66731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // If we have "1 && X", simplify the code.  "0 && X" would have constant
66831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // folded if the case was simple enough.
669e3eb83b93751544a5fab19b3824f56aeac454f82Bill Wendling      bool ConstantBool = false;
670c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner      if (ConstantFoldsToSimpleInteger(CondBOp->getLHS(), ConstantBool) &&
671c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner          ConstantBool) {
67231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        // br(1 && X) -> br(X).
67331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
67431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      }
6751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
67631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // If we have "X && 1", simplify the code to use an uncond branch.
67731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // "X && 0" would have been constant folded to 0.
678c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner      if (ConstantFoldsToSimpleInteger(CondBOp->getRHS(), ConstantBool) &&
679c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner          ConstantBool) {
68031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        // br(X && 1) -> br(X).
68131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock);
68231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      }
6831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
68431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // Emit the LHS as a conditional.  If the LHS conditional is false, we
68531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // want to jump to the FalseBlock.
6869615ecb44f549ae9fa2b4db6ff46bc78befbf62cDaniel Dunbar      llvm::BasicBlock *LHSTrue = createBasicBlock("land.lhs.true");
687150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
688150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      ConditionalEvaluation eval(*this);
68931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBranchOnBoolExpr(CondBOp->getLHS(), LHSTrue, FalseBlock);
69031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBlock(LHSTrue);
6911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
69208e9e453f40aff95a59bd67db49b8f050765e1f0Anders Carlsson      // Any temporaries created here are conditional.
693150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      eval.begin(*this);
69431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
695150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      eval.end(*this);
69608e9e453f40aff95a59bd67db49b8f050765e1f0Anders Carlsson
69731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      return;
698c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner    }
699c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner
700c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner    if (CondBOp->getOpcode() == BO_LOr) {
70131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // If we have "0 || X", simplify the code.  "1 || X" would have constant
70231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // folded if the case was simple enough.
703e3eb83b93751544a5fab19b3824f56aeac454f82Bill Wendling      bool ConstantBool = false;
704c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner      if (ConstantFoldsToSimpleInteger(CondBOp->getLHS(), ConstantBool) &&
705c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner          !ConstantBool) {
70631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        // br(0 || X) -> br(X).
70731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
70831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      }
7091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
71031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // If we have "X || 0", simplify the code to use an uncond branch.
71131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // "X || 1" would have been constant folded to 1.
712c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner      if (ConstantFoldsToSimpleInteger(CondBOp->getRHS(), ConstantBool) &&
713c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner          !ConstantBool) {
71431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        // br(X || 0) -> br(X).
71531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock);
71631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      }
7171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
71831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // Emit the LHS as a conditional.  If the LHS conditional is true, we
71931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // want to jump to the TrueBlock.
7209615ecb44f549ae9fa2b4db6ff46bc78befbf62cDaniel Dunbar      llvm::BasicBlock *LHSFalse = createBasicBlock("lor.lhs.false");
721150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
722150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      ConditionalEvaluation eval(*this);
72331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, LHSFalse);
72431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBlock(LHSFalse);
7251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
72608e9e453f40aff95a59bd67db49b8f050765e1f0Anders Carlsson      // Any temporaries created here are conditional.
727150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      eval.begin(*this);
72831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
729150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      eval.end(*this);
73008e9e453f40aff95a59bd67db49b8f050765e1f0Anders Carlsson
73131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      return;
73231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner    }
733552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner  }
7341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
735552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner  if (const UnaryOperator *CondUOp = dyn_cast<UnaryOperator>(Cond)) {
736552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner    // br(!x, t, f) -> br(x, f, t)
7372de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    if (CondUOp->getOpcode() == UO_LNot)
738552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner      return EmitBranchOnBoolExpr(CondUOp->getSubExpr(), FalseBlock, TrueBlock);
73931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  }
7401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
74109b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar  if (const ConditionalOperator *CondOp = dyn_cast<ConditionalOperator>(Cond)) {
742df33a35f6010fea92c786c0d8f85bfd7c73ebd3eEli Friedman    // br(c ? x : y, t, f) -> br(c, br(x, t, f), br(y, t, f))
743df33a35f6010fea92c786c0d8f85bfd7c73ebd3eEli Friedman    llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true");
744df33a35f6010fea92c786c0d8f85bfd7c73ebd3eEli Friedman    llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false");
74509b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar
746df33a35f6010fea92c786c0d8f85bfd7c73ebd3eEli Friedman    ConditionalEvaluation cond(*this);
747df33a35f6010fea92c786c0d8f85bfd7c73ebd3eEli Friedman    EmitBranchOnBoolExpr(CondOp->getCond(), LHSBlock, RHSBlock);
748150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
749df33a35f6010fea92c786c0d8f85bfd7c73ebd3eEli Friedman    cond.begin(*this);
750df33a35f6010fea92c786c0d8f85bfd7c73ebd3eEli Friedman    EmitBlock(LHSBlock);
751df33a35f6010fea92c786c0d8f85bfd7c73ebd3eEli Friedman    EmitBranchOnBoolExpr(CondOp->getLHS(), TrueBlock, FalseBlock);
752df33a35f6010fea92c786c0d8f85bfd7c73ebd3eEli Friedman    cond.end(*this);
753150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
754df33a35f6010fea92c786c0d8f85bfd7c73ebd3eEli Friedman    cond.begin(*this);
755df33a35f6010fea92c786c0d8f85bfd7c73ebd3eEli Friedman    EmitBlock(RHSBlock);
756df33a35f6010fea92c786c0d8f85bfd7c73ebd3eEli Friedman    EmitBranchOnBoolExpr(CondOp->getRHS(), TrueBlock, FalseBlock);
757df33a35f6010fea92c786c0d8f85bfd7c73ebd3eEli Friedman    cond.end(*this);
758150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
759df33a35f6010fea92c786c0d8f85bfd7c73ebd3eEli Friedman    return;
76009b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar  }
76109b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar
76231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  // Emit the code with the fully general case.
76331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  llvm::Value *CondV = EvaluateExprAsBool(Cond);
76431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  Builder.CreateCondBr(CondV, TrueBlock, FalseBlock);
76531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner}
76631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
767488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar/// ErrorUnsupported - Print out an error that codegen doesn't support the
768dc5e8268292046114ffe02e48773572a91a310f1Chris Lattner/// specified stmt yet.
76990df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbarvoid CodeGenFunction::ErrorUnsupported(const Stmt *S, const char *Type,
77090df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar                                       bool OmitOnError) {
77190df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar  CGM.ErrorUnsupported(S, Type, OmitOnError);
772dc5e8268292046114ffe02e48773572a91a310f1Chris Lattner}
773dc5e8268292046114ffe02e48773572a91a310f1Chris Lattner
7747143325db76d6c3dabce82500f8cc7c93a941970John McCall/// emitNonZeroVLAInit - Emit the "zero" initialization of a
7757143325db76d6c3dabce82500f8cc7c93a941970John McCall/// variable-length array whose elements have a non-zero bit-pattern.
7767143325db76d6c3dabce82500f8cc7c93a941970John McCall///
7772ee5ba35febf830d366b65dd0dcbf8e291c41342James Dennett/// \param baseType the inner-most element type of the array
7787143325db76d6c3dabce82500f8cc7c93a941970John McCall/// \param src - a char* pointing to the bit-pattern for a single
7797143325db76d6c3dabce82500f8cc7c93a941970John McCall/// base element of the array
7807143325db76d6c3dabce82500f8cc7c93a941970John McCall/// \param sizeInChars - the total size of the VLA, in chars
7817143325db76d6c3dabce82500f8cc7c93a941970John McCallstatic void emitNonZeroVLAInit(CodeGenFunction &CGF, QualType baseType,
7827143325db76d6c3dabce82500f8cc7c93a941970John McCall                               llvm::Value *dest, llvm::Value *src,
7837143325db76d6c3dabce82500f8cc7c93a941970John McCall                               llvm::Value *sizeInChars) {
7847143325db76d6c3dabce82500f8cc7c93a941970John McCall  std::pair<CharUnits,CharUnits> baseSizeAndAlign
7857143325db76d6c3dabce82500f8cc7c93a941970John McCall    = CGF.getContext().getTypeInfoInChars(baseType);
7867143325db76d6c3dabce82500f8cc7c93a941970John McCall
7877143325db76d6c3dabce82500f8cc7c93a941970John McCall  CGBuilderTy &Builder = CGF.Builder;
7887143325db76d6c3dabce82500f8cc7c93a941970John McCall
7897143325db76d6c3dabce82500f8cc7c93a941970John McCall  llvm::Value *baseSizeInChars
7907143325db76d6c3dabce82500f8cc7c93a941970John McCall    = llvm::ConstantInt::get(CGF.IntPtrTy, baseSizeAndAlign.first.getQuantity());
7917143325db76d6c3dabce82500f8cc7c93a941970John McCall
7922acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *i8p = Builder.getInt8PtrTy();
7937143325db76d6c3dabce82500f8cc7c93a941970John McCall
7947143325db76d6c3dabce82500f8cc7c93a941970John McCall  llvm::Value *begin = Builder.CreateBitCast(dest, i8p, "vla.begin");
7957143325db76d6c3dabce82500f8cc7c93a941970John McCall  llvm::Value *end = Builder.CreateInBoundsGEP(dest, sizeInChars, "vla.end");
7967143325db76d6c3dabce82500f8cc7c93a941970John McCall
7977143325db76d6c3dabce82500f8cc7c93a941970John McCall  llvm::BasicBlock *originBB = CGF.Builder.GetInsertBlock();
7987143325db76d6c3dabce82500f8cc7c93a941970John McCall  llvm::BasicBlock *loopBB = CGF.createBasicBlock("vla-init.loop");
7997143325db76d6c3dabce82500f8cc7c93a941970John McCall  llvm::BasicBlock *contBB = CGF.createBasicBlock("vla-init.cont");
8007143325db76d6c3dabce82500f8cc7c93a941970John McCall
8017143325db76d6c3dabce82500f8cc7c93a941970John McCall  // Make a loop over the VLA.  C99 guarantees that the VLA element
8027143325db76d6c3dabce82500f8cc7c93a941970John McCall  // count must be nonzero.
8037143325db76d6c3dabce82500f8cc7c93a941970John McCall  CGF.EmitBlock(loopBB);
8047143325db76d6c3dabce82500f8cc7c93a941970John McCall
805bbf3bacb3e0c1ebb3e8a4a8b1330404a7e379315Jay Foad  llvm::PHINode *cur = Builder.CreatePHI(i8p, 2, "vla.cur");
8067143325db76d6c3dabce82500f8cc7c93a941970John McCall  cur->addIncoming(begin, originBB);
8077143325db76d6c3dabce82500f8cc7c93a941970John McCall
8087143325db76d6c3dabce82500f8cc7c93a941970John McCall  // memcpy the individual element bit-pattern.
8097143325db76d6c3dabce82500f8cc7c93a941970John McCall  Builder.CreateMemCpy(cur, src, baseSizeInChars,
8107143325db76d6c3dabce82500f8cc7c93a941970John McCall                       baseSizeAndAlign.second.getQuantity(),
8117143325db76d6c3dabce82500f8cc7c93a941970John McCall                       /*volatile*/ false);
8127143325db76d6c3dabce82500f8cc7c93a941970John McCall
8137143325db76d6c3dabce82500f8cc7c93a941970John McCall  // Go to the next element.
8147143325db76d6c3dabce82500f8cc7c93a941970John McCall  llvm::Value *next = Builder.CreateConstInBoundsGEP1_32(cur, 1, "vla.next");
8157143325db76d6c3dabce82500f8cc7c93a941970John McCall
8167143325db76d6c3dabce82500f8cc7c93a941970John McCall  // Leave if that's the end of the VLA.
8177143325db76d6c3dabce82500f8cc7c93a941970John McCall  llvm::Value *done = Builder.CreateICmpEQ(next, end, "vla-init.isdone");
8187143325db76d6c3dabce82500f8cc7c93a941970John McCall  Builder.CreateCondBr(done, contBB, loopBB);
8197143325db76d6c3dabce82500f8cc7c93a941970John McCall  cur->addIncoming(next, loopBB);
8207143325db76d6c3dabce82500f8cc7c93a941970John McCall
8217143325db76d6c3dabce82500f8cc7c93a941970John McCall  CGF.EmitBlock(contBB);
8227143325db76d6c3dabce82500f8cc7c93a941970John McCall}
8237143325db76d6c3dabce82500f8cc7c93a941970John McCall
8241884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlssonvoid
8251884eb0b5c55edda4893ddec45e7dbad79758782Anders CarlssonCodeGenFunction::EmitNullInitialization(llvm::Value *DestPtr, QualType Ty) {
8260d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson  // Ignore empty classes in C++.
8274e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getContext().getLangOpts().CPlusPlus) {
8280d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson    if (const RecordType *RT = Ty->getAs<RecordType>()) {
8290d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson      if (cast<CXXRecordDecl>(RT->getDecl())->isEmpty())
8300d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson        return;
8310d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson    }
8320d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson  }
8339021718882441dd391a1960084580d3cd19c423aJohn McCall
8349021718882441dd391a1960084580d3cd19c423aJohn McCall  // Cast the dest ptr to the appropriate i8 pointer type.
8359021718882441dd391a1960084580d3cd19c423aJohn McCall  unsigned DestAS =
8369021718882441dd391a1960084580d3cd19c423aJohn McCall    cast<llvm::PointerType>(DestPtr->getType())->getAddressSpace();
8372acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *BP = Builder.getInt8PtrTy(DestAS);
8383d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson  if (DestPtr->getType() != BP)
839578faa837b552403e2002b97fdfbfde14f2448e5Benjamin Kramer    DestPtr = Builder.CreateBitCast(DestPtr, BP);
8403d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson
8413d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson  // Get size and alignment info for this aggregate.
84279be76c0e360d9e9c44285c9838af02adc43e55fKen Dyck  std::pair<CharUnits, CharUnits> TypeInfo =
84379be76c0e360d9e9c44285c9838af02adc43e55fKen Dyck    getContext().getTypeInfoInChars(Ty);
84479be76c0e360d9e9c44285c9838af02adc43e55fKen Dyck  CharUnits Size = TypeInfo.first;
84579be76c0e360d9e9c44285c9838af02adc43e55fKen Dyck  CharUnits Align = TypeInfo.second;
8463d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson
8475576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall  llvm::Value *SizeVal;
8487143325db76d6c3dabce82500f8cc7c93a941970John McCall  const VariableArrayType *vla;
8499021718882441dd391a1960084580d3cd19c423aJohn McCall
8505576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall  // Don't bother emitting a zero-byte memset.
85179be76c0e360d9e9c44285c9838af02adc43e55fKen Dyck  if (Size.isZero()) {
8525576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall    // But note that getTypeInfo returns 0 for a VLA.
8535576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall    if (const VariableArrayType *vlaType =
8545576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall          dyn_cast_or_null<VariableArrayType>(
8555576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall                                          getContext().getAsArrayType(Ty))) {
856bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      QualType eltType;
857bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      llvm::Value *numElts;
858bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      llvm::tie(numElts, eltType) = getVLASize(vlaType);
859bc8d40d85f3fa1e34569834916f18fecaa635152John McCall
860bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      SizeVal = numElts;
861bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      CharUnits eltSize = getContext().getTypeSizeInChars(eltType);
862bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      if (!eltSize.isOne())
863bc8d40d85f3fa1e34569834916f18fecaa635152John McCall        SizeVal = Builder.CreateNUWMul(SizeVal, CGM.getSize(eltSize));
8647143325db76d6c3dabce82500f8cc7c93a941970John McCall      vla = vlaType;
8655576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall    } else {
8665576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall      return;
8675576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall    }
8685576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall  } else {
869bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    SizeVal = CGM.getSize(Size);
8707143325db76d6c3dabce82500f8cc7c93a941970John McCall    vla = 0;
8715576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall  }
8729021718882441dd391a1960084580d3cd19c423aJohn McCall
8739021718882441dd391a1960084580d3cd19c423aJohn McCall  // If the type contains a pointer to data member we can't memset it to zero.
8749021718882441dd391a1960084580d3cd19c423aJohn McCall  // Instead, create a null constant and copy it to the destination.
8757143325db76d6c3dabce82500f8cc7c93a941970John McCall  // TODO: there are other patterns besides zero that we can usefully memset,
8767143325db76d6c3dabce82500f8cc7c93a941970John McCall  // like -1, which happens to be the pattern used by member-pointers.
877f16aa103d3afd42fbca2ab346f191bf745cec092John McCall  if (!CGM.getTypes().isZeroInitializable(Ty)) {
8787143325db76d6c3dabce82500f8cc7c93a941970John McCall    // For a VLA, emit a single element, then splat that over the VLA.
8797143325db76d6c3dabce82500f8cc7c93a941970John McCall    if (vla) Ty = getContext().getBaseElementType(vla);
8805576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall
8819021718882441dd391a1960084580d3cd19c423aJohn McCall    llvm::Constant *NullConstant = CGM.EmitNullConstant(Ty);
8829021718882441dd391a1960084580d3cd19c423aJohn McCall
8839021718882441dd391a1960084580d3cd19c423aJohn McCall    llvm::GlobalVariable *NullVariable =
8849021718882441dd391a1960084580d3cd19c423aJohn McCall      new llvm::GlobalVariable(CGM.getModule(), NullConstant->getType(),
8859021718882441dd391a1960084580d3cd19c423aJohn McCall                               /*isConstant=*/true,
8869021718882441dd391a1960084580d3cd19c423aJohn McCall                               llvm::GlobalVariable::PrivateLinkage,
8875f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                               NullConstant, Twine());
8889021718882441dd391a1960084580d3cd19c423aJohn McCall    llvm::Value *SrcPtr =
8899021718882441dd391a1960084580d3cd19c423aJohn McCall      Builder.CreateBitCast(NullVariable, Builder.getInt8PtrTy());
8909021718882441dd391a1960084580d3cd19c423aJohn McCall
8917143325db76d6c3dabce82500f8cc7c93a941970John McCall    if (vla) return emitNonZeroVLAInit(*this, Ty, DestPtr, SrcPtr, SizeVal);
8927143325db76d6c3dabce82500f8cc7c93a941970John McCall
8939021718882441dd391a1960084580d3cd19c423aJohn McCall    // Get and call the appropriate llvm.memcpy overload.
89479be76c0e360d9e9c44285c9838af02adc43e55fKen Dyck    Builder.CreateMemCpy(DestPtr, SrcPtr, SizeVal, Align.getQuantity(), false);
89588207c9ca218486b93ae7df14e9764cd0c2c3383Chris Lattner    return;
8969021718882441dd391a1960084580d3cd19c423aJohn McCall  }
8979021718882441dd391a1960084580d3cd19c423aJohn McCall
8989021718882441dd391a1960084580d3cd19c423aJohn McCall  // Otherwise, just memset the whole thing to zero.  This is legal
8999021718882441dd391a1960084580d3cd19c423aJohn McCall  // because in LLVM, all default initializers (other than the ones we just
9009021718882441dd391a1960084580d3cd19c423aJohn McCall  // handled above) are guaranteed to have a bit pattern of all zeros.
90179be76c0e360d9e9c44285c9838af02adc43e55fKen Dyck  Builder.CreateMemSet(DestPtr, Builder.getInt8(0), SizeVal,
90279be76c0e360d9e9c44285c9838af02adc43e55fKen Dyck                       Align.getQuantity(), false);
9033d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson}
9043d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson
905ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattnerllvm::BlockAddress *CodeGenFunction::GetAddrOfLabel(const LabelDecl *L) {
906d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // Make sure that there is a block for the indirect goto.
907d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  if (IndirectBranch == 0)
908d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    GetIndirectGotoBlock();
9093d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner
910ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  llvm::BasicBlock *BB = getJumpDestForLabel(L).getBlock();
9113d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner
912d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // Make sure the indirect branch includes all of the address-taken blocks.
913d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  IndirectBranch->addDestination(BB);
914d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  return llvm::BlockAddress::get(CurFn, BB);
9153d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner}
9161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9173d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattnerllvm::BasicBlock *CodeGenFunction::GetIndirectGotoBlock() {
918d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // If we already made the indirect branch for indirect goto, return its block.
919d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  if (IndirectBranch) return IndirectBranch->getParent();
9203d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner
921d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  CGBuilderTy TmpBuilder(createBasicBlock("indirectgoto"));
9223d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner
9233d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner  // Create the PHI node that indirect gotos will add entries to.
924bbf3bacb3e0c1ebb3e8a4a8b1330404a7e379315Jay Foad  llvm::Value *DestVal = TmpBuilder.CreatePHI(Int8PtrTy, 0,
925bbf3bacb3e0c1ebb3e8a4a8b1330404a7e379315Jay Foad                                              "indirect.goto.dest");
9263d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner
927d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // Create the indirect branch instruction.
928d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  IndirectBranch = TmpBuilder.CreateIndirectBr(DestVal);
929d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  return IndirectBranch->getParent();
9300ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar}
931ddf7cac45d85b73127adbbd91a2b28fc7291c57eAnders Carlsson
932bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall/// Computes the length of an array in elements, as well as the base
933bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall/// element type and a properly-typed first element pointer.
934bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCallllvm::Value *CodeGenFunction::emitArrayLength(const ArrayType *origArrayType,
935bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall                                              QualType &baseType,
936bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall                                              llvm::Value *&addr) {
937bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  const ArrayType *arrayType = origArrayType;
938bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
939bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  // If it's a VLA, we have to load the stored size.  Note that
940bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  // this is the size of the VLA in bytes, not its size in elements.
941bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  llvm::Value *numVLAElements = 0;
942bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  if (isa<VariableArrayType>(arrayType)) {
943bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    numVLAElements = getVLASize(cast<VariableArrayType>(arrayType)).first;
944bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
945bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    // Walk into all VLAs.  This doesn't require changes to addr,
946bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    // which has type T* where T is the first non-VLA element type.
947bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    do {
948bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall      QualType elementType = arrayType->getElementType();
949bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall      arrayType = getContext().getAsArrayType(elementType);
950bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
951bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall      // If we only have VLA components, 'addr' requires no adjustment.
952bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall      if (!arrayType) {
953bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall        baseType = elementType;
954bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall        return numVLAElements;
955bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall      }
956bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    } while (isa<VariableArrayType>(arrayType));
957bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
958bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    // We get out here only if we find a constant array type
959bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    // inside the VLA.
960bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  }
961bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
962bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  // We have some number of constant-length arrays, so addr should
963bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  // have LLVM type [M x [N x [...]]]*.  Build a GEP that walks
964bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  // down to the first element of addr.
9655f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<llvm::Value*, 8> gepIndices;
966bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
967bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  // GEP down to the array type.
968bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  llvm::ConstantInt *zero = Builder.getInt32(0);
969bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  gepIndices.push_back(zero);
970bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
971bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  uint64_t countFromCLAs = 1;
9721664d540d1524f0faffd2f839fccb56178975c60Richard Smith  QualType eltType;
973bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
9742acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::ArrayType *llvmArrayType =
9751664d540d1524f0faffd2f839fccb56178975c60Richard Smith    dyn_cast<llvm::ArrayType>(
976bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall      cast<llvm::PointerType>(addr->getType())->getElementType());
9771664d540d1524f0faffd2f839fccb56178975c60Richard Smith  while (llvmArrayType) {
978bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    assert(isa<ConstantArrayType>(arrayType));
979bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    assert(cast<ConstantArrayType>(arrayType)->getSize().getZExtValue()
980bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall             == llvmArrayType->getNumElements());
981bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
982bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    gepIndices.push_back(zero);
983bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    countFromCLAs *= llvmArrayType->getNumElements();
9841664d540d1524f0faffd2f839fccb56178975c60Richard Smith    eltType = arrayType->getElementType();
985bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
986bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    llvmArrayType =
987bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall      dyn_cast<llvm::ArrayType>(llvmArrayType->getElementType());
988bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    arrayType = getContext().getAsArrayType(arrayType->getElementType());
9891664d540d1524f0faffd2f839fccb56178975c60Richard Smith    assert((!llvmArrayType || arrayType) &&
9901664d540d1524f0faffd2f839fccb56178975c60Richard Smith           "LLVM and Clang types are out-of-synch");
991bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  }
992bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
9931664d540d1524f0faffd2f839fccb56178975c60Richard Smith  if (arrayType) {
9941664d540d1524f0faffd2f839fccb56178975c60Richard Smith    // From this point onwards, the Clang array type has been emitted
9951664d540d1524f0faffd2f839fccb56178975c60Richard Smith    // as some other type (probably a packed struct). Compute the array
9961664d540d1524f0faffd2f839fccb56178975c60Richard Smith    // size, and just emit the 'begin' expression as a bitcast.
9971664d540d1524f0faffd2f839fccb56178975c60Richard Smith    while (arrayType) {
9981664d540d1524f0faffd2f839fccb56178975c60Richard Smith      countFromCLAs *=
9991664d540d1524f0faffd2f839fccb56178975c60Richard Smith          cast<ConstantArrayType>(arrayType)->getSize().getZExtValue();
10001664d540d1524f0faffd2f839fccb56178975c60Richard Smith      eltType = arrayType->getElementType();
10011664d540d1524f0faffd2f839fccb56178975c60Richard Smith      arrayType = getContext().getAsArrayType(eltType);
10021664d540d1524f0faffd2f839fccb56178975c60Richard Smith    }
10031664d540d1524f0faffd2f839fccb56178975c60Richard Smith
10041664d540d1524f0faffd2f839fccb56178975c60Richard Smith    unsigned AddressSpace =
10051664d540d1524f0faffd2f839fccb56178975c60Richard Smith        cast<llvm::PointerType>(addr->getType())->getAddressSpace();
10061664d540d1524f0faffd2f839fccb56178975c60Richard Smith    llvm::Type *BaseType = ConvertType(eltType)->getPointerTo(AddressSpace);
10071664d540d1524f0faffd2f839fccb56178975c60Richard Smith    addr = Builder.CreateBitCast(addr, BaseType, "array.begin");
10081664d540d1524f0faffd2f839fccb56178975c60Richard Smith  } else {
10091664d540d1524f0faffd2f839fccb56178975c60Richard Smith    // Create the actual GEP.
10101664d540d1524f0faffd2f839fccb56178975c60Richard Smith    addr = Builder.CreateInBoundsGEP(addr, gepIndices, "array.begin");
10111664d540d1524f0faffd2f839fccb56178975c60Richard Smith  }
1012bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
10131664d540d1524f0faffd2f839fccb56178975c60Richard Smith  baseType = eltType;
1014bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
1015bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  llvm::Value *numElements
1016bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    = llvm::ConstantInt::get(SizeTy, countFromCLAs);
1017bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
1018bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  // If we had any VLA dimensions, factor them in.
1019bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  if (numVLAElements)
1020bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    numElements = Builder.CreateNUWMul(numVLAElements, numElements);
1021bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
1022bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  return numElements;
1023bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall}
1024bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
1025bc8d40d85f3fa1e34569834916f18fecaa635152John McCallstd::pair<llvm::Value*, QualType>
1026bc8d40d85f3fa1e34569834916f18fecaa635152John McCallCodeGenFunction::getVLASize(QualType type) {
1027bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  const VariableArrayType *vla = getContext().getAsVariableArrayType(type);
1028bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  assert(vla && "type was not a variable array type!");
1029bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  return getVLASize(vla);
1030f666b7780d04186521adcaedb0e15dfa4d5e6933Anders Carlsson}
1031dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlsson
1032bc8d40d85f3fa1e34569834916f18fecaa635152John McCallstd::pair<llvm::Value*, QualType>
1033bc8d40d85f3fa1e34569834916f18fecaa635152John McCallCodeGenFunction::getVLASize(const VariableArrayType *type) {
1034bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  // The number of elements so far; always size_t.
1035bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  llvm::Value *numElements = 0;
10361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1037bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  QualType elementType;
1038bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  do {
1039bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    elementType = type->getElementType();
1040bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    llvm::Value *vlaSize = VLASizeMap[type->getSizeExpr()];
1041bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    assert(vlaSize && "no size for VLA!");
1042bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    assert(vlaSize->getType() == SizeTy);
10431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1044bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    if (!numElements) {
1045bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      numElements = vlaSize;
1046bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    } else {
1047bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      // It's undefined behavior if this wraps around, so mark it that way.
1048930c05c32eac171ef1a94fabf80aecb4e6e2c840Richard Smith      // FIXME: Teach -fcatch-undefined-behavior to trap this.
1049bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      numElements = Builder.CreateNUWMul(numElements, vlaSize);
1050bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    }
1051bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  } while ((type = getContext().getAsVariableArrayType(elementType)));
10521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1053bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  return std::pair<llvm::Value*,QualType>(numElements, elementType);
1054bc8d40d85f3fa1e34569834916f18fecaa635152John McCall}
10551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1056bc8d40d85f3fa1e34569834916f18fecaa635152John McCallvoid CodeGenFunction::EmitVariablyModifiedType(QualType type) {
1057bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  assert(type->isVariablyModifiedType() &&
1058bc8d40d85f3fa1e34569834916f18fecaa635152John McCall         "Must pass variably modified type to EmitVLASizes!");
10591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1060bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  EnsureInsertPoint();
10611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1062bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  // We're going to walk down into the type and look for VLA
1063bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  // expressions.
1064bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  do {
1065bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    assert(type->isVariablyModifiedType());
1066bc8d40d85f3fa1e34569834916f18fecaa635152John McCall
1067bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    const Type *ty = type.getTypePtr();
1068bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    switch (ty->getTypeClass()) {
106906284c1dc56caed19850bc3766c89f51763724c3Abramo Bagnara
1070bc8d40d85f3fa1e34569834916f18fecaa635152John McCall#define TYPE(Class, Base)
1071bc8d40d85f3fa1e34569834916f18fecaa635152John McCall#define ABSTRACT_TYPE(Class, Base)
107206284c1dc56caed19850bc3766c89f51763724c3Abramo Bagnara#define NON_CANONICAL_TYPE(Class, Base)
1073bc8d40d85f3fa1e34569834916f18fecaa635152John McCall#define DEPENDENT_TYPE(Class, Base) case Type::Class:
107406284c1dc56caed19850bc3766c89f51763724c3Abramo Bagnara#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base)
1075bc8d40d85f3fa1e34569834916f18fecaa635152John McCall#include "clang/AST/TypeNodes.def"
107606284c1dc56caed19850bc3766c89f51763724c3Abramo Bagnara      llvm_unreachable("unexpected dependent type!");
1077bc8d40d85f3fa1e34569834916f18fecaa635152John McCall
1078bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    // These types are never variably-modified.
1079bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::Builtin:
1080bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::Complex:
1081bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::Vector:
1082bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::ExtVector:
1083bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::Record:
1084bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::Enum:
10855ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara    case Type::Elaborated:
10865ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara    case Type::TemplateSpecialization:
1087bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::ObjCObject:
1088bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::ObjCInterface:
1089bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::ObjCObjectPointer:
1090bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      llvm_unreachable("type class is never variably-modified!");
1091bc8d40d85f3fa1e34569834916f18fecaa635152John McCall
1092bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::Pointer:
1093bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      type = cast<PointerType>(ty)->getPointeeType();
1094bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      break;
1095bc8d40d85f3fa1e34569834916f18fecaa635152John McCall
1096bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::BlockPointer:
1097bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      type = cast<BlockPointerType>(ty)->getPointeeType();
1098bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      break;
1099bc8d40d85f3fa1e34569834916f18fecaa635152John McCall
1100bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::LValueReference:
1101bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::RValueReference:
1102bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      type = cast<ReferenceType>(ty)->getPointeeType();
1103bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      break;
1104bc8d40d85f3fa1e34569834916f18fecaa635152John McCall
1105bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::MemberPointer:
1106bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      type = cast<MemberPointerType>(ty)->getPointeeType();
1107bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      break;
1108bc8d40d85f3fa1e34569834916f18fecaa635152John McCall
1109bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::ConstantArray:
1110bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::IncompleteArray:
1111bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      // Losing element qualification here is fine.
1112bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      type = cast<ArrayType>(ty)->getElementType();
1113bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      break;
1114bc8d40d85f3fa1e34569834916f18fecaa635152John McCall
1115bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::VariableArray: {
1116bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      // Losing element qualification here is fine.
1117bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      const VariableArrayType *vat = cast<VariableArrayType>(ty);
1118bc8d40d85f3fa1e34569834916f18fecaa635152John McCall
1119bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      // Unknown size indication requires no size computation.
1120bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      // Otherwise, evaluate and record it.
1121bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      if (const Expr *size = vat->getSizeExpr()) {
1122bc8d40d85f3fa1e34569834916f18fecaa635152John McCall        // It's possible that we might have emitted this already,
1123bc8d40d85f3fa1e34569834916f18fecaa635152John McCall        // e.g. with a typedef and a pointer to it.
1124bc8d40d85f3fa1e34569834916f18fecaa635152John McCall        llvm::Value *&entry = VLASizeMap[size];
1125bc8d40d85f3fa1e34569834916f18fecaa635152John McCall        if (!entry) {
1126930c05c32eac171ef1a94fabf80aecb4e6e2c840Richard Smith          llvm::Value *Size = EmitScalarExpr(size);
1127930c05c32eac171ef1a94fabf80aecb4e6e2c840Richard Smith
1128930c05c32eac171ef1a94fabf80aecb4e6e2c840Richard Smith          // C11 6.7.6.2p5:
1129930c05c32eac171ef1a94fabf80aecb4e6e2c840Richard Smith          //   If the size is an expression that is not an integer constant
1130930c05c32eac171ef1a94fabf80aecb4e6e2c840Richard Smith          //   expression [...] each time it is evaluated it shall have a value
1131930c05c32eac171ef1a94fabf80aecb4e6e2c840Richard Smith          //   greater than zero.
1132930c05c32eac171ef1a94fabf80aecb4e6e2c840Richard Smith          if (CatchUndefined && size->getType()->isSignedIntegerType()) {
1133930c05c32eac171ef1a94fabf80aecb4e6e2c840Richard Smith            llvm::Value *Zero = llvm::Constant::getNullValue(Size->getType());
1134930c05c32eac171ef1a94fabf80aecb4e6e2c840Richard Smith            llvm::Constant *StaticArgs[] = {
1135930c05c32eac171ef1a94fabf80aecb4e6e2c840Richard Smith              EmitCheckSourceLocation(size->getLocStart()),
1136930c05c32eac171ef1a94fabf80aecb4e6e2c840Richard Smith              EmitCheckTypeDescriptor(size->getType())
1137930c05c32eac171ef1a94fabf80aecb4e6e2c840Richard Smith            };
1138930c05c32eac171ef1a94fabf80aecb4e6e2c840Richard Smith            EmitCheck(Builder.CreateICmpSGT(Size, Zero),
1139930c05c32eac171ef1a94fabf80aecb4e6e2c840Richard Smith                      "vla_bound_not_positive", StaticArgs, Size);
1140930c05c32eac171ef1a94fabf80aecb4e6e2c840Richard Smith          }
1141930c05c32eac171ef1a94fabf80aecb4e6e2c840Richard Smith
1142bc8d40d85f3fa1e34569834916f18fecaa635152John McCall          // Always zexting here would be wrong if it weren't
1143bc8d40d85f3fa1e34569834916f18fecaa635152John McCall          // undefined behavior to have a negative bound.
1144bf43f2fdfbd7c4f8cd1f1403765eca9e5dc6c8aeRichard Smith          entry = Builder.CreateIntCast(Size, SizeTy, /*signed*/ false);
1145bc8d40d85f3fa1e34569834916f18fecaa635152John McCall        }
1146bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      }
1147bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      type = vat->getElementType();
1148bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      break;
1149fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson    }
11501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
115106284c1dc56caed19850bc3766c89f51763724c3Abramo Bagnara    case Type::FunctionProto:
1152bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::FunctionNoProto:
1153bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      type = cast<FunctionType>(ty)->getResultType();
1154bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      break;
1155b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
11565ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara    case Type::Paren:
11575ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara    case Type::TypeOf:
11585ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara    case Type::UnaryTransform:
11595ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara    case Type::Attributed:
11605ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara    case Type::SubstTemplateTypeParm:
11615ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara      // Keep walking after single level desugaring.
11625ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara      type = type.getSingleStepDesugaredType(getContext());
11635ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara      break;
11645ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara
11655ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara    case Type::Typedef:
11665ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara    case Type::Decltype:
11675ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara    case Type::Auto:
11685ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara      // Stop walking: nothing to do.
11695ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara      return;
11705ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara
11715ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara    case Type::TypeOfExpr:
11725ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara      // Stop walking: emit typeof expression.
11735ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara      EmitIgnoredExpr(cast<TypeOfExprType>(ty)->getUnderlyingExpr());
11745ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara      return;
11755ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara
1176b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    case Type::Atomic:
1177b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      type = cast<AtomicType>(ty)->getValueType();
1178b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      break;
1179bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    }
1180bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  } while (type->isVariablyModifiedType());
1181dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlsson}
11824fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman
11834fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedmanllvm::Value* CodeGenFunction::EmitVAListRef(const Expr* E) {
1184bc07a55b68342a230aafd8875cc7a26450dd3f64Dan Gohman  if (getContext().getBuiltinVaListType()->isArrayType())
11854fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman    return EmitScalarExpr(E);
11864fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman  return EmitLValue(E).getAddress();
11874fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman}
11886ccc47698d0311ddabf32fa0f6db8e4f09ac96f8Anders Carlsson
11898d308384a220c7dc81755c47cdcbdd87dac25d5bDevang Patelvoid CodeGenFunction::EmitDeclRefExprDbgValue(const DeclRefExpr *E,
1190189d6ef40eff11b83b2cda941d5ed89a5cef09b2John McCall                                              llvm::Constant *Init) {
119125c2c8fb9309050612009a6571e2660e75531348Devang Patel  assert (Init && "Invalid DeclRefExpr initializer!");
119225c2c8fb9309050612009a6571e2660e75531348Devang Patel  if (CGDebugInfo *Dbg = getDebugInfo())
1193fd00eecad6fa5400cf37269d84361a0551d0e6d3Alexey Samsonov    if (CGM.getCodeGenOpts().DebugInfo >= CodeGenOptions::LimitedDebugInfo)
1194fd00eecad6fa5400cf37269d84361a0551d0e6d3Alexey Samsonov      Dbg->EmitGlobalVariable(E->getDecl(), Init);
11958d308384a220c7dc81755c47cdcbdd87dac25d5bDevang Patel}
119656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
119756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCallCodeGenFunction::PeepholeProtection
119856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCallCodeGenFunction::protectFromPeepholes(RValue rvalue) {
119956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  // At the moment, the only aggressive peephole we do in IR gen
120056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  // is trunc(zext) folding, but if we add more, we can easily
120156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  // extend this protection.
120256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
120356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  if (!rvalue.isScalar()) return PeepholeProtection();
120456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  llvm::Value *value = rvalue.getScalarVal();
120556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  if (!isa<llvm::ZExtInst>(value)) return PeepholeProtection();
120656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
120756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  // Just make an extra bitcast.
120856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  assert(HaveInsertPoint());
120956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  llvm::Instruction *inst = new llvm::BitCastInst(value, value->getType(), "",
121056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall                                                  Builder.GetInsertBlock());
121156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
121256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  PeepholeProtection protection;
121356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  protection.Inst = inst;
121456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  return protection;
121556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall}
121656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
121756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCallvoid CodeGenFunction::unprotectFromPeepholes(PeepholeProtection protection) {
121856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  if (!protection.Inst) return;
121956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
122056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  // In theory, we could try to duplicate the peepholes now, but whatever.
122156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  protection.Inst->eraseFromParent();
122256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall}
122377f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge
122477f68bb90af93b95045fb994e7cd68137adcc132Julien Lerougellvm::Value *CodeGenFunction::EmitAnnotationCall(llvm::Value *AnnotationFn,
122577f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge                                                 llvm::Value *AnnotatedVal,
122677f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge                                                 llvm::StringRef AnnotationStr,
122777f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge                                                 SourceLocation Location) {
122877f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  llvm::Value *Args[4] = {
122977f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge    AnnotatedVal,
123077f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge    Builder.CreateBitCast(CGM.EmitAnnotationString(AnnotationStr), Int8PtrTy),
123177f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge    Builder.CreateBitCast(CGM.EmitAnnotationUnit(Location), Int8PtrTy),
123277f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge    CGM.EmitAnnotationLineNo(Location)
123377f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  };
123477f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  return Builder.CreateCall(AnnotationFn, Args);
123577f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge}
123677f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge
123777f68bb90af93b95045fb994e7cd68137adcc132Julien Lerougevoid CodeGenFunction::EmitVarAnnotations(const VarDecl *D, llvm::Value *V) {
123877f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  assert(D->hasAttr<AnnotateAttr>() && "no annotate attribute");
123977f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  // FIXME We create a new bitcast for every annotation because that's what
124077f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  // llvm-gcc was doing.
124177f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  for (specific_attr_iterator<AnnotateAttr>
124277f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge       ai = D->specific_attr_begin<AnnotateAttr>(),
124377f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge       ae = D->specific_attr_end<AnnotateAttr>(); ai != ae; ++ai)
124477f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge    EmitAnnotationCall(CGM.getIntrinsic(llvm::Intrinsic::var_annotation),
124577f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge                       Builder.CreateBitCast(V, CGM.Int8PtrTy, V->getName()),
124677f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge                       (*ai)->getAnnotation(), D->getLocation());
124777f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge}
124877f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge
124977f68bb90af93b95045fb994e7cd68137adcc132Julien Lerougellvm::Value *CodeGenFunction::EmitFieldAnnotations(const FieldDecl *D,
125077f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge                                                   llvm::Value *V) {
125177f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  assert(D->hasAttr<AnnotateAttr>() && "no annotate attribute");
125277f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  llvm::Type *VTy = V->getType();
125377f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::ptr_annotation,
125477f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge                                    CGM.Int8PtrTy);
125577f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge
125677f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  for (specific_attr_iterator<AnnotateAttr>
125777f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge       ai = D->specific_attr_begin<AnnotateAttr>(),
125877f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge       ae = D->specific_attr_end<AnnotateAttr>(); ai != ae; ++ai) {
125977f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge    // FIXME Always emit the cast inst so we can differentiate between
126077f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge    // annotation on the first field of a struct and annotation on the struct
126177f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge    // itself.
126277f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge    if (VTy != CGM.Int8PtrTy)
126377f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge      V = Builder.Insert(new llvm::BitCastInst(V, CGM.Int8PtrTy));
126477f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge    V = EmitAnnotationCall(F, V, (*ai)->getAnnotation(), D->getLocation());
126577f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge    V = Builder.CreateBitCast(V, VTy);
126677f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  }
126777f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge
126877f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  return V;
126977f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge}
1270