clang  9.0.0svn
CGCall.cpp
Go to the documentation of this file.
1 //===--- CGCall.cpp - Encapsulate calling convention details --------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // These classes wrap the information about a call or function
10 // definition used to handle ABI compliancy.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "CGCall.h"
15 #include "ABIInfo.h"
16 #include "CGBlocks.h"
17 #include "CGCXXABI.h"
18 #include "CGCleanup.h"
19 #include "CodeGenFunction.h"
20 #include "CodeGenModule.h"
21 #include "TargetInfo.h"
22 #include "clang/AST/Decl.h"
23 #include "clang/AST/DeclCXX.h"
24 #include "clang/AST/DeclObjC.h"
27 #include "clang/Basic/TargetInfo.h"
30 #include "llvm/ADT/StringExtras.h"
31 #include "llvm/Transforms/Utils/Local.h"
32 #include "llvm/Analysis/ValueTracking.h"
33 #include "llvm/IR/Attributes.h"
34 #include "llvm/IR/CallingConv.h"
35 #include "llvm/IR/DataLayout.h"
36 #include "llvm/IR/InlineAsm.h"
37 #include "llvm/IR/IntrinsicInst.h"
38 #include "llvm/IR/Intrinsics.h"
39 using namespace clang;
40 using namespace CodeGen;
41 
42 /***/
43 
45  switch (CC) {
46  default: return llvm::CallingConv::C;
47  case CC_X86StdCall: return llvm::CallingConv::X86_StdCall;
48  case CC_X86FastCall: return llvm::CallingConv::X86_FastCall;
49  case CC_X86RegCall: return llvm::CallingConv::X86_RegCall;
50  case CC_X86ThisCall: return llvm::CallingConv::X86_ThisCall;
51  case CC_Win64: return llvm::CallingConv::Win64;
52  case CC_X86_64SysV: return llvm::CallingConv::X86_64_SysV;
53  case CC_AAPCS: return llvm::CallingConv::ARM_AAPCS;
54  case CC_AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
55  case CC_IntelOclBicc: return llvm::CallingConv::Intel_OCL_BI;
56  // TODO: Add support for __pascal to LLVM.
58  // TODO: Add support for __vectorcall to LLVM.
59  case CC_X86VectorCall: return llvm::CallingConv::X86_VectorCall;
60  case CC_AArch64VectorCall: return llvm::CallingConv::AArch64_VectorCall;
61  case CC_SpirFunction: return llvm::CallingConv::SPIR_FUNC;
63  case CC_PreserveMost: return llvm::CallingConv::PreserveMost;
64  case CC_PreserveAll: return llvm::CallingConv::PreserveAll;
65  case CC_Swift: return llvm::CallingConv::Swift;
66  }
67 }
68 
69 /// Derives the 'this' type for codegen purposes, i.e. ignoring method CVR
70 /// qualification. Either or both of RD and MD may be null. A null RD indicates
71 /// that there is no meaningful 'this' type, and a null MD can occur when
72 /// calling a method pointer.
74  const CXXMethodDecl *MD) {
75  QualType RecTy;
76  if (RD)
77  RecTy = Context.getTagDeclType(RD)->getCanonicalTypeInternal();
78  else
79  RecTy = Context.VoidTy;
80 
81  if (MD)
82  RecTy = Context.getAddrSpaceQualType(RecTy, MD->getMethodQualifiers().getAddressSpace());
83  return Context.getPointerType(CanQualType::CreateUnsafe(RecTy));
84 }
85 
86 /// Returns the canonical formal type of the given C++ method.
88  return MD->getType()->getCanonicalTypeUnqualified()
90 }
91 
92 /// Returns the "extra-canonicalized" return type, which discards
93 /// qualifiers on the return type. Codegen doesn't care about them,
94 /// and it makes ABI code a little easier to be able to assume that
95 /// all parameter and return types are top-level unqualified.
98 }
99 
100 /// Arrange the argument and result information for a value of the given
101 /// unprototyped freestanding function type.
102 const CGFunctionInfo &
104  // When translating an unprototyped function type, always use a
105  // variadic type.
106  return arrangeLLVMFunctionInfo(FTNP->getReturnType().getUnqualifiedType(),
107  /*instanceMethod=*/false,
108  /*chainCall=*/false, None,
109  FTNP->getExtInfo(), {}, RequiredArgs(0));
110 }
111 
114  const FunctionProtoType *proto,
115  unsigned prefixArgs,
116  unsigned totalArgs) {
117  assert(proto->hasExtParameterInfos());
118  assert(paramInfos.size() <= prefixArgs);
119  assert(proto->getNumParams() + prefixArgs <= totalArgs);
120 
121  paramInfos.reserve(totalArgs);
122 
123  // Add default infos for any prefix args that don't already have infos.
124  paramInfos.resize(prefixArgs);
125 
126  // Add infos for the prototype.
127  for (const auto &ParamInfo : proto->getExtParameterInfos()) {
128  paramInfos.push_back(ParamInfo);
129  // pass_object_size params have no parameter info.
130  if (ParamInfo.hasPassObjectSize())
131  paramInfos.emplace_back();
132  }
133 
134  assert(paramInfos.size() <= totalArgs &&
135  "Did we forget to insert pass_object_size args?");
136  // Add default infos for the variadic and/or suffix arguments.
137  paramInfos.resize(totalArgs);
138 }
139 
140 /// Adds the formal parameters in FPT to the given prefix. If any parameter in
141 /// FPT has pass_object_size attrs, then we'll add parameters for those, too.
142 static void appendParameterTypes(const CodeGenTypes &CGT,
146  // Fast path: don't touch param info if we don't need to.
147  if (!FPT->hasExtParameterInfos()) {
148  assert(paramInfos.empty() &&
149  "We have paramInfos, but the prototype doesn't?");
150  prefix.append(FPT->param_type_begin(), FPT->param_type_end());
151  return;
152  }
153 
154  unsigned PrefixSize = prefix.size();
155  // In the vast majority of cases, we'll have precisely FPT->getNumParams()
156  // parameters; the only thing that can change this is the presence of
157  // pass_object_size. So, we preallocate for the common case.
158  prefix.reserve(prefix.size() + FPT->getNumParams());
159 
160  auto ExtInfos = FPT->getExtParameterInfos();
161  assert(ExtInfos.size() == FPT->getNumParams());
162  for (unsigned I = 0, E = FPT->getNumParams(); I != E; ++I) {
163  prefix.push_back(FPT->getParamType(I));
164  if (ExtInfos[I].hasPassObjectSize())
165  prefix.push_back(CGT.getContext().getSizeType());
166  }
167 
168  addExtParameterInfosForCall(paramInfos, FPT.getTypePtr(), PrefixSize,
169  prefix.size());
170 }
171 
172 /// Arrange the LLVM function layout for a value of the given function
173 /// type, on top of any implicit parameters already stored.
174 static const CGFunctionInfo &
175 arrangeLLVMFunctionInfo(CodeGenTypes &CGT, bool instanceMethod,
179  RequiredArgs Required = RequiredArgs::forPrototypePlus(FTP, prefix.size());
180  // FIXME: Kill copy.
181  appendParameterTypes(CGT, prefix, paramInfos, FTP);
182  CanQualType resultType = FTP->getReturnType().getUnqualifiedType();
183 
184  return CGT.arrangeLLVMFunctionInfo(resultType, instanceMethod,
185  /*chainCall=*/false, prefix,
186  FTP->getExtInfo(), paramInfos,
187  Required);
188 }
189 
190 /// Arrange the argument and result information for a value of the
191 /// given freestanding function type.
192 const CGFunctionInfo &
195  return ::arrangeLLVMFunctionInfo(*this, /*instanceMethod=*/false, argTypes,
196  FTP);
197 }
198 
199 static CallingConv getCallingConventionForDecl(const Decl *D, bool IsWindows) {
200  // Set the appropriate calling convention for the Function.
201  if (D->hasAttr<StdCallAttr>())
202  return CC_X86StdCall;
203 
204  if (D->hasAttr<FastCallAttr>())
205  return CC_X86FastCall;
206 
207  if (D->hasAttr<RegCallAttr>())
208  return CC_X86RegCall;
209 
210  if (D->hasAttr<ThisCallAttr>())
211  return CC_X86ThisCall;
212 
213  if (D->hasAttr<VectorCallAttr>())
214  return CC_X86VectorCall;
215 
216  if (D->hasAttr<PascalAttr>())
217  return CC_X86Pascal;
218 
219  if (PcsAttr *PCS = D->getAttr<PcsAttr>())
220  return (PCS->getPCS() == PcsAttr::AAPCS ? CC_AAPCS : CC_AAPCS_VFP);
221 
222  if (D->hasAttr<AArch64VectorPcsAttr>())
223  return CC_AArch64VectorCall;
224 
225  if (D->hasAttr<IntelOclBiccAttr>())
226  return CC_IntelOclBicc;
227 
228  if (D->hasAttr<MSABIAttr>())
229  return IsWindows ? CC_C : CC_Win64;
230 
231  if (D->hasAttr<SysVABIAttr>())
232  return IsWindows ? CC_X86_64SysV : CC_C;
233 
234  if (D->hasAttr<PreserveMostAttr>())
235  return CC_PreserveMost;
236 
237  if (D->hasAttr<PreserveAllAttr>())
238  return CC_PreserveAll;
239 
240  return CC_C;
241 }
242 
243 /// Arrange the argument and result information for a call to an
244 /// unknown C++ non-static member function of the given abstract type.
245 /// (A null RD means we don't have any meaningful "this" argument type,
246 /// so fall back to a generic pointer type).
247 /// The member function must be an ordinary function, i.e. not a
248 /// constructor or destructor.
249 const CGFunctionInfo &
251  const FunctionProtoType *FTP,
252  const CXXMethodDecl *MD) {
254 
255  // Add the 'this' pointer.
256  argTypes.push_back(DeriveThisType(RD, MD));
257 
259  *this, true, argTypes,
261 }
262 
263 /// Set calling convention for CUDA/HIP kernel.
265  const FunctionDecl *FD) {
266  if (FD->hasAttr<CUDAGlobalAttr>()) {
267  const FunctionType *FT = FTy->getAs<FunctionType>();
269  FTy = FT->getCanonicalTypeUnqualified();
270  }
271 }
272 
273 /// Arrange the argument and result information for a declaration or
274 /// definition of the given C++ non-static member function. The
275 /// member function must be an ordinary function, i.e. not a
276 /// constructor or destructor.
277 const CGFunctionInfo &
279  assert(!isa<CXXConstructorDecl>(MD) && "wrong method for constructors!");
280  assert(!isa<CXXDestructorDecl>(MD) && "wrong method for destructors!");
281 
282  CanQualType FT = GetFormalType(MD).getAs<Type>();
283  setCUDAKernelCallingConvention(FT, CGM, MD);
284  auto prototype = FT.getAs<FunctionProtoType>();
285 
286  if (MD->isInstance()) {
287  // The abstract case is perfectly fine.
288  const CXXRecordDecl *ThisType = TheCXXABI.getThisArgumentTypeForMethod(MD);
289  return arrangeCXXMethodType(ThisType, prototype.getTypePtr(), MD);
290  }
291 
292  return arrangeFreeFunctionType(prototype);
293 }
294 
296  const InheritedConstructor &Inherited, CXXCtorType Type) {
297  // Parameters are unnecessary if we're constructing a base class subobject
298  // and the inherited constructor lives in a virtual base.
299  return Type == Ctor_Complete ||
300  !Inherited.getShadowDecl()->constructsVirtualBase() ||
301  !Target.getCXXABI().hasConstructorVariants();
302 }
303 
304 const CGFunctionInfo &
306  auto *MD = cast<CXXMethodDecl>(GD.getDecl());
307 
310  argTypes.push_back(DeriveThisType(MD->getParent(), MD));
311 
312  bool PassParams = true;
313 
314  if (auto *CD = dyn_cast<CXXConstructorDecl>(MD)) {
315  // A base class inheriting constructor doesn't get forwarded arguments
316  // needed to construct a virtual base (or base class thereof).
317  if (auto Inherited = CD->getInheritedConstructor())
318  PassParams = inheritingCtorHasParams(Inherited, GD.getCtorType());
319  }
320 
322 
323  // Add the formal parameters.
324  if (PassParams)
325  appendParameterTypes(*this, argTypes, paramInfos, FTP);
326 
327  CGCXXABI::AddedStructorArgs AddedArgs =
328  TheCXXABI.buildStructorSignature(GD, argTypes);
329  if (!paramInfos.empty()) {
330  // Note: prefix implies after the first param.
331  if (AddedArgs.Prefix)
332  paramInfos.insert(paramInfos.begin() + 1, AddedArgs.Prefix,
334  if (AddedArgs.Suffix)
335  paramInfos.append(AddedArgs.Suffix,
337  }
338 
339  RequiredArgs required =
340  (PassParams && MD->isVariadic() ? RequiredArgs(argTypes.size())
342 
343  FunctionType::ExtInfo extInfo = FTP->getExtInfo();
344  CanQualType resultType = TheCXXABI.HasThisReturn(GD)
345  ? argTypes.front()
346  : TheCXXABI.hasMostDerivedReturn(GD)
347  ? CGM.getContext().VoidPtrTy
348  : Context.VoidTy;
349  return arrangeLLVMFunctionInfo(resultType, /*instanceMethod=*/true,
350  /*chainCall=*/false, argTypes, extInfo,
351  paramInfos, required);
352 }
353 
357  for (auto &arg : args)
358  argTypes.push_back(ctx.getCanonicalParamType(arg.Ty));
359  return argTypes;
360 }
361 
365  for (auto &arg : args)
366  argTypes.push_back(ctx.getCanonicalParamType(arg->getType()));
367  return argTypes;
368 }
369 
372  unsigned prefixArgs, unsigned totalArgs) {
374  if (proto->hasExtParameterInfos()) {
375  addExtParameterInfosForCall(result, proto, prefixArgs, totalArgs);
376  }
377  return result;
378 }
379 
380 /// Arrange a call to a C++ method, passing the given arguments.
381 ///
382 /// ExtraPrefixArgs is the number of ABI-specific args passed after the `this`
383 /// parameter.
384 /// ExtraSuffixArgs is the number of ABI-specific args passed at the end of
385 /// args.
386 /// PassProtoArgs indicates whether `args` has args for the parameters in the
387 /// given CXXConstructorDecl.
388 const CGFunctionInfo &
390  const CXXConstructorDecl *D,
391  CXXCtorType CtorKind,
392  unsigned ExtraPrefixArgs,
393  unsigned ExtraSuffixArgs,
394  bool PassProtoArgs) {
395  // FIXME: Kill copy.
397  for (const auto &Arg : args)
398  ArgTypes.push_back(Context.getCanonicalParamType(Arg.Ty));
399 
400  // +1 for implicit this, which should always be args[0].
401  unsigned TotalPrefixArgs = 1 + ExtraPrefixArgs;
402 
404  RequiredArgs Required = PassProtoArgs
406  FPT, TotalPrefixArgs + ExtraSuffixArgs)
408 
409  GlobalDecl GD(D, CtorKind);
410  CanQualType ResultType = TheCXXABI.HasThisReturn(GD)
411  ? ArgTypes.front()
412  : TheCXXABI.hasMostDerivedReturn(GD)
413  ? CGM.getContext().VoidPtrTy
414  : Context.VoidTy;
415 
416  FunctionType::ExtInfo Info = FPT->getExtInfo();
418  // If the prototype args are elided, we should only have ABI-specific args,
419  // which never have param info.
420  if (PassProtoArgs && FPT->hasExtParameterInfos()) {
421  // ABI-specific suffix arguments are treated the same as variadic arguments.
422  addExtParameterInfosForCall(ParamInfos, FPT.getTypePtr(), TotalPrefixArgs,
423  ArgTypes.size());
424  }
425  return arrangeLLVMFunctionInfo(ResultType, /*instanceMethod=*/true,
426  /*chainCall=*/false, ArgTypes, Info,
427  ParamInfos, Required);
428 }
429 
430 /// Arrange the argument and result information for the declaration or
431 /// definition of the given function.
432 const CGFunctionInfo &
434  if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
435  if (MD->isInstance())
436  return arrangeCXXMethodDeclaration(MD);
437 
439 
440  assert(isa<FunctionType>(FTy));
441  setCUDAKernelCallingConvention(FTy, CGM, FD);
442 
443  // When declaring a function without a prototype, always use a
444  // non-variadic type.
447  noProto->getReturnType(), /*instanceMethod=*/false,
448  /*chainCall=*/false, None, noProto->getExtInfo(), {},RequiredArgs::All);
449  }
450 
452 }
453 
454 /// Arrange the argument and result information for the declaration or
455 /// definition of an Objective-C method.
456 const CGFunctionInfo &
458  // It happens that this is the same as a call with no optional
459  // arguments, except also using the formal 'self' type.
461 }
462 
463 /// Arrange the argument and result information for the function type
464 /// through which to perform a send to the given Objective-C method,
465 /// using the given receiver type. The receiver type is not always
466 /// the 'self' type of the method or even an Objective-C pointer type.
467 /// This is *not* the right method for actually performing such a
468 /// message send, due to the possibility of optional arguments.
469 const CGFunctionInfo &
471  QualType receiverType) {
474  argTys.push_back(Context.getCanonicalParamType(receiverType));
475  argTys.push_back(Context.getCanonicalParamType(Context.getObjCSelType()));
476  // FIXME: Kill copy?
477  for (const auto *I : MD->parameters()) {
478  argTys.push_back(Context.getCanonicalParamType(I->getType()));
480  I->hasAttr<NoEscapeAttr>());
481  extParamInfos.push_back(extParamInfo);
482  }
483 
484  FunctionType::ExtInfo einfo;
485  bool IsWindows = getContext().getTargetInfo().getTriple().isOSWindows();
486  einfo = einfo.withCallingConv(getCallingConventionForDecl(MD, IsWindows));
487 
488  if (getContext().getLangOpts().ObjCAutoRefCount &&
489  MD->hasAttr<NSReturnsRetainedAttr>())
490  einfo = einfo.withProducesResult(true);
491 
492  RequiredArgs required =
493  (MD->isVariadic() ? RequiredArgs(argTys.size()) : RequiredArgs::All);
494 
496  GetReturnType(MD->getReturnType()), /*instanceMethod=*/false,
497  /*chainCall=*/false, argTys, einfo, extParamInfos, required);
498 }
499 
500 const CGFunctionInfo &
502  const CallArgList &args) {
503  auto argTypes = getArgTypesForCall(Context, args);
504  FunctionType::ExtInfo einfo;
505 
507  GetReturnType(returnType), /*instanceMethod=*/false,
508  /*chainCall=*/false, argTypes, einfo, {}, RequiredArgs::All);
509 }
510 
511 const CGFunctionInfo &
513  // FIXME: Do we need to handle ObjCMethodDecl?
514  const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
515 
516  if (isa<CXXConstructorDecl>(GD.getDecl()) ||
517  isa<CXXDestructorDecl>(GD.getDecl()))
519 
520  return arrangeFunctionDeclaration(FD);
521 }
522 
523 /// Arrange a thunk that takes 'this' as the first parameter followed by
524 /// varargs. Return a void pointer, regardless of the actual return type.
525 /// The body of the thunk will end in a musttail call to a function of the
526 /// correct type, and the caller will bitcast the function to the correct
527 /// prototype.
528 const CGFunctionInfo &
530  assert(MD->isVirtual() && "only methods have thunks");
532  CanQualType ArgTys[] = {DeriveThisType(MD->getParent(), MD)};
533  return arrangeLLVMFunctionInfo(Context.VoidTy, /*instanceMethod=*/false,
534  /*chainCall=*/false, ArgTys,
535  FTP->getExtInfo(), {}, RequiredArgs(1));
536 }
537 
538 const CGFunctionInfo &
540  CXXCtorType CT) {
541  assert(CT == Ctor_CopyingClosure || CT == Ctor_DefaultClosure);
542 
545  const CXXRecordDecl *RD = CD->getParent();
546  ArgTys.push_back(DeriveThisType(RD, CD));
547  if (CT == Ctor_CopyingClosure)
548  ArgTys.push_back(*FTP->param_type_begin());
549  if (RD->getNumVBases() > 0)
550  ArgTys.push_back(Context.IntTy);
552  /*IsVariadic=*/false, /*IsCXXMethod=*/true);
553  return arrangeLLVMFunctionInfo(Context.VoidTy, /*instanceMethod=*/true,
554  /*chainCall=*/false, ArgTys,
555  FunctionType::ExtInfo(CC), {},
557 }
558 
559 /// Arrange a call as unto a free function, except possibly with an
560 /// additional number of formal parameters considered required.
561 static const CGFunctionInfo &
563  CodeGenModule &CGM,
564  const CallArgList &args,
565  const FunctionType *fnType,
566  unsigned numExtraRequiredArgs,
567  bool chainCall) {
568  assert(args.size() >= numExtraRequiredArgs);
569 
571 
572  // In most cases, there are no optional arguments.
573  RequiredArgs required = RequiredArgs::All;
574 
575  // If we have a variadic prototype, the required arguments are the
576  // extra prefix plus the arguments in the prototype.
577  if (const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(fnType)) {
578  if (proto->isVariadic())
579  required = RequiredArgs::forPrototypePlus(proto, numExtraRequiredArgs);
580 
581  if (proto->hasExtParameterInfos())
582  addExtParameterInfosForCall(paramInfos, proto, numExtraRequiredArgs,
583  args.size());
584 
585  // If we don't have a prototype at all, but we're supposed to
586  // explicitly use the variadic convention for unprototyped calls,
587  // treat all of the arguments as required but preserve the nominal
588  // possibility of variadics.
589  } else if (CGM.getTargetCodeGenInfo()
590  .isNoProtoCallVariadic(args,
591  cast<FunctionNoProtoType>(fnType))) {
592  required = RequiredArgs(args.size());
593  }
594 
595  // FIXME: Kill copy.
597  for (const auto &arg : args)
598  argTypes.push_back(CGT.getContext().getCanonicalParamType(arg.Ty));
600  /*instanceMethod=*/false, chainCall,
601  argTypes, fnType->getExtInfo(), paramInfos,
602  required);
603 }
604 
605 /// Figure out the rules for calling a function with the given formal
606 /// type using the given arguments. The arguments are necessary
607 /// because the function might be unprototyped, in which case it's
608 /// target-dependent in crazy ways.
609 const CGFunctionInfo &
611  const FunctionType *fnType,
612  bool chainCall) {
613  return arrangeFreeFunctionLikeCall(*this, CGM, args, fnType,
614  chainCall ? 1 : 0, chainCall);
615 }
616 
617 /// A block function is essentially a free function with an
618 /// extra implicit argument.
619 const CGFunctionInfo &
621  const FunctionType *fnType) {
622  return arrangeFreeFunctionLikeCall(*this, CGM, args, fnType, 1,
623  /*chainCall=*/false);
624 }
625 
626 const CGFunctionInfo &
628  const FunctionArgList &params) {
629  auto paramInfos = getExtParameterInfosForCall(proto, 1, params.size());
630  auto argTypes = getArgTypesForDeclaration(Context, params);
631 
633  /*instanceMethod*/ false, /*chainCall*/ false,
634  argTypes, proto->getExtInfo(), paramInfos,
636 }
637 
638 const CGFunctionInfo &
640  const CallArgList &args) {
641  // FIXME: Kill copy.
643  for (const auto &Arg : args)
644  argTypes.push_back(Context.getCanonicalParamType(Arg.Ty));
646  GetReturnType(resultType), /*instanceMethod=*/false,
647  /*chainCall=*/false, argTypes, FunctionType::ExtInfo(),
648  /*paramInfos=*/ {}, RequiredArgs::All);
649 }
650 
651 const CGFunctionInfo &
653  const FunctionArgList &args) {
654  auto argTypes = getArgTypesForDeclaration(Context, args);
655 
657  GetReturnType(resultType), /*instanceMethod=*/false, /*chainCall=*/false,
658  argTypes, FunctionType::ExtInfo(), {}, RequiredArgs::All);
659 }
660 
661 const CGFunctionInfo &
663  ArrayRef<CanQualType> argTypes) {
665  resultType, /*instanceMethod=*/false, /*chainCall=*/false,
666  argTypes, FunctionType::ExtInfo(), {}, RequiredArgs::All);
667 }
668 
669 /// Arrange a call to a C++ method, passing the given arguments.
670 ///
671 /// numPrefixArgs is the number of ABI-specific prefix arguments we have. It
672 /// does not count `this`.
673 const CGFunctionInfo &
675  const FunctionProtoType *proto,
676  RequiredArgs required,
677  unsigned numPrefixArgs) {
678  assert(numPrefixArgs + 1 <= args.size() &&
679  "Emitting a call with less args than the required prefix?");
680  // Add one to account for `this`. It's a bit awkward here, but we don't count
681  // `this` in similar places elsewhere.
682  auto paramInfos =
683  getExtParameterInfosForCall(proto, numPrefixArgs + 1, args.size());
684 
685  // FIXME: Kill copy.
686  auto argTypes = getArgTypesForCall(Context, args);
687 
688  FunctionType::ExtInfo info = proto->getExtInfo();
690  GetReturnType(proto->getReturnType()), /*instanceMethod=*/true,
691  /*chainCall=*/false, argTypes, info, paramInfos, required);
692 }
693 
696  getContext().VoidTy, /*instanceMethod=*/false, /*chainCall=*/false,
698 }
699 
700 const CGFunctionInfo &
702  const CallArgList &args) {
703  assert(signature.arg_size() <= args.size());
704  if (signature.arg_size() == args.size())
705  return signature;
706 
708  auto sigParamInfos = signature.getExtParameterInfos();
709  if (!sigParamInfos.empty()) {
710  paramInfos.append(sigParamInfos.begin(), sigParamInfos.end());
711  paramInfos.resize(args.size());
712  }
713 
714  auto argTypes = getArgTypesForCall(Context, args);
715 
716  assert(signature.getRequiredArgs().allowsOptionalArgs());
717  return arrangeLLVMFunctionInfo(signature.getReturnType(),
718  signature.isInstanceMethod(),
719  signature.isChainCall(),
720  argTypes,
721  signature.getExtInfo(),
722  paramInfos,
723  signature.getRequiredArgs());
724 }
725 
726 namespace clang {
727 namespace CodeGen {
729 }
730 }
731 
732 /// Arrange the argument and result information for an abstract value
733 /// of a given function type. This is the method which all of the
734 /// above functions ultimately defer to.
735 const CGFunctionInfo &
737  bool instanceMethod,
738  bool chainCall,
739  ArrayRef<CanQualType> argTypes,
742  RequiredArgs required) {
743  assert(llvm::all_of(argTypes,
744  [](CanQualType T) { return T.isCanonicalAsParam(); }));
745 
746  // Lookup or create unique function info.
747  llvm::FoldingSetNodeID ID;
748  CGFunctionInfo::Profile(ID, instanceMethod, chainCall, info, paramInfos,
749  required, resultType, argTypes);
750 
751  void *insertPos = nullptr;
752  CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, insertPos);
753  if (FI)
754  return *FI;
755 
756  unsigned CC = ClangCallConvToLLVMCallConv(info.getCC());
757 
758  // Construct the function info. We co-allocate the ArgInfos.
759  FI = CGFunctionInfo::create(CC, instanceMethod, chainCall, info,
760  paramInfos, resultType, argTypes, required);
761  FunctionInfos.InsertNode(FI, insertPos);
762 
763  bool inserted = FunctionsBeingProcessed.insert(FI).second;
764  (void)inserted;
765  assert(inserted && "Recursively being processed?");
766 
767  // Compute ABI information.
768  if (CC == llvm::CallingConv::SPIR_KERNEL) {
769  // Force target independent argument handling for the host visible
770  // kernel functions.
771  computeSPIRKernelABIInfo(CGM, *FI);
772  } else if (info.getCC() == CC_Swift) {
773  swiftcall::computeABIInfo(CGM, *FI);
774  } else {
775  getABIInfo().computeInfo(*FI);
776  }
777 
778  // Loop over all of the computed argument and return value info. If any of
779  // them are direct or extend without a specified coerce type, specify the
780  // default now.
781  ABIArgInfo &retInfo = FI->getReturnInfo();
782  if (retInfo.canHaveCoerceToType() && retInfo.getCoerceToType() == nullptr)
783  retInfo.setCoerceToType(ConvertType(FI->getReturnType()));
784 
785  for (auto &I : FI->arguments())
786  if (I.info.canHaveCoerceToType() && I.info.getCoerceToType() == nullptr)
787  I.info.setCoerceToType(ConvertType(I.type));
788 
789  bool erased = FunctionsBeingProcessed.erase(FI); (void)erased;
790  assert(erased && "Not in set?");
791 
792  return *FI;
793 }
794 
796  bool instanceMethod,
797  bool chainCall,
798  const FunctionType::ExtInfo &info,
799  ArrayRef<ExtParameterInfo> paramInfos,
800  CanQualType resultType,
801  ArrayRef<CanQualType> argTypes,
802  RequiredArgs required) {
803  assert(paramInfos.empty() || paramInfos.size() == argTypes.size());
804  assert(!required.allowsOptionalArgs() ||
805  required.getNumRequiredArgs() <= argTypes.size());
806 
807  void *buffer =
808  operator new(totalSizeToAlloc<ArgInfo, ExtParameterInfo>(
809  argTypes.size() + 1, paramInfos.size()));
810 
811  CGFunctionInfo *FI = new(buffer) CGFunctionInfo();
812  FI->CallingConvention = llvmCC;
813  FI->EffectiveCallingConvention = llvmCC;
814  FI->ASTCallingConvention = info.getCC();
815  FI->InstanceMethod = instanceMethod;
816  FI->ChainCall = chainCall;
817  FI->NoReturn = info.getNoReturn();
818  FI->ReturnsRetained = info.getProducesResult();
819  FI->NoCallerSavedRegs = info.getNoCallerSavedRegs();
820  FI->NoCfCheck = info.getNoCfCheck();
821  FI->Required = required;
822  FI->HasRegParm = info.getHasRegParm();
823  FI->RegParm = info.getRegParm();
824  FI->ArgStruct = nullptr;
825  FI->ArgStructAlign = 0;
826  FI->NumArgs = argTypes.size();
827  FI->HasExtParameterInfos = !paramInfos.empty();
828  FI->getArgsBuffer()[0].type = resultType;
829  for (unsigned i = 0, e = argTypes.size(); i != e; ++i)
830  FI->getArgsBuffer()[i + 1].type = argTypes[i];
831  for (unsigned i = 0, e = paramInfos.size(); i != e; ++i)
832  FI->getExtParameterInfosBuffer()[i] = paramInfos[i];
833  return FI;
834 }
835 
836 /***/
837 
838 namespace {
839 // ABIArgInfo::Expand implementation.
840 
841 // Specifies the way QualType passed as ABIArgInfo::Expand is expanded.
842 struct TypeExpansion {
843  enum TypeExpansionKind {
844  // Elements of constant arrays are expanded recursively.
845  TEK_ConstantArray,
846  // Record fields are expanded recursively (but if record is a union, only
847  // the field with the largest size is expanded).
848  TEK_Record,
849  // For complex types, real and imaginary parts are expanded recursively.
850  TEK_Complex,
851  // All other types are not expandable.
852  TEK_None
853  };
854 
855  const TypeExpansionKind Kind;
856 
857  TypeExpansion(TypeExpansionKind K) : Kind(K) {}
858  virtual ~TypeExpansion() {}
859 };
860 
861 struct ConstantArrayExpansion : TypeExpansion {
862  QualType EltTy;
863  uint64_t NumElts;
864 
865  ConstantArrayExpansion(QualType EltTy, uint64_t NumElts)
866  : TypeExpansion(TEK_ConstantArray), EltTy(EltTy), NumElts(NumElts) {}
867  static bool classof(const TypeExpansion *TE) {
868  return TE->Kind == TEK_ConstantArray;
869  }
870 };
871 
872 struct RecordExpansion : TypeExpansion {
874 
876 
877  RecordExpansion(SmallVector<const CXXBaseSpecifier *, 1> &&Bases,
879  : TypeExpansion(TEK_Record), Bases(std::move(Bases)),
880  Fields(std::move(Fields)) {}
881  static bool classof(const TypeExpansion *TE) {
882  return TE->Kind == TEK_Record;
883  }
884 };
885 
886 struct ComplexExpansion : TypeExpansion {
887  QualType EltTy;
888 
889  ComplexExpansion(QualType EltTy) : TypeExpansion(TEK_Complex), EltTy(EltTy) {}
890  static bool classof(const TypeExpansion *TE) {
891  return TE->Kind == TEK_Complex;
892  }
893 };
894 
895 struct NoExpansion : TypeExpansion {
896  NoExpansion() : TypeExpansion(TEK_None) {}
897  static bool classof(const TypeExpansion *TE) {
898  return TE->Kind == TEK_None;
899  }
900 };
901 } // namespace
902 
903 static std::unique_ptr<TypeExpansion>
904 getTypeExpansion(QualType Ty, const ASTContext &Context) {
905  if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
906  return llvm::make_unique<ConstantArrayExpansion>(
907  AT->getElementType(), AT->getSize().getZExtValue());
908  }
909  if (const RecordType *RT = Ty->getAs<RecordType>()) {
912  const RecordDecl *RD = RT->getDecl();
913  assert(!RD->hasFlexibleArrayMember() &&
914  "Cannot expand structure with flexible array.");
915  if (RD->isUnion()) {
916  // Unions can be here only in degenerative cases - all the fields are same
917  // after flattening. Thus we have to use the "largest" field.
918  const FieldDecl *LargestFD = nullptr;
919  CharUnits UnionSize = CharUnits::Zero();
920 
921  for (const auto *FD : RD->fields()) {
922  if (FD->isZeroLengthBitField(Context))
923  continue;
924  assert(!FD->isBitField() &&
925  "Cannot expand structure with bit-field members.");
926  CharUnits FieldSize = Context.getTypeSizeInChars(FD->getType());
927  if (UnionSize < FieldSize) {
928  UnionSize = FieldSize;
929  LargestFD = FD;
930  }
931  }
932  if (LargestFD)
933  Fields.push_back(LargestFD);
934  } else {
935  if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
936  assert(!CXXRD->isDynamicClass() &&
937  "cannot expand vtable pointers in dynamic classes");
938  for (const CXXBaseSpecifier &BS : CXXRD->bases())
939  Bases.push_back(&BS);
940  }
941 
942  for (const auto *FD : RD->fields()) {
943  if (FD->isZeroLengthBitField(Context))
944  continue;
945  assert(!FD->isBitField() &&
946  "Cannot expand structure with bit-field members.");
947  Fields.push_back(FD);
948  }
949  }
950  return llvm::make_unique<RecordExpansion>(std::move(Bases),
951  std::move(Fields));
952  }
953  if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
954  return llvm::make_unique<ComplexExpansion>(CT->getElementType());
955  }
956  return llvm::make_unique<NoExpansion>();
957 }
958 
959 static int getExpansionSize(QualType Ty, const ASTContext &Context) {
960  auto Exp = getTypeExpansion(Ty, Context);
961  if (auto CAExp = dyn_cast<ConstantArrayExpansion>(Exp.get())) {
962  return CAExp->NumElts * getExpansionSize(CAExp->EltTy, Context);
963  }
964  if (auto RExp = dyn_cast<RecordExpansion>(Exp.get())) {
965  int Res = 0;
966  for (auto BS : RExp->Bases)
967  Res += getExpansionSize(BS->getType(), Context);
968  for (auto FD : RExp->Fields)
969  Res += getExpansionSize(FD->getType(), Context);
970  return Res;
971  }
972  if (isa<ComplexExpansion>(Exp.get()))
973  return 2;
974  assert(isa<NoExpansion>(Exp.get()));
975  return 1;
976 }
977 
978 void
981  auto Exp = getTypeExpansion(Ty, Context);
982  if (auto CAExp = dyn_cast<ConstantArrayExpansion>(Exp.get())) {
983  for (int i = 0, n = CAExp->NumElts; i < n; i++) {
984  getExpandedTypes(CAExp->EltTy, TI);
985  }
986  } else if (auto RExp = dyn_cast<RecordExpansion>(Exp.get())) {
987  for (auto BS : RExp->Bases)
988  getExpandedTypes(BS->getType(), TI);
989  for (auto FD : RExp->Fields)
990  getExpandedTypes(FD->getType(), TI);
991  } else if (auto CExp = dyn_cast<ComplexExpansion>(Exp.get())) {
992  llvm::Type *EltTy = ConvertType(CExp->EltTy);
993  *TI++ = EltTy;
994  *TI++ = EltTy;
995  } else {
996  assert(isa<NoExpansion>(Exp.get()));
997  *TI++ = ConvertType(Ty);
998  }
999 }
1000 
1002  ConstantArrayExpansion *CAE,
1003  Address BaseAddr,
1004  llvm::function_ref<void(Address)> Fn) {
1005  CharUnits EltSize = CGF.getContext().getTypeSizeInChars(CAE->EltTy);
1006  CharUnits EltAlign =
1007  BaseAddr.getAlignment().alignmentOfArrayElement(EltSize);
1008 
1009  for (int i = 0, n = CAE->NumElts; i < n; i++) {
1010  llvm::Value *EltAddr =
1011  CGF.Builder.CreateConstGEP2_32(nullptr, BaseAddr.getPointer(), 0, i);
1012  Fn(Address(EltAddr, EltAlign));
1013  }
1014 }
1015 
1016 void CodeGenFunction::ExpandTypeFromArgs(
1018  assert(LV.isSimple() &&
1019  "Unexpected non-simple lvalue during struct expansion.");
1020 
1021  auto Exp = getTypeExpansion(Ty, getContext());
1022  if (auto CAExp = dyn_cast<ConstantArrayExpansion>(Exp.get())) {
1023  forConstantArrayExpansion(*this, CAExp, LV.getAddress(),
1024  [&](Address EltAddr) {
1025  LValue LV = MakeAddrLValue(EltAddr, CAExp->EltTy);
1026  ExpandTypeFromArgs(CAExp->EltTy, LV, AI);
1027  });
1028  } else if (auto RExp = dyn_cast<RecordExpansion>(Exp.get())) {
1029  Address This = LV.getAddress();
1030  for (const CXXBaseSpecifier *BS : RExp->Bases) {
1031  // Perform a single step derived-to-base conversion.
1032  Address Base =
1033  GetAddressOfBaseClass(This, Ty->getAsCXXRecordDecl(), &BS, &BS + 1,
1034  /*NullCheckValue=*/false, SourceLocation());
1035  LValue SubLV = MakeAddrLValue(Base, BS->getType());
1036 
1037  // Recurse onto bases.
1038  ExpandTypeFromArgs(BS->getType(), SubLV, AI);
1039  }
1040  for (auto FD : RExp->Fields) {
1041  // FIXME: What are the right qualifiers here?
1042  LValue SubLV = EmitLValueForFieldInitialization(LV, FD);
1043  ExpandTypeFromArgs(FD->getType(), SubLV, AI);
1044  }
1045  } else if (isa<ComplexExpansion>(Exp.get())) {
1046  auto realValue = *AI++;
1047  auto imagValue = *AI++;
1048  EmitStoreOfComplex(ComplexPairTy(realValue, imagValue), LV, /*init*/ true);
1049  } else {
1050  assert(isa<NoExpansion>(Exp.get()));
1051  EmitStoreThroughLValue(RValue::get(*AI++), LV);
1052  }
1053 }
1054 
1055 void CodeGenFunction::ExpandTypeToArgs(
1056  QualType Ty, CallArg Arg, llvm::FunctionType *IRFuncTy,
1057  SmallVectorImpl<llvm::Value *> &IRCallArgs, unsigned &IRCallArgPos) {
1058  auto Exp = getTypeExpansion(Ty, getContext());
1059  if (auto CAExp = dyn_cast<ConstantArrayExpansion>(Exp.get())) {
1060  Address Addr = Arg.hasLValue() ? Arg.getKnownLValue().getAddress()
1063  *this, CAExp, Addr, [&](Address EltAddr) {
1064  CallArg EltArg = CallArg(
1065  convertTempToRValue(EltAddr, CAExp->EltTy, SourceLocation()),
1066  CAExp->EltTy);
1067  ExpandTypeToArgs(CAExp->EltTy, EltArg, IRFuncTy, IRCallArgs,
1068  IRCallArgPos);
1069  });
1070  } else if (auto RExp = dyn_cast<RecordExpansion>(Exp.get())) {
1071  Address This = Arg.hasLValue() ? Arg.getKnownLValue().getAddress()
1073  for (const CXXBaseSpecifier *BS : RExp->Bases) {
1074  // Perform a single step derived-to-base conversion.
1075  Address Base =
1076  GetAddressOfBaseClass(This, Ty->getAsCXXRecordDecl(), &BS, &BS + 1,
1077  /*NullCheckValue=*/false, SourceLocation());
1078  CallArg BaseArg = CallArg(RValue::getAggregate(Base), BS->getType());
1079 
1080  // Recurse onto bases.
1081  ExpandTypeToArgs(BS->getType(), BaseArg, IRFuncTy, IRCallArgs,
1082  IRCallArgPos);
1083  }
1084 
1085  LValue LV = MakeAddrLValue(This, Ty);
1086  for (auto FD : RExp->Fields) {
1087  CallArg FldArg =
1088  CallArg(EmitRValueForField(LV, FD, SourceLocation()), FD->getType());
1089  ExpandTypeToArgs(FD->getType(), FldArg, IRFuncTy, IRCallArgs,
1090  IRCallArgPos);
1091  }
1092  } else if (isa<ComplexExpansion>(Exp.get())) {
1094  IRCallArgs[IRCallArgPos++] = CV.first;
1095  IRCallArgs[IRCallArgPos++] = CV.second;
1096  } else {
1097  assert(isa<NoExpansion>(Exp.get()));
1098  auto RV = Arg.getKnownRValue();
1099  assert(RV.isScalar() &&
1100  "Unexpected non-scalar rvalue during struct expansion.");
1101 
1102  // Insert a bitcast as needed.
1103  llvm::Value *V = RV.getScalarVal();
1104  if (IRCallArgPos < IRFuncTy->getNumParams() &&
1105  V->getType() != IRFuncTy->getParamType(IRCallArgPos))
1106  V = Builder.CreateBitCast(V, IRFuncTy->getParamType(IRCallArgPos));
1107 
1108  IRCallArgs[IRCallArgPos++] = V;
1109  }
1110 }
1111 
1112 /// Create a temporary allocation for the purposes of coercion.
1114  CharUnits MinAlign) {
1115  // Don't use an alignment that's worse than what LLVM would prefer.
1116  auto PrefAlign = CGF.CGM.getDataLayout().getPrefTypeAlignment(Ty);
1117  CharUnits Align = std::max(MinAlign, CharUnits::fromQuantity(PrefAlign));
1118 
1119  return CGF.CreateTempAlloca(Ty, Align);
1120 }
1121 
1122 /// EnterStructPointerForCoercedAccess - Given a struct pointer that we are
1123 /// accessing some number of bytes out of it, try to gep into the struct to get
1124 /// at its inner goodness. Dive as deep as possible without entering an element
1125 /// with an in-memory size smaller than DstSize.
1126 static Address
1128  llvm::StructType *SrcSTy,
1129  uint64_t DstSize, CodeGenFunction &CGF) {
1130  // We can't dive into a zero-element struct.
1131  if (SrcSTy->getNumElements() == 0) return SrcPtr;
1132 
1133  llvm::Type *FirstElt = SrcSTy->getElementType(0);
1134 
1135  // If the first elt is at least as large as what we're looking for, or if the
1136  // first element is the same size as the whole struct, we can enter it. The
1137  // comparison must be made on the store size and not the alloca size. Using
1138  // the alloca size may overstate the size of the load.
1139  uint64_t FirstEltSize =
1140  CGF.CGM.getDataLayout().getTypeStoreSize(FirstElt);
1141  if (FirstEltSize < DstSize &&
1142  FirstEltSize < CGF.CGM.getDataLayout().getTypeStoreSize(SrcSTy))
1143  return SrcPtr;
1144 
1145  // GEP into the first element.
1146  SrcPtr = CGF.Builder.CreateStructGEP(SrcPtr, 0, "coerce.dive");
1147 
1148  // If the first element is a struct, recurse.
1149  llvm::Type *SrcTy = SrcPtr.getElementType();
1150  if (llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy))
1151  return EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF);
1152 
1153  return SrcPtr;
1154 }
1155 
1156 /// CoerceIntOrPtrToIntOrPtr - Convert a value Val to the specific Ty where both
1157 /// are either integers or pointers. This does a truncation of the value if it
1158 /// is too large or a zero extension if it is too small.
1159 ///
1160 /// This behaves as if the value were coerced through memory, so on big-endian
1161 /// targets the high bits are preserved in a truncation, while little-endian
1162 /// targets preserve the low bits.
1164  llvm::Type *Ty,
1165  CodeGenFunction &CGF) {
1166  if (Val->getType() == Ty)
1167  return Val;
1168 
1169  if (isa<llvm::PointerType>(Val->getType())) {
1170  // If this is Pointer->Pointer avoid conversion to and from int.
1171  if (isa<llvm::PointerType>(Ty))
1172  return CGF.Builder.CreateBitCast(Val, Ty, "coerce.val");
1173 
1174  // Convert the pointer to an integer so we can play with its width.
1175  Val = CGF.Builder.CreatePtrToInt(Val, CGF.IntPtrTy, "coerce.val.pi");
1176  }
1177 
1178  llvm::Type *DestIntTy = Ty;
1179  if (isa<llvm::PointerType>(DestIntTy))
1180  DestIntTy = CGF.IntPtrTy;
1181 
1182  if (Val->getType() != DestIntTy) {
1183  const llvm::DataLayout &DL = CGF.CGM.getDataLayout();
1184  if (DL.isBigEndian()) {
1185  // Preserve the high bits on big-endian targets.
1186  // That is what memory coercion does.
1187  uint64_t SrcSize = DL.getTypeSizeInBits(Val->getType());
1188  uint64_t DstSize = DL.getTypeSizeInBits(DestIntTy);
1189 
1190  if (SrcSize > DstSize) {
1191  Val = CGF.Builder.CreateLShr(Val, SrcSize - DstSize, "coerce.highbits");
1192  Val = CGF.Builder.CreateTrunc(Val, DestIntTy, "coerce.val.ii");
1193  } else {
1194  Val = CGF.Builder.CreateZExt(Val, DestIntTy, "coerce.val.ii");
1195  Val = CGF.Builder.CreateShl(Val, DstSize - SrcSize, "coerce.highbits");
1196  }
1197  } else {
1198  // Little-endian targets preserve the low bits. No shifts required.
1199  Val = CGF.Builder.CreateIntCast(Val, DestIntTy, false, "coerce.val.ii");
1200  }
1201  }
1202 
1203  if (isa<llvm::PointerType>(Ty))
1204  Val = CGF.Builder.CreateIntToPtr(Val, Ty, "coerce.val.ip");
1205  return Val;
1206 }
1207 
1208 
1209 
1210 /// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
1211 /// a pointer to an object of type \arg Ty, known to be aligned to
1212 /// \arg SrcAlign bytes.
1213 ///
1214 /// This safely handles the case when the src type is smaller than the
1215 /// destination type; in this situation the values of bits which not
1216 /// present in the src are undefined.
1218  CodeGenFunction &CGF) {
1219  llvm::Type *SrcTy = Src.getElementType();
1220 
1221  // If SrcTy and Ty are the same, just do a load.
1222  if (SrcTy == Ty)
1223  return CGF.Builder.CreateLoad(Src);
1224 
1225  uint64_t DstSize = CGF.CGM.getDataLayout().getTypeAllocSize(Ty);
1226 
1227  if (llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy)) {
1228  Src = EnterStructPointerForCoercedAccess(Src, SrcSTy, DstSize, CGF);
1229  SrcTy = Src.getType()->getElementType();
1230  }
1231 
1232  uint64_t SrcSize = CGF.CGM.getDataLayout().getTypeAllocSize(SrcTy);
1233 
1234  // If the source and destination are integer or pointer types, just do an
1235  // extension or truncation to the desired type.
1236  if ((isa<llvm::IntegerType>(Ty) || isa<llvm::PointerType>(Ty)) &&
1237  (isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy))) {
1238  llvm::Value *Load = CGF.Builder.CreateLoad(Src);
1239  return CoerceIntOrPtrToIntOrPtr(Load, Ty, CGF);
1240  }
1241 
1242  // If load is legal, just bitcast the src pointer.
1243  if (SrcSize >= DstSize) {
1244  // Generally SrcSize is never greater than DstSize, since this means we are
1245  // losing bits. However, this can happen in cases where the structure has
1246  // additional padding, for example due to a user specified alignment.
1247  //
1248  // FIXME: Assert that we aren't truncating non-padding bits when have access
1249  // to that information.
1250  Src = CGF.Builder.CreateBitCast(Src,
1251  Ty->getPointerTo(Src.getAddressSpace()));
1252  return CGF.Builder.CreateLoad(Src);
1253  }
1254 
1255  // Otherwise do coercion through memory. This is stupid, but simple.
1256  Address Tmp = CreateTempAllocaForCoercion(CGF, Ty, Src.getAlignment());
1257  Address Casted = CGF.Builder.CreateElementBitCast(Tmp,CGF.Int8Ty);
1258  Address SrcCasted = CGF.Builder.CreateElementBitCast(Src,CGF.Int8Ty);
1259  CGF.Builder.CreateMemCpy(Casted, SrcCasted,
1260  llvm::ConstantInt::get(CGF.IntPtrTy, SrcSize),
1261  false);
1262  return CGF.Builder.CreateLoad(Tmp);
1263 }
1264 
1265 // Function to store a first-class aggregate into memory. We prefer to
1266 // store the elements rather than the aggregate to be more friendly to
1267 // fast-isel.
1268 // FIXME: Do we need to recurse here?
1270  Address Dest, bool DestIsVolatile) {
1271  // Prefer scalar stores to first-class aggregate stores.
1272  if (llvm::StructType *STy =
1273  dyn_cast<llvm::StructType>(Val->getType())) {
1274  for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1275  Address EltPtr = CGF.Builder.CreateStructGEP(Dest, i);
1276  llvm::Value *Elt = CGF.Builder.CreateExtractValue(Val, i);
1277  CGF.Builder.CreateStore(Elt, EltPtr, DestIsVolatile);
1278  }
1279  } else {
1280  CGF.Builder.CreateStore(Val, Dest, DestIsVolatile);
1281  }
1282 }
1283 
1284 /// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
1285 /// where the source and destination may have different types. The
1286 /// destination is known to be aligned to \arg DstAlign bytes.
1287 ///
1288 /// This safely handles the case when the src type is larger than the
1289 /// destination type; the upper bits of the src will be lost.
1291  Address Dst,
1292  bool DstIsVolatile,
1293  CodeGenFunction &CGF) {
1294  llvm::Type *SrcTy = Src->getType();
1295  llvm::Type *DstTy = Dst.getType()->getElementType();
1296  if (SrcTy == DstTy) {
1297  CGF.Builder.CreateStore(Src, Dst, DstIsVolatile);
1298  return;
1299  }
1300 
1301  uint64_t SrcSize = CGF.CGM.getDataLayout().getTypeAllocSize(SrcTy);
1302 
1303  if (llvm::StructType *DstSTy = dyn_cast<llvm::StructType>(DstTy)) {
1304  Dst = EnterStructPointerForCoercedAccess(Dst, DstSTy, SrcSize, CGF);
1305  DstTy = Dst.getType()->getElementType();
1306  }
1307 
1308  // If the source and destination are integer or pointer types, just do an
1309  // extension or truncation to the desired type.
1310  if ((isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy)) &&
1311  (isa<llvm::IntegerType>(DstTy) || isa<llvm::PointerType>(DstTy))) {
1312  Src = CoerceIntOrPtrToIntOrPtr(Src, DstTy, CGF);
1313  CGF.Builder.CreateStore(Src, Dst, DstIsVolatile);
1314  return;
1315  }
1316 
1317  uint64_t DstSize = CGF.CGM.getDataLayout().getTypeAllocSize(DstTy);
1318 
1319  // If store is legal, just bitcast the src pointer.
1320  if (SrcSize <= DstSize) {
1321  Dst = CGF.Builder.CreateElementBitCast(Dst, SrcTy);
1322  BuildAggStore(CGF, Src, Dst, DstIsVolatile);
1323  } else {
1324  // Otherwise do coercion through memory. This is stupid, but
1325  // simple.
1326 
1327  // Generally SrcSize is never greater than DstSize, since this means we are
1328  // losing bits. However, this can happen in cases where the structure has
1329  // additional padding, for example due to a user specified alignment.
1330  //
1331  // FIXME: Assert that we aren't truncating non-padding bits when have access
1332  // to that information.
1333  Address Tmp = CreateTempAllocaForCoercion(CGF, SrcTy, Dst.getAlignment());
1334  CGF.Builder.CreateStore(Src, Tmp);
1335  Address Casted = CGF.Builder.CreateElementBitCast(Tmp,CGF.Int8Ty);
1336  Address DstCasted = CGF.Builder.CreateElementBitCast(Dst,CGF.Int8Ty);
1337  CGF.Builder.CreateMemCpy(DstCasted, Casted,
1338  llvm::ConstantInt::get(CGF.IntPtrTy, DstSize),
1339  false);
1340  }
1341 }
1342 
1344  const ABIArgInfo &info) {
1345  if (unsigned offset = info.getDirectOffset()) {
1346  addr = CGF.Builder.CreateElementBitCast(addr, CGF.Int8Ty);
1347  addr = CGF.Builder.CreateConstInBoundsByteGEP(addr,
1348  CharUnits::fromQuantity(offset));
1349  addr = CGF.Builder.CreateElementBitCast(addr, info.getCoerceToType());
1350  }
1351  return addr;
1352 }
1353 
1354 namespace {
1355 
1356 /// Encapsulates information about the way function arguments from
1357 /// CGFunctionInfo should be passed to actual LLVM IR function.
1358 class ClangToLLVMArgMapping {
1359  static const unsigned InvalidIndex = ~0U;
1360  unsigned InallocaArgNo;
1361  unsigned SRetArgNo;
1362  unsigned TotalIRArgs;
1363 
1364  /// Arguments of LLVM IR function corresponding to single Clang argument.
1365  struct IRArgs {
1366  unsigned PaddingArgIndex;
1367  // Argument is expanded to IR arguments at positions
1368  // [FirstArgIndex, FirstArgIndex + NumberOfArgs).
1369  unsigned FirstArgIndex;
1370  unsigned NumberOfArgs;
1371 
1372  IRArgs()
1373  : PaddingArgIndex(InvalidIndex), FirstArgIndex(InvalidIndex),
1374  NumberOfArgs(0) {}
1375  };
1376 
1377  SmallVector<IRArgs, 8> ArgInfo;
1378 
1379 public:
1380  ClangToLLVMArgMapping(const ASTContext &Context, const CGFunctionInfo &FI,
1381  bool OnlyRequiredArgs = false)
1382  : InallocaArgNo(InvalidIndex), SRetArgNo(InvalidIndex), TotalIRArgs(0),
1383  ArgInfo(OnlyRequiredArgs ? FI.getNumRequiredArgs() : FI.arg_size()) {
1384  construct(Context, FI, OnlyRequiredArgs);
1385  }
1386 
1387  bool hasInallocaArg() const { return InallocaArgNo != InvalidIndex; }
1388  unsigned getInallocaArgNo() const {
1389  assert(hasInallocaArg());
1390  return InallocaArgNo;
1391  }
1392 
1393  bool hasSRetArg() const { return SRetArgNo != InvalidIndex; }
1394  unsigned getSRetArgNo() const {
1395  assert(hasSRetArg());
1396  return SRetArgNo;
1397  }
1398 
1399  unsigned totalIRArgs() const { return TotalIRArgs; }
1400 
1401  bool hasPaddingArg(unsigned ArgNo) const {
1402  assert(ArgNo < ArgInfo.size());
1403  return ArgInfo[ArgNo].PaddingArgIndex != InvalidIndex;
1404  }
1405  unsigned getPaddingArgNo(unsigned ArgNo) const {
1406  assert(hasPaddingArg(ArgNo));
1407  return ArgInfo[ArgNo].PaddingArgIndex;
1408  }
1409 
1410  /// Returns index of first IR argument corresponding to ArgNo, and their
1411  /// quantity.
1412  std::pair<unsigned, unsigned> getIRArgs(unsigned ArgNo) const {
1413  assert(ArgNo < ArgInfo.size());
1414  return std::make_pair(ArgInfo[ArgNo].FirstArgIndex,
1415  ArgInfo[ArgNo].NumberOfArgs);
1416  }
1417 
1418 private:
1419  void construct(const ASTContext &Context, const CGFunctionInfo &FI,
1420  bool OnlyRequiredArgs);
1421 };
1422 
1423 void ClangToLLVMArgMapping::construct(const ASTContext &Context,
1424  const CGFunctionInfo &FI,
1425  bool OnlyRequiredArgs) {
1426  unsigned IRArgNo = 0;
1427  bool SwapThisWithSRet = false;
1428  const ABIArgInfo &RetAI = FI.getReturnInfo();
1429 
1430  if (RetAI.getKind() == ABIArgInfo::Indirect) {
1431  SwapThisWithSRet = RetAI.isSRetAfterThis();
1432  SRetArgNo = SwapThisWithSRet ? 1 : IRArgNo++;
1433  }
1434 
1435  unsigned ArgNo = 0;
1436  unsigned NumArgs = OnlyRequiredArgs ? FI.getNumRequiredArgs() : FI.arg_size();
1437  for (CGFunctionInfo::const_arg_iterator I = FI.arg_begin(); ArgNo < NumArgs;
1438  ++I, ++ArgNo) {
1439  assert(I != FI.arg_end());
1440  QualType ArgType = I->type;
1441  const ABIArgInfo &AI = I->info;
1442  // Collect data about IR arguments corresponding to Clang argument ArgNo.
1443  auto &IRArgs = ArgInfo[ArgNo];
1444 
1445  if (AI.getPaddingType())
1446  IRArgs.PaddingArgIndex = IRArgNo++;
1447 
1448  switch (AI.getKind()) {
1449  case ABIArgInfo::Extend:
1450  case ABIArgInfo::Direct: {
1451  // FIXME: handle sseregparm someday...
1452  llvm::StructType *STy = dyn_cast<llvm::StructType>(AI.getCoerceToType());
1453  if (AI.isDirect() && AI.getCanBeFlattened() && STy) {
1454  IRArgs.NumberOfArgs = STy->getNumElements();
1455  } else {
1456  IRArgs.NumberOfArgs = 1;
1457  }
1458  break;
1459  }
1460  case ABIArgInfo::Indirect:
1461  IRArgs.NumberOfArgs = 1;
1462  break;
1463  case ABIArgInfo::Ignore:
1464  case ABIArgInfo::InAlloca:
1465  // ignore and inalloca doesn't have matching LLVM parameters.
1466  IRArgs.NumberOfArgs = 0;
1467  break;
1469  IRArgs.NumberOfArgs = AI.getCoerceAndExpandTypeSequence().size();
1470  break;
1471  case ABIArgInfo::Expand:
1472  IRArgs.NumberOfArgs = getExpansionSize(ArgType, Context);
1473  break;
1474  }
1475 
1476  if (IRArgs.NumberOfArgs > 0) {
1477  IRArgs.FirstArgIndex = IRArgNo;
1478  IRArgNo += IRArgs.NumberOfArgs;
1479  }
1480 
1481  // Skip over the sret parameter when it comes second. We already handled it
1482  // above.
1483  if (IRArgNo == 1 && SwapThisWithSRet)
1484  IRArgNo++;
1485  }
1486  assert(ArgNo == ArgInfo.size());
1487 
1488  if (FI.usesInAlloca())
1489  InallocaArgNo = IRArgNo++;
1490 
1491  TotalIRArgs = IRArgNo;
1492 }
1493 } // namespace
1494 
1495 /***/
1496 
1498  const auto &RI = FI.getReturnInfo();
1499  return RI.isIndirect() || (RI.isInAlloca() && RI.getInAllocaSRet());
1500 }
1501 
1503  return ReturnTypeUsesSRet(FI) &&
1504  getTargetCodeGenInfo().doesReturnSlotInterfereWithArgs();
1505 }
1506 
1508  if (const BuiltinType *BT = ResultType->getAs<BuiltinType>()) {
1509  switch (BT->getKind()) {
1510  default:
1511  return false;
1512  case BuiltinType::Float:
1514  case BuiltinType::Double:
1516  case BuiltinType::LongDouble:
1518  }
1519  }
1520 
1521  return false;
1522 }
1523 
1525  if (const ComplexType *CT = ResultType->getAs<ComplexType>()) {
1526  if (const BuiltinType *BT = CT->getElementType()->getAs<BuiltinType>()) {
1527  if (BT->getKind() == BuiltinType::LongDouble)
1529  }
1530  }
1531 
1532  return false;
1533 }
1534 
1536  const CGFunctionInfo &FI = arrangeGlobalDeclaration(GD);
1537  return GetFunctionType(FI);
1538 }
1539 
1540 llvm::FunctionType *
1542 
1543  bool Inserted = FunctionsBeingProcessed.insert(&FI).second;
1544  (void)Inserted;
1545  assert(Inserted && "Recursively being processed?");
1546 
1547  llvm::Type *resultType = nullptr;
1548  const ABIArgInfo &retAI = FI.getReturnInfo();
1549  switch (retAI.getKind()) {
1550  case ABIArgInfo::Expand:
1551  llvm_unreachable("Invalid ABI kind for return argument");
1552 
1553  case ABIArgInfo::Extend:
1554  case ABIArgInfo::Direct:
1555  resultType = retAI.getCoerceToType();
1556  break;
1557 
1558  case ABIArgInfo::InAlloca:
1559  if (retAI.getInAllocaSRet()) {
1560  // sret things on win32 aren't void, they return the sret pointer.
1561  QualType ret = FI.getReturnType();
1562  llvm::Type *ty = ConvertType(ret);
1563  unsigned addressSpace = Context.getTargetAddressSpace(ret);
1564  resultType = llvm::PointerType::get(ty, addressSpace);
1565  } else {
1566  resultType = llvm::Type::getVoidTy(getLLVMContext());
1567  }
1568  break;
1569 
1570  case ABIArgInfo::Indirect:
1571  case ABIArgInfo::Ignore:
1572  resultType = llvm::Type::getVoidTy(getLLVMContext());
1573  break;
1574 
1576  resultType = retAI.getUnpaddedCoerceAndExpandType();
1577  break;
1578  }
1579 
1580  ClangToLLVMArgMapping IRFunctionArgs(getContext(), FI, true);
1581  SmallVector<llvm::Type*, 8> ArgTypes(IRFunctionArgs.totalIRArgs());
1582 
1583  // Add type for sret argument.
1584  if (IRFunctionArgs.hasSRetArg()) {
1585  QualType Ret = FI.getReturnType();
1586  llvm::Type *Ty = ConvertType(Ret);
1587  unsigned AddressSpace = Context.getTargetAddressSpace(Ret);
1588  ArgTypes[IRFunctionArgs.getSRetArgNo()] =
1589  llvm::PointerType::get(Ty, AddressSpace);
1590  }
1591 
1592  // Add type for inalloca argument.
1593  if (IRFunctionArgs.hasInallocaArg()) {
1594  auto ArgStruct = FI.getArgStruct();
1595  assert(ArgStruct);
1596  ArgTypes[IRFunctionArgs.getInallocaArgNo()] = ArgStruct->getPointerTo();
1597  }
1598 
1599  // Add in all of the required arguments.
1600  unsigned ArgNo = 0;
1602  ie = it + FI.getNumRequiredArgs();
1603  for (; it != ie; ++it, ++ArgNo) {
1604  const ABIArgInfo &ArgInfo = it->info;
1605 
1606  // Insert a padding type to ensure proper alignment.
1607  if (IRFunctionArgs.hasPaddingArg(ArgNo))
1608  ArgTypes[IRFunctionArgs.getPaddingArgNo(ArgNo)] =
1609  ArgInfo.getPaddingType();
1610 
1611  unsigned FirstIRArg, NumIRArgs;
1612  std::tie(FirstIRArg, NumIRArgs) = IRFunctionArgs.getIRArgs(ArgNo);
1613 
1614  switch (ArgInfo.getKind()) {
1615  case ABIArgInfo::Ignore:
1616  case ABIArgInfo::InAlloca:
1617  assert(NumIRArgs == 0);
1618  break;
1619 
1620  case ABIArgInfo::Indirect: {
1621  assert(NumIRArgs == 1);
1622  // indirect arguments are always on the stack, which is alloca addr space.
1623  llvm::Type *LTy = ConvertTypeForMem(it->type);
1624  ArgTypes[FirstIRArg] = LTy->getPointerTo(
1625  CGM.getDataLayout().getAllocaAddrSpace());
1626  break;
1627  }
1628 
1629  case ABIArgInfo::Extend:
1630  case ABIArgInfo::Direct: {
1631  // Fast-isel and the optimizer generally like scalar values better than
1632  // FCAs, so we flatten them if this is safe to do for this argument.
1633  llvm::Type *argType = ArgInfo.getCoerceToType();
1634  llvm::StructType *st = dyn_cast<llvm::StructType>(argType);
1635  if (st && ArgInfo.isDirect() && ArgInfo.getCanBeFlattened()) {
1636  assert(NumIRArgs == st->getNumElements());
1637  for (unsigned i = 0, e = st->getNumElements(); i != e; ++i)
1638  ArgTypes[FirstIRArg + i] = st->getElementType(i);
1639  } else {
1640  assert(NumIRArgs == 1);
1641  ArgTypes[FirstIRArg] = argType;
1642  }
1643  break;
1644  }
1645 
1647  auto ArgTypesIter = ArgTypes.begin() + FirstIRArg;
1648  for (auto EltTy : ArgInfo.getCoerceAndExpandTypeSequence()) {
1649  *ArgTypesIter++ = EltTy;
1650  }
1651  assert(ArgTypesIter == ArgTypes.begin() + FirstIRArg + NumIRArgs);
1652  break;
1653  }
1654 
1655  case ABIArgInfo::Expand:
1656  auto ArgTypesIter = ArgTypes.begin() + FirstIRArg;
1657  getExpandedTypes(it->type, ArgTypesIter);
1658  assert(ArgTypesIter == ArgTypes.begin() + FirstIRArg + NumIRArgs);
1659  break;
1660  }
1661  }
1662 
1663  bool Erased = FunctionsBeingProcessed.erase(&FI); (void)Erased;
1664  assert(Erased && "Not in set?");
1665 
1666  return llvm::FunctionType::get(resultType, ArgTypes, FI.isVariadic());
1667 }
1668 
1670  const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
1671  const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
1672 
1673  if (!isFuncTypeConvertible(FPT))
1674  return llvm::StructType::get(getLLVMContext());
1675 
1676  return GetFunctionType(GD);
1677 }
1678 
1680  llvm::AttrBuilder &FuncAttrs,
1681  const FunctionProtoType *FPT) {
1682  if (!FPT)
1683  return;
1684 
1686  FPT->isNothrow())
1687  FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
1688 }
1689 
1690 void CodeGenModule::ConstructDefaultFnAttrList(StringRef Name, bool HasOptnone,
1691  bool AttrOnCallSite,
1692  llvm::AttrBuilder &FuncAttrs) {
1693  // OptimizeNoneAttr takes precedence over -Os or -Oz. No warning needed.
1694  if (!HasOptnone) {
1695  if (CodeGenOpts.OptimizeSize)
1696  FuncAttrs.addAttribute(llvm::Attribute::OptimizeForSize);
1697  if (CodeGenOpts.OptimizeSize == 2)
1698  FuncAttrs.addAttribute(llvm::Attribute::MinSize);
1699  }
1700 
1701  if (CodeGenOpts.DisableRedZone)
1702  FuncAttrs.addAttribute(llvm::Attribute::NoRedZone);
1703  if (CodeGenOpts.IndirectTlsSegRefs)
1704  FuncAttrs.addAttribute("indirect-tls-seg-refs");
1705  if (CodeGenOpts.NoImplicitFloat)
1706  FuncAttrs.addAttribute(llvm::Attribute::NoImplicitFloat);
1707 
1708  if (AttrOnCallSite) {
1709  // Attributes that should go on the call site only.
1710  if (!CodeGenOpts.SimplifyLibCalls ||
1711  CodeGenOpts.isNoBuiltinFunc(Name.data()))
1712  FuncAttrs.addAttribute(llvm::Attribute::NoBuiltin);
1713  if (!CodeGenOpts.TrapFuncName.empty())
1714  FuncAttrs.addAttribute("trap-func-name", CodeGenOpts.TrapFuncName);
1715  } else {
1716  // Attributes that should go on the function, but not the call site.
1717  if (!CodeGenOpts.DisableFPElim) {
1718  FuncAttrs.addAttribute("no-frame-pointer-elim", "false");
1719  } else if (CodeGenOpts.OmitLeafFramePointer) {
1720  FuncAttrs.addAttribute("no-frame-pointer-elim", "false");
1721  FuncAttrs.addAttribute("no-frame-pointer-elim-non-leaf");
1722  } else {
1723  FuncAttrs.addAttribute("no-frame-pointer-elim", "true");
1724  FuncAttrs.addAttribute("no-frame-pointer-elim-non-leaf");
1725  }
1726 
1727  FuncAttrs.addAttribute("less-precise-fpmad",
1728  llvm::toStringRef(CodeGenOpts.LessPreciseFPMAD));
1729 
1730  if (CodeGenOpts.NullPointerIsValid)
1731  FuncAttrs.addAttribute("null-pointer-is-valid", "true");
1732  if (!CodeGenOpts.FPDenormalMode.empty())
1733  FuncAttrs.addAttribute("denormal-fp-math", CodeGenOpts.FPDenormalMode);
1734 
1735  FuncAttrs.addAttribute("no-trapping-math",
1736  llvm::toStringRef(CodeGenOpts.NoTrappingMath));
1737 
1738  // Strict (compliant) code is the default, so only add this attribute to
1739  // indicate that we are trying to workaround a problem case.
1740  if (!CodeGenOpts.StrictFloatCastOverflow)
1741  FuncAttrs.addAttribute("strict-float-cast-overflow", "false");
1742 
1743  // TODO: Are these all needed?
1744  // unsafe/inf/nan/nsz are handled by instruction-level FastMathFlags.
1745  FuncAttrs.addAttribute("no-infs-fp-math",
1746  llvm::toStringRef(CodeGenOpts.NoInfsFPMath));
1747  FuncAttrs.addAttribute("no-nans-fp-math",
1748  llvm::toStringRef(CodeGenOpts.NoNaNsFPMath));
1749  FuncAttrs.addAttribute("unsafe-fp-math",
1750  llvm::toStringRef(CodeGenOpts.UnsafeFPMath));
1751  FuncAttrs.addAttribute("use-soft-float",
1752  llvm::toStringRef(CodeGenOpts.SoftFloat));
1753  FuncAttrs.addAttribute("stack-protector-buffer-size",
1754  llvm::utostr(CodeGenOpts.SSPBufferSize));
1755  FuncAttrs.addAttribute("no-signed-zeros-fp-math",
1756  llvm::toStringRef(CodeGenOpts.NoSignedZeros));
1757  FuncAttrs.addAttribute(
1758  "correctly-rounded-divide-sqrt-fp-math",
1759  llvm::toStringRef(CodeGenOpts.CorrectlyRoundedDivSqrt));
1760 
1761  if (getLangOpts().OpenCL)
1762  FuncAttrs.addAttribute("denorms-are-zero",
1763  llvm::toStringRef(CodeGenOpts.FlushDenorm));
1764 
1765  // TODO: Reciprocal estimate codegen options should apply to instructions?
1766  const std::vector<std::string> &Recips = CodeGenOpts.Reciprocals;
1767  if (!Recips.empty())
1768  FuncAttrs.addAttribute("reciprocal-estimates",
1769  llvm::join(Recips, ","));
1770 
1771  if (!CodeGenOpts.PreferVectorWidth.empty() &&
1772  CodeGenOpts.PreferVectorWidth != "none")
1773  FuncAttrs.addAttribute("prefer-vector-width",
1774  CodeGenOpts.PreferVectorWidth);
1775 
1776  if (CodeGenOpts.StackRealignment)
1777  FuncAttrs.addAttribute("stackrealign");
1778  if (CodeGenOpts.Backchain)
1779  FuncAttrs.addAttribute("backchain");
1780 
1781  if (CodeGenOpts.SpeculativeLoadHardening)
1782  FuncAttrs.addAttribute(llvm::Attribute::SpeculativeLoadHardening);
1783  }
1784 
1785  if (getLangOpts().assumeFunctionsAreConvergent()) {
1786  // Conservatively, mark all functions and calls in CUDA and OpenCL as
1787  // convergent (meaning, they may call an intrinsically convergent op, such
1788  // as __syncthreads() / barrier(), and so can't have certain optimizations
1789  // applied around them). LLVM will remove this attribute where it safely
1790  // can.
1791  FuncAttrs.addAttribute(llvm::Attribute::Convergent);
1792  }
1793 
1794  if (getLangOpts().CUDA && getLangOpts().CUDAIsDevice) {
1795  // Exceptions aren't supported in CUDA device code.
1796  FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
1797 
1798  // Respect -fcuda-flush-denormals-to-zero.
1799  if (CodeGenOpts.FlushDenorm)
1800  FuncAttrs.addAttribute("nvptx-f32ftz", "true");
1801  }
1802 
1803  for (StringRef Attr : CodeGenOpts.DefaultFunctionAttrs) {
1804  StringRef Var, Value;
1805  std::tie(Var, Value) = Attr.split('=');
1806  FuncAttrs.addAttribute(Var, Value);
1807  }
1808 }
1809 
1810 void CodeGenModule::AddDefaultFnAttrs(llvm::Function &F) {
1811  llvm::AttrBuilder FuncAttrs;
1812  ConstructDefaultFnAttrList(F.getName(), F.hasOptNone(),
1813  /* AttrOnCallsite = */ false, FuncAttrs);
1814  F.addAttributes(llvm::AttributeList::FunctionIndex, FuncAttrs);
1815 }
1816 
1818  StringRef Name, const CGFunctionInfo &FI, CGCalleeInfo CalleeInfo,
1819  llvm::AttributeList &AttrList, unsigned &CallingConv, bool AttrOnCallSite) {
1820  llvm::AttrBuilder FuncAttrs;
1821  llvm::AttrBuilder RetAttrs;
1822 
1823  CallingConv = FI.getEffectiveCallingConvention();
1824  if (FI.isNoReturn())
1825  FuncAttrs.addAttribute(llvm::Attribute::NoReturn);
1826 
1827  // If we have information about the function prototype, we can learn
1828  // attributes from there.
1830  CalleeInfo.getCalleeFunctionProtoType());
1831 
1832  const Decl *TargetDecl = CalleeInfo.getCalleeDecl().getDecl();
1833 
1834  bool HasOptnone = false;
1835  // FIXME: handle sseregparm someday...
1836  if (TargetDecl) {
1837  if (TargetDecl->hasAttr<ReturnsTwiceAttr>())
1838  FuncAttrs.addAttribute(llvm::Attribute::ReturnsTwice);
1839  if (TargetDecl->hasAttr<NoThrowAttr>())
1840  FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
1841  if (TargetDecl->hasAttr<NoReturnAttr>())
1842  FuncAttrs.addAttribute(llvm::Attribute::NoReturn);
1843  if (TargetDecl->hasAttr<ColdAttr>())
1844  FuncAttrs.addAttribute(llvm::Attribute::Cold);
1845  if (TargetDecl->hasAttr<NoDuplicateAttr>())
1846  FuncAttrs.addAttribute(llvm::Attribute::NoDuplicate);
1847  if (TargetDecl->hasAttr<ConvergentAttr>())
1848  FuncAttrs.addAttribute(llvm::Attribute::Convergent);
1849 
1850  if (const FunctionDecl *Fn = dyn_cast<FunctionDecl>(TargetDecl)) {
1852  getContext(), FuncAttrs, Fn->getType()->getAs<FunctionProtoType>());
1853  // Don't use [[noreturn]] or _Noreturn for a call to a virtual function.
1854  // These attributes are not inherited by overloads.
1855  const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Fn);
1856  if (Fn->isNoReturn() && !(AttrOnCallSite && MD && MD->isVirtual()))
1857  FuncAttrs.addAttribute(llvm::Attribute::NoReturn);
1858  }
1859 
1860  // 'const', 'pure' and 'noalias' attributed functions are also nounwind.
1861  if (TargetDecl->hasAttr<ConstAttr>()) {
1862  FuncAttrs.addAttribute(llvm::Attribute::ReadNone);
1863  FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
1864  } else if (TargetDecl->hasAttr<PureAttr>()) {
1865  FuncAttrs.addAttribute(llvm::Attribute::ReadOnly);
1866  FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
1867  } else if (TargetDecl->hasAttr<NoAliasAttr>()) {
1868  FuncAttrs.addAttribute(llvm::Attribute::ArgMemOnly);
1869  FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
1870  }
1871  if (TargetDecl->hasAttr<RestrictAttr>())
1872  RetAttrs.addAttribute(llvm::Attribute::NoAlias);
1873  if (TargetDecl->hasAttr<ReturnsNonNullAttr>() &&
1874  !CodeGenOpts.NullPointerIsValid)
1875  RetAttrs.addAttribute(llvm::Attribute::NonNull);
1876  if (TargetDecl->hasAttr<AnyX86NoCallerSavedRegistersAttr>())
1877  FuncAttrs.addAttribute("no_caller_saved_registers");
1878  if (TargetDecl->hasAttr<AnyX86NoCfCheckAttr>())
1879  FuncAttrs.addAttribute(llvm::Attribute::NoCfCheck);
1880 
1881  HasOptnone = TargetDecl->hasAttr<OptimizeNoneAttr>();
1882  if (auto *AllocSize = TargetDecl->getAttr<AllocSizeAttr>()) {
1883  Optional<unsigned> NumElemsParam;
1884  if (AllocSize->getNumElemsParam().isValid())
1885  NumElemsParam = AllocSize->getNumElemsParam().getLLVMIndex();
1886  FuncAttrs.addAllocSizeAttr(AllocSize->getElemSizeParam().getLLVMIndex(),
1887  NumElemsParam);
1888  }
1889  }
1890 
1891  ConstructDefaultFnAttrList(Name, HasOptnone, AttrOnCallSite, FuncAttrs);
1892 
1893  // This must run after constructing the default function attribute list
1894  // to ensure that the speculative load hardening attribute is removed
1895  // in the case where the -mspeculative-load-hardening flag was passed.
1896  if (TargetDecl) {
1897  if (TargetDecl->hasAttr<NoSpeculativeLoadHardeningAttr>())
1898  FuncAttrs.removeAttribute(llvm::Attribute::SpeculativeLoadHardening);
1899  if (TargetDecl->hasAttr<SpeculativeLoadHardeningAttr>())
1900  FuncAttrs.addAttribute(llvm::Attribute::SpeculativeLoadHardening);
1901  }
1902 
1903  if (CodeGenOpts.EnableSegmentedStacks &&
1904  !(TargetDecl && TargetDecl->hasAttr<NoSplitStackAttr>()))
1905  FuncAttrs.addAttribute("split-stack");
1906 
1907  // Add NonLazyBind attribute to function declarations when -fno-plt
1908  // is used.
1909  if (TargetDecl && CodeGenOpts.NoPLT) {
1910  if (auto *Fn = dyn_cast<FunctionDecl>(TargetDecl)) {
1911  if (!Fn->isDefined() && !AttrOnCallSite) {
1912  FuncAttrs.addAttribute(llvm::Attribute::NonLazyBind);
1913  }
1914  }
1915  }
1916 
1917  if (TargetDecl && TargetDecl->hasAttr<OpenCLKernelAttr>()) {
1918  if (getLangOpts().OpenCLVersion <= 120) {
1919  // OpenCL v1.2 Work groups are always uniform
1920  FuncAttrs.addAttribute("uniform-work-group-size", "true");
1921  } else {
1922  // OpenCL v2.0 Work groups may be whether uniform or not.
1923  // '-cl-uniform-work-group-size' compile option gets a hint
1924  // to the compiler that the global work-size be a multiple of
1925  // the work-group size specified to clEnqueueNDRangeKernel
1926  // (i.e. work groups are uniform).
1927  FuncAttrs.addAttribute("uniform-work-group-size",
1928  llvm::toStringRef(CodeGenOpts.UniformWGSize));
1929  }
1930  }
1931 
1932  if (!AttrOnCallSite) {
1933  bool DisableTailCalls = false;
1934 
1935  if (CodeGenOpts.DisableTailCalls)
1936  DisableTailCalls = true;
1937  else if (TargetDecl) {
1938  if (TargetDecl->hasAttr<DisableTailCallsAttr>() ||
1939  TargetDecl->hasAttr<AnyX86InterruptAttr>())
1940  DisableTailCalls = true;
1941  else if (CodeGenOpts.NoEscapingBlockTailCalls) {
1942  if (const auto *BD = dyn_cast<BlockDecl>(TargetDecl))
1943  if (!BD->doesNotEscape())
1944  DisableTailCalls = true;
1945  }
1946  }
1947 
1948  FuncAttrs.addAttribute("disable-tail-calls",
1949  llvm::toStringRef(DisableTailCalls));
1950  GetCPUAndFeaturesAttributes(CalleeInfo.getCalleeDecl(), FuncAttrs);
1951  }
1952 
1953  ClangToLLVMArgMapping IRFunctionArgs(getContext(), FI);
1954 
1955  QualType RetTy = FI.getReturnType();
1956  const ABIArgInfo &RetAI = FI.getReturnInfo();
1957  switch (RetAI.getKind()) {
1958  case ABIArgInfo::Extend:
1959  if (RetAI.isSignExt())
1960  RetAttrs.addAttribute(llvm::Attribute::SExt);
1961  else
1962  RetAttrs.addAttribute(llvm::Attribute::ZExt);
1963  LLVM_FALLTHROUGH;
1964  case ABIArgInfo::Direct:
1965  if (RetAI.getInReg())
1966  RetAttrs.addAttribute(llvm::Attribute::InReg);
1967  break;
1968  case ABIArgInfo::Ignore:
1969  break;
1970 
1971  case ABIArgInfo::InAlloca:
1972  case ABIArgInfo::Indirect: {
1973  // inalloca and sret disable readnone and readonly
1974  FuncAttrs.removeAttribute(llvm::Attribute::ReadOnly)
1975  .removeAttribute(llvm::Attribute::ReadNone);
1976  break;
1977  }
1978 
1980  break;
1981 
1982  case ABIArgInfo::Expand:
1983  llvm_unreachable("Invalid ABI kind for return argument");
1984  }
1985 
1986  if (const auto *RefTy = RetTy->getAs<ReferenceType>()) {
1987  QualType PTy = RefTy->getPointeeType();
1988  if (!PTy->isIncompleteType() && PTy->isConstantSizeType())
1989  RetAttrs.addDereferenceableAttr(getContext().getTypeSizeInChars(PTy)
1990  .getQuantity());
1991  else if (getContext().getTargetAddressSpace(PTy) == 0 &&
1992  !CodeGenOpts.NullPointerIsValid)
1993  RetAttrs.addAttribute(llvm::Attribute::NonNull);
1994  }
1995 
1996  bool hasUsedSRet = false;
1997  SmallVector<llvm::AttributeSet, 4> ArgAttrs(IRFunctionArgs.totalIRArgs());
1998 
1999  // Attach attributes to sret.
2000  if (IRFunctionArgs.hasSRetArg()) {
2001  llvm::AttrBuilder SRETAttrs;
2002  SRETAttrs.addAttribute(llvm::Attribute::StructRet);
2003  hasUsedSRet = true;
2004  if (RetAI.getInReg())
2005  SRETAttrs.addAttribute(llvm::Attribute::InReg);
2006  ArgAttrs[IRFunctionArgs.getSRetArgNo()] =
2007  llvm::AttributeSet::get(getLLVMContext(), SRETAttrs);
2008  }
2009 
2010  // Attach attributes to inalloca argument.
2011  if (IRFunctionArgs.hasInallocaArg()) {
2012  llvm::AttrBuilder Attrs;
2013  Attrs.addAttribute(llvm::Attribute::InAlloca);
2014  ArgAttrs[IRFunctionArgs.getInallocaArgNo()] =
2015  llvm::AttributeSet::get(getLLVMContext(), Attrs);
2016  }
2017 
2018  unsigned ArgNo = 0;
2020  E = FI.arg_end();
2021  I != E; ++I, ++ArgNo) {
2022  QualType ParamType = I->type;
2023  const ABIArgInfo &AI = I->info;
2024  llvm::AttrBuilder Attrs;
2025 
2026  // Add attribute for padding argument, if necessary.
2027  if (IRFunctionArgs.hasPaddingArg(ArgNo)) {
2028  if (AI.getPaddingInReg()) {
2029  ArgAttrs[IRFunctionArgs.getPaddingArgNo(ArgNo)] =
2030  llvm::AttributeSet::get(
2031  getLLVMContext(),
2032  llvm::AttrBuilder().addAttribute(llvm::Attribute::InReg));
2033  }
2034  }
2035 
2036  // 'restrict' -> 'noalias' is done in EmitFunctionProlog when we
2037  // have the corresponding parameter variable. It doesn't make
2038  // sense to do it here because parameters are so messed up.
2039  switch (AI.getKind()) {
2040  case ABIArgInfo::Extend:
2041  if (AI.isSignExt())
2042  Attrs.addAttribute(llvm::Attribute::SExt);
2043  else
2044  Attrs.addAttribute(llvm::Attribute::ZExt);
2045  LLVM_FALLTHROUGH;
2046  case ABIArgInfo::Direct:
2047  if (ArgNo == 0 && FI.isChainCall())
2048  Attrs.addAttribute(llvm::Attribute::Nest);
2049  else if (AI.getInReg())
2050  Attrs.addAttribute(llvm::Attribute::InReg);
2051  break;
2052 
2053  case ABIArgInfo::Indirect: {
2054  if (AI.getInReg())
2055  Attrs.addAttribute(llvm::Attribute::InReg);
2056 
2057  if (AI.getIndirectByVal())
2058  Attrs.addAttribute(llvm::Attribute::ByVal);
2059 
2060  CharUnits Align = AI.getIndirectAlign();
2061 
2062  // In a byval argument, it is important that the required
2063  // alignment of the type is honored, as LLVM might be creating a
2064  // *new* stack object, and needs to know what alignment to give
2065  // it. (Sometimes it can deduce a sensible alignment on its own,
2066  // but not if clang decides it must emit a packed struct, or the
2067  // user specifies increased alignment requirements.)
2068  //
2069  // This is different from indirect *not* byval, where the object
2070  // exists already, and the align attribute is purely
2071  // informative.
2072  assert(!Align.isZero());
2073 
2074  // For now, only add this when we have a byval argument.
2075  // TODO: be less lazy about updating test cases.
2076  if (AI.getIndirectByVal())
2077  Attrs.addAlignmentAttr(Align.getQuantity());
2078 
2079  // byval disables readnone and readonly.
2080  FuncAttrs.removeAttribute(llvm::Attribute::ReadOnly)
2081  .removeAttribute(llvm::Attribute::ReadNone);
2082  break;
2083  }
2084  case ABIArgInfo::Ignore:
2085  case ABIArgInfo::Expand:
2087  break;
2088 
2089  case ABIArgInfo::InAlloca:
2090  // inalloca disables readnone and readonly.
2091  FuncAttrs.removeAttribute(llvm::Attribute::ReadOnly)
2092  .removeAttribute(llvm::Attribute::ReadNone);
2093  continue;
2094  }
2095 
2096  if (const auto *RefTy = ParamType->getAs<ReferenceType>()) {
2097  QualType PTy = RefTy->getPointeeType();
2098  if (!PTy->isIncompleteType() && PTy->isConstantSizeType())
2099  Attrs.addDereferenceableAttr(getContext().getTypeSizeInChars(PTy)
2100  .getQuantity());
2101  else if (getContext().getTargetAddressSpace(PTy) == 0 &&
2102  !CodeGenOpts.NullPointerIsValid)
2103  Attrs.addAttribute(llvm::Attribute::NonNull);
2104  }
2105 
2106  switch (FI.getExtParameterInfo(ArgNo).getABI()) {
2108  break;
2109 
2111  // Add 'sret' if we haven't already used it for something, but
2112  // only if the result is void.
2113  if (!hasUsedSRet && RetTy->isVoidType()) {
2114  Attrs.addAttribute(llvm::Attribute::StructRet);
2115  hasUsedSRet = true;
2116  }
2117 
2118  // Add 'noalias' in either case.
2119  Attrs.addAttribute(llvm::Attribute::NoAlias);
2120 
2121  // Add 'dereferenceable' and 'alignment'.
2122  auto PTy = ParamType->getPointeeType();
2123  if (!PTy->isIncompleteType() && PTy->isConstantSizeType()) {
2124  auto info = getContext().getTypeInfoInChars(PTy);
2125  Attrs.addDereferenceableAttr(info.first.getQuantity());
2126  Attrs.addAttribute(llvm::Attribute::getWithAlignment(getLLVMContext(),
2127  info.second.getQuantity()));
2128  }
2129  break;
2130  }
2131 
2133  Attrs.addAttribute(llvm::Attribute::SwiftError);
2134  break;
2135 
2137  Attrs.addAttribute(llvm::Attribute::SwiftSelf);
2138  break;
2139  }
2140 
2141  if (FI.getExtParameterInfo(ArgNo).isNoEscape())
2142  Attrs.addAttribute(llvm::Attribute::NoCapture);
2143 
2144  if (Attrs.hasAttributes()) {
2145  unsigned FirstIRArg, NumIRArgs;
2146  std::tie(FirstIRArg, NumIRArgs) = IRFunctionArgs.getIRArgs(ArgNo);
2147  for (unsigned i = 0; i < NumIRArgs; i++)
2148  ArgAttrs[FirstIRArg + i] =
2149  llvm::AttributeSet::get(getLLVMContext(), Attrs);
2150  }
2151  }
2152  assert(ArgNo == FI.arg_size());
2153 
2154  AttrList = llvm::AttributeList::get(
2155  getLLVMContext(), llvm::AttributeSet::get(getLLVMContext(), FuncAttrs),
2156  llvm::AttributeSet::get(getLLVMContext(), RetAttrs), ArgAttrs);
2157 }
2158 
2159 /// An argument came in as a promoted argument; demote it back to its
2160 /// declared type.
2162  const VarDecl *var,
2163  llvm::Value *value) {
2164  llvm::Type *varType = CGF.ConvertType(var->getType());
2165 
2166  // This can happen with promotions that actually don't change the
2167  // underlying type, like the enum promotions.
2168  if (value->getType() == varType) return value;
2169 
2170  assert((varType->isIntegerTy() || varType->isFloatingPointTy())
2171  && "unexpected promotion type");
2172 
2173  if (isa<llvm::IntegerType>(varType))
2174  return CGF.Builder.CreateTrunc(value, varType, "arg.unpromote");
2175 
2176  return CGF.Builder.CreateFPCast(value, varType, "arg.unpromote");
2177 }
2178 
2179 /// Returns the attribute (either parameter attribute, or function
2180 /// attribute), which declares argument ArgNo to be non-null.
2181 static const NonNullAttr *getNonNullAttr(const Decl *FD, const ParmVarDecl *PVD,
2182  QualType ArgType, unsigned ArgNo) {
2183  // FIXME: __attribute__((nonnull)) can also be applied to:
2184  // - references to pointers, where the pointee is known to be
2185  // nonnull (apparently a Clang extension)
2186  // - transparent unions containing pointers
2187  // In the former case, LLVM IR cannot represent the constraint. In
2188  // the latter case, we have no guarantee that the transparent union
2189  // is in fact passed as a pointer.
2190  if (!ArgType->isAnyPointerType() && !ArgType->isBlockPointerType())
2191  return nullptr;
2192  // First, check attribute on parameter itself.
2193  if (PVD) {
2194  if (auto ParmNNAttr = PVD->getAttr<NonNullAttr>())
2195  return ParmNNAttr;
2196  }
2197  // Check function attributes.
2198  if (!FD)
2199  return nullptr;
2200  for (const auto *NNAttr : FD->specific_attrs<NonNullAttr>()) {
2201  if (NNAttr->isNonNull(ArgNo))
2202  return NNAttr;
2203  }
2204  return nullptr;
2205 }
2206 
2207 namespace {
2208  struct CopyBackSwiftError final : EHScopeStack::Cleanup {
2209  Address Temp;
2210  Address Arg;
2211  CopyBackSwiftError(Address temp, Address arg) : Temp(temp), Arg(arg) {}
2212  void Emit(CodeGenFunction &CGF, Flags flags) override {
2213  llvm::Value *errorValue = CGF.Builder.CreateLoad(Temp);
2214  CGF.Builder.CreateStore(errorValue, Arg);
2215  }
2216  };
2217 }
2218 
2220  llvm::Function *Fn,
2221  const FunctionArgList &Args) {
2222  if (CurCodeDecl && CurCodeDecl->hasAttr<NakedAttr>())
2223  // Naked functions don't have prologues.
2224  return;
2225 
2226  // If this is an implicit-return-zero function, go ahead and
2227  // initialize the return value. TODO: it might be nice to have
2228  // a more general mechanism for this that didn't require synthesized
2229  // return statements.
2230  if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurCodeDecl)) {
2231  if (FD->hasImplicitReturnZero()) {
2232  QualType RetTy = FD->getReturnType().getUnqualifiedType();
2233  llvm::Type* LLVMTy = CGM.getTypes().ConvertType(RetTy);
2234  llvm::Constant* Zero = llvm::Constant::getNullValue(LLVMTy);
2235  Builder.CreateStore(Zero, ReturnValue);
2236  }
2237  }
2238 
2239  // FIXME: We no longer need the types from FunctionArgList; lift up and
2240  // simplify.
2241 
2242  ClangToLLVMArgMapping IRFunctionArgs(CGM.getContext(), FI);
2243  // Flattened function arguments.
2245  FnArgs.reserve(IRFunctionArgs.totalIRArgs());
2246  for (auto &Arg : Fn->args()) {
2247  FnArgs.push_back(&Arg);
2248  }
2249  assert(FnArgs.size() == IRFunctionArgs.totalIRArgs());
2250 
2251  // If we're using inalloca, all the memory arguments are GEPs off of the last
2252  // parameter, which is a pointer to the complete memory area.
2253  Address ArgStruct = Address::invalid();
2254  if (IRFunctionArgs.hasInallocaArg()) {
2255  ArgStruct = Address(FnArgs[IRFunctionArgs.getInallocaArgNo()],
2256  FI.getArgStructAlignment());
2257 
2258  assert(ArgStruct.getType() == FI.getArgStruct()->getPointerTo());
2259  }
2260 
2261  // Name the struct return parameter.
2262  if (IRFunctionArgs.hasSRetArg()) {
2263  auto AI = cast<llvm::Argument>(FnArgs[IRFunctionArgs.getSRetArgNo()]);
2264  AI->setName("agg.result");
2265  AI->addAttr(llvm::Attribute::NoAlias);
2266  }
2267 
2268  // Track if we received the parameter as a pointer (indirect, byval, or
2269  // inalloca). If already have a pointer, EmitParmDecl doesn't need to copy it
2270  // into a local alloca for us.
2272  ArgVals.reserve(Args.size());
2273 
2274  // Create a pointer value for every parameter declaration. This usually
2275  // entails copying one or more LLVM IR arguments into an alloca. Don't push
2276  // any cleanups or do anything that might unwind. We do that separately, so
2277  // we can push the cleanups in the correct order for the ABI.
2278  assert(FI.arg_size() == Args.size() &&
2279  "Mismatch between function signature & arguments.");
2280  unsigned ArgNo = 0;
2282  for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
2283  i != e; ++i, ++info_it, ++ArgNo) {
2284  const VarDecl *Arg = *i;
2285  const ABIArgInfo &ArgI = info_it->info;
2286 
2287  bool isPromoted =
2288  isa<ParmVarDecl>(Arg) && cast<ParmVarDecl>(Arg)->isKNRPromoted();
2289  // We are converting from ABIArgInfo type to VarDecl type directly, unless
2290  // the parameter is promoted. In this case we convert to
2291  // CGFunctionInfo::ArgInfo type with subsequent argument demotion.
2292  QualType Ty = isPromoted ? info_it->type : Arg->getType();
2293  assert(hasScalarEvaluationKind(Ty) ==
2294  hasScalarEvaluationKind(Arg->getType()));
2295 
2296  unsigned FirstIRArg, NumIRArgs;
2297  std::tie(FirstIRArg, NumIRArgs) = IRFunctionArgs.getIRArgs(ArgNo);
2298 
2299  switch (ArgI.getKind()) {
2300  case ABIArgInfo::InAlloca: {
2301  assert(NumIRArgs == 0);
2302  auto FieldIndex = ArgI.getInAllocaFieldIndex();
2303  Address V =
2304  Builder.CreateStructGEP(ArgStruct, FieldIndex, Arg->getName());
2305  ArgVals.push_back(ParamValue::forIndirect(V));
2306  break;
2307  }
2308 
2309  case ABIArgInfo::Indirect: {
2310  assert(NumIRArgs == 1);
2311  Address ParamAddr = Address(FnArgs[FirstIRArg], ArgI.getIndirectAlign());
2312 
2313  if (!hasScalarEvaluationKind(Ty)) {
2314  // Aggregates and complex variables are accessed by reference. All we
2315  // need to do is realign the value, if requested.
2316  Address V = ParamAddr;
2317  if (ArgI.getIndirectRealign()) {
2318  Address AlignedTemp = CreateMemTemp(Ty, "coerce");
2319 
2320  // Copy from the incoming argument pointer to the temporary with the
2321  // appropriate alignment.
2322  //
2323  // FIXME: We should have a common utility for generating an aggregate
2324  // copy.
2326  auto SizeVal = llvm::ConstantInt::get(IntPtrTy, Size.getQuantity());
2327  Address Dst = Builder.CreateBitCast(AlignedTemp, Int8PtrTy);
2328  Address Src = Builder.CreateBitCast(ParamAddr, Int8PtrTy);
2329  Builder.CreateMemCpy(Dst, Src, SizeVal, false);
2330  V = AlignedTemp;
2331  }
2332  ArgVals.push_back(ParamValue::forIndirect(V));
2333  } else {
2334  // Load scalar value from indirect argument.
2335  llvm::Value *V =
2336  EmitLoadOfScalar(ParamAddr, false, Ty, Arg->getBeginLoc());
2337 
2338  if (isPromoted)
2339  V = emitArgumentDemotion(*this, Arg, V);
2340  ArgVals.push_back(ParamValue::forDirect(V));
2341  }
2342  break;
2343  }
2344 
2345  case ABIArgInfo::Extend:
2346  case ABIArgInfo::Direct: {
2347 
2348  // If we have the trivial case, handle it with no muss and fuss.
2349  if (!isa<llvm::StructType>(ArgI.getCoerceToType()) &&
2350  ArgI.getCoerceToType() == ConvertType(Ty) &&
2351  ArgI.getDirectOffset() == 0) {
2352  assert(NumIRArgs == 1);
2353  llvm::Value *V = FnArgs[FirstIRArg];
2354  auto AI = cast<llvm::Argument>(V);
2355 
2356  if (const ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(Arg)) {
2357  if (getNonNullAttr(CurCodeDecl, PVD, PVD->getType(),
2358  PVD->getFunctionScopeIndex()) &&
2359  !CGM.getCodeGenOpts().NullPointerIsValid)
2360  AI->addAttr(llvm::Attribute::NonNull);
2361 
2362  QualType OTy = PVD->getOriginalType();
2363  if (const auto *ArrTy =
2364  getContext().getAsConstantArrayType(OTy)) {
2365  // A C99 array parameter declaration with the static keyword also
2366  // indicates dereferenceability, and if the size is constant we can
2367  // use the dereferenceable attribute (which requires the size in
2368  // bytes).
2369  if (ArrTy->getSizeModifier() == ArrayType::Static) {
2370  QualType ETy = ArrTy->getElementType();
2371  uint64_t ArrSize = ArrTy->getSize().getZExtValue();
2372  if (!ETy->isIncompleteType() && ETy->isConstantSizeType() &&
2373  ArrSize) {
2374  llvm::AttrBuilder Attrs;
2375  Attrs.addDereferenceableAttr(
2376  getContext().getTypeSizeInChars(ETy).getQuantity()*ArrSize);
2377  AI->addAttrs(Attrs);
2378  } else if (getContext().getTargetAddressSpace(ETy) == 0 &&
2379  !CGM.getCodeGenOpts().NullPointerIsValid) {
2380  AI->addAttr(llvm::Attribute::NonNull);
2381  }
2382  }
2383  } else if (const auto *ArrTy =
2384  getContext().getAsVariableArrayType(OTy)) {
2385  // For C99 VLAs with the static keyword, we don't know the size so
2386  // we can't use the dereferenceable attribute, but in addrspace(0)
2387  // we know that it must be nonnull.
2388  if (ArrTy->getSizeModifier() == VariableArrayType::Static &&
2389  !getContext().getTargetAddressSpace(ArrTy->getElementType()) &&
2390  !CGM.getCodeGenOpts().NullPointerIsValid)
2391  AI->addAttr(llvm::Attribute::NonNull);
2392  }
2393 
2394  const auto *AVAttr = PVD->getAttr<AlignValueAttr>();
2395  if (!AVAttr)
2396  if (const auto *TOTy = dyn_cast<TypedefType>(OTy))
2397  AVAttr = TOTy->getDecl()->getAttr<AlignValueAttr>();
2398  if (AVAttr && !SanOpts.has(SanitizerKind::Alignment)) {
2399  // If alignment-assumption sanitizer is enabled, we do *not* add
2400  // alignment attribute here, but emit normal alignment assumption,
2401  // so the UBSAN check could function.
2402  llvm::Value *AlignmentValue =
2403  EmitScalarExpr(AVAttr->getAlignment());
2404  llvm::ConstantInt *AlignmentCI =
2405  cast<llvm::ConstantInt>(AlignmentValue);
2406  unsigned Alignment = std::min((unsigned)AlignmentCI->getZExtValue(),
2407  +llvm::Value::MaximumAlignment);
2408  AI->addAttrs(llvm::AttrBuilder().addAlignmentAttr(Alignment));
2409  }
2410  }
2411 
2412  if (Arg->getType().isRestrictQualified())
2413  AI->addAttr(llvm::Attribute::NoAlias);
2414 
2415  // LLVM expects swifterror parameters to be used in very restricted
2416  // ways. Copy the value into a less-restricted temporary.
2417  if (FI.getExtParameterInfo(ArgNo).getABI()
2419  QualType pointeeTy = Ty->getPointeeType();
2420  assert(pointeeTy->isPointerType());
2421  Address temp =
2422  CreateMemTemp(pointeeTy, getPointerAlign(), "swifterror.temp");
2423  Address arg = Address(V, getContext().getTypeAlignInChars(pointeeTy));
2424  llvm::Value *incomingErrorValue = Builder.CreateLoad(arg);
2425  Builder.CreateStore(incomingErrorValue, temp);
2426  V = temp.getPointer();
2427 
2428  // Push a cleanup to copy the value back at the end of the function.
2429  // The convention does not guarantee that the value will be written
2430  // back if the function exits with an unwind exception.
2431  EHStack.pushCleanup<CopyBackSwiftError>(NormalCleanup, temp, arg);
2432  }
2433 
2434  // Ensure the argument is the correct type.
2435  if (V->getType() != ArgI.getCoerceToType())
2436  V = Builder.CreateBitCast(V, ArgI.getCoerceToType());
2437 
2438  if (isPromoted)
2439  V = emitArgumentDemotion(*this, Arg, V);
2440 
2441  // Because of merging of function types from multiple decls it is
2442  // possible for the type of an argument to not match the corresponding
2443  // type in the function type. Since we are codegening the callee
2444  // in here, add a cast to the argument type.
2445  llvm::Type *LTy = ConvertType(Arg->getType());
2446  if (V->getType() != LTy)
2447  V = Builder.CreateBitCast(V, LTy);
2448 
2449  ArgVals.push_back(ParamValue::forDirect(V));
2450  break;
2451  }
2452 
2453  Address Alloca = CreateMemTemp(Ty, getContext().getDeclAlign(Arg),
2454  Arg->getName());
2455 
2456  // Pointer to store into.
2457  Address Ptr = emitAddressAtOffset(*this, Alloca, ArgI);
2458 
2459  // Fast-isel and the optimizer generally like scalar values better than
2460  // FCAs, so we flatten them if this is safe to do for this argument.
2461  llvm::StructType *STy = dyn_cast<llvm::StructType>(ArgI.getCoerceToType());
2462  if (ArgI.isDirect() && ArgI.getCanBeFlattened() && STy &&
2463  STy->getNumElements() > 1) {
2464  uint64_t SrcSize = CGM.getDataLayout().getTypeAllocSize(STy);
2465  llvm::Type *DstTy = Ptr.getElementType();
2466  uint64_t DstSize = CGM.getDataLayout().getTypeAllocSize(DstTy);
2467 
2468  Address AddrToStoreInto = Address::invalid();
2469  if (SrcSize <= DstSize) {
2470  AddrToStoreInto = Builder.CreateElementBitCast(Ptr, STy);
2471  } else {
2472  AddrToStoreInto =
2473  CreateTempAlloca(STy, Alloca.getAlignment(), "coerce");
2474  }
2475 
2476  assert(STy->getNumElements() == NumIRArgs);
2477  for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
2478  auto AI = FnArgs[FirstIRArg + i];
2479  AI->setName(Arg->getName() + ".coerce" + Twine(i));
2480  Address EltPtr = Builder.CreateStructGEP(AddrToStoreInto, i);
2481  Builder.CreateStore(AI, EltPtr);
2482  }
2483 
2484  if (SrcSize > DstSize) {
2485  Builder.CreateMemCpy(Ptr, AddrToStoreInto, DstSize);
2486  }
2487 
2488  } else {
2489  // Simple case, just do a coerced store of the argument into the alloca.
2490  assert(NumIRArgs == 1);
2491  auto AI = FnArgs[FirstIRArg];
2492  AI->setName(Arg->getName() + ".coerce");
2493  CreateCoercedStore(AI, Ptr, /*DestIsVolatile=*/false, *this);
2494  }
2495 
2496  // Match to what EmitParmDecl is expecting for this type.
2498  llvm::Value *V =
2499  EmitLoadOfScalar(Alloca, false, Ty, Arg->getBeginLoc());
2500  if (isPromoted)
2501  V = emitArgumentDemotion(*this, Arg, V);
2502  ArgVals.push_back(ParamValue::forDirect(V));
2503  } else {
2504  ArgVals.push_back(ParamValue::forIndirect(Alloca));
2505  }
2506  break;
2507  }
2508 
2510  // Reconstruct into a temporary.
2511  Address alloca = CreateMemTemp(Ty, getContext().getDeclAlign(Arg));
2512  ArgVals.push_back(ParamValue::forIndirect(alloca));
2513 
2514  auto coercionType = ArgI.getCoerceAndExpandType();
2515  alloca = Builder.CreateElementBitCast(alloca, coercionType);
2516 
2517  unsigned argIndex = FirstIRArg;
2518  for (unsigned i = 0, e = coercionType->getNumElements(); i != e; ++i) {
2519  llvm::Type *eltType = coercionType->getElementType(i);
2521  continue;
2522 
2523  auto eltAddr = Builder.CreateStructGEP(alloca, i);
2524  auto elt = FnArgs[argIndex++];
2525  Builder.CreateStore(elt, eltAddr);
2526  }
2527  assert(argIndex == FirstIRArg + NumIRArgs);
2528  break;
2529  }
2530 
2531  case ABIArgInfo::Expand: {
2532  // If this structure was expanded into multiple arguments then
2533  // we need to create a temporary and reconstruct it from the
2534  // arguments.
2535  Address Alloca = CreateMemTemp(Ty, getContext().getDeclAlign(Arg));
2536  LValue LV = MakeAddrLValue(Alloca, Ty);
2537  ArgVals.push_back(ParamValue::forIndirect(Alloca));
2538 
2539  auto FnArgIter = FnArgs.begin() + FirstIRArg;
2540  ExpandTypeFromArgs(Ty, LV, FnArgIter);
2541  assert(FnArgIter == FnArgs.begin() + FirstIRArg + NumIRArgs);
2542  for (unsigned i = 0, e = NumIRArgs; i != e; ++i) {
2543  auto AI = FnArgs[FirstIRArg + i];
2544  AI->setName(Arg->getName() + "." + Twine(i));
2545  }
2546  break;
2547  }
2548 
2549  case ABIArgInfo::Ignore:
2550  assert(NumIRArgs == 0);
2551  // Initialize the local variable appropriately.
2552  if (!hasScalarEvaluationKind(Ty)) {
2553  ArgVals.push_back(ParamValue::forIndirect(CreateMemTemp(Ty)));
2554  } else {
2555  llvm::Value *U = llvm::UndefValue::get(ConvertType(Arg->getType()));
2556  ArgVals.push_back(ParamValue::forDirect(U));
2557  }
2558  break;
2559  }
2560  }
2561 
2562  if (getTarget().getCXXABI().areArgsDestroyedLeftToRightInCallee()) {
2563  for (int I = Args.size() - 1; I >= 0; --I)
2564  EmitParmDecl(*Args[I], ArgVals[I], I + 1);
2565  } else {
2566  for (unsigned I = 0, E = Args.size(); I != E; ++I)
2567  EmitParmDecl(*Args[I], ArgVals[I], I + 1);
2568  }
2569 }
2570 
2571 static void eraseUnusedBitCasts(llvm::Instruction *insn) {
2572  while (insn->use_empty()) {
2573  llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(insn);
2574  if (!bitcast) return;
2575 
2576  // This is "safe" because we would have used a ConstantExpr otherwise.
2577  insn = cast<llvm::Instruction>(bitcast->getOperand(0));
2578  bitcast->eraseFromParent();
2579  }
2580 }
2581 
2582 /// Try to emit a fused autorelease of a return result.
2584  llvm::Value *result) {
2585  // We must be immediately followed the cast.
2586  llvm::BasicBlock *BB = CGF.Builder.GetInsertBlock();
2587  if (BB->empty()) return nullptr;
2588  if (&BB->back() != result) return nullptr;
2589 
2590  llvm::Type *resultType = result->getType();
2591 
2592  // result is in a BasicBlock and is therefore an Instruction.
2593  llvm::Instruction *generator = cast<llvm::Instruction>(result);
2594 
2596 
2597  // Look for:
2598  // %generator = bitcast %type1* %generator2 to %type2*
2599  while (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(generator)) {
2600  // We would have emitted this as a constant if the operand weren't
2601  // an Instruction.
2602  generator = cast<llvm::Instruction>(bitcast->getOperand(0));
2603 
2604  // Require the generator to be immediately followed by the cast.
2605  if (generator->getNextNode() != bitcast)
2606  return nullptr;
2607 
2608  InstsToKill.push_back(bitcast);
2609  }
2610 
2611  // Look for:
2612  // %generator = call i8* @objc_retain(i8* %originalResult)
2613  // or
2614  // %generator = call i8* @objc_retainAutoreleasedReturnValue(i8* %originalResult)
2615  llvm::CallInst *call = dyn_cast<llvm::CallInst>(generator);
2616  if (!call) return nullptr;
2617 
2618  bool doRetainAutorelease;
2619 
2620  if (call->getCalledValue() == CGF.CGM.getObjCEntrypoints().objc_retain) {
2621  doRetainAutorelease = true;
2622  } else if (call->getCalledValue() == CGF.CGM.getObjCEntrypoints()
2624  doRetainAutorelease = false;
2625 
2626  // If we emitted an assembly marker for this call (and the
2627  // ARCEntrypoints field should have been set if so), go looking
2628  // for that call. If we can't find it, we can't do this
2629  // optimization. But it should always be the immediately previous
2630  // instruction, unless we needed bitcasts around the call.
2632  llvm::Instruction *prev = call->getPrevNode();
2633  assert(prev);
2634  if (isa<llvm::BitCastInst>(prev)) {
2635  prev = prev->getPrevNode();
2636  assert(prev);
2637  }
2638  assert(isa<llvm::CallInst>(prev));
2639  assert(cast<llvm::CallInst>(prev)->getCalledValue() ==
2641  InstsToKill.push_back(prev);
2642  }
2643  } else {
2644  return nullptr;
2645  }
2646 
2647  result = call->getArgOperand(0);
2648  InstsToKill.push_back(call);
2649 
2650  // Keep killing bitcasts, for sanity. Note that we no longer care
2651  // about precise ordering as long as there's exactly one use.
2652  while (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(result)) {
2653  if (!bitcast->hasOneUse()) break;
2654  InstsToKill.push_back(bitcast);
2655  result = bitcast->getOperand(0);
2656  }
2657 
2658  // Delete all the unnecessary instructions, from latest to earliest.
2659  for (auto *I : InstsToKill)
2660  I->eraseFromParent();
2661 
2662  // Do the fused retain/autorelease if we were asked to.
2663  if (doRetainAutorelease)
2664  result = CGF.EmitARCRetainAutoreleaseReturnValue(result);
2665 
2666  // Cast back to the result type.
2667  return CGF.Builder.CreateBitCast(result, resultType);
2668 }
2669 
2670 /// If this is a +1 of the value of an immutable 'self', remove it.
2672  llvm::Value *result) {
2673  // This is only applicable to a method with an immutable 'self'.
2674  const ObjCMethodDecl *method =
2675  dyn_cast_or_null<ObjCMethodDecl>(CGF.CurCodeDecl);
2676  if (!method) return nullptr;
2677  const VarDecl *self = method->getSelfDecl();
2678  if (!self->getType().isConstQualified()) return nullptr;
2679 
2680  // Look for a retain call.
2681  llvm::CallInst *retainCall =
2682  dyn_cast<llvm::CallInst>(result->stripPointerCasts());
2683  if (!retainCall ||
2684  retainCall->getCalledValue() != CGF.CGM.getObjCEntrypoints().objc_retain)
2685  return nullptr;
2686 
2687  // Look for an ordinary load of 'self'.
2688  llvm::Value *retainedValue = retainCall->getArgOperand(0);
2689  llvm::LoadInst *load =
2690  dyn_cast<llvm::LoadInst>(retainedValue->stripPointerCasts());
2691  if (!load || load->isAtomic() || load->isVolatile() ||
2692  load->getPointerOperand() != CGF.GetAddrOfLocalVar(self).getPointer())
2693  return nullptr;
2694 
2695  // Okay! Burn it all down. This relies for correctness on the
2696  // assumption that the retain is emitted as part of the return and
2697  // that thereafter everything is used "linearly".
2698  llvm::Type *resultType = result->getType();
2699  eraseUnusedBitCasts(cast<llvm::Instruction>(result));
2700  assert(retainCall->use_empty());
2701  retainCall->eraseFromParent();
2702  eraseUnusedBitCasts(cast<llvm::Instruction>(retainedValue));
2703 
2704  return CGF.Builder.CreateBitCast(load, resultType);
2705 }
2706 
2707 /// Emit an ARC autorelease of the result of a function.
2708 ///
2709 /// \return the value to actually return from the function
2711  llvm::Value *result) {
2712  // If we're returning 'self', kill the initial retain. This is a
2713  // heuristic attempt to "encourage correctness" in the really unfortunate
2714  // case where we have a return of self during a dealloc and we desperately
2715  // need to avoid the possible autorelease.
2716  if (llvm::Value *self = tryRemoveRetainOfSelf(CGF, result))
2717  return self;
2718 
2719  // At -O0, try to emit a fused retain/autorelease.
2720  if (CGF.shouldUseFusedARCCalls())
2721  if (llvm::Value *fused = tryEmitFusedAutoreleaseOfResult(CGF, result))
2722  return fused;
2723 
2724  return CGF.EmitARCAutoreleaseReturnValue(result);
2725 }
2726 
2727 /// Heuristically search for a dominating store to the return-value slot.
2728 static llvm::StoreInst *findDominatingStoreToReturnValue(CodeGenFunction &CGF) {
2729  // Check if a User is a store which pointerOperand is the ReturnValue.
2730  // We are looking for stores to the ReturnValue, not for stores of the
2731  // ReturnValue to some other location.
2732  auto GetStoreIfValid = [&CGF](llvm::User *U) -> llvm::StoreInst * {
2733  auto *SI = dyn_cast<llvm::StoreInst>(U);
2734  if (!SI || SI->getPointerOperand() != CGF.ReturnValue.getPointer())
2735  return nullptr;
2736  // These aren't actually possible for non-coerced returns, and we
2737  // only care about non-coerced returns on this code path.
2738  assert(!SI->isAtomic() && !SI->isVolatile());
2739  return SI;
2740  };
2741  // If there are multiple uses of the return-value slot, just check
2742  // for something immediately preceding the IP. Sometimes this can
2743  // happen with how we generate implicit-returns; it can also happen
2744  // with noreturn cleanups.
2745  if (!CGF.ReturnValue.getPointer()->hasOneUse()) {
2746  llvm::BasicBlock *IP = CGF.Builder.GetInsertBlock();
2747  if (IP->empty()) return nullptr;
2748  llvm::Instruction *I = &IP->back();
2749 
2750  // Skip lifetime markers
2751  for (llvm::BasicBlock::reverse_iterator II = IP->rbegin(),
2752  IE = IP->rend();
2753  II != IE; ++II) {
2754  if (llvm::IntrinsicInst *Intrinsic =
2755  dyn_cast<llvm::IntrinsicInst>(&*II)) {
2756  if (Intrinsic->getIntrinsicID() == llvm::Intrinsic::lifetime_end) {
2757  const llvm::Value *CastAddr = Intrinsic->getArgOperand(1);
2758  ++II;
2759  if (II == IE)
2760  break;
2761  if (isa<llvm::BitCastInst>(&*II) && (CastAddr == &*II))
2762  continue;
2763  }
2764  }
2765  I = &*II;
2766  break;
2767  }
2768 
2769  return GetStoreIfValid(I);
2770  }
2771 
2772  llvm::StoreInst *store =
2773  GetStoreIfValid(CGF.ReturnValue.getPointer()->user_back());
2774  if (!store) return nullptr;
2775 
2776  // Now do a first-and-dirty dominance check: just walk up the
2777  // single-predecessors chain from the current insertion point.
2778  llvm::BasicBlock *StoreBB = store->getParent();
2779  llvm::BasicBlock *IP = CGF.Builder.GetInsertBlock();
2780  while (IP != StoreBB) {
2781  if (!(IP = IP->getSinglePredecessor()))
2782  return nullptr;
2783  }
2784 
2785  // Okay, the store's basic block dominates the insertion point; we
2786  // can do our thing.
2787  return store;
2788 }
2789 
2791  bool EmitRetDbgLoc,
2792  SourceLocation EndLoc) {
2793  if (FI.isNoReturn()) {
2794  // Noreturn functions don't return.
2795  EmitUnreachable(EndLoc);
2796  return;
2797  }
2798 
2799  if (CurCodeDecl && CurCodeDecl->hasAttr<NakedAttr>()) {
2800  // Naked functions don't have epilogues.
2801  Builder.CreateUnreachable();
2802  return;
2803  }
2804 
2805  // Functions with no result always return void.
2806  if (!ReturnValue.isValid()) {
2807  Builder.CreateRetVoid();
2808  return;
2809  }
2810 
2811  llvm::DebugLoc RetDbgLoc;
2812  llvm::Value *RV = nullptr;
2813  QualType RetTy = FI.getReturnType();
2814  const ABIArgInfo &RetAI = FI.getReturnInfo();
2815 
2816  switch (RetAI.getKind()) {
2817  case ABIArgInfo::InAlloca:
2818  // Aggregrates get evaluated directly into the destination. Sometimes we
2819  // need to return the sret value in a register, though.
2820  assert(hasAggregateEvaluationKind(RetTy));
2821  if (RetAI.getInAllocaSRet()) {
2822  llvm::Function::arg_iterator EI = CurFn->arg_end();
2823  --EI;
2824  llvm::Value *ArgStruct = &*EI;
2825  llvm::Value *SRet = Builder.CreateStructGEP(
2826  nullptr, ArgStruct, RetAI.getInAllocaFieldIndex());
2827  RV = Builder.CreateAlignedLoad(SRet, getPointerAlign(), "sret");
2828  }
2829  break;
2830 
2831  case ABIArgInfo::Indirect: {
2832  auto AI = CurFn->arg_begin();
2833  if (RetAI.isSRetAfterThis())
2834  ++AI;
2835  switch (getEvaluationKind(RetTy)) {
2836  case TEK_Complex: {
2837  ComplexPairTy RT =
2838  EmitLoadOfComplex(MakeAddrLValue(ReturnValue, RetTy), EndLoc);
2839  EmitStoreOfComplex(RT, MakeNaturalAlignAddrLValue(&*AI, RetTy),
2840  /*isInit*/ true);
2841  break;
2842  }
2843  case TEK_Aggregate:
2844  // Do nothing; aggregrates get evaluated directly into the destination.
2845  break;
2846  case TEK_Scalar:
2847  EmitStoreOfScalar(Builder.CreateLoad(ReturnValue),
2848  MakeNaturalAlignAddrLValue(&*AI, RetTy),
2849  /*isInit*/ true);
2850  break;
2851  }
2852  break;
2853  }
2854 
2855  case ABIArgInfo::Extend:
2856  case ABIArgInfo::Direct:
2857  if (RetAI.getCoerceToType() == ConvertType(RetTy) &&
2858  RetAI.getDirectOffset() == 0) {
2859  // The internal return value temp always will have pointer-to-return-type
2860  // type, just do a load.
2861 
2862  // If there is a dominating store to ReturnValue, we can elide
2863  // the load, zap the store, and usually zap the alloca.
2864  if (llvm::StoreInst *SI =
2866  // Reuse the debug location from the store unless there is
2867  // cleanup code to be emitted between the store and return
2868  // instruction.
2869  if (EmitRetDbgLoc && !AutoreleaseResult)
2870  RetDbgLoc = SI->getDebugLoc();
2871  // Get the stored value and nuke the now-dead store.
2872  RV = SI->getValueOperand();
2873  SI->eraseFromParent();
2874 
2875  // Otherwise, we have to do a simple load.
2876  } else {
2877  RV = Builder.CreateLoad(ReturnValue);
2878  }
2879  } else {
2880  // If the value is offset in memory, apply the offset now.
2881  Address V = emitAddressAtOffset(*this, ReturnValue, RetAI);
2882 
2883  RV = CreateCoercedLoad(V, RetAI.getCoerceToType(), *this);
2884  }
2885 
2886  // In ARC, end functions that return a retainable type with a call
2887  // to objc_autoreleaseReturnValue.
2888  if (AutoreleaseResult) {
2889 #ifndef NDEBUG
2890  // Type::isObjCRetainabletype has to be called on a QualType that hasn't
2891  // been stripped of the typedefs, so we cannot use RetTy here. Get the
2892  // original return type of FunctionDecl, CurCodeDecl, and BlockDecl from
2893  // CurCodeDecl or BlockInfo.
2894  QualType RT;
2895 
2896  if (auto *FD = dyn_cast<FunctionDecl>(CurCodeDecl))
2897  RT = FD->getReturnType();
2898  else if (auto *MD = dyn_cast<ObjCMethodDecl>(CurCodeDecl))
2899  RT = MD->getReturnType();
2900  else if (isa<BlockDecl>(CurCodeDecl))
2901  RT = BlockInfo->BlockExpression->getFunctionType()->getReturnType();
2902  else
2903  llvm_unreachable("Unexpected function/method type");
2904 
2905  assert(getLangOpts().ObjCAutoRefCount &&
2906  !FI.isReturnsRetained() &&
2907  RT->isObjCRetainableType());
2908 #endif
2909  RV = emitAutoreleaseOfResult(*this, RV);
2910  }
2911 
2912  break;
2913 
2914  case ABIArgInfo::Ignore:
2915  break;
2916 
2918  auto coercionType = RetAI.getCoerceAndExpandType();
2919 
2920  // Load all of the coerced elements out into results.
2922  Address addr = Builder.CreateElementBitCast(ReturnValue, coercionType);
2923  for (unsigned i = 0, e = coercionType->getNumElements(); i != e; ++i) {
2924  auto coercedEltType = coercionType->getElementType(i);
2925  if (ABIArgInfo::isPaddingForCoerceAndExpand(coercedEltType))
2926  continue;
2927 
2928  auto eltAddr = Builder.CreateStructGEP(addr, i);
2929  auto elt = Builder.CreateLoad(eltAddr);
2930  results.push_back(elt);
2931  }
2932 
2933  // If we have one result, it's the single direct result type.
2934  if (results.size() == 1) {
2935  RV = results[0];
2936 
2937  // Otherwise, we need to make a first-class aggregate.
2938  } else {
2939  // Construct a return type that lacks padding elements.
2940  llvm::Type *returnType = RetAI.getUnpaddedCoerceAndExpandType();
2941 
2942  RV = llvm::UndefValue::get(returnType);
2943  for (unsigned i = 0, e = results.size(); i != e; ++i) {
2944  RV = Builder.CreateInsertValue(RV, results[i], i);
2945  }
2946  }
2947  break;
2948  }
2949 
2950  case ABIArgInfo::Expand:
2951  llvm_unreachable("Invalid ABI kind for return argument");
2952  }
2953 
2954  llvm::Instruction *Ret;
2955  if (RV) {
2956  EmitReturnValueCheck(RV);
2957  Ret = Builder.CreateRet(RV);
2958  } else {
2959  Ret = Builder.CreateRetVoid();
2960  }
2961 
2962  if (RetDbgLoc)
2963  Ret->setDebugLoc(std::move(RetDbgLoc));
2964 }
2965 
2967  // A current decl may not be available when emitting vtable thunks.
2968  if (!CurCodeDecl)
2969  return;
2970 
2971  ReturnsNonNullAttr *RetNNAttr = nullptr;
2972  if (SanOpts.has(SanitizerKind::ReturnsNonnullAttribute))
2973  RetNNAttr = CurCodeDecl->getAttr<ReturnsNonNullAttr>();
2974 
2975  if (!RetNNAttr && !requiresReturnValueNullabilityCheck())
2976  return;
2977 
2978  // Prefer the returns_nonnull attribute if it's present.
2979  SourceLocation AttrLoc;
2980  SanitizerMask CheckKind;
2981  SanitizerHandler Handler;
2982  if (RetNNAttr) {
2983  assert(!requiresReturnValueNullabilityCheck() &&
2984  "Cannot check nullability and the nonnull attribute");
2985  AttrLoc = RetNNAttr->getLocation();
2986  CheckKind = SanitizerKind::ReturnsNonnullAttribute;
2987  Handler = SanitizerHandler::NonnullReturn;
2988  } else {
2989  if (auto *DD = dyn_cast<DeclaratorDecl>(CurCodeDecl))
2990  if (auto *TSI = DD->getTypeSourceInfo())
2991  if (auto FTL = TSI->getTypeLoc().castAs<FunctionTypeLoc>())
2992  AttrLoc = FTL.getReturnLoc().findNullabilityLoc();
2993  CheckKind = SanitizerKind::NullabilityReturn;
2994  Handler = SanitizerHandler::NullabilityReturn;
2995  }
2996 
2997  SanitizerScope SanScope(this);
2998 
2999  // Make sure the "return" source location is valid. If we're checking a
3000  // nullability annotation, make sure the preconditions for the check are met.
3001  llvm::BasicBlock *Check = createBasicBlock("nullcheck");
3002  llvm::BasicBlock *NoCheck = createBasicBlock("no.nullcheck");
3003  llvm::Value *SLocPtr = Builder.CreateLoad(ReturnLocation, "return.sloc.load");
3004  llvm::Value *CanNullCheck = Builder.CreateIsNotNull(SLocPtr);
3005  if (requiresReturnValueNullabilityCheck())
3006  CanNullCheck =
3007  Builder.CreateAnd(CanNullCheck, RetValNullabilityPrecondition);
3008  Builder.CreateCondBr(CanNullCheck, Check, NoCheck);
3009  EmitBlock(Check);
3010 
3011  // Now do the null check.
3012  llvm::Value *Cond = Builder.CreateIsNotNull(RV);
3013  llvm::Constant *StaticData[] = {EmitCheckSourceLocation(AttrLoc)};
3014  llvm::Value *DynamicData[] = {SLocPtr};
3015  EmitCheck(std::make_pair(Cond, CheckKind), Handler, StaticData, DynamicData);
3016 
3017  EmitBlock(NoCheck);
3018 
3019 #ifndef NDEBUG
3020  // The return location should not be used after the check has been emitted.
3021  ReturnLocation = Address::invalid();
3022 #endif
3023 }
3024 
3026  const CXXRecordDecl *RD = type->getAsCXXRecordDecl();
3027  return RD && ABI.getRecordArgABI(RD) == CGCXXABI::RAA_DirectInMemory;
3028 }
3029 
3031  QualType Ty) {
3032  // FIXME: Generate IR in one pass, rather than going back and fixing up these
3033  // placeholders.
3034  llvm::Type *IRTy = CGF.ConvertTypeForMem(Ty);
3035  llvm::Type *IRPtrTy = IRTy->getPointerTo();
3036  llvm::Value *Placeholder = llvm::UndefValue::get(IRPtrTy->getPointerTo());
3037 
3038  // FIXME: When we generate this IR in one pass, we shouldn't need
3039  // this win32-specific alignment hack.
3041  Placeholder = CGF.Builder.CreateAlignedLoad(IRPtrTy, Placeholder, Align);
3042 
3043  return AggValueSlot::forAddr(Address(Placeholder, Align),
3044  Ty.getQualifiers(),
3049 }
3050 
3052  const VarDecl *param,
3053  SourceLocation loc) {
3054  // StartFunction converted the ABI-lowered parameter(s) into a
3055  // local alloca. We need to turn that into an r-value suitable
3056  // for EmitCall.
3057  Address local = GetAddrOfLocalVar(param);
3058 
3059  QualType type = param->getType();
3060 
3061  if (isInAllocaArgument(CGM.getCXXABI(), type)) {
3062  CGM.ErrorUnsupported(param, "forwarded non-trivially copyable parameter");
3063  }
3064 
3065  // GetAddrOfLocalVar returns a pointer-to-pointer for references,
3066  // but the argument needs to be the original pointer.
3067  if (type->isReferenceType()) {
3068  args.add(RValue::get(Builder.CreateLoad(local)), type);
3069 
3070  // In ARC, move out of consumed arguments so that the release cleanup
3071  // entered by StartFunction doesn't cause an over-release. This isn't
3072  // optimal -O0 code generation, but it should get cleaned up when
3073  // optimization is enabled. This also assumes that delegate calls are
3074  // performed exactly once for a set of arguments, but that should be safe.
3075  } else if (getLangOpts().ObjCAutoRefCount &&
3076  param->hasAttr<NSConsumedAttr>() &&
3077  type->isObjCRetainableType()) {
3078  llvm::Value *ptr = Builder.CreateLoad(local);
3079  auto null =
3080  llvm::ConstantPointerNull::get(cast<llvm::PointerType>(ptr->getType()));
3081  Builder.CreateStore(null, local);
3082  args.add(RValue::get(ptr), type);
3083 
3084  // For the most part, we just need to load the alloca, except that
3085  // aggregate r-values are actually pointers to temporaries.
3086  } else {
3087  args.add(convertTempToRValue(local, type, loc), type);
3088  }
3089 
3090  // Deactivate the cleanup for the callee-destructed param that was pushed.
3091  if (hasAggregateEvaluationKind(type) && !CurFuncIsThunk &&
3093  type.isDestructedType()) {
3095  CalleeDestructedParamCleanups.lookup(cast<ParmVarDecl>(param));
3096  assert(cleanup.isValid() &&
3097  "cleanup for callee-destructed param not recorded");
3098  // This unreachable is a temporary marker which will be removed later.
3099  llvm::Instruction *isActive = Builder.CreateUnreachable();
3100  args.addArgCleanupDeactivation(cleanup, isActive);
3101  }
3102 }
3103 
3104 static bool isProvablyNull(llvm::Value *addr) {
3105  return isa<llvm::ConstantPointerNull>(addr);
3106 }
3107 
3108 /// Emit the actual writing-back of a writeback.
3110  const CallArgList::Writeback &writeback) {
3111  const LValue &srcLV = writeback.Source;
3112  Address srcAddr = srcLV.getAddress();
3113  assert(!isProvablyNull(srcAddr.getPointer()) &&
3114  "shouldn't have writeback for provably null argument");
3115 
3116  llvm::BasicBlock *contBB = nullptr;
3117 
3118  // If the argument wasn't provably non-null, we need to null check
3119  // before doing the store.
3120  bool provablyNonNull = llvm::isKnownNonZero(srcAddr.getPointer(),
3121  CGF.CGM.getDataLayout());
3122  if (!provablyNonNull) {
3123  llvm::BasicBlock *writebackBB = CGF.createBasicBlock("icr.writeback");
3124  contBB = CGF.createBasicBlock("icr.done");
3125 
3126  llvm::Value *isNull =
3127  CGF.Builder.CreateIsNull(srcAddr.getPointer(), "icr.isnull");
3128  CGF.Builder.CreateCondBr(isNull, contBB, writebackBB);
3129  CGF.EmitBlock(writebackBB);
3130  }
3131 
3132  // Load the value to writeback.
3133  llvm::Value *value = CGF.Builder.CreateLoad(writeback.Temporary);
3134 
3135  // Cast it back, in case we're writing an id to a Foo* or something.
3136  value = CGF.Builder.CreateBitCast(value, srcAddr.getElementType(),
3137  "icr.writeback-cast");
3138 
3139  // Perform the writeback.
3140 
3141  // If we have a "to use" value, it's something we need to emit a use
3142  // of. This has to be carefully threaded in: if it's done after the
3143  // release it's potentially undefined behavior (and the optimizer
3144  // will ignore it), and if it happens before the retain then the
3145  // optimizer could move the release there.
3146  if (writeback.ToUse) {
3147  assert(srcLV.getObjCLifetime() == Qualifiers::OCL_Strong);
3148 
3149  // Retain the new value. No need to block-copy here: the block's
3150  // being passed up the stack.
3151  value = CGF.EmitARCRetainNonBlock(value);
3152 
3153  // Emit the intrinsic use here.
3154  CGF.EmitARCIntrinsicUse(writeback.ToUse);
3155 
3156  // Load the old value (primitively).
3157  llvm::Value *oldValue = CGF.EmitLoadOfScalar(srcLV, SourceLocation());
3158 
3159  // Put the new value in place (primitively).
3160  CGF.EmitStoreOfScalar(value, srcLV, /*init*/ false);
3161 
3162  // Release the old value.
3163  CGF.EmitARCRelease(oldValue, srcLV.isARCPreciseLifetime());
3164 
3165  // Otherwise, we can just do a normal lvalue store.
3166  } else {
3167  CGF.EmitStoreThroughLValue(RValue::get(value), srcLV);
3168  }
3169 
3170  // Jump to the continuation block.
3171  if (!provablyNonNull)
3172  CGF.EmitBlock(contBB);
3173 }
3174 
3176  const CallArgList &args) {
3177  for (const auto &I : args.writebacks())
3178  emitWriteback(CGF, I);
3179 }
3180 
3182  const CallArgList &CallArgs) {
3184  CallArgs.getCleanupsToDeactivate();
3185  // Iterate in reverse to increase the likelihood of popping the cleanup.
3186  for (const auto &I : llvm::reverse(Cleanups)) {
3187  CGF.DeactivateCleanupBlock(I.Cleanup, I.IsActiveIP);
3188  I.IsActiveIP->eraseFromParent();
3189  }
3190 }
3191 
3192 static const Expr *maybeGetUnaryAddrOfOperand(const Expr *E) {
3193  if (const UnaryOperator *uop = dyn_cast<UnaryOperator>(E->IgnoreParens()))
3194  if (uop->getOpcode() == UO_AddrOf)
3195  return uop->getSubExpr();
3196  return nullptr;
3197 }
3198 
3199 /// Emit an argument that's being passed call-by-writeback. That is,
3200 /// we are passing the address of an __autoreleased temporary; it
3201 /// might be copy-initialized with the current value of the given
3202 /// address, but it will definitely be copied out of after the call.
3204  const ObjCIndirectCopyRestoreExpr *CRE) {
3205  LValue srcLV;
3206 
3207  // Make an optimistic effort to emit the address as an l-value.
3208  // This can fail if the argument expression is more complicated.
3209  if (const Expr *lvExpr = maybeGetUnaryAddrOfOperand(CRE->getSubExpr())) {
3210  srcLV = CGF.EmitLValue(lvExpr);
3211 
3212  // Otherwise, just emit it as a scalar.
3213  } else {
3214  Address srcAddr = CGF.EmitPointerWithAlignment(CRE->getSubExpr());
3215 
3216  QualType srcAddrType =
3217  CRE->getSubExpr()->getType()->castAs<PointerType>()->getPointeeType();
3218  srcLV = CGF.MakeAddrLValue(srcAddr, srcAddrType);
3219  }
3220  Address srcAddr = srcLV.getAddress();
3221 
3222  // The dest and src types don't necessarily match in LLVM terms
3223  // because of the crazy ObjC compatibility rules.
3224 
3225  llvm::PointerType *destType =
3226  cast<llvm::PointerType>(CGF.ConvertType(CRE->getType()));
3227 
3228  // If the address is a constant null, just pass the appropriate null.
3229  if (isProvablyNull(srcAddr.getPointer())) {
3230  args.add(RValue::get(llvm::ConstantPointerNull::get(destType)),
3231  CRE->getType());
3232  return;
3233  }
3234 
3235  // Create the temporary.
3236  Address temp = CGF.CreateTempAlloca(destType->getElementType(),
3237  CGF.getPointerAlign(),
3238  "icr.temp");
3239  // Loading an l-value can introduce a cleanup if the l-value is __weak,
3240  // and that cleanup will be conditional if we can't prove that the l-value
3241  // isn't null, so we need to register a dominating point so that the cleanups
3242  // system will make valid IR.
3244 
3245  // Zero-initialize it if we're not doing a copy-initialization.
3246  bool shouldCopy = CRE->shouldCopy();
3247  if (!shouldCopy) {
3248  llvm::Value *null =
3249  llvm::ConstantPointerNull::get(
3250  cast<llvm::PointerType>(destType->getElementType()));
3251  CGF.Builder.CreateStore(null, temp);
3252  }
3253 
3254  llvm::BasicBlock *contBB = nullptr;
3255  llvm::BasicBlock *originBB = nullptr;
3256 
3257  // If the address is *not* known to be non-null, we need to switch.
3258  llvm::Value *finalArgument;
3259 
3260  bool provablyNonNull = llvm::isKnownNonZero(srcAddr.getPointer(),
3261  CGF.CGM.getDataLayout());
3262  if (provablyNonNull) {
3263  finalArgument = temp.getPointer();
3264  } else {
3265  llvm::Value *isNull =
3266  CGF.Builder.CreateIsNull(srcAddr.getPointer(), "icr.isnull");
3267 
3268  finalArgument = CGF.Builder.CreateSelect(isNull,
3269  llvm::ConstantPointerNull::get(destType),
3270  temp.getPointer(), "icr.argument");
3271 
3272  // If we need to copy, then the load has to be conditional, which
3273  // means we need control flow.
3274  if (shouldCopy) {
3275  originBB = CGF.Builder.GetInsertBlock();
3276  contBB = CGF.createBasicBlock("icr.cont");
3277  llvm::BasicBlock *copyBB = CGF.createBasicBlock("icr.copy");
3278  CGF.Builder.CreateCondBr(isNull, contBB, copyBB);
3279  CGF.EmitBlock(copyBB);
3280  condEval.begin(CGF);
3281  }
3282  }
3283 
3284  llvm::Value *valueToUse = nullptr;
3285 
3286  // Perform a copy if necessary.
3287  if (shouldCopy) {
3288  RValue srcRV = CGF.EmitLoadOfLValue(srcLV, SourceLocation());
3289  assert(srcRV.isScalar());
3290 
3291  llvm::Value *src = srcRV.getScalarVal();
3292  src = CGF.Builder.CreateBitCast(src, destType->getElementType(),
3293  "icr.cast");
3294 
3295  // Use an ordinary store, not a store-to-lvalue.
3296  CGF.Builder.CreateStore(src, temp);
3297 
3298  // If optimization is enabled, and the value was held in a
3299  // __strong variable, we need to tell the optimizer that this
3300  // value has to stay alive until we're doing the store back.
3301  // This is because the temporary is effectively unretained,
3302  // and so otherwise we can violate the high-level semantics.
3303  if (CGF.CGM.getCodeGenOpts().OptimizationLevel != 0 &&
3305  valueToUse = src;
3306  }
3307  }
3308 
3309  // Finish the control flow if we needed it.
3310  if (shouldCopy && !provablyNonNull) {
3311  llvm::BasicBlock *copyBB = CGF.Builder.GetInsertBlock();
3312  CGF.EmitBlock(contBB);
3313 
3314  // Make a phi for the value to intrinsically use.
3315  if (valueToUse) {
3316  llvm::PHINode *phiToUse = CGF.Builder.CreatePHI(valueToUse->getType(), 2,
3317  "icr.to-use");
3318  phiToUse->addIncoming(valueToUse, copyBB);
3319  phiToUse->addIncoming(llvm::UndefValue::get(valueToUse->getType()),
3320  originBB);
3321  valueToUse = phiToUse;
3322  }
3323 
3324  condEval.end(CGF);
3325  }
3326 
3327  args.addWriteback(srcLV, temp, valueToUse);
3328  args.add(RValue::get(finalArgument), CRE->getType());
3329 }
3330 
3332  assert(!StackBase);
3333 
3334  // Save the stack.
3335  llvm::Function *F = CGF.CGM.getIntrinsic(llvm::Intrinsic::stacksave);
3336  StackBase = CGF.Builder.CreateCall(F, {}, "inalloca.save");
3337 }
3338 
3340  if (StackBase) {
3341  // Restore the stack after the call.
3342  llvm::Function *F = CGF.CGM.getIntrinsic(llvm::Intrinsic::stackrestore);
3343  CGF.Builder.CreateCall(F, StackBase);
3344  }
3345 }
3346 
3348  SourceLocation ArgLoc,
3349  AbstractCallee AC,
3350  unsigned ParmNum) {
3351  if (!AC.getDecl() || !(SanOpts.has(SanitizerKind::NonnullAttribute) ||
3352  SanOpts.has(SanitizerKind::NullabilityArg)))
3353  return;
3354 
3355  // The param decl may be missing in a variadic function.
3356  auto PVD = ParmNum < AC.getNumParams() ? AC.getParamDecl(ParmNum) : nullptr;
3357  unsigned ArgNo = PVD ? PVD->getFunctionScopeIndex() : ParmNum;
3358 
3359  // Prefer the nonnull attribute if it's present.
3360  const NonNullAttr *NNAttr = nullptr;
3361  if (SanOpts.has(SanitizerKind::NonnullAttribute))
3362  NNAttr = getNonNullAttr(AC.getDecl(), PVD, ArgType, ArgNo);
3363 
3364  bool CanCheckNullability = false;
3365  if (SanOpts.has(SanitizerKind::NullabilityArg) && !NNAttr && PVD) {
3366  auto Nullability = PVD->getType()->getNullability(getContext());
3367  CanCheckNullability = Nullability &&
3369  PVD->getTypeSourceInfo();
3370  }
3371 
3372  if (!NNAttr && !CanCheckNullability)
3373  return;
3374 
3375  SourceLocation AttrLoc;
3376  SanitizerMask CheckKind;
3377  SanitizerHandler Handler;
3378  if (NNAttr) {
3379  AttrLoc = NNAttr->getLocation();
3380  CheckKind = SanitizerKind::NonnullAttribute;
3381  Handler = SanitizerHandler::NonnullArg;
3382  } else {
3383  AttrLoc = PVD->getTypeSourceInfo()->getTypeLoc().findNullabilityLoc();
3384  CheckKind = SanitizerKind::NullabilityArg;
3385  Handler = SanitizerHandler::NullabilityArg;
3386  }
3387 
3388  SanitizerScope SanScope(this);
3389  assert(RV.isScalar());
3390  llvm::Value *V = RV.getScalarVal();
3391  llvm::Value *Cond =
3392  Builder.CreateICmpNE(V, llvm::Constant::getNullValue(V->getType()));
3393  llvm::Constant *StaticData[] = {
3394  EmitCheckSourceLocation(ArgLoc), EmitCheckSourceLocation(AttrLoc),
3395  llvm::ConstantInt::get(Int32Ty, ArgNo + 1),
3396  };
3397  EmitCheck(std::make_pair(Cond, CheckKind), Handler, StaticData, None);
3398 }
3399 
3401  CallArgList &Args, ArrayRef<QualType> ArgTypes,
3402  llvm::iterator_range<CallExpr::const_arg_iterator> ArgRange,
3403  AbstractCallee AC, unsigned ParamsToSkip, EvaluationOrder Order) {
3404  assert((int)ArgTypes.size() == (ArgRange.end() - ArgRange.begin()));
3405 
3406  // We *have* to evaluate arguments from right to left in the MS C++ ABI,
3407  // because arguments are destroyed left to right in the callee. As a special
3408  // case, there are certain language constructs that require left-to-right
3409  // evaluation, and in those cases we consider the evaluation order requirement
3410  // to trump the "destruction order is reverse construction order" guarantee.
3411  bool LeftToRight =
3412  CGM.getTarget().getCXXABI().areArgsDestroyedLeftToRightInCallee()
3413  ? Order == EvaluationOrder::ForceLeftToRight
3414  : Order != EvaluationOrder::ForceRightToLeft;
3415 
3416  auto MaybeEmitImplicitObjectSize = [&](unsigned I, const Expr *Arg,
3417  RValue EmittedArg) {
3418  if (!AC.hasFunctionDecl() || I >= AC.getNumParams())
3419  return;
3420  auto *PS = AC.getParamDecl(I)->getAttr<PassObjectSizeAttr>();
3421  if (PS == nullptr)
3422  return;
3423 
3424  const auto &Context = getContext();
3425  auto SizeTy = Context.getSizeType();
3426  auto T = Builder.getIntNTy(Context.getTypeSize(SizeTy));
3427  assert(EmittedArg.getScalarVal() && "We emitted nothing for the arg?");
3428  llvm::Value *V = evaluateOrEmitBuiltinObjectSize(Arg, PS->getType(), T,
3429  EmittedArg.getScalarVal(),
3430  PS->isDynamic());
3431  Args.add(RValue::get(V), SizeTy);
3432  // If we're emitting args in reverse, be sure to do so with
3433  // pass_object_size, as well.
3434  if (!LeftToRight)
3435  std::swap(Args.back(), *(&Args.back() - 1));
3436  };
3437 
3438  // Insert a stack save if we're going to need any inalloca args.
3439  bool HasInAllocaArgs = false;
3440  if (CGM.getTarget().getCXXABI().isMicrosoft()) {
3441  for (ArrayRef<QualType>::iterator I = ArgTypes.begin(), E = ArgTypes.end();
3442  I != E && !HasInAllocaArgs; ++I)
3443  HasInAllocaArgs = isInAllocaArgument(CGM.getCXXABI(), *I);
3444  if (HasInAllocaArgs) {
3445  assert(getTarget().getTriple().getArch() == llvm::Triple::x86);
3446  Args.allocateArgumentMemory(*this);
3447  }
3448  }
3449 
3450  // Evaluate each argument in the appropriate order.
3451  size_t CallArgsStart = Args.size();
3452  for (unsigned I = 0, E = ArgTypes.size(); I != E; ++I) {
3453  unsigned Idx = LeftToRight ? I : E - I - 1;
3454  CallExpr::const_arg_iterator Arg = ArgRange.begin() + Idx;
3455  unsigned InitialArgSize = Args.size();
3456  // If *Arg is an ObjCIndirectCopyRestoreExpr, check that either the types of
3457  // the argument and parameter match or the objc method is parameterized.
3458  assert((!isa<ObjCIndirectCopyRestoreExpr>(*Arg) ||
3459  getContext().hasSameUnqualifiedType((*Arg)->getType(),
3460  ArgTypes[Idx]) ||
3461  (isa<ObjCMethodDecl>(AC.getDecl()) &&
3462  isObjCMethodWithTypeParams(cast<ObjCMethodDecl>(AC.getDecl())))) &&
3463  "Argument and parameter types don't match");
3464  EmitCallArg(Args, *Arg, ArgTypes[Idx]);
3465  // In particular, we depend on it being the last arg in Args, and the
3466  // objectsize bits depend on there only being one arg if !LeftToRight.
3467  assert(InitialArgSize + 1 == Args.size() &&
3468  "The code below depends on only adding one arg per EmitCallArg");
3469  (void)InitialArgSize;
3470  // Since pointer argument are never emitted as LValue, it is safe to emit
3471  // non-null argument check for r-value only.
3472  if (!Args.back().hasLValue()) {
3473  RValue RVArg = Args.back().getKnownRValue();
3474  EmitNonNullArgCheck(RVArg, ArgTypes[Idx], (*Arg)->getExprLoc(), AC,
3475  ParamsToSkip + Idx);
3476  // @llvm.objectsize should never have side-effects and shouldn't need
3477  // destruction/cleanups, so we can safely "emit" it after its arg,
3478  // regardless of right-to-leftness
3479  MaybeEmitImplicitObjectSize(Idx, *Arg, RVArg);
3480  }
3481  }
3482 
3483  if (!LeftToRight) {
3484  // Un-reverse the arguments we just evaluated so they match up with the LLVM
3485  // IR function.
3486  std::reverse(Args.begin() + CallArgsStart, Args.end());
3487  }
3488 }
3489 
3490 namespace {
3491 
3492 struct DestroyUnpassedArg final : EHScopeStack::Cleanup {
3493  DestroyUnpassedArg(Address Addr, QualType Ty)
3494  : Addr(Addr), Ty(Ty) {}
3495 
3496  Address Addr;
3497  QualType Ty;
3498 
3499  void Emit(CodeGenFunction &CGF, Flags flags) override {
3501  if (DtorKind == QualType::DK_cxx_destructor) {
3502  const CXXDestructorDecl *Dtor = Ty->getAsCXXRecordDecl()->getDestructor();
3503  assert(!Dtor->isTrivial());
3504  CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete, /*for vbase*/ false,
3505  /*Delegating=*/false, Addr);
3506  } else {
3507  CGF.callCStructDestructor(CGF.MakeAddrLValue(Addr, Ty));
3508  }
3509  }
3510 };
3511 
3512 struct DisableDebugLocationUpdates {
3513  CodeGenFunction &CGF;
3514  bool disabledDebugInfo;
3515  DisableDebugLocationUpdates(CodeGenFunction &CGF, const Expr *E) : CGF(CGF) {
3516  if ((disabledDebugInfo = isa<CXXDefaultArgExpr>(E) && CGF.getDebugInfo()))
3517  CGF.disableDebugInfo();
3518  }
3519  ~DisableDebugLocationUpdates() {
3520  if (disabledDebugInfo)
3521  CGF.enableDebugInfo();
3522  }
3523 };
3524 
3525 } // end anonymous namespace
3526 
3528  if (!HasLV)
3529  return RV;
3530  LValue Copy = CGF.MakeAddrLValue(CGF.CreateMemTemp(Ty), Ty);
3532  LV.isVolatile());
3533  IsUsed = true;
3534  return RValue::getAggregate(Copy.getAddress());
3535 }
3536 
3538  LValue Dst = CGF.MakeAddrLValue(Addr, Ty);
3539  if (!HasLV && RV.isScalar())
3540  CGF.EmitStoreOfScalar(RV.getScalarVal(), Dst, /*init=*/true);
3541  else if (!HasLV && RV.isComplex())
3542  CGF.EmitStoreOfComplex(RV.getComplexVal(), Dst, /*init=*/true);
3543  else {
3544  auto Addr = HasLV ? LV.getAddress() : RV.getAggregateAddress();
3545  LValue SrcLV = CGF.MakeAddrLValue(Addr, Ty);
3546  // We assume that call args are never copied into subobjects.
3547  CGF.EmitAggregateCopy(Dst, SrcLV, Ty, AggValueSlot::DoesNotOverlap,
3548  HasLV ? LV.isVolatileQualified()
3549  : RV.isVolatileQualified());
3550  }
3551  IsUsed = true;
3552 }
3553 
3555  QualType type) {
3556  DisableDebugLocationUpdates Dis(*this, E);
3557  if (const ObjCIndirectCopyRestoreExpr *CRE
3558  = dyn_cast<ObjCIndirectCopyRestoreExpr>(E)) {
3559  assert(getLangOpts().ObjCAutoRefCount);
3560  return emitWritebackArg(*this, args, CRE);
3561  }
3562 
3563  assert(type->isReferenceType() == E->isGLValue() &&
3564  "reference binding to unmaterialized r-value!");
3565 
3566  if (E->isGLValue()) {
3567  assert(E->getObjectKind() == OK_Ordinary);
3568  return args.add(EmitReferenceBindingToExpr(E), type);
3569  }
3570 
3571  bool HasAggregateEvalKind = hasAggregateEvaluationKind(type);
3572 
3573  // In the Microsoft C++ ABI, aggregate arguments are destructed by the callee.
3574  // However, we still have to push an EH-only cleanup in case we unwind before
3575  // we make it to the call.
3576  if (HasAggregateEvalKind &&
3578  // If we're using inalloca, use the argument memory. Otherwise, use a
3579  // temporary.
3580  AggValueSlot Slot;
3581  if (args.isUsingInAlloca())
3582  Slot = createPlaceholderSlot(*this, type);
3583  else
3584  Slot = CreateAggTemp(type, "agg.tmp");
3585 
3586  bool DestroyedInCallee = true, NeedsEHCleanup = true;
3587  if (const auto *RD = type->getAsCXXRecordDecl())
3588  DestroyedInCallee = RD->hasNonTrivialDestructor();
3589  else
3590  NeedsEHCleanup = needsEHCleanup(type.isDestructedType());
3591 
3592  if (DestroyedInCallee)
3593  Slot.setExternallyDestructed();
3594 
3595  EmitAggExpr(E, Slot);
3596  RValue RV = Slot.asRValue();
3597  args.add(RV, type);
3598 
3599  if (DestroyedInCallee && NeedsEHCleanup) {
3600  // Create a no-op GEP between the placeholder and the cleanup so we can
3601  // RAUW it successfully. It also serves as a marker of the first
3602  // instruction where the cleanup is active.
3603  pushFullExprCleanup<DestroyUnpassedArg>(EHCleanup, Slot.getAddress(),
3604  type);
3605  // This unreachable is a temporary marker which will be removed later.
3606  llvm::Instruction *IsActive = Builder.CreateUnreachable();
3607  args.addArgCleanupDeactivation(EHStack.getInnermostEHScope(), IsActive);
3608  }
3609  return;
3610  }
3611 
3612  if (HasAggregateEvalKind && isa<ImplicitCastExpr>(E) &&
3613  cast<CastExpr>(E)->getCastKind() == CK_LValueToRValue) {
3614  LValue L = EmitLValue(cast<CastExpr>(E)->getSubExpr());
3615  assert(L.isSimple());
3616  args.addUncopiedAggregate(L, type);
3617  return;
3618  }
3619 
3620  args.add(EmitAnyExprToTemp(E), type);
3621 }
3622 
3623 QualType CodeGenFunction::getVarArgType(const Expr *Arg) {
3624  // System headers on Windows define NULL to 0 instead of 0LL on Win64. MSVC
3625  // implicitly widens null pointer constants that are arguments to varargs
3626  // functions to pointer-sized ints.
3627  if (!getTarget().getTriple().isOSWindows())
3628  return Arg->getType();
3629 
3630  if (Arg->getType()->isIntegerType() &&
3631  getContext().getTypeSize(Arg->getType()) <
3635  return getContext().getIntPtrType();
3636  }
3637 
3638  return Arg->getType();
3639 }
3640 
3641 // In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC
3642 // optimizer it can aggressively ignore unwind edges.
3643 void
3644 CodeGenFunction::AddObjCARCExceptionMetadata(llvm::Instruction *Inst) {
3645  if (CGM.getCodeGenOpts().OptimizationLevel != 0 &&
3646  !CGM.getCodeGenOpts().ObjCAutoRefCountExceptions)
3647  Inst->setMetadata("clang.arc.no_objc_arc_exceptions",
3648  CGM.getNoObjCARCExceptionsMetadata());
3649 }
3650 
3651 /// Emits a call to the given no-arguments nounwind runtime function.
3652 llvm::CallInst *
3653 CodeGenFunction::EmitNounwindRuntimeCall(llvm::FunctionCallee callee,
3654  const llvm::Twine &name) {
3655  return EmitNounwindRuntimeCall(callee, None, name);
3656 }
3657 
3658 /// Emits a call to the given nounwind runtime function.
3659 llvm::CallInst *
3660 CodeGenFunction::EmitNounwindRuntimeCall(llvm::FunctionCallee callee,
3662  const llvm::Twine &name) {
3663  llvm::CallInst *call = EmitRuntimeCall(callee, args, name);
3664  call->setDoesNotThrow();
3665  return call;
3666 }
3667 
3668 /// Emits a simple call (never an invoke) to the given no-arguments
3669 /// runtime function.
3670 llvm::CallInst *CodeGenFunction::EmitRuntimeCall(llvm::FunctionCallee callee,
3671  const llvm::Twine &name) {
3672  return EmitRuntimeCall(callee, None, name);
3673 }
3674 
3675 // Calls which may throw must have operand bundles indicating which funclet
3676 // they are nested within.
3680  // There is no need for a funclet operand bundle if we aren't inside a
3681  // funclet.
3682  if (!CurrentFuncletPad)
3683  return BundleList;
3684 
3685  // Skip intrinsics which cannot throw.
3686  auto *CalleeFn = dyn_cast<llvm::Function>(Callee->stripPointerCasts());
3687  if (CalleeFn && CalleeFn->isIntrinsic() && CalleeFn->doesNotThrow())
3688  return BundleList;
3689 
3690  BundleList.emplace_back("funclet", CurrentFuncletPad);
3691  return BundleList;
3692 }
3693 
3694 /// Emits a simple call (never an invoke) to the given runtime function.
3695 llvm::CallInst *CodeGenFunction::EmitRuntimeCall(llvm::FunctionCallee callee,
3697  const llvm::Twine &name) {
3698  llvm::CallInst *call = Builder.CreateCall(
3699  callee, args, getBundlesForFunclet(callee.getCallee()), name);
3700  call->setCallingConv(getRuntimeCC());
3701  return call;
3702 }
3703 
3704 /// Emits a call or invoke to the given noreturn runtime function.
3706  llvm::FunctionCallee callee, ArrayRef<llvm::Value *> args) {
3708  getBundlesForFunclet(callee.getCallee());
3709 
3710  if (getInvokeDest()) {
3711  llvm::InvokeInst *invoke =
3712  Builder.CreateInvoke(callee,
3713  getUnreachableBlock(),
3714  getInvokeDest(),
3715  args,
3716  BundleList);
3717  invoke->setDoesNotReturn();
3718  invoke->setCallingConv(getRuntimeCC());
3719  } else {
3720  llvm::CallInst *call = Builder.CreateCall(callee, args, BundleList);
3721  call->setDoesNotReturn();
3722  call->setCallingConv(getRuntimeCC());
3723  Builder.CreateUnreachable();
3724  }
3725 }
3726 
3727 /// Emits a call or invoke instruction to the given nullary runtime function.
3728 llvm::CallBase *
3729 CodeGenFunction::EmitRuntimeCallOrInvoke(llvm::FunctionCallee callee,
3730  const Twine &name) {
3731  return EmitRuntimeCallOrInvoke(callee, None, name);
3732 }
3733 
3734 /// Emits a call or invoke instruction to the given runtime function.
3735 llvm::CallBase *
3736 CodeGenFunction::EmitRuntimeCallOrInvoke(llvm::FunctionCallee callee,
3738  const Twine &name) {
3739  llvm::CallBase *call = EmitCallOrInvoke(callee, args, name);
3740  call->setCallingConv(getRuntimeCC());
3741  return call;
3742 }
3743 
3744 /// Emits a call or invoke instruction to the given function, depending
3745 /// on the current state of the EH stack.
3746 llvm::CallBase *CodeGenFunction::EmitCallOrInvoke(llvm::FunctionCallee Callee,
3748  const Twine &Name) {
3749  llvm::BasicBlock *InvokeDest = getInvokeDest();
3751  getBundlesForFunclet(Callee.getCallee());
3752 
3753  llvm::CallBase *Inst;
3754  if (!InvokeDest)
3755  Inst = Builder.CreateCall(Callee, Args, BundleList, Name);
3756  else {
3757  llvm::BasicBlock *ContBB = createBasicBlock("invoke.cont");
3758  Inst = Builder.CreateInvoke(Callee, ContBB, InvokeDest, Args, BundleList,
3759  Name);
3760  EmitBlock(ContBB);
3761  }
3762 
3763  // In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC
3764  // optimizer it can aggressively ignore unwind edges.
3765  if (CGM.getLangOpts().ObjCAutoRefCount)
3766  AddObjCARCExceptionMetadata(Inst);
3767 
3768  return Inst;
3769 }
3770 
3771 void CodeGenFunction::deferPlaceholderReplacement(llvm::Instruction *Old,
3772  llvm::Value *New) {
3773  DeferredReplacements.push_back(std::make_pair(Old, New));
3774 }
3775 
3777  const CGCallee &Callee,
3778  ReturnValueSlot ReturnValue,
3779  const CallArgList &CallArgs,
3780  llvm::CallBase **callOrInvoke,
3781  SourceLocation Loc) {
3782  // FIXME: We no longer need the types from CallArgs; lift up and simplify.
3783 
3784  assert(Callee.isOrdinary() || Callee.isVirtual());
3785 
3786  // Handle struct-return functions by passing a pointer to the
3787  // location that we would like to return into.
3788  QualType RetTy = CallInfo.getReturnType();
3789  const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
3790 
3791  llvm::FunctionType *IRFuncTy = getTypes().GetFunctionType(CallInfo);
3792 
3793  const Decl *TargetDecl = Callee.getAbstractInfo().getCalleeDecl().getDecl();
3794 
3795 #ifndef NDEBUG
3796  if (!(CallInfo.isVariadic() && CallInfo.getArgStruct())) {
3797  // For an inalloca varargs function, we don't expect CallInfo to match the
3798  // function pointer's type, because the inalloca struct a will have extra
3799  // fields in it for the varargs parameters. Code later in this function
3800  // bitcasts the function pointer to the type derived from CallInfo.
3801  //
3802  // In other cases, we assert that the types match up (until pointers stop
3803  // having pointee types).
3804  llvm::Type *TypeFromVal;
3805  if (Callee.isVirtual())
3806  TypeFromVal = Callee.getVirtualFunctionType();
3807  else
3808  TypeFromVal =
3809  Callee.getFunctionPointer()->getType()->getPointerElementType();
3810  assert(IRFuncTy == TypeFromVal);
3811  }
3812 #endif
3813 
3814  // 1. Set up the arguments.
3815 
3816  // If we're using inalloca, insert the allocation after the stack save.
3817  // FIXME: Do this earlier rather than hacking it in here!
3818  Address ArgMemory = Address::invalid();
3819  if (llvm::StructType *ArgStruct = CallInfo.getArgStruct()) {
3820  const llvm::DataLayout &DL = CGM.getDataLayout();
3821  llvm::Instruction *IP = CallArgs.getStackBase();
3822  llvm::AllocaInst *AI;
3823  if (IP) {
3824  IP = IP->getNextNode();
3825  AI = new llvm::AllocaInst(ArgStruct, DL.getAllocaAddrSpace(),
3826  "argmem", IP);
3827  } else {
3828  AI = CreateTempAlloca(ArgStruct, "argmem");
3829  }
3830  auto Align = CallInfo.getArgStructAlignment();
3831  AI->setAlignment(Align.getQuantity());
3832  AI->setUsedWithInAlloca(true);
3833  assert(AI->isUsedWithInAlloca() && !AI->isStaticAlloca());
3834  ArgMemory = Address(AI, Align);
3835  }
3836 
3837  ClangToLLVMArgMapping IRFunctionArgs(CGM.getContext(), CallInfo);
3838  SmallVector<llvm::Value *, 16> IRCallArgs(IRFunctionArgs.totalIRArgs());
3839 
3840  // If the call returns a temporary with struct return, create a temporary
3841  // alloca to hold the result, unless one is given to us.
3842  Address SRetPtr = Address::invalid();
3843  Address SRetAlloca = Address::invalid();
3844  llvm::Value *UnusedReturnSizePtr = nullptr;
3845  if (RetAI.isIndirect() || RetAI.isInAlloca() || RetAI.isCoerceAndExpand()) {
3846  if (!ReturnValue.isNull()) {
3847  SRetPtr = ReturnValue.getValue();
3848  } else {
3849  SRetPtr = CreateMemTemp(RetTy, "tmp", &SRetAlloca);
3850  if (HaveInsertPoint() && ReturnValue.isUnused()) {
3851  uint64_t size =
3852  CGM.getDataLayout().getTypeAllocSize(ConvertTypeForMem(RetTy));
3853  UnusedReturnSizePtr = EmitLifetimeStart(size, SRetAlloca.getPointer());
3854  }
3855  }
3856  if (IRFunctionArgs.hasSRetArg()) {
3857  IRCallArgs[IRFunctionArgs.getSRetArgNo()] = SRetPtr.getPointer();
3858  } else if (RetAI.isInAlloca()) {
3859  Address Addr =
3860  Builder.CreateStructGEP(ArgMemory, RetAI.getInAllocaFieldIndex());
3861  Builder.CreateStore(SRetPtr.getPointer(), Addr);
3862  }
3863  }
3864 
3865  Address swiftErrorTemp = Address::invalid();
3866  Address swiftErrorArg = Address::invalid();
3867 
3868  // Translate all of the arguments as necessary to match the IR lowering.
3869  assert(CallInfo.arg_size() == CallArgs.size() &&
3870  "Mismatch between function signature & arguments.");
3871  unsigned ArgNo = 0;
3872  CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
3873  for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
3874  I != E; ++I, ++info_it, ++ArgNo) {
3875  const ABIArgInfo &ArgInfo = info_it->info;
3876 
3877  // Insert a padding argument to ensure proper alignment.
3878  if (IRFunctionArgs.hasPaddingArg(ArgNo))
3879  IRCallArgs[IRFunctionArgs.getPaddingArgNo(ArgNo)] =
3880  llvm::UndefValue::get(ArgInfo.getPaddingType());
3881 
3882  unsigned FirstIRArg, NumIRArgs;
3883  std::tie(FirstIRArg, NumIRArgs) = IRFunctionArgs.getIRArgs(ArgNo);
3884 
3885  switch (ArgInfo.getKind()) {
3886  case ABIArgInfo::InAlloca: {
3887  assert(NumIRArgs == 0);
3888  assert(getTarget().getTriple().getArch() == llvm::Triple::x86);
3889  if (I->isAggregate()) {
3890  // Replace the placeholder with the appropriate argument slot GEP.
3891  Address Addr = I->hasLValue()
3892  ? I->getKnownLValue().getAddress()
3893  : I->getKnownRValue().getAggregateAddress();
3894  llvm::Instruction *Placeholder =
3895  cast<llvm::Instruction>(Addr.getPointer());
3896  CGBuilderTy::InsertPoint IP = Builder.saveIP();
3897  Builder.SetInsertPoint(Placeholder);
3898  Addr =
3899  Builder.CreateStructGEP(ArgMemory, ArgInfo.getInAllocaFieldIndex());
3900  Builder.restoreIP(IP);
3901  deferPlaceholderReplacement(Placeholder, Addr.getPointer());
3902  } else {
3903  // Store the RValue into the argument struct.
3904  Address Addr =
3905  Builder.CreateStructGEP(ArgMemory, ArgInfo.getInAllocaFieldIndex());
3906  unsigned AS = Addr.getType()->getPointerAddressSpace();
3907  llvm::Type *MemType = ConvertTypeForMem(I->Ty)->getPointerTo(AS);
3908  // There are some cases where a trivial bitcast is not avoidable. The
3909  // definition of a type later in a translation unit may change it's type
3910  // from {}* to (%struct.foo*)*.
3911  if (Addr.getType() != MemType)
3912  Addr = Builder.CreateBitCast(Addr, MemType);
3913  I->copyInto(*this, Addr);
3914  }
3915  break;
3916  }
3917 
3918  case ABIArgInfo::Indirect: {
3919  assert(NumIRArgs == 1);
3920  if (!I->isAggregate()) {
3921  // Make a temporary alloca to pass the argument.
3922  Address Addr = CreateMemTempWithoutCast(
3923  I->Ty, ArgInfo.getIndirectAlign(), "indirect-arg-temp");
3924  IRCallArgs[FirstIRArg] = Addr.getPointer();
3925 
3926  I->copyInto(*this, Addr);
3927  } else {
3928  // We want to avoid creating an unnecessary temporary+copy here;
3929  // however, we need one in three cases:
3930  // 1. If the argument is not byval, and we are required to copy the
3931  // source. (This case doesn't occur on any common architecture.)
3932  // 2. If the argument is byval, RV is not sufficiently aligned, and
3933  // we cannot force it to be sufficiently aligned.
3934  // 3. If the argument is byval, but RV is not located in default
3935  // or alloca address space.
3936  Address Addr = I->hasLValue()
3937  ? I->getKnownLValue().getAddress()
3938  : I->getKnownRValue().getAggregateAddress();
3939  llvm::Value *V = Addr.getPointer();
3940  CharUnits Align = ArgInfo.getIndirectAlign();
3941  const llvm::DataLayout *TD = &CGM.getDataLayout();
3942 
3943  assert((FirstIRArg >= IRFuncTy->getNumParams() ||
3944  IRFuncTy->getParamType(FirstIRArg)->getPointerAddressSpace() ==
3945  TD->getAllocaAddrSpace()) &&
3946  "indirect argument must be in alloca address space");
3947 
3948  bool NeedCopy = false;
3949 
3950  if (Addr.getAlignment() < Align &&
3951  llvm::getOrEnforceKnownAlignment(V, Align.getQuantity(), *TD) <
3952  Align.getQuantity()) {
3953  NeedCopy = true;
3954  } else if (I->hasLValue()) {
3955  auto LV = I->getKnownLValue();
3956  auto AS = LV.getAddressSpace();
3957 
3958  if ((!ArgInfo.getIndirectByVal() &&
3959  (LV.getAlignment() >=
3960  getContext().getTypeAlignInChars(I->Ty)))) {
3961  NeedCopy = true;
3962  }
3963  if (!getLangOpts().OpenCL) {
3964  if ((ArgInfo.getIndirectByVal() &&
3965  (AS != LangAS::Default &&
3966  AS != CGM.getASTAllocaAddressSpace()))) {
3967  NeedCopy = true;
3968  }
3969  }
3970  // For OpenCL even if RV is located in default or alloca address space
3971  // we don't want to perform address space cast for it.
3972  else if ((ArgInfo.getIndirectByVal() &&
3973  Addr.getType()->getAddressSpace() != IRFuncTy->
3974  getParamType(FirstIRArg)->getPointerAddressSpace())) {
3975  NeedCopy = true;
3976  }
3977  }
3978 
3979  if (NeedCopy) {
3980  // Create an aligned temporary, and copy to it.
3981  Address AI = CreateMemTempWithoutCast(
3982  I->Ty, ArgInfo.getIndirectAlign(), "byval-temp");
3983  IRCallArgs[FirstIRArg] = AI.getPointer();
3984  I->copyInto(*this, AI);
3985  } else {
3986  // Skip the extra memcpy call.
3987  auto *T = V->getType()->getPointerElementType()->getPointerTo(
3988  CGM.getDataLayout().getAllocaAddrSpace());
3989  IRCallArgs[FirstIRArg] = getTargetHooks().performAddrSpaceCast(
3990  *this, V, LangAS::Default, CGM.getASTAllocaAddressSpace(), T,
3991  true);
3992  }
3993  }
3994  break;
3995  }
3996 
3997  case ABIArgInfo::Ignore:
3998  assert(NumIRArgs == 0);
3999  break;
4000 
4001  case ABIArgInfo::Extend:
4002  case ABIArgInfo::Direct: {
4003  if (!isa<llvm::StructType>(ArgInfo.getCoerceToType()) &&
4004  ArgInfo.getCoerceToType() == ConvertType(info_it->type) &&
4005  ArgInfo.getDirectOffset() == 0) {
4006  assert(NumIRArgs == 1);
4007  llvm::Value *V;
4008  if (!I->isAggregate())
4009  V = I->getKnownRValue().getScalarVal();
4010  else
4011  V = Builder.CreateLoad(
4012  I->hasLValue() ? I->getKnownLValue().getAddress()
4013  : I->getKnownRValue().getAggregateAddress());
4014 
4015  // Implement swifterror by copying into a new swifterror argument.
4016  // We'll write back in the normal path out of the call.
4017  if (CallInfo.getExtParameterInfo(ArgNo).getABI()
4019  assert(!swiftErrorTemp.isValid() && "multiple swifterror args");
4020 
4021  QualType pointeeTy = I->Ty->getPointeeType();
4022  swiftErrorArg =
4023  Address(V, getContext().getTypeAlignInChars(pointeeTy));
4024 
4025  swiftErrorTemp =
4026  CreateMemTemp(pointeeTy, getPointerAlign(), "swifterror.temp");
4027  V = swiftErrorTemp.getPointer();
4028  cast<llvm::AllocaInst>(V)->setSwiftError(true);
4029 
4030  llvm::Value *errorValue = Builder.CreateLoad(swiftErrorArg);
4031  Builder.CreateStore(errorValue, swiftErrorTemp);
4032  }
4033 
4034  // We might have to widen integers, but we should never truncate.
4035  if (ArgInfo.getCoerceToType() != V->getType() &&
4036  V->getType()->isIntegerTy())
4037  V = Builder.CreateZExt(V, ArgInfo.getCoerceToType());
4038 
4039  // If the argument doesn't match, perform a bitcast to coerce it. This
4040  // can happen due to trivial type mismatches.
4041  if (FirstIRArg < IRFuncTy->getNumParams() &&
4042  V->getType() != IRFuncTy->getParamType(FirstIRArg))
4043  V = Builder.CreateBitCast(V, IRFuncTy->getParamType(FirstIRArg));
4044 
4045  IRCallArgs[FirstIRArg] = V;
4046  break;
4047  }
4048 
4049  // FIXME: Avoid the conversion through memory if possible.
4050  Address Src = Address::invalid();
4051  if (!I->isAggregate()) {
4052  Src = CreateMemTemp(I->Ty, "coerce");
4053  I->copyInto(*this, Src);
4054  } else {
4055  Src = I->hasLValue() ? I->getKnownLValue().getAddress()
4056  : I->getKnownRValue().getAggregateAddress();
4057  }
4058 
4059  // If the value is offset in memory, apply the offset now.
4060  Src = emitAddressAtOffset(*this, Src, ArgInfo);
4061 
4062  // Fast-isel and the optimizer generally like scalar values better than
4063  // FCAs, so we flatten them if this is safe to do for this argument.
4064  llvm::StructType *STy =
4065  dyn_cast<llvm::StructType>(ArgInfo.getCoerceToType());
4066  if (STy && ArgInfo.isDirect() && ArgInfo.getCanBeFlattened()) {
4067  llvm::Type *SrcTy = Src.getType()->getElementType();
4068  uint64_t SrcSize = CGM.getDataLayout().getTypeAllocSize(SrcTy);
4069  uint64_t DstSize = CGM.getDataLayout().getTypeAllocSize(STy);
4070 
4071  // If the source type is smaller than the destination type of the
4072  // coerce-to logic, copy the source value into a temp alloca the size
4073  // of the destination type to allow loading all of it. The bits past
4074  // the source value are left undef.
4075  if (SrcSize < DstSize) {
4076  Address TempAlloca
4077  = CreateTempAlloca(STy, Src.getAlignment(),
4078  Src.getName() + ".coerce");
4079  Builder.CreateMemCpy(TempAlloca, Src, SrcSize);
4080  Src = TempAlloca;
4081  } else {
4082  Src = Builder.CreateBitCast(Src,
4083  STy->getPointerTo(Src.getAddressSpace()));
4084  }
4085 
4086  assert(NumIRArgs == STy->getNumElements());
4087  for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
4088  Address EltPtr = Builder.CreateStructGEP(Src, i);
4089  llvm::Value *LI = Builder.CreateLoad(EltPtr);
4090  IRCallArgs[FirstIRArg + i] = LI;
4091  }
4092  } else {
4093  // In the simple case, just pass the coerced loaded value.
4094  assert(NumIRArgs == 1);
4095  IRCallArgs[FirstIRArg] =
4096  CreateCoercedLoad(Src, ArgInfo.getCoerceToType(), *this);
4097  }
4098 
4099  break;
4100  }
4101 
4103  auto coercionType = ArgInfo.getCoerceAndExpandType();
4104  auto layout = CGM.getDataLayout().getStructLayout(coercionType);
4105 
4106  llvm::Value *tempSize = nullptr;
4107  Address addr = Address::invalid();
4108  Address AllocaAddr = Address::invalid();
4109  if (I->isAggregate()) {
4110  addr = I->hasLValue() ? I->getKnownLValue().getAddress()
4111  : I->getKnownRValue().getAggregateAddress();
4112 
4113  } else {
4114  RValue RV = I->getKnownRValue();
4115  assert(RV.isScalar()); // complex should always just be direct
4116 
4117  llvm::Type *scalarType = RV.getScalarVal()->getType();
4118  auto scalarSize = CGM.getDataLayout().getTypeAllocSize(scalarType);
4119  auto scalarAlign = CGM.getDataLayout().getPrefTypeAlignment(scalarType);
4120 
4121  // Materialize to a temporary.
4122  addr = CreateTempAlloca(RV.getScalarVal()->getType(),
4124  layout->getAlignment(), scalarAlign)),
4125  "tmp",
4126  /*ArraySize=*/nullptr, &AllocaAddr);
4127  tempSize = EmitLifetimeStart(scalarSize, AllocaAddr.getPointer());
4128 
4129  Builder.CreateStore(RV.getScalarVal(), addr);
4130  }
4131 
4132  addr = Builder.CreateElementBitCast(addr, coercionType);
4133 
4134  unsigned IRArgPos = FirstIRArg;
4135  for (unsigned i = 0, e = coercionType->getNumElements(); i != e; ++i) {
4136  llvm::Type *eltType = coercionType->getElementType(i);
4137  if (ABIArgInfo::isPaddingForCoerceAndExpand(eltType)) continue;
4138  Address eltAddr = Builder.CreateStructGEP(addr, i);
4139  llvm::Value *elt = Builder.CreateLoad(eltAddr);
4140  IRCallArgs[IRArgPos++] = elt;
4141  }
4142  assert(IRArgPos == FirstIRArg + NumIRArgs);
4143 
4144  if (tempSize) {
4145  EmitLifetimeEnd(tempSize, AllocaAddr.getPointer());
4146  }
4147 
4148  break;
4149  }
4150 
4151  case ABIArgInfo::Expand:
4152  unsigned IRArgPos = FirstIRArg;
4153  ExpandTypeToArgs(I->Ty, *I, IRFuncTy, IRCallArgs, IRArgPos);
4154  assert(IRArgPos == FirstIRArg + NumIRArgs);
4155  break;
4156  }
4157  }
4158 
4159  const CGCallee &ConcreteCallee = Callee.prepareConcreteCallee(*this);
4160  llvm::Value *CalleePtr = ConcreteCallee.getFunctionPointer();
4161 
4162  // If we're using inalloca, set up that argument.
4163  if (ArgMemory.isValid()) {
4164  llvm::Value *Arg = ArgMemory.getPointer();
4165  if (CallInfo.isVariadic()) {
4166  // When passing non-POD arguments by value to variadic functions, we will
4167  // end up with a variadic prototype and an inalloca call site. In such
4168  // cases, we can't do any parameter mismatch checks. Give up and bitcast
4169  // the callee.
4170  unsigned CalleeAS = CalleePtr->getType()->getPointerAddressSpace();
4171  CalleePtr =
4172  Builder.CreateBitCast(CalleePtr, IRFuncTy->getPointerTo(CalleeAS));
4173  } else {
4174  llvm::Type *LastParamTy =
4175  IRFuncTy->getParamType(IRFuncTy->getNumParams() - 1);
4176  if (Arg->getType() != LastParamTy) {
4177 #ifndef NDEBUG
4178  // Assert that these structs have equivalent element types.
4179  llvm::StructType *FullTy = CallInfo.getArgStruct();
4180  llvm::StructType *DeclaredTy = cast<llvm::StructType>(
4181  cast<llvm::PointerType>(LastParamTy)->getElementType());
4182  assert(DeclaredTy->getNumElements() == FullTy->getNumElements());
4183  for (llvm::StructType::element_iterator DI = DeclaredTy->element_begin(),
4184  DE = DeclaredTy->element_end(),
4185  FI = FullTy->element_begin();
4186  DI != DE; ++DI, ++FI)
4187  assert(*DI == *FI);
4188 #endif
4189  Arg = Builder.CreateBitCast(Arg, LastParamTy);
4190  }
4191  }
4192  assert(IRFunctionArgs.hasInallocaArg());
4193  IRCallArgs[IRFunctionArgs.getInallocaArgNo()] = Arg;
4194  }
4195 
4196  // 2. Prepare the function pointer.
4197 
4198  // If the callee is a bitcast of a non-variadic function to have a
4199  // variadic function pointer type, check to see if we can remove the
4200  // bitcast. This comes up with unprototyped functions.
4201  //
4202  // This makes the IR nicer, but more importantly it ensures that we
4203  // can inline the function at -O0 if it is marked always_inline.
4204  auto simplifyVariadicCallee = [](llvm::FunctionType *CalleeFT,
4205  llvm::Value *Ptr) -> llvm::Function * {
4206  if (!CalleeFT->isVarArg())
4207  return nullptr;
4208 
4209  // Get underlying value if it's a bitcast
4210  if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Ptr)) {
4211  if (CE->getOpcode() == llvm::Instruction::BitCast)
4212  Ptr = CE->getOperand(0);
4213  }
4214 
4215  llvm::Function *OrigFn = dyn_cast<llvm::Function>(Ptr);
4216  if (!OrigFn)
4217  return nullptr;
4218 
4219  llvm::FunctionType *OrigFT = OrigFn->getFunctionType();
4220 
4221  // If the original type is variadic, or if any of the component types
4222  // disagree, we cannot remove the cast.
4223  if (OrigFT->isVarArg() ||
4224  OrigFT->getNumParams() != CalleeFT->getNumParams() ||
4225  OrigFT->getReturnType() != CalleeFT->getReturnType())
4226  return nullptr;
4227 
4228  for (unsigned i = 0, e = OrigFT->getNumParams(); i != e; ++i)
4229  if (OrigFT->getParamType(i) != CalleeFT->getParamType(i))
4230  return nullptr;
4231 
4232  return OrigFn;
4233  };
4234 
4235  if (llvm::Function *OrigFn = simplifyVariadicCallee(IRFuncTy, CalleePtr)) {
4236  CalleePtr = OrigFn;
4237  IRFuncTy = OrigFn->getFunctionType();
4238  }
4239 
4240  // 3. Perform the actual call.
4241 
4242  // Deactivate any cleanups that we're supposed to do immediately before
4243  // the call.
4244  if (!CallArgs.getCleanupsToDeactivate().empty())
4245  deactivateArgCleanupsBeforeCall(*this, CallArgs);
4246 
4247  // Assert that the arguments we computed match up. The IR verifier
4248  // will catch this, but this is a common enough source of problems
4249  // during IRGen changes that it's way better for debugging to catch
4250  // it ourselves here.
4251 #ifndef NDEBUG
4252  assert(IRCallArgs.size() == IRFuncTy->getNumParams() || IRFuncTy->isVarArg());
4253  for (unsigned i = 0; i < IRCallArgs.size(); ++i) {
4254  // Inalloca argument can have different type.
4255  if (IRFunctionArgs.hasInallocaArg() &&
4256  i == IRFunctionArgs.getInallocaArgNo())
4257  continue;
4258  if (i < IRFuncTy->getNumParams())
4259  assert(IRCallArgs[i]->getType() == IRFuncTy->getParamType(i));
4260  }
4261 #endif
4262 
4263  // Update the largest vector width if any arguments have vector types.
4264  for (unsigned i = 0; i < IRCallArgs.size(); ++i) {
4265  if (auto *VT = dyn_cast<llvm::VectorType>(IRCallArgs[i]->getType()))
4266  LargestVectorWidth = std::max(LargestVectorWidth,
4267  VT->getPrimitiveSizeInBits());
4268  }
4269 
4270  // Compute the calling convention and attributes.
4271  unsigned CallingConv;
4272  llvm::AttributeList Attrs;
4273  CGM.ConstructAttributeList(CalleePtr->getName(), CallInfo,
4274  Callee.getAbstractInfo(), Attrs, CallingConv,
4275  /*AttrOnCallSite=*/true);
4276 
4277  // Apply some call-site-specific attributes.
4278  // TODO: work this into building the attribute set.
4279 
4280  // Apply always_inline to all calls within flatten functions.
4281  // FIXME: should this really take priority over __try, below?
4282  if (CurCodeDecl && CurCodeDecl->hasAttr<FlattenAttr>() &&
4283  !(TargetDecl && TargetDecl->hasAttr<NoInlineAttr>())) {
4284  Attrs =
4285  Attrs.addAttribute(getLLVMContext(), llvm::AttributeList::FunctionIndex,
4286  llvm::Attribute::AlwaysInline);
4287  }
4288 
4289  // Disable inlining inside SEH __try blocks.
4290  if (isSEHTryScope()) {
4291  Attrs =
4292  Attrs.addAttribute(getLLVMContext(), llvm::AttributeList::FunctionIndex,
4293  llvm::Attribute::NoInline);
4294  }
4295 
4296  // Decide whether to use a call or an invoke.
4297  bool CannotThrow;
4298  if (currentFunctionUsesSEHTry()) {
4299  // SEH cares about asynchronous exceptions, so everything can "throw."
4300  CannotThrow = false;
4301  } else if (isCleanupPadScope() &&
4303  // The MSVC++ personality will implicitly terminate the program if an
4304  // exception is thrown during a cleanup outside of a try/catch.
4305  // We don't need to model anything in IR to get this behavior.
4306  CannotThrow = true;
4307  } else {
4308  // Otherwise, nounwind call sites will never throw.
4309  CannotThrow = Attrs.hasAttribute(llvm::AttributeList::FunctionIndex,
4310  llvm::Attribute::NoUnwind);
4311  }
4312 
4313  // If we made a temporary, be sure to clean up after ourselves. Note that we
4314  // can't depend on being inside of an ExprWithCleanups, so we need to manually
4315  // pop this cleanup later on. Being eager about this is OK, since this
4316  // temporary is 'invisible' outside of the callee.
4317  if (UnusedReturnSizePtr)
4318  pushFullExprCleanup<CallLifetimeEnd>(NormalEHLifetimeMarker, SRetAlloca,
4319  UnusedReturnSizePtr);
4320 
4321  llvm::BasicBlock *InvokeDest = CannotThrow ? nullptr : getInvokeDest();
4322 
4324  getBundlesForFunclet(CalleePtr);
4325 
4326  // Emit the actual call/invoke instruction.
4327  llvm::CallBase *CI;
4328  if (!InvokeDest) {
4329  CI = Builder.CreateCall(IRFuncTy, CalleePtr, IRCallArgs, BundleList);
4330  } else {
4331  llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
4332  CI = Builder.CreateInvoke(IRFuncTy, CalleePtr, Cont, InvokeDest, IRCallArgs,
4333  BundleList);
4334  EmitBlock(Cont);
4335  }
4336  if (callOrInvoke)
4337  *callOrInvoke = CI;
4338 
4339  // Apply the attributes and calling convention.
4340  CI->setAttributes(Attrs);
4341  CI->setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
4342 
4343  // Apply various metadata.
4344 
4345  if (!CI->getType()->isVoidTy())
4346  CI->setName("call");
4347 
4348  // Update largest vector width from the return type.
4349  if (auto *VT = dyn_cast<llvm::VectorType>(CI->getType()))
4350  LargestVectorWidth = std::max(LargestVectorWidth,
4351  VT->getPrimitiveSizeInBits());
4352 
4353  // Insert instrumentation or attach profile metadata at indirect call sites.
4354  // For more details, see the comment before the definition of
4355  // IPVK_IndirectCallTarget in InstrProfData.inc.
4356  if (!CI->getCalledFunction())
4357  PGO.valueProfile(Builder, llvm::IPVK_IndirectCallTarget,
4358  CI, CalleePtr);
4359 
4360  // In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC
4361  // optimizer it can aggressively ignore unwind edges.
4362  if (CGM.getLangOpts().ObjCAutoRefCount)
4363  AddObjCARCExceptionMetadata(CI);
4364 
4365  // Suppress tail calls if requested.
4366  if (llvm::CallInst *Call = dyn_cast<llvm::CallInst>(CI)) {
4367  if (TargetDecl && TargetDecl->hasAttr<NotTailCalledAttr>())
4368  Call->setTailCallKind(llvm::CallInst::TCK_NoTail);
4369  }
4370 
4371  // Add metadata for calls to MSAllocator functions
4372  if (getDebugInfo() && TargetDecl &&
4373  TargetDecl->hasAttr<MSAllocatorAttr>())
4374  getDebugInfo()->addHeapAllocSiteMetadata(CI, RetTy, Loc);
4375 
4376  // 4. Finish the call.
4377 
4378  // If the call doesn't return, finish the basic block and clear the
4379  // insertion point; this allows the rest of IRGen to discard
4380  // unreachable code.
4381  if (CI->doesNotReturn()) {
4382  if (UnusedReturnSizePtr)
4383  PopCleanupBlock();
4384 
4385  // Strip away the noreturn attribute to better diagnose unreachable UB.
4386  if (SanOpts.has(SanitizerKind::Unreachable)) {
4387  // Also remove from function since CallBase::hasFnAttr additionally checks
4388  // attributes of the called function.
4389  if (auto *F = CI->getCalledFunction())
4390  F->removeFnAttr(llvm::Attribute::NoReturn);
4391  CI->removeAttribute(llvm::AttributeList::FunctionIndex,
4392  llvm::Attribute::NoReturn);
4393 
4394  // Avoid incompatibility with ASan which relies on the `noreturn`
4395  // attribute to insert handler calls.
4396  if (SanOpts.hasOneOf(SanitizerKind::Address |
4397  SanitizerKind::KernelAddress)) {
4398  SanitizerScope SanScope(this);
4399  llvm::IRBuilder<>::InsertPointGuard IPGuard(Builder);
4400  Builder.SetInsertPoint(CI);
4401  auto *FnType = llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false);
4402  llvm::FunctionCallee Fn =
4403  CGM.CreateRuntimeFunction(FnType, "__asan_handle_no_return");
4404  EmitNounwindRuntimeCall(Fn);
4405  }
4406  }
4407 
4408  EmitUnreachable(Loc);
4409  Builder.ClearInsertionPoint();
4410 
4411  // FIXME: For now, emit a dummy basic block because expr emitters in
4412  // generally are not ready to handle emitting expressions at unreachable
4413  // points.
4414  EnsureInsertPoint();
4415 
4416  // Return a reasonable RValue.
4417  return GetUndefRValue(RetTy);
4418  }
4419 
4420  // Perform the swifterror writeback.
4421  if (swiftErrorTemp.isValid()) {
4422  llvm::Value *errorResult = Builder.CreateLoad(swiftErrorTemp);
4423  Builder.CreateStore(errorResult, swiftErrorArg);
4424  }
4425 
4426  // Emit any call-associated writebacks immediately. Arguably this
4427  // should happen after any return-value munging.
4428  if (CallArgs.hasWritebacks())
4429  emitWritebacks(*this, CallArgs);
4430 
4431  // The stack cleanup for inalloca arguments has to run out of the normal
4432  // lexical order, so deactivate it and run it manually here.
4433  CallArgs.freeArgumentMemory(*this);
4434 
4435  // Extract the return value.
4436  RValue Ret = [&] {
4437  switch (RetAI.getKind()) {
4439  auto coercionType = RetAI.getCoerceAndExpandType();
4440 
4441  Address addr = SRetPtr;
4442  addr = Builder.CreateElementBitCast(addr, coercionType);
4443 
4444  assert(CI->getType() == RetAI.getUnpaddedCoerceAndExpandType());
4445  bool requiresExtract = isa<llvm::StructType>(CI->getType());
4446 
4447  unsigned unpaddedIndex = 0;
4448  for (unsigned i = 0, e = coercionType->getNumElements(); i != e; ++i) {
4449  llvm::Type *eltType = coercionType->getElementType(i);
4450  if (ABIArgInfo::isPaddingForCoerceAndExpand(eltType)) continue;
4451  Address eltAddr = Builder.CreateStructGEP(addr, i);
4452  llvm::Value *elt = CI;
4453  if (requiresExtract)
4454  elt = Builder.CreateExtractValue(elt, unpaddedIndex++);
4455  else
4456  assert(unpaddedIndex == 0);
4457  Builder.CreateStore(elt, eltAddr);
4458  }
4459  // FALLTHROUGH
4460  LLVM_FALLTHROUGH;
4461  }
4462 
4463  case ABIArgInfo::InAlloca:
4464  case ABIArgInfo::Indirect: {
4465  RValue ret = convertTempToRValue(SRetPtr, RetTy, SourceLocation());
4466  if (UnusedReturnSizePtr)
4467  PopCleanupBlock();
4468  return ret;
4469  }
4470 
4471  case ABIArgInfo::Ignore:
4472  // If we are ignoring an argument that had a result, make sure to
4473  // construct the appropriate return value for our caller.
4474  return GetUndefRValue(RetTy);
4475 
4476  case ABIArgInfo::Extend:
4477  case ABIArgInfo::Direct: {
4478  llvm::Type *RetIRTy = ConvertType(RetTy);
4479  if (RetAI.getCoerceToType() == RetIRTy && RetAI.getDirectOffset() == 0) {
4480  switch (getEvaluationKind(RetTy)) {
4481  case TEK_Complex: {
4482  llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
4483  llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
4484  return RValue::getComplex(std::make_pair(Real, Imag));
4485  }
4486  case TEK_Aggregate: {
4487  Address DestPtr = ReturnValue.getValue();
4488  bool DestIsVolatile = ReturnValue.isVolatile();
4489 
4490  if (!DestPtr.isValid()) {
4491  DestPtr = CreateMemTemp(RetTy, "agg.tmp");
4492  DestIsVolatile = false;
4493  }
4494  BuildAggStore(*this, CI, DestPtr, DestIsVolatile);
4495  return RValue::getAggregate(DestPtr);
4496  }
4497  case TEK_Scalar: {
4498  // If the argument doesn't match, perform a bitcast to coerce it. This
4499  // can happen due to trivial type mismatches.
4500  llvm::Value *V = CI;
4501  if (V->getType() != RetIRTy)
4502  V = Builder.CreateBitCast(V, RetIRTy);
4503  return RValue::get(V);
4504  }
4505  }
4506  llvm_unreachable("bad evaluation kind");
4507  }
4508 
4509  Address DestPtr = ReturnValue.getValue();
4510  bool DestIsVolatile = ReturnValue.isVolatile();
4511 
4512  if (!DestPtr.isValid()) {
4513  DestPtr = CreateMemTemp(RetTy, "coerce");
4514  DestIsVolatile = false;
4515  }
4516 
4517  // If the value is offset in memory, apply the offset now.
4518  Address StorePtr = emitAddressAtOffset(*this, DestPtr, RetAI);
4519  CreateCoercedStore(CI, StorePtr, DestIsVolatile, *this);
4520 
4521  return convertTempToRValue(DestPtr, RetTy, SourceLocation());
4522  }
4523 
4524  case ABIArgInfo::Expand:
4525  llvm_unreachable("Invalid ABI kind for return argument");
4526  }
4527 
4528  llvm_unreachable("Unhandled ABIArgInfo::Kind");
4529  } ();
4530 
4531  // Emit the assume_aligned check on the return value.
4532  if (Ret.isScalar() && TargetDecl) {
4533  if (const auto *AA = TargetDecl->getAttr<AssumeAlignedAttr>()) {
4534  llvm::Value *OffsetValue = nullptr;
4535  if (const auto *Offset = AA->getOffset())
4536  OffsetValue = EmitScalarExpr(Offset);
4537 
4538  llvm::Value *Alignment = EmitScalarExpr(AA->getAlignment());
4539  llvm::ConstantInt *AlignmentCI = cast<llvm::ConstantInt>(Alignment);
4540  EmitAlignmentAssumption(Ret.getScalarVal(), RetTy, Loc, AA->getLocation(),
4541  AlignmentCI->getZExtValue(), OffsetValue);
4542  } else if (const auto *AA = TargetDecl->getAttr<AllocAlignAttr>()) {
4543  llvm::Value *AlignmentVal = CallArgs[AA->getParamIndex().getLLVMIndex()]
4544  .getRValue(*this)
4545  .getScalarVal();
4546  EmitAlignmentAssumption(Ret.getScalarVal(), RetTy, Loc, AA->getLocation(),
4547  AlignmentVal);
4548  }
4549  }
4550 
4551  return Ret;
4552 }
4553 
4555  if (isVirtual()) {
4556  const CallExpr *CE = getVirtualCallExpr();
4558  CGF, getVirtualMethodDecl(), getThisAddress(), getVirtualFunctionType(),
4559  CE ? CE->getBeginLoc() : SourceLocation());
4560  }
4561 
4562  return *this;
4563 }
4564 
4565 /* VarArg handling */
4566 
4568  VAListAddr = VE->isMicrosoftABI()
4569  ? EmitMSVAListRef(VE->getSubExpr())
4570  : EmitVAListRef(VE->getSubExpr());
4571  QualType Ty = VE->getType();
4572  if (VE->isMicrosoftABI())
4573  return CGM.getTypes().getABIInfo().EmitMSVAArg(*this, VAListAddr, Ty);
4574  return CGM.getTypes().getABIInfo().EmitVAArg(*this, VAListAddr, Ty);
4575 }
const CGFunctionInfo & arrangeBuiltinFunctionDeclaration(QualType resultType, const FunctionArgList &args)
A builtin function is a freestanding function using the default C conventions.
Definition: CGCall.cpp:652
const llvm::DataLayout & getDataLayout() const
static CanQual< Type > CreateUnsafe(QualType Other)
Builds a canonical type from a QualType.
ObjCIndirectCopyRestoreExpr - Represents the passing of a function argument by indirect copy-restore ...
Definition: ExprObjC.h:1577
CGCXXABI & getCXXABI() const
Definition: CodeGenTypes.h:119
Ignore - Ignore the argument (treat as void).
ReturnValueSlot - Contains the address where the return value of a function can be stored...
Definition: CGCall.h:363
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
Address CreateStructGEP(Address Addr, unsigned Index, const llvm::Twine &Name="")
Definition: CGBuilder.h:178
QualType getAddrSpaceQualType(QualType T, LangAS AddressSpace) const
Return the uniqued reference to the type for an address space qualified type with the specified type ...
CanQualType DeriveThisType(const CXXRecordDecl *RD, const CXXMethodDecl *MD)
Derives the &#39;this&#39; type for codegen purposes, i.e.
Definition: CGCall.cpp:73
Represents a function declaration or definition.
Definition: Decl.h:1743
Address getAddress() const
Definition: CGValue.h:582
const CGFunctionInfo & arrangeBlockFunctionDeclaration(const FunctionProtoType *type, const FunctionArgList &args)
Block invocation functions are C functions with an implicit parameter.
Definition: CGCall.cpp:627
void EmitReturnValueCheck(llvm::Value *RV)
Emit a test that checks if the return value RV is nonnull.
Definition: CGCall.cpp:2966
PointerType - C99 6.7.5.1 - Pointer Declarators.
Definition: Type.h:2549
Complete object ctor.
Definition: ABI.h:25
CanQualType VoidPtrTy
Definition: ASTContext.h:1039
A (possibly-)qualified type.
Definition: Type.h:639
bool isBlockPointerType() const
Definition: Type.h:6353
bool ReturnTypeUsesSRet(const CGFunctionInfo &FI)
Return true iff the given type uses &#39;sret&#39; when used as a return type.
Definition: CGCall.cpp:1497
bool getNoCfCheck() const
Definition: Type.h:3527
llvm::Type * ConvertTypeForMem(QualType T)
const CodeGenOptions & getCodeGenOpts() const
bool isReturnsRetained() const
In ARC, whether this function retains its return value.
static void setCUDAKernelCallingConvention(CanQualType &FTy, CodeGenModule &CGM, const FunctionDecl *FD)
Set calling convention for CUDA/HIP kernel.
Definition: CGCall.cpp:264
static CanQual< FunctionProtoType > GetFormalType(const CXXMethodDecl *MD)
Returns the canonical formal type of the given C++ method.
Definition: CGCall.cpp:87
Address CreateMemTemp(QualType T, const Twine &Name="tmp", Address *Alloca=nullptr)
CreateMemTemp - Create a temporary memory object of the given type, with appropriate alignmen and cas...
Definition: CGExpr.cpp:138
static void emitWriteback(CodeGenFunction &CGF, const CallArgList::Writeback &writeback)
Emit the actual writing-back of a writeback.
Definition: CGCall.cpp:3109
static Address CreateTempAllocaForCoercion(CodeGenFunction &CGF, llvm::Type *Ty, CharUnits MinAlign)
Create a temporary allocation for the purposes of coercion.
Definition: CGCall.cpp:1113
static llvm::Value * emitAutoreleaseOfResult(CodeGenFunction &CGF, llvm::Value *result)
Emit an ARC autorelease of the result of a function.
Definition: CGCall.cpp:2710
static const CGFunctionInfo & arrangeFreeFunctionLikeCall(CodeGenTypes &CGT, CodeGenModule &CGM, const CallArgList &args, const FunctionType *fnType, unsigned numExtraRequiredArgs, bool chainCall)
Arrange a call as unto a free function, except possibly with an additional number of formal parameter...
Definition: CGCall.cpp:562
const ABIInfo & getABIInfo() const
Definition: CodeGenTypes.h:117
FunctionType - C99 6.7.5.3 - Function Declarators.
Definition: Type.h:3367
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee...
Definition: Type.cpp:505
const llvm::Triple & getTriple() const
Returns the target triple of the primary target.
Definition: TargetInfo.h:983
bool hasExtParameterInfos() const
Is there any interesting extra information for any of the parameters of this function type...
Definition: Type.h:4071
tooling::Replacements cleanup(const FormatStyle &Style, StringRef Code, ArrayRef< tooling::Range > Ranges, StringRef FileName="<stdin>")
Clean up any erroneous/redundant code in the given Ranges in Code.
Definition: Format.cpp:2324
unsigned getNumVBases() const
Retrieves the number of virtual base classes of this class.
Definition: DeclCXX.h:837
Extend - Valid only for integer argument types.
Address EmitPointerWithAlignment(const Expr *Addr, LValueBaseInfo *BaseInfo=nullptr, TBAAAccessInfo *TBAAInfo=nullptr)
EmitPointerWithAlignment - Given an expression with a pointer type, emit the value and compute our be...
Definition: CGExpr.cpp:1036
Address EmitVAArg(VAArgExpr *VE, Address &VAListAddr)
Generate code to get an argument from the passed in pointer and update it accordingly.
Definition: CGCall.cpp:4567
static bool isProvablyNull(llvm::Value *addr)
Definition: CGCall.cpp:3104
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:88
const CGFunctionInfo & arrangeCXXMethodType(const CXXRecordDecl *RD, const FunctionProtoType *FTP, const CXXMethodDecl *MD)
Arrange the argument and result information for a call to an unknown C++ non-static member function o...
Definition: CGCall.cpp:250
bool isVirtual() const
Definition: DeclCXX.h:2153
CGCallee prepareConcreteCallee(CodeGenFunction &CGF) const
If this is a delayed callee computation of some sort, prepare a concrete callee.
Definition: CGCall.cpp:4554
const Decl * CurCodeDecl
CurCodeDecl - This is the inner-most code context, which includes blocks.
Direct - Pass the argument directly using the normal converted LLVM type, or by coercing to another s...
const Expr * getSubExpr() const
Definition: Expr.h:4157
void addUncopiedAggregate(LValue LV, QualType type)
Definition: CGCall.h:289
bool isVolatile() const
Definition: CGValue.h:300
The base class of the type hierarchy.
Definition: Type.h:1414
void EmitStoreThroughLValue(RValue Src, LValue Dst, bool isInit=false)
EmitStoreThroughLValue - Store the specified rvalue into the specified lvalue, where both are guarant...
Definition: CGExpr.cpp:1924
CanQual< T > getUnqualifiedType() const
Retrieve the unqualified form of this type.
static const NonNullAttr * getNonNullAttr(const Decl *FD, const ParmVarDecl *PVD, QualType ArgType, unsigned ArgNo)
Returns the attribute (either parameter attribute, or function attribute), which declares argument Ar...
Definition: CGCall.cpp:2181
bool isRestrictQualified() const
Determine whether this type is restrict-qualified.
Definition: Type.h:6185
bool isZero() const
isZero - Test whether the quantity equals zero.
Definition: CharUnits.h:115
static int getExpansionSize(QualType Ty, const ASTContext &Context)
Definition: CGCall.cpp:959
const TargetInfo & getTargetInfo() const
Definition: ASTContext.h:690
const ParmVarDecl * getParamDecl(unsigned I) const
bool isFuncTypeConvertible(const FunctionType *FT)
isFuncTypeConvertible - Utility to check whether a function type can be converted to an LLVM type (i...
RangeSelector name(std::string ID)
Given a node with a "name", (like NamedDecl, DeclRefExpr or CxxCtorInitializer) selects the name&#39;s to...
llvm::Value * EmitARCRetainNonBlock(llvm::Value *value)
Retain the given object, with normal retain semantics.
Definition: CGObjC.cpp:2100
static llvm::SmallVector< FunctionProtoType::ExtParameterInfo, 16 > getExtParameterInfosForCall(const FunctionProtoType *proto, unsigned prefixArgs, unsigned totalArgs)
Definition: CGCall.cpp:371
llvm::IntegerType * Int8Ty
i8, i16, i32, and i64
Represents a C++ constructor within a class.
Definition: DeclCXX.h:2562
static llvm::Value * emitArgumentDemotion(CodeGenFunction &CGF, const VarDecl *var, llvm::Value *value)
An argument came in as a promoted argument; demote it back to its declared type.
Definition: CGCall.cpp:2161
bool hasWritebacks() const
Definition: CGCall.h:314
Default closure variant of a ctor.
Definition: ABI.h:29
Address GetAddrOfLocalVar(const VarDecl *VD)
GetAddrOfLocalVar - Return the address of a local variable.
CanQualType getCanonicalParamType(QualType T) const
Return the canonical parameter type corresponding to the specific potentially non-canonical one...
Represents a variable declaration or definition.
Definition: Decl.h:812
static void addExtParameterInfosForCall(llvm::SmallVectorImpl< FunctionProtoType::ExtParameterInfo > &paramInfos, const FunctionProtoType *proto, unsigned prefixArgs, unsigned totalArgs)
Definition: CGCall.cpp:112
llvm::Instruction * getStackBase() const
Definition: CGCall.h:336
unsigned getNumParams() const
Definition: Type.h:3900
RAII object to set/unset CodeGenFunction::IsSanitizerScope.
llvm::Value * getFunctionPointer() const
Definition: CGCall.h:183
static llvm::Value * CreateCoercedLoad(Address Src, llvm::Type *Ty, CodeGenFunction &CGF)
CreateCoercedLoad - Create a load from.
Definition: CGCall.cpp:1217
const T * getAs() const
Member-template getAs<specific type>&#39;.
Definition: Type.h:6805
void setCoerceToType(llvm::Type *T)
ExtInfo withProducesResult(bool producesResult) const
Definition: Type.h:3556
ObjCMethodDecl - Represents an instance or class method declaration.
Definition: DeclObjC.h:138
static const CGFunctionInfo & arrangeLLVMFunctionInfo(CodeGenTypes &CGT, bool instanceMethod, SmallVectorImpl< CanQualType > &prefix, CanQual< FunctionProtoType > FTP)
Arrange the LLVM function layout for a value of the given function type, on top of any implicit param...
Definition: CGCall.cpp:175
void EmitNonNullArgCheck(RValue RV, QualType ArgType, SourceLocation ArgLoc, AbstractCallee AC, unsigned ParmNum)
Create a check for a function parameter that may potentially be declared as non-null.
Definition: CGCall.cpp:3347
Address CreateConstInBoundsByteGEP(Address Addr, CharUnits Offset, const llvm::Twine &Name="")
Given a pointer to i8, adjust it by a given constant offset.
Definition: CGBuilder.h:244
llvm::Value * getPointer() const
Definition: Address.h:37
Address getValue() const
Definition: CGCall.h:383
llvm::Type * ConvertTypeForMem(QualType T)
ConvertTypeForMem - Convert type T into a llvm::Type.
const CGFunctionInfo & arrangeFreeFunctionType(CanQual< FunctionProtoType > Ty)
Arrange the argument and result information for a value of the given freestanding function type...
Definition: CGCall.cpp:193
Represents a parameter to a function.
Definition: Decl.h:1559
unsigned getAddressSpace() const
Return the address space that this address resides in.
Definition: Address.h:56
void add(RValue rvalue, QualType type)
Definition: CGCall.h:287
unsigned ClangCallConvToLLVMCallConv(CallingConv CC)
Convert clang calling convention to LLVM callilng convention.
Definition: CGCall.cpp:44
virtual unsigned getOpenCLKernelCallingConv() const
Get LLVM calling convention for OpenCL kernel.
Definition: TargetInfo.cpp:418
Represents a struct/union/class.
Definition: Decl.h:3598
void freeArgumentMemory(CodeGenFunction &CGF) const
Definition: CGCall.cpp:3339
uint64_t getPointerWidth(unsigned AddrSpace) const
Return the width of pointers on this target, for the specified address space.
Definition: TargetInfo.h:358
An object to manage conditionally-evaluated expressions.
Description of a constructor that was inherited from a base class.
Definition: DeclCXX.h:2536
bool usesInAlloca() const
Return true if this function uses inalloca arguments.
TargetCXXABI getCXXABI() const
Get the C++ ABI currently in use.
Definition: TargetInfo.h:1052
static void emitWritebacks(CodeGenFunction &CGF, const CallArgList &args)
Definition: CGCall.cpp:3175
void EmitFunctionEpilog(const CGFunctionInfo &FI, bool EmitRetDbgLoc, SourceLocation EndLoc)
EmitFunctionEpilog - Emit the target specific LLVM code to return the given temporary.
Definition: CGCall.cpp:2790
bool isNothrow(bool ResultIfDependent=false) const
Determine whether this function type has a non-throwing exception specification.
Definition: Type.h:4009
Address getAddress() const
Definition: CGValue.h:326
unsigned getRegParm() const
Definition: Type.h:3530
Indirect - Pass the argument indirectly via a hidden pointer with the specified alignment (0 indicate...
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
llvm::Type * ConvertType(QualType T)
ConvertType - Convert type T into a llvm::Type.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:154
ArrayRef< ExtParameterInfo > getExtParameterInfos() const
Definition: Type.h:4075
field_range fields() const
Definition: Decl.h:3789
bool isVolatileQualified() const
Definition: CGValue.h:257
llvm::Value * EmitARCRetainAutoreleaseReturnValue(llvm::Value *value)
Do a fused retain/autorelease of the given object.
Definition: CGObjC.cpp:2332
Represents a member of a struct/union/class.
Definition: Decl.h:2584
CharUnits getAlignment() const
Definition: CGValue.h:315
RequiredArgs getRequiredArgs() const
bool isUsingInAlloca() const
Returns if we&#39;re using an inalloca struct to pass arguments in memory.
Definition: CGCall.h:341
unsigned getFunctionScopeIndex() const
Returns the index of this parameter in its prototype or method scope.
Definition: Decl.h:1612
bool isOrdinary() const
Definition: CGCall.h:174
Qualifiers::ObjCLifetime getObjCLifetime() const
Definition: CGValue.h:265
CharUnits getArgStructAlignment() const
bool isReferenceType() const
Definition: Type.h:6357
Denotes a cleanup that should run when a scope is exited using exceptional control flow (a throw stat...
Definition: EHScopeStack.h:80
llvm::Value * EmitARCAutoreleaseReturnValue(llvm::Value *value)
Autorelease the given object.
Definition: CGObjC.cpp:2322
static void emitWritebackArg(CodeGenFunction &CGF, CallArgList &args, const ObjCIndirectCopyRestoreExpr *CRE)
Emit an argument that&#39;s being passed call-by-writeback.
Definition: CGCall.cpp:3203
static AggValueSlot forAddr(Address addr, Qualifiers quals, IsDestructed_t isDestructed, NeedsGCBarriers_t needsGC, IsAliased_t isAliased, Overlap_t mayOverlap, IsZeroed_t isZeroed=IsNotZeroed, IsSanitizerChecked_t isChecked=IsNotSanitizerChecked)
forAddr - Make a slot for an aggregate value.
Definition: CGValue.h:513
static CharUnits Zero()
Zero - Construct a CharUnits quantity of zero.
Definition: CharUnits.h:52
bool isVirtual() const
Definition: CGCall.h:192
static const EHPersonality & get(CodeGenModule &CGM, const FunctionDecl *FD)
SourceLocation getBeginLoc() const LLVM_READONLY
Definition: Decl.h:738
void EmitStoreOfScalar(llvm::Value *Value, Address Addr, bool Volatile, QualType Ty, AlignmentSource Source=AlignmentSource::Type, bool isInit=false, bool isNontemporal=false)
EmitStoreOfScalar - Store a scalar value to an address, taking care to appropriately convert from the...
void addArgCleanupDeactivation(EHScopeStack::stable_iterator Cleanup, llvm::Instruction *IsActiveIP)
Definition: CGCall.h:323
bool getProducesResult() const
Definition: Type.h:3525
bool isGLValue() const
Definition: Expr.h:261
ARCPreciseLifetime_t isARCPreciseLifetime() const
Definition: CGValue.h:284
This parameter (which must have pointer type) uses the special Swift context-pointer ABI treatment...
static bool hasScalarEvaluationKind(QualType T)
static llvm::Value * tryEmitFusedAutoreleaseOfResult(CodeGenFunction &CGF, llvm::Value *result)
Try to emit a fused autorelease of a return result.
Definition: CGCall.cpp:2583
void copyInto(CodeGenFunction &CGF, Address A) const
Definition: CGCall.cpp:3537
Address CreateElementBitCast(Address Addr, llvm::Type *Ty, const llvm::Twine &Name="")
Cast the element type of the given address to a different type, preserving information like the align...
Definition: CGBuilder.h:156
CharUnits - This is an opaque type for sizes expressed in character units.
Definition: CharUnits.h:37
llvm::StructType * getCoerceAndExpandType() const
llvm::CallInst * EmitRuntimeCall(llvm::FunctionCallee callee, const Twine &name="")
bool hasConstructorVariants() const
Does this ABI have different entrypoints for complete-object and base-subobject constructors?
Definition: TargetCXXABI.h:214
Wrapper for source info for functions.
Definition: TypeLoc.h:1362
CharUnits getAlignment() const
Return the alignment of this pointer.
Definition: Address.h:66
virtual bool hasMostDerivedReturn(GlobalDecl GD) const
Definition: CGCXXABI.h:108
unsigned getInAllocaFieldIndex() const
const_arg_iterator arg_begin() const
CXXCtorType getCtorType() const
Definition: GlobalDecl.h:78
const CGFunctionInfo & arrangeCXXConstructorCall(const CallArgList &Args, const CXXConstructorDecl *D, CXXCtorType CtorKind, unsigned ExtraPrefixArgs, unsigned ExtraSuffixArgs, bool PassProtoArgs=true)
Arrange a call to a C++ method, passing the given arguments.
Definition: CGCall.cpp:389
LangAS getAddressSpace() const
Definition: Type.h:353
void ConstructAttributeList(StringRef Name, const CGFunctionInfo &Info, CGCalleeInfo CalleeInfo, llvm::AttributeList &Attrs, unsigned &CallingConv, bool AttrOnCallSite)
Get the LLVM attributes and calling convention to use for a particular function type.
Definition: CGCall.cpp:1817
llvm::CallInst * CreateMemCpy(Address Dest, Address Src, llvm::Value *Size, bool IsVolatile=false)
Definition: CGBuilder.h:274
ABIArgInfo - Helper class to encapsulate information about how a specific C type should be passed to ...
static void appendParameterTypes(const CodeGenTypes &CGT, SmallVectorImpl< CanQualType > &prefix, SmallVectorImpl< FunctionProtoType::ExtParameterInfo > &paramInfos, CanQual< FunctionProtoType > FPT)
Adds the formal parameters in FPT to the given prefix.
Definition: CGCall.cpp:142
const CGFunctionInfo & arrangeObjCMessageSendSignature(const ObjCMethodDecl *MD, QualType receiverType)
Arrange the argument and result information for the function type through which to perform a send to ...
Definition: CGCall.cpp:470
const CGFunctionInfo & arrangeCall(const CGFunctionInfo &declFI, const CallArgList &args)
Given a function info for a declaration, return the function info for a call with the given arguments...
Definition: CGCall.cpp:701
Values of this type can never be null.
llvm::BasicBlock * createBasicBlock(const Twine &name="", llvm::Function *parent=nullptr, llvm::BasicBlock *before=nullptr)
createBasicBlock - Create an LLVM basic block.
Denotes a cleanup that should run when a scope is exited using normal control flow (falling off the e...
Definition: EHScopeStack.h:84
bool isSimple() const
Definition: CGValue.h:251
const CGFunctionInfo & arrangeCXXMethodDeclaration(const CXXMethodDecl *MD)
C++ methods have some special rules and also have implicit parameters.
Definition: CGCall.cpp:278
bool isInstance() const
Definition: DeclCXX.h:2136
An ordinary object is located at an address in memory.
Definition: Specifiers.h:133
CXXDestructorDecl * getDestructor() const
Returns the destructor decl for this class.
Definition: DeclCXX.cpp:1724
llvm::AllocaInst * CreateTempAlloca(llvm::Type *Ty, const Twine &Name="tmp", llvm::Value *ArraySize=nullptr)
CreateTempAlloca - This creates an alloca and inserts it into the entry block if ArraySize is nullptr...
Definition: CGExpr.cpp:105
FunctionType::ExtInfo getExtInfo() const
QualType getReturnType() const
Definition: DeclObjC.h:322
const CGFunctionInfo & arrangeLLVMFunctionInfo(CanQualType returnType, bool instanceMethod, bool chainCall, ArrayRef< CanQualType > argTypes, FunctionType::ExtInfo info, ArrayRef< FunctionProtoType::ExtParameterInfo > paramInfos, RequiredArgs args)
"Arrange" the LLVM information for a call or type with the given signature.
Definition: CGCall.cpp:736
bool getNoReturn() const
Definition: Type.h:3524
const T * getTypePtr() const
Retrieve the underlying type pointer, which refers to a canonical type.
Definition: CanonicalType.h:83
Address getAggregateAddress() const
getAggregateAddr() - Return the Value* of the address of the aggregate.
Definition: CGValue.h:70
bool getNoCallerSavedRegs() const
Definition: Type.h:3526
virtual AddedStructorArgs buildStructorSignature(GlobalDecl GD, SmallVectorImpl< CanQualType > &ArgTys)=0
Build the signature of the given constructor or destructor variant by adding any required parameters...
This parameter (which must have pointer-to-pointer type) uses the special Swift error-result ABI trea...
void EmitCallArg(CallArgList &args, const Expr *E, QualType ArgType)
EmitCallArg - Emit a single call argument.
Definition: CGCall.cpp:3554
CallingConv getDefaultCallingConvention(bool IsVariadic, bool IsCXXMethod, bool IsBuiltin=false) const
Retrieves the default calling convention for the current target.
const CGFunctionInfo & arrangeGlobalDeclaration(GlobalDecl GD)
Definition: CGCall.cpp:512
virtual void setCUDAKernelCallingConvention(const FunctionType *&FT) const
Definition: TargetInfo.h:312
ExtInfo withCallingConv(CallingConv cc) const
Definition: Type.h:3583
llvm::Value * EmitLoadOfScalar(Address Addr, bool Volatile, QualType Ty, SourceLocation Loc, AlignmentSource Source=AlignmentSource::Type, bool isNontemporal=false)
EmitLoadOfScalar - Load a scalar value from an address, taking care to appropriately convert from the...
const CGFunctionInfo & arrangeUnprototypedObjCMessageSend(QualType returnType, const CallArgList &args)
Definition: CGCall.cpp:501
Represents a K&R-style &#39;int foo()&#39; function, which has no information available about its arguments...
Definition: Type.h:3662
bool hasAttr() const
Definition: DeclBase.h:542
CanQualType getReturnType() const
bool isValid() const
Definition: Address.h:35
unsigned getNumRequiredArgs() const
std::pair< llvm::Value *, llvm::Value * > ComplexPairTy
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition: Type.cpp:1636
Represents a prototype with parameter type info, e.g.
Definition: Type.h:3699
bool isMicrosoftABI() const
Returns whether this is really a Win64 ABI va_arg expression.
Definition: Expr.h:4162
const TargetCodeGenInfo & getTargetCodeGenInfo()
llvm::Function * objc_retainAutoreleasedReturnValue
id objc_retainAutoreleasedReturnValue(id);
RValue - This trivial value class is used to represent the result of an expression that is evaluated...
Definition: CGValue.h:38
writeback_const_range writebacks() const
Definition: CGCall.h:319
void addWriteback(LValue srcLV, Address temporary, llvm::Value *toUse)
Definition: CGCall.h:308
void EmitDelegateCallArg(CallArgList &args, const VarDecl *param, SourceLocation loc)
EmitDelegateCallArg - We are performing a delegate call; that is, the current function is delegating ...
Definition: CGCall.cpp:3051
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
Definition: CharUnits.h:178
Represents a call to the builtin function __builtin_va_arg.
Definition: Expr.h:4139
Address Temporary
The temporary alloca.
Definition: CGCall.h:273
virtual bool HasThisReturn(GlobalDecl GD) const
Returns true if the given constructor or destructor is one of the kinds that the ABI says returns &#39;th...
Definition: CGCXXABI.h:106
unsigned Offset
Definition: Format.cpp:1709
llvm::Value * ToUse
A value to "use" after the writeback, or null.
Definition: CGCall.h:276
const CGFunctionInfo & arrangeCXXStructorDeclaration(GlobalDecl GD)
Definition: CGCall.cpp:305
ExtParameterInfo withIsNoEscape(bool NoEscape) const
Definition: Type.h:3433
static AggValueSlot createPlaceholderSlot(CodeGenFunction &CGF, QualType Ty)
Definition: CGCall.cpp:3030
This represents one expression.
Definition: Expr.h:108
static Address invalid()
Definition: Address.h:34
llvm::Type * getUnpaddedCoerceAndExpandType() const
static bool isInAllocaArgument(CGCXXABI &ABI, QualType type)
Definition: CGCall.cpp:3025
bool useObjCFPRetForRealType(RealType T) const
Check whether the given real type should use the "fpret" flavor of Objective-C message passing on thi...
Definition: TargetInfo.h:735
static CanQualType GetReturnType(QualType RetTy)
Returns the "extra-canonicalized" return type, which discards qualifiers on the return type...
Definition: CGCall.cpp:96
std::pair< llvm::Value *, llvm::Value * > getComplexVal() const
getComplexVal - Return the real/imag components of this complex value.
Definition: CGValue.h:65
void EmitCallArgs(CallArgList &Args, const T *CallArgTypeInfo, llvm::iterator_range< CallExpr::const_arg_iterator > ArgRange, AbstractCallee AC=AbstractCallee(), unsigned ParamsToSkip=0, EvaluationOrder Order=EvaluationOrder::Default)
EmitCallArgs - Emit call arguments for a function.
const CGFunctionInfo & arrangeNullaryFunction()
A nullary function is a freestanding function of type &#39;void ()&#39;.
Definition: CGCall.cpp:694
bool getHasRegParm() const
Definition: Type.h:3528
const T * castAs() const
Member-template castAs<specific type>.
Definition: Type.h:6870
bool isObjCRetainableType() const
Definition: Type.cpp:4027
#define V(N, I)
Definition: ASTContext.h:2906
Represents a C++ destructor within a class.
Definition: DeclCXX.h:2826
QualType getTagDeclType(const TagDecl *Decl) const
Return the unique reference to the type for the specified TagDecl (struct/union/class/enum) decl...
llvm::PointerType * getType() const
Return the type of the pointer value.
Definition: Address.h:43
CharUnits getTypeAlignInChars(QualType T) const
Return the ABI-specified alignment of a (complete) type T, in characters.
static CharUnits fromQuantity(QuantityType Quantity)
fromQuantity - Construct a CharUnits quantity from a raw integer type.
Definition: CharUnits.h:62
static bool isPaddingForCoerceAndExpand(llvm::Type *eltType)
static SmallVector< CanQualType, 16 > getArgTypesForCall(ASTContext &ctx, const CallArgList &args)
Definition: CGCall.cpp:355
static void eraseUnusedBitCasts(llvm::Instruction *insn)
Definition: CGCall.cpp:2571
SmallVector< llvm::OperandBundleDef, 1 > getBundlesForFunclet(llvm::Value *Callee)
Definition: CGCall.cpp:3678
A class for recording the number of arguments that a function signature requires. ...
bool ReturnSlotInterferesWithArgs(const CGFunctionInfo &FI)
Return true iff the given type uses an argument slot when &#39;sret&#39; is used as a return type...
Definition: CGCall.cpp:1502
const CGFunctionInfo & arrangeBuiltinFunctionCall(QualType resultType, const CallArgList &args)
Definition: CGCall.cpp:639
QualType getType() const
Definition: Expr.h:137
static Address emitAddressAtOffset(CodeGenFunction &CGF, Address addr, const ABIArgInfo &info)
Definition: CGCall.cpp:1343
const CGFunctionInfo & arrangeUnprototypedMustTailThunk(const CXXMethodDecl *MD)
Arrange a thunk that takes &#39;this&#39; as the first parameter followed by varargs.
Definition: CGCall.cpp:529
static llvm::StoreInst * findDominatingStoreToReturnValue(CodeGenFunction &CGF)
Heuristically search for a dominating store to the return-value slot.
Definition: CGCall.cpp:2728
CharUnits alignmentOfArrayElement(CharUnits elementSize) const
Given that this is the alignment of the first element of an array, return the minimum alignment of an...
Definition: CharUnits.h:196
void Profile(llvm::FoldingSetNodeID &ID)
UnaryOperator - This represents the unary-expression&#39;s (except sizeof and alignof), the postinc/postdec operators from postfix-expression, and various extensions.
Definition: Expr.h:1941
bool isTrivial() const
Whether this function is "trivial" in some specialized C++ senses.
Definition: Decl.h:2040
ASTContext & getContext() const
ImplicitParamDecl * getSelfDecl() const
Definition: DeclObjC.h:413
static llvm::Value * CoerceIntOrPtrToIntOrPtr(llvm::Value *Val, llvm::Type *Ty, CodeGenFunction &CGF)
CoerceIntOrPtrToIntOrPtr - Convert a value Val to the specific Ty where both are either integers or p...
Definition: CGCall.cpp:1163
CallingConv
CallingConv - Specifies the calling convention that a function uses.
Definition: Specifiers.h:243
GlobalDecl - represents a global declaration.
Definition: GlobalDecl.h:40
static void BuildAggStore(CodeGenFunction &CGF, llvm::Value *Val, Address Dest, bool DestIsVolatile)
Definition: CGCall.cpp:1269
ExprObjectKind getObjectKind() const
getObjectKind - The object kind that this expression produces.
Definition: Expr.h:421
CanQualType getCanonicalTypeUnqualified() const
LValue getKnownLValue() const
Definition: CGCall.h:242
The l-value was considered opaque, so the alignment was determined from a type.
RecordDecl * getDecl() const
Definition: Type.h:4427
unsigned getEffectiveCallingConvention() const
getEffectiveCallingConvention - Return the actual calling convention to use, which may depend on the ...
static void CreateCoercedStore(llvm::Value *Src, Address Dst, bool DstIsVolatile, CodeGenFunction &CGF)
CreateCoercedStore - Create a store to.
Definition: CGCall.cpp:1290
Enumerates target-specific builtins in their own namespaces within namespace clang.
Address CreateBitCast(Address Addr, llvm::Type *Ty, const llvm::Twine &Name="")
Definition: CGBuilder.h:141
Assigning into this object requires the old value to be released and the new value to be retained...
Definition: Type.h:165
Kind
bool ReturnTypeUsesFPRet(QualType ResultType)
Return true iff the given type uses &#39;fpret&#39; when used as a return type.
Definition: CGCall.cpp:1507
CanProxy< U > castAs() const
static const Expr * maybeGetUnaryAddrOfOperand(const Expr *E)
Definition: CGCall.cpp:3192
NullPointerConstantKind isNullPointerConstant(ASTContext &Ctx, NullPointerConstantValueDependence NPC) const
isNullPointerConstant - C99 6.3.2.3p3 - Test if this reduces down to a Null pointer constant...
Definition: Expr.cpp:3524
Encodes a location in the source.
QualType getReturnType() const
Definition: Type.h:3625
void EmitARCRelease(llvm::Value *value, ARCPreciseLifetime_t precise)
Release the given object.
Definition: CGObjC.cpp:2209
A saved depth on the scope stack.
Definition: EHScopeStack.h:106
llvm::FunctionType * getVirtualFunctionType() const
Definition: CGCall.h:207
RValue EmitCall(const CGFunctionInfo &CallInfo, const CGCallee &Callee, ReturnValueSlot ReturnValue, const CallArgList &Args, llvm::CallBase **callOrInvoke, SourceLocation Loc)
EmitCall - Generate a call of the given function, expecting the given result type, and using the given argument list which specifies both the LLVM arguments and the types they were derived from.
Definition: CGCall.cpp:3776
bool inheritingCtorHasParams(const InheritedConstructor &Inherited, CXXCtorType Type)
Determine if a C++ inheriting constructor should have parameters matching those of its inherited cons...
Definition: CGCall.cpp:295
ParameterABI getABI() const
Return the ABI treatment of this parameter.
Definition: Type.h:3406
void DeactivateCleanupBlock(EHScopeStack::stable_iterator Cleanup, llvm::Instruction *DominatingIP)
DeactivateCleanupBlock - Deactivates the given cleanup block.
Definition: CGCleanup.cpp:1239
CallingConv getCC() const
Definition: Type.h:3537
const Decl * getDecl() const
Definition: GlobalDecl.h:76
QualType getObjCSelType() const
Retrieve the type that corresponds to the predefined Objective-C &#39;SEL&#39; type.
Definition: ASTContext.h:1861
An aggregate value slot.
Definition: CGValue.h:436
virtual void computeInfo(CodeGen::CGFunctionInfo &FI) const =0
const CGFunctionInfo & arrangeObjCMethodDeclaration(const ObjCMethodDecl *MD)
Objective-C methods are C functions with some implicit parameters.
Definition: CGCall.cpp:457
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:2108
void computeABIInfo(CodeGenModule &CGM, CGFunctionInfo &FI)
Compute the ABI information of a swiftcall function.
const ConstantArrayType * getAsConstantArrayType(QualType T) const
Definition: ASTContext.h:2438
const_arg_iterator arg_end() const
llvm::StructType * getArgStruct() const
Get the struct type used to represent all the arguments in memory.
ObjCEntrypoints & getObjCEntrypoints() const
CoerceAndExpand - Only valid for aggregate argument types.
void allocateArgumentMemory(CodeGenFunction &CGF)
Definition: CGCall.cpp:3331
Specifies that a value-dependent expression should be considered to never be a null pointer constant...
Definition: Expr.h:734
CanQualType VoidTy
Definition: ASTContext.h:1011
llvm::InlineAsm * retainAutoreleasedReturnValueMarker
A void(void) inline asm to use to mark that the return value of a call will be immediately retain...
bool isAnyPointerType() const
Definition: Type.h:6349
An aligned address.
Definition: Address.h:24
DestructionKind isDestructedType() const
Returns a nonzero value if objects of this type require non-trivial work to clean up after...
Definition: Type.h:1159
bool useObjCFP2RetForComplexLongDouble() const
Check whether _Complex long double should use the "fp2ret" flavor of Objective-C message passing on t...
Definition: TargetInfo.h:741
llvm::LLVMContext & getLLVMContext()
Definition: CodeGenTypes.h:120
All available information about a concrete callee.
Definition: CGCall.h:66
static SmallVector< CanQualType, 16 > getArgTypesForDeclaration(ASTContext &ctx, const FunctionArgList &args)
Definition: CGCall.cpp:363
Complete object dtor.
Definition: ABI.h:35
InAlloca - Pass the argument directly using the LLVM inalloca attribute.
bool ReturnTypeUsesFP2Ret(QualType ResultType)
Return true iff the given type uses &#39;fp2ret&#39; when used as a return type.
Definition: CGCall.cpp:1524
static void AddAttributesFromFunctionProtoType(ASTContext &Ctx, llvm::AttrBuilder &FuncAttrs, const FunctionProtoType *FPT)
Definition: CGCall.cpp:1679
bool hasFlexibleArrayMember() const
Definition: Decl.h:3652
ExceptionSpecificationType getExceptionSpecType() const
Get the kind of exception specification on this function.
Definition: Type.h:3934
CXXCtorType
C++ constructor types.
Definition: ABI.h:24
CanProxy< U > getAs() const
Retrieve a canonical type pointer with a different static type, upcasting or downcasting as needed...
const CGFunctionInfo & arrangeBlockFunctionCall(const CallArgList &args, const FunctionType *type)
A block function is essentially a free function with an extra implicit argument.
Definition: CGCall.cpp:620
std::pair< CharUnits, CharUnits > getTypeInfoInChars(const Type *T) const
llvm::Type * getPaddingType() const
void setExternallyDestructed(bool destructed=true)
Definition: CGValue.h:553
static Address EnterStructPointerForCoercedAccess(Address SrcPtr, llvm::StructType *SrcSTy, uint64_t DstSize, CodeGenFunction &CGF)
EnterStructPointerForCoercedAccess - Given a struct pointer that we are accessing some number of byte...
Definition: CGCall.cpp:1127
FunctionArgList - Type for representing both the decl and type of parameters to a function...
Definition: CGCall.h:358
bool getInAllocaSRet() const
Return true if this field of an inalloca struct should be returned to implement a struct return calli...
llvm::Value * getScalarVal() const
getScalarVal() - Return the Value* of this scalar value.
Definition: CGValue.h:58
const TargetInfo & getTarget() const
Definition: CodeGenTypes.h:118
llvm::CallBase * EmitCallOrInvoke(llvm::FunctionCallee Callee, ArrayRef< llvm::Value *> Args, const Twine &Name="")
Emits a call or invoke instruction to the given function, depending on the current state of the EH st...
Definition: CGCall.cpp:3746
CGFunctionInfo - Class to encapsulate the information about a function definition.
This class organizes the cross-function state that is used while generating LLVM code.
Dataflow Directional Tag Classes.
void EmitCXXDestructorCall(const CXXDestructorDecl *D, CXXDtorType Type, bool ForVirtualBase, bool Delegating, Address This)
Definition: CGClass.cpp:2402
ExtInfo getExtInfo() const
Definition: Type.h:3636
static RValue getComplex(llvm::Value *V1, llvm::Value *V2)
Definition: CGValue.h:92
CodeGenFunction::ComplexPairTy ComplexPairTy
LValue Source
The original argument.
Definition: CGCall.h:270
const CGFunctionInfo & arrangeFunctionDeclaration(const FunctionDecl *FD)
Free functions are functions that are compatible with an ordinary C function pointer type...
Definition: CGCall.cpp:433
Qualifiers getMethodQualifiers() const
Definition: DeclCXX.h:2255
llvm::LoadInst * CreateAlignedLoad(llvm::Value *Addr, CharUnits Align, const llvm::Twine &Name="")
Definition: CGBuilder.h:90
static void forConstantArrayExpansion(CodeGenFunction &CGF, ConstantArrayExpansion *CAE, Address BaseAddr, llvm::function_ref< void(Address)> Fn)
Definition: CGCall.cpp:1001
ArrayRef< ExtParameterInfo > getExtParameterInfos() const
Interesting information about a specific parameter that can&#39;t simply be reflected in parameter&#39;s type...
Definition: Type.h:3393
void EmitARCIntrinsicUse(ArrayRef< llvm::Value *> values)
Given a number of pointers, inform the optimizer that they&#39;re being intrinsically used up until this ...
Definition: CGObjC.cpp:1931
llvm::LoadInst * CreateLoad(Address Addr, const llvm::Twine &Name="")
Definition: CGBuilder.h:69
const CXXRecordDecl * getParent() const
Returns the parent of this method declaration, which is the class in which this method is defined...
Definition: DeclCXX.h:2233
llvm::Function * getIntrinsic(unsigned IID, ArrayRef< llvm::Type *> Tys=None)
RValue getRValue(CodeGenFunction &CGF) const
Definition: CGCall.cpp:3527
static CGFunctionInfo * create(unsigned llvmCC, bool instanceMethod, bool chainCall, const FunctionType::ExtInfo &extInfo, ArrayRef< ExtParameterInfo > paramInfos, CanQualType resultType, ArrayRef< CanQualType > argTypes, RequiredArgs required)
Definition: CGCall.cpp:795
llvm::StoreInst * CreateStore(llvm::Value *Val, Address Addr, bool IsVolatile=false)
Definition: CGBuilder.h:107
virtual bool isNoProtoCallVariadic(const CodeGen::CallArgList &args, const FunctionNoProtoType *fnType) const
Determine whether a call to an unprototyped functions under the given calling convention should use t...
Definition: TargetInfo.cpp:399
LValue MakeAddrLValue(Address Addr, QualType T, AlignmentSource Source=AlignmentSource::Type)
void EmitNoreturnRuntimeCallOrInvoke(llvm::FunctionCallee callee, ArrayRef< llvm::Value *> args)
Emits a call or invoke to the given noreturn runtime function.
Definition: CGCall.cpp:3705
ArrayRef< llvm::Type * > getCoerceAndExpandTypeSequence() const
static RequiredArgs forPrototypePlus(const FunctionProtoType *prototype, unsigned additional)
Compute the arguments required by the given formal prototype, given that there may be some additional...
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
Definition: Type.h:4417
Complex values, per C99 6.2.5p11.
Definition: Type.h:2489
Iterator for iterating over Stmt * arrays that contain only T *.
Definition: Stmt.h:1014
static bool classof(const OMPClause *T)
bool isConstantSizeType() const
Return true if this is not a variable sized type, according to the rules of C99 6.7.5p3.
Definition: Type.cpp:2052
QualType getCanonicalTypeInternal() const
Definition: Type.h:2367
bool isIntegerType() const
isIntegerType() does not include complex integers (a GCC extension).
Definition: Type.h:6631
void EmitStoreOfComplex(ComplexPairTy V, LValue dest, bool isInit)
EmitStoreOfComplex - Store a complex number into the specified l-value.
static llvm::Value * tryRemoveRetainOfSelf(CodeGenFunction &CGF, llvm::Value *result)
If this is a +1 of the value of an immutable &#39;self&#39;, remove it.
Definition: CGCall.cpp:2671
CharUnits getIndirectAlign() const
Implements C++ ABI-specific code generation functions.
Definition: CGCXXABI.h:43
T * getAttr() const
Definition: DeclBase.h:538
llvm::Type * getElementType() const
Return the type of the values stored in this address.
Definition: Address.h:51
bool isMSVCXXPersonality() const
Definition: CGCleanup.h:644
This class organizes the cross-module state that is used while lowering AST types to LLVM types...
Definition: CodeGenTypes.h:59
llvm::StringRef getName() const
Return the IR name of the pointer value.
Definition: Address.h:61
Expand - Only valid for aggregate argument types.
Base for LValueReferenceType and RValueReferenceType.
Definition: Type.h:2685
void getExpandedTypes(QualType Ty, SmallVectorImpl< llvm::Type *>::iterator &TI)
getExpandedTypes - Expand the type
Definition: CGCall.cpp:979
static std::unique_ptr< TypeExpansion > getTypeExpansion(QualType Ty, const ASTContext &Context)
Definition: CGCall.cpp:904
bool isParamDestroyedInCallee() const
Definition: Decl.h:3736
void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false)
EmitBlock - Emit the given block.
Definition: CGStmt.cpp:450
Represents a base class of a C++ class.
Definition: DeclCXX.h:191
uint64_t getTypeSize(QualType T) const
Return the size of the specified (complete) type T, in bits.
Definition: ASTContext.h:2076
bool isIncompleteType(NamedDecl **Def=nullptr) const
Types are partitioned into 3 broad categories (C99 6.2.5p1): object types, function types...
Definition: Type.cpp:2062
ASTContext & getContext() const
Definition: CodeGenTypes.h:116
Pass it on the stack using its defined layout.
Definition: CGCXXABI.h:133
llvm::iterator_range< specific_attr_iterator< T > > specific_attrs() const
Definition: DeclBase.h:524