Home | History | Annotate | Download | only in CodeGen
      1 //===--- CodeGenFunction.cpp - Emit LLVM Code from ASTs for a Function ----===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This coordinates the per-function state used while generating code.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #include "CodeGenFunction.h"
     15 #include "CGBlocks.h"
     16 #include "CGCleanup.h"
     17 #include "CGCUDARuntime.h"
     18 #include "CGCXXABI.h"
     19 #include "CGDebugInfo.h"
     20 #include "CGOpenMPRuntime.h"
     21 #include "CodeGenModule.h"
     22 #include "CodeGenPGO.h"
     23 #include "TargetInfo.h"
     24 #include "clang/AST/ASTContext.h"
     25 #include "clang/AST/Decl.h"
     26 #include "clang/AST/DeclCXX.h"
     27 #include "clang/AST/StmtCXX.h"
     28 #include "clang/Basic/Builtins.h"
     29 #include "clang/Basic/TargetInfo.h"
     30 #include "clang/CodeGen/CGFunctionInfo.h"
     31 #include "clang/Frontend/CodeGenOptions.h"
     32 #include "clang/Sema/SemaDiagnostic.h"
     33 #include "llvm/IR/DataLayout.h"
     34 #include "llvm/IR/Intrinsics.h"
     35 #include "llvm/IR/MDBuilder.h"
     36 #include "llvm/IR/Operator.h"
     37 using namespace clang;
     38 using namespace CodeGen;
     39 
     40 CodeGenFunction::CodeGenFunction(CodeGenModule &cgm, bool suppressNewContext)
     41     : CodeGenTypeCache(cgm), CGM(cgm), Target(cgm.getTarget()),
     42       Builder(cgm, cgm.getModule().getContext(), llvm::ConstantFolder(),
     43               CGBuilderInserterTy(this)),
     44       CurFn(nullptr), ReturnValue(Address::invalid()),
     45       CapturedStmtInfo(nullptr),
     46       SanOpts(CGM.getLangOpts().Sanitize), IsSanitizerScope(false),
     47       CurFuncIsThunk(false), AutoreleaseResult(false), SawAsmBlock(false),
     48       IsOutlinedSEHHelper(false),
     49       BlockInfo(nullptr), BlockPointer(nullptr),
     50       LambdaThisCaptureField(nullptr), NormalCleanupDest(nullptr),
     51       NextCleanupDestIndex(1), FirstBlockInfo(nullptr), EHResumeBlock(nullptr),
     52       ExceptionSlot(nullptr), EHSelectorSlot(nullptr),
     53       DebugInfo(CGM.getModuleDebugInfo()),
     54       DisableDebugInfo(false), DidCallStackSave(false), IndirectBranch(nullptr),
     55       PGO(cgm), SwitchInsn(nullptr), SwitchWeights(nullptr),
     56       CaseRangeBlock(nullptr), UnreachableBlock(nullptr), NumReturnExprs(0),
     57       NumSimpleReturnExprs(0), CXXABIThisDecl(nullptr),
     58       CXXABIThisValue(nullptr), CXXThisValue(nullptr),
     59       CXXStructorImplicitParamDecl(nullptr),
     60       CXXStructorImplicitParamValue(nullptr), OutermostConditional(nullptr),
     61       CurLexicalScope(nullptr), TerminateLandingPad(nullptr),
     62       TerminateHandler(nullptr), TrapBB(nullptr) {
     63   if (!suppressNewContext)
     64     CGM.getCXXABI().getMangleContext().startNewFunction();
     65 
     66   llvm::FastMathFlags FMF;
     67   if (CGM.getLangOpts().FastMath)
     68     FMF.setUnsafeAlgebra();
     69   if (CGM.getLangOpts().FiniteMathOnly) {
     70     FMF.setNoNaNs();
     71     FMF.setNoInfs();
     72   }
     73   if (CGM.getCodeGenOpts().NoNaNsFPMath) {
     74     FMF.setNoNaNs();
     75   }
     76   if (CGM.getCodeGenOpts().NoSignedZeros) {
     77     FMF.setNoSignedZeros();
     78   }
     79   if (CGM.getCodeGenOpts().ReciprocalMath) {
     80     FMF.setAllowReciprocal();
     81   }
     82   Builder.setFastMathFlags(FMF);
     83 }
     84 
     85 CodeGenFunction::~CodeGenFunction() {
     86   assert(LifetimeExtendedCleanupStack.empty() && "failed to emit a cleanup");
     87 
     88   // If there are any unclaimed block infos, go ahead and destroy them
     89   // now.  This can happen if IR-gen gets clever and skips evaluating
     90   // something.
     91   if (FirstBlockInfo)
     92     destroyBlockInfos(FirstBlockInfo);
     93 
     94   if (getLangOpts().OpenMP) {
     95     CGM.getOpenMPRuntime().functionFinished(*this);
     96   }
     97 }
     98 
     99 CharUnits CodeGenFunction::getNaturalPointeeTypeAlignment(QualType T,
    100                                                      AlignmentSource *Source) {
    101   return getNaturalTypeAlignment(T->getPointeeType(), Source,
    102                                  /*forPointee*/ true);
    103 }
    104 
    105 CharUnits CodeGenFunction::getNaturalTypeAlignment(QualType T,
    106                                                    AlignmentSource *Source,
    107                                                    bool forPointeeType) {
    108   // Honor alignment typedef attributes even on incomplete types.
    109   // We also honor them straight for C++ class types, even as pointees;
    110   // there's an expressivity gap here.
    111   if (auto TT = T->getAs<TypedefType>()) {
    112     if (auto Align = TT->getDecl()->getMaxAlignment()) {
    113       if (Source) *Source = AlignmentSource::AttributedType;
    114       return getContext().toCharUnitsFromBits(Align);
    115     }
    116   }
    117 
    118   if (Source) *Source = AlignmentSource::Type;
    119 
    120   CharUnits Alignment;
    121   if (T->isIncompleteType()) {
    122     Alignment = CharUnits::One(); // Shouldn't be used, but pessimistic is best.
    123   } else {
    124     // For C++ class pointees, we don't know whether we're pointing at a
    125     // base or a complete object, so we generally need to use the
    126     // non-virtual alignment.
    127     const CXXRecordDecl *RD;
    128     if (forPointeeType && (RD = T->getAsCXXRecordDecl())) {
    129       Alignment = CGM.getClassPointerAlignment(RD);
    130     } else {
    131       Alignment = getContext().getTypeAlignInChars(T);
    132     }
    133 
    134     // Cap to the global maximum type alignment unless the alignment
    135     // was somehow explicit on the type.
    136     if (unsigned MaxAlign = getLangOpts().MaxTypeAlign) {
    137       if (Alignment.getQuantity() > MaxAlign &&
    138           !getContext().isAlignmentRequired(T))
    139         Alignment = CharUnits::fromQuantity(MaxAlign);
    140     }
    141   }
    142   return Alignment;
    143 }
    144 
    145 LValue CodeGenFunction::MakeNaturalAlignAddrLValue(llvm::Value *V, QualType T) {
    146   AlignmentSource AlignSource;
    147   CharUnits Alignment = getNaturalTypeAlignment(T, &AlignSource);
    148   return LValue::MakeAddr(Address(V, Alignment), T, getContext(), AlignSource,
    149                           CGM.getTBAAInfo(T));
    150 }
    151 
    152 /// Given a value of type T* that may not be to a complete object,
    153 /// construct an l-value with the natural pointee alignment of T.
    154 LValue
    155 CodeGenFunction::MakeNaturalAlignPointeeAddrLValue(llvm::Value *V, QualType T) {
    156   AlignmentSource AlignSource;
    157   CharUnits Align = getNaturalTypeAlignment(T, &AlignSource, /*pointee*/ true);
    158   return MakeAddrLValue(Address(V, Align), T, AlignSource);
    159 }
    160 
    161 
    162 llvm::Type *CodeGenFunction::ConvertTypeForMem(QualType T) {
    163   return CGM.getTypes().ConvertTypeForMem(T);
    164 }
    165 
    166 llvm::Type *CodeGenFunction::ConvertType(QualType T) {
    167   return CGM.getTypes().ConvertType(T);
    168 }
    169 
    170 TypeEvaluationKind CodeGenFunction::getEvaluationKind(QualType type) {
    171   type = type.getCanonicalType();
    172   while (true) {
    173     switch (type->getTypeClass()) {
    174 #define TYPE(name, parent)
    175 #define ABSTRACT_TYPE(name, parent)
    176 #define NON_CANONICAL_TYPE(name, parent) case Type::name:
    177 #define DEPENDENT_TYPE(name, parent) case Type::name:
    178 #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(name, parent) case Type::name:
    179 #include "clang/AST/TypeNodes.def"
    180       llvm_unreachable("non-canonical or dependent type in IR-generation");
    181 
    182     case Type::Auto:
    183       llvm_unreachable("undeduced auto type in IR-generation");
    184 
    185     // Various scalar types.
    186     case Type::Builtin:
    187     case Type::Pointer:
    188     case Type::BlockPointer:
    189     case Type::LValueReference:
    190     case Type::RValueReference:
    191     case Type::MemberPointer:
    192     case Type::Vector:
    193     case Type::ExtVector:
    194     case Type::FunctionProto:
    195     case Type::FunctionNoProto:
    196     case Type::Enum:
    197     case Type::ObjCObjectPointer:
    198     case Type::Pipe:
    199       return TEK_Scalar;
    200 
    201     // Complexes.
    202     case Type::Complex:
    203       return TEK_Complex;
    204 
    205     // Arrays, records, and Objective-C objects.
    206     case Type::ConstantArray:
    207     case Type::IncompleteArray:
    208     case Type::VariableArray:
    209     case Type::Record:
    210     case Type::ObjCObject:
    211     case Type::ObjCInterface:
    212       return TEK_Aggregate;
    213 
    214     // We operate on atomic values according to their underlying type.
    215     case Type::Atomic:
    216       type = cast<AtomicType>(type)->getValueType();
    217       continue;
    218     }
    219     llvm_unreachable("unknown type kind!");
    220   }
    221 }
    222 
    223 llvm::DebugLoc CodeGenFunction::EmitReturnBlock() {
    224   // For cleanliness, we try to avoid emitting the return block for
    225   // simple cases.
    226   llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
    227 
    228   if (CurBB) {
    229     assert(!CurBB->getTerminator() && "Unexpected terminated block.");
    230 
    231     // We have a valid insert point, reuse it if it is empty or there are no
    232     // explicit jumps to the return block.
    233     if (CurBB->empty() || ReturnBlock.getBlock()->use_empty()) {
    234       ReturnBlock.getBlock()->replaceAllUsesWith(CurBB);
    235       delete ReturnBlock.getBlock();
    236     } else
    237       EmitBlock(ReturnBlock.getBlock());
    238     return llvm::DebugLoc();
    239   }
    240 
    241   // Otherwise, if the return block is the target of a single direct
    242   // branch then we can just put the code in that block instead. This
    243   // cleans up functions which started with a unified return block.
    244   if (ReturnBlock.getBlock()->hasOneUse()) {
    245     llvm::BranchInst *BI =
    246       dyn_cast<llvm::BranchInst>(*ReturnBlock.getBlock()->user_begin());
    247     if (BI && BI->isUnconditional() &&
    248         BI->getSuccessor(0) == ReturnBlock.getBlock()) {
    249       // Record/return the DebugLoc of the simple 'return' expression to be used
    250       // later by the actual 'ret' instruction.
    251       llvm::DebugLoc Loc = BI->getDebugLoc();
    252       Builder.SetInsertPoint(BI->getParent());
    253       BI->eraseFromParent();
    254       delete ReturnBlock.getBlock();
    255       return Loc;
    256     }
    257   }
    258 
    259   // FIXME: We are at an unreachable point, there is no reason to emit the block
    260   // unless it has uses. However, we still need a place to put the debug
    261   // region.end for now.
    262 
    263   EmitBlock(ReturnBlock.getBlock());
    264   return llvm::DebugLoc();
    265 }
    266 
    267 static void EmitIfUsed(CodeGenFunction &CGF, llvm::BasicBlock *BB) {
    268   if (!BB) return;
    269   if (!BB->use_empty())
    270     return CGF.CurFn->getBasicBlockList().push_back(BB);
    271   delete BB;
    272 }
    273 
    274 void CodeGenFunction::FinishFunction(SourceLocation EndLoc) {
    275   assert(BreakContinueStack.empty() &&
    276          "mismatched push/pop in break/continue stack!");
    277 
    278   bool OnlySimpleReturnStmts = NumSimpleReturnExprs > 0
    279     && NumSimpleReturnExprs == NumReturnExprs
    280     && ReturnBlock.getBlock()->use_empty();
    281   // Usually the return expression is evaluated before the cleanup
    282   // code.  If the function contains only a simple return statement,
    283   // such as a constant, the location before the cleanup code becomes
    284   // the last useful breakpoint in the function, because the simple
    285   // return expression will be evaluated after the cleanup code. To be
    286   // safe, set the debug location for cleanup code to the location of
    287   // the return statement.  Otherwise the cleanup code should be at the
    288   // end of the function's lexical scope.
    289   //
    290   // If there are multiple branches to the return block, the branch
    291   // instructions will get the location of the return statements and
    292   // all will be fine.
    293   if (CGDebugInfo *DI = getDebugInfo()) {
    294     if (OnlySimpleReturnStmts)
    295       DI->EmitLocation(Builder, LastStopPoint);
    296     else
    297       DI->EmitLocation(Builder, EndLoc);
    298   }
    299 
    300   // Pop any cleanups that might have been associated with the
    301   // parameters.  Do this in whatever block we're currently in; it's
    302   // important to do this before we enter the return block or return
    303   // edges will be *really* confused.
    304   bool HasCleanups = EHStack.stable_begin() != PrologueCleanupDepth;
    305   bool HasOnlyLifetimeMarkers =
    306       HasCleanups && EHStack.containsOnlyLifetimeMarkers(PrologueCleanupDepth);
    307   bool EmitRetDbgLoc = !HasCleanups || HasOnlyLifetimeMarkers;
    308   if (HasCleanups) {
    309     // Make sure the line table doesn't jump back into the body for
    310     // the ret after it's been at EndLoc.
    311     if (CGDebugInfo *DI = getDebugInfo())
    312       if (OnlySimpleReturnStmts)
    313         DI->EmitLocation(Builder, EndLoc);
    314 
    315     PopCleanupBlocks(PrologueCleanupDepth);
    316   }
    317 
    318   // Emit function epilog (to return).
    319   llvm::DebugLoc Loc = EmitReturnBlock();
    320 
    321   if (ShouldInstrumentFunction())
    322     EmitFunctionInstrumentation("__cyg_profile_func_exit");
    323 
    324   // Emit debug descriptor for function end.
    325   if (CGDebugInfo *DI = getDebugInfo())
    326     DI->EmitFunctionEnd(Builder);
    327 
    328   // Reset the debug location to that of the simple 'return' expression, if any
    329   // rather than that of the end of the function's scope '}'.
    330   ApplyDebugLocation AL(*this, Loc);
    331   EmitFunctionEpilog(*CurFnInfo, EmitRetDbgLoc, EndLoc);
    332   EmitEndEHSpec(CurCodeDecl);
    333 
    334   assert(EHStack.empty() &&
    335          "did not remove all scopes from cleanup stack!");
    336 
    337   // If someone did an indirect goto, emit the indirect goto block at the end of
    338   // the function.
    339   if (IndirectBranch) {
    340     EmitBlock(IndirectBranch->getParent());
    341     Builder.ClearInsertionPoint();
    342   }
    343 
    344   // If some of our locals escaped, insert a call to llvm.localescape in the
    345   // entry block.
    346   if (!EscapedLocals.empty()) {
    347     // Invert the map from local to index into a simple vector. There should be
    348     // no holes.
    349     SmallVector<llvm::Value *, 4> EscapeArgs;
    350     EscapeArgs.resize(EscapedLocals.size());
    351     for (auto &Pair : EscapedLocals)
    352       EscapeArgs[Pair.second] = Pair.first;
    353     llvm::Function *FrameEscapeFn = llvm::Intrinsic::getDeclaration(
    354         &CGM.getModule(), llvm::Intrinsic::localescape);
    355     CGBuilderTy(*this, AllocaInsertPt).CreateCall(FrameEscapeFn, EscapeArgs);
    356   }
    357 
    358   // Remove the AllocaInsertPt instruction, which is just a convenience for us.
    359   llvm::Instruction *Ptr = AllocaInsertPt;
    360   AllocaInsertPt = nullptr;
    361   Ptr->eraseFromParent();
    362 
    363   // If someone took the address of a label but never did an indirect goto, we
    364   // made a zero entry PHI node, which is illegal, zap it now.
    365   if (IndirectBranch) {
    366     llvm::PHINode *PN = cast<llvm::PHINode>(IndirectBranch->getAddress());
    367     if (PN->getNumIncomingValues() == 0) {
    368       PN->replaceAllUsesWith(llvm::UndefValue::get(PN->getType()));
    369       PN->eraseFromParent();
    370     }
    371   }
    372 
    373   EmitIfUsed(*this, EHResumeBlock);
    374   EmitIfUsed(*this, TerminateLandingPad);
    375   EmitIfUsed(*this, TerminateHandler);
    376   EmitIfUsed(*this, UnreachableBlock);
    377 
    378   if (CGM.getCodeGenOpts().EmitDeclMetadata)
    379     EmitDeclMetadata();
    380 
    381   for (SmallVectorImpl<std::pair<llvm::Instruction *, llvm::Value *> >::iterator
    382            I = DeferredReplacements.begin(),
    383            E = DeferredReplacements.end();
    384        I != E; ++I) {
    385     I->first->replaceAllUsesWith(I->second);
    386     I->first->eraseFromParent();
    387   }
    388 }
    389 
    390 /// ShouldInstrumentFunction - Return true if the current function should be
    391 /// instrumented with __cyg_profile_func_* calls
    392 bool CodeGenFunction::ShouldInstrumentFunction() {
    393   if (!CGM.getCodeGenOpts().InstrumentFunctions)
    394     return false;
    395   if (!CurFuncDecl || CurFuncDecl->hasAttr<NoInstrumentFunctionAttr>())
    396     return false;
    397   return true;
    398 }
    399 
    400 /// ShouldXRayInstrument - Return true if the current function should be
    401 /// instrumented with XRay nop sleds.
    402 bool CodeGenFunction::ShouldXRayInstrumentFunction() const {
    403   return CGM.getCodeGenOpts().XRayInstrumentFunctions;
    404 }
    405 
    406 /// EmitFunctionInstrumentation - Emit LLVM code to call the specified
    407 /// instrumentation function with the current function and the call site, if
    408 /// function instrumentation is enabled.
    409 void CodeGenFunction::EmitFunctionInstrumentation(const char *Fn) {
    410   auto NL = ApplyDebugLocation::CreateArtificial(*this);
    411   // void __cyg_profile_func_{enter,exit} (void *this_fn, void *call_site);
    412   llvm::PointerType *PointerTy = Int8PtrTy;
    413   llvm::Type *ProfileFuncArgs[] = { PointerTy, PointerTy };
    414   llvm::FunctionType *FunctionTy =
    415     llvm::FunctionType::get(VoidTy, ProfileFuncArgs, false);
    416 
    417   llvm::Constant *F = CGM.CreateRuntimeFunction(FunctionTy, Fn);
    418   llvm::CallInst *CallSite = Builder.CreateCall(
    419     CGM.getIntrinsic(llvm::Intrinsic::returnaddress),
    420     llvm::ConstantInt::get(Int32Ty, 0),
    421     "callsite");
    422 
    423   llvm::Value *args[] = {
    424     llvm::ConstantExpr::getBitCast(CurFn, PointerTy),
    425     CallSite
    426   };
    427 
    428   EmitNounwindRuntimeCall(F, args);
    429 }
    430 
    431 void CodeGenFunction::EmitMCountInstrumentation() {
    432   llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
    433 
    434   llvm::Constant *MCountFn =
    435     CGM.CreateRuntimeFunction(FTy, getTarget().getMCountName());
    436   EmitNounwindRuntimeCall(MCountFn);
    437 }
    438 
    439 // OpenCL v1.2 s5.6.4.6 allows the compiler to store kernel argument
    440 // information in the program executable. The argument information stored
    441 // includes the argument name, its type, the address and access qualifiers used.
    442 static void GenOpenCLArgMetadata(const FunctionDecl *FD, llvm::Function *Fn,
    443                                  CodeGenModule &CGM, llvm::LLVMContext &Context,
    444                                  CGBuilderTy &Builder, ASTContext &ASTCtx) {
    445   // Create MDNodes that represent the kernel arg metadata.
    446   // Each MDNode is a list in the form of "key", N number of values which is
    447   // the same number of values as their are kernel arguments.
    448 
    449   const PrintingPolicy &Policy = ASTCtx.getPrintingPolicy();
    450 
    451   // MDNode for the kernel argument address space qualifiers.
    452   SmallVector<llvm::Metadata *, 8> addressQuals;
    453 
    454   // MDNode for the kernel argument access qualifiers (images only).
    455   SmallVector<llvm::Metadata *, 8> accessQuals;
    456 
    457   // MDNode for the kernel argument type names.
    458   SmallVector<llvm::Metadata *, 8> argTypeNames;
    459 
    460   // MDNode for the kernel argument base type names.
    461   SmallVector<llvm::Metadata *, 8> argBaseTypeNames;
    462 
    463   // MDNode for the kernel argument type qualifiers.
    464   SmallVector<llvm::Metadata *, 8> argTypeQuals;
    465 
    466   // MDNode for the kernel argument names.
    467   SmallVector<llvm::Metadata *, 8> argNames;
    468 
    469   for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
    470     const ParmVarDecl *parm = FD->getParamDecl(i);
    471     QualType ty = parm->getType();
    472     std::string typeQuals;
    473 
    474     if (ty->isPointerType()) {
    475       QualType pointeeTy = ty->getPointeeType();
    476 
    477       // Get address qualifier.
    478       addressQuals.push_back(llvm::ConstantAsMetadata::get(Builder.getInt32(
    479           ASTCtx.getTargetAddressSpace(pointeeTy.getAddressSpace()))));
    480 
    481       // Get argument type name.
    482       std::string typeName =
    483           pointeeTy.getUnqualifiedType().getAsString(Policy) + "*";
    484 
    485       // Turn "unsigned type" to "utype"
    486       std::string::size_type pos = typeName.find("unsigned");
    487       if (pointeeTy.isCanonical() && pos != std::string::npos)
    488         typeName.erase(pos+1, 8);
    489 
    490       argTypeNames.push_back(llvm::MDString::get(Context, typeName));
    491 
    492       std::string baseTypeName =
    493           pointeeTy.getUnqualifiedType().getCanonicalType().getAsString(
    494               Policy) +
    495           "*";
    496 
    497       // Turn "unsigned type" to "utype"
    498       pos = baseTypeName.find("unsigned");
    499       if (pos != std::string::npos)
    500         baseTypeName.erase(pos+1, 8);
    501 
    502       argBaseTypeNames.push_back(llvm::MDString::get(Context, baseTypeName));
    503 
    504       // Get argument type qualifiers:
    505       if (ty.isRestrictQualified())
    506         typeQuals = "restrict";
    507       if (pointeeTy.isConstQualified() ||
    508           (pointeeTy.getAddressSpace() == LangAS::opencl_constant))
    509         typeQuals += typeQuals.empty() ? "const" : " const";
    510       if (pointeeTy.isVolatileQualified())
    511         typeQuals += typeQuals.empty() ? "volatile" : " volatile";
    512     } else {
    513       uint32_t AddrSpc = 0;
    514       bool isPipe = ty->isPipeType();
    515       if (ty->isImageType() || isPipe)
    516         AddrSpc =
    517           CGM.getContext().getTargetAddressSpace(LangAS::opencl_global);
    518 
    519       addressQuals.push_back(
    520           llvm::ConstantAsMetadata::get(Builder.getInt32(AddrSpc)));
    521 
    522       // Get argument type name.
    523       std::string typeName;
    524       if (isPipe)
    525         typeName = ty.getCanonicalType()->getAs<PipeType>()->getElementType()
    526                      .getAsString(Policy);
    527       else
    528         typeName = ty.getUnqualifiedType().getAsString(Policy);
    529 
    530       // Turn "unsigned type" to "utype"
    531       std::string::size_type pos = typeName.find("unsigned");
    532       if (ty.isCanonical() && pos != std::string::npos)
    533         typeName.erase(pos+1, 8);
    534 
    535       argTypeNames.push_back(llvm::MDString::get(Context, typeName));
    536 
    537       std::string baseTypeName;
    538       if (isPipe)
    539         baseTypeName = ty.getCanonicalType()->getAs<PipeType>()
    540                           ->getElementType().getCanonicalType()
    541                           .getAsString(Policy);
    542       else
    543         baseTypeName =
    544           ty.getUnqualifiedType().getCanonicalType().getAsString(Policy);
    545 
    546       // Turn "unsigned type" to "utype"
    547       pos = baseTypeName.find("unsigned");
    548       if (pos != std::string::npos)
    549         baseTypeName.erase(pos+1, 8);
    550 
    551       argBaseTypeNames.push_back(llvm::MDString::get(Context, baseTypeName));
    552 
    553       // Get argument type qualifiers:
    554       if (ty.isConstQualified())
    555         typeQuals = "const";
    556       if (ty.isVolatileQualified())
    557         typeQuals += typeQuals.empty() ? "volatile" : " volatile";
    558       if (isPipe)
    559         typeQuals = "pipe";
    560     }
    561 
    562     argTypeQuals.push_back(llvm::MDString::get(Context, typeQuals));
    563 
    564     // Get image and pipe access qualifier:
    565     if (ty->isImageType()|| ty->isPipeType()) {
    566       const OpenCLAccessAttr *A = parm->getAttr<OpenCLAccessAttr>();
    567       if (A && A->isWriteOnly())
    568         accessQuals.push_back(llvm::MDString::get(Context, "write_only"));
    569       else if (A && A->isReadWrite())
    570         accessQuals.push_back(llvm::MDString::get(Context, "read_write"));
    571       else
    572         accessQuals.push_back(llvm::MDString::get(Context, "read_only"));
    573     } else
    574       accessQuals.push_back(llvm::MDString::get(Context, "none"));
    575 
    576     // Get argument name.
    577     argNames.push_back(llvm::MDString::get(Context, parm->getName()));
    578   }
    579 
    580   Fn->setMetadata("kernel_arg_addr_space",
    581                   llvm::MDNode::get(Context, addressQuals));
    582   Fn->setMetadata("kernel_arg_access_qual",
    583                   llvm::MDNode::get(Context, accessQuals));
    584   Fn->setMetadata("kernel_arg_type",
    585                   llvm::MDNode::get(Context, argTypeNames));
    586   Fn->setMetadata("kernel_arg_base_type",
    587                   llvm::MDNode::get(Context, argBaseTypeNames));
    588   Fn->setMetadata("kernel_arg_type_qual",
    589                   llvm::MDNode::get(Context, argTypeQuals));
    590   if (CGM.getCodeGenOpts().EmitOpenCLArgMetadata)
    591     Fn->setMetadata("kernel_arg_name",
    592                     llvm::MDNode::get(Context, argNames));
    593 }
    594 
    595 void CodeGenFunction::EmitOpenCLKernelMetadata(const FunctionDecl *FD,
    596                                                llvm::Function *Fn)
    597 {
    598   if (!FD->hasAttr<OpenCLKernelAttr>())
    599     return;
    600 
    601   llvm::LLVMContext &Context = getLLVMContext();
    602 
    603   GenOpenCLArgMetadata(FD, Fn, CGM, Context, Builder, getContext());
    604 
    605   if (const VecTypeHintAttr *A = FD->getAttr<VecTypeHintAttr>()) {
    606     QualType hintQTy = A->getTypeHint();
    607     const ExtVectorType *hintEltQTy = hintQTy->getAs<ExtVectorType>();
    608     bool isSignedInteger =
    609         hintQTy->isSignedIntegerType() ||
    610         (hintEltQTy && hintEltQTy->getElementType()->isSignedIntegerType());
    611     llvm::Metadata *attrMDArgs[] = {
    612         llvm::ConstantAsMetadata::get(llvm::UndefValue::get(
    613             CGM.getTypes().ConvertType(A->getTypeHint()))),
    614         llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
    615             llvm::IntegerType::get(Context, 32),
    616             llvm::APInt(32, (uint64_t)(isSignedInteger ? 1 : 0))))};
    617     Fn->setMetadata("vec_type_hint", llvm::MDNode::get(Context, attrMDArgs));
    618   }
    619 
    620   if (const WorkGroupSizeHintAttr *A = FD->getAttr<WorkGroupSizeHintAttr>()) {
    621     llvm::Metadata *attrMDArgs[] = {
    622         llvm::ConstantAsMetadata::get(Builder.getInt32(A->getXDim())),
    623         llvm::ConstantAsMetadata::get(Builder.getInt32(A->getYDim())),
    624         llvm::ConstantAsMetadata::get(Builder.getInt32(A->getZDim()))};
    625     Fn->setMetadata("work_group_size_hint", llvm::MDNode::get(Context, attrMDArgs));
    626   }
    627 
    628   if (const ReqdWorkGroupSizeAttr *A = FD->getAttr<ReqdWorkGroupSizeAttr>()) {
    629     llvm::Metadata *attrMDArgs[] = {
    630         llvm::ConstantAsMetadata::get(Builder.getInt32(A->getXDim())),
    631         llvm::ConstantAsMetadata::get(Builder.getInt32(A->getYDim())),
    632         llvm::ConstantAsMetadata::get(Builder.getInt32(A->getZDim()))};
    633     Fn->setMetadata("reqd_work_group_size", llvm::MDNode::get(Context, attrMDArgs));
    634   }
    635 }
    636 
    637 /// Determine whether the function F ends with a return stmt.
    638 static bool endsWithReturn(const Decl* F) {
    639   const Stmt *Body = nullptr;
    640   if (auto *FD = dyn_cast_or_null<FunctionDecl>(F))
    641     Body = FD->getBody();
    642   else if (auto *OMD = dyn_cast_or_null<ObjCMethodDecl>(F))
    643     Body = OMD->getBody();
    644 
    645   if (auto *CS = dyn_cast_or_null<CompoundStmt>(Body)) {
    646     auto LastStmt = CS->body_rbegin();
    647     if (LastStmt != CS->body_rend())
    648       return isa<ReturnStmt>(*LastStmt);
    649   }
    650   return false;
    651 }
    652 
    653 void CodeGenFunction::StartFunction(GlobalDecl GD,
    654                                     QualType RetTy,
    655                                     llvm::Function *Fn,
    656                                     const CGFunctionInfo &FnInfo,
    657                                     const FunctionArgList &Args,
    658                                     SourceLocation Loc,
    659                                     SourceLocation StartLoc) {
    660   assert(!CurFn &&
    661          "Do not use a CodeGenFunction object for more than one function");
    662 
    663   const Decl *D = GD.getDecl();
    664 
    665   DidCallStackSave = false;
    666   CurCodeDecl = D;
    667   if (const auto *FD = dyn_cast_or_null<FunctionDecl>(D))
    668     if (FD->usesSEHTry())
    669       CurSEHParent = FD;
    670   CurFuncDecl = (D ? D->getNonClosureContext() : nullptr);
    671   FnRetTy = RetTy;
    672   CurFn = Fn;
    673   CurFnInfo = &FnInfo;
    674   assert(CurFn->isDeclaration() && "Function already has body?");
    675 
    676   if (CGM.isInSanitizerBlacklist(Fn, Loc))
    677     SanOpts.clear();
    678 
    679   if (D) {
    680     // Apply the no_sanitize* attributes to SanOpts.
    681     for (auto Attr : D->specific_attrs<NoSanitizeAttr>())
    682       SanOpts.Mask &= ~Attr->getMask();
    683   }
    684 
    685   // Apply sanitizer attributes to the function.
    686   if (SanOpts.hasOneOf(SanitizerKind::Address | SanitizerKind::KernelAddress))
    687     Fn->addFnAttr(llvm::Attribute::SanitizeAddress);
    688   if (SanOpts.has(SanitizerKind::Thread))
    689     Fn->addFnAttr(llvm::Attribute::SanitizeThread);
    690   if (SanOpts.has(SanitizerKind::Memory))
    691     Fn->addFnAttr(llvm::Attribute::SanitizeMemory);
    692   if (SanOpts.has(SanitizerKind::SafeStack))
    693     Fn->addFnAttr(llvm::Attribute::SafeStack);
    694 
    695   // Apply xray attributes to the function (as a string, for now)
    696   if (D && ShouldXRayInstrumentFunction()) {
    697     if (const auto *XRayAttr = D->getAttr<XRayInstrumentAttr>()) {
    698       if (XRayAttr->alwaysXRayInstrument())
    699         Fn->addFnAttr("function-instrument", "xray-always");
    700       if (XRayAttr->neverXRayInstrument())
    701         Fn->addFnAttr("function-instrument", "xray-never");
    702     } else {
    703       Fn->addFnAttr(
    704           "xray-instruction-threshold",
    705           llvm::itostr(CGM.getCodeGenOpts().XRayInstructionThreshold));
    706     }
    707   }
    708 
    709   // Pass inline keyword to optimizer if it appears explicitly on any
    710   // declaration. Also, in the case of -fno-inline attach NoInline
    711   // attribute to all functions that are not marked AlwaysInline, or
    712   // to all functions that are not marked inline or implicitly inline
    713   // in the case of -finline-hint-functions.
    714   if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
    715     const CodeGenOptions& CodeGenOpts = CGM.getCodeGenOpts();
    716     if (!CodeGenOpts.NoInline) {
    717       for (auto RI : FD->redecls())
    718         if (RI->isInlineSpecified()) {
    719           Fn->addFnAttr(llvm::Attribute::InlineHint);
    720           break;
    721         }
    722       if (CodeGenOpts.getInlining() == CodeGenOptions::OnlyHintInlining &&
    723           !FD->isInlined() && !Fn->hasFnAttribute(llvm::Attribute::InlineHint))
    724         Fn->addFnAttr(llvm::Attribute::NoInline);
    725     } else if (!FD->hasAttr<AlwaysInlineAttr>())
    726       Fn->addFnAttr(llvm::Attribute::NoInline);
    727     if (CGM.getLangOpts().OpenMP && FD->hasAttr<OMPDeclareSimdDeclAttr>())
    728       CGM.getOpenMPRuntime().emitDeclareSimdFunction(FD, Fn);
    729   }
    730 
    731   // Add no-jump-tables value.
    732   Fn->addFnAttr("no-jump-tables",
    733                 llvm::toStringRef(CGM.getCodeGenOpts().NoUseJumpTables));
    734 
    735   if (getLangOpts().OpenCL) {
    736     // Add metadata for a kernel function.
    737     if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
    738       EmitOpenCLKernelMetadata(FD, Fn);
    739   }
    740 
    741   // If we are checking function types, emit a function type signature as
    742   // prologue data.
    743   if (getLangOpts().CPlusPlus && SanOpts.has(SanitizerKind::Function)) {
    744     if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
    745       if (llvm::Constant *PrologueSig =
    746               CGM.getTargetCodeGenInfo().getUBSanFunctionSignature(CGM)) {
    747         llvm::Constant *FTRTTIConst =
    748             CGM.GetAddrOfRTTIDescriptor(FD->getType(), /*ForEH=*/true);
    749         llvm::Constant *PrologueStructElems[] = { PrologueSig, FTRTTIConst };
    750         llvm::Constant *PrologueStructConst =
    751             llvm::ConstantStruct::getAnon(PrologueStructElems, /*Packed=*/true);
    752         Fn->setPrologueData(PrologueStructConst);
    753       }
    754     }
    755   }
    756 
    757   // If we're in C++ mode and the function name is "main", it is guaranteed
    758   // to be norecurse by the standard (3.6.1.3 "The function main shall not be
    759   // used within a program").
    760   if (getLangOpts().CPlusPlus)
    761     if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
    762       if (FD->isMain())
    763         Fn->addFnAttr(llvm::Attribute::NoRecurse);
    764 
    765   llvm::BasicBlock *EntryBB = createBasicBlock("entry", CurFn);
    766 
    767   // Create a marker to make it easy to insert allocas into the entryblock
    768   // later.  Don't create this with the builder, because we don't want it
    769   // folded.
    770   llvm::Value *Undef = llvm::UndefValue::get(Int32Ty);
    771   AllocaInsertPt = new llvm::BitCastInst(Undef, Int32Ty, "allocapt", EntryBB);
    772 
    773   ReturnBlock = getJumpDestInCurrentScope("return");
    774 
    775   Builder.SetInsertPoint(EntryBB);
    776 
    777   // Emit subprogram debug descriptor.
    778   if (CGDebugInfo *DI = getDebugInfo()) {
    779     // Reconstruct the type from the argument list so that implicit parameters,
    780     // such as 'this' and 'vtt', show up in the debug info. Preserve the calling
    781     // convention.
    782     CallingConv CC = CallingConv::CC_C;
    783     if (auto *FD = dyn_cast_or_null<FunctionDecl>(D))
    784       if (const auto *SrcFnTy = FD->getType()->getAs<FunctionType>())
    785         CC = SrcFnTy->getCallConv();
    786     SmallVector<QualType, 16> ArgTypes;
    787     for (const VarDecl *VD : Args)
    788       ArgTypes.push_back(VD->getType());
    789     QualType FnType = getContext().getFunctionType(
    790         RetTy, ArgTypes, FunctionProtoType::ExtProtoInfo(CC));
    791     DI->EmitFunctionStart(GD, Loc, StartLoc, FnType, CurFn, Builder);
    792   }
    793 
    794   if (ShouldInstrumentFunction())
    795     EmitFunctionInstrumentation("__cyg_profile_func_enter");
    796 
    797   if (CGM.getCodeGenOpts().InstrumentForProfiling)
    798     EmitMCountInstrumentation();
    799 
    800   if (RetTy->isVoidType()) {
    801     // Void type; nothing to return.
    802     ReturnValue = Address::invalid();
    803 
    804     // Count the implicit return.
    805     if (!endsWithReturn(D))
    806       ++NumReturnExprs;
    807   } else if (CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect &&
    808              !hasScalarEvaluationKind(CurFnInfo->getReturnType())) {
    809     // Indirect aggregate return; emit returned value directly into sret slot.
    810     // This reduces code size, and affects correctness in C++.
    811     auto AI = CurFn->arg_begin();
    812     if (CurFnInfo->getReturnInfo().isSRetAfterThis())
    813       ++AI;
    814     ReturnValue = Address(&*AI, CurFnInfo->getReturnInfo().getIndirectAlign());
    815   } else if (CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::InAlloca &&
    816              !hasScalarEvaluationKind(CurFnInfo->getReturnType())) {
    817     // Load the sret pointer from the argument struct and return into that.
    818     unsigned Idx = CurFnInfo->getReturnInfo().getInAllocaFieldIndex();
    819     llvm::Function::arg_iterator EI = CurFn->arg_end();
    820     --EI;
    821     llvm::Value *Addr = Builder.CreateStructGEP(nullptr, &*EI, Idx);
    822     Addr = Builder.CreateAlignedLoad(Addr, getPointerAlign(), "agg.result");
    823     ReturnValue = Address(Addr, getNaturalTypeAlignment(RetTy));
    824   } else {
    825     ReturnValue = CreateIRTemp(RetTy, "retval");
    826 
    827     // Tell the epilog emitter to autorelease the result.  We do this
    828     // now so that various specialized functions can suppress it
    829     // during their IR-generation.
    830     if (getLangOpts().ObjCAutoRefCount &&
    831         !CurFnInfo->isReturnsRetained() &&
    832         RetTy->isObjCRetainableType())
    833       AutoreleaseResult = true;
    834   }
    835 
    836   EmitStartEHSpec(CurCodeDecl);
    837 
    838   PrologueCleanupDepth = EHStack.stable_begin();
    839   EmitFunctionProlog(*CurFnInfo, CurFn, Args);
    840 
    841   if (D && isa<CXXMethodDecl>(D) && cast<CXXMethodDecl>(D)->isInstance()) {
    842     CGM.getCXXABI().EmitInstanceFunctionProlog(*this);
    843     const CXXMethodDecl *MD = cast<CXXMethodDecl>(D);
    844     if (MD->getParent()->isLambda() &&
    845         MD->getOverloadedOperator() == OO_Call) {
    846       // We're in a lambda; figure out the captures.
    847       MD->getParent()->getCaptureFields(LambdaCaptureFields,
    848                                         LambdaThisCaptureField);
    849       if (LambdaThisCaptureField) {
    850         // If the lambda captures the object referred to by '*this' - either by
    851         // value or by reference, make sure CXXThisValue points to the correct
    852         // object.
    853 
    854         // Get the lvalue for the field (which is a copy of the enclosing object
    855         // or contains the address of the enclosing object).
    856         LValue ThisFieldLValue = EmitLValueForLambdaField(LambdaThisCaptureField);
    857         if (!LambdaThisCaptureField->getType()->isPointerType()) {
    858           // If the enclosing object was captured by value, just use its address.
    859           CXXThisValue = ThisFieldLValue.getAddress().getPointer();
    860         } else {
    861           // Load the lvalue pointed to by the field, since '*this' was captured
    862           // by reference.
    863           CXXThisValue =
    864               EmitLoadOfLValue(ThisFieldLValue, SourceLocation()).getScalarVal();
    865         }
    866       }
    867       for (auto *FD : MD->getParent()->fields()) {
    868         if (FD->hasCapturedVLAType()) {
    869           auto *ExprArg = EmitLoadOfLValue(EmitLValueForLambdaField(FD),
    870                                            SourceLocation()).getScalarVal();
    871           auto VAT = FD->getCapturedVLAType();
    872           VLASizeMap[VAT->getSizeExpr()] = ExprArg;
    873         }
    874       }
    875     } else {
    876       // Not in a lambda; just use 'this' from the method.
    877       // FIXME: Should we generate a new load for each use of 'this'?  The
    878       // fast register allocator would be happier...
    879       CXXThisValue = CXXABIThisValue;
    880     }
    881   }
    882 
    883   // If any of the arguments have a variably modified type, make sure to
    884   // emit the type size.
    885   for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
    886        i != e; ++i) {
    887     const VarDecl *VD = *i;
    888 
    889     // Dig out the type as written from ParmVarDecls; it's unclear whether
    890     // the standard (C99 6.9.1p10) requires this, but we're following the
    891     // precedent set by gcc.
    892     QualType Ty;
    893     if (const ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(VD))
    894       Ty = PVD->getOriginalType();
    895     else
    896       Ty = VD->getType();
    897 
    898     if (Ty->isVariablyModifiedType())
    899       EmitVariablyModifiedType(Ty);
    900   }
    901   // Emit a location at the end of the prologue.
    902   if (CGDebugInfo *DI = getDebugInfo())
    903     DI->EmitLocation(Builder, StartLoc);
    904 }
    905 
    906 void CodeGenFunction::EmitFunctionBody(FunctionArgList &Args,
    907                                        const Stmt *Body) {
    908   incrementProfileCounter(Body);
    909   if (const CompoundStmt *S = dyn_cast<CompoundStmt>(Body))
    910     EmitCompoundStmtWithoutScope(*S);
    911   else
    912     EmitStmt(Body);
    913 }
    914 
    915 /// When instrumenting to collect profile data, the counts for some blocks
    916 /// such as switch cases need to not include the fall-through counts, so
    917 /// emit a branch around the instrumentation code. When not instrumenting,
    918 /// this just calls EmitBlock().
    919 void CodeGenFunction::EmitBlockWithFallThrough(llvm::BasicBlock *BB,
    920                                                const Stmt *S) {
    921   llvm::BasicBlock *SkipCountBB = nullptr;
    922   if (HaveInsertPoint() && CGM.getCodeGenOpts().hasProfileClangInstr()) {
    923     // When instrumenting for profiling, the fallthrough to certain
    924     // statements needs to skip over the instrumentation code so that we
    925     // get an accurate count.
    926     SkipCountBB = createBasicBlock("skipcount");
    927     EmitBranch(SkipCountBB);
    928   }
    929   EmitBlock(BB);
    930   uint64_t CurrentCount = getCurrentProfileCount();
    931   incrementProfileCounter(S);
    932   setCurrentProfileCount(getCurrentProfileCount() + CurrentCount);
    933   if (SkipCountBB)
    934     EmitBlock(SkipCountBB);
    935 }
    936 
    937 /// Tries to mark the given function nounwind based on the
    938 /// non-existence of any throwing calls within it.  We believe this is
    939 /// lightweight enough to do at -O0.
    940 static void TryMarkNoThrow(llvm::Function *F) {
    941   // LLVM treats 'nounwind' on a function as part of the type, so we
    942   // can't do this on functions that can be overwritten.
    943   if (F->isInterposable()) return;
    944 
    945   for (llvm::BasicBlock &BB : *F)
    946     for (llvm::Instruction &I : BB)
    947       if (I.mayThrow())
    948         return;
    949 
    950   F->setDoesNotThrow();
    951 }
    952 
    953 QualType CodeGenFunction::BuildFunctionArgList(GlobalDecl GD,
    954                                                FunctionArgList &Args) {
    955   const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
    956   QualType ResTy = FD->getReturnType();
    957 
    958   const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD);
    959   if (MD && MD->isInstance()) {
    960     if (CGM.getCXXABI().HasThisReturn(GD))
    961       ResTy = MD->getThisType(getContext());
    962     else if (CGM.getCXXABI().hasMostDerivedReturn(GD))
    963       ResTy = CGM.getContext().VoidPtrTy;
    964     CGM.getCXXABI().buildThisParam(*this, Args);
    965   }
    966 
    967   // The base version of an inheriting constructor whose constructed base is a
    968   // virtual base is not passed any arguments (because it doesn't actually call
    969   // the inherited constructor).
    970   bool PassedParams = true;
    971   if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD))
    972     if (auto Inherited = CD->getInheritedConstructor())
    973       PassedParams =
    974           getTypes().inheritingCtorHasParams(Inherited, GD.getCtorType());
    975 
    976   if (PassedParams) {
    977     for (auto *Param : FD->parameters()) {
    978       Args.push_back(Param);
    979       if (!Param->hasAttr<PassObjectSizeAttr>())
    980         continue;
    981 
    982       IdentifierInfo *NoID = nullptr;
    983       auto *Implicit = ImplicitParamDecl::Create(
    984           getContext(), Param->getDeclContext(), Param->getLocation(), NoID,
    985           getContext().getSizeType());
    986       SizeArguments[Param] = Implicit;
    987       Args.push_back(Implicit);
    988     }
    989   }
    990 
    991   if (MD && (isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD)))
    992     CGM.getCXXABI().addImplicitStructorParams(*this, ResTy, Args);
    993 
    994   return ResTy;
    995 }
    996 
    997 void CodeGenFunction::GenerateCode(GlobalDecl GD, llvm::Function *Fn,
    998                                    const CGFunctionInfo &FnInfo) {
    999   const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
   1000   CurGD = GD;
   1001 
   1002   FunctionArgList Args;
   1003   QualType ResTy = BuildFunctionArgList(GD, Args);
   1004 
   1005   // Check if we should generate debug info for this function.
   1006   if (FD->hasAttr<NoDebugAttr>())
   1007     DebugInfo = nullptr; // disable debug info indefinitely for this function
   1008 
   1009   SourceRange BodyRange;
   1010   if (Stmt *Body = FD->getBody()) BodyRange = Body->getSourceRange();
   1011   CurEHLocation = BodyRange.getEnd();
   1012 
   1013   // Use the location of the start of the function to determine where
   1014   // the function definition is located. By default use the location
   1015   // of the declaration as the location for the subprogram. A function
   1016   // may lack a declaration in the source code if it is created by code
   1017   // gen. (examples: _GLOBAL__I_a, __cxx_global_array_dtor, thunk).
   1018   SourceLocation Loc = FD->getLocation();
   1019 
   1020   // If this is a function specialization then use the pattern body
   1021   // as the location for the function.
   1022   if (const FunctionDecl *SpecDecl = FD->getTemplateInstantiationPattern())
   1023     if (SpecDecl->hasBody(SpecDecl))
   1024       Loc = SpecDecl->getLocation();
   1025 
   1026   // Emit the standard function prologue.
   1027   StartFunction(GD, ResTy, Fn, FnInfo, Args, Loc, BodyRange.getBegin());
   1028 
   1029   // Generate the body of the function.
   1030   PGO.assignRegionCounters(GD, CurFn);
   1031   if (isa<CXXDestructorDecl>(FD))
   1032     EmitDestructorBody(Args);
   1033   else if (isa<CXXConstructorDecl>(FD))
   1034     EmitConstructorBody(Args);
   1035   else if (getLangOpts().CUDA &&
   1036            !getLangOpts().CUDAIsDevice &&
   1037            FD->hasAttr<CUDAGlobalAttr>())
   1038     CGM.getCUDARuntime().emitDeviceStub(*this, Args);
   1039   else if (isa<CXXConversionDecl>(FD) &&
   1040            cast<CXXConversionDecl>(FD)->isLambdaToBlockPointerConversion()) {
   1041     // The lambda conversion to block pointer is special; the semantics can't be
   1042     // expressed in the AST, so IRGen needs to special-case it.
   1043     EmitLambdaToBlockPointerBody(Args);
   1044   } else if (isa<CXXMethodDecl>(FD) &&
   1045              cast<CXXMethodDecl>(FD)->isLambdaStaticInvoker()) {
   1046     // The lambda static invoker function is special, because it forwards or
   1047     // clones the body of the function call operator (but is actually static).
   1048     EmitLambdaStaticInvokeFunction(cast<CXXMethodDecl>(FD));
   1049   } else if (FD->isDefaulted() && isa<CXXMethodDecl>(FD) &&
   1050              (cast<CXXMethodDecl>(FD)->isCopyAssignmentOperator() ||
   1051               cast<CXXMethodDecl>(FD)->isMoveAssignmentOperator())) {
   1052     // Implicit copy-assignment gets the same special treatment as implicit
   1053     // copy-constructors.
   1054     emitImplicitAssignmentOperatorBody(Args);
   1055   } else if (Stmt *Body = FD->getBody()) {
   1056     EmitFunctionBody(Args, Body);
   1057   } else
   1058     llvm_unreachable("no definition for emitted function");
   1059 
   1060   // C++11 [stmt.return]p2:
   1061   //   Flowing off the end of a function [...] results in undefined behavior in
   1062   //   a value-returning function.
   1063   // C11 6.9.1p12:
   1064   //   If the '}' that terminates a function is reached, and the value of the
   1065   //   function call is used by the caller, the behavior is undefined.
   1066   if (getLangOpts().CPlusPlus && !FD->hasImplicitReturnZero() && !SawAsmBlock &&
   1067       !FD->getReturnType()->isVoidType() && Builder.GetInsertBlock()) {
   1068     if (SanOpts.has(SanitizerKind::Return)) {
   1069       SanitizerScope SanScope(this);
   1070       llvm::Value *IsFalse = Builder.getFalse();
   1071       EmitCheck(std::make_pair(IsFalse, SanitizerKind::Return),
   1072                 "missing_return", EmitCheckSourceLocation(FD->getLocation()),
   1073                 None);
   1074     } else if (CGM.getCodeGenOpts().OptimizationLevel == 0) {
   1075       EmitTrapCall(llvm::Intrinsic::trap);
   1076     }
   1077     Builder.CreateUnreachable();
   1078     Builder.ClearInsertionPoint();
   1079   }
   1080 
   1081   // Emit the standard function epilogue.
   1082   FinishFunction(BodyRange.getEnd());
   1083 
   1084   // If we haven't marked the function nothrow through other means, do
   1085   // a quick pass now to see if we can.
   1086   if (!CurFn->doesNotThrow())
   1087     TryMarkNoThrow(CurFn);
   1088 }
   1089 
   1090 /// ContainsLabel - Return true if the statement contains a label in it.  If
   1091 /// this statement is not executed normally, it not containing a label means
   1092 /// that we can just remove the code.
   1093 bool CodeGenFunction::ContainsLabel(const Stmt *S, bool IgnoreCaseStmts) {
   1094   // Null statement, not a label!
   1095   if (!S) return false;
   1096 
   1097   // If this is a label, we have to emit the code, consider something like:
   1098   // if (0) {  ...  foo:  bar(); }  goto foo;
   1099   //
   1100   // TODO: If anyone cared, we could track __label__'s, since we know that you
   1101   // can't jump to one from outside their declared region.
   1102   if (isa<LabelStmt>(S))
   1103     return true;
   1104 
   1105   // If this is a case/default statement, and we haven't seen a switch, we have
   1106   // to emit the code.
   1107   if (isa<SwitchCase>(S) && !IgnoreCaseStmts)
   1108     return true;
   1109 
   1110   // If this is a switch statement, we want to ignore cases below it.
   1111   if (isa<SwitchStmt>(S))
   1112     IgnoreCaseStmts = true;
   1113 
   1114   // Scan subexpressions for verboten labels.
   1115   for (const Stmt *SubStmt : S->children())
   1116     if (ContainsLabel(SubStmt, IgnoreCaseStmts))
   1117       return true;
   1118 
   1119   return false;
   1120 }
   1121 
   1122 /// containsBreak - Return true if the statement contains a break out of it.
   1123 /// If the statement (recursively) contains a switch or loop with a break
   1124 /// inside of it, this is fine.
   1125 bool CodeGenFunction::containsBreak(const Stmt *S) {
   1126   // Null statement, not a label!
   1127   if (!S) return false;
   1128 
   1129   // If this is a switch or loop that defines its own break scope, then we can
   1130   // include it and anything inside of it.
   1131   if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || isa<DoStmt>(S) ||
   1132       isa<ForStmt>(S))
   1133     return false;
   1134 
   1135   if (isa<BreakStmt>(S))
   1136     return true;
   1137 
   1138   // Scan subexpressions for verboten breaks.
   1139   for (const Stmt *SubStmt : S->children())
   1140     if (containsBreak(SubStmt))
   1141       return true;
   1142 
   1143   return false;
   1144 }
   1145 
   1146 
   1147 /// ConstantFoldsToSimpleInteger - If the specified expression does not fold
   1148 /// to a constant, or if it does but contains a label, return false.  If it
   1149 /// constant folds return true and set the boolean result in Result.
   1150 bool CodeGenFunction::ConstantFoldsToSimpleInteger(const Expr *Cond,
   1151                                                    bool &ResultBool,
   1152                                                    bool AllowLabels) {
   1153   llvm::APSInt ResultInt;
   1154   if (!ConstantFoldsToSimpleInteger(Cond, ResultInt, AllowLabels))
   1155     return false;
   1156 
   1157   ResultBool = ResultInt.getBoolValue();
   1158   return true;
   1159 }
   1160 
   1161 /// ConstantFoldsToSimpleInteger - If the specified expression does not fold
   1162 /// to a constant, or if it does but contains a label, return false.  If it
   1163 /// constant folds return true and set the folded value.
   1164 bool CodeGenFunction::ConstantFoldsToSimpleInteger(const Expr *Cond,
   1165                                                    llvm::APSInt &ResultInt,
   1166                                                    bool AllowLabels) {
   1167   // FIXME: Rename and handle conversion of other evaluatable things
   1168   // to bool.
   1169   llvm::APSInt Int;
   1170   if (!Cond->EvaluateAsInt(Int, getContext()))
   1171     return false;  // Not foldable, not integer or not fully evaluatable.
   1172 
   1173   if (!AllowLabels && CodeGenFunction::ContainsLabel(Cond))
   1174     return false;  // Contains a label.
   1175 
   1176   ResultInt = Int;
   1177   return true;
   1178 }
   1179 
   1180 
   1181 
   1182 /// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an if
   1183 /// statement) to the specified blocks.  Based on the condition, this might try
   1184 /// to simplify the codegen of the conditional based on the branch.
   1185 ///
   1186 void CodeGenFunction::EmitBranchOnBoolExpr(const Expr *Cond,
   1187                                            llvm::BasicBlock *TrueBlock,
   1188                                            llvm::BasicBlock *FalseBlock,
   1189                                            uint64_t TrueCount) {
   1190   Cond = Cond->IgnoreParens();
   1191 
   1192   if (const BinaryOperator *CondBOp = dyn_cast<BinaryOperator>(Cond)) {
   1193 
   1194     // Handle X && Y in a condition.
   1195     if (CondBOp->getOpcode() == BO_LAnd) {
   1196       // If we have "1 && X", simplify the code.  "0 && X" would have constant
   1197       // folded if the case was simple enough.
   1198       bool ConstantBool = false;
   1199       if (ConstantFoldsToSimpleInteger(CondBOp->getLHS(), ConstantBool) &&
   1200           ConstantBool) {
   1201         // br(1 && X) -> br(X).
   1202         incrementProfileCounter(CondBOp);
   1203         return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock,
   1204                                     TrueCount);
   1205       }
   1206 
   1207       // If we have "X && 1", simplify the code to use an uncond branch.
   1208       // "X && 0" would have been constant folded to 0.
   1209       if (ConstantFoldsToSimpleInteger(CondBOp->getRHS(), ConstantBool) &&
   1210           ConstantBool) {
   1211         // br(X && 1) -> br(X).
   1212         return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock,
   1213                                     TrueCount);
   1214       }
   1215 
   1216       // Emit the LHS as a conditional.  If the LHS conditional is false, we
   1217       // want to jump to the FalseBlock.
   1218       llvm::BasicBlock *LHSTrue = createBasicBlock("land.lhs.true");
   1219       // The counter tells us how often we evaluate RHS, and all of TrueCount
   1220       // can be propagated to that branch.
   1221       uint64_t RHSCount = getProfileCount(CondBOp->getRHS());
   1222 
   1223       ConditionalEvaluation eval(*this);
   1224       {
   1225         ApplyDebugLocation DL(*this, Cond);
   1226         EmitBranchOnBoolExpr(CondBOp->getLHS(), LHSTrue, FalseBlock, RHSCount);
   1227         EmitBlock(LHSTrue);
   1228       }
   1229 
   1230       incrementProfileCounter(CondBOp);
   1231       setCurrentProfileCount(getProfileCount(CondBOp->getRHS()));
   1232 
   1233       // Any temporaries created here are conditional.
   1234       eval.begin(*this);
   1235       EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock, TrueCount);
   1236       eval.end(*this);
   1237 
   1238       return;
   1239     }
   1240 
   1241     if (CondBOp->getOpcode() == BO_LOr) {
   1242       // If we have "0 || X", simplify the code.  "1 || X" would have constant
   1243       // folded if the case was simple enough.
   1244       bool ConstantBool = false;
   1245       if (ConstantFoldsToSimpleInteger(CondBOp->getLHS(), ConstantBool) &&
   1246           !ConstantBool) {
   1247         // br(0 || X) -> br(X).
   1248         incrementProfileCounter(CondBOp);
   1249         return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock,
   1250                                     TrueCount);
   1251       }
   1252 
   1253       // If we have "X || 0", simplify the code to use an uncond branch.
   1254       // "X || 1" would have been constant folded to 1.
   1255       if (ConstantFoldsToSimpleInteger(CondBOp->getRHS(), ConstantBool) &&
   1256           !ConstantBool) {
   1257         // br(X || 0) -> br(X).
   1258         return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock,
   1259                                     TrueCount);
   1260       }
   1261 
   1262       // Emit the LHS as a conditional.  If the LHS conditional is true, we
   1263       // want to jump to the TrueBlock.
   1264       llvm::BasicBlock *LHSFalse = createBasicBlock("lor.lhs.false");
   1265       // We have the count for entry to the RHS and for the whole expression
   1266       // being true, so we can divy up True count between the short circuit and
   1267       // the RHS.
   1268       uint64_t LHSCount =
   1269           getCurrentProfileCount() - getProfileCount(CondBOp->getRHS());
   1270       uint64_t RHSCount = TrueCount - LHSCount;
   1271 
   1272       ConditionalEvaluation eval(*this);
   1273       {
   1274         ApplyDebugLocation DL(*this, Cond);
   1275         EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, LHSFalse, LHSCount);
   1276         EmitBlock(LHSFalse);
   1277       }
   1278 
   1279       incrementProfileCounter(CondBOp);
   1280       setCurrentProfileCount(getProfileCount(CondBOp->getRHS()));
   1281 
   1282       // Any temporaries created here are conditional.
   1283       eval.begin(*this);
   1284       EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock, RHSCount);
   1285 
   1286       eval.end(*this);
   1287 
   1288       return;
   1289     }
   1290   }
   1291 
   1292   if (const UnaryOperator *CondUOp = dyn_cast<UnaryOperator>(Cond)) {
   1293     // br(!x, t, f) -> br(x, f, t)
   1294     if (CondUOp->getOpcode() == UO_LNot) {
   1295       // Negate the count.
   1296       uint64_t FalseCount = getCurrentProfileCount() - TrueCount;
   1297       // Negate the condition and swap the destination blocks.
   1298       return EmitBranchOnBoolExpr(CondUOp->getSubExpr(), FalseBlock, TrueBlock,
   1299                                   FalseCount);
   1300     }
   1301   }
   1302 
   1303   if (const ConditionalOperator *CondOp = dyn_cast<ConditionalOperator>(Cond)) {
   1304     // br(c ? x : y, t, f) -> br(c, br(x, t, f), br(y, t, f))
   1305     llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true");
   1306     llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false");
   1307 
   1308     ConditionalEvaluation cond(*this);
   1309     EmitBranchOnBoolExpr(CondOp->getCond(), LHSBlock, RHSBlock,
   1310                          getProfileCount(CondOp));
   1311 
   1312     // When computing PGO branch weights, we only know the overall count for
   1313     // the true block. This code is essentially doing tail duplication of the
   1314     // naive code-gen, introducing new edges for which counts are not
   1315     // available. Divide the counts proportionally between the LHS and RHS of
   1316     // the conditional operator.
   1317     uint64_t LHSScaledTrueCount = 0;
   1318     if (TrueCount) {
   1319       double LHSRatio =
   1320           getProfileCount(CondOp) / (double)getCurrentProfileCount();
   1321       LHSScaledTrueCount = TrueCount * LHSRatio;
   1322     }
   1323 
   1324     cond.begin(*this);
   1325     EmitBlock(LHSBlock);
   1326     incrementProfileCounter(CondOp);
   1327     {
   1328       ApplyDebugLocation DL(*this, Cond);
   1329       EmitBranchOnBoolExpr(CondOp->getLHS(), TrueBlock, FalseBlock,
   1330                            LHSScaledTrueCount);
   1331     }
   1332     cond.end(*this);
   1333 
   1334     cond.begin(*this);
   1335     EmitBlock(RHSBlock);
   1336     EmitBranchOnBoolExpr(CondOp->getRHS(), TrueBlock, FalseBlock,
   1337                          TrueCount - LHSScaledTrueCount);
   1338     cond.end(*this);
   1339 
   1340     return;
   1341   }
   1342 
   1343   if (const CXXThrowExpr *Throw = dyn_cast<CXXThrowExpr>(Cond)) {
   1344     // Conditional operator handling can give us a throw expression as a
   1345     // condition for a case like:
   1346     //   br(c ? throw x : y, t, f) -> br(c, br(throw x, t, f), br(y, t, f)
   1347     // Fold this to:
   1348     //   br(c, throw x, br(y, t, f))
   1349     EmitCXXThrowExpr(Throw, /*KeepInsertionPoint*/false);
   1350     return;
   1351   }
   1352 
   1353   // If the branch has a condition wrapped by __builtin_unpredictable,
   1354   // create metadata that specifies that the branch is unpredictable.
   1355   // Don't bother if not optimizing because that metadata would not be used.
   1356   llvm::MDNode *Unpredictable = nullptr;
   1357   auto *Call = dyn_cast<CallExpr>(Cond);
   1358   if (Call && CGM.getCodeGenOpts().OptimizationLevel != 0) {
   1359     auto *FD = dyn_cast_or_null<FunctionDecl>(Call->getCalleeDecl());
   1360     if (FD && FD->getBuiltinID() == Builtin::BI__builtin_unpredictable) {
   1361       llvm::MDBuilder MDHelper(getLLVMContext());
   1362       Unpredictable = MDHelper.createUnpredictable();
   1363     }
   1364   }
   1365 
   1366   // Create branch weights based on the number of times we get here and the
   1367   // number of times the condition should be true.
   1368   uint64_t CurrentCount = std::max(getCurrentProfileCount(), TrueCount);
   1369   llvm::MDNode *Weights =
   1370       createProfileWeights(TrueCount, CurrentCount - TrueCount);
   1371 
   1372   // Emit the code with the fully general case.
   1373   llvm::Value *CondV;
   1374   {
   1375     ApplyDebugLocation DL(*this, Cond);
   1376     CondV = EvaluateExprAsBool(Cond);
   1377   }
   1378   Builder.CreateCondBr(CondV, TrueBlock, FalseBlock, Weights, Unpredictable);
   1379 }
   1380 
   1381 /// ErrorUnsupported - Print out an error that codegen doesn't support the
   1382 /// specified stmt yet.
   1383 void CodeGenFunction::ErrorUnsupported(const Stmt *S, const char *Type) {
   1384   CGM.ErrorUnsupported(S, Type);
   1385 }
   1386 
   1387 /// emitNonZeroVLAInit - Emit the "zero" initialization of a
   1388 /// variable-length array whose elements have a non-zero bit-pattern.
   1389 ///
   1390 /// \param baseType the inner-most element type of the array
   1391 /// \param src - a char* pointing to the bit-pattern for a single
   1392 /// base element of the array
   1393 /// \param sizeInChars - the total size of the VLA, in chars
   1394 static void emitNonZeroVLAInit(CodeGenFunction &CGF, QualType baseType,
   1395                                Address dest, Address src,
   1396                                llvm::Value *sizeInChars) {
   1397   CGBuilderTy &Builder = CGF.Builder;
   1398 
   1399   CharUnits baseSize = CGF.getContext().getTypeSizeInChars(baseType);
   1400   llvm::Value *baseSizeInChars
   1401     = llvm::ConstantInt::get(CGF.IntPtrTy, baseSize.getQuantity());
   1402 
   1403   Address begin =
   1404     Builder.CreateElementBitCast(dest, CGF.Int8Ty, "vla.begin");
   1405   llvm::Value *end =
   1406     Builder.CreateInBoundsGEP(begin.getPointer(), sizeInChars, "vla.end");
   1407 
   1408   llvm::BasicBlock *originBB = CGF.Builder.GetInsertBlock();
   1409   llvm::BasicBlock *loopBB = CGF.createBasicBlock("vla-init.loop");
   1410   llvm::BasicBlock *contBB = CGF.createBasicBlock("vla-init.cont");
   1411 
   1412   // Make a loop over the VLA.  C99 guarantees that the VLA element
   1413   // count must be nonzero.
   1414   CGF.EmitBlock(loopBB);
   1415 
   1416   llvm::PHINode *cur = Builder.CreatePHI(begin.getType(), 2, "vla.cur");
   1417   cur->addIncoming(begin.getPointer(), originBB);
   1418 
   1419   CharUnits curAlign =
   1420     dest.getAlignment().alignmentOfArrayElement(baseSize);
   1421 
   1422   // memcpy the individual element bit-pattern.
   1423   Builder.CreateMemCpy(Address(cur, curAlign), src, baseSizeInChars,
   1424                        /*volatile*/ false);
   1425 
   1426   // Go to the next element.
   1427   llvm::Value *next =
   1428     Builder.CreateInBoundsGEP(CGF.Int8Ty, cur, baseSizeInChars, "vla.next");
   1429 
   1430   // Leave if that's the end of the VLA.
   1431   llvm::Value *done = Builder.CreateICmpEQ(next, end, "vla-init.isdone");
   1432   Builder.CreateCondBr(done, contBB, loopBB);
   1433   cur->addIncoming(next, loopBB);
   1434 
   1435   CGF.EmitBlock(contBB);
   1436 }
   1437 
   1438 void
   1439 CodeGenFunction::EmitNullInitialization(Address DestPtr, QualType Ty) {
   1440   // Ignore empty classes in C++.
   1441   if (getLangOpts().CPlusPlus) {
   1442     if (const RecordType *RT = Ty->getAs<RecordType>()) {
   1443       if (cast<CXXRecordDecl>(RT->getDecl())->isEmpty())
   1444         return;
   1445     }
   1446   }
   1447 
   1448   // Cast the dest ptr to the appropriate i8 pointer type.
   1449   if (DestPtr.getElementType() != Int8Ty)
   1450     DestPtr = Builder.CreateElementBitCast(DestPtr, Int8Ty);
   1451 
   1452   // Get size and alignment info for this aggregate.
   1453   CharUnits size = getContext().getTypeSizeInChars(Ty);
   1454 
   1455   llvm::Value *SizeVal;
   1456   const VariableArrayType *vla;
   1457 
   1458   // Don't bother emitting a zero-byte memset.
   1459   if (size.isZero()) {
   1460     // But note that getTypeInfo returns 0 for a VLA.
   1461     if (const VariableArrayType *vlaType =
   1462           dyn_cast_or_null<VariableArrayType>(
   1463                                           getContext().getAsArrayType(Ty))) {
   1464       QualType eltType;
   1465       llvm::Value *numElts;
   1466       std::tie(numElts, eltType) = getVLASize(vlaType);
   1467 
   1468       SizeVal = numElts;
   1469       CharUnits eltSize = getContext().getTypeSizeInChars(eltType);
   1470       if (!eltSize.isOne())
   1471         SizeVal = Builder.CreateNUWMul(SizeVal, CGM.getSize(eltSize));
   1472       vla = vlaType;
   1473     } else {
   1474       return;
   1475     }
   1476   } else {
   1477     SizeVal = CGM.getSize(size);
   1478     vla = nullptr;
   1479   }
   1480 
   1481   // If the type contains a pointer to data member we can't memset it to zero.
   1482   // Instead, create a null constant and copy it to the destination.
   1483   // TODO: there are other patterns besides zero that we can usefully memset,
   1484   // like -1, which happens to be the pattern used by member-pointers.
   1485   if (!CGM.getTypes().isZeroInitializable(Ty)) {
   1486     // For a VLA, emit a single element, then splat that over the VLA.
   1487     if (vla) Ty = getContext().getBaseElementType(vla);
   1488 
   1489     llvm::Constant *NullConstant = CGM.EmitNullConstant(Ty);
   1490 
   1491     llvm::GlobalVariable *NullVariable =
   1492       new llvm::GlobalVariable(CGM.getModule(), NullConstant->getType(),
   1493                                /*isConstant=*/true,
   1494                                llvm::GlobalVariable::PrivateLinkage,
   1495                                NullConstant, Twine());
   1496     CharUnits NullAlign = DestPtr.getAlignment();
   1497     NullVariable->setAlignment(NullAlign.getQuantity());
   1498     Address SrcPtr(Builder.CreateBitCast(NullVariable, Builder.getInt8PtrTy()),
   1499                    NullAlign);
   1500 
   1501     if (vla) return emitNonZeroVLAInit(*this, Ty, DestPtr, SrcPtr, SizeVal);
   1502 
   1503     // Get and call the appropriate llvm.memcpy overload.
   1504     Builder.CreateMemCpy(DestPtr, SrcPtr, SizeVal, false);
   1505     return;
   1506   }
   1507 
   1508   // Otherwise, just memset the whole thing to zero.  This is legal
   1509   // because in LLVM, all default initializers (other than the ones we just
   1510   // handled above) are guaranteed to have a bit pattern of all zeros.
   1511   Builder.CreateMemSet(DestPtr, Builder.getInt8(0), SizeVal, false);
   1512 }
   1513 
   1514 llvm::BlockAddress *CodeGenFunction::GetAddrOfLabel(const LabelDecl *L) {
   1515   // Make sure that there is a block for the indirect goto.
   1516   if (!IndirectBranch)
   1517     GetIndirectGotoBlock();
   1518 
   1519   llvm::BasicBlock *BB = getJumpDestForLabel(L).getBlock();
   1520 
   1521   // Make sure the indirect branch includes all of the address-taken blocks.
   1522   IndirectBranch->addDestination(BB);
   1523   return llvm::BlockAddress::get(CurFn, BB);
   1524 }
   1525 
   1526 llvm::BasicBlock *CodeGenFunction::GetIndirectGotoBlock() {
   1527   // If we already made the indirect branch for indirect goto, return its block.
   1528   if (IndirectBranch) return IndirectBranch->getParent();
   1529 
   1530   CGBuilderTy TmpBuilder(*this, createBasicBlock("indirectgoto"));
   1531 
   1532   // Create the PHI node that indirect gotos will add entries to.
   1533   llvm::Value *DestVal = TmpBuilder.CreatePHI(Int8PtrTy, 0,
   1534                                               "indirect.goto.dest");
   1535 
   1536   // Create the indirect branch instruction.
   1537   IndirectBranch = TmpBuilder.CreateIndirectBr(DestVal);
   1538   return IndirectBranch->getParent();
   1539 }
   1540 
   1541 /// Computes the length of an array in elements, as well as the base
   1542 /// element type and a properly-typed first element pointer.
   1543 llvm::Value *CodeGenFunction::emitArrayLength(const ArrayType *origArrayType,
   1544                                               QualType &baseType,
   1545                                               Address &addr) {
   1546   const ArrayType *arrayType = origArrayType;
   1547 
   1548   // If it's a VLA, we have to load the stored size.  Note that
   1549   // this is the size of the VLA in bytes, not its size in elements.
   1550   llvm::Value *numVLAElements = nullptr;
   1551   if (isa<VariableArrayType>(arrayType)) {
   1552     numVLAElements = getVLASize(cast<VariableArrayType>(arrayType)).first;
   1553 
   1554     // Walk into all VLAs.  This doesn't require changes to addr,
   1555     // which has type T* where T is the first non-VLA element type.
   1556     do {
   1557       QualType elementType = arrayType->getElementType();
   1558       arrayType = getContext().getAsArrayType(elementType);
   1559 
   1560       // If we only have VLA components, 'addr' requires no adjustment.
   1561       if (!arrayType) {
   1562         baseType = elementType;
   1563         return numVLAElements;
   1564       }
   1565     } while (isa<VariableArrayType>(arrayType));
   1566 
   1567     // We get out here only if we find a constant array type
   1568     // inside the VLA.
   1569   }
   1570 
   1571   // We have some number of constant-length arrays, so addr should
   1572   // have LLVM type [M x [N x [...]]]*.  Build a GEP that walks
   1573   // down to the first element of addr.
   1574   SmallVector<llvm::Value*, 8> gepIndices;
   1575 
   1576   // GEP down to the array type.
   1577   llvm::ConstantInt *zero = Builder.getInt32(0);
   1578   gepIndices.push_back(zero);
   1579 
   1580   uint64_t countFromCLAs = 1;
   1581   QualType eltType;
   1582 
   1583   llvm::ArrayType *llvmArrayType =
   1584     dyn_cast<llvm::ArrayType>(addr.getElementType());
   1585   while (llvmArrayType) {
   1586     assert(isa<ConstantArrayType>(arrayType));
   1587     assert(cast<ConstantArrayType>(arrayType)->getSize().getZExtValue()
   1588              == llvmArrayType->getNumElements());
   1589 
   1590     gepIndices.push_back(zero);
   1591     countFromCLAs *= llvmArrayType->getNumElements();
   1592     eltType = arrayType->getElementType();
   1593 
   1594     llvmArrayType =
   1595       dyn_cast<llvm::ArrayType>(llvmArrayType->getElementType());
   1596     arrayType = getContext().getAsArrayType(arrayType->getElementType());
   1597     assert((!llvmArrayType || arrayType) &&
   1598            "LLVM and Clang types are out-of-synch");
   1599   }
   1600 
   1601   if (arrayType) {
   1602     // From this point onwards, the Clang array type has been emitted
   1603     // as some other type (probably a packed struct). Compute the array
   1604     // size, and just emit the 'begin' expression as a bitcast.
   1605     while (arrayType) {
   1606       countFromCLAs *=
   1607           cast<ConstantArrayType>(arrayType)->getSize().getZExtValue();
   1608       eltType = arrayType->getElementType();
   1609       arrayType = getContext().getAsArrayType(eltType);
   1610     }
   1611 
   1612     llvm::Type *baseType = ConvertType(eltType);
   1613     addr = Builder.CreateElementBitCast(addr, baseType, "array.begin");
   1614   } else {
   1615     // Create the actual GEP.
   1616     addr = Address(Builder.CreateInBoundsGEP(addr.getPointer(),
   1617                                              gepIndices, "array.begin"),
   1618                    addr.getAlignment());
   1619   }
   1620 
   1621   baseType = eltType;
   1622 
   1623   llvm::Value *numElements
   1624     = llvm::ConstantInt::get(SizeTy, countFromCLAs);
   1625 
   1626   // If we had any VLA dimensions, factor them in.
   1627   if (numVLAElements)
   1628     numElements = Builder.CreateNUWMul(numVLAElements, numElements);
   1629 
   1630   return numElements;
   1631 }
   1632 
   1633 std::pair<llvm::Value*, QualType>
   1634 CodeGenFunction::getVLASize(QualType type) {
   1635   const VariableArrayType *vla = getContext().getAsVariableArrayType(type);
   1636   assert(vla && "type was not a variable array type!");
   1637   return getVLASize(vla);
   1638 }
   1639 
   1640 std::pair<llvm::Value*, QualType>
   1641 CodeGenFunction::getVLASize(const VariableArrayType *type) {
   1642   // The number of elements so far; always size_t.
   1643   llvm::Value *numElements = nullptr;
   1644 
   1645   QualType elementType;
   1646   do {
   1647     elementType = type->getElementType();
   1648     llvm::Value *vlaSize = VLASizeMap[type->getSizeExpr()];
   1649     assert(vlaSize && "no size for VLA!");
   1650     assert(vlaSize->getType() == SizeTy);
   1651 
   1652     if (!numElements) {
   1653       numElements = vlaSize;
   1654     } else {
   1655       // It's undefined behavior if this wraps around, so mark it that way.
   1656       // FIXME: Teach -fsanitize=undefined to trap this.
   1657       numElements = Builder.CreateNUWMul(numElements, vlaSize);
   1658     }
   1659   } while ((type = getContext().getAsVariableArrayType(elementType)));
   1660 
   1661   return std::pair<llvm::Value*,QualType>(numElements, elementType);
   1662 }
   1663 
   1664 void CodeGenFunction::EmitVariablyModifiedType(QualType type) {
   1665   assert(type->isVariablyModifiedType() &&
   1666          "Must pass variably modified type to EmitVLASizes!");
   1667 
   1668   EnsureInsertPoint();
   1669 
   1670   // We're going to walk down into the type and look for VLA
   1671   // expressions.
   1672   do {
   1673     assert(type->isVariablyModifiedType());
   1674 
   1675     const Type *ty = type.getTypePtr();
   1676     switch (ty->getTypeClass()) {
   1677 
   1678 #define TYPE(Class, Base)
   1679 #define ABSTRACT_TYPE(Class, Base)
   1680 #define NON_CANONICAL_TYPE(Class, Base)
   1681 #define DEPENDENT_TYPE(Class, Base) case Type::Class:
   1682 #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base)
   1683 #include "clang/AST/TypeNodes.def"
   1684       llvm_unreachable("unexpected dependent type!");
   1685 
   1686     // These types are never variably-modified.
   1687     case Type::Builtin:
   1688     case Type::Complex:
   1689     case Type::Vector:
   1690     case Type::ExtVector:
   1691     case Type::Record:
   1692     case Type::Enum:
   1693     case Type::Elaborated:
   1694     case Type::TemplateSpecialization:
   1695     case Type::ObjCObject:
   1696     case Type::ObjCInterface:
   1697     case Type::ObjCObjectPointer:
   1698       llvm_unreachable("type class is never variably-modified!");
   1699 
   1700     case Type::Adjusted:
   1701       type = cast<AdjustedType>(ty)->getAdjustedType();
   1702       break;
   1703 
   1704     case Type::Decayed:
   1705       type = cast<DecayedType>(ty)->getPointeeType();
   1706       break;
   1707 
   1708     case Type::Pointer:
   1709       type = cast<PointerType>(ty)->getPointeeType();
   1710       break;
   1711 
   1712     case Type::BlockPointer:
   1713       type = cast<BlockPointerType>(ty)->getPointeeType();
   1714       break;
   1715 
   1716     case Type::LValueReference:
   1717     case Type::RValueReference:
   1718       type = cast<ReferenceType>(ty)->getPointeeType();
   1719       break;
   1720 
   1721     case Type::MemberPointer:
   1722       type = cast<MemberPointerType>(ty)->getPointeeType();
   1723       break;
   1724 
   1725     case Type::ConstantArray:
   1726     case Type::IncompleteArray:
   1727       // Losing element qualification here is fine.
   1728       type = cast<ArrayType>(ty)->getElementType();
   1729       break;
   1730 
   1731     case Type::VariableArray: {
   1732       // Losing element qualification here is fine.
   1733       const VariableArrayType *vat = cast<VariableArrayType>(ty);
   1734 
   1735       // Unknown size indication requires no size computation.
   1736       // Otherwise, evaluate and record it.
   1737       if (const Expr *size = vat->getSizeExpr()) {
   1738         // It's possible that we might have emitted this already,
   1739         // e.g. with a typedef and a pointer to it.
   1740         llvm::Value *&entry = VLASizeMap[size];
   1741         if (!entry) {
   1742           llvm::Value *Size = EmitScalarExpr(size);
   1743 
   1744           // C11 6.7.6.2p5:
   1745           //   If the size is an expression that is not an integer constant
   1746           //   expression [...] each time it is evaluated it shall have a value
   1747           //   greater than zero.
   1748           if (SanOpts.has(SanitizerKind::VLABound) &&
   1749               size->getType()->isSignedIntegerType()) {
   1750             SanitizerScope SanScope(this);
   1751             llvm::Value *Zero = llvm::Constant::getNullValue(Size->getType());
   1752             llvm::Constant *StaticArgs[] = {
   1753               EmitCheckSourceLocation(size->getLocStart()),
   1754               EmitCheckTypeDescriptor(size->getType())
   1755             };
   1756             EmitCheck(std::make_pair(Builder.CreateICmpSGT(Size, Zero),
   1757                                      SanitizerKind::VLABound),
   1758                       "vla_bound_not_positive", StaticArgs, Size);
   1759           }
   1760 
   1761           // Always zexting here would be wrong if it weren't
   1762           // undefined behavior to have a negative bound.
   1763           entry = Builder.CreateIntCast(Size, SizeTy, /*signed*/ false);
   1764         }
   1765       }
   1766       type = vat->getElementType();
   1767       break;
   1768     }
   1769 
   1770     case Type::FunctionProto:
   1771     case Type::FunctionNoProto:
   1772       type = cast<FunctionType>(ty)->getReturnType();
   1773       break;
   1774 
   1775     case Type::Paren:
   1776     case Type::TypeOf:
   1777     case Type::UnaryTransform:
   1778     case Type::Attributed:
   1779     case Type::SubstTemplateTypeParm:
   1780     case Type::PackExpansion:
   1781       // Keep walking after single level desugaring.
   1782       type = type.getSingleStepDesugaredType(getContext());
   1783       break;
   1784 
   1785     case Type::Typedef:
   1786     case Type::Decltype:
   1787     case Type::Auto:
   1788       // Stop walking: nothing to do.
   1789       return;
   1790 
   1791     case Type::TypeOfExpr:
   1792       // Stop walking: emit typeof expression.
   1793       EmitIgnoredExpr(cast<TypeOfExprType>(ty)->getUnderlyingExpr());
   1794       return;
   1795 
   1796     case Type::Atomic:
   1797       type = cast<AtomicType>(ty)->getValueType();
   1798       break;
   1799 
   1800     case Type::Pipe:
   1801       type = cast<PipeType>(ty)->getElementType();
   1802       break;
   1803     }
   1804   } while (type->isVariablyModifiedType());
   1805 }
   1806 
   1807 Address CodeGenFunction::EmitVAListRef(const Expr* E) {
   1808   if (getContext().getBuiltinVaListType()->isArrayType())
   1809     return EmitPointerWithAlignment(E);
   1810   return EmitLValue(E).getAddress();
   1811 }
   1812 
   1813 Address CodeGenFunction::EmitMSVAListRef(const Expr *E) {
   1814   return EmitLValue(E).getAddress();
   1815 }
   1816 
   1817 void CodeGenFunction::EmitDeclRefExprDbgValue(const DeclRefExpr *E,
   1818                                               llvm::Constant *Init) {
   1819   assert (Init && "Invalid DeclRefExpr initializer!");
   1820   if (CGDebugInfo *Dbg = getDebugInfo())
   1821     if (CGM.getCodeGenOpts().getDebugInfo() >= codegenoptions::LimitedDebugInfo)
   1822       Dbg->EmitGlobalVariable(E->getDecl(), Init);
   1823 }
   1824 
   1825 CodeGenFunction::PeepholeProtection
   1826 CodeGenFunction::protectFromPeepholes(RValue rvalue) {
   1827   // At the moment, the only aggressive peephole we do in IR gen
   1828   // is trunc(zext) folding, but if we add more, we can easily
   1829   // extend this protection.
   1830 
   1831   if (!rvalue.isScalar()) return PeepholeProtection();
   1832   llvm::Value *value = rvalue.getScalarVal();
   1833   if (!isa<llvm::ZExtInst>(value)) return PeepholeProtection();
   1834 
   1835   // Just make an extra bitcast.
   1836   assert(HaveInsertPoint());
   1837   llvm::Instruction *inst = new llvm::BitCastInst(value, value->getType(), "",
   1838                                                   Builder.GetInsertBlock());
   1839 
   1840   PeepholeProtection protection;
   1841   protection.Inst = inst;
   1842   return protection;
   1843 }
   1844 
   1845 void CodeGenFunction::unprotectFromPeepholes(PeepholeProtection protection) {
   1846   if (!protection.Inst) return;
   1847 
   1848   // In theory, we could try to duplicate the peepholes now, but whatever.
   1849   protection.Inst->eraseFromParent();
   1850 }
   1851 
   1852 llvm::Value *CodeGenFunction::EmitAnnotationCall(llvm::Value *AnnotationFn,
   1853                                                  llvm::Value *AnnotatedVal,
   1854                                                  StringRef AnnotationStr,
   1855                                                  SourceLocation Location) {
   1856   llvm::Value *Args[4] = {
   1857     AnnotatedVal,
   1858     Builder.CreateBitCast(CGM.EmitAnnotationString(AnnotationStr), Int8PtrTy),
   1859     Builder.CreateBitCast(CGM.EmitAnnotationUnit(Location), Int8PtrTy),
   1860     CGM.EmitAnnotationLineNo(Location)
   1861   };
   1862   return Builder.CreateCall(AnnotationFn, Args);
   1863 }
   1864 
   1865 void CodeGenFunction::EmitVarAnnotations(const VarDecl *D, llvm::Value *V) {
   1866   assert(D->hasAttr<AnnotateAttr>() && "no annotate attribute");
   1867   // FIXME We create a new bitcast for every annotation because that's what
   1868   // llvm-gcc was doing.
   1869   for (const auto *I : D->specific_attrs<AnnotateAttr>())
   1870     EmitAnnotationCall(CGM.getIntrinsic(llvm::Intrinsic::var_annotation),
   1871                        Builder.CreateBitCast(V, CGM.Int8PtrTy, V->getName()),
   1872                        I->getAnnotation(), D->getLocation());
   1873 }
   1874 
   1875 Address CodeGenFunction::EmitFieldAnnotations(const FieldDecl *D,
   1876                                               Address Addr) {
   1877   assert(D->hasAttr<AnnotateAttr>() && "no annotate attribute");
   1878   llvm::Value *V = Addr.getPointer();
   1879   llvm::Type *VTy = V->getType();
   1880   llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::ptr_annotation,
   1881                                     CGM.Int8PtrTy);
   1882 
   1883   for (const auto *I : D->specific_attrs<AnnotateAttr>()) {
   1884     // FIXME Always emit the cast inst so we can differentiate between
   1885     // annotation on the first field of a struct and annotation on the struct
   1886     // itself.
   1887     if (VTy != CGM.Int8PtrTy)
   1888       V = Builder.Insert(new llvm::BitCastInst(V, CGM.Int8PtrTy));
   1889     V = EmitAnnotationCall(F, V, I->getAnnotation(), D->getLocation());
   1890     V = Builder.CreateBitCast(V, VTy);
   1891   }
   1892 
   1893   return Address(V, Addr.getAlignment());
   1894 }
   1895 
   1896 CodeGenFunction::CGCapturedStmtInfo::~CGCapturedStmtInfo() { }
   1897 
   1898 CodeGenFunction::SanitizerScope::SanitizerScope(CodeGenFunction *CGF)
   1899     : CGF(CGF) {
   1900   assert(!CGF->IsSanitizerScope);
   1901   CGF->IsSanitizerScope = true;
   1902 }
   1903 
   1904 CodeGenFunction::SanitizerScope::~SanitizerScope() {
   1905   CGF->IsSanitizerScope = false;
   1906 }
   1907 
   1908 void CodeGenFunction::InsertHelper(llvm::Instruction *I,
   1909                                    const llvm::Twine &Name,
   1910                                    llvm::BasicBlock *BB,
   1911                                    llvm::BasicBlock::iterator InsertPt) const {
   1912   LoopStack.InsertHelper(I);
   1913   if (IsSanitizerScope)
   1914     CGM.getSanitizerMetadata()->disableSanitizerForInstruction(I);
   1915 }
   1916 
   1917 void CGBuilderInserter::InsertHelper(
   1918     llvm::Instruction *I, const llvm::Twine &Name, llvm::BasicBlock *BB,
   1919     llvm::BasicBlock::iterator InsertPt) const {
   1920   llvm::IRBuilderDefaultInserter::InsertHelper(I, Name, BB, InsertPt);
   1921   if (CGF)
   1922     CGF->InsertHelper(I, Name, BB, InsertPt);
   1923 }
   1924 
   1925 static bool hasRequiredFeatures(const SmallVectorImpl<StringRef> &ReqFeatures,
   1926                                 CodeGenModule &CGM, const FunctionDecl *FD,
   1927                                 std::string &FirstMissing) {
   1928   // If there aren't any required features listed then go ahead and return.
   1929   if (ReqFeatures.empty())
   1930     return false;
   1931 
   1932   // Now build up the set of caller features and verify that all the required
   1933   // features are there.
   1934   llvm::StringMap<bool> CallerFeatureMap;
   1935   CGM.getFunctionFeatureMap(CallerFeatureMap, FD);
   1936 
   1937   // If we have at least one of the features in the feature list return
   1938   // true, otherwise return false.
   1939   return std::all_of(
   1940       ReqFeatures.begin(), ReqFeatures.end(), [&](StringRef Feature) {
   1941         SmallVector<StringRef, 1> OrFeatures;
   1942         Feature.split(OrFeatures, "|");
   1943         return std::any_of(OrFeatures.begin(), OrFeatures.end(),
   1944                            [&](StringRef Feature) {
   1945                              if (!CallerFeatureMap.lookup(Feature)) {
   1946                                FirstMissing = Feature.str();
   1947                                return false;
   1948                              }
   1949                              return true;
   1950                            });
   1951       });
   1952 }
   1953 
   1954 // Emits an error if we don't have a valid set of target features for the
   1955 // called function.
   1956 void CodeGenFunction::checkTargetFeatures(const CallExpr *E,
   1957                                           const FunctionDecl *TargetDecl) {
   1958   // Early exit if this is an indirect call.
   1959   if (!TargetDecl)
   1960     return;
   1961 
   1962   // Get the current enclosing function if it exists. If it doesn't
   1963   // we can't check the target features anyhow.
   1964   const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl);
   1965   if (!FD)
   1966     return;
   1967 
   1968   // Grab the required features for the call. For a builtin this is listed in
   1969   // the td file with the default cpu, for an always_inline function this is any
   1970   // listed cpu and any listed features.
   1971   unsigned BuiltinID = TargetDecl->getBuiltinID();
   1972   std::string MissingFeature;
   1973   if (BuiltinID) {
   1974     SmallVector<StringRef, 1> ReqFeatures;
   1975     const char *FeatureList =
   1976         CGM.getContext().BuiltinInfo.getRequiredFeatures(BuiltinID);
   1977     // Return if the builtin doesn't have any required features.
   1978     if (!FeatureList || StringRef(FeatureList) == "")
   1979       return;
   1980     StringRef(FeatureList).split(ReqFeatures, ",");
   1981     if (!hasRequiredFeatures(ReqFeatures, CGM, FD, MissingFeature))
   1982       CGM.getDiags().Report(E->getLocStart(), diag::err_builtin_needs_feature)
   1983           << TargetDecl->getDeclName()
   1984           << CGM.getContext().BuiltinInfo.getRequiredFeatures(BuiltinID);
   1985 
   1986   } else if (TargetDecl->hasAttr<TargetAttr>()) {
   1987     // Get the required features for the callee.
   1988     SmallVector<StringRef, 1> ReqFeatures;
   1989     llvm::StringMap<bool> CalleeFeatureMap;
   1990     CGM.getFunctionFeatureMap(CalleeFeatureMap, TargetDecl);
   1991     for (const auto &F : CalleeFeatureMap) {
   1992       // Only positive features are "required".
   1993       if (F.getValue())
   1994         ReqFeatures.push_back(F.getKey());
   1995     }
   1996     if (!hasRequiredFeatures(ReqFeatures, CGM, FD, MissingFeature))
   1997       CGM.getDiags().Report(E->getLocStart(), diag::err_function_needs_feature)
   1998           << FD->getDeclName() << TargetDecl->getDeclName() << MissingFeature;
   1999   }
   2000 }
   2001 
   2002 void CodeGenFunction::EmitSanitizerStatReport(llvm::SanitizerStatKind SSK) {
   2003   if (!CGM.getCodeGenOpts().SanitizeStats)
   2004     return;
   2005 
   2006   llvm::IRBuilder<> IRB(Builder.GetInsertBlock(), Builder.GetInsertPoint());
   2007   IRB.SetCurrentDebugLocation(Builder.getCurrentDebugLocation());
   2008   CGM.getSanStats().create(IRB, SSK);
   2009 }
   2010