CodeGenFunction.cpp revision 04a67a6aa3dfdc92d57f7f8d93ba397348c868a4
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),
33f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson    CXXThisDecl(0), CXXVTTDecl(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;
404111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner}
415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerASTContext &CodeGenFunction::getContext() const {
435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return CGM.getContext();
445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerllvm::BasicBlock *CodeGenFunction::getBasicBlockForLabel(const LabelStmt *S) {
485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::BasicBlock *&BB = LabelMap[S];
495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (BB) return BB;
501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Create, but don't insert, the new block.
5255e874299f2ad827646a4ca9ea38c402aaeb38c9Daniel Dunbar  return BB = createBasicBlock(S->getName());
535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
550096acf421c4609ce7f43e8b05f8c5ca866d4611Daniel Dunbarllvm::Value *CodeGenFunction::GetAddrOfLocalVar(const VarDecl *VD) {
560096acf421c4609ce7f43e8b05f8c5ca866d4611Daniel Dunbar  llvm::Value *Res = LocalDeclMap[VD];
570096acf421c4609ce7f43e8b05f8c5ca866d4611Daniel Dunbar  assert(Res && "Invalid argument to GetAddrOfLocalVar(), no decl!");
580096acf421c4609ce7f43e8b05f8c5ca866d4611Daniel Dunbar  return Res;
59813733577d33ec56479667b49e1bff42dc6bba90Lauro Ramos Venancio}
605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
610096acf421c4609ce7f43e8b05f8c5ca866d4611Daniel Dunbarllvm::Constant *
620096acf421c4609ce7f43e8b05f8c5ca866d4611Daniel DunbarCodeGenFunction::GetAddrOfStaticLocalVar(const VarDecl *BVD) {
630096acf421c4609ce7f43e8b05f8c5ca866d4611Daniel Dunbar  return cast<llvm::Constant>(GetAddrOfLocalVar(BVD));
64dde0a94120915fa925d1ffcdb997c7b44dc9fa21Anders Carlsson}
65dde0a94120915fa925d1ffcdb997c7b44dc9fa21Anders Carlsson
668b1a343b6b360d63d5dc8a6beb841ce4414c1e00Daniel Dunbarconst llvm::Type *CodeGenFunction::ConvertTypeForMem(QualType T) {
678b1a343b6b360d63d5dc8a6beb841ce4414c1e00Daniel Dunbar  return CGM.getTypes().ConvertTypeForMem(T);
688b1a343b6b360d63d5dc8a6beb841ce4414c1e00Daniel Dunbar}
698b1a343b6b360d63d5dc8a6beb841ce4414c1e00Daniel Dunbar
705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerconst llvm::Type *CodeGenFunction::ConvertType(QualType T) {
715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return CGM.getTypes().ConvertType(T);
725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
744111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattnerbool CodeGenFunction::hasAggregateLLVMType(QualType T) {
75e9d34dc7afe06c9adaacad7a678a0cbbf749ea75Anders Carlsson  return T->isRecordType() || T->isArrayType() || T->isAnyComplexType() ||
76e9d34dc7afe06c9adaacad7a678a0cbbf749ea75Anders Carlsson    T->isMemberFunctionPointerType();
774111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner}
78391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner
791c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbarvoid CodeGenFunction::EmitReturnBlock() {
801c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // For cleanliness, we try to avoid emitting the return block for
811c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // simple cases.
821c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
831c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
841c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  if (CurBB) {
851c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar    assert(!CurBB->getTerminator() && "Unexpected terminated block.");
861c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
8796e18b05ea6b55aa92a1a576f29e9cee73a7e20bDaniel Dunbar    // We have a valid insert point, reuse it if it is empty or there are no
8896e18b05ea6b55aa92a1a576f29e9cee73a7e20bDaniel Dunbar    // explicit jumps to the return block.
8996e18b05ea6b55aa92a1a576f29e9cee73a7e20bDaniel Dunbar    if (CurBB->empty() || ReturnBlock->use_empty()) {
9096e18b05ea6b55aa92a1a576f29e9cee73a7e20bDaniel Dunbar      ReturnBlock->replaceAllUsesWith(CurBB);
911c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      delete ReturnBlock;
9296e18b05ea6b55aa92a1a576f29e9cee73a7e20bDaniel Dunbar    } else
931c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      EmitBlock(ReturnBlock);
941c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar    return;
951c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  }
961c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
971c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // Otherwise, if the return block is the target of a single direct
981c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // branch then we can just put the code in that block instead. This
991c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // cleans up functions which started with a unified return block.
1001c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  if (ReturnBlock->hasOneUse()) {
1011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    llvm::BranchInst *BI =
1021c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      dyn_cast<llvm::BranchInst>(*ReturnBlock->use_begin());
1031c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar    if (BI && BI->isUnconditional() && BI->getSuccessor(0) == ReturnBlock) {
1041c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      // Reset insertion point and delete the branch.
1051c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      Builder.SetInsertPoint(BI->getParent());
1061c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      BI->eraseFromParent();
1071c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      delete ReturnBlock;
1081c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      return;
1091c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar    }
1101c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  }
1111c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
112f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // FIXME: We are at an unreachable point, there is no reason to emit the block
113f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // unless it has uses. However, we still need a place to put the debug
114f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // region.end for now.
1151c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
1161c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  EmitBlock(ReturnBlock);
1171c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar}
1181c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
119af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbarvoid CodeGenFunction::FinishFunction(SourceLocation EndLoc) {
120391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  assert(BreakContinueStack.empty() &&
121391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner         "mismatched push/pop in break/continue stack!");
122bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson  assert(BlockScopes.empty() &&
123bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson         "did not remove all blocks from block scope map!");
124bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson  assert(CleanupEntries.empty() &&
125bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson         "mismatched push/pop in cleanup stack!");
1261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Emit function epilog (to return).
1281c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  EmitReturnBlock();
129f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar
130f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar  // Emit debug descriptor for function end.
131e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson  if (CGDebugInfo *DI = getDebugInfo()) {
132f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar    DI->setLocation(EndLoc);
133f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar    DI->EmitRegionEnd(CurFn, Builder);
134f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar  }
135f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar
13688b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar  EmitFunctionEpilog(*CurFnInfo, ReturnValue);
137cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump  EmitEndEHSpec(CurCodeDecl);
1385ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar
139d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // If someone did an indirect goto, emit the indirect goto block at the end of
140d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // the function.
141d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  if (IndirectBranch) {
142d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    EmitBlock(IndirectBranch->getParent());
143d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    Builder.ClearInsertionPoint();
144d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  }
145d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner
146391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  // Remove the AllocaInsertPt instruction, which is just a convenience for us.
147481769b5dc102b0256b35581e787909ad5edfab5Chris Lattner  llvm::Instruction *Ptr = AllocaInsertPt;
148391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  AllocaInsertPt = 0;
149481769b5dc102b0256b35581e787909ad5edfab5Chris Lattner  Ptr->eraseFromParent();
150d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner
151d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // If someone took the address of a label but never did an indirect goto, we
152d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // made a zero entry PHI node, which is illegal, zap it now.
153d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  if (IndirectBranch) {
154d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    llvm::PHINode *PN = cast<llvm::PHINode>(IndirectBranch->getAddress());
155d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    if (PN->getNumIncomingValues() == 0) {
156d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner      PN->replaceAllUsesWith(llvm::UndefValue::get(PN->getType()));
157d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner      PN->eraseFromParent();
158d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    }
159d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  }
160c8aa5f1f264fb230c38182adab944232bb160c2bChris Lattner}
161c8aa5f1f264fb230c38182adab944232bb160c2bChris Lattner
1620ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlssonvoid CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy,
1637c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar                                    llvm::Function *Fn,
1642284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar                                    const FunctionArgList &Args,
1652284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar                                    SourceLocation StartLoc) {
1660ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlsson  const Decl *D = GD.getDecl();
1670ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlsson
1684cc1a4703363ff940b6273aeef9d96a87edeb04bAnders Carlsson  DidCallStackSave = false;
169b5437d238752dc297e42410e98d38d5250fe0463Chris Lattner  CurCodeDecl = CurFuncDecl = D;
1707c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar  FnRetTy = RetTy;
171bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  CurFn = Fn;
1725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(CurFn->isDeclaration() && "Function already has body?");
173ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner
17455e874299f2ad827646a4ca9ea38c402aaeb38c9Daniel Dunbar  llvm::BasicBlock *EntryBB = createBasicBlock("entry", CurFn);
1755ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar
1765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Create a marker to make it easy to insert allocas into the entryblock
17755352a2d616cf9fbb621d10faf8b960b4b268bd8Chris Lattner  // later.  Don't create this with the builder, because we don't want it
17855352a2d616cf9fbb621d10faf8b960b4b268bd8Chris Lattner  // folded.
1790032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson  llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::getInt32Ty(VMContext));
180bcdc0f0aff015b6507560f50c71d1b7b6c6a9932Mike Stump  AllocaInsertPt = new llvm::BitCastInst(Undef,
181bcdc0f0aff015b6507560f50c71d1b7b6c6a9932Mike Stump                                         llvm::Type::getInt32Ty(VMContext), "",
18255352a2d616cf9fbb621d10faf8b960b4b268bd8Chris Lattner                                         EntryBB);
183f1466848dce9c4c75d96a6cabdc8db560e26aac8Chris Lattner  if (Builder.isNamePreserving())
184f1466848dce9c4c75d96a6cabdc8db560e26aac8Chris Lattner    AllocaInsertPt->setName("allocapt");
1851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18655e874299f2ad827646a4ca9ea38c402aaeb38c9Daniel Dunbar  ReturnBlock = createBasicBlock("return");
1871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18855352a2d616cf9fbb621d10faf8b960b4b268bd8Chris Lattner  Builder.SetInsertPoint(EntryBB);
1891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19091cc815ffd13d4a78ae1b5bd617e19dd555de4f4Mike Stump  QualType FnType = getContext().getFunctionType(RetTy, 0, 0, false, 0);
19191cc815ffd13d4a78ae1b5bd617e19dd555de4f4Mike Stump
192af99417156c652a6f04dff643925036dc3241d60Sanjiv Gupta  // Emit subprogram debug descriptor.
193e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson  if (CGDebugInfo *DI = getDebugInfo()) {
1942284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar    DI->setLocation(StartLoc);
1959c6c3a0e3ae09626d2d4b04e4ffa42c3d7cab32bDevang Patel    DI->EmitFunctionStart(GD, FnType, CurFn, Builder);
196af99417156c652a6f04dff643925036dc3241d60Sanjiv Gupta  }
197af99417156c652a6f04dff643925036dc3241d60Sanjiv Gupta
19888b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar  // FIXME: Leaked.
19904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  // CC info is ignored, hopefully?
20004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  CurFnInfo = &CGM.getTypes().getFunctionInfo(FnRetTy, Args,
20104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall                                              CC_Default, false);
202b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman
203b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman  if (RetTy->isVoidType()) {
204b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman    // Void type; nothing to return.
205b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman    ReturnValue = 0;
206b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman  } else if (CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect &&
207b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman             hasAggregateLLVMType(CurFnInfo->getReturnType())) {
208b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman    // Indirect aggregate return; emit returned value directly into sret slot.
209b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman    // This reduces code size, and is also affects correctness in C++.
210b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman    ReturnValue = CurFn->arg_begin();
211b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman  } else {
212b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman    ReturnValue = CreateTempAlloca(ConvertType(RetTy), "retval");
213b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman  }
214b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman
215cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump  EmitStartEHSpec(CurCodeDecl);
21688b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar  EmitFunctionProlog(*CurFnInfo, CurFn, Args);
2171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
218751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson  // If any of the arguments have a variably modified type, make sure to
219751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson  // emit the type size.
220751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson  for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
221751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson       i != e; ++i) {
222751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson    QualType Ty = i->second;
223751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson
224751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson    if (Ty->isVariablyModifiedType())
225751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson      EmitVLASize(Ty);
226751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson  }
2277c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar}
228eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman
229c997d4278d329e18891aac9698fb991b2d4622ebAnders Carlssonvoid CodeGenFunction::GenerateCode(GlobalDecl GD, llvm::Function *Fn) {
2300ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlsson  const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
2310ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlsson
232e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson  // Check if we should generate debug info for this function.
2331feade8e520be483293dbf55eb57a51720899589Mike Stump  if (CGM.getDebugInfo() && !FD->hasAttr<NoDebugAttr>())
234e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson    DebugInfo = CGM.getDebugInfo();
2351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2367c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar  FunctionArgList Args;
2371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2386a1e0eb557d47e85185e09bdf8721f53f4bf9c9cMike Stump  CurGD = GD;
2396a1e0eb557d47e85185e09bdf8721f53f4bf9c9cMike Stump  OuterTryBlock = 0;
2402b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson  if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
2412b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson    if (MD->isInstance()) {
2422b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson      // Create the implicit 'this' decl.
2432b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson      // FIXME: I'm not entirely sure I like using a fake decl just for code
2442b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson      // generation. Maybe we can come up with a better way?
2452b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson      CXXThisDecl = ImplicitParamDecl::Create(getContext(), 0, SourceLocation(),
2461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                              &getContext().Idents.get("this"),
2472b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson                                              MD->getThisType(getContext()));
2482b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson      Args.push_back(std::make_pair(CXXThisDecl, CXXThisDecl->getType()));
249f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson
250f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson      // Check if we need a VTT parameter as well.
251c997d4278d329e18891aac9698fb991b2d4622ebAnders Carlsson      if (CGVtableInfo::needsVTTParameter(GD)) {
252f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson        // FIXME: The comment about using a fake decl above applies here too.
253f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson        QualType T = getContext().getPointerType(getContext().VoidPtrTy);
254f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson        CXXVTTDecl =
255f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson          ImplicitParamDecl::Create(getContext(), 0, SourceLocation(),
256f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson                                    &getContext().Idents.get("vtt"), T);
257f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson        Args.push_back(std::make_pair(CXXVTTDecl, CXXVTTDecl->getType()));
258f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson      }
2592b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson    }
2602b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson  }
2611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
262eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman  if (FD->getNumParams()) {
263183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    const FunctionProtoType* FProto = FD->getType()->getAs<FunctionProtoType>();
264eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman    assert(FProto && "Function def must have prototype!");
2657c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar
2667c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar    for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i)
2671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Args.push_back(std::make_pair(FD->getParamDecl(i),
2687c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar                                    FProto->getArgType(i)));
2695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
270af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
2716fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis  if (const CompoundStmt *S = FD->getCompoundBody()) {
2720ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlsson    StartFunction(GD, FD->getResultType(), Fn, Args, S->getLBracLoc());
2734365bba95be15342575b4f030c6583a770a5da3dAnders Carlsson
2744365bba95be15342575b4f030c6583a770a5da3dAnders Carlsson    if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) {
275de1d26b9c1d8823b173e4d77015ad88b4da70559Anders Carlsson      EmitCtorPrologue(CD, GD.getCtorType());
2764365bba95be15342575b4f030c6583a770a5da3dAnders Carlsson      EmitStmt(S);
2775e1b91875c275f0ec50d3680afbac150d684fdbaAnders Carlsson
2785e1b91875c275f0ec50d3680afbac150d684fdbaAnders Carlsson      // If any of the member initializers are temporaries bound to references
2795e1b91875c275f0ec50d3680afbac150d684fdbaAnders Carlsson      // make sure to emit their destructors.
2805e1b91875c275f0ec50d3680afbac150d684fdbaAnders Carlsson      EmitCleanupBlocks(0);
2815e1b91875c275f0ec50d3680afbac150d684fdbaAnders Carlsson
2824365bba95be15342575b4f030c6583a770a5da3dAnders Carlsson    } else if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD)) {
2834365bba95be15342575b4f030c6583a770a5da3dAnders Carlsson      llvm::BasicBlock *DtorEpilogue  = createBasicBlock("dtor.epilogue");
2844365bba95be15342575b4f030c6583a770a5da3dAnders Carlsson      PushCleanupBlock(DtorEpilogue);
2854365bba95be15342575b4f030c6583a770a5da3dAnders Carlsson
2864365bba95be15342575b4f030c6583a770a5da3dAnders Carlsson      EmitStmt(S);
287c33e4ba20304e222692e77f2c6ad26a5d8d32f83Anders Carlsson
288c33e4ba20304e222692e77f2c6ad26a5d8d32f83Anders Carlsson      CleanupBlockInfo Info = PopCleanupBlock();
289c33e4ba20304e222692e77f2c6ad26a5d8d32f83Anders Carlsson
290c33e4ba20304e222692e77f2c6ad26a5d8d32f83Anders Carlsson      assert(Info.CleanupBlock == DtorEpilogue && "Block mismatch!");
291c33e4ba20304e222692e77f2c6ad26a5d8d32f83Anders Carlsson      EmitBlock(DtorEpilogue);
292de1d26b9c1d8823b173e4d77015ad88b4da70559Anders Carlsson      EmitDtorEpilogue(DD, GD.getDtorType());
293c33e4ba20304e222692e77f2c6ad26a5d8d32f83Anders Carlsson
294c33e4ba20304e222692e77f2c6ad26a5d8d32f83Anders Carlsson      if (Info.SwitchBlock)
295c33e4ba20304e222692e77f2c6ad26a5d8d32f83Anders Carlsson        EmitBlock(Info.SwitchBlock);
296c33e4ba20304e222692e77f2c6ad26a5d8d32f83Anders Carlsson      if (Info.EndBlock)
297c33e4ba20304e222692e77f2c6ad26a5d8d32f83Anders Carlsson        EmitBlock(Info.EndBlock);
2984365bba95be15342575b4f030c6583a770a5da3dAnders Carlsson    } else {
2994365bba95be15342575b4f030c6583a770a5da3dAnders Carlsson      // Just a regular function, emit its body.
3004365bba95be15342575b4f030c6583a770a5da3dAnders Carlsson      EmitStmt(S);
301c33e4ba20304e222692e77f2c6ad26a5d8d32f83Anders Carlsson    }
3024365bba95be15342575b4f030c6583a770a5da3dAnders Carlsson
303d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl    FinishFunction(S->getRBracLoc());
3044513272f444c59705123ccb6207414ce62c9a568Douglas Gregor  } else if (FD->isImplicit()) {
3054513272f444c59705123ccb6207414ce62c9a568Douglas Gregor    const CXXRecordDecl *ClassDecl =
3064513272f444c59705123ccb6207414ce62c9a568Douglas Gregor      cast<CXXRecordDecl>(FD->getDeclContext());
3074513272f444c59705123ccb6207414ce62c9a568Douglas Gregor    (void) ClassDecl;
308c7ff8e19081c2e974f05f66c4fa9b40750fc655fFariborz Jahanian    if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) {
3094513272f444c59705123ccb6207414ce62c9a568Douglas Gregor      // FIXME: For C++0x, we want to look for implicit *definitions* of
3104513272f444c59705123ccb6207414ce62c9a568Douglas Gregor      // these special member functions, rather than implicit *declarations*.
3119e9199d8649cf3e10c98a69403f05dbb666d8fb1Douglas Gregor      if (CD->isCopyConstructor()) {
3129889652dbc10060cd604861ed2e5bc6719f845b0Fariborz Jahanian        assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
3134513272f444c59705123ccb6207414ce62c9a568Douglas Gregor               "Cannot synthesize a non-implicit copy constructor");
314de1d26b9c1d8823b173e4d77015ad88b4da70559Anders Carlsson        SynthesizeCXXCopyConstructor(CD, GD.getCtorType(), Fn, Args);
3154513272f444c59705123ccb6207414ce62c9a568Douglas Gregor      } else if (CD->isDefaultConstructor()) {
3169889652dbc10060cd604861ed2e5bc6719f845b0Fariborz Jahanian        assert(!ClassDecl->hasUserDeclaredConstructor() &&
3174513272f444c59705123ccb6207414ce62c9a568Douglas Gregor               "Cannot synthesize a non-implicit default constructor.");
318de1d26b9c1d8823b173e4d77015ad88b4da70559Anders Carlsson        SynthesizeDefaultConstructor(CD, GD.getCtorType(), Fn, Args);
3194513272f444c59705123ccb6207414ce62c9a568Douglas Gregor      } else {
3204513272f444c59705123ccb6207414ce62c9a568Douglas Gregor        assert(false && "Implicit constructor cannot be synthesized");
3219889652dbc10060cd604861ed2e5bc6719f845b0Fariborz Jahanian      }
3224513272f444c59705123ccb6207414ce62c9a568Douglas Gregor    } else if (const CXXDestructorDecl *CD = dyn_cast<CXXDestructorDecl>(FD)) {
3234513272f444c59705123ccb6207414ce62c9a568Douglas Gregor      assert(!ClassDecl->hasUserDeclaredDestructor() &&
3244513272f444c59705123ccb6207414ce62c9a568Douglas Gregor             "Cannot synthesize a non-implicit destructor");
3254513272f444c59705123ccb6207414ce62c9a568Douglas Gregor      SynthesizeDefaultDestructor(CD, GD.getDtorType(), Fn, Args);
3264513272f444c59705123ccb6207414ce62c9a568Douglas Gregor    } else if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
3274513272f444c59705123ccb6207414ce62c9a568Douglas Gregor      assert(MD->isCopyAssignment() &&
3284513272f444c59705123ccb6207414ce62c9a568Douglas Gregor             !ClassDecl->hasUserDeclaredCopyAssignment() &&
3294513272f444c59705123ccb6207414ce62c9a568Douglas Gregor             "Cannot synthesize a method that is not an implicit-defined "
3304513272f444c59705123ccb6207414ce62c9a568Douglas Gregor             "copy constructor");
331de1d26b9c1d8823b173e4d77015ad88b4da70559Anders Carlsson      SynthesizeCXXCopyAssignment(MD, Fn, Args);
3324513272f444c59705123ccb6207414ce62c9a568Douglas Gregor    } else {
3334513272f444c59705123ccb6207414ce62c9a568Douglas Gregor      assert(false && "Cannot synthesize unknown implicit function");
3344513272f444c59705123ccb6207414ce62c9a568Douglas Gregor    }
3356a1e0eb557d47e85185e09bdf8721f53f4bf9c9cMike Stump  } else if (const Stmt *S = FD->getBody()) {
3366a1e0eb557d47e85185e09bdf8721f53f4bf9c9cMike Stump    if (const CXXTryStmt *TS = dyn_cast<CXXTryStmt>(S)) {
3376a1e0eb557d47e85185e09bdf8721f53f4bf9c9cMike Stump      OuterTryBlock = TS;
3386a1e0eb557d47e85185e09bdf8721f53f4bf9c9cMike Stump      StartFunction(GD, FD->getResultType(), Fn, Args, TS->getTryLoc());
3396a1e0eb557d47e85185e09bdf8721f53f4bf9c9cMike Stump      EmitStmt(TS);
3406a1e0eb557d47e85185e09bdf8721f53f4bf9c9cMike Stump      FinishFunction(TS->getEndLoc());
3416a1e0eb557d47e85185e09bdf8721f53f4bf9c9cMike Stump    }
3420ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlsson  }
3431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3442b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson  // Destroy the 'this' declaration.
3452b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson  if (CXXThisDecl)
3462b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson    CXXThisDecl->Destroy(getContext());
347f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson
348f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson  // Destroy the VTT declaration.
349f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson  if (CXXVTTDecl)
350f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson    CXXVTTDecl->Destroy(getContext());
3515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
3525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3530946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner/// ContainsLabel - Return true if the statement contains a label in it.  If
3540946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner/// this statement is not executed normally, it not containing a label means
3550946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner/// that we can just remove the code.
3560946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattnerbool CodeGenFunction::ContainsLabel(const Stmt *S, bool IgnoreCaseStmts) {
3570946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // Null statement, not a label!
3580946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  if (S == 0) return false;
3591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3600946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // If this is a label, we have to emit the code, consider something like:
3610946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // if (0) {  ...  foo:  bar(); }  goto foo;
3620946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  if (isa<LabelStmt>(S))
3630946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner    return true;
3641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3650946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // If this is a case/default statement, and we haven't seen a switch, we have
3660946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // to emit the code.
3670946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  if (isa<SwitchCase>(S) && !IgnoreCaseStmts)
3680946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner    return true;
3691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3700946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // If this is a switch statement, we want to ignore cases below it.
3710946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  if (isa<SwitchStmt>(S))
3720946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner    IgnoreCaseStmts = true;
3731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3740946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // Scan subexpressions for verboten labels.
3750946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
3760946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner       I != E; ++I)
3770946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner    if (ContainsLabel(*I, IgnoreCaseStmts))
3780946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner      return true;
3791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3800946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  return false;
3810946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner}
3820946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner
38331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
38431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// ConstantFoldsToSimpleInteger - If the sepcified expression does not fold to
38531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// a constant, or if it does but contains a label, return 0.  If it constant
38631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// folds to 'true' and does not contain a label, return 1, if it constant folds
38731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// to 'false' and does not contain a label, return -1.
38831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattnerint CodeGenFunction::ConstantFoldsToSimpleInteger(const Expr *Cond) {
38936bc14c3a1cf63ee306df5687ac8e85f924f8639Daniel Dunbar  // FIXME: Rename and handle conversion of other evaluatable things
39036bc14c3a1cf63ee306df5687ac8e85f924f8639Daniel Dunbar  // to bool.
39164712f196bffd41fb0552c2643b07a25c3e45082Anders Carlsson  Expr::EvalResult Result;
3921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!Cond->Evaluate(Result, getContext()) || !Result.Val.isInt() ||
39364712f196bffd41fb0552c2643b07a25c3e45082Anders Carlsson      Result.HasSideEffects)
394ef5a66d8171eb95e948107f8ee7707b360aaff25Anders Carlsson    return 0;  // Not foldable, not integer or not fully evaluatable.
3951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
39631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  if (CodeGenFunction::ContainsLabel(Cond))
39731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner    return 0;  // Contains a label.
3981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
39964712f196bffd41fb0552c2643b07a25c3e45082Anders Carlsson  return Result.Val.getInt().getBoolValue() ? 1 : -1;
40031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner}
40131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
40231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
40331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an if
40431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// statement) to the specified blocks.  Based on the condition, this might try
40531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// to simplify the codegen of the conditional based on the branch.
40631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner///
40731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattnervoid CodeGenFunction::EmitBranchOnBoolExpr(const Expr *Cond,
40831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner                                           llvm::BasicBlock *TrueBlock,
40931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner                                           llvm::BasicBlock *FalseBlock) {
41031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  if (const ParenExpr *PE = dyn_cast<ParenExpr>(Cond))
41131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner    return EmitBranchOnBoolExpr(PE->getSubExpr(), TrueBlock, FalseBlock);
4121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
41331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  if (const BinaryOperator *CondBOp = dyn_cast<BinaryOperator>(Cond)) {
41431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner    // Handle X && Y in a condition.
41531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner    if (CondBOp->getOpcode() == BinaryOperator::LAnd) {
41631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // If we have "1 && X", simplify the code.  "0 && X" would have constant
41731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // folded if the case was simple enough.
41831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == 1) {
41931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        // br(1 && X) -> br(X).
42031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
42131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      }
4221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
42331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // If we have "X && 1", simplify the code to use an uncond branch.
42431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // "X && 0" would have been constant folded to 0.
42531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == 1) {
42631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        // br(X && 1) -> br(X).
42731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock);
42831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      }
4291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
43031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // Emit the LHS as a conditional.  If the LHS conditional is false, we
43131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // want to jump to the FalseBlock.
4329615ecb44f549ae9fa2b4db6ff46bc78befbf62cDaniel Dunbar      llvm::BasicBlock *LHSTrue = createBasicBlock("land.lhs.true");
43331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBranchOnBoolExpr(CondBOp->getLHS(), LHSTrue, FalseBlock);
43431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBlock(LHSTrue);
4351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
43608e9e453f40aff95a59bd67db49b8f050765e1f0Anders Carlsson      // Any temporaries created here are conditional.
43772119a887c4af39c90f10b6bdc158389071f42eaAnders Carlsson      BeginConditionalBranch();
43831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
43972119a887c4af39c90f10b6bdc158389071f42eaAnders Carlsson      EndConditionalBranch();
44008e9e453f40aff95a59bd67db49b8f050765e1f0Anders Carlsson
44131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      return;
44231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner    } else if (CondBOp->getOpcode() == BinaryOperator::LOr) {
44331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // If we have "0 || X", simplify the code.  "1 || X" would have constant
44431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // folded if the case was simple enough.
44531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == -1) {
44631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        // br(0 || X) -> br(X).
44731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
44831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      }
4491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
45031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // If we have "X || 0", simplify the code to use an uncond branch.
45131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // "X || 1" would have been constant folded to 1.
45231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == -1) {
45331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        // br(X || 0) -> br(X).
45431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock);
45531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      }
4561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
45731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // Emit the LHS as a conditional.  If the LHS conditional is true, we
45831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // want to jump to the TrueBlock.
4599615ecb44f549ae9fa2b4db6ff46bc78befbf62cDaniel Dunbar      llvm::BasicBlock *LHSFalse = createBasicBlock("lor.lhs.false");
46031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, LHSFalse);
46131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBlock(LHSFalse);
4621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
46308e9e453f40aff95a59bd67db49b8f050765e1f0Anders Carlsson      // Any temporaries created here are conditional.
46472119a887c4af39c90f10b6bdc158389071f42eaAnders Carlsson      BeginConditionalBranch();
46531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
46672119a887c4af39c90f10b6bdc158389071f42eaAnders Carlsson      EndConditionalBranch();
46708e9e453f40aff95a59bd67db49b8f050765e1f0Anders Carlsson
46831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      return;
46931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner    }
470552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner  }
4711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
472552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner  if (const UnaryOperator *CondUOp = dyn_cast<UnaryOperator>(Cond)) {
473552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner    // br(!x, t, f) -> br(x, f, t)
474552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner    if (CondUOp->getOpcode() == UnaryOperator::LNot)
475552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner      return EmitBranchOnBoolExpr(CondUOp->getSubExpr(), FalseBlock, TrueBlock);
47631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  }
4771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
47809b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar  if (const ConditionalOperator *CondOp = dyn_cast<ConditionalOperator>(Cond)) {
47909b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar    // Handle ?: operator.
48009b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar
48109b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar    // Just ignore GNU ?: extension.
48209b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar    if (CondOp->getLHS()) {
48309b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      // br(c ? x : y, t, f) -> br(c, br(x, t, f), br(y, t, f))
48409b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true");
48509b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false");
48609b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      EmitBranchOnBoolExpr(CondOp->getCond(), LHSBlock, RHSBlock);
48709b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      EmitBlock(LHSBlock);
48809b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      EmitBranchOnBoolExpr(CondOp->getLHS(), TrueBlock, FalseBlock);
48909b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      EmitBlock(RHSBlock);
49009b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      EmitBranchOnBoolExpr(CondOp->getRHS(), TrueBlock, FalseBlock);
49109b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      return;
49209b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar    }
49309b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar  }
49409b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar
49531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  // Emit the code with the fully general case.
49631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  llvm::Value *CondV = EvaluateExprAsBool(Cond);
49731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  Builder.CreateCondBr(CondV, TrueBlock, FalseBlock);
49831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner}
49931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
500488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar/// ErrorUnsupported - Print out an error that codegen doesn't support the
501dc5e8268292046114ffe02e48773572a91a310f1Chris Lattner/// specified stmt yet.
50290df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbarvoid CodeGenFunction::ErrorUnsupported(const Stmt *S, const char *Type,
50390df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar                                       bool OmitOnError) {
50490df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar  CGM.ErrorUnsupported(S, Type, OmitOnError);
505dc5e8268292046114ffe02e48773572a91a310f1Chris Lattner}
506dc5e8268292046114ffe02e48773572a91a310f1Chris Lattner
50788207c9ca218486b93ae7df14e9764cd0c2c3383Chris Lattnervoid CodeGenFunction::EmitMemSetToZero(llvm::Value *DestPtr, QualType Ty) {
50836afd38fb749ab34c8341a1dc70366bca904eaa3Chris Lattner  const llvm::Type *BP = llvm::Type::getInt8PtrTy(VMContext);
5093d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson  if (DestPtr->getType() != BP)
5103d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson    DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
5113d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson
5123d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson  // Get size and alignment info for this aggregate.
5133d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson  std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty);
5143d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson
51588207c9ca218486b93ae7df14e9764cd0c2c3383Chris Lattner  // Don't bother emitting a zero-byte memset.
51688207c9ca218486b93ae7df14e9764cd0c2c3383Chris Lattner  if (TypeInfo.first == 0)
51788207c9ca218486b93ae7df14e9764cd0c2c3383Chris Lattner    return;
5181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5193d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson  // FIXME: Handle variable sized types.
5201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  const llvm::Type *IntPtr = llvm::IntegerType::get(VMContext,
5210032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson                                                    LLVMPointerWidth);
5223d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson
5233d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson  Builder.CreateCall4(CGM.getMemSetFn(), DestPtr,
5240032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson                 llvm::Constant::getNullValue(llvm::Type::getInt8Ty(VMContext)),
5253d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson                      // TypeInfo.first describes size in bits.
5264a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson                      llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
5271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                      llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
5283d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson                                             TypeInfo.second/8));
5293d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson}
5303d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson
531d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattnerllvm::BlockAddress *CodeGenFunction::GetAddrOfLabel(const LabelStmt *L) {
532d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // Make sure that there is a block for the indirect goto.
533d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  if (IndirectBranch == 0)
534d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    GetIndirectGotoBlock();
5353d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner
536d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  llvm::BasicBlock *BB = getBasicBlockForLabel(L);
5373d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner
538d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // Make sure the indirect branch includes all of the address-taken blocks.
539d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  IndirectBranch->addDestination(BB);
540d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  return llvm::BlockAddress::get(CurFn, BB);
5413d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner}
5421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5433d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattnerllvm::BasicBlock *CodeGenFunction::GetIndirectGotoBlock() {
544d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // If we already made the indirect branch for indirect goto, return its block.
545d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  if (IndirectBranch) return IndirectBranch->getParent();
5463d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner
547d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  CGBuilderTy TmpBuilder(createBasicBlock("indirectgoto"));
5483d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner
549d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
55085e74acfcfb0c835a2e6c1adab314e997917039aChris Lattner
5513d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner  // Create the PHI node that indirect gotos will add entries to.
552d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  llvm::Value *DestVal = TmpBuilder.CreatePHI(Int8PtrTy, "indirect.goto.dest");
5533d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner
554d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // Create the indirect branch instruction.
555d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  IndirectBranch = TmpBuilder.CreateIndirectBr(DestVal);
556d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  return IndirectBranch->getParent();
5570ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar}
558ddf7cac45d85b73127adbbd91a2b28fc7291c57eAnders Carlsson
559d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbarllvm::Value *CodeGenFunction::GetVLASize(const VariableArrayType *VAT) {
560bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman  llvm::Value *&SizeEntry = VLASizeMap[VAT->getSizeExpr()];
5611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
562f666b7780d04186521adcaedb0e15dfa4d5e6933Anders Carlsson  assert(SizeEntry && "Did not emit size for type");
563f666b7780d04186521adcaedb0e15dfa4d5e6933Anders Carlsson  return SizeEntry;
564f666b7780d04186521adcaedb0e15dfa4d5e6933Anders Carlsson}
565dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlsson
566d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbarllvm::Value *CodeGenFunction::EmitVLASize(QualType Ty) {
56760d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson  assert(Ty->isVariablyModifiedType() &&
56860d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson         "Must pass variably modified type to EmitVLASizes!");
5691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
570d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar  EnsureInsertPoint();
5711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
57260d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson  if (const VariableArrayType *VAT = getContext().getAsVariableArrayType(Ty)) {
573bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman    llvm::Value *&SizeEntry = VLASizeMap[VAT->getSizeExpr()];
5741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
575fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson    if (!SizeEntry) {
57696f214776c0f69069fee4d67557c8c7f416009a8Anders Carlsson      const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
5771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
578ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner      // Get the element size;
579ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner      QualType ElemTy = VAT->getElementType();
580ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner      llvm::Value *ElemSize;
581fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson      if (ElemTy->isVariableArrayType())
582fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson        ElemSize = EmitVLASize(ElemTy);
583ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner      else
5844a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson        ElemSize = llvm::ConstantInt::get(SizeTy,
585199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck            getContext().getTypeSizeInChars(ElemTy).getQuantity());
5861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
587fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson      llvm::Value *NumElements = EmitScalarExpr(VAT->getSizeExpr());
58896f214776c0f69069fee4d67557c8c7f416009a8Anders Carlsson      NumElements = Builder.CreateIntCast(NumElements, SizeTy, false, "tmp");
5891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
590fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson      SizeEntry = Builder.CreateMul(ElemSize, NumElements);
591fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson    }
5921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
59360d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson    return SizeEntry;
594dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlsson  }
5951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
596ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner  if (const ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
597ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner    EmitVLASize(AT->getElementType());
598ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner    return 0;
5991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
6001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
601ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner  const PointerType *PT = Ty->getAs<PointerType>();
602ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner  assert(PT && "unknown VM type!");
603ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner  EmitVLASize(PT->getPointeeType());
60460d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson  return 0;
605dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlsson}
6064fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman
6074fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedmanllvm::Value* CodeGenFunction::EmitVAListRef(const Expr* E) {
6084fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman  if (CGM.getContext().getBuiltinVaListType()->isArrayType()) {
6094fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman    return EmitScalarExpr(E);
6104fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman  }
6114fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman  return EmitLValue(E).getAddress();
6124fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman}
6136ccc47698d0311ddabf32fa0f6db8e4f09ac96f8Anders Carlsson
6147799621ad7d16f3c6a1aeda024312d63ecd77777Fariborz Jahanianvoid CodeGenFunction::PushCleanupBlock(llvm::BasicBlock *CleanupEntryBlock,
61599533834ba8f3658559f334e68a518ebb6388ceaMike Stump                                       llvm::BasicBlock *CleanupExitBlock,
616d88ea5687968640ada2bc5a10211cbeb68a671ecMike Stump                                       llvm::BasicBlock *PreviousInvokeDest,
61799533834ba8f3658559f334e68a518ebb6388ceaMike Stump                                       bool EHOnly) {
61899533834ba8f3658559f334e68a518ebb6388ceaMike Stump  CleanupEntries.push_back(CleanupEntry(CleanupEntryBlock, CleanupExitBlock,
619d88ea5687968640ada2bc5a10211cbeb68a671ecMike Stump                                        PreviousInvokeDest, EHOnly));
6206ccc47698d0311ddabf32fa0f6db8e4f09ac96f8Anders Carlsson}
621c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson
6221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid CodeGenFunction::EmitCleanupBlocks(size_t OldCleanupStackSize) {
6231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(CleanupEntries.size() >= OldCleanupStackSize &&
624c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson         "Cleanup stack mismatch!");
6251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
626c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson  while (CleanupEntries.size() > OldCleanupStackSize)
627c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson    EmitCleanupBlock();
628c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson}
629c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson
6301eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpCodeGenFunction::CleanupBlockInfo CodeGenFunction::PopCleanupBlock() {
631bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  CleanupEntry &CE = CleanupEntries.back();
6321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6337799621ad7d16f3c6a1aeda024312d63ecd77777Fariborz Jahanian  llvm::BasicBlock *CleanupEntryBlock = CE.CleanupEntryBlock;
6341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
635bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  std::vector<llvm::BasicBlock *> Blocks;
636bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  std::swap(Blocks, CE.Blocks);
6371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
638bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  std::vector<llvm::BranchInst *> BranchFixups;
639bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  std::swap(BranchFixups, CE.BranchFixups);
6401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
64199533834ba8f3658559f334e68a518ebb6388ceaMike Stump  bool EHOnly = CE.EHOnly;
64299533834ba8f3658559f334e68a518ebb6388ceaMike Stump
643d88ea5687968640ada2bc5a10211cbeb68a671ecMike Stump  setInvokeDest(CE.PreviousInvokeDest);
644d88ea5687968640ada2bc5a10211cbeb68a671ecMike Stump
645bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  CleanupEntries.pop_back();
646bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson
647ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson  // Check if any branch fixups pointed to the scope we just popped. If so,
648ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson  // we can remove them.
649ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson  for (size_t i = 0, e = BranchFixups.size(); i != e; ++i) {
650ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson    llvm::BasicBlock *Dest = BranchFixups[i]->getSuccessor(0);
651ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson    BlockScopeMap::iterator I = BlockScopes.find(Dest);
6521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
653ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson    if (I == BlockScopes.end())
654ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson      continue;
6551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
656ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson    assert(I->second <= CleanupEntries.size() && "Invalid branch fixup!");
6571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
658ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson    if (I->second == CleanupEntries.size()) {
659ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson      // We don't need to do this branch fixup.
660ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson      BranchFixups[i] = BranchFixups.back();
661ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson      BranchFixups.pop_back();
662ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson      i--;
663ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson      e--;
664ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson      continue;
6651093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson    }
6661093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson  }
6671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6687799621ad7d16f3c6a1aeda024312d63ecd77777Fariborz Jahanian  llvm::BasicBlock *SwitchBlock = CE.CleanupExitBlock;
669bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  llvm::BasicBlock *EndBlock = 0;
6701093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson  if (!BranchFixups.empty()) {
6717799621ad7d16f3c6a1aeda024312d63ecd77777Fariborz Jahanian    if (!SwitchBlock)
6727799621ad7d16f3c6a1aeda024312d63ecd77777Fariborz Jahanian      SwitchBlock = createBasicBlock("cleanup.switch");
673bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson    EndBlock = createBasicBlock("cleanup.end");
6741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
675bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson    llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
6761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
677bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson    Builder.SetInsertPoint(SwitchBlock);
678bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson
67999533834ba8f3658559f334e68a518ebb6388ceaMike Stump    llvm::Value *DestCodePtr
68099533834ba8f3658559f334e68a518ebb6388ceaMike Stump      = CreateTempAlloca(llvm::Type::getInt32Ty(VMContext),
68199533834ba8f3658559f334e68a518ebb6388ceaMike Stump                         "cleanup.dst");
6821093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson    llvm::Value *DestCode = Builder.CreateLoad(DestCodePtr, "tmp");
6831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6841093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson    // Create a switch instruction to determine where to jump next.
6851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    llvm::SwitchInst *SI = Builder.CreateSwitch(DestCode, EndBlock,
6861093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson                                                BranchFixups.size());
687bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson
68846831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson    // Restore the current basic block (if any)
6890ae7b2b2f964bd6145d65ef52dc2a28025b2bd06Anders Carlsson    if (CurBB) {
69046831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson      Builder.SetInsertPoint(CurBB);
6911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6920ae7b2b2f964bd6145d65ef52dc2a28025b2bd06Anders Carlsson      // If we had a current basic block, we also need to emit an instruction
6930ae7b2b2f964bd6145d65ef52dc2a28025b2bd06Anders Carlsson      // to initialize the cleanup destination.
6940032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson      Builder.CreateStore(llvm::Constant::getNullValue(llvm::Type::getInt32Ty(VMContext)),
6950ae7b2b2f964bd6145d65ef52dc2a28025b2bd06Anders Carlsson                          DestCodePtr);
6960ae7b2b2f964bd6145d65ef52dc2a28025b2bd06Anders Carlsson    } else
69746831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson      Builder.ClearInsertionPoint();
698bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson
6991093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson    for (size_t i = 0, e = BranchFixups.size(); i != e; ++i) {
7001093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson      llvm::BranchInst *BI = BranchFixups[i];
7011093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson      llvm::BasicBlock *Dest = BI->getSuccessor(0);
7021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7031093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson      // Fixup the branch instruction to point to the cleanup block.
7047799621ad7d16f3c6a1aeda024312d63ecd77777Fariborz Jahanian      BI->setSuccessor(0, CleanupEntryBlock);
7051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7061093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson      if (CleanupEntries.empty()) {
707cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        llvm::ConstantInt *ID;
7081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
709cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        // Check if we already have a destination for this block.
710cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        if (Dest == SI->getDefaultDest())
7110032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson          ID = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 0);
712cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        else {
713cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson          ID = SI->findCaseDest(Dest);
714cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson          if (!ID) {
715cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson            // No code found, get a new unique one by using the number of
716cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson            // switch successors.
7171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump            ID = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
718cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson                                        SI->getNumSuccessors());
719cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson            SI->addCase(ID, Dest);
720cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson          }
721cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        }
7221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
723cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        // Store the jump destination before the branch instruction.
724cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        new llvm::StoreInst(ID, DestCodePtr, BI);
7251093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson      } else {
7261093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson        // We need to jump through another cleanup block. Create a pad block
72799533834ba8f3658559f334e68a518ebb6388ceaMike Stump        // with a branch instruction that jumps to the final destination and add
72899533834ba8f3658559f334e68a518ebb6388ceaMike Stump        // it as a branch fixup to the current cleanup scope.
7291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7301093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson        // Create the pad block.
7311093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson        llvm::BasicBlock *CleanupPad = createBasicBlock("cleanup.pad", CurFn);
732cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson
733cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        // Create a unique case ID.
73499533834ba8f3658559f334e68a518ebb6388ceaMike Stump        llvm::ConstantInt *ID
73599533834ba8f3658559f334e68a518ebb6388ceaMike Stump          = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
73699533834ba8f3658559f334e68a518ebb6388ceaMike Stump                                   SI->getNumSuccessors());
737cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson
738cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        // Store the jump destination before the branch instruction.
739cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        new llvm::StoreInst(ID, DestCodePtr, BI);
740cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson
7411093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson        // Add it as the destination.
742cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        SI->addCase(ID, CleanupPad);
7431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7441093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson        // Create the branch to the final destination.
7451093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson        llvm::BranchInst *BI = llvm::BranchInst::Create(Dest);
7461093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson        CleanupPad->getInstList().push_back(BI);
7471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7481093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson        // And add it as a branch fixup.
7491093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson        CleanupEntries.back().BranchFixups.push_back(BI);
7501093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson      }
7511093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson    }
7521093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson  }
7531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
754bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson  // Remove all blocks from the block scope map.
755bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson  for (size_t i = 0, e = Blocks.size(); i != e; ++i) {
756bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson    assert(BlockScopes.count(Blocks[i]) &&
757bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson           "Did not find block in scope map!");
7581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
759bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson    BlockScopes.erase(Blocks[i]);
760bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson  }
7611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
76299533834ba8f3658559f334e68a518ebb6388ceaMike Stump  return CleanupBlockInfo(CleanupEntryBlock, SwitchBlock, EndBlock, EHOnly);
763d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson}
764d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson
7651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid CodeGenFunction::EmitCleanupBlock() {
766bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  CleanupBlockInfo Info = PopCleanupBlock();
7671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
76899533834ba8f3658559f334e68a518ebb6388ceaMike Stump  if (Info.EHOnly) {
76999533834ba8f3658559f334e68a518ebb6388ceaMike Stump    // FIXME: Add this to the exceptional edge
77099533834ba8f3658559f334e68a518ebb6388ceaMike Stump    if (Info.CleanupBlock->getNumUses() == 0)
77199533834ba8f3658559f334e68a518ebb6388ceaMike Stump      delete Info.CleanupBlock;
77299533834ba8f3658559f334e68a518ebb6388ceaMike Stump    return;
77399533834ba8f3658559f334e68a518ebb6388ceaMike Stump  }
77499533834ba8f3658559f334e68a518ebb6388ceaMike Stump
775eb6437a9b23b3d98a4590ba5557111e14eba2edfAnders Carlsson  llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
7761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (CurBB && !CurBB->getTerminator() &&
777eb6437a9b23b3d98a4590ba5557111e14eba2edfAnders Carlsson      Info.CleanupBlock->getNumUses() == 0) {
778eb6437a9b23b3d98a4590ba5557111e14eba2edfAnders Carlsson    CurBB->getInstList().splice(CurBB->end(), Info.CleanupBlock->getInstList());
779eb6437a9b23b3d98a4590ba5557111e14eba2edfAnders Carlsson    delete Info.CleanupBlock;
7801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  } else
781eb6437a9b23b3d98a4590ba5557111e14eba2edfAnders Carlsson    EmitBlock(Info.CleanupBlock);
7821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
783bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  if (Info.SwitchBlock)
784bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson    EmitBlock(Info.SwitchBlock);
785bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  if (Info.EndBlock)
786bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson    EmitBlock(Info.EndBlock);
787d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson}
788d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson
7891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid CodeGenFunction::AddBranchFixup(llvm::BranchInst *BI) {
7901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(!CleanupEntries.empty() &&
79187eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson         "Trying to add branch fixup without cleanup block!");
7921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
793f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // FIXME: We could be more clever here and check if there's already a branch
794f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // fixup for this destination and recycle it.
79587eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  CleanupEntries.back().BranchFixups.push_back(BI);
79687eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson}
79787eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson
7981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid CodeGenFunction::EmitBranchThroughCleanup(llvm::BasicBlock *Dest) {
79946831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson  if (!HaveInsertPoint())
80046831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson    return;
8011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
80287eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  llvm::BranchInst* BI = Builder.CreateBr(Dest);
8031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
80446831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson  Builder.ClearInsertionPoint();
8051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
80687eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  // The stack is empty, no need to do any cleanup.
80787eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  if (CleanupEntries.empty())
80887eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson    return;
8091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
81087eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  if (!Dest->getParent()) {
81187eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson    // We are trying to branch to a block that hasn't been inserted yet.
81287eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson    AddBranchFixup(BI);
81387eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson    return;
81487eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  }
8151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
81687eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  BlockScopeMap::iterator I = BlockScopes.find(Dest);
81787eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  if (I == BlockScopes.end()) {
81887eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson    // We are trying to jump to a block that is outside of any cleanup scope.
81987eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson    AddBranchFixup(BI);
82087eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson    return;
82187eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  }
8221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
82387eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  assert(I->second < CleanupEntries.size() &&
82487eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson         "Trying to branch into cleanup region");
8251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
82687eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  if (I->second == CleanupEntries.size() - 1) {
82787eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson    // We have a branch to a block in the same scope.
82887eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson    return;
82987eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  }
8301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
83187eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  AddBranchFixup(BI);
83287eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson}
833