CodeGenFunction.cpp revision b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1
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" 224e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump#include "llvm/Target/TargetData.h" 235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang; 245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace CodeGen; 255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 261eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpCodeGenFunction::CodeGenFunction(CodeGenModule &cgm) 27a4f668f3b7e03629066a01b04e415cb2b4655dafMike Stump : BlockFunction(cgm, *this, Builder), CGM(cgm), 28a4f668f3b7e03629066a01b04e415cb2b4655dafMike Stump Target(CGM.getContext().Target), 29aac8705c046f01a264a4f82832895a5d9e695633Owen Anderson Builder(cgm.getModule().getContext()), 30d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner DebugInfo(0), IndirectBranch(0), 313d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner SwitchInsn(0), CaseRangeBlock(0), InvokeDest(0), 32f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson CXXThisDecl(0), CXXVTTDecl(0), 33a36bf8f74180e834c6bf2db867796fd5338495abAnders Carlsson ConditionalBranchLevel(0) { 344e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump LLVMIntTy = ConvertType(getContext().IntTy); 354e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump LLVMPointerWidth = Target.getPointerWidth(0); 364111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner} 375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerASTContext &CodeGenFunction::getContext() const { 395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return CGM.getContext(); 405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerllvm::BasicBlock *CodeGenFunction::getBasicBlockForLabel(const LabelStmt *S) { 445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer llvm::BasicBlock *&BB = LabelMap[S]; 455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (BB) return BB; 461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Create, but don't insert, the new block. 4855e874299f2ad827646a4ca9ea38c402aaeb38c9Daniel Dunbar return BB = createBasicBlock(S->getName()); 495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 510096acf421c4609ce7f43e8b05f8c5ca866d4611Daniel Dunbarllvm::Value *CodeGenFunction::GetAddrOfLocalVar(const VarDecl *VD) { 520096acf421c4609ce7f43e8b05f8c5ca866d4611Daniel Dunbar llvm::Value *Res = LocalDeclMap[VD]; 530096acf421c4609ce7f43e8b05f8c5ca866d4611Daniel Dunbar assert(Res && "Invalid argument to GetAddrOfLocalVar(), no decl!"); 540096acf421c4609ce7f43e8b05f8c5ca866d4611Daniel Dunbar return Res; 55813733577d33ec56479667b49e1bff42dc6bba90Lauro Ramos Venancio} 565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 570096acf421c4609ce7f43e8b05f8c5ca866d4611Daniel Dunbarllvm::Constant * 580096acf421c4609ce7f43e8b05f8c5ca866d4611Daniel DunbarCodeGenFunction::GetAddrOfStaticLocalVar(const VarDecl *BVD) { 590096acf421c4609ce7f43e8b05f8c5ca866d4611Daniel Dunbar return cast<llvm::Constant>(GetAddrOfLocalVar(BVD)); 60dde0a94120915fa925d1ffcdb997c7b44dc9fa21Anders Carlsson} 61dde0a94120915fa925d1ffcdb997c7b44dc9fa21Anders Carlsson 628b1a343b6b360d63d5dc8a6beb841ce4414c1e00Daniel Dunbarconst llvm::Type *CodeGenFunction::ConvertTypeForMem(QualType T) { 638b1a343b6b360d63d5dc8a6beb841ce4414c1e00Daniel Dunbar return CGM.getTypes().ConvertTypeForMem(T); 648b1a343b6b360d63d5dc8a6beb841ce4414c1e00Daniel Dunbar} 658b1a343b6b360d63d5dc8a6beb841ce4414c1e00Daniel Dunbar 665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerconst llvm::Type *CodeGenFunction::ConvertType(QualType T) { 675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return CGM.getTypes().ConvertType(T); 685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 704111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattnerbool CodeGenFunction::hasAggregateLLVMType(QualType T) { 71e9d34dc7afe06c9adaacad7a678a0cbbf749ea75Anders Carlsson return T->isRecordType() || T->isArrayType() || T->isAnyComplexType() || 72e9d34dc7afe06c9adaacad7a678a0cbbf749ea75Anders Carlsson T->isMemberFunctionPointerType(); 734111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner} 74391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner 751c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbarvoid CodeGenFunction::EmitReturnBlock() { 761c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar // For cleanliness, we try to avoid emitting the return block for 771c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar // simple cases. 781c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar llvm::BasicBlock *CurBB = Builder.GetInsertBlock(); 791c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar 801c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar if (CurBB) { 811c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar assert(!CurBB->getTerminator() && "Unexpected terminated block."); 821c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar 8396e18b05ea6b55aa92a1a576f29e9cee73a7e20bDaniel Dunbar // We have a valid insert point, reuse it if it is empty or there are no 8496e18b05ea6b55aa92a1a576f29e9cee73a7e20bDaniel Dunbar // explicit jumps to the return block. 8596e18b05ea6b55aa92a1a576f29e9cee73a7e20bDaniel Dunbar if (CurBB->empty() || ReturnBlock->use_empty()) { 8696e18b05ea6b55aa92a1a576f29e9cee73a7e20bDaniel Dunbar ReturnBlock->replaceAllUsesWith(CurBB); 871c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar delete ReturnBlock; 8896e18b05ea6b55aa92a1a576f29e9cee73a7e20bDaniel Dunbar } else 891c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar EmitBlock(ReturnBlock); 901c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar return; 911c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar } 921c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar 931c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar // Otherwise, if the return block is the target of a single direct 941c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar // branch then we can just put the code in that block instead. This 951c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar // cleans up functions which started with a unified return block. 961c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar if (ReturnBlock->hasOneUse()) { 971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::BranchInst *BI = 981c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar dyn_cast<llvm::BranchInst>(*ReturnBlock->use_begin()); 991c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar if (BI && BI->isUnconditional() && BI->getSuccessor(0) == ReturnBlock) { 1001c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar // Reset insertion point and delete the branch. 1011c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar Builder.SetInsertPoint(BI->getParent()); 1021c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar BI->eraseFromParent(); 1031c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar delete ReturnBlock; 1041c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar return; 1051c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar } 1061c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar } 1071c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar 108f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // FIXME: We are at an unreachable point, there is no reason to emit the block 109f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // unless it has uses. However, we still need a place to put the debug 110f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // region.end for now. 1111c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar 1121c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar EmitBlock(ReturnBlock); 1131c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar} 1141c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar 115af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbarvoid CodeGenFunction::FinishFunction(SourceLocation EndLoc) { 116391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner assert(BreakContinueStack.empty() && 117391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner "mismatched push/pop in break/continue stack!"); 118bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson assert(BlockScopes.empty() && 119bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson "did not remove all blocks from block scope map!"); 120bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson assert(CleanupEntries.empty() && 121bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson "mismatched push/pop in cleanup stack!"); 1221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // Emit function epilog (to return). 1241c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar EmitReturnBlock(); 125f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar 126f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar // Emit debug descriptor for function end. 127e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson if (CGDebugInfo *DI = getDebugInfo()) { 128f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar DI->setLocation(EndLoc); 129f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar DI->EmitRegionEnd(CurFn, Builder); 130f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar } 131f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar 13288b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar EmitFunctionEpilog(*CurFnInfo, ReturnValue); 1335ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar 134d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner // If someone did an indirect goto, emit the indirect goto block at the end of 135d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner // the function. 136d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner if (IndirectBranch) { 137d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner EmitBlock(IndirectBranch->getParent()); 138d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner Builder.ClearInsertionPoint(); 139d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner } 140d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner 141391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner // Remove the AllocaInsertPt instruction, which is just a convenience for us. 142481769b5dc102b0256b35581e787909ad5edfab5Chris Lattner llvm::Instruction *Ptr = AllocaInsertPt; 143391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner AllocaInsertPt = 0; 144481769b5dc102b0256b35581e787909ad5edfab5Chris Lattner Ptr->eraseFromParent(); 145d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner 146d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner // If someone took the address of a label but never did an indirect goto, we 147d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner // made a zero entry PHI node, which is illegal, zap it now. 148d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner if (IndirectBranch) { 149d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner llvm::PHINode *PN = cast<llvm::PHINode>(IndirectBranch->getAddress()); 150d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner if (PN->getNumIncomingValues() == 0) { 151d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner PN->replaceAllUsesWith(llvm::UndefValue::get(PN->getType())); 152d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner PN->eraseFromParent(); 153d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner } 154d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner } 155c8aa5f1f264fb230c38182adab944232bb160c2bChris Lattner} 156c8aa5f1f264fb230c38182adab944232bb160c2bChris Lattner 1570ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlssonvoid CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy, 1587c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar llvm::Function *Fn, 1592284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar const FunctionArgList &Args, 1602284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar SourceLocation StartLoc) { 1610ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlsson const Decl *D = GD.getDecl(); 1620ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlsson 1634cc1a4703363ff940b6273aeef9d96a87edeb04bAnders Carlsson DidCallStackSave = false; 164b5437d238752dc297e42410e98d38d5250fe0463Chris Lattner CurCodeDecl = CurFuncDecl = D; 1657c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar FnRetTy = RetTy; 166bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar CurFn = Fn; 1675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer assert(CurFn->isDeclaration() && "Function already has body?"); 168ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner 16955e874299f2ad827646a4ca9ea38c402aaeb38c9Daniel Dunbar llvm::BasicBlock *EntryBB = createBasicBlock("entry", CurFn); 1705ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar 1715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Create a marker to make it easy to insert allocas into the entryblock 17255352a2d616cf9fbb621d10faf8b960b4b268bd8Chris Lattner // later. Don't create this with the builder, because we don't want it 17355352a2d616cf9fbb621d10faf8b960b4b268bd8Chris Lattner // folded. 1740032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::getInt32Ty(VMContext)); 175bcdc0f0aff015b6507560f50c71d1b7b6c6a9932Mike Stump AllocaInsertPt = new llvm::BitCastInst(Undef, 176bcdc0f0aff015b6507560f50c71d1b7b6c6a9932Mike Stump llvm::Type::getInt32Ty(VMContext), "", 17755352a2d616cf9fbb621d10faf8b960b4b268bd8Chris Lattner EntryBB); 178f1466848dce9c4c75d96a6cabdc8db560e26aac8Chris Lattner if (Builder.isNamePreserving()) 179f1466848dce9c4c75d96a6cabdc8db560e26aac8Chris Lattner AllocaInsertPt->setName("allocapt"); 1801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 18155e874299f2ad827646a4ca9ea38c402aaeb38c9Daniel Dunbar ReturnBlock = createBasicBlock("return"); 1821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 18355352a2d616cf9fbb621d10faf8b960b4b268bd8Chris Lattner Builder.SetInsertPoint(EntryBB); 1841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 18591cc815ffd13d4a78ae1b5bd617e19dd555de4f4Mike Stump QualType FnType = getContext().getFunctionType(RetTy, 0, 0, false, 0); 18691cc815ffd13d4a78ae1b5bd617e19dd555de4f4Mike Stump 187af99417156c652a6f04dff643925036dc3241d60Sanjiv Gupta // Emit subprogram debug descriptor. 1887c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar // FIXME: The cast here is a huge hack. 189e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson if (CGDebugInfo *DI = getDebugInfo()) { 1902284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar DI->setLocation(StartLoc); 1911860a31edf826bc4001e153f1a8c10e17d97af87Anders Carlsson if (isa<FunctionDecl>(D)) { 19291cc815ffd13d4a78ae1b5bd617e19dd555de4f4Mike Stump DI->EmitFunctionStart(CGM.getMangledName(GD), FnType, CurFn, Builder); 1932284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar } else { 1942284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar // Just use LLVM function name. 1951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 19642719fc8145cb0da244b1826551e6b441db89209Daniel Dunbar // FIXME: Remove unnecessary conversion to std::string when API settles. 1971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump DI->EmitFunctionStart(std::string(Fn->getName()).c_str(), 19891cc815ffd13d4a78ae1b5bd617e19dd555de4f4Mike Stump FnType, CurFn, Builder); 199af99417156c652a6f04dff643925036dc3241d60Sanjiv Gupta } 200af99417156c652a6f04dff643925036dc3241d60Sanjiv Gupta } 201af99417156c652a6f04dff643925036dc3241d60Sanjiv Gupta 20288b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar // FIXME: Leaked. 203541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar CurFnInfo = &CGM.getTypes().getFunctionInfo(FnRetTy, Args); 204b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman 205b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman if (RetTy->isVoidType()) { 206b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman // Void type; nothing to return. 207b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman ReturnValue = 0; 208b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman } else if (CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect && 209b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman hasAggregateLLVMType(CurFnInfo->getReturnType())) { 210b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman // Indirect aggregate return; emit returned value directly into sret slot. 211b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman // This reduces code size, and is also affects correctness in C++. 212b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman ReturnValue = CurFn->arg_begin(); 213b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman } else { 214b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman ReturnValue = CreateTempAlloca(ConvertType(RetTy), "retval"); 215b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman } 216b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman 21788b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar EmitFunctionProlog(*CurFnInfo, CurFn, Args); 2181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 219751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson // If any of the arguments have a variably modified type, make sure to 220751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson // emit the type size. 221751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end(); 222751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson i != e; ++i) { 223751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson QualType Ty = i->second; 224751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson 225751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson if (Ty->isVariablyModifiedType()) 226751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson EmitVLASize(Ty); 227751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson } 2287c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar} 229eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman 230f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlssonstatic bool NeedsVTTParameter(GlobalDecl GD) { 231f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); 232f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson 233f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson // We don't have any virtual bases, just return early. 234f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson if (!MD->getParent()->getNumVBases()) 235f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson return false; 236f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson 237f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson // Check if we have a base constructor. 238f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson if (isa<CXXConstructorDecl>(MD) && GD.getCtorType() == Ctor_Base) 239f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson return true; 240f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson 241f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson // Check if we have a base destructor. 242f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base) 243f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson return true; 244f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson 245f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson return false; 246f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson} 247f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson 2480ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlssonvoid CodeGenFunction::GenerateCode(GlobalDecl GD, 2497c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar llvm::Function *Fn) { 2500ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlsson const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl()); 2510ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlsson 252e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson // Check if we should generate debug info for this function. 2531feade8e520be483293dbf55eb57a51720899589Mike Stump if (CGM.getDebugInfo() && !FD->hasAttr<NoDebugAttr>()) 254e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson DebugInfo = CGM.getDebugInfo(); 2551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2567c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar FunctionArgList Args; 2571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2582b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { 2592b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson if (MD->isInstance()) { 2602b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson // Create the implicit 'this' decl. 2612b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson // FIXME: I'm not entirely sure I like using a fake decl just for code 2622b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson // generation. Maybe we can come up with a better way? 2632b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson CXXThisDecl = ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 2641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump &getContext().Idents.get("this"), 2652b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson MD->getThisType(getContext())); 2662b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson Args.push_back(std::make_pair(CXXThisDecl, CXXThisDecl->getType())); 267f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson 268f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson // Check if we need a VTT parameter as well. 269f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson if (NeedsVTTParameter(GD)) { 270f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson // FIXME: The comment about using a fake decl above applies here too. 271f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson QualType T = getContext().getPointerType(getContext().VoidPtrTy); 272f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson CXXVTTDecl = 273f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 274f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson &getContext().Idents.get("vtt"), T); 275f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson Args.push_back(std::make_pair(CXXVTTDecl, CXXVTTDecl->getType())); 276f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson } 2772b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson } 2782b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson } 2791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 280eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman if (FD->getNumParams()) { 281183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall const FunctionProtoType* FProto = FD->getType()->getAs<FunctionProtoType>(); 282eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman assert(FProto && "Function def must have prototype!"); 2837c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar 2847c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) 2851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Args.push_back(std::make_pair(FD->getParamDecl(i), 2867c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar FProto->getArgType(i))); 2875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 288af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 289d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl // FIXME: Support CXXTryStmt here, too. 2906fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis if (const CompoundStmt *S = FD->getCompoundBody()) { 2910ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlsson StartFunction(GD, FD->getResultType(), Fn, Args, S->getLBracLoc()); 2924365bba95be15342575b4f030c6583a770a5da3dAnders Carlsson 2934365bba95be15342575b4f030c6583a770a5da3dAnders Carlsson if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) { 294de1d26b9c1d8823b173e4d77015ad88b4da70559Anders Carlsson EmitCtorPrologue(CD, GD.getCtorType()); 2954365bba95be15342575b4f030c6583a770a5da3dAnders Carlsson EmitStmt(S); 2965e1b91875c275f0ec50d3680afbac150d684fdbaAnders Carlsson 2975e1b91875c275f0ec50d3680afbac150d684fdbaAnders Carlsson // If any of the member initializers are temporaries bound to references 2985e1b91875c275f0ec50d3680afbac150d684fdbaAnders Carlsson // make sure to emit their destructors. 2995e1b91875c275f0ec50d3680afbac150d684fdbaAnders Carlsson EmitCleanupBlocks(0); 3005e1b91875c275f0ec50d3680afbac150d684fdbaAnders Carlsson 3014365bba95be15342575b4f030c6583a770a5da3dAnders Carlsson } else if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD)) { 3024365bba95be15342575b4f030c6583a770a5da3dAnders Carlsson llvm::BasicBlock *DtorEpilogue = createBasicBlock("dtor.epilogue"); 3034365bba95be15342575b4f030c6583a770a5da3dAnders Carlsson PushCleanupBlock(DtorEpilogue); 3044365bba95be15342575b4f030c6583a770a5da3dAnders Carlsson 3054365bba95be15342575b4f030c6583a770a5da3dAnders Carlsson EmitStmt(S); 306c33e4ba20304e222692e77f2c6ad26a5d8d32f83Anders Carlsson 307c33e4ba20304e222692e77f2c6ad26a5d8d32f83Anders Carlsson CleanupBlockInfo Info = PopCleanupBlock(); 308c33e4ba20304e222692e77f2c6ad26a5d8d32f83Anders Carlsson 309c33e4ba20304e222692e77f2c6ad26a5d8d32f83Anders Carlsson assert(Info.CleanupBlock == DtorEpilogue && "Block mismatch!"); 310c33e4ba20304e222692e77f2c6ad26a5d8d32f83Anders Carlsson EmitBlock(DtorEpilogue); 311de1d26b9c1d8823b173e4d77015ad88b4da70559Anders Carlsson EmitDtorEpilogue(DD, GD.getDtorType()); 312c33e4ba20304e222692e77f2c6ad26a5d8d32f83Anders Carlsson 313c33e4ba20304e222692e77f2c6ad26a5d8d32f83Anders Carlsson if (Info.SwitchBlock) 314c33e4ba20304e222692e77f2c6ad26a5d8d32f83Anders Carlsson EmitBlock(Info.SwitchBlock); 315c33e4ba20304e222692e77f2c6ad26a5d8d32f83Anders Carlsson if (Info.EndBlock) 316c33e4ba20304e222692e77f2c6ad26a5d8d32f83Anders Carlsson EmitBlock(Info.EndBlock); 3174365bba95be15342575b4f030c6583a770a5da3dAnders Carlsson } else { 3184365bba95be15342575b4f030c6583a770a5da3dAnders Carlsson // Just a regular function, emit its body. 3194365bba95be15342575b4f030c6583a770a5da3dAnders Carlsson EmitStmt(S); 320c33e4ba20304e222692e77f2c6ad26a5d8d32f83Anders Carlsson } 3214365bba95be15342575b4f030c6583a770a5da3dAnders Carlsson 322d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl FinishFunction(S->getRBracLoc()); 3234513272f444c59705123ccb6207414ce62c9a568Douglas Gregor } else if (FD->isImplicit()) { 3244513272f444c59705123ccb6207414ce62c9a568Douglas Gregor const CXXRecordDecl *ClassDecl = 3254513272f444c59705123ccb6207414ce62c9a568Douglas Gregor cast<CXXRecordDecl>(FD->getDeclContext()); 3264513272f444c59705123ccb6207414ce62c9a568Douglas Gregor (void) ClassDecl; 327c7ff8e19081c2e974f05f66c4fa9b40750fc655fFariborz Jahanian if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) { 3284513272f444c59705123ccb6207414ce62c9a568Douglas Gregor // FIXME: For C++0x, we want to look for implicit *definitions* of 3294513272f444c59705123ccb6207414ce62c9a568Douglas Gregor // these special member functions, rather than implicit *declarations*. 3309889652dbc10060cd604861ed2e5bc6719f845b0Fariborz Jahanian if (CD->isCopyConstructor(getContext())) { 3319889652dbc10060cd604861ed2e5bc6719f845b0Fariborz Jahanian assert(!ClassDecl->hasUserDeclaredCopyConstructor() && 3324513272f444c59705123ccb6207414ce62c9a568Douglas Gregor "Cannot synthesize a non-implicit copy constructor"); 333de1d26b9c1d8823b173e4d77015ad88b4da70559Anders Carlsson SynthesizeCXXCopyConstructor(CD, GD.getCtorType(), Fn, Args); 3344513272f444c59705123ccb6207414ce62c9a568Douglas Gregor } else if (CD->isDefaultConstructor()) { 3359889652dbc10060cd604861ed2e5bc6719f845b0Fariborz Jahanian assert(!ClassDecl->hasUserDeclaredConstructor() && 3364513272f444c59705123ccb6207414ce62c9a568Douglas Gregor "Cannot synthesize a non-implicit default constructor."); 337de1d26b9c1d8823b173e4d77015ad88b4da70559Anders Carlsson SynthesizeDefaultConstructor(CD, GD.getCtorType(), Fn, Args); 3384513272f444c59705123ccb6207414ce62c9a568Douglas Gregor } else { 3394513272f444c59705123ccb6207414ce62c9a568Douglas Gregor assert(false && "Implicit constructor cannot be synthesized"); 3409889652dbc10060cd604861ed2e5bc6719f845b0Fariborz Jahanian } 3414513272f444c59705123ccb6207414ce62c9a568Douglas Gregor } else if (const CXXDestructorDecl *CD = dyn_cast<CXXDestructorDecl>(FD)) { 3424513272f444c59705123ccb6207414ce62c9a568Douglas Gregor assert(!ClassDecl->hasUserDeclaredDestructor() && 3434513272f444c59705123ccb6207414ce62c9a568Douglas Gregor "Cannot synthesize a non-implicit destructor"); 3444513272f444c59705123ccb6207414ce62c9a568Douglas Gregor SynthesizeDefaultDestructor(CD, GD.getDtorType(), Fn, Args); 3454513272f444c59705123ccb6207414ce62c9a568Douglas Gregor } else if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { 3464513272f444c59705123ccb6207414ce62c9a568Douglas Gregor assert(MD->isCopyAssignment() && 3474513272f444c59705123ccb6207414ce62c9a568Douglas Gregor !ClassDecl->hasUserDeclaredCopyAssignment() && 3484513272f444c59705123ccb6207414ce62c9a568Douglas Gregor "Cannot synthesize a method that is not an implicit-defined " 3494513272f444c59705123ccb6207414ce62c9a568Douglas Gregor "copy constructor"); 350de1d26b9c1d8823b173e4d77015ad88b4da70559Anders Carlsson SynthesizeCXXCopyAssignment(MD, Fn, Args); 3514513272f444c59705123ccb6207414ce62c9a568Douglas Gregor } else { 3524513272f444c59705123ccb6207414ce62c9a568Douglas Gregor assert(false && "Cannot synthesize unknown implicit function"); 3534513272f444c59705123ccb6207414ce62c9a568Douglas Gregor } 3540ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlsson } 3551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3562b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson // Destroy the 'this' declaration. 3572b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson if (CXXThisDecl) 3582b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson CXXThisDecl->Destroy(getContext()); 359f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson 360f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson // Destroy the VTT declaration. 361f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson if (CXXVTTDecl) 362f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson CXXVTTDecl->Destroy(getContext()); 3635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 3645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 3650946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner/// ContainsLabel - Return true if the statement contains a label in it. If 3660946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner/// this statement is not executed normally, it not containing a label means 3670946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner/// that we can just remove the code. 3680946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattnerbool CodeGenFunction::ContainsLabel(const Stmt *S, bool IgnoreCaseStmts) { 3690946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner // Null statement, not a label! 3700946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner if (S == 0) return false; 3711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3720946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner // If this is a label, we have to emit the code, consider something like: 3730946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner // if (0) { ... foo: bar(); } goto foo; 3740946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner if (isa<LabelStmt>(S)) 3750946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner return true; 3761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3770946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner // If this is a case/default statement, and we haven't seen a switch, we have 3780946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner // to emit the code. 3790946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner if (isa<SwitchCase>(S) && !IgnoreCaseStmts) 3800946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner return true; 3811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3820946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner // If this is a switch statement, we want to ignore cases below it. 3830946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner if (isa<SwitchStmt>(S)) 3840946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner IgnoreCaseStmts = true; 3851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3860946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner // Scan subexpressions for verboten labels. 3870946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end(); 3880946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner I != E; ++I) 3890946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner if (ContainsLabel(*I, IgnoreCaseStmts)) 3900946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner return true; 3911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3920946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner return false; 3930946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner} 3940946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner 39531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner 39631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// ConstantFoldsToSimpleInteger - If the sepcified expression does not fold to 39731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// a constant, or if it does but contains a label, return 0. If it constant 39831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// folds to 'true' and does not contain a label, return 1, if it constant folds 39931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// to 'false' and does not contain a label, return -1. 40031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattnerint CodeGenFunction::ConstantFoldsToSimpleInteger(const Expr *Cond) { 40136bc14c3a1cf63ee306df5687ac8e85f924f8639Daniel Dunbar // FIXME: Rename and handle conversion of other evaluatable things 40236bc14c3a1cf63ee306df5687ac8e85f924f8639Daniel Dunbar // to bool. 40364712f196bffd41fb0552c2643b07a25c3e45082Anders Carlsson Expr::EvalResult Result; 4041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (!Cond->Evaluate(Result, getContext()) || !Result.Val.isInt() || 40564712f196bffd41fb0552c2643b07a25c3e45082Anders Carlsson Result.HasSideEffects) 406ef5a66d8171eb95e948107f8ee7707b360aaff25Anders Carlsson return 0; // Not foldable, not integer or not fully evaluatable. 4071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 40831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner if (CodeGenFunction::ContainsLabel(Cond)) 40931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner return 0; // Contains a label. 4101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 41164712f196bffd41fb0552c2643b07a25c3e45082Anders Carlsson return Result.Val.getInt().getBoolValue() ? 1 : -1; 41231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner} 41331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner 41431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner 41531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an if 41631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// statement) to the specified blocks. Based on the condition, this might try 41731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// to simplify the codegen of the conditional based on the branch. 41831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// 41931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattnervoid CodeGenFunction::EmitBranchOnBoolExpr(const Expr *Cond, 42031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner llvm::BasicBlock *TrueBlock, 42131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner llvm::BasicBlock *FalseBlock) { 42231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner if (const ParenExpr *PE = dyn_cast<ParenExpr>(Cond)) 42331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner return EmitBranchOnBoolExpr(PE->getSubExpr(), TrueBlock, FalseBlock); 4241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 42531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner if (const BinaryOperator *CondBOp = dyn_cast<BinaryOperator>(Cond)) { 42631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // Handle X && Y in a condition. 42731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner if (CondBOp->getOpcode() == BinaryOperator::LAnd) { 42831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // If we have "1 && X", simplify the code. "0 && X" would have constant 42931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // folded if the case was simple enough. 43031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == 1) { 43131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // br(1 && X) -> br(X). 43231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); 43331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner } 4341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 43531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // If we have "X && 1", simplify the code to use an uncond branch. 43631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // "X && 0" would have been constant folded to 0. 43731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == 1) { 43831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // br(X && 1) -> br(X). 43931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock); 44031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner } 4411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 44231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // Emit the LHS as a conditional. If the LHS conditional is false, we 44331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // want to jump to the FalseBlock. 4449615ecb44f549ae9fa2b4db6ff46bc78befbf62cDaniel Dunbar llvm::BasicBlock *LHSTrue = createBasicBlock("land.lhs.true"); 44531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner EmitBranchOnBoolExpr(CondBOp->getLHS(), LHSTrue, FalseBlock); 44631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner EmitBlock(LHSTrue); 4471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 44831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); 44931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner return; 45031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner } else if (CondBOp->getOpcode() == BinaryOperator::LOr) { 45131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // If we have "0 || X", simplify the code. "1 || X" would have constant 45231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // folded if the case was simple enough. 45331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == -1) { 45431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // br(0 || X) -> br(X). 45531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); 45631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner } 4571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 45831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // If we have "X || 0", simplify the code to use an uncond branch. 45931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // "X || 1" would have been constant folded to 1. 46031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == -1) { 46131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // br(X || 0) -> br(X). 46231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock); 46331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner } 4641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 46531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // Emit the LHS as a conditional. If the LHS conditional is true, we 46631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // want to jump to the TrueBlock. 4679615ecb44f549ae9fa2b4db6ff46bc78befbf62cDaniel Dunbar llvm::BasicBlock *LHSFalse = createBasicBlock("lor.lhs.false"); 46831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, LHSFalse); 46931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner EmitBlock(LHSFalse); 4701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 47131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); 47231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner return; 47331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner } 474552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner } 4751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 476552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner if (const UnaryOperator *CondUOp = dyn_cast<UnaryOperator>(Cond)) { 477552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner // br(!x, t, f) -> br(x, f, t) 478552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner if (CondUOp->getOpcode() == UnaryOperator::LNot) 479552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner return EmitBranchOnBoolExpr(CondUOp->getSubExpr(), FalseBlock, TrueBlock); 48031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner } 4811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 48209b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar if (const ConditionalOperator *CondOp = dyn_cast<ConditionalOperator>(Cond)) { 48309b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar // Handle ?: operator. 48409b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar 48509b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar // Just ignore GNU ?: extension. 48609b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar if (CondOp->getLHS()) { 48709b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar // br(c ? x : y, t, f) -> br(c, br(x, t, f), br(y, t, f)) 48809b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true"); 48909b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false"); 49009b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar EmitBranchOnBoolExpr(CondOp->getCond(), LHSBlock, RHSBlock); 49109b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar EmitBlock(LHSBlock); 49209b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar EmitBranchOnBoolExpr(CondOp->getLHS(), TrueBlock, FalseBlock); 49309b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar EmitBlock(RHSBlock); 49409b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar EmitBranchOnBoolExpr(CondOp->getRHS(), TrueBlock, FalseBlock); 49509b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar return; 49609b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar } 49709b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar } 49809b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar 49931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // Emit the code with the fully general case. 50031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner llvm::Value *CondV = EvaluateExprAsBool(Cond); 50131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner Builder.CreateCondBr(CondV, TrueBlock, FalseBlock); 50231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner} 50331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner 504488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar/// ErrorUnsupported - Print out an error that codegen doesn't support the 505dc5e8268292046114ffe02e48773572a91a310f1Chris Lattner/// specified stmt yet. 50690df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbarvoid CodeGenFunction::ErrorUnsupported(const Stmt *S, const char *Type, 50790df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar bool OmitOnError) { 50890df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar CGM.ErrorUnsupported(S, Type, OmitOnError); 509dc5e8268292046114ffe02e48773572a91a310f1Chris Lattner} 510dc5e8268292046114ffe02e48773572a91a310f1Chris Lattner 51188207c9ca218486b93ae7df14e9764cd0c2c3383Chris Lattnervoid CodeGenFunction::EmitMemSetToZero(llvm::Value *DestPtr, QualType Ty) { 51236afd38fb749ab34c8341a1dc70366bca904eaa3Chris Lattner const llvm::Type *BP = llvm::Type::getInt8PtrTy(VMContext); 5133d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson if (DestPtr->getType() != BP) 5143d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp"); 5153d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson 5163d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson // Get size and alignment info for this aggregate. 5173d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty); 5183d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson 51988207c9ca218486b93ae7df14e9764cd0c2c3383Chris Lattner // Don't bother emitting a zero-byte memset. 52088207c9ca218486b93ae7df14e9764cd0c2c3383Chris Lattner if (TypeInfo.first == 0) 52188207c9ca218486b93ae7df14e9764cd0c2c3383Chris Lattner return; 5221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 5233d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson // FIXME: Handle variable sized types. 5241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const llvm::Type *IntPtr = llvm::IntegerType::get(VMContext, 5250032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson LLVMPointerWidth); 5263d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson 5273d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson Builder.CreateCall4(CGM.getMemSetFn(), DestPtr, 5280032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson llvm::Constant::getNullValue(llvm::Type::getInt8Ty(VMContext)), 5293d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson // TypeInfo.first describes size in bits. 5304a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson llvm::ConstantInt::get(IntPtr, TypeInfo.first/8), 5311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 5323d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson TypeInfo.second/8)); 5333d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson} 5343d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson 535d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattnerllvm::BlockAddress *CodeGenFunction::GetAddrOfLabel(const LabelStmt *L) { 536d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner // Make sure that there is a block for the indirect goto. 537d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner if (IndirectBranch == 0) 538d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner GetIndirectGotoBlock(); 5393d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner 540d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner llvm::BasicBlock *BB = getBasicBlockForLabel(L); 5413d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner 542d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner // Make sure the indirect branch includes all of the address-taken blocks. 543d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner IndirectBranch->addDestination(BB); 544d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner return llvm::BlockAddress::get(CurFn, BB); 5453d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner} 5461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 5473d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattnerllvm::BasicBlock *CodeGenFunction::GetIndirectGotoBlock() { 548d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner // If we already made the indirect branch for indirect goto, return its block. 549d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner if (IndirectBranch) return IndirectBranch->getParent(); 5503d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner 551d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner CGBuilderTy TmpBuilder(createBasicBlock("indirectgoto")); 5523d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner 553d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext); 55485e74acfcfb0c835a2e6c1adab314e997917039aChris Lattner 5553d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner // Create the PHI node that indirect gotos will add entries to. 556d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner llvm::Value *DestVal = TmpBuilder.CreatePHI(Int8PtrTy, "indirect.goto.dest"); 5573d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner 558d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner // Create the indirect branch instruction. 559d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner IndirectBranch = TmpBuilder.CreateIndirectBr(DestVal); 560d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner return IndirectBranch->getParent(); 5610ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar} 562ddf7cac45d85b73127adbbd91a2b28fc7291c57eAnders Carlsson 563d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbarllvm::Value *CodeGenFunction::GetVLASize(const VariableArrayType *VAT) { 564bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman llvm::Value *&SizeEntry = VLASizeMap[VAT->getSizeExpr()]; 5651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 566f666b7780d04186521adcaedb0e15dfa4d5e6933Anders Carlsson assert(SizeEntry && "Did not emit size for type"); 567f666b7780d04186521adcaedb0e15dfa4d5e6933Anders Carlsson return SizeEntry; 568f666b7780d04186521adcaedb0e15dfa4d5e6933Anders Carlsson} 569dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlsson 570d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbarllvm::Value *CodeGenFunction::EmitVLASize(QualType Ty) { 57160d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson assert(Ty->isVariablyModifiedType() && 57260d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson "Must pass variably modified type to EmitVLASizes!"); 5731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 574d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar EnsureInsertPoint(); 5751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 57660d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson if (const VariableArrayType *VAT = getContext().getAsVariableArrayType(Ty)) { 577bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman llvm::Value *&SizeEntry = VLASizeMap[VAT->getSizeExpr()]; 5781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 579fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson if (!SizeEntry) { 58096f214776c0f69069fee4d67557c8c7f416009a8Anders Carlsson const llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); 5811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 582ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner // Get the element size; 583ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner QualType ElemTy = VAT->getElementType(); 584ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner llvm::Value *ElemSize; 585fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson if (ElemTy->isVariableArrayType()) 586fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson ElemSize = EmitVLASize(ElemTy); 587ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner else 5884a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson ElemSize = llvm::ConstantInt::get(SizeTy, 589fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson getContext().getTypeSize(ElemTy) / 8); 5901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 591fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson llvm::Value *NumElements = EmitScalarExpr(VAT->getSizeExpr()); 59296f214776c0f69069fee4d67557c8c7f416009a8Anders Carlsson NumElements = Builder.CreateIntCast(NumElements, SizeTy, false, "tmp"); 5931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 594fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson SizeEntry = Builder.CreateMul(ElemSize, NumElements); 595fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson } 5961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 59760d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson return SizeEntry; 598dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlsson } 5991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 600ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner if (const ArrayType *AT = dyn_cast<ArrayType>(Ty)) { 601ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner EmitVLASize(AT->getElementType()); 602ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner return 0; 6031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump } 6041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 605ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner const PointerType *PT = Ty->getAs<PointerType>(); 606ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner assert(PT && "unknown VM type!"); 607ec18ddd33d5dc2cba5f64fa903bac7a83dc1e01eChris Lattner EmitVLASize(PT->getPointeeType()); 60860d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson return 0; 609dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlsson} 6104fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman 6114fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedmanllvm::Value* CodeGenFunction::EmitVAListRef(const Expr* E) { 6124fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman if (CGM.getContext().getBuiltinVaListType()->isArrayType()) { 6134fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman return EmitScalarExpr(E); 6144fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman } 6154fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman return EmitLValue(E).getAddress(); 6164fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman} 6176ccc47698d0311ddabf32fa0f6db8e4f09ac96f8Anders Carlsson 6187799621ad7d16f3c6a1aeda024312d63ecd77777Fariborz Jahanianvoid CodeGenFunction::PushCleanupBlock(llvm::BasicBlock *CleanupEntryBlock, 61999533834ba8f3658559f334e68a518ebb6388ceaMike Stump llvm::BasicBlock *CleanupExitBlock, 62099533834ba8f3658559f334e68a518ebb6388ceaMike Stump bool EHOnly) { 62199533834ba8f3658559f334e68a518ebb6388ceaMike Stump CleanupEntries.push_back(CleanupEntry(CleanupEntryBlock, CleanupExitBlock, 62299533834ba8f3658559f334e68a518ebb6388ceaMike Stump EHOnly)); 6236ccc47698d0311ddabf32fa0f6db8e4f09ac96f8Anders Carlsson} 624c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson 6251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid CodeGenFunction::EmitCleanupBlocks(size_t OldCleanupStackSize) { 6261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump assert(CleanupEntries.size() >= OldCleanupStackSize && 627c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson "Cleanup stack mismatch!"); 6281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 629c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson while (CleanupEntries.size() > OldCleanupStackSize) 630c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson EmitCleanupBlock(); 631c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson} 632c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson 6331eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpCodeGenFunction::CleanupBlockInfo CodeGenFunction::PopCleanupBlock() { 634bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson CleanupEntry &CE = CleanupEntries.back(); 6351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 6367799621ad7d16f3c6a1aeda024312d63ecd77777Fariborz Jahanian llvm::BasicBlock *CleanupEntryBlock = CE.CleanupEntryBlock; 6371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 638bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson std::vector<llvm::BasicBlock *> Blocks; 639bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson std::swap(Blocks, CE.Blocks); 6401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 641bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson std::vector<llvm::BranchInst *> BranchFixups; 642bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson std::swap(BranchFixups, CE.BranchFixups); 6431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 64499533834ba8f3658559f334e68a518ebb6388ceaMike Stump bool EHOnly = CE.EHOnly; 64599533834ba8f3658559f334e68a518ebb6388ceaMike Stump 646bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson CleanupEntries.pop_back(); 647bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson 648ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson // Check if any branch fixups pointed to the scope we just popped. If so, 649ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson // we can remove them. 650ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson for (size_t i = 0, e = BranchFixups.size(); i != e; ++i) { 651ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson llvm::BasicBlock *Dest = BranchFixups[i]->getSuccessor(0); 652ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson BlockScopeMap::iterator I = BlockScopes.find(Dest); 6531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 654ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson if (I == BlockScopes.end()) 655ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson continue; 6561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 657ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson assert(I->second <= CleanupEntries.size() && "Invalid branch fixup!"); 6581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 659ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson if (I->second == CleanupEntries.size()) { 660ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson // We don't need to do this branch fixup. 661ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson BranchFixups[i] = BranchFixups.back(); 662ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson BranchFixups.pop_back(); 663ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson i--; 664ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson e--; 665ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson continue; 6661093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson } 6671093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson } 6681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 6697799621ad7d16f3c6a1aeda024312d63ecd77777Fariborz Jahanian llvm::BasicBlock *SwitchBlock = CE.CleanupExitBlock; 670bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson llvm::BasicBlock *EndBlock = 0; 6711093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson if (!BranchFixups.empty()) { 6727799621ad7d16f3c6a1aeda024312d63ecd77777Fariborz Jahanian if (!SwitchBlock) 6737799621ad7d16f3c6a1aeda024312d63ecd77777Fariborz Jahanian SwitchBlock = createBasicBlock("cleanup.switch"); 674bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson EndBlock = createBasicBlock("cleanup.end"); 6751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 676bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson llvm::BasicBlock *CurBB = Builder.GetInsertBlock(); 6771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 678bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson Builder.SetInsertPoint(SwitchBlock); 679bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson 68099533834ba8f3658559f334e68a518ebb6388ceaMike Stump llvm::Value *DestCodePtr 68199533834ba8f3658559f334e68a518ebb6388ceaMike Stump = CreateTempAlloca(llvm::Type::getInt32Ty(VMContext), 68299533834ba8f3658559f334e68a518ebb6388ceaMike Stump "cleanup.dst"); 6831093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson llvm::Value *DestCode = Builder.CreateLoad(DestCodePtr, "tmp"); 6841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 6851093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson // Create a switch instruction to determine where to jump next. 6861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::SwitchInst *SI = Builder.CreateSwitch(DestCode, EndBlock, 6871093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson BranchFixups.size()); 688bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson 68946831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson // Restore the current basic block (if any) 6900ae7b2b2f964bd6145d65ef52dc2a28025b2bd06Anders Carlsson if (CurBB) { 69146831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson Builder.SetInsertPoint(CurBB); 6921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 6930ae7b2b2f964bd6145d65ef52dc2a28025b2bd06Anders Carlsson // If we had a current basic block, we also need to emit an instruction 6940ae7b2b2f964bd6145d65ef52dc2a28025b2bd06Anders Carlsson // to initialize the cleanup destination. 6950032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson Builder.CreateStore(llvm::Constant::getNullValue(llvm::Type::getInt32Ty(VMContext)), 6960ae7b2b2f964bd6145d65ef52dc2a28025b2bd06Anders Carlsson DestCodePtr); 6970ae7b2b2f964bd6145d65ef52dc2a28025b2bd06Anders Carlsson } else 69846831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson Builder.ClearInsertionPoint(); 699bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson 7001093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson for (size_t i = 0, e = BranchFixups.size(); i != e; ++i) { 7011093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson llvm::BranchInst *BI = BranchFixups[i]; 7021093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson llvm::BasicBlock *Dest = BI->getSuccessor(0); 7031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 7041093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson // Fixup the branch instruction to point to the cleanup block. 7057799621ad7d16f3c6a1aeda024312d63ecd77777Fariborz Jahanian BI->setSuccessor(0, CleanupEntryBlock); 7061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 7071093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson if (CleanupEntries.empty()) { 708cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson llvm::ConstantInt *ID; 7091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 710cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson // Check if we already have a destination for this block. 711cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson if (Dest == SI->getDefaultDest()) 7120032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson ID = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 0); 713cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson else { 714cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson ID = SI->findCaseDest(Dest); 715cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson if (!ID) { 716cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson // No code found, get a new unique one by using the number of 717cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson // switch successors. 7181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump ID = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 719cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson SI->getNumSuccessors()); 720cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson SI->addCase(ID, Dest); 721cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson } 722cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson } 7231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 724cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson // Store the jump destination before the branch instruction. 725cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson new llvm::StoreInst(ID, DestCodePtr, BI); 7261093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson } else { 7271093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson // We need to jump through another cleanup block. Create a pad block 72899533834ba8f3658559f334e68a518ebb6388ceaMike Stump // with a branch instruction that jumps to the final destination and add 72999533834ba8f3658559f334e68a518ebb6388ceaMike Stump // it as a branch fixup to the current cleanup scope. 7301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 7311093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson // Create the pad block. 7321093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson llvm::BasicBlock *CleanupPad = createBasicBlock("cleanup.pad", CurFn); 733cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson 734cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson // Create a unique case ID. 73599533834ba8f3658559f334e68a518ebb6388ceaMike Stump llvm::ConstantInt *ID 73699533834ba8f3658559f334e68a518ebb6388ceaMike Stump = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 73799533834ba8f3658559f334e68a518ebb6388ceaMike Stump SI->getNumSuccessors()); 738cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson 739cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson // Store the jump destination before the branch instruction. 740cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson new llvm::StoreInst(ID, DestCodePtr, BI); 741cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson 7421093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson // Add it as the destination. 743cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson SI->addCase(ID, CleanupPad); 7441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 7451093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson // Create the branch to the final destination. 7461093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson llvm::BranchInst *BI = llvm::BranchInst::Create(Dest); 7471093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson CleanupPad->getInstList().push_back(BI); 7481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 7491093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson // And add it as a branch fixup. 7501093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson CleanupEntries.back().BranchFixups.push_back(BI); 7511093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson } 7521093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson } 7531093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson } 7541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 755bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson // Remove all blocks from the block scope map. 756bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson for (size_t i = 0, e = Blocks.size(); i != e; ++i) { 757bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson assert(BlockScopes.count(Blocks[i]) && 758bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson "Did not find block in scope map!"); 7591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 760bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson BlockScopes.erase(Blocks[i]); 761bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson } 7621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 76399533834ba8f3658559f334e68a518ebb6388ceaMike Stump return CleanupBlockInfo(CleanupEntryBlock, SwitchBlock, EndBlock, EHOnly); 764d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson} 765d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson 7661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid CodeGenFunction::EmitCleanupBlock() { 767bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson CleanupBlockInfo Info = PopCleanupBlock(); 7681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 76999533834ba8f3658559f334e68a518ebb6388ceaMike Stump if (Info.EHOnly) { 77099533834ba8f3658559f334e68a518ebb6388ceaMike Stump // FIXME: Add this to the exceptional edge 77199533834ba8f3658559f334e68a518ebb6388ceaMike Stump if (Info.CleanupBlock->getNumUses() == 0) 77299533834ba8f3658559f334e68a518ebb6388ceaMike Stump delete Info.CleanupBlock; 77399533834ba8f3658559f334e68a518ebb6388ceaMike Stump return; 77499533834ba8f3658559f334e68a518ebb6388ceaMike Stump } 77599533834ba8f3658559f334e68a518ebb6388ceaMike Stump 776eb6437a9b23b3d98a4590ba5557111e14eba2edfAnders Carlsson llvm::BasicBlock *CurBB = Builder.GetInsertBlock(); 7771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (CurBB && !CurBB->getTerminator() && 778eb6437a9b23b3d98a4590ba5557111e14eba2edfAnders Carlsson Info.CleanupBlock->getNumUses() == 0) { 779eb6437a9b23b3d98a4590ba5557111e14eba2edfAnders Carlsson CurBB->getInstList().splice(CurBB->end(), Info.CleanupBlock->getInstList()); 780eb6437a9b23b3d98a4590ba5557111e14eba2edfAnders Carlsson delete Info.CleanupBlock; 7811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump } else 782eb6437a9b23b3d98a4590ba5557111e14eba2edfAnders Carlsson EmitBlock(Info.CleanupBlock); 7831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 784bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson if (Info.SwitchBlock) 785bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson EmitBlock(Info.SwitchBlock); 786bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson if (Info.EndBlock) 787bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson EmitBlock(Info.EndBlock); 788d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson} 789d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson 7901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid CodeGenFunction::AddBranchFixup(llvm::BranchInst *BI) { 7911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump assert(!CleanupEntries.empty() && 79287eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson "Trying to add branch fixup without cleanup block!"); 7931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 794f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // FIXME: We could be more clever here and check if there's already a branch 795f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // fixup for this destination and recycle it. 79687eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson CleanupEntries.back().BranchFixups.push_back(BI); 79787eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson} 79887eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson 7991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid CodeGenFunction::EmitBranchThroughCleanup(llvm::BasicBlock *Dest) { 80046831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson if (!HaveInsertPoint()) 80146831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson return; 8021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 80387eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson llvm::BranchInst* BI = Builder.CreateBr(Dest); 8041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 80546831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson Builder.ClearInsertionPoint(); 8061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 80787eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson // The stack is empty, no need to do any cleanup. 80887eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson if (CleanupEntries.empty()) 80987eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson return; 8101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 81187eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson if (!Dest->getParent()) { 81287eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson // We are trying to branch to a block that hasn't been inserted yet. 81387eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson AddBranchFixup(BI); 81487eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson return; 81587eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson } 8161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 81787eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson BlockScopeMap::iterator I = BlockScopes.find(Dest); 81887eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson if (I == BlockScopes.end()) { 81987eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson // We are trying to jump to a block that is outside of any cleanup scope. 82087eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson AddBranchFixup(BI); 82187eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson return; 82287eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson } 8231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 82487eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson assert(I->second < CleanupEntries.size() && 82587eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson "Trying to branch into cleanup region"); 8261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 82787eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson if (I->second == CleanupEntries.size() - 1) { 82887eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson // We have a branch to a block in the same scope. 82987eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson return; 83087eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson } 8311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 83287eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson AddBranchFixup(BI); 83387eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson} 834