CodeGenFunction.cpp revision 777d6e56ad9b1fed9866daf3ee6486d85c5b7d32
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" 164c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall#include "CGCXXABI.h" 173f2af1002249c8acc9ce17f1fc50324864feb8e1Eli Friedman#include "CGDebugInfo.h" 18f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall#include "CGException.h" 195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Basic/TargetInfo.h" 2031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner#include "clang/AST/APValue.h" 21de7fb8413b13651fd85b7125d08b3c9ac2816d9dDaniel Dunbar#include "clang/AST/ASTContext.h" 22c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel Dunbar#include "clang/AST/Decl.h" 232b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson#include "clang/AST/DeclCXX.h" 246a1e0eb557d47e85185e09bdf8721f53f4bf9c9cMike Stump#include "clang/AST/StmtCXX.h" 257255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner#include "clang/Frontend/CodeGenOptions.h" 264e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump#include "llvm/Target/TargetData.h" 277255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner#include "llvm/Intrinsics.h" 285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang; 295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace CodeGen; 305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 311eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpCodeGenFunction::CodeGenFunction(CodeGenModule &cgm) 325936e33bf74dd6bf126ceee0f6169a2593d03a69John McCall : CodeGenTypeCache(cgm), CGM(cgm), 335936e33bf74dd6bf126ceee0f6169a2593d03a69John McCall Target(CGM.getContext().Target), Builder(cgm.getModule().getContext()), 34f85e193739c953358c865005855253af4f68a497John McCall AutoreleaseResult(false), BlockInfo(0), BlockPointer(0), 35777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall NormalCleanupDest(0), NextCleanupDestIndex(1), 36777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall EHResumeBlock(0), ExceptionSlot(0), EHSelectorSlot(0), 3793c332a8ba2c193c435b293966d343dab15f555bJohn McCall DebugInfo(0), DisableDebugInfo(false), DidCallStackSave(false), 3893c332a8ba2c193c435b293966d343dab15f555bJohn McCall IndirectBranch(0), SwitchInsn(0), CaseRangeBlock(0), UnreachableBlock(0), 392504941793b549323f9d29c62507cf21d865fadeJohn McCall CXXThisDecl(0), CXXThisValue(0), CXXVTTDecl(0), CXXVTTValue(0), 40150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall OutermostConditional(0), TerminateLandingPad(0), TerminateHandler(0), 4183252dcfe61aaebcb6bc117e71dc12968729513fChris Lattner TrapBB(0) { 42c1cfdf8647a499b6b3024f4bd14a236cddb23988Anders Carlsson 439c276ae0f24d4cee8f7954069d4b8eae45d0447dMike Stump CatchUndefined = getContext().getLangOptions().CatchUndefined; 444c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall CGM.getCXXABI().getMangleContext().startNewFunction(); 454111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner} 465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 489cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnerllvm::Type *CodeGenFunction::ConvertTypeForMem(QualType T) { 498b1a343b6b360d63d5dc8a6beb841ce4414c1e00Daniel Dunbar return CGM.getTypes().ConvertTypeForMem(T); 508b1a343b6b360d63d5dc8a6beb841ce4414c1e00Daniel Dunbar} 518b1a343b6b360d63d5dc8a6beb841ce4414c1e00Daniel Dunbar 529cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnerllvm::Type *CodeGenFunction::ConvertType(QualType T) { 535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return CGM.getTypes().ConvertType(T); 545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 56f2aac84709c418189e476ad591848dad50291885John McCallbool CodeGenFunction::hasAggregateLLVMType(QualType type) { 57f2aac84709c418189e476ad591848dad50291885John McCall switch (type.getCanonicalType()->getTypeClass()) { 58f2aac84709c418189e476ad591848dad50291885John McCall#define TYPE(name, parent) 59f2aac84709c418189e476ad591848dad50291885John McCall#define ABSTRACT_TYPE(name, parent) 60f2aac84709c418189e476ad591848dad50291885John McCall#define NON_CANONICAL_TYPE(name, parent) case Type::name: 61f2aac84709c418189e476ad591848dad50291885John McCall#define DEPENDENT_TYPE(name, parent) case Type::name: 62f2aac84709c418189e476ad591848dad50291885John McCall#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(name, parent) case Type::name: 63f2aac84709c418189e476ad591848dad50291885John McCall#include "clang/AST/TypeNodes.def" 64f2aac84709c418189e476ad591848dad50291885John McCall llvm_unreachable("non-canonical or dependent type in IR-generation"); 65f2aac84709c418189e476ad591848dad50291885John McCall 66f2aac84709c418189e476ad591848dad50291885John McCall case Type::Builtin: 67f2aac84709c418189e476ad591848dad50291885John McCall case Type::Pointer: 68f2aac84709c418189e476ad591848dad50291885John McCall case Type::BlockPointer: 69f2aac84709c418189e476ad591848dad50291885John McCall case Type::LValueReference: 70f2aac84709c418189e476ad591848dad50291885John McCall case Type::RValueReference: 71f2aac84709c418189e476ad591848dad50291885John McCall case Type::MemberPointer: 72f2aac84709c418189e476ad591848dad50291885John McCall case Type::Vector: 73f2aac84709c418189e476ad591848dad50291885John McCall case Type::ExtVector: 74f2aac84709c418189e476ad591848dad50291885John McCall case Type::FunctionProto: 75f2aac84709c418189e476ad591848dad50291885John McCall case Type::FunctionNoProto: 76f2aac84709c418189e476ad591848dad50291885John McCall case Type::Enum: 77f2aac84709c418189e476ad591848dad50291885John McCall case Type::ObjCObjectPointer: 78f2aac84709c418189e476ad591848dad50291885John McCall return false; 79f2aac84709c418189e476ad591848dad50291885John McCall 80f2aac84709c418189e476ad591848dad50291885John McCall // Complexes, arrays, records, and Objective-C objects. 81f2aac84709c418189e476ad591848dad50291885John McCall case Type::Complex: 82f2aac84709c418189e476ad591848dad50291885John McCall case Type::ConstantArray: 83f2aac84709c418189e476ad591848dad50291885John McCall case Type::IncompleteArray: 84f2aac84709c418189e476ad591848dad50291885John McCall case Type::VariableArray: 85f2aac84709c418189e476ad591848dad50291885John McCall case Type::Record: 86f2aac84709c418189e476ad591848dad50291885John McCall case Type::ObjCObject: 87f2aac84709c418189e476ad591848dad50291885John McCall case Type::ObjCInterface: 88f2aac84709c418189e476ad591848dad50291885John McCall return true; 89f2aac84709c418189e476ad591848dad50291885John McCall } 90f2aac84709c418189e476ad591848dad50291885John McCall llvm_unreachable("unknown type kind!"); 914111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner} 92391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner 931c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbarvoid CodeGenFunction::EmitReturnBlock() { 941c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar // For cleanliness, we try to avoid emitting the return block for 951c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar // simple cases. 961c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar llvm::BasicBlock *CurBB = Builder.GetInsertBlock(); 971c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar 981c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar if (CurBB) { 991c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar assert(!CurBB->getTerminator() && "Unexpected terminated block."); 1001c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar 10196e18b05ea6b55aa92a1a576f29e9cee73a7e20bDaniel Dunbar // We have a valid insert point, reuse it if it is empty or there are no 10296e18b05ea6b55aa92a1a576f29e9cee73a7e20bDaniel Dunbar // explicit jumps to the return block. 103ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall if (CurBB->empty() || ReturnBlock.getBlock()->use_empty()) { 104ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall ReturnBlock.getBlock()->replaceAllUsesWith(CurBB); 105ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall delete ReturnBlock.getBlock(); 10696e18b05ea6b55aa92a1a576f29e9cee73a7e20bDaniel Dunbar } else 107ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall EmitBlock(ReturnBlock.getBlock()); 1081c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar return; 1091c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar } 1101c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar 1111c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar // Otherwise, if the return block is the target of a single direct 1121c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar // branch then we can just put the code in that block instead. This 1131c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar // cleans up functions which started with a unified return block. 114ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall if (ReturnBlock.getBlock()->hasOneUse()) { 1151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::BranchInst *BI = 116ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall dyn_cast<llvm::BranchInst>(*ReturnBlock.getBlock()->use_begin()); 117f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall if (BI && BI->isUnconditional() && 118ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall BI->getSuccessor(0) == ReturnBlock.getBlock()) { 1191c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar // Reset insertion point and delete the branch. 1201c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar Builder.SetInsertPoint(BI->getParent()); 1211c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar BI->eraseFromParent(); 122ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall delete ReturnBlock.getBlock(); 1231c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar return; 1241c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar } 1251c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar } 1261c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar 127f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // FIXME: We are at an unreachable point, there is no reason to emit the block 128f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // unless it has uses. However, we still need a place to put the debug 129f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // region.end for now. 1301c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar 131ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall EmitBlock(ReturnBlock.getBlock()); 132f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall} 133f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall 134f1549f66a8216a78112286e3978cea2c29d6334cJohn McCallstatic void EmitIfUsed(CodeGenFunction &CGF, llvm::BasicBlock *BB) { 135f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall if (!BB) return; 136f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall if (!BB->use_empty()) 137f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall return CGF.CurFn->getBasicBlockList().push_back(BB); 138f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall delete BB; 1391c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar} 1401c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar 141af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbarvoid CodeGenFunction::FinishFunction(SourceLocation EndLoc) { 142391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner assert(BreakContinueStack.empty() && 143391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner "mismatched push/pop in break/continue stack!"); 1441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 145f85e193739c953358c865005855253af4f68a497John McCall // Pop any cleanups that might have been associated with the 146f85e193739c953358c865005855253af4f68a497John McCall // parameters. Do this in whatever block we're currently in; it's 147f85e193739c953358c865005855253af4f68a497John McCall // important to do this before we enter the return block or return 148f85e193739c953358c865005855253af4f68a497John McCall // edges will be *really* confused. 149f85e193739c953358c865005855253af4f68a497John McCall if (EHStack.stable_begin() != PrologueCleanupDepth) 150f85e193739c953358c865005855253af4f68a497John McCall PopCleanupBlocks(PrologueCleanupDepth); 151f85e193739c953358c865005855253af4f68a497John McCall 1521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // Emit function epilog (to return). 1531c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar EmitReturnBlock(); 154f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar 155a18652fe1e8233fbf8b67484945c7f7b2bf272beDaniel Dunbar if (ShouldInstrumentFunction()) 156a18652fe1e8233fbf8b67484945c7f7b2bf272beDaniel Dunbar EmitFunctionInstrumentation("__cyg_profile_func_exit"); 1577255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner 158f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar // Emit debug descriptor for function end. 159e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson if (CGDebugInfo *DI = getDebugInfo()) { 160f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar DI->setLocation(EndLoc); 1615a6fbcfd8c15a2296f94a0473a68ec09d429827fDevang Patel DI->EmitFunctionEnd(Builder); 162f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar } 163f5bd45c8e6fa7519cdc17ec3ff4917e279c6a041Daniel Dunbar 16435b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner EmitFunctionEpilog(*CurFnInfo); 165cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump EmitEndEHSpec(CurCodeDecl); 1665ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar 167f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall assert(EHStack.empty() && 168f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall "did not remove all scopes from cleanup stack!"); 169f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall 170d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner // If someone did an indirect goto, emit the indirect goto block at the end of 171d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner // the function. 172d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner if (IndirectBranch) { 173d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner EmitBlock(IndirectBranch->getParent()); 174d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner Builder.ClearInsertionPoint(); 175d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner } 176d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner 177391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner // Remove the AllocaInsertPt instruction, which is just a convenience for us. 178481769b5dc102b0256b35581e787909ad5edfab5Chris Lattner llvm::Instruction *Ptr = AllocaInsertPt; 179391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner AllocaInsertPt = 0; 180481769b5dc102b0256b35581e787909ad5edfab5Chris Lattner Ptr->eraseFromParent(); 181d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner 182d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner // If someone took the address of a label but never did an indirect goto, we 183d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner // made a zero entry PHI node, which is illegal, zap it now. 184d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner if (IndirectBranch) { 185d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner llvm::PHINode *PN = cast<llvm::PHINode>(IndirectBranch->getAddress()); 186d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner if (PN->getNumIncomingValues() == 0) { 187d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner PN->replaceAllUsesWith(llvm::UndefValue::get(PN->getType())); 188d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner PN->eraseFromParent(); 189d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner } 190d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner } 191f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall 192777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall EmitIfUsed(*this, EHResumeBlock); 193f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall EmitIfUsed(*this, TerminateLandingPad); 194f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall EmitIfUsed(*this, TerminateHandler); 195f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall EmitIfUsed(*this, UnreachableBlock); 196744016dde06fcffd50931e94a98c850f8b12cd87John McCall 197744016dde06fcffd50931e94a98c850f8b12cd87John McCall if (CGM.getCodeGenOpts().EmitDeclMetadata) 198744016dde06fcffd50931e94a98c850f8b12cd87John McCall EmitDeclMetadata(); 199c8aa5f1f264fb230c38182adab944232bb160c2bChris Lattner} 200c8aa5f1f264fb230c38182adab944232bb160c2bChris Lattner 2017255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner/// ShouldInstrumentFunction - Return true if the current function should be 2027255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner/// instrumented with __cyg_profile_func_* calls 2037255a2d997b15beae82e627052fdb1b2474495c2Chris Lattnerbool CodeGenFunction::ShouldInstrumentFunction() { 2047255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner if (!CGM.getCodeGenOpts().InstrumentFunctions) 2057255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner return false; 2067aa488a7fc5c3a8cd1a2b93476150e9737760713Ted Kremenek if (!CurFuncDecl || CurFuncDecl->hasAttr<NoInstrumentFunctionAttr>()) 2077255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner return false; 2087255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner return true; 2097255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner} 2107255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner 2117255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner/// EmitFunctionInstrumentation - Emit LLVM code to call the specified 2127255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner/// instrumentation function with the current function and the call site, if 2137255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner/// function instrumentation is enabled. 2147255a2d997b15beae82e627052fdb1b2474495c2Chris Lattnervoid CodeGenFunction::EmitFunctionInstrumentation(const char *Fn) { 2158dab6571b2cab96f44d0a1d6e3edbfdb68b7ed6bChris Lattner // void __cyg_profile_func_{enter,exit} (void *this_fn, void *call_site); 2169cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner llvm::PointerType *PointerTy = Int8PtrTy; 2179cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner llvm::Type *ProfileFuncArgs[] = { PointerTy, PointerTy }; 2182acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *FunctionTy = 21995d318c4c10437db40ca6e15fdf32e04012da58eBenjamin Kramer llvm::FunctionType::get(llvm::Type::getVoidTy(getLLVMContext()), 22095d318c4c10437db40ca6e15fdf32e04012da58eBenjamin Kramer ProfileFuncArgs, false); 2217255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner 2227255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner llvm::Constant *F = CGM.CreateRuntimeFunction(FunctionTy, Fn); 2237255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner llvm::CallInst *CallSite = Builder.CreateCall( 2248dd55a3c3b28d195717c87bbc60e765951d408feBenjamin Kramer CGM.getIntrinsic(llvm::Intrinsic::returnaddress), 22577b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner llvm::ConstantInt::get(Int32Ty, 0), 2267255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner "callsite"); 2277255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner 2288dab6571b2cab96f44d0a1d6e3edbfdb68b7ed6bChris Lattner Builder.CreateCall2(F, 2298dab6571b2cab96f44d0a1d6e3edbfdb68b7ed6bChris Lattner llvm::ConstantExpr::getBitCast(CurFn, PointerTy), 2308dab6571b2cab96f44d0a1d6e3edbfdb68b7ed6bChris Lattner CallSite); 2317255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner} 2327255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner 233be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divackyvoid CodeGenFunction::EmitMCountInstrumentation() { 234be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky llvm::FunctionType *FTy = 235be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky llvm::FunctionType::get(llvm::Type::getVoidTy(getLLVMContext()), false); 236be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky 237be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky llvm::Constant *MCountFn = CGM.CreateRuntimeFunction(FTy, 238be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky Target.getMCountName()); 239be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky Builder.CreateCall(MCountFn); 240be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky} 241be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky 2420ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlssonvoid CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy, 2437c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar llvm::Function *Fn, 244d26bc76c98006609002d9930f8840490e88ac5b5John McCall const CGFunctionInfo &FnInfo, 2452284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar const FunctionArgList &Args, 2469c6082fe89c61af697f017aa80937581cc2128d8Tilmann Scheller SourceLocation StartLoc) { 2470ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlsson const Decl *D = GD.getDecl(); 2480ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlsson 2494cc1a4703363ff940b6273aeef9d96a87edeb04bAnders Carlsson DidCallStackSave = false; 250b5437d238752dc297e42410e98d38d5250fe0463Chris Lattner CurCodeDecl = CurFuncDecl = D; 2517c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar FnRetTy = RetTy; 252bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar CurFn = Fn; 253d26bc76c98006609002d9930f8840490e88ac5b5John McCall CurFnInfo = &FnInfo; 2545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer assert(CurFn->isDeclaration() && "Function already has body?"); 255ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner 256a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen // Pass inline keyword to optimizer if it appears explicitly on any 257a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen // declaration. 258a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) 259a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen for (FunctionDecl::redecl_iterator RI = FD->redecls_begin(), 260a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen RE = FD->redecls_end(); RI != RE; ++RI) 261a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen if (RI->isInlineSpecified()) { 262a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen Fn->addFnAttr(llvm::Attribute::InlineHint); 263a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen break; 264a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen } 265a3fe2842e0cf953241ccc05809afdf84f13798e9Jakob Stoklund Olesen 266f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne if (getContext().getLangOptions().OpenCL) { 267f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne // Add metadata for a kernel function. 268f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) 269f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne if (FD->hasAttr<OpenCLKernelAttr>()) { 270f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne llvm::LLVMContext &Context = getLLVMContext(); 271f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne llvm::NamedMDNode *OpenCLMetadata = 272f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne CGM.getModule().getOrInsertNamedMetadata("opencl.kernels"); 273f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne 274f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne llvm::Value *Op = Fn; 2756f141659cab11109d9931d92d0988f8850778de3Jay Foad OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Op)); 276f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne } 277f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne } 278f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne 27955e874299f2ad827646a4ca9ea38c402aaeb38c9Daniel Dunbar llvm::BasicBlock *EntryBB = createBasicBlock("entry", CurFn); 2805ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar 2815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Create a marker to make it easy to insert allocas into the entryblock 28255352a2d616cf9fbb621d10faf8b960b4b268bd8Chris Lattner // later. Don't create this with the builder, because we don't want it 28355352a2d616cf9fbb621d10faf8b960b4b268bd8Chris Lattner // folded. 28477b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner llvm::Value *Undef = llvm::UndefValue::get(Int32Ty); 28577b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner AllocaInsertPt = new llvm::BitCastInst(Undef, Int32Ty, "", EntryBB); 286f1466848dce9c4c75d96a6cabdc8db560e26aac8Chris Lattner if (Builder.isNamePreserving()) 287f1466848dce9c4c75d96a6cabdc8db560e26aac8Chris Lattner AllocaInsertPt->setName("allocapt"); 2881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 289f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall ReturnBlock = getJumpDestInCurrentScope("return"); 2901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 29155352a2d616cf9fbb621d10faf8b960b4b268bd8Chris Lattner Builder.SetInsertPoint(EntryBB); 2921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 293af99417156c652a6f04dff643925036dc3241d60Sanjiv Gupta // Emit subprogram debug descriptor. 294e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson if (CGDebugInfo *DI = getDebugInfo()) { 295e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall // FIXME: what is going on here and why does it ignore all these 296e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall // interesting type properties? 297e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall QualType FnType = 298e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall getContext().getFunctionType(RetTy, 0, 0, 299e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall FunctionProtoType::ExtProtoInfo()); 300e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall 3012284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar DI->setLocation(StartLoc); 3029c6c3a0e3ae09626d2d4b04e4ffa42c3d7cab32bDevang Patel DI->EmitFunctionStart(GD, FnType, CurFn, Builder); 303af99417156c652a6f04dff643925036dc3241d60Sanjiv Gupta } 304af99417156c652a6f04dff643925036dc3241d60Sanjiv Gupta 305a18652fe1e8233fbf8b67484945c7f7b2bf272beDaniel Dunbar if (ShouldInstrumentFunction()) 306a18652fe1e8233fbf8b67484945c7f7b2bf272beDaniel Dunbar EmitFunctionInstrumentation("__cyg_profile_func_enter"); 3077255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner 308be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky if (CGM.getCodeGenOpts().InstrumentForProfiling) 309be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky EmitMCountInstrumentation(); 310be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky 311b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman if (RetTy->isVoidType()) { 312b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman // Void type; nothing to return. 313b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman ReturnValue = 0; 314b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman } else if (CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect && 315b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman hasAggregateLLVMType(CurFnInfo->getReturnType())) { 316b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman // Indirect aggregate return; emit returned value directly into sret slot. 317647a1ec397fa13af176d07d9f5d071560a94c7a9Daniel Dunbar // This reduces code size, and affects correctness in C++. 318b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman ReturnValue = CurFn->arg_begin(); 319b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman } else { 320647a1ec397fa13af176d07d9f5d071560a94c7a9Daniel Dunbar ReturnValue = CreateIRTemp(RetTy, "retval"); 321f85e193739c953358c865005855253af4f68a497John McCall 322f85e193739c953358c865005855253af4f68a497John McCall // Tell the epilog emitter to autorelease the result. We do this 323f85e193739c953358c865005855253af4f68a497John McCall // now so that various specialized functions can suppress it 324f85e193739c953358c865005855253af4f68a497John McCall // during their IR-generation. 325f85e193739c953358c865005855253af4f68a497John McCall if (getLangOptions().ObjCAutoRefCount && 326f85e193739c953358c865005855253af4f68a497John McCall !CurFnInfo->isReturnsRetained() && 327f85e193739c953358c865005855253af4f68a497John McCall RetTy->isObjCRetainableType()) 328f85e193739c953358c865005855253af4f68a497John McCall AutoreleaseResult = true; 329b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman } 330b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman 331cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump EmitStartEHSpec(CurCodeDecl); 332f85e193739c953358c865005855253af4f68a497John McCall 333f85e193739c953358c865005855253af4f68a497John McCall PrologueCleanupDepth = EHStack.stable_begin(); 33488b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar EmitFunctionProlog(*CurFnInfo, CurFn, Args); 3351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3364c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall if (D && isa<CXXMethodDecl>(D) && cast<CXXMethodDecl>(D)->isInstance()) 3374c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall CGM.getCXXABI().EmitInstanceFunctionProlog(*this); 3382504941793b549323f9d29c62507cf21d865fadeJohn McCall 339751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson // If any of the arguments have a variably modified type, make sure to 340751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson // emit the type size. 341751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end(); 342751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson i != e; ++i) { 343d26bc76c98006609002d9930f8840490e88ac5b5John McCall QualType Ty = (*i)->getType(); 344751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson 345751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson if (Ty->isVariablyModifiedType()) 346bc8d40d85f3fa1e34569834916f18fecaa635152John McCall EmitVariablyModifiedType(Ty); 347751358ff73b155f5384e151e1d18aa3f6e7b061cAnders Carlsson } 3487c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar} 349eb4b7051a596560ef4a1846e3714707f44e9dc30Eli Friedman 3509fc6a7774643a810c8501dae2323e863fefb623eJohn McCallvoid CodeGenFunction::EmitFunctionBody(FunctionArgList &Args) { 3519fc6a7774643a810c8501dae2323e863fefb623eJohn McCall const FunctionDecl *FD = cast<FunctionDecl>(CurGD.getDecl()); 35206a9f3680d22529a2fcf20c52d71cf221d99d910Douglas Gregor assert(FD->getBody()); 35306a9f3680d22529a2fcf20c52d71cf221d99d910Douglas Gregor EmitStmt(FD->getBody()); 354a355e07454463b19829ac92ffd115a097faff0e0John McCall} 355a355e07454463b19829ac92ffd115a097faff0e0John McCall 35639dad53772c42eb36ebec1c81c56ba99d038fb94John McCall/// Tries to mark the given function nounwind based on the 35739dad53772c42eb36ebec1c81c56ba99d038fb94John McCall/// non-existence of any throwing calls within it. We believe this is 35839dad53772c42eb36ebec1c81c56ba99d038fb94John McCall/// lightweight enough to do at -O0. 35939dad53772c42eb36ebec1c81c56ba99d038fb94John McCallstatic void TryMarkNoThrow(llvm::Function *F) { 360b3a29f132794f67108bccc9c7cc3795365e8a965John McCall // LLVM treats 'nounwind' on a function as part of the type, so we 361b3a29f132794f67108bccc9c7cc3795365e8a965John McCall // can't do this on functions that can be overwritten. 362b3a29f132794f67108bccc9c7cc3795365e8a965John McCall if (F->mayBeOverridden()) return; 363b3a29f132794f67108bccc9c7cc3795365e8a965John McCall 36439dad53772c42eb36ebec1c81c56ba99d038fb94John McCall for (llvm::Function::iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI) 36539dad53772c42eb36ebec1c81c56ba99d038fb94John McCall for (llvm::BasicBlock::iterator 36639dad53772c42eb36ebec1c81c56ba99d038fb94John McCall BI = FI->begin(), BE = FI->end(); BI != BE; ++BI) 36739dad53772c42eb36ebec1c81c56ba99d038fb94John McCall if (llvm::CallInst *Call = dyn_cast<llvm::CallInst>(&*BI)) 36839dad53772c42eb36ebec1c81c56ba99d038fb94John McCall if (!Call->doesNotThrow()) 36939dad53772c42eb36ebec1c81c56ba99d038fb94John McCall return; 37039dad53772c42eb36ebec1c81c56ba99d038fb94John McCall F->setDoesNotThrow(true); 37139dad53772c42eb36ebec1c81c56ba99d038fb94John McCall} 37239dad53772c42eb36ebec1c81c56ba99d038fb94John McCall 373d26bc76c98006609002d9930f8840490e88ac5b5John McCallvoid CodeGenFunction::GenerateCode(GlobalDecl GD, llvm::Function *Fn, 374d26bc76c98006609002d9930f8840490e88ac5b5John McCall const CGFunctionInfo &FnInfo) { 3750ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlsson const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl()); 3760ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlsson 377e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson // Check if we should generate debug info for this function. 378aa11289f754d220c9c155b68a4f84cdcfcefef6aDevang Patel if (CGM.getModuleDebugInfo() && !FD->hasAttr<NoDebugAttr>()) 379aa11289f754d220c9c155b68a4f84cdcfcefef6aDevang Patel DebugInfo = CGM.getModuleDebugInfo(); 3801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3817c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar FunctionArgList Args; 3824c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall QualType ResTy = FD->getResultType(); 3831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3846a1e0eb557d47e85185e09bdf8721f53f4bf9c9cMike Stump CurGD = GD; 3854c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall if (isa<CXXMethodDecl>(FD) && cast<CXXMethodDecl>(FD)->isInstance()) 3864c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall CGM.getCXXABI().BuildInstanceFunctionParams(*this, ResTy, Args); 3871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 388d26bc76c98006609002d9930f8840490e88ac5b5John McCall if (FD->getNumParams()) 3897c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) 390d26bc76c98006609002d9930f8840490e88ac5b5John McCall Args.push_back(FD->getParamDecl(i)); 391af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 392a355e07454463b19829ac92ffd115a097faff0e0John McCall SourceRange BodyRange; 393a355e07454463b19829ac92ffd115a097faff0e0John McCall if (Stmt *Body = FD->getBody()) BodyRange = Body->getSourceRange(); 3944365bba95be15342575b4f030c6583a770a5da3dAnders Carlsson 395a355e07454463b19829ac92ffd115a097faff0e0John McCall // Emit the standard function prologue. 396d26bc76c98006609002d9930f8840490e88ac5b5John McCall StartFunction(GD, ResTy, Fn, FnInfo, Args, BodyRange.getBegin()); 3971851a12605bc6f1ea70d11974a315340ebaab6ebAnders Carlsson 398a355e07454463b19829ac92ffd115a097faff0e0John McCall // Generate the body of the function. 3999fc6a7774643a810c8501dae2323e863fefb623eJohn McCall if (isa<CXXDestructorDecl>(FD)) 4009fc6a7774643a810c8501dae2323e863fefb623eJohn McCall EmitDestructorBody(Args); 4019fc6a7774643a810c8501dae2323e863fefb623eJohn McCall else if (isa<CXXConstructorDecl>(FD)) 4029fc6a7774643a810c8501dae2323e863fefb623eJohn McCall EmitConstructorBody(Args); 4039fc6a7774643a810c8501dae2323e863fefb623eJohn McCall else 4049fc6a7774643a810c8501dae2323e863fefb623eJohn McCall EmitFunctionBody(Args); 405c33e4ba20304e222692e77f2c6ad26a5d8d32f83Anders Carlsson 406a355e07454463b19829ac92ffd115a097faff0e0John McCall // Emit the standard function epilogue. 407a355e07454463b19829ac92ffd115a097faff0e0John McCall FinishFunction(BodyRange.getEnd()); 40839dad53772c42eb36ebec1c81c56ba99d038fb94John McCall 40939dad53772c42eb36ebec1c81c56ba99d038fb94John McCall // If we haven't marked the function nothrow through other means, do 41039dad53772c42eb36ebec1c81c56ba99d038fb94John McCall // a quick pass now to see if we can. 41139dad53772c42eb36ebec1c81c56ba99d038fb94John McCall if (!CurFn->doesNotThrow()) 41239dad53772c42eb36ebec1c81c56ba99d038fb94John McCall TryMarkNoThrow(CurFn); 4135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 4145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 4150946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner/// ContainsLabel - Return true if the statement contains a label in it. If 4160946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner/// this statement is not executed normally, it not containing a label means 4170946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner/// that we can just remove the code. 4180946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattnerbool CodeGenFunction::ContainsLabel(const Stmt *S, bool IgnoreCaseStmts) { 4190946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner // Null statement, not a label! 4200946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner if (S == 0) return false; 4211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4220946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner // If this is a label, we have to emit the code, consider something like: 4230946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner // if (0) { ... foo: bar(); } goto foo; 424ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner // 425ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner // TODO: If anyone cared, we could track __label__'s, since we know that you 426ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner // can't jump to one from outside their declared region. 4270946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner if (isa<LabelStmt>(S)) 4280946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner return true; 429ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner 4300946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner // If this is a case/default statement, and we haven't seen a switch, we have 4310946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner // to emit the code. 4320946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner if (isa<SwitchCase>(S) && !IgnoreCaseStmts) 4330946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner return true; 4341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4350946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner // If this is a switch statement, we want to ignore cases below it. 4360946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner if (isa<SwitchStmt>(S)) 4370946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner IgnoreCaseStmts = true; 4381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4390946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner // Scan subexpressions for verboten labels. 4407502c1d3ce8bb97bcc4f7bebef507040bd93b26fJohn McCall for (Stmt::const_child_range I = S->children(); I; ++I) 4410946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner if (ContainsLabel(*I, IgnoreCaseStmts)) 4420946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner return true; 4431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4440946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner return false; 4450946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner} 4460946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner 447ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner/// containsBreak - Return true if the statement contains a break out of it. 448ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner/// If the statement (recursively) contains a switch or loop with a break 449ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner/// inside of it, this is fine. 450ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattnerbool CodeGenFunction::containsBreak(const Stmt *S) { 451ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner // Null statement, not a label! 452ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner if (S == 0) return false; 453ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner 454ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner // If this is a switch or loop that defines its own break scope, then we can 455ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner // include it and anything inside of it. 456ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || isa<DoStmt>(S) || 457ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner isa<ForStmt>(S)) 4582bef7f5499541e3b68f114cc4d7d197e9a902fe7Chris Lattner return false; 4592bef7f5499541e3b68f114cc4d7d197e9a902fe7Chris Lattner 4602bef7f5499541e3b68f114cc4d7d197e9a902fe7Chris Lattner if (isa<BreakStmt>(S)) 461ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner return true; 462ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner 463ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner // Scan subexpressions for verboten breaks. 464ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner for (Stmt::const_child_range I = S->children(); I; ++I) 465ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner if (containsBreak(*I)) 466ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner return true; 467ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner 468ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner return false; 469ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner} 470ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner 47131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner 472c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner/// ConstantFoldsToSimpleInteger - If the specified expression does not fold 473c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner/// to a constant, or if it does but contains a label, return false. If it 474c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner/// constant folds return true and set the boolean result in Result. 475c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattnerbool CodeGenFunction::ConstantFoldsToSimpleInteger(const Expr *Cond, 476c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner bool &ResultBool) { 477ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner llvm::APInt ResultInt; 478ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner if (!ConstantFoldsToSimpleInteger(Cond, ResultInt)) 479ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner return false; 480ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner 481ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner ResultBool = ResultInt.getBoolValue(); 482ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner return true; 483ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner} 484ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner 485ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner/// ConstantFoldsToSimpleInteger - If the specified expression does not fold 486ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner/// to a constant, or if it does but contains a label, return false. If it 487ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner/// constant folds return true and set the folded value. 488ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattnerbool CodeGenFunction:: 489ef425a69006afaa87751ee41ccf8ff405d9ede70Chris LattnerConstantFoldsToSimpleInteger(const Expr *Cond, llvm::APInt &ResultInt) { 49036bc14c3a1cf63ee306df5687ac8e85f924f8639Daniel Dunbar // FIXME: Rename and handle conversion of other evaluatable things 49136bc14c3a1cf63ee306df5687ac8e85f924f8639Daniel Dunbar // to bool. 49264712f196bffd41fb0552c2643b07a25c3e45082Anders Carlsson Expr::EvalResult Result; 4931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (!Cond->Evaluate(Result, getContext()) || !Result.Val.isInt() || 49464712f196bffd41fb0552c2643b07a25c3e45082Anders Carlsson Result.HasSideEffects) 495c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner return false; // Not foldable, not integer or not fully evaluatable. 496ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner 49731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner if (CodeGenFunction::ContainsLabel(Cond)) 498c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner return false; // Contains a label. 499ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner 500ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner ResultInt = Result.Val.getInt(); 501c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner return true; 50231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner} 50331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner 50431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner 505ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner 50631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an if 50731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// statement) to the specified blocks. Based on the condition, this might try 50831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// to simplify the codegen of the conditional based on the branch. 50931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner/// 51031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattnervoid CodeGenFunction::EmitBranchOnBoolExpr(const Expr *Cond, 51131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner llvm::BasicBlock *TrueBlock, 51231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner llvm::BasicBlock *FalseBlock) { 513f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne Cond = Cond->IgnoreParens(); 5141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 51531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner if (const BinaryOperator *CondBOp = dyn_cast<BinaryOperator>(Cond)) { 51631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // Handle X && Y in a condition. 5172de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall if (CondBOp->getOpcode() == BO_LAnd) { 51831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // If we have "1 && X", simplify the code. "0 && X" would have constant 51931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // folded if the case was simple enough. 520e3eb83b93751544a5fab19b3824f56aeac454f82Bill Wendling bool ConstantBool = false; 521c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner if (ConstantFoldsToSimpleInteger(CondBOp->getLHS(), ConstantBool) && 522c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner ConstantBool) { 52331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // br(1 && X) -> br(X). 52431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); 52531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner } 5261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 52731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // If we have "X && 1", simplify the code to use an uncond branch. 52831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // "X && 0" would have been constant folded to 0. 529c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner if (ConstantFoldsToSimpleInteger(CondBOp->getRHS(), ConstantBool) && 530c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner ConstantBool) { 53131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // br(X && 1) -> br(X). 53231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock); 53331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner } 5341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 53531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // Emit the LHS as a conditional. If the LHS conditional is false, we 53631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // want to jump to the FalseBlock. 5379615ecb44f549ae9fa2b4db6ff46bc78befbf62cDaniel Dunbar llvm::BasicBlock *LHSTrue = createBasicBlock("land.lhs.true"); 538150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall 539150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall ConditionalEvaluation eval(*this); 54031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner EmitBranchOnBoolExpr(CondBOp->getLHS(), LHSTrue, FalseBlock); 54131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner EmitBlock(LHSTrue); 5421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 54308e9e453f40aff95a59bd67db49b8f050765e1f0Anders Carlsson // Any temporaries created here are conditional. 544150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall eval.begin(*this); 54531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); 546150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall eval.end(*this); 54708e9e453f40aff95a59bd67db49b8f050765e1f0Anders Carlsson 54831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner return; 549c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner } 550c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner 551c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner if (CondBOp->getOpcode() == BO_LOr) { 55231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // If we have "0 || X", simplify the code. "1 || X" would have constant 55331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // folded if the case was simple enough. 554e3eb83b93751544a5fab19b3824f56aeac454f82Bill Wendling bool ConstantBool = false; 555c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner if (ConstantFoldsToSimpleInteger(CondBOp->getLHS(), ConstantBool) && 556c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner !ConstantBool) { 55731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // br(0 || X) -> br(X). 55831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); 55931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner } 5601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 56131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // If we have "X || 0", simplify the code to use an uncond branch. 56231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // "X || 1" would have been constant folded to 1. 563c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner if (ConstantFoldsToSimpleInteger(CondBOp->getRHS(), ConstantBool) && 564c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner !ConstantBool) { 56531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // br(X || 0) -> br(X). 56631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock); 56731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner } 5681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 56931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // Emit the LHS as a conditional. If the LHS conditional is true, we 57031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // want to jump to the TrueBlock. 5719615ecb44f549ae9fa2b4db6ff46bc78befbf62cDaniel Dunbar llvm::BasicBlock *LHSFalse = createBasicBlock("lor.lhs.false"); 572150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall 573150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall ConditionalEvaluation eval(*this); 57431a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, LHSFalse); 57531a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner EmitBlock(LHSFalse); 5761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 57708e9e453f40aff95a59bd67db49b8f050765e1f0Anders Carlsson // Any temporaries created here are conditional. 578150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall eval.begin(*this); 57931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); 580150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall eval.end(*this); 58108e9e453f40aff95a59bd67db49b8f050765e1f0Anders Carlsson 58231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner return; 58331a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner } 584552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner } 5851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 586552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner if (const UnaryOperator *CondUOp = dyn_cast<UnaryOperator>(Cond)) { 587552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner // br(!x, t, f) -> br(x, f, t) 5882de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall if (CondUOp->getOpcode() == UO_LNot) 589552f4c45ba4f4a01f86d585edabd871a27829867Chris Lattner return EmitBranchOnBoolExpr(CondUOp->getSubExpr(), FalseBlock, TrueBlock); 59031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner } 5911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 59209b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar if (const ConditionalOperator *CondOp = dyn_cast<ConditionalOperator>(Cond)) { 59309b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar // Handle ?: operator. 59409b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar 59509b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar // Just ignore GNU ?: extension. 59609b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar if (CondOp->getLHS()) { 59709b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar // br(c ? x : y, t, f) -> br(c, br(x, t, f), br(y, t, f)) 59809b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true"); 59909b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false"); 600150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall 601150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall ConditionalEvaluation cond(*this); 60209b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar EmitBranchOnBoolExpr(CondOp->getCond(), LHSBlock, RHSBlock); 603150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall 604150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall cond.begin(*this); 60509b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar EmitBlock(LHSBlock); 60609b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar EmitBranchOnBoolExpr(CondOp->getLHS(), TrueBlock, FalseBlock); 607150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall cond.end(*this); 608150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall 609150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall cond.begin(*this); 61009b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar EmitBlock(RHSBlock); 61109b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar EmitBranchOnBoolExpr(CondOp->getRHS(), TrueBlock, FalseBlock); 612150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall cond.end(*this); 613150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall 61409b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar return; 61509b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar } 61609b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar } 61709b14899039d828094c06ac25d60de62608e57b7Daniel Dunbar 61831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner // Emit the code with the fully general case. 61931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner llvm::Value *CondV = EvaluateExprAsBool(Cond); 62031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner Builder.CreateCondBr(CondV, TrueBlock, FalseBlock); 62131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner} 62231a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner 623488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar/// ErrorUnsupported - Print out an error that codegen doesn't support the 624dc5e8268292046114ffe02e48773572a91a310f1Chris Lattner/// specified stmt yet. 62590df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbarvoid CodeGenFunction::ErrorUnsupported(const Stmt *S, const char *Type, 62690df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar bool OmitOnError) { 62790df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar CGM.ErrorUnsupported(S, Type, OmitOnError); 628dc5e8268292046114ffe02e48773572a91a310f1Chris Lattner} 629dc5e8268292046114ffe02e48773572a91a310f1Chris Lattner 6307143325db76d6c3dabce82500f8cc7c93a941970John McCall/// emitNonZeroVLAInit - Emit the "zero" initialization of a 6317143325db76d6c3dabce82500f8cc7c93a941970John McCall/// variable-length array whose elements have a non-zero bit-pattern. 6327143325db76d6c3dabce82500f8cc7c93a941970John McCall/// 6337143325db76d6c3dabce82500f8cc7c93a941970John McCall/// \param src - a char* pointing to the bit-pattern for a single 6347143325db76d6c3dabce82500f8cc7c93a941970John McCall/// base element of the array 6357143325db76d6c3dabce82500f8cc7c93a941970John McCall/// \param sizeInChars - the total size of the VLA, in chars 6367143325db76d6c3dabce82500f8cc7c93a941970John McCall/// \param align - the total alignment of the VLA 6377143325db76d6c3dabce82500f8cc7c93a941970John McCallstatic void emitNonZeroVLAInit(CodeGenFunction &CGF, QualType baseType, 6387143325db76d6c3dabce82500f8cc7c93a941970John McCall llvm::Value *dest, llvm::Value *src, 6397143325db76d6c3dabce82500f8cc7c93a941970John McCall llvm::Value *sizeInChars) { 6407143325db76d6c3dabce82500f8cc7c93a941970John McCall std::pair<CharUnits,CharUnits> baseSizeAndAlign 6417143325db76d6c3dabce82500f8cc7c93a941970John McCall = CGF.getContext().getTypeInfoInChars(baseType); 6427143325db76d6c3dabce82500f8cc7c93a941970John McCall 6437143325db76d6c3dabce82500f8cc7c93a941970John McCall CGBuilderTy &Builder = CGF.Builder; 6447143325db76d6c3dabce82500f8cc7c93a941970John McCall 6457143325db76d6c3dabce82500f8cc7c93a941970John McCall llvm::Value *baseSizeInChars 6467143325db76d6c3dabce82500f8cc7c93a941970John McCall = llvm::ConstantInt::get(CGF.IntPtrTy, baseSizeAndAlign.first.getQuantity()); 6477143325db76d6c3dabce82500f8cc7c93a941970John McCall 6482acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *i8p = Builder.getInt8PtrTy(); 6497143325db76d6c3dabce82500f8cc7c93a941970John McCall 6507143325db76d6c3dabce82500f8cc7c93a941970John McCall llvm::Value *begin = Builder.CreateBitCast(dest, i8p, "vla.begin"); 6517143325db76d6c3dabce82500f8cc7c93a941970John McCall llvm::Value *end = Builder.CreateInBoundsGEP(dest, sizeInChars, "vla.end"); 6527143325db76d6c3dabce82500f8cc7c93a941970John McCall 6537143325db76d6c3dabce82500f8cc7c93a941970John McCall llvm::BasicBlock *originBB = CGF.Builder.GetInsertBlock(); 6547143325db76d6c3dabce82500f8cc7c93a941970John McCall llvm::BasicBlock *loopBB = CGF.createBasicBlock("vla-init.loop"); 6557143325db76d6c3dabce82500f8cc7c93a941970John McCall llvm::BasicBlock *contBB = CGF.createBasicBlock("vla-init.cont"); 6567143325db76d6c3dabce82500f8cc7c93a941970John McCall 6577143325db76d6c3dabce82500f8cc7c93a941970John McCall // Make a loop over the VLA. C99 guarantees that the VLA element 6587143325db76d6c3dabce82500f8cc7c93a941970John McCall // count must be nonzero. 6597143325db76d6c3dabce82500f8cc7c93a941970John McCall CGF.EmitBlock(loopBB); 6607143325db76d6c3dabce82500f8cc7c93a941970John McCall 661bbf3bacb3e0c1ebb3e8a4a8b1330404a7e379315Jay Foad llvm::PHINode *cur = Builder.CreatePHI(i8p, 2, "vla.cur"); 6627143325db76d6c3dabce82500f8cc7c93a941970John McCall cur->addIncoming(begin, originBB); 6637143325db76d6c3dabce82500f8cc7c93a941970John McCall 6647143325db76d6c3dabce82500f8cc7c93a941970John McCall // memcpy the individual element bit-pattern. 6657143325db76d6c3dabce82500f8cc7c93a941970John McCall Builder.CreateMemCpy(cur, src, baseSizeInChars, 6667143325db76d6c3dabce82500f8cc7c93a941970John McCall baseSizeAndAlign.second.getQuantity(), 6677143325db76d6c3dabce82500f8cc7c93a941970John McCall /*volatile*/ false); 6687143325db76d6c3dabce82500f8cc7c93a941970John McCall 6697143325db76d6c3dabce82500f8cc7c93a941970John McCall // Go to the next element. 6707143325db76d6c3dabce82500f8cc7c93a941970John McCall llvm::Value *next = Builder.CreateConstInBoundsGEP1_32(cur, 1, "vla.next"); 6717143325db76d6c3dabce82500f8cc7c93a941970John McCall 6727143325db76d6c3dabce82500f8cc7c93a941970John McCall // Leave if that's the end of the VLA. 6737143325db76d6c3dabce82500f8cc7c93a941970John McCall llvm::Value *done = Builder.CreateICmpEQ(next, end, "vla-init.isdone"); 6747143325db76d6c3dabce82500f8cc7c93a941970John McCall Builder.CreateCondBr(done, contBB, loopBB); 6757143325db76d6c3dabce82500f8cc7c93a941970John McCall cur->addIncoming(next, loopBB); 6767143325db76d6c3dabce82500f8cc7c93a941970John McCall 6777143325db76d6c3dabce82500f8cc7c93a941970John McCall CGF.EmitBlock(contBB); 6787143325db76d6c3dabce82500f8cc7c93a941970John McCall} 6797143325db76d6c3dabce82500f8cc7c93a941970John McCall 6801884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlssonvoid 6811884eb0b5c55edda4893ddec45e7dbad79758782Anders CarlssonCodeGenFunction::EmitNullInitialization(llvm::Value *DestPtr, QualType Ty) { 6820d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson // Ignore empty classes in C++. 6830d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson if (getContext().getLangOptions().CPlusPlus) { 6840d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson if (const RecordType *RT = Ty->getAs<RecordType>()) { 6850d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson if (cast<CXXRecordDecl>(RT->getDecl())->isEmpty()) 6860d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson return; 6870d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson } 6880d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson } 6899021718882441dd391a1960084580d3cd19c423aJohn McCall 6909021718882441dd391a1960084580d3cd19c423aJohn McCall // Cast the dest ptr to the appropriate i8 pointer type. 6919021718882441dd391a1960084580d3cd19c423aJohn McCall unsigned DestAS = 6929021718882441dd391a1960084580d3cd19c423aJohn McCall cast<llvm::PointerType>(DestPtr->getType())->getAddressSpace(); 6932acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *BP = Builder.getInt8PtrTy(DestAS); 6943d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson if (DestPtr->getType() != BP) 6953d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp"); 6963d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson 6973d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson // Get size and alignment info for this aggregate. 69879be76c0e360d9e9c44285c9838af02adc43e55fKen Dyck std::pair<CharUnits, CharUnits> TypeInfo = 69979be76c0e360d9e9c44285c9838af02adc43e55fKen Dyck getContext().getTypeInfoInChars(Ty); 70079be76c0e360d9e9c44285c9838af02adc43e55fKen Dyck CharUnits Size = TypeInfo.first; 70179be76c0e360d9e9c44285c9838af02adc43e55fKen Dyck CharUnits Align = TypeInfo.second; 7023d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson 7035576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall llvm::Value *SizeVal; 7047143325db76d6c3dabce82500f8cc7c93a941970John McCall const VariableArrayType *vla; 7059021718882441dd391a1960084580d3cd19c423aJohn McCall 7065576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall // Don't bother emitting a zero-byte memset. 70779be76c0e360d9e9c44285c9838af02adc43e55fKen Dyck if (Size.isZero()) { 7085576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall // But note that getTypeInfo returns 0 for a VLA. 7095576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall if (const VariableArrayType *vlaType = 7105576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall dyn_cast_or_null<VariableArrayType>( 7115576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall getContext().getAsArrayType(Ty))) { 712bc8d40d85f3fa1e34569834916f18fecaa635152John McCall QualType eltType; 713bc8d40d85f3fa1e34569834916f18fecaa635152John McCall llvm::Value *numElts; 714bc8d40d85f3fa1e34569834916f18fecaa635152John McCall llvm::tie(numElts, eltType) = getVLASize(vlaType); 715bc8d40d85f3fa1e34569834916f18fecaa635152John McCall 716bc8d40d85f3fa1e34569834916f18fecaa635152John McCall SizeVal = numElts; 717bc8d40d85f3fa1e34569834916f18fecaa635152John McCall CharUnits eltSize = getContext().getTypeSizeInChars(eltType); 718bc8d40d85f3fa1e34569834916f18fecaa635152John McCall if (!eltSize.isOne()) 719bc8d40d85f3fa1e34569834916f18fecaa635152John McCall SizeVal = Builder.CreateNUWMul(SizeVal, CGM.getSize(eltSize)); 7207143325db76d6c3dabce82500f8cc7c93a941970John McCall vla = vlaType; 7215576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall } else { 7225576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall return; 7235576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall } 7245576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall } else { 725bc8d40d85f3fa1e34569834916f18fecaa635152John McCall SizeVal = CGM.getSize(Size); 7267143325db76d6c3dabce82500f8cc7c93a941970John McCall vla = 0; 7275576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall } 7289021718882441dd391a1960084580d3cd19c423aJohn McCall 7299021718882441dd391a1960084580d3cd19c423aJohn McCall // If the type contains a pointer to data member we can't memset it to zero. 7309021718882441dd391a1960084580d3cd19c423aJohn McCall // Instead, create a null constant and copy it to the destination. 7317143325db76d6c3dabce82500f8cc7c93a941970John McCall // TODO: there are other patterns besides zero that we can usefully memset, 7327143325db76d6c3dabce82500f8cc7c93a941970John McCall // like -1, which happens to be the pattern used by member-pointers. 733f16aa103d3afd42fbca2ab346f191bf745cec092John McCall if (!CGM.getTypes().isZeroInitializable(Ty)) { 7347143325db76d6c3dabce82500f8cc7c93a941970John McCall // For a VLA, emit a single element, then splat that over the VLA. 7357143325db76d6c3dabce82500f8cc7c93a941970John McCall if (vla) Ty = getContext().getBaseElementType(vla); 7365576d9b0a389f6f1f89bdcd37194f4e992b1fbcbJohn McCall 7379021718882441dd391a1960084580d3cd19c423aJohn McCall llvm::Constant *NullConstant = CGM.EmitNullConstant(Ty); 7389021718882441dd391a1960084580d3cd19c423aJohn McCall 7399021718882441dd391a1960084580d3cd19c423aJohn McCall llvm::GlobalVariable *NullVariable = 7409021718882441dd391a1960084580d3cd19c423aJohn McCall new llvm::GlobalVariable(CGM.getModule(), NullConstant->getType(), 7419021718882441dd391a1960084580d3cd19c423aJohn McCall /*isConstant=*/true, 7429021718882441dd391a1960084580d3cd19c423aJohn McCall llvm::GlobalVariable::PrivateLinkage, 7435f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner NullConstant, Twine()); 7449021718882441dd391a1960084580d3cd19c423aJohn McCall llvm::Value *SrcPtr = 7459021718882441dd391a1960084580d3cd19c423aJohn McCall Builder.CreateBitCast(NullVariable, Builder.getInt8PtrTy()); 7469021718882441dd391a1960084580d3cd19c423aJohn McCall 7477143325db76d6c3dabce82500f8cc7c93a941970John McCall if (vla) return emitNonZeroVLAInit(*this, Ty, DestPtr, SrcPtr, SizeVal); 7487143325db76d6c3dabce82500f8cc7c93a941970John McCall 7499021718882441dd391a1960084580d3cd19c423aJohn McCall // Get and call the appropriate llvm.memcpy overload. 75079be76c0e360d9e9c44285c9838af02adc43e55fKen Dyck Builder.CreateMemCpy(DestPtr, SrcPtr, SizeVal, Align.getQuantity(), false); 75188207c9ca218486b93ae7df14e9764cd0c2c3383Chris Lattner return; 7529021718882441dd391a1960084580d3cd19c423aJohn McCall } 7539021718882441dd391a1960084580d3cd19c423aJohn McCall 7549021718882441dd391a1960084580d3cd19c423aJohn McCall // Otherwise, just memset the whole thing to zero. This is legal 7559021718882441dd391a1960084580d3cd19c423aJohn McCall // because in LLVM, all default initializers (other than the ones we just 7569021718882441dd391a1960084580d3cd19c423aJohn McCall // handled above) are guaranteed to have a bit pattern of all zeros. 75779be76c0e360d9e9c44285c9838af02adc43e55fKen Dyck Builder.CreateMemSet(DestPtr, Builder.getInt8(0), SizeVal, 75879be76c0e360d9e9c44285c9838af02adc43e55fKen Dyck Align.getQuantity(), false); 7593d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson} 7603d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson 761ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattnerllvm::BlockAddress *CodeGenFunction::GetAddrOfLabel(const LabelDecl *L) { 762d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner // Make sure that there is a block for the indirect goto. 763d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner if (IndirectBranch == 0) 764d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner GetIndirectGotoBlock(); 7653d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner 766ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall llvm::BasicBlock *BB = getJumpDestForLabel(L).getBlock(); 7673d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner 768d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner // Make sure the indirect branch includes all of the address-taken blocks. 769d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner IndirectBranch->addDestination(BB); 770d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner return llvm::BlockAddress::get(CurFn, BB); 7713d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner} 7721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 7733d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattnerllvm::BasicBlock *CodeGenFunction::GetIndirectGotoBlock() { 774d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner // If we already made the indirect branch for indirect goto, return its block. 775d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner if (IndirectBranch) return IndirectBranch->getParent(); 7763d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner 777d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner CGBuilderTy TmpBuilder(createBasicBlock("indirectgoto")); 7783d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner 7793d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner // Create the PHI node that indirect gotos will add entries to. 780bbf3bacb3e0c1ebb3e8a4a8b1330404a7e379315Jay Foad llvm::Value *DestVal = TmpBuilder.CreatePHI(Int8PtrTy, 0, 781bbf3bacb3e0c1ebb3e8a4a8b1330404a7e379315Jay Foad "indirect.goto.dest"); 7823d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner 783d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner // Create the indirect branch instruction. 784d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner IndirectBranch = TmpBuilder.CreateIndirectBr(DestVal); 785d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner return IndirectBranch->getParent(); 7860ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar} 787ddf7cac45d85b73127adbbd91a2b28fc7291c57eAnders Carlsson 788bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall/// Computes the length of an array in elements, as well as the base 789bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall/// element type and a properly-typed first element pointer. 790bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCallllvm::Value *CodeGenFunction::emitArrayLength(const ArrayType *origArrayType, 791bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall QualType &baseType, 792bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall llvm::Value *&addr) { 793bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall const ArrayType *arrayType = origArrayType; 794bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall 795bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall // If it's a VLA, we have to load the stored size. Note that 796bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall // this is the size of the VLA in bytes, not its size in elements. 797bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall llvm::Value *numVLAElements = 0; 798bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall if (isa<VariableArrayType>(arrayType)) { 799bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall numVLAElements = getVLASize(cast<VariableArrayType>(arrayType)).first; 800bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall 801bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall // Walk into all VLAs. This doesn't require changes to addr, 802bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall // which has type T* where T is the first non-VLA element type. 803bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall do { 804bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall QualType elementType = arrayType->getElementType(); 805bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall arrayType = getContext().getAsArrayType(elementType); 806bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall 807bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall // If we only have VLA components, 'addr' requires no adjustment. 808bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall if (!arrayType) { 809bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall baseType = elementType; 810bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall return numVLAElements; 811bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall } 812bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall } while (isa<VariableArrayType>(arrayType)); 813bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall 814bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall // We get out here only if we find a constant array type 815bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall // inside the VLA. 816bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall } 817bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall 818bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall // We have some number of constant-length arrays, so addr should 819bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall // have LLVM type [M x [N x [...]]]*. Build a GEP that walks 820bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall // down to the first element of addr. 8215f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVector<llvm::Value*, 8> gepIndices; 822bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall 823bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall // GEP down to the array type. 824bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall llvm::ConstantInt *zero = Builder.getInt32(0); 825bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall gepIndices.push_back(zero); 826bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall 827bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall // It's more efficient to calculate the count from the LLVM 828bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall // constant-length arrays than to re-evaluate the array bounds. 829bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall uint64_t countFromCLAs = 1; 830bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall 8312acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::ArrayType *llvmArrayType = 832bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall cast<llvm::ArrayType>( 833bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall cast<llvm::PointerType>(addr->getType())->getElementType()); 834bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall while (true) { 835bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall assert(isa<ConstantArrayType>(arrayType)); 836bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall assert(cast<ConstantArrayType>(arrayType)->getSize().getZExtValue() 837bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall == llvmArrayType->getNumElements()); 838bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall 839bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall gepIndices.push_back(zero); 840bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall countFromCLAs *= llvmArrayType->getNumElements(); 841bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall 842bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall llvmArrayType = 843bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall dyn_cast<llvm::ArrayType>(llvmArrayType->getElementType()); 844bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall if (!llvmArrayType) break; 845bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall 846bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall arrayType = getContext().getAsArrayType(arrayType->getElementType()); 847bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall assert(arrayType && "LLVM and Clang types are out-of-synch"); 848bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall } 849bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall 850bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall baseType = arrayType->getElementType(); 851bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall 852bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall // Create the actual GEP. 8530f6ac7cf7bc6a02c1a5c19d2c90ec0d1dd7786e7Jay Foad addr = Builder.CreateInBoundsGEP(addr, gepIndices, "array.begin"); 854bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall 855bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall llvm::Value *numElements 856bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall = llvm::ConstantInt::get(SizeTy, countFromCLAs); 857bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall 858bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall // If we had any VLA dimensions, factor them in. 859bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall if (numVLAElements) 860bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall numElements = Builder.CreateNUWMul(numVLAElements, numElements); 861bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall 862bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall return numElements; 863bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall} 864bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall 865bc8d40d85f3fa1e34569834916f18fecaa635152John McCallstd::pair<llvm::Value*, QualType> 866bc8d40d85f3fa1e34569834916f18fecaa635152John McCallCodeGenFunction::getVLASize(QualType type) { 867bc8d40d85f3fa1e34569834916f18fecaa635152John McCall const VariableArrayType *vla = getContext().getAsVariableArrayType(type); 868bc8d40d85f3fa1e34569834916f18fecaa635152John McCall assert(vla && "type was not a variable array type!"); 869bc8d40d85f3fa1e34569834916f18fecaa635152John McCall return getVLASize(vla); 870f666b7780d04186521adcaedb0e15dfa4d5e6933Anders Carlsson} 871dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlsson 872bc8d40d85f3fa1e34569834916f18fecaa635152John McCallstd::pair<llvm::Value*, QualType> 873bc8d40d85f3fa1e34569834916f18fecaa635152John McCallCodeGenFunction::getVLASize(const VariableArrayType *type) { 874bc8d40d85f3fa1e34569834916f18fecaa635152John McCall // The number of elements so far; always size_t. 875bc8d40d85f3fa1e34569834916f18fecaa635152John McCall llvm::Value *numElements = 0; 8761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 877bc8d40d85f3fa1e34569834916f18fecaa635152John McCall QualType elementType; 878bc8d40d85f3fa1e34569834916f18fecaa635152John McCall do { 879bc8d40d85f3fa1e34569834916f18fecaa635152John McCall elementType = type->getElementType(); 880bc8d40d85f3fa1e34569834916f18fecaa635152John McCall llvm::Value *vlaSize = VLASizeMap[type->getSizeExpr()]; 881bc8d40d85f3fa1e34569834916f18fecaa635152John McCall assert(vlaSize && "no size for VLA!"); 882bc8d40d85f3fa1e34569834916f18fecaa635152John McCall assert(vlaSize->getType() == SizeTy); 8831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 884bc8d40d85f3fa1e34569834916f18fecaa635152John McCall if (!numElements) { 885bc8d40d85f3fa1e34569834916f18fecaa635152John McCall numElements = vlaSize; 886bc8d40d85f3fa1e34569834916f18fecaa635152John McCall } else { 887bc8d40d85f3fa1e34569834916f18fecaa635152John McCall // It's undefined behavior if this wraps around, so mark it that way. 888bc8d40d85f3fa1e34569834916f18fecaa635152John McCall numElements = Builder.CreateNUWMul(numElements, vlaSize); 889bc8d40d85f3fa1e34569834916f18fecaa635152John McCall } 890bc8d40d85f3fa1e34569834916f18fecaa635152John McCall } while ((type = getContext().getAsVariableArrayType(elementType))); 8911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 892bc8d40d85f3fa1e34569834916f18fecaa635152John McCall return std::pair<llvm::Value*,QualType>(numElements, elementType); 893bc8d40d85f3fa1e34569834916f18fecaa635152John McCall} 8941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 895bc8d40d85f3fa1e34569834916f18fecaa635152John McCallvoid CodeGenFunction::EmitVariablyModifiedType(QualType type) { 896bc8d40d85f3fa1e34569834916f18fecaa635152John McCall assert(type->isVariablyModifiedType() && 897bc8d40d85f3fa1e34569834916f18fecaa635152John McCall "Must pass variably modified type to EmitVLASizes!"); 8981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 899bc8d40d85f3fa1e34569834916f18fecaa635152John McCall EnsureInsertPoint(); 9001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 901bc8d40d85f3fa1e34569834916f18fecaa635152John McCall // We're going to walk down into the type and look for VLA 902bc8d40d85f3fa1e34569834916f18fecaa635152John McCall // expressions. 903bc8d40d85f3fa1e34569834916f18fecaa635152John McCall type = type.getCanonicalType(); 904bc8d40d85f3fa1e34569834916f18fecaa635152John McCall do { 905bc8d40d85f3fa1e34569834916f18fecaa635152John McCall assert(type->isVariablyModifiedType()); 906bc8d40d85f3fa1e34569834916f18fecaa635152John McCall 907bc8d40d85f3fa1e34569834916f18fecaa635152John McCall const Type *ty = type.getTypePtr(); 908bc8d40d85f3fa1e34569834916f18fecaa635152John McCall switch (ty->getTypeClass()) { 909bc8d40d85f3fa1e34569834916f18fecaa635152John McCall#define TYPE(Class, Base) 910bc8d40d85f3fa1e34569834916f18fecaa635152John McCall#define ABSTRACT_TYPE(Class, Base) 911bc8d40d85f3fa1e34569834916f18fecaa635152John McCall#define NON_CANONICAL_TYPE(Class, Base) case Type::Class: 912bc8d40d85f3fa1e34569834916f18fecaa635152John McCall#define DEPENDENT_TYPE(Class, Base) case Type::Class: 913bc8d40d85f3fa1e34569834916f18fecaa635152John McCall#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: 914bc8d40d85f3fa1e34569834916f18fecaa635152John McCall#include "clang/AST/TypeNodes.def" 915bc8d40d85f3fa1e34569834916f18fecaa635152John McCall llvm_unreachable("unexpected dependent or non-canonical type!"); 916bc8d40d85f3fa1e34569834916f18fecaa635152John McCall 917bc8d40d85f3fa1e34569834916f18fecaa635152John McCall // These types are never variably-modified. 918bc8d40d85f3fa1e34569834916f18fecaa635152John McCall case Type::Builtin: 919bc8d40d85f3fa1e34569834916f18fecaa635152John McCall case Type::Complex: 920bc8d40d85f3fa1e34569834916f18fecaa635152John McCall case Type::Vector: 921bc8d40d85f3fa1e34569834916f18fecaa635152John McCall case Type::ExtVector: 922bc8d40d85f3fa1e34569834916f18fecaa635152John McCall case Type::Record: 923bc8d40d85f3fa1e34569834916f18fecaa635152John McCall case Type::Enum: 924bc8d40d85f3fa1e34569834916f18fecaa635152John McCall case Type::ObjCObject: 925bc8d40d85f3fa1e34569834916f18fecaa635152John McCall case Type::ObjCInterface: 926bc8d40d85f3fa1e34569834916f18fecaa635152John McCall case Type::ObjCObjectPointer: 927bc8d40d85f3fa1e34569834916f18fecaa635152John McCall llvm_unreachable("type class is never variably-modified!"); 928bc8d40d85f3fa1e34569834916f18fecaa635152John McCall 929bc8d40d85f3fa1e34569834916f18fecaa635152John McCall case Type::Pointer: 930bc8d40d85f3fa1e34569834916f18fecaa635152John McCall type = cast<PointerType>(ty)->getPointeeType(); 931bc8d40d85f3fa1e34569834916f18fecaa635152John McCall break; 932bc8d40d85f3fa1e34569834916f18fecaa635152John McCall 933bc8d40d85f3fa1e34569834916f18fecaa635152John McCall case Type::BlockPointer: 934bc8d40d85f3fa1e34569834916f18fecaa635152John McCall type = cast<BlockPointerType>(ty)->getPointeeType(); 935bc8d40d85f3fa1e34569834916f18fecaa635152John McCall break; 936bc8d40d85f3fa1e34569834916f18fecaa635152John McCall 937bc8d40d85f3fa1e34569834916f18fecaa635152John McCall case Type::LValueReference: 938bc8d40d85f3fa1e34569834916f18fecaa635152John McCall case Type::RValueReference: 939bc8d40d85f3fa1e34569834916f18fecaa635152John McCall type = cast<ReferenceType>(ty)->getPointeeType(); 940bc8d40d85f3fa1e34569834916f18fecaa635152John McCall break; 941bc8d40d85f3fa1e34569834916f18fecaa635152John McCall 942bc8d40d85f3fa1e34569834916f18fecaa635152John McCall case Type::MemberPointer: 943bc8d40d85f3fa1e34569834916f18fecaa635152John McCall type = cast<MemberPointerType>(ty)->getPointeeType(); 944bc8d40d85f3fa1e34569834916f18fecaa635152John McCall break; 945bc8d40d85f3fa1e34569834916f18fecaa635152John McCall 946bc8d40d85f3fa1e34569834916f18fecaa635152John McCall case Type::ConstantArray: 947bc8d40d85f3fa1e34569834916f18fecaa635152John McCall case Type::IncompleteArray: 948bc8d40d85f3fa1e34569834916f18fecaa635152John McCall // Losing element qualification here is fine. 949bc8d40d85f3fa1e34569834916f18fecaa635152John McCall type = cast<ArrayType>(ty)->getElementType(); 950bc8d40d85f3fa1e34569834916f18fecaa635152John McCall break; 951bc8d40d85f3fa1e34569834916f18fecaa635152John McCall 952bc8d40d85f3fa1e34569834916f18fecaa635152John McCall case Type::VariableArray: { 953bc8d40d85f3fa1e34569834916f18fecaa635152John McCall // Losing element qualification here is fine. 954bc8d40d85f3fa1e34569834916f18fecaa635152John McCall const VariableArrayType *vat = cast<VariableArrayType>(ty); 955bc8d40d85f3fa1e34569834916f18fecaa635152John McCall 956bc8d40d85f3fa1e34569834916f18fecaa635152John McCall // Unknown size indication requires no size computation. 957bc8d40d85f3fa1e34569834916f18fecaa635152John McCall // Otherwise, evaluate and record it. 958bc8d40d85f3fa1e34569834916f18fecaa635152John McCall if (const Expr *size = vat->getSizeExpr()) { 959bc8d40d85f3fa1e34569834916f18fecaa635152John McCall // It's possible that we might have emitted this already, 960bc8d40d85f3fa1e34569834916f18fecaa635152John McCall // e.g. with a typedef and a pointer to it. 961bc8d40d85f3fa1e34569834916f18fecaa635152John McCall llvm::Value *&entry = VLASizeMap[size]; 962bc8d40d85f3fa1e34569834916f18fecaa635152John McCall if (!entry) { 963bc8d40d85f3fa1e34569834916f18fecaa635152John McCall // Always zexting here would be wrong if it weren't 964bc8d40d85f3fa1e34569834916f18fecaa635152John McCall // undefined behavior to have a negative bound. 965bc8d40d85f3fa1e34569834916f18fecaa635152John McCall entry = Builder.CreateIntCast(EmitScalarExpr(size), SizeTy, 966bc8d40d85f3fa1e34569834916f18fecaa635152John McCall /*signed*/ false); 967bc8d40d85f3fa1e34569834916f18fecaa635152John McCall } 968bc8d40d85f3fa1e34569834916f18fecaa635152John McCall } 969bc8d40d85f3fa1e34569834916f18fecaa635152John McCall type = vat->getElementType(); 970bc8d40d85f3fa1e34569834916f18fecaa635152John McCall break; 971fcdbb93749ed69aa9022437052c390522355ec3dAnders Carlsson } 9721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 973bc8d40d85f3fa1e34569834916f18fecaa635152John McCall case Type::FunctionProto: 974bc8d40d85f3fa1e34569834916f18fecaa635152John McCall case Type::FunctionNoProto: 975bc8d40d85f3fa1e34569834916f18fecaa635152John McCall type = cast<FunctionType>(ty)->getResultType(); 976bc8d40d85f3fa1e34569834916f18fecaa635152John McCall break; 977bc8d40d85f3fa1e34569834916f18fecaa635152John McCall } 978bc8d40d85f3fa1e34569834916f18fecaa635152John McCall } while (type->isVariablyModifiedType()); 979dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlsson} 9804fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman 9814fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedmanllvm::Value* CodeGenFunction::EmitVAListRef(const Expr* E) { 982bc07a55b68342a230aafd8875cc7a26450dd3f64Dan Gohman if (getContext().getBuiltinVaListType()->isArrayType()) 9834fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman return EmitScalarExpr(E); 9844fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman return EmitLValue(E).getAddress(); 9854fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman} 9866ccc47698d0311ddabf32fa0f6db8e4f09ac96f8Anders Carlsson 9878d308384a220c7dc81755c47cdcbdd87dac25d5bDevang Patelvoid CodeGenFunction::EmitDeclRefExprDbgValue(const DeclRefExpr *E, 988189d6ef40eff11b83b2cda941d5ed89a5cef09b2John McCall llvm::Constant *Init) { 98925c2c8fb9309050612009a6571e2660e75531348Devang Patel assert (Init && "Invalid DeclRefExpr initializer!"); 99025c2c8fb9309050612009a6571e2660e75531348Devang Patel if (CGDebugInfo *Dbg = getDebugInfo()) 991d2829b76a87a70791184b3cac89900d2cda46ce4Devang Patel Dbg->EmitGlobalVariable(E->getDecl(), Init); 9928d308384a220c7dc81755c47cdcbdd87dac25d5bDevang Patel} 99356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall 99456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCallCodeGenFunction::PeepholeProtection 99556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCallCodeGenFunction::protectFromPeepholes(RValue rvalue) { 99656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall // At the moment, the only aggressive peephole we do in IR gen 99756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall // is trunc(zext) folding, but if we add more, we can easily 99856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall // extend this protection. 99956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall 100056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall if (!rvalue.isScalar()) return PeepholeProtection(); 100156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall llvm::Value *value = rvalue.getScalarVal(); 100256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall if (!isa<llvm::ZExtInst>(value)) return PeepholeProtection(); 100356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall 100456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall // Just make an extra bitcast. 100556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall assert(HaveInsertPoint()); 100656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall llvm::Instruction *inst = new llvm::BitCastInst(value, value->getType(), "", 100756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall Builder.GetInsertBlock()); 100856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall 100956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall PeepholeProtection protection; 101056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall protection.Inst = inst; 101156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall return protection; 101256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall} 101356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall 101456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCallvoid CodeGenFunction::unprotectFromPeepholes(PeepholeProtection protection) { 101556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall if (!protection.Inst) return; 101656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall 101756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall // In theory, we could try to duplicate the peepholes now, but whatever. 101856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall protection.Inst->eraseFromParent(); 101956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall} 1020