1f22ef01cSRoman Divacky //===--- CodeGenFunction.cpp - Emit LLVM Code from ASTs for a Function ----===// 2f22ef01cSRoman Divacky // 3f22ef01cSRoman Divacky // The LLVM Compiler Infrastructure 4f22ef01cSRoman Divacky // 5f22ef01cSRoman Divacky // This file is distributed under the University of Illinois Open Source 6f22ef01cSRoman Divacky // License. See LICENSE.TXT for details. 7f22ef01cSRoman Divacky // 8f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 9f22ef01cSRoman Divacky // 10f22ef01cSRoman Divacky // This coordinates the per-function state used while generating code. 11f22ef01cSRoman Divacky // 12f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 13f22ef01cSRoman Divacky 14f22ef01cSRoman Divacky #include "CodeGenFunction.h" 15f22ef01cSRoman Divacky #include "CodeGenModule.h" 166122f3e6SDimitry Andric #include "CGCUDARuntime.h" 17e580952dSDimitry Andric #include "CGCXXABI.h" 18f22ef01cSRoman Divacky #include "CGDebugInfo.h" 19f22ef01cSRoman Divacky #include "clang/Basic/TargetInfo.h" 20f22ef01cSRoman Divacky #include "clang/AST/ASTContext.h" 21f22ef01cSRoman Divacky #include "clang/AST/Decl.h" 22f22ef01cSRoman Divacky #include "clang/AST/DeclCXX.h" 23f22ef01cSRoman Divacky #include "clang/AST/StmtCXX.h" 24ffd1746dSEd Schouten #include "clang/Frontend/CodeGenOptions.h" 25f22ef01cSRoman Divacky #include "llvm/Target/TargetData.h" 26ffd1746dSEd Schouten #include "llvm/Intrinsics.h" 27f22ef01cSRoman Divacky using namespace clang; 28f22ef01cSRoman Divacky using namespace CodeGen; 29f22ef01cSRoman Divacky 30f22ef01cSRoman Divacky CodeGenFunction::CodeGenFunction(CodeGenModule &cgm) 312754fe60SDimitry Andric : CodeGenTypeCache(cgm), CGM(cgm), 32dff0c46cSDimitry Andric Target(CGM.getContext().getTargetInfo()), 33dff0c46cSDimitry Andric Builder(cgm.getModule().getContext()), 3417a519f9SDimitry Andric AutoreleaseResult(false), BlockInfo(0), BlockPointer(0), 35dff0c46cSDimitry Andric LambdaThisCaptureField(0), NormalCleanupDest(0), NextCleanupDestIndex(1), 36dff0c46cSDimitry Andric FirstBlockInfo(0), EHResumeBlock(0), ExceptionSlot(0), EHSelectorSlot(0), 37bd5abe19SDimitry Andric DebugInfo(0), DisableDebugInfo(false), DidCallStackSave(false), 38bd5abe19SDimitry Andric IndirectBranch(0), SwitchInsn(0), CaseRangeBlock(0), UnreachableBlock(0), 39dff0c46cSDimitry Andric CXXABIThisDecl(0), CXXABIThisValue(0), CXXThisValue(0), CXXVTTDecl(0), 40dff0c46cSDimitry Andric CXXVTTValue(0), OutermostConditional(0), TerminateLandingPad(0), 41dff0c46cSDimitry Andric TerminateHandler(0), TrapBB(0) { 42ffd1746dSEd Schouten 43dff0c46cSDimitry Andric CatchUndefined = getContext().getLangOpts().CatchUndefined; 44e580952dSDimitry Andric CGM.getCXXABI().getMangleContext().startNewFunction(); 45f22ef01cSRoman Divacky } 46f22ef01cSRoman Divacky 47dff0c46cSDimitry Andric CodeGenFunction::~CodeGenFunction() { 48dff0c46cSDimitry Andric // If there are any unclaimed block infos, go ahead and destroy them 49dff0c46cSDimitry Andric // now. This can happen if IR-gen gets clever and skips evaluating 50dff0c46cSDimitry Andric // something. 51dff0c46cSDimitry Andric if (FirstBlockInfo) 52dff0c46cSDimitry Andric destroyBlockInfos(FirstBlockInfo); 53dff0c46cSDimitry Andric } 54dff0c46cSDimitry Andric 55f22ef01cSRoman Divacky 5617a519f9SDimitry Andric llvm::Type *CodeGenFunction::ConvertTypeForMem(QualType T) { 57f22ef01cSRoman Divacky return CGM.getTypes().ConvertTypeForMem(T); 58f22ef01cSRoman Divacky } 59f22ef01cSRoman Divacky 6017a519f9SDimitry Andric llvm::Type *CodeGenFunction::ConvertType(QualType T) { 61f22ef01cSRoman Divacky return CGM.getTypes().ConvertType(T); 62f22ef01cSRoman Divacky } 63f22ef01cSRoman Divacky 64bd5abe19SDimitry Andric bool CodeGenFunction::hasAggregateLLVMType(QualType type) { 65bd5abe19SDimitry Andric switch (type.getCanonicalType()->getTypeClass()) { 66bd5abe19SDimitry Andric #define TYPE(name, parent) 67bd5abe19SDimitry Andric #define ABSTRACT_TYPE(name, parent) 68bd5abe19SDimitry Andric #define NON_CANONICAL_TYPE(name, parent) case Type::name: 69bd5abe19SDimitry Andric #define DEPENDENT_TYPE(name, parent) case Type::name: 70bd5abe19SDimitry Andric #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(name, parent) case Type::name: 71bd5abe19SDimitry Andric #include "clang/AST/TypeNodes.def" 72bd5abe19SDimitry Andric llvm_unreachable("non-canonical or dependent type in IR-generation"); 73bd5abe19SDimitry Andric 74bd5abe19SDimitry Andric case Type::Builtin: 75bd5abe19SDimitry Andric case Type::Pointer: 76bd5abe19SDimitry Andric case Type::BlockPointer: 77bd5abe19SDimitry Andric case Type::LValueReference: 78bd5abe19SDimitry Andric case Type::RValueReference: 79bd5abe19SDimitry Andric case Type::MemberPointer: 80bd5abe19SDimitry Andric case Type::Vector: 81bd5abe19SDimitry Andric case Type::ExtVector: 82bd5abe19SDimitry Andric case Type::FunctionProto: 83bd5abe19SDimitry Andric case Type::FunctionNoProto: 84bd5abe19SDimitry Andric case Type::Enum: 85bd5abe19SDimitry Andric case Type::ObjCObjectPointer: 86bd5abe19SDimitry Andric return false; 87bd5abe19SDimitry Andric 88bd5abe19SDimitry Andric // Complexes, arrays, records, and Objective-C objects. 89bd5abe19SDimitry Andric case Type::Complex: 90bd5abe19SDimitry Andric case Type::ConstantArray: 91bd5abe19SDimitry Andric case Type::IncompleteArray: 92bd5abe19SDimitry Andric case Type::VariableArray: 93bd5abe19SDimitry Andric case Type::Record: 94bd5abe19SDimitry Andric case Type::ObjCObject: 95bd5abe19SDimitry Andric case Type::ObjCInterface: 96bd5abe19SDimitry Andric return true; 976122f3e6SDimitry Andric 986122f3e6SDimitry Andric // In IRGen, atomic types are just the underlying type 996122f3e6SDimitry Andric case Type::Atomic: 1006122f3e6SDimitry Andric return hasAggregateLLVMType(type->getAs<AtomicType>()->getValueType()); 101bd5abe19SDimitry Andric } 102bd5abe19SDimitry Andric llvm_unreachable("unknown type kind!"); 103f22ef01cSRoman Divacky } 104f22ef01cSRoman Divacky 105f22ef01cSRoman Divacky void CodeGenFunction::EmitReturnBlock() { 106f22ef01cSRoman Divacky // For cleanliness, we try to avoid emitting the return block for 107f22ef01cSRoman Divacky // simple cases. 108f22ef01cSRoman Divacky llvm::BasicBlock *CurBB = Builder.GetInsertBlock(); 109f22ef01cSRoman Divacky 110f22ef01cSRoman Divacky if (CurBB) { 111f22ef01cSRoman Divacky assert(!CurBB->getTerminator() && "Unexpected terminated block."); 112f22ef01cSRoman Divacky 113f22ef01cSRoman Divacky // We have a valid insert point, reuse it if it is empty or there are no 114f22ef01cSRoman Divacky // explicit jumps to the return block. 115e580952dSDimitry Andric if (CurBB->empty() || ReturnBlock.getBlock()->use_empty()) { 116e580952dSDimitry Andric ReturnBlock.getBlock()->replaceAllUsesWith(CurBB); 117e580952dSDimitry Andric delete ReturnBlock.getBlock(); 118f22ef01cSRoman Divacky } else 119e580952dSDimitry Andric EmitBlock(ReturnBlock.getBlock()); 120f22ef01cSRoman Divacky return; 121f22ef01cSRoman Divacky } 122f22ef01cSRoman Divacky 123f22ef01cSRoman Divacky // Otherwise, if the return block is the target of a single direct 124f22ef01cSRoman Divacky // branch then we can just put the code in that block instead. This 125f22ef01cSRoman Divacky // cleans up functions which started with a unified return block. 126e580952dSDimitry Andric if (ReturnBlock.getBlock()->hasOneUse()) { 127f22ef01cSRoman Divacky llvm::BranchInst *BI = 128e580952dSDimitry Andric dyn_cast<llvm::BranchInst>(*ReturnBlock.getBlock()->use_begin()); 129ffd1746dSEd Schouten if (BI && BI->isUnconditional() && 130e580952dSDimitry Andric BI->getSuccessor(0) == ReturnBlock.getBlock()) { 1316122f3e6SDimitry Andric // Reset insertion point, including debug location, and delete the branch. 1326122f3e6SDimitry Andric Builder.SetCurrentDebugLocation(BI->getDebugLoc()); 133f22ef01cSRoman Divacky Builder.SetInsertPoint(BI->getParent()); 134f22ef01cSRoman Divacky BI->eraseFromParent(); 135e580952dSDimitry Andric delete ReturnBlock.getBlock(); 136f22ef01cSRoman Divacky return; 137f22ef01cSRoman Divacky } 138f22ef01cSRoman Divacky } 139f22ef01cSRoman Divacky 140f22ef01cSRoman Divacky // FIXME: We are at an unreachable point, there is no reason to emit the block 141f22ef01cSRoman Divacky // unless it has uses. However, we still need a place to put the debug 142f22ef01cSRoman Divacky // region.end for now. 143f22ef01cSRoman Divacky 144e580952dSDimitry Andric EmitBlock(ReturnBlock.getBlock()); 145ffd1746dSEd Schouten } 146ffd1746dSEd Schouten 147ffd1746dSEd Schouten static void EmitIfUsed(CodeGenFunction &CGF, llvm::BasicBlock *BB) { 148ffd1746dSEd Schouten if (!BB) return; 149ffd1746dSEd Schouten if (!BB->use_empty()) 150ffd1746dSEd Schouten return CGF.CurFn->getBasicBlockList().push_back(BB); 151ffd1746dSEd Schouten delete BB; 152f22ef01cSRoman Divacky } 153f22ef01cSRoman Divacky 154f22ef01cSRoman Divacky void CodeGenFunction::FinishFunction(SourceLocation EndLoc) { 155f22ef01cSRoman Divacky assert(BreakContinueStack.empty() && 156f22ef01cSRoman Divacky "mismatched push/pop in break/continue stack!"); 157f22ef01cSRoman Divacky 15817a519f9SDimitry Andric // Pop any cleanups that might have been associated with the 15917a519f9SDimitry Andric // parameters. Do this in whatever block we're currently in; it's 16017a519f9SDimitry Andric // important to do this before we enter the return block or return 16117a519f9SDimitry Andric // edges will be *really* confused. 16217a519f9SDimitry Andric if (EHStack.stable_begin() != PrologueCleanupDepth) 16317a519f9SDimitry Andric PopCleanupBlocks(PrologueCleanupDepth); 16417a519f9SDimitry Andric 165f22ef01cSRoman Divacky // Emit function epilog (to return). 166f22ef01cSRoman Divacky EmitReturnBlock(); 167f22ef01cSRoman Divacky 1682754fe60SDimitry Andric if (ShouldInstrumentFunction()) 169ffd1746dSEd Schouten EmitFunctionInstrumentation("__cyg_profile_func_exit"); 170ffd1746dSEd Schouten 171f22ef01cSRoman Divacky // Emit debug descriptor for function end. 172f22ef01cSRoman Divacky if (CGDebugInfo *DI = getDebugInfo()) { 173f22ef01cSRoman Divacky DI->setLocation(EndLoc); 174e580952dSDimitry Andric DI->EmitFunctionEnd(Builder); 175f22ef01cSRoman Divacky } 176f22ef01cSRoman Divacky 177ffd1746dSEd Schouten EmitFunctionEpilog(*CurFnInfo); 178f22ef01cSRoman Divacky EmitEndEHSpec(CurCodeDecl); 179f22ef01cSRoman Divacky 180ffd1746dSEd Schouten assert(EHStack.empty() && 181ffd1746dSEd Schouten "did not remove all scopes from cleanup stack!"); 182ffd1746dSEd Schouten 183f22ef01cSRoman Divacky // If someone did an indirect goto, emit the indirect goto block at the end of 184f22ef01cSRoman Divacky // the function. 185f22ef01cSRoman Divacky if (IndirectBranch) { 186f22ef01cSRoman Divacky EmitBlock(IndirectBranch->getParent()); 187f22ef01cSRoman Divacky Builder.ClearInsertionPoint(); 188f22ef01cSRoman Divacky } 189f22ef01cSRoman Divacky 190f22ef01cSRoman Divacky // Remove the AllocaInsertPt instruction, which is just a convenience for us. 191f22ef01cSRoman Divacky llvm::Instruction *Ptr = AllocaInsertPt; 192f22ef01cSRoman Divacky AllocaInsertPt = 0; 193f22ef01cSRoman Divacky Ptr->eraseFromParent(); 194f22ef01cSRoman Divacky 195f22ef01cSRoman Divacky // If someone took the address of a label but never did an indirect goto, we 196f22ef01cSRoman Divacky // made a zero entry PHI node, which is illegal, zap it now. 197f22ef01cSRoman Divacky if (IndirectBranch) { 198f22ef01cSRoman Divacky llvm::PHINode *PN = cast<llvm::PHINode>(IndirectBranch->getAddress()); 199f22ef01cSRoman Divacky if (PN->getNumIncomingValues() == 0) { 200f22ef01cSRoman Divacky PN->replaceAllUsesWith(llvm::UndefValue::get(PN->getType())); 201f22ef01cSRoman Divacky PN->eraseFromParent(); 202f22ef01cSRoman Divacky } 203f22ef01cSRoman Divacky } 204ffd1746dSEd Schouten 2056122f3e6SDimitry Andric EmitIfUsed(*this, EHResumeBlock); 206ffd1746dSEd Schouten EmitIfUsed(*this, TerminateLandingPad); 207ffd1746dSEd Schouten EmitIfUsed(*this, TerminateHandler); 208ffd1746dSEd Schouten EmitIfUsed(*this, UnreachableBlock); 209ffd1746dSEd Schouten 210ffd1746dSEd Schouten if (CGM.getCodeGenOpts().EmitDeclMetadata) 211ffd1746dSEd Schouten EmitDeclMetadata(); 212ffd1746dSEd Schouten } 213ffd1746dSEd Schouten 214ffd1746dSEd Schouten /// ShouldInstrumentFunction - Return true if the current function should be 215ffd1746dSEd Schouten /// instrumented with __cyg_profile_func_* calls 216ffd1746dSEd Schouten bool CodeGenFunction::ShouldInstrumentFunction() { 217ffd1746dSEd Schouten if (!CGM.getCodeGenOpts().InstrumentFunctions) 218ffd1746dSEd Schouten return false; 219bd5abe19SDimitry Andric if (!CurFuncDecl || CurFuncDecl->hasAttr<NoInstrumentFunctionAttr>()) 220ffd1746dSEd Schouten return false; 221ffd1746dSEd Schouten return true; 222ffd1746dSEd Schouten } 223ffd1746dSEd Schouten 224ffd1746dSEd Schouten /// EmitFunctionInstrumentation - Emit LLVM code to call the specified 225ffd1746dSEd Schouten /// instrumentation function with the current function and the call site, if 226ffd1746dSEd Schouten /// function instrumentation is enabled. 227ffd1746dSEd Schouten void CodeGenFunction::EmitFunctionInstrumentation(const char *Fn) { 228ffd1746dSEd Schouten // void __cyg_profile_func_{enter,exit} (void *this_fn, void *call_site); 22917a519f9SDimitry Andric llvm::PointerType *PointerTy = Int8PtrTy; 23017a519f9SDimitry Andric llvm::Type *ProfileFuncArgs[] = { PointerTy, PointerTy }; 2316122f3e6SDimitry Andric llvm::FunctionType *FunctionTy = 232dff0c46cSDimitry Andric llvm::FunctionType::get(VoidTy, ProfileFuncArgs, false); 233ffd1746dSEd Schouten 234ffd1746dSEd Schouten llvm::Constant *F = CGM.CreateRuntimeFunction(FunctionTy, Fn); 235ffd1746dSEd Schouten llvm::CallInst *CallSite = Builder.CreateCall( 23617a519f9SDimitry Andric CGM.getIntrinsic(llvm::Intrinsic::returnaddress), 237ffd1746dSEd Schouten llvm::ConstantInt::get(Int32Ty, 0), 238ffd1746dSEd Schouten "callsite"); 239ffd1746dSEd Schouten 240ffd1746dSEd Schouten Builder.CreateCall2(F, 241ffd1746dSEd Schouten llvm::ConstantExpr::getBitCast(CurFn, PointerTy), 242ffd1746dSEd Schouten CallSite); 243f22ef01cSRoman Divacky } 244f22ef01cSRoman Divacky 2452754fe60SDimitry Andric void CodeGenFunction::EmitMCountInstrumentation() { 246dff0c46cSDimitry Andric llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false); 2472754fe60SDimitry Andric 2482754fe60SDimitry Andric llvm::Constant *MCountFn = CGM.CreateRuntimeFunction(FTy, 2492754fe60SDimitry Andric Target.getMCountName()); 2502754fe60SDimitry Andric Builder.CreateCall(MCountFn); 2512754fe60SDimitry Andric } 2522754fe60SDimitry Andric 253f22ef01cSRoman Divacky void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy, 254f22ef01cSRoman Divacky llvm::Function *Fn, 2553b0f4066SDimitry Andric const CGFunctionInfo &FnInfo, 256f22ef01cSRoman Divacky const FunctionArgList &Args, 257f22ef01cSRoman Divacky SourceLocation StartLoc) { 258f22ef01cSRoman Divacky const Decl *D = GD.getDecl(); 259f22ef01cSRoman Divacky 260f22ef01cSRoman Divacky DidCallStackSave = false; 261f22ef01cSRoman Divacky CurCodeDecl = CurFuncDecl = D; 262f22ef01cSRoman Divacky FnRetTy = RetTy; 263f22ef01cSRoman Divacky CurFn = Fn; 2643b0f4066SDimitry Andric CurFnInfo = &FnInfo; 265f22ef01cSRoman Divacky assert(CurFn->isDeclaration() && "Function already has body?"); 266f22ef01cSRoman Divacky 267f22ef01cSRoman Divacky // Pass inline keyword to optimizer if it appears explicitly on any 268f22ef01cSRoman Divacky // declaration. 269dff0c46cSDimitry Andric if (!CGM.getCodeGenOpts().NoInline) 270f22ef01cSRoman Divacky if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) 271f22ef01cSRoman Divacky for (FunctionDecl::redecl_iterator RI = FD->redecls_begin(), 272f22ef01cSRoman Divacky RE = FD->redecls_end(); RI != RE; ++RI) 273f22ef01cSRoman Divacky if (RI->isInlineSpecified()) { 274f22ef01cSRoman Divacky Fn->addFnAttr(llvm::Attribute::InlineHint); 275f22ef01cSRoman Divacky break; 276f22ef01cSRoman Divacky } 277f22ef01cSRoman Divacky 278dff0c46cSDimitry Andric if (getContext().getLangOpts().OpenCL) { 2792754fe60SDimitry Andric // Add metadata for a kernel function. 2802754fe60SDimitry Andric if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) 2812754fe60SDimitry Andric if (FD->hasAttr<OpenCLKernelAttr>()) { 2822754fe60SDimitry Andric llvm::LLVMContext &Context = getLLVMContext(); 2832754fe60SDimitry Andric llvm::NamedMDNode *OpenCLMetadata = 2842754fe60SDimitry Andric CGM.getModule().getOrInsertNamedMetadata("opencl.kernels"); 2852754fe60SDimitry Andric 2862754fe60SDimitry Andric llvm::Value *Op = Fn; 2873b0f4066SDimitry Andric OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Op)); 2882754fe60SDimitry Andric } 2892754fe60SDimitry Andric } 2902754fe60SDimitry Andric 291f22ef01cSRoman Divacky llvm::BasicBlock *EntryBB = createBasicBlock("entry", CurFn); 292f22ef01cSRoman Divacky 293f22ef01cSRoman Divacky // Create a marker to make it easy to insert allocas into the entryblock 294f22ef01cSRoman Divacky // later. Don't create this with the builder, because we don't want it 295f22ef01cSRoman Divacky // folded. 296ffd1746dSEd Schouten llvm::Value *Undef = llvm::UndefValue::get(Int32Ty); 297ffd1746dSEd Schouten AllocaInsertPt = new llvm::BitCastInst(Undef, Int32Ty, "", EntryBB); 298f22ef01cSRoman Divacky if (Builder.isNamePreserving()) 299f22ef01cSRoman Divacky AllocaInsertPt->setName("allocapt"); 300f22ef01cSRoman Divacky 301ffd1746dSEd Schouten ReturnBlock = getJumpDestInCurrentScope("return"); 302f22ef01cSRoman Divacky 303f22ef01cSRoman Divacky Builder.SetInsertPoint(EntryBB); 304f22ef01cSRoman Divacky 305f22ef01cSRoman Divacky // Emit subprogram debug descriptor. 306f22ef01cSRoman Divacky if (CGDebugInfo *DI = getDebugInfo()) { 307dff0c46cSDimitry Andric unsigned NumArgs = 0; 308dff0c46cSDimitry Andric QualType *ArgsArray = new QualType[Args.size()]; 309dff0c46cSDimitry Andric for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end(); 310dff0c46cSDimitry Andric i != e; ++i) { 311dff0c46cSDimitry Andric ArgsArray[NumArgs++] = (*i)->getType(); 312dff0c46cSDimitry Andric } 313dff0c46cSDimitry Andric 3142754fe60SDimitry Andric QualType FnType = 315dff0c46cSDimitry Andric getContext().getFunctionType(RetTy, ArgsArray, NumArgs, 3162754fe60SDimitry Andric FunctionProtoType::ExtProtoInfo()); 3172754fe60SDimitry Andric 318dff0c46cSDimitry Andric delete[] ArgsArray; 319dff0c46cSDimitry Andric 320f22ef01cSRoman Divacky DI->setLocation(StartLoc); 321f22ef01cSRoman Divacky DI->EmitFunctionStart(GD, FnType, CurFn, Builder); 322f22ef01cSRoman Divacky } 323f22ef01cSRoman Divacky 3242754fe60SDimitry Andric if (ShouldInstrumentFunction()) 325ffd1746dSEd Schouten EmitFunctionInstrumentation("__cyg_profile_func_enter"); 326ffd1746dSEd Schouten 3272754fe60SDimitry Andric if (CGM.getCodeGenOpts().InstrumentForProfiling) 3282754fe60SDimitry Andric EmitMCountInstrumentation(); 3292754fe60SDimitry Andric 330f22ef01cSRoman Divacky if (RetTy->isVoidType()) { 331f22ef01cSRoman Divacky // Void type; nothing to return. 332f22ef01cSRoman Divacky ReturnValue = 0; 333f22ef01cSRoman Divacky } else if (CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect && 334f22ef01cSRoman Divacky hasAggregateLLVMType(CurFnInfo->getReturnType())) { 335f22ef01cSRoman Divacky // Indirect aggregate return; emit returned value directly into sret slot. 336f22ef01cSRoman Divacky // This reduces code size, and affects correctness in C++. 337f22ef01cSRoman Divacky ReturnValue = CurFn->arg_begin(); 338f22ef01cSRoman Divacky } else { 339f22ef01cSRoman Divacky ReturnValue = CreateIRTemp(RetTy, "retval"); 34017a519f9SDimitry Andric 34117a519f9SDimitry Andric // Tell the epilog emitter to autorelease the result. We do this 34217a519f9SDimitry Andric // now so that various specialized functions can suppress it 34317a519f9SDimitry Andric // during their IR-generation. 344dff0c46cSDimitry Andric if (getLangOpts().ObjCAutoRefCount && 34517a519f9SDimitry Andric !CurFnInfo->isReturnsRetained() && 34617a519f9SDimitry Andric RetTy->isObjCRetainableType()) 34717a519f9SDimitry Andric AutoreleaseResult = true; 348f22ef01cSRoman Divacky } 349f22ef01cSRoman Divacky 350f22ef01cSRoman Divacky EmitStartEHSpec(CurCodeDecl); 35117a519f9SDimitry Andric 35217a519f9SDimitry Andric PrologueCleanupDepth = EHStack.stable_begin(); 353f22ef01cSRoman Divacky EmitFunctionProlog(*CurFnInfo, CurFn, Args); 354f22ef01cSRoman Divacky 355dff0c46cSDimitry Andric if (D && isa<CXXMethodDecl>(D) && cast<CXXMethodDecl>(D)->isInstance()) { 356e580952dSDimitry Andric CGM.getCXXABI().EmitInstanceFunctionProlog(*this); 357dff0c46cSDimitry Andric const CXXMethodDecl *MD = cast<CXXMethodDecl>(D); 358dff0c46cSDimitry Andric if (MD->getParent()->isLambda() && 359dff0c46cSDimitry Andric MD->getOverloadedOperator() == OO_Call) { 360dff0c46cSDimitry Andric // We're in a lambda; figure out the captures. 361dff0c46cSDimitry Andric MD->getParent()->getCaptureFields(LambdaCaptureFields, 362dff0c46cSDimitry Andric LambdaThisCaptureField); 363dff0c46cSDimitry Andric if (LambdaThisCaptureField) { 364dff0c46cSDimitry Andric // If this lambda captures this, load it. 365dff0c46cSDimitry Andric LValue ThisLValue = EmitLValueForField(CXXABIThisValue, 366dff0c46cSDimitry Andric LambdaThisCaptureField, 0); 367dff0c46cSDimitry Andric CXXThisValue = EmitLoadOfLValue(ThisLValue).getScalarVal(); 368dff0c46cSDimitry Andric } 369dff0c46cSDimitry Andric } else { 370dff0c46cSDimitry Andric // Not in a lambda; just use 'this' from the method. 371dff0c46cSDimitry Andric // FIXME: Should we generate a new load for each use of 'this'? The 372dff0c46cSDimitry Andric // fast register allocator would be happier... 373dff0c46cSDimitry Andric CXXThisValue = CXXABIThisValue; 374dff0c46cSDimitry Andric } 375dff0c46cSDimitry Andric } 376f22ef01cSRoman Divacky 377f22ef01cSRoman Divacky // If any of the arguments have a variably modified type, make sure to 378f22ef01cSRoman Divacky // emit the type size. 379f22ef01cSRoman Divacky for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end(); 380f22ef01cSRoman Divacky i != e; ++i) { 3813b0f4066SDimitry Andric QualType Ty = (*i)->getType(); 382f22ef01cSRoman Divacky 383f22ef01cSRoman Divacky if (Ty->isVariablyModifiedType()) 38417a519f9SDimitry Andric EmitVariablyModifiedType(Ty); 385f22ef01cSRoman Divacky } 3866122f3e6SDimitry Andric // Emit a location at the end of the prologue. 3876122f3e6SDimitry Andric if (CGDebugInfo *DI = getDebugInfo()) 3886122f3e6SDimitry Andric DI->EmitLocation(Builder, StartLoc); 389f22ef01cSRoman Divacky } 390f22ef01cSRoman Divacky 391f22ef01cSRoman Divacky void CodeGenFunction::EmitFunctionBody(FunctionArgList &Args) { 392f22ef01cSRoman Divacky const FunctionDecl *FD = cast<FunctionDecl>(CurGD.getDecl()); 393f22ef01cSRoman Divacky assert(FD->getBody()); 394f22ef01cSRoman Divacky EmitStmt(FD->getBody()); 395f22ef01cSRoman Divacky } 396f22ef01cSRoman Divacky 397e580952dSDimitry Andric /// Tries to mark the given function nounwind based on the 398e580952dSDimitry Andric /// non-existence of any throwing calls within it. We believe this is 399e580952dSDimitry Andric /// lightweight enough to do at -O0. 400e580952dSDimitry Andric static void TryMarkNoThrow(llvm::Function *F) { 401e580952dSDimitry Andric // LLVM treats 'nounwind' on a function as part of the type, so we 402e580952dSDimitry Andric // can't do this on functions that can be overwritten. 403e580952dSDimitry Andric if (F->mayBeOverridden()) return; 404e580952dSDimitry Andric 405e580952dSDimitry Andric for (llvm::Function::iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI) 406e580952dSDimitry Andric for (llvm::BasicBlock::iterator 407e580952dSDimitry Andric BI = FI->begin(), BE = FI->end(); BI != BE; ++BI) 4086122f3e6SDimitry Andric if (llvm::CallInst *Call = dyn_cast<llvm::CallInst>(&*BI)) { 409e580952dSDimitry Andric if (!Call->doesNotThrow()) 410e580952dSDimitry Andric return; 4116122f3e6SDimitry Andric } else if (isa<llvm::ResumeInst>(&*BI)) { 4126122f3e6SDimitry Andric return; 4136122f3e6SDimitry Andric } 414e580952dSDimitry Andric F->setDoesNotThrow(true); 415e580952dSDimitry Andric } 416e580952dSDimitry Andric 4173b0f4066SDimitry Andric void CodeGenFunction::GenerateCode(GlobalDecl GD, llvm::Function *Fn, 4183b0f4066SDimitry Andric const CGFunctionInfo &FnInfo) { 419f22ef01cSRoman Divacky const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl()); 420f22ef01cSRoman Divacky 421f22ef01cSRoman Divacky // Check if we should generate debug info for this function. 4223b0f4066SDimitry Andric if (CGM.getModuleDebugInfo() && !FD->hasAttr<NoDebugAttr>()) 4233b0f4066SDimitry Andric DebugInfo = CGM.getModuleDebugInfo(); 424f22ef01cSRoman Divacky 425f22ef01cSRoman Divacky FunctionArgList Args; 426e580952dSDimitry Andric QualType ResTy = FD->getResultType(); 427f22ef01cSRoman Divacky 428f22ef01cSRoman Divacky CurGD = GD; 429e580952dSDimitry Andric if (isa<CXXMethodDecl>(FD) && cast<CXXMethodDecl>(FD)->isInstance()) 430e580952dSDimitry Andric CGM.getCXXABI().BuildInstanceFunctionParams(*this, ResTy, Args); 431f22ef01cSRoman Divacky 432f22ef01cSRoman Divacky for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) 4333b0f4066SDimitry Andric Args.push_back(FD->getParamDecl(i)); 434f22ef01cSRoman Divacky 435f22ef01cSRoman Divacky SourceRange BodyRange; 436f22ef01cSRoman Divacky if (Stmt *Body = FD->getBody()) BodyRange = Body->getSourceRange(); 437f22ef01cSRoman Divacky 438f22ef01cSRoman Divacky // Emit the standard function prologue. 4393b0f4066SDimitry Andric StartFunction(GD, ResTy, Fn, FnInfo, Args, BodyRange.getBegin()); 440f22ef01cSRoman Divacky 441f22ef01cSRoman Divacky // Generate the body of the function. 442f22ef01cSRoman Divacky if (isa<CXXDestructorDecl>(FD)) 443f22ef01cSRoman Divacky EmitDestructorBody(Args); 444f22ef01cSRoman Divacky else if (isa<CXXConstructorDecl>(FD)) 445f22ef01cSRoman Divacky EmitConstructorBody(Args); 446dff0c46cSDimitry Andric else if (getContext().getLangOpts().CUDA && 4476122f3e6SDimitry Andric !CGM.getCodeGenOpts().CUDAIsDevice && 4486122f3e6SDimitry Andric FD->hasAttr<CUDAGlobalAttr>()) 4496122f3e6SDimitry Andric CGM.getCUDARuntime().EmitDeviceStubBody(*this, Args); 450dff0c46cSDimitry Andric else if (isa<CXXConversionDecl>(FD) && 451dff0c46cSDimitry Andric cast<CXXConversionDecl>(FD)->isLambdaToBlockPointerConversion()) { 452dff0c46cSDimitry Andric // The lambda conversion to block pointer is special; the semantics can't be 453dff0c46cSDimitry Andric // expressed in the AST, so IRGen needs to special-case it. 454dff0c46cSDimitry Andric EmitLambdaToBlockPointerBody(Args); 455dff0c46cSDimitry Andric } else if (isa<CXXMethodDecl>(FD) && 456dff0c46cSDimitry Andric cast<CXXMethodDecl>(FD)->isLambdaStaticInvoker()) { 457dff0c46cSDimitry Andric // The lambda "__invoke" function is special, because it forwards or 458dff0c46cSDimitry Andric // clones the body of the function call operator (but is actually static). 459dff0c46cSDimitry Andric EmitLambdaStaticInvokeFunction(cast<CXXMethodDecl>(FD)); 460dff0c46cSDimitry Andric } 461f22ef01cSRoman Divacky else 462f22ef01cSRoman Divacky EmitFunctionBody(Args); 463f22ef01cSRoman Divacky 464f22ef01cSRoman Divacky // Emit the standard function epilogue. 465f22ef01cSRoman Divacky FinishFunction(BodyRange.getEnd()); 466f22ef01cSRoman Divacky 467e580952dSDimitry Andric // If we haven't marked the function nothrow through other means, do 468e580952dSDimitry Andric // a quick pass now to see if we can. 469e580952dSDimitry Andric if (!CurFn->doesNotThrow()) 470e580952dSDimitry Andric TryMarkNoThrow(CurFn); 471f22ef01cSRoman Divacky } 472f22ef01cSRoman Divacky 473f22ef01cSRoman Divacky /// ContainsLabel - Return true if the statement contains a label in it. If 474f22ef01cSRoman Divacky /// this statement is not executed normally, it not containing a label means 475f22ef01cSRoman Divacky /// that we can just remove the code. 476f22ef01cSRoman Divacky bool CodeGenFunction::ContainsLabel(const Stmt *S, bool IgnoreCaseStmts) { 477f22ef01cSRoman Divacky // Null statement, not a label! 478f22ef01cSRoman Divacky if (S == 0) return false; 479f22ef01cSRoman Divacky 480f22ef01cSRoman Divacky // If this is a label, we have to emit the code, consider something like: 481f22ef01cSRoman Divacky // if (0) { ... foo: bar(); } goto foo; 4823b0f4066SDimitry Andric // 4833b0f4066SDimitry Andric // TODO: If anyone cared, we could track __label__'s, since we know that you 4843b0f4066SDimitry Andric // can't jump to one from outside their declared region. 485f22ef01cSRoman Divacky if (isa<LabelStmt>(S)) 486f22ef01cSRoman Divacky return true; 487f22ef01cSRoman Divacky 488f22ef01cSRoman Divacky // If this is a case/default statement, and we haven't seen a switch, we have 489f22ef01cSRoman Divacky // to emit the code. 490f22ef01cSRoman Divacky if (isa<SwitchCase>(S) && !IgnoreCaseStmts) 491f22ef01cSRoman Divacky return true; 492f22ef01cSRoman Divacky 493f22ef01cSRoman Divacky // If this is a switch statement, we want to ignore cases below it. 494f22ef01cSRoman Divacky if (isa<SwitchStmt>(S)) 495f22ef01cSRoman Divacky IgnoreCaseStmts = true; 496f22ef01cSRoman Divacky 497f22ef01cSRoman Divacky // Scan subexpressions for verboten labels. 4982754fe60SDimitry Andric for (Stmt::const_child_range I = S->children(); I; ++I) 499f22ef01cSRoman Divacky if (ContainsLabel(*I, IgnoreCaseStmts)) 500f22ef01cSRoman Divacky return true; 501f22ef01cSRoman Divacky 502f22ef01cSRoman Divacky return false; 503f22ef01cSRoman Divacky } 504f22ef01cSRoman Divacky 5053b0f4066SDimitry Andric /// containsBreak - Return true if the statement contains a break out of it. 5063b0f4066SDimitry Andric /// If the statement (recursively) contains a switch or loop with a break 5073b0f4066SDimitry Andric /// inside of it, this is fine. 5083b0f4066SDimitry Andric bool CodeGenFunction::containsBreak(const Stmt *S) { 5093b0f4066SDimitry Andric // Null statement, not a label! 5103b0f4066SDimitry Andric if (S == 0) return false; 511f22ef01cSRoman Divacky 5123b0f4066SDimitry Andric // If this is a switch or loop that defines its own break scope, then we can 5133b0f4066SDimitry Andric // include it and anything inside of it. 5143b0f4066SDimitry Andric if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || isa<DoStmt>(S) || 5153b0f4066SDimitry Andric isa<ForStmt>(S)) 5163b0f4066SDimitry Andric return false; 5173b0f4066SDimitry Andric 5183b0f4066SDimitry Andric if (isa<BreakStmt>(S)) 5193b0f4066SDimitry Andric return true; 5203b0f4066SDimitry Andric 5213b0f4066SDimitry Andric // Scan subexpressions for verboten breaks. 5223b0f4066SDimitry Andric for (Stmt::const_child_range I = S->children(); I; ++I) 5233b0f4066SDimitry Andric if (containsBreak(*I)) 5243b0f4066SDimitry Andric return true; 5253b0f4066SDimitry Andric 5263b0f4066SDimitry Andric return false; 5273b0f4066SDimitry Andric } 5283b0f4066SDimitry Andric 5293b0f4066SDimitry Andric 5303b0f4066SDimitry Andric /// ConstantFoldsToSimpleInteger - If the specified expression does not fold 5313b0f4066SDimitry Andric /// to a constant, or if it does but contains a label, return false. If it 5323b0f4066SDimitry Andric /// constant folds return true and set the boolean result in Result. 5333b0f4066SDimitry Andric bool CodeGenFunction::ConstantFoldsToSimpleInteger(const Expr *Cond, 5343b0f4066SDimitry Andric bool &ResultBool) { 5353b0f4066SDimitry Andric llvm::APInt ResultInt; 5363b0f4066SDimitry Andric if (!ConstantFoldsToSimpleInteger(Cond, ResultInt)) 5373b0f4066SDimitry Andric return false; 5383b0f4066SDimitry Andric 5393b0f4066SDimitry Andric ResultBool = ResultInt.getBoolValue(); 5403b0f4066SDimitry Andric return true; 5413b0f4066SDimitry Andric } 5423b0f4066SDimitry Andric 5433b0f4066SDimitry Andric /// ConstantFoldsToSimpleInteger - If the specified expression does not fold 5443b0f4066SDimitry Andric /// to a constant, or if it does but contains a label, return false. If it 5453b0f4066SDimitry Andric /// constant folds return true and set the folded value. 5463b0f4066SDimitry Andric bool CodeGenFunction:: 5473b0f4066SDimitry Andric ConstantFoldsToSimpleInteger(const Expr *Cond, llvm::APInt &ResultInt) { 548f22ef01cSRoman Divacky // FIXME: Rename and handle conversion of other evaluatable things 549f22ef01cSRoman Divacky // to bool. 550dff0c46cSDimitry Andric llvm::APSInt Int; 551dff0c46cSDimitry Andric if (!Cond->EvaluateAsInt(Int, getContext())) 5523b0f4066SDimitry Andric return false; // Not foldable, not integer or not fully evaluatable. 553f22ef01cSRoman Divacky 554f22ef01cSRoman Divacky if (CodeGenFunction::ContainsLabel(Cond)) 5553b0f4066SDimitry Andric return false; // Contains a label. 556f22ef01cSRoman Divacky 557dff0c46cSDimitry Andric ResultInt = Int; 5583b0f4066SDimitry Andric return true; 559f22ef01cSRoman Divacky } 560f22ef01cSRoman Divacky 561f22ef01cSRoman Divacky 5623b0f4066SDimitry Andric 563f22ef01cSRoman Divacky /// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an if 564f22ef01cSRoman Divacky /// statement) to the specified blocks. Based on the condition, this might try 565f22ef01cSRoman Divacky /// to simplify the codegen of the conditional based on the branch. 566f22ef01cSRoman Divacky /// 567f22ef01cSRoman Divacky void CodeGenFunction::EmitBranchOnBoolExpr(const Expr *Cond, 568f22ef01cSRoman Divacky llvm::BasicBlock *TrueBlock, 569f22ef01cSRoman Divacky llvm::BasicBlock *FalseBlock) { 5703b0f4066SDimitry Andric Cond = Cond->IgnoreParens(); 571f22ef01cSRoman Divacky 572f22ef01cSRoman Divacky if (const BinaryOperator *CondBOp = dyn_cast<BinaryOperator>(Cond)) { 573f22ef01cSRoman Divacky // Handle X && Y in a condition. 574e580952dSDimitry Andric if (CondBOp->getOpcode() == BO_LAnd) { 575f22ef01cSRoman Divacky // If we have "1 && X", simplify the code. "0 && X" would have constant 576f22ef01cSRoman Divacky // folded if the case was simple enough. 5773b0f4066SDimitry Andric bool ConstantBool = false; 5783b0f4066SDimitry Andric if (ConstantFoldsToSimpleInteger(CondBOp->getLHS(), ConstantBool) && 5793b0f4066SDimitry Andric ConstantBool) { 580f22ef01cSRoman Divacky // br(1 && X) -> br(X). 581f22ef01cSRoman Divacky return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); 582f22ef01cSRoman Divacky } 583f22ef01cSRoman Divacky 584f22ef01cSRoman Divacky // If we have "X && 1", simplify the code to use an uncond branch. 585f22ef01cSRoman Divacky // "X && 0" would have been constant folded to 0. 5863b0f4066SDimitry Andric if (ConstantFoldsToSimpleInteger(CondBOp->getRHS(), ConstantBool) && 5873b0f4066SDimitry Andric ConstantBool) { 588f22ef01cSRoman Divacky // br(X && 1) -> br(X). 589f22ef01cSRoman Divacky return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock); 590f22ef01cSRoman Divacky } 591f22ef01cSRoman Divacky 592f22ef01cSRoman Divacky // Emit the LHS as a conditional. If the LHS conditional is false, we 593f22ef01cSRoman Divacky // want to jump to the FalseBlock. 594f22ef01cSRoman Divacky llvm::BasicBlock *LHSTrue = createBasicBlock("land.lhs.true"); 5952754fe60SDimitry Andric 5962754fe60SDimitry Andric ConditionalEvaluation eval(*this); 597f22ef01cSRoman Divacky EmitBranchOnBoolExpr(CondBOp->getLHS(), LHSTrue, FalseBlock); 598f22ef01cSRoman Divacky EmitBlock(LHSTrue); 599f22ef01cSRoman Divacky 600f22ef01cSRoman Divacky // Any temporaries created here are conditional. 6012754fe60SDimitry Andric eval.begin(*this); 602f22ef01cSRoman Divacky EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); 6032754fe60SDimitry Andric eval.end(*this); 604f22ef01cSRoman Divacky 605f22ef01cSRoman Divacky return; 6063b0f4066SDimitry Andric } 6073b0f4066SDimitry Andric 6083b0f4066SDimitry Andric if (CondBOp->getOpcode() == BO_LOr) { 609f22ef01cSRoman Divacky // If we have "0 || X", simplify the code. "1 || X" would have constant 610f22ef01cSRoman Divacky // folded if the case was simple enough. 6113b0f4066SDimitry Andric bool ConstantBool = false; 6123b0f4066SDimitry Andric if (ConstantFoldsToSimpleInteger(CondBOp->getLHS(), ConstantBool) && 6133b0f4066SDimitry Andric !ConstantBool) { 614f22ef01cSRoman Divacky // br(0 || X) -> br(X). 615f22ef01cSRoman Divacky return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); 616f22ef01cSRoman Divacky } 617f22ef01cSRoman Divacky 618f22ef01cSRoman Divacky // If we have "X || 0", simplify the code to use an uncond branch. 619f22ef01cSRoman Divacky // "X || 1" would have been constant folded to 1. 6203b0f4066SDimitry Andric if (ConstantFoldsToSimpleInteger(CondBOp->getRHS(), ConstantBool) && 6213b0f4066SDimitry Andric !ConstantBool) { 622f22ef01cSRoman Divacky // br(X || 0) -> br(X). 623f22ef01cSRoman Divacky return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock); 624f22ef01cSRoman Divacky } 625f22ef01cSRoman Divacky 626f22ef01cSRoman Divacky // Emit the LHS as a conditional. If the LHS conditional is true, we 627f22ef01cSRoman Divacky // want to jump to the TrueBlock. 628f22ef01cSRoman Divacky llvm::BasicBlock *LHSFalse = createBasicBlock("lor.lhs.false"); 6292754fe60SDimitry Andric 6302754fe60SDimitry Andric ConditionalEvaluation eval(*this); 631f22ef01cSRoman Divacky EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, LHSFalse); 632f22ef01cSRoman Divacky EmitBlock(LHSFalse); 633f22ef01cSRoman Divacky 634f22ef01cSRoman Divacky // Any temporaries created here are conditional. 6352754fe60SDimitry Andric eval.begin(*this); 636f22ef01cSRoman Divacky EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); 6372754fe60SDimitry Andric eval.end(*this); 638f22ef01cSRoman Divacky 639f22ef01cSRoman Divacky return; 640f22ef01cSRoman Divacky } 641f22ef01cSRoman Divacky } 642f22ef01cSRoman Divacky 643f22ef01cSRoman Divacky if (const UnaryOperator *CondUOp = dyn_cast<UnaryOperator>(Cond)) { 644f22ef01cSRoman Divacky // br(!x, t, f) -> br(x, f, t) 645e580952dSDimitry Andric if (CondUOp->getOpcode() == UO_LNot) 646f22ef01cSRoman Divacky return EmitBranchOnBoolExpr(CondUOp->getSubExpr(), FalseBlock, TrueBlock); 647f22ef01cSRoman Divacky } 648f22ef01cSRoman Divacky 649f22ef01cSRoman Divacky if (const ConditionalOperator *CondOp = dyn_cast<ConditionalOperator>(Cond)) { 650f22ef01cSRoman Divacky // br(c ? x : y, t, f) -> br(c, br(x, t, f), br(y, t, f)) 651f22ef01cSRoman Divacky llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true"); 652f22ef01cSRoman Divacky llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false"); 6532754fe60SDimitry Andric 6542754fe60SDimitry Andric ConditionalEvaluation cond(*this); 655f22ef01cSRoman Divacky EmitBranchOnBoolExpr(CondOp->getCond(), LHSBlock, RHSBlock); 6562754fe60SDimitry Andric 6572754fe60SDimitry Andric cond.begin(*this); 658f22ef01cSRoman Divacky EmitBlock(LHSBlock); 659f22ef01cSRoman Divacky EmitBranchOnBoolExpr(CondOp->getLHS(), TrueBlock, FalseBlock); 6602754fe60SDimitry Andric cond.end(*this); 6612754fe60SDimitry Andric 6622754fe60SDimitry Andric cond.begin(*this); 663f22ef01cSRoman Divacky EmitBlock(RHSBlock); 664f22ef01cSRoman Divacky EmitBranchOnBoolExpr(CondOp->getRHS(), TrueBlock, FalseBlock); 6652754fe60SDimitry Andric cond.end(*this); 6662754fe60SDimitry Andric 667f22ef01cSRoman Divacky return; 668f22ef01cSRoman Divacky } 669f22ef01cSRoman Divacky 670f22ef01cSRoman Divacky // Emit the code with the fully general case. 671f22ef01cSRoman Divacky llvm::Value *CondV = EvaluateExprAsBool(Cond); 672f22ef01cSRoman Divacky Builder.CreateCondBr(CondV, TrueBlock, FalseBlock); 673f22ef01cSRoman Divacky } 674f22ef01cSRoman Divacky 675f22ef01cSRoman Divacky /// ErrorUnsupported - Print out an error that codegen doesn't support the 676f22ef01cSRoman Divacky /// specified stmt yet. 677f22ef01cSRoman Divacky void CodeGenFunction::ErrorUnsupported(const Stmt *S, const char *Type, 678f22ef01cSRoman Divacky bool OmitOnError) { 679f22ef01cSRoman Divacky CGM.ErrorUnsupported(S, Type, OmitOnError); 680f22ef01cSRoman Divacky } 681f22ef01cSRoman Divacky 6822754fe60SDimitry Andric /// emitNonZeroVLAInit - Emit the "zero" initialization of a 6832754fe60SDimitry Andric /// variable-length array whose elements have a non-zero bit-pattern. 6842754fe60SDimitry Andric /// 6852754fe60SDimitry Andric /// \param src - a char* pointing to the bit-pattern for a single 6862754fe60SDimitry Andric /// base element of the array 6872754fe60SDimitry Andric /// \param sizeInChars - the total size of the VLA, in chars 6882754fe60SDimitry Andric /// \param align - the total alignment of the VLA 6892754fe60SDimitry Andric static void emitNonZeroVLAInit(CodeGenFunction &CGF, QualType baseType, 6902754fe60SDimitry Andric llvm::Value *dest, llvm::Value *src, 6912754fe60SDimitry Andric llvm::Value *sizeInChars) { 6922754fe60SDimitry Andric std::pair<CharUnits,CharUnits> baseSizeAndAlign 6932754fe60SDimitry Andric = CGF.getContext().getTypeInfoInChars(baseType); 6942754fe60SDimitry Andric 6952754fe60SDimitry Andric CGBuilderTy &Builder = CGF.Builder; 6962754fe60SDimitry Andric 6972754fe60SDimitry Andric llvm::Value *baseSizeInChars 6982754fe60SDimitry Andric = llvm::ConstantInt::get(CGF.IntPtrTy, baseSizeAndAlign.first.getQuantity()); 6992754fe60SDimitry Andric 7006122f3e6SDimitry Andric llvm::Type *i8p = Builder.getInt8PtrTy(); 7012754fe60SDimitry Andric 7022754fe60SDimitry Andric llvm::Value *begin = Builder.CreateBitCast(dest, i8p, "vla.begin"); 7032754fe60SDimitry Andric llvm::Value *end = Builder.CreateInBoundsGEP(dest, sizeInChars, "vla.end"); 7042754fe60SDimitry Andric 7052754fe60SDimitry Andric llvm::BasicBlock *originBB = CGF.Builder.GetInsertBlock(); 7062754fe60SDimitry Andric llvm::BasicBlock *loopBB = CGF.createBasicBlock("vla-init.loop"); 7072754fe60SDimitry Andric llvm::BasicBlock *contBB = CGF.createBasicBlock("vla-init.cont"); 7082754fe60SDimitry Andric 7092754fe60SDimitry Andric // Make a loop over the VLA. C99 guarantees that the VLA element 7102754fe60SDimitry Andric // count must be nonzero. 7112754fe60SDimitry Andric CGF.EmitBlock(loopBB); 7122754fe60SDimitry Andric 7133b0f4066SDimitry Andric llvm::PHINode *cur = Builder.CreatePHI(i8p, 2, "vla.cur"); 7142754fe60SDimitry Andric cur->addIncoming(begin, originBB); 7152754fe60SDimitry Andric 7162754fe60SDimitry Andric // memcpy the individual element bit-pattern. 7172754fe60SDimitry Andric Builder.CreateMemCpy(cur, src, baseSizeInChars, 7182754fe60SDimitry Andric baseSizeAndAlign.second.getQuantity(), 7192754fe60SDimitry Andric /*volatile*/ false); 7202754fe60SDimitry Andric 7212754fe60SDimitry Andric // Go to the next element. 7222754fe60SDimitry Andric llvm::Value *next = Builder.CreateConstInBoundsGEP1_32(cur, 1, "vla.next"); 7232754fe60SDimitry Andric 7242754fe60SDimitry Andric // Leave if that's the end of the VLA. 7252754fe60SDimitry Andric llvm::Value *done = Builder.CreateICmpEQ(next, end, "vla-init.isdone"); 7262754fe60SDimitry Andric Builder.CreateCondBr(done, contBB, loopBB); 7272754fe60SDimitry Andric cur->addIncoming(next, loopBB); 7282754fe60SDimitry Andric 7292754fe60SDimitry Andric CGF.EmitBlock(contBB); 7302754fe60SDimitry Andric } 7312754fe60SDimitry Andric 732f22ef01cSRoman Divacky void 733f22ef01cSRoman Divacky CodeGenFunction::EmitNullInitialization(llvm::Value *DestPtr, QualType Ty) { 734f22ef01cSRoman Divacky // Ignore empty classes in C++. 735dff0c46cSDimitry Andric if (getContext().getLangOpts().CPlusPlus) { 736f22ef01cSRoman Divacky if (const RecordType *RT = Ty->getAs<RecordType>()) { 737f22ef01cSRoman Divacky if (cast<CXXRecordDecl>(RT->getDecl())->isEmpty()) 738f22ef01cSRoman Divacky return; 739f22ef01cSRoman Divacky } 740f22ef01cSRoman Divacky } 741f22ef01cSRoman Divacky 742e580952dSDimitry Andric // Cast the dest ptr to the appropriate i8 pointer type. 743e580952dSDimitry Andric unsigned DestAS = 744e580952dSDimitry Andric cast<llvm::PointerType>(DestPtr->getType())->getAddressSpace(); 7456122f3e6SDimitry Andric llvm::Type *BP = Builder.getInt8PtrTy(DestAS); 746f22ef01cSRoman Divacky if (DestPtr->getType() != BP) 7476122f3e6SDimitry Andric DestPtr = Builder.CreateBitCast(DestPtr, BP); 748f22ef01cSRoman Divacky 749f22ef01cSRoman Divacky // Get size and alignment info for this aggregate. 7503b0f4066SDimitry Andric std::pair<CharUnits, CharUnits> TypeInfo = 7513b0f4066SDimitry Andric getContext().getTypeInfoInChars(Ty); 7523b0f4066SDimitry Andric CharUnits Size = TypeInfo.first; 7533b0f4066SDimitry Andric CharUnits Align = TypeInfo.second; 7542754fe60SDimitry Andric 7552754fe60SDimitry Andric llvm::Value *SizeVal; 7562754fe60SDimitry Andric const VariableArrayType *vla; 757f22ef01cSRoman Divacky 758f22ef01cSRoman Divacky // Don't bother emitting a zero-byte memset. 7593b0f4066SDimitry Andric if (Size.isZero()) { 7602754fe60SDimitry Andric // But note that getTypeInfo returns 0 for a VLA. 7612754fe60SDimitry Andric if (const VariableArrayType *vlaType = 7622754fe60SDimitry Andric dyn_cast_or_null<VariableArrayType>( 7632754fe60SDimitry Andric getContext().getAsArrayType(Ty))) { 76417a519f9SDimitry Andric QualType eltType; 76517a519f9SDimitry Andric llvm::Value *numElts; 76617a519f9SDimitry Andric llvm::tie(numElts, eltType) = getVLASize(vlaType); 76717a519f9SDimitry Andric 76817a519f9SDimitry Andric SizeVal = numElts; 76917a519f9SDimitry Andric CharUnits eltSize = getContext().getTypeSizeInChars(eltType); 77017a519f9SDimitry Andric if (!eltSize.isOne()) 77117a519f9SDimitry Andric SizeVal = Builder.CreateNUWMul(SizeVal, CGM.getSize(eltSize)); 7722754fe60SDimitry Andric vla = vlaType; 7732754fe60SDimitry Andric } else { 774f22ef01cSRoman Divacky return; 7752754fe60SDimitry Andric } 7762754fe60SDimitry Andric } else { 77717a519f9SDimitry Andric SizeVal = CGM.getSize(Size); 7782754fe60SDimitry Andric vla = 0; 7792754fe60SDimitry Andric } 780e580952dSDimitry Andric 781e580952dSDimitry Andric // If the type contains a pointer to data member we can't memset it to zero. 782e580952dSDimitry Andric // Instead, create a null constant and copy it to the destination. 7832754fe60SDimitry Andric // TODO: there are other patterns besides zero that we can usefully memset, 7842754fe60SDimitry Andric // like -1, which happens to be the pattern used by member-pointers. 785e580952dSDimitry Andric if (!CGM.getTypes().isZeroInitializable(Ty)) { 7862754fe60SDimitry Andric // For a VLA, emit a single element, then splat that over the VLA. 7872754fe60SDimitry Andric if (vla) Ty = getContext().getBaseElementType(vla); 7882754fe60SDimitry Andric 789e580952dSDimitry Andric llvm::Constant *NullConstant = CGM.EmitNullConstant(Ty); 790e580952dSDimitry Andric 791e580952dSDimitry Andric llvm::GlobalVariable *NullVariable = 792e580952dSDimitry Andric new llvm::GlobalVariable(CGM.getModule(), NullConstant->getType(), 793e580952dSDimitry Andric /*isConstant=*/true, 794e580952dSDimitry Andric llvm::GlobalVariable::PrivateLinkage, 7956122f3e6SDimitry Andric NullConstant, Twine()); 796e580952dSDimitry Andric llvm::Value *SrcPtr = 797e580952dSDimitry Andric Builder.CreateBitCast(NullVariable, Builder.getInt8PtrTy()); 798e580952dSDimitry Andric 7992754fe60SDimitry Andric if (vla) return emitNonZeroVLAInit(*this, Ty, DestPtr, SrcPtr, SizeVal); 800e580952dSDimitry Andric 801e580952dSDimitry Andric // Get and call the appropriate llvm.memcpy overload. 8023b0f4066SDimitry Andric Builder.CreateMemCpy(DestPtr, SrcPtr, SizeVal, Align.getQuantity(), false); 803e580952dSDimitry Andric return; 804e580952dSDimitry Andric } 805e580952dSDimitry Andric 806e580952dSDimitry Andric // Otherwise, just memset the whole thing to zero. This is legal 807e580952dSDimitry Andric // because in LLVM, all default initializers (other than the ones we just 808e580952dSDimitry Andric // handled above) are guaranteed to have a bit pattern of all zeros. 8093b0f4066SDimitry Andric Builder.CreateMemSet(DestPtr, Builder.getInt8(0), SizeVal, 8103b0f4066SDimitry Andric Align.getQuantity(), false); 811f22ef01cSRoman Divacky } 812f22ef01cSRoman Divacky 8132754fe60SDimitry Andric llvm::BlockAddress *CodeGenFunction::GetAddrOfLabel(const LabelDecl *L) { 814f22ef01cSRoman Divacky // Make sure that there is a block for the indirect goto. 815f22ef01cSRoman Divacky if (IndirectBranch == 0) 816f22ef01cSRoman Divacky GetIndirectGotoBlock(); 817f22ef01cSRoman Divacky 818e580952dSDimitry Andric llvm::BasicBlock *BB = getJumpDestForLabel(L).getBlock(); 819f22ef01cSRoman Divacky 820f22ef01cSRoman Divacky // Make sure the indirect branch includes all of the address-taken blocks. 821f22ef01cSRoman Divacky IndirectBranch->addDestination(BB); 822f22ef01cSRoman Divacky return llvm::BlockAddress::get(CurFn, BB); 823f22ef01cSRoman Divacky } 824f22ef01cSRoman Divacky 825f22ef01cSRoman Divacky llvm::BasicBlock *CodeGenFunction::GetIndirectGotoBlock() { 826f22ef01cSRoman Divacky // If we already made the indirect branch for indirect goto, return its block. 827f22ef01cSRoman Divacky if (IndirectBranch) return IndirectBranch->getParent(); 828f22ef01cSRoman Divacky 829f22ef01cSRoman Divacky CGBuilderTy TmpBuilder(createBasicBlock("indirectgoto")); 830f22ef01cSRoman Divacky 831f22ef01cSRoman Divacky // Create the PHI node that indirect gotos will add entries to. 8323b0f4066SDimitry Andric llvm::Value *DestVal = TmpBuilder.CreatePHI(Int8PtrTy, 0, 8333b0f4066SDimitry Andric "indirect.goto.dest"); 834f22ef01cSRoman Divacky 835f22ef01cSRoman Divacky // Create the indirect branch instruction. 836f22ef01cSRoman Divacky IndirectBranch = TmpBuilder.CreateIndirectBr(DestVal); 837f22ef01cSRoman Divacky return IndirectBranch->getParent(); 838f22ef01cSRoman Divacky } 839f22ef01cSRoman Divacky 84017a519f9SDimitry Andric /// Computes the length of an array in elements, as well as the base 84117a519f9SDimitry Andric /// element type and a properly-typed first element pointer. 84217a519f9SDimitry Andric llvm::Value *CodeGenFunction::emitArrayLength(const ArrayType *origArrayType, 84317a519f9SDimitry Andric QualType &baseType, 84417a519f9SDimitry Andric llvm::Value *&addr) { 84517a519f9SDimitry Andric const ArrayType *arrayType = origArrayType; 846f22ef01cSRoman Divacky 84717a519f9SDimitry Andric // If it's a VLA, we have to load the stored size. Note that 84817a519f9SDimitry Andric // this is the size of the VLA in bytes, not its size in elements. 84917a519f9SDimitry Andric llvm::Value *numVLAElements = 0; 85017a519f9SDimitry Andric if (isa<VariableArrayType>(arrayType)) { 85117a519f9SDimitry Andric numVLAElements = getVLASize(cast<VariableArrayType>(arrayType)).first; 85217a519f9SDimitry Andric 85317a519f9SDimitry Andric // Walk into all VLAs. This doesn't require changes to addr, 85417a519f9SDimitry Andric // which has type T* where T is the first non-VLA element type. 85517a519f9SDimitry Andric do { 85617a519f9SDimitry Andric QualType elementType = arrayType->getElementType(); 85717a519f9SDimitry Andric arrayType = getContext().getAsArrayType(elementType); 85817a519f9SDimitry Andric 85917a519f9SDimitry Andric // If we only have VLA components, 'addr' requires no adjustment. 86017a519f9SDimitry Andric if (!arrayType) { 86117a519f9SDimitry Andric baseType = elementType; 86217a519f9SDimitry Andric return numVLAElements; 86317a519f9SDimitry Andric } 86417a519f9SDimitry Andric } while (isa<VariableArrayType>(arrayType)); 86517a519f9SDimitry Andric 86617a519f9SDimitry Andric // We get out here only if we find a constant array type 86717a519f9SDimitry Andric // inside the VLA. 868f22ef01cSRoman Divacky } 869f22ef01cSRoman Divacky 87017a519f9SDimitry Andric // We have some number of constant-length arrays, so addr should 87117a519f9SDimitry Andric // have LLVM type [M x [N x [...]]]*. Build a GEP that walks 87217a519f9SDimitry Andric // down to the first element of addr. 8736122f3e6SDimitry Andric SmallVector<llvm::Value*, 8> gepIndices; 87417a519f9SDimitry Andric 87517a519f9SDimitry Andric // GEP down to the array type. 87617a519f9SDimitry Andric llvm::ConstantInt *zero = Builder.getInt32(0); 87717a519f9SDimitry Andric gepIndices.push_back(zero); 87817a519f9SDimitry Andric 87917a519f9SDimitry Andric // It's more efficient to calculate the count from the LLVM 88017a519f9SDimitry Andric // constant-length arrays than to re-evaluate the array bounds. 88117a519f9SDimitry Andric uint64_t countFromCLAs = 1; 88217a519f9SDimitry Andric 8836122f3e6SDimitry Andric llvm::ArrayType *llvmArrayType = 88417a519f9SDimitry Andric cast<llvm::ArrayType>( 88517a519f9SDimitry Andric cast<llvm::PointerType>(addr->getType())->getElementType()); 88617a519f9SDimitry Andric while (true) { 88717a519f9SDimitry Andric assert(isa<ConstantArrayType>(arrayType)); 88817a519f9SDimitry Andric assert(cast<ConstantArrayType>(arrayType)->getSize().getZExtValue() 88917a519f9SDimitry Andric == llvmArrayType->getNumElements()); 89017a519f9SDimitry Andric 89117a519f9SDimitry Andric gepIndices.push_back(zero); 89217a519f9SDimitry Andric countFromCLAs *= llvmArrayType->getNumElements(); 89317a519f9SDimitry Andric 89417a519f9SDimitry Andric llvmArrayType = 89517a519f9SDimitry Andric dyn_cast<llvm::ArrayType>(llvmArrayType->getElementType()); 89617a519f9SDimitry Andric if (!llvmArrayType) break; 89717a519f9SDimitry Andric 89817a519f9SDimitry Andric arrayType = getContext().getAsArrayType(arrayType->getElementType()); 89917a519f9SDimitry Andric assert(arrayType && "LLVM and Clang types are out-of-synch"); 90017a519f9SDimitry Andric } 90117a519f9SDimitry Andric 90217a519f9SDimitry Andric baseType = arrayType->getElementType(); 90317a519f9SDimitry Andric 90417a519f9SDimitry Andric // Create the actual GEP. 9056122f3e6SDimitry Andric addr = Builder.CreateInBoundsGEP(addr, gepIndices, "array.begin"); 90617a519f9SDimitry Andric 90717a519f9SDimitry Andric llvm::Value *numElements 90817a519f9SDimitry Andric = llvm::ConstantInt::get(SizeTy, countFromCLAs); 90917a519f9SDimitry Andric 91017a519f9SDimitry Andric // If we had any VLA dimensions, factor them in. 91117a519f9SDimitry Andric if (numVLAElements) 91217a519f9SDimitry Andric numElements = Builder.CreateNUWMul(numVLAElements, numElements); 91317a519f9SDimitry Andric 91417a519f9SDimitry Andric return numElements; 91517a519f9SDimitry Andric } 91617a519f9SDimitry Andric 91717a519f9SDimitry Andric std::pair<llvm::Value*, QualType> 91817a519f9SDimitry Andric CodeGenFunction::getVLASize(QualType type) { 91917a519f9SDimitry Andric const VariableArrayType *vla = getContext().getAsVariableArrayType(type); 92017a519f9SDimitry Andric assert(vla && "type was not a variable array type!"); 92117a519f9SDimitry Andric return getVLASize(vla); 92217a519f9SDimitry Andric } 92317a519f9SDimitry Andric 92417a519f9SDimitry Andric std::pair<llvm::Value*, QualType> 92517a519f9SDimitry Andric CodeGenFunction::getVLASize(const VariableArrayType *type) { 92617a519f9SDimitry Andric // The number of elements so far; always size_t. 92717a519f9SDimitry Andric llvm::Value *numElements = 0; 92817a519f9SDimitry Andric 92917a519f9SDimitry Andric QualType elementType; 93017a519f9SDimitry Andric do { 93117a519f9SDimitry Andric elementType = type->getElementType(); 93217a519f9SDimitry Andric llvm::Value *vlaSize = VLASizeMap[type->getSizeExpr()]; 93317a519f9SDimitry Andric assert(vlaSize && "no size for VLA!"); 93417a519f9SDimitry Andric assert(vlaSize->getType() == SizeTy); 93517a519f9SDimitry Andric 93617a519f9SDimitry Andric if (!numElements) { 93717a519f9SDimitry Andric numElements = vlaSize; 93817a519f9SDimitry Andric } else { 93917a519f9SDimitry Andric // It's undefined behavior if this wraps around, so mark it that way. 94017a519f9SDimitry Andric numElements = Builder.CreateNUWMul(numElements, vlaSize); 94117a519f9SDimitry Andric } 94217a519f9SDimitry Andric } while ((type = getContext().getAsVariableArrayType(elementType))); 94317a519f9SDimitry Andric 94417a519f9SDimitry Andric return std::pair<llvm::Value*,QualType>(numElements, elementType); 94517a519f9SDimitry Andric } 94617a519f9SDimitry Andric 94717a519f9SDimitry Andric void CodeGenFunction::EmitVariablyModifiedType(QualType type) { 94817a519f9SDimitry Andric assert(type->isVariablyModifiedType() && 949f22ef01cSRoman Divacky "Must pass variably modified type to EmitVLASizes!"); 950f22ef01cSRoman Divacky 951f22ef01cSRoman Divacky EnsureInsertPoint(); 952f22ef01cSRoman Divacky 95317a519f9SDimitry Andric // We're going to walk down into the type and look for VLA 95417a519f9SDimitry Andric // expressions. 95517a519f9SDimitry Andric do { 95617a519f9SDimitry Andric assert(type->isVariablyModifiedType()); 957f22ef01cSRoman Divacky 95817a519f9SDimitry Andric const Type *ty = type.getTypePtr(); 95917a519f9SDimitry Andric switch (ty->getTypeClass()) { 960dff0c46cSDimitry Andric 96117a519f9SDimitry Andric #define TYPE(Class, Base) 96217a519f9SDimitry Andric #define ABSTRACT_TYPE(Class, Base) 963dff0c46cSDimitry Andric #define NON_CANONICAL_TYPE(Class, Base) 96417a519f9SDimitry Andric #define DEPENDENT_TYPE(Class, Base) case Type::Class: 965dff0c46cSDimitry Andric #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) 96617a519f9SDimitry Andric #include "clang/AST/TypeNodes.def" 967dff0c46cSDimitry Andric llvm_unreachable("unexpected dependent type!"); 968f22ef01cSRoman Divacky 96917a519f9SDimitry Andric // These types are never variably-modified. 97017a519f9SDimitry Andric case Type::Builtin: 97117a519f9SDimitry Andric case Type::Complex: 97217a519f9SDimitry Andric case Type::Vector: 97317a519f9SDimitry Andric case Type::ExtVector: 97417a519f9SDimitry Andric case Type::Record: 97517a519f9SDimitry Andric case Type::Enum: 976dff0c46cSDimitry Andric case Type::Elaborated: 977dff0c46cSDimitry Andric case Type::TemplateSpecialization: 97817a519f9SDimitry Andric case Type::ObjCObject: 97917a519f9SDimitry Andric case Type::ObjCInterface: 98017a519f9SDimitry Andric case Type::ObjCObjectPointer: 98117a519f9SDimitry Andric llvm_unreachable("type class is never variably-modified!"); 982f22ef01cSRoman Divacky 98317a519f9SDimitry Andric case Type::Pointer: 98417a519f9SDimitry Andric type = cast<PointerType>(ty)->getPointeeType(); 98517a519f9SDimitry Andric break; 986f22ef01cSRoman Divacky 98717a519f9SDimitry Andric case Type::BlockPointer: 98817a519f9SDimitry Andric type = cast<BlockPointerType>(ty)->getPointeeType(); 98917a519f9SDimitry Andric break; 99017a519f9SDimitry Andric 99117a519f9SDimitry Andric case Type::LValueReference: 99217a519f9SDimitry Andric case Type::RValueReference: 99317a519f9SDimitry Andric type = cast<ReferenceType>(ty)->getPointeeType(); 99417a519f9SDimitry Andric break; 99517a519f9SDimitry Andric 99617a519f9SDimitry Andric case Type::MemberPointer: 99717a519f9SDimitry Andric type = cast<MemberPointerType>(ty)->getPointeeType(); 99817a519f9SDimitry Andric break; 99917a519f9SDimitry Andric 100017a519f9SDimitry Andric case Type::ConstantArray: 100117a519f9SDimitry Andric case Type::IncompleteArray: 100217a519f9SDimitry Andric // Losing element qualification here is fine. 100317a519f9SDimitry Andric type = cast<ArrayType>(ty)->getElementType(); 100417a519f9SDimitry Andric break; 100517a519f9SDimitry Andric 100617a519f9SDimitry Andric case Type::VariableArray: { 100717a519f9SDimitry Andric // Losing element qualification here is fine. 100817a519f9SDimitry Andric const VariableArrayType *vat = cast<VariableArrayType>(ty); 100917a519f9SDimitry Andric 101017a519f9SDimitry Andric // Unknown size indication requires no size computation. 101117a519f9SDimitry Andric // Otherwise, evaluate and record it. 101217a519f9SDimitry Andric if (const Expr *size = vat->getSizeExpr()) { 101317a519f9SDimitry Andric // It's possible that we might have emitted this already, 101417a519f9SDimitry Andric // e.g. with a typedef and a pointer to it. 101517a519f9SDimitry Andric llvm::Value *&entry = VLASizeMap[size]; 101617a519f9SDimitry Andric if (!entry) { 101717a519f9SDimitry Andric // Always zexting here would be wrong if it weren't 101817a519f9SDimitry Andric // undefined behavior to have a negative bound. 101917a519f9SDimitry Andric entry = Builder.CreateIntCast(EmitScalarExpr(size), SizeTy, 102017a519f9SDimitry Andric /*signed*/ false); 102117a519f9SDimitry Andric } 102217a519f9SDimitry Andric } 102317a519f9SDimitry Andric type = vat->getElementType(); 102417a519f9SDimitry Andric break; 1025f22ef01cSRoman Divacky } 1026f22ef01cSRoman Divacky 102717a519f9SDimitry Andric case Type::FunctionProto: 102817a519f9SDimitry Andric case Type::FunctionNoProto: 102917a519f9SDimitry Andric type = cast<FunctionType>(ty)->getResultType(); 103017a519f9SDimitry Andric break; 10316122f3e6SDimitry Andric 1032dff0c46cSDimitry Andric case Type::Paren: 1033dff0c46cSDimitry Andric case Type::TypeOf: 1034dff0c46cSDimitry Andric case Type::UnaryTransform: 1035dff0c46cSDimitry Andric case Type::Attributed: 1036dff0c46cSDimitry Andric case Type::SubstTemplateTypeParm: 1037dff0c46cSDimitry Andric // Keep walking after single level desugaring. 1038dff0c46cSDimitry Andric type = type.getSingleStepDesugaredType(getContext()); 1039dff0c46cSDimitry Andric break; 1040dff0c46cSDimitry Andric 1041dff0c46cSDimitry Andric case Type::Typedef: 1042dff0c46cSDimitry Andric case Type::Decltype: 1043dff0c46cSDimitry Andric case Type::Auto: 1044dff0c46cSDimitry Andric // Stop walking: nothing to do. 1045dff0c46cSDimitry Andric return; 1046dff0c46cSDimitry Andric 1047dff0c46cSDimitry Andric case Type::TypeOfExpr: 1048dff0c46cSDimitry Andric // Stop walking: emit typeof expression. 1049dff0c46cSDimitry Andric EmitIgnoredExpr(cast<TypeOfExprType>(ty)->getUnderlyingExpr()); 1050dff0c46cSDimitry Andric return; 1051dff0c46cSDimitry Andric 10526122f3e6SDimitry Andric case Type::Atomic: 10536122f3e6SDimitry Andric type = cast<AtomicType>(ty)->getValueType(); 10546122f3e6SDimitry Andric break; 1055f22ef01cSRoman Divacky } 105617a519f9SDimitry Andric } while (type->isVariablyModifiedType()); 1057f22ef01cSRoman Divacky } 1058f22ef01cSRoman Divacky 1059f22ef01cSRoman Divacky llvm::Value* CodeGenFunction::EmitVAListRef(const Expr* E) { 10602754fe60SDimitry Andric if (getContext().getBuiltinVaListType()->isArrayType()) 1061f22ef01cSRoman Divacky return EmitScalarExpr(E); 1062f22ef01cSRoman Divacky return EmitLValue(E).getAddress(); 1063f22ef01cSRoman Divacky } 1064f22ef01cSRoman Divacky 1065e580952dSDimitry Andric void CodeGenFunction::EmitDeclRefExprDbgValue(const DeclRefExpr *E, 10662754fe60SDimitry Andric llvm::Constant *Init) { 1067e580952dSDimitry Andric assert (Init && "Invalid DeclRefExpr initializer!"); 1068e580952dSDimitry Andric if (CGDebugInfo *Dbg = getDebugInfo()) 10692754fe60SDimitry Andric Dbg->EmitGlobalVariable(E->getDecl(), Init); 10702754fe60SDimitry Andric } 10712754fe60SDimitry Andric 10722754fe60SDimitry Andric CodeGenFunction::PeepholeProtection 10732754fe60SDimitry Andric CodeGenFunction::protectFromPeepholes(RValue rvalue) { 10742754fe60SDimitry Andric // At the moment, the only aggressive peephole we do in IR gen 10752754fe60SDimitry Andric // is trunc(zext) folding, but if we add more, we can easily 10762754fe60SDimitry Andric // extend this protection. 10772754fe60SDimitry Andric 10782754fe60SDimitry Andric if (!rvalue.isScalar()) return PeepholeProtection(); 10792754fe60SDimitry Andric llvm::Value *value = rvalue.getScalarVal(); 10802754fe60SDimitry Andric if (!isa<llvm::ZExtInst>(value)) return PeepholeProtection(); 10812754fe60SDimitry Andric 10822754fe60SDimitry Andric // Just make an extra bitcast. 10832754fe60SDimitry Andric assert(HaveInsertPoint()); 10842754fe60SDimitry Andric llvm::Instruction *inst = new llvm::BitCastInst(value, value->getType(), "", 10852754fe60SDimitry Andric Builder.GetInsertBlock()); 10862754fe60SDimitry Andric 10872754fe60SDimitry Andric PeepholeProtection protection; 10882754fe60SDimitry Andric protection.Inst = inst; 10892754fe60SDimitry Andric return protection; 10902754fe60SDimitry Andric } 10912754fe60SDimitry Andric 10922754fe60SDimitry Andric void CodeGenFunction::unprotectFromPeepholes(PeepholeProtection protection) { 10932754fe60SDimitry Andric if (!protection.Inst) return; 10942754fe60SDimitry Andric 10952754fe60SDimitry Andric // In theory, we could try to duplicate the peepholes now, but whatever. 10962754fe60SDimitry Andric protection.Inst->eraseFromParent(); 1097e580952dSDimitry Andric } 10986122f3e6SDimitry Andric 10996122f3e6SDimitry Andric llvm::Value *CodeGenFunction::EmitAnnotationCall(llvm::Value *AnnotationFn, 11006122f3e6SDimitry Andric llvm::Value *AnnotatedVal, 11016122f3e6SDimitry Andric llvm::StringRef AnnotationStr, 11026122f3e6SDimitry Andric SourceLocation Location) { 11036122f3e6SDimitry Andric llvm::Value *Args[4] = { 11046122f3e6SDimitry Andric AnnotatedVal, 11056122f3e6SDimitry Andric Builder.CreateBitCast(CGM.EmitAnnotationString(AnnotationStr), Int8PtrTy), 11066122f3e6SDimitry Andric Builder.CreateBitCast(CGM.EmitAnnotationUnit(Location), Int8PtrTy), 11076122f3e6SDimitry Andric CGM.EmitAnnotationLineNo(Location) 11086122f3e6SDimitry Andric }; 11096122f3e6SDimitry Andric return Builder.CreateCall(AnnotationFn, Args); 11106122f3e6SDimitry Andric } 11116122f3e6SDimitry Andric 11126122f3e6SDimitry Andric void CodeGenFunction::EmitVarAnnotations(const VarDecl *D, llvm::Value *V) { 11136122f3e6SDimitry Andric assert(D->hasAttr<AnnotateAttr>() && "no annotate attribute"); 11146122f3e6SDimitry Andric // FIXME We create a new bitcast for every annotation because that's what 11156122f3e6SDimitry Andric // llvm-gcc was doing. 11166122f3e6SDimitry Andric for (specific_attr_iterator<AnnotateAttr> 11176122f3e6SDimitry Andric ai = D->specific_attr_begin<AnnotateAttr>(), 11186122f3e6SDimitry Andric ae = D->specific_attr_end<AnnotateAttr>(); ai != ae; ++ai) 11196122f3e6SDimitry Andric EmitAnnotationCall(CGM.getIntrinsic(llvm::Intrinsic::var_annotation), 11206122f3e6SDimitry Andric Builder.CreateBitCast(V, CGM.Int8PtrTy, V->getName()), 11216122f3e6SDimitry Andric (*ai)->getAnnotation(), D->getLocation()); 11226122f3e6SDimitry Andric } 11236122f3e6SDimitry Andric 11246122f3e6SDimitry Andric llvm::Value *CodeGenFunction::EmitFieldAnnotations(const FieldDecl *D, 11256122f3e6SDimitry Andric llvm::Value *V) { 11266122f3e6SDimitry Andric assert(D->hasAttr<AnnotateAttr>() && "no annotate attribute"); 11276122f3e6SDimitry Andric llvm::Type *VTy = V->getType(); 11286122f3e6SDimitry Andric llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::ptr_annotation, 11296122f3e6SDimitry Andric CGM.Int8PtrTy); 11306122f3e6SDimitry Andric 11316122f3e6SDimitry Andric for (specific_attr_iterator<AnnotateAttr> 11326122f3e6SDimitry Andric ai = D->specific_attr_begin<AnnotateAttr>(), 11336122f3e6SDimitry Andric ae = D->specific_attr_end<AnnotateAttr>(); ai != ae; ++ai) { 11346122f3e6SDimitry Andric // FIXME Always emit the cast inst so we can differentiate between 11356122f3e6SDimitry Andric // annotation on the first field of a struct and annotation on the struct 11366122f3e6SDimitry Andric // itself. 11376122f3e6SDimitry Andric if (VTy != CGM.Int8PtrTy) 11386122f3e6SDimitry Andric V = Builder.Insert(new llvm::BitCastInst(V, CGM.Int8PtrTy)); 11396122f3e6SDimitry Andric V = EmitAnnotationCall(F, V, (*ai)->getAnnotation(), D->getLocation()); 11406122f3e6SDimitry Andric V = Builder.CreateBitCast(V, VTy); 11416122f3e6SDimitry Andric } 11426122f3e6SDimitry Andric 11436122f3e6SDimitry Andric return V; 11446122f3e6SDimitry Andric } 1145