CodeGenFunction.cpp revision 6ec3668a2608b63473207319f5ceff9bbd22ea51
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"
225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace CodeGen;
245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerCodeGenFunction::CodeGenFunction(CodeGenModule &cgm)
26e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson  : CGM(cgm), Target(CGM.getContext().Target), DebugInfo(0), SwitchInsn(0),
27e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson  CaseRangeBlock(0) {
284111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner    LLVMIntTy = ConvertType(getContext().IntTy);
294111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner    LLVMPointerWidth = Target.getPointerWidth(0);
304111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner}
315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerASTContext &CodeGenFunction::getContext() const {
335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return CGM.getContext();
345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerllvm::BasicBlock *CodeGenFunction::getBasicBlockForLabel(const LabelStmt *S) {
385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::BasicBlock *&BB = LabelMap[S];
395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (BB) return BB;
405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Create, but don't insert, the new block.
4255e874299f2ad827646a4ca9ea38c402aaeb38c9Daniel Dunbar  return BB = createBasicBlock(S->getName());
435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
45813733577d33ec56479667b49e1bff42dc6bba90Lauro Ramos Venanciollvm::Constant *
46248a753f6b670692523c99afaeb8fe98f7ae3ca7Steve NaroffCodeGenFunction::GetAddrOfStaticLocalVar(const VarDecl *BVD) {
47813733577d33ec56479667b49e1bff42dc6bba90Lauro Ramos Venancio  return cast<llvm::Constant>(LocalDeclMap[BVD]);
48813733577d33ec56479667b49e1bff42dc6bba90Lauro Ramos Venancio}
495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
50dde0a94120915fa925d1ffcdb997c7b44dc9fa21Anders Carlssonllvm::Value *CodeGenFunction::GetAddrOfLocalVar(const VarDecl *VD)
51dde0a94120915fa925d1ffcdb997c7b44dc9fa21Anders Carlsson{
52dde0a94120915fa925d1ffcdb997c7b44dc9fa21Anders Carlsson  return LocalDeclMap[VD];
53dde0a94120915fa925d1ffcdb997c7b44dc9fa21Anders Carlsson}
54dde0a94120915fa925d1ffcdb997c7b44dc9fa21Anders Carlsson
558b1a343b6b360d63d5dc8a6beb841ce4414c1e00Daniel Dunbarconst llvm::Type *CodeGenFunction::ConvertTypeForMem(QualType T) {
568b1a343b6b360d63d5dc8a6beb841ce4414c1e00Daniel Dunbar  return CGM.getTypes().ConvertTypeForMem(T);
578b1a343b6b360d63d5dc8a6beb841ce4414c1e00Daniel Dunbar}
588b1a343b6b360d63d5dc8a6beb841ce4414c1e00Daniel Dunbar
595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerconst llvm::Type *CodeGenFunction::ConvertType(QualType T) {
605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return CGM.getTypes().ConvertType(T);
615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
634111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattnerbool CodeGenFunction::isObjCPointerType(QualType T) {
644111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner  // All Objective-C types are pointers.
654111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner  return T->isObjCInterfaceType() ||
664111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner    T->isObjCQualifiedInterfaceType() || T->isObjCQualifiedIdType();
675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
694111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattnerbool CodeGenFunction::hasAggregateLLVMType(QualType T) {
70a782ca76cc7dfdd69e331a4fa722fc23b18d5c34Daniel Dunbar  // FIXME: Use positive checks instead of negative ones to be more
71a782ca76cc7dfdd69e331a4fa722fc23b18d5c34Daniel Dunbar  // robust in the face of extension.
724111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner  return !isObjCPointerType(T) &&!T->isRealType() && !T->isPointerLikeType() &&
73a782ca76cc7dfdd69e331a4fa722fc23b18d5c34Daniel Dunbar    !T->isVoidType() && !T->isVectorType() && !T->isFunctionType() &&
74a782ca76cc7dfdd69e331a4fa722fc23b18d5c34Daniel Dunbar    !T->isBlockPointerType();
754111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner}
76391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner
771c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbarvoid CodeGenFunction::EmitReturnBlock() {
781c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // For cleanliness, we try to avoid emitting the return block for
791c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // simple cases.
801c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
811c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
821c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  if (CurBB) {
831c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar    assert(!CurBB->getTerminator() && "Unexpected terminated block.");
841c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
851c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar    // We have a valid insert point, reuse it if there are no explicit
861c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar    // jumps to the return block.
871c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar    if (ReturnBlock->use_empty())
881c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      delete ReturnBlock;
891c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar    else
901c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      EmitBlock(ReturnBlock);
911c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar    return;
921c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  }
931c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
941c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // Otherwise, if the return block is the target of a single direct
951c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // branch then we can just put the code in that block instead. This
961c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // cleans up functions which started with a unified return block.
971c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  if (ReturnBlock->hasOneUse()) {
981c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar    llvm::BranchInst *BI =
991c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      dyn_cast<llvm::BranchInst>(*ReturnBlock->use_begin());
1001c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar    if (BI && BI->isUnconditional() && BI->getSuccessor(0) == ReturnBlock) {
1011c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      // Reset insertion point and delete the branch.
1021c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      Builder.SetInsertPoint(BI->getParent());
1031c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      BI->eraseFromParent();
1041c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      delete ReturnBlock;
1051c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      return;
1061c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar    }
1071c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  }
1081c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
1091c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // FIXME: We are at an unreachable point, there is no reason to emit
1101c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // the block unless it has uses. However, we still need a place to
1111c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // put the debug region.end for now.
1121c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
1131c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  EmitBlock(ReturnBlock);
1141c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar}
1151c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
116af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbarvoid CodeGenFunction::FinishFunction(SourceLocation EndLoc) {
1170ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar  // Finish emission of indirect switches.
1180ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar  EmitIndirectSwitches();
1190ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar
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!");
126bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson
1271c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // 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);
1375ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar
138391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  // Remove the AllocaInsertPt instruction, which is just a convenience for us.
139391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  AllocaInsertPt->eraseFromParent();
140391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  AllocaInsertPt = 0;
141c8aa5f1f264fb230c38182adab944232bb160c2bChris Lattner}
142c8aa5f1f264fb230c38182adab944232bb160c2bChris Lattner
1437c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbarvoid CodeGenFunction::StartFunction(const Decl *D, QualType RetTy,
1447c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar                                    llvm::Function *Fn,
1452284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar                                    const FunctionArgList &Args,
1462284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar                                    SourceLocation StartLoc) {
1474cc1a4703363ff940b6273aeef9d96a87edeb04bAnders Carlsson  DidCallStackSave = false;
1487c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar  CurFuncDecl = D;
1497c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar  FnRetTy = RetTy;
150bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  CurFn = Fn;
1515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(CurFn->isDeclaration() && "Function already has body?");
152ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner
15355e874299f2ad827646a4ca9ea38c402aaeb38c9Daniel Dunbar  llvm::BasicBlock *EntryBB = createBasicBlock("entry", CurFn);
1545ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar
1555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Create a marker to make it easy to insert allocas into the entryblock
15655352a2d616cf9fbb621d10faf8b960b4b268bd8Chris Lattner  // later.  Don't create this with the builder, because we don't want it
15755352a2d616cf9fbb621d10faf8b960b4b268bd8Chris Lattner  // folded.
1585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty);
15955352a2d616cf9fbb621d10faf8b960b4b268bd8Chris Lattner  AllocaInsertPt = new llvm::BitCastInst(Undef, llvm::Type::Int32Ty, "allocapt",
16055352a2d616cf9fbb621d10faf8b960b4b268bd8Chris Lattner                                         EntryBB);
1615ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar
16255e874299f2ad827646a4ca9ea38c402aaeb38c9Daniel Dunbar  ReturnBlock = createBasicBlock("return");
1635ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar  ReturnValue = 0;
1647c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar  if (!RetTy->isVoidType())
1657c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar    ReturnValue = CreateTempAlloca(ConvertType(RetTy), "retval");
1665ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar
16755352a2d616cf9fbb621d10faf8b960b4b268bd8Chris Lattner  Builder.SetInsertPoint(EntryBB);
1684111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner
169af99417156c652a6f04dff643925036dc3241d60Sanjiv Gupta  // Emit subprogram debug descriptor.
1707c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar  // FIXME: The cast here is a huge hack.
171e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson  if (CGDebugInfo *DI = getDebugInfo()) {
1722284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar    DI->setLocation(StartLoc);
1732284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar    if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
1746ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor      DI->EmitFunctionStart(CGM.getMangledName(FD), RetTy, CurFn, Builder);
1752284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar    } else {
1762284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar      // Just use LLVM function name.
1772284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar      DI->EmitFunctionStart(Fn->getName().c_str(),
1782284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar                            RetTy, CurFn, Builder);
179af99417156c652a6f04dff643925036dc3241d60Sanjiv Gupta    }
180af99417156c652a6f04dff643925036dc3241d60Sanjiv Gupta  }
181af99417156c652a6f04dff643925036dc3241d60Sanjiv Gupta
18288b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar  // FIXME: Leaked.
183541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar  CurFnInfo = &CGM.getTypes().getFunctionInfo(FnRetTy, Args);
18488b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar  EmitFunctionProlog(*CurFnInfo, CurFn, Args);
185751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson
186751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson  // If any of the arguments have a variably modified type, make sure to
187751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson  // emit the type size.
188751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson  for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
189751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson       i != e; ++i) {
190751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson    QualType Ty = i->second;
191751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson
192751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson    if (Ty->isVariablyModifiedType())
193751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson      EmitVLASize(Ty);
194751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson  }
1957c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar}
196eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman
1977c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbarvoid CodeGenFunction::GenerateCode(const FunctionDecl *FD,
1987c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar                                   llvm::Function *Fn) {
199e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson  // Check if we should generate debug info for this function.
200e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson  if (CGM.getDebugInfo() && !FD->getAttr<NodebugAttr>())
201e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson    DebugInfo = CGM.getDebugInfo();
202e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson
2037c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar  FunctionArgList Args;
204eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman  if (FD->getNumParams()) {
205eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman    const FunctionTypeProto* FProto = FD->getType()->getAsFunctionTypeProto();
206eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman    assert(FProto && "Function def must have prototype!");
2077c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar
2087c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar    for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i)
2097c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar      Args.push_back(std::make_pair(FD->getParamDecl(i),
2107c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar                                    FProto->getArgType(i)));
2115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
212af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
2132284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar  StartFunction(FD, FD->getResultType(), Fn, Args,
2142284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar                cast<CompoundStmt>(FD->getBody())->getLBracLoc());
2157c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar
216af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar  EmitStmt(FD->getBody());
217af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
218af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar  const CompoundStmt *S = dyn_cast<CompoundStmt>(FD->getBody());
219af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar  if (S) {
220af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar    FinishFunction(S->getRBracLoc());
221af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar  } else {
222af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar    FinishFunction();
223af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar  }
2245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
2255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2260946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner/// ContainsLabel - Return true if the statement contains a label in it.  If
2270946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner/// this statement is not executed normally, it not containing a label means
2280946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner/// that we can just remove the code.
2290946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattnerbool CodeGenFunction::ContainsLabel(const Stmt *S, bool IgnoreCaseStmts) {
2300946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // Null statement, not a label!
2310946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  if (S == 0) return false;
2320946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner
2330946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // If this is a label, we have to emit the code, consider something like:
2340946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // if (0) {  ...  foo:  bar(); }  goto foo;
2350946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  if (isa<LabelStmt>(S))
2360946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner    return true;
2370946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner
2380946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // If this is a case/default statement, and we haven't seen a switch, we have
2390946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // to emit the code.
2400946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  if (isa<SwitchCase>(S) && !IgnoreCaseStmts)
2410946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner    return true;
2420946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner
2430946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // If this is a switch statement, we want to ignore cases below it.
2440946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  if (isa<SwitchStmt>(S))
2450946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner    IgnoreCaseStmts = true;
2460946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner
2470946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // Scan subexpressions for verboten labels.
2480946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
2490946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner       I != E; ++I)
2500946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner    if (ContainsLabel(*I, IgnoreCaseStmts))
2510946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner      return true;
2520946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner
2530946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  return false;
2540946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner}
2550946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner
25631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
25731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// ConstantFoldsToSimpleInteger - If the sepcified expression does not fold to
25831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// a constant, or if it does but contains a label, return 0.  If it constant
25931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// folds to 'true' and does not contain a label, return 1, if it constant folds
26031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// to 'false' and does not contain a label, return -1.
26131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattnerint CodeGenFunction::ConstantFoldsToSimpleInteger(const Expr *Cond) {
26236bc14c3a1cf63ee306df5687ac8e85f924f8639Daniel Dunbar  // FIXME: Rename and handle conversion of other evaluatable things
26336bc14c3a1cf63ee306df5687ac8e85f924f8639Daniel Dunbar  // to bool.
26464712f196bffd41fb0552c2643b07a25c3e45082Anders Carlsson  Expr::EvalResult Result;
26564712f196bffd41fb0552c2643b07a25c3e45082Anders Carlsson  if (!Cond->Evaluate(Result, getContext()) || !Result.Val.isInt() ||
26664712f196bffd41fb0552c2643b07a25c3e45082Anders Carlsson      Result.HasSideEffects)
267ef5a66d8171eb95e948107f8ee7707b360aaff25Anders Carlsson    return 0;  // Not foldable, not integer or not fully evaluatable.
26831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
26931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  if (CodeGenFunction::ContainsLabel(Cond))
27031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner    return 0;  // Contains a label.
27131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
27264712f196bffd41fb0552c2643b07a25c3e45082Anders Carlsson  return Result.Val.getInt().getBoolValue() ? 1 : -1;
27331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner}
27431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
27531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
27631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an if
27731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// statement) to the specified blocks.  Based on the condition, this might try
27831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// to simplify the codegen of the conditional based on the branch.
27931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner///
28031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattnervoid CodeGenFunction::EmitBranchOnBoolExpr(const Expr *Cond,
28131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner                                           llvm::BasicBlock *TrueBlock,
28231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner                                           llvm::BasicBlock *FalseBlock) {
28331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  if (const ParenExpr *PE = dyn_cast<ParenExpr>(Cond))
28431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner    return EmitBranchOnBoolExpr(PE->getSubExpr(), TrueBlock, FalseBlock);
28531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
28631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  if (const BinaryOperator *CondBOp = dyn_cast<BinaryOperator>(Cond)) {
28731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner    // Handle X && Y in a condition.
28831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner    if (CondBOp->getOpcode() == BinaryOperator::LAnd) {
28931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // If we have "1 && X", simplify the code.  "0 && X" would have constant
29031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // folded if the case was simple enough.
29131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == 1) {
29231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        // br(1 && X) -> br(X).
29331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
29431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      }
29531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
29631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // If we have "X && 1", simplify the code to use an uncond branch.
29731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // "X && 0" would have been constant folded to 0.
29831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == 1) {
29931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        // br(X && 1) -> br(X).
30031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock);
30131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      }
30231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
30331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // Emit the LHS as a conditional.  If the LHS conditional is false, we
30431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // want to jump to the FalseBlock.
3059615ecb44f549ae9fa2b4db6ff46bc78befbf62cDaniel Dunbar      llvm::BasicBlock *LHSTrue = createBasicBlock("land.lhs.true");
30631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBranchOnBoolExpr(CondBOp->getLHS(), LHSTrue, FalseBlock);
30731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBlock(LHSTrue);
30831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
30931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
31031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      return;
31131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner    } else if (CondBOp->getOpcode() == BinaryOperator::LOr) {
31231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // If we have "0 || X", simplify the code.  "1 || X" would have constant
31331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // folded if the case was simple enough.
31431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == -1) {
31531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        // br(0 || X) -> br(X).
31631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
31731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      }
31831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
31931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // If we have "X || 0", simplify the code to use an uncond branch.
32031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // "X || 1" would have been constant folded to 1.
32131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == -1) {
32231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        // br(X || 0) -> br(X).
32331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock);
32431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      }
32531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
32631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // Emit the LHS as a conditional.  If the LHS conditional is true, we
32731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // want to jump to the TrueBlock.
3289615ecb44f549ae9fa2b4db6ff46bc78befbf62cDaniel Dunbar      llvm::BasicBlock *LHSFalse = createBasicBlock("lor.lhs.false");
32931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, LHSFalse);
33031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBlock(LHSFalse);
33131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
33231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
33331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      return;
33431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner    }
335552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner  }
336552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner
337552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner  if (const UnaryOperator *CondUOp = dyn_cast<UnaryOperator>(Cond)) {
338552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner    // br(!x, t, f) -> br(x, f, t)
339552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner    if (CondUOp->getOpcode() == UnaryOperator::LNot)
340552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner      return EmitBranchOnBoolExpr(CondUOp->getSubExpr(), FalseBlock, TrueBlock);
34131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  }
34231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
34309b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar  if (const ConditionalOperator *CondOp = dyn_cast<ConditionalOperator>(Cond)) {
34409b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar    // Handle ?: operator.
34509b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar
34609b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar    // Just ignore GNU ?: extension.
34709b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar    if (CondOp->getLHS()) {
34809b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      // br(c ? x : y, t, f) -> br(c, br(x, t, f), br(y, t, f))
34909b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true");
35009b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false");
35109b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      EmitBranchOnBoolExpr(CondOp->getCond(), LHSBlock, RHSBlock);
35209b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      EmitBlock(LHSBlock);
35309b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      EmitBranchOnBoolExpr(CondOp->getLHS(), TrueBlock, FalseBlock);
35409b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      EmitBlock(RHSBlock);
35509b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      EmitBranchOnBoolExpr(CondOp->getRHS(), TrueBlock, FalseBlock);
35609b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      return;
35709b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar    }
35809b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar  }
35909b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar
36031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  // Emit the code with the fully general case.
36131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  llvm::Value *CondV = EvaluateExprAsBool(Cond);
36231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  Builder.CreateCondBr(CondV, TrueBlock, FalseBlock);
36331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner}
36431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
36588a981b47c7face1b1fdaa9074256245107b9ca9Devang Patel/// getCGRecordLayout - Return record layout info.
36688a981b47c7face1b1fdaa9074256245107b9ca9Devang Patelconst CGRecordLayout *CodeGenFunction::getCGRecordLayout(CodeGenTypes &CGT,
367af31913e48c96fddb45a0fd33f25617546502cbbChris Lattner                                                         QualType Ty) {
368af31913e48c96fddb45a0fd33f25617546502cbbChris Lattner  const RecordType *RTy = Ty->getAsRecordType();
369af31913e48c96fddb45a0fd33f25617546502cbbChris Lattner  assert (RTy && "Unexpected type. RecordType expected here.");
370b84a06e68ffd71da22e3c75b6e4bbdba37816413Devang Patel
371af31913e48c96fddb45a0fd33f25617546502cbbChris Lattner  return CGT.getCGRecordLayout(RTy->getDecl());
372b84a06e68ffd71da22e3c75b6e4bbdba37816413Devang Patel}
373dc5e8268292046114ffe02e48773572a91a310f1Chris Lattner
374488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar/// ErrorUnsupported - Print out an error that codegen doesn't support the
375dc5e8268292046114ffe02e48773572a91a310f1Chris Lattner/// specified stmt yet.
37690df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbarvoid CodeGenFunction::ErrorUnsupported(const Stmt *S, const char *Type,
37790df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar                                       bool OmitOnError) {
37890df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar  CGM.ErrorUnsupported(S, Type, OmitOnError);
379dc5e8268292046114ffe02e48773572a91a310f1Chris Lattner}
380dc5e8268292046114ffe02e48773572a91a310f1Chris Lattner
3810ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbarunsigned CodeGenFunction::GetIDForAddrOfLabel(const LabelStmt *L) {
3820ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar  // Use LabelIDs.size() as the new ID if one hasn't been assigned.
3830ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar  return LabelIDs.insert(std::make_pair(L, LabelIDs.size())).first->second;
3840ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar}
3850ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar
3863d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlssonvoid CodeGenFunction::EmitMemSetToZero(llvm::Value *DestPtr, QualType Ty)
3873d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson{
3883d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson  const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
3893d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson  if (DestPtr->getType() != BP)
3903d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson    DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
3913d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson
3923d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson  // Get size and alignment info for this aggregate.
3933d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson  std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty);
3943d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson
3953d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson  // FIXME: Handle variable sized types.
3963d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson  const llvm::Type *IntPtr = llvm::IntegerType::get(LLVMPointerWidth);
3973d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson
3983d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson  Builder.CreateCall4(CGM.getMemSetFn(), DestPtr,
3993d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson                      llvm::ConstantInt::getNullValue(llvm::Type::Int8Ty),
4003d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson                      // TypeInfo.first describes size in bits.
4013d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson                      llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
4023d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson                      llvm::ConstantInt::get(llvm::Type::Int32Ty,
4033d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson                                             TypeInfo.second/8));
4043d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson}
4053d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson
4060ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbarvoid CodeGenFunction::EmitIndirectSwitches() {
4070ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar  llvm::BasicBlock *Default;
4080ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar
40976526a5245ec2283b0c85fcef507d4c2dec90715Daniel Dunbar  if (IndirectSwitches.empty())
41076526a5245ec2283b0c85fcef507d4c2dec90715Daniel Dunbar    return;
41176526a5245ec2283b0c85fcef507d4c2dec90715Daniel Dunbar
4120ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar  if (!LabelIDs.empty()) {
4130ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar    Default = getBasicBlockForLabel(LabelIDs.begin()->first);
4140ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar  } else {
4150ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar    // No possible targets for indirect goto, just emit an infinite
4160ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar    // loop.
41755e874299f2ad827646a4ca9ea38c402aaeb38c9Daniel Dunbar    Default = createBasicBlock("indirectgoto.loop", CurFn);
4180ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar    llvm::BranchInst::Create(Default, Default);
4190ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar  }
4200ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar
4210ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar  for (std::vector<llvm::SwitchInst*>::iterator i = IndirectSwitches.begin(),
4220ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar         e = IndirectSwitches.end(); i != e; ++i) {
4230ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar    llvm::SwitchInst *I = *i;
4240ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar
4250ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar    I->setSuccessor(0, Default);
4260ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar    for (std::map<const LabelStmt*,unsigned>::iterator LI = LabelIDs.begin(),
4270ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar           LE = LabelIDs.end(); LI != LE; ++LI) {
4280ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar      I->addCase(llvm::ConstantInt::get(llvm::Type::Int32Ty,
4290ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar                                        LI->second),
4300ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar                 getBasicBlockForLabel(LI->first));
4310ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar    }
4320ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar  }
4330ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar}
434ddf7cac45d85b73127adbbd91a2b28fc7291c57eAnders Carlsson
435dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlssonllvm::Value *CodeGenFunction::GetVLASize(const VariableArrayType *VAT)
436dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlsson{
437dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlsson  llvm::Value *&SizeEntry = VLASizeMap[VAT];
438dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlsson
439f666b7780d04186521adcaedb0e15dfa4d5e6933Anders Carlsson  assert(SizeEntry && "Did not emit size for type");
440f666b7780d04186521adcaedb0e15dfa4d5e6933Anders Carlsson  return SizeEntry;
441f666b7780d04186521adcaedb0e15dfa4d5e6933Anders Carlsson}
442dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlsson
44360d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlssonllvm::Value *CodeGenFunction::EmitVLASize(QualType Ty)
444f666b7780d04186521adcaedb0e15dfa4d5e6933Anders Carlsson{
44560d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson  assert(Ty->isVariablyModifiedType() &&
44660d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson         "Must pass variably modified type to EmitVLASizes!");
447f666b7780d04186521adcaedb0e15dfa4d5e6933Anders Carlsson
44860d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson  if (const VariableArrayType *VAT = getContext().getAsVariableArrayType(Ty)) {
44960d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson    llvm::Value *&SizeEntry = VLASizeMap[VAT];
45060d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson
451fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson    if (!SizeEntry) {
452fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson      // Get the element size;
453fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson      llvm::Value *ElemSize;
45460d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson
455fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson      QualType ElemTy = VAT->getElementType();
45696f214776c0f69069fee4d67557c8c7f416009a8Anders Carlsson
45796f214776c0f69069fee4d67557c8c7f416009a8Anders Carlsson      const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
45896f214776c0f69069fee4d67557c8c7f416009a8Anders Carlsson
459fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson      if (ElemTy->isVariableArrayType())
460fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson        ElemSize = EmitVLASize(ElemTy);
461fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson      else {
46296f214776c0f69069fee4d67557c8c7f416009a8Anders Carlsson        ElemSize = llvm::ConstantInt::get(SizeTy,
463fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson                                          getContext().getTypeSize(ElemTy) / 8);
464fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson      }
46560d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson
466fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson      llvm::Value *NumElements = EmitScalarExpr(VAT->getSizeExpr());
46796f214776c0f69069fee4d67557c8c7f416009a8Anders Carlsson      NumElements = Builder.CreateIntCast(NumElements, SizeTy, false, "tmp");
46896f214776c0f69069fee4d67557c8c7f416009a8Anders Carlsson
469fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson      SizeEntry = Builder.CreateMul(ElemSize, NumElements);
470fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson    }
47160d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson
47260d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson    return SizeEntry;
47360d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson  } else if (const PointerType *PT = Ty->getAsPointerType())
47460d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson    EmitVLASize(PT->getPointeeType());
475f666b7780d04186521adcaedb0e15dfa4d5e6933Anders Carlsson  else {
47660d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson    assert(0 && "unknown VM type!");
477dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlsson  }
47860d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson
47960d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson  return 0;
480dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlsson}
4814fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman
4824fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedmanllvm::Value* CodeGenFunction::EmitVAListRef(const Expr* E) {
4834fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman  if (CGM.getContext().getBuiltinVaListType()->isArrayType()) {
4844fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman    return EmitScalarExpr(E);
4854fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman  }
4864fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman  return EmitLValue(E).getAddress();
4874fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman}
4886ccc47698d0311ddabf32fa0f6db8e4f09ac96f8Anders Carlsson
4896fc559136b8ef98bfb824a0fd49df385405f2879Anders Carlssonvoid CodeGenFunction::PushCleanupBlock(llvm::BasicBlock *CleanupBlock)
4906ccc47698d0311ddabf32fa0f6db8e4f09ac96f8Anders Carlsson{
4916ccc47698d0311ddabf32fa0f6db8e4f09ac96f8Anders Carlsson  CleanupEntries.push_back(CleanupEntry(CleanupBlock));
4926ccc47698d0311ddabf32fa0f6db8e4f09ac96f8Anders Carlsson}
493c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson
494c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlssonvoid CodeGenFunction::EmitCleanupBlocks(size_t OldCleanupStackSize)
495c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson{
496c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson  assert(CleanupEntries.size() >= OldCleanupStackSize &&
497c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson         "Cleanup stack mismatch!");
498c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson
499c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson  while (CleanupEntries.size() > OldCleanupStackSize)
500c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson    EmitCleanupBlock();
501c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson}
502c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson
503bb66f9f2e454135b86462d121629275b6ac38e96Anders CarlssonCodeGenFunction::CleanupBlockInfo CodeGenFunction::PopCleanupBlock()
504c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson{
505bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  CleanupEntry &CE = CleanupEntries.back();
506bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson
507bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  llvm::BasicBlock *CleanupBlock = CE.CleanupBlock;
508bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson
509bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  std::vector<llvm::BasicBlock *> Blocks;
510bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  std::swap(Blocks, CE.Blocks);
511bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson
512bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  std::vector<llvm::BranchInst *> BranchFixups;
513bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  std::swap(BranchFixups, CE.BranchFixups);
514bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson
515bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  CleanupEntries.pop_back();
516bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson
517ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson  // Check if any branch fixups pointed to the scope we just popped. If so,
518ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson  // we can remove them.
519ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson  for (size_t i = 0, e = BranchFixups.size(); i != e; ++i) {
520ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson    llvm::BasicBlock *Dest = BranchFixups[i]->getSuccessor(0);
521ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson    BlockScopeMap::iterator I = BlockScopes.find(Dest);
522d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson
523ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson    if (I == BlockScopes.end())
524ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson      continue;
5251093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson
526ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson    assert(I->second <= CleanupEntries.size() && "Invalid branch fixup!");
527d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson
528ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson    if (I->second == CleanupEntries.size()) {
529ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson      // We don't need to do this branch fixup.
530ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson      BranchFixups[i] = BranchFixups.back();
531ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson      BranchFixups.pop_back();
532ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson      i--;
533ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson      e--;
534ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson      continue;
5351093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson    }
5361093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson  }
537d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson
538bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  llvm::BasicBlock *SwitchBlock = 0;
539bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  llvm::BasicBlock *EndBlock = 0;
5401093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson  if (!BranchFixups.empty()) {
541bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson    SwitchBlock = createBasicBlock("cleanup.switch");
542bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson    EndBlock = createBasicBlock("cleanup.end");
5431093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson
544bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson    llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
545bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson
546bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson    Builder.SetInsertPoint(SwitchBlock);
547bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson
5481093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson    llvm::Value *DestCodePtr = CreateTempAlloca(llvm::Type::Int32Ty,
5491093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson                                                "cleanup.dst");
5501093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson    llvm::Value *DestCode = Builder.CreateLoad(DestCodePtr, "tmp");
5511093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson
5521093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson    // Create a switch instruction to determine where to jump next.
553bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson    llvm::SwitchInst *SI = Builder.CreateSwitch(DestCode, EndBlock,
5541093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson                                                BranchFixups.size());
555bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson
55646831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson    // Restore the current basic block (if any)
55746831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson    if (CurBB)
55846831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson      Builder.SetInsertPoint(CurBB);
55946831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson    else
56046831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson      Builder.ClearInsertionPoint();
561bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson
5621093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson    for (size_t i = 0, e = BranchFixups.size(); i != e; ++i) {
5631093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson      llvm::BranchInst *BI = BranchFixups[i];
5641093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson      llvm::BasicBlock *Dest = BI->getSuccessor(0);
5651093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson
5661093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson      // Fixup the branch instruction to point to the cleanup block.
5671093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson      BI->setSuccessor(0, CleanupBlock);
568d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson
5691093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson      if (CleanupEntries.empty()) {
570cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        llvm::ConstantInt *ID;
571cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson
572cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        // Check if we already have a destination for this block.
573cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        if (Dest == SI->getDefaultDest())
574cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson          ID = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
575cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        else {
576cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson          ID = SI->findCaseDest(Dest);
577cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson          if (!ID) {
578cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson            // No code found, get a new unique one by using the number of
579cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson            // switch successors.
580cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson            ID = llvm::ConstantInt::get(llvm::Type::Int32Ty,
581cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson                                        SI->getNumSuccessors());
582cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson            SI->addCase(ID, Dest);
583cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson          }
584cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        }
585cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson
586cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        // Store the jump destination before the branch instruction.
587cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        new llvm::StoreInst(ID, DestCodePtr, BI);
5881093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson      } else {
5891093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson        // We need to jump through another cleanup block. Create a pad block
5901093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson        // with a branch instruction that jumps to the final destination and
5911093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson        // add it as a branch fixup to the current cleanup scope.
5921093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson
5931093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson        // Create the pad block.
5941093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson        llvm::BasicBlock *CleanupPad = createBasicBlock("cleanup.pad", CurFn);
595cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson
596cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        // Create a unique case ID.
597cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        llvm::ConstantInt *ID = llvm::ConstantInt::get(llvm::Type::Int32Ty,
598cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson                                                       SI->getNumSuccessors());
599cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson
600cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        // Store the jump destination before the branch instruction.
601cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        new llvm::StoreInst(ID, DestCodePtr, BI);
602cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson
6031093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson        // Add it as the destination.
604cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        SI->addCase(ID, CleanupPad);
6051093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson
6061093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson        // Create the branch to the final destination.
6071093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson        llvm::BranchInst *BI = llvm::BranchInst::Create(Dest);
6081093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson        CleanupPad->getInstList().push_back(BI);
6091093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson
6101093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson        // And add it as a branch fixup.
6111093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson        CleanupEntries.back().BranchFixups.push_back(BI);
6121093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson      }
6131093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson    }
6141093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson  }
615d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson
616bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson  // Remove all blocks from the block scope map.
617bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson  for (size_t i = 0, e = Blocks.size(); i != e; ++i) {
618bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson    assert(BlockScopes.count(Blocks[i]) &&
619bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson           "Did not find block in scope map!");
620bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson
621bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson    BlockScopes.erase(Blocks[i]);
622bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson  }
623d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson
624bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  return CleanupBlockInfo(CleanupBlock, SwitchBlock, EndBlock);
625d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson}
626d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson
627d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlssonvoid CodeGenFunction::EmitCleanupBlock()
628d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson{
629bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  CleanupBlockInfo Info = PopCleanupBlock();
630d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson
631bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  EmitBlock(Info.CleanupBlock);
632d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson
633bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  if (Info.SwitchBlock)
634bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson    EmitBlock(Info.SwitchBlock);
635bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  if (Info.EndBlock)
636bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson    EmitBlock(Info.EndBlock);
637d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson}
638d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson
63987eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlssonvoid CodeGenFunction::AddBranchFixup(llvm::BranchInst *BI)
64087eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson{
64187eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  assert(!CleanupEntries.empty() &&
64287eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson         "Trying to add branch fixup without cleanup block!");
64387eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson
64487eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  // FIXME: We could be more clever here and check if there's already a
64587eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  // branch fixup for this destination and recycle it.
64687eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  CleanupEntries.back().BranchFixups.push_back(BI);
64787eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson}
64887eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson
64987eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlssonvoid CodeGenFunction::EmitBranchThroughCleanup(llvm::BasicBlock *Dest)
65087eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson{
65146831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson  if (!HaveInsertPoint())
65246831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson    return;
65346831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson
65487eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  llvm::BranchInst* BI = Builder.CreateBr(Dest);
65587eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson
65646831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson  Builder.ClearInsertionPoint();
65746831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson
65887eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  // The stack is empty, no need to do any cleanup.
65987eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  if (CleanupEntries.empty())
66087eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson    return;
66187eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson
66287eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  if (!Dest->getParent()) {
66387eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson    // We are trying to branch to a block that hasn't been inserted yet.
66487eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson    AddBranchFixup(BI);
66587eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson    return;
66687eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  }
66787eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson
66887eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  BlockScopeMap::iterator I = BlockScopes.find(Dest);
66987eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  if (I == BlockScopes.end()) {
67087eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson    // We are trying to jump to a block that is outside of any cleanup scope.
67187eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson    AddBranchFixup(BI);
67287eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson    return;
67387eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  }
67487eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson
67587eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  assert(I->second < CleanupEntries.size() &&
67687eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson         "Trying to branch into cleanup region");
67787eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson
67887eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  if (I->second == CleanupEntries.size() - 1) {
67987eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson    // We have a branch to a block in the same scope.
68087eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson    return;
68187eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  }
68287eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson
68387eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  AddBranchFixup(BI);
68487eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson}
685