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