CodeGenFunction.cpp revision 35415f5132f70ad5097a3514ab84251e10db3664
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"
163f2af1002249c8acc9ce17f1fc50324864feb8e1Eli Friedman#include "CGDebugInfo.h"
175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Basic/TargetInfo.h"
1831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner#include "clang/AST/APValue.h"
19de7fb8413b13651fd85b7125d08b3c9ac2816d9dDaniel Dunbar#include "clang/AST/ASTContext.h"
20c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel Dunbar#include "clang/AST/Decl.h"
212b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson#include "clang/AST/DeclCXX.h"
226a1e0eb557d47e85185e09bdf8721f53f4bf9c9cMike Stump#include "clang/AST/StmtCXX.h"
234e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump#include "llvm/Target/TargetData.h"
245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace CodeGen;
265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
271eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpCodeGenFunction::CodeGenFunction(CodeGenModule &cgm)
28a4f668f3b7e03629066a01b04e415cb2b4655dafMike Stump  : BlockFunction(cgm, *this, Builder), CGM(cgm),
29a4f668f3b7e03629066a01b04e415cb2b4655dafMike Stump    Target(CGM.getContext().Target),
30aac8705c046f01a264a4f82832895a5d9e695633Owen Anderson    Builder(cgm.getModule().getContext()),
31d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    DebugInfo(0), IndirectBranch(0),
323d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner    SwitchInsn(0), CaseRangeBlock(0), InvokeDest(0),
332504941793b549323f9d29c62507cf21d865fadeJohn McCall    CXXThisDecl(0), CXXThisValue(0), CXXVTTDecl(0), CXXVTTValue(0),
3415037caa1542bb810ad54c653aeb80f61df7b00cMike Stump    ConditionalBranchLevel(0), TerminateHandler(0), TrapBB(0),
35be07f60131bc6f8d6696f4644df1ef667a1730d5Mike Stump    UniqueAggrDestructorCount(0) {
364e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump  LLVMIntTy = ConvertType(getContext().IntTy);
374e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump  LLVMPointerWidth = Target.getPointerWidth(0);
38d88ea5687968640ada2bc5a10211cbeb68a671ecMike Stump  Exceptions = getContext().getLangOptions().Exceptions;
399c276ae0f24d4cee8f7954069d4b8eae45d0447dMike Stump  CatchUndefined = getContext().getLangOptions().CatchUndefined;
4035415f5132f70ad5097a3514ab84251e10db3664Douglas Gregor  CGM.getMangleContext().startNewFunction();
414111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner}
425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerASTContext &CodeGenFunction::getContext() const {
445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return CGM.getContext();
455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerllvm::BasicBlock *CodeGenFunction::getBasicBlockForLabel(const LabelStmt *S) {
495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::BasicBlock *&BB = LabelMap[S];
505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (BB) return BB;
511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Create, but don't insert, the new block.
5355e874299f2ad827646a4ca9ea38c402aaeb38c9Daniel Dunbar  return BB = createBasicBlock(S->getName());
545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
560096acf421c4609ce7f43e8b05f8c5ca866d4611Daniel Dunbarllvm::Value *CodeGenFunction::GetAddrOfLocalVar(const VarDecl *VD) {
570096acf421c4609ce7f43e8b05f8c5ca866d4611Daniel Dunbar  llvm::Value *Res = LocalDeclMap[VD];
580096acf421c4609ce7f43e8b05f8c5ca866d4611Daniel Dunbar  assert(Res && "Invalid argument to GetAddrOfLocalVar(), no decl!");
590096acf421c4609ce7f43e8b05f8c5ca866d4611Daniel Dunbar  return Res;
60813733577d33ec56479667b49e1bff42dc6bba90Lauro Ramos Venancio}
615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
620096acf421c4609ce7f43e8b05f8c5ca866d4611Daniel Dunbarllvm::Constant *
630096acf421c4609ce7f43e8b05f8c5ca866d4611Daniel DunbarCodeGenFunction::GetAddrOfStaticLocalVar(const VarDecl *BVD) {
640096acf421c4609ce7f43e8b05f8c5ca866d4611Daniel Dunbar  return cast<llvm::Constant>(GetAddrOfLocalVar(BVD));
65dde0a94120915fa925d1ffcdb997c7b44dc9fa21Anders Carlsson}
66dde0a94120915fa925d1ffcdb997c7b44dc9fa21Anders Carlsson
678b1a343b6b360d63d5dc8a6beb841ce4414c1e00Daniel Dunbarconst llvm::Type *CodeGenFunction::ConvertTypeForMem(QualType T) {
688b1a343b6b360d63d5dc8a6beb841ce4414c1e00Daniel Dunbar  return CGM.getTypes().ConvertTypeForMem(T);
698b1a343b6b360d63d5dc8a6beb841ce4414c1e00Daniel Dunbar}
708b1a343b6b360d63d5dc8a6beb841ce4414c1e00Daniel Dunbar
715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerconst llvm::Type *CodeGenFunction::ConvertType(QualType T) {
725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return CGM.getTypes().ConvertType(T);
735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
754111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattnerbool CodeGenFunction::hasAggregateLLVMType(QualType T) {
76e9d34dc7afe06c9adaacad7a678a0cbbf749ea75Anders Carlsson  return T->isRecordType() || T->isArrayType() || T->isAnyComplexType() ||
77e9d34dc7afe06c9adaacad7a678a0cbbf749ea75Anders Carlsson    T->isMemberFunctionPointerType();
784111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner}
79391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner
801c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbarvoid CodeGenFunction::EmitReturnBlock() {
811c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // For cleanliness, we try to avoid emitting the return block for
821c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // simple cases.
831c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
841c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
851c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  if (CurBB) {
861c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar    assert(!CurBB->getTerminator() && "Unexpected terminated block.");
871c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
8896e18b05ea6b55aa92a1a576f29e9cee73a7e20bDaniel Dunbar    // We have a valid insert point, reuse it if it is empty or there are no
8996e18b05ea6b55aa92a1a576f29e9cee73a7e20bDaniel Dunbar    // explicit jumps to the return block.
9096e18b05ea6b55aa92a1a576f29e9cee73a7e20bDaniel Dunbar    if (CurBB->empty() || ReturnBlock->use_empty()) {
9196e18b05ea6b55aa92a1a576f29e9cee73a7e20bDaniel Dunbar      ReturnBlock->replaceAllUsesWith(CurBB);
921c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      delete ReturnBlock;
9396e18b05ea6b55aa92a1a576f29e9cee73a7e20bDaniel Dunbar    } else
941c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      EmitBlock(ReturnBlock);
951c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar    return;
961c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  }
971c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
981c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // Otherwise, if the return block is the target of a single direct
991c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // branch then we can just put the code in that block instead. This
1001c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // cleans up functions which started with a unified return block.
1011c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  if (ReturnBlock->hasOneUse()) {
1021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    llvm::BranchInst *BI =
1031c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      dyn_cast<llvm::BranchInst>(*ReturnBlock->use_begin());
1041c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar    if (BI && BI->isUnconditional() && BI->getSuccessor(0) == ReturnBlock) {
1051c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      // Reset insertion point and delete the branch.
1061c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      Builder.SetInsertPoint(BI->getParent());
1071c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      BI->eraseFromParent();
1081c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      delete ReturnBlock;
1091c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      return;
1101c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar    }
1111c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  }
1121c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
113f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // FIXME: We are at an unreachable point, there is no reason to emit the block
114f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // unless it has uses. However, we still need a place to put the debug
115f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // region.end for now.
1161c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
1171c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  EmitBlock(ReturnBlock);
1181c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar}
1191c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
120af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbarvoid CodeGenFunction::FinishFunction(SourceLocation EndLoc) {
121391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  assert(BreakContinueStack.empty() &&
122391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner         "mismatched push/pop in break/continue stack!");
123bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson  assert(BlockScopes.empty() &&
124bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson         "did not remove all blocks from block scope map!");
125bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson  assert(CleanupEntries.empty() &&
126bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson         "mismatched push/pop in cleanup stack!");
1271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Emit function epilog (to return).
1291c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  EmitReturnBlock();
130f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar
131f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar  // Emit debug descriptor for function end.
132e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson  if (CGDebugInfo *DI = getDebugInfo()) {
133f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar    DI->setLocation(EndLoc);
134f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar    DI->EmitRegionEnd(CurFn, Builder);
135f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar  }
136f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar
13788b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar  EmitFunctionEpilog(*CurFnInfo, ReturnValue);
138cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump  EmitEndEHSpec(CurCodeDecl);
1395ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar
140d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // If someone did an indirect goto, emit the indirect goto block at the end of
141d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // the function.
142d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  if (IndirectBranch) {
143d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    EmitBlock(IndirectBranch->getParent());
144d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    Builder.ClearInsertionPoint();
145d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  }
146d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner
147391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  // Remove the AllocaInsertPt instruction, which is just a convenience for us.
148481769b5dc102b0256b35581e787909ad5edfab5Chris Lattner  llvm::Instruction *Ptr = AllocaInsertPt;
149391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  AllocaInsertPt = 0;
150481769b5dc102b0256b35581e787909ad5edfab5Chris Lattner  Ptr->eraseFromParent();
151d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner
152d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // If someone took the address of a label but never did an indirect goto, we
153d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // made a zero entry PHI node, which is illegal, zap it now.
154d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  if (IndirectBranch) {
155d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    llvm::PHINode *PN = cast<llvm::PHINode>(IndirectBranch->getAddress());
156d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    if (PN->getNumIncomingValues() == 0) {
157d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner      PN->replaceAllUsesWith(llvm::UndefValue::get(PN->getType()));
158d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner      PN->eraseFromParent();
159d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    }
160d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  }
161c8aa5f1f264fb230c38182adab944232bb160c2bChris Lattner}
162c8aa5f1f264fb230c38182adab944232bb160c2bChris Lattner
1630ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlssonvoid CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy,
1647c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar                                    llvm::Function *Fn,
1652284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar                                    const FunctionArgList &Args,
1662284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar                                    SourceLocation StartLoc) {
1670ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlsson  const Decl *D = GD.getDecl();
1680ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlsson
1694cc1a4703363ff940b6273aeef9d96a87edeb04bAnders Carlsson  DidCallStackSave = false;
170b5437d238752dc297e42410e98d38d5250fe0463Chris Lattner  CurCodeDecl = CurFuncDecl = D;
1717c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar  FnRetTy = RetTy;
172bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  CurFn = Fn;
1735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(CurFn->isDeclaration() && "Function already has body?");
174ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner
175a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen  // Pass inline keyword to optimizer if it appears explicitly on any
176a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen  // declaration.
177a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen  if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
178a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen    for (FunctionDecl::redecl_iterator RI = FD->redecls_begin(),
179a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen           RE = FD->redecls_end(); RI != RE; ++RI)
180a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen      if (RI->isInlineSpecified()) {
181a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen        Fn->addFnAttr(llvm::Attribute::InlineHint);
182a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen        break;
183a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen      }
184a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen
18555e874299f2ad827646a4ca9ea38c402aaeb38c9Daniel Dunbar  llvm::BasicBlock *EntryBB = createBasicBlock("entry", CurFn);
1865ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar
1875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Create a marker to make it easy to insert allocas into the entryblock
18855352a2d616cf9fbb621d10faf8b960b4b268bd8Chris Lattner  // later.  Don't create this with the builder, because we don't want it
18955352a2d616cf9fbb621d10faf8b960b4b268bd8Chris Lattner  // folded.
1900032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson  llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::getInt32Ty(VMContext));
191bcdc0f0aff015b6507560f50c71d1b7b6c6a9932Mike Stump  AllocaInsertPt = new llvm::BitCastInst(Undef,
192bcdc0f0aff015b6507560f50c71d1b7b6c6a9932Mike Stump                                         llvm::Type::getInt32Ty(VMContext), "",
19355352a2d616cf9fbb621d10faf8b960b4b268bd8Chris Lattner                                         EntryBB);
194f1466848dce9c4c75d96a6cabdc8db560e26aac8Chris Lattner  if (Builder.isNamePreserving())
195f1466848dce9c4c75d96a6cabdc8db560e26aac8Chris Lattner    AllocaInsertPt->setName("allocapt");
1961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19755e874299f2ad827646a4ca9ea38c402aaeb38c9Daniel Dunbar  ReturnBlock = createBasicBlock("return");
1981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19955352a2d616cf9fbb621d10faf8b960b4b268bd8Chris Lattner  Builder.SetInsertPoint(EntryBB);
2001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
201ce056bcaa1c97b89a4b2de2112c62d060863be2bDouglas Gregor  QualType FnType = getContext().getFunctionType(RetTy, 0, 0, false, 0,
202ce056bcaa1c97b89a4b2de2112c62d060863be2bDouglas Gregor                                                 false, false, 0, 0,
203264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                                 /*FIXME?*/
204264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                                 FunctionType::ExtInfo());
20591cc815ffd13d4a78ae1b5bd617e19dd555de4f4Mike Stump
206af99417156c652a6f04dff643925036dc3241d60Sanjiv Gupta  // Emit subprogram debug descriptor.
207e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson  if (CGDebugInfo *DI = getDebugInfo()) {
2082284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar    DI->setLocation(StartLoc);
2099c6c3a0e3ae09626d2d4b04e4ffa42c3d7cab32bDevang Patel    DI->EmitFunctionStart(GD, FnType, CurFn, Builder);
210af99417156c652a6f04dff643925036dc3241d60Sanjiv Gupta  }
211af99417156c652a6f04dff643925036dc3241d60Sanjiv Gupta
21288b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar  // FIXME: Leaked.
21304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  // CC info is ignored, hopefully?
21404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  CurFnInfo = &CGM.getTypes().getFunctionInfo(FnRetTy, Args,
215264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                              FunctionType::ExtInfo());
216b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman
217b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman  if (RetTy->isVoidType()) {
218b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman    // Void type; nothing to return.
219b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman    ReturnValue = 0;
220b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman  } else if (CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect &&
221b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman             hasAggregateLLVMType(CurFnInfo->getReturnType())) {
222b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman    // Indirect aggregate return; emit returned value directly into sret slot.
223647a1ec397fa13af176d07d9f5d071560a94c7a9Daniel Dunbar    // This reduces code size, and affects correctness in C++.
224b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman    ReturnValue = CurFn->arg_begin();
225b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman  } else {
226647a1ec397fa13af176d07d9f5d071560a94c7a9Daniel Dunbar    ReturnValue = CreateIRTemp(RetTy, "retval");
227b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman  }
228b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman
229cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump  EmitStartEHSpec(CurCodeDecl);
23088b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar  EmitFunctionProlog(*CurFnInfo, CurFn, Args);
2311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2322504941793b549323f9d29c62507cf21d865fadeJohn McCall  if (CXXThisDecl)
2332504941793b549323f9d29c62507cf21d865fadeJohn McCall    CXXThisValue = Builder.CreateLoad(LocalDeclMap[CXXThisDecl], "this");
2342504941793b549323f9d29c62507cf21d865fadeJohn McCall  if (CXXVTTDecl)
2352504941793b549323f9d29c62507cf21d865fadeJohn McCall    CXXVTTValue = Builder.CreateLoad(LocalDeclMap[CXXVTTDecl], "vtt");
2362504941793b549323f9d29c62507cf21d865fadeJohn McCall
237751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson  // If any of the arguments have a variably modified type, make sure to
238751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson  // emit the type size.
239751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson  for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
240751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson       i != e; ++i) {
241751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson    QualType Ty = i->second;
242751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson
243751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson    if (Ty->isVariablyModifiedType())
244751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson      EmitVLASize(Ty);
245751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson  }
2467c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar}
247eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman
2489fc6a7774643a810c8501dae2323e863fefb623eJohn McCallvoid CodeGenFunction::EmitFunctionBody(FunctionArgList &Args) {
2499fc6a7774643a810c8501dae2323e863fefb623eJohn McCall  const FunctionDecl *FD = cast<FunctionDecl>(CurGD.getDecl());
25006a9f3680d22529a2fcf20c52d71cf221d99d910Douglas Gregor  assert(FD->getBody());
25106a9f3680d22529a2fcf20c52d71cf221d99d910Douglas Gregor  EmitStmt(FD->getBody());
252a355e07454463b19829ac92ffd115a097faff0e0John McCall}
253a355e07454463b19829ac92ffd115a097faff0e0John McCall
254c997d4278d329e18891aac9698fb991b2d4622ebAnders Carlssonvoid CodeGenFunction::GenerateCode(GlobalDecl GD, llvm::Function *Fn) {
2550ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlsson  const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
2560ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlsson
257e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson  // Check if we should generate debug info for this function.
2581feade8e520be483293dbf55eb57a51720899589Mike Stump  if (CGM.getDebugInfo() && !FD->hasAttr<NoDebugAttr>())
259e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson    DebugInfo = CGM.getDebugInfo();
2601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2617c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar  FunctionArgList Args;
2621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2636a1e0eb557d47e85185e09bdf8721f53f4bf9c9cMike Stump  CurGD = GD;
2642b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson  if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
2652b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson    if (MD->isInstance()) {
2662b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson      // Create the implicit 'this' decl.
2672b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson      // FIXME: I'm not entirely sure I like using a fake decl just for code
2682b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson      // generation. Maybe we can come up with a better way?
2692504941793b549323f9d29c62507cf21d865fadeJohn McCall      CXXThisDecl = ImplicitParamDecl::Create(getContext(), 0,
2702504941793b549323f9d29c62507cf21d865fadeJohn McCall                                              FD->getLocation(),
2711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                              &getContext().Idents.get("this"),
2722b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson                                              MD->getThisType(getContext()));
2732b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson      Args.push_back(std::make_pair(CXXThisDecl, CXXThisDecl->getType()));
274f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson
275f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson      // Check if we need a VTT parameter as well.
276af4403545a50a60d208e6fcae057308d576a92e0Anders Carlsson      if (CodeGenVTables::needsVTTParameter(GD)) {
277f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson        // FIXME: The comment about using a fake decl above applies here too.
278f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson        QualType T = getContext().getPointerType(getContext().VoidPtrTy);
279f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson        CXXVTTDecl =
2802504941793b549323f9d29c62507cf21d865fadeJohn McCall          ImplicitParamDecl::Create(getContext(), 0, FD->getLocation(),
281f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson                                    &getContext().Idents.get("vtt"), T);
282f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson        Args.push_back(std::make_pair(CXXVTTDecl, CXXVTTDecl->getType()));
283f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson      }
2842b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson    }
2852b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson  }
2861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
287eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman  if (FD->getNumParams()) {
288183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    const FunctionProtoType* FProto = FD->getType()->getAs<FunctionProtoType>();
289eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman    assert(FProto && "Function def must have prototype!");
2907c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar
2917c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar    for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i)
2921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Args.push_back(std::make_pair(FD->getParamDecl(i),
2937c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar                                    FProto->getArgType(i)));
2945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
295af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
296a355e07454463b19829ac92ffd115a097faff0e0John McCall  SourceRange BodyRange;
297a355e07454463b19829ac92ffd115a097faff0e0John McCall  if (Stmt *Body = FD->getBody()) BodyRange = Body->getSourceRange();
2984365bba95be15342575b4f030c6583a770a5da3dAnders Carlsson
299a355e07454463b19829ac92ffd115a097faff0e0John McCall  // Emit the standard function prologue.
300a355e07454463b19829ac92ffd115a097faff0e0John McCall  StartFunction(GD, FD->getResultType(), Fn, Args, BodyRange.getBegin());
3011851a12605bc6f1ea70d11974a315340ebaab6ebAnders Carlsson
302a355e07454463b19829ac92ffd115a097faff0e0John McCall  // Generate the body of the function.
3039fc6a7774643a810c8501dae2323e863fefb623eJohn McCall  if (isa<CXXDestructorDecl>(FD))
3049fc6a7774643a810c8501dae2323e863fefb623eJohn McCall    EmitDestructorBody(Args);
3059fc6a7774643a810c8501dae2323e863fefb623eJohn McCall  else if (isa<CXXConstructorDecl>(FD))
3069fc6a7774643a810c8501dae2323e863fefb623eJohn McCall    EmitConstructorBody(Args);
3079fc6a7774643a810c8501dae2323e863fefb623eJohn McCall  else
3089fc6a7774643a810c8501dae2323e863fefb623eJohn McCall    EmitFunctionBody(Args);
309c33e4ba20304e222692e77f2c6ad26a5d8d32f83Anders Carlsson
310a355e07454463b19829ac92ffd115a097faff0e0John McCall  // Emit the standard function epilogue.
311a355e07454463b19829ac92ffd115a097faff0e0John McCall  FinishFunction(BodyRange.getEnd());
3121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3132b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson  // Destroy the 'this' declaration.
3142b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson  if (CXXThisDecl)
3152b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson    CXXThisDecl->Destroy(getContext());
316f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson
317f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson  // Destroy the VTT declaration.
318f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson  if (CXXVTTDecl)
319f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson    CXXVTTDecl->Destroy(getContext());
3205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
3215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3220946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner/// ContainsLabel - Return true if the statement contains a label in it.  If
3230946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner/// this statement is not executed normally, it not containing a label means
3240946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner/// that we can just remove the code.
3250946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattnerbool CodeGenFunction::ContainsLabel(const Stmt *S, bool IgnoreCaseStmts) {
3260946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // Null statement, not a label!
3270946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  if (S == 0) return false;
3281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3290946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // If this is a label, we have to emit the code, consider something like:
3300946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // if (0) {  ...  foo:  bar(); }  goto foo;
3310946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  if (isa<LabelStmt>(S))
3320946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner    return true;
3331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3340946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // If this is a case/default statement, and we haven't seen a switch, we have
3350946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // to emit the code.
3360946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  if (isa<SwitchCase>(S) && !IgnoreCaseStmts)
3370946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner    return true;
3381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3390946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // If this is a switch statement, we want to ignore cases below it.
3400946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  if (isa<SwitchStmt>(S))
3410946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner    IgnoreCaseStmts = true;
3421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3430946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // Scan subexpressions for verboten labels.
3440946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
3450946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner       I != E; ++I)
3460946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner    if (ContainsLabel(*I, IgnoreCaseStmts))
3470946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner      return true;
3481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3490946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  return false;
3500946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner}
3510946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner
35231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
35331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// ConstantFoldsToSimpleInteger - If the sepcified expression does not fold to
35431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// a constant, or if it does but contains a label, return 0.  If it constant
35531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// folds to 'true' and does not contain a label, return 1, if it constant folds
35631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// to 'false' and does not contain a label, return -1.
35731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattnerint CodeGenFunction::ConstantFoldsToSimpleInteger(const Expr *Cond) {
35836bc14c3a1cf63ee306df5687ac8e85f924f8639Daniel Dunbar  // FIXME: Rename and handle conversion of other evaluatable things
35936bc14c3a1cf63ee306df5687ac8e85f924f8639Daniel Dunbar  // to bool.
36064712f196bffd41fb0552c2643b07a25c3e45082Anders Carlsson  Expr::EvalResult Result;
3611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!Cond->Evaluate(Result, getContext()) || !Result.Val.isInt() ||
36264712f196bffd41fb0552c2643b07a25c3e45082Anders Carlsson      Result.HasSideEffects)
363ef5a66d8171eb95e948107f8ee7707b360aaff25Anders Carlsson    return 0;  // Not foldable, not integer or not fully evaluatable.
3641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
36531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  if (CodeGenFunction::ContainsLabel(Cond))
36631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner    return 0;  // Contains a label.
3671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
36864712f196bffd41fb0552c2643b07a25c3e45082Anders Carlsson  return Result.Val.getInt().getBoolValue() ? 1 : -1;
36931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner}
37031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
37131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
37231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an if
37331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// statement) to the specified blocks.  Based on the condition, this might try
37431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// to simplify the codegen of the conditional based on the branch.
37531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner///
37631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattnervoid CodeGenFunction::EmitBranchOnBoolExpr(const Expr *Cond,
37731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner                                           llvm::BasicBlock *TrueBlock,
37831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner                                           llvm::BasicBlock *FalseBlock) {
37931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  if (const ParenExpr *PE = dyn_cast<ParenExpr>(Cond))
38031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner    return EmitBranchOnBoolExpr(PE->getSubExpr(), TrueBlock, FalseBlock);
3811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
38231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  if (const BinaryOperator *CondBOp = dyn_cast<BinaryOperator>(Cond)) {
38331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner    // Handle X && Y in a condition.
38431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner    if (CondBOp->getOpcode() == BinaryOperator::LAnd) {
38531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // If we have "1 && X", simplify the code.  "0 && X" would have constant
38631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // folded if the case was simple enough.
38731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == 1) {
38831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        // br(1 && X) -> br(X).
38931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
39031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      }
3911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
39231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // If we have "X && 1", simplify the code to use an uncond branch.
39331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // "X && 0" would have been constant folded to 0.
39431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == 1) {
39531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        // br(X && 1) -> br(X).
39631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock);
39731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      }
3981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
39931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // Emit the LHS as a conditional.  If the LHS conditional is false, we
40031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // want to jump to the FalseBlock.
4019615ecb44f549ae9fa2b4db6ff46bc78befbf62cDaniel Dunbar      llvm::BasicBlock *LHSTrue = createBasicBlock("land.lhs.true");
40231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBranchOnBoolExpr(CondBOp->getLHS(), LHSTrue, FalseBlock);
40331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBlock(LHSTrue);
4041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
40508e9e453f40aff95a59bd67db49b8f050765e1f0Anders Carlsson      // Any temporaries created here are conditional.
40672119a887c4af39c90f10b6bdc158389071f42eaAnders Carlsson      BeginConditionalBranch();
40731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
40872119a887c4af39c90f10b6bdc158389071f42eaAnders Carlsson      EndConditionalBranch();
40908e9e453f40aff95a59bd67db49b8f050765e1f0Anders Carlsson
41031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      return;
41131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner    } else if (CondBOp->getOpcode() == BinaryOperator::LOr) {
41231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // If we have "0 || X", simplify the code.  "1 || X" would have constant
41331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // folded if the case was simple enough.
41431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == -1) {
41531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        // br(0 || X) -> br(X).
41631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
41731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      }
4181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
41931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // If we have "X || 0", simplify the code to use an uncond branch.
42031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // "X || 1" would have been constant folded to 1.
42131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == -1) {
42231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        // br(X || 0) -> br(X).
42331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock);
42431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      }
4251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
42631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // Emit the LHS as a conditional.  If the LHS conditional is true, we
42731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // want to jump to the TrueBlock.
4289615ecb44f549ae9fa2b4db6ff46bc78befbf62cDaniel Dunbar      llvm::BasicBlock *LHSFalse = createBasicBlock("lor.lhs.false");
42931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, LHSFalse);
43031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBlock(LHSFalse);
4311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
43208e9e453f40aff95a59bd67db49b8f050765e1f0Anders Carlsson      // Any temporaries created here are conditional.
43372119a887c4af39c90f10b6bdc158389071f42eaAnders Carlsson      BeginConditionalBranch();
43431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
43572119a887c4af39c90f10b6bdc158389071f42eaAnders Carlsson      EndConditionalBranch();
43608e9e453f40aff95a59bd67db49b8f050765e1f0Anders Carlsson
43731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      return;
43831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner    }
439552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner  }
4401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
441552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner  if (const UnaryOperator *CondUOp = dyn_cast<UnaryOperator>(Cond)) {
442552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner    // br(!x, t, f) -> br(x, f, t)
443552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner    if (CondUOp->getOpcode() == UnaryOperator::LNot)
444552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner      return EmitBranchOnBoolExpr(CondUOp->getSubExpr(), FalseBlock, TrueBlock);
44531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  }
4461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
44709b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar  if (const ConditionalOperator *CondOp = dyn_cast<ConditionalOperator>(Cond)) {
44809b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar    // Handle ?: operator.
44909b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar
45009b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar    // Just ignore GNU ?: extension.
45109b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar    if (CondOp->getLHS()) {
45209b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      // br(c ? x : y, t, f) -> br(c, br(x, t, f), br(y, t, f))
45309b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true");
45409b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false");
45509b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      EmitBranchOnBoolExpr(CondOp->getCond(), LHSBlock, RHSBlock);
45609b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      EmitBlock(LHSBlock);
45709b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      EmitBranchOnBoolExpr(CondOp->getLHS(), TrueBlock, FalseBlock);
45809b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      EmitBlock(RHSBlock);
45909b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      EmitBranchOnBoolExpr(CondOp->getRHS(), TrueBlock, FalseBlock);
46009b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      return;
46109b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar    }
46209b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar  }
46309b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar
46431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  // Emit the code with the fully general case.
46531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  llvm::Value *CondV = EvaluateExprAsBool(Cond);
46631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  Builder.CreateCondBr(CondV, TrueBlock, FalseBlock);
46731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner}
46831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
469488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar/// ErrorUnsupported - Print out an error that codegen doesn't support the
470dc5e8268292046114ffe02e48773572a91a310f1Chris Lattner/// specified stmt yet.
47190df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbarvoid CodeGenFunction::ErrorUnsupported(const Stmt *S, const char *Type,
47290df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar                                       bool OmitOnError) {
47390df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar  CGM.ErrorUnsupported(S, Type, OmitOnError);
474dc5e8268292046114ffe02e48773572a91a310f1Chris Lattner}
475dc5e8268292046114ffe02e48773572a91a310f1Chris Lattner
4761884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlssonvoid
4771884eb0b5c55edda4893ddec45e7dbad79758782Anders CarlssonCodeGenFunction::EmitNullInitialization(llvm::Value *DestPtr, QualType Ty) {
4781884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlsson  // If the type contains a pointer to data member we can't memset it to zero.
4791884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlsson  // Instead, create a null constant and copy it to the destination.
4801884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlsson  if (CGM.getTypes().ContainsPointerToDataMember(Ty)) {
4811884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlsson    llvm::Constant *NullConstant = CGM.EmitNullConstant(Ty);
4821884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlsson
4831884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlsson    llvm::GlobalVariable *NullVariable =
4841884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlsson      new llvm::GlobalVariable(CGM.getModule(), NullConstant->getType(),
4851884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlsson                               /*isConstant=*/true,
4861884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlsson                               llvm::GlobalVariable::PrivateLinkage,
4871884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlsson                               NullConstant, llvm::Twine());
4881884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlsson    EmitAggregateCopy(DestPtr, NullVariable, Ty, /*isVolatile=*/false);
4891884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlsson    return;
4901884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlsson  }
4911884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlsson
4921884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlsson
4930d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson  // Ignore empty classes in C++.
4940d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson  if (getContext().getLangOptions().CPlusPlus) {
4950d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson    if (const RecordType *RT = Ty->getAs<RecordType>()) {
4960d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson      if (cast<CXXRecordDecl>(RT->getDecl())->isEmpty())
4970d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson        return;
4980d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson    }
4990d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson  }
5000d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson
5011884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlsson  // Otherwise, just memset the whole thing to zero.  This is legal
5021884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlsson  // because in LLVM, all default initializers (other than the ones we just
5031884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlsson  // handled above) are guaranteed to have a bit pattern of all zeros.
50436afd38fb749ab34c8341a1dc70366bca904eaa3Chris Lattner  const llvm::Type *BP = llvm::Type::getInt8PtrTy(VMContext);
5053d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson  if (DestPtr->getType() != BP)
5063d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson    DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
5073d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson
5083d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson  // Get size and alignment info for this aggregate.
5093d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson  std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty);
5103d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson
51188207c9ca218486b93ae7df14e9764cd0c2c3383Chris Lattner  // Don't bother emitting a zero-byte memset.
51288207c9ca218486b93ae7df14e9764cd0c2c3383Chris Lattner  if (TypeInfo.first == 0)
51388207c9ca218486b93ae7df14e9764cd0c2c3383Chris Lattner    return;
5141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5153d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson  // FIXME: Handle variable sized types.
5161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  const llvm::Type *IntPtr = llvm::IntegerType::get(VMContext,
5170032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson                                                    LLVMPointerWidth);
5183d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson
5193ecd785aff34381f3704d9cb28fe3ef85af759deMon P Wang  Builder.CreateCall5(CGM.getMemSetFn(BP, IntPtr), DestPtr,
5200032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson                 llvm::Constant::getNullValue(llvm::Type::getInt8Ty(VMContext)),
5213d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson                      // TypeInfo.first describes size in bits.
5224a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson                      llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
5231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                      llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
5243ecd785aff34381f3704d9cb28fe3ef85af759deMon P Wang                                             TypeInfo.second/8),
5253ecd785aff34381f3704d9cb28fe3ef85af759deMon P Wang                      llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext),
5263ecd785aff34381f3704d9cb28fe3ef85af759deMon P Wang                                             0));
5273d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson}
5283d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson
529d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattnerllvm::BlockAddress *CodeGenFunction::GetAddrOfLabel(const LabelStmt *L) {
530d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // Make sure that there is a block for the indirect goto.
531d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  if (IndirectBranch == 0)
532d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    GetIndirectGotoBlock();
5333d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner
534d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  llvm::BasicBlock *BB = getBasicBlockForLabel(L);
5353d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner
536d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // Make sure the indirect branch includes all of the address-taken blocks.
537d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  IndirectBranch->addDestination(BB);
538d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  return llvm::BlockAddress::get(CurFn, BB);
5393d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner}
5401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5413d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattnerllvm::BasicBlock *CodeGenFunction::GetIndirectGotoBlock() {
542d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // If we already made the indirect branch for indirect goto, return its block.
543d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  if (IndirectBranch) return IndirectBranch->getParent();
5443d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner
545d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  CGBuilderTy TmpBuilder(createBasicBlock("indirectgoto"));
5463d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner
547d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
54885e74acfcfb0c835a2e6c1adab314e997917039aChris Lattner
5493d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner  // Create the PHI node that indirect gotos will add entries to.
550d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  llvm::Value *DestVal = TmpBuilder.CreatePHI(Int8PtrTy, "indirect.goto.dest");
5513d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner
552d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // Create the indirect branch instruction.
553d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  IndirectBranch = TmpBuilder.CreateIndirectBr(DestVal);
554d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  return IndirectBranch->getParent();
5550ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar}
556ddf7cac45d85b73127adbbd91a2b28fc7291c57eAnders Carlsson
557d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbarllvm::Value *CodeGenFunction::GetVLASize(const VariableArrayType *VAT) {
558bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman  llvm::Value *&SizeEntry = VLASizeMap[VAT->getSizeExpr()];
5591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
560f666b7780d04186521adcaedb0e15dfa4d5e6933Anders Carlsson  assert(SizeEntry && "Did not emit size for type");
561f666b7780d04186521adcaedb0e15dfa4d5e6933Anders Carlsson  return SizeEntry;
562f666b7780d04186521adcaedb0e15dfa4d5e6933Anders Carlsson}
563dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlsson
564d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbarllvm::Value *CodeGenFunction::EmitVLASize(QualType Ty) {
56560d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson  assert(Ty->isVariablyModifiedType() &&
56660d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson         "Must pass variably modified type to EmitVLASizes!");
5671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
568d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar  EnsureInsertPoint();
5691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
57060d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson  if (const VariableArrayType *VAT = getContext().getAsVariableArrayType(Ty)) {
571bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman    llvm::Value *&SizeEntry = VLASizeMap[VAT->getSizeExpr()];
5721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
573fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson    if (!SizeEntry) {
57496f214776c0f69069fee4d67557c8c7f416009a8Anders Carlsson      const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
5751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
576ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner      // Get the element size;
577ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner      QualType ElemTy = VAT->getElementType();
578ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner      llvm::Value *ElemSize;
579fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson      if (ElemTy->isVariableArrayType())
580fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson        ElemSize = EmitVLASize(ElemTy);
581ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner      else
5824a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson        ElemSize = llvm::ConstantInt::get(SizeTy,
583199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck            getContext().getTypeSizeInChars(ElemTy).getQuantity());
5841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
585fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson      llvm::Value *NumElements = EmitScalarExpr(VAT->getSizeExpr());
58696f214776c0f69069fee4d67557c8c7f416009a8Anders Carlsson      NumElements = Builder.CreateIntCast(NumElements, SizeTy, false, "tmp");
5871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
588fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson      SizeEntry = Builder.CreateMul(ElemSize, NumElements);
589fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson    }
5901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
59160d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson    return SizeEntry;
592dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlsson  }
5931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
594ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner  if (const ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
595ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner    EmitVLASize(AT->getElementType());
596ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner    return 0;
5971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
5981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
599ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner  const PointerType *PT = Ty->getAs<PointerType>();
600ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner  assert(PT && "unknown VM type!");
601ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner  EmitVLASize(PT->getPointeeType());
60260d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson  return 0;
603dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlsson}
6044fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman
6054fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedmanllvm::Value* CodeGenFunction::EmitVAListRef(const Expr* E) {
6064fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman  if (CGM.getContext().getBuiltinVaListType()->isArrayType()) {
6074fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman    return EmitScalarExpr(E);
6084fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman  }
6094fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman  return EmitLValue(E).getAddress();
6104fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman}
6116ccc47698d0311ddabf32fa0f6db8e4f09ac96f8Anders Carlsson
6127799621ad7d16f3c6a1aeda024312d63ecd77777Fariborz Jahanianvoid CodeGenFunction::PushCleanupBlock(llvm::BasicBlock *CleanupEntryBlock,
61399533834ba8f3658559f334e68a518ebb6388ceaMike Stump                                       llvm::BasicBlock *CleanupExitBlock,
614d88ea5687968640ada2bc5a10211cbeb68a671ecMike Stump                                       llvm::BasicBlock *PreviousInvokeDest,
61599533834ba8f3658559f334e68a518ebb6388ceaMike Stump                                       bool EHOnly) {
61699533834ba8f3658559f334e68a518ebb6388ceaMike Stump  CleanupEntries.push_back(CleanupEntry(CleanupEntryBlock, CleanupExitBlock,
617d88ea5687968640ada2bc5a10211cbeb68a671ecMike Stump                                        PreviousInvokeDest, EHOnly));
6186ccc47698d0311ddabf32fa0f6db8e4f09ac96f8Anders Carlsson}
619c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson
6201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid CodeGenFunction::EmitCleanupBlocks(size_t OldCleanupStackSize) {
6211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(CleanupEntries.size() >= OldCleanupStackSize &&
622c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson         "Cleanup stack mismatch!");
6231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
624c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson  while (CleanupEntries.size() > OldCleanupStackSize)
625c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson    EmitCleanupBlock();
626c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson}
627c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson
6281eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpCodeGenFunction::CleanupBlockInfo CodeGenFunction::PopCleanupBlock() {
629bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  CleanupEntry &CE = CleanupEntries.back();
6301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6317799621ad7d16f3c6a1aeda024312d63ecd77777Fariborz Jahanian  llvm::BasicBlock *CleanupEntryBlock = CE.CleanupEntryBlock;
6321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
633bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  std::vector<llvm::BasicBlock *> Blocks;
634bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  std::swap(Blocks, CE.Blocks);
6351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
636bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  std::vector<llvm::BranchInst *> BranchFixups;
637bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  std::swap(BranchFixups, CE.BranchFixups);
6381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
63999533834ba8f3658559f334e68a518ebb6388ceaMike Stump  bool EHOnly = CE.EHOnly;
64099533834ba8f3658559f334e68a518ebb6388ceaMike Stump
641d88ea5687968640ada2bc5a10211cbeb68a671ecMike Stump  setInvokeDest(CE.PreviousInvokeDest);
642d88ea5687968640ada2bc5a10211cbeb68a671ecMike Stump
643bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  CleanupEntries.pop_back();
644bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson
645ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson  // Check if any branch fixups pointed to the scope we just popped. If so,
646ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson  // we can remove them.
647ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson  for (size_t i = 0, e = BranchFixups.size(); i != e; ++i) {
648ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson    llvm::BasicBlock *Dest = BranchFixups[i]->getSuccessor(0);
649ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson    BlockScopeMap::iterator I = BlockScopes.find(Dest);
6501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
651ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson    if (I == BlockScopes.end())
652ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson      continue;
6531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
654ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson    assert(I->second <= CleanupEntries.size() && "Invalid branch fixup!");
6551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
656ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson    if (I->second == CleanupEntries.size()) {
657ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson      // We don't need to do this branch fixup.
658ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson      BranchFixups[i] = BranchFixups.back();
659ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson      BranchFixups.pop_back();
660ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson      i--;
661ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson      e--;
662ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson      continue;
6631093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson    }
6641093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson  }
6651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6667799621ad7d16f3c6a1aeda024312d63ecd77777Fariborz Jahanian  llvm::BasicBlock *SwitchBlock = CE.CleanupExitBlock;
667bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  llvm::BasicBlock *EndBlock = 0;
6681093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson  if (!BranchFixups.empty()) {
6697799621ad7d16f3c6a1aeda024312d63ecd77777Fariborz Jahanian    if (!SwitchBlock)
6707799621ad7d16f3c6a1aeda024312d63ecd77777Fariborz Jahanian      SwitchBlock = createBasicBlock("cleanup.switch");
671bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson    EndBlock = createBasicBlock("cleanup.end");
6721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
673bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson    llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
6741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
675bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson    Builder.SetInsertPoint(SwitchBlock);
676bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson
67799533834ba8f3658559f334e68a518ebb6388ceaMike Stump    llvm::Value *DestCodePtr
67899533834ba8f3658559f334e68a518ebb6388ceaMike Stump      = CreateTempAlloca(llvm::Type::getInt32Ty(VMContext),
67999533834ba8f3658559f334e68a518ebb6388ceaMike Stump                         "cleanup.dst");
6801093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson    llvm::Value *DestCode = Builder.CreateLoad(DestCodePtr, "tmp");
6811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6821093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson    // Create a switch instruction to determine where to jump next.
6831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    llvm::SwitchInst *SI = Builder.CreateSwitch(DestCode, EndBlock,
6841093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson                                                BranchFixups.size());
685bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson
68646831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson    // Restore the current basic block (if any)
6870ae7b2b2f964bd6145d65ef52dc2a28025b2bd06Anders Carlsson    if (CurBB) {
68846831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson      Builder.SetInsertPoint(CurBB);
6891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6900ae7b2b2f964bd6145d65ef52dc2a28025b2bd06Anders Carlsson      // If we had a current basic block, we also need to emit an instruction
6910ae7b2b2f964bd6145d65ef52dc2a28025b2bd06Anders Carlsson      // to initialize the cleanup destination.
6920032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson      Builder.CreateStore(llvm::Constant::getNullValue(llvm::Type::getInt32Ty(VMContext)),
6930ae7b2b2f964bd6145d65ef52dc2a28025b2bd06Anders Carlsson                          DestCodePtr);
6940ae7b2b2f964bd6145d65ef52dc2a28025b2bd06Anders Carlsson    } else
69546831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson      Builder.ClearInsertionPoint();
696bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson
6971093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson    for (size_t i = 0, e = BranchFixups.size(); i != e; ++i) {
6981093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson      llvm::BranchInst *BI = BranchFixups[i];
6991093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson      llvm::BasicBlock *Dest = BI->getSuccessor(0);
7001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7011093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson      // Fixup the branch instruction to point to the cleanup block.
7027799621ad7d16f3c6a1aeda024312d63ecd77777Fariborz Jahanian      BI->setSuccessor(0, CleanupEntryBlock);
7031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7041093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson      if (CleanupEntries.empty()) {
705cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        llvm::ConstantInt *ID;
7061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
707cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        // Check if we already have a destination for this block.
708cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        if (Dest == SI->getDefaultDest())
7090032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson          ID = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 0);
710cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        else {
711cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson          ID = SI->findCaseDest(Dest);
712cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson          if (!ID) {
713cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson            // No code found, get a new unique one by using the number of
714cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson            // switch successors.
7151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump            ID = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
716cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson                                        SI->getNumSuccessors());
717cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson            SI->addCase(ID, Dest);
718cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson          }
719cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        }
7201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
721cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        // Store the jump destination before the branch instruction.
722cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        new llvm::StoreInst(ID, DestCodePtr, BI);
7231093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson      } else {
7241093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson        // We need to jump through another cleanup block. Create a pad block
72599533834ba8f3658559f334e68a518ebb6388ceaMike Stump        // with a branch instruction that jumps to the final destination and add
72699533834ba8f3658559f334e68a518ebb6388ceaMike Stump        // it as a branch fixup to the current cleanup scope.
7271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7281093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson        // Create the pad block.
7291093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson        llvm::BasicBlock *CleanupPad = createBasicBlock("cleanup.pad", CurFn);
730cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson
731cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        // Create a unique case ID.
73299533834ba8f3658559f334e68a518ebb6388ceaMike Stump        llvm::ConstantInt *ID
73399533834ba8f3658559f334e68a518ebb6388ceaMike Stump          = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
73499533834ba8f3658559f334e68a518ebb6388ceaMike Stump                                   SI->getNumSuccessors());
735cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson
736cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        // Store the jump destination before the branch instruction.
737cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        new llvm::StoreInst(ID, DestCodePtr, BI);
738cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson
7391093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson        // Add it as the destination.
740cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        SI->addCase(ID, CleanupPad);
7411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7421093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson        // Create the branch to the final destination.
7431093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson        llvm::BranchInst *BI = llvm::BranchInst::Create(Dest);
7441093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson        CleanupPad->getInstList().push_back(BI);
7451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7461093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson        // And add it as a branch fixup.
7471093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson        CleanupEntries.back().BranchFixups.push_back(BI);
7481093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson      }
7491093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson    }
7501093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson  }
7511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
752bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson  // Remove all blocks from the block scope map.
753bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson  for (size_t i = 0, e = Blocks.size(); i != e; ++i) {
754bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson    assert(BlockScopes.count(Blocks[i]) &&
755bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson           "Did not find block in scope map!");
7561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
757bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson    BlockScopes.erase(Blocks[i]);
758bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson  }
7591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
76099533834ba8f3658559f334e68a518ebb6388ceaMike Stump  return CleanupBlockInfo(CleanupEntryBlock, SwitchBlock, EndBlock, EHOnly);
761d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson}
762d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson
7631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid CodeGenFunction::EmitCleanupBlock() {
764bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  CleanupBlockInfo Info = PopCleanupBlock();
7651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
76699533834ba8f3658559f334e68a518ebb6388ceaMike Stump  if (Info.EHOnly) {
76799533834ba8f3658559f334e68a518ebb6388ceaMike Stump    // FIXME: Add this to the exceptional edge
76899533834ba8f3658559f334e68a518ebb6388ceaMike Stump    if (Info.CleanupBlock->getNumUses() == 0)
76999533834ba8f3658559f334e68a518ebb6388ceaMike Stump      delete Info.CleanupBlock;
77099533834ba8f3658559f334e68a518ebb6388ceaMike Stump    return;
77199533834ba8f3658559f334e68a518ebb6388ceaMike Stump  }
77299533834ba8f3658559f334e68a518ebb6388ceaMike Stump
773cd9199eb3dd1a665ea642f3918020a53e5ecb673Devang Patel  //  Scrub debug location info.
774cd9199eb3dd1a665ea642f3918020a53e5ecb673Devang Patel  for (llvm::BasicBlock::iterator LBI = Info.CleanupBlock->begin(),
775cd9199eb3dd1a665ea642f3918020a53e5ecb673Devang Patel         LBE = Info.CleanupBlock->end(); LBI != LBE; ++LBI)
776cd9199eb3dd1a665ea642f3918020a53e5ecb673Devang Patel    Builder.SetInstDebugLocation(LBI);
777cd9199eb3dd1a665ea642f3918020a53e5ecb673Devang Patel
778eb6437a9b23b3d98a4590ba5557111e14eba2edfAnders Carlsson  llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
7791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (CurBB && !CurBB->getTerminator() &&
780eb6437a9b23b3d98a4590ba5557111e14eba2edfAnders Carlsson      Info.CleanupBlock->getNumUses() == 0) {
781eb6437a9b23b3d98a4590ba5557111e14eba2edfAnders Carlsson    CurBB->getInstList().splice(CurBB->end(), Info.CleanupBlock->getInstList());
782eb6437a9b23b3d98a4590ba5557111e14eba2edfAnders Carlsson    delete Info.CleanupBlock;
7831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  } else
784eb6437a9b23b3d98a4590ba5557111e14eba2edfAnders Carlsson    EmitBlock(Info.CleanupBlock);
7851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
786bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  if (Info.SwitchBlock)
787bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson    EmitBlock(Info.SwitchBlock);
788bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  if (Info.EndBlock)
789bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson    EmitBlock(Info.EndBlock);
790d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson}
791d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson
7921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid CodeGenFunction::AddBranchFixup(llvm::BranchInst *BI) {
7931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(!CleanupEntries.empty() &&
79487eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson         "Trying to add branch fixup without cleanup block!");
7951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
796f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // FIXME: We could be more clever here and check if there's already a branch
797f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // fixup for this destination and recycle it.
79887eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  CleanupEntries.back().BranchFixups.push_back(BI);
79987eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson}
80087eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson
8011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid CodeGenFunction::EmitBranchThroughCleanup(llvm::BasicBlock *Dest) {
80246831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson  if (!HaveInsertPoint())
80346831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson    return;
8041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
80587eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  llvm::BranchInst* BI = Builder.CreateBr(Dest);
8061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
80746831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson  Builder.ClearInsertionPoint();
8081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
80987eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  // The stack is empty, no need to do any cleanup.
81087eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  if (CleanupEntries.empty())
81187eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson    return;
8121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
81387eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  if (!Dest->getParent()) {
81487eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson    // We are trying to branch to a block that hasn't been inserted yet.
81587eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson    AddBranchFixup(BI);
81687eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson    return;
81787eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  }
8181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
81987eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  BlockScopeMap::iterator I = BlockScopes.find(Dest);
82087eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  if (I == BlockScopes.end()) {
82187eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson    // We are trying to jump to a block that is outside of any cleanup scope.
82287eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson    AddBranchFixup(BI);
82387eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson    return;
82487eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  }
8251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
82687eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  assert(I->second < CleanupEntries.size() &&
82787eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson         "Trying to branch into cleanup region");
8281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
82987eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  if (I->second == CleanupEntries.size() - 1) {
83087eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson    // We have a branch to a block in the same scope.
83187eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson    return;
83287eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  }
8331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
83487eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  AddBranchFixup(BI);
83587eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson}
836