CodeGenFunction.cpp revision 4e7a1f7682d94811bd41fca8aefccc38f686db23
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"
21d9363c3a80168283b3da518b4e17f545a6246857Devang Patel#include "llvm/Support/CFG.h"
224e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump#include "llvm/Target/TargetData.h"
235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace CodeGen;
255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerCodeGenFunction::CodeGenFunction(CodeGenModule &cgm)
27e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson  : CGM(cgm), Target(CGM.getContext().Target), DebugInfo(0), SwitchInsn(0),
284e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump    CaseRangeBlock(0) {
294e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump  LLVMIntTy = ConvertType(getContext().IntTy);
304e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump  LLVMPointerWidth = Target.getPointerWidth(0);
314e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump
324e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump  // FIXME: We need to rearrange the code for copy/dispose so we have this
334e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump  // sooner, so we can calculate offsets correctly.
344e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump  BlockHasCopyDispose = false;
354e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump  if (!BlockHasCopyDispose)
364e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump    BlockOffset = CGM.getTargetData()
374e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump      .getTypeStoreSizeInBits(CGM.getGenericBlockLiteralType()) / 8;
384e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump  else
394e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump    BlockOffset = CGM.getTargetData()
404e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump      .getTypeStoreSizeInBits(CGM.getGenericExtendedBlockLiteralType()) / 8;
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;
515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Create, but don't insert, the new block.
5355e874299f2ad827646a4ca9ea38c402aaeb38c9Daniel Dunbar  return BB = createBasicBlock(S->getName());
545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
56813733577d33ec56479667b49e1bff42dc6bba90Lauro Ramos Venanciollvm::Constant *
57248a753f6b670692523c99afaeb8fe98f7ae3ca7Steve NaroffCodeGenFunction::GetAddrOfStaticLocalVar(const VarDecl *BVD) {
58813733577d33ec56479667b49e1bff42dc6bba90Lauro Ramos Venancio  return cast<llvm::Constant>(LocalDeclMap[BVD]);
59813733577d33ec56479667b49e1bff42dc6bba90Lauro Ramos Venancio}
605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
61dde0a94120915fa925d1ffcdb997c7b44dc9fa21Anders Carlssonllvm::Value *CodeGenFunction::GetAddrOfLocalVar(const VarDecl *VD)
62dde0a94120915fa925d1ffcdb997c7b44dc9fa21Anders Carlsson{
63dde0a94120915fa925d1ffcdb997c7b44dc9fa21Anders Carlsson  return LocalDeclMap[VD];
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::isObjCPointerType(QualType T) {
754111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner  // All Objective-C types are pointers.
764111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner  return T->isObjCInterfaceType() ||
774111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner    T->isObjCQualifiedInterfaceType() || T->isObjCQualifiedIdType();
785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
804111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattnerbool CodeGenFunction::hasAggregateLLVMType(QualType T) {
81a782ca76cc7dfdd69e331a4fa722fc23b18d5c34Daniel Dunbar  // FIXME: Use positive checks instead of negative ones to be more
82a782ca76cc7dfdd69e331a4fa722fc23b18d5c34Daniel Dunbar  // robust in the face of extension.
834111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner  return !isObjCPointerType(T) &&!T->isRealType() && !T->isPointerLikeType() &&
84a782ca76cc7dfdd69e331a4fa722fc23b18d5c34Daniel Dunbar    !T->isVoidType() && !T->isVectorType() && !T->isFunctionType() &&
85a782ca76cc7dfdd69e331a4fa722fc23b18d5c34Daniel Dunbar    !T->isBlockPointerType();
864111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner}
87391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner
881c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbarvoid CodeGenFunction::EmitReturnBlock() {
891c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // For cleanliness, we try to avoid emitting the return block for
901c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // simple cases.
911c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
921c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
931c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  if (CurBB) {
941c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar    assert(!CurBB->getTerminator() && "Unexpected terminated block.");
951c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
961c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar    // We have a valid insert point, reuse it if there are no explicit
971c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar    // jumps to the return block.
981c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar    if (ReturnBlock->use_empty())
991c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      delete ReturnBlock;
1001c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar    else
1011c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      EmitBlock(ReturnBlock);
1021c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar    return;
1031c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  }
1041c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
1051c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // Otherwise, if the return block is the target of a single direct
1061c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // branch then we can just put the code in that block instead. This
1071c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // cleans up functions which started with a unified return block.
1081c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  if (ReturnBlock->hasOneUse()) {
1091c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar    llvm::BranchInst *BI =
1101c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      dyn_cast<llvm::BranchInst>(*ReturnBlock->use_begin());
1111c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar    if (BI && BI->isUnconditional() && BI->getSuccessor(0) == ReturnBlock) {
1121c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      // Reset insertion point and delete the branch.
1131c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      Builder.SetInsertPoint(BI->getParent());
1141c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      BI->eraseFromParent();
1151c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      delete ReturnBlock;
1161c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      return;
1171c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar    }
1181c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  }
1191c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
1201c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // FIXME: We are at an unreachable point, there is no reason to emit
1211c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // the block unless it has uses. However, we still need a place to
1221c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // put the debug region.end for now.
1231c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
1241c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  EmitBlock(ReturnBlock);
1251c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar}
1261c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
127af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbarvoid CodeGenFunction::FinishFunction(SourceLocation EndLoc) {
1280ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar  // Finish emission of indirect switches.
1290ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar  EmitIndirectSwitches();
1300ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar
131391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  assert(BreakContinueStack.empty() &&
132391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner         "mismatched push/pop in break/continue stack!");
133bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson  assert(BlockScopes.empty() &&
134bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson         "did not remove all blocks from block scope map!");
135bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson  assert(CleanupEntries.empty() &&
136bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson         "mismatched push/pop in cleanup stack!");
137bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson
1381c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // Emit function epilog (to return).
1391c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  EmitReturnBlock();
140f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar
141f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar  // Emit debug descriptor for function end.
142e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson  if (CGDebugInfo *DI = getDebugInfo()) {
143f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar    DI->setLocation(EndLoc);
144f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar    DI->EmitRegionEnd(CurFn, Builder);
145f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar  }
146f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar
14788b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar  EmitFunctionEpilog(*CurFnInfo, ReturnValue);
1485ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar
149391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  // Remove the AllocaInsertPt instruction, which is just a convenience for us.
150391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  AllocaInsertPt->eraseFromParent();
151391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  AllocaInsertPt = 0;
152c8aa5f1f264fb230c38182adab944232bb160c2bChris Lattner}
153c8aa5f1f264fb230c38182adab944232bb160c2bChris Lattner
1547c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbarvoid CodeGenFunction::StartFunction(const Decl *D, QualType RetTy,
1557c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar                                    llvm::Function *Fn,
1562284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar                                    const FunctionArgList &Args,
1572284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar                                    SourceLocation StartLoc) {
1584cc1a4703363ff940b6273aeef9d96a87edeb04bAnders Carlsson  DidCallStackSave = false;
1597c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar  CurFuncDecl = D;
1607c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar  FnRetTy = RetTy;
161bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  CurFn = Fn;
1625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(CurFn->isDeclaration() && "Function already has body?");
163ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner
16455e874299f2ad827646a4ca9ea38c402aaeb38c9Daniel Dunbar  llvm::BasicBlock *EntryBB = createBasicBlock("entry", CurFn);
1655ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar
1665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Create a marker to make it easy to insert allocas into the entryblock
16755352a2d616cf9fbb621d10faf8b960b4b268bd8Chris Lattner  // later.  Don't create this with the builder, because we don't want it
16855352a2d616cf9fbb621d10faf8b960b4b268bd8Chris Lattner  // folded.
1695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty);
17055352a2d616cf9fbb621d10faf8b960b4b268bd8Chris Lattner  AllocaInsertPt = new llvm::BitCastInst(Undef, llvm::Type::Int32Ty, "allocapt",
17155352a2d616cf9fbb621d10faf8b960b4b268bd8Chris Lattner                                         EntryBB);
1725ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar
17355e874299f2ad827646a4ca9ea38c402aaeb38c9Daniel Dunbar  ReturnBlock = createBasicBlock("return");
1745ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar  ReturnValue = 0;
1757c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar  if (!RetTy->isVoidType())
1767c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar    ReturnValue = CreateTempAlloca(ConvertType(RetTy), "retval");
1775ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar
17855352a2d616cf9fbb621d10faf8b960b4b268bd8Chris Lattner  Builder.SetInsertPoint(EntryBB);
1794111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner
180af99417156c652a6f04dff643925036dc3241d60Sanjiv Gupta  // Emit subprogram debug descriptor.
1817c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar  // FIXME: The cast here is a huge hack.
182e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson  if (CGDebugInfo *DI = getDebugInfo()) {
1832284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar    DI->setLocation(StartLoc);
1842284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar    if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
1856ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor      DI->EmitFunctionStart(CGM.getMangledName(FD), RetTy, CurFn, Builder);
1862284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar    } else {
1872284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar      // Just use LLVM function name.
1882284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar      DI->EmitFunctionStart(Fn->getName().c_str(),
1892284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar                            RetTy, CurFn, Builder);
190af99417156c652a6f04dff643925036dc3241d60Sanjiv Gupta    }
191af99417156c652a6f04dff643925036dc3241d60Sanjiv Gupta  }
192af99417156c652a6f04dff643925036dc3241d60Sanjiv Gupta
19388b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar  // FIXME: Leaked.
194541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar  CurFnInfo = &CGM.getTypes().getFunctionInfo(FnRetTy, Args);
19588b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar  EmitFunctionProlog(*CurFnInfo, CurFn, Args);
196751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson
197751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson  // If any of the arguments have a variably modified type, make sure to
198751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson  // emit the type size.
199751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson  for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
200751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson       i != e; ++i) {
201751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson    QualType Ty = i->second;
202751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson
203751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson    if (Ty->isVariablyModifiedType())
204751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson      EmitVLASize(Ty);
205751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson  }
2067c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar}
207eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman
2087c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbarvoid CodeGenFunction::GenerateCode(const FunctionDecl *FD,
2097c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar                                   llvm::Function *Fn) {
210e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson  // Check if we should generate debug info for this function.
211e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson  if (CGM.getDebugInfo() && !FD->getAttr<NodebugAttr>())
212e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson    DebugInfo = CGM.getDebugInfo();
213e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson
2147c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar  FunctionArgList Args;
215eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman  if (FD->getNumParams()) {
216eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman    const FunctionTypeProto* FProto = FD->getType()->getAsFunctionTypeProto();
217eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman    assert(FProto && "Function def must have prototype!");
2187c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar
2197c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar    for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i)
2207c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar      Args.push_back(std::make_pair(FD->getParamDecl(i),
2217c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar                                    FProto->getArgType(i)));
2225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
223af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
2242284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar  StartFunction(FD, FD->getResultType(), Fn, Args,
2252284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar                cast<CompoundStmt>(FD->getBody())->getLBracLoc());
2267c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar
227af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar  EmitStmt(FD->getBody());
228af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
229af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar  const CompoundStmt *S = dyn_cast<CompoundStmt>(FD->getBody());
230af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar  if (S) {
231af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar    FinishFunction(S->getRBracLoc());
232af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar  } else {
233af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar    FinishFunction();
234af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar  }
2355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
2365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2370946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner/// ContainsLabel - Return true if the statement contains a label in it.  If
2380946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner/// this statement is not executed normally, it not containing a label means
2390946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner/// that we can just remove the code.
2400946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattnerbool CodeGenFunction::ContainsLabel(const Stmt *S, bool IgnoreCaseStmts) {
2410946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // Null statement, not a label!
2420946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  if (S == 0) return false;
2430946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner
2440946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // If this is a label, we have to emit the code, consider something like:
2450946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // if (0) {  ...  foo:  bar(); }  goto foo;
2460946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  if (isa<LabelStmt>(S))
2470946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner    return true;
2480946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner
2490946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // If this is a case/default statement, and we haven't seen a switch, we have
2500946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // to emit the code.
2510946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  if (isa<SwitchCase>(S) && !IgnoreCaseStmts)
2520946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner    return true;
2530946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner
2540946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // If this is a switch statement, we want to ignore cases below it.
2550946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  if (isa<SwitchStmt>(S))
2560946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner    IgnoreCaseStmts = true;
2570946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner
2580946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // Scan subexpressions for verboten labels.
2590946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
2600946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner       I != E; ++I)
2610946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner    if (ContainsLabel(*I, IgnoreCaseStmts))
2620946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner      return true;
2630946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner
2640946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  return false;
2650946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner}
2660946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner
26731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
26831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// ConstantFoldsToSimpleInteger - If the sepcified expression does not fold to
26931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// a constant, or if it does but contains a label, return 0.  If it constant
27031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// folds to 'true' and does not contain a label, return 1, if it constant folds
27131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// to 'false' and does not contain a label, return -1.
27231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattnerint CodeGenFunction::ConstantFoldsToSimpleInteger(const Expr *Cond) {
27336bc14c3a1cf63ee306df5687ac8e85f924f8639Daniel Dunbar  // FIXME: Rename and handle conversion of other evaluatable things
27436bc14c3a1cf63ee306df5687ac8e85f924f8639Daniel Dunbar  // to bool.
27564712f196bffd41fb0552c2643b07a25c3e45082Anders Carlsson  Expr::EvalResult Result;
27664712f196bffd41fb0552c2643b07a25c3e45082Anders Carlsson  if (!Cond->Evaluate(Result, getContext()) || !Result.Val.isInt() ||
27764712f196bffd41fb0552c2643b07a25c3e45082Anders Carlsson      Result.HasSideEffects)
278ef5a66d8171eb95e948107f8ee7707b360aaff25Anders Carlsson    return 0;  // Not foldable, not integer or not fully evaluatable.
27931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
28031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  if (CodeGenFunction::ContainsLabel(Cond))
28131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner    return 0;  // Contains a label.
28231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
28364712f196bffd41fb0552c2643b07a25c3e45082Anders Carlsson  return Result.Val.getInt().getBoolValue() ? 1 : -1;
28431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner}
28531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
28631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
28731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an if
28831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// statement) to the specified blocks.  Based on the condition, this might try
28931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// to simplify the codegen of the conditional based on the branch.
29031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner///
29131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattnervoid CodeGenFunction::EmitBranchOnBoolExpr(const Expr *Cond,
29231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner                                           llvm::BasicBlock *TrueBlock,
29331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner                                           llvm::BasicBlock *FalseBlock) {
29431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  if (const ParenExpr *PE = dyn_cast<ParenExpr>(Cond))
29531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner    return EmitBranchOnBoolExpr(PE->getSubExpr(), TrueBlock, FalseBlock);
29631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
29731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  if (const BinaryOperator *CondBOp = dyn_cast<BinaryOperator>(Cond)) {
29831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner    // Handle X && Y in a condition.
29931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner    if (CondBOp->getOpcode() == BinaryOperator::LAnd) {
30031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // If we have "1 && X", simplify the code.  "0 && X" would have constant
30131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // folded if the case was simple enough.
30231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == 1) {
30331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        // br(1 && X) -> br(X).
30431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
30531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      }
30631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
30731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // If we have "X && 1", simplify the code to use an uncond branch.
30831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // "X && 0" would have been constant folded to 0.
30931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == 1) {
31031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        // br(X && 1) -> br(X).
31131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock);
31231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      }
31331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
31431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // Emit the LHS as a conditional.  If the LHS conditional is false, we
31531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // want to jump to the FalseBlock.
3169615ecb44f549ae9fa2b4db6ff46bc78befbf62cDaniel Dunbar      llvm::BasicBlock *LHSTrue = createBasicBlock("land.lhs.true");
31731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBranchOnBoolExpr(CondBOp->getLHS(), LHSTrue, FalseBlock);
31831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBlock(LHSTrue);
31931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
32031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
32131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      return;
32231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner    } else if (CondBOp->getOpcode() == BinaryOperator::LOr) {
32331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // If we have "0 || X", simplify the code.  "1 || X" would have constant
32431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // folded if the case was simple enough.
32531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == -1) {
32631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        // br(0 || X) -> br(X).
32731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
32831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      }
32931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
33031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // If we have "X || 0", simplify the code to use an uncond branch.
33131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // "X || 1" would have been constant folded to 1.
33231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == -1) {
33331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        // br(X || 0) -> br(X).
33431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock);
33531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      }
33631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
33731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // Emit the LHS as a conditional.  If the LHS conditional is true, we
33831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // want to jump to the TrueBlock.
3399615ecb44f549ae9fa2b4db6ff46bc78befbf62cDaniel Dunbar      llvm::BasicBlock *LHSFalse = createBasicBlock("lor.lhs.false");
34031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, LHSFalse);
34131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBlock(LHSFalse);
34231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
34331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
34431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      return;
34531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner    }
346552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner  }
347552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner
348552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner  if (const UnaryOperator *CondUOp = dyn_cast<UnaryOperator>(Cond)) {
349552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner    // br(!x, t, f) -> br(x, f, t)
350552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner    if (CondUOp->getOpcode() == UnaryOperator::LNot)
351552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner      return EmitBranchOnBoolExpr(CondUOp->getSubExpr(), FalseBlock, TrueBlock);
35231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  }
35331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
35409b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar  if (const ConditionalOperator *CondOp = dyn_cast<ConditionalOperator>(Cond)) {
35509b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar    // Handle ?: operator.
35609b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar
35709b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar    // Just ignore GNU ?: extension.
35809b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar    if (CondOp->getLHS()) {
35909b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      // br(c ? x : y, t, f) -> br(c, br(x, t, f), br(y, t, f))
36009b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true");
36109b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false");
36209b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      EmitBranchOnBoolExpr(CondOp->getCond(), LHSBlock, RHSBlock);
36309b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      EmitBlock(LHSBlock);
36409b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      EmitBranchOnBoolExpr(CondOp->getLHS(), TrueBlock, FalseBlock);
36509b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      EmitBlock(RHSBlock);
36609b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      EmitBranchOnBoolExpr(CondOp->getRHS(), TrueBlock, FalseBlock);
36709b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      return;
36809b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar    }
36909b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar  }
37009b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar
37131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  // Emit the code with the fully general case.
37231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  llvm::Value *CondV = EvaluateExprAsBool(Cond);
37331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  Builder.CreateCondBr(CondV, TrueBlock, FalseBlock);
37431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner}
37531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
37688a981b47c7face1b1fdaa9074256245107b9ca9Devang Patel/// getCGRecordLayout - Return record layout info.
37788a981b47c7face1b1fdaa9074256245107b9ca9Devang Patelconst CGRecordLayout *CodeGenFunction::getCGRecordLayout(CodeGenTypes &CGT,
378af31913e48c96fddb45a0fd33f25617546502cbbChris Lattner                                                         QualType Ty) {
379af31913e48c96fddb45a0fd33f25617546502cbbChris Lattner  const RecordType *RTy = Ty->getAsRecordType();
380af31913e48c96fddb45a0fd33f25617546502cbbChris Lattner  assert (RTy && "Unexpected type. RecordType expected here.");
381b84a06e68ffd71da22e3c75b6e4bbdba37816413Devang Patel
382af31913e48c96fddb45a0fd33f25617546502cbbChris Lattner  return CGT.getCGRecordLayout(RTy->getDecl());
383b84a06e68ffd71da22e3c75b6e4bbdba37816413Devang Patel}
384dc5e8268292046114ffe02e48773572a91a310f1Chris Lattner
385488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar/// ErrorUnsupported - Print out an error that codegen doesn't support the
386dc5e8268292046114ffe02e48773572a91a310f1Chris Lattner/// specified stmt yet.
38790df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbarvoid CodeGenFunction::ErrorUnsupported(const Stmt *S, const char *Type,
38890df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar                                       bool OmitOnError) {
38990df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar  CGM.ErrorUnsupported(S, Type, OmitOnError);
390dc5e8268292046114ffe02e48773572a91a310f1Chris Lattner}
391dc5e8268292046114ffe02e48773572a91a310f1Chris Lattner
3920ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbarunsigned CodeGenFunction::GetIDForAddrOfLabel(const LabelStmt *L) {
3930ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar  // Use LabelIDs.size() as the new ID if one hasn't been assigned.
3940ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar  return LabelIDs.insert(std::make_pair(L, LabelIDs.size())).first->second;
3950ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar}
3960ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar
3973d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlssonvoid CodeGenFunction::EmitMemSetToZero(llvm::Value *DestPtr, QualType Ty)
3983d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson{
3993d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson  const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
4003d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson  if (DestPtr->getType() != BP)
4013d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson    DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
4023d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson
4033d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson  // Get size and alignment info for this aggregate.
4043d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson  std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty);
4053d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson
4063d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson  // FIXME: Handle variable sized types.
4073d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson  const llvm::Type *IntPtr = llvm::IntegerType::get(LLVMPointerWidth);
4083d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson
4093d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson  Builder.CreateCall4(CGM.getMemSetFn(), DestPtr,
4103d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson                      llvm::ConstantInt::getNullValue(llvm::Type::Int8Ty),
4113d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson                      // TypeInfo.first describes size in bits.
4123d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson                      llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
4133d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson                      llvm::ConstantInt::get(llvm::Type::Int32Ty,
4143d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson                                             TypeInfo.second/8));
4153d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson}
4163d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson
4170ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbarvoid CodeGenFunction::EmitIndirectSwitches() {
4180ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar  llvm::BasicBlock *Default;
4190ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar
42076526a5245ec2283b0c85fcef507d4c2dec90715Daniel Dunbar  if (IndirectSwitches.empty())
42176526a5245ec2283b0c85fcef507d4c2dec90715Daniel Dunbar    return;
42276526a5245ec2283b0c85fcef507d4c2dec90715Daniel Dunbar
4230ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar  if (!LabelIDs.empty()) {
4240ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar    Default = getBasicBlockForLabel(LabelIDs.begin()->first);
4250ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar  } else {
4260ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar    // No possible targets for indirect goto, just emit an infinite
4270ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar    // loop.
42855e874299f2ad827646a4ca9ea38c402aaeb38c9Daniel Dunbar    Default = createBasicBlock("indirectgoto.loop", CurFn);
4290ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar    llvm::BranchInst::Create(Default, Default);
4300ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar  }
4310ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar
4320ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar  for (std::vector<llvm::SwitchInst*>::iterator i = IndirectSwitches.begin(),
4330ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar         e = IndirectSwitches.end(); i != e; ++i) {
4340ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar    llvm::SwitchInst *I = *i;
4350ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar
4360ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar    I->setSuccessor(0, Default);
4370ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar    for (std::map<const LabelStmt*,unsigned>::iterator LI = LabelIDs.begin(),
4380ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar           LE = LabelIDs.end(); LI != LE; ++LI) {
4390ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar      I->addCase(llvm::ConstantInt::get(llvm::Type::Int32Ty,
4400ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar                                        LI->second),
4410ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar                 getBasicBlockForLabel(LI->first));
4420ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar    }
4430ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar  }
4440ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar}
445ddf7cac45d85b73127adbbd91a2b28fc7291c57eAnders Carlsson
446dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlssonllvm::Value *CodeGenFunction::GetVLASize(const VariableArrayType *VAT)
447dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlsson{
448dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlsson  llvm::Value *&SizeEntry = VLASizeMap[VAT];
449dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlsson
450f666b7780d04186521adcaedb0e15dfa4d5e6933Anders Carlsson  assert(SizeEntry && "Did not emit size for type");
451f666b7780d04186521adcaedb0e15dfa4d5e6933Anders Carlsson  return SizeEntry;
452f666b7780d04186521adcaedb0e15dfa4d5e6933Anders Carlsson}
453dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlsson
45460d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlssonllvm::Value *CodeGenFunction::EmitVLASize(QualType Ty)
455f666b7780d04186521adcaedb0e15dfa4d5e6933Anders Carlsson{
45660d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson  assert(Ty->isVariablyModifiedType() &&
45760d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson         "Must pass variably modified type to EmitVLASizes!");
458f666b7780d04186521adcaedb0e15dfa4d5e6933Anders Carlsson
45960d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson  if (const VariableArrayType *VAT = getContext().getAsVariableArrayType(Ty)) {
46060d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson    llvm::Value *&SizeEntry = VLASizeMap[VAT];
46160d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson
462fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson    if (!SizeEntry) {
463fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson      // Get the element size;
464fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson      llvm::Value *ElemSize;
46560d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson
466fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson      QualType ElemTy = VAT->getElementType();
46796f214776c0f69069fee4d67557c8c7f416009a8Anders Carlsson
46896f214776c0f69069fee4d67557c8c7f416009a8Anders Carlsson      const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
46996f214776c0f69069fee4d67557c8c7f416009a8Anders Carlsson
470fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson      if (ElemTy->isVariableArrayType())
471fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson        ElemSize = EmitVLASize(ElemTy);
472fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson      else {
47396f214776c0f69069fee4d67557c8c7f416009a8Anders Carlsson        ElemSize = llvm::ConstantInt::get(SizeTy,
474fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson                                          getContext().getTypeSize(ElemTy) / 8);
475fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson      }
47660d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson
477fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson      llvm::Value *NumElements = EmitScalarExpr(VAT->getSizeExpr());
47896f214776c0f69069fee4d67557c8c7f416009a8Anders Carlsson      NumElements = Builder.CreateIntCast(NumElements, SizeTy, false, "tmp");
47996f214776c0f69069fee4d67557c8c7f416009a8Anders Carlsson
480fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson      SizeEntry = Builder.CreateMul(ElemSize, NumElements);
481fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson    }
48260d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson
48360d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson    return SizeEntry;
48460d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson  } else if (const PointerType *PT = Ty->getAsPointerType())
48560d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson    EmitVLASize(PT->getPointeeType());
486f666b7780d04186521adcaedb0e15dfa4d5e6933Anders Carlsson  else {
48760d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson    assert(0 && "unknown VM type!");
488dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlsson  }
48960d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson
49060d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson  return 0;
491dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlsson}
4924fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman
4934fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedmanllvm::Value* CodeGenFunction::EmitVAListRef(const Expr* E) {
4944fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman  if (CGM.getContext().getBuiltinVaListType()->isArrayType()) {
4954fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman    return EmitScalarExpr(E);
4964fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman  }
4974fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman  return EmitLValue(E).getAddress();
4984fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman}
4996ccc47698d0311ddabf32fa0f6db8e4f09ac96f8Anders Carlsson
5006fc559136b8ef98bfb824a0fd49df385405f2879Anders Carlssonvoid CodeGenFunction::PushCleanupBlock(llvm::BasicBlock *CleanupBlock)
5016ccc47698d0311ddabf32fa0f6db8e4f09ac96f8Anders Carlsson{
5026ccc47698d0311ddabf32fa0f6db8e4f09ac96f8Anders Carlsson  CleanupEntries.push_back(CleanupEntry(CleanupBlock));
5036ccc47698d0311ddabf32fa0f6db8e4f09ac96f8Anders Carlsson}
504c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson
505c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlssonvoid CodeGenFunction::EmitCleanupBlocks(size_t OldCleanupStackSize)
506c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson{
507c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson  assert(CleanupEntries.size() >= OldCleanupStackSize &&
508c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson         "Cleanup stack mismatch!");
509c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson
510c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson  while (CleanupEntries.size() > OldCleanupStackSize)
511c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson    EmitCleanupBlock();
512c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson}
513c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson
514bb66f9f2e454135b86462d121629275b6ac38e96Anders CarlssonCodeGenFunction::CleanupBlockInfo CodeGenFunction::PopCleanupBlock()
515c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson{
516bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  CleanupEntry &CE = CleanupEntries.back();
517bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson
518bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  llvm::BasicBlock *CleanupBlock = CE.CleanupBlock;
519bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson
520bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  std::vector<llvm::BasicBlock *> Blocks;
521bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  std::swap(Blocks, CE.Blocks);
522bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson
523bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  std::vector<llvm::BranchInst *> BranchFixups;
524bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  std::swap(BranchFixups, CE.BranchFixups);
525bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson
526bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  CleanupEntries.pop_back();
527bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson
528ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson  // Check if any branch fixups pointed to the scope we just popped. If so,
529ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson  // we can remove them.
530ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson  for (size_t i = 0, e = BranchFixups.size(); i != e; ++i) {
531ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson    llvm::BasicBlock *Dest = BranchFixups[i]->getSuccessor(0);
532ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson    BlockScopeMap::iterator I = BlockScopes.find(Dest);
533d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson
534ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson    if (I == BlockScopes.end())
535ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson      continue;
5361093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson
537ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson    assert(I->second <= CleanupEntries.size() && "Invalid branch fixup!");
538d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson
539ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson    if (I->second == CleanupEntries.size()) {
540ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson      // We don't need to do this branch fixup.
541ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson      BranchFixups[i] = BranchFixups.back();
542ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson      BranchFixups.pop_back();
543ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson      i--;
544ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson      e--;
545ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson      continue;
5461093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson    }
5471093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson  }
548d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson
549bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  llvm::BasicBlock *SwitchBlock = 0;
550bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  llvm::BasicBlock *EndBlock = 0;
5511093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson  if (!BranchFixups.empty()) {
552bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson    SwitchBlock = createBasicBlock("cleanup.switch");
553bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson    EndBlock = createBasicBlock("cleanup.end");
5541093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson
555bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson    llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
556bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson
557bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson    Builder.SetInsertPoint(SwitchBlock);
558bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson
5591093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson    llvm::Value *DestCodePtr = CreateTempAlloca(llvm::Type::Int32Ty,
5601093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson                                                "cleanup.dst");
5611093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson    llvm::Value *DestCode = Builder.CreateLoad(DestCodePtr, "tmp");
5621093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson
5631093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson    // Create a switch instruction to determine where to jump next.
564bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson    llvm::SwitchInst *SI = Builder.CreateSwitch(DestCode, EndBlock,
5651093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson                                                BranchFixups.size());
566bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson
56746831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson    // Restore the current basic block (if any)
56846831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson    if (CurBB)
56946831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson      Builder.SetInsertPoint(CurBB);
57046831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson    else
57146831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson      Builder.ClearInsertionPoint();
572bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson
5731093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson    for (size_t i = 0, e = BranchFixups.size(); i != e; ++i) {
5741093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson      llvm::BranchInst *BI = BranchFixups[i];
5751093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson      llvm::BasicBlock *Dest = BI->getSuccessor(0);
5761093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson
5771093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson      // Fixup the branch instruction to point to the cleanup block.
5781093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson      BI->setSuccessor(0, CleanupBlock);
579d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson
5801093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson      if (CleanupEntries.empty()) {
581cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        llvm::ConstantInt *ID;
582cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson
583cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        // Check if we already have a destination for this block.
584cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        if (Dest == SI->getDefaultDest())
585cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson          ID = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
586cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        else {
587cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson          ID = SI->findCaseDest(Dest);
588cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson          if (!ID) {
589cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson            // No code found, get a new unique one by using the number of
590cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson            // switch successors.
591cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson            ID = llvm::ConstantInt::get(llvm::Type::Int32Ty,
592cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson                                        SI->getNumSuccessors());
593cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson            SI->addCase(ID, Dest);
594cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson          }
595cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        }
596cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson
597cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        // Store the jump destination before the branch instruction.
598cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        new llvm::StoreInst(ID, DestCodePtr, BI);
5991093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson      } else {
6001093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson        // We need to jump through another cleanup block. Create a pad block
6011093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson        // with a branch instruction that jumps to the final destination and
6021093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson        // add it as a branch fixup to the current cleanup scope.
6031093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson
6041093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson        // Create the pad block.
6051093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson        llvm::BasicBlock *CleanupPad = createBasicBlock("cleanup.pad", CurFn);
606cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson
607cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        // Create a unique case ID.
608cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        llvm::ConstantInt *ID = llvm::ConstantInt::get(llvm::Type::Int32Ty,
609cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson                                                       SI->getNumSuccessors());
610cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson
611cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        // Store the jump destination before the branch instruction.
612cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        new llvm::StoreInst(ID, DestCodePtr, BI);
613cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson
6141093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson        // Add it as the destination.
615cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        SI->addCase(ID, CleanupPad);
6161093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson
6171093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson        // Create the branch to the final destination.
6181093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson        llvm::BranchInst *BI = llvm::BranchInst::Create(Dest);
6191093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson        CleanupPad->getInstList().push_back(BI);
6201093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson
6211093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson        // And add it as a branch fixup.
6221093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson        CleanupEntries.back().BranchFixups.push_back(BI);
6231093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson      }
6241093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson    }
6251093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson  }
626d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson
627bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson  // Remove all blocks from the block scope map.
628bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson  for (size_t i = 0, e = Blocks.size(); i != e; ++i) {
629bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson    assert(BlockScopes.count(Blocks[i]) &&
630bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson           "Did not find block in scope map!");
631bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson
632bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson    BlockScopes.erase(Blocks[i]);
633bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson  }
634d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson
635bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  return CleanupBlockInfo(CleanupBlock, SwitchBlock, EndBlock);
636d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson}
637d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson
638d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlssonvoid CodeGenFunction::EmitCleanupBlock()
639d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson{
640bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  CleanupBlockInfo Info = PopCleanupBlock();
641d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson
642bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  EmitBlock(Info.CleanupBlock);
643d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson
644bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  if (Info.SwitchBlock)
645bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson    EmitBlock(Info.SwitchBlock);
646bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  if (Info.EndBlock)
647bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson    EmitBlock(Info.EndBlock);
648d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson}
649d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson
65087eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlssonvoid CodeGenFunction::AddBranchFixup(llvm::BranchInst *BI)
65187eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson{
65287eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  assert(!CleanupEntries.empty() &&
65387eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson         "Trying to add branch fixup without cleanup block!");
65487eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson
65587eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  // FIXME: We could be more clever here and check if there's already a
65687eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  // branch fixup for this destination and recycle it.
65787eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  CleanupEntries.back().BranchFixups.push_back(BI);
65887eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson}
65987eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson
66087eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlssonvoid CodeGenFunction::EmitBranchThroughCleanup(llvm::BasicBlock *Dest)
66187eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson{
66246831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson  if (!HaveInsertPoint())
66346831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson    return;
66446831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson
66587eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  llvm::BranchInst* BI = Builder.CreateBr(Dest);
66687eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson
66746831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson  Builder.ClearInsertionPoint();
66846831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson
66987eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  // The stack is empty, no need to do any cleanup.
67087eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  if (CleanupEntries.empty())
67187eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson    return;
67287eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson
67387eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  if (!Dest->getParent()) {
67487eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson    // We are trying to branch to a block that hasn't been inserted yet.
67587eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson    AddBranchFixup(BI);
67687eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson    return;
67787eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  }
67887eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson
67987eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  BlockScopeMap::iterator I = BlockScopes.find(Dest);
68087eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  if (I == BlockScopes.end()) {
68187eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson    // We are trying to jump to a block that is outside of any cleanup scope.
68287eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson    AddBranchFixup(BI);
68387eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson    return;
68487eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  }
68587eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson
68687eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  assert(I->second < CleanupEntries.size() &&
68787eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson         "Trying to branch into cleanup region");
68887eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson
68987eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  if (I->second == CleanupEntries.size() - 1) {
69087eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson    // We have a branch to a block in the same scope.
69187eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson    return;
69287eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  }
69387eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson
69487eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  AddBranchFixup(BI);
69587eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson}
696