clang  9.0.0svn
CodeGenFunction.cpp
Go to the documentation of this file.
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"
31 #include "clang/Basic/TargetInfo.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)
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 
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)
103 
104  if (getLangOpts().OpenMP && CurFn)
105  CGM.getOpenMPRuntime().functionFinished(*this);
106 }
107 
109  LValueBaseInfo *BaseInfo,
110  TBAAAccessInfo *TBAAInfo) {
111  return getNaturalTypeAlignment(T->getPointeeType(), BaseInfo, TBAAInfo,
112  /* forPointeeType= */ true);
113 }
114 
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)
129  return getContext().toCharUnitsFromBits(Align);
130  }
131  }
132 
133  if (BaseInfo)
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 &&
157  Alignment = CharUnits::fromQuantity(MaxAlign);
158  }
159  }
160  return Alignment;
161 }
162 
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
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 
184  return CGM.getTypes().ConvertTypeForMem(T);
185 }
186 
188  return CGM.getTypes().ConvertType(T);
189 }
190 
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 
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
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 
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 
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 =
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 
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);
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.
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.
426  llvm::DominatorTree DT(*CurFn);
427  llvm::PromoteMemToReg(
428  cast<llvm::AllocaInst>(NormalCleanupDest.getPointer()), DT);
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
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.
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.
474  return CGM.getCodeGenOpts().XRayInstrumentFunctions &&
475  (CGM.getCodeGenOpts().XRayAlwaysEmitCustomEvents ||
478 }
479 
481  return CGM.getCodeGenOpts().XRayInstrumentFunctions &&
482  (CGM.getCodeGenOpts().XRayAlwaysEmitTypedEvents ||
485 }
486 
487 llvm::Constant *
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 *
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.
575 
576  // MDNode for the kernel argument access qualifiers (images only).
578 
579  // MDNode for the kernel argument type names.
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.
587 
588  // MDNode for the kernel argument names.
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 =
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)
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 =
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 
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;
823 }
824 
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"))) {
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)) {
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>()) {
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>())
928  Fn->addFnAttr("xray-log-args",
929  llvm::utostr(LogArgs->getArgumentCount()));
930  }
931  } else {
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 =
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)) {
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 
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.
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.
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();
1080  ++AI;
1082  } else if (CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::InAlloca &&
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");
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 &&
1099  RetTy->isObjCRetainableType())
1100  AutoreleaseResult = true;
1101  }
1102 
1104 
1106 
1107  // Emit OpenMP specific initialization of the device functions.
1108  if (getLangOpts().OpenMP && CurCodeDecl)
1109  CGM.getOpenMPRuntime().emitFunctionProlog(*this, CurCodeDecl);
1110 
1112 
1113  if (D && isa<CXXMethodDecl>(D) && cast<CXXMethodDecl>(D)->isInstance()) {
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.
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).
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),
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) &&
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())
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 
1206  if (const CompoundStmt *S = dyn_cast<CompoundStmt>(Body))
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().
1217  const Stmt *S) {
1218  llvm::BasicBlock *SkipCountBB = nullptr;
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();
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 
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
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.
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.
1383  !FD->getReturnType()->isVoidType() && Builder.GetInsertBlock()) {
1384  bool ShouldEmitUnreachable =
1385  CGM.getCodeGenOpts().StrictReturn ||
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())
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.
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 
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.
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.
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 ///
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
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.
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)
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 
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.
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 
1977  assert(vla && "type was not a variable array type!");
1978  return getVLASize(vla);
1979 }
1980 
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 
2008  assert(vla && "type was not a variable array type!");
2009  return getVLAElements1D(vla);
2010 }
2011 
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 
2021  assert(type->isVariablyModifiedType() &&
2022  "Must pass variably modified type to EmitVLASizes!");
2023 
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 
2165  if (getContext().getBuiltinVaListType()->isArrayType())
2166  return EmitPointerWithAlignment(E);
2167  return EmitLValue(E).getAddress();
2168 }
2169 
2171  return EmitLValue(E).getAddress();
2172 }
2173 
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 
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 
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 
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 
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 
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,
2260  CGM.EmitAnnotationLineNo(Location)
2261  };
2262  return Builder.CreateCall(AnnotationFn, Args);
2263 }
2264 
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 
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)
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 
2297 
2299  : CGF(CGF) {
2300  assert(!CGF->IsSanitizerScope);
2301  CGF->IsSanitizerScope = true;
2302 }
2303 
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 {
2313  if (IsSanitizerScope)
2315 }
2316 
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.
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 =
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()
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 
2437  llvm::Function *Resolver,
2439  llvm::Function *FuncToReturn,
2440  bool SupportsIFunc) {
2441  if (SupportsIFunc) {
2442  Builder.CreateRet(FuncToReturn);
2443  return;
2444  }
2445 
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 
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");
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.
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),
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 
2560  if (CGDebugInfo *DI = getDebugInfo())
2561  return DI->SourceLocToDebugLoc(Location);
2562 
2563  return llvm::DebugLoc();
2564 }
const llvm::DataLayout & getDataLayout() const
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
Address CreateStructGEP(Address Addr, unsigned Index, const llvm::Twine &Name="")
Definition: CGBuilder.h:178
Defines the clang::ASTContext interface.
Represents a function declaration or definition.
Definition: Decl.h:1737
LValue MakeNaturalAlignPointeeAddrLValue(llvm::Value *V, QualType T)
Given a value of type T* that may not be to a complete object, construct an l-value with the natural ...
Other implicit parameter.
Definition: Decl.h:1509
no exception specification
PointerType - C99 6.7.5.1 - Pointer Declarators.
Definition: Type.h:2542
CanQualType VoidPtrTy
Definition: ASTContext.h:1043
A (possibly-)qualified type.
Definition: Type.h:634
llvm::Type * ConvertTypeForMem(QualType T)
void EmitBranchOnBoolExpr(const Expr *Cond, llvm::BasicBlock *TrueBlock, llvm::BasicBlock *FalseBlock, uint64_t TrueCount)
EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g.
const CodeGenOptions & getCodeGenOpts() const
bool isReturnsRetained() const
In ARC, whether this function retains its return value.
llvm::Constant * EmitCheckTypeDescriptor(QualType T)
Emit a description of a type in a format suitable for passing to a runtime sanitizer handler...
Definition: CGExpr.cpp:2714
bool HaveInsertPoint() const
HaveInsertPoint - True if an insertion point is defined.
llvm::Constant * EncodeAddrForUseInPrologue(llvm::Function *F, llvm::Constant *Addr)
Encode an address into a form suitable for use in a function prologue.
XRayInstrMask Mask
Definition: XRayInstr.h:64
DominatorTree GraphTraits specialization so the DominatorTree can be iterable by generic graph iterat...
Definition: Dominators.h:29
virtual void addImplicitStructorParams(CodeGenFunction &CGF, QualType &ResTy, FunctionArgList &Params)=0
Insert any ABI-specific implicit parameters into the parameter list for a function.
CharUnits getClassPointerAlignment(const CXXRecordDecl *CD)
Returns the assumed alignment of an opaque pointer to the given class.
Definition: CGClass.cpp:36
bool EvaluateAsInt(EvalResult &Result, const ASTContext &Ctx, SideEffectsKind AllowSideEffects=SE_NoSideEffects) const
EvaluateAsInt - Return true if this is a constant which we can fold and convert to an integer...
Stmt - This represents one statement.
Definition: Stmt.h:65
FunctionType - C99 6.7.5.3 - Function Declarators.
Definition: Type.h:3360
SanitizerSet Sanitize
Set of enabled sanitizers.
Definition: LangOptions.h:183
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee...
Definition: Type.cpp:505
bool isMain() const
Determines whether this function is "main", which is the entry point into an executable program...
Definition: Decl.cpp:2759
static bool endsWithReturn(const Decl *F)
Determine whether the function F ends with a return stmt.
QualType getThisType() const
Returns the type of the this pointer.
Definition: DeclCXX.cpp:2199
Checking the &#39;this&#39; pointer for a constructor call.
Address EmitPointerWithAlignment(const Expr *Addr, LValueBaseInfo *BaseInfo=nullptr, TBAAAccessInfo *TBAAInfo=nullptr)
EmitPointerWithAlignment - Given an expression with a pointer type, emit the value and compute our be...
Definition: CGExpr.cpp:1036
constexpr XRayInstrMask Typed
Definition: XRayInstr.h:40
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
const Decl * CurCodeDecl
CurCodeDecl - This is the inner-most code context, which includes blocks.
void EmitLambdaStaticInvokeBody(const CXXMethodDecl *MD)
Definition: CGClass.cpp:2896
void EmitCheck(ArrayRef< std::pair< llvm::Value *, SanitizerMask >> Checked, SanitizerHandler Check, ArrayRef< llvm::Constant *> StaticArgs, ArrayRef< llvm::Value *> DynamicArgs)
Create a basic block that will either trap or call a handler function in the UBSan runtime with the p...
Definition: CGExpr.cpp:2931
bool isUninit() const
Definition: APValue.h:237
The base class of the type hierarchy.
Definition: Type.h:1409
bool ShouldInstrumentFunction()
ShouldInstrumentFunction - Return true if the current function should be instrumented with __cyg_prof...
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
Definition: Diagnostic.h:1294
bool usesSEHTry() const
Indicates the function uses __try.
Definition: Decl.h:2113
void GenerateCode(GlobalDecl GD, llvm::Function *Fn, const CGFunctionInfo &FnInfo)
Represents an array type, per C99 6.7.5.2 - Array Declarators.
Definition: Type.h:2817
bool isRestrictQualified() const
Determine whether this type is restrict-qualified.
Definition: Type.h:6141
bool isZero() const
isZero - Test whether the quantity equals zero.
Definition: CharUnits.h:115
static bool hasRequiredFeatures(const SmallVectorImpl< StringRef > &ReqFeatures, CodeGenModule &CGM, const FunctionDecl *FD, std::string &FirstMissing)
stable_iterator stable_begin() const
Create a stable reference to the top of the EH stack.
Definition: EHScopeStack.h:378
const TargetInfo & getTargetInfo() const
Definition: ASTContext.h:689
void InsertHelper(llvm::Instruction *I, const llvm::Twine &Name, llvm::BasicBlock *BB, llvm::BasicBlock::iterator InsertPt) const
CGBuilder insert helper.
bool imbueXRayAttrs(llvm::Function *Fn, SourceLocation Loc, StringRef Category=StringRef()) const
Imbue XRay attributes to a function, applying the always/never attribute lists in the process...
constexpr XRayInstrMask Function
Definition: XRayInstr.h:38
llvm::IntegerType * Int8Ty
i8, i16, i32, and i64
Represents a C++ constructor within a class.
Definition: DeclCXX.h:2491
bool ConstantFoldsToSimpleInteger(const Expr *Cond, bool &Result, bool AllowLabels=false)
ConstantFoldsToSimpleInteger - If the specified expression does not fold to a constant, or if it does but contains a label, return false.
QualType getElementType() const
Definition: Type.h:2852
bool empty() const
Determines whether the exception-scopes stack is empty.
Definition: EHScopeStack.h:344
This file provides some common utility functions for processing Lambda related AST Constructs...
TypeEvaluationKind
The kind of evaluation to perform on values of a particular type.
Represents a variable declaration or definition.
Definition: Decl.h:812
QualType getReturnType() const
Definition: Decl.h:2301
RAII object to set/unset CodeGenFunction::IsSanitizerScope.
const T * getAs() const
Member-template getAs<specific type>&#39;.
Definition: Type.h:6761
Extra information about a function prototype.
Definition: Type.h:3772
static bool shouldEmitLifetimeMarkers(const CodeGenOptions &CGOpts, const LangOptions &LangOpts)
shouldEmitLifetimeMarkers - Decide whether we need emit the life-time markers.
uint64_t getProfileCount(const Stmt *S)
Get the profiler&#39;s count for the given statement.
LangAS
Defines the address space values used by the address space qualifier of QualType. ...
Definition: AddressSpaces.h:25
This class gathers all debug information during compilation and is responsible for emitting to llvm g...
Definition: CGDebugInfo.h:53
static bool mightAddDeclToScope(const Stmt *S)
Determine if the given statement might introduce a declaration into the current scope, by being a (possibly-labelled) DeclStmt.
DiagnosticsEngine & getDiags() const
llvm::CallInst * EmitTrapCall(llvm::Intrinsic::ID IntrID)
Emit a call to trap or debugtrap and attach function attribute "trap-func-name" if specified...
Definition: CGExpr.cpp:3228
void EmitVariablyModifiedType(QualType Ty)
EmitVLASize - Capture all the sizes for the VLA expressions in the given variably-modified type and s...
void setCurrentProfileCount(uint64_t Count)
Set the profiler&#39;s current count.
bool ShouldXRayInstrumentFunction() const
ShouldXRayInstrument - Return true if the current function should be instrumented with XRay nop sleds...
llvm::Value * getPointer() const
Definition: Address.h:37
virtual llvm::Constant * getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const
Return a constant used by UBSan as a signature to identify functions possessing type information...
Definition: TargetInfo.h:162
void markAsIgnoreThreadCheckingAtRuntime(llvm::Function *Fn)
Annotate the function with an attribute that disables TSan checking at runtime.
bool IsSanitizerScope
True if CodeGen currently emits code implementing sanitizer checks.
Describes how types, statements, expressions, and declarations should be printed. ...
Definition: PrettyPrinter.h:37
Defines the Objective-C statement AST node classes.
llvm::Type * ConvertTypeForMem(QualType T)
ConvertTypeForMem - Convert type T into a llvm::Type.
A C++ throw-expression (C++ [except.throw]).
Definition: ExprCXX.h:1014
bool supportsIFunc() const
Identify whether this target supports IFuncs.
Definition: TargetInfo.h:1106
Represents a parameter to a function.
Definition: Decl.h:1549
static void destroyBlockInfos(CGBlockInfo *info)
Destroy a chain of block layouts.
Definition: CGBlocks.cpp:888
void emitImplicitAssignmentOperatorBody(FunctionArgList &Args)
Definition: CGClass.cpp:1515
EHScopeStack::stable_iterator PrologueCleanupDepth
PrologueCleanupDepth - The cleanup depth enclosing all the cleanups associated with the parameters...
PipeType - OpenCL20.
Definition: Type.h:6007
JumpDest getJumpDestForLabel(const LabelDecl *S)
getBasicBlockForLabel - Return the LLVM basicblock that the specified label maps to.
Definition: CGStmt.cpp:505
llvm::DenseMap< const VarDecl *, FieldDecl * > LambdaCaptureFields
const TargetInfo & getTarget() const
An object to manage conditionally-evaluated expressions.
PeepholeProtection protectFromPeepholes(RValue rvalue)
protectFromPeepholes - Protect a value that we&#39;re intending to store to the side, but which will prob...
DeclarationName getDeclName() const
Get the actual, stored name of the declaration, which may be a special name.
Definition: Decl.h:297
void EmitAlignmentAssumptionCheck(llvm::Value *Ptr, QualType Ty, SourceLocation Loc, SourceLocation AssumptionLoc, llvm::Value *Alignment, llvm::Value *OffsetValue, llvm::Value *TheCheck, llvm::Instruction *Assumption)
One of these records is kept for each identifier that is lexed.
VlaSizePair getVLAElements1D(const VariableArrayType *vla)
Return the number of elements for a single dimension for the given array type.
CGBlockInfo * FirstBlockInfo
FirstBlockInfo - The head of a singly-linked-list of block layouts.
void EmitFunctionEpilog(const CGFunctionInfo &FI, bool EmitRetDbgLoc, SourceLocation EndLoc)
EmitFunctionEpilog - Emit the target specific LLVM code to return the given temporary.
Definition: CGCall.cpp:2806
static void CreateMultiVersionResolverReturn(CodeGenModule &CGM, llvm::Function *Resolver, CGBuilderTy &Builder, llvm::Function *FuncToReturn, bool SupportsIFunc)
Address getAddress() const
Definition: CGValue.h:326
bool isStr(const char(&Str)[StrLen]) const
Return true if this is the identifier for the specified string.
Indirect - Pass the argument indirectly via a hidden pointer with the specified alignment (0 indicate...
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
llvm::Type * ConvertType(QualType T)
ConvertType - Convert type T into a llvm::Type.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:154
LValue EmitLValueForLambdaField(const FieldDecl *Field)
Given that we are currently emitting a lambda, emit an l-value for one of its members.
Definition: CGExpr.cpp:3806
bool hasOneOf(SanitizerMask K) const
Check if one or more sanitizers are enabled.
Definition: Sanitizers.h:57
field_range fields() const
Definition: Decl.h:3783
Represents a member of a struct/union/class.
Definition: Decl.h:2578
SanitizerMask Mask
Bitmask of enabled sanitizers.
Definition: Sanitizers.h:72
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:49
void InitTempAlloca(Address Alloca, llvm::Value *Value)
InitTempAlloca - Provide an initial value for the given alloca which will be observable at all locati...
Definition: CGExpr.cpp:125
void disableSanitizerForInstruction(llvm::Instruction *I)
bool AlwaysEmitXRayTypedEvents() const
AlwaysEmitXRayTypedEvents - Return true if clang must unconditionally emit XRay typed event handling ...
ArrayRef< ParmVarDecl * > parameters() const
Definition: Decl.h:2261
Address CreateIRTemp(QualType T, const Twine &Name="tmp")
CreateIRTemp - Create a temporary IR object of the given type, with appropriate alignment.
Definition: CGExpr.cpp:133
virtual void EmitInstanceFunctionProlog(CodeGenFunction &CGF)=0
Emit the ABI-specific prolog for the function.
CGCUDARuntime & getCUDARuntime()
Return a reference to the configured CUDA runtime.
static bool hasScalarEvaluationKind(QualType T)
Stmt * getBody(const FunctionDecl *&Definition) const
Retrieve the body (definition) of the function.
Definition: Decl.cpp:2730
Address CreateElementBitCast(Address Addr, llvm::Type *Ty, const llvm::Twine &Name="")
Cast the element type of the given address to a different type, preserving information like the align...
Definition: CGBuilder.h:156
CharUnits - This is an opaque type for sizes expressed in character units.
Definition: CharUnits.h:37
APValue Val
Val - This is the value the expression can be folded to.
Definition: Expr.h:575
bool isTriviallyCopyableType(const ASTContext &Context) const
Return true if this is a trivially copyable type (C++0x [basic.types]p9)
Definition: Type.cpp:2205
bool AlwaysEmitXRayCustomEvents() const
AlwaysEmitXRayCustomEvents - Return true if we must unconditionally emit XRay custom event handling c...
bool isOne() const
isOne - Test whether the quantity equals one.
Definition: CharUnits.h:118
TBAAAccessInfo getTBAAAccessInfo(QualType AccessType)
getTBAAAccessInfo - Get TBAA information that describes an access to an object of the given type...
CharUnits getAlignment() const
Return the alignment of this pointer.
Definition: Address.h:66
child_range children()
Definition: Stmt.cpp:212
virtual bool hasMostDerivedReturn(GlobalDecl GD) const
Definition: CGCXXABI.h:108
const clang::PrintingPolicy & getPrintingPolicy() const
Definition: ASTContext.h:653
unsigned getInAllocaFieldIndex() const
A builtin binary operation expression such as "x + y" or "x <= y".
Definition: Expr.h:3313
bool isVolatileQualified() const
Determine whether this type is volatile-qualified.
Definition: Type.h:6147
The l-value was considered opaque, so the alignment was determined from a type, but that type was an ...
CXXCtorType getCtorType() const
Definition: GlobalDecl.h:70
llvm::CallInst * CreateMemCpy(Address Dest, Address Src, llvm::Value *Size, bool IsVolatile=false)
Definition: CGBuilder.h:274
bool containsOnlyLifetimeMarkers(stable_iterator Old) const
Definition: CGCleanup.cpp:141
bool isLambda() const
Determine whether this class describes a lambda function object.
Definition: DeclCXX.h:1195
Values of this type can never be null.
Expr * getSizeExpr() const
Definition: Type.h:2996
const Type * getTypePtr() const
Retrieves a pointer to the underlying (unqualified) type.
Definition: Type.h:6077
llvm::BasicBlock * createBasicBlock(const Twine &name="", llvm::Function *parent=nullptr, llvm::BasicBlock *before=nullptr)
createBasicBlock - Create an LLVM basic block.
void EmitIgnoredExpr(const Expr *E)
EmitIgnoredExpr - Emit an expression in a context which ignores the result.
Definition: CGExpr.cpp:181
void assignRegionCounters(GlobalDecl GD, llvm::Function *Fn)
Assign counters to regions and configure them for PGO of a given function.
Definition: CodeGenPGO.cpp:759
GlobalDecl CurGD
CurGD - The GlobalDecl for the current function being compiled.
bool isInstance() const
Definition: DeclCXX.h:2076
static bool ContainsLabel(const Stmt *S, bool IgnoreCaseStmts=false)
ContainsLabel - Return true if the statement contains a label in it.
bool isAlignmentRequired(const Type *T) const
Determine if the alignment the type has was required using an alignment attribute.
llvm::SanitizerStatReport & getSanStats()
llvm::DebugLoc EmitReturnBlock()
Emit the unified return block, trying to avoid its emission when possible.
bool isLambdaCallOperator(const CXXMethodDecl *MD)
Definition: ASTLambda.h:27
void incrementProfileCounter(const Stmt *S, llvm::Value *StepV=nullptr)
Increment the profiler&#39;s counter for the given statement by StepV.
uint64_t getCurrentProfileCount()
Get the profiler&#39;s current count.
Address NormalCleanupDest
i32s containing the indexes of the cleanup destinations.
virtual void emitDeviceStub(CodeGenFunction &CGF, FunctionArgList &Args)=0
Emits a kernel launch stub.
static void removeImageAccessQualifier(std::string &TyName)
llvm::Value * EmitCheckValue(llvm::Value *V)
Convert a value into a format suitable for passing to a runtime sanitizer handler.
Definition: CGExpr.cpp:2757
Expr * IgnoreImpCasts() LLVM_READONLY
Skip past any implicit casts which might surround this expression until reaching a fixed point...
Definition: Expr.cpp:2559
Checking the &#39;this&#39; pointer for a call to a non-static member function.
static ImplicitParamDecl * Create(ASTContext &C, DeclContext *DC, SourceLocation IdLoc, IdentifierInfo *Id, QualType T, ImplicitParamKind ParamKind)
Create implicit parameter.
Definition: Decl.cpp:4409
void InsertHelper(llvm::Instruction *I) const
Function called by the CodeGenFunction when an instruction is created.
Definition: CGLoopInfo.cpp:370
bool hasAttr() const
Definition: DeclBase.h:533
ConditionalOperator - The ?: ternary operator.
Definition: Expr.h:3608
CanQualType getReturnType() const
bool isValid() const
Definition: Address.h:35
static CharUnits One()
One - Construct a CharUnits quantity of one.
Definition: CharUnits.h:57
CompoundStmt - This represents a group of statements like { stmt stmt }.
Definition: Stmt.h:1237
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition: Type.cpp:1613
const TargetCodeGenInfo & getTargetCodeGenInfo()
OverloadedOperatorKind getOverloadedOperator() const
getOverloadedOperator - Which C++ overloaded operator this function represents, if any...
Definition: Decl.cpp:3299
RValue - This trivial value class is used to represent the result of an expression that is evaluated...
Definition: CGValue.h:38
void EmitSanitizerStatReport(llvm::SanitizerStatKind SSK)
static void EmitIfUsed(CodeGenFunction &CGF, llvm::BasicBlock *BB)
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
Definition: CharUnits.h:178
llvm::Value * DecodeAddrUsedInPrologue(llvm::Value *F, llvm::Value *EncodedAddr)
Decode an address used in a function prologue, encoded by EncodeAddrForUseInPrologue.
Address CreateDefaultAlignTempAlloca(llvm::Type *Ty, const Twine &Name="tmp")
CreateDefaultAlignedTempAlloca - This creates an alloca with the default ABI alignment of the given L...
Definition: CGExpr.cpp:118
virtual bool HasThisReturn(GlobalDecl GD) const
Returns true if the given constructor or destructor is one of the kinds that the ABI says returns &#39;th...
Definition: CGCXXABI.h:106
static TypeEvaluationKind getEvaluationKind(QualType T)
getEvaluationKind - Return the TypeEvaluationKind of QualType T.
llvm::Constant * EmitAnnotationUnit(SourceLocation Loc)
Emit the annotation&#39;s translation unit.
llvm::BasicBlock * EHResumeBlock
EHResumeBlock - Unified block containing a call to llvm.eh.resume.
This represents one expression.
Definition: Expr.h:108
Emit only debug info necessary for generating line number tables (-gline-tables-only).
bool isDefaulted() const
Whether this function is defaulted per C++0x.
Definition: Decl.h:2033
static Address invalid()
Definition: Address.h:34
bool isObjCRetainableType() const
Definition: Type.cpp:3978
static void TryMarkNoThrow(llvm::Function *F)
Tries to mark the given function nounwind based on the non-existence of any throwing calls within it...
VlaSizePair getVLASize(const VariableArrayType *vla)
Returns an LLVM value that corresponds to the size, in non-variably-sized elements, of a variable length array type, plus that largest non-variably-sized element type.
const char * getRequiredFeatures(unsigned ID) const
Definition: Builtins.h:210
CharUnits getTypeAlignInChars(QualType T) const
Return the ABI-specified alignment of a (complete) type T, in characters.
static CharUnits fromQuantity(QuantityType Quantity)
fromQuantity - Construct a CharUnits quantity from a raw integer type.
Definition: CharUnits.h:62
const AstTypeMatcher< ArrayType > arrayType
Matches all kinds of arrays.
llvm::LLVMContext & getLLVMContext()
llvm::BasicBlock * GetIndirectGotoBlock()
bool isSignedIntegerType() const
Return true if this is an integer type that is signed, according to C99 6.2.5p4 [char, signed char, short, int, long..], or an enum decl which has a signed representation.
Definition: Type.cpp:1844
static unsigned ArgInfoAddressSpace(LangAS AS)
QualType getType() const
Definition: Expr.h:130
void EmitConstructorBody(FunctionArgList &Args)
EmitConstructorBody - Emits the body of the current constructor.
Definition: CGClass.cpp:819
void EmitTypeCheck(TypeCheckKind TCK, SourceLocation Loc, llvm::Value *V, QualType Type, CharUnits Alignment=CharUnits::Zero(), SanitizerSet SkippedChecks=SanitizerSet(), llvm::Value *ArraySize=nullptr)
Emit a check that V is the address of storage of the appropriate size and alignment for an object of ...
Definition: CGExpr.cpp:652
CharUnits alignmentOfArrayElement(CharUnits elementSize) const
Given that this is the alignment of the first element of an array, return the minimum alignment of an...
Definition: CharUnits.h:196
llvm::Constant * EmitAnnotationString(StringRef Str)
Emit an annotation string.
LValue MakeNaturalAlignAddrLValue(llvm::Value *V, QualType T)
llvm::CallInst * CreateMemSet(Address Dest, llvm::Value *Value, llvm::Value *Size, bool IsVolatile=false)
Definition: CGBuilder.h:296
SourceLocation getEnd() const
QualType getFunctionTypeWithExceptionSpec(QualType Orig, const FunctionProtoType::ExceptionSpecInfo &ESI)
Get a function type and produce the equivalent function type with the specified exception specificati...
An object which temporarily prevents a value from being destroyed by aggressive peephole optimization...
UnaryOperator - This represents the unary-expression&#39;s (except sizeof and alignof), the postinc/postdec operators from postfix-expression, and various extensions.
Definition: Expr.h:1929
QualType getFunctionType(QualType ResultTy, ArrayRef< QualType > Args, const FunctionProtoType::ExtProtoInfo &EPI) const
Return a normal function type with a typed argument list.
Definition: ASTContext.h:1379
ValueDecl * getDecl()
Definition: Expr.h:1147
const LangOptions & getLangOpts() const
ASTContext & getContext() const
virtual void startNewFunction()
Definition: Mangle.h:75
CallingConv
CallingConv - Specifies the calling convention that a function uses.
Definition: Specifiers.h:235
GlobalDecl - represents a global declaration.
Definition: GlobalDecl.h:34
bool isConstQualified() const
Determine whether this type is const-qualified.
Definition: Type.h:6136
The l-value was considered opaque, so the alignment was determined from a type.
void PopCleanupBlocks(EHScopeStack::stable_iterator OldCleanupStackSize, std::initializer_list< llvm::Value **> ValuesToReload={})
Takes the old cleanup stack size and emits the cleanup blocks that have been added.
Definition: CGCleanup.cpp:417
void set(SanitizerMask K, bool Value)
Enable or disable a certain (single) sanitizer.
Definition: Sanitizers.h:60
bool SawAsmBlock
Whether we processed a Microsoft-style asm block during CodeGen.
Address CreateBitCast(Address Addr, llvm::Type *Ty, const llvm::Twine &Name="")
Definition: CGBuilder.h:141
QualType getCanonicalType() const
Definition: Type.h:6116
Encodes a location in the source.
void EnsureInsertPoint()
EnsureInsertPoint - Ensure that an insertion point is defined so that emitted IR has a place to go...
LangAS getAddressSpace() const
Return the address space of this type.
Definition: Type.h:6193
QualType getSingleStepDesugaredType(const ASTContext &Context) const
Return the specified type with one level of "sugar" removed from the type.
Definition: Type.h:947
llvm::Value * EvaluateExprAsBool(const Expr *E)
EvaluateExprAsBool - Perform the usual unary conversions on the specified expression and compare the ...
Definition: CGExpr.cpp:163
bool isVariablyModifiedType() const
Whether this type is a variably-modified type (C99 6.7.5).
Definition: Type.h:2097
bool inheritingCtorHasParams(const InheritedConstructor &Inherited, CXXCtorType Type)
Determine if a C++ inheriting constructor should have parameters matching those of its inherited cons...
Definition: CGCall.cpp:295
CharUnits getNaturalPointeeTypeAlignment(QualType T, LValueBaseInfo *BaseInfo=nullptr, TBAAAccessInfo *TBAAInfo=nullptr)
QualType getElementType() const
Definition: Type.h:3208
const Decl * getDecl() const
Definition: GlobalDecl.h:68
Represents the declaration of a label.
Definition: Decl.h:468
ParsedAttr - Represents a syntactic attribute.
Definition: ParsedAttr.h:116
A scoped helper to set the current debug location to the specified location or preferred location of ...
Definition: CGDebugInfo.h:686
void InsertHelper(llvm::Instruction *I, const llvm::Twine &Name, llvm::BasicBlock *BB, llvm::BasicBlock::iterator InsertPt) const
This forwards to CodeGenFunction::InsertHelper.
QualType getBaseElementType(const ArrayType *VAT) const
Return the innermost element type of an array type.
TargetAttr::ParsedTargetAttr filterFunctionTargetAttrs(const TargetAttr *TD)
Parses the target attributes passed in, and returns only the ones that are valid feature names...
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:2048
void EmitStmt(const Stmt *S, ArrayRef< const Attr *> Attrs=None)
EmitStmt - Emit the code for the statement.
Definition: CGStmt.cpp:44
const ParmVarDecl * getParamDecl(unsigned i) const
Definition: Decl.h:2284
SanitizerSet SanOpts
Sanitizers enabled for this function.
constexpr XRayInstrMask Custom
Definition: XRayInstr.h:39
llvm::Constant * GetAddrOfRTTIDescriptor(QualType Ty, bool ForEH=false)
Get the address of the RTTI descriptor for the given type.
const ArrayType * getAsArrayType(QualType T) const
Type Query functions.
An aligned address.
Definition: Address.h:24
void StartFunction(GlobalDecl GD, QualType RetTy, llvm::Function *Fn, const CGFunctionInfo &FnInfo, const FunctionArgList &Args, SourceLocation Loc=SourceLocation(), SourceLocation StartLoc=SourceLocation())
Emit code for the start of a function.
XRayInstrSet XRayInstrumentationBundle
Set of XRay instrumentation kinds to emit.
TypeClass getTypeClass() const
Definition: Type.h:1813
MangleContext & getMangleContext()
Gets the mangle context.
Definition: CGCXXABI.h:96
JumpDest getJumpDestInCurrentScope(llvm::BasicBlock *Target)
The given basic block lies in the current EH scope, but may be a target of a potentially scope-crossi...
llvm::Constant * EmitAnnotationLineNo(SourceLocation L)
Emit the annotation line number.
InAlloca - Pass the argument directly using the LLVM inalloca attribute.
CharUnits getNaturalTypeAlignment(QualType T, LValueBaseInfo *BaseInfo=nullptr, TBAAAccessInfo *TBAAInfo=nullptr, bool forPointeeType=false)
bool hasImplicitReturnZero() const
Whether falling off this function implicitly returns null/zero.
Definition: Decl.h:2051
Address EmitMSVAListRef(const Expr *E)
Emit a "reference" to a __builtin_ms_va_list; this is always the value of the expression, because a __builtin_ms_va_list is a pointer to a char.
void getFunctionFeatureMap(llvm::StringMap< bool > &FeatureMap, GlobalDecl GD)
const CGFunctionInfo * CurFnInfo
void EmitStartEHSpec(const Decl *D)
EmitStartEHSpec - Emit the start of the exception spec.
bool isCanonical() const
Definition: Type.h:6121
void FinishFunction(SourceLocation EndLoc=SourceLocation())
FinishFunction - Complete IR generation of the current function.
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Definition: Expr.cpp:214
This is an IRBuilder insertion helper that forwards to CodeGenFunction::InsertHelper, which adds necessary metadata to instructions.
Definition: CGBuilder.h:25
Address EmitVAListRef(const Expr *E)
llvm::Value * EmitScalarExpr(const Expr *E, bool IgnoreResultAssign=false)
EmitScalarExpr - Emit the computation of the specified expression of LLVM scalar type, returning the result.
FunctionArgList - Type for representing both the decl and type of parameters to a function...
Definition: CGCall.h:358
llvm::Value * getScalarVal() const
getScalarVal() - Return the Value* of this scalar value.
Definition: CGValue.h:58
void ErrorUnsupported(const Stmt *S, const char *Type)
Print out an error that codegen doesn&#39;t support the specified stmt yet.
bool isPipeType() const
Definition: Type.h:6486
CGFunctionInfo - Class to encapsulate the information about a function definition.
This class organizes the cross-function state that is used while generating LLVM code.
CGOpenMPRuntime & getOpenMPRuntime()
Return a reference to the configured OpenMP runtime.
void EmitDeclRefExprDbgValue(const DeclRefExpr *E, const APValue &Init)
static void emitNonZeroVLAInit(CodeGenFunction &CGF, QualType baseType, Address dest, Address src, llvm::Value *sizeInChars)
emitNonZeroVLAInit - Emit the "zero" initialization of a variable-length array whose elements have a ...
llvm::Value * EmitAnnotationCall(llvm::Function *AnnotationFn, llvm::Value *AnnotatedVal, StringRef AnnotationStr, SourceLocation Location)
Emit an annotation call (intrinsic).
Dataflow Directional Tag Classes.
Address EmitFieldAnnotations(const FieldDecl *D, Address V)
Emit field annotations for the given field & value.
EvalResult is a struct with detailed info about an evaluated expression.
Definition: Expr.h:573
static bool matchesStlAllocatorFn(const Decl *D, const ASTContext &Ctx)
void Init(const Stmt *Body)
Clear the object and pre-process for the given statement, usually function body statement.
llvm::LoadInst * CreateAlignedLoad(llvm::Value *Addr, CharUnits Align, const llvm::Twine &Name="")
Definition: CGBuilder.h:90
llvm::LoadInst * CreateLoad(Address Addr, const llvm::Twine &Name="")
Definition: CGBuilder.h:69
FunctionDecl * getTemplateInstantiationPattern() const
Retrieve the function declaration from which this function could be instantiated, if it is an instant...
Definition: Decl.cpp:3413
void EmitFunctionBody(const Stmt *Body)
static std::string getAsString(SplitQualType split, const PrintingPolicy &Policy)
Definition: Type.h:967
const CXXRecordDecl * getParent() const
Returns the parent of this method declaration, which is the class in which this method is defined...
Definition: DeclCXX.h:2173
Decl * getNonClosureContext()
Find the innermost non-closure ancestor of this declaration, walking up through blocks, lambdas, etc.
Definition: DeclBase.cpp:1001
bool isMSVCRTEntryPoint() const
Determines whether this function is a MSVCRT user defined entry point.
Definition: Decl.cpp:2767
void buildThisParam(CodeGenFunction &CGF, FunctionArgList &Params)
Build a parameter variable suitable for &#39;this&#39;.
Definition: CGCXXABI.cpp:127
llvm::Constant * EmitNullConstant(QualType T)
Return the result of value-initializing the given type, i.e.
llvm::Function * getIntrinsic(unsigned IID, ArrayRef< llvm::Type *> Tys=None)
llvm::SmallVector< char, 256 > LifetimeExtendedCleanupStack
bool has(XRayInstrMask K) const
Definition: XRayInstr.h:46
llvm::Module & getModule() const
LValue MakeAddrLValue(Address Addr, QualType T, AlignmentSource Source=AlignmentSource::Type)
bool hasProfileClangInstr() const
Check if Clang profile instrumenation is on.
static void GenOpenCLArgMetadata(const FunctionDecl *FD, llvm::Function *Fn, CodeGenModule &CGM, llvm::LLVMContext &Context, CGBuilderTy &Builder, ASTContext &ASTCtx)
JumpDest ReturnBlock
ReturnBlock - Unified return block.
uint64_t SanitizerMask
Definition: Sanitizers.h:25
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
Definition: Type.h:4375
void EmitBlockWithFallThrough(llvm::BasicBlock *BB, const Stmt *S)
When instrumenting to collect profile data, the counts for some blocks such as switch cases need to n...
llvm::DebugLoc SourceLocToDebugLoc(SourceLocation Location)
Converts Location to a DebugLoc, if debug information is enabled.
CodeGenTypes & getTypes() const
CharUnits getIndirectAlign() const
T * getAttr() const
Definition: DeclBase.h:529
llvm::Type * getElementType() const
Return the type of the values stored in this address.
Definition: Address.h:51
bool isImageType() const
Definition: Type.h:6479
llvm::AssertingVH< llvm::Instruction > AllocaInsertPt
AllocaInsertPoint - This is an instruction in the entry block before which we prefer to insert alloca...
ExtVectorType - Extended vector type.
Definition: Type.h:3292
QualType BuildFunctionArgList(GlobalDecl GD, FunctionArgList &Args)
CodeGenOptions - Track various options which control how the code is optimized and passed to the back...
void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false)
EmitBlock - Emit the given block.
Definition: CGStmt.cpp:450
void checkTargetFeatures(const CallExpr *E, const FunctionDecl *TargetDecl)
Optional< NullabilityKind > getNullability(const ASTContext &context) const
Determine the nullability of the given type.
Definition: Type.cpp:3753
APValue - This class implements a discriminated union of [uninitialized] [APSInt] [APFloat]...
Definition: APValue.h:38
void getCaptureFields(llvm::DenseMap< const VarDecl *, FieldDecl *> &Captures, FieldDecl *&ThisCapture) const
For a closure type, retrieve the mapping from captured variables and this to the non-static data memb...
Definition: DeclCXX.cpp:1404
bool isIncompleteType(NamedDecl **Def=nullptr) const
Types are partitioned into 3 broad categories (C99 6.2.5p1): object types, function types...
Definition: Type.cpp:2031
llvm::ConstantInt * getSize(CharUnits numChars)
Emit the given number of characters as a value of type size_t.
const Decl * CurFuncDecl
CurFuncDecl - Holds the Decl for the current outermost non-closure context.
llvm::iterator_range< specific_attr_iterator< T > > specific_attrs() const
Definition: DeclBase.h:515
CharUnits toCharUnitsFromBits(int64_t BitSize) const
Convert a size in bits to a size in characters.
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate.h) and friends (in DeclFriend.h).
void unprotectFromPeepholes(PeepholeProtection protection)
QualType getUnqualifiedType() const
Retrieve the unqualified variant of the given type, removing as little sugar as possible.
Definition: Type.h:6157
void ErrorUnsupported(const Stmt *S, const char *Type)
ErrorUnsupported - Print out an error that codegen doesn&#39;t support the specified stmt yet...
bool hasUnaligned() const
Definition: Type.h:293
Represents a C++ struct/union/class.
Definition: DeclCXX.h:299
void EmitBranch(llvm::BasicBlock *Block)
EmitBranch - Emit a branch to the specified basic block from the current insert block, taking care to avoid creation of branches from dummy blocks.
Definition: CGStmt.cpp:470
bool isVoidType() const
Definition: Type.h:6553
unsigned getBuiltinID() const
Returns a value indicating whether this function corresponds to a builtin function.
Definition: Decl.cpp:2993
llvm::Type * ConvertType(QualType T)
void EmitFunctionProlog(const CGFunctionInfo &FI, llvm::Function *Fn, const FunctionArgList &Args)
EmitFunctionProlog - Emit the target specific LLVM code to load the arguments for the given function...
Definition: CGCall.cpp:2235
Qualifiers getQualifiers() const
Retrieve the set of qualifiers applied to this type.
Definition: Type.h:6104
static llvm::Constant * getPrologueSignature(CodeGenModule &CGM, const FunctionDecl *FD)
Return the UBSan prologue signature for FD if one is available.
LambdaCaptureDefault getLambdaCaptureDefault() const
Definition: DeclCXX.h:1224
LValue EmitLValue(const Expr *E)
EmitLValue - Emit code to compute a designator that specifies the location of the expression...
Definition: CGExpr.cpp:1243
Address ReturnValue
ReturnValue - The temporary alloca to hold the return value.
Builtin::Context & BuiltinInfo
Definition: ASTContext.h:567
bool AutoreleaseResult
In ARC, whether we should autorelease the return value.
RValue EmitLoadOfLValue(LValue V, SourceLocation Loc)
EmitLoadOfLValue - Given an expression that represents a value lvalue, this method emits the address ...
Definition: CGExpr.cpp:1765
bool has(SanitizerMask K) const
Check if a certain (single) sanitizer is enabled.
Definition: Sanitizers.h:51
Defines the clang::TargetInfo interface.
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition: Expr.h:2429
void EmitMultiVersionResolver(llvm::Function *Resolver, ArrayRef< MultiVersionResolverOption > Options)
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
Definition: Decl.h:275
CGCXXABI & getCXXABI() const
const VariableArrayType * getAsVariableArrayType(QualType T) const
Definition: ASTContext.h:2429
__DEVICE__ int max(int __a, int __b)
A reference to a declared variable, function, enum, etc.
Definition: Expr.h:1074
static bool shouldUseUndefinedBehaviorReturnOptimization(const FunctionDecl *FD, const ASTContext &Context)
void EmitDestructorBody(FunctionArgList &Args)
EmitDestructorBody - Emits the body of the current destructor.
Definition: CGClass.cpp:1405
bool isPointerType() const
Definition: Type.h:6301
This structure provides a set of types that are commonly used during IR emission. ...
void EmitEndEHSpec(const Decl *D)
EmitEndEHSpec - Emit the end of the exception spec.
struct clang::CodeGen::CodeGenFunction::MultiVersionResolverOption::Conds Conditions
CharUnits getTypeSizeInChars(QualType T) const
Return the size of the specified (complete) type T, in characters.
llvm::Constant * EmitCheckSourceLocation(SourceLocation Loc)
Emit a description of a source location in a format suitable for passing to a runtime sanitizer handl...
Definition: CGExpr.cpp:2795
Address EmitCompoundStmtWithoutScope(const CompoundStmt &S, bool GetLast=false, AggValueSlot AVS=AggValueSlot::ignored())
Definition: CGStmt.cpp:380
void EmitAlignmentAssumption(llvm::Value *PtrValue, QualType Ty, SourceLocation Loc, SourceLocation AssumptionLoc, llvm::Value *Alignment, llvm::Value *OffsetValue=nullptr)
QualType getType() const
Definition: Decl.h:647
A trivial tuple used to represent a source range.
LValue - This represents an lvalue references.
Definition: CGValue.h:166
llvm::BlockAddress * GetAddrOfLabel(const LabelDecl *L)
SourceLocation getBeginLoc() const LLVM_READONLY
Definition: Expr.cpp:1427
void EmitVarAnnotations(const VarDecl *D, llvm::Value *V)
Emit local annotations for the local variable V, declared by D.
Represents a C array with a specified size that is not an integer-constant-expression.
Definition: Type.h:2976
SanitizerMetadata * getSanitizerMetadata()
bool CurFuncIsThunk
In C++, whether we are code generating a thunk.
APSInt & getInt()
Definition: APValue.h:257
const LangOptions & getLangOpts() const
llvm::Value * emitArrayLength(const ArrayType *arrayType, QualType &baseType, Address &addr)
emitArrayLength - Compute the length of an array, even if it&#39;s a VLA, and drill down to the base elem...
static LValue MakeAddr(Address address, QualType type, ASTContext &Context, LValueBaseInfo BaseInfo, TBAAAccessInfo TBAAInfo)
Definition: CGValue.h:366
unsigned getNumParams() const
Return the number of parameters this function must have based on its FunctionType.
Definition: Decl.cpp:3054
static bool containsBreak(const Stmt *S)
containsBreak - Return true if the statement contains a break out of it.
SourceLocation getBegin() const
bool isZeroInitializable(QualType T)
IsZeroInitializable - Return whether a type can be zero-initialized (in the C++ sense) with an LLVM z...
Defines enum values for all the target-independent builtin functions.
void EmitCXXThrowExpr(const CXXThrowExpr *E, bool KeepInsertionPoint=true)
bool isScalar() const
Definition: CGValue.h:51
Attr - This represents one attribute.
Definition: Attr.h:43
SourceLocation getLocation() const
Definition: DeclBase.h:420
void EmitNullInitialization(Address DestPtr, QualType Ty)
EmitNullInitialization - Generate code to set a value of the given type to null, If the type contains...
Expr * IgnoreParens() LLVM_READONLY
Skip past any parentheses which might surround this expression until reaching a fixed point...
Definition: Expr.cpp:2593
CanQualType getSizeType() const
Return the unique type for "size_t" (C99 7.17), defined in <stddef.h>.