CodeGenFunction.cpp revision fd00eecad6fa5400cf37269d84361a0551d0e6d3
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"
2610e675d4824710426ab894336cfb659742c94d08Duncan Sands#include "llvm/Support/MDBuilder.h"
2710e675d4824710426ab894336cfb659742c94d08Duncan Sands#include "llvm/Target/TargetData.h"
285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace CodeGen;
305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
311eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpCodeGenFunction::CodeGenFunction(CodeGenModule &cgm)
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;
454c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  CGM.getCXXABI().getMangleContext().startNewFunction();
464111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner}
475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
481a343ebbf413e8eae6b2737b2b2d79cbf5765571John McCallCodeGenFunction::~CodeGenFunction() {
491a343ebbf413e8eae6b2737b2b2d79cbf5765571John McCall  // If there are any unclaimed block infos, go ahead and destroy them
501a343ebbf413e8eae6b2737b2b2d79cbf5765571John McCall  // now.  This can happen if IR-gen gets clever and skips evaluating
511a343ebbf413e8eae6b2737b2b2d79cbf5765571John McCall  // something.
521a343ebbf413e8eae6b2737b2b2d79cbf5765571John McCall  if (FirstBlockInfo)
531a343ebbf413e8eae6b2737b2b2d79cbf5765571John McCall    destroyBlockInfos(FirstBlockInfo);
541a343ebbf413e8eae6b2737b2b2d79cbf5765571John McCall}
551a343ebbf413e8eae6b2737b2b2d79cbf5765571John McCall
565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
579cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnerllvm::Type *CodeGenFunction::ConvertTypeForMem(QualType T) {
588b1a343b6b360d63d5dc8a6beb841ce4414c1e00Daniel Dunbar  return CGM.getTypes().ConvertTypeForMem(T);
598b1a343b6b360d63d5dc8a6beb841ce4414c1e00Daniel Dunbar}
608b1a343b6b360d63d5dc8a6beb841ce4414c1e00Daniel Dunbar
619cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnerllvm::Type *CodeGenFunction::ConvertType(QualType T) {
625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return CGM.getTypes().ConvertType(T);
635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
65f2aac84709c418189e476ad591848dad50291885John McCallbool CodeGenFunction::hasAggregateLLVMType(QualType type) {
66f2aac84709c418189e476ad591848dad50291885John McCall  switch (type.getCanonicalType()->getTypeClass()) {
67f2aac84709c418189e476ad591848dad50291885John McCall#define TYPE(name, parent)
68f2aac84709c418189e476ad591848dad50291885John McCall#define ABSTRACT_TYPE(name, parent)
69f2aac84709c418189e476ad591848dad50291885John McCall#define NON_CANONICAL_TYPE(name, parent) case Type::name:
70f2aac84709c418189e476ad591848dad50291885John McCall#define DEPENDENT_TYPE(name, parent) case Type::name:
71f2aac84709c418189e476ad591848dad50291885John McCall#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(name, parent) case Type::name:
72f2aac84709c418189e476ad591848dad50291885John McCall#include "clang/AST/TypeNodes.def"
73f2aac84709c418189e476ad591848dad50291885John McCall    llvm_unreachable("non-canonical or dependent type in IR-generation");
74f2aac84709c418189e476ad591848dad50291885John McCall
75f2aac84709c418189e476ad591848dad50291885John McCall  case Type::Builtin:
76f2aac84709c418189e476ad591848dad50291885John McCall  case Type::Pointer:
77f2aac84709c418189e476ad591848dad50291885John McCall  case Type::BlockPointer:
78f2aac84709c418189e476ad591848dad50291885John McCall  case Type::LValueReference:
79f2aac84709c418189e476ad591848dad50291885John McCall  case Type::RValueReference:
80f2aac84709c418189e476ad591848dad50291885John McCall  case Type::MemberPointer:
81f2aac84709c418189e476ad591848dad50291885John McCall  case Type::Vector:
82f2aac84709c418189e476ad591848dad50291885John McCall  case Type::ExtVector:
83f2aac84709c418189e476ad591848dad50291885John McCall  case Type::FunctionProto:
84f2aac84709c418189e476ad591848dad50291885John McCall  case Type::FunctionNoProto:
85f2aac84709c418189e476ad591848dad50291885John McCall  case Type::Enum:
86f2aac84709c418189e476ad591848dad50291885John McCall  case Type::ObjCObjectPointer:
87f2aac84709c418189e476ad591848dad50291885John McCall    return false;
88f2aac84709c418189e476ad591848dad50291885John McCall
89f2aac84709c418189e476ad591848dad50291885John McCall  // Complexes, arrays, records, and Objective-C objects.
90f2aac84709c418189e476ad591848dad50291885John McCall  case Type::Complex:
91f2aac84709c418189e476ad591848dad50291885John McCall  case Type::ConstantArray:
92f2aac84709c418189e476ad591848dad50291885John McCall  case Type::IncompleteArray:
93f2aac84709c418189e476ad591848dad50291885John McCall  case Type::VariableArray:
94f2aac84709c418189e476ad591848dad50291885John McCall  case Type::Record:
95f2aac84709c418189e476ad591848dad50291885John McCall  case Type::ObjCObject:
96f2aac84709c418189e476ad591848dad50291885John McCall  case Type::ObjCInterface:
97f2aac84709c418189e476ad591848dad50291885John McCall    return true;
98b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
99b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  // In IRGen, atomic types are just the underlying type
100b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  case Type::Atomic:
101b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    return hasAggregateLLVMType(type->getAs<AtomicType>()->getValueType());
102f2aac84709c418189e476ad591848dad50291885John McCall  }
103f2aac84709c418189e476ad591848dad50291885John McCall  llvm_unreachable("unknown type kind!");
1044111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner}
105391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner
1061c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbarvoid CodeGenFunction::EmitReturnBlock() {
1071c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // For cleanliness, we try to avoid emitting the return block for
1081c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // simple cases.
1091c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
1101c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
1111c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  if (CurBB) {
1121c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar    assert(!CurBB->getTerminator() && "Unexpected terminated block.");
1131c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
11496e18b05ea6b55aa92a1a576f29e9cee73a7e20bDaniel Dunbar    // We have a valid insert point, reuse it if it is empty or there are no
11596e18b05ea6b55aa92a1a576f29e9cee73a7e20bDaniel Dunbar    // explicit jumps to the return block.
116ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    if (CurBB->empty() || ReturnBlock.getBlock()->use_empty()) {
117ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      ReturnBlock.getBlock()->replaceAllUsesWith(CurBB);
118ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      delete ReturnBlock.getBlock();
11996e18b05ea6b55aa92a1a576f29e9cee73a7e20bDaniel Dunbar    } else
120ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      EmitBlock(ReturnBlock.getBlock());
1211c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar    return;
1221c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  }
1231c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
1241c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // Otherwise, if the return block is the target of a single direct
1251c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // branch then we can just put the code in that block instead. This
1261c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // cleans up functions which started with a unified return block.
127ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  if (ReturnBlock.getBlock()->hasOneUse()) {
1281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    llvm::BranchInst *BI =
129ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      dyn_cast<llvm::BranchInst>(*ReturnBlock.getBlock()->use_begin());
130f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    if (BI && BI->isUnconditional() &&
131ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall        BI->getSuccessor(0) == ReturnBlock.getBlock()) {
132acae01124151392a842bd6c37bd01b1ad56d6b4dEric Christopher      // Reset insertion point, including debug location, and delete the branch.
133acae01124151392a842bd6c37bd01b1ad56d6b4dEric Christopher      Builder.SetCurrentDebugLocation(BI->getDebugLoc());
1341c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      Builder.SetInsertPoint(BI->getParent());
1351c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      BI->eraseFromParent();
136ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      delete ReturnBlock.getBlock();
1371c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      return;
1381c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar    }
1391c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  }
1401c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
141f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // FIXME: We are at an unreachable point, there is no reason to emit the block
142f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // unless it has uses. However, we still need a place to put the debug
143f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // region.end for now.
1441c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
145ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  EmitBlock(ReturnBlock.getBlock());
146f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall}
147f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
148f1549f66a8216a78112286e3978cea2c29d6334cJohn McCallstatic void EmitIfUsed(CodeGenFunction &CGF, llvm::BasicBlock *BB) {
149f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  if (!BB) return;
150f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  if (!BB->use_empty())
151f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    return CGF.CurFn->getBasicBlockList().push_back(BB);
152f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  delete BB;
1531c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar}
1541c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
155af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbarvoid CodeGenFunction::FinishFunction(SourceLocation EndLoc) {
156391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  assert(BreakContinueStack.empty() &&
157391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner         "mismatched push/pop in break/continue stack!");
1581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
159f85e193739c953358c865005855253af4f68a497John McCall  // Pop any cleanups that might have been associated with the
160f85e193739c953358c865005855253af4f68a497John McCall  // parameters.  Do this in whatever block we're currently in; it's
161f85e193739c953358c865005855253af4f68a497John McCall  // important to do this before we enter the return block or return
162f85e193739c953358c865005855253af4f68a497John McCall  // edges will be *really* confused.
163f85e193739c953358c865005855253af4f68a497John McCall  if (EHStack.stable_begin() != PrologueCleanupDepth)
164f85e193739c953358c865005855253af4f68a497John McCall    PopCleanupBlocks(PrologueCleanupDepth);
165f85e193739c953358c865005855253af4f68a497John McCall
1661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Emit function epilog (to return).
1671c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  EmitReturnBlock();
168f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar
169a18652fe1e8233fbf8b67484945c7f7b2bf272beDaniel Dunbar  if (ShouldInstrumentFunction())
170a18652fe1e8233fbf8b67484945c7f7b2bf272beDaniel Dunbar    EmitFunctionInstrumentation("__cyg_profile_func_exit");
1717255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner
172f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar  // Emit debug descriptor for function end.
173e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson  if (CGDebugInfo *DI = getDebugInfo()) {
174f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar    DI->setLocation(EndLoc);
1755a6fbcfd8c15a2296f94a0473a68ec09d429827fDevang Patel    DI->EmitFunctionEnd(Builder);
176f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar  }
177f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar
17835b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner  EmitFunctionEpilog(*CurFnInfo);
179cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump  EmitEndEHSpec(CurCodeDecl);
1805ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar
181f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  assert(EHStack.empty() &&
182f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall         "did not remove all scopes from cleanup stack!");
183f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
184d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // If someone did an indirect goto, emit the indirect goto block at the end of
185d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // the function.
186d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  if (IndirectBranch) {
187d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    EmitBlock(IndirectBranch->getParent());
188d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    Builder.ClearInsertionPoint();
189d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  }
190d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner
191391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  // Remove the AllocaInsertPt instruction, which is just a convenience for us.
192481769b5dc102b0256b35581e787909ad5edfab5Chris Lattner  llvm::Instruction *Ptr = AllocaInsertPt;
193391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  AllocaInsertPt = 0;
194481769b5dc102b0256b35581e787909ad5edfab5Chris Lattner  Ptr->eraseFromParent();
195d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner
196d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // If someone took the address of a label but never did an indirect goto, we
197d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // made a zero entry PHI node, which is illegal, zap it now.
198d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  if (IndirectBranch) {
199d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    llvm::PHINode *PN = cast<llvm::PHINode>(IndirectBranch->getAddress());
200d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    if (PN->getNumIncomingValues() == 0) {
201d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner      PN->replaceAllUsesWith(llvm::UndefValue::get(PN->getType()));
202d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner      PN->eraseFromParent();
203d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    }
204d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  }
205f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
206777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  EmitIfUsed(*this, EHResumeBlock);
207f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  EmitIfUsed(*this, TerminateLandingPad);
208f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  EmitIfUsed(*this, TerminateHandler);
209f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  EmitIfUsed(*this, UnreachableBlock);
210744016dde06fcffd50931e94a98c850f8b12cd87John McCall
211744016dde06fcffd50931e94a98c850f8b12cd87John McCall  if (CGM.getCodeGenOpts().EmitDeclMetadata)
212744016dde06fcffd50931e94a98c850f8b12cd87John McCall    EmitDeclMetadata();
213c8aa5f1f264fb230c38182adab944232bb160c2bChris Lattner}
214c8aa5f1f264fb230c38182adab944232bb160c2bChris Lattner
2157255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner/// ShouldInstrumentFunction - Return true if the current function should be
2167255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner/// instrumented with __cyg_profile_func_* calls
2177255a2d997b15beae82e627052fdb1b2474495c2Chris Lattnerbool CodeGenFunction::ShouldInstrumentFunction() {
2187255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  if (!CGM.getCodeGenOpts().InstrumentFunctions)
2197255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner    return false;
2207aa488a7fc5c3a8cd1a2b93476150e9737760713Ted Kremenek  if (!CurFuncDecl || CurFuncDecl->hasAttr<NoInstrumentFunctionAttr>())
2217255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner    return false;
2227255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  return true;
2237255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner}
2247255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner
2257255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner/// EmitFunctionInstrumentation - Emit LLVM code to call the specified
2267255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner/// instrumentation function with the current function and the call site, if
2277255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner/// function instrumentation is enabled.
2287255a2d997b15beae82e627052fdb1b2474495c2Chris Lattnervoid CodeGenFunction::EmitFunctionInstrumentation(const char *Fn) {
2298dab6571b2cab96f44d0a1d6e3edbfdb68b7ed6bChris Lattner  // void __cyg_profile_func_{enter,exit} (void *this_fn, void *call_site);
2309cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::PointerType *PointerTy = Int8PtrTy;
2319cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::Type *ProfileFuncArgs[] = { PointerTy, PointerTy };
2322acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::FunctionType *FunctionTy =
2338b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner    llvm::FunctionType::get(VoidTy, ProfileFuncArgs, false);
2347255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner
2357255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  llvm::Constant *F = CGM.CreateRuntimeFunction(FunctionTy, Fn);
2367255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  llvm::CallInst *CallSite = Builder.CreateCall(
2378dd55a3c3b28d195717c87bbc60e765951d408feBenjamin Kramer    CGM.getIntrinsic(llvm::Intrinsic::returnaddress),
23877b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner    llvm::ConstantInt::get(Int32Ty, 0),
2397255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner    "callsite");
2407255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner
2418dab6571b2cab96f44d0a1d6e3edbfdb68b7ed6bChris Lattner  Builder.CreateCall2(F,
2428dab6571b2cab96f44d0a1d6e3edbfdb68b7ed6bChris Lattner                      llvm::ConstantExpr::getBitCast(CurFn, PointerTy),
2438dab6571b2cab96f44d0a1d6e3edbfdb68b7ed6bChris Lattner                      CallSite);
2447255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner}
2457255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner
246be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divackyvoid CodeGenFunction::EmitMCountInstrumentation() {
2478b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner  llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
248be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky
249be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky  llvm::Constant *MCountFn = CGM.CreateRuntimeFunction(FTy,
250be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky                                                       Target.getMCountName());
251be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky  Builder.CreateCall(MCountFn);
252be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky}
253be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky
2540ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlssonvoid CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy,
2557c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar                                    llvm::Function *Fn,
256d26bc76c98006609002d9930f8840490e88ac5b5John McCall                                    const CGFunctionInfo &FnInfo,
2572284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar                                    const FunctionArgList &Args,
2589c6082fe89c61af697f017aa80937581cc2128d8Tilmann Scheller                                    SourceLocation StartLoc) {
2590ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlsson  const Decl *D = GD.getDecl();
2600ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlsson
2614cc1a4703363ff940b6273aeef9d96a87edeb04bAnders Carlsson  DidCallStackSave = false;
262b5437d238752dc297e42410e98d38d5250fe0463Chris Lattner  CurCodeDecl = CurFuncDecl = D;
2637c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar  FnRetTy = RetTy;
264bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  CurFn = Fn;
265d26bc76c98006609002d9930f8840490e88ac5b5John McCall  CurFnInfo = &FnInfo;
2665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(CurFn->isDeclaration() && "Function already has body?");
267ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner
268a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen  // Pass inline keyword to optimizer if it appears explicitly on any
269a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen  // declaration.
2708fbe3855db0c341964bb550e13659505efe06c43Chad Rosier  if (!CGM.getCodeGenOpts().NoInline)
2718fbe3855db0c341964bb550e13659505efe06c43Chad Rosier    if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
2728fbe3855db0c341964bb550e13659505efe06c43Chad Rosier      for (FunctionDecl::redecl_iterator RI = FD->redecls_begin(),
2738fbe3855db0c341964bb550e13659505efe06c43Chad Rosier             RE = FD->redecls_end(); RI != RE; ++RI)
2748fbe3855db0c341964bb550e13659505efe06c43Chad Rosier        if (RI->isInlineSpecified()) {
2758fbe3855db0c341964bb550e13659505efe06c43Chad Rosier          Fn->addFnAttr(llvm::Attribute::InlineHint);
2768fbe3855db0c341964bb550e13659505efe06c43Chad Rosier          break;
2778fbe3855db0c341964bb550e13659505efe06c43Chad Rosier        }
278a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen
2794e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getContext().getLangOpts().OpenCL) {
280f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne    // Add metadata for a kernel function.
281f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne    if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
282f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne      if (FD->hasAttr<OpenCLKernelAttr>()) {
283f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne        llvm::LLVMContext &Context = getLLVMContext();
284f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne        llvm::NamedMDNode *OpenCLMetadata =
285f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne          CGM.getModule().getOrInsertNamedMetadata("opencl.kernels");
286f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne
287f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne        llvm::Value *Op = Fn;
2886f141659cab11109d9931d92d0988f8850778de3Jay Foad        OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Op));
289f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne      }
290f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne  }
291f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne
29255e874299f2ad827646a4ca9ea38c402aaeb38c9Daniel Dunbar  llvm::BasicBlock *EntryBB = createBasicBlock("entry", CurFn);
2935ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar
2945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Create a marker to make it easy to insert allocas into the entryblock
29555352a2d616cf9fbb621d10faf8b960b4b268bd8Chris Lattner  // later.  Don't create this with the builder, because we don't want it
29655352a2d616cf9fbb621d10faf8b960b4b268bd8Chris Lattner  // folded.
29777b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner  llvm::Value *Undef = llvm::UndefValue::get(Int32Ty);
29877b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner  AllocaInsertPt = new llvm::BitCastInst(Undef, Int32Ty, "", EntryBB);
299f1466848dce9c4c75d96a6cabdc8db560e26aac8Chris Lattner  if (Builder.isNamePreserving())
300f1466848dce9c4c75d96a6cabdc8db560e26aac8Chris Lattner    AllocaInsertPt->setName("allocapt");
3011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
302f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  ReturnBlock = getJumpDestInCurrentScope("return");
3031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
30455352a2d616cf9fbb621d10faf8b960b4b268bd8Chris Lattner  Builder.SetInsertPoint(EntryBB);
3051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
306af99417156c652a6f04dff643925036dc3241d60Sanjiv Gupta  // Emit subprogram debug descriptor.
307e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson  if (CGDebugInfo *DI = getDebugInfo()) {
30806253664315307d34ab57b892b6a0c6c5b3153bbEric Christopher    unsigned NumArgs = 0;
30906253664315307d34ab57b892b6a0c6c5b3153bbEric Christopher    QualType *ArgsArray = new QualType[Args.size()];
31006253664315307d34ab57b892b6a0c6c5b3153bbEric Christopher    for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
31106253664315307d34ab57b892b6a0c6c5b3153bbEric Christopher	 i != e; ++i) {
31206253664315307d34ab57b892b6a0c6c5b3153bbEric Christopher      ArgsArray[NumArgs++] = (*i)->getType();
31306253664315307d34ab57b892b6a0c6c5b3153bbEric Christopher    }
31406253664315307d34ab57b892b6a0c6c5b3153bbEric Christopher
315e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    QualType FnType =
31606253664315307d34ab57b892b6a0c6c5b3153bbEric Christopher      getContext().getFunctionType(RetTy, ArgsArray, NumArgs,
317e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall                                   FunctionProtoType::ExtProtoInfo());
318e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
3192a04f1cf72d57cf5d74b24e785c04f7a3fc3398fBenjamin Kramer    delete[] ArgsArray;
32006253664315307d34ab57b892b6a0c6c5b3153bbEric Christopher
3212284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar    DI->setLocation(StartLoc);
3229c6c3a0e3ae09626d2d4b04e4ffa42c3d7cab32bDevang Patel    DI->EmitFunctionStart(GD, FnType, CurFn, Builder);
323af99417156c652a6f04dff643925036dc3241d60Sanjiv Gupta  }
324af99417156c652a6f04dff643925036dc3241d60Sanjiv Gupta
325a18652fe1e8233fbf8b67484945c7f7b2bf272beDaniel Dunbar  if (ShouldInstrumentFunction())
326a18652fe1e8233fbf8b67484945c7f7b2bf272beDaniel Dunbar    EmitFunctionInstrumentation("__cyg_profile_func_enter");
3277255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner
328be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky  if (CGM.getCodeGenOpts().InstrumentForProfiling)
329be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky    EmitMCountInstrumentation();
330be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky
331b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman  if (RetTy->isVoidType()) {
332b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman    // Void type; nothing to return.
333b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman    ReturnValue = 0;
334b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman  } else if (CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect &&
335b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman             hasAggregateLLVMType(CurFnInfo->getReturnType())) {
336b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman    // Indirect aggregate return; emit returned value directly into sret slot.
337647a1ec397fa13af176d07d9f5d071560a94c7a9Daniel Dunbar    // This reduces code size, and affects correctness in C++.
338b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman    ReturnValue = CurFn->arg_begin();
339b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman  } else {
340647a1ec397fa13af176d07d9f5d071560a94c7a9Daniel Dunbar    ReturnValue = CreateIRTemp(RetTy, "retval");
341f85e193739c953358c865005855253af4f68a497John McCall
342f85e193739c953358c865005855253af4f68a497John McCall    // Tell the epilog emitter to autorelease the result.  We do this
343f85e193739c953358c865005855253af4f68a497John McCall    // now so that various specialized functions can suppress it
344f85e193739c953358c865005855253af4f68a497John McCall    // during their IR-generation.
3454e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (getLangOpts().ObjCAutoRefCount &&
346f85e193739c953358c865005855253af4f68a497John McCall        !CurFnInfo->isReturnsRetained() &&
347f85e193739c953358c865005855253af4f68a497John McCall        RetTy->isObjCRetainableType())
348f85e193739c953358c865005855253af4f68a497John McCall      AutoreleaseResult = true;
349b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman  }
350b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman
351cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump  EmitStartEHSpec(CurCodeDecl);
352f85e193739c953358c865005855253af4f68a497John McCall
353f85e193739c953358c865005855253af4f68a497John McCall  PrologueCleanupDepth = EHStack.stable_begin();
35488b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar  EmitFunctionProlog(*CurFnInfo, CurFn, Args);
3551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
356cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman  if (D && isa<CXXMethodDecl>(D) && cast<CXXMethodDecl>(D)->isInstance()) {
3574c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    CGM.getCXXABI().EmitInstanceFunctionProlog(*this);
358cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman    const CXXMethodDecl *MD = cast<CXXMethodDecl>(D);
359cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman    if (MD->getParent()->isLambda() &&
360cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman        MD->getOverloadedOperator() == OO_Call) {
361cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman      // We're in a lambda; figure out the captures.
362cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman      MD->getParent()->getCaptureFields(LambdaCaptureFields,
363cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman                                        LambdaThisCaptureField);
364cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman      if (LambdaThisCaptureField) {
365cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman        // If this lambda captures this, load it.
366377ecc7996dce6803f7b7b6208cab5e197c9c5b8Eli Friedman        QualType LambdaTagType =
367377ecc7996dce6803f7b7b6208cab5e197c9c5b8Eli Friedman            getContext().getTagDeclType(LambdaThisCaptureField->getParent());
368377ecc7996dce6803f7b7b6208cab5e197c9c5b8Eli Friedman        LValue LambdaLV = MakeNaturalAlignAddrLValue(CXXABIThisValue,
369377ecc7996dce6803f7b7b6208cab5e197c9c5b8Eli Friedman                                                     LambdaTagType);
370377ecc7996dce6803f7b7b6208cab5e197c9c5b8Eli Friedman        LValue ThisLValue = EmitLValueForField(LambdaLV,
371377ecc7996dce6803f7b7b6208cab5e197c9c5b8Eli Friedman                                               LambdaThisCaptureField);
372cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman        CXXThisValue = EmitLoadOfLValue(ThisLValue).getScalarVal();
373cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman      }
374cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman    } else {
375cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman      // Not in a lambda; just use 'this' from the method.
376cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman      // FIXME: Should we generate a new load for each use of 'this'?  The
377cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman      // fast register allocator would be happier...
378cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman      CXXThisValue = CXXABIThisValue;
379cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman    }
380cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman  }
3812504941793b549323f9d29c62507cf21d865fadeJohn McCall
382751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson  // If any of the arguments have a variably modified type, make sure to
383751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson  // emit the type size.
384751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson  for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
385751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson       i != e; ++i) {
386d26bc76c98006609002d9930f8840490e88ac5b5John McCall    QualType Ty = (*i)->getType();
387751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson
388751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson    if (Ty->isVariablyModifiedType())
389bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      EmitVariablyModifiedType(Ty);
390751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson  }
39173fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher  // Emit a location at the end of the prologue.
39273fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher  if (CGDebugInfo *DI = getDebugInfo())
39373fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher    DI->EmitLocation(Builder, StartLoc);
3947c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar}
395eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman
3969fc6a7774643a810c8501dae2323e863fefb623eJohn McCallvoid CodeGenFunction::EmitFunctionBody(FunctionArgList &Args) {
3979fc6a7774643a810c8501dae2323e863fefb623eJohn McCall  const FunctionDecl *FD = cast<FunctionDecl>(CurGD.getDecl());
39806a9f3680d22529a2fcf20c52d71cf221d99d910Douglas Gregor  assert(FD->getBody());
39906a9f3680d22529a2fcf20c52d71cf221d99d910Douglas Gregor  EmitStmt(FD->getBody());
400a355e07454463b19829ac92ffd115a097faff0e0John McCall}
401a355e07454463b19829ac92ffd115a097faff0e0John McCall
40239dad53772c42eb36ebec1c81c56ba99d038fb94John McCall/// Tries to mark the given function nounwind based on the
40339dad53772c42eb36ebec1c81c56ba99d038fb94John McCall/// non-existence of any throwing calls within it.  We believe this is
40439dad53772c42eb36ebec1c81c56ba99d038fb94John McCall/// lightweight enough to do at -O0.
40539dad53772c42eb36ebec1c81c56ba99d038fb94John McCallstatic void TryMarkNoThrow(llvm::Function *F) {
406b3a29f132794f67108bccc9c7cc3795365e8a965John McCall  // LLVM treats 'nounwind' on a function as part of the type, so we
407b3a29f132794f67108bccc9c7cc3795365e8a965John McCall  // can't do this on functions that can be overwritten.
408b3a29f132794f67108bccc9c7cc3795365e8a965John McCall  if (F->mayBeOverridden()) return;
409b3a29f132794f67108bccc9c7cc3795365e8a965John McCall
41039dad53772c42eb36ebec1c81c56ba99d038fb94John McCall  for (llvm::Function::iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI)
41139dad53772c42eb36ebec1c81c56ba99d038fb94John McCall    for (llvm::BasicBlock::iterator
41239dad53772c42eb36ebec1c81c56ba99d038fb94John McCall           BI = FI->begin(), BE = FI->end(); BI != BE; ++BI)
413285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling      if (llvm::CallInst *Call = dyn_cast<llvm::CallInst>(&*BI)) {
41439dad53772c42eb36ebec1c81c56ba99d038fb94John McCall        if (!Call->doesNotThrow())
41539dad53772c42eb36ebec1c81c56ba99d038fb94John McCall          return;
416285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling      } else if (isa<llvm::ResumeInst>(&*BI)) {
417285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling        return;
418285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling      }
41939dad53772c42eb36ebec1c81c56ba99d038fb94John McCall  F->setDoesNotThrow(true);
42039dad53772c42eb36ebec1c81c56ba99d038fb94John McCall}
42139dad53772c42eb36ebec1c81c56ba99d038fb94John McCall
422d26bc76c98006609002d9930f8840490e88ac5b5John McCallvoid CodeGenFunction::GenerateCode(GlobalDecl GD, llvm::Function *Fn,
423d26bc76c98006609002d9930f8840490e88ac5b5John McCall                                   const CGFunctionInfo &FnInfo) {
4240ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlsson  const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
4250ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlsson
426e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson  // Check if we should generate debug info for this function.
427aa11289f754d220c9c155b68a4f84cdcfcefef6aDevang Patel  if (CGM.getModuleDebugInfo() && !FD->hasAttr<NoDebugAttr>())
428aa11289f754d220c9c155b68a4f84cdcfcefef6aDevang Patel    DebugInfo = CGM.getModuleDebugInfo();
4291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4307c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar  FunctionArgList Args;
4314c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  QualType ResTy = FD->getResultType();
4321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4336a1e0eb557d47e85185e09bdf8721f53f4bf9c9cMike Stump  CurGD = GD;
4344c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  if (isa<CXXMethodDecl>(FD) && cast<CXXMethodDecl>(FD)->isInstance())
4354c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    CGM.getCXXABI().BuildInstanceFunctionParams(*this, ResTy, Args);
4361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4376e94f6c0f124c9a88b3dae0eea5e6b27957df183Chad Rosier  for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i)
4386e94f6c0f124c9a88b3dae0eea5e6b27957df183Chad Rosier    Args.push_back(FD->getParamDecl(i));
439af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
440a355e07454463b19829ac92ffd115a097faff0e0John McCall  SourceRange BodyRange;
441a355e07454463b19829ac92ffd115a097faff0e0John McCall  if (Stmt *Body = FD->getBody()) BodyRange = Body->getSourceRange();
4424365bba95be15342575b4f030c6583a770a5da3dAnders Carlsson
443a355e07454463b19829ac92ffd115a097faff0e0John McCall  // Emit the standard function prologue.
444d26bc76c98006609002d9930f8840490e88ac5b5John McCall  StartFunction(GD, ResTy, Fn, FnInfo, Args, BodyRange.getBegin());
4451851a12605bc6f1ea70d11974a315340ebaab6ebAnders Carlsson
446a355e07454463b19829ac92ffd115a097faff0e0John McCall  // Generate the body of the function.
4479fc6a7774643a810c8501dae2323e863fefb623eJohn McCall  if (isa<CXXDestructorDecl>(FD))
4489fc6a7774643a810c8501dae2323e863fefb623eJohn McCall    EmitDestructorBody(Args);
4499fc6a7774643a810c8501dae2323e863fefb623eJohn McCall  else if (isa<CXXConstructorDecl>(FD))
4509fc6a7774643a810c8501dae2323e863fefb623eJohn McCall    EmitConstructorBody(Args);
4514e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  else if (getContext().getLangOpts().CUDA &&
452a4ae2294b6ebfb2554aacb6a6a0682fb5ed1f276Peter Collingbourne           !CGM.getCodeGenOpts().CUDAIsDevice &&
453a4ae2294b6ebfb2554aacb6a6a0682fb5ed1f276Peter Collingbourne           FD->hasAttr<CUDAGlobalAttr>())
454a4ae2294b6ebfb2554aacb6a6a0682fb5ed1f276Peter Collingbourne    CGM.getCUDARuntime().EmitDeviceStubBody(*this, Args);
455bd89f8c2caa9550e41daa1aa9bf30f0f1e0dfaf7Eli Friedman  else if (isa<CXXConversionDecl>(FD) &&
45627dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor           cast<CXXConversionDecl>(FD)->isLambdaToBlockPointerConversion()) {
45727dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor    // The lambda conversion to block pointer is special; the semantics can't be
45827dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor    // expressed in the AST, so IRGen needs to special-case it.
45927dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor    EmitLambdaToBlockPointerBody(Args);
46027dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor  } else if (isa<CXXMethodDecl>(FD) &&
46127dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor             cast<CXXMethodDecl>(FD)->isLambdaStaticInvoker()) {
46227dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor    // The lambda "__invoke" function is special, because it forwards or
46327dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor    // clones the body of the function call operator (but is actually static).
46427dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor    EmitLambdaStaticInvokeFunction(cast<CXXMethodDecl>(FD));
465bd89f8c2caa9550e41daa1aa9bf30f0f1e0dfaf7Eli Friedman  }
4669fc6a7774643a810c8501dae2323e863fefb623eJohn McCall  else
4679fc6a7774643a810c8501dae2323e863fefb623eJohn McCall    EmitFunctionBody(Args);
468c33e4ba20304e222692e77f2c6ad26a5d8d32f83Anders Carlsson
469a355e07454463b19829ac92ffd115a097faff0e0John McCall  // Emit the standard function epilogue.
470a355e07454463b19829ac92ffd115a097faff0e0John McCall  FinishFunction(BodyRange.getEnd());
47139dad53772c42eb36ebec1c81c56ba99d038fb94John McCall
47239dad53772c42eb36ebec1c81c56ba99d038fb94John McCall  // If we haven't marked the function nothrow through other means, do
47339dad53772c42eb36ebec1c81c56ba99d038fb94John McCall  // a quick pass now to see if we can.
47439dad53772c42eb36ebec1c81c56ba99d038fb94John McCall  if (!CurFn->doesNotThrow())
47539dad53772c42eb36ebec1c81c56ba99d038fb94John McCall    TryMarkNoThrow(CurFn);
4765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
4775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4780946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner/// ContainsLabel - Return true if the statement contains a label in it.  If
4790946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner/// this statement is not executed normally, it not containing a label means
4800946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner/// that we can just remove the code.
4810946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattnerbool CodeGenFunction::ContainsLabel(const Stmt *S, bool IgnoreCaseStmts) {
4820946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // Null statement, not a label!
4830946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  if (S == 0) return false;
4841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4850946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // If this is a label, we have to emit the code, consider something like:
4860946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // if (0) {  ...  foo:  bar(); }  goto foo;
487ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  //
488ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  // TODO: If anyone cared, we could track __label__'s, since we know that you
489ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  // can't jump to one from outside their declared region.
4900946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  if (isa<LabelStmt>(S))
4910946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner    return true;
492ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner
4930946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // If this is a case/default statement, and we haven't seen a switch, we have
4940946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // to emit the code.
4950946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  if (isa<SwitchCase>(S) && !IgnoreCaseStmts)
4960946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner    return true;
4971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4980946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // If this is a switch statement, we want to ignore cases below it.
4990946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  if (isa<SwitchStmt>(S))
5000946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner    IgnoreCaseStmts = true;
5011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5020946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // Scan subexpressions for verboten labels.
5037502c1d3ce8bb97bcc4f7bebef507040bd93b26fJohn McCall  for (Stmt::const_child_range I = S->children(); I; ++I)
5040946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner    if (ContainsLabel(*I, IgnoreCaseStmts))
5050946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner      return true;
5061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5070946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  return false;
5080946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner}
5090946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner
510ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner/// containsBreak - Return true if the statement contains a break out of it.
511ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner/// If the statement (recursively) contains a switch or loop with a break
512ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner/// inside of it, this is fine.
513ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattnerbool CodeGenFunction::containsBreak(const Stmt *S) {
514ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  // Null statement, not a label!
515ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  if (S == 0) return false;
516ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner
517ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  // If this is a switch or loop that defines its own break scope, then we can
518ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  // include it and anything inside of it.
519ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || isa<DoStmt>(S) ||
520ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner      isa<ForStmt>(S))
5212bef7f5499541e3b68f114cc4d7d197e9a902fe7Chris Lattner    return false;
5222bef7f5499541e3b68f114cc4d7d197e9a902fe7Chris Lattner
5232bef7f5499541e3b68f114cc4d7d197e9a902fe7Chris Lattner  if (isa<BreakStmt>(S))
524ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner    return true;
525ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner
526ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  // Scan subexpressions for verboten breaks.
527ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  for (Stmt::const_child_range I = S->children(); I; ++I)
528ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner    if (containsBreak(*I))
529ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner      return true;
530ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner
531ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  return false;
532ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner}
533ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner
53431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
535c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner/// ConstantFoldsToSimpleInteger - If the specified expression does not fold
536c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner/// to a constant, or if it does but contains a label, return false.  If it
537c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner/// constant folds return true and set the boolean result in Result.
538c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattnerbool CodeGenFunction::ConstantFoldsToSimpleInteger(const Expr *Cond,
539c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner                                                   bool &ResultBool) {
540ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  llvm::APInt ResultInt;
541ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  if (!ConstantFoldsToSimpleInteger(Cond, ResultInt))
542ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner    return false;
543ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner
544ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  ResultBool = ResultInt.getBoolValue();
545ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  return true;
546ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner}
547ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner
548ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner/// ConstantFoldsToSimpleInteger - If the specified expression does not fold
549ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner/// to a constant, or if it does but contains a label, return false.  If it
550ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner/// constant folds return true and set the folded value.
551ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattnerbool CodeGenFunction::
552ef425a69006afaa87751ee41ccf8ff405d9ede70Chris LattnerConstantFoldsToSimpleInteger(const Expr *Cond, llvm::APInt &ResultInt) {
55336bc14c3a1cf63ee306df5687ac8e85f924f8639Daniel Dunbar  // FIXME: Rename and handle conversion of other evaluatable things
55436bc14c3a1cf63ee306df5687ac8e85f924f8639Daniel Dunbar  // to bool.
55580d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  llvm::APSInt Int;
55680d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  if (!Cond->EvaluateAsInt(Int, getContext()))
557c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner    return false;  // Not foldable, not integer or not fully evaluatable.
55880d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith
55931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  if (CodeGenFunction::ContainsLabel(Cond))
560c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner    return false;  // Contains a label.
56180d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith
56280d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  ResultInt = Int;
563c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner  return true;
56431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner}
56531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
56631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
567ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner
56831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an if
56931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// statement) to the specified blocks.  Based on the condition, this might try
57031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// to simplify the codegen of the conditional based on the branch.
57131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner///
57231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattnervoid CodeGenFunction::EmitBranchOnBoolExpr(const Expr *Cond,
57331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner                                           llvm::BasicBlock *TrueBlock,
57431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner                                           llvm::BasicBlock *FalseBlock) {
575f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  Cond = Cond->IgnoreParens();
5761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
57731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  if (const BinaryOperator *CondBOp = dyn_cast<BinaryOperator>(Cond)) {
57831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner    // Handle X && Y in a condition.
5792de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    if (CondBOp->getOpcode() == BO_LAnd) {
58031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // If we have "1 && X", simplify the code.  "0 && X" would have constant
58131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // folded if the case was simple enough.
582e3eb83b93751544a5fab19b3824f56aeac454f82Bill Wendling      bool ConstantBool = false;
583c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner      if (ConstantFoldsToSimpleInteger(CondBOp->getLHS(), ConstantBool) &&
584c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner          ConstantBool) {
58531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        // br(1 && X) -> br(X).
58631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
58731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      }
5881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
58931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // If we have "X && 1", simplify the code to use an uncond branch.
59031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // "X && 0" would have been constant folded to 0.
591c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner      if (ConstantFoldsToSimpleInteger(CondBOp->getRHS(), ConstantBool) &&
592c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner          ConstantBool) {
59331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        // br(X && 1) -> br(X).
59431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock);
59531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      }
5961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
59731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // Emit the LHS as a conditional.  If the LHS conditional is false, we
59831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // want to jump to the FalseBlock.
5999615ecb44f549ae9fa2b4db6ff46bc78befbf62cDaniel Dunbar      llvm::BasicBlock *LHSTrue = createBasicBlock("land.lhs.true");
600150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
601150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      ConditionalEvaluation eval(*this);
60231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBranchOnBoolExpr(CondBOp->getLHS(), LHSTrue, FalseBlock);
60331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBlock(LHSTrue);
6041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
60508e9e453f40aff95a59bd67db49b8f050765e1f0Anders Carlsson      // Any temporaries created here are conditional.
606150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      eval.begin(*this);
60731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
608150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      eval.end(*this);
60908e9e453f40aff95a59bd67db49b8f050765e1f0Anders Carlsson
61031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      return;
611c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner    }
612c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner
613c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner    if (CondBOp->getOpcode() == BO_LOr) {
61431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // If we have "0 || X", simplify the code.  "1 || X" would have constant
61531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // folded if the case was simple enough.
616e3eb83b93751544a5fab19b3824f56aeac454f82Bill Wendling      bool ConstantBool = false;
617c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner      if (ConstantFoldsToSimpleInteger(CondBOp->getLHS(), ConstantBool) &&
618c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner          !ConstantBool) {
61931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        // br(0 || X) -> br(X).
62031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
62131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      }
6221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
62331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // If we have "X || 0", simplify the code to use an uncond branch.
62431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // "X || 1" would have been constant folded to 1.
625c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner      if (ConstantFoldsToSimpleInteger(CondBOp->getRHS(), ConstantBool) &&
626c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner          !ConstantBool) {
62731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        // br(X || 0) -> br(X).
62831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock);
62931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      }
6301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
63131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // Emit the LHS as a conditional.  If the LHS conditional is true, we
63231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // want to jump to the TrueBlock.
6339615ecb44f549ae9fa2b4db6ff46bc78befbf62cDaniel Dunbar      llvm::BasicBlock *LHSFalse = createBasicBlock("lor.lhs.false");
634150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
635150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      ConditionalEvaluation eval(*this);
63631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, LHSFalse);
63731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBlock(LHSFalse);
6381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
63908e9e453f40aff95a59bd67db49b8f050765e1f0Anders Carlsson      // Any temporaries created here are conditional.
640150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      eval.begin(*this);
64131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
642150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      eval.end(*this);
64308e9e453f40aff95a59bd67db49b8f050765e1f0Anders Carlsson
64431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      return;
64531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner    }
646552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner  }
6471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
648552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner  if (const UnaryOperator *CondUOp = dyn_cast<UnaryOperator>(Cond)) {
649552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner    // br(!x, t, f) -> br(x, f, t)
6502de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    if (CondUOp->getOpcode() == UO_LNot)
651552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner      return EmitBranchOnBoolExpr(CondUOp->getSubExpr(), FalseBlock, TrueBlock);
65231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  }
6531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
65409b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar  if (const ConditionalOperator *CondOp = dyn_cast<ConditionalOperator>(Cond)) {
655df33a35f6010fea92c786c0d8f85bfd7c73ebd3eEli Friedman    // br(c ? x : y, t, f) -> br(c, br(x, t, f), br(y, t, f))
656df33a35f6010fea92c786c0d8f85bfd7c73ebd3eEli Friedman    llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true");
657df33a35f6010fea92c786c0d8f85bfd7c73ebd3eEli Friedman    llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false");
65809b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar
659df33a35f6010fea92c786c0d8f85bfd7c73ebd3eEli Friedman    ConditionalEvaluation cond(*this);
660df33a35f6010fea92c786c0d8f85bfd7c73ebd3eEli Friedman    EmitBranchOnBoolExpr(CondOp->getCond(), LHSBlock, RHSBlock);
661150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
662df33a35f6010fea92c786c0d8f85bfd7c73ebd3eEli Friedman    cond.begin(*this);
663df33a35f6010fea92c786c0d8f85bfd7c73ebd3eEli Friedman    EmitBlock(LHSBlock);
664df33a35f6010fea92c786c0d8f85bfd7c73ebd3eEli Friedman    EmitBranchOnBoolExpr(CondOp->getLHS(), TrueBlock, FalseBlock);
665df33a35f6010fea92c786c0d8f85bfd7c73ebd3eEli Friedman    cond.end(*this);
666150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
667df33a35f6010fea92c786c0d8f85bfd7c73ebd3eEli Friedman    cond.begin(*this);
668df33a35f6010fea92c786c0d8f85bfd7c73ebd3eEli Friedman    EmitBlock(RHSBlock);
669df33a35f6010fea92c786c0d8f85bfd7c73ebd3eEli Friedman    EmitBranchOnBoolExpr(CondOp->getRHS(), TrueBlock, FalseBlock);
670df33a35f6010fea92c786c0d8f85bfd7c73ebd3eEli Friedman    cond.end(*this);
671150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
672df33a35f6010fea92c786c0d8f85bfd7c73ebd3eEli Friedman    return;
67309b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar  }
67409b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar
67531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  // Emit the code with the fully general case.
67631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  llvm::Value *CondV = EvaluateExprAsBool(Cond);
67731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  Builder.CreateCondBr(CondV, TrueBlock, FalseBlock);
67831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner}
67931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
680488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar/// ErrorUnsupported - Print out an error that codegen doesn't support the
681dc5e8268292046114ffe02e48773572a91a310f1Chris Lattner/// specified stmt yet.
68290df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbarvoid CodeGenFunction::ErrorUnsupported(const Stmt *S, const char *Type,
68390df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar                                       bool OmitOnError) {
68490df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar  CGM.ErrorUnsupported(S, Type, OmitOnError);
685dc5e8268292046114ffe02e48773572a91a310f1Chris Lattner}
686dc5e8268292046114ffe02e48773572a91a310f1Chris Lattner
6877143325db76d6c3dabce82500f8cc7c93a941970John McCall/// emitNonZeroVLAInit - Emit the "zero" initialization of a
6887143325db76d6c3dabce82500f8cc7c93a941970John McCall/// variable-length array whose elements have a non-zero bit-pattern.
6897143325db76d6c3dabce82500f8cc7c93a941970John McCall///
6907143325db76d6c3dabce82500f8cc7c93a941970John McCall/// \param src - a char* pointing to the bit-pattern for a single
6917143325db76d6c3dabce82500f8cc7c93a941970John McCall/// base element of the array
6927143325db76d6c3dabce82500f8cc7c93a941970John McCall/// \param sizeInChars - the total size of the VLA, in chars
6937143325db76d6c3dabce82500f8cc7c93a941970John McCall/// \param align - the total alignment of the VLA
6947143325db76d6c3dabce82500f8cc7c93a941970John McCallstatic void emitNonZeroVLAInit(CodeGenFunction &CGF, QualType baseType,
6957143325db76d6c3dabce82500f8cc7c93a941970John McCall                               llvm::Value *dest, llvm::Value *src,
6967143325db76d6c3dabce82500f8cc7c93a941970John McCall                               llvm::Value *sizeInChars) {
6977143325db76d6c3dabce82500f8cc7c93a941970John McCall  std::pair<CharUnits,CharUnits> baseSizeAndAlign
6987143325db76d6c3dabce82500f8cc7c93a941970John McCall    = CGF.getContext().getTypeInfoInChars(baseType);
6997143325db76d6c3dabce82500f8cc7c93a941970John McCall
7007143325db76d6c3dabce82500f8cc7c93a941970John McCall  CGBuilderTy &Builder = CGF.Builder;
7017143325db76d6c3dabce82500f8cc7c93a941970John McCall
7027143325db76d6c3dabce82500f8cc7c93a941970John McCall  llvm::Value *baseSizeInChars
7037143325db76d6c3dabce82500f8cc7c93a941970John McCall    = llvm::ConstantInt::get(CGF.IntPtrTy, baseSizeAndAlign.first.getQuantity());
7047143325db76d6c3dabce82500f8cc7c93a941970John McCall
7052acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *i8p = Builder.getInt8PtrTy();
7067143325db76d6c3dabce82500f8cc7c93a941970John McCall
7077143325db76d6c3dabce82500f8cc7c93a941970John McCall  llvm::Value *begin = Builder.CreateBitCast(dest, i8p, "vla.begin");
7087143325db76d6c3dabce82500f8cc7c93a941970John McCall  llvm::Value *end = Builder.CreateInBoundsGEP(dest, sizeInChars, "vla.end");
7097143325db76d6c3dabce82500f8cc7c93a941970John McCall
7107143325db76d6c3dabce82500f8cc7c93a941970John McCall  llvm::BasicBlock *originBB = CGF.Builder.GetInsertBlock();
7117143325db76d6c3dabce82500f8cc7c93a941970John McCall  llvm::BasicBlock *loopBB = CGF.createBasicBlock("vla-init.loop");
7127143325db76d6c3dabce82500f8cc7c93a941970John McCall  llvm::BasicBlock *contBB = CGF.createBasicBlock("vla-init.cont");
7137143325db76d6c3dabce82500f8cc7c93a941970John McCall
7147143325db76d6c3dabce82500f8cc7c93a941970John McCall  // Make a loop over the VLA.  C99 guarantees that the VLA element
7157143325db76d6c3dabce82500f8cc7c93a941970John McCall  // count must be nonzero.
7167143325db76d6c3dabce82500f8cc7c93a941970John McCall  CGF.EmitBlock(loopBB);
7177143325db76d6c3dabce82500f8cc7c93a941970John McCall
718bbf3bacb3e0c1ebb3e8a4a8b1330404a7e379315Jay Foad  llvm::PHINode *cur = Builder.CreatePHI(i8p, 2, "vla.cur");
7197143325db76d6c3dabce82500f8cc7c93a941970John McCall  cur->addIncoming(begin, originBB);
7207143325db76d6c3dabce82500f8cc7c93a941970John McCall
7217143325db76d6c3dabce82500f8cc7c93a941970John McCall  // memcpy the individual element bit-pattern.
7227143325db76d6c3dabce82500f8cc7c93a941970John McCall  Builder.CreateMemCpy(cur, src, baseSizeInChars,
7237143325db76d6c3dabce82500f8cc7c93a941970John McCall                       baseSizeAndAlign.second.getQuantity(),
7247143325db76d6c3dabce82500f8cc7c93a941970John McCall                       /*volatile*/ false);
7257143325db76d6c3dabce82500f8cc7c93a941970John McCall
7267143325db76d6c3dabce82500f8cc7c93a941970John McCall  // Go to the next element.
7277143325db76d6c3dabce82500f8cc7c93a941970John McCall  llvm::Value *next = Builder.CreateConstInBoundsGEP1_32(cur, 1, "vla.next");
7287143325db76d6c3dabce82500f8cc7c93a941970John McCall
7297143325db76d6c3dabce82500f8cc7c93a941970John McCall  // Leave if that's the end of the VLA.
7307143325db76d6c3dabce82500f8cc7c93a941970John McCall  llvm::Value *done = Builder.CreateICmpEQ(next, end, "vla-init.isdone");
7317143325db76d6c3dabce82500f8cc7c93a941970John McCall  Builder.CreateCondBr(done, contBB, loopBB);
7327143325db76d6c3dabce82500f8cc7c93a941970John McCall  cur->addIncoming(next, loopBB);
7337143325db76d6c3dabce82500f8cc7c93a941970John McCall
7347143325db76d6c3dabce82500f8cc7c93a941970John McCall  CGF.EmitBlock(contBB);
7357143325db76d6c3dabce82500f8cc7c93a941970John McCall}
7367143325db76d6c3dabce82500f8cc7c93a941970John McCall
7371884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlssonvoid
7381884eb0b5c55edda4893ddec45e7dbad79758782Anders CarlssonCodeGenFunction::EmitNullInitialization(llvm::Value *DestPtr, QualType Ty) {
7390d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson  // Ignore empty classes in C++.
7404e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getContext().getLangOpts().CPlusPlus) {
7410d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson    if (const RecordType *RT = Ty->getAs<RecordType>()) {
7420d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson      if (cast<CXXRecordDecl>(RT->getDecl())->isEmpty())
7430d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson        return;
7440d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson    }
7450d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson  }
7469021718882441dd391a1960084580d3cd19c423aJohn McCall
7479021718882441dd391a1960084580d3cd19c423aJohn McCall  // Cast the dest ptr to the appropriate i8 pointer type.
7489021718882441dd391a1960084580d3cd19c423aJohn McCall  unsigned DestAS =
7499021718882441dd391a1960084580d3cd19c423aJohn McCall    cast<llvm::PointerType>(DestPtr->getType())->getAddressSpace();
7502acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *BP = Builder.getInt8PtrTy(DestAS);
7513d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson  if (DestPtr->getType() != BP)
752578faa837b552403e2002b97fdfbfde14f2448e5Benjamin Kramer    DestPtr = Builder.CreateBitCast(DestPtr, BP);
7533d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson
7543d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson  // Get size and alignment info for this aggregate.
75579be76c0e360d9e9c44285c9838af02adc43e55fKen Dyck  std::pair<CharUnits, CharUnits> TypeInfo =
75679be76c0e360d9e9c44285c9838af02adc43e55fKen Dyck    getContext().getTypeInfoInChars(Ty);
75779be76c0e360d9e9c44285c9838af02adc43e55fKen Dyck  CharUnits Size = TypeInfo.first;
75879be76c0e360d9e9c44285c9838af02adc43e55fKen Dyck  CharUnits Align = TypeInfo.second;
7593d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson
7605576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall  llvm::Value *SizeVal;
7617143325db76d6c3dabce82500f8cc7c93a941970John McCall  const VariableArrayType *vla;
7629021718882441dd391a1960084580d3cd19c423aJohn McCall
7635576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall  // Don't bother emitting a zero-byte memset.
76479be76c0e360d9e9c44285c9838af02adc43e55fKen Dyck  if (Size.isZero()) {
7655576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall    // But note that getTypeInfo returns 0 for a VLA.
7665576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall    if (const VariableArrayType *vlaType =
7675576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall          dyn_cast_or_null<VariableArrayType>(
7685576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall                                          getContext().getAsArrayType(Ty))) {
769bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      QualType eltType;
770bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      llvm::Value *numElts;
771bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      llvm::tie(numElts, eltType) = getVLASize(vlaType);
772bc8d40d85f3fa1e34569834916f18fecaa635152John McCall
773bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      SizeVal = numElts;
774bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      CharUnits eltSize = getContext().getTypeSizeInChars(eltType);
775bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      if (!eltSize.isOne())
776bc8d40d85f3fa1e34569834916f18fecaa635152John McCall        SizeVal = Builder.CreateNUWMul(SizeVal, CGM.getSize(eltSize));
7777143325db76d6c3dabce82500f8cc7c93a941970John McCall      vla = vlaType;
7785576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall    } else {
7795576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall      return;
7805576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall    }
7815576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall  } else {
782bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    SizeVal = CGM.getSize(Size);
7837143325db76d6c3dabce82500f8cc7c93a941970John McCall    vla = 0;
7845576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall  }
7859021718882441dd391a1960084580d3cd19c423aJohn McCall
7869021718882441dd391a1960084580d3cd19c423aJohn McCall  // If the type contains a pointer to data member we can't memset it to zero.
7879021718882441dd391a1960084580d3cd19c423aJohn McCall  // Instead, create a null constant and copy it to the destination.
7887143325db76d6c3dabce82500f8cc7c93a941970John McCall  // TODO: there are other patterns besides zero that we can usefully memset,
7897143325db76d6c3dabce82500f8cc7c93a941970John McCall  // like -1, which happens to be the pattern used by member-pointers.
790f16aa103d3afd42fbca2ab346f191bf745cec092John McCall  if (!CGM.getTypes().isZeroInitializable(Ty)) {
7917143325db76d6c3dabce82500f8cc7c93a941970John McCall    // For a VLA, emit a single element, then splat that over the VLA.
7927143325db76d6c3dabce82500f8cc7c93a941970John McCall    if (vla) Ty = getContext().getBaseElementType(vla);
7935576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall
7949021718882441dd391a1960084580d3cd19c423aJohn McCall    llvm::Constant *NullConstant = CGM.EmitNullConstant(Ty);
7959021718882441dd391a1960084580d3cd19c423aJohn McCall
7969021718882441dd391a1960084580d3cd19c423aJohn McCall    llvm::GlobalVariable *NullVariable =
7979021718882441dd391a1960084580d3cd19c423aJohn McCall      new llvm::GlobalVariable(CGM.getModule(), NullConstant->getType(),
7989021718882441dd391a1960084580d3cd19c423aJohn McCall                               /*isConstant=*/true,
7999021718882441dd391a1960084580d3cd19c423aJohn McCall                               llvm::GlobalVariable::PrivateLinkage,
8005f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                               NullConstant, Twine());
8019021718882441dd391a1960084580d3cd19c423aJohn McCall    llvm::Value *SrcPtr =
8029021718882441dd391a1960084580d3cd19c423aJohn McCall      Builder.CreateBitCast(NullVariable, Builder.getInt8PtrTy());
8039021718882441dd391a1960084580d3cd19c423aJohn McCall
8047143325db76d6c3dabce82500f8cc7c93a941970John McCall    if (vla) return emitNonZeroVLAInit(*this, Ty, DestPtr, SrcPtr, SizeVal);
8057143325db76d6c3dabce82500f8cc7c93a941970John McCall
8069021718882441dd391a1960084580d3cd19c423aJohn McCall    // Get and call the appropriate llvm.memcpy overload.
80779be76c0e360d9e9c44285c9838af02adc43e55fKen Dyck    Builder.CreateMemCpy(DestPtr, SrcPtr, SizeVal, Align.getQuantity(), false);
80888207c9ca218486b93ae7df14e9764cd0c2c3383Chris Lattner    return;
8099021718882441dd391a1960084580d3cd19c423aJohn McCall  }
8109021718882441dd391a1960084580d3cd19c423aJohn McCall
8119021718882441dd391a1960084580d3cd19c423aJohn McCall  // Otherwise, just memset the whole thing to zero.  This is legal
8129021718882441dd391a1960084580d3cd19c423aJohn McCall  // because in LLVM, all default initializers (other than the ones we just
8139021718882441dd391a1960084580d3cd19c423aJohn McCall  // handled above) are guaranteed to have a bit pattern of all zeros.
81479be76c0e360d9e9c44285c9838af02adc43e55fKen Dyck  Builder.CreateMemSet(DestPtr, Builder.getInt8(0), SizeVal,
81579be76c0e360d9e9c44285c9838af02adc43e55fKen Dyck                       Align.getQuantity(), false);
8163d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson}
8173d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson
818ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattnerllvm::BlockAddress *CodeGenFunction::GetAddrOfLabel(const LabelDecl *L) {
819d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // Make sure that there is a block for the indirect goto.
820d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  if (IndirectBranch == 0)
821d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    GetIndirectGotoBlock();
8223d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner
823ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  llvm::BasicBlock *BB = getJumpDestForLabel(L).getBlock();
8243d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner
825d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // Make sure the indirect branch includes all of the address-taken blocks.
826d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  IndirectBranch->addDestination(BB);
827d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  return llvm::BlockAddress::get(CurFn, BB);
8283d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner}
8291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8303d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattnerllvm::BasicBlock *CodeGenFunction::GetIndirectGotoBlock() {
831d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // If we already made the indirect branch for indirect goto, return its block.
832d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  if (IndirectBranch) return IndirectBranch->getParent();
8333d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner
834d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  CGBuilderTy TmpBuilder(createBasicBlock("indirectgoto"));
8353d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner
8363d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner  // Create the PHI node that indirect gotos will add entries to.
837bbf3bacb3e0c1ebb3e8a4a8b1330404a7e379315Jay Foad  llvm::Value *DestVal = TmpBuilder.CreatePHI(Int8PtrTy, 0,
838bbf3bacb3e0c1ebb3e8a4a8b1330404a7e379315Jay Foad                                              "indirect.goto.dest");
8393d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner
840d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // Create the indirect branch instruction.
841d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  IndirectBranch = TmpBuilder.CreateIndirectBr(DestVal);
842d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  return IndirectBranch->getParent();
8430ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar}
844ddf7cac45d85b73127adbbd91a2b28fc7291c57eAnders Carlsson
845bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall/// Computes the length of an array in elements, as well as the base
846bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall/// element type and a properly-typed first element pointer.
847bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCallllvm::Value *CodeGenFunction::emitArrayLength(const ArrayType *origArrayType,
848bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall                                              QualType &baseType,
849bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall                                              llvm::Value *&addr) {
850bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  const ArrayType *arrayType = origArrayType;
851bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
852bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  // If it's a VLA, we have to load the stored size.  Note that
853bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  // this is the size of the VLA in bytes, not its size in elements.
854bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  llvm::Value *numVLAElements = 0;
855bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  if (isa<VariableArrayType>(arrayType)) {
856bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    numVLAElements = getVLASize(cast<VariableArrayType>(arrayType)).first;
857bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
858bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    // Walk into all VLAs.  This doesn't require changes to addr,
859bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    // which has type T* where T is the first non-VLA element type.
860bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    do {
861bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall      QualType elementType = arrayType->getElementType();
862bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall      arrayType = getContext().getAsArrayType(elementType);
863bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
864bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall      // If we only have VLA components, 'addr' requires no adjustment.
865bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall      if (!arrayType) {
866bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall        baseType = elementType;
867bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall        return numVLAElements;
868bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall      }
869bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    } while (isa<VariableArrayType>(arrayType));
870bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
871bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    // We get out here only if we find a constant array type
872bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    // inside the VLA.
873bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  }
874bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
875bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  // We have some number of constant-length arrays, so addr should
876bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  // have LLVM type [M x [N x [...]]]*.  Build a GEP that walks
877bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  // down to the first element of addr.
8785f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<llvm::Value*, 8> gepIndices;
879bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
880bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  // GEP down to the array type.
881bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  llvm::ConstantInt *zero = Builder.getInt32(0);
882bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  gepIndices.push_back(zero);
883bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
884bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  uint64_t countFromCLAs = 1;
8851664d540d1524f0faffd2f839fccb56178975c60Richard Smith  QualType eltType;
886bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
8872acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::ArrayType *llvmArrayType =
8881664d540d1524f0faffd2f839fccb56178975c60Richard Smith    dyn_cast<llvm::ArrayType>(
889bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall      cast<llvm::PointerType>(addr->getType())->getElementType());
8901664d540d1524f0faffd2f839fccb56178975c60Richard Smith  while (llvmArrayType) {
891bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    assert(isa<ConstantArrayType>(arrayType));
892bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    assert(cast<ConstantArrayType>(arrayType)->getSize().getZExtValue()
893bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall             == llvmArrayType->getNumElements());
894bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
895bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    gepIndices.push_back(zero);
896bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    countFromCLAs *= llvmArrayType->getNumElements();
8971664d540d1524f0faffd2f839fccb56178975c60Richard Smith    eltType = arrayType->getElementType();
898bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
899bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    llvmArrayType =
900bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall      dyn_cast<llvm::ArrayType>(llvmArrayType->getElementType());
901bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    arrayType = getContext().getAsArrayType(arrayType->getElementType());
9021664d540d1524f0faffd2f839fccb56178975c60Richard Smith    assert((!llvmArrayType || arrayType) &&
9031664d540d1524f0faffd2f839fccb56178975c60Richard Smith           "LLVM and Clang types are out-of-synch");
904bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  }
905bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
9061664d540d1524f0faffd2f839fccb56178975c60Richard Smith  if (arrayType) {
9071664d540d1524f0faffd2f839fccb56178975c60Richard Smith    // From this point onwards, the Clang array type has been emitted
9081664d540d1524f0faffd2f839fccb56178975c60Richard Smith    // as some other type (probably a packed struct). Compute the array
9091664d540d1524f0faffd2f839fccb56178975c60Richard Smith    // size, and just emit the 'begin' expression as a bitcast.
9101664d540d1524f0faffd2f839fccb56178975c60Richard Smith    while (arrayType) {
9111664d540d1524f0faffd2f839fccb56178975c60Richard Smith      countFromCLAs *=
9121664d540d1524f0faffd2f839fccb56178975c60Richard Smith          cast<ConstantArrayType>(arrayType)->getSize().getZExtValue();
9131664d540d1524f0faffd2f839fccb56178975c60Richard Smith      eltType = arrayType->getElementType();
9141664d540d1524f0faffd2f839fccb56178975c60Richard Smith      arrayType = getContext().getAsArrayType(eltType);
9151664d540d1524f0faffd2f839fccb56178975c60Richard Smith    }
9161664d540d1524f0faffd2f839fccb56178975c60Richard Smith
9171664d540d1524f0faffd2f839fccb56178975c60Richard Smith    unsigned AddressSpace =
9181664d540d1524f0faffd2f839fccb56178975c60Richard Smith        cast<llvm::PointerType>(addr->getType())->getAddressSpace();
9191664d540d1524f0faffd2f839fccb56178975c60Richard Smith    llvm::Type *BaseType = ConvertType(eltType)->getPointerTo(AddressSpace);
9201664d540d1524f0faffd2f839fccb56178975c60Richard Smith    addr = Builder.CreateBitCast(addr, BaseType, "array.begin");
9211664d540d1524f0faffd2f839fccb56178975c60Richard Smith  } else {
9221664d540d1524f0faffd2f839fccb56178975c60Richard Smith    // Create the actual GEP.
9231664d540d1524f0faffd2f839fccb56178975c60Richard Smith    addr = Builder.CreateInBoundsGEP(addr, gepIndices, "array.begin");
9241664d540d1524f0faffd2f839fccb56178975c60Richard Smith  }
925bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
9261664d540d1524f0faffd2f839fccb56178975c60Richard Smith  baseType = eltType;
927bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
928bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  llvm::Value *numElements
929bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    = llvm::ConstantInt::get(SizeTy, countFromCLAs);
930bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
931bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  // If we had any VLA dimensions, factor them in.
932bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  if (numVLAElements)
933bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    numElements = Builder.CreateNUWMul(numVLAElements, numElements);
934bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
935bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  return numElements;
936bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall}
937bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
938bc8d40d85f3fa1e34569834916f18fecaa635152John McCallstd::pair<llvm::Value*, QualType>
939bc8d40d85f3fa1e34569834916f18fecaa635152John McCallCodeGenFunction::getVLASize(QualType type) {
940bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  const VariableArrayType *vla = getContext().getAsVariableArrayType(type);
941bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  assert(vla && "type was not a variable array type!");
942bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  return getVLASize(vla);
943f666b7780d04186521adcaedb0e15dfa4d5e6933Anders Carlsson}
944dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlsson
945bc8d40d85f3fa1e34569834916f18fecaa635152John McCallstd::pair<llvm::Value*, QualType>
946bc8d40d85f3fa1e34569834916f18fecaa635152John McCallCodeGenFunction::getVLASize(const VariableArrayType *type) {
947bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  // The number of elements so far; always size_t.
948bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  llvm::Value *numElements = 0;
9491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
950bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  QualType elementType;
951bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  do {
952bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    elementType = type->getElementType();
953bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    llvm::Value *vlaSize = VLASizeMap[type->getSizeExpr()];
954bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    assert(vlaSize && "no size for VLA!");
955bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    assert(vlaSize->getType() == SizeTy);
9561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
957bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    if (!numElements) {
958bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      numElements = vlaSize;
959bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    } else {
960bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      // It's undefined behavior if this wraps around, so mark it that way.
961bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      numElements = Builder.CreateNUWMul(numElements, vlaSize);
962bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    }
963bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  } while ((type = getContext().getAsVariableArrayType(elementType)));
9641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
965bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  return std::pair<llvm::Value*,QualType>(numElements, elementType);
966bc8d40d85f3fa1e34569834916f18fecaa635152John McCall}
9671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
968bc8d40d85f3fa1e34569834916f18fecaa635152John McCallvoid CodeGenFunction::EmitVariablyModifiedType(QualType type) {
969bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  assert(type->isVariablyModifiedType() &&
970bc8d40d85f3fa1e34569834916f18fecaa635152John McCall         "Must pass variably modified type to EmitVLASizes!");
9711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
972bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  EnsureInsertPoint();
9731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
974bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  // We're going to walk down into the type and look for VLA
975bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  // expressions.
976bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  do {
977bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    assert(type->isVariablyModifiedType());
978bc8d40d85f3fa1e34569834916f18fecaa635152John McCall
979bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    const Type *ty = type.getTypePtr();
980bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    switch (ty->getTypeClass()) {
98106284c1dc56caed19850bc3766c89f51763724c3Abramo Bagnara
982bc8d40d85f3fa1e34569834916f18fecaa635152John McCall#define TYPE(Class, Base)
983bc8d40d85f3fa1e34569834916f18fecaa635152John McCall#define ABSTRACT_TYPE(Class, Base)
98406284c1dc56caed19850bc3766c89f51763724c3Abramo Bagnara#define NON_CANONICAL_TYPE(Class, Base)
985bc8d40d85f3fa1e34569834916f18fecaa635152John McCall#define DEPENDENT_TYPE(Class, Base) case Type::Class:
98606284c1dc56caed19850bc3766c89f51763724c3Abramo Bagnara#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base)
987bc8d40d85f3fa1e34569834916f18fecaa635152John McCall#include "clang/AST/TypeNodes.def"
98806284c1dc56caed19850bc3766c89f51763724c3Abramo Bagnara      llvm_unreachable("unexpected dependent type!");
989bc8d40d85f3fa1e34569834916f18fecaa635152John McCall
990bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    // These types are never variably-modified.
991bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::Builtin:
992bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::Complex:
993bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::Vector:
994bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::ExtVector:
995bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::Record:
996bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::Enum:
9975ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara    case Type::Elaborated:
9985ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara    case Type::TemplateSpecialization:
999bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::ObjCObject:
1000bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::ObjCInterface:
1001bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::ObjCObjectPointer:
1002bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      llvm_unreachable("type class is never variably-modified!");
1003bc8d40d85f3fa1e34569834916f18fecaa635152John McCall
1004bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::Pointer:
1005bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      type = cast<PointerType>(ty)->getPointeeType();
1006bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      break;
1007bc8d40d85f3fa1e34569834916f18fecaa635152John McCall
1008bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::BlockPointer:
1009bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      type = cast<BlockPointerType>(ty)->getPointeeType();
1010bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      break;
1011bc8d40d85f3fa1e34569834916f18fecaa635152John McCall
1012bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::LValueReference:
1013bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::RValueReference:
1014bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      type = cast<ReferenceType>(ty)->getPointeeType();
1015bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      break;
1016bc8d40d85f3fa1e34569834916f18fecaa635152John McCall
1017bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::MemberPointer:
1018bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      type = cast<MemberPointerType>(ty)->getPointeeType();
1019bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      break;
1020bc8d40d85f3fa1e34569834916f18fecaa635152John McCall
1021bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::ConstantArray:
1022bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::IncompleteArray:
1023bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      // Losing element qualification here is fine.
1024bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      type = cast<ArrayType>(ty)->getElementType();
1025bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      break;
1026bc8d40d85f3fa1e34569834916f18fecaa635152John McCall
1027bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::VariableArray: {
1028bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      // Losing element qualification here is fine.
1029bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      const VariableArrayType *vat = cast<VariableArrayType>(ty);
1030bc8d40d85f3fa1e34569834916f18fecaa635152John McCall
1031bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      // Unknown size indication requires no size computation.
1032bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      // Otherwise, evaluate and record it.
1033bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      if (const Expr *size = vat->getSizeExpr()) {
1034bc8d40d85f3fa1e34569834916f18fecaa635152John McCall        // It's possible that we might have emitted this already,
1035bc8d40d85f3fa1e34569834916f18fecaa635152John McCall        // e.g. with a typedef and a pointer to it.
1036bc8d40d85f3fa1e34569834916f18fecaa635152John McCall        llvm::Value *&entry = VLASizeMap[size];
1037bc8d40d85f3fa1e34569834916f18fecaa635152John McCall        if (!entry) {
1038bc8d40d85f3fa1e34569834916f18fecaa635152John McCall          // Always zexting here would be wrong if it weren't
1039bc8d40d85f3fa1e34569834916f18fecaa635152John McCall          // undefined behavior to have a negative bound.
1040bc8d40d85f3fa1e34569834916f18fecaa635152John McCall          entry = Builder.CreateIntCast(EmitScalarExpr(size), SizeTy,
1041bc8d40d85f3fa1e34569834916f18fecaa635152John McCall                                        /*signed*/ false);
1042bc8d40d85f3fa1e34569834916f18fecaa635152John McCall        }
1043bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      }
1044bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      type = vat->getElementType();
1045bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      break;
1046fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson    }
10471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
104806284c1dc56caed19850bc3766c89f51763724c3Abramo Bagnara    case Type::FunctionProto:
1049bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    case Type::FunctionNoProto:
1050bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      type = cast<FunctionType>(ty)->getResultType();
1051bc8d40d85f3fa1e34569834916f18fecaa635152John McCall      break;
1052b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
10535ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara    case Type::Paren:
10545ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara    case Type::TypeOf:
10555ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara    case Type::UnaryTransform:
10565ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara    case Type::Attributed:
10575ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara    case Type::SubstTemplateTypeParm:
10585ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara      // Keep walking after single level desugaring.
10595ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara      type = type.getSingleStepDesugaredType(getContext());
10605ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara      break;
10615ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara
10625ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara    case Type::Typedef:
10635ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara    case Type::Decltype:
10645ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara    case Type::Auto:
10655ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara      // Stop walking: nothing to do.
10665ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara      return;
10675ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara
10685ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara    case Type::TypeOfExpr:
10695ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara      // Stop walking: emit typeof expression.
10705ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara      EmitIgnoredExpr(cast<TypeOfExprType>(ty)->getUnderlyingExpr());
10715ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara      return;
10725ff53b27dfd918a5d9943cd008de51edc8cbec2cAbramo Bagnara
1073b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    case Type::Atomic:
1074b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      type = cast<AtomicType>(ty)->getValueType();
1075b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      break;
1076bc8d40d85f3fa1e34569834916f18fecaa635152John McCall    }
1077bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  } while (type->isVariablyModifiedType());
1078dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlsson}
10794fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman
10804fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedmanllvm::Value* CodeGenFunction::EmitVAListRef(const Expr* E) {
1081bc07a55b68342a230aafd8875cc7a26450dd3f64Dan Gohman  if (getContext().getBuiltinVaListType()->isArrayType())
10824fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman    return EmitScalarExpr(E);
10834fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman  return EmitLValue(E).getAddress();
10844fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman}
10856ccc47698d0311ddabf32fa0f6db8e4f09ac96f8Anders Carlsson
10868d308384a220c7dc81755c47cdcbdd87dac25d5bDevang Patelvoid CodeGenFunction::EmitDeclRefExprDbgValue(const DeclRefExpr *E,
1087189d6ef40eff11b83b2cda941d5ed89a5cef09b2John McCall                                              llvm::Constant *Init) {
108825c2c8fb9309050612009a6571e2660e75531348Devang Patel  assert (Init && "Invalid DeclRefExpr initializer!");
108925c2c8fb9309050612009a6571e2660e75531348Devang Patel  if (CGDebugInfo *Dbg = getDebugInfo())
1090fd00eecad6fa5400cf37269d84361a0551d0e6d3Alexey Samsonov    if (CGM.getCodeGenOpts().DebugInfo >= CodeGenOptions::LimitedDebugInfo)
1091fd00eecad6fa5400cf37269d84361a0551d0e6d3Alexey Samsonov      Dbg->EmitGlobalVariable(E->getDecl(), Init);
10928d308384a220c7dc81755c47cdcbdd87dac25d5bDevang Patel}
109356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
109456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCallCodeGenFunction::PeepholeProtection
109556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCallCodeGenFunction::protectFromPeepholes(RValue rvalue) {
109656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  // At the moment, the only aggressive peephole we do in IR gen
109756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  // is trunc(zext) folding, but if we add more, we can easily
109856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  // extend this protection.
109956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
110056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  if (!rvalue.isScalar()) return PeepholeProtection();
110156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  llvm::Value *value = rvalue.getScalarVal();
110256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  if (!isa<llvm::ZExtInst>(value)) return PeepholeProtection();
110356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
110456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  // Just make an extra bitcast.
110556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  assert(HaveInsertPoint());
110656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  llvm::Instruction *inst = new llvm::BitCastInst(value, value->getType(), "",
110756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall                                                  Builder.GetInsertBlock());
110856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
110956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  PeepholeProtection protection;
111056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  protection.Inst = inst;
111156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  return protection;
111256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall}
111356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
111456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCallvoid CodeGenFunction::unprotectFromPeepholes(PeepholeProtection protection) {
111556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  if (!protection.Inst) return;
111656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
111756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  // In theory, we could try to duplicate the peepholes now, but whatever.
111856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  protection.Inst->eraseFromParent();
111956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall}
112077f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge
112177f68bb90af93b95045fb994e7cd68137adcc132Julien Lerougellvm::Value *CodeGenFunction::EmitAnnotationCall(llvm::Value *AnnotationFn,
112277f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge                                                 llvm::Value *AnnotatedVal,
112377f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge                                                 llvm::StringRef AnnotationStr,
112477f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge                                                 SourceLocation Location) {
112577f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  llvm::Value *Args[4] = {
112677f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge    AnnotatedVal,
112777f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge    Builder.CreateBitCast(CGM.EmitAnnotationString(AnnotationStr), Int8PtrTy),
112877f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge    Builder.CreateBitCast(CGM.EmitAnnotationUnit(Location), Int8PtrTy),
112977f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge    CGM.EmitAnnotationLineNo(Location)
113077f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  };
113177f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  return Builder.CreateCall(AnnotationFn, Args);
113277f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge}
113377f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge
113477f68bb90af93b95045fb994e7cd68137adcc132Julien Lerougevoid CodeGenFunction::EmitVarAnnotations(const VarDecl *D, llvm::Value *V) {
113577f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  assert(D->hasAttr<AnnotateAttr>() && "no annotate attribute");
113677f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  // FIXME We create a new bitcast for every annotation because that's what
113777f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  // llvm-gcc was doing.
113877f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  for (specific_attr_iterator<AnnotateAttr>
113977f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge       ai = D->specific_attr_begin<AnnotateAttr>(),
114077f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge       ae = D->specific_attr_end<AnnotateAttr>(); ai != ae; ++ai)
114177f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge    EmitAnnotationCall(CGM.getIntrinsic(llvm::Intrinsic::var_annotation),
114277f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge                       Builder.CreateBitCast(V, CGM.Int8PtrTy, V->getName()),
114377f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge                       (*ai)->getAnnotation(), D->getLocation());
114477f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge}
114577f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge
114677f68bb90af93b95045fb994e7cd68137adcc132Julien Lerougellvm::Value *CodeGenFunction::EmitFieldAnnotations(const FieldDecl *D,
114777f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge                                                   llvm::Value *V) {
114877f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  assert(D->hasAttr<AnnotateAttr>() && "no annotate attribute");
114977f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  llvm::Type *VTy = V->getType();
115077f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::ptr_annotation,
115177f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge                                    CGM.Int8PtrTy);
115277f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge
115377f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  for (specific_attr_iterator<AnnotateAttr>
115477f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge       ai = D->specific_attr_begin<AnnotateAttr>(),
115577f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge       ae = D->specific_attr_end<AnnotateAttr>(); ai != ae; ++ai) {
115677f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge    // FIXME Always emit the cast inst so we can differentiate between
115777f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge    // annotation on the first field of a struct and annotation on the struct
115877f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge    // itself.
115977f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge    if (VTy != CGM.Int8PtrTy)
116077f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge      V = Builder.Insert(new llvm::BitCastInst(V, CGM.Int8PtrTy));
116177f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge    V = EmitAnnotationCall(F, V, (*ai)->getAnnotation(), D->getLocation());
116277f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge    V = Builder.CreateBitCast(V, VTy);
116377f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  }
116477f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge
116577f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge  return V;
116677f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge}
1167