CodeGenFunction.cpp revision 35b21b884e5c3447a52a74d7ffaba966b07ac81f
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"
237255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner#include "clang/Frontend/CodeGenOptions.h"
244e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump#include "llvm/Target/TargetData.h"
257255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner#include "llvm/Intrinsics.h"
265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace CodeGen;
285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
291eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpCodeGenFunction::CodeGenFunction(CodeGenModule &cgm)
30a4f668f3b7e03629066a01b04e415cb2b4655dafMike Stump  : BlockFunction(cgm, *this, Builder), CGM(cgm),
31a4f668f3b7e03629066a01b04e415cb2b4655dafMike Stump    Target(CGM.getContext().Target),
32aac8705c046f01a264a4f82832895a5d9e695633Owen Anderson    Builder(cgm.getModule().getContext()),
33d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    DebugInfo(0), IndirectBranch(0),
343d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner    SwitchInsn(0), CaseRangeBlock(0), InvokeDest(0),
352504941793b549323f9d29c62507cf21d865fadeJohn McCall    CXXThisDecl(0), CXXThisValue(0), CXXVTTDecl(0), CXXVTTValue(0),
365687a5cb5941744c42aaaa9a1e7cd6d7c4c07fbfAnders Carlsson    ConditionalBranchLevel(0), TerminateHandler(0), TrapBB(0) {
374e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump  LLVMIntTy = ConvertType(getContext().IntTy);
384e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump  LLVMPointerWidth = Target.getPointerWidth(0);
39d88ea5687968640ada2bc5a10211cbeb68a671ecMike Stump  Exceptions = getContext().getLangOptions().Exceptions;
409c276ae0f24d4cee8f7954069d4b8eae45d0447dMike Stump  CatchUndefined = getContext().getLangOptions().CatchUndefined;
4135415f5132f70ad5097a3514ab84251e10db3664Douglas Gregor  CGM.getMangleContext().startNewFunction();
424111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner}
435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerASTContext &CodeGenFunction::getContext() const {
455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return CGM.getContext();
465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerllvm::BasicBlock *CodeGenFunction::getBasicBlockForLabel(const LabelStmt *S) {
505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::BasicBlock *&BB = LabelMap[S];
515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (BB) return BB;
521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Create, but don't insert, the new block.
5455e874299f2ad827646a4ca9ea38c402aaeb38c9Daniel Dunbar  return BB = createBasicBlock(S->getName());
555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
570096acf421c4609ce7f43e8b05f8c5ca866d4611Daniel Dunbarllvm::Value *CodeGenFunction::GetAddrOfLocalVar(const VarDecl *VD) {
580096acf421c4609ce7f43e8b05f8c5ca866d4611Daniel Dunbar  llvm::Value *Res = LocalDeclMap[VD];
590096acf421c4609ce7f43e8b05f8c5ca866d4611Daniel Dunbar  assert(Res && "Invalid argument to GetAddrOfLocalVar(), no decl!");
600096acf421c4609ce7f43e8b05f8c5ca866d4611Daniel Dunbar  return Res;
61813733577d33ec56479667b49e1bff42dc6bba90Lauro Ramos Venancio}
625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
630096acf421c4609ce7f43e8b05f8c5ca866d4611Daniel Dunbarllvm::Constant *
640096acf421c4609ce7f43e8b05f8c5ca866d4611Daniel DunbarCodeGenFunction::GetAddrOfStaticLocalVar(const VarDecl *BVD) {
650096acf421c4609ce7f43e8b05f8c5ca866d4611Daniel Dunbar  return cast<llvm::Constant>(GetAddrOfLocalVar(BVD));
66dde0a94120915fa925d1ffcdb997c7b44dc9fa21Anders Carlsson}
67dde0a94120915fa925d1ffcdb997c7b44dc9fa21Anders Carlsson
688b1a343b6b360d63d5dc8a6beb841ce4414c1e00Daniel Dunbarconst llvm::Type *CodeGenFunction::ConvertTypeForMem(QualType T) {
698b1a343b6b360d63d5dc8a6beb841ce4414c1e00Daniel Dunbar  return CGM.getTypes().ConvertTypeForMem(T);
708b1a343b6b360d63d5dc8a6beb841ce4414c1e00Daniel Dunbar}
718b1a343b6b360d63d5dc8a6beb841ce4414c1e00Daniel Dunbar
725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerconst llvm::Type *CodeGenFunction::ConvertType(QualType T) {
735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return CGM.getTypes().ConvertType(T);
745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
764111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattnerbool CodeGenFunction::hasAggregateLLVMType(QualType T) {
77e9d34dc7afe06c9adaacad7a678a0cbbf749ea75Anders Carlsson  return T->isRecordType() || T->isArrayType() || T->isAnyComplexType() ||
78e9d34dc7afe06c9adaacad7a678a0cbbf749ea75Anders Carlsson    T->isMemberFunctionPointerType();
794111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner}
80391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner
811c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbarvoid CodeGenFunction::EmitReturnBlock() {
821c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // For cleanliness, we try to avoid emitting the return block for
831c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // simple cases.
841c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
851c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
861c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  if (CurBB) {
871c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar    assert(!CurBB->getTerminator() && "Unexpected terminated block.");
881c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
8996e18b05ea6b55aa92a1a576f29e9cee73a7e20bDaniel Dunbar    // We have a valid insert point, reuse it if it is empty or there are no
9096e18b05ea6b55aa92a1a576f29e9cee73a7e20bDaniel Dunbar    // explicit jumps to the return block.
9196e18b05ea6b55aa92a1a576f29e9cee73a7e20bDaniel Dunbar    if (CurBB->empty() || ReturnBlock->use_empty()) {
9296e18b05ea6b55aa92a1a576f29e9cee73a7e20bDaniel Dunbar      ReturnBlock->replaceAllUsesWith(CurBB);
931c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      delete ReturnBlock;
9496e18b05ea6b55aa92a1a576f29e9cee73a7e20bDaniel Dunbar    } else
951c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      EmitBlock(ReturnBlock);
961c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar    return;
971c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  }
981c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
991c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // Otherwise, if the return block is the target of a single direct
1001c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // branch then we can just put the code in that block instead. This
1011c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // cleans up functions which started with a unified return block.
1021c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  if (ReturnBlock->hasOneUse()) {
1031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    llvm::BranchInst *BI =
1041c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      dyn_cast<llvm::BranchInst>(*ReturnBlock->use_begin());
1051c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar    if (BI && BI->isUnconditional() && BI->getSuccessor(0) == ReturnBlock) {
1061c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      // Reset insertion point and delete the branch.
1071c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      Builder.SetInsertPoint(BI->getParent());
1081c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      BI->eraseFromParent();
1091c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      delete ReturnBlock;
1101c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      return;
1111c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar    }
1121c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  }
1131c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
114f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // FIXME: We are at an unreachable point, there is no reason to emit the block
115f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // unless it has uses. However, we still need a place to put the debug
116f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // region.end for now.
1171c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
1181c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  EmitBlock(ReturnBlock);
1191c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar}
1201c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
121af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbarvoid CodeGenFunction::FinishFunction(SourceLocation EndLoc) {
122391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  assert(BreakContinueStack.empty() &&
123391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner         "mismatched push/pop in break/continue stack!");
124bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson  assert(BlockScopes.empty() &&
125bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson         "did not remove all blocks from block scope map!");
126bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson  assert(CleanupEntries.empty() &&
127bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson         "mismatched push/pop in cleanup stack!");
1281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Emit function epilog (to return).
1301c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  EmitReturnBlock();
131f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar
1327255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  EmitFunctionInstrumentation("__cyg_profile_func_exit");
1337255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner
134f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar  // Emit debug descriptor for function end.
135e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson  if (CGDebugInfo *DI = getDebugInfo()) {
136f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar    DI->setLocation(EndLoc);
137f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar    DI->EmitRegionEnd(CurFn, Builder);
138f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar  }
139f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar
14035b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner  EmitFunctionEpilog(*CurFnInfo);
141cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump  EmitEndEHSpec(CurCodeDecl);
1425ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar
143d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // If someone did an indirect goto, emit the indirect goto block at the end of
144d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // the function.
145d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  if (IndirectBranch) {
146d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    EmitBlock(IndirectBranch->getParent());
147d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    Builder.ClearInsertionPoint();
148d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  }
149d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner
150391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  // Remove the AllocaInsertPt instruction, which is just a convenience for us.
151481769b5dc102b0256b35581e787909ad5edfab5Chris Lattner  llvm::Instruction *Ptr = AllocaInsertPt;
152391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  AllocaInsertPt = 0;
153481769b5dc102b0256b35581e787909ad5edfab5Chris Lattner  Ptr->eraseFromParent();
154d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner
155d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // If someone took the address of a label but never did an indirect goto, we
156d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // made a zero entry PHI node, which is illegal, zap it now.
157d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  if (IndirectBranch) {
158d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    llvm::PHINode *PN = cast<llvm::PHINode>(IndirectBranch->getAddress());
159d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    if (PN->getNumIncomingValues() == 0) {
160d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner      PN->replaceAllUsesWith(llvm::UndefValue::get(PN->getType()));
161d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner      PN->eraseFromParent();
162d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    }
163d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  }
164c8aa5f1f264fb230c38182adab944232bb160c2bChris Lattner}
165c8aa5f1f264fb230c38182adab944232bb160c2bChris Lattner
1667255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner/// ShouldInstrumentFunction - Return true if the current function should be
1677255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner/// instrumented with __cyg_profile_func_* calls
1687255a2d997b15beae82e627052fdb1b2474495c2Chris Lattnerbool CodeGenFunction::ShouldInstrumentFunction() {
1697255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  if (!CGM.getCodeGenOpts().InstrumentFunctions)
1707255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner    return false;
1717255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  if (CurFuncDecl->hasAttr<NoInstrumentFunctionAttr>())
1727255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner    return false;
1737255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  return true;
1747255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner}
1757255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner
1767255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner/// EmitFunctionInstrumentation - Emit LLVM code to call the specified
1777255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner/// instrumentation function with the current function and the call site, if
1787255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner/// function instrumentation is enabled.
1797255a2d997b15beae82e627052fdb1b2474495c2Chris Lattnervoid CodeGenFunction::EmitFunctionInstrumentation(const char *Fn) {
1807255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  if (!ShouldInstrumentFunction())
1817255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner    return;
1827255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner
1838dab6571b2cab96f44d0a1d6e3edbfdb68b7ed6bChris Lattner  const llvm::PointerType *PointerTy;
1847255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  const llvm::FunctionType *FunctionTy;
1857255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  std::vector<const llvm::Type*> ProfileFuncArgs;
1867255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner
1878dab6571b2cab96f44d0a1d6e3edbfdb68b7ed6bChris Lattner  // void __cyg_profile_func_{enter,exit} (void *this_fn, void *call_site);
1888dab6571b2cab96f44d0a1d6e3edbfdb68b7ed6bChris Lattner  PointerTy = llvm::Type::getInt8PtrTy(VMContext);
1898dab6571b2cab96f44d0a1d6e3edbfdb68b7ed6bChris Lattner  ProfileFuncArgs.push_back(PointerTy);
1908dab6571b2cab96f44d0a1d6e3edbfdb68b7ed6bChris Lattner  ProfileFuncArgs.push_back(PointerTy);
1917255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  FunctionTy = llvm::FunctionType::get(
1927255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner    llvm::Type::getVoidTy(VMContext),
1937255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner    ProfileFuncArgs, false);
1947255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner
1957255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  llvm::Constant *F = CGM.CreateRuntimeFunction(FunctionTy, Fn);
1967255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  llvm::CallInst *CallSite = Builder.CreateCall(
1977255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner    CGM.getIntrinsic(llvm::Intrinsic::returnaddress, 0, 0),
1987255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner    llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 0),
1997255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner    "callsite");
2007255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner
2018dab6571b2cab96f44d0a1d6e3edbfdb68b7ed6bChris Lattner  Builder.CreateCall2(F,
2028dab6571b2cab96f44d0a1d6e3edbfdb68b7ed6bChris Lattner                      llvm::ConstantExpr::getBitCast(CurFn, PointerTy),
2038dab6571b2cab96f44d0a1d6e3edbfdb68b7ed6bChris Lattner                      CallSite);
2047255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner}
2057255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner
2060ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlssonvoid CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy,
2077c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar                                    llvm::Function *Fn,
2082284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar                                    const FunctionArgList &Args,
2092284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar                                    SourceLocation StartLoc) {
2100ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlsson  const Decl *D = GD.getDecl();
2110ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlsson
2124cc1a4703363ff940b6273aeef9d96a87edeb04bAnders Carlsson  DidCallStackSave = false;
213b5437d238752dc297e42410e98d38d5250fe0463Chris Lattner  CurCodeDecl = CurFuncDecl = D;
2147c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar  FnRetTy = RetTy;
215bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  CurFn = Fn;
2165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(CurFn->isDeclaration() && "Function already has body?");
217ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner
218a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen  // Pass inline keyword to optimizer if it appears explicitly on any
219a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen  // declaration.
220a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen  if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
221a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen    for (FunctionDecl::redecl_iterator RI = FD->redecls_begin(),
222a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen           RE = FD->redecls_end(); RI != RE; ++RI)
223a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen      if (RI->isInlineSpecified()) {
224a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen        Fn->addFnAttr(llvm::Attribute::InlineHint);
225a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen        break;
226a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen      }
227a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen
22855e874299f2ad827646a4ca9ea38c402aaeb38c9Daniel Dunbar  llvm::BasicBlock *EntryBB = createBasicBlock("entry", CurFn);
2295ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar
2305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Create a marker to make it easy to insert allocas into the entryblock
23155352a2d616cf9fbb621d10faf8b960b4b268bd8Chris Lattner  // later.  Don't create this with the builder, because we don't want it
23255352a2d616cf9fbb621d10faf8b960b4b268bd8Chris Lattner  // folded.
2330032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson  llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::getInt32Ty(VMContext));
234bcdc0f0aff015b6507560f50c71d1b7b6c6a9932Mike Stump  AllocaInsertPt = new llvm::BitCastInst(Undef,
235bcdc0f0aff015b6507560f50c71d1b7b6c6a9932Mike Stump                                         llvm::Type::getInt32Ty(VMContext), "",
23655352a2d616cf9fbb621d10faf8b960b4b268bd8Chris Lattner                                         EntryBB);
237f1466848dce9c4c75d96a6cabdc8db560e26aac8Chris Lattner  if (Builder.isNamePreserving())
238f1466848dce9c4c75d96a6cabdc8db560e26aac8Chris Lattner    AllocaInsertPt->setName("allocapt");
2391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24055e874299f2ad827646a4ca9ea38c402aaeb38c9Daniel Dunbar  ReturnBlock = createBasicBlock("return");
2411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24255352a2d616cf9fbb621d10faf8b960b4b268bd8Chris Lattner  Builder.SetInsertPoint(EntryBB);
2431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
244ce056bcaa1c97b89a4b2de2112c62d060863be2bDouglas Gregor  QualType FnType = getContext().getFunctionType(RetTy, 0, 0, false, 0,
245ce056bcaa1c97b89a4b2de2112c62d060863be2bDouglas Gregor                                                 false, false, 0, 0,
246264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                                 /*FIXME?*/
247264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                                 FunctionType::ExtInfo());
24891cc815ffd13d4a78ae1b5bd617e19dd555de4f4Mike Stump
249af99417156c652a6f04dff643925036dc3241d60Sanjiv Gupta  // Emit subprogram debug descriptor.
250e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson  if (CGDebugInfo *DI = getDebugInfo()) {
2512284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar    DI->setLocation(StartLoc);
2529c6c3a0e3ae09626d2d4b04e4ffa42c3d7cab32bDevang Patel    DI->EmitFunctionStart(GD, FnType, CurFn, Builder);
253af99417156c652a6f04dff643925036dc3241d60Sanjiv Gupta  }
254af99417156c652a6f04dff643925036dc3241d60Sanjiv Gupta
2557255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  EmitFunctionInstrumentation("__cyg_profile_func_enter");
2567255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner
25788b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar  // FIXME: Leaked.
25804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  // CC info is ignored, hopefully?
25904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  CurFnInfo = &CGM.getTypes().getFunctionInfo(FnRetTy, Args,
260264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                              FunctionType::ExtInfo());
261b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman
262b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman  if (RetTy->isVoidType()) {
263b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman    // Void type; nothing to return.
264b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman    ReturnValue = 0;
265b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman  } else if (CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect &&
266b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman             hasAggregateLLVMType(CurFnInfo->getReturnType())) {
267b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman    // Indirect aggregate return; emit returned value directly into sret slot.
268647a1ec397fa13af176d07d9f5d071560a94c7a9Daniel Dunbar    // This reduces code size, and affects correctness in C++.
269b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman    ReturnValue = CurFn->arg_begin();
270b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman  } else {
271647a1ec397fa13af176d07d9f5d071560a94c7a9Daniel Dunbar    ReturnValue = CreateIRTemp(RetTy, "retval");
272b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman  }
273b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman
274cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump  EmitStartEHSpec(CurCodeDecl);
27588b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar  EmitFunctionProlog(*CurFnInfo, CurFn, Args);
2761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2772504941793b549323f9d29c62507cf21d865fadeJohn McCall  if (CXXThisDecl)
2782504941793b549323f9d29c62507cf21d865fadeJohn McCall    CXXThisValue = Builder.CreateLoad(LocalDeclMap[CXXThisDecl], "this");
2792504941793b549323f9d29c62507cf21d865fadeJohn McCall  if (CXXVTTDecl)
2802504941793b549323f9d29c62507cf21d865fadeJohn McCall    CXXVTTValue = Builder.CreateLoad(LocalDeclMap[CXXVTTDecl], "vtt");
2812504941793b549323f9d29c62507cf21d865fadeJohn McCall
282751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson  // If any of the arguments have a variably modified type, make sure to
283751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson  // emit the type size.
284751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson  for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
285751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson       i != e; ++i) {
286751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson    QualType Ty = i->second;
287751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson
288751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson    if (Ty->isVariablyModifiedType())
289751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson      EmitVLASize(Ty);
290751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson  }
2917c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar}
292eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman
2939fc6a7774643a810c8501dae2323e863fefb623eJohn McCallvoid CodeGenFunction::EmitFunctionBody(FunctionArgList &Args) {
2949fc6a7774643a810c8501dae2323e863fefb623eJohn McCall  const FunctionDecl *FD = cast<FunctionDecl>(CurGD.getDecl());
29506a9f3680d22529a2fcf20c52d71cf221d99d910Douglas Gregor  assert(FD->getBody());
29606a9f3680d22529a2fcf20c52d71cf221d99d910Douglas Gregor  EmitStmt(FD->getBody());
297a355e07454463b19829ac92ffd115a097faff0e0John McCall}
298a355e07454463b19829ac92ffd115a097faff0e0John McCall
299c997d4278d329e18891aac9698fb991b2d4622ebAnders Carlssonvoid CodeGenFunction::GenerateCode(GlobalDecl GD, llvm::Function *Fn) {
3000ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlsson  const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
3010ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlsson
302e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson  // Check if we should generate debug info for this function.
3031feade8e520be483293dbf55eb57a51720899589Mike Stump  if (CGM.getDebugInfo() && !FD->hasAttr<NoDebugAttr>())
304e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson    DebugInfo = CGM.getDebugInfo();
3051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3067c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar  FunctionArgList Args;
3071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3086a1e0eb557d47e85185e09bdf8721f53f4bf9c9cMike Stump  CurGD = GD;
3092b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson  if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
3102b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson    if (MD->isInstance()) {
3112b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson      // Create the implicit 'this' decl.
3122b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson      // FIXME: I'm not entirely sure I like using a fake decl just for code
3132b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson      // generation. Maybe we can come up with a better way?
3142504941793b549323f9d29c62507cf21d865fadeJohn McCall      CXXThisDecl = ImplicitParamDecl::Create(getContext(), 0,
3152504941793b549323f9d29c62507cf21d865fadeJohn McCall                                              FD->getLocation(),
3161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                              &getContext().Idents.get("this"),
3172b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson                                              MD->getThisType(getContext()));
3182b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson      Args.push_back(std::make_pair(CXXThisDecl, CXXThisDecl->getType()));
319f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson
320f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson      // Check if we need a VTT parameter as well.
321af4403545a50a60d208e6fcae057308d576a92e0Anders Carlsson      if (CodeGenVTables::needsVTTParameter(GD)) {
322f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson        // FIXME: The comment about using a fake decl above applies here too.
323f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson        QualType T = getContext().getPointerType(getContext().VoidPtrTy);
324f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson        CXXVTTDecl =
3252504941793b549323f9d29c62507cf21d865fadeJohn McCall          ImplicitParamDecl::Create(getContext(), 0, FD->getLocation(),
326f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson                                    &getContext().Idents.get("vtt"), T);
327f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson        Args.push_back(std::make_pair(CXXVTTDecl, CXXVTTDecl->getType()));
328f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson      }
3292b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson    }
3302b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson  }
3311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
332eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman  if (FD->getNumParams()) {
333183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    const FunctionProtoType* FProto = FD->getType()->getAs<FunctionProtoType>();
334eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman    assert(FProto && "Function def must have prototype!");
3357c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar
3367c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar    for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i)
3371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Args.push_back(std::make_pair(FD->getParamDecl(i),
3387c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar                                    FProto->getArgType(i)));
3395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
340af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
341a355e07454463b19829ac92ffd115a097faff0e0John McCall  SourceRange BodyRange;
342a355e07454463b19829ac92ffd115a097faff0e0John McCall  if (Stmt *Body = FD->getBody()) BodyRange = Body->getSourceRange();
3434365bba95be15342575b4f030c6583a770a5da3dAnders Carlsson
344a355e07454463b19829ac92ffd115a097faff0e0John McCall  // Emit the standard function prologue.
345a355e07454463b19829ac92ffd115a097faff0e0John McCall  StartFunction(GD, FD->getResultType(), Fn, Args, BodyRange.getBegin());
3461851a12605bc6f1ea70d11974a315340ebaab6ebAnders Carlsson
347a355e07454463b19829ac92ffd115a097faff0e0John McCall  // Generate the body of the function.
3489fc6a7774643a810c8501dae2323e863fefb623eJohn McCall  if (isa<CXXDestructorDecl>(FD))
3499fc6a7774643a810c8501dae2323e863fefb623eJohn McCall    EmitDestructorBody(Args);
3509fc6a7774643a810c8501dae2323e863fefb623eJohn McCall  else if (isa<CXXConstructorDecl>(FD))
3519fc6a7774643a810c8501dae2323e863fefb623eJohn McCall    EmitConstructorBody(Args);
3529fc6a7774643a810c8501dae2323e863fefb623eJohn McCall  else
3539fc6a7774643a810c8501dae2323e863fefb623eJohn McCall    EmitFunctionBody(Args);
354c33e4ba20304e222692e77f2c6ad26a5d8d32f83Anders Carlsson
355a355e07454463b19829ac92ffd115a097faff0e0John McCall  // Emit the standard function epilogue.
356a355e07454463b19829ac92ffd115a097faff0e0John McCall  FinishFunction(BodyRange.getEnd());
3571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3582b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson  // Destroy the 'this' declaration.
3592b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson  if (CXXThisDecl)
3602b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson    CXXThisDecl->Destroy(getContext());
361f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson
362f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson  // Destroy the VTT declaration.
363f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson  if (CXXVTTDecl)
364f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson    CXXVTTDecl->Destroy(getContext());
3655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
3665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3670946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner/// ContainsLabel - Return true if the statement contains a label in it.  If
3680946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner/// this statement is not executed normally, it not containing a label means
3690946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner/// that we can just remove the code.
3700946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattnerbool CodeGenFunction::ContainsLabel(const Stmt *S, bool IgnoreCaseStmts) {
3710946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // Null statement, not a label!
3720946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  if (S == 0) return false;
3731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3740946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // If this is a label, we have to emit the code, consider something like:
3750946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // if (0) {  ...  foo:  bar(); }  goto foo;
3760946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  if (isa<LabelStmt>(S))
3770946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner    return true;
3781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3790946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // If this is a case/default statement, and we haven't seen a switch, we have
3800946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // to emit the code.
3810946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  if (isa<SwitchCase>(S) && !IgnoreCaseStmts)
3820946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner    return true;
3831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3840946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // If this is a switch statement, we want to ignore cases below it.
3850946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  if (isa<SwitchStmt>(S))
3860946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner    IgnoreCaseStmts = true;
3871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3880946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // Scan subexpressions for verboten labels.
3890946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
3900946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner       I != E; ++I)
3910946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner    if (ContainsLabel(*I, IgnoreCaseStmts))
3920946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner      return true;
3931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3940946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  return false;
3950946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner}
3960946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner
39731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
39831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// ConstantFoldsToSimpleInteger - If the sepcified expression does not fold to
39931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// a constant, or if it does but contains a label, return 0.  If it constant
40031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// folds to 'true' and does not contain a label, return 1, if it constant folds
40131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// to 'false' and does not contain a label, return -1.
40231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattnerint CodeGenFunction::ConstantFoldsToSimpleInteger(const Expr *Cond) {
40336bc14c3a1cf63ee306df5687ac8e85f924f8639Daniel Dunbar  // FIXME: Rename and handle conversion of other evaluatable things
40436bc14c3a1cf63ee306df5687ac8e85f924f8639Daniel Dunbar  // to bool.
40564712f196bffd41fb0552c2643b07a25c3e45082Anders Carlsson  Expr::EvalResult Result;
4061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!Cond->Evaluate(Result, getContext()) || !Result.Val.isInt() ||
40764712f196bffd41fb0552c2643b07a25c3e45082Anders Carlsson      Result.HasSideEffects)
408ef5a66d8171eb95e948107f8ee7707b360aaff25Anders Carlsson    return 0;  // Not foldable, not integer or not fully evaluatable.
4091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
41031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  if (CodeGenFunction::ContainsLabel(Cond))
41131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner    return 0;  // Contains a label.
4121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
41364712f196bffd41fb0552c2643b07a25c3e45082Anders Carlsson  return Result.Val.getInt().getBoolValue() ? 1 : -1;
41431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner}
41531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
41631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
41731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an if
41831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// statement) to the specified blocks.  Based on the condition, this might try
41931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// to simplify the codegen of the conditional based on the branch.
42031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner///
42131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattnervoid CodeGenFunction::EmitBranchOnBoolExpr(const Expr *Cond,
42231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner                                           llvm::BasicBlock *TrueBlock,
42331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner                                           llvm::BasicBlock *FalseBlock) {
42431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  if (const ParenExpr *PE = dyn_cast<ParenExpr>(Cond))
42531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner    return EmitBranchOnBoolExpr(PE->getSubExpr(), TrueBlock, FalseBlock);
4261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
42731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  if (const BinaryOperator *CondBOp = dyn_cast<BinaryOperator>(Cond)) {
42831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner    // Handle X && Y in a condition.
42931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner    if (CondBOp->getOpcode() == BinaryOperator::LAnd) {
43031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // If we have "1 && X", simplify the code.  "0 && X" would have constant
43131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // folded if the case was simple enough.
43231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == 1) {
43331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        // br(1 && X) -> br(X).
43431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
43531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      }
4361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
43731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // If we have "X && 1", simplify the code to use an uncond branch.
43831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // "X && 0" would have been constant folded to 0.
43931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == 1) {
44031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        // br(X && 1) -> br(X).
44131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock);
44231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      }
4431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
44431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // Emit the LHS as a conditional.  If the LHS conditional is false, we
44531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // want to jump to the FalseBlock.
4469615ecb44f549ae9fa2b4db6ff46bc78befbf62cDaniel Dunbar      llvm::BasicBlock *LHSTrue = createBasicBlock("land.lhs.true");
44731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBranchOnBoolExpr(CondBOp->getLHS(), LHSTrue, FalseBlock);
44831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBlock(LHSTrue);
4491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
45008e9e453f40aff95a59bd67db49b8f050765e1f0Anders Carlsson      // Any temporaries created here are conditional.
45172119a887c4af39c90f10b6bdc158389071f42eaAnders Carlsson      BeginConditionalBranch();
45231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
45372119a887c4af39c90f10b6bdc158389071f42eaAnders Carlsson      EndConditionalBranch();
45408e9e453f40aff95a59bd67db49b8f050765e1f0Anders Carlsson
45531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      return;
45631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner    } else if (CondBOp->getOpcode() == BinaryOperator::LOr) {
45731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // If we have "0 || X", simplify the code.  "1 || X" would have constant
45831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // folded if the case was simple enough.
45931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == -1) {
46031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        // br(0 || X) -> br(X).
46131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
46231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      }
4631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
46431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // If we have "X || 0", simplify the code to use an uncond branch.
46531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // "X || 1" would have been constant folded to 1.
46631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == -1) {
46731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        // br(X || 0) -> br(X).
46831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock);
46931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      }
4701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
47131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // Emit the LHS as a conditional.  If the LHS conditional is true, we
47231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // want to jump to the TrueBlock.
4739615ecb44f549ae9fa2b4db6ff46bc78befbf62cDaniel Dunbar      llvm::BasicBlock *LHSFalse = createBasicBlock("lor.lhs.false");
47431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, LHSFalse);
47531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBlock(LHSFalse);
4761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
47708e9e453f40aff95a59bd67db49b8f050765e1f0Anders Carlsson      // Any temporaries created here are conditional.
47872119a887c4af39c90f10b6bdc158389071f42eaAnders Carlsson      BeginConditionalBranch();
47931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
48072119a887c4af39c90f10b6bdc158389071f42eaAnders Carlsson      EndConditionalBranch();
48108e9e453f40aff95a59bd67db49b8f050765e1f0Anders Carlsson
48231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      return;
48331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner    }
484552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner  }
4851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
486552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner  if (const UnaryOperator *CondUOp = dyn_cast<UnaryOperator>(Cond)) {
487552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner    // br(!x, t, f) -> br(x, f, t)
488552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner    if (CondUOp->getOpcode() == UnaryOperator::LNot)
489552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner      return EmitBranchOnBoolExpr(CondUOp->getSubExpr(), FalseBlock, TrueBlock);
49031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  }
4911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
49209b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar  if (const ConditionalOperator *CondOp = dyn_cast<ConditionalOperator>(Cond)) {
49309b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar    // Handle ?: operator.
49409b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar
49509b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar    // Just ignore GNU ?: extension.
49609b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar    if (CondOp->getLHS()) {
49709b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      // br(c ? x : y, t, f) -> br(c, br(x, t, f), br(y, t, f))
49809b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true");
49909b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false");
50009b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      EmitBranchOnBoolExpr(CondOp->getCond(), LHSBlock, RHSBlock);
50109b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      EmitBlock(LHSBlock);
50209b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      EmitBranchOnBoolExpr(CondOp->getLHS(), TrueBlock, FalseBlock);
50309b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      EmitBlock(RHSBlock);
50409b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      EmitBranchOnBoolExpr(CondOp->getRHS(), TrueBlock, FalseBlock);
50509b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      return;
50609b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar    }
50709b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar  }
50809b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar
50931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  // Emit the code with the fully general case.
51031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  llvm::Value *CondV = EvaluateExprAsBool(Cond);
51131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  Builder.CreateCondBr(CondV, TrueBlock, FalseBlock);
51231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner}
51331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
514488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar/// ErrorUnsupported - Print out an error that codegen doesn't support the
515dc5e8268292046114ffe02e48773572a91a310f1Chris Lattner/// specified stmt yet.
51690df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbarvoid CodeGenFunction::ErrorUnsupported(const Stmt *S, const char *Type,
51790df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar                                       bool OmitOnError) {
51890df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar  CGM.ErrorUnsupported(S, Type, OmitOnError);
519dc5e8268292046114ffe02e48773572a91a310f1Chris Lattner}
520dc5e8268292046114ffe02e48773572a91a310f1Chris Lattner
5211884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlssonvoid
5221884eb0b5c55edda4893ddec45e7dbad79758782Anders CarlssonCodeGenFunction::EmitNullInitialization(llvm::Value *DestPtr, QualType Ty) {
5231884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlsson  // If the type contains a pointer to data member we can't memset it to zero.
5241884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlsson  // Instead, create a null constant and copy it to the destination.
5251884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlsson  if (CGM.getTypes().ContainsPointerToDataMember(Ty)) {
5261884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlsson    llvm::Constant *NullConstant = CGM.EmitNullConstant(Ty);
5271884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlsson
5281884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlsson    llvm::GlobalVariable *NullVariable =
5291884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlsson      new llvm::GlobalVariable(CGM.getModule(), NullConstant->getType(),
5301884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlsson                               /*isConstant=*/true,
5311884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlsson                               llvm::GlobalVariable::PrivateLinkage,
5321884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlsson                               NullConstant, llvm::Twine());
5331884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlsson    EmitAggregateCopy(DestPtr, NullVariable, Ty, /*isVolatile=*/false);
5341884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlsson    return;
5351884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlsson  }
5361884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlsson
5371884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlsson
5380d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson  // Ignore empty classes in C++.
5390d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson  if (getContext().getLangOptions().CPlusPlus) {
5400d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson    if (const RecordType *RT = Ty->getAs<RecordType>()) {
5410d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson      if (cast<CXXRecordDecl>(RT->getDecl())->isEmpty())
5420d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson        return;
5430d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson    }
5440d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson  }
5450d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson
5461884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlsson  // Otherwise, just memset the whole thing to zero.  This is legal
5471884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlsson  // because in LLVM, all default initializers (other than the ones we just
5481884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlsson  // handled above) are guaranteed to have a bit pattern of all zeros.
54936afd38fb749ab34c8341a1dc70366bca904eaa3Chris Lattner  const llvm::Type *BP = llvm::Type::getInt8PtrTy(VMContext);
5503d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson  if (DestPtr->getType() != BP)
5513d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson    DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
5523d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson
5533d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson  // Get size and alignment info for this aggregate.
5543d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson  std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty);
5553d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson
55688207c9ca218486b93ae7df14e9764cd0c2c3383Chris Lattner  // Don't bother emitting a zero-byte memset.
55788207c9ca218486b93ae7df14e9764cd0c2c3383Chris Lattner  if (TypeInfo.first == 0)
55888207c9ca218486b93ae7df14e9764cd0c2c3383Chris Lattner    return;
5591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5603d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson  // FIXME: Handle variable sized types.
5611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  const llvm::Type *IntPtr = llvm::IntegerType::get(VMContext,
5620032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson                                                    LLVMPointerWidth);
5633d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson
5643ecd785aff34381f3704d9cb28fe3ef85af759deMon P Wang  Builder.CreateCall5(CGM.getMemSetFn(BP, IntPtr), DestPtr,
5650032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson                 llvm::Constant::getNullValue(llvm::Type::getInt8Ty(VMContext)),
5663d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson                      // TypeInfo.first describes size in bits.
5674a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson                      llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
5681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                      llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
5693ecd785aff34381f3704d9cb28fe3ef85af759deMon P Wang                                             TypeInfo.second/8),
5703ecd785aff34381f3704d9cb28fe3ef85af759deMon P Wang                      llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext),
5713ecd785aff34381f3704d9cb28fe3ef85af759deMon P Wang                                             0));
5723d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson}
5733d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson
574d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattnerllvm::BlockAddress *CodeGenFunction::GetAddrOfLabel(const LabelStmt *L) {
575d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // Make sure that there is a block for the indirect goto.
576d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  if (IndirectBranch == 0)
577d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    GetIndirectGotoBlock();
5783d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner
579d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  llvm::BasicBlock *BB = getBasicBlockForLabel(L);
5803d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner
581d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // Make sure the indirect branch includes all of the address-taken blocks.
582d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  IndirectBranch->addDestination(BB);
583d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  return llvm::BlockAddress::get(CurFn, BB);
5843d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner}
5851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5863d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattnerllvm::BasicBlock *CodeGenFunction::GetIndirectGotoBlock() {
587d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // If we already made the indirect branch for indirect goto, return its block.
588d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  if (IndirectBranch) return IndirectBranch->getParent();
5893d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner
590d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  CGBuilderTy TmpBuilder(createBasicBlock("indirectgoto"));
5913d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner
592d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
59385e74acfcfb0c835a2e6c1adab314e997917039aChris Lattner
5943d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner  // Create the PHI node that indirect gotos will add entries to.
595d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  llvm::Value *DestVal = TmpBuilder.CreatePHI(Int8PtrTy, "indirect.goto.dest");
5963d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner
597d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // Create the indirect branch instruction.
598d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  IndirectBranch = TmpBuilder.CreateIndirectBr(DestVal);
599d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  return IndirectBranch->getParent();
6000ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar}
601ddf7cac45d85b73127adbbd91a2b28fc7291c57eAnders Carlsson
602d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbarllvm::Value *CodeGenFunction::GetVLASize(const VariableArrayType *VAT) {
603bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman  llvm::Value *&SizeEntry = VLASizeMap[VAT->getSizeExpr()];
6041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
605f666b7780d04186521adcaedb0e15dfa4d5e6933Anders Carlsson  assert(SizeEntry && "Did not emit size for type");
606f666b7780d04186521adcaedb0e15dfa4d5e6933Anders Carlsson  return SizeEntry;
607f666b7780d04186521adcaedb0e15dfa4d5e6933Anders Carlsson}
608dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlsson
609d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbarllvm::Value *CodeGenFunction::EmitVLASize(QualType Ty) {
61060d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson  assert(Ty->isVariablyModifiedType() &&
61160d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson         "Must pass variably modified type to EmitVLASizes!");
6121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
613d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar  EnsureInsertPoint();
6141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
61560d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson  if (const VariableArrayType *VAT = getContext().getAsVariableArrayType(Ty)) {
616bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman    llvm::Value *&SizeEntry = VLASizeMap[VAT->getSizeExpr()];
6171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
618fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson    if (!SizeEntry) {
61996f214776c0f69069fee4d67557c8c7f416009a8Anders Carlsson      const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
6201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
621ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner      // Get the element size;
622ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner      QualType ElemTy = VAT->getElementType();
623ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner      llvm::Value *ElemSize;
624fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson      if (ElemTy->isVariableArrayType())
625fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson        ElemSize = EmitVLASize(ElemTy);
626ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner      else
6274a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson        ElemSize = llvm::ConstantInt::get(SizeTy,
628199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck            getContext().getTypeSizeInChars(ElemTy).getQuantity());
6291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
630fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson      llvm::Value *NumElements = EmitScalarExpr(VAT->getSizeExpr());
63196f214776c0f69069fee4d67557c8c7f416009a8Anders Carlsson      NumElements = Builder.CreateIntCast(NumElements, SizeTy, false, "tmp");
6321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
633fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson      SizeEntry = Builder.CreateMul(ElemSize, NumElements);
634fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson    }
6351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
63660d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson    return SizeEntry;
637dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlsson  }
6381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
639ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner  if (const ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
640ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner    EmitVLASize(AT->getElementType());
641ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner    return 0;
6421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
6431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
644ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner  const PointerType *PT = Ty->getAs<PointerType>();
645ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner  assert(PT && "unknown VM type!");
646ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner  EmitVLASize(PT->getPointeeType());
64760d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson  return 0;
648dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlsson}
6494fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman
6504fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedmanllvm::Value* CodeGenFunction::EmitVAListRef(const Expr* E) {
6514fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman  if (CGM.getContext().getBuiltinVaListType()->isArrayType()) {
6524fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman    return EmitScalarExpr(E);
6534fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman  }
6544fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman  return EmitLValue(E).getAddress();
6554fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman}
6566ccc47698d0311ddabf32fa0f6db8e4f09ac96f8Anders Carlsson
6577799621ad7d16f3c6a1aeda024312d63ecd77777Fariborz Jahanianvoid CodeGenFunction::PushCleanupBlock(llvm::BasicBlock *CleanupEntryBlock,
65899533834ba8f3658559f334e68a518ebb6388ceaMike Stump                                       llvm::BasicBlock *CleanupExitBlock,
659d88ea5687968640ada2bc5a10211cbeb68a671ecMike Stump                                       llvm::BasicBlock *PreviousInvokeDest,
66099533834ba8f3658559f334e68a518ebb6388ceaMike Stump                                       bool EHOnly) {
66199533834ba8f3658559f334e68a518ebb6388ceaMike Stump  CleanupEntries.push_back(CleanupEntry(CleanupEntryBlock, CleanupExitBlock,
662d88ea5687968640ada2bc5a10211cbeb68a671ecMike Stump                                        PreviousInvokeDest, EHOnly));
6636ccc47698d0311ddabf32fa0f6db8e4f09ac96f8Anders Carlsson}
664c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson
6651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid CodeGenFunction::EmitCleanupBlocks(size_t OldCleanupStackSize) {
6661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(CleanupEntries.size() >= OldCleanupStackSize &&
667c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson         "Cleanup stack mismatch!");
6681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
669c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson  while (CleanupEntries.size() > OldCleanupStackSize)
670c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson    EmitCleanupBlock();
671c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson}
672c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson
6731eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpCodeGenFunction::CleanupBlockInfo CodeGenFunction::PopCleanupBlock() {
674bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  CleanupEntry &CE = CleanupEntries.back();
6751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6767799621ad7d16f3c6a1aeda024312d63ecd77777Fariborz Jahanian  llvm::BasicBlock *CleanupEntryBlock = CE.CleanupEntryBlock;
6771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
678bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  std::vector<llvm::BasicBlock *> Blocks;
679bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  std::swap(Blocks, CE.Blocks);
6801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
681bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  std::vector<llvm::BranchInst *> BranchFixups;
682bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  std::swap(BranchFixups, CE.BranchFixups);
6831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
68499533834ba8f3658559f334e68a518ebb6388ceaMike Stump  bool EHOnly = CE.EHOnly;
68599533834ba8f3658559f334e68a518ebb6388ceaMike Stump
686d88ea5687968640ada2bc5a10211cbeb68a671ecMike Stump  setInvokeDest(CE.PreviousInvokeDest);
687d88ea5687968640ada2bc5a10211cbeb68a671ecMike Stump
688bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  CleanupEntries.pop_back();
689bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson
690ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson  // Check if any branch fixups pointed to the scope we just popped. If so,
691ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson  // we can remove them.
692ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson  for (size_t i = 0, e = BranchFixups.size(); i != e; ++i) {
693ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson    llvm::BasicBlock *Dest = BranchFixups[i]->getSuccessor(0);
694ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson    BlockScopeMap::iterator I = BlockScopes.find(Dest);
6951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
696ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson    if (I == BlockScopes.end())
697ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson      continue;
6981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
699ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson    assert(I->second <= CleanupEntries.size() && "Invalid branch fixup!");
7001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
701ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson    if (I->second == CleanupEntries.size()) {
702ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson      // We don't need to do this branch fixup.
703ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson      BranchFixups[i] = BranchFixups.back();
704ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson      BranchFixups.pop_back();
705ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson      i--;
706ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson      e--;
707ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson      continue;
7081093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson    }
7091093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson  }
7101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7117799621ad7d16f3c6a1aeda024312d63ecd77777Fariborz Jahanian  llvm::BasicBlock *SwitchBlock = CE.CleanupExitBlock;
712bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  llvm::BasicBlock *EndBlock = 0;
7131093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson  if (!BranchFixups.empty()) {
7147799621ad7d16f3c6a1aeda024312d63ecd77777Fariborz Jahanian    if (!SwitchBlock)
7157799621ad7d16f3c6a1aeda024312d63ecd77777Fariborz Jahanian      SwitchBlock = createBasicBlock("cleanup.switch");
716bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson    EndBlock = createBasicBlock("cleanup.end");
7171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
718bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson    llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
7191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
720bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson    Builder.SetInsertPoint(SwitchBlock);
721bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson
72299533834ba8f3658559f334e68a518ebb6388ceaMike Stump    llvm::Value *DestCodePtr
72399533834ba8f3658559f334e68a518ebb6388ceaMike Stump      = CreateTempAlloca(llvm::Type::getInt32Ty(VMContext),
72499533834ba8f3658559f334e68a518ebb6388ceaMike Stump                         "cleanup.dst");
7251093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson    llvm::Value *DestCode = Builder.CreateLoad(DestCodePtr, "tmp");
7261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7271093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson    // Create a switch instruction to determine where to jump next.
7281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    llvm::SwitchInst *SI = Builder.CreateSwitch(DestCode, EndBlock,
7291093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson                                                BranchFixups.size());
730bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson
73146831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson    // Restore the current basic block (if any)
7320ae7b2b2f964bd6145d65ef52dc2a28025b2bd06Anders Carlsson    if (CurBB) {
73346831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson      Builder.SetInsertPoint(CurBB);
7341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7350ae7b2b2f964bd6145d65ef52dc2a28025b2bd06Anders Carlsson      // If we had a current basic block, we also need to emit an instruction
7360ae7b2b2f964bd6145d65ef52dc2a28025b2bd06Anders Carlsson      // to initialize the cleanup destination.
7370032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson      Builder.CreateStore(llvm::Constant::getNullValue(llvm::Type::getInt32Ty(VMContext)),
7380ae7b2b2f964bd6145d65ef52dc2a28025b2bd06Anders Carlsson                          DestCodePtr);
7390ae7b2b2f964bd6145d65ef52dc2a28025b2bd06Anders Carlsson    } else
74046831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson      Builder.ClearInsertionPoint();
741bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson
7421093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson    for (size_t i = 0, e = BranchFixups.size(); i != e; ++i) {
7431093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson      llvm::BranchInst *BI = BranchFixups[i];
7441093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson      llvm::BasicBlock *Dest = BI->getSuccessor(0);
7451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7461093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson      // Fixup the branch instruction to point to the cleanup block.
7477799621ad7d16f3c6a1aeda024312d63ecd77777Fariborz Jahanian      BI->setSuccessor(0, CleanupEntryBlock);
7481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7491093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson      if (CleanupEntries.empty()) {
750cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        llvm::ConstantInt *ID;
7511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
752cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        // Check if we already have a destination for this block.
753cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        if (Dest == SI->getDefaultDest())
7540032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson          ID = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 0);
755cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        else {
756cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson          ID = SI->findCaseDest(Dest);
757cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson          if (!ID) {
758cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson            // No code found, get a new unique one by using the number of
759cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson            // switch successors.
7601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump            ID = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
761cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson                                        SI->getNumSuccessors());
762cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson            SI->addCase(ID, Dest);
763cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson          }
764cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        }
7651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
766cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        // Store the jump destination before the branch instruction.
767cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        new llvm::StoreInst(ID, DestCodePtr, BI);
7681093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson      } else {
7691093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson        // We need to jump through another cleanup block. Create a pad block
77099533834ba8f3658559f334e68a518ebb6388ceaMike Stump        // with a branch instruction that jumps to the final destination and add
77199533834ba8f3658559f334e68a518ebb6388ceaMike Stump        // it as a branch fixup to the current cleanup scope.
7721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7731093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson        // Create the pad block.
7741093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson        llvm::BasicBlock *CleanupPad = createBasicBlock("cleanup.pad", CurFn);
775cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson
776cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        // Create a unique case ID.
77799533834ba8f3658559f334e68a518ebb6388ceaMike Stump        llvm::ConstantInt *ID
77899533834ba8f3658559f334e68a518ebb6388ceaMike Stump          = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
77999533834ba8f3658559f334e68a518ebb6388ceaMike Stump                                   SI->getNumSuccessors());
780cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson
781cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        // Store the jump destination before the branch instruction.
782cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        new llvm::StoreInst(ID, DestCodePtr, BI);
783cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson
7841093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson        // Add it as the destination.
785cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson        SI->addCase(ID, CleanupPad);
7861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7871093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson        // Create the branch to the final destination.
7881093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson        llvm::BranchInst *BI = llvm::BranchInst::Create(Dest);
7891093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson        CleanupPad->getInstList().push_back(BI);
7901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7911093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson        // And add it as a branch fixup.
7921093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson        CleanupEntries.back().BranchFixups.push_back(BI);
7931093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson      }
7941093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson    }
7951093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson  }
7961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
797bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson  // Remove all blocks from the block scope map.
798bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson  for (size_t i = 0, e = Blocks.size(); i != e; ++i) {
799bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson    assert(BlockScopes.count(Blocks[i]) &&
800bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson           "Did not find block in scope map!");
8011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
802bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson    BlockScopes.erase(Blocks[i]);
803bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson  }
8041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
80599533834ba8f3658559f334e68a518ebb6388ceaMike Stump  return CleanupBlockInfo(CleanupEntryBlock, SwitchBlock, EndBlock, EHOnly);
806d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson}
807d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson
8081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid CodeGenFunction::EmitCleanupBlock() {
809bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  CleanupBlockInfo Info = PopCleanupBlock();
8101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
81199533834ba8f3658559f334e68a518ebb6388ceaMike Stump  if (Info.EHOnly) {
81299533834ba8f3658559f334e68a518ebb6388ceaMike Stump    // FIXME: Add this to the exceptional edge
81399533834ba8f3658559f334e68a518ebb6388ceaMike Stump    if (Info.CleanupBlock->getNumUses() == 0)
81499533834ba8f3658559f334e68a518ebb6388ceaMike Stump      delete Info.CleanupBlock;
81599533834ba8f3658559f334e68a518ebb6388ceaMike Stump    return;
81699533834ba8f3658559f334e68a518ebb6388ceaMike Stump  }
81799533834ba8f3658559f334e68a518ebb6388ceaMike Stump
818cd9199eb3dd1a665ea642f3918020a53e5ecb673Devang Patel  //  Scrub debug location info.
819cd9199eb3dd1a665ea642f3918020a53e5ecb673Devang Patel  for (llvm::BasicBlock::iterator LBI = Info.CleanupBlock->begin(),
820cd9199eb3dd1a665ea642f3918020a53e5ecb673Devang Patel         LBE = Info.CleanupBlock->end(); LBI != LBE; ++LBI)
821cd9199eb3dd1a665ea642f3918020a53e5ecb673Devang Patel    Builder.SetInstDebugLocation(LBI);
822cd9199eb3dd1a665ea642f3918020a53e5ecb673Devang Patel
823eb6437a9b23b3d98a4590ba5557111e14eba2edfAnders Carlsson  llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
8241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (CurBB && !CurBB->getTerminator() &&
825eb6437a9b23b3d98a4590ba5557111e14eba2edfAnders Carlsson      Info.CleanupBlock->getNumUses() == 0) {
826eb6437a9b23b3d98a4590ba5557111e14eba2edfAnders Carlsson    CurBB->getInstList().splice(CurBB->end(), Info.CleanupBlock->getInstList());
827eb6437a9b23b3d98a4590ba5557111e14eba2edfAnders Carlsson    delete Info.CleanupBlock;
8281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  } else
829eb6437a9b23b3d98a4590ba5557111e14eba2edfAnders Carlsson    EmitBlock(Info.CleanupBlock);
8301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
831bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  if (Info.SwitchBlock)
832bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson    EmitBlock(Info.SwitchBlock);
833bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson  if (Info.EndBlock)
834bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson    EmitBlock(Info.EndBlock);
835d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson}
836d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson
8371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid CodeGenFunction::AddBranchFixup(llvm::BranchInst *BI) {
8381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(!CleanupEntries.empty() &&
83987eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson         "Trying to add branch fixup without cleanup block!");
8401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
841f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // FIXME: We could be more clever here and check if there's already a branch
842f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // fixup for this destination and recycle it.
84387eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  CleanupEntries.back().BranchFixups.push_back(BI);
84487eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson}
84587eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson
8461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid CodeGenFunction::EmitBranchThroughCleanup(llvm::BasicBlock *Dest) {
84746831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson  if (!HaveInsertPoint())
84846831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson    return;
8491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
85087eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  llvm::BranchInst* BI = Builder.CreateBr(Dest);
8511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
85246831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson  Builder.ClearInsertionPoint();
8531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
85487eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  // The stack is empty, no need to do any cleanup.
85587eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  if (CleanupEntries.empty())
85687eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson    return;
8571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
85887eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  if (!Dest->getParent()) {
85987eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson    // We are trying to branch to a block that hasn't been inserted yet.
86087eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson    AddBranchFixup(BI);
86187eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson    return;
86287eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  }
8631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
86487eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  BlockScopeMap::iterator I = BlockScopes.find(Dest);
86587eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  if (I == BlockScopes.end()) {
86687eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson    // We are trying to jump to a block that is outside of any cleanup scope.
86787eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson    AddBranchFixup(BI);
86887eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson    return;
86987eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  }
8701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
87187eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  assert(I->second < CleanupEntries.size() &&
87287eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson         "Trying to branch into cleanup region");
8731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
87487eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  if (I->second == CleanupEntries.size() - 1) {
87587eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson    // We have a branch to a block in the same scope.
87687eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson    return;
87787eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  }
8781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
87987eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  AddBranchFixup(BI);
88087eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson}
881