CodeGenFunction.cpp revision d608cdb7c044365cf4e8764ade1e11e99c176078
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"
17f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall#include "CGException.h"
185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Basic/TargetInfo.h"
1931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner#include "clang/AST/APValue.h"
20de7fb8413b13651fd85b7125d08b3c9ac2816d9dDaniel Dunbar#include "clang/AST/ASTContext.h"
21c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel Dunbar#include "clang/AST/Decl.h"
222b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson#include "clang/AST/DeclCXX.h"
236a1e0eb557d47e85185e09bdf8721f53f4bf9c9cMike Stump#include "clang/AST/StmtCXX.h"
247255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner#include "clang/Frontend/CodeGenOptions.h"
254e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump#include "llvm/Target/TargetData.h"
267255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner#include "llvm/Intrinsics.h"
275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace CodeGen;
295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
301eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpCodeGenFunction::CodeGenFunction(CodeGenModule &cgm)
31a4f668f3b7e03629066a01b04e415cb2b4655dafMike Stump  : BlockFunction(cgm, *this, Builder), CGM(cgm),
32a4f668f3b7e03629066a01b04e415cb2b4655dafMike Stump    Target(CGM.getContext().Target),
33aac8705c046f01a264a4f82832895a5d9e695633Owen Anderson    Builder(cgm.getModule().getContext()),
34ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    NormalCleanupDest(0), EHCleanupDest(0), NextCleanupDestIndex(1),
35f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    ExceptionSlot(0), DebugInfo(0), IndirectBranch(0),
36ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    SwitchInsn(0), CaseRangeBlock(0),
37f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    DidCallStackSave(false), UnreachableBlock(0),
382504941793b549323f9d29c62507cf21d865fadeJohn McCall    CXXThisDecl(0), CXXThisValue(0), CXXVTTDecl(0), CXXVTTValue(0),
39f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    ConditionalBranchLevel(0), TerminateLandingPad(0), TerminateHandler(0),
4083252dcfe61aaebcb6bc117e71dc12968729513fChris Lattner    TrapBB(0) {
4177b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner
4277b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner  // Get some frequently used types.
434e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump  LLVMPointerWidth = Target.getPointerWidth(0);
4477b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner  llvm::LLVMContext &LLVMContext = CGM.getLLVMContext();
4577b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner  IntPtrTy = llvm::IntegerType::get(LLVMContext, LLVMPointerWidth);
4677b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner  Int32Ty  = llvm::Type::getInt32Ty(LLVMContext);
4777b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner  Int64Ty  = llvm::Type::getInt64Ty(LLVMContext);
4877b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner
49d88ea5687968640ada2bc5a10211cbeb68a671ecMike Stump  Exceptions = getContext().getLangOptions().Exceptions;
509c276ae0f24d4cee8f7954069d4b8eae45d0447dMike Stump  CatchUndefined = getContext().getLangOptions().CatchUndefined;
5135415f5132f70ad5097a3514ab84251e10db3664Douglas Gregor  CGM.getMangleContext().startNewFunction();
524111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner}
535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerASTContext &CodeGenFunction::getContext() const {
555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return CGM.getContext();
565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
590096acf421c4609ce7f43e8b05f8c5ca866d4611Daniel Dunbarllvm::Value *CodeGenFunction::GetAddrOfLocalVar(const VarDecl *VD) {
600096acf421c4609ce7f43e8b05f8c5ca866d4611Daniel Dunbar  llvm::Value *Res = LocalDeclMap[VD];
610096acf421c4609ce7f43e8b05f8c5ca866d4611Daniel Dunbar  assert(Res && "Invalid argument to GetAddrOfLocalVar(), no decl!");
620096acf421c4609ce7f43e8b05f8c5ca866d4611Daniel Dunbar  return Res;
63813733577d33ec56479667b49e1bff42dc6bba90Lauro Ramos Venancio}
645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
650096acf421c4609ce7f43e8b05f8c5ca866d4611Daniel Dunbarllvm::Constant *
660096acf421c4609ce7f43e8b05f8c5ca866d4611Daniel DunbarCodeGenFunction::GetAddrOfStaticLocalVar(const VarDecl *BVD) {
670096acf421c4609ce7f43e8b05f8c5ca866d4611Daniel Dunbar  return cast<llvm::Constant>(GetAddrOfLocalVar(BVD));
68dde0a94120915fa925d1ffcdb997c7b44dc9fa21Anders Carlsson}
69dde0a94120915fa925d1ffcdb997c7b44dc9fa21Anders Carlsson
708b1a343b6b360d63d5dc8a6beb841ce4414c1e00Daniel Dunbarconst llvm::Type *CodeGenFunction::ConvertTypeForMem(QualType T) {
718b1a343b6b360d63d5dc8a6beb841ce4414c1e00Daniel Dunbar  return CGM.getTypes().ConvertTypeForMem(T);
728b1a343b6b360d63d5dc8a6beb841ce4414c1e00Daniel Dunbar}
738b1a343b6b360d63d5dc8a6beb841ce4414c1e00Daniel Dunbar
745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerconst llvm::Type *CodeGenFunction::ConvertType(QualType T) {
755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return CGM.getTypes().ConvertType(T);
765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
784111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattnerbool CodeGenFunction::hasAggregateLLVMType(QualType T) {
79e9d34dc7afe06c9adaacad7a678a0cbbf749ea75Anders Carlsson  return T->isRecordType() || T->isArrayType() || T->isAnyComplexType() ||
80d608cdb7c044365cf4e8764ade1e11e99c176078John McCall    T->isObjCObjectType();
814111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner}
82391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner
831c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbarvoid CodeGenFunction::EmitReturnBlock() {
841c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // For cleanliness, we try to avoid emitting the return block for
851c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // simple cases.
861c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
871c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
881c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  if (CurBB) {
891c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar    assert(!CurBB->getTerminator() && "Unexpected terminated block.");
901c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
9196e18b05ea6b55aa92a1a576f29e9cee73a7e20bDaniel Dunbar    // We have a valid insert point, reuse it if it is empty or there are no
9296e18b05ea6b55aa92a1a576f29e9cee73a7e20bDaniel Dunbar    // explicit jumps to the return block.
93ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    if (CurBB->empty() || ReturnBlock.getBlock()->use_empty()) {
94ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      ReturnBlock.getBlock()->replaceAllUsesWith(CurBB);
95ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      delete ReturnBlock.getBlock();
9696e18b05ea6b55aa92a1a576f29e9cee73a7e20bDaniel Dunbar    } else
97ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      EmitBlock(ReturnBlock.getBlock());
981c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar    return;
991c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  }
1001c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
1011c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // Otherwise, if the return block is the target of a single direct
1021c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // branch then we can just put the code in that block instead. This
1031c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  // cleans up functions which started with a unified return block.
104ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  if (ReturnBlock.getBlock()->hasOneUse()) {
1051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    llvm::BranchInst *BI =
106ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      dyn_cast<llvm::BranchInst>(*ReturnBlock.getBlock()->use_begin());
107f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    if (BI && BI->isUnconditional() &&
108ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall        BI->getSuccessor(0) == ReturnBlock.getBlock()) {
1091c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      // Reset insertion point and delete the branch.
1101c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      Builder.SetInsertPoint(BI->getParent());
1111c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      BI->eraseFromParent();
112ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      delete ReturnBlock.getBlock();
1131c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar      return;
1141c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar    }
1151c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  }
1161c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
117f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // FIXME: We are at an unreachable point, there is no reason to emit the block
118f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // unless it has uses. However, we still need a place to put the debug
119f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // region.end for now.
1201c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
121ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  EmitBlock(ReturnBlock.getBlock());
122f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall}
123f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
124f1549f66a8216a78112286e3978cea2c29d6334cJohn McCallstatic void EmitIfUsed(CodeGenFunction &CGF, llvm::BasicBlock *BB) {
125f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  if (!BB) return;
126f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  if (!BB->use_empty())
127f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    return CGF.CurFn->getBasicBlockList().push_back(BB);
128f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  delete BB;
1291c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar}
1301c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
131af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbarvoid CodeGenFunction::FinishFunction(SourceLocation EndLoc) {
132391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  assert(BreakContinueStack.empty() &&
133391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner         "mismatched push/pop in break/continue stack!");
1341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Emit function epilog (to return).
1361c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  EmitReturnBlock();
137f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar
1387255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  EmitFunctionInstrumentation("__cyg_profile_func_exit");
1397255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner
140f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar  // Emit debug descriptor for function end.
141e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson  if (CGDebugInfo *DI = getDebugInfo()) {
142f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar    DI->setLocation(EndLoc);
1435a6fbcfd8c15a2296f94a0473a68ec09d429827fDevang Patel    DI->EmitFunctionEnd(Builder);
144f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar  }
145f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar
14635b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner  EmitFunctionEpilog(*CurFnInfo);
147cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump  EmitEndEHSpec(CurCodeDecl);
1485ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar
149f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  assert(EHStack.empty() &&
150f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall         "did not remove all scopes from cleanup stack!");
151f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
152d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // If someone did an indirect goto, emit the indirect goto block at the end of
153d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // the function.
154d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  if (IndirectBranch) {
155d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    EmitBlock(IndirectBranch->getParent());
156d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    Builder.ClearInsertionPoint();
157d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  }
158d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner
159391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  // Remove the AllocaInsertPt instruction, which is just a convenience for us.
160481769b5dc102b0256b35581e787909ad5edfab5Chris Lattner  llvm::Instruction *Ptr = AllocaInsertPt;
161391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  AllocaInsertPt = 0;
162481769b5dc102b0256b35581e787909ad5edfab5Chris Lattner  Ptr->eraseFromParent();
163d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner
164d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // If someone took the address of a label but never did an indirect goto, we
165d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // made a zero entry PHI node, which is illegal, zap it now.
166d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  if (IndirectBranch) {
167d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    llvm::PHINode *PN = cast<llvm::PHINode>(IndirectBranch->getAddress());
168d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    if (PN->getNumIncomingValues() == 0) {
169d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner      PN->replaceAllUsesWith(llvm::UndefValue::get(PN->getType()));
170d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner      PN->eraseFromParent();
171d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    }
172d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  }
173f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
174ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  EmitIfUsed(*this, RethrowBlock.getBlock());
175f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  EmitIfUsed(*this, TerminateLandingPad);
176f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  EmitIfUsed(*this, TerminateHandler);
177f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  EmitIfUsed(*this, UnreachableBlock);
178744016dde06fcffd50931e94a98c850f8b12cd87John McCall
179744016dde06fcffd50931e94a98c850f8b12cd87John McCall  if (CGM.getCodeGenOpts().EmitDeclMetadata)
180744016dde06fcffd50931e94a98c850f8b12cd87John McCall    EmitDeclMetadata();
181c8aa5f1f264fb230c38182adab944232bb160c2bChris Lattner}
182c8aa5f1f264fb230c38182adab944232bb160c2bChris Lattner
1837255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner/// ShouldInstrumentFunction - Return true if the current function should be
1847255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner/// instrumented with __cyg_profile_func_* calls
1857255a2d997b15beae82e627052fdb1b2474495c2Chris Lattnerbool CodeGenFunction::ShouldInstrumentFunction() {
1867255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  if (!CGM.getCodeGenOpts().InstrumentFunctions)
1877255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner    return false;
1887255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  if (CurFuncDecl->hasAttr<NoInstrumentFunctionAttr>())
1897255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner    return false;
1907255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  return true;
1917255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner}
1927255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner
1937255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner/// EmitFunctionInstrumentation - Emit LLVM code to call the specified
1947255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner/// instrumentation function with the current function and the call site, if
1957255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner/// function instrumentation is enabled.
1967255a2d997b15beae82e627052fdb1b2474495c2Chris Lattnervoid CodeGenFunction::EmitFunctionInstrumentation(const char *Fn) {
1977255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  if (!ShouldInstrumentFunction())
1987255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner    return;
1997255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner
2008dab6571b2cab96f44d0a1d6e3edbfdb68b7ed6bChris Lattner  const llvm::PointerType *PointerTy;
2017255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  const llvm::FunctionType *FunctionTy;
2027255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  std::vector<const llvm::Type*> ProfileFuncArgs;
2037255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner
2048dab6571b2cab96f44d0a1d6e3edbfdb68b7ed6bChris Lattner  // void __cyg_profile_func_{enter,exit} (void *this_fn, void *call_site);
2058dab6571b2cab96f44d0a1d6e3edbfdb68b7ed6bChris Lattner  PointerTy = llvm::Type::getInt8PtrTy(VMContext);
2068dab6571b2cab96f44d0a1d6e3edbfdb68b7ed6bChris Lattner  ProfileFuncArgs.push_back(PointerTy);
2078dab6571b2cab96f44d0a1d6e3edbfdb68b7ed6bChris Lattner  ProfileFuncArgs.push_back(PointerTy);
2087255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  FunctionTy = llvm::FunctionType::get(
2097255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner    llvm::Type::getVoidTy(VMContext),
2107255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner    ProfileFuncArgs, false);
2117255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner
2127255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  llvm::Constant *F = CGM.CreateRuntimeFunction(FunctionTy, Fn);
2137255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  llvm::CallInst *CallSite = Builder.CreateCall(
2147255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner    CGM.getIntrinsic(llvm::Intrinsic::returnaddress, 0, 0),
21577b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner    llvm::ConstantInt::get(Int32Ty, 0),
2167255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner    "callsite");
2177255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner
2188dab6571b2cab96f44d0a1d6e3edbfdb68b7ed6bChris Lattner  Builder.CreateCall2(F,
2198dab6571b2cab96f44d0a1d6e3edbfdb68b7ed6bChris Lattner                      llvm::ConstantExpr::getBitCast(CurFn, PointerTy),
2208dab6571b2cab96f44d0a1d6e3edbfdb68b7ed6bChris Lattner                      CallSite);
2217255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner}
2227255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner
2230ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlssonvoid CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy,
2247c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar                                    llvm::Function *Fn,
2252284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar                                    const FunctionArgList &Args,
2262284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar                                    SourceLocation StartLoc) {
2270ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlsson  const Decl *D = GD.getDecl();
2280ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlsson
2294cc1a4703363ff940b6273aeef9d96a87edeb04bAnders Carlsson  DidCallStackSave = false;
230b5437d238752dc297e42410e98d38d5250fe0463Chris Lattner  CurCodeDecl = CurFuncDecl = D;
2317c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar  FnRetTy = RetTy;
232bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  CurFn = Fn;
2335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(CurFn->isDeclaration() && "Function already has body?");
234ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner
235a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen  // Pass inline keyword to optimizer if it appears explicitly on any
236a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen  // declaration.
237a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen  if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
238a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen    for (FunctionDecl::redecl_iterator RI = FD->redecls_begin(),
239a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen           RE = FD->redecls_end(); RI != RE; ++RI)
240a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen      if (RI->isInlineSpecified()) {
241a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen        Fn->addFnAttr(llvm::Attribute::InlineHint);
242a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen        break;
243a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen      }
244a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen
24555e874299f2ad827646a4ca9ea38c402aaeb38c9Daniel Dunbar  llvm::BasicBlock *EntryBB = createBasicBlock("entry", CurFn);
2465ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar
2475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Create a marker to make it easy to insert allocas into the entryblock
24855352a2d616cf9fbb621d10faf8b960b4b268bd8Chris Lattner  // later.  Don't create this with the builder, because we don't want it
24955352a2d616cf9fbb621d10faf8b960b4b268bd8Chris Lattner  // folded.
25077b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner  llvm::Value *Undef = llvm::UndefValue::get(Int32Ty);
25177b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner  AllocaInsertPt = new llvm::BitCastInst(Undef, Int32Ty, "", EntryBB);
252f1466848dce9c4c75d96a6cabdc8db560e26aac8Chris Lattner  if (Builder.isNamePreserving())
253f1466848dce9c4c75d96a6cabdc8db560e26aac8Chris Lattner    AllocaInsertPt->setName("allocapt");
2541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
255f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  ReturnBlock = getJumpDestInCurrentScope("return");
2561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25755352a2d616cf9fbb621d10faf8b960b4b268bd8Chris Lattner  Builder.SetInsertPoint(EntryBB);
2581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
259ce056bcaa1c97b89a4b2de2112c62d060863be2bDouglas Gregor  QualType FnType = getContext().getFunctionType(RetTy, 0, 0, false, 0,
260ce056bcaa1c97b89a4b2de2112c62d060863be2bDouglas Gregor                                                 false, false, 0, 0,
261264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                                 /*FIXME?*/
262264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                                 FunctionType::ExtInfo());
26391cc815ffd13d4a78ae1b5bd617e19dd555de4f4Mike Stump
264af99417156c652a6f04dff643925036dc3241d60Sanjiv Gupta  // Emit subprogram debug descriptor.
265e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson  if (CGDebugInfo *DI = getDebugInfo()) {
2662284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar    DI->setLocation(StartLoc);
2679c6c3a0e3ae09626d2d4b04e4ffa42c3d7cab32bDevang Patel    DI->EmitFunctionStart(GD, FnType, CurFn, Builder);
268af99417156c652a6f04dff643925036dc3241d60Sanjiv Gupta  }
269af99417156c652a6f04dff643925036dc3241d60Sanjiv Gupta
2707255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  EmitFunctionInstrumentation("__cyg_profile_func_enter");
2717255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner
27288b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar  // FIXME: Leaked.
27304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  // CC info is ignored, hopefully?
27404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  CurFnInfo = &CGM.getTypes().getFunctionInfo(FnRetTy, Args,
275264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                              FunctionType::ExtInfo());
276b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman
277b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman  if (RetTy->isVoidType()) {
278b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman    // Void type; nothing to return.
279b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman    ReturnValue = 0;
280b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman  } else if (CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect &&
281b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman             hasAggregateLLVMType(CurFnInfo->getReturnType())) {
282b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman    // Indirect aggregate return; emit returned value directly into sret slot.
283647a1ec397fa13af176d07d9f5d071560a94c7a9Daniel Dunbar    // This reduces code size, and affects correctness in C++.
284b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman    ReturnValue = CurFn->arg_begin();
285b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman  } else {
286647a1ec397fa13af176d07d9f5d071560a94c7a9Daniel Dunbar    ReturnValue = CreateIRTemp(RetTy, "retval");
287b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman  }
288b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman
289cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump  EmitStartEHSpec(CurCodeDecl);
29088b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar  EmitFunctionProlog(*CurFnInfo, CurFn, Args);
2911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2922504941793b549323f9d29c62507cf21d865fadeJohn McCall  if (CXXThisDecl)
2932504941793b549323f9d29c62507cf21d865fadeJohn McCall    CXXThisValue = Builder.CreateLoad(LocalDeclMap[CXXThisDecl], "this");
2942504941793b549323f9d29c62507cf21d865fadeJohn McCall  if (CXXVTTDecl)
2952504941793b549323f9d29c62507cf21d865fadeJohn McCall    CXXVTTValue = Builder.CreateLoad(LocalDeclMap[CXXVTTDecl], "vtt");
2962504941793b549323f9d29c62507cf21d865fadeJohn McCall
297751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson  // If any of the arguments have a variably modified type, make sure to
298751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson  // emit the type size.
299751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson  for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
300751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson       i != e; ++i) {
301751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson    QualType Ty = i->second;
302751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson
303751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson    if (Ty->isVariablyModifiedType())
304751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson      EmitVLASize(Ty);
305751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson  }
3067c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar}
307eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman
3089fc6a7774643a810c8501dae2323e863fefb623eJohn McCallvoid CodeGenFunction::EmitFunctionBody(FunctionArgList &Args) {
3099fc6a7774643a810c8501dae2323e863fefb623eJohn McCall  const FunctionDecl *FD = cast<FunctionDecl>(CurGD.getDecl());
31006a9f3680d22529a2fcf20c52d71cf221d99d910Douglas Gregor  assert(FD->getBody());
31106a9f3680d22529a2fcf20c52d71cf221d99d910Douglas Gregor  EmitStmt(FD->getBody());
312a355e07454463b19829ac92ffd115a097faff0e0John McCall}
313a355e07454463b19829ac92ffd115a097faff0e0John McCall
31439dad53772c42eb36ebec1c81c56ba99d038fb94John McCall/// Tries to mark the given function nounwind based on the
31539dad53772c42eb36ebec1c81c56ba99d038fb94John McCall/// non-existence of any throwing calls within it.  We believe this is
31639dad53772c42eb36ebec1c81c56ba99d038fb94John McCall/// lightweight enough to do at -O0.
31739dad53772c42eb36ebec1c81c56ba99d038fb94John McCallstatic void TryMarkNoThrow(llvm::Function *F) {
318b3a29f132794f67108bccc9c7cc3795365e8a965John McCall  // LLVM treats 'nounwind' on a function as part of the type, so we
319b3a29f132794f67108bccc9c7cc3795365e8a965John McCall  // can't do this on functions that can be overwritten.
320b3a29f132794f67108bccc9c7cc3795365e8a965John McCall  if (F->mayBeOverridden()) return;
321b3a29f132794f67108bccc9c7cc3795365e8a965John McCall
32239dad53772c42eb36ebec1c81c56ba99d038fb94John McCall  for (llvm::Function::iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI)
32339dad53772c42eb36ebec1c81c56ba99d038fb94John McCall    for (llvm::BasicBlock::iterator
32439dad53772c42eb36ebec1c81c56ba99d038fb94John McCall           BI = FI->begin(), BE = FI->end(); BI != BE; ++BI)
32539dad53772c42eb36ebec1c81c56ba99d038fb94John McCall      if (llvm::CallInst *Call = dyn_cast<llvm::CallInst>(&*BI))
32639dad53772c42eb36ebec1c81c56ba99d038fb94John McCall        if (!Call->doesNotThrow())
32739dad53772c42eb36ebec1c81c56ba99d038fb94John McCall          return;
32839dad53772c42eb36ebec1c81c56ba99d038fb94John McCall  F->setDoesNotThrow(true);
32939dad53772c42eb36ebec1c81c56ba99d038fb94John McCall}
33039dad53772c42eb36ebec1c81c56ba99d038fb94John McCall
331c997d4278d329e18891aac9698fb991b2d4622ebAnders Carlssonvoid CodeGenFunction::GenerateCode(GlobalDecl GD, llvm::Function *Fn) {
3320ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlsson  const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
3330ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlsson
334e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson  // Check if we should generate debug info for this function.
3351feade8e520be483293dbf55eb57a51720899589Mike Stump  if (CGM.getDebugInfo() && !FD->hasAttr<NoDebugAttr>())
336e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson    DebugInfo = CGM.getDebugInfo();
3371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3387c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar  FunctionArgList Args;
3391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3406a1e0eb557d47e85185e09bdf8721f53f4bf9c9cMike Stump  CurGD = GD;
3412b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson  if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
3422b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson    if (MD->isInstance()) {
3432b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson      // Create the implicit 'this' decl.
3442b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson      // FIXME: I'm not entirely sure I like using a fake decl just for code
3452b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson      // generation. Maybe we can come up with a better way?
3462504941793b549323f9d29c62507cf21d865fadeJohn McCall      CXXThisDecl = ImplicitParamDecl::Create(getContext(), 0,
3472504941793b549323f9d29c62507cf21d865fadeJohn McCall                                              FD->getLocation(),
3481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                              &getContext().Idents.get("this"),
3492b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson                                              MD->getThisType(getContext()));
3502b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson      Args.push_back(std::make_pair(CXXThisDecl, CXXThisDecl->getType()));
351f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson
352f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson      // Check if we need a VTT parameter as well.
353af4403545a50a60d208e6fcae057308d576a92e0Anders Carlsson      if (CodeGenVTables::needsVTTParameter(GD)) {
354f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson        // FIXME: The comment about using a fake decl above applies here too.
355f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson        QualType T = getContext().getPointerType(getContext().VoidPtrTy);
356f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson        CXXVTTDecl =
3572504941793b549323f9d29c62507cf21d865fadeJohn McCall          ImplicitParamDecl::Create(getContext(), 0, FD->getLocation(),
358f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson                                    &getContext().Idents.get("vtt"), T);
359f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson        Args.push_back(std::make_pair(CXXVTTDecl, CXXVTTDecl->getType()));
360f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson      }
3612b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson    }
3622b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson  }
3631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
364eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman  if (FD->getNumParams()) {
365183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    const FunctionProtoType* FProto = FD->getType()->getAs<FunctionProtoType>();
366eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman    assert(FProto && "Function def must have prototype!");
3677c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar
3687c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar    for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i)
3691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Args.push_back(std::make_pair(FD->getParamDecl(i),
3707c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar                                    FProto->getArgType(i)));
3715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
372af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
373a355e07454463b19829ac92ffd115a097faff0e0John McCall  SourceRange BodyRange;
374a355e07454463b19829ac92ffd115a097faff0e0John McCall  if (Stmt *Body = FD->getBody()) BodyRange = Body->getSourceRange();
3754365bba95be15342575b4f030c6583a770a5da3dAnders Carlsson
376a355e07454463b19829ac92ffd115a097faff0e0John McCall  // Emit the standard function prologue.
377a355e07454463b19829ac92ffd115a097faff0e0John McCall  StartFunction(GD, FD->getResultType(), Fn, Args, BodyRange.getBegin());
3781851a12605bc6f1ea70d11974a315340ebaab6ebAnders Carlsson
379a355e07454463b19829ac92ffd115a097faff0e0John McCall  // Generate the body of the function.
3809fc6a7774643a810c8501dae2323e863fefb623eJohn McCall  if (isa<CXXDestructorDecl>(FD))
3819fc6a7774643a810c8501dae2323e863fefb623eJohn McCall    EmitDestructorBody(Args);
3829fc6a7774643a810c8501dae2323e863fefb623eJohn McCall  else if (isa<CXXConstructorDecl>(FD))
3839fc6a7774643a810c8501dae2323e863fefb623eJohn McCall    EmitConstructorBody(Args);
3849fc6a7774643a810c8501dae2323e863fefb623eJohn McCall  else
3859fc6a7774643a810c8501dae2323e863fefb623eJohn McCall    EmitFunctionBody(Args);
386c33e4ba20304e222692e77f2c6ad26a5d8d32f83Anders Carlsson
387a355e07454463b19829ac92ffd115a097faff0e0John McCall  // Emit the standard function epilogue.
388a355e07454463b19829ac92ffd115a097faff0e0John McCall  FinishFunction(BodyRange.getEnd());
38939dad53772c42eb36ebec1c81c56ba99d038fb94John McCall
39039dad53772c42eb36ebec1c81c56ba99d038fb94John McCall  // If we haven't marked the function nothrow through other means, do
39139dad53772c42eb36ebec1c81c56ba99d038fb94John McCall  // a quick pass now to see if we can.
39239dad53772c42eb36ebec1c81c56ba99d038fb94John McCall  if (!CurFn->doesNotThrow())
39339dad53772c42eb36ebec1c81c56ba99d038fb94John McCall    TryMarkNoThrow(CurFn);
3945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
3955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3960946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner/// ContainsLabel - Return true if the statement contains a label in it.  If
3970946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner/// this statement is not executed normally, it not containing a label means
3980946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner/// that we can just remove the code.
3990946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattnerbool CodeGenFunction::ContainsLabel(const Stmt *S, bool IgnoreCaseStmts) {
4000946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // Null statement, not a label!
4010946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  if (S == 0) return false;
4021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4030946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // If this is a label, we have to emit the code, consider something like:
4040946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // if (0) {  ...  foo:  bar(); }  goto foo;
4050946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  if (isa<LabelStmt>(S))
4060946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner    return true;
4071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4080946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // If this is a case/default statement, and we haven't seen a switch, we have
4090946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // to emit the code.
4100946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  if (isa<SwitchCase>(S) && !IgnoreCaseStmts)
4110946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner    return true;
4121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4130946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // If this is a switch statement, we want to ignore cases below it.
4140946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  if (isa<SwitchStmt>(S))
4150946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner    IgnoreCaseStmts = true;
4161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4170946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  // Scan subexpressions for verboten labels.
4180946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
4190946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner       I != E; ++I)
4200946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner    if (ContainsLabel(*I, IgnoreCaseStmts))
4210946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner      return true;
4221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4230946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  return false;
4240946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner}
4250946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner
42631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
42731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// ConstantFoldsToSimpleInteger - If the sepcified expression does not fold to
42831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// a constant, or if it does but contains a label, return 0.  If it constant
42931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// folds to 'true' and does not contain a label, return 1, if it constant folds
43031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// to 'false' and does not contain a label, return -1.
43131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattnerint CodeGenFunction::ConstantFoldsToSimpleInteger(const Expr *Cond) {
43236bc14c3a1cf63ee306df5687ac8e85f924f8639Daniel Dunbar  // FIXME: Rename and handle conversion of other evaluatable things
43336bc14c3a1cf63ee306df5687ac8e85f924f8639Daniel Dunbar  // to bool.
43464712f196bffd41fb0552c2643b07a25c3e45082Anders Carlsson  Expr::EvalResult Result;
4351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!Cond->Evaluate(Result, getContext()) || !Result.Val.isInt() ||
43664712f196bffd41fb0552c2643b07a25c3e45082Anders Carlsson      Result.HasSideEffects)
437ef5a66d8171eb95e948107f8ee7707b360aaff25Anders Carlsson    return 0;  // Not foldable, not integer or not fully evaluatable.
4381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
43931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  if (CodeGenFunction::ContainsLabel(Cond))
44031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner    return 0;  // Contains a label.
4411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
44264712f196bffd41fb0552c2643b07a25c3e45082Anders Carlsson  return Result.Val.getInt().getBoolValue() ? 1 : -1;
44331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner}
44431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
44531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
44631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an if
44731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// statement) to the specified blocks.  Based on the condition, this might try
44831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// to simplify the codegen of the conditional based on the branch.
44931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner///
45031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattnervoid CodeGenFunction::EmitBranchOnBoolExpr(const Expr *Cond,
45131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner                                           llvm::BasicBlock *TrueBlock,
45231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner                                           llvm::BasicBlock *FalseBlock) {
45331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  if (const ParenExpr *PE = dyn_cast<ParenExpr>(Cond))
45431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner    return EmitBranchOnBoolExpr(PE->getSubExpr(), TrueBlock, FalseBlock);
4551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
45631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  if (const BinaryOperator *CondBOp = dyn_cast<BinaryOperator>(Cond)) {
45731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner    // Handle X && Y in a condition.
45831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner    if (CondBOp->getOpcode() == BinaryOperator::LAnd) {
45931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // If we have "1 && X", simplify the code.  "0 && X" would have constant
46031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // folded if the case was simple enough.
46131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == 1) {
46231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        // br(1 && X) -> br(X).
46331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
46431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      }
4651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
46631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // If we have "X && 1", simplify the code to use an uncond branch.
46731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // "X && 0" would have been constant folded to 0.
46831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == 1) {
46931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        // br(X && 1) -> br(X).
47031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock);
47131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      }
4721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
47331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // Emit the LHS as a conditional.  If the LHS conditional is false, we
47431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // want to jump to the FalseBlock.
4759615ecb44f549ae9fa2b4db6ff46bc78befbf62cDaniel Dunbar      llvm::BasicBlock *LHSTrue = createBasicBlock("land.lhs.true");
47631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBranchOnBoolExpr(CondBOp->getLHS(), LHSTrue, FalseBlock);
47731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBlock(LHSTrue);
4781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
47908e9e453f40aff95a59bd67db49b8f050765e1f0Anders Carlsson      // Any temporaries created here are conditional.
48072119a887c4af39c90f10b6bdc158389071f42eaAnders Carlsson      BeginConditionalBranch();
48131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
48272119a887c4af39c90f10b6bdc158389071f42eaAnders Carlsson      EndConditionalBranch();
48308e9e453f40aff95a59bd67db49b8f050765e1f0Anders Carlsson
48431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      return;
48531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner    } else if (CondBOp->getOpcode() == BinaryOperator::LOr) {
48631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // If we have "0 || X", simplify the code.  "1 || X" would have constant
48731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // folded if the case was simple enough.
48831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == -1) {
48931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        // br(0 || X) -> br(X).
49031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
49131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      }
4921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
49331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // If we have "X || 0", simplify the code to use an uncond branch.
49431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // "X || 1" would have been constant folded to 1.
49531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == -1) {
49631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        // br(X || 0) -> br(X).
49731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner        return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock);
49831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      }
4991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
50031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // Emit the LHS as a conditional.  If the LHS conditional is true, we
50131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      // want to jump to the TrueBlock.
5029615ecb44f549ae9fa2b4db6ff46bc78befbf62cDaniel Dunbar      llvm::BasicBlock *LHSFalse = createBasicBlock("lor.lhs.false");
50331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, LHSFalse);
50431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBlock(LHSFalse);
5051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
50608e9e453f40aff95a59bd67db49b8f050765e1f0Anders Carlsson      // Any temporaries created here are conditional.
50772119a887c4af39c90f10b6bdc158389071f42eaAnders Carlsson      BeginConditionalBranch();
50831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
50972119a887c4af39c90f10b6bdc158389071f42eaAnders Carlsson      EndConditionalBranch();
51008e9e453f40aff95a59bd67db49b8f050765e1f0Anders Carlsson
51131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner      return;
51231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner    }
513552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner  }
5141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
515552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner  if (const UnaryOperator *CondUOp = dyn_cast<UnaryOperator>(Cond)) {
516552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner    // br(!x, t, f) -> br(x, f, t)
517552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner    if (CondUOp->getOpcode() == UnaryOperator::LNot)
518552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner      return EmitBranchOnBoolExpr(CondUOp->getSubExpr(), FalseBlock, TrueBlock);
51931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  }
5201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
52109b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar  if (const ConditionalOperator *CondOp = dyn_cast<ConditionalOperator>(Cond)) {
52209b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar    // Handle ?: operator.
52309b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar
52409b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar    // Just ignore GNU ?: extension.
52509b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar    if (CondOp->getLHS()) {
52609b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      // br(c ? x : y, t, f) -> br(c, br(x, t, f), br(y, t, f))
52709b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true");
52809b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false");
52909b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      EmitBranchOnBoolExpr(CondOp->getCond(), LHSBlock, RHSBlock);
53009b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      EmitBlock(LHSBlock);
53109b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      EmitBranchOnBoolExpr(CondOp->getLHS(), TrueBlock, FalseBlock);
53209b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      EmitBlock(RHSBlock);
53309b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      EmitBranchOnBoolExpr(CondOp->getRHS(), TrueBlock, FalseBlock);
53409b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar      return;
53509b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar    }
53609b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar  }
53709b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar
53831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  // Emit the code with the fully general case.
53931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  llvm::Value *CondV = EvaluateExprAsBool(Cond);
54031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  Builder.CreateCondBr(CondV, TrueBlock, FalseBlock);
54131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner}
54231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner
543488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar/// ErrorUnsupported - Print out an error that codegen doesn't support the
544dc5e8268292046114ffe02e48773572a91a310f1Chris Lattner/// specified stmt yet.
54590df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbarvoid CodeGenFunction::ErrorUnsupported(const Stmt *S, const char *Type,
54690df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar                                       bool OmitOnError) {
54790df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar  CGM.ErrorUnsupported(S, Type, OmitOnError);
548dc5e8268292046114ffe02e48773572a91a310f1Chris Lattner}
549dc5e8268292046114ffe02e48773572a91a310f1Chris Lattner
5501884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlssonvoid
5511884eb0b5c55edda4893ddec45e7dbad79758782Anders CarlssonCodeGenFunction::EmitNullInitialization(llvm::Value *DestPtr, QualType Ty) {
5520d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson  // Ignore empty classes in C++.
5530d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson  if (getContext().getLangOptions().CPlusPlus) {
5540d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson    if (const RecordType *RT = Ty->getAs<RecordType>()) {
5550d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson      if (cast<CXXRecordDecl>(RT->getDecl())->isEmpty())
5560d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson        return;
5570d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson    }
5580d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson  }
5599021718882441dd391a1960084580d3cd19c423aJohn McCall
5609021718882441dd391a1960084580d3cd19c423aJohn McCall  // Cast the dest ptr to the appropriate i8 pointer type.
5619021718882441dd391a1960084580d3cd19c423aJohn McCall  unsigned DestAS =
5629021718882441dd391a1960084580d3cd19c423aJohn McCall    cast<llvm::PointerType>(DestPtr->getType())->getAddressSpace();
5639021718882441dd391a1960084580d3cd19c423aJohn McCall  const llvm::Type *BP =
5649021718882441dd391a1960084580d3cd19c423aJohn McCall    llvm::Type::getInt8PtrTy(VMContext, DestAS);
5653d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson  if (DestPtr->getType() != BP)
5663d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson    DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
5673d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson
5683d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson  // Get size and alignment info for this aggregate.
5693d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson  std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty);
5709021718882441dd391a1960084580d3cd19c423aJohn McCall  uint64_t Size = TypeInfo.first;
5719021718882441dd391a1960084580d3cd19c423aJohn McCall  unsigned Align = TypeInfo.second;
5723d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson
57388207c9ca218486b93ae7df14e9764cd0c2c3383Chris Lattner  // Don't bother emitting a zero-byte memset.
5749021718882441dd391a1960084580d3cd19c423aJohn McCall  if (Size == 0)
5759021718882441dd391a1960084580d3cd19c423aJohn McCall    return;
5769021718882441dd391a1960084580d3cd19c423aJohn McCall
5779021718882441dd391a1960084580d3cd19c423aJohn McCall  llvm::ConstantInt *SizeVal = llvm::ConstantInt::get(IntPtrTy, Size / 8);
5789021718882441dd391a1960084580d3cd19c423aJohn McCall  llvm::ConstantInt *AlignVal = Builder.getInt32(Align / 8);
5799021718882441dd391a1960084580d3cd19c423aJohn McCall
5809021718882441dd391a1960084580d3cd19c423aJohn McCall  // If the type contains a pointer to data member we can't memset it to zero.
5819021718882441dd391a1960084580d3cd19c423aJohn McCall  // Instead, create a null constant and copy it to the destination.
582cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall  if (CGM.getLangOptions().CPlusPlus &&
583cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall      CGM.getCXXABI().RequiresNonZeroInitializer(Ty)) {
5849021718882441dd391a1960084580d3cd19c423aJohn McCall    llvm::Constant *NullConstant = CGM.EmitNullConstant(Ty);
5859021718882441dd391a1960084580d3cd19c423aJohn McCall
5869021718882441dd391a1960084580d3cd19c423aJohn McCall    llvm::GlobalVariable *NullVariable =
5879021718882441dd391a1960084580d3cd19c423aJohn McCall      new llvm::GlobalVariable(CGM.getModule(), NullConstant->getType(),
5889021718882441dd391a1960084580d3cd19c423aJohn McCall                               /*isConstant=*/true,
5899021718882441dd391a1960084580d3cd19c423aJohn McCall                               llvm::GlobalVariable::PrivateLinkage,
5909021718882441dd391a1960084580d3cd19c423aJohn McCall                               NullConstant, llvm::Twine());
5919021718882441dd391a1960084580d3cd19c423aJohn McCall    llvm::Value *SrcPtr =
5929021718882441dd391a1960084580d3cd19c423aJohn McCall      Builder.CreateBitCast(NullVariable, Builder.getInt8PtrTy());
5939021718882441dd391a1960084580d3cd19c423aJohn McCall
5949021718882441dd391a1960084580d3cd19c423aJohn McCall    // FIXME: variable-size types?
5959021718882441dd391a1960084580d3cd19c423aJohn McCall
5969021718882441dd391a1960084580d3cd19c423aJohn McCall    // Get and call the appropriate llvm.memcpy overload.
5979021718882441dd391a1960084580d3cd19c423aJohn McCall    llvm::Constant *Memcpy =
5989021718882441dd391a1960084580d3cd19c423aJohn McCall      CGM.getMemCpyFn(DestPtr->getType(), SrcPtr->getType(), IntPtrTy);
5999021718882441dd391a1960084580d3cd19c423aJohn McCall    Builder.CreateCall5(Memcpy, DestPtr, SrcPtr, SizeVal, AlignVal,
6009021718882441dd391a1960084580d3cd19c423aJohn McCall                        /*volatile*/ Builder.getFalse());
60188207c9ca218486b93ae7df14e9764cd0c2c3383Chris Lattner    return;
6029021718882441dd391a1960084580d3cd19c423aJohn McCall  }
6039021718882441dd391a1960084580d3cd19c423aJohn McCall
6049021718882441dd391a1960084580d3cd19c423aJohn McCall  // Otherwise, just memset the whole thing to zero.  This is legal
6059021718882441dd391a1960084580d3cd19c423aJohn McCall  // because in LLVM, all default initializers (other than the ones we just
6069021718882441dd391a1960084580d3cd19c423aJohn McCall  // handled above) are guaranteed to have a bit pattern of all zeros.
6071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6083d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson  // FIXME: Handle variable sized types.
60977b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner  Builder.CreateCall5(CGM.getMemSetFn(BP, IntPtrTy), DestPtr,
6109021718882441dd391a1960084580d3cd19c423aJohn McCall                      Builder.getInt8(0),
6119021718882441dd391a1960084580d3cd19c423aJohn McCall                      SizeVal, AlignVal, /*volatile*/ Builder.getFalse());
6123d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson}
6133d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson
614d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattnerllvm::BlockAddress *CodeGenFunction::GetAddrOfLabel(const LabelStmt *L) {
615d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // Make sure that there is a block for the indirect goto.
616d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  if (IndirectBranch == 0)
617d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner    GetIndirectGotoBlock();
6183d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner
619ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  llvm::BasicBlock *BB = getJumpDestForLabel(L).getBlock();
6203d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner
621d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // Make sure the indirect branch includes all of the address-taken blocks.
622d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  IndirectBranch->addDestination(BB);
623d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  return llvm::BlockAddress::get(CurFn, BB);
6243d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner}
6251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6263d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattnerllvm::BasicBlock *CodeGenFunction::GetIndirectGotoBlock() {
627d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // If we already made the indirect branch for indirect goto, return its block.
628d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  if (IndirectBranch) return IndirectBranch->getParent();
6293d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner
630d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  CGBuilderTy TmpBuilder(createBasicBlock("indirectgoto"));
6313d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner
632d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
63385e74acfcfb0c835a2e6c1adab314e997917039aChris Lattner
6343d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner  // Create the PHI node that indirect gotos will add entries to.
635d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  llvm::Value *DestVal = TmpBuilder.CreatePHI(Int8PtrTy, "indirect.goto.dest");
6363d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner
637d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  // Create the indirect branch instruction.
638d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  IndirectBranch = TmpBuilder.CreateIndirectBr(DestVal);
639d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  return IndirectBranch->getParent();
6400ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar}
641ddf7cac45d85b73127adbbd91a2b28fc7291c57eAnders Carlsson
642d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbarllvm::Value *CodeGenFunction::GetVLASize(const VariableArrayType *VAT) {
643bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman  llvm::Value *&SizeEntry = VLASizeMap[VAT->getSizeExpr()];
6441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
645f666b7780d04186521adcaedb0e15dfa4d5e6933Anders Carlsson  assert(SizeEntry && "Did not emit size for type");
646f666b7780d04186521adcaedb0e15dfa4d5e6933Anders Carlsson  return SizeEntry;
647f666b7780d04186521adcaedb0e15dfa4d5e6933Anders Carlsson}
648dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlsson
649d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbarllvm::Value *CodeGenFunction::EmitVLASize(QualType Ty) {
65060d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson  assert(Ty->isVariablyModifiedType() &&
65160d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson         "Must pass variably modified type to EmitVLASizes!");
6521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
653d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar  EnsureInsertPoint();
6541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
65560d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson  if (const VariableArrayType *VAT = getContext().getAsVariableArrayType(Ty)) {
656bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman    llvm::Value *&SizeEntry = VLASizeMap[VAT->getSizeExpr()];
6571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
658fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson    if (!SizeEntry) {
65996f214776c0f69069fee4d67557c8c7f416009a8Anders Carlsson      const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
6601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
661ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner      // Get the element size;
662ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner      QualType ElemTy = VAT->getElementType();
663ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner      llvm::Value *ElemSize;
664fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson      if (ElemTy->isVariableArrayType())
665fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson        ElemSize = EmitVLASize(ElemTy);
666ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner      else
6674a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson        ElemSize = llvm::ConstantInt::get(SizeTy,
668199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck            getContext().getTypeSizeInChars(ElemTy).getQuantity());
6691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
670fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson      llvm::Value *NumElements = EmitScalarExpr(VAT->getSizeExpr());
67196f214776c0f69069fee4d67557c8c7f416009a8Anders Carlsson      NumElements = Builder.CreateIntCast(NumElements, SizeTy, false, "tmp");
6721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
673fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson      SizeEntry = Builder.CreateMul(ElemSize, NumElements);
674fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson    }
6751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
67660d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson    return SizeEntry;
677dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlsson  }
6781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
679ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner  if (const ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
680ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner    EmitVLASize(AT->getElementType());
681ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner    return 0;
6821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
6831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
684ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner  const PointerType *PT = Ty->getAs<PointerType>();
685ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner  assert(PT && "unknown VM type!");
686ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner  EmitVLASize(PT->getPointeeType());
68760d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson  return 0;
688dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlsson}
6894fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman
6904fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedmanllvm::Value* CodeGenFunction::EmitVAListRef(const Expr* E) {
691fbe02ffb573ac2aa51351278af1c7970573b6ed5Chris Lattner  if (CGM.getContext().getBuiltinVaListType()->isArrayType())
6924fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman    return EmitScalarExpr(E);
6934fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman  return EmitLValue(E).getAddress();
6944fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman}
6956ccc47698d0311ddabf32fa0f6db8e4f09ac96f8Anders Carlsson
696f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall/// Pops cleanup blocks until the given savepoint is reached.
697f1549f66a8216a78112286e3978cea2c29d6334cJohn McCallvoid CodeGenFunction::PopCleanupBlocks(EHScopeStack::stable_iterator Old) {
698f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  assert(Old.isValid());
699f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
700ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  while (EHStack.stable_begin() != Old) {
701ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.begin());
702ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
703ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    // As long as Old strictly encloses the scope's enclosing normal
704ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    // cleanup, we're going to emit another normal cleanup which
705ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    // fallthrough can propagate through.
706ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    bool FallThroughIsBranchThrough =
707ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      Old.strictlyEncloses(Scope.getEnclosingNormalCleanup());
708ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
709ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    PopCleanupBlock(FallThroughIsBranchThrough);
710ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  }
7116ccc47698d0311ddabf32fa0f6db8e4f09ac96f8Anders Carlsson}
712c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson
713ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCallstatic llvm::BasicBlock *CreateNormalEntry(CodeGenFunction &CGF,
714ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall                                           EHCleanupScope &Scope) {
715ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  assert(Scope.isNormalCleanup());
716ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  llvm::BasicBlock *Entry = Scope.getNormalBlock();
717ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  if (!Entry) {
718ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    Entry = CGF.createBasicBlock("cleanup");
719ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    Scope.setNormalBlock(Entry);
720f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  }
721ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  return Entry;
722ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall}
723f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
724ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCallstatic llvm::BasicBlock *CreateEHEntry(CodeGenFunction &CGF,
725ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall                                       EHCleanupScope &Scope) {
726ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  assert(Scope.isEHCleanup());
727ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  llvm::BasicBlock *Entry = Scope.getEHBlock();
728ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  if (!Entry) {
729ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    Entry = CGF.createBasicBlock("eh.cleanup");
730ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    Scope.setEHBlock(Entry);
731ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  }
732ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  return Entry;
733ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall}
734f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
735ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall/// Transitions the terminator of the given exit-block of a cleanup to
736ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall/// be a cleanup switch.
737ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCallstatic llvm::SwitchInst *TransitionToCleanupSwitch(CodeGenFunction &CGF,
738ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall                                                   llvm::BasicBlock *Block) {
739ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  // If it's a branch, turn it into a switch whose default
740ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  // destination is its original target.
741ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  llvm::TerminatorInst *Term = Block->getTerminator();
742ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  assert(Term && "can't transition block without terminator");
743ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
744ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  if (llvm::BranchInst *Br = dyn_cast<llvm::BranchInst>(Term)) {
745ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    assert(Br->isUnconditional());
746ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    llvm::LoadInst *Load =
747ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      new llvm::LoadInst(CGF.getNormalCleanupDestSlot(), "cleanup.dest", Term);
748ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    llvm::SwitchInst *Switch =
749ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      llvm::SwitchInst::Create(Load, Br->getSuccessor(0), 4, Block);
750ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    Br->eraseFromParent();
751ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    return Switch;
752ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  } else {
753ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    return cast<llvm::SwitchInst>(Term);
754ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  }
755c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson}
756c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson
757f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall/// Attempts to reduce a cleanup's entry block to a fallthrough.  This
758f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall/// is basically llvm::MergeBlockIntoPredecessor, except
759ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall/// simplified/optimized for the tighter constraints on cleanup blocks.
760ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall///
761ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall/// Returns the new block, whatever it is.
762ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCallstatic llvm::BasicBlock *SimplifyCleanupEntry(CodeGenFunction &CGF,
763ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall                                              llvm::BasicBlock *Entry) {
764f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  llvm::BasicBlock *Pred = Entry->getSinglePredecessor();
765ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  if (!Pred) return Entry;
7661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
767f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  llvm::BranchInst *Br = dyn_cast<llvm::BranchInst>(Pred->getTerminator());
768ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  if (!Br || Br->isConditional()) return Entry;
769f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  assert(Br->getSuccessor(0) == Entry);
7701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
771f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // If we were previously inserting at the end of the cleanup entry
772f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // block, we'll need to continue inserting at the end of the
773f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // predecessor.
774f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  bool WasInsertBlock = CGF.Builder.GetInsertBlock() == Entry;
775f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  assert(!WasInsertBlock || CGF.Builder.GetInsertPoint() == Entry->end());
7761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
777f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // Kill the branch.
778f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  Br->eraseFromParent();
7791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
780f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // Merge the blocks.
781f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  Pred->getInstList().splice(Pred->end(), Entry->getInstList());
78299533834ba8f3658559f334e68a518ebb6388ceaMike Stump
783f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // Kill the entry block.
784f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  Entry->eraseFromParent();
785d88ea5687968640ada2bc5a10211cbeb68a671ecMike Stump
786f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  if (WasInsertBlock)
787f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    CGF.Builder.SetInsertPoint(Pred);
788f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
789ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  return Pred;
790f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall}
791f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
7921f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCallstatic void EmitCleanup(CodeGenFunction &CGF,
7931f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall                        EHScopeStack::Cleanup *Fn,
7941f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall                        bool ForEH) {
795da65ea86482bc116906edfb9ba1d7124f76cc867John McCall  if (ForEH) CGF.EHStack.pushTerminate();
796da65ea86482bc116906edfb9ba1d7124f76cc867John McCall  Fn->Emit(CGF, ForEH);
797da65ea86482bc116906edfb9ba1d7124f76cc867John McCall  if (ForEH) CGF.EHStack.popTerminate();
798da65ea86482bc116906edfb9ba1d7124f76cc867John McCall  assert(CGF.HaveInsertPoint() && "cleanup ended with no insertion point?");
799da65ea86482bc116906edfb9ba1d7124f76cc867John McCall}
800da65ea86482bc116906edfb9ba1d7124f76cc867John McCall
8011f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall/// Pops a cleanup block.  If the block includes a normal cleanup, the
8021f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall/// current insertion point is threaded through the cleanup, as are
8031f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall/// any branch fixups on the cleanup.
804ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCallvoid CodeGenFunction::PopCleanupBlock(bool FallthroughIsBranchThrough) {
8051f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall  assert(!EHStack.empty() && "cleanup stack is empty!");
8061f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall  assert(isa<EHCleanupScope>(*EHStack.begin()) && "top not a cleanup!");
8071f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall  EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.begin());
8081f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall  assert(Scope.getFixupDepth() <= EHStack.getNumBranchFixups());
809838d796d020f6a8cd2b2d1e2a0a85c83bbf29543John McCall  assert(Scope.isActive() && "cleanup was still inactive when popped!");
810da65ea86482bc116906edfb9ba1d7124f76cc867John McCall
811da65ea86482bc116906edfb9ba1d7124f76cc867John McCall  // Check whether we need an EH cleanup.  This is only true if we've
812da65ea86482bc116906edfb9ba1d7124f76cc867John McCall  // generated a lazy EH cleanup block.
813ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  bool RequiresEHCleanup = Scope.hasEHBranches();
814da65ea86482bc116906edfb9ba1d7124f76cc867John McCall
815da65ea86482bc116906edfb9ba1d7124f76cc867John McCall  // Check the three conditions which might require a normal cleanup:
816da65ea86482bc116906edfb9ba1d7124f76cc867John McCall
817da65ea86482bc116906edfb9ba1d7124f76cc867John McCall  // - whether there are branch fix-ups through this cleanup
818da65ea86482bc116906edfb9ba1d7124f76cc867John McCall  unsigned FixupDepth = Scope.getFixupDepth();
8191f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall  bool HasFixups = EHStack.getNumBranchFixups() != FixupDepth;
820da65ea86482bc116906edfb9ba1d7124f76cc867John McCall
821ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  // - whether there are branch-throughs or branch-afters
822ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  bool HasExistingBranches = Scope.hasBranches();
823da65ea86482bc116906edfb9ba1d7124f76cc867John McCall
824da65ea86482bc116906edfb9ba1d7124f76cc867John McCall  // - whether there's a fallthrough
8251f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall  llvm::BasicBlock *FallthroughSource = Builder.GetInsertBlock();
826da65ea86482bc116906edfb9ba1d7124f76cc867John McCall  bool HasFallthrough = (FallthroughSource != 0);
827da65ea86482bc116906edfb9ba1d7124f76cc867John McCall
828da65ea86482bc116906edfb9ba1d7124f76cc867John McCall  bool RequiresNormalCleanup = false;
829da65ea86482bc116906edfb9ba1d7124f76cc867John McCall  if (Scope.isNormalCleanup() &&
830da65ea86482bc116906edfb9ba1d7124f76cc867John McCall      (HasFixups || HasExistingBranches || HasFallthrough)) {
831da65ea86482bc116906edfb9ba1d7124f76cc867John McCall    RequiresNormalCleanup = true;
832da65ea86482bc116906edfb9ba1d7124f76cc867John McCall  }
833da65ea86482bc116906edfb9ba1d7124f76cc867John McCall
834da65ea86482bc116906edfb9ba1d7124f76cc867John McCall  // If we don't need the cleanup at all, we're done.
835da65ea86482bc116906edfb9ba1d7124f76cc867John McCall  if (!RequiresNormalCleanup && !RequiresEHCleanup) {
836ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    EHStack.popCleanup(); // safe because there are no fixups
8371f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall    assert(EHStack.getNumBranchFixups() == 0 ||
8381f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall           EHStack.hasNormalCleanups());
839da65ea86482bc116906edfb9ba1d7124f76cc867John McCall    return;
840da65ea86482bc116906edfb9ba1d7124f76cc867John McCall  }
841da65ea86482bc116906edfb9ba1d7124f76cc867John McCall
842da65ea86482bc116906edfb9ba1d7124f76cc867John McCall  // Copy the cleanup emission data out.  Note that SmallVector
843da65ea86482bc116906edfb9ba1d7124f76cc867John McCall  // guarantees maximal alignment for its buffer regardless of its
844da65ea86482bc116906edfb9ba1d7124f76cc867John McCall  // type parameter.
845da65ea86482bc116906edfb9ba1d7124f76cc867John McCall  llvm::SmallVector<char, 8*sizeof(void*)> CleanupBuffer;
846da65ea86482bc116906edfb9ba1d7124f76cc867John McCall  CleanupBuffer.reserve(Scope.getCleanupSize());
847da65ea86482bc116906edfb9ba1d7124f76cc867John McCall  memcpy(CleanupBuffer.data(),
848da65ea86482bc116906edfb9ba1d7124f76cc867John McCall         Scope.getCleanupBuffer(), Scope.getCleanupSize());
849da65ea86482bc116906edfb9ba1d7124f76cc867John McCall  CleanupBuffer.set_size(Scope.getCleanupSize());
8501f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall  EHScopeStack::Cleanup *Fn =
8511f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall    reinterpret_cast<EHScopeStack::Cleanup*>(CleanupBuffer.data());
852da65ea86482bc116906edfb9ba1d7124f76cc867John McCall
853ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  // We want to emit the EH cleanup after the normal cleanup, but go
854ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  // ahead and do the setup for the EH cleanup while the scope is still
855ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  // alive.
856ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  llvm::BasicBlock *EHEntry = 0;
857ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  llvm::SmallVector<llvm::Instruction*, 2> EHInstsToAppend;
858ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  if (RequiresEHCleanup) {
859ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    EHEntry = CreateEHEntry(*this, Scope);
860ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
861ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    // Figure out the branch-through dest if necessary.
862ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    llvm::BasicBlock *EHBranchThroughDest = 0;
863ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    if (Scope.hasEHBranchThroughs()) {
864ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      assert(Scope.getEnclosingEHCleanup() != EHStack.stable_end());
865ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      EHScope &S = *EHStack.find(Scope.getEnclosingEHCleanup());
866ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      EHBranchThroughDest = CreateEHEntry(*this, cast<EHCleanupScope>(S));
867ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    }
868ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
869ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    // If we have exactly one branch-after and no branch-throughs, we
870ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    // can dispatch it without a switch.
8717cd4b060f1e59f6d29126383ceee614e4772f859John McCall    if (!Scope.hasEHBranchThroughs() &&
872ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall        Scope.getNumEHBranchAfters() == 1) {
873ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      assert(!EHBranchThroughDest);
874ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
875ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      // TODO: remove the spurious eh.cleanup.dest stores if this edge
876ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      // never went through any switches.
877ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      llvm::BasicBlock *BranchAfterDest = Scope.getEHBranchAfterBlock(0);
878ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      EHInstsToAppend.push_back(llvm::BranchInst::Create(BranchAfterDest));
879ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
880ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    // Otherwise, if we have any branch-afters, we need a switch.
881ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    } else if (Scope.getNumEHBranchAfters()) {
882ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      // The default of the switch belongs to the branch-throughs if
883ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      // they exist.
884ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      llvm::BasicBlock *Default =
885ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall        (EHBranchThroughDest ? EHBranchThroughDest : getUnreachableBlock());
886ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
887ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      const unsigned SwitchCapacity = Scope.getNumEHBranchAfters();
888ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
889ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      llvm::LoadInst *Load =
890ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall        new llvm::LoadInst(getEHCleanupDestSlot(), "cleanup.dest");
891ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      llvm::SwitchInst *Switch =
892ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall        llvm::SwitchInst::Create(Load, Default, SwitchCapacity);
893ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
894ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      EHInstsToAppend.push_back(Load);
895ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      EHInstsToAppend.push_back(Switch);
896ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
897ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      for (unsigned I = 0, E = Scope.getNumEHBranchAfters(); I != E; ++I)
898ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall        Switch->addCase(Scope.getEHBranchAfterIndex(I),
899ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall                        Scope.getEHBranchAfterBlock(I));
900ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
901ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    // Otherwise, we have only branch-throughs; jump to the next EH
902ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    // cleanup.
903ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    } else {
904ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      assert(EHBranchThroughDest);
905ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      EHInstsToAppend.push_back(llvm::BranchInst::Create(EHBranchThroughDest));
906ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    }
907ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  }
908ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
909ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  if (!RequiresNormalCleanup) {
910ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    EHStack.popCleanup();
911ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  } else {
912ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    // As a kindof crazy internal case, branch-through fall-throughs
913ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    // leave the insertion point set to the end of the last cleanup.
914ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    bool HasPrebranchedFallthrough =
915ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      (HasFallthrough && FallthroughSource->getTerminator());
916ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    assert(!HasPrebranchedFallthrough ||
917ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall           FallthroughSource->getTerminator()->getSuccessor(0)
918ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall             == Scope.getNormalBlock());
919da65ea86482bc116906edfb9ba1d7124f76cc867John McCall
920da65ea86482bc116906edfb9ba1d7124f76cc867John McCall    // If we have a fallthrough and no other need for the cleanup,
921da65ea86482bc116906edfb9ba1d7124f76cc867John McCall    // emit it directly.
922ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    if (HasFallthrough && !HasPrebranchedFallthrough &&
923ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall        !HasFixups && !HasExistingBranches) {
924ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
925ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      // Fixups can cause us to optimistically create a normal block,
926ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      // only to later have no real uses for it.  Just delete it in
927ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      // this case.
928ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      // TODO: we can potentially simplify all the uses after this.
929ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      if (Scope.getNormalBlock()) {
930ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall        Scope.getNormalBlock()->replaceAllUsesWith(getUnreachableBlock());
931ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall        delete Scope.getNormalBlock();
932ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      }
933ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
934ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      EHStack.popCleanup();
935ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
9361f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall      EmitCleanup(*this, Fn, /*ForEH*/ false);
937da65ea86482bc116906edfb9ba1d7124f76cc867John McCall
938da65ea86482bc116906edfb9ba1d7124f76cc867John McCall    // Otherwise, the best approach is to thread everything through
939da65ea86482bc116906edfb9ba1d7124f76cc867John McCall    // the cleanup block and then try to clean up after ourselves.
940da65ea86482bc116906edfb9ba1d7124f76cc867John McCall    } else {
941da65ea86482bc116906edfb9ba1d7124f76cc867John McCall      // Force the entry block to exist.
942ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      llvm::BasicBlock *NormalEntry = CreateNormalEntry(*this, Scope);
943da65ea86482bc116906edfb9ba1d7124f76cc867John McCall
944ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      // If there's a fallthrough, we need to store the cleanup
945ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      // destination index.  For fall-throughs this is always zero.
946ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      if (HasFallthrough && !HasPrebranchedFallthrough)
947ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall        Builder.CreateStore(Builder.getInt32(0), getNormalCleanupDestSlot());
948ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
949ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      // Emit the entry block.  This implicitly branches to it if we
950ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      // have fallthrough.  All the fixups and existing branches should
951ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      // already be branched to it.
9521f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall      EmitBlock(NormalEntry);
953da65ea86482bc116906edfb9ba1d7124f76cc867John McCall
954ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      bool HasEnclosingCleanups =
955ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall        (Scope.getEnclosingNormalCleanup() != EHStack.stable_end());
956ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
957ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      // Compute the branch-through dest if we need it:
958ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      //   - if there are branch-throughs threaded through the scope
959ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      //   - if fall-through is a branch-through
960ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      //   - if there are fixups that will be optimistically forwarded
961ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      //     to the enclosing cleanup
962ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      llvm::BasicBlock *BranchThroughDest = 0;
963ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      if (Scope.hasBranchThroughs() ||
964ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall          (HasFallthrough && FallthroughIsBranchThrough) ||
965ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall          (HasFixups && HasEnclosingCleanups)) {
966ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall        assert(HasEnclosingCleanups);
967ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall        EHScope &S = *EHStack.find(Scope.getEnclosingNormalCleanup());
968ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall        BranchThroughDest = CreateNormalEntry(*this, cast<EHCleanupScope>(S));
969da65ea86482bc116906edfb9ba1d7124f76cc867John McCall      }
970da65ea86482bc116906edfb9ba1d7124f76cc867John McCall
971ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      llvm::BasicBlock *FallthroughDest = 0;
972ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      llvm::SmallVector<llvm::Instruction*, 2> InstsToAppend;
973ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
974ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      // If there's exactly one branch-after and no other threads,
975ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      // we can route it without a switch.
976ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      if (!Scope.hasBranchThroughs() && !HasFixups && !HasFallthrough &&
977ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall          Scope.getNumBranchAfters() == 1) {
978ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall        assert(!BranchThroughDest);
979ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
980ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall        // TODO: clean up the possibly dead stores to the cleanup dest slot.
981ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall        llvm::BasicBlock *BranchAfter = Scope.getBranchAfterBlock(0);
982ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall        InstsToAppend.push_back(llvm::BranchInst::Create(BranchAfter));
983ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
984ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      // Build a switch-out if we need it:
985ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      //   - if there are branch-afters threaded through the scope
986ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      //   - if fall-through is a branch-after
987ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      //   - if there are fixups that have nowhere left to go and
988ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      //     so must be immediately resolved
989ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      } else if (Scope.getNumBranchAfters() ||
990ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall                 (HasFallthrough && !FallthroughIsBranchThrough) ||
991ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall                 (HasFixups && !HasEnclosingCleanups)) {
992ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
993ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall        llvm::BasicBlock *Default =
994ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall          (BranchThroughDest ? BranchThroughDest : getUnreachableBlock());
995ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
996ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall        // TODO: base this on the number of branch-afters and fixups
997ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall        const unsigned SwitchCapacity = 10;
998ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
999ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall        llvm::LoadInst *Load =
1000ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall          new llvm::LoadInst(getNormalCleanupDestSlot(), "cleanup.dest");
1001ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall        llvm::SwitchInst *Switch =
1002ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall          llvm::SwitchInst::Create(Load, Default, SwitchCapacity);
1003ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
1004ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall        InstsToAppend.push_back(Load);
1005ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall        InstsToAppend.push_back(Switch);
1006ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
1007ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall        // Branch-after fallthrough.
1008ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall        if (HasFallthrough && !FallthroughIsBranchThrough) {
1009ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall          FallthroughDest = createBasicBlock("cleanup.cont");
1010ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall          Switch->addCase(Builder.getInt32(0), FallthroughDest);
1011ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall        }
1012ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
1013ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall        for (unsigned I = 0, E = Scope.getNumBranchAfters(); I != E; ++I) {
1014ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall          Switch->addCase(Scope.getBranchAfterIndex(I),
1015ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall                          Scope.getBranchAfterBlock(I));
1016ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall        }
1017ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
1018ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall        if (HasFixups && !HasEnclosingCleanups)
1019ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall          ResolveAllBranchFixups(Switch);
1020ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      } else {
1021ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall        // We should always have a branch-through destination in this case.
1022ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall        assert(BranchThroughDest);
1023ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall        InstsToAppend.push_back(llvm::BranchInst::Create(BranchThroughDest));
1024ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      }
1025da65ea86482bc116906edfb9ba1d7124f76cc867John McCall
1026ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      // We're finally ready to pop the cleanup.
1027ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      EHStack.popCleanup();
1028ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      assert(EHStack.hasNormalCleanups() == HasEnclosingCleanups);
1029da65ea86482bc116906edfb9ba1d7124f76cc867John McCall
1030ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      EmitCleanup(*this, Fn, /*ForEH*/ false);
1031ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
1032ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      // Append the prepared cleanup prologue from above.
1033ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      llvm::BasicBlock *NormalExit = Builder.GetInsertBlock();
1034ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      for (unsigned I = 0, E = InstsToAppend.size(); I != E; ++I)
1035ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall        NormalExit->getInstList().push_back(InstsToAppend[I]);
1036ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
1037ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      // Optimistically hope that any fixups will continue falling through.
1038ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      for (unsigned I = FixupDepth, E = EHStack.getNumBranchFixups();
1039ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall           I < E; ++I) {
1040ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall        BranchFixup &Fixup = CGF.EHStack.getBranchFixup(I);
1041ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall        if (!Fixup.Destination) continue;
1042ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall        if (!Fixup.OptimisticBranchBlock) {
1043ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall          new llvm::StoreInst(Builder.getInt32(Fixup.DestinationIndex),
1044ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall                              getNormalCleanupDestSlot(),
1045ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall                              Fixup.InitialBranch);
1046ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall          Fixup.InitialBranch->setSuccessor(0, NormalEntry);
1047ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall        }
1048ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall        Fixup.OptimisticBranchBlock = NormalExit;
1049ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      }
1050ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
1051ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      if (FallthroughDest)
1052ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall        EmitBlock(FallthroughDest);
1053ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      else if (!HasFallthrough)
1054ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall        Builder.ClearInsertionPoint();
1055ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
1056ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      // Check whether we can merge NormalEntry into a single predecessor.
1057ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      // This might invalidate (non-IR) pointers to NormalEntry.
1058ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      llvm::BasicBlock *NewNormalEntry =
1059ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall        SimplifyCleanupEntry(*this, NormalEntry);
1060ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
1061ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      // If it did invalidate those pointers, and NormalEntry was the same
1062ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      // as NormalExit, go back and patch up the fixups.
1063ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      if (NewNormalEntry != NormalEntry && NormalEntry == NormalExit)
1064ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall        for (unsigned I = FixupDepth, E = EHStack.getNumBranchFixups();
1065ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall               I < E; ++I)
1066ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall          CGF.EHStack.getBranchFixup(I).OptimisticBranchBlock = NewNormalEntry;
1067da65ea86482bc116906edfb9ba1d7124f76cc867John McCall    }
1068da65ea86482bc116906edfb9ba1d7124f76cc867John McCall  }
1069da65ea86482bc116906edfb9ba1d7124f76cc867John McCall
1070ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  assert(EHStack.hasNormalCleanups() || EHStack.getNumBranchFixups() == 0);
1071ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
1072da65ea86482bc116906edfb9ba1d7124f76cc867John McCall  // Emit the EH cleanup if required.
1073da65ea86482bc116906edfb9ba1d7124f76cc867John McCall  if (RequiresEHCleanup) {
10741f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall    CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP();
1075ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
10761f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall    EmitBlock(EHEntry);
1077ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    EmitCleanup(*this, Fn, /*ForEH*/ true);
1078ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
1079ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    // Append the prepared cleanup prologue from above.
1080ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    llvm::BasicBlock *EHExit = Builder.GetInsertBlock();
1081ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    for (unsigned I = 0, E = EHInstsToAppend.size(); I != E; ++I)
1082ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      EHExit->getInstList().push_back(EHInstsToAppend[I]);
1083ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
10841f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall    Builder.restoreIP(SavedIP);
1085ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
1086ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    SimplifyCleanupEntry(*this, EHEntry);
1087da65ea86482bc116906edfb9ba1d7124f76cc867John McCall  }
1088da65ea86482bc116906edfb9ba1d7124f76cc867John McCall}
1089da65ea86482bc116906edfb9ba1d7124f76cc867John McCall
1090ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall/// Terminate the current block by emitting a branch which might leave
1091ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall/// the current cleanup-protected scope.  The target scope may not yet
1092ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall/// be known, in which case this will require a fixup.
1093ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall///
1094ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall/// As a side-effect, this method clears the insertion point.
1095f1549f66a8216a78112286e3978cea2c29d6334cJohn McCallvoid CodeGenFunction::EmitBranchThroughCleanup(JumpDest Dest) {
1096413e67778d593215d2f2161a4e712c8568f1ddd0John McCall  assert(Dest.getScopeDepth().encloses(EHStack.getInnermostNormalCleanup())
1097413e67778d593215d2f2161a4e712c8568f1ddd0John McCall         && "stale jump destination");
1098413e67778d593215d2f2161a4e712c8568f1ddd0John McCall
109946831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson  if (!HaveInsertPoint())
110046831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson    return;
11011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1102f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // Create the branch.
1103ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  llvm::BranchInst *BI = Builder.CreateBr(Dest.getBlock());
11041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1105838d796d020f6a8cd2b2d1e2a0a85c83bbf29543John McCall  // Calculate the innermost active normal cleanup.
1106838d796d020f6a8cd2b2d1e2a0a85c83bbf29543John McCall  EHScopeStack::stable_iterator
1107838d796d020f6a8cd2b2d1e2a0a85c83bbf29543John McCall    TopCleanup = EHStack.getInnermostActiveNormalCleanup();
1108838d796d020f6a8cd2b2d1e2a0a85c83bbf29543John McCall
1109838d796d020f6a8cd2b2d1e2a0a85c83bbf29543John McCall  // If we're not in an active normal cleanup scope, or if the
1110838d796d020f6a8cd2b2d1e2a0a85c83bbf29543John McCall  // destination scope is within the innermost active normal cleanup
1111838d796d020f6a8cd2b2d1e2a0a85c83bbf29543John McCall  // scope, we don't need to worry about fixups.
1112838d796d020f6a8cd2b2d1e2a0a85c83bbf29543John McCall  if (TopCleanup == EHStack.stable_end() ||
1113838d796d020f6a8cd2b2d1e2a0a85c83bbf29543John McCall      TopCleanup.encloses(Dest.getScopeDepth())) { // works for invalid
1114f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    Builder.ClearInsertionPoint();
111587eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson    return;
1116f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  }
11171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1118f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // If we can't resolve the destination cleanup scope, just add this
1119ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  // to the current cleanup scope as a branch fixup.
1120ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  if (!Dest.getScopeDepth().isValid()) {
1121ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    BranchFixup &Fixup = EHStack.addBranchFixup();
1122ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    Fixup.Destination = Dest.getBlock();
1123ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    Fixup.DestinationIndex = Dest.getDestIndex();
1124ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    Fixup.InitialBranch = BI;
1125ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    Fixup.OptimisticBranchBlock = 0;
1126ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
1127f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    Builder.ClearInsertionPoint();
112887eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson    return;
112987eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  }
11301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1131ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  // Otherwise, thread through all the normal cleanups in scope.
1132ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
1133ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  // Store the index at the start.
1134ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  llvm::ConstantInt *Index = Builder.getInt32(Dest.getDestIndex());
1135ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  new llvm::StoreInst(Index, getNormalCleanupDestSlot(), BI);
1136ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
1137ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  // Adjust BI to point to the first cleanup block.
1138ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  {
1139ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    EHCleanupScope &Scope =
1140838d796d020f6a8cd2b2d1e2a0a85c83bbf29543John McCall      cast<EHCleanupScope>(*EHStack.find(TopCleanup));
1141ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    BI->setSuccessor(0, CreateNormalEntry(*this, Scope));
1142ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  }
1143ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
1144ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  // Add this destination to all the scopes involved.
1145838d796d020f6a8cd2b2d1e2a0a85c83bbf29543John McCall  EHScopeStack::stable_iterator I = TopCleanup;
1146ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  EHScopeStack::stable_iterator E = Dest.getScopeDepth();
1147ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  if (E.strictlyEncloses(I)) {
1148ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    while (true) {
1149ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.find(I));
1150ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      assert(Scope.isNormalCleanup());
1151ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      I = Scope.getEnclosingNormalCleanup();
1152ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
1153ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      // If this is the last cleanup we're propagating through, tell it
1154ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      // that there's a resolved jump moving through it.
1155ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      if (!E.strictlyEncloses(I)) {
1156ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall        Scope.addBranchAfter(Index, Dest.getBlock());
1157ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall        break;
1158da65ea86482bc116906edfb9ba1d7124f76cc867John McCall      }
1159ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
1160ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      // Otherwise, tell the scope that there's a jump propoagating
1161ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      // through it.  If this isn't new information, all the rest of
1162ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      // the work has been done before.
1163ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      if (!Scope.addBranchThrough(Dest.getBlock()))
1164ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall        break;
1165f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    }
116687eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  }
1167f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1168f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  Builder.ClearInsertionPoint();
1169f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall}
11701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1171ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCallvoid CodeGenFunction::EmitBranchThroughEHCleanup(UnwindDest Dest) {
1172ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  // We should never get invalid scope depths for an UnwindDest; that
1173ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  // implies that the destination wasn't set up correctly.
1174ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  assert(Dest.getScopeDepth().isValid() && "invalid scope depth on EH dest?");
1175ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
1176f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  if (!HaveInsertPoint())
1177f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    return;
11781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1179f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // Create the branch.
1180ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  llvm::BranchInst *BI = Builder.CreateBr(Dest.getBlock());
1181f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1182838d796d020f6a8cd2b2d1e2a0a85c83bbf29543John McCall  // Calculate the innermost active cleanup.
1183838d796d020f6a8cd2b2d1e2a0a85c83bbf29543John McCall  EHScopeStack::stable_iterator
1184838d796d020f6a8cd2b2d1e2a0a85c83bbf29543John McCall    InnermostCleanup = EHStack.getInnermostActiveEHCleanup();
1185838d796d020f6a8cd2b2d1e2a0a85c83bbf29543John McCall
1186ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  // If the destination is in the same EH cleanup scope as us, we
1187ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  // don't need to thread through anything.
1188838d796d020f6a8cd2b2d1e2a0a85c83bbf29543John McCall  if (InnermostCleanup.encloses(Dest.getScopeDepth())) {
1189f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    Builder.ClearInsertionPoint();
119087eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson    return;
119187eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson  }
1192838d796d020f6a8cd2b2d1e2a0a85c83bbf29543John McCall  assert(InnermostCleanup != EHStack.stable_end());
11931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1194ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  // Store the index at the start.
1195ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  llvm::ConstantInt *Index = Builder.getInt32(Dest.getDestIndex());
1196ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  new llvm::StoreInst(Index, getEHCleanupDestSlot(), BI);
1197ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
1198ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  // Adjust BI to point to the first cleanup block.
1199ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  {
1200ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    EHCleanupScope &Scope =
1201838d796d020f6a8cd2b2d1e2a0a85c83bbf29543John McCall      cast<EHCleanupScope>(*EHStack.find(InnermostCleanup));
1202ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    BI->setSuccessor(0, CreateEHEntry(*this, Scope));
1203ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  }
1204ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
1205ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  // Add this destination to all the scopes involved.
1206ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  for (EHScopeStack::stable_iterator
1207838d796d020f6a8cd2b2d1e2a0a85c83bbf29543John McCall         I = InnermostCleanup, E = Dest.getScopeDepth(); ; ) {
1208ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    assert(E.strictlyEncloses(I));
1209ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.find(I));
1210ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    assert(Scope.isEHCleanup());
1211ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    I = Scope.getEnclosingEHCleanup();
1212ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
1213ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    // If this is the last cleanup we're propagating through, add this
1214ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    // as a branch-after.
1215ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    if (I == E) {
1216ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      Scope.addEHBranchAfter(Index, Dest.getBlock());
1217ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      break;
1218f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    }
1219ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
1220ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    // Otherwise, add it as a branch-through.  If this isn't new
1221ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    // information, all the rest of the work has been done before.
1222ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    if (!Scope.addEHBranchThrough(Dest.getBlock()))
1223ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      break;
1224f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  }
1225f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1226f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  Builder.ClearInsertionPoint();
122787eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson}
1228ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
1229ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall/// All the branch fixups on the EH stack have propagated out past the
1230ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall/// outermost normal cleanup; resolve them all by adding cases to the
1231ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall/// given switch instruction.
1232ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCallvoid CodeGenFunction::ResolveAllBranchFixups(llvm::SwitchInst *Switch) {
1233ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  llvm::SmallPtrSet<llvm::BasicBlock*, 4> CasesAdded;
1234ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
1235ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  for (unsigned I = 0, E = EHStack.getNumBranchFixups(); I != E; ++I) {
1236ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    // Skip this fixup if its destination isn't set or if we've
1237ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    // already treated it.
1238ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    BranchFixup &Fixup = EHStack.getBranchFixup(I);
1239ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    if (Fixup.Destination == 0) continue;
1240ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    if (!CasesAdded.insert(Fixup.Destination)) continue;
1241ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
1242ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    Switch->addCase(Builder.getInt32(Fixup.DestinationIndex),
1243ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall                    Fixup.Destination);
1244ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  }
1245ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
1246ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  EHStack.clearFixups();
1247ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall}
1248ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
1249ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCallvoid CodeGenFunction::ResolveBranchFixups(llvm::BasicBlock *Block) {
1250ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  assert(Block && "resolving a null target block");
1251ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  if (!EHStack.getNumBranchFixups()) return;
1252ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
1253ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  assert(EHStack.hasNormalCleanups() &&
1254ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall         "branch fixups exist with no normal cleanups on stack");
1255ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
1256ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  llvm::SmallPtrSet<llvm::BasicBlock*, 4> ModifiedOptimisticBlocks;
1257ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  bool ResolvedAny = false;
1258ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
1259ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  for (unsigned I = 0, E = EHStack.getNumBranchFixups(); I != E; ++I) {
1260ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    // Skip this fixup if its destination doesn't match.
1261ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    BranchFixup &Fixup = EHStack.getBranchFixup(I);
1262ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    if (Fixup.Destination != Block) continue;
1263ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
1264ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    Fixup.Destination = 0;
1265ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    ResolvedAny = true;
1266ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
1267ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    // If it doesn't have an optimistic branch block, LatestBranch is
1268ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    // already pointing to the right place.
1269ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    llvm::BasicBlock *BranchBB = Fixup.OptimisticBranchBlock;
1270ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    if (!BranchBB)
1271ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      continue;
1272ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
1273ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    // Don't process the same optimistic branch block twice.
1274ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    if (!ModifiedOptimisticBlocks.insert(BranchBB))
1275ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      continue;
1276ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
1277ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    llvm::SwitchInst *Switch = TransitionToCleanupSwitch(*this, BranchBB);
1278ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
1279ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    // Add a case to the switch.
1280ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    Switch->addCase(Builder.getInt32(Fixup.DestinationIndex), Block);
1281ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  }
1282ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
1283ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  if (ResolvedAny)
1284ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    EHStack.popNullFixups();
1285ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall}
1286ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
1287cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall/// Activate a cleanup that was created in an inactivated state.
1288cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCallvoid CodeGenFunction::ActivateCleanup(EHScopeStack::stable_iterator C) {
1289cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall  assert(C != EHStack.stable_end() && "activating bottom of stack?");
1290cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall  EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.find(C));
1291cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall  assert(!Scope.isActive() && "double activation");
1292cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall
1293cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall  // Calculate whether the cleanup was used:
1294cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall  bool Used = false;
1295cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall
1296cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall  //   - as a normal cleanup
1297cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall  if (Scope.isNormalCleanup()) {
1298cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall    bool NormalUsed = false;
1299cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall    if (Scope.getNormalBlock()) {
1300cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall      NormalUsed = true;
1301cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall    } else {
1302cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall      // Check whether any enclosed cleanups were needed.
1303cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall      for (EHScopeStack::stable_iterator
1304cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall             I = EHStack.getInnermostNormalCleanup(); I != C; ) {
1305cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall        assert(C.strictlyEncloses(I));
1306cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall        EHCleanupScope &S = cast<EHCleanupScope>(*EHStack.find(I));
1307cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall        if (S.getNormalBlock()) {
1308cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall          NormalUsed = true;
1309cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall          break;
1310cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall        }
1311cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall        I = S.getEnclosingNormalCleanup();
1312cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall      }
1313cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall    }
1314cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall
1315cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall    if (NormalUsed)
1316cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall      Used = true;
1317cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall    else
1318cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall      Scope.setActivatedBeforeNormalUse(true);
1319cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall  }
1320cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall
1321cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall  //  - as an EH cleanup
1322cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall  if (Scope.isEHCleanup()) {
1323cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall    bool EHUsed = false;
1324cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall    if (Scope.getEHBlock()) {
1325cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall      EHUsed = true;
1326cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall    } else {
1327cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall      // Check whether any enclosed cleanups were needed.
1328cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall      for (EHScopeStack::stable_iterator
1329cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall             I = EHStack.getInnermostEHCleanup(); I != C; ) {
1330cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall        assert(C.strictlyEncloses(I));
1331cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall        EHCleanupScope &S = cast<EHCleanupScope>(*EHStack.find(I));
1332cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall        if (S.getEHBlock()) {
1333cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall          EHUsed = true;
1334cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall          break;
1335cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall        }
1336cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall        I = S.getEnclosingEHCleanup();
1337cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall      }
1338cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall    }
1339cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall
1340cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall    if (EHUsed)
1341cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall      Used = true;
1342cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall    else
1343cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall      Scope.setActivatedBeforeEHUse(true);
1344cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall  }
1345cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall
1346cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall  llvm::AllocaInst *Var = EHCleanupScope::activeSentinel();
1347cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall  if (Used) {
1348cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall    Var = CreateTempAlloca(Builder.getInt1Ty());
1349cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall    InitTempAlloca(Var, Builder.getFalse());
1350cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall  }
1351cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall  Scope.setActiveVar(Var);
1352cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall}
1353cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall
1354ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCallllvm::Value *CodeGenFunction::getNormalCleanupDestSlot() {
1355ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  if (!NormalCleanupDest)
1356ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    NormalCleanupDest =
1357ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      CreateTempAlloca(Builder.getInt32Ty(), "cleanup.dest.slot");
1358ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  return NormalCleanupDest;
1359ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall}
1360ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
1361ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCallllvm::Value *CodeGenFunction::getEHCleanupDestSlot() {
1362ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  if (!EHCleanupDest)
1363ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    EHCleanupDest =
1364ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      CreateTempAlloca(Builder.getInt32Ty(), "eh.cleanup.dest.slot");
1365ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  return EHCleanupDest;
1366ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall}
13678d308384a220c7dc81755c47cdcbdd87dac25d5bDevang Patel
13688d308384a220c7dc81755c47cdcbdd87dac25d5bDevang Patelvoid CodeGenFunction::EmitDeclRefExprDbgValue(const DeclRefExpr *E,
136925c2c8fb9309050612009a6571e2660e75531348Devang Patel                                              llvm::ConstantInt *Init) {
137025c2c8fb9309050612009a6571e2660e75531348Devang Patel  assert (Init && "Invalid DeclRefExpr initializer!");
137125c2c8fb9309050612009a6571e2660e75531348Devang Patel  if (CGDebugInfo *Dbg = getDebugInfo())
137225c2c8fb9309050612009a6571e2660e75531348Devang Patel    Dbg->EmitGlobalVariable(E->getDecl(), Init, Builder);
13738d308384a220c7dc81755c47cdcbdd87dac25d5bDevang Patel}
1374