1 //===--- CodeGenFunction.cpp - Emit LLVM Code from ASTs for a Function ----===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This coordinates the per-function state used while generating code.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "CodeGenFunction.h"
14 #include "CGBlocks.h"
15 #include "CGCleanup.h"
16 #include "CGCUDARuntime.h"
17 #include "CGCXXABI.h"
18 #include "CGDebugInfo.h"
19 #include "CGOpenMPRuntime.h"
20 #include "CodeGenModule.h"
21 #include "CodeGenPGO.h"
22 #include "TargetInfo.h"
23 #include "clang/AST/ASTContext.h"
24 #include "clang/AST/ASTLambda.h"
25 #include "clang/AST/Decl.h"
26 #include "clang/AST/DeclCXX.h"
27 #include "clang/AST/StmtCXX.h"
28 #include "clang/AST/StmtObjC.h"
29 #include "clang/Basic/Builtins.h"
30 #include "clang/Basic/CodeGenOptions.h"
31 #include "clang/Basic/TargetInfo.h"
32 #include "clang/CodeGen/CGFunctionInfo.h"
33 #include "clang/Frontend/FrontendDiagnostic.h"
34 #include "llvm/IR/DataLayout.h"
35 #include "llvm/IR/Dominators.h"
36 #include "llvm/IR/Intrinsics.h"
37 #include "llvm/IR/MDBuilder.h"
38 #include "llvm/IR/Operator.h"
39 #include "llvm/Transforms/Utils/PromoteMemToReg.h"
40 using namespace clang;
41 using namespace CodeGen;
42 
43 /// shouldEmitLifetimeMarkers - Decide whether we need emit the life-time
44 /// markers.
45 static bool shouldEmitLifetimeMarkers(const CodeGenOptions &CGOpts,
46                                       const LangOptions &LangOpts) {
47   if (CGOpts.DisableLifetimeMarkers)
48     return false;
49 
50   // Disable lifetime markers in msan builds.
51   // FIXME: Remove this when msan works with lifetime markers.
52   if (LangOpts.Sanitize.has(SanitizerKind::Memory))
53     return false;
54 
55   // Asan uses markers for use-after-scope checks.
56   if (CGOpts.SanitizeAddressUseAfterScope)
57     return true;
58 
59   // For now, only in optimized builds.
60   return CGOpts.OptimizationLevel != 0;
61 }
62 
63 CodeGenFunction::CodeGenFunction(CodeGenModule &cgm, bool suppressNewContext)
64     : CodeGenTypeCache(cgm), CGM(cgm), Target(cgm.getTarget()),
65       Builder(cgm, cgm.getModule().getContext(), llvm::ConstantFolder(),
66               CGBuilderInserterTy(this)),
67       SanOpts(CGM.getLangOpts().Sanitize), DebugInfo(CGM.getModuleDebugInfo()),
68       PGO(cgm), ShouldEmitLifetimeMarkers(shouldEmitLifetimeMarkers(
69                     CGM.getCodeGenOpts(), CGM.getLangOpts())) {
70   if (!suppressNewContext)
71     CGM.getCXXABI().getMangleContext().startNewFunction();
72 
73   llvm::FastMathFlags FMF;
74   if (CGM.getLangOpts().FastMath)
75     FMF.setFast();
76   if (CGM.getLangOpts().FiniteMathOnly) {
77     FMF.setNoNaNs();
78     FMF.setNoInfs();
79   }
80   if (CGM.getCodeGenOpts().NoNaNsFPMath) {
81     FMF.setNoNaNs();
82   }
83   if (CGM.getCodeGenOpts().NoSignedZeros) {
84     FMF.setNoSignedZeros();
85   }
86   if (CGM.getCodeGenOpts().ReciprocalMath) {
87     FMF.setAllowReciprocal();
88   }
89   if (CGM.getCodeGenOpts().Reassociate) {
90     FMF.setAllowReassoc();
91   }
92   Builder.setFastMathFlags(FMF);
93 }
94 
95 CodeGenFunction::~CodeGenFunction() {
96   assert(LifetimeExtendedCleanupStack.empty() && "failed to emit a cleanup");
97 
98   // If there are any unclaimed block infos, go ahead and destroy them
99   // now.  This can happen if IR-gen gets clever and skips evaluating
100   // something.
101   if (FirstBlockInfo)
102     destroyBlockInfos(FirstBlockInfo);
103 
104   if (getLangOpts().OpenMP && CurFn)
105     CGM.getOpenMPRuntime().functionFinished(*this);
106 }
107 
108 CharUnits CodeGenFunction::getNaturalPointeeTypeAlignment(QualType T,
109                                                     LValueBaseInfo *BaseInfo,
110                                                     TBAAAccessInfo *TBAAInfo) {
111   return getNaturalTypeAlignment(T->getPointeeType(), BaseInfo, TBAAInfo,
112                                  /* forPointeeType= */ true);
113 }
114 
115 CharUnits CodeGenFunction::getNaturalTypeAlignment(QualType T,
116                                                    LValueBaseInfo *BaseInfo,
117                                                    TBAAAccessInfo *TBAAInfo,
118                                                    bool forPointeeType) {
119   if (TBAAInfo)
120     *TBAAInfo = CGM.getTBAAAccessInfo(T);
121 
122   // Honor alignment typedef attributes even on incomplete types.
123   // We also honor them straight for C++ class types, even as pointees;
124   // there's an expressivity gap here.
125   if (auto TT = T->getAs<TypedefType>()) {
126     if (auto Align = TT->getDecl()->getMaxAlignment()) {
127       if (BaseInfo)
128         *BaseInfo = LValueBaseInfo(AlignmentSource::AttributedType);
129       return getContext().toCharUnitsFromBits(Align);
130     }
131   }
132 
133   if (BaseInfo)
134     *BaseInfo = LValueBaseInfo(AlignmentSource::Type);
135 
136   CharUnits Alignment;
137   if (T->isIncompleteType()) {
138     Alignment = CharUnits::One(); // Shouldn't be used, but pessimistic is best.
139   } else {
140     // For C++ class pointees, we don't know whether we're pointing at a
141     // base or a complete object, so we generally need to use the
142     // non-virtual alignment.
143     const CXXRecordDecl *RD;
144     if (forPointeeType && (RD = T->getAsCXXRecordDecl())) {
145       Alignment = CGM.getClassPointerAlignment(RD);
146     } else {
147       Alignment = getContext().getTypeAlignInChars(T);
148       if (T.getQualifiers().hasUnaligned())
149         Alignment = CharUnits::One();
150     }
151 
152     // Cap to the global maximum type alignment unless the alignment
153     // was somehow explicit on the type.
154     if (unsigned MaxAlign = getLangOpts().MaxTypeAlign) {
155       if (Alignment.getQuantity() > MaxAlign &&
156           !getContext().isAlignmentRequired(T))
157         Alignment = CharUnits::fromQuantity(MaxAlign);
158     }
159   }
160   return Alignment;
161 }
162 
163 LValue CodeGenFunction::MakeNaturalAlignAddrLValue(llvm::Value *V, QualType T) {
164   LValueBaseInfo BaseInfo;
165   TBAAAccessInfo TBAAInfo;
166   CharUnits Alignment = getNaturalTypeAlignment(T, &BaseInfo, &TBAAInfo);
167   return LValue::MakeAddr(Address(V, Alignment), T, getContext(), BaseInfo,
168                           TBAAInfo);
169 }
170 
171 /// Given a value of type T* that may not be to a complete object,
172 /// construct an l-value with the natural pointee alignment of T.
173 LValue
174 CodeGenFunction::MakeNaturalAlignPointeeAddrLValue(llvm::Value *V, QualType T) {
175   LValueBaseInfo BaseInfo;
176   TBAAAccessInfo TBAAInfo;
177   CharUnits Align = getNaturalTypeAlignment(T, &BaseInfo, &TBAAInfo,
178                                             /* forPointeeType= */ true);
179   return MakeAddrLValue(Address(V, Align), T, BaseInfo, TBAAInfo);
180 }
181 
182 
183 llvm::Type *CodeGenFunction::ConvertTypeForMem(QualType T) {
184   return CGM.getTypes().ConvertTypeForMem(T);
185 }
186 
187 llvm::Type *CodeGenFunction::ConvertType(QualType T) {
188   return CGM.getTypes().ConvertType(T);
189 }
190 
191 TypeEvaluationKind CodeGenFunction::getEvaluationKind(QualType type) {
192   type = type.getCanonicalType();
193   while (true) {
194     switch (type->getTypeClass()) {
195 #define TYPE(name, parent)
196 #define ABSTRACT_TYPE(name, parent)
197 #define NON_CANONICAL_TYPE(name, parent) case Type::name:
198 #define DEPENDENT_TYPE(name, parent) case Type::name:
199 #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(name, parent) case Type::name:
200 #include "clang/AST/TypeNodes.def"
201       llvm_unreachable("non-canonical or dependent type in IR-generation");
202 
203     case Type::Auto:
204     case Type::DeducedTemplateSpecialization:
205       llvm_unreachable("undeduced type in IR-generation");
206 
207     // Various scalar types.
208     case Type::Builtin:
209     case Type::Pointer:
210     case Type::BlockPointer:
211     case Type::LValueReference:
212     case Type::RValueReference:
213     case Type::MemberPointer:
214     case Type::Vector:
215     case Type::ExtVector:
216     case Type::FunctionProto:
217     case Type::FunctionNoProto:
218     case Type::Enum:
219     case Type::ObjCObjectPointer:
220     case Type::Pipe:
221       return TEK_Scalar;
222 
223     // Complexes.
224     case Type::Complex:
225       return TEK_Complex;
226 
227     // Arrays, records, and Objective-C objects.
228     case Type::ConstantArray:
229     case Type::IncompleteArray:
230     case Type::VariableArray:
231     case Type::Record:
232     case Type::ObjCObject:
233     case Type::ObjCInterface:
234       return TEK_Aggregate;
235 
236     // We operate on atomic values according to their underlying type.
237     case Type::Atomic:
238       type = cast<AtomicType>(type)->getValueType();
239       continue;
240     }
241     llvm_unreachable("unknown type kind!");
242   }
243 }
244 
245 llvm::DebugLoc CodeGenFunction::EmitReturnBlock() {
246   // For cleanliness, we try to avoid emitting the return block for
247   // simple cases.
248   llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
249 
250   if (CurBB) {
251     assert(!CurBB->getTerminator() && "Unexpected terminated block.");
252 
253     // We have a valid insert point, reuse it if it is empty or there are no
254     // explicit jumps to the return block.
255     if (CurBB->empty() || ReturnBlock.getBlock()->use_empty()) {
256       ReturnBlock.getBlock()->replaceAllUsesWith(CurBB);
257       delete ReturnBlock.getBlock();
258     } else
259       EmitBlock(ReturnBlock.getBlock());
260     return llvm::DebugLoc();
261   }
262 
263   // Otherwise, if the return block is the target of a single direct
264   // branch then we can just put the code in that block instead. This
265   // cleans up functions which started with a unified return block.
266   if (ReturnBlock.getBlock()->hasOneUse()) {
267     llvm::BranchInst *BI =
268       dyn_cast<llvm::BranchInst>(*ReturnBlock.getBlock()->user_begin());
269     if (BI && BI->isUnconditional() &&
270         BI->getSuccessor(0) == ReturnBlock.getBlock()) {
271       // Record/return the DebugLoc of the simple 'return' expression to be used
272       // later by the actual 'ret' instruction.
273       llvm::DebugLoc Loc = BI->getDebugLoc();
274       Builder.SetInsertPoint(BI->getParent());
275       BI->eraseFromParent();
276       delete ReturnBlock.getBlock();
277       return Loc;
278     }
279   }
280 
281   // FIXME: We are at an unreachable point, there is no reason to emit the block
282   // unless it has uses. However, we still need a place to put the debug
283   // region.end for now.
284 
285   EmitBlock(ReturnBlock.getBlock());
286   return llvm::DebugLoc();
287 }
288 
289 static void EmitIfUsed(CodeGenFunction &CGF, llvm::BasicBlock *BB) {
290   if (!BB) return;
291   if (!BB->use_empty())
292     return CGF.CurFn->getBasicBlockList().push_back(BB);
293   delete BB;
294 }
295 
296 void CodeGenFunction::FinishFunction(SourceLocation EndLoc) {
297   assert(BreakContinueStack.empty() &&
298          "mismatched push/pop in break/continue stack!");
299 
300   bool OnlySimpleReturnStmts = NumSimpleReturnExprs > 0
301     && NumSimpleReturnExprs == NumReturnExprs
302     && ReturnBlock.getBlock()->use_empty();
303   // Usually the return expression is evaluated before the cleanup
304   // code.  If the function contains only a simple return statement,
305   // such as a constant, the location before the cleanup code becomes
306   // the last useful breakpoint in the function, because the simple
307   // return expression will be evaluated after the cleanup code. To be
308   // safe, set the debug location for cleanup code to the location of
309   // the return statement.  Otherwise the cleanup code should be at the
310   // end of the function's lexical scope.
311   //
312   // If there are multiple branches to the return block, the branch
313   // instructions will get the location of the return statements and
314   // all will be fine.
315   if (CGDebugInfo *DI = getDebugInfo()) {
316     if (OnlySimpleReturnStmts)
317       DI->EmitLocation(Builder, LastStopPoint);
318     else
319       DI->EmitLocation(Builder, EndLoc);
320   }
321 
322   // Pop any cleanups that might have been associated with the
323   // parameters.  Do this in whatever block we're currently in; it's
324   // important to do this before we enter the return block or return
325   // edges will be *really* confused.
326   bool HasCleanups = EHStack.stable_begin() != PrologueCleanupDepth;
327   bool HasOnlyLifetimeMarkers =
328       HasCleanups && EHStack.containsOnlyLifetimeMarkers(PrologueCleanupDepth);
329   bool EmitRetDbgLoc = !HasCleanups || HasOnlyLifetimeMarkers;
330   if (HasCleanups) {
331     // Make sure the line table doesn't jump back into the body for
332     // the ret after it's been at EndLoc.
333     if (CGDebugInfo *DI = getDebugInfo())
334       if (OnlySimpleReturnStmts)
335         DI->EmitLocation(Builder, EndLoc);
336 
337     PopCleanupBlocks(PrologueCleanupDepth);
338   }
339 
340   // Emit function epilog (to return).
341   llvm::DebugLoc Loc = EmitReturnBlock();
342 
343   if (ShouldInstrumentFunction()) {
344     if (CGM.getCodeGenOpts().InstrumentFunctions)
345       CurFn->addFnAttr("instrument-function-exit", "__cyg_profile_func_exit");
346     if (CGM.getCodeGenOpts().InstrumentFunctionsAfterInlining)
347       CurFn->addFnAttr("instrument-function-exit-inlined",
348                        "__cyg_profile_func_exit");
349   }
350 
351   // Emit debug descriptor for function end.
352   if (CGDebugInfo *DI = getDebugInfo())
353     DI->EmitFunctionEnd(Builder, CurFn);
354 
355   // Reset the debug location to that of the simple 'return' expression, if any
356   // rather than that of the end of the function's scope '}'.
357   ApplyDebugLocation AL(*this, Loc);
358   EmitFunctionEpilog(*CurFnInfo, EmitRetDbgLoc, EndLoc);
359   EmitEndEHSpec(CurCodeDecl);
360 
361   assert(EHStack.empty() &&
362          "did not remove all scopes from cleanup stack!");
363 
364   // If someone did an indirect goto, emit the indirect goto block at the end of
365   // the function.
366   if (IndirectBranch) {
367     EmitBlock(IndirectBranch->getParent());
368     Builder.ClearInsertionPoint();
369   }
370 
371   // If some of our locals escaped, insert a call to llvm.localescape in the
372   // entry block.
373   if (!EscapedLocals.empty()) {
374     // Invert the map from local to index into a simple vector. There should be
375     // no holes.
376     SmallVector<llvm::Value *, 4> EscapeArgs;
377     EscapeArgs.resize(EscapedLocals.size());
378     for (auto &Pair : EscapedLocals)
379       EscapeArgs[Pair.second] = Pair.first;
380     llvm::Function *FrameEscapeFn = llvm::Intrinsic::getDeclaration(
381         &CGM.getModule(), llvm::Intrinsic::localescape);
382     CGBuilderTy(*this, AllocaInsertPt).CreateCall(FrameEscapeFn, EscapeArgs);
383   }
384 
385   // Remove the AllocaInsertPt instruction, which is just a convenience for us.
386   llvm::Instruction *Ptr = AllocaInsertPt;
387   AllocaInsertPt = nullptr;
388   Ptr->eraseFromParent();
389 
390   // If someone took the address of a label but never did an indirect goto, we
391   // made a zero entry PHI node, which is illegal, zap it now.
392   if (IndirectBranch) {
393     llvm::PHINode *PN = cast<llvm::PHINode>(IndirectBranch->getAddress());
394     if (PN->getNumIncomingValues() == 0) {
395       PN->replaceAllUsesWith(llvm::UndefValue::get(PN->getType()));
396       PN->eraseFromParent();
397     }
398   }
399 
400   EmitIfUsed(*this, EHResumeBlock);
401   EmitIfUsed(*this, TerminateLandingPad);
402   EmitIfUsed(*this, TerminateHandler);
403   EmitIfUsed(*this, UnreachableBlock);
404 
405   for (const auto &FuncletAndParent : TerminateFunclets)
406     EmitIfUsed(*this, FuncletAndParent.second);
407 
408   if (CGM.getCodeGenOpts().EmitDeclMetadata)
409     EmitDeclMetadata();
410 
411   for (SmallVectorImpl<std::pair<llvm::Instruction *, llvm::Value *> >::iterator
412            I = DeferredReplacements.begin(),
413            E = DeferredReplacements.end();
414        I != E; ++I) {
415     I->first->replaceAllUsesWith(I->second);
416     I->first->eraseFromParent();
417   }
418 
419   // Eliminate CleanupDestSlot alloca by replacing it with SSA values and
420   // PHIs if the current function is a coroutine. We don't do it for all
421   // functions as it may result in slight increase in numbers of instructions
422   // if compiled with no optimizations. We do it for coroutine as the lifetime
423   // of CleanupDestSlot alloca make correct coroutine frame building very
424   // difficult.
425   if (NormalCleanupDest.isValid() && isCoroutine()) {
426     llvm::DominatorTree DT(*CurFn);
427     llvm::PromoteMemToReg(
428         cast<llvm::AllocaInst>(NormalCleanupDest.getPointer()), DT);
429     NormalCleanupDest = Address::invalid();
430   }
431 
432   // Scan function arguments for vector width.
433   for (llvm::Argument &A : CurFn->args())
434     if (auto *VT = dyn_cast<llvm::VectorType>(A.getType()))
435       LargestVectorWidth = std::max(LargestVectorWidth,
436                                     VT->getPrimitiveSizeInBits());
437 
438   // Update vector width based on return type.
439   if (auto *VT = dyn_cast<llvm::VectorType>(CurFn->getReturnType()))
440     LargestVectorWidth = std::max(LargestVectorWidth,
441                                   VT->getPrimitiveSizeInBits());
442 
443   // Add the required-vector-width attribute. This contains the max width from:
444   // 1. min-vector-width attribute used in the source program.
445   // 2. Any builtins used that have a vector width specified.
446   // 3. Values passed in and out of inline assembly.
447   // 4. Width of vector arguments and return types for this function.
448   // 5. Width of vector aguments and return types for functions called by this
449   //    function.
450   CurFn->addFnAttr("min-legal-vector-width", llvm::utostr(LargestVectorWidth));
451 }
452 
453 /// ShouldInstrumentFunction - Return true if the current function should be
454 /// instrumented with __cyg_profile_func_* calls
455 bool CodeGenFunction::ShouldInstrumentFunction() {
456   if (!CGM.getCodeGenOpts().InstrumentFunctions &&
457       !CGM.getCodeGenOpts().InstrumentFunctionsAfterInlining &&
458       !CGM.getCodeGenOpts().InstrumentFunctionEntryBare)
459     return false;
460   if (!CurFuncDecl || CurFuncDecl->hasAttr<NoInstrumentFunctionAttr>())
461     return false;
462   return true;
463 }
464 
465 /// ShouldXRayInstrument - Return true if the current function should be
466 /// instrumented with XRay nop sleds.
467 bool CodeGenFunction::ShouldXRayInstrumentFunction() const {
468   return CGM.getCodeGenOpts().XRayInstrumentFunctions;
469 }
470 
471 /// AlwaysEmitXRayCustomEvents - Return true if we should emit IR for calls to
472 /// the __xray_customevent(...) builtin calls, when doing XRay instrumentation.
473 bool CodeGenFunction::AlwaysEmitXRayCustomEvents() const {
474   return CGM.getCodeGenOpts().XRayInstrumentFunctions &&
475          (CGM.getCodeGenOpts().XRayAlwaysEmitCustomEvents ||
476           CGM.getCodeGenOpts().XRayInstrumentationBundle.Mask ==
477               XRayInstrKind::Custom);
478 }
479 
480 bool CodeGenFunction::AlwaysEmitXRayTypedEvents() const {
481   return CGM.getCodeGenOpts().XRayInstrumentFunctions &&
482          (CGM.getCodeGenOpts().XRayAlwaysEmitTypedEvents ||
483           CGM.getCodeGenOpts().XRayInstrumentationBundle.Mask ==
484               XRayInstrKind::Typed);
485 }
486 
487 llvm::Constant *
488 CodeGenFunction::EncodeAddrForUseInPrologue(llvm::Function *F,
489                                             llvm::Constant *Addr) {
490   // Addresses stored in prologue data can't require run-time fixups and must
491   // be PC-relative. Run-time fixups are undesirable because they necessitate
492   // writable text segments, which are unsafe. And absolute addresses are
493   // undesirable because they break PIE mode.
494 
495   // Add a layer of indirection through a private global. Taking its address
496   // won't result in a run-time fixup, even if Addr has linkonce_odr linkage.
497   auto *GV = new llvm::GlobalVariable(CGM.getModule(), Addr->getType(),
498                                       /*isConstant=*/true,
499                                       llvm::GlobalValue::PrivateLinkage, Addr);
500 
501   // Create a PC-relative address.
502   auto *GOTAsInt = llvm::ConstantExpr::getPtrToInt(GV, IntPtrTy);
503   auto *FuncAsInt = llvm::ConstantExpr::getPtrToInt(F, IntPtrTy);
504   auto *PCRelAsInt = llvm::ConstantExpr::getSub(GOTAsInt, FuncAsInt);
505   return (IntPtrTy == Int32Ty)
506              ? PCRelAsInt
507              : llvm::ConstantExpr::getTrunc(PCRelAsInt, Int32Ty);
508 }
509 
510 llvm::Value *
511 CodeGenFunction::DecodeAddrUsedInPrologue(llvm::Value *F,
512                                           llvm::Value *EncodedAddr) {
513   // Reconstruct the address of the global.
514   auto *PCRelAsInt = Builder.CreateSExt(EncodedAddr, IntPtrTy);
515   auto *FuncAsInt = Builder.CreatePtrToInt(F, IntPtrTy, "func_addr.int");
516   auto *GOTAsInt = Builder.CreateAdd(PCRelAsInt, FuncAsInt, "global_addr.int");
517   auto *GOTAddr = Builder.CreateIntToPtr(GOTAsInt, Int8PtrPtrTy, "global_addr");
518 
519   // Load the original pointer through the global.
520   return Builder.CreateLoad(Address(GOTAddr, getPointerAlign()),
521                             "decoded_addr");
522 }
523 
524 static void removeImageAccessQualifier(std::string& TyName) {
525   std::string ReadOnlyQual("__read_only");
526   std::string::size_type ReadOnlyPos = TyName.find(ReadOnlyQual);
527   if (ReadOnlyPos != std::string::npos)
528     // "+ 1" for the space after access qualifier.
529     TyName.erase(ReadOnlyPos, ReadOnlyQual.size() + 1);
530   else {
531     std::string WriteOnlyQual("__write_only");
532     std::string::size_type WriteOnlyPos = TyName.find(WriteOnlyQual);
533     if (WriteOnlyPos != std::string::npos)
534       TyName.erase(WriteOnlyPos, WriteOnlyQual.size() + 1);
535     else {
536       std::string ReadWriteQual("__read_write");
537       std::string::size_type ReadWritePos = TyName.find(ReadWriteQual);
538       if (ReadWritePos != std::string::npos)
539         TyName.erase(ReadWritePos, ReadWriteQual.size() + 1);
540     }
541   }
542 }
543 
544 // Returns the address space id that should be produced to the
545 // kernel_arg_addr_space metadata. This is always fixed to the ids
546 // as specified in the SPIR 2.0 specification in order to differentiate
547 // for example in clGetKernelArgInfo() implementation between the address
548 // spaces with targets without unique mapping to the OpenCL address spaces
549 // (basically all single AS CPUs).
550 static unsigned ArgInfoAddressSpace(LangAS AS) {
551   switch (AS) {
552   case LangAS::opencl_global:   return 1;
553   case LangAS::opencl_constant: return 2;
554   case LangAS::opencl_local:    return 3;
555   case LangAS::opencl_generic:  return 4; // Not in SPIR 2.0 specs.
556   default:
557     return 0; // Assume private.
558   }
559 }
560 
561 // OpenCL v1.2 s5.6.4.6 allows the compiler to store kernel argument
562 // information in the program executable. The argument information stored
563 // includes the argument name, its type, the address and access qualifiers used.
564 static void GenOpenCLArgMetadata(const FunctionDecl *FD, llvm::Function *Fn,
565                                  CodeGenModule &CGM, llvm::LLVMContext &Context,
566                                  CGBuilderTy &Builder, ASTContext &ASTCtx) {
567   // Create MDNodes that represent the kernel arg metadata.
568   // Each MDNode is a list in the form of "key", N number of values which is
569   // the same number of values as their are kernel arguments.
570 
571   const PrintingPolicy &Policy = ASTCtx.getPrintingPolicy();
572 
573   // MDNode for the kernel argument address space qualifiers.
574   SmallVector<llvm::Metadata *, 8> addressQuals;
575 
576   // MDNode for the kernel argument access qualifiers (images only).
577   SmallVector<llvm::Metadata *, 8> accessQuals;
578 
579   // MDNode for the kernel argument type names.
580   SmallVector<llvm::Metadata *, 8> argTypeNames;
581 
582   // MDNode for the kernel argument base type names.
583   SmallVector<llvm::Metadata *, 8> argBaseTypeNames;
584 
585   // MDNode for the kernel argument type qualifiers.
586   SmallVector<llvm::Metadata *, 8> argTypeQuals;
587 
588   // MDNode for the kernel argument names.
589   SmallVector<llvm::Metadata *, 8> argNames;
590 
591   for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
592     const ParmVarDecl *parm = FD->getParamDecl(i);
593     QualType ty = parm->getType();
594     std::string typeQuals;
595 
596     if (ty->isPointerType()) {
597       QualType pointeeTy = ty->getPointeeType();
598 
599       // Get address qualifier.
600       addressQuals.push_back(llvm::ConstantAsMetadata::get(Builder.getInt32(
601         ArgInfoAddressSpace(pointeeTy.getAddressSpace()))));
602 
603       // Get argument type name.
604       std::string typeName =
605           pointeeTy.getUnqualifiedType().getAsString(Policy) + "*";
606 
607       // Turn "unsigned type" to "utype"
608       std::string::size_type pos = typeName.find("unsigned");
609       if (pointeeTy.isCanonical() && pos != std::string::npos)
610         typeName.erase(pos+1, 8);
611 
612       argTypeNames.push_back(llvm::MDString::get(Context, typeName));
613 
614       std::string baseTypeName =
615           pointeeTy.getUnqualifiedType().getCanonicalType().getAsString(
616               Policy) +
617           "*";
618 
619       // Turn "unsigned type" to "utype"
620       pos = baseTypeName.find("unsigned");
621       if (pos != std::string::npos)
622         baseTypeName.erase(pos+1, 8);
623 
624       argBaseTypeNames.push_back(llvm::MDString::get(Context, baseTypeName));
625 
626       // Get argument type qualifiers:
627       if (ty.isRestrictQualified())
628         typeQuals = "restrict";
629       if (pointeeTy.isConstQualified() ||
630           (pointeeTy.getAddressSpace() == LangAS::opencl_constant))
631         typeQuals += typeQuals.empty() ? "const" : " const";
632       if (pointeeTy.isVolatileQualified())
633         typeQuals += typeQuals.empty() ? "volatile" : " volatile";
634     } else {
635       uint32_t AddrSpc = 0;
636       bool isPipe = ty->isPipeType();
637       if (ty->isImageType() || isPipe)
638         AddrSpc = ArgInfoAddressSpace(LangAS::opencl_global);
639 
640       addressQuals.push_back(
641           llvm::ConstantAsMetadata::get(Builder.getInt32(AddrSpc)));
642 
643       // Get argument type name.
644       std::string typeName;
645       if (isPipe)
646         typeName = ty.getCanonicalType()->getAs<PipeType>()->getElementType()
647                      .getAsString(Policy);
648       else
649         typeName = ty.getUnqualifiedType().getAsString(Policy);
650 
651       // Turn "unsigned type" to "utype"
652       std::string::size_type pos = typeName.find("unsigned");
653       if (ty.isCanonical() && pos != std::string::npos)
654         typeName.erase(pos+1, 8);
655 
656       std::string baseTypeName;
657       if (isPipe)
658         baseTypeName = ty.getCanonicalType()->getAs<PipeType>()
659                           ->getElementType().getCanonicalType()
660                           .getAsString(Policy);
661       else
662         baseTypeName =
663           ty.getUnqualifiedType().getCanonicalType().getAsString(Policy);
664 
665       // Remove access qualifiers on images
666       // (as they are inseparable from type in clang implementation,
667       // but OpenCL spec provides a special query to get access qualifier
668       // via clGetKernelArgInfo with CL_KERNEL_ARG_ACCESS_QUALIFIER):
669       if (ty->isImageType()) {
670         removeImageAccessQualifier(typeName);
671         removeImageAccessQualifier(baseTypeName);
672       }
673 
674       argTypeNames.push_back(llvm::MDString::get(Context, typeName));
675 
676       // Turn "unsigned type" to "utype"
677       pos = baseTypeName.find("unsigned");
678       if (pos != std::string::npos)
679         baseTypeName.erase(pos+1, 8);
680 
681       argBaseTypeNames.push_back(llvm::MDString::get(Context, baseTypeName));
682 
683       if (isPipe)
684         typeQuals = "pipe";
685     }
686 
687     argTypeQuals.push_back(llvm::MDString::get(Context, typeQuals));
688 
689     // Get image and pipe access qualifier:
690     if (ty->isImageType()|| ty->isPipeType()) {
691       const Decl *PDecl = parm;
692       if (auto *TD = dyn_cast<TypedefType>(ty))
693         PDecl = TD->getDecl();
694       const OpenCLAccessAttr *A = PDecl->getAttr<OpenCLAccessAttr>();
695       if (A && A->isWriteOnly())
696         accessQuals.push_back(llvm::MDString::get(Context, "write_only"));
697       else if (A && A->isReadWrite())
698         accessQuals.push_back(llvm::MDString::get(Context, "read_write"));
699       else
700         accessQuals.push_back(llvm::MDString::get(Context, "read_only"));
701     } else
702       accessQuals.push_back(llvm::MDString::get(Context, "none"));
703 
704     // Get argument name.
705     argNames.push_back(llvm::MDString::get(Context, parm->getName()));
706   }
707 
708   Fn->setMetadata("kernel_arg_addr_space",
709                   llvm::MDNode::get(Context, addressQuals));
710   Fn->setMetadata("kernel_arg_access_qual",
711                   llvm::MDNode::get(Context, accessQuals));
712   Fn->setMetadata("kernel_arg_type",
713                   llvm::MDNode::get(Context, argTypeNames));
714   Fn->setMetadata("kernel_arg_base_type",
715                   llvm::MDNode::get(Context, argBaseTypeNames));
716   Fn->setMetadata("kernel_arg_type_qual",
717                   llvm::MDNode::get(Context, argTypeQuals));
718   if (CGM.getCodeGenOpts().EmitOpenCLArgMetadata)
719     Fn->setMetadata("kernel_arg_name",
720                     llvm::MDNode::get(Context, argNames));
721 }
722 
723 void CodeGenFunction::EmitOpenCLKernelMetadata(const FunctionDecl *FD,
724                                                llvm::Function *Fn)
725 {
726   if (!FD->hasAttr<OpenCLKernelAttr>())
727     return;
728 
729   llvm::LLVMContext &Context = getLLVMContext();
730 
731   GenOpenCLArgMetadata(FD, Fn, CGM, Context, Builder, getContext());
732 
733   if (const VecTypeHintAttr *A = FD->getAttr<VecTypeHintAttr>()) {
734     QualType HintQTy = A->getTypeHint();
735     const ExtVectorType *HintEltQTy = HintQTy->getAs<ExtVectorType>();
736     bool IsSignedInteger =
737         HintQTy->isSignedIntegerType() ||
738         (HintEltQTy && HintEltQTy->getElementType()->isSignedIntegerType());
739     llvm::Metadata *AttrMDArgs[] = {
740         llvm::ConstantAsMetadata::get(llvm::UndefValue::get(
741             CGM.getTypes().ConvertType(A->getTypeHint()))),
742         llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
743             llvm::IntegerType::get(Context, 32),
744             llvm::APInt(32, (uint64_t)(IsSignedInteger ? 1 : 0))))};
745     Fn->setMetadata("vec_type_hint", llvm::MDNode::get(Context, AttrMDArgs));
746   }
747 
748   if (const WorkGroupSizeHintAttr *A = FD->getAttr<WorkGroupSizeHintAttr>()) {
749     llvm::Metadata *AttrMDArgs[] = {
750         llvm::ConstantAsMetadata::get(Builder.getInt32(A->getXDim())),
751         llvm::ConstantAsMetadata::get(Builder.getInt32(A->getYDim())),
752         llvm::ConstantAsMetadata::get(Builder.getInt32(A->getZDim()))};
753     Fn->setMetadata("work_group_size_hint", llvm::MDNode::get(Context, AttrMDArgs));
754   }
755 
756   if (const ReqdWorkGroupSizeAttr *A = FD->getAttr<ReqdWorkGroupSizeAttr>()) {
757     llvm::Metadata *AttrMDArgs[] = {
758         llvm::ConstantAsMetadata::get(Builder.getInt32(A->getXDim())),
759         llvm::ConstantAsMetadata::get(Builder.getInt32(A->getYDim())),
760         llvm::ConstantAsMetadata::get(Builder.getInt32(A->getZDim()))};
761     Fn->setMetadata("reqd_work_group_size", llvm::MDNode::get(Context, AttrMDArgs));
762   }
763 
764   if (const OpenCLIntelReqdSubGroupSizeAttr *A =
765           FD->getAttr<OpenCLIntelReqdSubGroupSizeAttr>()) {
766     llvm::Metadata *AttrMDArgs[] = {
767         llvm::ConstantAsMetadata::get(Builder.getInt32(A->getSubGroupSize()))};
768     Fn->setMetadata("intel_reqd_sub_group_size",
769                     llvm::MDNode::get(Context, AttrMDArgs));
770   }
771 }
772 
773 /// Determine whether the function F ends with a return stmt.
774 static bool endsWithReturn(const Decl* F) {
775   const Stmt *Body = nullptr;
776   if (auto *FD = dyn_cast_or_null<FunctionDecl>(F))
777     Body = FD->getBody();
778   else if (auto *OMD = dyn_cast_or_null<ObjCMethodDecl>(F))
779     Body = OMD->getBody();
780 
781   if (auto *CS = dyn_cast_or_null<CompoundStmt>(Body)) {
782     auto LastStmt = CS->body_rbegin();
783     if (LastStmt != CS->body_rend())
784       return isa<ReturnStmt>(*LastStmt);
785   }
786   return false;
787 }
788 
789 void CodeGenFunction::markAsIgnoreThreadCheckingAtRuntime(llvm::Function *Fn) {
790   if (SanOpts.has(SanitizerKind::Thread)) {
791     Fn->addFnAttr("sanitize_thread_no_checking_at_run_time");
792     Fn->removeFnAttr(llvm::Attribute::SanitizeThread);
793   }
794 }
795 
796 static bool matchesStlAllocatorFn(const Decl *D, const ASTContext &Ctx) {
797   auto *MD = dyn_cast_or_null<CXXMethodDecl>(D);
798   if (!MD || !MD->getDeclName().getAsIdentifierInfo() ||
799       !MD->getDeclName().getAsIdentifierInfo()->isStr("allocate") ||
800       (MD->getNumParams() != 1 && MD->getNumParams() != 2))
801     return false;
802 
803   if (MD->parameters()[0]->getType().getCanonicalType() != Ctx.getSizeType())
804     return false;
805 
806   if (MD->getNumParams() == 2) {
807     auto *PT = MD->parameters()[1]->getType()->getAs<PointerType>();
808     if (!PT || !PT->isVoidPointerType() ||
809         !PT->getPointeeType().isConstQualified())
810       return false;
811   }
812 
813   return true;
814 }
815 
816 /// Return the UBSan prologue signature for \p FD if one is available.
817 static llvm::Constant *getPrologueSignature(CodeGenModule &CGM,
818                                             const FunctionDecl *FD) {
819   if (const auto *MD = dyn_cast<CXXMethodDecl>(FD))
820     if (!MD->isStatic())
821       return nullptr;
822   return CGM.getTargetCodeGenInfo().getUBSanFunctionSignature(CGM);
823 }
824 
825 void CodeGenFunction::StartFunction(GlobalDecl GD,
826                                     QualType RetTy,
827                                     llvm::Function *Fn,
828                                     const CGFunctionInfo &FnInfo,
829                                     const FunctionArgList &Args,
830                                     SourceLocation Loc,
831                                     SourceLocation StartLoc) {
832   assert(!CurFn &&
833          "Do not use a CodeGenFunction object for more than one function");
834 
835   const Decl *D = GD.getDecl();
836 
837   DidCallStackSave = false;
838   CurCodeDecl = D;
839   if (const auto *FD = dyn_cast_or_null<FunctionDecl>(D))
840     if (FD->usesSEHTry())
841       CurSEHParent = FD;
842   CurFuncDecl = (D ? D->getNonClosureContext() : nullptr);
843   FnRetTy = RetTy;
844   CurFn = Fn;
845   CurFnInfo = &FnInfo;
846   assert(CurFn->isDeclaration() && "Function already has body?");
847 
848   // If this function has been blacklisted for any of the enabled sanitizers,
849   // disable the sanitizer for the function.
850   do {
851 #define SANITIZER(NAME, ID)                                                    \
852   if (SanOpts.empty())                                                         \
853     break;                                                                     \
854   if (SanOpts.has(SanitizerKind::ID))                                          \
855     if (CGM.isInSanitizerBlacklist(SanitizerKind::ID, Fn, Loc))                \
856       SanOpts.set(SanitizerKind::ID, false);
857 
858 #include "clang/Basic/Sanitizers.def"
859 #undef SANITIZER
860   } while (0);
861 
862   if (D) {
863     // Apply the no_sanitize* attributes to SanOpts.
864     for (auto Attr : D->specific_attrs<NoSanitizeAttr>()) {
865       SanitizerMask mask = Attr->getMask();
866       SanOpts.Mask &= ~mask;
867       if (mask & SanitizerKind::Address)
868         SanOpts.set(SanitizerKind::KernelAddress, false);
869       if (mask & SanitizerKind::KernelAddress)
870         SanOpts.set(SanitizerKind::Address, false);
871       if (mask & SanitizerKind::HWAddress)
872         SanOpts.set(SanitizerKind::KernelHWAddress, false);
873       if (mask & SanitizerKind::KernelHWAddress)
874         SanOpts.set(SanitizerKind::HWAddress, false);
875     }
876   }
877 
878   // Apply sanitizer attributes to the function.
879   if (SanOpts.hasOneOf(SanitizerKind::Address | SanitizerKind::KernelAddress))
880     Fn->addFnAttr(llvm::Attribute::SanitizeAddress);
881   if (SanOpts.hasOneOf(SanitizerKind::HWAddress | SanitizerKind::KernelHWAddress))
882     Fn->addFnAttr(llvm::Attribute::SanitizeHWAddress);
883   if (SanOpts.has(SanitizerKind::Thread))
884     Fn->addFnAttr(llvm::Attribute::SanitizeThread);
885   if (SanOpts.hasOneOf(SanitizerKind::Memory | SanitizerKind::KernelMemory))
886     Fn->addFnAttr(llvm::Attribute::SanitizeMemory);
887   if (SanOpts.has(SanitizerKind::SafeStack))
888     Fn->addFnAttr(llvm::Attribute::SafeStack);
889   if (SanOpts.has(SanitizerKind::ShadowCallStack))
890     Fn->addFnAttr(llvm::Attribute::ShadowCallStack);
891 
892   // Apply fuzzing attribute to the function.
893   if (SanOpts.hasOneOf(SanitizerKind::Fuzzer | SanitizerKind::FuzzerNoLink))
894     Fn->addFnAttr(llvm::Attribute::OptForFuzzing);
895 
896   // Ignore TSan memory acesses from within ObjC/ObjC++ dealloc, initialize,
897   // .cxx_destruct, __destroy_helper_block_ and all of their calees at run time.
898   if (SanOpts.has(SanitizerKind::Thread)) {
899     if (const auto *OMD = dyn_cast_or_null<ObjCMethodDecl>(D)) {
900       IdentifierInfo *II = OMD->getSelector().getIdentifierInfoForSlot(0);
901       if (OMD->getMethodFamily() == OMF_dealloc ||
902           OMD->getMethodFamily() == OMF_initialize ||
903           (OMD->getSelector().isUnarySelector() && II->isStr(".cxx_destruct"))) {
904         markAsIgnoreThreadCheckingAtRuntime(Fn);
905       }
906     }
907   }
908 
909   // Ignore unrelated casts in STL allocate() since the allocator must cast
910   // from void* to T* before object initialization completes. Don't match on the
911   // namespace because not all allocators are in std::
912   if (D && SanOpts.has(SanitizerKind::CFIUnrelatedCast)) {
913     if (matchesStlAllocatorFn(D, getContext()))
914       SanOpts.Mask &= ~SanitizerKind::CFIUnrelatedCast;
915   }
916 
917   // Apply xray attributes to the function (as a string, for now)
918   if (D) {
919     if (const auto *XRayAttr = D->getAttr<XRayInstrumentAttr>()) {
920       if (CGM.getCodeGenOpts().XRayInstrumentationBundle.has(
921               XRayInstrKind::Function)) {
922         if (XRayAttr->alwaysXRayInstrument() && ShouldXRayInstrumentFunction())
923           Fn->addFnAttr("function-instrument", "xray-always");
924         if (XRayAttr->neverXRayInstrument())
925           Fn->addFnAttr("function-instrument", "xray-never");
926         if (const auto *LogArgs = D->getAttr<XRayLogArgsAttr>())
927           if (ShouldXRayInstrumentFunction())
928             Fn->addFnAttr("xray-log-args",
929                           llvm::utostr(LogArgs->getArgumentCount()));
930       }
931     } else {
932       if (ShouldXRayInstrumentFunction() && !CGM.imbueXRayAttrs(Fn, Loc))
933         Fn->addFnAttr(
934             "xray-instruction-threshold",
935             llvm::itostr(CGM.getCodeGenOpts().XRayInstructionThreshold));
936     }
937   }
938 
939   // Add no-jump-tables value.
940   Fn->addFnAttr("no-jump-tables",
941                 llvm::toStringRef(CGM.getCodeGenOpts().NoUseJumpTables));
942 
943   // Add profile-sample-accurate value.
944   if (CGM.getCodeGenOpts().ProfileSampleAccurate)
945     Fn->addFnAttr("profile-sample-accurate");
946 
947   if (getLangOpts().OpenCL) {
948     // Add metadata for a kernel function.
949     if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
950       EmitOpenCLKernelMetadata(FD, Fn);
951   }
952 
953   // If we are checking function types, emit a function type signature as
954   // prologue data.
955   if (getLangOpts().CPlusPlus && SanOpts.has(SanitizerKind::Function)) {
956     if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
957       if (llvm::Constant *PrologueSig = getPrologueSignature(CGM, FD)) {
958         // Remove any (C++17) exception specifications, to allow calling e.g. a
959         // noexcept function through a non-noexcept pointer.
960         auto ProtoTy =
961           getContext().getFunctionTypeWithExceptionSpec(FD->getType(),
962                                                         EST_None);
963         llvm::Constant *FTRTTIConst =
964             CGM.GetAddrOfRTTIDescriptor(ProtoTy, /*ForEH=*/true);
965         llvm::Constant *FTRTTIConstEncoded =
966             EncodeAddrForUseInPrologue(Fn, FTRTTIConst);
967         llvm::Constant *PrologueStructElems[] = {PrologueSig,
968                                                  FTRTTIConstEncoded};
969         llvm::Constant *PrologueStructConst =
970             llvm::ConstantStruct::getAnon(PrologueStructElems, /*Packed=*/true);
971         Fn->setPrologueData(PrologueStructConst);
972       }
973     }
974   }
975 
976   // If we're checking nullability, we need to know whether we can check the
977   // return value. Initialize the flag to 'true' and refine it in EmitParmDecl.
978   if (SanOpts.has(SanitizerKind::NullabilityReturn)) {
979     auto Nullability = FnRetTy->getNullability(getContext());
980     if (Nullability && *Nullability == NullabilityKind::NonNull) {
981       if (!(SanOpts.has(SanitizerKind::ReturnsNonnullAttribute) &&
982             CurCodeDecl && CurCodeDecl->getAttr<ReturnsNonNullAttr>()))
983         RetValNullabilityPrecondition =
984             llvm::ConstantInt::getTrue(getLLVMContext());
985     }
986   }
987 
988   // If we're in C++ mode and the function name is "main", it is guaranteed
989   // to be norecurse by the standard (3.6.1.3 "The function main shall not be
990   // used within a program").
991   if (getLangOpts().CPlusPlus)
992     if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
993       if (FD->isMain())
994         Fn->addFnAttr(llvm::Attribute::NoRecurse);
995 
996   // If a custom alignment is used, force realigning to this alignment on
997   // any main function which certainly will need it.
998   if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
999     if ((FD->isMain() || FD->isMSVCRTEntryPoint()) &&
1000         CGM.getCodeGenOpts().StackAlignment)
1001       Fn->addFnAttr("stackrealign");
1002 
1003   llvm::BasicBlock *EntryBB = createBasicBlock("entry", CurFn);
1004 
1005   // Create a marker to make it easy to insert allocas into the entryblock
1006   // later.  Don't create this with the builder, because we don't want it
1007   // folded.
1008   llvm::Value *Undef = llvm::UndefValue::get(Int32Ty);
1009   AllocaInsertPt = new llvm::BitCastInst(Undef, Int32Ty, "allocapt", EntryBB);
1010 
1011   ReturnBlock = getJumpDestInCurrentScope("return");
1012 
1013   Builder.SetInsertPoint(EntryBB);
1014 
1015   // If we're checking the return value, allocate space for a pointer to a
1016   // precise source location of the checked return statement.
1017   if (requiresReturnValueCheck()) {
1018     ReturnLocation = CreateDefaultAlignTempAlloca(Int8PtrTy, "return.sloc.ptr");
1019     InitTempAlloca(ReturnLocation, llvm::ConstantPointerNull::get(Int8PtrTy));
1020   }
1021 
1022   // Emit subprogram debug descriptor.
1023   if (CGDebugInfo *DI = getDebugInfo()) {
1024     // Reconstruct the type from the argument list so that implicit parameters,
1025     // such as 'this' and 'vtt', show up in the debug info. Preserve the calling
1026     // convention.
1027     CallingConv CC = CallingConv::CC_C;
1028     if (auto *FD = dyn_cast_or_null<FunctionDecl>(D))
1029       if (const auto *SrcFnTy = FD->getType()->getAs<FunctionType>())
1030         CC = SrcFnTy->getCallConv();
1031     SmallVector<QualType, 16> ArgTypes;
1032     for (const VarDecl *VD : Args)
1033       ArgTypes.push_back(VD->getType());
1034     QualType FnType = getContext().getFunctionType(
1035         RetTy, ArgTypes, FunctionProtoType::ExtProtoInfo(CC));
1036     DI->EmitFunctionStart(GD, Loc, StartLoc, FnType, CurFn, CurFuncIsThunk,
1037                           Builder);
1038   }
1039 
1040   if (ShouldInstrumentFunction()) {
1041     if (CGM.getCodeGenOpts().InstrumentFunctions)
1042       CurFn->addFnAttr("instrument-function-entry", "__cyg_profile_func_enter");
1043     if (CGM.getCodeGenOpts().InstrumentFunctionsAfterInlining)
1044       CurFn->addFnAttr("instrument-function-entry-inlined",
1045                        "__cyg_profile_func_enter");
1046     if (CGM.getCodeGenOpts().InstrumentFunctionEntryBare)
1047       CurFn->addFnAttr("instrument-function-entry-inlined",
1048                        "__cyg_profile_func_enter_bare");
1049   }
1050 
1051   // Since emitting the mcount call here impacts optimizations such as function
1052   // inlining, we just add an attribute to insert a mcount call in backend.
1053   // The attribute "counting-function" is set to mcount function name which is
1054   // architecture dependent.
1055   if (CGM.getCodeGenOpts().InstrumentForProfiling) {
1056     // Calls to fentry/mcount should not be generated if function has
1057     // the no_instrument_function attribute.
1058     if (!CurFuncDecl || !CurFuncDecl->hasAttr<NoInstrumentFunctionAttr>()) {
1059       if (CGM.getCodeGenOpts().CallFEntry)
1060         Fn->addFnAttr("fentry-call", "true");
1061       else {
1062         Fn->addFnAttr("instrument-function-entry-inlined",
1063                       getTarget().getMCountName());
1064       }
1065     }
1066   }
1067 
1068   if (RetTy->isVoidType()) {
1069     // Void type; nothing to return.
1070     ReturnValue = Address::invalid();
1071 
1072     // Count the implicit return.
1073     if (!endsWithReturn(D))
1074       ++NumReturnExprs;
1075   } else if (CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect) {
1076     // Indirect return; emit returned value directly into sret slot.
1077     // This reduces code size, and affects correctness in C++.
1078     auto AI = CurFn->arg_begin();
1079     if (CurFnInfo->getReturnInfo().isSRetAfterThis())
1080       ++AI;
1081     ReturnValue = Address(&*AI, CurFnInfo->getReturnInfo().getIndirectAlign());
1082   } else if (CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::InAlloca &&
1083              !hasScalarEvaluationKind(CurFnInfo->getReturnType())) {
1084     // Load the sret pointer from the argument struct and return into that.
1085     unsigned Idx = CurFnInfo->getReturnInfo().getInAllocaFieldIndex();
1086     llvm::Function::arg_iterator EI = CurFn->arg_end();
1087     --EI;
1088     llvm::Value *Addr = Builder.CreateStructGEP(nullptr, &*EI, Idx);
1089     Addr = Builder.CreateAlignedLoad(Addr, getPointerAlign(), "agg.result");
1090     ReturnValue = Address(Addr, getNaturalTypeAlignment(RetTy));
1091   } else {
1092     ReturnValue = CreateIRTemp(RetTy, "retval");
1093 
1094     // Tell the epilog emitter to autorelease the result.  We do this
1095     // now so that various specialized functions can suppress it
1096     // during their IR-generation.
1097     if (getLangOpts().ObjCAutoRefCount &&
1098         !CurFnInfo->isReturnsRetained() &&
1099         RetTy->isObjCRetainableType())
1100       AutoreleaseResult = true;
1101   }
1102 
1103   EmitStartEHSpec(CurCodeDecl);
1104 
1105   PrologueCleanupDepth = EHStack.stable_begin();
1106 
1107   // Emit OpenMP specific initialization of the device functions.
1108   if (getLangOpts().OpenMP && CurCodeDecl)
1109     CGM.getOpenMPRuntime().emitFunctionProlog(*this, CurCodeDecl);
1110 
1111   EmitFunctionProlog(*CurFnInfo, CurFn, Args);
1112 
1113   if (D && isa<CXXMethodDecl>(D) && cast<CXXMethodDecl>(D)->isInstance()) {
1114     CGM.getCXXABI().EmitInstanceFunctionProlog(*this);
1115     const CXXMethodDecl *MD = cast<CXXMethodDecl>(D);
1116     if (MD->getParent()->isLambda() &&
1117         MD->getOverloadedOperator() == OO_Call) {
1118       // We're in a lambda; figure out the captures.
1119       MD->getParent()->getCaptureFields(LambdaCaptureFields,
1120                                         LambdaThisCaptureField);
1121       if (LambdaThisCaptureField) {
1122         // If the lambda captures the object referred to by '*this' - either by
1123         // value or by reference, make sure CXXThisValue points to the correct
1124         // object.
1125 
1126         // Get the lvalue for the field (which is a copy of the enclosing object
1127         // or contains the address of the enclosing object).
1128         LValue ThisFieldLValue = EmitLValueForLambdaField(LambdaThisCaptureField);
1129         if (!LambdaThisCaptureField->getType()->isPointerType()) {
1130           // If the enclosing object was captured by value, just use its address.
1131           CXXThisValue = ThisFieldLValue.getAddress().getPointer();
1132         } else {
1133           // Load the lvalue pointed to by the field, since '*this' was captured
1134           // by reference.
1135           CXXThisValue =
1136               EmitLoadOfLValue(ThisFieldLValue, SourceLocation()).getScalarVal();
1137         }
1138       }
1139       for (auto *FD : MD->getParent()->fields()) {
1140         if (FD->hasCapturedVLAType()) {
1141           auto *ExprArg = EmitLoadOfLValue(EmitLValueForLambdaField(FD),
1142                                            SourceLocation()).getScalarVal();
1143           auto VAT = FD->getCapturedVLAType();
1144           VLASizeMap[VAT->getSizeExpr()] = ExprArg;
1145         }
1146       }
1147     } else {
1148       // Not in a lambda; just use 'this' from the method.
1149       // FIXME: Should we generate a new load for each use of 'this'?  The
1150       // fast register allocator would be happier...
1151       CXXThisValue = CXXABIThisValue;
1152     }
1153 
1154     // Check the 'this' pointer once per function, if it's available.
1155     if (CXXABIThisValue) {
1156       SanitizerSet SkippedChecks;
1157       SkippedChecks.set(SanitizerKind::ObjectSize, true);
1158       QualType ThisTy = MD->getThisType();
1159 
1160       // If this is the call operator of a lambda with no capture-default, it
1161       // may have a static invoker function, which may call this operator with
1162       // a null 'this' pointer.
1163       if (isLambdaCallOperator(MD) &&
1164           MD->getParent()->getLambdaCaptureDefault() == LCD_None)
1165         SkippedChecks.set(SanitizerKind::Null, true);
1166 
1167       EmitTypeCheck(isa<CXXConstructorDecl>(MD) ? TCK_ConstructorCall
1168                                                 : TCK_MemberCall,
1169                     Loc, CXXABIThisValue, ThisTy,
1170                     getContext().getTypeAlignInChars(ThisTy->getPointeeType()),
1171                     SkippedChecks);
1172     }
1173   }
1174 
1175   // If any of the arguments have a variably modified type, make sure to
1176   // emit the type size.
1177   for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
1178        i != e; ++i) {
1179     const VarDecl *VD = *i;
1180 
1181     // Dig out the type as written from ParmVarDecls; it's unclear whether
1182     // the standard (C99 6.9.1p10) requires this, but we're following the
1183     // precedent set by gcc.
1184     QualType Ty;
1185     if (const ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(VD))
1186       Ty = PVD->getOriginalType();
1187     else
1188       Ty = VD->getType();
1189 
1190     if (Ty->isVariablyModifiedType())
1191       EmitVariablyModifiedType(Ty);
1192   }
1193   // Emit a location at the end of the prologue.
1194   if (CGDebugInfo *DI = getDebugInfo())
1195     DI->EmitLocation(Builder, StartLoc);
1196 
1197   // TODO: Do we need to handle this in two places like we do with
1198   // target-features/target-cpu?
1199   if (CurFuncDecl)
1200     if (const auto *VecWidth = CurFuncDecl->getAttr<MinVectorWidthAttr>())
1201       LargestVectorWidth = VecWidth->getVectorWidth();
1202 }
1203 
1204 void CodeGenFunction::EmitFunctionBody(const Stmt *Body) {
1205   incrementProfileCounter(Body);
1206   if (const CompoundStmt *S = dyn_cast<CompoundStmt>(Body))
1207     EmitCompoundStmtWithoutScope(*S);
1208   else
1209     EmitStmt(Body);
1210 }
1211 
1212 /// When instrumenting to collect profile data, the counts for some blocks
1213 /// such as switch cases need to not include the fall-through counts, so
1214 /// emit a branch around the instrumentation code. When not instrumenting,
1215 /// this just calls EmitBlock().
1216 void CodeGenFunction::EmitBlockWithFallThrough(llvm::BasicBlock *BB,
1217                                                const Stmt *S) {
1218   llvm::BasicBlock *SkipCountBB = nullptr;
1219   if (HaveInsertPoint() && CGM.getCodeGenOpts().hasProfileClangInstr()) {
1220     // When instrumenting for profiling, the fallthrough to certain
1221     // statements needs to skip over the instrumentation code so that we
1222     // get an accurate count.
1223     SkipCountBB = createBasicBlock("skipcount");
1224     EmitBranch(SkipCountBB);
1225   }
1226   EmitBlock(BB);
1227   uint64_t CurrentCount = getCurrentProfileCount();
1228   incrementProfileCounter(S);
1229   setCurrentProfileCount(getCurrentProfileCount() + CurrentCount);
1230   if (SkipCountBB)
1231     EmitBlock(SkipCountBB);
1232 }
1233 
1234 /// Tries to mark the given function nounwind based on the
1235 /// non-existence of any throwing calls within it.  We believe this is
1236 /// lightweight enough to do at -O0.
1237 static void TryMarkNoThrow(llvm::Function *F) {
1238   // LLVM treats 'nounwind' on a function as part of the type, so we
1239   // can't do this on functions that can be overwritten.
1240   if (F->isInterposable()) return;
1241 
1242   for (llvm::BasicBlock &BB : *F)
1243     for (llvm::Instruction &I : BB)
1244       if (I.mayThrow())
1245         return;
1246 
1247   F->setDoesNotThrow();
1248 }
1249 
1250 QualType CodeGenFunction::BuildFunctionArgList(GlobalDecl GD,
1251                                                FunctionArgList &Args) {
1252   const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
1253   QualType ResTy = FD->getReturnType();
1254 
1255   const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD);
1256   if (MD && MD->isInstance()) {
1257     if (CGM.getCXXABI().HasThisReturn(GD))
1258       ResTy = MD->getThisType();
1259     else if (CGM.getCXXABI().hasMostDerivedReturn(GD))
1260       ResTy = CGM.getContext().VoidPtrTy;
1261     CGM.getCXXABI().buildThisParam(*this, Args);
1262   }
1263 
1264   // The base version of an inheriting constructor whose constructed base is a
1265   // virtual base is not passed any arguments (because it doesn't actually call
1266   // the inherited constructor).
1267   bool PassedParams = true;
1268   if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD))
1269     if (auto Inherited = CD->getInheritedConstructor())
1270       PassedParams =
1271           getTypes().inheritingCtorHasParams(Inherited, GD.getCtorType());
1272 
1273   if (PassedParams) {
1274     for (auto *Param : FD->parameters()) {
1275       Args.push_back(Param);
1276       if (!Param->hasAttr<PassObjectSizeAttr>())
1277         continue;
1278 
1279       auto *Implicit = ImplicitParamDecl::Create(
1280           getContext(), Param->getDeclContext(), Param->getLocation(),
1281           /*Id=*/nullptr, getContext().getSizeType(), ImplicitParamDecl::Other);
1282       SizeArguments[Param] = Implicit;
1283       Args.push_back(Implicit);
1284     }
1285   }
1286 
1287   if (MD && (isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD)))
1288     CGM.getCXXABI().addImplicitStructorParams(*this, ResTy, Args);
1289 
1290   return ResTy;
1291 }
1292 
1293 static bool
1294 shouldUseUndefinedBehaviorReturnOptimization(const FunctionDecl *FD,
1295                                              const ASTContext &Context) {
1296   QualType T = FD->getReturnType();
1297   // Avoid the optimization for functions that return a record type with a
1298   // trivial destructor or another trivially copyable type.
1299   if (const RecordType *RT = T.getCanonicalType()->getAs<RecordType>()) {
1300     if (const auto *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl()))
1301       return !ClassDecl->hasTrivialDestructor();
1302   }
1303   return !T.isTriviallyCopyableType(Context);
1304 }
1305 
1306 void CodeGenFunction::GenerateCode(GlobalDecl GD, llvm::Function *Fn,
1307                                    const CGFunctionInfo &FnInfo) {
1308   const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
1309   CurGD = GD;
1310 
1311   FunctionArgList Args;
1312   QualType ResTy = BuildFunctionArgList(GD, Args);
1313 
1314   // Check if we should generate debug info for this function.
1315   if (FD->hasAttr<NoDebugAttr>())
1316     DebugInfo = nullptr; // disable debug info indefinitely for this function
1317 
1318   // The function might not have a body if we're generating thunks for a
1319   // function declaration.
1320   SourceRange BodyRange;
1321   if (Stmt *Body = FD->getBody())
1322     BodyRange = Body->getSourceRange();
1323   else
1324     BodyRange = FD->getLocation();
1325   CurEHLocation = BodyRange.getEnd();
1326 
1327   // Use the location of the start of the function to determine where
1328   // the function definition is located. By default use the location
1329   // of the declaration as the location for the subprogram. A function
1330   // may lack a declaration in the source code if it is created by code
1331   // gen. (examples: _GLOBAL__I_a, __cxx_global_array_dtor, thunk).
1332   SourceLocation Loc = FD->getLocation();
1333 
1334   // If this is a function specialization then use the pattern body
1335   // as the location for the function.
1336   if (const FunctionDecl *SpecDecl = FD->getTemplateInstantiationPattern())
1337     if (SpecDecl->hasBody(SpecDecl))
1338       Loc = SpecDecl->getLocation();
1339 
1340   Stmt *Body = FD->getBody();
1341 
1342   // Initialize helper which will detect jumps which can cause invalid lifetime
1343   // markers.
1344   if (Body && ShouldEmitLifetimeMarkers)
1345     Bypasses.Init(Body);
1346 
1347   // Emit the standard function prologue.
1348   StartFunction(GD, ResTy, Fn, FnInfo, Args, Loc, BodyRange.getBegin());
1349 
1350   // Generate the body of the function.
1351   PGO.assignRegionCounters(GD, CurFn);
1352   if (isa<CXXDestructorDecl>(FD))
1353     EmitDestructorBody(Args);
1354   else if (isa<CXXConstructorDecl>(FD))
1355     EmitConstructorBody(Args);
1356   else if (getLangOpts().CUDA &&
1357            !getLangOpts().CUDAIsDevice &&
1358            FD->hasAttr<CUDAGlobalAttr>())
1359     CGM.getCUDARuntime().emitDeviceStub(*this, Args);
1360   else if (isa<CXXMethodDecl>(FD) &&
1361            cast<CXXMethodDecl>(FD)->isLambdaStaticInvoker()) {
1362     // The lambda static invoker function is special, because it forwards or
1363     // clones the body of the function call operator (but is actually static).
1364     EmitLambdaStaticInvokeBody(cast<CXXMethodDecl>(FD));
1365   } else if (FD->isDefaulted() && isa<CXXMethodDecl>(FD) &&
1366              (cast<CXXMethodDecl>(FD)->isCopyAssignmentOperator() ||
1367               cast<CXXMethodDecl>(FD)->isMoveAssignmentOperator())) {
1368     // Implicit copy-assignment gets the same special treatment as implicit
1369     // copy-constructors.
1370     emitImplicitAssignmentOperatorBody(Args);
1371   } else if (Body) {
1372     EmitFunctionBody(Body);
1373   } else
1374     llvm_unreachable("no definition for emitted function");
1375 
1376   // C++11 [stmt.return]p2:
1377   //   Flowing off the end of a function [...] results in undefined behavior in
1378   //   a value-returning function.
1379   // C11 6.9.1p12:
1380   //   If the '}' that terminates a function is reached, and the value of the
1381   //   function call is used by the caller, the behavior is undefined.
1382   if (getLangOpts().CPlusPlus && !FD->hasImplicitReturnZero() && !SawAsmBlock &&
1383       !FD->getReturnType()->isVoidType() && Builder.GetInsertBlock()) {
1384     bool ShouldEmitUnreachable =
1385         CGM.getCodeGenOpts().StrictReturn ||
1386         shouldUseUndefinedBehaviorReturnOptimization(FD, getContext());
1387     if (SanOpts.has(SanitizerKind::Return)) {
1388       SanitizerScope SanScope(this);
1389       llvm::Value *IsFalse = Builder.getFalse();
1390       EmitCheck(std::make_pair(IsFalse, SanitizerKind::Return),
1391                 SanitizerHandler::MissingReturn,
1392                 EmitCheckSourceLocation(FD->getLocation()), None);
1393     } else if (ShouldEmitUnreachable) {
1394       if (CGM.getCodeGenOpts().OptimizationLevel == 0)
1395         EmitTrapCall(llvm::Intrinsic::trap);
1396     }
1397     if (SanOpts.has(SanitizerKind::Return) || ShouldEmitUnreachable) {
1398       Builder.CreateUnreachable();
1399       Builder.ClearInsertionPoint();
1400     }
1401   }
1402 
1403   // Emit the standard function epilogue.
1404   FinishFunction(BodyRange.getEnd());
1405 
1406   // If we haven't marked the function nothrow through other means, do
1407   // a quick pass now to see if we can.
1408   if (!CurFn->doesNotThrow())
1409     TryMarkNoThrow(CurFn);
1410 }
1411 
1412 /// ContainsLabel - Return true if the statement contains a label in it.  If
1413 /// this statement is not executed normally, it not containing a label means
1414 /// that we can just remove the code.
1415 bool CodeGenFunction::ContainsLabel(const Stmt *S, bool IgnoreCaseStmts) {
1416   // Null statement, not a label!
1417   if (!S) return false;
1418 
1419   // If this is a label, we have to emit the code, consider something like:
1420   // if (0) {  ...  foo:  bar(); }  goto foo;
1421   //
1422   // TODO: If anyone cared, we could track __label__'s, since we know that you
1423   // can't jump to one from outside their declared region.
1424   if (isa<LabelStmt>(S))
1425     return true;
1426 
1427   // If this is a case/default statement, and we haven't seen a switch, we have
1428   // to emit the code.
1429   if (isa<SwitchCase>(S) && !IgnoreCaseStmts)
1430     return true;
1431 
1432   // If this is a switch statement, we want to ignore cases below it.
1433   if (isa<SwitchStmt>(S))
1434     IgnoreCaseStmts = true;
1435 
1436   // Scan subexpressions for verboten labels.
1437   for (const Stmt *SubStmt : S->children())
1438     if (ContainsLabel(SubStmt, IgnoreCaseStmts))
1439       return true;
1440 
1441   return false;
1442 }
1443 
1444 /// containsBreak - Return true if the statement contains a break out of it.
1445 /// If the statement (recursively) contains a switch or loop with a break
1446 /// inside of it, this is fine.
1447 bool CodeGenFunction::containsBreak(const Stmt *S) {
1448   // Null statement, not a label!
1449   if (!S) return false;
1450 
1451   // If this is a switch or loop that defines its own break scope, then we can
1452   // include it and anything inside of it.
1453   if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || isa<DoStmt>(S) ||
1454       isa<ForStmt>(S))
1455     return false;
1456 
1457   if (isa<BreakStmt>(S))
1458     return true;
1459 
1460   // Scan subexpressions for verboten breaks.
1461   for (const Stmt *SubStmt : S->children())
1462     if (containsBreak(SubStmt))
1463       return true;
1464 
1465   return false;
1466 }
1467 
1468 bool CodeGenFunction::mightAddDeclToScope(const Stmt *S) {
1469   if (!S) return false;
1470 
1471   // Some statement kinds add a scope and thus never add a decl to the current
1472   // scope. Note, this list is longer than the list of statements that might
1473   // have an unscoped decl nested within them, but this way is conservatively
1474   // correct even if more statement kinds are added.
1475   if (isa<IfStmt>(S) || isa<SwitchStmt>(S) || isa<WhileStmt>(S) ||
1476       isa<DoStmt>(S) || isa<ForStmt>(S) || isa<CompoundStmt>(S) ||
1477       isa<CXXForRangeStmt>(S) || isa<CXXTryStmt>(S) ||
1478       isa<ObjCForCollectionStmt>(S) || isa<ObjCAtTryStmt>(S))
1479     return false;
1480 
1481   if (isa<DeclStmt>(S))
1482     return true;
1483 
1484   for (const Stmt *SubStmt : S->children())
1485     if (mightAddDeclToScope(SubStmt))
1486       return true;
1487 
1488   return false;
1489 }
1490 
1491 /// ConstantFoldsToSimpleInteger - If the specified expression does not fold
1492 /// to a constant, or if it does but contains a label, return false.  If it
1493 /// constant folds return true and set the boolean result in Result.
1494 bool CodeGenFunction::ConstantFoldsToSimpleInteger(const Expr *Cond,
1495                                                    bool &ResultBool,
1496                                                    bool AllowLabels) {
1497   llvm::APSInt ResultInt;
1498   if (!ConstantFoldsToSimpleInteger(Cond, ResultInt, AllowLabels))
1499     return false;
1500 
1501   ResultBool = ResultInt.getBoolValue();
1502   return true;
1503 }
1504 
1505 /// ConstantFoldsToSimpleInteger - If the specified expression does not fold
1506 /// to a constant, or if it does but contains a label, return false.  If it
1507 /// constant folds return true and set the folded value.
1508 bool CodeGenFunction::ConstantFoldsToSimpleInteger(const Expr *Cond,
1509                                                    llvm::APSInt &ResultInt,
1510                                                    bool AllowLabels) {
1511   // FIXME: Rename and handle conversion of other evaluatable things
1512   // to bool.
1513   Expr::EvalResult Result;
1514   if (!Cond->EvaluateAsInt(Result, getContext()))
1515     return false;  // Not foldable, not integer or not fully evaluatable.
1516 
1517   llvm::APSInt Int = Result.Val.getInt();
1518   if (!AllowLabels && CodeGenFunction::ContainsLabel(Cond))
1519     return false;  // Contains a label.
1520 
1521   ResultInt = Int;
1522   return true;
1523 }
1524 
1525 
1526 
1527 /// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an if
1528 /// statement) to the specified blocks.  Based on the condition, this might try
1529 /// to simplify the codegen of the conditional based on the branch.
1530 ///
1531 void CodeGenFunction::EmitBranchOnBoolExpr(const Expr *Cond,
1532                                            llvm::BasicBlock *TrueBlock,
1533                                            llvm::BasicBlock *FalseBlock,
1534                                            uint64_t TrueCount) {
1535   Cond = Cond->IgnoreParens();
1536 
1537   if (const BinaryOperator *CondBOp = dyn_cast<BinaryOperator>(Cond)) {
1538 
1539     // Handle X && Y in a condition.
1540     if (CondBOp->getOpcode() == BO_LAnd) {
1541       // If we have "1 && X", simplify the code.  "0 && X" would have constant
1542       // folded if the case was simple enough.
1543       bool ConstantBool = false;
1544       if (ConstantFoldsToSimpleInteger(CondBOp->getLHS(), ConstantBool) &&
1545           ConstantBool) {
1546         // br(1 && X) -> br(X).
1547         incrementProfileCounter(CondBOp);
1548         return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock,
1549                                     TrueCount);
1550       }
1551 
1552       // If we have "X && 1", simplify the code to use an uncond branch.
1553       // "X && 0" would have been constant folded to 0.
1554       if (ConstantFoldsToSimpleInteger(CondBOp->getRHS(), ConstantBool) &&
1555           ConstantBool) {
1556         // br(X && 1) -> br(X).
1557         return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock,
1558                                     TrueCount);
1559       }
1560 
1561       // Emit the LHS as a conditional.  If the LHS conditional is false, we
1562       // want to jump to the FalseBlock.
1563       llvm::BasicBlock *LHSTrue = createBasicBlock("land.lhs.true");
1564       // The counter tells us how often we evaluate RHS, and all of TrueCount
1565       // can be propagated to that branch.
1566       uint64_t RHSCount = getProfileCount(CondBOp->getRHS());
1567 
1568       ConditionalEvaluation eval(*this);
1569       {
1570         ApplyDebugLocation DL(*this, Cond);
1571         EmitBranchOnBoolExpr(CondBOp->getLHS(), LHSTrue, FalseBlock, RHSCount);
1572         EmitBlock(LHSTrue);
1573       }
1574 
1575       incrementProfileCounter(CondBOp);
1576       setCurrentProfileCount(getProfileCount(CondBOp->getRHS()));
1577 
1578       // Any temporaries created here are conditional.
1579       eval.begin(*this);
1580       EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock, TrueCount);
1581       eval.end(*this);
1582 
1583       return;
1584     }
1585 
1586     if (CondBOp->getOpcode() == BO_LOr) {
1587       // If we have "0 || X", simplify the code.  "1 || X" would have constant
1588       // folded if the case was simple enough.
1589       bool ConstantBool = false;
1590       if (ConstantFoldsToSimpleInteger(CondBOp->getLHS(), ConstantBool) &&
1591           !ConstantBool) {
1592         // br(0 || X) -> br(X).
1593         incrementProfileCounter(CondBOp);
1594         return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock,
1595                                     TrueCount);
1596       }
1597 
1598       // If we have "X || 0", simplify the code to use an uncond branch.
1599       // "X || 1" would have been constant folded to 1.
1600       if (ConstantFoldsToSimpleInteger(CondBOp->getRHS(), ConstantBool) &&
1601           !ConstantBool) {
1602         // br(X || 0) -> br(X).
1603         return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock,
1604                                     TrueCount);
1605       }
1606 
1607       // Emit the LHS as a conditional.  If the LHS conditional is true, we
1608       // want to jump to the TrueBlock.
1609       llvm::BasicBlock *LHSFalse = createBasicBlock("lor.lhs.false");
1610       // We have the count for entry to the RHS and for the whole expression
1611       // being true, so we can divy up True count between the short circuit and
1612       // the RHS.
1613       uint64_t LHSCount =
1614           getCurrentProfileCount() - getProfileCount(CondBOp->getRHS());
1615       uint64_t RHSCount = TrueCount - LHSCount;
1616 
1617       ConditionalEvaluation eval(*this);
1618       {
1619         ApplyDebugLocation DL(*this, Cond);
1620         EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, LHSFalse, LHSCount);
1621         EmitBlock(LHSFalse);
1622       }
1623 
1624       incrementProfileCounter(CondBOp);
1625       setCurrentProfileCount(getProfileCount(CondBOp->getRHS()));
1626 
1627       // Any temporaries created here are conditional.
1628       eval.begin(*this);
1629       EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock, RHSCount);
1630 
1631       eval.end(*this);
1632 
1633       return;
1634     }
1635   }
1636 
1637   if (const UnaryOperator *CondUOp = dyn_cast<UnaryOperator>(Cond)) {
1638     // br(!x, t, f) -> br(x, f, t)
1639     if (CondUOp->getOpcode() == UO_LNot) {
1640       // Negate the count.
1641       uint64_t FalseCount = getCurrentProfileCount() - TrueCount;
1642       // Negate the condition and swap the destination blocks.
1643       return EmitBranchOnBoolExpr(CondUOp->getSubExpr(), FalseBlock, TrueBlock,
1644                                   FalseCount);
1645     }
1646   }
1647 
1648   if (const ConditionalOperator *CondOp = dyn_cast<ConditionalOperator>(Cond)) {
1649     // br(c ? x : y, t, f) -> br(c, br(x, t, f), br(y, t, f))
1650     llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true");
1651     llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false");
1652 
1653     ConditionalEvaluation cond(*this);
1654     EmitBranchOnBoolExpr(CondOp->getCond(), LHSBlock, RHSBlock,
1655                          getProfileCount(CondOp));
1656 
1657     // When computing PGO branch weights, we only know the overall count for
1658     // the true block. This code is essentially doing tail duplication of the
1659     // naive code-gen, introducing new edges for which counts are not
1660     // available. Divide the counts proportionally between the LHS and RHS of
1661     // the conditional operator.
1662     uint64_t LHSScaledTrueCount = 0;
1663     if (TrueCount) {
1664       double LHSRatio =
1665           getProfileCount(CondOp) / (double)getCurrentProfileCount();
1666       LHSScaledTrueCount = TrueCount * LHSRatio;
1667     }
1668 
1669     cond.begin(*this);
1670     EmitBlock(LHSBlock);
1671     incrementProfileCounter(CondOp);
1672     {
1673       ApplyDebugLocation DL(*this, Cond);
1674       EmitBranchOnBoolExpr(CondOp->getLHS(), TrueBlock, FalseBlock,
1675                            LHSScaledTrueCount);
1676     }
1677     cond.end(*this);
1678 
1679     cond.begin(*this);
1680     EmitBlock(RHSBlock);
1681     EmitBranchOnBoolExpr(CondOp->getRHS(), TrueBlock, FalseBlock,
1682                          TrueCount - LHSScaledTrueCount);
1683     cond.end(*this);
1684 
1685     return;
1686   }
1687 
1688   if (const CXXThrowExpr *Throw = dyn_cast<CXXThrowExpr>(Cond)) {
1689     // Conditional operator handling can give us a throw expression as a
1690     // condition for a case like:
1691     //   br(c ? throw x : y, t, f) -> br(c, br(throw x, t, f), br(y, t, f)
1692     // Fold this to:
1693     //   br(c, throw x, br(y, t, f))
1694     EmitCXXThrowExpr(Throw, /*KeepInsertionPoint*/false);
1695     return;
1696   }
1697 
1698   // If the branch has a condition wrapped by __builtin_unpredictable,
1699   // create metadata that specifies that the branch is unpredictable.
1700   // Don't bother if not optimizing because that metadata would not be used.
1701   llvm::MDNode *Unpredictable = nullptr;
1702   auto *Call = dyn_cast<CallExpr>(Cond->IgnoreImpCasts());
1703   if (Call && CGM.getCodeGenOpts().OptimizationLevel != 0) {
1704     auto *FD = dyn_cast_or_null<FunctionDecl>(Call->getCalleeDecl());
1705     if (FD && FD->getBuiltinID() == Builtin::BI__builtin_unpredictable) {
1706       llvm::MDBuilder MDHelper(getLLVMContext());
1707       Unpredictable = MDHelper.createUnpredictable();
1708     }
1709   }
1710 
1711   // Create branch weights based on the number of times we get here and the
1712   // number of times the condition should be true.
1713   uint64_t CurrentCount = std::max(getCurrentProfileCount(), TrueCount);
1714   llvm::MDNode *Weights =
1715       createProfileWeights(TrueCount, CurrentCount - TrueCount);
1716 
1717   // Emit the code with the fully general case.
1718   llvm::Value *CondV;
1719   {
1720     ApplyDebugLocation DL(*this, Cond);
1721     CondV = EvaluateExprAsBool(Cond);
1722   }
1723   Builder.CreateCondBr(CondV, TrueBlock, FalseBlock, Weights, Unpredictable);
1724 }
1725 
1726 /// ErrorUnsupported - Print out an error that codegen doesn't support the
1727 /// specified stmt yet.
1728 void CodeGenFunction::ErrorUnsupported(const Stmt *S, const char *Type) {
1729   CGM.ErrorUnsupported(S, Type);
1730 }
1731 
1732 /// emitNonZeroVLAInit - Emit the "zero" initialization of a
1733 /// variable-length array whose elements have a non-zero bit-pattern.
1734 ///
1735 /// \param baseType the inner-most element type of the array
1736 /// \param src - a char* pointing to the bit-pattern for a single
1737 /// base element of the array
1738 /// \param sizeInChars - the total size of the VLA, in chars
1739 static void emitNonZeroVLAInit(CodeGenFunction &CGF, QualType baseType,
1740                                Address dest, Address src,
1741                                llvm::Value *sizeInChars) {
1742   CGBuilderTy &Builder = CGF.Builder;
1743 
1744   CharUnits baseSize = CGF.getContext().getTypeSizeInChars(baseType);
1745   llvm::Value *baseSizeInChars
1746     = llvm::ConstantInt::get(CGF.IntPtrTy, baseSize.getQuantity());
1747 
1748   Address begin =
1749     Builder.CreateElementBitCast(dest, CGF.Int8Ty, "vla.begin");
1750   llvm::Value *end =
1751     Builder.CreateInBoundsGEP(begin.getPointer(), sizeInChars, "vla.end");
1752 
1753   llvm::BasicBlock *originBB = CGF.Builder.GetInsertBlock();
1754   llvm::BasicBlock *loopBB = CGF.createBasicBlock("vla-init.loop");
1755   llvm::BasicBlock *contBB = CGF.createBasicBlock("vla-init.cont");
1756 
1757   // Make a loop over the VLA.  C99 guarantees that the VLA element
1758   // count must be nonzero.
1759   CGF.EmitBlock(loopBB);
1760 
1761   llvm::PHINode *cur = Builder.CreatePHI(begin.getType(), 2, "vla.cur");
1762   cur->addIncoming(begin.getPointer(), originBB);
1763 
1764   CharUnits curAlign =
1765     dest.getAlignment().alignmentOfArrayElement(baseSize);
1766 
1767   // memcpy the individual element bit-pattern.
1768   Builder.CreateMemCpy(Address(cur, curAlign), src, baseSizeInChars,
1769                        /*volatile*/ false);
1770 
1771   // Go to the next element.
1772   llvm::Value *next =
1773     Builder.CreateInBoundsGEP(CGF.Int8Ty, cur, baseSizeInChars, "vla.next");
1774 
1775   // Leave if that's the end of the VLA.
1776   llvm::Value *done = Builder.CreateICmpEQ(next, end, "vla-init.isdone");
1777   Builder.CreateCondBr(done, contBB, loopBB);
1778   cur->addIncoming(next, loopBB);
1779 
1780   CGF.EmitBlock(contBB);
1781 }
1782 
1783 void
1784 CodeGenFunction::EmitNullInitialization(Address DestPtr, QualType Ty) {
1785   // Ignore empty classes in C++.
1786   if (getLangOpts().CPlusPlus) {
1787     if (const RecordType *RT = Ty->getAs<RecordType>()) {
1788       if (cast<CXXRecordDecl>(RT->getDecl())->isEmpty())
1789         return;
1790     }
1791   }
1792 
1793   // Cast the dest ptr to the appropriate i8 pointer type.
1794   if (DestPtr.getElementType() != Int8Ty)
1795     DestPtr = Builder.CreateElementBitCast(DestPtr, Int8Ty);
1796 
1797   // Get size and alignment info for this aggregate.
1798   CharUnits size = getContext().getTypeSizeInChars(Ty);
1799 
1800   llvm::Value *SizeVal;
1801   const VariableArrayType *vla;
1802 
1803   // Don't bother emitting a zero-byte memset.
1804   if (size.isZero()) {
1805     // But note that getTypeInfo returns 0 for a VLA.
1806     if (const VariableArrayType *vlaType =
1807           dyn_cast_or_null<VariableArrayType>(
1808                                           getContext().getAsArrayType(Ty))) {
1809       auto VlaSize = getVLASize(vlaType);
1810       SizeVal = VlaSize.NumElts;
1811       CharUnits eltSize = getContext().getTypeSizeInChars(VlaSize.Type);
1812       if (!eltSize.isOne())
1813         SizeVal = Builder.CreateNUWMul(SizeVal, CGM.getSize(eltSize));
1814       vla = vlaType;
1815     } else {
1816       return;
1817     }
1818   } else {
1819     SizeVal = CGM.getSize(size);
1820     vla = nullptr;
1821   }
1822 
1823   // If the type contains a pointer to data member we can't memset it to zero.
1824   // Instead, create a null constant and copy it to the destination.
1825   // TODO: there are other patterns besides zero that we can usefully memset,
1826   // like -1, which happens to be the pattern used by member-pointers.
1827   if (!CGM.getTypes().isZeroInitializable(Ty)) {
1828     // For a VLA, emit a single element, then splat that over the VLA.
1829     if (vla) Ty = getContext().getBaseElementType(vla);
1830 
1831     llvm::Constant *NullConstant = CGM.EmitNullConstant(Ty);
1832 
1833     llvm::GlobalVariable *NullVariable =
1834       new llvm::GlobalVariable(CGM.getModule(), NullConstant->getType(),
1835                                /*isConstant=*/true,
1836                                llvm::GlobalVariable::PrivateLinkage,
1837                                NullConstant, Twine());
1838     CharUnits NullAlign = DestPtr.getAlignment();
1839     NullVariable->setAlignment(NullAlign.getQuantity());
1840     Address SrcPtr(Builder.CreateBitCast(NullVariable, Builder.getInt8PtrTy()),
1841                    NullAlign);
1842 
1843     if (vla) return emitNonZeroVLAInit(*this, Ty, DestPtr, SrcPtr, SizeVal);
1844 
1845     // Get and call the appropriate llvm.memcpy overload.
1846     Builder.CreateMemCpy(DestPtr, SrcPtr, SizeVal, false);
1847     return;
1848   }
1849 
1850   // Otherwise, just memset the whole thing to zero.  This is legal
1851   // because in LLVM, all default initializers (other than the ones we just
1852   // handled above) are guaranteed to have a bit pattern of all zeros.
1853   Builder.CreateMemSet(DestPtr, Builder.getInt8(0), SizeVal, false);
1854 }
1855 
1856 llvm::BlockAddress *CodeGenFunction::GetAddrOfLabel(const LabelDecl *L) {
1857   // Make sure that there is a block for the indirect goto.
1858   if (!IndirectBranch)
1859     GetIndirectGotoBlock();
1860 
1861   llvm::BasicBlock *BB = getJumpDestForLabel(L).getBlock();
1862 
1863   // Make sure the indirect branch includes all of the address-taken blocks.
1864   IndirectBranch->addDestination(BB);
1865   return llvm::BlockAddress::get(CurFn, BB);
1866 }
1867 
1868 llvm::BasicBlock *CodeGenFunction::GetIndirectGotoBlock() {
1869   // If we already made the indirect branch for indirect goto, return its block.
1870   if (IndirectBranch) return IndirectBranch->getParent();
1871 
1872   CGBuilderTy TmpBuilder(*this, createBasicBlock("indirectgoto"));
1873 
1874   // Create the PHI node that indirect gotos will add entries to.
1875   llvm::Value *DestVal = TmpBuilder.CreatePHI(Int8PtrTy, 0,
1876                                               "indirect.goto.dest");
1877 
1878   // Create the indirect branch instruction.
1879   IndirectBranch = TmpBuilder.CreateIndirectBr(DestVal);
1880   return IndirectBranch->getParent();
1881 }
1882 
1883 /// Computes the length of an array in elements, as well as the base
1884 /// element type and a properly-typed first element pointer.
1885 llvm::Value *CodeGenFunction::emitArrayLength(const ArrayType *origArrayType,
1886                                               QualType &baseType,
1887                                               Address &addr) {
1888   const ArrayType *arrayType = origArrayType;
1889 
1890   // If it's a VLA, we have to load the stored size.  Note that
1891   // this is the size of the VLA in bytes, not its size in elements.
1892   llvm::Value *numVLAElements = nullptr;
1893   if (isa<VariableArrayType>(arrayType)) {
1894     numVLAElements = getVLASize(cast<VariableArrayType>(arrayType)).NumElts;
1895 
1896     // Walk into all VLAs.  This doesn't require changes to addr,
1897     // which has type T* where T is the first non-VLA element type.
1898     do {
1899       QualType elementType = arrayType->getElementType();
1900       arrayType = getContext().getAsArrayType(elementType);
1901 
1902       // If we only have VLA components, 'addr' requires no adjustment.
1903       if (!arrayType) {
1904         baseType = elementType;
1905         return numVLAElements;
1906       }
1907     } while (isa<VariableArrayType>(arrayType));
1908 
1909     // We get out here only if we find a constant array type
1910     // inside the VLA.
1911   }
1912 
1913   // We have some number of constant-length arrays, so addr should
1914   // have LLVM type [M x [N x [...]]]*.  Build a GEP that walks
1915   // down to the first element of addr.
1916   SmallVector<llvm::Value*, 8> gepIndices;
1917 
1918   // GEP down to the array type.
1919   llvm::ConstantInt *zero = Builder.getInt32(0);
1920   gepIndices.push_back(zero);
1921 
1922   uint64_t countFromCLAs = 1;
1923   QualType eltType;
1924 
1925   llvm::ArrayType *llvmArrayType =
1926     dyn_cast<llvm::ArrayType>(addr.getElementType());
1927   while (llvmArrayType) {
1928     assert(isa<ConstantArrayType>(arrayType));
1929     assert(cast<ConstantArrayType>(arrayType)->getSize().getZExtValue()
1930              == llvmArrayType->getNumElements());
1931 
1932     gepIndices.push_back(zero);
1933     countFromCLAs *= llvmArrayType->getNumElements();
1934     eltType = arrayType->getElementType();
1935 
1936     llvmArrayType =
1937       dyn_cast<llvm::ArrayType>(llvmArrayType->getElementType());
1938     arrayType = getContext().getAsArrayType(arrayType->getElementType());
1939     assert((!llvmArrayType || arrayType) &&
1940            "LLVM and Clang types are out-of-synch");
1941   }
1942 
1943   if (arrayType) {
1944     // From this point onwards, the Clang array type has been emitted
1945     // as some other type (probably a packed struct). Compute the array
1946     // size, and just emit the 'begin' expression as a bitcast.
1947     while (arrayType) {
1948       countFromCLAs *=
1949           cast<ConstantArrayType>(arrayType)->getSize().getZExtValue();
1950       eltType = arrayType->getElementType();
1951       arrayType = getContext().getAsArrayType(eltType);
1952     }
1953 
1954     llvm::Type *baseType = ConvertType(eltType);
1955     addr = Builder.CreateElementBitCast(addr, baseType, "array.begin");
1956   } else {
1957     // Create the actual GEP.
1958     addr = Address(Builder.CreateInBoundsGEP(addr.getPointer(),
1959                                              gepIndices, "array.begin"),
1960                    addr.getAlignment());
1961   }
1962 
1963   baseType = eltType;
1964 
1965   llvm::Value *numElements
1966     = llvm::ConstantInt::get(SizeTy, countFromCLAs);
1967 
1968   // If we had any VLA dimensions, factor them in.
1969   if (numVLAElements)
1970     numElements = Builder.CreateNUWMul(numVLAElements, numElements);
1971 
1972   return numElements;
1973 }
1974 
1975 CodeGenFunction::VlaSizePair CodeGenFunction::getVLASize(QualType type) {
1976   const VariableArrayType *vla = getContext().getAsVariableArrayType(type);
1977   assert(vla && "type was not a variable array type!");
1978   return getVLASize(vla);
1979 }
1980 
1981 CodeGenFunction::VlaSizePair
1982 CodeGenFunction::getVLASize(const VariableArrayType *type) {
1983   // The number of elements so far; always size_t.
1984   llvm::Value *numElements = nullptr;
1985 
1986   QualType elementType;
1987   do {
1988     elementType = type->getElementType();
1989     llvm::Value *vlaSize = VLASizeMap[type->getSizeExpr()];
1990     assert(vlaSize && "no size for VLA!");
1991     assert(vlaSize->getType() == SizeTy);
1992 
1993     if (!numElements) {
1994       numElements = vlaSize;
1995     } else {
1996       // It's undefined behavior if this wraps around, so mark it that way.
1997       // FIXME: Teach -fsanitize=undefined to trap this.
1998       numElements = Builder.CreateNUWMul(numElements, vlaSize);
1999     }
2000   } while ((type = getContext().getAsVariableArrayType(elementType)));
2001 
2002   return { numElements, elementType };
2003 }
2004 
2005 CodeGenFunction::VlaSizePair
2006 CodeGenFunction::getVLAElements1D(QualType type) {
2007   const VariableArrayType *vla = getContext().getAsVariableArrayType(type);
2008   assert(vla && "type was not a variable array type!");
2009   return getVLAElements1D(vla);
2010 }
2011 
2012 CodeGenFunction::VlaSizePair
2013 CodeGenFunction::getVLAElements1D(const VariableArrayType *Vla) {
2014   llvm::Value *VlaSize = VLASizeMap[Vla->getSizeExpr()];
2015   assert(VlaSize && "no size for VLA!");
2016   assert(VlaSize->getType() == SizeTy);
2017   return { VlaSize, Vla->getElementType() };
2018 }
2019 
2020 void CodeGenFunction::EmitVariablyModifiedType(QualType type) {
2021   assert(type->isVariablyModifiedType() &&
2022          "Must pass variably modified type to EmitVLASizes!");
2023 
2024   EnsureInsertPoint();
2025 
2026   // We're going to walk down into the type and look for VLA
2027   // expressions.
2028   do {
2029     assert(type->isVariablyModifiedType());
2030 
2031     const Type *ty = type.getTypePtr();
2032     switch (ty->getTypeClass()) {
2033 
2034 #define TYPE(Class, Base)
2035 #define ABSTRACT_TYPE(Class, Base)
2036 #define NON_CANONICAL_TYPE(Class, Base)
2037 #define DEPENDENT_TYPE(Class, Base) case Type::Class:
2038 #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base)
2039 #include "clang/AST/TypeNodes.def"
2040       llvm_unreachable("unexpected dependent type!");
2041 
2042     // These types are never variably-modified.
2043     case Type::Builtin:
2044     case Type::Complex:
2045     case Type::Vector:
2046     case Type::ExtVector:
2047     case Type::Record:
2048     case Type::Enum:
2049     case Type::Elaborated:
2050     case Type::TemplateSpecialization:
2051     case Type::ObjCTypeParam:
2052     case Type::ObjCObject:
2053     case Type::ObjCInterface:
2054     case Type::ObjCObjectPointer:
2055       llvm_unreachable("type class is never variably-modified!");
2056 
2057     case Type::Adjusted:
2058       type = cast<AdjustedType>(ty)->getAdjustedType();
2059       break;
2060 
2061     case Type::Decayed:
2062       type = cast<DecayedType>(ty)->getPointeeType();
2063       break;
2064 
2065     case Type::Pointer:
2066       type = cast<PointerType>(ty)->getPointeeType();
2067       break;
2068 
2069     case Type::BlockPointer:
2070       type = cast<BlockPointerType>(ty)->getPointeeType();
2071       break;
2072 
2073     case Type::LValueReference:
2074     case Type::RValueReference:
2075       type = cast<ReferenceType>(ty)->getPointeeType();
2076       break;
2077 
2078     case Type::MemberPointer:
2079       type = cast<MemberPointerType>(ty)->getPointeeType();
2080       break;
2081 
2082     case Type::ConstantArray:
2083     case Type::IncompleteArray:
2084       // Losing element qualification here is fine.
2085       type = cast<ArrayType>(ty)->getElementType();
2086       break;
2087 
2088     case Type::VariableArray: {
2089       // Losing element qualification here is fine.
2090       const VariableArrayType *vat = cast<VariableArrayType>(ty);
2091 
2092       // Unknown size indication requires no size computation.
2093       // Otherwise, evaluate and record it.
2094       if (const Expr *size = vat->getSizeExpr()) {
2095         // It's possible that we might have emitted this already,
2096         // e.g. with a typedef and a pointer to it.
2097         llvm::Value *&entry = VLASizeMap[size];
2098         if (!entry) {
2099           llvm::Value *Size = EmitScalarExpr(size);
2100 
2101           // C11 6.7.6.2p5:
2102           //   If the size is an expression that is not an integer constant
2103           //   expression [...] each time it is evaluated it shall have a value
2104           //   greater than zero.
2105           if (SanOpts.has(SanitizerKind::VLABound) &&
2106               size->getType()->isSignedIntegerType()) {
2107             SanitizerScope SanScope(this);
2108             llvm::Value *Zero = llvm::Constant::getNullValue(Size->getType());
2109             llvm::Constant *StaticArgs[] = {
2110                 EmitCheckSourceLocation(size->getBeginLoc()),
2111                 EmitCheckTypeDescriptor(size->getType())};
2112             EmitCheck(std::make_pair(Builder.CreateICmpSGT(Size, Zero),
2113                                      SanitizerKind::VLABound),
2114                       SanitizerHandler::VLABoundNotPositive, StaticArgs, Size);
2115           }
2116 
2117           // Always zexting here would be wrong if it weren't
2118           // undefined behavior to have a negative bound.
2119           entry = Builder.CreateIntCast(Size, SizeTy, /*signed*/ false);
2120         }
2121       }
2122       type = vat->getElementType();
2123       break;
2124     }
2125 
2126     case Type::FunctionProto:
2127     case Type::FunctionNoProto:
2128       type = cast<FunctionType>(ty)->getReturnType();
2129       break;
2130 
2131     case Type::Paren:
2132     case Type::TypeOf:
2133     case Type::UnaryTransform:
2134     case Type::Attributed:
2135     case Type::SubstTemplateTypeParm:
2136     case Type::PackExpansion:
2137       // Keep walking after single level desugaring.
2138       type = type.getSingleStepDesugaredType(getContext());
2139       break;
2140 
2141     case Type::Typedef:
2142     case Type::Decltype:
2143     case Type::Auto:
2144     case Type::DeducedTemplateSpecialization:
2145       // Stop walking: nothing to do.
2146       return;
2147 
2148     case Type::TypeOfExpr:
2149       // Stop walking: emit typeof expression.
2150       EmitIgnoredExpr(cast<TypeOfExprType>(ty)->getUnderlyingExpr());
2151       return;
2152 
2153     case Type::Atomic:
2154       type = cast<AtomicType>(ty)->getValueType();
2155       break;
2156 
2157     case Type::Pipe:
2158       type = cast<PipeType>(ty)->getElementType();
2159       break;
2160     }
2161   } while (type->isVariablyModifiedType());
2162 }
2163 
2164 Address CodeGenFunction::EmitVAListRef(const Expr* E) {
2165   if (getContext().getBuiltinVaListType()->isArrayType())
2166     return EmitPointerWithAlignment(E);
2167   return EmitLValue(E).getAddress();
2168 }
2169 
2170 Address CodeGenFunction::EmitMSVAListRef(const Expr *E) {
2171   return EmitLValue(E).getAddress();
2172 }
2173 
2174 void CodeGenFunction::EmitDeclRefExprDbgValue(const DeclRefExpr *E,
2175                                               const APValue &Init) {
2176   assert(!Init.isUninit() && "Invalid DeclRefExpr initializer!");
2177   if (CGDebugInfo *Dbg = getDebugInfo())
2178     if (CGM.getCodeGenOpts().getDebugInfo() >= codegenoptions::LimitedDebugInfo)
2179       Dbg->EmitGlobalVariable(E->getDecl(), Init);
2180 }
2181 
2182 CodeGenFunction::PeepholeProtection
2183 CodeGenFunction::protectFromPeepholes(RValue rvalue) {
2184   // At the moment, the only aggressive peephole we do in IR gen
2185   // is trunc(zext) folding, but if we add more, we can easily
2186   // extend this protection.
2187 
2188   if (!rvalue.isScalar()) return PeepholeProtection();
2189   llvm::Value *value = rvalue.getScalarVal();
2190   if (!isa<llvm::ZExtInst>(value)) return PeepholeProtection();
2191 
2192   // Just make an extra bitcast.
2193   assert(HaveInsertPoint());
2194   llvm::Instruction *inst = new llvm::BitCastInst(value, value->getType(), "",
2195                                                   Builder.GetInsertBlock());
2196 
2197   PeepholeProtection protection;
2198   protection.Inst = inst;
2199   return protection;
2200 }
2201 
2202 void CodeGenFunction::unprotectFromPeepholes(PeepholeProtection protection) {
2203   if (!protection.Inst) return;
2204 
2205   // In theory, we could try to duplicate the peepholes now, but whatever.
2206   protection.Inst->eraseFromParent();
2207 }
2208 
2209 void CodeGenFunction::EmitAlignmentAssumption(llvm::Value *PtrValue,
2210                                               QualType Ty, SourceLocation Loc,
2211                                               SourceLocation AssumptionLoc,
2212                                               llvm::Value *Alignment,
2213                                               llvm::Value *OffsetValue) {
2214   llvm::Value *TheCheck;
2215   llvm::Instruction *Assumption = Builder.CreateAlignmentAssumption(
2216       CGM.getDataLayout(), PtrValue, Alignment, OffsetValue, &TheCheck);
2217   if (SanOpts.has(SanitizerKind::Alignment)) {
2218     EmitAlignmentAssumptionCheck(PtrValue, Ty, Loc, AssumptionLoc, Alignment,
2219                                  OffsetValue, TheCheck, Assumption);
2220   }
2221 }
2222 
2223 void CodeGenFunction::EmitAlignmentAssumption(llvm::Value *PtrValue,
2224                                               QualType Ty, SourceLocation Loc,
2225                                               SourceLocation AssumptionLoc,
2226                                               unsigned Alignment,
2227                                               llvm::Value *OffsetValue) {
2228   llvm::Value *TheCheck;
2229   llvm::Instruction *Assumption = Builder.CreateAlignmentAssumption(
2230       CGM.getDataLayout(), PtrValue, Alignment, OffsetValue, &TheCheck);
2231   if (SanOpts.has(SanitizerKind::Alignment)) {
2232     llvm::Value *AlignmentVal = llvm::ConstantInt::get(IntPtrTy, Alignment);
2233     EmitAlignmentAssumptionCheck(PtrValue, Ty, Loc, AssumptionLoc, AlignmentVal,
2234                                  OffsetValue, TheCheck, Assumption);
2235   }
2236 }
2237 
2238 void CodeGenFunction::EmitAlignmentAssumption(llvm::Value *PtrValue,
2239                                               const Expr *E,
2240                                               SourceLocation AssumptionLoc,
2241                                               unsigned Alignment,
2242                                               llvm::Value *OffsetValue) {
2243   if (auto *CE = dyn_cast<CastExpr>(E))
2244     E = CE->getSubExprAsWritten();
2245   QualType Ty = E->getType();
2246   SourceLocation Loc = E->getExprLoc();
2247 
2248   EmitAlignmentAssumption(PtrValue, Ty, Loc, AssumptionLoc, Alignment,
2249                           OffsetValue);
2250 }
2251 
2252 llvm::Value *CodeGenFunction::EmitAnnotationCall(llvm::Function *AnnotationFn,
2253                                                  llvm::Value *AnnotatedVal,
2254                                                  StringRef AnnotationStr,
2255                                                  SourceLocation Location) {
2256   llvm::Value *Args[4] = {
2257     AnnotatedVal,
2258     Builder.CreateBitCast(CGM.EmitAnnotationString(AnnotationStr), Int8PtrTy),
2259     Builder.CreateBitCast(CGM.EmitAnnotationUnit(Location), Int8PtrTy),
2260     CGM.EmitAnnotationLineNo(Location)
2261   };
2262   return Builder.CreateCall(AnnotationFn, Args);
2263 }
2264 
2265 void CodeGenFunction::EmitVarAnnotations(const VarDecl *D, llvm::Value *V) {
2266   assert(D->hasAttr<AnnotateAttr>() && "no annotate attribute");
2267   // FIXME We create a new bitcast for every annotation because that's what
2268   // llvm-gcc was doing.
2269   for (const auto *I : D->specific_attrs<AnnotateAttr>())
2270     EmitAnnotationCall(CGM.getIntrinsic(llvm::Intrinsic::var_annotation),
2271                        Builder.CreateBitCast(V, CGM.Int8PtrTy, V->getName()),
2272                        I->getAnnotation(), D->getLocation());
2273 }
2274 
2275 Address CodeGenFunction::EmitFieldAnnotations(const FieldDecl *D,
2276                                               Address Addr) {
2277   assert(D->hasAttr<AnnotateAttr>() && "no annotate attribute");
2278   llvm::Value *V = Addr.getPointer();
2279   llvm::Type *VTy = V->getType();
2280   llvm::Function *F = CGM.getIntrinsic(llvm::Intrinsic::ptr_annotation,
2281                                     CGM.Int8PtrTy);
2282 
2283   for (const auto *I : D->specific_attrs<AnnotateAttr>()) {
2284     // FIXME Always emit the cast inst so we can differentiate between
2285     // annotation on the first field of a struct and annotation on the struct
2286     // itself.
2287     if (VTy != CGM.Int8PtrTy)
2288       V = Builder.CreateBitCast(V, CGM.Int8PtrTy);
2289     V = EmitAnnotationCall(F, V, I->getAnnotation(), D->getLocation());
2290     V = Builder.CreateBitCast(V, VTy);
2291   }
2292 
2293   return Address(V, Addr.getAlignment());
2294 }
2295 
2296 CodeGenFunction::CGCapturedStmtInfo::~CGCapturedStmtInfo() { }
2297 
2298 CodeGenFunction::SanitizerScope::SanitizerScope(CodeGenFunction *CGF)
2299     : CGF(CGF) {
2300   assert(!CGF->IsSanitizerScope);
2301   CGF->IsSanitizerScope = true;
2302 }
2303 
2304 CodeGenFunction::SanitizerScope::~SanitizerScope() {
2305   CGF->IsSanitizerScope = false;
2306 }
2307 
2308 void CodeGenFunction::InsertHelper(llvm::Instruction *I,
2309                                    const llvm::Twine &Name,
2310                                    llvm::BasicBlock *BB,
2311                                    llvm::BasicBlock::iterator InsertPt) const {
2312   LoopStack.InsertHelper(I);
2313   if (IsSanitizerScope)
2314     CGM.getSanitizerMetadata()->disableSanitizerForInstruction(I);
2315 }
2316 
2317 void CGBuilderInserter::InsertHelper(
2318     llvm::Instruction *I, const llvm::Twine &Name, llvm::BasicBlock *BB,
2319     llvm::BasicBlock::iterator InsertPt) const {
2320   llvm::IRBuilderDefaultInserter::InsertHelper(I, Name, BB, InsertPt);
2321   if (CGF)
2322     CGF->InsertHelper(I, Name, BB, InsertPt);
2323 }
2324 
2325 static bool hasRequiredFeatures(const SmallVectorImpl<StringRef> &ReqFeatures,
2326                                 CodeGenModule &CGM, const FunctionDecl *FD,
2327                                 std::string &FirstMissing) {
2328   // If there aren't any required features listed then go ahead and return.
2329   if (ReqFeatures.empty())
2330     return false;
2331 
2332   // Now build up the set of caller features and verify that all the required
2333   // features are there.
2334   llvm::StringMap<bool> CallerFeatureMap;
2335   CGM.getFunctionFeatureMap(CallerFeatureMap, GlobalDecl().getWithDecl(FD));
2336 
2337   // If we have at least one of the features in the feature list return
2338   // true, otherwise return false.
2339   return std::all_of(
2340       ReqFeatures.begin(), ReqFeatures.end(), [&](StringRef Feature) {
2341         SmallVector<StringRef, 1> OrFeatures;
2342         Feature.split(OrFeatures, '|');
2343         return llvm::any_of(OrFeatures, [&](StringRef Feature) {
2344           if (!CallerFeatureMap.lookup(Feature)) {
2345             FirstMissing = Feature.str();
2346             return false;
2347           }
2348           return true;
2349         });
2350       });
2351 }
2352 
2353 // Emits an error if we don't have a valid set of target features for the
2354 // called function.
2355 void CodeGenFunction::checkTargetFeatures(const CallExpr *E,
2356                                           const FunctionDecl *TargetDecl) {
2357   // Early exit if this is an indirect call.
2358   if (!TargetDecl)
2359     return;
2360 
2361   // Get the current enclosing function if it exists. If it doesn't
2362   // we can't check the target features anyhow.
2363   const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl);
2364   if (!FD)
2365     return;
2366 
2367   // Grab the required features for the call. For a builtin this is listed in
2368   // the td file with the default cpu, for an always_inline function this is any
2369   // listed cpu and any listed features.
2370   unsigned BuiltinID = TargetDecl->getBuiltinID();
2371   std::string MissingFeature;
2372   if (BuiltinID) {
2373     SmallVector<StringRef, 1> ReqFeatures;
2374     const char *FeatureList =
2375         CGM.getContext().BuiltinInfo.getRequiredFeatures(BuiltinID);
2376     // Return if the builtin doesn't have any required features.
2377     if (!FeatureList || StringRef(FeatureList) == "")
2378       return;
2379     StringRef(FeatureList).split(ReqFeatures, ',');
2380     if (!hasRequiredFeatures(ReqFeatures, CGM, FD, MissingFeature))
2381       CGM.getDiags().Report(E->getBeginLoc(), diag::err_builtin_needs_feature)
2382           << TargetDecl->getDeclName()
2383           << CGM.getContext().BuiltinInfo.getRequiredFeatures(BuiltinID);
2384 
2385   } else if (TargetDecl->hasAttr<TargetAttr>() ||
2386              TargetDecl->hasAttr<CPUSpecificAttr>()) {
2387     // Get the required features for the callee.
2388 
2389     const TargetAttr *TD = TargetDecl->getAttr<TargetAttr>();
2390     TargetAttr::ParsedTargetAttr ParsedAttr = CGM.filterFunctionTargetAttrs(TD);
2391 
2392     SmallVector<StringRef, 1> ReqFeatures;
2393     llvm::StringMap<bool> CalleeFeatureMap;
2394     CGM.getFunctionFeatureMap(CalleeFeatureMap, TargetDecl);
2395 
2396     for (const auto &F : ParsedAttr.Features) {
2397       if (F[0] == '+' && CalleeFeatureMap.lookup(F.substr(1)))
2398         ReqFeatures.push_back(StringRef(F).substr(1));
2399     }
2400 
2401     for (const auto &F : CalleeFeatureMap) {
2402       // Only positive features are "required".
2403       if (F.getValue())
2404         ReqFeatures.push_back(F.getKey());
2405     }
2406     if (!hasRequiredFeatures(ReqFeatures, CGM, FD, MissingFeature))
2407       CGM.getDiags().Report(E->getBeginLoc(), diag::err_function_needs_feature)
2408           << FD->getDeclName() << TargetDecl->getDeclName() << MissingFeature;
2409   }
2410 }
2411 
2412 void CodeGenFunction::EmitSanitizerStatReport(llvm::SanitizerStatKind SSK) {
2413   if (!CGM.getCodeGenOpts().SanitizeStats)
2414     return;
2415 
2416   llvm::IRBuilder<> IRB(Builder.GetInsertBlock(), Builder.GetInsertPoint());
2417   IRB.SetCurrentDebugLocation(Builder.getCurrentDebugLocation());
2418   CGM.getSanStats().create(IRB, SSK);
2419 }
2420 
2421 llvm::Value *
2422 CodeGenFunction::FormResolverCondition(const MultiVersionResolverOption &RO) {
2423   llvm::Value *Condition = nullptr;
2424 
2425   if (!RO.Conditions.Architecture.empty())
2426     Condition = EmitX86CpuIs(RO.Conditions.Architecture);
2427 
2428   if (!RO.Conditions.Features.empty()) {
2429     llvm::Value *FeatureCond = EmitX86CpuSupports(RO.Conditions.Features);
2430     Condition =
2431         Condition ? Builder.CreateAnd(Condition, FeatureCond) : FeatureCond;
2432   }
2433   return Condition;
2434 }
2435 
2436 static void CreateMultiVersionResolverReturn(CodeGenModule &CGM,
2437                                              llvm::Function *Resolver,
2438                                              CGBuilderTy &Builder,
2439                                              llvm::Function *FuncToReturn,
2440                                              bool SupportsIFunc) {
2441   if (SupportsIFunc) {
2442     Builder.CreateRet(FuncToReturn);
2443     return;
2444   }
2445 
2446   llvm::SmallVector<llvm::Value *, 10> Args;
2447   llvm::for_each(Resolver->args(),
2448                  [&](llvm::Argument &Arg) { Args.push_back(&Arg); });
2449 
2450   llvm::CallInst *Result = Builder.CreateCall(FuncToReturn, Args);
2451   Result->setTailCallKind(llvm::CallInst::TCK_MustTail);
2452 
2453   if (Resolver->getReturnType()->isVoidTy())
2454     Builder.CreateRetVoid();
2455   else
2456     Builder.CreateRet(Result);
2457 }
2458 
2459 void CodeGenFunction::EmitMultiVersionResolver(
2460     llvm::Function *Resolver, ArrayRef<MultiVersionResolverOption> Options) {
2461   assert((getContext().getTargetInfo().getTriple().getArch() ==
2462               llvm::Triple::x86 ||
2463           getContext().getTargetInfo().getTriple().getArch() ==
2464               llvm::Triple::x86_64) &&
2465          "Only implemented for x86 targets");
2466 
2467   bool SupportsIFunc = getContext().getTargetInfo().supportsIFunc();
2468 
2469   // Main function's basic block.
2470   llvm::BasicBlock *CurBlock = createBasicBlock("resolver_entry", Resolver);
2471   Builder.SetInsertPoint(CurBlock);
2472   EmitX86CpuInit();
2473 
2474   for (const MultiVersionResolverOption &RO : Options) {
2475     Builder.SetInsertPoint(CurBlock);
2476     llvm::Value *Condition = FormResolverCondition(RO);
2477 
2478     // The 'default' or 'generic' case.
2479     if (!Condition) {
2480       assert(&RO == Options.end() - 1 &&
2481              "Default or Generic case must be last");
2482       CreateMultiVersionResolverReturn(CGM, Resolver, Builder, RO.Function,
2483                                        SupportsIFunc);
2484       return;
2485     }
2486 
2487     llvm::BasicBlock *RetBlock = createBasicBlock("resolver_return", Resolver);
2488     CGBuilderTy RetBuilder(*this, RetBlock);
2489     CreateMultiVersionResolverReturn(CGM, Resolver, RetBuilder, RO.Function,
2490                                      SupportsIFunc);
2491     CurBlock = createBasicBlock("resolver_else", Resolver);
2492     Builder.CreateCondBr(Condition, RetBlock, CurBlock);
2493   }
2494 
2495   // If no generic/default, emit an unreachable.
2496   Builder.SetInsertPoint(CurBlock);
2497   llvm::CallInst *TrapCall = EmitTrapCall(llvm::Intrinsic::trap);
2498   TrapCall->setDoesNotReturn();
2499   TrapCall->setDoesNotThrow();
2500   Builder.CreateUnreachable();
2501   Builder.ClearInsertionPoint();
2502 }
2503 
2504 // Loc - where the diagnostic will point, where in the source code this
2505 //  alignment has failed.
2506 // SecondaryLoc - if present (will be present if sufficiently different from
2507 //  Loc), the diagnostic will additionally point a "Note:" to this location.
2508 //  It should be the location where the __attribute__((assume_aligned))
2509 //  was written e.g.
2510 void CodeGenFunction::EmitAlignmentAssumptionCheck(
2511     llvm::Value *Ptr, QualType Ty, SourceLocation Loc,
2512     SourceLocation SecondaryLoc, llvm::Value *Alignment,
2513     llvm::Value *OffsetValue, llvm::Value *TheCheck,
2514     llvm::Instruction *Assumption) {
2515   assert(Assumption && isa<llvm::CallInst>(Assumption) &&
2516          cast<llvm::CallInst>(Assumption)->getCalledValue() ==
2517              llvm::Intrinsic::getDeclaration(
2518                  Builder.GetInsertBlock()->getParent()->getParent(),
2519                  llvm::Intrinsic::assume) &&
2520          "Assumption should be a call to llvm.assume().");
2521   assert(&(Builder.GetInsertBlock()->back()) == Assumption &&
2522          "Assumption should be the last instruction of the basic block, "
2523          "since the basic block is still being generated.");
2524 
2525   if (!SanOpts.has(SanitizerKind::Alignment))
2526     return;
2527 
2528   // Don't check pointers to volatile data. The behavior here is implementation-
2529   // defined.
2530   if (Ty->getPointeeType().isVolatileQualified())
2531     return;
2532 
2533   // We need to temorairly remove the assumption so we can insert the
2534   // sanitizer check before it, else the check will be dropped by optimizations.
2535   Assumption->removeFromParent();
2536 
2537   {
2538     SanitizerScope SanScope(this);
2539 
2540     if (!OffsetValue)
2541       OffsetValue = Builder.getInt1(0); // no offset.
2542 
2543     llvm::Constant *StaticData[] = {EmitCheckSourceLocation(Loc),
2544                                     EmitCheckSourceLocation(SecondaryLoc),
2545                                     EmitCheckTypeDescriptor(Ty)};
2546     llvm::Value *DynamicData[] = {EmitCheckValue(Ptr),
2547                                   EmitCheckValue(Alignment),
2548                                   EmitCheckValue(OffsetValue)};
2549     EmitCheck({std::make_pair(TheCheck, SanitizerKind::Alignment)},
2550               SanitizerHandler::AlignmentAssumption, StaticData, DynamicData);
2551   }
2552 
2553   // We are now in the (new, empty) "cont" basic block.
2554   // Reintroduce the assumption.
2555   Builder.Insert(Assumption);
2556   // FIXME: Assumption still has it's original basic block as it's Parent.
2557 }
2558 
2559 llvm::DebugLoc CodeGenFunction::SourceLocToDebugLoc(SourceLocation Location) {
2560   if (CGDebugInfo *DI = getDebugInfo())
2561     return DI->SourceLocToDebugLoc(Location);
2562 
2563   return llvm::DebugLoc();
2564 }
2565