CodeGenFunction.cpp revision f1466848dce9c4c75d96a6cabdc8db560e26aac8
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" 21d9363c3a80168283b3da518b4e17f545a6246857Devang Patel#include "llvm/Support/CFG.h" 224e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump#include "llvm/Target/TargetData.h" 235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang; 245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace CodeGen; 255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerCodeGenFunction::CodeGenFunction(CodeGenModule &cgm) 27a4f668f3b7e03629066a01b04e415cb2b4655dafMike Stump : BlockFunction(cgm, *this, Builder), CGM(cgm), 28a4f668f3b7e03629066a01b04e415cb2b4655dafMike Stump Target(CGM.getContext().Target), 293947de5edda7e0c32060c568cc5f7b2aae6f70b4Mike Stump DebugInfo(0), SwitchInsn(0), CaseRangeBlock(0), InvokeDest(0) { 304e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump LLVMIntTy = ConvertType(getContext().IntTy); 314e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump LLVMPointerWidth = Target.getPointerWidth(0); 324e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump 334e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump // FIXME: We need to rearrange the code for copy/dispose so we have this 344e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump // sooner, so we can calculate offsets correctly. 354e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump if (!BlockHasCopyDispose) 364e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump BlockOffset = CGM.getTargetData() 374e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump .getTypeStoreSizeInBits(CGM.getGenericBlockLiteralType()) / 8; 384e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump else 394e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump BlockOffset = CGM.getTargetData() 404e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump .getTypeStoreSizeInBits(CGM.getGenericExtendedBlockLiteralType()) / 8; 418a2b4b1c5b960710db95e9b296d9a600aee37c00Mike Stump BlockAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8; 424111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner} 435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerASTContext &CodeGenFunction::getContext() const { 455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return CGM.getContext(); 465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerllvm::BasicBlock *CodeGenFunction::getBasicBlockForLabel(const LabelStmt *S) { 505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer llvm::BasicBlock *&BB = LabelMap[S]; 515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (BB) return BB; 525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Create, but don't insert, the new block. 5455e874299f2ad827646a4ca9ea38c402aaeb38c9Daniel Dunbar return BB = createBasicBlock(S->getName()); 555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 570096acf421c4609ce7f43e8b05f8c5ca866d4611Daniel Dunbarllvm::Value *CodeGenFunction::GetAddrOfLocalVar(const VarDecl *VD) { 580096acf421c4609ce7f43e8b05f8c5ca866d4611Daniel Dunbar llvm::Value *Res = LocalDeclMap[VD]; 590096acf421c4609ce7f43e8b05f8c5ca866d4611Daniel Dunbar assert(Res && "Invalid argument to GetAddrOfLocalVar(), no decl!"); 600096acf421c4609ce7f43e8b05f8c5ca866d4611Daniel Dunbar return Res; 61813733577d33ec56479667b49e1bff42dc6bba90Lauro Ramos Venancio} 625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 630096acf421c4609ce7f43e8b05f8c5ca866d4611Daniel Dunbarllvm::Constant * 640096acf421c4609ce7f43e8b05f8c5ca866d4611Daniel DunbarCodeGenFunction::GetAddrOfStaticLocalVar(const VarDecl *BVD) { 650096acf421c4609ce7f43e8b05f8c5ca866d4611Daniel Dunbar return cast<llvm::Constant>(GetAddrOfLocalVar(BVD)); 66dde0a94120915fa925d1ffcdb997c7b44dc9fa21Anders Carlsson} 67dde0a94120915fa925d1ffcdb997c7b44dc9fa21Anders Carlsson 688b1a343b6b360d63d5dc8a6beb841ce4414c1e00Daniel Dunbarconst llvm::Type *CodeGenFunction::ConvertTypeForMem(QualType T) { 698b1a343b6b360d63d5dc8a6beb841ce4414c1e00Daniel Dunbar return CGM.getTypes().ConvertTypeForMem(T); 708b1a343b6b360d63d5dc8a6beb841ce4414c1e00Daniel Dunbar} 718b1a343b6b360d63d5dc8a6beb841ce4414c1e00Daniel Dunbar 725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerconst llvm::Type *CodeGenFunction::ConvertType(QualType T) { 735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return CGM.getTypes().ConvertType(T); 745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 764111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattnerbool CodeGenFunction::hasAggregateLLVMType(QualType T) { 77a782ca76cc7dfdd69e331a4fa722fc23b18d5c34Daniel Dunbar // FIXME: Use positive checks instead of negative ones to be more 78a782ca76cc7dfdd69e331a4fa722fc23b18d5c34Daniel Dunbar // robust in the face of extension. 798958891f5fa1e593c4519a36b3df427ee019d70bDaniel Dunbar return !T->hasPointerRepresentation() &&!T->isRealType() && 808958891f5fa1e593c4519a36b3df427ee019d70bDaniel Dunbar !T->isVoidType() && !T->isVectorType() && !T->isFunctionType() && 81a782ca76cc7dfdd69e331a4fa722fc23b18d5c34Daniel Dunbar !T->isBlockPointerType(); 824111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner} 83391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner 841c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbarvoid CodeGenFunction::EmitReturnBlock() { 851c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar // For cleanliness, we try to avoid emitting the return block for 861c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar // simple cases. 871c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar llvm::BasicBlock *CurBB = Builder.GetInsertBlock(); 881c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar 891c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar if (CurBB) { 901c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar assert(!CurBB->getTerminator() && "Unexpected terminated block."); 911c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar 921c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar // We have a valid insert point, reuse it if there are no explicit 931c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar // jumps to the return block. 941c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar if (ReturnBlock->use_empty()) 951c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar delete ReturnBlock; 961c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar else 971c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar EmitBlock(ReturnBlock); 981c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar return; 991c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar } 1001c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar 1011c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar // Otherwise, if the return block is the target of a single direct 1021c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar // branch then we can just put the code in that block instead. This 1031c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar // cleans up functions which started with a unified return block. 1041c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar if (ReturnBlock->hasOneUse()) { 1051c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar llvm::BranchInst *BI = 1061c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar dyn_cast<llvm::BranchInst>(*ReturnBlock->use_begin()); 1071c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar if (BI && BI->isUnconditional() && BI->getSuccessor(0) == ReturnBlock) { 1081c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar // Reset insertion point and delete the branch. 1091c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar Builder.SetInsertPoint(BI->getParent()); 1101c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar BI->eraseFromParent(); 1111c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar delete ReturnBlock; 1121c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar return; 1131c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar } 1141c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar } 1151c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar 1161c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar // FIXME: We are at an unreachable point, there is no reason to emit 1171c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar // the block unless it has uses. However, we still need a place to 1181c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar // put the debug region.end for now. 1191c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar 1201c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar EmitBlock(ReturnBlock); 1211c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar} 1221c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar 123af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbarvoid CodeGenFunction::FinishFunction(SourceLocation EndLoc) { 1240ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar // Finish emission of indirect switches. 1250ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar EmitIndirectSwitches(); 1260ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar 127391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner assert(BreakContinueStack.empty() && 128391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner "mismatched push/pop in break/continue stack!"); 129bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson assert(BlockScopes.empty() && 130bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson "did not remove all blocks from block scope map!"); 131bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson assert(CleanupEntries.empty() && 132bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson "mismatched push/pop in cleanup stack!"); 133bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson 1341c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar // Emit function epilog (to return). 1351c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar EmitReturnBlock(); 136f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar 137f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar // Emit debug descriptor for function end. 138e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson if (CGDebugInfo *DI = getDebugInfo()) { 139f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar DI->setLocation(EndLoc); 140f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar DI->EmitRegionEnd(CurFn, Builder); 141f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar } 142f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar 14388b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar EmitFunctionEpilog(*CurFnInfo, ReturnValue); 1445ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar 145391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner // Remove the AllocaInsertPt instruction, which is just a convenience for us. 146391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner AllocaInsertPt->eraseFromParent(); 147391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner AllocaInsertPt = 0; 148c8aa5f1f264fb230c38182adab944232bb160c2bChris Lattner} 149c8aa5f1f264fb230c38182adab944232bb160c2bChris Lattner 1507c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbarvoid CodeGenFunction::StartFunction(const Decl *D, QualType RetTy, 1517c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar llvm::Function *Fn, 1522284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar const FunctionArgList &Args, 1532284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar SourceLocation StartLoc) { 1544cc1a4703363ff940b6273aeef9d96a87edeb04bAnders Carlsson DidCallStackSave = false; 1557c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar CurFuncDecl = D; 1567c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar FnRetTy = RetTy; 157bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar CurFn = Fn; 1585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer assert(CurFn->isDeclaration() && "Function already has body?"); 159ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner 16055e874299f2ad827646a4ca9ea38c402aaeb38c9Daniel Dunbar llvm::BasicBlock *EntryBB = createBasicBlock("entry", CurFn); 1615ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar 1625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Create a marker to make it easy to insert allocas into the entryblock 16355352a2d616cf9fbb621d10faf8b960b4b268bd8Chris Lattner // later. Don't create this with the builder, because we don't want it 16455352a2d616cf9fbb621d10faf8b960b4b268bd8Chris Lattner // folded. 1655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty); 166f1466848dce9c4c75d96a6cabdc8db560e26aac8Chris Lattner AllocaInsertPt = new llvm::BitCastInst(Undef, llvm::Type::Int32Ty, "", 16755352a2d616cf9fbb621d10faf8b960b4b268bd8Chris Lattner EntryBB); 168f1466848dce9c4c75d96a6cabdc8db560e26aac8Chris Lattner if (Builder.isNamePreserving()) 169f1466848dce9c4c75d96a6cabdc8db560e26aac8Chris Lattner AllocaInsertPt->setName("allocapt"); 170f1466848dce9c4c75d96a6cabdc8db560e26aac8Chris Lattner 17155e874299f2ad827646a4ca9ea38c402aaeb38c9Daniel Dunbar ReturnBlock = createBasicBlock("return"); 1725ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar ReturnValue = 0; 1737c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar if (!RetTy->isVoidType()) 1747c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar ReturnValue = CreateTempAlloca(ConvertType(RetTy), "retval"); 1755ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar 17655352a2d616cf9fbb621d10faf8b960b4b268bd8Chris Lattner Builder.SetInsertPoint(EntryBB); 1774111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner 178af99417156c652a6f04dff643925036dc3241d60Sanjiv Gupta // Emit subprogram debug descriptor. 1797c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar // FIXME: The cast here is a huge hack. 180e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson if (CGDebugInfo *DI = getDebugInfo()) { 1812284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar DI->setLocation(StartLoc); 1822284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 1836ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor DI->EmitFunctionStart(CGM.getMangledName(FD), RetTy, CurFn, Builder); 1842284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar } else { 1852284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar // Just use LLVM function name. 1862284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar DI->EmitFunctionStart(Fn->getName().c_str(), 1872284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar RetTy, CurFn, Builder); 188af99417156c652a6f04dff643925036dc3241d60Sanjiv Gupta } 189af99417156c652a6f04dff643925036dc3241d60Sanjiv Gupta } 190af99417156c652a6f04dff643925036dc3241d60Sanjiv Gupta 19188b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar // FIXME: Leaked. 192541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar CurFnInfo = &CGM.getTypes().getFunctionInfo(FnRetTy, Args); 19388b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar EmitFunctionProlog(*CurFnInfo, CurFn, Args); 194751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson 195751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson // If any of the arguments have a variably modified type, make sure to 196751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson // emit the type size. 197751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end(); 198751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson i != e; ++i) { 199751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson QualType Ty = i->second; 200751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson 201751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson if (Ty->isVariablyModifiedType()) 202751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson EmitVLASize(Ty); 203751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson } 2047c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar} 205eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman 2067c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbarvoid CodeGenFunction::GenerateCode(const FunctionDecl *FD, 2077c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar llvm::Function *Fn) { 208e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson // Check if we should generate debug info for this function. 209e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson if (CGM.getDebugInfo() && !FD->getAttr<NodebugAttr>()) 210e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson DebugInfo = CGM.getDebugInfo(); 211e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson 2127c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar FunctionArgList Args; 213eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman if (FD->getNumParams()) { 21472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor const FunctionProtoType* FProto = FD->getType()->getAsFunctionProtoType(); 215eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman assert(FProto && "Function def must have prototype!"); 2167c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar 2177c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) 2187c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar Args.push_back(std::make_pair(FD->getParamDecl(i), 2197c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar FProto->getArgType(i))); 2205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 221af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 2222284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar StartFunction(FD, FD->getResultType(), Fn, Args, 2232284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar cast<CompoundStmt>(FD->getBody())->getLBracLoc()); 2247c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar 225af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar EmitStmt(FD->getBody()); 226af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 227af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar const CompoundStmt *S = dyn_cast<CompoundStmt>(FD->getBody()); 228af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar if (S) { 229af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar FinishFunction(S->getRBracLoc()); 230af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar } else { 231af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar FinishFunction(); 232af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar } 2335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 2345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 2350946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner/// ContainsLabel - Return true if the statement contains a label in it. If 2360946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner/// this statement is not executed normally, it not containing a label means 2370946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner/// that we can just remove the code. 2380946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattnerbool CodeGenFunction::ContainsLabel(const Stmt *S, bool IgnoreCaseStmts) { 2390946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner // Null statement, not a label! 2400946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner if (S == 0) return false; 2410946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner 2420946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner // If this is a label, we have to emit the code, consider something like: 2430946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner // if (0) { ... foo: bar(); } goto foo; 2440946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner if (isa<LabelStmt>(S)) 2450946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner return true; 2460946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner 2470946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner // If this is a case/default statement, and we haven't seen a switch, we have 2480946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner // to emit the code. 2490946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner if (isa<SwitchCase>(S) && !IgnoreCaseStmts) 2500946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner return true; 2510946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner 2520946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner // If this is a switch statement, we want to ignore cases below it. 2530946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner if (isa<SwitchStmt>(S)) 2540946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner IgnoreCaseStmts = true; 2550946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner 2560946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner // Scan subexpressions for verboten labels. 2570946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end(); 2580946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner I != E; ++I) 2590946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner if (ContainsLabel(*I, IgnoreCaseStmts)) 2600946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner return true; 2610946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner 2620946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner return false; 2630946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner} 2640946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner 26531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner 26631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// ConstantFoldsToSimpleInteger - If the sepcified expression does not fold to 26731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// a constant, or if it does but contains a label, return 0. If it constant 26831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// folds to 'true' and does not contain a label, return 1, if it constant folds 26931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// to 'false' and does not contain a label, return -1. 27031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattnerint CodeGenFunction::ConstantFoldsToSimpleInteger(const Expr *Cond) { 27136bc14c3a1cf63ee306df5687ac8e85f924f8639Daniel Dunbar // FIXME: Rename and handle conversion of other evaluatable things 27236bc14c3a1cf63ee306df5687ac8e85f924f8639Daniel Dunbar // to bool. 27364712f196bffd41fb0552c2643b07a25c3e45082Anders Carlsson Expr::EvalResult Result; 27464712f196bffd41fb0552c2643b07a25c3e45082Anders Carlsson if (!Cond->Evaluate(Result, getContext()) || !Result.Val.isInt() || 27564712f196bffd41fb0552c2643b07a25c3e45082Anders Carlsson Result.HasSideEffects) 276ef5a66d8171eb95e948107f8ee7707b360aaff25Anders Carlsson return 0; // Not foldable, not integer or not fully evaluatable. 27731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner 27831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner if (CodeGenFunction::ContainsLabel(Cond)) 27931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner return 0; // Contains a label. 28031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner 28164712f196bffd41fb0552c2643b07a25c3e45082Anders Carlsson return Result.Val.getInt().getBoolValue() ? 1 : -1; 28231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner} 28331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner 28431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner 28531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an if 28631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// statement) to the specified blocks. Based on the condition, this might try 28731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// to simplify the codegen of the conditional based on the branch. 28831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// 28931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattnervoid CodeGenFunction::EmitBranchOnBoolExpr(const Expr *Cond, 29031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner llvm::BasicBlock *TrueBlock, 29131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner llvm::BasicBlock *FalseBlock) { 29231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner if (const ParenExpr *PE = dyn_cast<ParenExpr>(Cond)) 29331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner return EmitBranchOnBoolExpr(PE->getSubExpr(), TrueBlock, FalseBlock); 29431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner 29531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner if (const BinaryOperator *CondBOp = dyn_cast<BinaryOperator>(Cond)) { 29631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // Handle X && Y in a condition. 29731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner if (CondBOp->getOpcode() == BinaryOperator::LAnd) { 29831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // If we have "1 && X", simplify the code. "0 && X" would have constant 29931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // folded if the case was simple enough. 30031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == 1) { 30131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // br(1 && X) -> br(X). 30231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); 30331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner } 30431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner 30531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // If we have "X && 1", simplify the code to use an uncond branch. 30631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // "X && 0" would have been constant folded to 0. 30731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == 1) { 30831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // br(X && 1) -> br(X). 30931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock); 31031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner } 31131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner 31231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // Emit the LHS as a conditional. If the LHS conditional is false, we 31331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // want to jump to the FalseBlock. 3149615ecb44f549ae9fa2b4db6ff46bc78befbf62cDaniel Dunbar llvm::BasicBlock *LHSTrue = createBasicBlock("land.lhs.true"); 31531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner EmitBranchOnBoolExpr(CondBOp->getLHS(), LHSTrue, FalseBlock); 31631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner EmitBlock(LHSTrue); 31731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner 31831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); 31931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner return; 32031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner } else if (CondBOp->getOpcode() == BinaryOperator::LOr) { 32131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // If we have "0 || X", simplify the code. "1 || X" would have constant 32231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // folded if the case was simple enough. 32331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == -1) { 32431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // br(0 || X) -> br(X). 32531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); 32631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner } 32731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner 32831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // If we have "X || 0", simplify the code to use an uncond branch. 32931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // "X || 1" would have been constant folded to 1. 33031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == -1) { 33131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // br(X || 0) -> br(X). 33231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock); 33331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner } 33431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner 33531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // Emit the LHS as a conditional. If the LHS conditional is true, we 33631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // want to jump to the TrueBlock. 3379615ecb44f549ae9fa2b4db6ff46bc78befbf62cDaniel Dunbar llvm::BasicBlock *LHSFalse = createBasicBlock("lor.lhs.false"); 33831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, LHSFalse); 33931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner EmitBlock(LHSFalse); 34031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner 34131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); 34231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner return; 34331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner } 344552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner } 345552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner 346552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner if (const UnaryOperator *CondUOp = dyn_cast<UnaryOperator>(Cond)) { 347552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner // br(!x, t, f) -> br(x, f, t) 348552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner if (CondUOp->getOpcode() == UnaryOperator::LNot) 349552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner return EmitBranchOnBoolExpr(CondUOp->getSubExpr(), FalseBlock, TrueBlock); 35031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner } 35131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner 35209b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar if (const ConditionalOperator *CondOp = dyn_cast<ConditionalOperator>(Cond)) { 35309b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar // Handle ?: operator. 35409b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar 35509b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar // Just ignore GNU ?: extension. 35609b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar if (CondOp->getLHS()) { 35709b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar // br(c ? x : y, t, f) -> br(c, br(x, t, f), br(y, t, f)) 35809b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true"); 35909b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false"); 36009b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar EmitBranchOnBoolExpr(CondOp->getCond(), LHSBlock, RHSBlock); 36109b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar EmitBlock(LHSBlock); 36209b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar EmitBranchOnBoolExpr(CondOp->getLHS(), TrueBlock, FalseBlock); 36309b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar EmitBlock(RHSBlock); 36409b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar EmitBranchOnBoolExpr(CondOp->getRHS(), TrueBlock, FalseBlock); 36509b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar return; 36609b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar } 36709b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar } 36809b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar 36931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // Emit the code with the fully general case. 37031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner llvm::Value *CondV = EvaluateExprAsBool(Cond); 37131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner Builder.CreateCondBr(CondV, TrueBlock, FalseBlock); 37231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner} 37331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner 37488a981b47c7face1b1fdaa9074256245107b9ca9Devang Patel/// getCGRecordLayout - Return record layout info. 37588a981b47c7face1b1fdaa9074256245107b9ca9Devang Patelconst CGRecordLayout *CodeGenFunction::getCGRecordLayout(CodeGenTypes &CGT, 376af31913e48c96fddb45a0fd33f25617546502cbbChris Lattner QualType Ty) { 377af31913e48c96fddb45a0fd33f25617546502cbbChris Lattner const RecordType *RTy = Ty->getAsRecordType(); 378af31913e48c96fddb45a0fd33f25617546502cbbChris Lattner assert (RTy && "Unexpected type. RecordType expected here."); 379b84a06e68ffd71da22e3c75b6e4bbdba37816413Devang Patel 380af31913e48c96fddb45a0fd33f25617546502cbbChris Lattner return CGT.getCGRecordLayout(RTy->getDecl()); 381b84a06e68ffd71da22e3c75b6e4bbdba37816413Devang Patel} 382dc5e8268292046114ffe02e48773572a91a310f1Chris Lattner 383488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar/// ErrorUnsupported - Print out an error that codegen doesn't support the 384dc5e8268292046114ffe02e48773572a91a310f1Chris Lattner/// specified stmt yet. 38590df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbarvoid CodeGenFunction::ErrorUnsupported(const Stmt *S, const char *Type, 38690df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar bool OmitOnError) { 38790df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar CGM.ErrorUnsupported(S, Type, OmitOnError); 388dc5e8268292046114ffe02e48773572a91a310f1Chris Lattner} 389dc5e8268292046114ffe02e48773572a91a310f1Chris Lattner 3900ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbarunsigned CodeGenFunction::GetIDForAddrOfLabel(const LabelStmt *L) { 3910ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar // Use LabelIDs.size() as the new ID if one hasn't been assigned. 3920ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar return LabelIDs.insert(std::make_pair(L, LabelIDs.size())).first->second; 3930ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar} 3940ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar 3953d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlssonvoid CodeGenFunction::EmitMemSetToZero(llvm::Value *DestPtr, QualType Ty) 3963d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson{ 3973d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); 3983d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson if (DestPtr->getType() != BP) 3993d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp"); 4003d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson 4013d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson // Get size and alignment info for this aggregate. 4023d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty); 4033d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson 4043d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson // FIXME: Handle variable sized types. 4053d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson const llvm::Type *IntPtr = llvm::IntegerType::get(LLVMPointerWidth); 4063d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson 4073d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson Builder.CreateCall4(CGM.getMemSetFn(), DestPtr, 4083d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson llvm::ConstantInt::getNullValue(llvm::Type::Int8Ty), 4093d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson // TypeInfo.first describes size in bits. 4103d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson llvm::ConstantInt::get(IntPtr, TypeInfo.first/8), 4113d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson llvm::ConstantInt::get(llvm::Type::Int32Ty, 4123d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson TypeInfo.second/8)); 4133d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson} 4143d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson 4150ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbarvoid CodeGenFunction::EmitIndirectSwitches() { 4160ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar llvm::BasicBlock *Default; 4170ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar 41876526a5245ec2283b0c85fcef507d4c2dec90715Daniel Dunbar if (IndirectSwitches.empty()) 41976526a5245ec2283b0c85fcef507d4c2dec90715Daniel Dunbar return; 42076526a5245ec2283b0c85fcef507d4c2dec90715Daniel Dunbar 4210ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar if (!LabelIDs.empty()) { 4220ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar Default = getBasicBlockForLabel(LabelIDs.begin()->first); 4230ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar } else { 4240ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar // No possible targets for indirect goto, just emit an infinite 4250ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar // loop. 42655e874299f2ad827646a4ca9ea38c402aaeb38c9Daniel Dunbar Default = createBasicBlock("indirectgoto.loop", CurFn); 4270ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar llvm::BranchInst::Create(Default, Default); 4280ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar } 4290ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar 4300ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar for (std::vector<llvm::SwitchInst*>::iterator i = IndirectSwitches.begin(), 4310ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar e = IndirectSwitches.end(); i != e; ++i) { 4320ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar llvm::SwitchInst *I = *i; 4330ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar 4340ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar I->setSuccessor(0, Default); 4350ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar for (std::map<const LabelStmt*,unsigned>::iterator LI = LabelIDs.begin(), 4360ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar LE = LabelIDs.end(); LI != LE; ++LI) { 4370ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar I->addCase(llvm::ConstantInt::get(llvm::Type::Int32Ty, 4380ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar LI->second), 4390ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar getBasicBlockForLabel(LI->first)); 4400ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar } 4410ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar } 4420ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar} 443ddf7cac45d85b73127adbbd91a2b28fc7291c57eAnders Carlsson 444dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlssonllvm::Value *CodeGenFunction::GetVLASize(const VariableArrayType *VAT) 445dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlsson{ 446dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlsson llvm::Value *&SizeEntry = VLASizeMap[VAT]; 447dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlsson 448f666b7780d04186521adcaedb0e15dfa4d5e6933Anders Carlsson assert(SizeEntry && "Did not emit size for type"); 449f666b7780d04186521adcaedb0e15dfa4d5e6933Anders Carlsson return SizeEntry; 450f666b7780d04186521adcaedb0e15dfa4d5e6933Anders Carlsson} 451dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlsson 45260d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlssonllvm::Value *CodeGenFunction::EmitVLASize(QualType Ty) 453f666b7780d04186521adcaedb0e15dfa4d5e6933Anders Carlsson{ 45460d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson assert(Ty->isVariablyModifiedType() && 45560d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson "Must pass variably modified type to EmitVLASizes!"); 456f666b7780d04186521adcaedb0e15dfa4d5e6933Anders Carlsson 45760d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson if (const VariableArrayType *VAT = getContext().getAsVariableArrayType(Ty)) { 45860d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson llvm::Value *&SizeEntry = VLASizeMap[VAT]; 45960d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson 460fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson if (!SizeEntry) { 461fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson // Get the element size; 462fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson llvm::Value *ElemSize; 46360d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson 464fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson QualType ElemTy = VAT->getElementType(); 46596f214776c0f69069fee4d67557c8c7f416009a8Anders Carlsson 46696f214776c0f69069fee4d67557c8c7f416009a8Anders Carlsson const llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); 46796f214776c0f69069fee4d67557c8c7f416009a8Anders Carlsson 468fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson if (ElemTy->isVariableArrayType()) 469fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson ElemSize = EmitVLASize(ElemTy); 470fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson else { 47196f214776c0f69069fee4d67557c8c7f416009a8Anders Carlsson ElemSize = llvm::ConstantInt::get(SizeTy, 472fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson getContext().getTypeSize(ElemTy) / 8); 473fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson } 47460d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson 475fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson llvm::Value *NumElements = EmitScalarExpr(VAT->getSizeExpr()); 47696f214776c0f69069fee4d67557c8c7f416009a8Anders Carlsson NumElements = Builder.CreateIntCast(NumElements, SizeTy, false, "tmp"); 47796f214776c0f69069fee4d67557c8c7f416009a8Anders Carlsson 478fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson SizeEntry = Builder.CreateMul(ElemSize, NumElements); 479fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson } 48060d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson 48160d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson return SizeEntry; 48260d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson } else if (const PointerType *PT = Ty->getAsPointerType()) 48360d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson EmitVLASize(PT->getPointeeType()); 484f666b7780d04186521adcaedb0e15dfa4d5e6933Anders Carlsson else { 48560d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson assert(0 && "unknown VM type!"); 486dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlsson } 48760d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson 48860d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson return 0; 489dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlsson} 4904fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman 4914fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedmanllvm::Value* CodeGenFunction::EmitVAListRef(const Expr* E) { 4924fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman if (CGM.getContext().getBuiltinVaListType()->isArrayType()) { 4934fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman return EmitScalarExpr(E); 4944fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman } 4954fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman return EmitLValue(E).getAddress(); 4964fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman} 4976ccc47698d0311ddabf32fa0f6db8e4f09ac96f8Anders Carlsson 4986fc559136b8ef98bfb824a0fd49df385405f2879Anders Carlssonvoid CodeGenFunction::PushCleanupBlock(llvm::BasicBlock *CleanupBlock) 4996ccc47698d0311ddabf32fa0f6db8e4f09ac96f8Anders Carlsson{ 5006ccc47698d0311ddabf32fa0f6db8e4f09ac96f8Anders Carlsson CleanupEntries.push_back(CleanupEntry(CleanupBlock)); 5016ccc47698d0311ddabf32fa0f6db8e4f09ac96f8Anders Carlsson} 502c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson 503c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlssonvoid CodeGenFunction::EmitCleanupBlocks(size_t OldCleanupStackSize) 504c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson{ 505c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson assert(CleanupEntries.size() >= OldCleanupStackSize && 506c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson "Cleanup stack mismatch!"); 507c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson 508c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson while (CleanupEntries.size() > OldCleanupStackSize) 509c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson EmitCleanupBlock(); 510c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson} 511c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson 512bb66f9f2e454135b86462d121629275b6ac38e96Anders CarlssonCodeGenFunction::CleanupBlockInfo CodeGenFunction::PopCleanupBlock() 513c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson{ 514bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson CleanupEntry &CE = CleanupEntries.back(); 515bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson 516bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson llvm::BasicBlock *CleanupBlock = CE.CleanupBlock; 517bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson 518bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson std::vector<llvm::BasicBlock *> Blocks; 519bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson std::swap(Blocks, CE.Blocks); 520bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson 521bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson std::vector<llvm::BranchInst *> BranchFixups; 522bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson std::swap(BranchFixups, CE.BranchFixups); 523bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson 524bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson CleanupEntries.pop_back(); 525bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson 526ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson // Check if any branch fixups pointed to the scope we just popped. If so, 527ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson // we can remove them. 528ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson for (size_t i = 0, e = BranchFixups.size(); i != e; ++i) { 529ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson llvm::BasicBlock *Dest = BranchFixups[i]->getSuccessor(0); 530ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson BlockScopeMap::iterator I = BlockScopes.find(Dest); 531d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson 532ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson if (I == BlockScopes.end()) 533ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson continue; 5341093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson 535ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson assert(I->second <= CleanupEntries.size() && "Invalid branch fixup!"); 536d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson 537ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson if (I->second == CleanupEntries.size()) { 538ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson // We don't need to do this branch fixup. 539ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson BranchFixups[i] = BranchFixups.back(); 540ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson BranchFixups.pop_back(); 541ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson i--; 542ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson e--; 543ad9d00e371f4f4e63a540f4e4c501797db2a43deAnders Carlsson continue; 5441093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson } 5451093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson } 546d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson 547bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson llvm::BasicBlock *SwitchBlock = 0; 548bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson llvm::BasicBlock *EndBlock = 0; 5491093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson if (!BranchFixups.empty()) { 550bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson SwitchBlock = createBasicBlock("cleanup.switch"); 551bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson EndBlock = createBasicBlock("cleanup.end"); 5521093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson 553bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson llvm::BasicBlock *CurBB = Builder.GetInsertBlock(); 5540ae7b2b2f964bd6145d65ef52dc2a28025b2bd06Anders Carlsson 555bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson Builder.SetInsertPoint(SwitchBlock); 556bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson 5571093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson llvm::Value *DestCodePtr = CreateTempAlloca(llvm::Type::Int32Ty, 5581093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson "cleanup.dst"); 5591093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson llvm::Value *DestCode = Builder.CreateLoad(DestCodePtr, "tmp"); 5601093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson 5611093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson // Create a switch instruction to determine where to jump next. 562bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson llvm::SwitchInst *SI = Builder.CreateSwitch(DestCode, EndBlock, 5631093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson BranchFixups.size()); 564bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson 56546831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson // Restore the current basic block (if any) 5660ae7b2b2f964bd6145d65ef52dc2a28025b2bd06Anders Carlsson if (CurBB) { 56746831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson Builder.SetInsertPoint(CurBB); 5680ae7b2b2f964bd6145d65ef52dc2a28025b2bd06Anders Carlsson 5690ae7b2b2f964bd6145d65ef52dc2a28025b2bd06Anders Carlsson // If we had a current basic block, we also need to emit an instruction 5700ae7b2b2f964bd6145d65ef52dc2a28025b2bd06Anders Carlsson // to initialize the cleanup destination. 5710ae7b2b2f964bd6145d65ef52dc2a28025b2bd06Anders Carlsson Builder.CreateStore(llvm::Constant::getNullValue(llvm::Type::Int32Ty), 5720ae7b2b2f964bd6145d65ef52dc2a28025b2bd06Anders Carlsson DestCodePtr); 5730ae7b2b2f964bd6145d65ef52dc2a28025b2bd06Anders Carlsson } else 57446831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson Builder.ClearInsertionPoint(); 575bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson 5761093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson for (size_t i = 0, e = BranchFixups.size(); i != e; ++i) { 5771093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson llvm::BranchInst *BI = BranchFixups[i]; 5781093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson llvm::BasicBlock *Dest = BI->getSuccessor(0); 5791093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson 5801093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson // Fixup the branch instruction to point to the cleanup block. 5811093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson BI->setSuccessor(0, CleanupBlock); 582d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson 5831093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson if (CleanupEntries.empty()) { 584cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson llvm::ConstantInt *ID; 585cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson 586cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson // Check if we already have a destination for this block. 587cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson if (Dest == SI->getDefaultDest()) 588cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson ID = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0); 589cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson else { 590cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson ID = SI->findCaseDest(Dest); 591cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson if (!ID) { 592cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson // No code found, get a new unique one by using the number of 593cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson // switch successors. 594cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson ID = llvm::ConstantInt::get(llvm::Type::Int32Ty, 595cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson SI->getNumSuccessors()); 596cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson SI->addCase(ID, Dest); 597cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson } 598cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson } 599cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson 600cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson // Store the jump destination before the branch instruction. 601cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson new llvm::StoreInst(ID, DestCodePtr, BI); 6021093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson } else { 6031093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson // We need to jump through another cleanup block. Create a pad block 6041093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson // with a branch instruction that jumps to the final destination and 6051093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson // add it as a branch fixup to the current cleanup scope. 6061093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson 6071093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson // Create the pad block. 6081093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson llvm::BasicBlock *CleanupPad = createBasicBlock("cleanup.pad", CurFn); 609cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson 610cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson // Create a unique case ID. 611cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson llvm::ConstantInt *ID = llvm::ConstantInt::get(llvm::Type::Int32Ty, 612cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson SI->getNumSuccessors()); 613cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson 614cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson // Store the jump destination before the branch instruction. 615cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson new llvm::StoreInst(ID, DestCodePtr, BI); 616cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson 6171093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson // Add it as the destination. 618cc8992021caf316a282116d509d2a4fb54349341Anders Carlsson SI->addCase(ID, CleanupPad); 6191093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson 6201093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson // Create the branch to the final destination. 6211093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson llvm::BranchInst *BI = llvm::BranchInst::Create(Dest); 6221093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson CleanupPad->getInstList().push_back(BI); 6231093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson 6241093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson // And add it as a branch fixup. 6251093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson CleanupEntries.back().BranchFixups.push_back(BI); 6261093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson } 6271093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson } 6281093c2c40a7c262d206d724e912b32cbad2d4e14Anders Carlsson } 629d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson 630bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson // Remove all blocks from the block scope map. 631bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson for (size_t i = 0, e = Blocks.size(); i != e; ++i) { 632bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson assert(BlockScopes.count(Blocks[i]) && 633bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson "Did not find block in scope map!"); 634bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson 635bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson BlockScopes.erase(Blocks[i]); 636bd6fa3d032acd7eafc6c10827c41103df45beab7Anders Carlsson } 637d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson 638bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson return CleanupBlockInfo(CleanupBlock, SwitchBlock, EndBlock); 639d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson} 640d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson 641d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlssonvoid CodeGenFunction::EmitCleanupBlock() 642d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson{ 643bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson CleanupBlockInfo Info = PopCleanupBlock(); 644d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson 645bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson EmitBlock(Info.CleanupBlock); 646d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson 647bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson if (Info.SwitchBlock) 648bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson EmitBlock(Info.SwitchBlock); 649bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson if (Info.EndBlock) 650bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson EmitBlock(Info.EndBlock); 651d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson} 652d66a9f9019c00d889990c947e16a8f019aa2c1f8Anders Carlsson 65387eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlssonvoid CodeGenFunction::AddBranchFixup(llvm::BranchInst *BI) 65487eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson{ 65587eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson assert(!CleanupEntries.empty() && 65687eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson "Trying to add branch fixup without cleanup block!"); 65787eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson 65887eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson // FIXME: We could be more clever here and check if there's already a 65987eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson // branch fixup for this destination and recycle it. 66087eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson CleanupEntries.back().BranchFixups.push_back(BI); 66187eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson} 66287eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson 66387eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlssonvoid CodeGenFunction::EmitBranchThroughCleanup(llvm::BasicBlock *Dest) 66487eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson{ 66546831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson if (!HaveInsertPoint()) 66646831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson return; 66746831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson 66887eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson llvm::BranchInst* BI = Builder.CreateBr(Dest); 66987eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson 67046831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson Builder.ClearInsertionPoint(); 67146831a93e1805ddaebd68f37cdb5496a86b44cf0Anders Carlsson 67287eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson // The stack is empty, no need to do any cleanup. 67387eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson if (CleanupEntries.empty()) 67487eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson return; 67587eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson 67687eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson if (!Dest->getParent()) { 67787eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson // We are trying to branch to a block that hasn't been inserted yet. 67887eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson AddBranchFixup(BI); 67987eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson return; 68087eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson } 68187eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson 68287eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson BlockScopeMap::iterator I = BlockScopes.find(Dest); 68387eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson if (I == BlockScopes.end()) { 68487eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson // We are trying to jump to a block that is outside of any cleanup scope. 68587eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson AddBranchFixup(BI); 68687eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson return; 68787eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson } 68887eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson 68987eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson assert(I->second < CleanupEntries.size() && 69087eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson "Trying to branch into cleanup region"); 69187eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson 69287eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson if (I->second == CleanupEntries.size() - 1) { 69387eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson // We have a branch to a block in the same scope. 69487eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson return; 69587eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson } 69687eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson 69787eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson AddBranchFixup(BI); 69887eaf17cc88516277e4389dfa15df93ecfdce559Anders Carlsson} 699