CodeGenFunction.cpp revision 1851a12605bc6f1ea70d11974a315340ebaab6eb
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- CodeGenFunction.cpp - Emit LLVM Code from ASTs for a Function ----===// 25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// The LLVM Compiler Infrastructure 45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source 60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details. 75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// This coordinates the per-function state used while generating code. 115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "CodeGenFunction.h" 155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "CodeGenModule.h" 163f2af1002249c8acc9ce17f1fc50324864feb8e1Eli Friedman#include "CGDebugInfo.h" 175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Basic/TargetInfo.h" 1831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner#include "clang/AST/APValue.h" 19de7fb8413b13651fd85b7125d08b3c9ac2816d9dDaniel Dunbar#include "clang/AST/ASTContext.h" 20c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel Dunbar#include "clang/AST/Decl.h" 212b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson#include "clang/AST/DeclCXX.h" 226a1e0eb557d47e85185e09bdf8721f53f4bf9c9cMike Stump#include "clang/AST/StmtCXX.h" 234e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump#include "llvm/Target/TargetData.h" 245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang; 255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace CodeGen; 265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 271eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpCodeGenFunction::CodeGenFunction(CodeGenModule &cgm) 28a4f668f3b7e03629066a01b04e415cb2b4655dafMike Stump : BlockFunction(cgm, *this, Builder), CGM(cgm), 29a4f668f3b7e03629066a01b04e415cb2b4655dafMike Stump Target(CGM.getContext().Target), 30aac8705c046f01a264a4f82832895a5d9e695633Owen Anderson Builder(cgm.getModule().getContext()), 31d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner DebugInfo(0), IndirectBranch(0), 323d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner SwitchInsn(0), CaseRangeBlock(0), InvokeDest(0), 33f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson CXXThisDecl(0), CXXVTTDecl(0), 3415037caa1542bb810ad54c653aeb80f61df7b00cMike Stump ConditionalBranchLevel(0), TerminateHandler(0), TrapBB(0), 35be07f60131bc6f8d6696f4644df1ef667a1730d5Mike Stump UniqueAggrDestructorCount(0) { 364e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump LLVMIntTy = ConvertType(getContext().IntTy); 374e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump LLVMPointerWidth = Target.getPointerWidth(0); 38d88ea5687968640ada2bc5a10211cbeb68a671ecMike Stump Exceptions = getContext().getLangOptions().Exceptions; 399c276ae0f24d4cee8f7954069d4b8eae45d0447dMike Stump CatchUndefined = getContext().getLangOptions().CatchUndefined; 404111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner} 415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerASTContext &CodeGenFunction::getContext() const { 435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return CGM.getContext(); 445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerllvm::BasicBlock *CodeGenFunction::getBasicBlockForLabel(const LabelStmt *S) { 485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer llvm::BasicBlock *&BB = LabelMap[S]; 495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (BB) return BB; 501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Create, but don't insert, the new block. 5255e874299f2ad827646a4ca9ea38c402aaeb38c9Daniel Dunbar return BB = createBasicBlock(S->getName()); 535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 550096acf421c4609ce7f43e8b05f8c5ca866d4611Daniel Dunbarllvm::Value *CodeGenFunction::GetAddrOfLocalVar(const VarDecl *VD) { 560096acf421c4609ce7f43e8b05f8c5ca866d4611Daniel Dunbar llvm::Value *Res = LocalDeclMap[VD]; 570096acf421c4609ce7f43e8b05f8c5ca866d4611Daniel Dunbar assert(Res && "Invalid argument to GetAddrOfLocalVar(), no decl!"); 580096acf421c4609ce7f43e8b05f8c5ca866d4611Daniel Dunbar return Res; 59813733577d33ec56479667b49e1bff42dc6bba90Lauro Ramos Venancio} 605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 610096acf421c4609ce7f43e8b05f8c5ca866d4611Daniel Dunbarllvm::Constant * 620096acf421c4609ce7f43e8b05f8c5ca866d4611Daniel DunbarCodeGenFunction::GetAddrOfStaticLocalVar(const VarDecl *BVD) { 630096acf421c4609ce7f43e8b05f8c5ca866d4611Daniel Dunbar return cast<llvm::Constant>(GetAddrOfLocalVar(BVD)); 64dde0a94120915fa925d1ffcdb997c7b44dc9fa21Anders Carlsson} 65dde0a94120915fa925d1ffcdb997c7b44dc9fa21Anders Carlsson 668b1a343b6b360d63d5dc8a6beb841ce4414c1e00Daniel Dunbarconst llvm::Type *CodeGenFunction::ConvertTypeForMem(QualType T) { 678b1a343b6b360d63d5dc8a6beb841ce4414c1e00Daniel Dunbar return CGM.getTypes().ConvertTypeForMem(T); 688b1a343b6b360d63d5dc8a6beb841ce4414c1e00Daniel Dunbar} 698b1a343b6b360d63d5dc8a6beb841ce4414c1e00Daniel Dunbar 705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerconst llvm::Type *CodeGenFunction::ConvertType(QualType T) { 715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return CGM.getTypes().ConvertType(T); 725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 744111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattnerbool CodeGenFunction::hasAggregateLLVMType(QualType T) { 75e9d34dc7afe06c9adaacad7a678a0cbbf749ea75Anders Carlsson return T->isRecordType() || T->isArrayType() || T->isAnyComplexType() || 76e9d34dc7afe06c9adaacad7a678a0cbbf749ea75Anders Carlsson T->isMemberFunctionPointerType(); 774111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner} 78391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner 791c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbarvoid CodeGenFunction::EmitReturnBlock() { 801c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar // For cleanliness, we try to avoid emitting the return block for 811c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar // simple cases. 821c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar llvm::BasicBlock *CurBB = Builder.GetInsertBlock(); 831c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar 841c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar if (CurBB) { 851c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar assert(!CurBB->getTerminator() && "Unexpected terminated block."); 861c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar 8796e18b05ea6b55aa92a1a576f29e9cee73a7e20bDaniel Dunbar // We have a valid insert point, reuse it if it is empty or there are no 8896e18b05ea6b55aa92a1a576f29e9cee73a7e20bDaniel Dunbar // explicit jumps to the return block. 8996e18b05ea6b55aa92a1a576f29e9cee73a7e20bDaniel Dunbar if (CurBB->empty() || ReturnBlock->use_empty()) { 9096e18b05ea6b55aa92a1a576f29e9cee73a7e20bDaniel Dunbar ReturnBlock->replaceAllUsesWith(CurBB); 911c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar delete ReturnBlock; 9296e18b05ea6b55aa92a1a576f29e9cee73a7e20bDaniel Dunbar } else 931c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar EmitBlock(ReturnBlock); 941c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar return; 951c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar } 961c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar 971c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar // Otherwise, if the return block is the target of a single direct 981c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar // branch then we can just put the code in that block instead. This 991c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar // cleans up functions which started with a unified return block. 1001c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar if (ReturnBlock->hasOneUse()) { 1011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::BranchInst *BI = 1021c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar dyn_cast<llvm::BranchInst>(*ReturnBlock->use_begin()); 1031c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar if (BI && BI->isUnconditional() && BI->getSuccessor(0) == ReturnBlock) { 1041c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar // Reset insertion point and delete the branch. 1051c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar Builder.SetInsertPoint(BI->getParent()); 1061c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar BI->eraseFromParent(); 1071c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar delete ReturnBlock; 1081c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar return; 1091c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar } 1101c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar } 1111c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar 112f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // FIXME: We are at an unreachable point, there is no reason to emit the block 113f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // unless it has uses. However, we still need a place to put the debug 114f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // region.end for now. 1151c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar 1161c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar EmitBlock(ReturnBlock); 1171c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar} 1181c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar 119af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbarvoid CodeGenFunction::FinishFunction(SourceLocation EndLoc) { 120391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner assert(BreakContinueStack.empty() && 121391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner "mismatched push/pop in break/continue stack!"); 122bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson assert(BlockScopes.empty() && 123bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson "did not remove all blocks from block scope map!"); 124bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson assert(CleanupEntries.empty() && 125bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson "mismatched push/pop in cleanup stack!"); 1261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // Emit function epilog (to return). 1281c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar EmitReturnBlock(); 129f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar 130f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar // Emit debug descriptor for function end. 131e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson if (CGDebugInfo *DI = getDebugInfo()) { 132f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar DI->setLocation(EndLoc); 133f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar DI->EmitRegionEnd(CurFn, Builder); 134f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar } 135f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar 13688b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar EmitFunctionEpilog(*CurFnInfo, ReturnValue); 137cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump EmitEndEHSpec(CurCodeDecl); 1385ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar 139d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner // If someone did an indirect goto, emit the indirect goto block at the end of 140d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner // the function. 141d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner if (IndirectBranch) { 142d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner EmitBlock(IndirectBranch->getParent()); 143d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner Builder.ClearInsertionPoint(); 144d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner } 145d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner 146391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner // Remove the AllocaInsertPt instruction, which is just a convenience for us. 147481769b5dc102b0256b35581e787909ad5edfab5Chris Lattner llvm::Instruction *Ptr = AllocaInsertPt; 148391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner AllocaInsertPt = 0; 149481769b5dc102b0256b35581e787909ad5edfab5Chris Lattner Ptr->eraseFromParent(); 150d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner 151d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner // If someone took the address of a label but never did an indirect goto, we 152d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner // made a zero entry PHI node, which is illegal, zap it now. 153d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner if (IndirectBranch) { 154d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner llvm::PHINode *PN = cast<llvm::PHINode>(IndirectBranch->getAddress()); 155d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner if (PN->getNumIncomingValues() == 0) { 156d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner PN->replaceAllUsesWith(llvm::UndefValue::get(PN->getType())); 157d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner PN->eraseFromParent(); 158d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner } 159d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner } 160c8aa5f1f264fb230c38182adab944232bb160c2bChris Lattner} 161c8aa5f1f264fb230c38182adab944232bb160c2bChris Lattner 1620ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlssonvoid CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy, 1637c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar llvm::Function *Fn, 1642284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar const FunctionArgList &Args, 1652284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar SourceLocation StartLoc) { 1660ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlsson const Decl *D = GD.getDecl(); 1670ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlsson 1684cc1a4703363ff940b6273aeef9d96a87edeb04bAnders Carlsson DidCallStackSave = false; 169b5437d238752dc297e42410e98d38d5250fe0463Chris Lattner CurCodeDecl = CurFuncDecl = D; 1707c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar FnRetTy = RetTy; 171bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar CurFn = Fn; 1725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer assert(CurFn->isDeclaration() && "Function already has body?"); 173ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner 17455e874299f2ad827646a4ca9ea38c402aaeb38c9Daniel Dunbar llvm::BasicBlock *EntryBB = createBasicBlock("entry", CurFn); 1755ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar 1765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Create a marker to make it easy to insert allocas into the entryblock 17755352a2d616cf9fbb621d10faf8b960b4b268bd8Chris Lattner // later. Don't create this with the builder, because we don't want it 17855352a2d616cf9fbb621d10faf8b960b4b268bd8Chris Lattner // folded. 1790032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::getInt32Ty(VMContext)); 180bcdc0f0aff015b6507560f50c71d1b7b6c6a9932Mike Stump AllocaInsertPt = new llvm::BitCastInst(Undef, 181bcdc0f0aff015b6507560f50c71d1b7b6c6a9932Mike Stump llvm::Type::getInt32Ty(VMContext), "", 18255352a2d616cf9fbb621d10faf8b960b4b268bd8Chris Lattner EntryBB); 183f1466848dce9c4c75d96a6cabdc8db560e26aac8Chris Lattner if (Builder.isNamePreserving()) 184f1466848dce9c4c75d96a6cabdc8db560e26aac8Chris Lattner AllocaInsertPt->setName("allocapt"); 1851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 18655e874299f2ad827646a4ca9ea38c402aaeb38c9Daniel Dunbar ReturnBlock = createBasicBlock("return"); 1871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 18855352a2d616cf9fbb621d10faf8b960b4b268bd8Chris Lattner Builder.SetInsertPoint(EntryBB); 1891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 19091cc815ffd13d4a78ae1b5bd617e19dd555de4f4Mike Stump QualType FnType = getContext().getFunctionType(RetTy, 0, 0, false, 0); 19191cc815ffd13d4a78ae1b5bd617e19dd555de4f4Mike Stump 192af99417156c652a6f04dff643925036dc3241d60Sanjiv Gupta // Emit subprogram debug descriptor. 193e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson if (CGDebugInfo *DI = getDebugInfo()) { 1942284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar DI->setLocation(StartLoc); 1959c6c3a0e3ae09626d2d4b04e4ffa42c3d7cab32bDevang Patel DI->EmitFunctionStart(GD, FnType, CurFn, Builder); 196af99417156c652a6f04dff643925036dc3241d60Sanjiv Gupta } 197af99417156c652a6f04dff643925036dc3241d60Sanjiv Gupta 19888b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar // FIXME: Leaked. 19904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall // CC info is ignored, hopefully? 20004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall CurFnInfo = &CGM.getTypes().getFunctionInfo(FnRetTy, Args, 20104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall CC_Default, false); 202b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman 203b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman if (RetTy->isVoidType()) { 204b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman // Void type; nothing to return. 205b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman ReturnValue = 0; 206b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman } else if (CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect && 207b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman hasAggregateLLVMType(CurFnInfo->getReturnType())) { 208b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman // Indirect aggregate return; emit returned value directly into sret slot. 209b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman // This reduces code size, and is also affects correctness in C++. 210b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman ReturnValue = CurFn->arg_begin(); 211b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman } else { 212b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman ReturnValue = CreateTempAlloca(ConvertType(RetTy), "retval"); 213b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman } 214b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman 215cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump EmitStartEHSpec(CurCodeDecl); 21688b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar EmitFunctionProlog(*CurFnInfo, CurFn, Args); 2171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 218751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson // If any of the arguments have a variably modified type, make sure to 219751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson // emit the type size. 220751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end(); 221751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson i != e; ++i) { 222751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson QualType Ty = i->second; 223751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson 224751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson if (Ty->isVariablyModifiedType()) 225751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson EmitVLASize(Ty); 226751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson } 2277c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar} 228eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman 229c997d4278d329e18891aac9698fb991b2d4622ebAnders Carlssonvoid CodeGenFunction::GenerateCode(GlobalDecl GD, llvm::Function *Fn) { 2300ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlsson const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl()); 2310ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlsson 232e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson // Check if we should generate debug info for this function. 2331feade8e520be483293dbf55eb57a51720899589Mike Stump if (CGM.getDebugInfo() && !FD->hasAttr<NoDebugAttr>()) 234e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson DebugInfo = CGM.getDebugInfo(); 2351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2367c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar FunctionArgList Args; 2371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2386a1e0eb557d47e85185e09bdf8721f53f4bf9c9cMike Stump CurGD = GD; 2396a1e0eb557d47e85185e09bdf8721f53f4bf9c9cMike Stump OuterTryBlock = 0; 2402b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { 2412b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson if (MD->isInstance()) { 2422b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson // Create the implicit 'this' decl. 2432b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson // FIXME: I'm not entirely sure I like using a fake decl just for code 2442b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson // generation. Maybe we can come up with a better way? 2452b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson CXXThisDecl = ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 2461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump &getContext().Idents.get("this"), 2472b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson MD->getThisType(getContext())); 2482b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson Args.push_back(std::make_pair(CXXThisDecl, CXXThisDecl->getType())); 249f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson 250f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson // Check if we need a VTT parameter as well. 251c997d4278d329e18891aac9698fb991b2d4622ebAnders Carlsson if (CGVtableInfo::needsVTTParameter(GD)) { 252f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson // FIXME: The comment about using a fake decl above applies here too. 253f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson QualType T = getContext().getPointerType(getContext().VoidPtrTy); 254f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson CXXVTTDecl = 255f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 256f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson &getContext().Idents.get("vtt"), T); 257f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson Args.push_back(std::make_pair(CXXVTTDecl, CXXVTTDecl->getType())); 258f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson } 2592b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson } 2602b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson } 2611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 262eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman if (FD->getNumParams()) { 263183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall const FunctionProtoType* FProto = FD->getType()->getAs<FunctionProtoType>(); 264eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman assert(FProto && "Function def must have prototype!"); 2657c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar 2667c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) 2671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Args.push_back(std::make_pair(FD->getParamDecl(i), 2687c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar FProto->getArgType(i))); 2695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 270af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 2716fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis if (const CompoundStmt *S = FD->getCompoundBody()) { 2720ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlsson StartFunction(GD, FD->getResultType(), Fn, Args, S->getLBracLoc()); 2734365bba95be15342575b4f030c6583a770a5da3dAnders Carlsson 2744365bba95be15342575b4f030c6583a770a5da3dAnders Carlsson if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) { 275de1d26b9c1d8823b173e4d77015ad88b4da70559Anders Carlsson EmitCtorPrologue(CD, GD.getCtorType()); 2764365bba95be15342575b4f030c6583a770a5da3dAnders Carlsson EmitStmt(S); 2775e1b91875c275f0ec50d3680afbac150d684fdbaAnders Carlsson 2785e1b91875c275f0ec50d3680afbac150d684fdbaAnders Carlsson // If any of the member initializers are temporaries bound to references 2795e1b91875c275f0ec50d3680afbac150d684fdbaAnders Carlsson // make sure to emit their destructors. 2805e1b91875c275f0ec50d3680afbac150d684fdbaAnders Carlsson EmitCleanupBlocks(0); 2815e1b91875c275f0ec50d3680afbac150d684fdbaAnders Carlsson 2824365bba95be15342575b4f030c6583a770a5da3dAnders Carlsson } else if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD)) { 2834365bba95be15342575b4f030c6583a770a5da3dAnders Carlsson llvm::BasicBlock *DtorEpilogue = createBasicBlock("dtor.epilogue"); 2844365bba95be15342575b4f030c6583a770a5da3dAnders Carlsson PushCleanupBlock(DtorEpilogue); 2854365bba95be15342575b4f030c6583a770a5da3dAnders Carlsson 2861851a12605bc6f1ea70d11974a315340ebaab6ebAnders Carlsson InitializeVtablePtrs(DD->getParent()); 2871851a12605bc6f1ea70d11974a315340ebaab6ebAnders Carlsson 2884365bba95be15342575b4f030c6583a770a5da3dAnders Carlsson EmitStmt(S); 289c33e4ba20304e222692e77f2c6ad26a5d8d32f83Anders Carlsson 290c33e4ba20304e222692e77f2c6ad26a5d8d32f83Anders Carlsson CleanupBlockInfo Info = PopCleanupBlock(); 291c33e4ba20304e222692e77f2c6ad26a5d8d32f83Anders Carlsson 292c33e4ba20304e222692e77f2c6ad26a5d8d32f83Anders Carlsson assert(Info.CleanupBlock == DtorEpilogue && "Block mismatch!"); 293c33e4ba20304e222692e77f2c6ad26a5d8d32f83Anders Carlsson EmitBlock(DtorEpilogue); 294de1d26b9c1d8823b173e4d77015ad88b4da70559Anders Carlsson EmitDtorEpilogue(DD, GD.getDtorType()); 295c33e4ba20304e222692e77f2c6ad26a5d8d32f83Anders Carlsson 296c33e4ba20304e222692e77f2c6ad26a5d8d32f83Anders Carlsson if (Info.SwitchBlock) 297c33e4ba20304e222692e77f2c6ad26a5d8d32f83Anders Carlsson EmitBlock(Info.SwitchBlock); 298c33e4ba20304e222692e77f2c6ad26a5d8d32f83Anders Carlsson if (Info.EndBlock) 299c33e4ba20304e222692e77f2c6ad26a5d8d32f83Anders Carlsson EmitBlock(Info.EndBlock); 3004365bba95be15342575b4f030c6583a770a5da3dAnders Carlsson } else { 3014365bba95be15342575b4f030c6583a770a5da3dAnders Carlsson // Just a regular function, emit its body. 3024365bba95be15342575b4f030c6583a770a5da3dAnders Carlsson EmitStmt(S); 303c33e4ba20304e222692e77f2c6ad26a5d8d32f83Anders Carlsson } 3044365bba95be15342575b4f030c6583a770a5da3dAnders Carlsson 305d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl FinishFunction(S->getRBracLoc()); 3064513272f444c59705123ccb6207414ce62c9a568Douglas Gregor } else if (FD->isImplicit()) { 3074513272f444c59705123ccb6207414ce62c9a568Douglas Gregor const CXXRecordDecl *ClassDecl = 3084513272f444c59705123ccb6207414ce62c9a568Douglas Gregor cast<CXXRecordDecl>(FD->getDeclContext()); 3094513272f444c59705123ccb6207414ce62c9a568Douglas Gregor (void) ClassDecl; 310c7ff8e19081c2e974f05f66c4fa9b40750fc655fFariborz Jahanian if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) { 3114513272f444c59705123ccb6207414ce62c9a568Douglas Gregor // FIXME: For C++0x, we want to look for implicit *definitions* of 3124513272f444c59705123ccb6207414ce62c9a568Douglas Gregor // these special member functions, rather than implicit *declarations*. 3139e9199d8649cf3e10c98a69403f05dbb666d8fb1Douglas Gregor if (CD->isCopyConstructor()) { 3149889652dbc10060cd604861ed2e5bc6719f845b0Fariborz Jahanian assert(!ClassDecl->hasUserDeclaredCopyConstructor() && 3154513272f444c59705123ccb6207414ce62c9a568Douglas Gregor "Cannot synthesize a non-implicit copy constructor"); 316de1d26b9c1d8823b173e4d77015ad88b4da70559Anders Carlsson SynthesizeCXXCopyConstructor(CD, GD.getCtorType(), Fn, Args); 3174513272f444c59705123ccb6207414ce62c9a568Douglas Gregor } else if (CD->isDefaultConstructor()) { 3189889652dbc10060cd604861ed2e5bc6719f845b0Fariborz Jahanian assert(!ClassDecl->hasUserDeclaredConstructor() && 3194513272f444c59705123ccb6207414ce62c9a568Douglas Gregor "Cannot synthesize a non-implicit default constructor."); 320de1d26b9c1d8823b173e4d77015ad88b4da70559Anders Carlsson SynthesizeDefaultConstructor(CD, GD.getCtorType(), Fn, Args); 3214513272f444c59705123ccb6207414ce62c9a568Douglas Gregor } else { 3224513272f444c59705123ccb6207414ce62c9a568Douglas Gregor assert(false && "Implicit constructor cannot be synthesized"); 3239889652dbc10060cd604861ed2e5bc6719f845b0Fariborz Jahanian } 3244513272f444c59705123ccb6207414ce62c9a568Douglas Gregor } else if (const CXXDestructorDecl *CD = dyn_cast<CXXDestructorDecl>(FD)) { 3254513272f444c59705123ccb6207414ce62c9a568Douglas Gregor assert(!ClassDecl->hasUserDeclaredDestructor() && 3264513272f444c59705123ccb6207414ce62c9a568Douglas Gregor "Cannot synthesize a non-implicit destructor"); 3274513272f444c59705123ccb6207414ce62c9a568Douglas Gregor SynthesizeDefaultDestructor(CD, GD.getDtorType(), Fn, Args); 3284513272f444c59705123ccb6207414ce62c9a568Douglas Gregor } else if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { 3294513272f444c59705123ccb6207414ce62c9a568Douglas Gregor assert(MD->isCopyAssignment() && 3304513272f444c59705123ccb6207414ce62c9a568Douglas Gregor !ClassDecl->hasUserDeclaredCopyAssignment() && 3314513272f444c59705123ccb6207414ce62c9a568Douglas Gregor "Cannot synthesize a method that is not an implicit-defined " 3324513272f444c59705123ccb6207414ce62c9a568Douglas Gregor "copy constructor"); 333de1d26b9c1d8823b173e4d77015ad88b4da70559Anders Carlsson SynthesizeCXXCopyAssignment(MD, Fn, Args); 3344513272f444c59705123ccb6207414ce62c9a568Douglas Gregor } else { 3354513272f444c59705123ccb6207414ce62c9a568Douglas Gregor assert(false && "Cannot synthesize unknown implicit function"); 3364513272f444c59705123ccb6207414ce62c9a568Douglas Gregor } 3376a1e0eb557d47e85185e09bdf8721f53f4bf9c9cMike Stump } else if (const Stmt *S = FD->getBody()) { 3386a1e0eb557d47e85185e09bdf8721f53f4bf9c9cMike Stump if (const CXXTryStmt *TS = dyn_cast<CXXTryStmt>(S)) { 3396a1e0eb557d47e85185e09bdf8721f53f4bf9c9cMike Stump OuterTryBlock = TS; 3406a1e0eb557d47e85185e09bdf8721f53f4bf9c9cMike Stump StartFunction(GD, FD->getResultType(), Fn, Args, TS->getTryLoc()); 3416a1e0eb557d47e85185e09bdf8721f53f4bf9c9cMike Stump EmitStmt(TS); 3426a1e0eb557d47e85185e09bdf8721f53f4bf9c9cMike Stump FinishFunction(TS->getEndLoc()); 3436a1e0eb557d47e85185e09bdf8721f53f4bf9c9cMike Stump } 3440ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlsson } 3451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3462b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson // Destroy the 'this' declaration. 3472b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson if (CXXThisDecl) 3482b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson CXXThisDecl->Destroy(getContext()); 349f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson 350f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson // Destroy the VTT declaration. 351f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson if (CXXVTTDecl) 352f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson CXXVTTDecl->Destroy(getContext()); 3535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 3545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 3550946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner/// ContainsLabel - Return true if the statement contains a label in it. If 3560946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner/// this statement is not executed normally, it not containing a label means 3570946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner/// that we can just remove the code. 3580946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattnerbool CodeGenFunction::ContainsLabel(const Stmt *S, bool IgnoreCaseStmts) { 3590946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner // Null statement, not a label! 3600946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner if (S == 0) return false; 3611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3620946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner // If this is a label, we have to emit the code, consider something like: 3630946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner // if (0) { ... foo: bar(); } goto foo; 3640946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner if (isa<LabelStmt>(S)) 3650946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner return true; 3661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3670946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner // If this is a case/default statement, and we haven't seen a switch, we have 3680946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner // to emit the code. 3690946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner if (isa<SwitchCase>(S) && !IgnoreCaseStmts) 3700946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner return true; 3711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3720946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner // If this is a switch statement, we want to ignore cases below it. 3730946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner if (isa<SwitchStmt>(S)) 3740946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner IgnoreCaseStmts = true; 3751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3760946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner // Scan subexpressions for verboten labels. 3770946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end(); 3780946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner I != E; ++I) 3790946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner if (ContainsLabel(*I, IgnoreCaseStmts)) 3800946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner return true; 3811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3820946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner return false; 3830946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner} 3840946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner 38531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner 38631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// ConstantFoldsToSimpleInteger - If the sepcified expression does not fold to 38731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// a constant, or if it does but contains a label, return 0. If it constant 38831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// folds to 'true' and does not contain a label, return 1, if it constant folds 38931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// to 'false' and does not contain a label, return -1. 39031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattnerint CodeGenFunction::ConstantFoldsToSimpleInteger(const Expr *Cond) { 39136bc14c3a1cf63ee306df5687ac8e85f924f8639Daniel Dunbar // FIXME: Rename and handle conversion of other evaluatable things 39236bc14c3a1cf63ee306df5687ac8e85f924f8639Daniel Dunbar // to bool. 39364712f196bffd41fb0552c2643b07a25c3e45082Anders Carlsson Expr::EvalResult Result; 3941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (!Cond->Evaluate(Result, getContext()) || !Result.Val.isInt() || 39564712f196bffd41fb0552c2643b07a25c3e45082Anders Carlsson Result.HasSideEffects) 396ef5a66d8171eb95e948107f8ee7707b360aaff25Anders Carlsson return 0; // Not foldable, not integer or not fully evaluatable. 3971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 39831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner if (CodeGenFunction::ContainsLabel(Cond)) 39931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner return 0; // Contains a label. 4001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 40164712f196bffd41fb0552c2643b07a25c3e45082Anders Carlsson return Result.Val.getInt().getBoolValue() ? 1 : -1; 40231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner} 40331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner 40431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner 40531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an if 40631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// statement) to the specified blocks. Based on the condition, this might try 40731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// to simplify the codegen of the conditional based on the branch. 40831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// 40931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattnervoid CodeGenFunction::EmitBranchOnBoolExpr(const Expr *Cond, 41031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner llvm::BasicBlock *TrueBlock, 41131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner llvm::BasicBlock *FalseBlock) { 41231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner if (const ParenExpr *PE = dyn_cast<ParenExpr>(Cond)) 41331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner return EmitBranchOnBoolExpr(PE->getSubExpr(), TrueBlock, FalseBlock); 4141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 41531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner if (const BinaryOperator *CondBOp = dyn_cast<BinaryOperator>(Cond)) { 41631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // Handle X && Y in a condition. 41731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner if (CondBOp->getOpcode() == BinaryOperator::LAnd) { 41831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // If we have "1 && X", simplify the code. "0 && X" would have constant 41931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // folded if the case was simple enough. 42031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == 1) { 42131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // br(1 && X) -> br(X). 42231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); 42331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner } 4241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 42531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // If we have "X && 1", simplify the code to use an uncond branch. 42631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // "X && 0" would have been constant folded to 0. 42731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == 1) { 42831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // br(X && 1) -> br(X). 42931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock); 43031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner } 4311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 43231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // Emit the LHS as a conditional. If the LHS conditional is false, we 43331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // want to jump to the FalseBlock. 4349615ecb44f549ae9fa2b4db6ff46bc78befbf62cDaniel Dunbar llvm::BasicBlock *LHSTrue = createBasicBlock("land.lhs.true"); 43531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner EmitBranchOnBoolExpr(CondBOp->getLHS(), LHSTrue, FalseBlock); 43631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner EmitBlock(LHSTrue); 4371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 43808e9e453f40aff95a59bd67db49b8f050765e1f0Anders Carlsson // Any temporaries created here are conditional. 43972119a887c4af39c90f10b6bdc158389071f42eaAnders Carlsson BeginConditionalBranch(); 44031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); 44172119a887c4af39c90f10b6bdc158389071f42eaAnders Carlsson EndConditionalBranch(); 44208e9e453f40aff95a59bd67db49b8f050765e1f0Anders Carlsson 44331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner return; 44431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner } else if (CondBOp->getOpcode() == BinaryOperator::LOr) { 44531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // If we have "0 || X", simplify the code. "1 || X" would have constant 44631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // folded if the case was simple enough. 44731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == -1) { 44831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // br(0 || X) -> br(X). 44931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); 45031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner } 4511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 45231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // If we have "X || 0", simplify the code to use an uncond branch. 45331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // "X || 1" would have been constant folded to 1. 45431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == -1) { 45531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // br(X || 0) -> br(X). 45631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock); 45731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner } 4581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 45931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // Emit the LHS as a conditional. If the LHS conditional is true, we 46031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // want to jump to the TrueBlock. 4619615ecb44f549ae9fa2b4db6ff46bc78befbf62cDaniel Dunbar llvm::BasicBlock *LHSFalse = createBasicBlock("lor.lhs.false"); 46231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, LHSFalse); 46331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner EmitBlock(LHSFalse); 4641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 46508e9e453f40aff95a59bd67db49b8f050765e1f0Anders Carlsson // Any temporaries created here are conditional. 46672119a887c4af39c90f10b6bdc158389071f42eaAnders Carlsson BeginConditionalBranch(); 46731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); 46872119a887c4af39c90f10b6bdc158389071f42eaAnders Carlsson EndConditionalBranch(); 46908e9e453f40aff95a59bd67db49b8f050765e1f0Anders Carlsson 47031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner return; 47131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner } 472552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner } 4731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 474552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner if (const UnaryOperator *CondUOp = dyn_cast<UnaryOperator>(Cond)) { 475552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner // br(!x, t, f) -> br(x, f, t) 476552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner if (CondUOp->getOpcode() == UnaryOperator::LNot) 477552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner return EmitBranchOnBoolExpr(CondUOp->getSubExpr(), FalseBlock, TrueBlock); 47831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner } 4791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 48009b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar if (const ConditionalOperator *CondOp = dyn_cast<ConditionalOperator>(Cond)) { 48109b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar // Handle ?: operator. 48209b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar 48309b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar // Just ignore GNU ?: extension. 48409b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar if (CondOp->getLHS()) { 48509b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar // br(c ? x : y, t, f) -> br(c, br(x, t, f), br(y, t, f)) 48609b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true"); 48709b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false"); 48809b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar EmitBranchOnBoolExpr(CondOp->getCond(), LHSBlock, RHSBlock); 48909b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar EmitBlock(LHSBlock); 49009b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar EmitBranchOnBoolExpr(CondOp->getLHS(), TrueBlock, FalseBlock); 49109b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar EmitBlock(RHSBlock); 49209b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar EmitBranchOnBoolExpr(CondOp->getRHS(), TrueBlock, FalseBlock); 49309b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar return; 49409b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar } 49509b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar } 49609b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar 49731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // Emit the code with the fully general case. 49831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner llvm::Value *CondV = EvaluateExprAsBool(Cond); 49931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner Builder.CreateCondBr(CondV, TrueBlock, FalseBlock); 50031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner} 50131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner 502488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar/// ErrorUnsupported - Print out an error that codegen doesn't support the 503dc5e8268292046114ffe02e48773572a91a310f1Chris Lattner/// specified stmt yet. 50490df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbarvoid CodeGenFunction::ErrorUnsupported(const Stmt *S, const char *Type, 50590df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar bool OmitOnError) { 50690df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar CGM.ErrorUnsupported(S, Type, OmitOnError); 507dc5e8268292046114ffe02e48773572a91a310f1Chris Lattner} 508dc5e8268292046114ffe02e48773572a91a310f1Chris Lattner 50988207c9ca218486b93ae7df14e9764cd0c2c3383Chris Lattnervoid CodeGenFunction::EmitMemSetToZero(llvm::Value *DestPtr, QualType Ty) { 51036afd38fb749ab34c8341a1dc70366bca904eaa3Chris Lattner const llvm::Type *BP = llvm::Type::getInt8PtrTy(VMContext); 5113d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson if (DestPtr->getType() != BP) 5123d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp"); 5133d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson 5143d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson // Get size and alignment info for this aggregate. 5153d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty); 5163d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson 51788207c9ca218486b93ae7df14e9764cd0c2c3383Chris Lattner // Don't bother emitting a zero-byte memset. 51888207c9ca218486b93ae7df14e9764cd0c2c3383Chris Lattner if (TypeInfo.first == 0) 51988207c9ca218486b93ae7df14e9764cd0c2c3383Chris Lattner return; 5201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 5213d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson // FIXME: Handle variable sized types. 5221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const llvm::Type *IntPtr = llvm::IntegerType::get(VMContext, 5230032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson LLVMPointerWidth); 5243d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson 5253d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson Builder.CreateCall4(CGM.getMemSetFn(), DestPtr, 5260032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson llvm::Constant::getNullValue(llvm::Type::getInt8Ty(VMContext)), 5273d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson // TypeInfo.first describes size in bits. 5284a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson llvm::ConstantInt::get(IntPtr, TypeInfo.first/8), 5291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 5303d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson TypeInfo.second/8)); 5313d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson} 5323d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson 533d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattnerllvm::BlockAddress *CodeGenFunction::GetAddrOfLabel(const LabelStmt *L) { 534d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner // Make sure that there is a block for the indirect goto. 535d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner if (IndirectBranch == 0) 536d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner GetIndirectGotoBlock(); 5373d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner 538d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner llvm::BasicBlock *BB = getBasicBlockForLabel(L); 5393d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner 540d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner // Make sure the indirect branch includes all of the address-taken blocks. 541d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner IndirectBranch->addDestination(BB); 542d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner return llvm::BlockAddress::get(CurFn, BB); 5433d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner} 5441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 5453d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattnerllvm::BasicBlock *CodeGenFunction::GetIndirectGotoBlock() { 546d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner // If we already made the indirect branch for indirect goto, return its block. 547d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner if (IndirectBranch) return IndirectBranch->getParent(); 5483d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner 549d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner CGBuilderTy TmpBuilder(createBasicBlock("indirectgoto")); 5503d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner 551d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext); 55285e74acfcfb0c835a2e6c1adab314e997917039aChris Lattner 5533d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner // Create the PHI node that indirect gotos will add entries to. 554d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner llvm::Value *DestVal = TmpBuilder.CreatePHI(Int8PtrTy, "indirect.goto.dest"); 5553d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner 556d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner // Create the indirect branch instruction. 557d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner IndirectBranch = TmpBuilder.CreateIndirectBr(DestVal); 558d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner return IndirectBranch->getParent(); 5590ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar} 560ddf7cac45d85b73127adbbd91a2b28fc7291c57eAnders Carlsson 561d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbarllvm::Value *CodeGenFunction::GetVLASize(const VariableArrayType *VAT) { 562bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman llvm::Value *&SizeEntry = VLASizeMap[VAT->getSizeExpr()]; 5631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 564f666b7780d04186521adcaedb0e15dfa4d5e6933Anders Carlsson assert(SizeEntry && "Did not emit size for type"); 565f666b7780d04186521adcaedb0e15dfa4d5e6933Anders Carlsson return SizeEntry; 566f666b7780d04186521adcaedb0e15dfa4d5e6933Anders Carlsson} 567dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlsson 568d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbarllvm::Value *CodeGenFunction::EmitVLASize(QualType Ty) { 56960d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson assert(Ty->isVariablyModifiedType() && 57060d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson "Must pass variably modified type to EmitVLASizes!"); 5711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 572d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar EnsureInsertPoint(); 5731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 57460d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson if (const VariableArrayType *VAT = getContext().getAsVariableArrayType(Ty)) { 575bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman llvm::Value *&SizeEntry = VLASizeMap[VAT->getSizeExpr()]; 5761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 577fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson if (!SizeEntry) { 57896f214776c0f69069fee4d67557c8c7f416009a8Anders Carlsson const llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); 5791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 580ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner // Get the element size; 581ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner QualType ElemTy = VAT->getElementType(); 582ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner llvm::Value *ElemSize; 583fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson if (ElemTy->isVariableArrayType()) 584fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson ElemSize = EmitVLASize(ElemTy); 585ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner else 5864a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson ElemSize = llvm::ConstantInt::get(SizeTy, 587199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck getContext().getTypeSizeInChars(ElemTy).getQuantity()); 5881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 589fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson llvm::Value *NumElements = EmitScalarExpr(VAT->getSizeExpr()); 59096f214776c0f69069fee4d67557c8c7f416009a8Anders Carlsson NumElements = Builder.CreateIntCast(NumElements, SizeTy, false, "tmp"); 5911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 592fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson SizeEntry = Builder.CreateMul(ElemSize, NumElements); 593fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson } 5941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 59560d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson return SizeEntry; 596dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlsson } 5971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 598ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner if (const ArrayType *AT = dyn_cast<ArrayType>(Ty)) { 599ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner EmitVLASize(AT->getElementType()); 600ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner return 0; 6011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump } 6021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 603ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner const PointerType *PT = Ty->getAs<PointerType>(); 604ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner assert(PT && "unknown VM type!"); 605ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner EmitVLASize(PT->getPointeeType()); 60660d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson return 0; 607dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlsson} 6084fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman 6094fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedmanllvm::Value* CodeGenFunction::EmitVAListRef(const Expr* E) { 6104fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman if (CGM.getContext().getBuiltinVaListType()->isArrayType()) { 6114fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman return EmitScalarExpr(E); 6124fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman } 6134fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman return EmitLValue(E).getAddress(); 6144fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman} 6156ccc47698d0311ddabf32fa0f6db8e4f09ac96f8Anders Carlsson 6167799621ad7d16f3c6a1aeda024312d63ecd77777Fariborz Jahanianvoid CodeGenFunction::PushCleanupBlock(llvm::BasicBlock *CleanupEntryBlock, 61799533834ba8f3658559f334e68a518ebb6388ceaMike Stump llvm::BasicBlock *CleanupExitBlock, 618d88ea5687968640ada2bc5a10211cbeb68a671ecMike Stump llvm::BasicBlock *PreviousInvokeDest, 61999533834ba8f3658559f334e68a518ebb6388ceaMike Stump bool EHOnly) { 62099533834ba8f3658559f334e68a518ebb6388ceaMike Stump CleanupEntries.push_back(CleanupEntry(CleanupEntryBlock, CleanupExitBlock, 621d88ea5687968640ada2bc5a10211cbeb68a671ecMike Stump PreviousInvokeDest, EHOnly)); 6226ccc47698d0311ddabf32fa0f6db8e4f09ac96f8Anders Carlsson} 623c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson 6241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid CodeGenFunction::EmitCleanupBlocks(size_t OldCleanupStackSize) { 6251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump assert(CleanupEntries.size() >= OldCleanupStackSize && 626c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson "Cleanup stack mismatch!"); 6271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 628c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson while (CleanupEntries.size() > OldCleanupStackSize) 629c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson EmitCleanupBlock(); 630c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson} 631c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson 6321eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpCodeGenFunction::CleanupBlockInfo CodeGenFunction::PopCleanupBlock() { 633bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson CleanupEntry &CE = CleanupEntries.back(); 6341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 6357799621ad7d16f3c6a1aeda024312d63ecd77777Fariborz Jahanian llvm::BasicBlock *CleanupEntryBlock = CE.CleanupEntryBlock; 6361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 637bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson std::vector<llvm::BasicBlock *> Blocks; 638bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson std::swap(Blocks, CE.Blocks); 6391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 640bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson std::vector<llvm::BranchInst *> BranchFixups; 641bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson std::swap(BranchFixups, CE.BranchFixups); 6421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 64399533834ba8f3658559f334e68a518ebb6388ceaMike Stump bool EHOnly = CE.EHOnly; 64499533834ba8f3658559f334e68a518ebb6388ceaMike Stump 645d88ea5687968640ada2bc5a10211cbeb68a671ecMike Stump setInvokeDest(CE.PreviousInvokeDest); 646d88ea5687968640ada2bc5a10211cbeb68a671ecMike Stump 647bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson CleanupEntries.pop_back(); 648bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson 649ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson // Check if any branch fixups pointed to the scope we just popped. If so, 650ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson // we can remove them. 651ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson for (size_t i = 0, e = BranchFixups.size(); i != e; ++i) { 652ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson llvm::BasicBlock *Dest = BranchFixups[i]->getSuccessor(0); 653ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson BlockScopeMap::iterator I = BlockScopes.find(Dest); 6541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 655ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson if (I == BlockScopes.end()) 656ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson continue; 6571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 658ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson assert(I->second <= CleanupEntries.size() && "Invalid branch fixup!"); 6591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 660ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson if (I->second == CleanupEntries.size()) { 661ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson // We don't need to do this branch fixup. 662ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson BranchFixups[i] = BranchFixups.back(); 663ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson BranchFixups.pop_back(); 664ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson i--; 665ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson e--; 666ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson continue; 6671093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson } 6681093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson } 6691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 6707799621ad7d16f3c6a1aeda024312d63ecd77777Fariborz Jahanian llvm::BasicBlock *SwitchBlock = CE.CleanupExitBlock; 671bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson llvm::BasicBlock *EndBlock = 0; 6721093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson if (!BranchFixups.empty()) { 6737799621ad7d16f3c6a1aeda024312d63ecd77777Fariborz Jahanian if (!SwitchBlock) 6747799621ad7d16f3c6a1aeda024312d63ecd77777Fariborz Jahanian SwitchBlock = createBasicBlock("cleanup.switch"); 675bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson EndBlock = createBasicBlock("cleanup.end"); 6761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 677bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson llvm::BasicBlock *CurBB = Builder.GetInsertBlock(); 6781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 679bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson Builder.SetInsertPoint(SwitchBlock); 680bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson 68199533834ba8f3658559f334e68a518ebb6388ceaMike Stump llvm::Value *DestCodePtr 68299533834ba8f3658559f334e68a518ebb6388ceaMike Stump = CreateTempAlloca(llvm::Type::getInt32Ty(VMContext), 68399533834ba8f3658559f334e68a518ebb6388ceaMike Stump "cleanup.dst"); 6841093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson llvm::Value *DestCode = Builder.CreateLoad(DestCodePtr, "tmp"); 6851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 6861093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson // Create a switch instruction to determine where to jump next. 6871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::SwitchInst *SI = Builder.CreateSwitch(DestCode, EndBlock, 6881093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson BranchFixups.size()); 689bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson 69046831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson // Restore the current basic block (if any) 6910ae7b2b2f964bd6145d65ef52dc2a28025b2bd06Anders Carlsson if (CurBB) { 69246831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson Builder.SetInsertPoint(CurBB); 6931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 6940ae7b2b2f964bd6145d65ef52dc2a28025b2bd06Anders Carlsson // If we had a current basic block, we also need to emit an instruction 6950ae7b2b2f964bd6145d65ef52dc2a28025b2bd06Anders Carlsson // to initialize the cleanup destination. 6960032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson Builder.CreateStore(llvm::Constant::getNullValue(llvm::Type::getInt32Ty(VMContext)), 6970ae7b2b2f964bd6145d65ef52dc2a28025b2bd06Anders Carlsson DestCodePtr); 6980ae7b2b2f964bd6145d65ef52dc2a28025b2bd06Anders Carlsson } else 69946831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson Builder.ClearInsertionPoint(); 700bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson 7011093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson for (size_t i = 0, e = BranchFixups.size(); i != e; ++i) { 7021093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson llvm::BranchInst *BI = BranchFixups[i]; 7031093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson llvm::BasicBlock *Dest = BI->getSuccessor(0); 7041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 7051093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson // Fixup the branch instruction to point to the cleanup block. 7067799621ad7d16f3c6a1aeda024312d63ecd77777Fariborz Jahanian BI->setSuccessor(0, CleanupEntryBlock); 7071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 7081093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson if (CleanupEntries.empty()) { 709cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson llvm::ConstantInt *ID; 7101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 711cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson // Check if we already have a destination for this block. 712cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson if (Dest == SI->getDefaultDest()) 7130032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson ID = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 0); 714cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson else { 715cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson ID = SI->findCaseDest(Dest); 716cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson if (!ID) { 717cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson // No code found, get a new unique one by using the number of 718cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson // switch successors. 7191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump ID = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 720cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson SI->getNumSuccessors()); 721cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson SI->addCase(ID, Dest); 722cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson } 723cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson } 7241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 725cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson // Store the jump destination before the branch instruction. 726cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson new llvm::StoreInst(ID, DestCodePtr, BI); 7271093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson } else { 7281093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson // We need to jump through another cleanup block. Create a pad block 72999533834ba8f3658559f334e68a518ebb6388ceaMike Stump // with a branch instruction that jumps to the final destination and add 73099533834ba8f3658559f334e68a518ebb6388ceaMike Stump // it as a branch fixup to the current cleanup scope. 7311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 7321093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson // Create the pad block. 7331093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson llvm::BasicBlock *CleanupPad = createBasicBlock("cleanup.pad", CurFn); 734cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson 735cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson // Create a unique case ID. 73699533834ba8f3658559f334e68a518ebb6388ceaMike Stump llvm::ConstantInt *ID 73799533834ba8f3658559f334e68a518ebb6388ceaMike Stump = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 73899533834ba8f3658559f334e68a518ebb6388ceaMike Stump SI->getNumSuccessors()); 739cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson 740cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson // Store the jump destination before the branch instruction. 741cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson new llvm::StoreInst(ID, DestCodePtr, BI); 742cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson 7431093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson // Add it as the destination. 744cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson SI->addCase(ID, CleanupPad); 7451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 7461093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson // Create the branch to the final destination. 7471093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson llvm::BranchInst *BI = llvm::BranchInst::Create(Dest); 7481093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson CleanupPad->getInstList().push_back(BI); 7491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 7501093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson // And add it as a branch fixup. 7511093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson CleanupEntries.back().BranchFixups.push_back(BI); 7521093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson } 7531093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson } 7541093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson } 7551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 756bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson // Remove all blocks from the block scope map. 757bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson for (size_t i = 0, e = Blocks.size(); i != e; ++i) { 758bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson assert(BlockScopes.count(Blocks[i]) && 759bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson "Did not find block in scope map!"); 7601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 761bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson BlockScopes.erase(Blocks[i]); 762bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson } 7631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 76499533834ba8f3658559f334e68a518ebb6388ceaMike Stump return CleanupBlockInfo(CleanupEntryBlock, SwitchBlock, EndBlock, EHOnly); 765d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson} 766d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson 7671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid CodeGenFunction::EmitCleanupBlock() { 768bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson CleanupBlockInfo Info = PopCleanupBlock(); 7691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 77099533834ba8f3658559f334e68a518ebb6388ceaMike Stump if (Info.EHOnly) { 77199533834ba8f3658559f334e68a518ebb6388ceaMike Stump // FIXME: Add this to the exceptional edge 77299533834ba8f3658559f334e68a518ebb6388ceaMike Stump if (Info.CleanupBlock->getNumUses() == 0) 77399533834ba8f3658559f334e68a518ebb6388ceaMike Stump delete Info.CleanupBlock; 77499533834ba8f3658559f334e68a518ebb6388ceaMike Stump return; 77599533834ba8f3658559f334e68a518ebb6388ceaMike Stump } 77699533834ba8f3658559f334e68a518ebb6388ceaMike Stump 777eb6437a9b23b3d98a4590ba5557111e14eba2edfAnders Carlsson llvm::BasicBlock *CurBB = Builder.GetInsertBlock(); 7781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (CurBB && !CurBB->getTerminator() && 779eb6437a9b23b3d98a4590ba5557111e14eba2edfAnders Carlsson Info.CleanupBlock->getNumUses() == 0) { 780eb6437a9b23b3d98a4590ba5557111e14eba2edfAnders Carlsson CurBB->getInstList().splice(CurBB->end(), Info.CleanupBlock->getInstList()); 781eb6437a9b23b3d98a4590ba5557111e14eba2edfAnders Carlsson delete Info.CleanupBlock; 7821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump } else 783eb6437a9b23b3d98a4590ba5557111e14eba2edfAnders Carlsson EmitBlock(Info.CleanupBlock); 7841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 785bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson if (Info.SwitchBlock) 786bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson EmitBlock(Info.SwitchBlock); 787bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson if (Info.EndBlock) 788bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson EmitBlock(Info.EndBlock); 789d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson} 790d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson 7911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid CodeGenFunction::AddBranchFixup(llvm::BranchInst *BI) { 7921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump assert(!CleanupEntries.empty() && 79387eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson "Trying to add branch fixup without cleanup block!"); 7941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 795f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // FIXME: We could be more clever here and check if there's already a branch 796f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // fixup for this destination and recycle it. 79787eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson CleanupEntries.back().BranchFixups.push_back(BI); 79887eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson} 79987eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson 8001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid CodeGenFunction::EmitBranchThroughCleanup(llvm::BasicBlock *Dest) { 80146831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson if (!HaveInsertPoint()) 80246831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson return; 8031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 80487eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson llvm::BranchInst* BI = Builder.CreateBr(Dest); 8051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 80646831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson Builder.ClearInsertionPoint(); 8071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 80887eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson // The stack is empty, no need to do any cleanup. 80987eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson if (CleanupEntries.empty()) 81087eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson return; 8111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 81287eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson if (!Dest->getParent()) { 81387eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson // We are trying to branch to a block that hasn't been inserted yet. 81487eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson AddBranchFixup(BI); 81587eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson return; 81687eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson } 8171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 81887eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson BlockScopeMap::iterator I = BlockScopes.find(Dest); 81987eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson if (I == BlockScopes.end()) { 82087eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson // We are trying to jump to a block that is outside of any cleanup scope. 82187eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson AddBranchFixup(BI); 82287eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson return; 82387eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson } 8241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 82587eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson assert(I->second < CleanupEntries.size() && 82687eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson "Trying to branch into cleanup region"); 8271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 82887eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson if (I->second == CleanupEntries.size() - 1) { 82987eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson // We have a branch to a block in the same scope. 83087eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson return; 83187eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson } 8321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 83387eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson AddBranchFixup(BI); 83487eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson} 835