CodeGenFunction.cpp revision af4403545a50a60d208e6fcae057308d576a92e0
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- CodeGenFunction.cpp - Emit LLVM Code from ASTs for a Function ----===//
25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//                     The LLVM Compiler Infrastructure
45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details.
75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// This coordinates the per-function state used while generating code.
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "CodeGenFunction.h"
155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "CodeGenModule.h"
163f2af1002249c8acc9ce17f1fc50324864feb8e1Eli Friedman#include "CGDebugInfo.h"
175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Basic/TargetInfo.h"
1831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner#include "clang/AST/APValue.h"
19de7fb8413b13651fd85b7125d08b3c9ac2816d9dDaniel Dunbar#include "clang/AST/ASTContext.h"
20c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel Dunbar#include "clang/AST/Decl.h"
212b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson#include "clang/AST/DeclCXX.h"
226a1e0eb557d47e85185e09bdf8721f53f4bf9c9cMike Stump#include "clang/AST/StmtCXX.h"
234e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump#include "llvm/Target/TargetData.h"
245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace CodeGen;
265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
271eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpCodeGenFunction::CodeGenFunction(CodeGenModule &cgm)
28a4f668f3b7e03629066a01b04e415cb2b4655dafMike Stump  : BlockFunction(cgm, *this, Builder), CGM(cgm),
29a4f668f3b7e03629066a01b04e415cb2b4655dafMike Stump    Target(CGM.getContext().Target),
30aac8705c046f01a264a4f82832895a5d9e695633Owen Anderson    Builder(cgm.getModule().getContext()),
31d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    DebugInfo(0), IndirectBranch(0),
323d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner    SwitchInsn(0), CaseRangeBlock(0), InvokeDest(0),
332504941793b549323f9d29c62507cf21d865fadeJohn McCall    CXXThisDecl(0), CXXThisValue(0), CXXVTTDecl(0), CXXVTTValue(0),
3415037caa1542bb810ad54c653aeb80f61df7b00cMike Stump    ConditionalBranchLevel(0), TerminateHandler(0), TrapBB(0),
35be07f60131bc6f8d6696f4644df1ef667a1730d5Mike Stump    UniqueAggrDestructorCount(0) {
364e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump  LLVMIntTy = ConvertType(getContext().IntTy);
374e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump  LLVMPointerWidth = Target.getPointerWidth(0);
38d88ea5687968640ada2bc5a10211cbeb68a671ecMike Stump  Exceptions = getContext().getLangOptions().Exceptions;
399c276ae0f24d4cee8f7954069d4b8eae45d0447dMike Stump  CatchUndefined = getContext().getLangOptions().CatchUndefined;
404111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner}
415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerASTContext &CodeGenFunction::getContext() const {
435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return CGM.getContext();
445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerllvm::BasicBlock *CodeGenFunction::getBasicBlockForLabel(const LabelStmt *S) {
485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::BasicBlock *&BB = LabelMap[S];
495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (BB) return BB;
501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Create, but don't insert, the new block.
5255e874299f2ad827646a4ca9ea38c402aaeb38c9Daniel Dunbar  return BB = createBasicBlock(S->getName());
535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
550096acf421c4609ce7f43e8b05f8c5ca866d4611Daniel Dunbarllvm::Value *CodeGenFunction::GetAddrOfLocalVar(const VarDecl *VD) {
560096acf421c4609ce7f43e8b05f8c5ca866d4611Daniel Dunbar  llvm::Value *Res = LocalDeclMap[VD];
570096acf421c4609ce7f43e8b05f8c5ca866d4611Daniel Dunbar  assert(Res && "Invalid argument to GetAddrOfLocalVar(), no decl!");
580096acf421c4609ce7f43e8b05f8c5ca866d4611Daniel Dunbar  return Res;
59813733577d33ec56479667b49e1bff42dc6bba90Lauro Ramos Venancio}
605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
610096acf421c4609ce7f43e8b05f8c5ca866d4611Daniel Dunbarllvm::Constant *
620096acf421c4609ce7f43e8b05f8c5ca866d4611Daniel DunbarCodeGenFunction::GetAddrOfStaticLocalVar(const VarDecl *BVD) {
630096acf421c4609ce7f43e8b05f8c5ca866d4611Daniel Dunbar  return cast<llvm::Constant>(GetAddrOfLocalVar(BVD));
64dde0a94120915fa925d1ffcdb997c7b44dc9fa21Anders Carlsson}
65dde0a94120915fa925d1ffcdb997c7b44dc9fa21Anders Carlsson
668b1a343b6b360d63d5dc8a6beb841ce4414c1e00Daniel Dunbarconst llvm::Type *CodeGenFunction::ConvertTypeForMem(QualType T) {
678b1a343b6b360d63d5dc8a6beb841ce4414c1e00Daniel Dunbar  return CGM.getTypes().ConvertTypeForMem(T);
688b1a343b6b360d63d5dc8a6beb841ce4414c1e00Daniel Dunbar}
698b1a343b6b360d63d5dc8a6beb841ce4414c1e00Daniel Dunbar
705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerconst llvm::Type *CodeGenFunction::ConvertType(QualType T) {
715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return CGM.getTypes().ConvertType(T);
725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
744111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattnerbool CodeGenFunction::hasAggregateLLVMType(QualType T) {
75e9d34dc7afe06c9adaacad7a678a0cbbf749ea75Anders Carlsson  return T->isRecordType() || T->isArrayType() || T->isAnyComplexType() ||
76e9d34dc7afe06c9adaacad7a678a0cbbf749ea75Anders Carlsson    T->isMemberFunctionPointerType();
774111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner}
78391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner
791c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbarvoid CodeGenFunction::EmitReturnBlock() {
801c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // For cleanliness, we try to avoid emitting the return block for
811c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // simple cases.
821c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
831c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
841c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  if (CurBB) {
851c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar    assert(!CurBB->getTerminator() && "Unexpected terminated block.");
861c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
8796e18b05ea6b55aa92a1a576f29e9cee73a7e20bDaniel Dunbar    // We have a valid insert point, reuse it if it is empty or there are no
8896e18b05ea6b55aa92a1a576f29e9cee73a7e20bDaniel Dunbar    // explicit jumps to the return block.
8996e18b05ea6b55aa92a1a576f29e9cee73a7e20bDaniel Dunbar    if (CurBB->empty() || ReturnBlock->use_empty()) {
9096e18b05ea6b55aa92a1a576f29e9cee73a7e20bDaniel Dunbar      ReturnBlock->replaceAllUsesWith(CurBB);
911c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      delete ReturnBlock;
9296e18b05ea6b55aa92a1a576f29e9cee73a7e20bDaniel Dunbar    } else
931c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      EmitBlock(ReturnBlock);
941c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar    return;
951c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  }
961c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
971c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // Otherwise, if the return block is the target of a single direct
981c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // branch then we can just put the code in that block instead. This
991c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // cleans up functions which started with a unified return block.
1001c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  if (ReturnBlock->hasOneUse()) {
1011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    llvm::BranchInst *BI =
1021c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      dyn_cast<llvm::BranchInst>(*ReturnBlock->use_begin());
1031c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar    if (BI && BI->isUnconditional() && BI->getSuccessor(0) == ReturnBlock) {
1041c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      // Reset insertion point and delete the branch.
1051c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      Builder.SetInsertPoint(BI->getParent());
1061c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      BI->eraseFromParent();
1071c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      delete ReturnBlock;
1081c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      return;
1091c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar    }
1101c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  }
1111c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
112f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // FIXME: We are at an unreachable point, there is no reason to emit the block
113f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // unless it has uses. However, we still need a place to put the debug
114f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // region.end for now.
1151c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
1161c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  EmitBlock(ReturnBlock);
1171c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar}
1181c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
119af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbarvoid CodeGenFunction::FinishFunction(SourceLocation EndLoc) {
120391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  assert(BreakContinueStack.empty() &&
121391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner         "mismatched push/pop in break/continue stack!");
122bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson  assert(BlockScopes.empty() &&
123bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson         "did not remove all blocks from block scope map!");
124bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson  assert(CleanupEntries.empty() &&
125bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson         "mismatched push/pop in cleanup stack!");
1261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Emit function epilog (to return).
1281c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  EmitReturnBlock();
129f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar
130f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar  // Emit debug descriptor for function end.
131e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson  if (CGDebugInfo *DI = getDebugInfo()) {
132f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar    DI->setLocation(EndLoc);
133f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar    DI->EmitRegionEnd(CurFn, Builder);
134f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar  }
135f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar
13688b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar  EmitFunctionEpilog(*CurFnInfo, ReturnValue);
137cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump  EmitEndEHSpec(CurCodeDecl);
1385ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar
139d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // If someone did an indirect goto, emit the indirect goto block at the end of
140d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // the function.
141d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  if (IndirectBranch) {
142d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    EmitBlock(IndirectBranch->getParent());
143d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    Builder.ClearInsertionPoint();
144d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  }
145d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner
146391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  // Remove the AllocaInsertPt instruction, which is just a convenience for us.
147481769b5dc102b0256b35581e787909ad5edfab5Chris Lattner  llvm::Instruction *Ptr = AllocaInsertPt;
148391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  AllocaInsertPt = 0;
149481769b5dc102b0256b35581e787909ad5edfab5Chris Lattner  Ptr->eraseFromParent();
150d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner
151d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // If someone took the address of a label but never did an indirect goto, we
152d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // made a zero entry PHI node, which is illegal, zap it now.
153d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  if (IndirectBranch) {
154d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    llvm::PHINode *PN = cast<llvm::PHINode>(IndirectBranch->getAddress());
155d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    if (PN->getNumIncomingValues() == 0) {
156d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner      PN->replaceAllUsesWith(llvm::UndefValue::get(PN->getType()));
157d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner      PN->eraseFromParent();
158d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    }
159d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  }
160c8aa5f1f264fb230c38182adab944232bb160c2bChris Lattner}
161c8aa5f1f264fb230c38182adab944232bb160c2bChris Lattner
1620ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlssonvoid CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy,
1637c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar                                    llvm::Function *Fn,
1642284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar                                    const FunctionArgList &Args,
1652284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar                                    SourceLocation StartLoc) {
1660ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlsson  const Decl *D = GD.getDecl();
1670ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlsson
1684cc1a4703363ff940b6273aeef9d96a87edeb04bAnders Carlsson  DidCallStackSave = false;
169b5437d238752dc297e42410e98d38d5250fe0463Chris Lattner  CurCodeDecl = CurFuncDecl = D;
1707c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar  FnRetTy = RetTy;
171bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  CurFn = Fn;
1725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(CurFn->isDeclaration() && "Function already has body?");
173ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner
174a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen  // Pass inline keyword to optimizer if it appears explicitly on any
175a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen  // declaration.
176a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen  if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
177a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen    for (FunctionDecl::redecl_iterator RI = FD->redecls_begin(),
178a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen           RE = FD->redecls_end(); RI != RE; ++RI)
179a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen      if (RI->isInlineSpecified()) {
180a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen        Fn->addFnAttr(llvm::Attribute::InlineHint);
181a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen        break;
182a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen      }
183a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen
18455e874299f2ad827646a4ca9ea38c402aaeb38c9Daniel Dunbar  llvm::BasicBlock *EntryBB = createBasicBlock("entry", CurFn);
1855ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar
1865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Create a marker to make it easy to insert allocas into the entryblock
18755352a2d616cf9fbb621d10faf8b960b4b268bd8Chris Lattner  // later.  Don't create this with the builder, because we don't want it
18855352a2d616cf9fbb621d10faf8b960b4b268bd8Chris Lattner  // folded.
1890032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson  llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::getInt32Ty(VMContext));
190bcdc0f0aff015b6507560f50c71d1b7b6c6a9932Mike Stump  AllocaInsertPt = new llvm::BitCastInst(Undef,
191bcdc0f0aff015b6507560f50c71d1b7b6c6a9932Mike Stump                                         llvm::Type::getInt32Ty(VMContext), "",
19255352a2d616cf9fbb621d10faf8b960b4b268bd8Chris Lattner                                         EntryBB);
193f1466848dce9c4c75d96a6cabdc8db560e26aac8Chris Lattner  if (Builder.isNamePreserving())
194f1466848dce9c4c75d96a6cabdc8db560e26aac8Chris Lattner    AllocaInsertPt->setName("allocapt");
1951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19655e874299f2ad827646a4ca9ea38c402aaeb38c9Daniel Dunbar  ReturnBlock = createBasicBlock("return");
1971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19855352a2d616cf9fbb621d10faf8b960b4b268bd8Chris Lattner  Builder.SetInsertPoint(EntryBB);
1991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
200ce056bcaa1c97b89a4b2de2112c62d060863be2bDouglas Gregor  QualType FnType = getContext().getFunctionType(RetTy, 0, 0, false, 0,
201ce056bcaa1c97b89a4b2de2112c62d060863be2bDouglas Gregor                                                 false, false, 0, 0,
202ce056bcaa1c97b89a4b2de2112c62d060863be2bDouglas Gregor                                                 /*FIXME?*/false,
203ce056bcaa1c97b89a4b2de2112c62d060863be2bDouglas Gregor                                                 /*FIXME?*/CC_Default);
20491cc815ffd13d4a78ae1b5bd617e19dd555de4f4Mike Stump
205af99417156c652a6f04dff643925036dc3241d60Sanjiv Gupta  // Emit subprogram debug descriptor.
206e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson  if (CGDebugInfo *DI = getDebugInfo()) {
2072284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar    DI->setLocation(StartLoc);
2089c6c3a0e3ae09626d2d4b04e4ffa42c3d7cab32bDevang Patel    DI->EmitFunctionStart(GD, FnType, CurFn, Builder);
209af99417156c652a6f04dff643925036dc3241d60Sanjiv Gupta  }
210af99417156c652a6f04dff643925036dc3241d60Sanjiv Gupta
21188b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar  // FIXME: Leaked.
21204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  // CC info is ignored, hopefully?
21304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  CurFnInfo = &CGM.getTypes().getFunctionInfo(FnRetTy, Args,
21404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall                                              CC_Default, false);
215b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman
216b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman  if (RetTy->isVoidType()) {
217b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman    // Void type; nothing to return.
218b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman    ReturnValue = 0;
219b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman  } else if (CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect &&
220b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman             hasAggregateLLVMType(CurFnInfo->getReturnType())) {
221b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman    // Indirect aggregate return; emit returned value directly into sret slot.
222647a1ec397fa13af176d07d9f5d071560a94c7a9Daniel Dunbar    // This reduces code size, and affects correctness in C++.
223b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman    ReturnValue = CurFn->arg_begin();
224b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman  } else {
225647a1ec397fa13af176d07d9f5d071560a94c7a9Daniel Dunbar    ReturnValue = CreateIRTemp(RetTy, "retval");
226b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman  }
227b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman
228cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump  EmitStartEHSpec(CurCodeDecl);
22988b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar  EmitFunctionProlog(*CurFnInfo, CurFn, Args);
2301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2312504941793b549323f9d29c62507cf21d865fadeJohn McCall  if (CXXThisDecl)
2322504941793b549323f9d29c62507cf21d865fadeJohn McCall    CXXThisValue = Builder.CreateLoad(LocalDeclMap[CXXThisDecl], "this");
2332504941793b549323f9d29c62507cf21d865fadeJohn McCall  if (CXXVTTDecl)
2342504941793b549323f9d29c62507cf21d865fadeJohn McCall    CXXVTTValue = Builder.CreateLoad(LocalDeclMap[CXXVTTDecl], "vtt");
2352504941793b549323f9d29c62507cf21d865fadeJohn McCall
236751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson  // If any of the arguments have a variably modified type, make sure to
237751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson  // emit the type size.
238751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson  for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
239751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson       i != e; ++i) {
240751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson    QualType Ty = i->second;
241751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson
242751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson    if (Ty->isVariablyModifiedType())
243751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson      EmitVLASize(Ty);
244751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson  }
2457c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar}
246eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman
2479fc6a7774643a810c8501dae2323e863fefb623eJohn McCallvoid CodeGenFunction::EmitFunctionBody(FunctionArgList &Args) {
2489fc6a7774643a810c8501dae2323e863fefb623eJohn McCall  const FunctionDecl *FD = cast<FunctionDecl>(CurGD.getDecl());
249a355e07454463b19829ac92ffd115a097faff0e0John McCall
250a355e07454463b19829ac92ffd115a097faff0e0John McCall  Stmt *Body = FD->getBody();
2519fc6a7774643a810c8501dae2323e863fefb623eJohn McCall  if (Body)
252a355e07454463b19829ac92ffd115a097faff0e0John McCall    EmitStmt(Body);
2539fc6a7774643a810c8501dae2323e863fefb623eJohn McCall  else {
2549fc6a7774643a810c8501dae2323e863fefb623eJohn McCall    assert(FD->isImplicit() && "non-implicit function def has no body");
2559fc6a7774643a810c8501dae2323e863fefb623eJohn McCall    assert(FD->isCopyAssignment() && "implicit function not copy assignment");
2569fc6a7774643a810c8501dae2323e863fefb623eJohn McCall    SynthesizeCXXCopyAssignment(Args);
257a355e07454463b19829ac92ffd115a097faff0e0John McCall  }
258a355e07454463b19829ac92ffd115a097faff0e0John McCall}
259a355e07454463b19829ac92ffd115a097faff0e0John McCall
260c997d4278d329e18891aac9698fb991b2d4622ebAnders Carlssonvoid CodeGenFunction::GenerateCode(GlobalDecl GD, llvm::Function *Fn) {
2610ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlsson  const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
2620ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlsson
263e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson  // Check if we should generate debug info for this function.
2641feade8e520be483293dbf55eb57a51720899589Mike Stump  if (CGM.getDebugInfo() && !FD->hasAttr<NoDebugAttr>())
265e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson    DebugInfo = CGM.getDebugInfo();
2661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2677c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar  FunctionArgList Args;
2681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2696a1e0eb557d47e85185e09bdf8721f53f4bf9c9cMike Stump  CurGD = GD;
2702b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson  if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
2712b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson    if (MD->isInstance()) {
2722b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson      // Create the implicit 'this' decl.
2732b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson      // FIXME: I'm not entirely sure I like using a fake decl just for code
2742b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson      // generation. Maybe we can come up with a better way?
2752504941793b549323f9d29c62507cf21d865fadeJohn McCall      CXXThisDecl = ImplicitParamDecl::Create(getContext(), 0,
2762504941793b549323f9d29c62507cf21d865fadeJohn McCall                                              FD->getLocation(),
2771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                              &getContext().Idents.get("this"),
2782b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson                                              MD->getThisType(getContext()));
2792b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson      Args.push_back(std::make_pair(CXXThisDecl, CXXThisDecl->getType()));
280f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson
281f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson      // Check if we need a VTT parameter as well.
282af4403545a50a60d208e6fcae057308d576a92e0Anders Carlsson      if (CodeGenVTables::needsVTTParameter(GD)) {
283f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson        // FIXME: The comment about using a fake decl above applies here too.
284f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson        QualType T = getContext().getPointerType(getContext().VoidPtrTy);
285f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson        CXXVTTDecl =
2862504941793b549323f9d29c62507cf21d865fadeJohn McCall          ImplicitParamDecl::Create(getContext(), 0, FD->getLocation(),
287f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson                                    &getContext().Idents.get("vtt"), T);
288f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson        Args.push_back(std::make_pair(CXXVTTDecl, CXXVTTDecl->getType()));
289f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson      }
2902b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson    }
2912b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson  }
2921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
293eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman  if (FD->getNumParams()) {
294183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    const FunctionProtoType* FProto = FD->getType()->getAs<FunctionProtoType>();
295eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman    assert(FProto && "Function def must have prototype!");
2967c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar
2977c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar    for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i)
2981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Args.push_back(std::make_pair(FD->getParamDecl(i),
2997c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar                                    FProto->getArgType(i)));
3005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
301af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
302a355e07454463b19829ac92ffd115a097faff0e0John McCall  SourceRange BodyRange;
303a355e07454463b19829ac92ffd115a097faff0e0John McCall  if (Stmt *Body = FD->getBody()) BodyRange = Body->getSourceRange();
3044365bba95be15342575b4f030c6583a770a5da3dAnders Carlsson
305a355e07454463b19829ac92ffd115a097faff0e0John McCall  // Emit the standard function prologue.
306a355e07454463b19829ac92ffd115a097faff0e0John McCall  StartFunction(GD, FD->getResultType(), Fn, Args, BodyRange.getBegin());
3071851a12605bc6f1ea70d11974a315340ebaab6ebAnders Carlsson
308a355e07454463b19829ac92ffd115a097faff0e0John McCall  // Generate the body of the function.
3099fc6a7774643a810c8501dae2323e863fefb623eJohn McCall  if (isa<CXXDestructorDecl>(FD))
3109fc6a7774643a810c8501dae2323e863fefb623eJohn McCall    EmitDestructorBody(Args);
3119fc6a7774643a810c8501dae2323e863fefb623eJohn McCall  else if (isa<CXXConstructorDecl>(FD))
3129fc6a7774643a810c8501dae2323e863fefb623eJohn McCall    EmitConstructorBody(Args);
3139fc6a7774643a810c8501dae2323e863fefb623eJohn McCall  else
3149fc6a7774643a810c8501dae2323e863fefb623eJohn McCall    EmitFunctionBody(Args);
315c33e4ba20304e222692e77f2c6ad26a5d8d32f83Anders Carlsson
316a355e07454463b19829ac92ffd115a097faff0e0John McCall  // Emit the standard function epilogue.
317a355e07454463b19829ac92ffd115a097faff0e0John McCall  FinishFunction(BodyRange.getEnd());
3181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3192b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson  // Destroy the 'this' declaration.
3202b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson  if (CXXThisDecl)
3212b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson    CXXThisDecl->Destroy(getContext());
322f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson
323f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson  // Destroy the VTT declaration.
324f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson  if (CXXVTTDecl)
325f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson    CXXVTTDecl->Destroy(getContext());
3265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
3275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3280946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner/// ContainsLabel - Return true if the statement contains a label in it.  If
3290946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner/// this statement is not executed normally, it not containing a label means
3300946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner/// that we can just remove the code.
3310946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattnerbool CodeGenFunction::ContainsLabel(const Stmt *S, bool IgnoreCaseStmts) {
3320946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // Null statement, not a label!
3330946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  if (S == 0) return false;
3341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3350946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // If this is a label, we have to emit the code, consider something like:
3360946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // if (0) {  ...  foo:  bar(); }  goto foo;
3370946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  if (isa<LabelStmt>(S))
3380946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner    return true;
3391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3400946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // If this is a case/default statement, and we haven't seen a switch, we have
3410946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // to emit the code.
3420946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  if (isa<SwitchCase>(S) && !IgnoreCaseStmts)
3430946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner    return true;
3441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3450946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // If this is a switch statement, we want to ignore cases below it.
3460946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  if (isa<SwitchStmt>(S))
3470946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner    IgnoreCaseStmts = true;
3481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3490946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // Scan subexpressions for verboten labels.
3500946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
3510946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner       I != E; ++I)
3520946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner    if (ContainsLabel(*I, IgnoreCaseStmts))
3530946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner      return true;
3541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3550946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  return false;
3560946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner}
3570946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner
35831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
35931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// ConstantFoldsToSimpleInteger - If the sepcified expression does not fold to
36031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// a constant, or if it does but contains a label, return 0.  If it constant
36131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// folds to 'true' and does not contain a label, return 1, if it constant folds
36231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// to 'false' and does not contain a label, return -1.
36331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattnerint CodeGenFunction::ConstantFoldsToSimpleInteger(const Expr *Cond) {
36436bc14c3a1cf63ee306df5687ac8e85f924f8639Daniel Dunbar  // FIXME: Rename and handle conversion of other evaluatable things
36536bc14c3a1cf63ee306df5687ac8e85f924f8639Daniel Dunbar  // to bool.
36664712f196bffd41fb0552c2643b07a25c3e45082Anders Carlsson  Expr::EvalResult Result;
3671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!Cond->Evaluate(Result, getContext()) || !Result.Val.isInt() ||
36864712f196bffd41fb0552c2643b07a25c3e45082Anders Carlsson      Result.HasSideEffects)
369ef5a66d8171eb95e948107f8ee7707b360aaff25Anders Carlsson    return 0;  // Not foldable, not integer or not fully evaluatable.
3701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
37131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  if (CodeGenFunction::ContainsLabel(Cond))
37231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner    return 0;  // Contains a label.
3731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
37464712f196bffd41fb0552c2643b07a25c3e45082Anders Carlsson  return Result.Val.getInt().getBoolValue() ? 1 : -1;
37531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner}
37631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
37731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
37831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an if
37931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// statement) to the specified blocks.  Based on the condition, this might try
38031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// to simplify the codegen of the conditional based on the branch.
38131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner///
38231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattnervoid CodeGenFunction::EmitBranchOnBoolExpr(const Expr *Cond,
38331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner                                           llvm::BasicBlock *TrueBlock,
38431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner                                           llvm::BasicBlock *FalseBlock) {
38531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  if (const ParenExpr *PE = dyn_cast<ParenExpr>(Cond))
38631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner    return EmitBranchOnBoolExpr(PE->getSubExpr(), TrueBlock, FalseBlock);
3871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
38831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  if (const BinaryOperator *CondBOp = dyn_cast<BinaryOperator>(Cond)) {
38931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner    // Handle X && Y in a condition.
39031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner    if (CondBOp->getOpcode() == BinaryOperator::LAnd) {
39131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // If we have "1 && X", simplify the code.  "0 && X" would have constant
39231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // folded if the case was simple enough.
39331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == 1) {
39431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        // br(1 && X) -> br(X).
39531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
39631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      }
3971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
39831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // If we have "X && 1", simplify the code to use an uncond branch.
39931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // "X && 0" would have been constant folded to 0.
40031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == 1) {
40131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        // br(X && 1) -> br(X).
40231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock);
40331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      }
4041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
40531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // Emit the LHS as a conditional.  If the LHS conditional is false, we
40631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // want to jump to the FalseBlock.
4079615ecb44f549ae9fa2b4db6ff46bc78befbf62cDaniel Dunbar      llvm::BasicBlock *LHSTrue = createBasicBlock("land.lhs.true");
40831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBranchOnBoolExpr(CondBOp->getLHS(), LHSTrue, FalseBlock);
40931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBlock(LHSTrue);
4101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
41108e9e453f40aff95a59bd67db49b8f050765e1f0Anders Carlsson      // Any temporaries created here are conditional.
41272119a887c4af39c90f10b6bdc158389071f42eaAnders Carlsson      BeginConditionalBranch();
41331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
41472119a887c4af39c90f10b6bdc158389071f42eaAnders Carlsson      EndConditionalBranch();
41508e9e453f40aff95a59bd67db49b8f050765e1f0Anders Carlsson
41631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      return;
41731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner    } else if (CondBOp->getOpcode() == BinaryOperator::LOr) {
41831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // If we have "0 || X", simplify the code.  "1 || X" would have constant
41931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // folded if the case was simple enough.
42031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == -1) {
42131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        // br(0 || X) -> br(X).
42231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
42331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      }
4241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
42531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // If we have "X || 0", simplify the code to use an uncond branch.
42631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // "X || 1" would have been constant folded to 1.
42731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == -1) {
42831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        // br(X || 0) -> br(X).
42931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock);
43031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      }
4311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
43231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // Emit the LHS as a conditional.  If the LHS conditional is true, we
43331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // want to jump to the TrueBlock.
4349615ecb44f549ae9fa2b4db6ff46bc78befbf62cDaniel Dunbar      llvm::BasicBlock *LHSFalse = createBasicBlock("lor.lhs.false");
43531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, LHSFalse);
43631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBlock(LHSFalse);
4371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
43808e9e453f40aff95a59bd67db49b8f050765e1f0Anders Carlsson      // Any temporaries created here are conditional.
43972119a887c4af39c90f10b6bdc158389071f42eaAnders Carlsson      BeginConditionalBranch();
44031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
44172119a887c4af39c90f10b6bdc158389071f42eaAnders Carlsson      EndConditionalBranch();
44208e9e453f40aff95a59bd67db49b8f050765e1f0Anders Carlsson
44331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      return;
44431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner    }
445552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner  }
4461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
447552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner  if (const UnaryOperator *CondUOp = dyn_cast<UnaryOperator>(Cond)) {
448552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner    // br(!x, t, f) -> br(x, f, t)
449552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner    if (CondUOp->getOpcode() == UnaryOperator::LNot)
450552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner      return EmitBranchOnBoolExpr(CondUOp->getSubExpr(), FalseBlock, TrueBlock);
45131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  }
4521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
45309b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar  if (const ConditionalOperator *CondOp = dyn_cast<ConditionalOperator>(Cond)) {
45409b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar    // Handle ?: operator.
45509b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar
45609b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar    // Just ignore GNU ?: extension.
45709b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar    if (CondOp->getLHS()) {
45809b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      // br(c ? x : y, t, f) -> br(c, br(x, t, f), br(y, t, f))
45909b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true");
46009b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false");
46109b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      EmitBranchOnBoolExpr(CondOp->getCond(), LHSBlock, RHSBlock);
46209b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      EmitBlock(LHSBlock);
46309b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      EmitBranchOnBoolExpr(CondOp->getLHS(), TrueBlock, FalseBlock);
46409b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      EmitBlock(RHSBlock);
46509b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      EmitBranchOnBoolExpr(CondOp->getRHS(), TrueBlock, FalseBlock);
46609b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      return;
46709b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar    }
46809b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar  }
46909b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar
47031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  // Emit the code with the fully general case.
47131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  llvm::Value *CondV = EvaluateExprAsBool(Cond);
47231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  Builder.CreateCondBr(CondV, TrueBlock, FalseBlock);
47331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner}
47431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
475488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar/// ErrorUnsupported - Print out an error that codegen doesn't support the
476dc5e8268292046114ffe02e48773572a91a310f1Chris Lattner/// specified stmt yet.
47790df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbarvoid CodeGenFunction::ErrorUnsupported(const Stmt *S, const char *Type,
47890df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar                                       bool OmitOnError) {
47990df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar  CGM.ErrorUnsupported(S, Type, OmitOnError);
480dc5e8268292046114ffe02e48773572a91a310f1Chris Lattner}
481dc5e8268292046114ffe02e48773572a91a310f1Chris Lattner
48288207c9ca218486b93ae7df14e9764cd0c2c3383Chris Lattnervoid CodeGenFunction::EmitMemSetToZero(llvm::Value *DestPtr, QualType Ty) {
48336afd38fb749ab34c8341a1dc70366bca904eaa3Chris Lattner  const llvm::Type *BP = llvm::Type::getInt8PtrTy(VMContext);
4843d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson  if (DestPtr->getType() != BP)
4853d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson    DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
4863d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson
4873d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson  // Get size and alignment info for this aggregate.
4883d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson  std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty);
4893d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson
49088207c9ca218486b93ae7df14e9764cd0c2c3383Chris Lattner  // Don't bother emitting a zero-byte memset.
49188207c9ca218486b93ae7df14e9764cd0c2c3383Chris Lattner  if (TypeInfo.first == 0)
49288207c9ca218486b93ae7df14e9764cd0c2c3383Chris Lattner    return;
4931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4943d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson  // FIXME: Handle variable sized types.
4951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  const llvm::Type *IntPtr = llvm::IntegerType::get(VMContext,
4960032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson                                                    LLVMPointerWidth);
4973d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson
4983d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson  Builder.CreateCall4(CGM.getMemSetFn(), DestPtr,
4990032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson                 llvm::Constant::getNullValue(llvm::Type::getInt8Ty(VMContext)),
5003d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson                      // TypeInfo.first describes size in bits.
5014a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson                      llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
5021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                      llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
5033d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson                                             TypeInfo.second/8));
5043d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson}
5053d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson
506d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattnerllvm::BlockAddress *CodeGenFunction::GetAddrOfLabel(const LabelStmt *L) {
507d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // Make sure that there is a block for the indirect goto.
508d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  if (IndirectBranch == 0)
509d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    GetIndirectGotoBlock();
5103d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner
511d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  llvm::BasicBlock *BB = getBasicBlockForLabel(L);
5123d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner
513d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // Make sure the indirect branch includes all of the address-taken blocks.
514d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  IndirectBranch->addDestination(BB);
515d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  return llvm::BlockAddress::get(CurFn, BB);
5163d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner}
5171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5183d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattnerllvm::BasicBlock *CodeGenFunction::GetIndirectGotoBlock() {
519d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // If we already made the indirect branch for indirect goto, return its block.
520d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  if (IndirectBranch) return IndirectBranch->getParent();
5213d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner
522d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  CGBuilderTy TmpBuilder(createBasicBlock("indirectgoto"));
5233d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner
524d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
52585e74acfcfb0c835a2e6c1adab314e997917039aChris Lattner
5263d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner  // Create the PHI node that indirect gotos will add entries to.
527d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  llvm::Value *DestVal = TmpBuilder.CreatePHI(Int8PtrTy, "indirect.goto.dest");
5283d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner
529d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // Create the indirect branch instruction.
530d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  IndirectBranch = TmpBuilder.CreateIndirectBr(DestVal);
531d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  return IndirectBranch->getParent();
5320ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar}
533ddf7cac45d85b73127adbbd91a2b28fc7291c57eAnders Carlsson
534d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbarllvm::Value *CodeGenFunction::GetVLASize(const VariableArrayType *VAT) {
535bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman  llvm::Value *&SizeEntry = VLASizeMap[VAT->getSizeExpr()];
5361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
537f666b7780d04186521adcaedb0e15dfa4d5e6933Anders Carlsson  assert(SizeEntry && "Did not emit size for type");
538f666b7780d04186521adcaedb0e15dfa4d5e6933Anders Carlsson  return SizeEntry;
539f666b7780d04186521adcaedb0e15dfa4d5e6933Anders Carlsson}
540dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlsson
541d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbarllvm::Value *CodeGenFunction::EmitVLASize(QualType Ty) {
54260d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson  assert(Ty->isVariablyModifiedType() &&
54360d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson         "Must pass variably modified type to EmitVLASizes!");
5441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
545d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar  EnsureInsertPoint();
5461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
54760d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson  if (const VariableArrayType *VAT = getContext().getAsVariableArrayType(Ty)) {
548bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman    llvm::Value *&SizeEntry = VLASizeMap[VAT->getSizeExpr()];
5491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
550fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson    if (!SizeEntry) {
55196f214776c0f69069fee4d67557c8c7f416009a8Anders Carlsson      const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
5521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
553ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner      // Get the element size;
554ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner      QualType ElemTy = VAT->getElementType();
555ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner      llvm::Value *ElemSize;
556fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson      if (ElemTy->isVariableArrayType())
557fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson        ElemSize = EmitVLASize(ElemTy);
558ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner      else
5594a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson        ElemSize = llvm::ConstantInt::get(SizeTy,
560199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck            getContext().getTypeSizeInChars(ElemTy).getQuantity());
5611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
562fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson      llvm::Value *NumElements = EmitScalarExpr(VAT->getSizeExpr());
56396f214776c0f69069fee4d67557c8c7f416009a8Anders Carlsson      NumElements = Builder.CreateIntCast(NumElements, SizeTy, false, "tmp");
5641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
565fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson      SizeEntry = Builder.CreateMul(ElemSize, NumElements);
566fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson    }
5671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
56860d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson    return SizeEntry;
569dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlsson  }
5701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
571ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner  if (const ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
572ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner    EmitVLASize(AT->getElementType());
573ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner    return 0;
5741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
5751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
576ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner  const PointerType *PT = Ty->getAs<PointerType>();
577ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner  assert(PT && "unknown VM type!");
578ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner  EmitVLASize(PT->getPointeeType());
57960d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson  return 0;
580dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlsson}
5814fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman
5824fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedmanllvm::Value* CodeGenFunction::EmitVAListRef(const Expr* E) {
5834fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman  if (CGM.getContext().getBuiltinVaListType()->isArrayType()) {
5844fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman    return EmitScalarExpr(E);
5854fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman  }
5864fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman  return EmitLValue(E).getAddress();
5874fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman}
5886ccc47698d0311ddabf32fa0f6db8e4f09ac96f8Anders Carlsson
5897799621ad7d16f3c6a1aeda024312d63ecd77777Fariborz Jahanianvoid CodeGenFunction::PushCleanupBlock(llvm::BasicBlock *CleanupEntryBlock,
59099533834ba8f3658559f334e68a518ebb6388ceaMike Stump                                       llvm::BasicBlock *CleanupExitBlock,
591d88ea5687968640ada2bc5a10211cbeb68a671ecMike Stump                                       llvm::BasicBlock *PreviousInvokeDest,
59299533834ba8f3658559f334e68a518ebb6388ceaMike Stump                                       bool EHOnly) {
59399533834ba8f3658559f334e68a518ebb6388ceaMike Stump  CleanupEntries.push_back(CleanupEntry(CleanupEntryBlock, CleanupExitBlock,
594d88ea5687968640ada2bc5a10211cbeb68a671ecMike Stump                                        PreviousInvokeDest, EHOnly));
5956ccc47698d0311ddabf32fa0f6db8e4f09ac96f8Anders Carlsson}
596c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson
5971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid CodeGenFunction::EmitCleanupBlocks(size_t OldCleanupStackSize) {
5981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(CleanupEntries.size() >= OldCleanupStackSize &&
599c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson         "Cleanup stack mismatch!");
6001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
601c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson  while (CleanupEntries.size() > OldCleanupStackSize)
602c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson    EmitCleanupBlock();
603c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson}
604c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson
6051eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpCodeGenFunction::CleanupBlockInfo CodeGenFunction::PopCleanupBlock() {
606bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  CleanupEntry &CE = CleanupEntries.back();
6071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6087799621ad7d16f3c6a1aeda024312d63ecd77777Fariborz Jahanian  llvm::BasicBlock *CleanupEntryBlock = CE.CleanupEntryBlock;
6091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
610bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  std::vector<llvm::BasicBlock *> Blocks;
611bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  std::swap(Blocks, CE.Blocks);
6121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
613bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  std::vector<llvm::BranchInst *> BranchFixups;
614bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  std::swap(BranchFixups, CE.BranchFixups);
6151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
61699533834ba8f3658559f334e68a518ebb6388ceaMike Stump  bool EHOnly = CE.EHOnly;
61799533834ba8f3658559f334e68a518ebb6388ceaMike Stump
618d88ea5687968640ada2bc5a10211cbeb68a671ecMike Stump  setInvokeDest(CE.PreviousInvokeDest);
619d88ea5687968640ada2bc5a10211cbeb68a671ecMike Stump
620bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  CleanupEntries.pop_back();
621bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson
622ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson  // Check if any branch fixups pointed to the scope we just popped. If so,
623ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson  // we can remove them.
624ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson  for (size_t i = 0, e = BranchFixups.size(); i != e; ++i) {
625ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson    llvm::BasicBlock *Dest = BranchFixups[i]->getSuccessor(0);
626ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson    BlockScopeMap::iterator I = BlockScopes.find(Dest);
6271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
628ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson    if (I == BlockScopes.end())
629ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson      continue;
6301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
631ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson    assert(I->second <= CleanupEntries.size() && "Invalid branch fixup!");
6321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
633ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson    if (I->second == CleanupEntries.size()) {
634ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson      // We don't need to do this branch fixup.
635ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson      BranchFixups[i] = BranchFixups.back();
636ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson      BranchFixups.pop_back();
637ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson      i--;
638ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson      e--;
639ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson      continue;
6401093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson    }
6411093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson  }
6421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6437799621ad7d16f3c6a1aeda024312d63ecd77777Fariborz Jahanian  llvm::BasicBlock *SwitchBlock = CE.CleanupExitBlock;
644bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  llvm::BasicBlock *EndBlock = 0;
6451093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson  if (!BranchFixups.empty()) {
6467799621ad7d16f3c6a1aeda024312d63ecd77777Fariborz Jahanian    if (!SwitchBlock)
6477799621ad7d16f3c6a1aeda024312d63ecd77777Fariborz Jahanian      SwitchBlock = createBasicBlock("cleanup.switch");
648bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson    EndBlock = createBasicBlock("cleanup.end");
6491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
650bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson    llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
6511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
652bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson    Builder.SetInsertPoint(SwitchBlock);
653bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson
65499533834ba8f3658559f334e68a518ebb6388ceaMike Stump    llvm::Value *DestCodePtr
65599533834ba8f3658559f334e68a518ebb6388ceaMike Stump      = CreateTempAlloca(llvm::Type::getInt32Ty(VMContext),
65699533834ba8f3658559f334e68a518ebb6388ceaMike Stump                         "cleanup.dst");
6571093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson    llvm::Value *DestCode = Builder.CreateLoad(DestCodePtr, "tmp");
6581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6591093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson    // Create a switch instruction to determine where to jump next.
6601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    llvm::SwitchInst *SI = Builder.CreateSwitch(DestCode, EndBlock,
6611093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson                                                BranchFixups.size());
662bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson
66346831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson    // Restore the current basic block (if any)
6640ae7b2b2f964bd6145d65ef52dc2a28025b2bd06Anders Carlsson    if (CurBB) {
66546831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson      Builder.SetInsertPoint(CurBB);
6661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6670ae7b2b2f964bd6145d65ef52dc2a28025b2bd06Anders Carlsson      // If we had a current basic block, we also need to emit an instruction
6680ae7b2b2f964bd6145d65ef52dc2a28025b2bd06Anders Carlsson      // to initialize the cleanup destination.
6690032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson      Builder.CreateStore(llvm::Constant::getNullValue(llvm::Type::getInt32Ty(VMContext)),
6700ae7b2b2f964bd6145d65ef52dc2a28025b2bd06Anders Carlsson                          DestCodePtr);
6710ae7b2b2f964bd6145d65ef52dc2a28025b2bd06Anders Carlsson    } else
67246831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson      Builder.ClearInsertionPoint();
673bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson
6741093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson    for (size_t i = 0, e = BranchFixups.size(); i != e; ++i) {
6751093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson      llvm::BranchInst *BI = BranchFixups[i];
6761093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson      llvm::BasicBlock *Dest = BI->getSuccessor(0);
6771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6781093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson      // Fixup the branch instruction to point to the cleanup block.
6797799621ad7d16f3c6a1aeda024312d63ecd77777Fariborz Jahanian      BI->setSuccessor(0, CleanupEntryBlock);
6801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6811093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson      if (CleanupEntries.empty()) {
682cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        llvm::ConstantInt *ID;
6831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
684cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        // Check if we already have a destination for this block.
685cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        if (Dest == SI->getDefaultDest())
6860032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson          ID = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 0);
687cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        else {
688cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson          ID = SI->findCaseDest(Dest);
689cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson          if (!ID) {
690cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson            // No code found, get a new unique one by using the number of
691cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson            // switch successors.
6921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump            ID = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
693cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson                                        SI->getNumSuccessors());
694cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson            SI->addCase(ID, Dest);
695cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson          }
696cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        }
6971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
698cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        // Store the jump destination before the branch instruction.
699cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        new llvm::StoreInst(ID, DestCodePtr, BI);
7001093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson      } else {
7011093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson        // We need to jump through another cleanup block. Create a pad block
70299533834ba8f3658559f334e68a518ebb6388ceaMike Stump        // with a branch instruction that jumps to the final destination and add
70399533834ba8f3658559f334e68a518ebb6388ceaMike Stump        // it as a branch fixup to the current cleanup scope.
7041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7051093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson        // Create the pad block.
7061093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson        llvm::BasicBlock *CleanupPad = createBasicBlock("cleanup.pad", CurFn);
707cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson
708cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        // Create a unique case ID.
70999533834ba8f3658559f334e68a518ebb6388ceaMike Stump        llvm::ConstantInt *ID
71099533834ba8f3658559f334e68a518ebb6388ceaMike Stump          = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
71199533834ba8f3658559f334e68a518ebb6388ceaMike Stump                                   SI->getNumSuccessors());
712cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson
713cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        // Store the jump destination before the branch instruction.
714cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        new llvm::StoreInst(ID, DestCodePtr, BI);
715cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson
7161093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson        // Add it as the destination.
717cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        SI->addCase(ID, CleanupPad);
7181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7191093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson        // Create the branch to the final destination.
7201093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson        llvm::BranchInst *BI = llvm::BranchInst::Create(Dest);
7211093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson        CleanupPad->getInstList().push_back(BI);
7221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7231093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson        // And add it as a branch fixup.
7241093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson        CleanupEntries.back().BranchFixups.push_back(BI);
7251093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson      }
7261093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson    }
7271093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson  }
7281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
729bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson  // Remove all blocks from the block scope map.
730bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson  for (size_t i = 0, e = Blocks.size(); i != e; ++i) {
731bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson    assert(BlockScopes.count(Blocks[i]) &&
732bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson           "Did not find block in scope map!");
7331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
734bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson    BlockScopes.erase(Blocks[i]);
735bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson  }
7361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
73799533834ba8f3658559f334e68a518ebb6388ceaMike Stump  return CleanupBlockInfo(CleanupEntryBlock, SwitchBlock, EndBlock, EHOnly);
738d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson}
739d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson
7401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid CodeGenFunction::EmitCleanupBlock() {
741bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  CleanupBlockInfo Info = PopCleanupBlock();
7421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
74399533834ba8f3658559f334e68a518ebb6388ceaMike Stump  if (Info.EHOnly) {
74499533834ba8f3658559f334e68a518ebb6388ceaMike Stump    // FIXME: Add this to the exceptional edge
74599533834ba8f3658559f334e68a518ebb6388ceaMike Stump    if (Info.CleanupBlock->getNumUses() == 0)
74699533834ba8f3658559f334e68a518ebb6388ceaMike Stump      delete Info.CleanupBlock;
74799533834ba8f3658559f334e68a518ebb6388ceaMike Stump    return;
74899533834ba8f3658559f334e68a518ebb6388ceaMike Stump  }
74999533834ba8f3658559f334e68a518ebb6388ceaMike Stump
750eb6437a9b23b3d98a4590ba5557111e14eba2edfAnders Carlsson  llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
7511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (CurBB && !CurBB->getTerminator() &&
752eb6437a9b23b3d98a4590ba5557111e14eba2edfAnders Carlsson      Info.CleanupBlock->getNumUses() == 0) {
753eb6437a9b23b3d98a4590ba5557111e14eba2edfAnders Carlsson    CurBB->getInstList().splice(CurBB->end(), Info.CleanupBlock->getInstList());
754eb6437a9b23b3d98a4590ba5557111e14eba2edfAnders Carlsson    delete Info.CleanupBlock;
7551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  } else
756eb6437a9b23b3d98a4590ba5557111e14eba2edfAnders Carlsson    EmitBlock(Info.CleanupBlock);
7571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
758bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  if (Info.SwitchBlock)
759bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson    EmitBlock(Info.SwitchBlock);
760bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  if (Info.EndBlock)
761bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson    EmitBlock(Info.EndBlock);
762d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson}
763d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson
7641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid CodeGenFunction::AddBranchFixup(llvm::BranchInst *BI) {
7651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(!CleanupEntries.empty() &&
76687eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson         "Trying to add branch fixup without cleanup block!");
7671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
768f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // FIXME: We could be more clever here and check if there's already a branch
769f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // fixup for this destination and recycle it.
77087eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  CleanupEntries.back().BranchFixups.push_back(BI);
77187eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson}
77287eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson
7731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid CodeGenFunction::EmitBranchThroughCleanup(llvm::BasicBlock *Dest) {
77446831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson  if (!HaveInsertPoint())
77546831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson    return;
7761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
77787eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  llvm::BranchInst* BI = Builder.CreateBr(Dest);
7781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
77946831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson  Builder.ClearInsertionPoint();
7801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
78187eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  // The stack is empty, no need to do any cleanup.
78287eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  if (CleanupEntries.empty())
78387eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson    return;
7841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
78587eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  if (!Dest->getParent()) {
78687eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson    // We are trying to branch to a block that hasn't been inserted yet.
78787eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson    AddBranchFixup(BI);
78887eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson    return;
78987eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  }
7901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
79187eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  BlockScopeMap::iterator I = BlockScopes.find(Dest);
79287eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  if (I == BlockScopes.end()) {
79387eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson    // We are trying to jump to a block that is outside of any cleanup scope.
79487eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson    AddBranchFixup(BI);
79587eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson    return;
79687eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  }
7971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
79887eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  assert(I->second < CleanupEntries.size() &&
79987eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson         "Trying to branch into cleanup region");
8001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
80187eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  if (I->second == CleanupEntries.size() - 1) {
80287eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson    // We have a branch to a block in the same scope.
80387eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson    return;
80487eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  }
8051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
80687eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  AddBranchFixup(BI);
80787eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson}
808