CodeGenFunction.cpp revision ef425a69006afaa87751ee41ccf8ff405d9ede70
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"
164c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall#include "CGCXXABI.h"
173f2af1002249c8acc9ce17f1fc50324864feb8e1Eli Friedman#include "CGDebugInfo.h"
18f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall#include "CGException.h"
195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Basic/TargetInfo.h"
2031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner#include "clang/AST/APValue.h"
21de7fb8413b13651fd85b7125d08b3c9ac2816d9dDaniel Dunbar#include "clang/AST/ASTContext.h"
22c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel Dunbar#include "clang/AST/Decl.h"
232b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson#include "clang/AST/DeclCXX.h"
246a1e0eb557d47e85185e09bdf8721f53f4bf9c9cMike Stump#include "clang/AST/StmtCXX.h"
257255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner#include "clang/Frontend/CodeGenOptions.h"
264e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump#include "llvm/Target/TargetData.h"
277255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner#include "llvm/Intrinsics.h"
285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace CodeGen;
305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
311eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpCodeGenFunction::CodeGenFunction(CodeGenModule &cgm)
325936e33bf74dd6bf126ceee0f6169a2593d03a69John McCall  : CodeGenTypeCache(cgm), CGM(cgm),
335936e33bf74dd6bf126ceee0f6169a2593d03a69John McCall    Target(CGM.getContext().Target), Builder(cgm.getModule().getContext()),
34d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall    BlockInfo(0), BlockPointer(0),
35ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    NormalCleanupDest(0), EHCleanupDest(0), NextCleanupDestIndex(1),
36f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    ExceptionSlot(0), DebugInfo(0), IndirectBranch(0),
37ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    SwitchInsn(0), CaseRangeBlock(0),
38f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    DidCallStackSave(false), UnreachableBlock(0),
392504941793b549323f9d29c62507cf21d865fadeJohn McCall    CXXThisDecl(0), CXXThisValue(0), CXXVTTDecl(0), CXXVTTValue(0),
40150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    OutermostConditional(0), TerminateLandingPad(0), TerminateHandler(0),
4183252dcfe61aaebcb6bc117e71dc12968729513fChris Lattner    TrapBB(0) {
42c1cfdf8647a499b6b3024f4bd14a236cddb23988Anders Carlsson
439c276ae0f24d4cee8f7954069d4b8eae45d0447dMike Stump  CatchUndefined = getContext().getLangOptions().CatchUndefined;
444c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  CGM.getCXXABI().getMangleContext().startNewFunction();
454111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner}
465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerASTContext &CodeGenFunction::getContext() const {
485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return CGM.getContext();
495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
528b1a343b6b360d63d5dc8a6beb841ce4414c1e00Daniel Dunbarconst llvm::Type *CodeGenFunction::ConvertTypeForMem(QualType T) {
538b1a343b6b360d63d5dc8a6beb841ce4414c1e00Daniel Dunbar  return CGM.getTypes().ConvertTypeForMem(T);
548b1a343b6b360d63d5dc8a6beb841ce4414c1e00Daniel Dunbar}
558b1a343b6b360d63d5dc8a6beb841ce4414c1e00Daniel Dunbar
565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerconst llvm::Type *CodeGenFunction::ConvertType(QualType T) {
575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return CGM.getTypes().ConvertType(T);
585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
604111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattnerbool CodeGenFunction::hasAggregateLLVMType(QualType T) {
61e9d34dc7afe06c9adaacad7a678a0cbbf749ea75Anders Carlsson  return T->isRecordType() || T->isArrayType() || T->isAnyComplexType() ||
62d608cdb7c044365cf4e8764ade1e11e99c176078John McCall    T->isObjCObjectType();
634111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner}
64391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner
651c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbarvoid CodeGenFunction::EmitReturnBlock() {
661c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // For cleanliness, we try to avoid emitting the return block for
671c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // simple cases.
681c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
691c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
701c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  if (CurBB) {
711c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar    assert(!CurBB->getTerminator() && "Unexpected terminated block.");
721c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
7396e18b05ea6b55aa92a1a576f29e9cee73a7e20bDaniel Dunbar    // We have a valid insert point, reuse it if it is empty or there are no
7496e18b05ea6b55aa92a1a576f29e9cee73a7e20bDaniel Dunbar    // explicit jumps to the return block.
75ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    if (CurBB->empty() || ReturnBlock.getBlock()->use_empty()) {
76ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      ReturnBlock.getBlock()->replaceAllUsesWith(CurBB);
77ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      delete ReturnBlock.getBlock();
7896e18b05ea6b55aa92a1a576f29e9cee73a7e20bDaniel Dunbar    } else
79ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      EmitBlock(ReturnBlock.getBlock());
801c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar    return;
811c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  }
821c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
831c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // Otherwise, if the return block is the target of a single direct
841c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // branch then we can just put the code in that block instead. This
851c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // cleans up functions which started with a unified return block.
86ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  if (ReturnBlock.getBlock()->hasOneUse()) {
871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    llvm::BranchInst *BI =
88ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      dyn_cast<llvm::BranchInst>(*ReturnBlock.getBlock()->use_begin());
89f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    if (BI && BI->isUnconditional() &&
90ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall        BI->getSuccessor(0) == ReturnBlock.getBlock()) {
911c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      // Reset insertion point and delete the branch.
921c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      Builder.SetInsertPoint(BI->getParent());
931c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      BI->eraseFromParent();
94ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      delete ReturnBlock.getBlock();
951c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      return;
961c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar    }
971c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  }
981c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
99f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // FIXME: We are at an unreachable point, there is no reason to emit the block
100f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // unless it has uses. However, we still need a place to put the debug
101f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // region.end for now.
1021c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
103ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  EmitBlock(ReturnBlock.getBlock());
104f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall}
105f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
106f1549f66a8216a78112286e3978cea2c29d6334cJohn McCallstatic void EmitIfUsed(CodeGenFunction &CGF, llvm::BasicBlock *BB) {
107f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  if (!BB) return;
108f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  if (!BB->use_empty())
109f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    return CGF.CurFn->getBasicBlockList().push_back(BB);
110f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  delete BB;
1111c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar}
1121c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
113af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbarvoid CodeGenFunction::FinishFunction(SourceLocation EndLoc) {
114391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  assert(BreakContinueStack.empty() &&
115391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner         "mismatched push/pop in break/continue stack!");
1161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Emit function epilog (to return).
1181c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  EmitReturnBlock();
119f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar
120a18652fe1e8233fbf8b67484945c7f7b2bf272beDaniel Dunbar  if (ShouldInstrumentFunction())
121a18652fe1e8233fbf8b67484945c7f7b2bf272beDaniel Dunbar    EmitFunctionInstrumentation("__cyg_profile_func_exit");
1227255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner
123f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar  // Emit debug descriptor for function end.
124e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson  if (CGDebugInfo *DI = getDebugInfo()) {
125f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar    DI->setLocation(EndLoc);
1265a6fbcfd8c15a2296f94a0473a68ec09d429827fDevang Patel    DI->EmitFunctionEnd(Builder);
127f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar  }
128f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar
12935b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner  EmitFunctionEpilog(*CurFnInfo);
130cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump  EmitEndEHSpec(CurCodeDecl);
1315ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar
132f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  assert(EHStack.empty() &&
133f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall         "did not remove all scopes from cleanup stack!");
134f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
135d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // If someone did an indirect goto, emit the indirect goto block at the end of
136d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // the function.
137d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  if (IndirectBranch) {
138d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    EmitBlock(IndirectBranch->getParent());
139d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    Builder.ClearInsertionPoint();
140d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  }
141d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner
142391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  // Remove the AllocaInsertPt instruction, which is just a convenience for us.
143481769b5dc102b0256b35581e787909ad5edfab5Chris Lattner  llvm::Instruction *Ptr = AllocaInsertPt;
144391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  AllocaInsertPt = 0;
145481769b5dc102b0256b35581e787909ad5edfab5Chris Lattner  Ptr->eraseFromParent();
146d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner
147d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // If someone took the address of a label but never did an indirect goto, we
148d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // made a zero entry PHI node, which is illegal, zap it now.
149d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  if (IndirectBranch) {
150d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    llvm::PHINode *PN = cast<llvm::PHINode>(IndirectBranch->getAddress());
151d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    if (PN->getNumIncomingValues() == 0) {
152d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner      PN->replaceAllUsesWith(llvm::UndefValue::get(PN->getType()));
153d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner      PN->eraseFromParent();
154d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    }
155d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  }
156f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
157ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  EmitIfUsed(*this, RethrowBlock.getBlock());
158f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  EmitIfUsed(*this, TerminateLandingPad);
159f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  EmitIfUsed(*this, TerminateHandler);
160f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  EmitIfUsed(*this, UnreachableBlock);
161744016dde06fcffd50931e94a98c850f8b12cd87John McCall
162744016dde06fcffd50931e94a98c850f8b12cd87John McCall  if (CGM.getCodeGenOpts().EmitDeclMetadata)
163744016dde06fcffd50931e94a98c850f8b12cd87John McCall    EmitDeclMetadata();
164c8aa5f1f264fb230c38182adab944232bb160c2bChris Lattner}
165c8aa5f1f264fb230c38182adab944232bb160c2bChris Lattner
1667255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner/// ShouldInstrumentFunction - Return true if the current function should be
1677255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner/// instrumented with __cyg_profile_func_* calls
1687255a2d997b15beae82e627052fdb1b2474495c2Chris Lattnerbool CodeGenFunction::ShouldInstrumentFunction() {
1697255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  if (!CGM.getCodeGenOpts().InstrumentFunctions)
1707255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner    return false;
1717255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  if (CurFuncDecl->hasAttr<NoInstrumentFunctionAttr>())
1727255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner    return false;
1737255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  return true;
1747255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner}
1757255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner
1767255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner/// EmitFunctionInstrumentation - Emit LLVM code to call the specified
1777255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner/// instrumentation function with the current function and the call site, if
1787255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner/// function instrumentation is enabled.
1797255a2d997b15beae82e627052fdb1b2474495c2Chris Lattnervoid CodeGenFunction::EmitFunctionInstrumentation(const char *Fn) {
1808dab6571b2cab96f44d0a1d6e3edbfdb68b7ed6bChris Lattner  const llvm::PointerType *PointerTy;
1817255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  const llvm::FunctionType *FunctionTy;
1827255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  std::vector<const llvm::Type*> ProfileFuncArgs;
1837255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner
1848dab6571b2cab96f44d0a1d6e3edbfdb68b7ed6bChris Lattner  // void __cyg_profile_func_{enter,exit} (void *this_fn, void *call_site);
185d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall  PointerTy = Int8PtrTy;
1868dab6571b2cab96f44d0a1d6e3edbfdb68b7ed6bChris Lattner  ProfileFuncArgs.push_back(PointerTy);
1878dab6571b2cab96f44d0a1d6e3edbfdb68b7ed6bChris Lattner  ProfileFuncArgs.push_back(PointerTy);
188d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall  FunctionTy = llvm::FunctionType::get(llvm::Type::getVoidTy(getLLVMContext()),
189d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall                                       ProfileFuncArgs, false);
1907255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner
1917255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  llvm::Constant *F = CGM.CreateRuntimeFunction(FunctionTy, Fn);
1927255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  llvm::CallInst *CallSite = Builder.CreateCall(
1937255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner    CGM.getIntrinsic(llvm::Intrinsic::returnaddress, 0, 0),
19477b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner    llvm::ConstantInt::get(Int32Ty, 0),
1957255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner    "callsite");
1967255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner
1978dab6571b2cab96f44d0a1d6e3edbfdb68b7ed6bChris Lattner  Builder.CreateCall2(F,
1988dab6571b2cab96f44d0a1d6e3edbfdb68b7ed6bChris Lattner                      llvm::ConstantExpr::getBitCast(CurFn, PointerTy),
1998dab6571b2cab96f44d0a1d6e3edbfdb68b7ed6bChris Lattner                      CallSite);
2007255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner}
2017255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner
202be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divackyvoid CodeGenFunction::EmitMCountInstrumentation() {
203be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky  llvm::FunctionType *FTy =
204be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky    llvm::FunctionType::get(llvm::Type::getVoidTy(getLLVMContext()), false);
205be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky
206be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky  llvm::Constant *MCountFn = CGM.CreateRuntimeFunction(FTy,
207be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky                                                       Target.getMCountName());
208be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky  Builder.CreateCall(MCountFn);
209be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky}
210be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky
2110ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlssonvoid CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy,
2127c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar                                    llvm::Function *Fn,
2132284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar                                    const FunctionArgList &Args,
2142284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar                                    SourceLocation StartLoc) {
2150ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlsson  const Decl *D = GD.getDecl();
2160ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlsson
2174cc1a4703363ff940b6273aeef9d96a87edeb04bAnders Carlsson  DidCallStackSave = false;
218b5437d238752dc297e42410e98d38d5250fe0463Chris Lattner  CurCodeDecl = CurFuncDecl = D;
2197c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar  FnRetTy = RetTy;
220bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  CurFn = Fn;
2215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(CurFn->isDeclaration() && "Function already has body?");
222ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner
223a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen  // Pass inline keyword to optimizer if it appears explicitly on any
224a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen  // declaration.
225a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen  if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
226a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen    for (FunctionDecl::redecl_iterator RI = FD->redecls_begin(),
227a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen           RE = FD->redecls_end(); RI != RE; ++RI)
228a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen      if (RI->isInlineSpecified()) {
229a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen        Fn->addFnAttr(llvm::Attribute::InlineHint);
230a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen        break;
231a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen      }
232a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen
233f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne  if (getContext().getLangOptions().OpenCL) {
234f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne    // Add metadata for a kernel function.
235f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne    if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
236f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne      if (FD->hasAttr<OpenCLKernelAttr>()) {
237f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne        llvm::LLVMContext &Context = getLLVMContext();
238f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne        llvm::NamedMDNode *OpenCLMetadata =
239f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne          CGM.getModule().getOrInsertNamedMetadata("opencl.kernels");
240f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne
241f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne        llvm::Value *Op = Fn;
242f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne        OpenCLMetadata->addOperand(llvm::MDNode::get(Context, &Op, 1));
243f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne      }
244f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne  }
245f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne
24655e874299f2ad827646a4ca9ea38c402aaeb38c9Daniel Dunbar  llvm::BasicBlock *EntryBB = createBasicBlock("entry", CurFn);
2475ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar
2485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Create a marker to make it easy to insert allocas into the entryblock
24955352a2d616cf9fbb621d10faf8b960b4b268bd8Chris Lattner  // later.  Don't create this with the builder, because we don't want it
25055352a2d616cf9fbb621d10faf8b960b4b268bd8Chris Lattner  // folded.
25177b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner  llvm::Value *Undef = llvm::UndefValue::get(Int32Ty);
25277b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner  AllocaInsertPt = new llvm::BitCastInst(Undef, Int32Ty, "", EntryBB);
253f1466848dce9c4c75d96a6cabdc8db560e26aac8Chris Lattner  if (Builder.isNamePreserving())
254f1466848dce9c4c75d96a6cabdc8db560e26aac8Chris Lattner    AllocaInsertPt->setName("allocapt");
2551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
256f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  ReturnBlock = getJumpDestInCurrentScope("return");
2571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25855352a2d616cf9fbb621d10faf8b960b4b268bd8Chris Lattner  Builder.SetInsertPoint(EntryBB);
2591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
260af99417156c652a6f04dff643925036dc3241d60Sanjiv Gupta  // Emit subprogram debug descriptor.
261e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson  if (CGDebugInfo *DI = getDebugInfo()) {
262e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    // FIXME: what is going on here and why does it ignore all these
263e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    // interesting type properties?
264e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    QualType FnType =
265e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall      getContext().getFunctionType(RetTy, 0, 0,
266e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall                                   FunctionProtoType::ExtProtoInfo());
267e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
2682284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar    DI->setLocation(StartLoc);
2699c6c3a0e3ae09626d2d4b04e4ffa42c3d7cab32bDevang Patel    DI->EmitFunctionStart(GD, FnType, CurFn, Builder);
270af99417156c652a6f04dff643925036dc3241d60Sanjiv Gupta  }
271af99417156c652a6f04dff643925036dc3241d60Sanjiv Gupta
272a18652fe1e8233fbf8b67484945c7f7b2bf272beDaniel Dunbar  if (ShouldInstrumentFunction())
273a18652fe1e8233fbf8b67484945c7f7b2bf272beDaniel Dunbar    EmitFunctionInstrumentation("__cyg_profile_func_enter");
2747255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner
275be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky  if (CGM.getCodeGenOpts().InstrumentForProfiling)
276be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky    EmitMCountInstrumentation();
277be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky
27888b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar  // FIXME: Leaked.
27904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  // CC info is ignored, hopefully?
28004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  CurFnInfo = &CGM.getTypes().getFunctionInfo(FnRetTy, Args,
281264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                              FunctionType::ExtInfo());
282b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman
283b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman  if (RetTy->isVoidType()) {
284b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman    // Void type; nothing to return.
285b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman    ReturnValue = 0;
286b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman  } else if (CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect &&
287b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman             hasAggregateLLVMType(CurFnInfo->getReturnType())) {
288b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman    // Indirect aggregate return; emit returned value directly into sret slot.
289647a1ec397fa13af176d07d9f5d071560a94c7a9Daniel Dunbar    // This reduces code size, and affects correctness in C++.
290b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman    ReturnValue = CurFn->arg_begin();
291b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman  } else {
292647a1ec397fa13af176d07d9f5d071560a94c7a9Daniel Dunbar    ReturnValue = CreateIRTemp(RetTy, "retval");
293b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman  }
294b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman
295cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump  EmitStartEHSpec(CurCodeDecl);
29688b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar  EmitFunctionProlog(*CurFnInfo, CurFn, Args);
2971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2984c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  if (D && isa<CXXMethodDecl>(D) && cast<CXXMethodDecl>(D)->isInstance())
2994c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    CGM.getCXXABI().EmitInstanceFunctionProlog(*this);
3002504941793b549323f9d29c62507cf21d865fadeJohn McCall
301751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson  // If any of the arguments have a variably modified type, make sure to
302751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson  // emit the type size.
303751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson  for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
304751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson       i != e; ++i) {
305751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson    QualType Ty = i->second;
306751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson
307751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson    if (Ty->isVariablyModifiedType())
308751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson      EmitVLASize(Ty);
309751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson  }
3107c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar}
311eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman
3129fc6a7774643a810c8501dae2323e863fefb623eJohn McCallvoid CodeGenFunction::EmitFunctionBody(FunctionArgList &Args) {
3139fc6a7774643a810c8501dae2323e863fefb623eJohn McCall  const FunctionDecl *FD = cast<FunctionDecl>(CurGD.getDecl());
31406a9f3680d22529a2fcf20c52d71cf221d99d910Douglas Gregor  assert(FD->getBody());
31506a9f3680d22529a2fcf20c52d71cf221d99d910Douglas Gregor  EmitStmt(FD->getBody());
316a355e07454463b19829ac92ffd115a097faff0e0John McCall}
317a355e07454463b19829ac92ffd115a097faff0e0John McCall
31839dad53772c42eb36ebec1c81c56ba99d038fb94John McCall/// Tries to mark the given function nounwind based on the
31939dad53772c42eb36ebec1c81c56ba99d038fb94John McCall/// non-existence of any throwing calls within it.  We believe this is
32039dad53772c42eb36ebec1c81c56ba99d038fb94John McCall/// lightweight enough to do at -O0.
32139dad53772c42eb36ebec1c81c56ba99d038fb94John McCallstatic void TryMarkNoThrow(llvm::Function *F) {
322b3a29f132794f67108bccc9c7cc3795365e8a965John McCall  // LLVM treats 'nounwind' on a function as part of the type, so we
323b3a29f132794f67108bccc9c7cc3795365e8a965John McCall  // can't do this on functions that can be overwritten.
324b3a29f132794f67108bccc9c7cc3795365e8a965John McCall  if (F->mayBeOverridden()) return;
325b3a29f132794f67108bccc9c7cc3795365e8a965John McCall
32639dad53772c42eb36ebec1c81c56ba99d038fb94John McCall  for (llvm::Function::iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI)
32739dad53772c42eb36ebec1c81c56ba99d038fb94John McCall    for (llvm::BasicBlock::iterator
32839dad53772c42eb36ebec1c81c56ba99d038fb94John McCall           BI = FI->begin(), BE = FI->end(); BI != BE; ++BI)
32939dad53772c42eb36ebec1c81c56ba99d038fb94John McCall      if (llvm::CallInst *Call = dyn_cast<llvm::CallInst>(&*BI))
33039dad53772c42eb36ebec1c81c56ba99d038fb94John McCall        if (!Call->doesNotThrow())
33139dad53772c42eb36ebec1c81c56ba99d038fb94John McCall          return;
33239dad53772c42eb36ebec1c81c56ba99d038fb94John McCall  F->setDoesNotThrow(true);
33339dad53772c42eb36ebec1c81c56ba99d038fb94John McCall}
33439dad53772c42eb36ebec1c81c56ba99d038fb94John McCall
335c997d4278d329e18891aac9698fb991b2d4622ebAnders Carlssonvoid CodeGenFunction::GenerateCode(GlobalDecl GD, llvm::Function *Fn) {
3360ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlsson  const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
3370ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlsson
338e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson  // Check if we should generate debug info for this function.
3391feade8e520be483293dbf55eb57a51720899589Mike Stump  if (CGM.getDebugInfo() && !FD->hasAttr<NoDebugAttr>())
340e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson    DebugInfo = CGM.getDebugInfo();
3411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3427c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar  FunctionArgList Args;
3434c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  QualType ResTy = FD->getResultType();
3441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3456a1e0eb557d47e85185e09bdf8721f53f4bf9c9cMike Stump  CurGD = GD;
3464c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  if (isa<CXXMethodDecl>(FD) && cast<CXXMethodDecl>(FD)->isInstance())
3474c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    CGM.getCXXABI().BuildInstanceFunctionParams(*this, ResTy, Args);
3481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
349eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman  if (FD->getNumParams()) {
350183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    const FunctionProtoType* FProto = FD->getType()->getAs<FunctionProtoType>();
351eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman    assert(FProto && "Function def must have prototype!");
3527c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar
3537c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar    for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i)
3541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Args.push_back(std::make_pair(FD->getParamDecl(i),
3557c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar                                    FProto->getArgType(i)));
3565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
357af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
358a355e07454463b19829ac92ffd115a097faff0e0John McCall  SourceRange BodyRange;
359a355e07454463b19829ac92ffd115a097faff0e0John McCall  if (Stmt *Body = FD->getBody()) BodyRange = Body->getSourceRange();
3604365bba95be15342575b4f030c6583a770a5da3dAnders Carlsson
361a355e07454463b19829ac92ffd115a097faff0e0John McCall  // Emit the standard function prologue.
3624c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  StartFunction(GD, ResTy, Fn, Args, BodyRange.getBegin());
3631851a12605bc6f1ea70d11974a315340ebaab6ebAnders Carlsson
364a355e07454463b19829ac92ffd115a097faff0e0John McCall  // Generate the body of the function.
3659fc6a7774643a810c8501dae2323e863fefb623eJohn McCall  if (isa<CXXDestructorDecl>(FD))
3669fc6a7774643a810c8501dae2323e863fefb623eJohn McCall    EmitDestructorBody(Args);
3679fc6a7774643a810c8501dae2323e863fefb623eJohn McCall  else if (isa<CXXConstructorDecl>(FD))
3689fc6a7774643a810c8501dae2323e863fefb623eJohn McCall    EmitConstructorBody(Args);
3699fc6a7774643a810c8501dae2323e863fefb623eJohn McCall  else
3709fc6a7774643a810c8501dae2323e863fefb623eJohn McCall    EmitFunctionBody(Args);
371c33e4ba20304e222692e77f2c6ad26a5d8d32f83Anders Carlsson
372a355e07454463b19829ac92ffd115a097faff0e0John McCall  // Emit the standard function epilogue.
373a355e07454463b19829ac92ffd115a097faff0e0John McCall  FinishFunction(BodyRange.getEnd());
37439dad53772c42eb36ebec1c81c56ba99d038fb94John McCall
37539dad53772c42eb36ebec1c81c56ba99d038fb94John McCall  // If we haven't marked the function nothrow through other means, do
37639dad53772c42eb36ebec1c81c56ba99d038fb94John McCall  // a quick pass now to see if we can.
37739dad53772c42eb36ebec1c81c56ba99d038fb94John McCall  if (!CurFn->doesNotThrow())
37839dad53772c42eb36ebec1c81c56ba99d038fb94John McCall    TryMarkNoThrow(CurFn);
3795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
3805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3810946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner/// ContainsLabel - Return true if the statement contains a label in it.  If
3820946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner/// this statement is not executed normally, it not containing a label means
3830946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner/// that we can just remove the code.
3840946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattnerbool CodeGenFunction::ContainsLabel(const Stmt *S, bool IgnoreCaseStmts) {
3850946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // Null statement, not a label!
3860946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  if (S == 0) return false;
3871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3880946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // If this is a label, we have to emit the code, consider something like:
3890946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // if (0) {  ...  foo:  bar(); }  goto foo;
390ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  //
391ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  // TODO: If anyone cared, we could track __label__'s, since we know that you
392ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  // can't jump to one from outside their declared region.
3930946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  if (isa<LabelStmt>(S))
3940946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner    return true;
395ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner
3960946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // If this is a case/default statement, and we haven't seen a switch, we have
3970946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // to emit the code.
3980946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  if (isa<SwitchCase>(S) && !IgnoreCaseStmts)
3990946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner    return true;
4001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4010946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // If this is a switch statement, we want to ignore cases below it.
4020946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  if (isa<SwitchStmt>(S))
4030946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner    IgnoreCaseStmts = true;
4041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4050946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // Scan subexpressions for verboten labels.
4067502c1d3ce8bb97bcc4f7bebef507040bd93b26fJohn McCall  for (Stmt::const_child_range I = S->children(); I; ++I)
4070946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner    if (ContainsLabel(*I, IgnoreCaseStmts))
4080946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner      return true;
4091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4100946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  return false;
4110946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner}
4120946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner
413ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner/// containsBreak - Return true if the statement contains a break out of it.
414ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner/// If the statement (recursively) contains a switch or loop with a break
415ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner/// inside of it, this is fine.
416ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattnerbool CodeGenFunction::containsBreak(const Stmt *S) {
417ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  // Null statement, not a label!
418ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  if (S == 0) return false;
419ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner
420ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  // If this is a switch or loop that defines its own break scope, then we can
421ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  // include it and anything inside of it.
422ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || isa<DoStmt>(S) ||
423ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner      isa<ForStmt>(S))
424ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner    return true;
425ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner
426ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  // Scan subexpressions for verboten breaks.
427ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  for (Stmt::const_child_range I = S->children(); I; ++I)
428ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner    if (containsBreak(*I))
429ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner      return true;
430ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner
431ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  return false;
432ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner}
433ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner
43431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
435c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner/// ConstantFoldsToSimpleInteger - If the specified expression does not fold
436c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner/// to a constant, or if it does but contains a label, return false.  If it
437c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner/// constant folds return true and set the boolean result in Result.
438c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattnerbool CodeGenFunction::ConstantFoldsToSimpleInteger(const Expr *Cond,
439c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner                                                   bool &ResultBool) {
440ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  llvm::APInt ResultInt;
441ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  if (!ConstantFoldsToSimpleInteger(Cond, ResultInt))
442ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner    return false;
443ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner
444ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  ResultBool = ResultInt.getBoolValue();
445ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  return true;
446ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner}
447ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner
448ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner/// ConstantFoldsToSimpleInteger - If the specified expression does not fold
449ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner/// to a constant, or if it does but contains a label, return false.  If it
450ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner/// constant folds return true and set the folded value.
451ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattnerbool CodeGenFunction::
452ef425a69006afaa87751ee41ccf8ff405d9ede70Chris LattnerConstantFoldsToSimpleInteger(const Expr *Cond, llvm::APInt &ResultInt) {
45336bc14c3a1cf63ee306df5687ac8e85f924f8639Daniel Dunbar  // FIXME: Rename and handle conversion of other evaluatable things
45436bc14c3a1cf63ee306df5687ac8e85f924f8639Daniel Dunbar  // to bool.
45564712f196bffd41fb0552c2643b07a25c3e45082Anders Carlsson  Expr::EvalResult Result;
4561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!Cond->Evaluate(Result, getContext()) || !Result.Val.isInt() ||
45764712f196bffd41fb0552c2643b07a25c3e45082Anders Carlsson      Result.HasSideEffects)
458c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner    return false;  // Not foldable, not integer or not fully evaluatable.
459ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner
46031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  if (CodeGenFunction::ContainsLabel(Cond))
461c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner    return false;  // Contains a label.
462ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner
463ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  ResultInt = Result.Val.getInt();
464c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner  return true;
46531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner}
46631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
46731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
468ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner
46931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an if
47031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// statement) to the specified blocks.  Based on the condition, this might try
47131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// to simplify the codegen of the conditional based on the branch.
47231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner///
47331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattnervoid CodeGenFunction::EmitBranchOnBoolExpr(const Expr *Cond,
47431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner                                           llvm::BasicBlock *TrueBlock,
47531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner                                           llvm::BasicBlock *FalseBlock) {
47631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  if (const ParenExpr *PE = dyn_cast<ParenExpr>(Cond))
47731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner    return EmitBranchOnBoolExpr(PE->getSubExpr(), TrueBlock, FalseBlock);
4781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
47931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  if (const BinaryOperator *CondBOp = dyn_cast<BinaryOperator>(Cond)) {
48031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner    // Handle X && Y in a condition.
4812de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    if (CondBOp->getOpcode() == BO_LAnd) {
48231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // If we have "1 && X", simplify the code.  "0 && X" would have constant
48331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // folded if the case was simple enough.
484c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner      bool ConstantBool;
485c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner      if (ConstantFoldsToSimpleInteger(CondBOp->getLHS(), ConstantBool) &&
486c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner          ConstantBool) {
48731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        // br(1 && X) -> br(X).
48831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
48931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      }
4901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
49131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // If we have "X && 1", simplify the code to use an uncond branch.
49231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // "X && 0" would have been constant folded to 0.
493c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner      if (ConstantFoldsToSimpleInteger(CondBOp->getRHS(), ConstantBool) &&
494c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner          ConstantBool) {
49531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        // br(X && 1) -> br(X).
49631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock);
49731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      }
4981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
49931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // Emit the LHS as a conditional.  If the LHS conditional is false, we
50031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // want to jump to the FalseBlock.
5019615ecb44f549ae9fa2b4db6ff46bc78befbf62cDaniel Dunbar      llvm::BasicBlock *LHSTrue = createBasicBlock("land.lhs.true");
502150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
503150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      ConditionalEvaluation eval(*this);
50431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBranchOnBoolExpr(CondBOp->getLHS(), LHSTrue, FalseBlock);
50531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBlock(LHSTrue);
5061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
50708e9e453f40aff95a59bd67db49b8f050765e1f0Anders Carlsson      // Any temporaries created here are conditional.
508150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      eval.begin(*this);
50931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
510150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      eval.end(*this);
51108e9e453f40aff95a59bd67db49b8f050765e1f0Anders Carlsson
51231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      return;
513c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner    }
514c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner
515c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner    if (CondBOp->getOpcode() == BO_LOr) {
51631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // If we have "0 || X", simplify the code.  "1 || X" would have constant
51731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // folded if the case was simple enough.
518c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner      bool ConstantBool;
519c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner      if (ConstantFoldsToSimpleInteger(CondBOp->getLHS(), ConstantBool) &&
520c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner          !ConstantBool) {
52131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        // br(0 || X) -> br(X).
52231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
52331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      }
5241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
52531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // If we have "X || 0", simplify the code to use an uncond branch.
52631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // "X || 1" would have been constant folded to 1.
527c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner      if (ConstantFoldsToSimpleInteger(CondBOp->getRHS(), ConstantBool) &&
528c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner          !ConstantBool) {
52931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        // br(X || 0) -> br(X).
53031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock);
53131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      }
5321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
53331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // Emit the LHS as a conditional.  If the LHS conditional is true, we
53431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // want to jump to the TrueBlock.
5359615ecb44f549ae9fa2b4db6ff46bc78befbf62cDaniel Dunbar      llvm::BasicBlock *LHSFalse = createBasicBlock("lor.lhs.false");
536150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
537150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      ConditionalEvaluation eval(*this);
53831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, LHSFalse);
53931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBlock(LHSFalse);
5401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
54108e9e453f40aff95a59bd67db49b8f050765e1f0Anders Carlsson      // Any temporaries created here are conditional.
542150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      eval.begin(*this);
54331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
544150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      eval.end(*this);
54508e9e453f40aff95a59bd67db49b8f050765e1f0Anders Carlsson
54631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      return;
54731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner    }
548552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner  }
5491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
550552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner  if (const UnaryOperator *CondUOp = dyn_cast<UnaryOperator>(Cond)) {
551552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner    // br(!x, t, f) -> br(x, f, t)
5522de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    if (CondUOp->getOpcode() == UO_LNot)
553552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner      return EmitBranchOnBoolExpr(CondUOp->getSubExpr(), FalseBlock, TrueBlock);
55431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  }
5551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
55609b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar  if (const ConditionalOperator *CondOp = dyn_cast<ConditionalOperator>(Cond)) {
55709b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar    // Handle ?: operator.
55809b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar
55909b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar    // Just ignore GNU ?: extension.
56009b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar    if (CondOp->getLHS()) {
56109b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      // br(c ? x : y, t, f) -> br(c, br(x, t, f), br(y, t, f))
56209b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true");
56309b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false");
564150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
565150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      ConditionalEvaluation cond(*this);
56609b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      EmitBranchOnBoolExpr(CondOp->getCond(), LHSBlock, RHSBlock);
567150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
568150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      cond.begin(*this);
56909b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      EmitBlock(LHSBlock);
57009b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      EmitBranchOnBoolExpr(CondOp->getLHS(), TrueBlock, FalseBlock);
571150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      cond.end(*this);
572150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
573150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      cond.begin(*this);
57409b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      EmitBlock(RHSBlock);
57509b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      EmitBranchOnBoolExpr(CondOp->getRHS(), TrueBlock, FalseBlock);
576150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      cond.end(*this);
577150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
57809b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      return;
57909b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar    }
58009b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar  }
58109b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar
58231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  // Emit the code with the fully general case.
58331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  llvm::Value *CondV = EvaluateExprAsBool(Cond);
58431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  Builder.CreateCondBr(CondV, TrueBlock, FalseBlock);
58531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner}
58631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
587488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar/// ErrorUnsupported - Print out an error that codegen doesn't support the
588dc5e8268292046114ffe02e48773572a91a310f1Chris Lattner/// specified stmt yet.
58990df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbarvoid CodeGenFunction::ErrorUnsupported(const Stmt *S, const char *Type,
59090df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar                                       bool OmitOnError) {
59190df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar  CGM.ErrorUnsupported(S, Type, OmitOnError);
592dc5e8268292046114ffe02e48773572a91a310f1Chris Lattner}
593dc5e8268292046114ffe02e48773572a91a310f1Chris Lattner
5947143325db76d6c3dabce82500f8cc7c93a941970John McCall/// emitNonZeroVLAInit - Emit the "zero" initialization of a
5957143325db76d6c3dabce82500f8cc7c93a941970John McCall/// variable-length array whose elements have a non-zero bit-pattern.
5967143325db76d6c3dabce82500f8cc7c93a941970John McCall///
5977143325db76d6c3dabce82500f8cc7c93a941970John McCall/// \param src - a char* pointing to the bit-pattern for a single
5987143325db76d6c3dabce82500f8cc7c93a941970John McCall/// base element of the array
5997143325db76d6c3dabce82500f8cc7c93a941970John McCall/// \param sizeInChars - the total size of the VLA, in chars
6007143325db76d6c3dabce82500f8cc7c93a941970John McCall/// \param align - the total alignment of the VLA
6017143325db76d6c3dabce82500f8cc7c93a941970John McCallstatic void emitNonZeroVLAInit(CodeGenFunction &CGF, QualType baseType,
6027143325db76d6c3dabce82500f8cc7c93a941970John McCall                               llvm::Value *dest, llvm::Value *src,
6037143325db76d6c3dabce82500f8cc7c93a941970John McCall                               llvm::Value *sizeInChars) {
6047143325db76d6c3dabce82500f8cc7c93a941970John McCall  std::pair<CharUnits,CharUnits> baseSizeAndAlign
6057143325db76d6c3dabce82500f8cc7c93a941970John McCall    = CGF.getContext().getTypeInfoInChars(baseType);
6067143325db76d6c3dabce82500f8cc7c93a941970John McCall
6077143325db76d6c3dabce82500f8cc7c93a941970John McCall  CGBuilderTy &Builder = CGF.Builder;
6087143325db76d6c3dabce82500f8cc7c93a941970John McCall
6097143325db76d6c3dabce82500f8cc7c93a941970John McCall  llvm::Value *baseSizeInChars
6107143325db76d6c3dabce82500f8cc7c93a941970John McCall    = llvm::ConstantInt::get(CGF.IntPtrTy, baseSizeAndAlign.first.getQuantity());
6117143325db76d6c3dabce82500f8cc7c93a941970John McCall
6127143325db76d6c3dabce82500f8cc7c93a941970John McCall  const llvm::Type *i8p = Builder.getInt8PtrTy();
6137143325db76d6c3dabce82500f8cc7c93a941970John McCall
6147143325db76d6c3dabce82500f8cc7c93a941970John McCall  llvm::Value *begin = Builder.CreateBitCast(dest, i8p, "vla.begin");
6157143325db76d6c3dabce82500f8cc7c93a941970John McCall  llvm::Value *end = Builder.CreateInBoundsGEP(dest, sizeInChars, "vla.end");
6167143325db76d6c3dabce82500f8cc7c93a941970John McCall
6177143325db76d6c3dabce82500f8cc7c93a941970John McCall  llvm::BasicBlock *originBB = CGF.Builder.GetInsertBlock();
6187143325db76d6c3dabce82500f8cc7c93a941970John McCall  llvm::BasicBlock *loopBB = CGF.createBasicBlock("vla-init.loop");
6197143325db76d6c3dabce82500f8cc7c93a941970John McCall  llvm::BasicBlock *contBB = CGF.createBasicBlock("vla-init.cont");
6207143325db76d6c3dabce82500f8cc7c93a941970John McCall
6217143325db76d6c3dabce82500f8cc7c93a941970John McCall  // Make a loop over the VLA.  C99 guarantees that the VLA element
6227143325db76d6c3dabce82500f8cc7c93a941970John McCall  // count must be nonzero.
6237143325db76d6c3dabce82500f8cc7c93a941970John McCall  CGF.EmitBlock(loopBB);
6247143325db76d6c3dabce82500f8cc7c93a941970John McCall
6257143325db76d6c3dabce82500f8cc7c93a941970John McCall  llvm::PHINode *cur = Builder.CreatePHI(i8p, "vla.cur");
6267143325db76d6c3dabce82500f8cc7c93a941970John McCall  cur->reserveOperandSpace(2);
6277143325db76d6c3dabce82500f8cc7c93a941970John McCall  cur->addIncoming(begin, originBB);
6287143325db76d6c3dabce82500f8cc7c93a941970John McCall
6297143325db76d6c3dabce82500f8cc7c93a941970John McCall  // memcpy the individual element bit-pattern.
6307143325db76d6c3dabce82500f8cc7c93a941970John McCall  Builder.CreateMemCpy(cur, src, baseSizeInChars,
6317143325db76d6c3dabce82500f8cc7c93a941970John McCall                       baseSizeAndAlign.second.getQuantity(),
6327143325db76d6c3dabce82500f8cc7c93a941970John McCall                       /*volatile*/ false);
6337143325db76d6c3dabce82500f8cc7c93a941970John McCall
6347143325db76d6c3dabce82500f8cc7c93a941970John McCall  // Go to the next element.
6357143325db76d6c3dabce82500f8cc7c93a941970John McCall  llvm::Value *next = Builder.CreateConstInBoundsGEP1_32(cur, 1, "vla.next");
6367143325db76d6c3dabce82500f8cc7c93a941970John McCall
6377143325db76d6c3dabce82500f8cc7c93a941970John McCall  // Leave if that's the end of the VLA.
6387143325db76d6c3dabce82500f8cc7c93a941970John McCall  llvm::Value *done = Builder.CreateICmpEQ(next, end, "vla-init.isdone");
6397143325db76d6c3dabce82500f8cc7c93a941970John McCall  Builder.CreateCondBr(done, contBB, loopBB);
6407143325db76d6c3dabce82500f8cc7c93a941970John McCall  cur->addIncoming(next, loopBB);
6417143325db76d6c3dabce82500f8cc7c93a941970John McCall
6427143325db76d6c3dabce82500f8cc7c93a941970John McCall  CGF.EmitBlock(contBB);
6437143325db76d6c3dabce82500f8cc7c93a941970John McCall}
6447143325db76d6c3dabce82500f8cc7c93a941970John McCall
6451884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlssonvoid
6461884eb0b5c55edda4893ddec45e7dbad79758782Anders CarlssonCodeGenFunction::EmitNullInitialization(llvm::Value *DestPtr, QualType Ty) {
6470d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson  // Ignore empty classes in C++.
6480d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson  if (getContext().getLangOptions().CPlusPlus) {
6490d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson    if (const RecordType *RT = Ty->getAs<RecordType>()) {
6500d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson      if (cast<CXXRecordDecl>(RT->getDecl())->isEmpty())
6510d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson        return;
6520d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson    }
6530d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson  }
6549021718882441dd391a1960084580d3cd19c423aJohn McCall
6559021718882441dd391a1960084580d3cd19c423aJohn McCall  // Cast the dest ptr to the appropriate i8 pointer type.
6569021718882441dd391a1960084580d3cd19c423aJohn McCall  unsigned DestAS =
6579021718882441dd391a1960084580d3cd19c423aJohn McCall    cast<llvm::PointerType>(DestPtr->getType())->getAddressSpace();
6589f0c7cc36d29cf591c33962931f5862847145f3eBenjamin Kramer  const llvm::Type *BP = Builder.getInt8PtrTy(DestAS);
6593d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson  if (DestPtr->getType() != BP)
6603d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson    DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
6613d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson
6623d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson  // Get size and alignment info for this aggregate.
6633d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson  std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty);
6649f0c7cc36d29cf591c33962931f5862847145f3eBenjamin Kramer  uint64_t Size = TypeInfo.first / 8;
6659f0c7cc36d29cf591c33962931f5862847145f3eBenjamin Kramer  unsigned Align = TypeInfo.second / 8;
6663d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson
6675576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall  llvm::Value *SizeVal;
6687143325db76d6c3dabce82500f8cc7c93a941970John McCall  const VariableArrayType *vla;
6699021718882441dd391a1960084580d3cd19c423aJohn McCall
6705576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall  // Don't bother emitting a zero-byte memset.
6715576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall  if (Size == 0) {
6725576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall    // But note that getTypeInfo returns 0 for a VLA.
6735576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall    if (const VariableArrayType *vlaType =
6745576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall          dyn_cast_or_null<VariableArrayType>(
6755576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall                                          getContext().getAsArrayType(Ty))) {
6765576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall      SizeVal = GetVLASize(vlaType);
6777143325db76d6c3dabce82500f8cc7c93a941970John McCall      vla = vlaType;
6785576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall    } else {
6795576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall      return;
6805576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall    }
6815576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall  } else {
6825576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall    SizeVal = llvm::ConstantInt::get(IntPtrTy, Size);
6837143325db76d6c3dabce82500f8cc7c93a941970John McCall    vla = 0;
6845576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall  }
6859021718882441dd391a1960084580d3cd19c423aJohn McCall
6869021718882441dd391a1960084580d3cd19c423aJohn McCall  // If the type contains a pointer to data member we can't memset it to zero.
6879021718882441dd391a1960084580d3cd19c423aJohn McCall  // Instead, create a null constant and copy it to the destination.
6887143325db76d6c3dabce82500f8cc7c93a941970John McCall  // TODO: there are other patterns besides zero that we can usefully memset,
6897143325db76d6c3dabce82500f8cc7c93a941970John McCall  // like -1, which happens to be the pattern used by member-pointers.
690f16aa103d3afd42fbca2ab346f191bf745cec092John McCall  if (!CGM.getTypes().isZeroInitializable(Ty)) {
6917143325db76d6c3dabce82500f8cc7c93a941970John McCall    // For a VLA, emit a single element, then splat that over the VLA.
6927143325db76d6c3dabce82500f8cc7c93a941970John McCall    if (vla) Ty = getContext().getBaseElementType(vla);
6935576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall
6949021718882441dd391a1960084580d3cd19c423aJohn McCall    llvm::Constant *NullConstant = CGM.EmitNullConstant(Ty);
6959021718882441dd391a1960084580d3cd19c423aJohn McCall
6969021718882441dd391a1960084580d3cd19c423aJohn McCall    llvm::GlobalVariable *NullVariable =
6979021718882441dd391a1960084580d3cd19c423aJohn McCall      new llvm::GlobalVariable(CGM.getModule(), NullConstant->getType(),
6989021718882441dd391a1960084580d3cd19c423aJohn McCall                               /*isConstant=*/true,
6999021718882441dd391a1960084580d3cd19c423aJohn McCall                               llvm::GlobalVariable::PrivateLinkage,
7009021718882441dd391a1960084580d3cd19c423aJohn McCall                               NullConstant, llvm::Twine());
7019021718882441dd391a1960084580d3cd19c423aJohn McCall    llvm::Value *SrcPtr =
7029021718882441dd391a1960084580d3cd19c423aJohn McCall      Builder.CreateBitCast(NullVariable, Builder.getInt8PtrTy());
7039021718882441dd391a1960084580d3cd19c423aJohn McCall
7047143325db76d6c3dabce82500f8cc7c93a941970John McCall    if (vla) return emitNonZeroVLAInit(*this, Ty, DestPtr, SrcPtr, SizeVal);
7057143325db76d6c3dabce82500f8cc7c93a941970John McCall
7069021718882441dd391a1960084580d3cd19c423aJohn McCall    // Get and call the appropriate llvm.memcpy overload.
7079f0c7cc36d29cf591c33962931f5862847145f3eBenjamin Kramer    Builder.CreateMemCpy(DestPtr, SrcPtr, SizeVal, Align, false);
70888207c9ca218486b93ae7df14e9764cd0c2c3383Chris Lattner    return;
7099021718882441dd391a1960084580d3cd19c423aJohn McCall  }
7109021718882441dd391a1960084580d3cd19c423aJohn McCall
7119021718882441dd391a1960084580d3cd19c423aJohn McCall  // Otherwise, just memset the whole thing to zero.  This is legal
7129021718882441dd391a1960084580d3cd19c423aJohn McCall  // because in LLVM, all default initializers (other than the ones we just
7139021718882441dd391a1960084580d3cd19c423aJohn McCall  // handled above) are guaranteed to have a bit pattern of all zeros.
7149f0c7cc36d29cf591c33962931f5862847145f3eBenjamin Kramer  Builder.CreateMemSet(DestPtr, Builder.getInt8(0), SizeVal, Align, false);
7153d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson}
7163d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson
717ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattnerllvm::BlockAddress *CodeGenFunction::GetAddrOfLabel(const LabelDecl *L) {
718d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // Make sure that there is a block for the indirect goto.
719d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  if (IndirectBranch == 0)
720d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    GetIndirectGotoBlock();
7213d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner
722ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  llvm::BasicBlock *BB = getJumpDestForLabel(L).getBlock();
7233d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner
724d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // Make sure the indirect branch includes all of the address-taken blocks.
725d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  IndirectBranch->addDestination(BB);
726d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  return llvm::BlockAddress::get(CurFn, BB);
7273d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner}
7281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7293d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattnerllvm::BasicBlock *CodeGenFunction::GetIndirectGotoBlock() {
730d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // If we already made the indirect branch for indirect goto, return its block.
731d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  if (IndirectBranch) return IndirectBranch->getParent();
7323d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner
733d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  CGBuilderTy TmpBuilder(createBasicBlock("indirectgoto"));
7343d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner
7353d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner  // Create the PHI node that indirect gotos will add entries to.
736d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  llvm::Value *DestVal = TmpBuilder.CreatePHI(Int8PtrTy, "indirect.goto.dest");
7373d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner
738d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // Create the indirect branch instruction.
739d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  IndirectBranch = TmpBuilder.CreateIndirectBr(DestVal);
740d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  return IndirectBranch->getParent();
7410ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar}
742ddf7cac45d85b73127adbbd91a2b28fc7291c57eAnders Carlsson
743d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbarllvm::Value *CodeGenFunction::GetVLASize(const VariableArrayType *VAT) {
744bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman  llvm::Value *&SizeEntry = VLASizeMap[VAT->getSizeExpr()];
7451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
746f666b7780d04186521adcaedb0e15dfa4d5e6933Anders Carlsson  assert(SizeEntry && "Did not emit size for type");
747f666b7780d04186521adcaedb0e15dfa4d5e6933Anders Carlsson  return SizeEntry;
748f666b7780d04186521adcaedb0e15dfa4d5e6933Anders Carlsson}
749dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlsson
750d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbarllvm::Value *CodeGenFunction::EmitVLASize(QualType Ty) {
75160d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson  assert(Ty->isVariablyModifiedType() &&
75260d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson         "Must pass variably modified type to EmitVLASizes!");
7531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
754d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar  EnsureInsertPoint();
7551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
75660d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson  if (const VariableArrayType *VAT = getContext().getAsVariableArrayType(Ty)) {
757745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian    // unknown size indication requires no size computation.
758745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian    if (!VAT->getSizeExpr())
759745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian      return 0;
760bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman    llvm::Value *&SizeEntry = VLASizeMap[VAT->getSizeExpr()];
7611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
762fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson    if (!SizeEntry) {
76396f214776c0f69069fee4d67557c8c7f416009a8Anders Carlsson      const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
7641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
765ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner      // Get the element size;
766ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner      QualType ElemTy = VAT->getElementType();
767ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner      llvm::Value *ElemSize;
768fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson      if (ElemTy->isVariableArrayType())
769fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson        ElemSize = EmitVLASize(ElemTy);
770ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner      else
7714a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson        ElemSize = llvm::ConstantInt::get(SizeTy,
772199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck            getContext().getTypeSizeInChars(ElemTy).getQuantity());
7731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
774fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson      llvm::Value *NumElements = EmitScalarExpr(VAT->getSizeExpr());
77596f214776c0f69069fee4d67557c8c7f416009a8Anders Carlsson      NumElements = Builder.CreateIntCast(NumElements, SizeTy, false, "tmp");
7761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
777fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson      SizeEntry = Builder.CreateMul(ElemSize, NumElements);
778fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson    }
7791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
78060d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson    return SizeEntry;
781dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlsson  }
7821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
783ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner  if (const ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
784ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner    EmitVLASize(AT->getElementType());
785ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner    return 0;
7861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
7871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
788075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  if (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
789075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    EmitVLASize(PT->getInnerType());
790075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    return 0;
791075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  }
792075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
793ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner  const PointerType *PT = Ty->getAs<PointerType>();
794ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner  assert(PT && "unknown VM type!");
795ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner  EmitVLASize(PT->getPointeeType());
79660d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson  return 0;
797dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlsson}
7984fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman
7994fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedmanllvm::Value* CodeGenFunction::EmitVAListRef(const Expr* E) {
800bc07a55b68342a230aafd8875cc7a26450dd3f64Dan Gohman  if (getContext().getBuiltinVaListType()->isArrayType())
8014fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman    return EmitScalarExpr(E);
8024fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman  return EmitLValue(E).getAddress();
8034fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman}
8046ccc47698d0311ddabf32fa0f6db8e4f09ac96f8Anders Carlsson
8058d308384a220c7dc81755c47cdcbdd87dac25d5bDevang Patelvoid CodeGenFunction::EmitDeclRefExprDbgValue(const DeclRefExpr *E,
806189d6ef40eff11b83b2cda941d5ed89a5cef09b2John McCall                                              llvm::Constant *Init) {
80725c2c8fb9309050612009a6571e2660e75531348Devang Patel  assert (Init && "Invalid DeclRefExpr initializer!");
80825c2c8fb9309050612009a6571e2660e75531348Devang Patel  if (CGDebugInfo *Dbg = getDebugInfo())
809d2829b76a87a70791184b3cac89900d2cda46ce4Devang Patel    Dbg->EmitGlobalVariable(E->getDecl(), Init);
8108d308384a220c7dc81755c47cdcbdd87dac25d5bDevang Patel}
81156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
81256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCallCodeGenFunction::PeepholeProtection
81356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCallCodeGenFunction::protectFromPeepholes(RValue rvalue) {
81456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  // At the moment, the only aggressive peephole we do in IR gen
81556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  // is trunc(zext) folding, but if we add more, we can easily
81656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  // extend this protection.
81756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
81856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  if (!rvalue.isScalar()) return PeepholeProtection();
81956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  llvm::Value *value = rvalue.getScalarVal();
82056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  if (!isa<llvm::ZExtInst>(value)) return PeepholeProtection();
82156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
82256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  // Just make an extra bitcast.
82356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  assert(HaveInsertPoint());
82456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  llvm::Instruction *inst = new llvm::BitCastInst(value, value->getType(), "",
82556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall                                                  Builder.GetInsertBlock());
82656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
82756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  PeepholeProtection protection;
82856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  protection.Inst = inst;
82956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  return protection;
83056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall}
83156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
83256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCallvoid CodeGenFunction::unprotectFromPeepholes(PeepholeProtection protection) {
83356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  if (!protection.Inst) return;
83456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
83556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  // In theory, we could try to duplicate the peepholes now, but whatever.
83656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  protection.Inst->eraseFromParent();
83756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall}
838