clang  8.0.0svn
CGBuiltin.cpp
Go to the documentation of this file.
1 //===---- CGBuiltin.cpp - Emit LLVM Code for builtins ---------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This contains code to emit Builtin calls as LLVM code.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "CGCXXABI.h"
15 #include "CGObjCRuntime.h"
16 #include "CGOpenCLRuntime.h"
17 #include "CGRecordLayout.h"
18 #include "CodeGenFunction.h"
19 #include "CodeGenModule.h"
20 #include "ConstantEmitter.h"
21 #include "TargetInfo.h"
22 #include "clang/AST/ASTContext.h"
23 #include "clang/AST/Decl.h"
26 #include "clang/Basic/TargetInfo.h"
28 #include "llvm/ADT/StringExtras.h"
29 #include "llvm/IR/CallSite.h"
30 #include "llvm/IR/DataLayout.h"
31 #include "llvm/IR/InlineAsm.h"
32 #include "llvm/IR/Intrinsics.h"
33 #include "llvm/IR/MDBuilder.h"
34 #include "llvm/Support/ConvertUTF.h"
35 #include "llvm/Support/ScopedPrinter.h"
36 #include "llvm/Support/TargetParser.h"
37 #include <sstream>
38 
39 using namespace clang;
40 using namespace CodeGen;
41 using namespace llvm;
42 
43 static
44 int64_t clamp(int64_t Value, int64_t Low, int64_t High) {
45  return std::min(High, std::max(Low, Value));
46 }
47 
48 /// getBuiltinLibFunction - Given a builtin id for a function like
49 /// "__builtin_fabsf", return a Function* for "fabsf".
51  unsigned BuiltinID) {
52  assert(Context.BuiltinInfo.isLibFunction(BuiltinID));
53 
54  // Get the name, skip over the __builtin_ prefix (if necessary).
55  StringRef Name;
56  GlobalDecl D(FD);
57 
58  // If the builtin has been declared explicitly with an assembler label,
59  // use the mangled name. This differs from the plain label on platforms
60  // that prefix labels.
61  if (FD->hasAttr<AsmLabelAttr>())
62  Name = getMangledName(D);
63  else
64  Name = Context.BuiltinInfo.getName(BuiltinID) + 10;
65 
66  llvm::FunctionType *Ty =
67  cast<llvm::FunctionType>(getTypes().ConvertType(FD->getType()));
68 
69  return GetOrCreateLLVMFunction(Name, Ty, D, /*ForVTable=*/false);
70 }
71 
72 /// Emit the conversions required to turn the given value into an
73 /// integer of the given size.
75  QualType T, llvm::IntegerType *IntType) {
76  V = CGF.EmitToMemory(V, T);
77 
78  if (V->getType()->isPointerTy())
79  return CGF.Builder.CreatePtrToInt(V, IntType);
80 
81  assert(V->getType() == IntType);
82  return V;
83 }
84 
86  QualType T, llvm::Type *ResultType) {
87  V = CGF.EmitFromMemory(V, T);
88 
89  if (ResultType->isPointerTy())
90  return CGF.Builder.CreateIntToPtr(V, ResultType);
91 
92  assert(V->getType() == ResultType);
93  return V;
94 }
95 
96 /// Utility to insert an atomic instruction based on Instrinsic::ID
97 /// and the expression node.
99  llvm::AtomicRMWInst::BinOp Kind,
100  const CallExpr *E) {
101  QualType T = E->getType();
102  assert(E->getArg(0)->getType()->isPointerType());
103  assert(CGF.getContext().hasSameUnqualifiedType(T,
104  E->getArg(0)->getType()->getPointeeType()));
105  assert(CGF.getContext().hasSameUnqualifiedType(T, E->getArg(1)->getType()));
106 
107  llvm::Value *DestPtr = CGF.EmitScalarExpr(E->getArg(0));
108  unsigned AddrSpace = DestPtr->getType()->getPointerAddressSpace();
109 
110  llvm::IntegerType *IntType =
111  llvm::IntegerType::get(CGF.getLLVMContext(),
112  CGF.getContext().getTypeSize(T));
113  llvm::Type *IntPtrType = IntType->getPointerTo(AddrSpace);
114 
115  llvm::Value *Args[2];
116  Args[0] = CGF.Builder.CreateBitCast(DestPtr, IntPtrType);
117  Args[1] = CGF.EmitScalarExpr(E->getArg(1));
118  llvm::Type *ValueType = Args[1]->getType();
119  Args[1] = EmitToInt(CGF, Args[1], T, IntType);
120 
121  llvm::Value *Result = CGF.Builder.CreateAtomicRMW(
122  Kind, Args[0], Args[1], llvm::AtomicOrdering::SequentiallyConsistent);
123  return EmitFromInt(CGF, Result, T, ValueType);
124 }
125 
127  Value *Val = CGF.EmitScalarExpr(E->getArg(0));
128  Value *Address = CGF.EmitScalarExpr(E->getArg(1));
129 
130  // Convert the type of the pointer to a pointer to the stored type.
131  Val = CGF.EmitToMemory(Val, E->getArg(0)->getType());
132  Value *BC = CGF.Builder.CreateBitCast(
133  Address, llvm::PointerType::getUnqual(Val->getType()), "cast");
134  LValue LV = CGF.MakeNaturalAlignAddrLValue(BC, E->getArg(0)->getType());
135  LV.setNontemporal(true);
136  CGF.EmitStoreOfScalar(Val, LV, false);
137  return nullptr;
138 }
139 
141  Value *Address = CGF.EmitScalarExpr(E->getArg(0));
142 
143  LValue LV = CGF.MakeNaturalAlignAddrLValue(Address, E->getType());
144  LV.setNontemporal(true);
145  return CGF.EmitLoadOfScalar(LV, E->getExprLoc());
146 }
147 
149  llvm::AtomicRMWInst::BinOp Kind,
150  const CallExpr *E) {
151  return RValue::get(MakeBinaryAtomicValue(CGF, Kind, E));
152 }
153 
154 /// Utility to insert an atomic instruction based Instrinsic::ID and
155 /// the expression node, where the return value is the result of the
156 /// operation.
158  llvm::AtomicRMWInst::BinOp Kind,
159  const CallExpr *E,
160  Instruction::BinaryOps Op,
161  bool Invert = false) {
162  QualType T = E->getType();
163  assert(E->getArg(0)->getType()->isPointerType());
164  assert(CGF.getContext().hasSameUnqualifiedType(T,
165  E->getArg(0)->getType()->getPointeeType()));
166  assert(CGF.getContext().hasSameUnqualifiedType(T, E->getArg(1)->getType()));
167 
168  llvm::Value *DestPtr = CGF.EmitScalarExpr(E->getArg(0));
169  unsigned AddrSpace = DestPtr->getType()->getPointerAddressSpace();
170 
171  llvm::IntegerType *IntType =
172  llvm::IntegerType::get(CGF.getLLVMContext(),
173  CGF.getContext().getTypeSize(T));
174  llvm::Type *IntPtrType = IntType->getPointerTo(AddrSpace);
175 
176  llvm::Value *Args[2];
177  Args[1] = CGF.EmitScalarExpr(E->getArg(1));
178  llvm::Type *ValueType = Args[1]->getType();
179  Args[1] = EmitToInt(CGF, Args[1], T, IntType);
180  Args[0] = CGF.Builder.CreateBitCast(DestPtr, IntPtrType);
181 
182  llvm::Value *Result = CGF.Builder.CreateAtomicRMW(
183  Kind, Args[0], Args[1], llvm::AtomicOrdering::SequentiallyConsistent);
184  Result = CGF.Builder.CreateBinOp(Op, Result, Args[1]);
185  if (Invert)
186  Result = CGF.Builder.CreateBinOp(llvm::Instruction::Xor, Result,
187  llvm::ConstantInt::get(IntType, -1));
188  Result = EmitFromInt(CGF, Result, T, ValueType);
189  return RValue::get(Result);
190 }
191 
192 /// Utility to insert an atomic cmpxchg instruction.
193 ///
194 /// @param CGF The current codegen function.
195 /// @param E Builtin call expression to convert to cmpxchg.
196 /// arg0 - address to operate on
197 /// arg1 - value to compare with
198 /// arg2 - new value
199 /// @param ReturnBool Specifies whether to return success flag of
200 /// cmpxchg result or the old value.
201 ///
202 /// @returns result of cmpxchg, according to ReturnBool
204  bool ReturnBool) {
205  QualType T = ReturnBool ? E->getArg(1)->getType() : E->getType();
206  llvm::Value *DestPtr = CGF.EmitScalarExpr(E->getArg(0));
207  unsigned AddrSpace = DestPtr->getType()->getPointerAddressSpace();
208 
209  llvm::IntegerType *IntType = llvm::IntegerType::get(
210  CGF.getLLVMContext(), CGF.getContext().getTypeSize(T));
211  llvm::Type *IntPtrType = IntType->getPointerTo(AddrSpace);
212 
213  Value *Args[3];
214  Args[0] = CGF.Builder.CreateBitCast(DestPtr, IntPtrType);
215  Args[1] = CGF.EmitScalarExpr(E->getArg(1));
216  llvm::Type *ValueType = Args[1]->getType();
217  Args[1] = EmitToInt(CGF, Args[1], T, IntType);
218  Args[2] = EmitToInt(CGF, CGF.EmitScalarExpr(E->getArg(2)), T, IntType);
219 
220  Value *Pair = CGF.Builder.CreateAtomicCmpXchg(
221  Args[0], Args[1], Args[2], llvm::AtomicOrdering::SequentiallyConsistent,
222  llvm::AtomicOrdering::SequentiallyConsistent);
223  if (ReturnBool)
224  // Extract boolean success flag and zext it to int.
225  return CGF.Builder.CreateZExt(CGF.Builder.CreateExtractValue(Pair, 1),
226  CGF.ConvertType(E->getType()));
227  else
228  // Extract old value and emit it using the same type as compare value.
229  return EmitFromInt(CGF, CGF.Builder.CreateExtractValue(Pair, 0), T,
230  ValueType);
231 }
232 
233 // Emit a simple mangled intrinsic that has 1 argument and a return type
234 // matching the argument type.
236  const CallExpr *E,
237  unsigned IntrinsicID) {
238  llvm::Value *Src0 = CGF.EmitScalarExpr(E->getArg(0));
239 
240  Value *F = CGF.CGM.getIntrinsic(IntrinsicID, Src0->getType());
241  return CGF.Builder.CreateCall(F, Src0);
242 }
243 
244 // Emit an intrinsic that has 2 operands of the same type as its result.
246  const CallExpr *E,
247  unsigned IntrinsicID) {
248  llvm::Value *Src0 = CGF.EmitScalarExpr(E->getArg(0));
249  llvm::Value *Src1 = CGF.EmitScalarExpr(E->getArg(1));
250 
251  Value *F = CGF.CGM.getIntrinsic(IntrinsicID, Src0->getType());
252  return CGF.Builder.CreateCall(F, { Src0, Src1 });
253 }
254 
255 // Emit an intrinsic that has 3 operands of the same type as its result.
257  const CallExpr *E,
258  unsigned IntrinsicID) {
259  llvm::Value *Src0 = CGF.EmitScalarExpr(E->getArg(0));
260  llvm::Value *Src1 = CGF.EmitScalarExpr(E->getArg(1));
261  llvm::Value *Src2 = CGF.EmitScalarExpr(E->getArg(2));
262 
263  Value *F = CGF.CGM.getIntrinsic(IntrinsicID, Src0->getType());
264  return CGF.Builder.CreateCall(F, { Src0, Src1, Src2 });
265 }
266 
267 // Emit an intrinsic that has 1 float or double operand, and 1 integer.
269  const CallExpr *E,
270  unsigned IntrinsicID) {
271  llvm::Value *Src0 = CGF.EmitScalarExpr(E->getArg(0));
272  llvm::Value *Src1 = CGF.EmitScalarExpr(E->getArg(1));
273 
274  Value *F = CGF.CGM.getIntrinsic(IntrinsicID, Src0->getType());
275  return CGF.Builder.CreateCall(F, {Src0, Src1});
276 }
277 
278 /// EmitFAbs - Emit a call to @llvm.fabs().
279 static Value *EmitFAbs(CodeGenFunction &CGF, Value *V) {
280  Value *F = CGF.CGM.getIntrinsic(Intrinsic::fabs, V->getType());
281  llvm::CallInst *Call = CGF.Builder.CreateCall(F, V);
282  Call->setDoesNotAccessMemory();
283  return Call;
284 }
285 
286 /// Emit the computation of the sign bit for a floating point value. Returns
287 /// the i1 sign bit value.
289  LLVMContext &C = CGF.CGM.getLLVMContext();
290 
291  llvm::Type *Ty = V->getType();
292  int Width = Ty->getPrimitiveSizeInBits();
293  llvm::Type *IntTy = llvm::IntegerType::get(C, Width);
294  V = CGF.Builder.CreateBitCast(V, IntTy);
295  if (Ty->isPPC_FP128Ty()) {
296  // We want the sign bit of the higher-order double. The bitcast we just
297  // did works as if the double-double was stored to memory and then
298  // read as an i128. The "store" will put the higher-order double in the
299  // lower address in both little- and big-Endian modes, but the "load"
300  // will treat those bits as a different part of the i128: the low bits in
301  // little-Endian, the high bits in big-Endian. Therefore, on big-Endian
302  // we need to shift the high bits down to the low before truncating.
303  Width >>= 1;
304  if (CGF.getTarget().isBigEndian()) {
305  Value *ShiftCst = llvm::ConstantInt::get(IntTy, Width);
306  V = CGF.Builder.CreateLShr(V, ShiftCst);
307  }
308  // We are truncating value in order to extract the higher-order
309  // double, which we will be using to extract the sign from.
310  IntTy = llvm::IntegerType::get(C, Width);
311  V = CGF.Builder.CreateTrunc(V, IntTy);
312  }
313  Value *Zero = llvm::Constant::getNullValue(IntTy);
314  return CGF.Builder.CreateICmpSLT(V, Zero);
315 }
316 
318  const CallExpr *E, llvm::Constant *calleeValue) {
319  CGCallee callee = CGCallee::forDirect(calleeValue, FD);
320  return CGF.EmitCall(E->getCallee()->getType(), callee, E, ReturnValueSlot());
321 }
322 
323 /// Emit a call to llvm.{sadd,uadd,ssub,usub,smul,umul}.with.overflow.*
324 /// depending on IntrinsicID.
325 ///
326 /// \arg CGF The current codegen function.
327 /// \arg IntrinsicID The ID for the Intrinsic we wish to generate.
328 /// \arg X The first argument to the llvm.*.with.overflow.*.
329 /// \arg Y The second argument to the llvm.*.with.overflow.*.
330 /// \arg Carry The carry returned by the llvm.*.with.overflow.*.
331 /// \returns The result (i.e. sum/product) returned by the intrinsic.
333  const llvm::Intrinsic::ID IntrinsicID,
335  llvm::Value *&Carry) {
336  // Make sure we have integers of the same width.
337  assert(X->getType() == Y->getType() &&
338  "Arguments must be the same type. (Did you forget to make sure both "
339  "arguments have the same integer width?)");
340 
341  llvm::Value *Callee = CGF.CGM.getIntrinsic(IntrinsicID, X->getType());
342  llvm::Value *Tmp = CGF.Builder.CreateCall(Callee, {X, Y});
343  Carry = CGF.Builder.CreateExtractValue(Tmp, 1);
344  return CGF.Builder.CreateExtractValue(Tmp, 0);
345 }
346 
348  unsigned IntrinsicID,
349  int low, int high) {
350  llvm::MDBuilder MDHelper(CGF.getLLVMContext());
351  llvm::MDNode *RNode = MDHelper.createRange(APInt(32, low), APInt(32, high));
352  Value *F = CGF.CGM.getIntrinsic(IntrinsicID, {});
353  llvm::Instruction *Call = CGF.Builder.CreateCall(F);
354  Call->setMetadata(llvm::LLVMContext::MD_range, RNode);
355  return Call;
356 }
357 
358 namespace {
359  struct WidthAndSignedness {
360  unsigned Width;
361  bool Signed;
362  };
363 }
364 
365 static WidthAndSignedness
367  const clang::QualType Type) {
368  assert(Type->isIntegerType() && "Given type is not an integer.");
369  unsigned Width = Type->isBooleanType() ? 1 : context.getTypeInfo(Type).Width;
370  bool Signed = Type->isSignedIntegerType();
371  return {Width, Signed};
372 }
373 
374 // Given one or more integer types, this function produces an integer type that
375 // encompasses them: any value in one of the given types could be expressed in
376 // the encompassing type.
377 static struct WidthAndSignedness
378 EncompassingIntegerType(ArrayRef<struct WidthAndSignedness> Types) {
379  assert(Types.size() > 0 && "Empty list of types.");
380 
381  // If any of the given types is signed, we must return a signed type.
382  bool Signed = false;
383  for (const auto &Type : Types) {
384  Signed |= Type.Signed;
385  }
386 
387  // The encompassing type must have a width greater than or equal to the width
388  // of the specified types. Additionally, if the encompassing type is signed,
389  // its width must be strictly greater than the width of any unsigned types
390  // given.
391  unsigned Width = 0;
392  for (const auto &Type : Types) {
393  unsigned MinWidth = Type.Width + (Signed && !Type.Signed);
394  if (Width < MinWidth) {
395  Width = MinWidth;
396  }
397  }
398 
399  return {Width, Signed};
400 }
401 
402 Value *CodeGenFunction::EmitVAStartEnd(Value *ArgValue, bool IsStart) {
403  llvm::Type *DestType = Int8PtrTy;
404  if (ArgValue->getType() != DestType)
405  ArgValue =
406  Builder.CreateBitCast(ArgValue, DestType, ArgValue->getName().data());
407 
408  Intrinsic::ID inst = IsStart ? Intrinsic::vastart : Intrinsic::vaend;
409  return Builder.CreateCall(CGM.getIntrinsic(inst), ArgValue);
410 }
411 
412 /// Checks if using the result of __builtin_object_size(p, @p From) in place of
413 /// __builtin_object_size(p, @p To) is correct
414 static bool areBOSTypesCompatible(int From, int To) {
415  // Note: Our __builtin_object_size implementation currently treats Type=0 and
416  // Type=2 identically. Encoding this implementation detail here may make
417  // improving __builtin_object_size difficult in the future, so it's omitted.
418  return From == To || (From == 0 && To == 1) || (From == 3 && To == 2);
419 }
420 
421 static llvm::Value *
422 getDefaultBuiltinObjectSizeResult(unsigned Type, llvm::IntegerType *ResType) {
423  return ConstantInt::get(ResType, (Type & 2) ? 0 : -1, /*isSigned=*/true);
424 }
425 
426 llvm::Value *
427 CodeGenFunction::evaluateOrEmitBuiltinObjectSize(const Expr *E, unsigned Type,
428  llvm::IntegerType *ResType,
429  llvm::Value *EmittedE) {
430  uint64_t ObjectSize;
431  if (!E->tryEvaluateObjectSize(ObjectSize, getContext(), Type))
432  return emitBuiltinObjectSize(E, Type, ResType, EmittedE);
433  return ConstantInt::get(ResType, ObjectSize, /*isSigned=*/true);
434 }
435 
436 /// Returns a Value corresponding to the size of the given expression.
437 /// This Value may be either of the following:
438 /// - A llvm::Argument (if E is a param with the pass_object_size attribute on
439 /// it)
440 /// - A call to the @llvm.objectsize intrinsic
441 ///
442 /// EmittedE is the result of emitting `E` as a scalar expr. If it's non-null
443 /// and we wouldn't otherwise try to reference a pass_object_size parameter,
444 /// we'll call @llvm.objectsize on EmittedE, rather than emitting E.
445 llvm::Value *
446 CodeGenFunction::emitBuiltinObjectSize(const Expr *E, unsigned Type,
447  llvm::IntegerType *ResType,
448  llvm::Value *EmittedE) {
449  // We need to reference an argument if the pointer is a parameter with the
450  // pass_object_size attribute.
451  if (auto *D = dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts())) {
452  auto *Param = dyn_cast<ParmVarDecl>(D->getDecl());
453  auto *PS = D->getDecl()->getAttr<PassObjectSizeAttr>();
454  if (Param != nullptr && PS != nullptr &&
455  areBOSTypesCompatible(PS->getType(), Type)) {
456  auto Iter = SizeArguments.find(Param);
457  assert(Iter != SizeArguments.end());
458 
459  const ImplicitParamDecl *D = Iter->second;
460  auto DIter = LocalDeclMap.find(D);
461  assert(DIter != LocalDeclMap.end());
462 
463  return EmitLoadOfScalar(DIter->second, /*volatile=*/false,
464  getContext().getSizeType(), E->getBeginLoc());
465  }
466  }
467 
468  // LLVM can't handle Type=3 appropriately, and __builtin_object_size shouldn't
469  // evaluate E for side-effects. In either case, we shouldn't lower to
470  // @llvm.objectsize.
471  if (Type == 3 || (!EmittedE && E->HasSideEffects(getContext())))
472  return getDefaultBuiltinObjectSizeResult(Type, ResType);
473 
474  Value *Ptr = EmittedE ? EmittedE : EmitScalarExpr(E);
475  assert(Ptr->getType()->isPointerTy() &&
476  "Non-pointer passed to __builtin_object_size?");
477 
478  Value *F = CGM.getIntrinsic(Intrinsic::objectsize, {ResType, Ptr->getType()});
479 
480  // LLVM only supports 0 and 2, make sure that we pass along that as a boolean.
481  Value *Min = Builder.getInt1((Type & 2) != 0);
482  // For GCC compatibility, __builtin_object_size treat NULL as unknown size.
483  Value *NullIsUnknown = Builder.getTrue();
484  return Builder.CreateCall(F, {Ptr, Min, NullIsUnknown});
485 }
486 
487 namespace {
488 /// A struct to generically desribe a bit test intrinsic.
489 struct BitTest {
490  enum ActionKind : uint8_t { TestOnly, Complement, Reset, Set };
491  enum InterlockingKind : uint8_t {
492  Unlocked,
493  Sequential,
494  Acquire,
495  Release,
496  NoFence
497  };
498 
499  ActionKind Action;
500  InterlockingKind Interlocking;
501  bool Is64Bit;
502 
503  static BitTest decodeBitTestBuiltin(unsigned BuiltinID);
504 };
505 } // namespace
506 
507 BitTest BitTest::decodeBitTestBuiltin(unsigned BuiltinID) {
508  switch (BuiltinID) {
509  // Main portable variants.
510  case Builtin::BI_bittest:
511  return {TestOnly, Unlocked, false};
512  case Builtin::BI_bittestandcomplement:
513  return {Complement, Unlocked, false};
514  case Builtin::BI_bittestandreset:
515  return {Reset, Unlocked, false};
516  case Builtin::BI_bittestandset:
517  return {Set, Unlocked, false};
518  case Builtin::BI_interlockedbittestandreset:
519  return {Reset, Sequential, false};
520  case Builtin::BI_interlockedbittestandset:
521  return {Set, Sequential, false};
522 
523  // X86-specific 64-bit variants.
524  case Builtin::BI_bittest64:
525  return {TestOnly, Unlocked, true};
526  case Builtin::BI_bittestandcomplement64:
527  return {Complement, Unlocked, true};
528  case Builtin::BI_bittestandreset64:
529  return {Reset, Unlocked, true};
530  case Builtin::BI_bittestandset64:
531  return {Set, Unlocked, true};
532  case Builtin::BI_interlockedbittestandreset64:
533  return {Reset, Sequential, true};
534  case Builtin::BI_interlockedbittestandset64:
535  return {Set, Sequential, true};
536 
537  // ARM/AArch64-specific ordering variants.
538  case Builtin::BI_interlockedbittestandset_acq:
539  return {Set, Acquire, false};
540  case Builtin::BI_interlockedbittestandset_rel:
541  return {Set, Release, false};
542  case Builtin::BI_interlockedbittestandset_nf:
543  return {Set, NoFence, false};
544  case Builtin::BI_interlockedbittestandreset_acq:
545  return {Reset, Acquire, false};
546  case Builtin::BI_interlockedbittestandreset_rel:
547  return {Reset, Release, false};
548  case Builtin::BI_interlockedbittestandreset_nf:
549  return {Reset, NoFence, false};
550  }
551  llvm_unreachable("expected only bittest intrinsics");
552 }
553 
554 static char bitActionToX86BTCode(BitTest::ActionKind A) {
555  switch (A) {
556  case BitTest::TestOnly: return '\0';
557  case BitTest::Complement: return 'c';
558  case BitTest::Reset: return 'r';
559  case BitTest::Set: return 's';
560  }
561  llvm_unreachable("invalid action");
562 }
563 
565  BitTest BT,
566  const CallExpr *E, Value *BitBase,
567  Value *BitPos) {
568  char Action = bitActionToX86BTCode(BT.Action);
569  char SizeSuffix = BT.Is64Bit ? 'q' : 'l';
570 
571  // Build the assembly.
572  SmallString<64> Asm;
573  raw_svector_ostream AsmOS(Asm);
574  if (BT.Interlocking != BitTest::Unlocked)
575  AsmOS << "lock ";
576  AsmOS << "bt";
577  if (Action)
578  AsmOS << Action;
579  AsmOS << SizeSuffix << " $2, ($1)\n\tsetc ${0:b}";
580 
581  // Build the constraints. FIXME: We should support immediates when possible.
582  std::string Constraints = "=r,r,r,~{cc},~{flags},~{fpsr}";
583  llvm::IntegerType *IntType = llvm::IntegerType::get(
584  CGF.getLLVMContext(),
585  CGF.getContext().getTypeSize(E->getArg(1)->getType()));
586  llvm::Type *IntPtrType = IntType->getPointerTo();
587  llvm::FunctionType *FTy =
588  llvm::FunctionType::get(CGF.Int8Ty, {IntPtrType, IntType}, false);
589 
590  llvm::InlineAsm *IA =
591  llvm::InlineAsm::get(FTy, Asm, Constraints, /*SideEffects=*/true);
592  return CGF.Builder.CreateCall(IA, {BitBase, BitPos});
593 }
594 
595 static llvm::AtomicOrdering
596 getBitTestAtomicOrdering(BitTest::InterlockingKind I) {
597  switch (I) {
598  case BitTest::Unlocked: return llvm::AtomicOrdering::NotAtomic;
599  case BitTest::Sequential: return llvm::AtomicOrdering::SequentiallyConsistent;
600  case BitTest::Acquire: return llvm::AtomicOrdering::Acquire;
601  case BitTest::Release: return llvm::AtomicOrdering::Release;
602  case BitTest::NoFence: return llvm::AtomicOrdering::Monotonic;
603  }
604  llvm_unreachable("invalid interlocking");
605 }
606 
607 /// Emit a _bittest* intrinsic. These intrinsics take a pointer to an array of
608 /// bits and a bit position and read and optionally modify the bit at that
609 /// position. The position index can be arbitrarily large, i.e. it can be larger
610 /// than 31 or 63, so we need an indexed load in the general case.
612  unsigned BuiltinID,
613  const CallExpr *E) {
614  Value *BitBase = CGF.EmitScalarExpr(E->getArg(0));
615  Value *BitPos = CGF.EmitScalarExpr(E->getArg(1));
616 
617  BitTest BT = BitTest::decodeBitTestBuiltin(BuiltinID);
618 
619  // X86 has special BT, BTC, BTR, and BTS instructions that handle the array
620  // indexing operation internally. Use them if possible.
621  llvm::Triple::ArchType Arch = CGF.getTarget().getTriple().getArch();
622  if (Arch == llvm::Triple::x86 || Arch == llvm::Triple::x86_64)
623  return EmitX86BitTestIntrinsic(CGF, BT, E, BitBase, BitPos);
624 
625  // Otherwise, use generic code to load one byte and test the bit. Use all but
626  // the bottom three bits as the array index, and the bottom three bits to form
627  // a mask.
628  // Bit = BitBaseI8[BitPos >> 3] & (1 << (BitPos & 0x7)) != 0;
629  Value *ByteIndex = CGF.Builder.CreateAShr(
630  BitPos, llvm::ConstantInt::get(BitPos->getType(), 3), "bittest.byteidx");
631  Value *BitBaseI8 = CGF.Builder.CreatePointerCast(BitBase, CGF.Int8PtrTy);
632  Address ByteAddr(CGF.Builder.CreateInBoundsGEP(CGF.Int8Ty, BitBaseI8,
633  ByteIndex, "bittest.byteaddr"),
634  CharUnits::One());
635  Value *PosLow =
636  CGF.Builder.CreateAnd(CGF.Builder.CreateTrunc(BitPos, CGF.Int8Ty),
637  llvm::ConstantInt::get(CGF.Int8Ty, 0x7));
638 
639  // The updating instructions will need a mask.
640  Value *Mask = nullptr;
641  if (BT.Action != BitTest::TestOnly) {
642  Mask = CGF.Builder.CreateShl(llvm::ConstantInt::get(CGF.Int8Ty, 1), PosLow,
643  "bittest.mask");
644  }
645 
646  // Check the action and ordering of the interlocked intrinsics.
647  llvm::AtomicOrdering Ordering = getBitTestAtomicOrdering(BT.Interlocking);
648 
649  Value *OldByte = nullptr;
650  if (Ordering != llvm::AtomicOrdering::NotAtomic) {
651  // Emit a combined atomicrmw load/store operation for the interlocked
652  // intrinsics.
653  llvm::AtomicRMWInst::BinOp RMWOp = llvm::AtomicRMWInst::Or;
654  if (BT.Action == BitTest::Reset) {
655  Mask = CGF.Builder.CreateNot(Mask);
656  RMWOp = llvm::AtomicRMWInst::And;
657  }
658  OldByte = CGF.Builder.CreateAtomicRMW(RMWOp, ByteAddr.getPointer(), Mask,
659  Ordering);
660  } else {
661  // Emit a plain load for the non-interlocked intrinsics.
662  OldByte = CGF.Builder.CreateLoad(ByteAddr, "bittest.byte");
663  Value *NewByte = nullptr;
664  switch (BT.Action) {
665  case BitTest::TestOnly:
666  // Don't store anything.
667  break;
668  case BitTest::Complement:
669  NewByte = CGF.Builder.CreateXor(OldByte, Mask);
670  break;
671  case BitTest::Reset:
672  NewByte = CGF.Builder.CreateAnd(OldByte, CGF.Builder.CreateNot(Mask));
673  break;
674  case BitTest::Set:
675  NewByte = CGF.Builder.CreateOr(OldByte, Mask);
676  break;
677  }
678  if (NewByte)
679  CGF.Builder.CreateStore(NewByte, ByteAddr);
680  }
681 
682  // However we loaded the old byte, either by plain load or atomicrmw, shift
683  // the bit into the low position and mask it to 0 or 1.
684  Value *ShiftedByte = CGF.Builder.CreateLShr(OldByte, PosLow, "bittest.shr");
685  return CGF.Builder.CreateAnd(
686  ShiftedByte, llvm::ConstantInt::get(CGF.Int8Ty, 1), "bittest.res");
687 }
688 
689 namespace {
690 enum class MSVCSetJmpKind {
691  _setjmpex,
692  _setjmp3,
693  _setjmp
694 };
695 }
696 
697 /// MSVC handles setjmp a bit differently on different platforms. On every
698 /// architecture except 32-bit x86, the frame address is passed. On x86, extra
699 /// parameters can be passed as variadic arguments, but we always pass none.
701  const CallExpr *E) {
702  llvm::Value *Arg1 = nullptr;
703  llvm::Type *Arg1Ty = nullptr;
704  StringRef Name;
705  bool IsVarArg = false;
706  if (SJKind == MSVCSetJmpKind::_setjmp3) {
707  Name = "_setjmp3";
708  Arg1Ty = CGF.Int32Ty;
709  Arg1 = llvm::ConstantInt::get(CGF.IntTy, 0);
710  IsVarArg = true;
711  } else {
712  Name = SJKind == MSVCSetJmpKind::_setjmp ? "_setjmp" : "_setjmpex";
713  Arg1Ty = CGF.Int8PtrTy;
714  Arg1 = CGF.Builder.CreateCall(CGF.CGM.getIntrinsic(Intrinsic::frameaddress),
715  llvm::ConstantInt::get(CGF.Int32Ty, 0));
716  }
717 
718  // Mark the call site and declaration with ReturnsTwice.
719  llvm::Type *ArgTypes[2] = {CGF.Int8PtrTy, Arg1Ty};
720  llvm::AttributeList ReturnsTwiceAttr = llvm::AttributeList::get(
721  CGF.getLLVMContext(), llvm::AttributeList::FunctionIndex,
722  llvm::Attribute::ReturnsTwice);
723  llvm::Constant *SetJmpFn = CGF.CGM.CreateRuntimeFunction(
724  llvm::FunctionType::get(CGF.IntTy, ArgTypes, IsVarArg), Name,
725  ReturnsTwiceAttr, /*Local=*/true);
726 
727  llvm::Value *Buf = CGF.Builder.CreateBitOrPointerCast(
728  CGF.EmitScalarExpr(E->getArg(0)), CGF.Int8PtrTy);
729  llvm::Value *Args[] = {Buf, Arg1};
730  llvm::CallSite CS = CGF.EmitRuntimeCallOrInvoke(SetJmpFn, Args);
731  CS.setAttributes(ReturnsTwiceAttr);
732  return RValue::get(CS.getInstruction());
733 }
734 
735 // Many of MSVC builtins are on x64, ARM and AArch64; to avoid repeating code,
736 // we handle them here.
738  _BitScanForward,
739  _BitScanReverse,
740  _InterlockedAnd,
741  _InterlockedDecrement,
742  _InterlockedExchange,
743  _InterlockedExchangeAdd,
744  _InterlockedExchangeSub,
745  _InterlockedIncrement,
746  _InterlockedOr,
747  _InterlockedXor,
748  __fastfail,
749 };
750 
752  const CallExpr *E) {
753  switch (BuiltinID) {
754  case MSVCIntrin::_BitScanForward:
755  case MSVCIntrin::_BitScanReverse: {
756  Value *ArgValue = EmitScalarExpr(E->getArg(1));
757 
758  llvm::Type *ArgType = ArgValue->getType();
759  llvm::Type *IndexType =
760  EmitScalarExpr(E->getArg(0))->getType()->getPointerElementType();
761  llvm::Type *ResultType = ConvertType(E->getType());
762 
763  Value *ArgZero = llvm::Constant::getNullValue(ArgType);
764  Value *ResZero = llvm::Constant::getNullValue(ResultType);
765  Value *ResOne = llvm::ConstantInt::get(ResultType, 1);
766 
767  BasicBlock *Begin = Builder.GetInsertBlock();
768  BasicBlock *End = createBasicBlock("bitscan_end", this->CurFn);
769  Builder.SetInsertPoint(End);
770  PHINode *Result = Builder.CreatePHI(ResultType, 2, "bitscan_result");
771 
772  Builder.SetInsertPoint(Begin);
773  Value *IsZero = Builder.CreateICmpEQ(ArgValue, ArgZero);
774  BasicBlock *NotZero = createBasicBlock("bitscan_not_zero", this->CurFn);
775  Builder.CreateCondBr(IsZero, End, NotZero);
776  Result->addIncoming(ResZero, Begin);
777 
778  Builder.SetInsertPoint(NotZero);
779  Address IndexAddress = EmitPointerWithAlignment(E->getArg(0));
780 
781  if (BuiltinID == MSVCIntrin::_BitScanForward) {
782  Value *F = CGM.getIntrinsic(Intrinsic::cttz, ArgType);
783  Value *ZeroCount = Builder.CreateCall(F, {ArgValue, Builder.getTrue()});
784  ZeroCount = Builder.CreateIntCast(ZeroCount, IndexType, false);
785  Builder.CreateStore(ZeroCount, IndexAddress, false);
786  } else {
787  unsigned ArgWidth = cast<llvm::IntegerType>(ArgType)->getBitWidth();
788  Value *ArgTypeLastIndex = llvm::ConstantInt::get(IndexType, ArgWidth - 1);
789 
790  Value *F = CGM.getIntrinsic(Intrinsic::ctlz, ArgType);
791  Value *ZeroCount = Builder.CreateCall(F, {ArgValue, Builder.getTrue()});
792  ZeroCount = Builder.CreateIntCast(ZeroCount, IndexType, false);
793  Value *Index = Builder.CreateNSWSub(ArgTypeLastIndex, ZeroCount);
794  Builder.CreateStore(Index, IndexAddress, false);
795  }
796  Builder.CreateBr(End);
797  Result->addIncoming(ResOne, NotZero);
798 
799  Builder.SetInsertPoint(End);
800  return Result;
801  }
802  case MSVCIntrin::_InterlockedAnd:
803  return MakeBinaryAtomicValue(*this, AtomicRMWInst::And, E);
804  case MSVCIntrin::_InterlockedExchange:
805  return MakeBinaryAtomicValue(*this, AtomicRMWInst::Xchg, E);
806  case MSVCIntrin::_InterlockedExchangeAdd:
807  return MakeBinaryAtomicValue(*this, AtomicRMWInst::Add, E);
808  case MSVCIntrin::_InterlockedExchangeSub:
809  return MakeBinaryAtomicValue(*this, AtomicRMWInst::Sub, E);
810  case MSVCIntrin::_InterlockedOr:
811  return MakeBinaryAtomicValue(*this, AtomicRMWInst::Or, E);
812  case MSVCIntrin::_InterlockedXor:
813  return MakeBinaryAtomicValue(*this, AtomicRMWInst::Xor, E);
814 
815  case MSVCIntrin::_InterlockedDecrement: {
816  llvm::Type *IntTy = ConvertType(E->getType());
817  AtomicRMWInst *RMWI = Builder.CreateAtomicRMW(
818  AtomicRMWInst::Sub,
819  EmitScalarExpr(E->getArg(0)),
820  ConstantInt::get(IntTy, 1),
821  llvm::AtomicOrdering::SequentiallyConsistent);
822  return Builder.CreateSub(RMWI, ConstantInt::get(IntTy, 1));
823  }
824  case MSVCIntrin::_InterlockedIncrement: {
825  llvm::Type *IntTy = ConvertType(E->getType());
826  AtomicRMWInst *RMWI = Builder.CreateAtomicRMW(
827  AtomicRMWInst::Add,
828  EmitScalarExpr(E->getArg(0)),
829  ConstantInt::get(IntTy, 1),
830  llvm::AtomicOrdering::SequentiallyConsistent);
831  return Builder.CreateAdd(RMWI, ConstantInt::get(IntTy, 1));
832  }
833 
834  case MSVCIntrin::__fastfail: {
835  // Request immediate process termination from the kernel. The instruction
836  // sequences to do this are documented on MSDN:
837  // https://msdn.microsoft.com/en-us/library/dn774154.aspx
838  llvm::Triple::ArchType ISA = getTarget().getTriple().getArch();
839  StringRef Asm, Constraints;
840  switch (ISA) {
841  default:
842  ErrorUnsupported(E, "__fastfail call for this architecture");
843  break;
844  case llvm::Triple::x86:
845  case llvm::Triple::x86_64:
846  Asm = "int $$0x29";
847  Constraints = "{cx}";
848  break;
849  case llvm::Triple::thumb:
850  Asm = "udf #251";
851  Constraints = "{r0}";
852  break;
853  }
854  llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, {Int32Ty}, false);
855  llvm::InlineAsm *IA =
856  llvm::InlineAsm::get(FTy, Asm, Constraints, /*SideEffects=*/true);
857  llvm::AttributeList NoReturnAttr = llvm::AttributeList::get(
858  getLLVMContext(), llvm::AttributeList::FunctionIndex,
859  llvm::Attribute::NoReturn);
860  CallSite CS = Builder.CreateCall(IA, EmitScalarExpr(E->getArg(0)));
861  CS.setAttributes(NoReturnAttr);
862  return CS.getInstruction();
863  }
864  }
865  llvm_unreachable("Incorrect MSVC intrinsic!");
866 }
867 
868 namespace {
869 // ARC cleanup for __builtin_os_log_format
870 struct CallObjCArcUse final : EHScopeStack::Cleanup {
871  CallObjCArcUse(llvm::Value *object) : object(object) {}
872  llvm::Value *object;
873 
874  void Emit(CodeGenFunction &CGF, Flags flags) override {
875  CGF.EmitARCIntrinsicUse(object);
876  }
877 };
878 }
879 
882  assert((Kind == BCK_CLZPassedZero || Kind == BCK_CTZPassedZero)
883  && "Unsupported builtin check kind");
884 
885  Value *ArgValue = EmitScalarExpr(E);
886  if (!SanOpts.has(SanitizerKind::Builtin) || !getTarget().isCLZForZeroUndef())
887  return ArgValue;
888 
889  SanitizerScope SanScope(this);
890  Value *Cond = Builder.CreateICmpNE(
891  ArgValue, llvm::Constant::getNullValue(ArgValue->getType()));
892  EmitCheck(std::make_pair(Cond, SanitizerKind::Builtin),
893  SanitizerHandler::InvalidBuiltin,
894  {EmitCheckSourceLocation(E->getExprLoc()),
895  llvm::ConstantInt::get(Builder.getInt8Ty(), Kind)},
896  None);
897  return ArgValue;
898 }
899 
900 /// Get the argument type for arguments to os_log_helper.
901 static CanQualType getOSLogArgType(ASTContext &C, int Size) {
902  QualType UnsignedTy = C.getIntTypeForBitwidth(Size * 8, /*Signed=*/false);
903  return C.getCanonicalType(UnsignedTy);
904 }
905 
907  const analyze_os_log::OSLogBufferLayout &Layout,
908  CharUnits BufferAlignment) {
909  ASTContext &Ctx = getContext();
910 
912  {
913  raw_svector_ostream OS(Name);
914  OS << "__os_log_helper";
915  OS << "_" << BufferAlignment.getQuantity();
916  OS << "_" << int(Layout.getSummaryByte());
917  OS << "_" << int(Layout.getNumArgsByte());
918  for (const auto &Item : Layout.Items)
919  OS << "_" << int(Item.getSizeByte()) << "_"
920  << int(Item.getDescriptorByte());
921  }
922 
923  if (llvm::Function *F = CGM.getModule().getFunction(Name))
924  return F;
925 
927  Params.emplace_back(Ctx, nullptr, SourceLocation(), &Ctx.Idents.get("buffer"),
929 
930  for (unsigned int I = 0, E = Layout.Items.size(); I < E; ++I) {
931  char Size = Layout.Items[I].getSizeByte();
932  if (!Size)
933  continue;
934 
935  Params.emplace_back(
936  Ctx, nullptr, SourceLocation(),
937  &Ctx.Idents.get(std::string("arg") + llvm::to_string(I)),
939  }
940 
941  FunctionArgList Args;
942  for (auto &P : Params)
943  Args.push_back(&P);
944 
945  // The helper function has linkonce_odr linkage to enable the linker to merge
946  // identical functions. To ensure the merging always happens, 'noinline' is
947  // attached to the function when compiling with -Oz.
948  const CGFunctionInfo &FI =
949  CGM.getTypes().arrangeBuiltinFunctionDeclaration(Ctx.VoidTy, Args);
950  llvm::FunctionType *FuncTy = CGM.getTypes().GetFunctionType(FI);
951  llvm::Function *Fn = llvm::Function::Create(
952  FuncTy, llvm::GlobalValue::LinkOnceODRLinkage, Name, &CGM.getModule());
953  Fn->setVisibility(llvm::GlobalValue::HiddenVisibility);
954  CGM.SetLLVMFunctionAttributes(nullptr, FI, Fn);
955  CGM.SetLLVMFunctionAttributesForDefinition(nullptr, Fn);
956 
957  // Attach 'noinline' at -Oz.
958  if (CGM.getCodeGenOpts().OptimizeSize == 2)
959  Fn->addFnAttr(llvm::Attribute::NoInline);
960 
961  auto NL = ApplyDebugLocation::CreateEmpty(*this);
962  IdentifierInfo *II = &Ctx.Idents.get(Name);
965  Ctx.VoidTy, nullptr, SC_PrivateExtern, false, false);
966 
967  StartFunction(FD, Ctx.VoidTy, Fn, FI, Args);
968 
969  // Create a scope with an artificial location for the body of this function.
970  auto AL = ApplyDebugLocation::CreateArtificial(*this);
971 
973  Address BufAddr(Builder.CreateLoad(GetAddrOfLocalVar(&Params[0]), "buf"),
974  BufferAlignment);
975  Builder.CreateStore(Builder.getInt8(Layout.getSummaryByte()),
976  Builder.CreateConstByteGEP(BufAddr, Offset++, "summary"));
977  Builder.CreateStore(Builder.getInt8(Layout.getNumArgsByte()),
978  Builder.CreateConstByteGEP(BufAddr, Offset++, "numArgs"));
979 
980  unsigned I = 1;
981  for (const auto &Item : Layout.Items) {
982  Builder.CreateStore(
983  Builder.getInt8(Item.getDescriptorByte()),
984  Builder.CreateConstByteGEP(BufAddr, Offset++, "argDescriptor"));
985  Builder.CreateStore(
986  Builder.getInt8(Item.getSizeByte()),
987  Builder.CreateConstByteGEP(BufAddr, Offset++, "argSize"));
988 
989  CharUnits Size = Item.size();
990  if (!Size.getQuantity())
991  continue;
992 
993  Address Arg = GetAddrOfLocalVar(&Params[I]);
994  Address Addr = Builder.CreateConstByteGEP(BufAddr, Offset, "argData");
995  Addr = Builder.CreateBitCast(Addr, Arg.getPointer()->getType(),
996  "argDataCast");
997  Builder.CreateStore(Builder.CreateLoad(Arg), Addr);
998  Offset += Size;
999  ++I;
1000  }
1001 
1002  FinishFunction();
1003 
1004  return Fn;
1005 }
1006 
1008  assert(E.getNumArgs() >= 2 &&
1009  "__builtin_os_log_format takes at least 2 arguments");
1010  ASTContext &Ctx = getContext();
1013  Address BufAddr = EmitPointerWithAlignment(E.getArg(0));
1014  llvm::SmallVector<llvm::Value *, 4> RetainableOperands;
1015 
1016  // Ignore argument 1, the format string. It is not currently used.
1017  CallArgList Args;
1018  Args.add(RValue::get(BufAddr.getPointer()), Ctx.VoidPtrTy);
1019 
1020  for (const auto &Item : Layout.Items) {
1021  int Size = Item.getSizeByte();
1022  if (!Size)
1023  continue;
1024 
1025  llvm::Value *ArgVal;
1026 
1027  if (const Expr *TheExpr = Item.getExpr()) {
1028  ArgVal = EmitScalarExpr(TheExpr, /*Ignore*/ false);
1029 
1030  // Check if this is a retainable type.
1031  if (TheExpr->getType()->isObjCRetainableType()) {
1032  assert(getEvaluationKind(TheExpr->getType()) == TEK_Scalar &&
1033  "Only scalar can be a ObjC retainable type");
1034  // Check if the object is constant, if not, save it in
1035  // RetainableOperands.
1036  if (!isa<Constant>(ArgVal))
1037  RetainableOperands.push_back(ArgVal);
1038  }
1039  } else {
1040  ArgVal = Builder.getInt32(Item.getConstValue().getQuantity());
1041  }
1042 
1043  unsigned ArgValSize =
1044  CGM.getDataLayout().getTypeSizeInBits(ArgVal->getType());
1045  llvm::IntegerType *IntTy = llvm::Type::getIntNTy(getLLVMContext(),
1046  ArgValSize);
1047  ArgVal = Builder.CreateBitOrPointerCast(ArgVal, IntTy);
1048  CanQualType ArgTy = getOSLogArgType(Ctx, Size);
1049  // If ArgVal has type x86_fp80, zero-extend ArgVal.
1050  ArgVal = Builder.CreateZExtOrBitCast(ArgVal, ConvertType(ArgTy));
1051  Args.add(RValue::get(ArgVal), ArgTy);
1052  }
1053 
1054  const CGFunctionInfo &FI =
1055  CGM.getTypes().arrangeBuiltinFunctionCall(Ctx.VoidTy, Args);
1056  llvm::Function *F = CodeGenFunction(CGM).generateBuiltinOSLogHelperFunction(
1057  Layout, BufAddr.getAlignment());
1058  EmitCall(FI, CGCallee::forDirect(F), ReturnValueSlot(), Args);
1059 
1060  // Push a clang.arc.use cleanup for each object in RetainableOperands. The
1061  // cleanup will cause the use to appear after the final log call, keeping
1062  // the object valid while it’s held in the log buffer. Note that if there’s
1063  // a release cleanup on the object, it will already be active; since
1064  // cleanups are emitted in reverse order, the use will occur before the
1065  // object is released.
1066  if (!RetainableOperands.empty() && getLangOpts().ObjCAutoRefCount &&
1067  CGM.getCodeGenOpts().OptimizationLevel != 0)
1068  for (llvm::Value *Object : RetainableOperands)
1069  pushFullExprCleanup<CallObjCArcUse>(getARCCleanupKind(), Object);
1070 
1071  return RValue::get(BufAddr.getPointer());
1072 }
1073 
1074 /// Determine if a binop is a checked mixed-sign multiply we can specialize.
1075 static bool isSpecialMixedSignMultiply(unsigned BuiltinID,
1076  WidthAndSignedness Op1Info,
1077  WidthAndSignedness Op2Info,
1078  WidthAndSignedness ResultInfo) {
1079  return BuiltinID == Builtin::BI__builtin_mul_overflow &&
1080  Op1Info.Width == Op2Info.Width && Op1Info.Width >= ResultInfo.Width &&
1081  Op1Info.Signed != Op2Info.Signed;
1082 }
1083 
1084 /// Emit a checked mixed-sign multiply. This is a cheaper specialization of
1085 /// the generic checked-binop irgen.
1086 static RValue
1088  WidthAndSignedness Op1Info, const clang::Expr *Op2,
1089  WidthAndSignedness Op2Info,
1090  const clang::Expr *ResultArg, QualType ResultQTy,
1091  WidthAndSignedness ResultInfo) {
1092  assert(isSpecialMixedSignMultiply(Builtin::BI__builtin_mul_overflow, Op1Info,
1093  Op2Info, ResultInfo) &&
1094  "Not a mixed-sign multipliction we can specialize");
1095 
1096  // Emit the signed and unsigned operands.
1097  const clang::Expr *SignedOp = Op1Info.Signed ? Op1 : Op2;
1098  const clang::Expr *UnsignedOp = Op1Info.Signed ? Op2 : Op1;
1099  llvm::Value *Signed = CGF.EmitScalarExpr(SignedOp);
1100  llvm::Value *Unsigned = CGF.EmitScalarExpr(UnsignedOp);
1101 
1102  llvm::Type *OpTy = Signed->getType();
1103  llvm::Value *Zero = llvm::Constant::getNullValue(OpTy);
1104  Address ResultPtr = CGF.EmitPointerWithAlignment(ResultArg);
1105  llvm::Type *ResTy = ResultPtr.getElementType();
1106 
1107  // Take the absolute value of the signed operand.
1108  llvm::Value *IsNegative = CGF.Builder.CreateICmpSLT(Signed, Zero);
1109  llvm::Value *AbsOfNegative = CGF.Builder.CreateSub(Zero, Signed);
1110  llvm::Value *AbsSigned =
1111  CGF.Builder.CreateSelect(IsNegative, AbsOfNegative, Signed);
1112 
1113  // Perform a checked unsigned multiplication.
1114  llvm::Value *UnsignedOverflow;
1115  llvm::Value *UnsignedResult =
1116  EmitOverflowIntrinsic(CGF, llvm::Intrinsic::umul_with_overflow, AbsSigned,
1117  Unsigned, UnsignedOverflow);
1118 
1119  llvm::Value *Overflow, *Result;
1120  if (ResultInfo.Signed) {
1121  // Signed overflow occurs if the result is greater than INT_MAX or lesser
1122  // than INT_MIN, i.e when |Result| > (INT_MAX + IsNegative).
1123  auto IntMax = llvm::APInt::getSignedMaxValue(ResultInfo.Width)
1124  .zextOrSelf(Op1Info.Width);
1125  llvm::Value *MaxResult =
1126  CGF.Builder.CreateAdd(llvm::ConstantInt::get(OpTy, IntMax),
1127  CGF.Builder.CreateZExt(IsNegative, OpTy));
1128  llvm::Value *SignedOverflow =
1129  CGF.Builder.CreateICmpUGT(UnsignedResult, MaxResult);
1130  Overflow = CGF.Builder.CreateOr(UnsignedOverflow, SignedOverflow);
1131 
1132  // Prepare the signed result (possibly by negating it).
1133  llvm::Value *NegativeResult = CGF.Builder.CreateNeg(UnsignedResult);
1134  llvm::Value *SignedResult =
1135  CGF.Builder.CreateSelect(IsNegative, NegativeResult, UnsignedResult);
1136  Result = CGF.Builder.CreateTrunc(SignedResult, ResTy);
1137  } else {
1138  // Unsigned overflow occurs if the result is < 0 or greater than UINT_MAX.
1139  llvm::Value *Underflow = CGF.Builder.CreateAnd(
1140  IsNegative, CGF.Builder.CreateIsNotNull(UnsignedResult));
1141  Overflow = CGF.Builder.CreateOr(UnsignedOverflow, Underflow);
1142  if (ResultInfo.Width < Op1Info.Width) {
1143  auto IntMax =
1144  llvm::APInt::getMaxValue(ResultInfo.Width).zext(Op1Info.Width);
1145  llvm::Value *TruncOverflow = CGF.Builder.CreateICmpUGT(
1146  UnsignedResult, llvm::ConstantInt::get(OpTy, IntMax));
1147  Overflow = CGF.Builder.CreateOr(Overflow, TruncOverflow);
1148  }
1149 
1150  // Negate the product if it would be negative in infinite precision.
1151  Result = CGF.Builder.CreateSelect(
1152  IsNegative, CGF.Builder.CreateNeg(UnsignedResult), UnsignedResult);
1153 
1154  Result = CGF.Builder.CreateTrunc(Result, ResTy);
1155  }
1156  assert(Overflow && Result && "Missing overflow or result");
1157 
1158  bool isVolatile =
1159  ResultArg->getType()->getPointeeType().isVolatileQualified();
1160  CGF.Builder.CreateStore(CGF.EmitToMemory(Result, ResultQTy), ResultPtr,
1161  isVolatile);
1162  return RValue::get(Overflow);
1163 }
1164 
1166  Value *&RecordPtr, CharUnits Align, Value *Func,
1167  int Lvl) {
1168  const auto *RT = RType->getAs<RecordType>();
1169  ASTContext &Context = CGF.getContext();
1170  RecordDecl *RD = RT->getDecl()->getDefinition();
1171  ASTContext &Ctx = RD->getASTContext();
1172  const ASTRecordLayout &RL = Ctx.getASTRecordLayout(RD);
1173  std::string Pad = std::string(Lvl * 4, ' ');
1174 
1175  Value *GString =
1176  CGF.Builder.CreateGlobalStringPtr(RType.getAsString() + " {\n");
1177  Value *Res = CGF.Builder.CreateCall(Func, {GString});
1178 
1179  static llvm::DenseMap<QualType, const char *> Types;
1180  if (Types.empty()) {
1181  Types[Context.CharTy] = "%c";
1182  Types[Context.BoolTy] = "%d";
1183  Types[Context.SignedCharTy] = "%hhd";
1184  Types[Context.UnsignedCharTy] = "%hhu";
1185  Types[Context.IntTy] = "%d";
1186  Types[Context.UnsignedIntTy] = "%u";
1187  Types[Context.LongTy] = "%ld";
1188  Types[Context.UnsignedLongTy] = "%lu";
1189  Types[Context.LongLongTy] = "%lld";
1190  Types[Context.UnsignedLongLongTy] = "%llu";
1191  Types[Context.ShortTy] = "%hd";
1192  Types[Context.UnsignedShortTy] = "%hu";
1193  Types[Context.VoidPtrTy] = "%p";
1194  Types[Context.FloatTy] = "%f";
1195  Types[Context.DoubleTy] = "%f";
1196  Types[Context.LongDoubleTy] = "%Lf";
1197  Types[Context.getPointerType(Context.CharTy)] = "%s";
1198  Types[Context.getPointerType(Context.getConstType(Context.CharTy))] = "%s";
1199  }
1200 
1201  for (const auto *FD : RD->fields()) {
1202  uint64_t Off = RL.getFieldOffset(FD->getFieldIndex());
1203  Off = Ctx.toCharUnitsFromBits(Off).getQuantity();
1204 
1205  Value *FieldPtr = RecordPtr;
1206  if (RD->isUnion())
1207  FieldPtr = CGF.Builder.CreatePointerCast(
1208  FieldPtr, CGF.ConvertType(Context.getPointerType(FD->getType())));
1209  else
1210  FieldPtr = CGF.Builder.CreateStructGEP(CGF.ConvertType(RType), FieldPtr,
1211  FD->getFieldIndex());
1212 
1213  GString = CGF.Builder.CreateGlobalStringPtr(
1214  llvm::Twine(Pad)
1215  .concat(FD->getType().getAsString())
1216  .concat(llvm::Twine(' '))
1217  .concat(FD->getNameAsString())
1218  .concat(" : ")
1219  .str());
1220  Value *TmpRes = CGF.Builder.CreateCall(Func, {GString});
1221  Res = CGF.Builder.CreateAdd(Res, TmpRes);
1222 
1223  QualType CanonicalType =
1224  FD->getType().getUnqualifiedType().getCanonicalType();
1225 
1226  // We check whether we are in a recursive type
1227  if (CanonicalType->isRecordType()) {
1228  Value *TmpRes =
1229  dumpRecord(CGF, CanonicalType, FieldPtr, Align, Func, Lvl + 1);
1230  Res = CGF.Builder.CreateAdd(TmpRes, Res);
1231  continue;
1232  }
1233 
1234  // We try to determine the best format to print the current field
1235  llvm::Twine Format = Types.find(CanonicalType) == Types.end()
1236  ? Types[Context.VoidPtrTy]
1237  : Types[CanonicalType];
1238 
1239  Address FieldAddress = Address(FieldPtr, Align);
1240  FieldPtr = CGF.Builder.CreateLoad(FieldAddress);
1241 
1242  // FIXME Need to handle bitfield here
1243  GString = CGF.Builder.CreateGlobalStringPtr(
1244  Format.concat(llvm::Twine('\n')).str());
1245  TmpRes = CGF.Builder.CreateCall(Func, {GString, FieldPtr});
1246  Res = CGF.Builder.CreateAdd(Res, TmpRes);
1247  }
1248 
1249  GString = CGF.Builder.CreateGlobalStringPtr(Pad + "}\n");
1250  Value *TmpRes = CGF.Builder.CreateCall(Func, {GString});
1251  Res = CGF.Builder.CreateAdd(Res, TmpRes);
1252  return Res;
1253 }
1254 
1256  unsigned BuiltinID, const CallExpr *E,
1257  ReturnValueSlot ReturnValue) {
1258  // See if we can constant fold this builtin. If so, don't emit it at all.
1259  Expr::EvalResult Result;
1260  if (E->EvaluateAsRValue(Result, CGM.getContext()) &&
1261  !Result.hasSideEffects()) {
1262  if (Result.Val.isInt())
1263  return RValue::get(llvm::ConstantInt::get(getLLVMContext(),
1264  Result.Val.getInt()));
1265  if (Result.Val.isFloat())
1266  return RValue::get(llvm::ConstantFP::get(getLLVMContext(),
1267  Result.Val.getFloat()));
1268  }
1269 
1270  // There are LLVM math intrinsics/instructions corresponding to math library
1271  // functions except the LLVM op will never set errno while the math library
1272  // might. Also, math builtins have the same semantics as their math library
1273  // twins. Thus, we can transform math library and builtin calls to their
1274  // LLVM counterparts if the call is marked 'const' (known to never set errno).
1275  if (FD->hasAttr<ConstAttr>()) {
1276  switch (BuiltinID) {
1277  case Builtin::BIceil:
1278  case Builtin::BIceilf:
1279  case Builtin::BIceill:
1280  case Builtin::BI__builtin_ceil:
1281  case Builtin::BI__builtin_ceilf:
1282  case Builtin::BI__builtin_ceill:
1283  return RValue::get(emitUnaryBuiltin(*this, E, Intrinsic::ceil));
1284 
1285  case Builtin::BIcopysign:
1286  case Builtin::BIcopysignf:
1287  case Builtin::BIcopysignl:
1288  case Builtin::BI__builtin_copysign:
1289  case Builtin::BI__builtin_copysignf:
1290  case Builtin::BI__builtin_copysignl:
1291  case Builtin::BI__builtin_copysignf128:
1293 
1294  case Builtin::BIcos:
1295  case Builtin::BIcosf:
1296  case Builtin::BIcosl:
1297  case Builtin::BI__builtin_cos:
1298  case Builtin::BI__builtin_cosf:
1299  case Builtin::BI__builtin_cosl:
1300  return RValue::get(emitUnaryBuiltin(*this, E, Intrinsic::cos));
1301 
1302  case Builtin::BIexp:
1303  case Builtin::BIexpf:
1304  case Builtin::BIexpl:
1305  case Builtin::BI__builtin_exp:
1306  case Builtin::BI__builtin_expf:
1307  case Builtin::BI__builtin_expl:
1308  return RValue::get(emitUnaryBuiltin(*this, E, Intrinsic::exp));
1309 
1310  case Builtin::BIexp2:
1311  case Builtin::BIexp2f:
1312  case Builtin::BIexp2l:
1313  case Builtin::BI__builtin_exp2:
1314  case Builtin::BI__builtin_exp2f:
1315  case Builtin::BI__builtin_exp2l:
1316  return RValue::get(emitUnaryBuiltin(*this, E, Intrinsic::exp2));
1317 
1318  case Builtin::BIfabs:
1319  case Builtin::BIfabsf:
1320  case Builtin::BIfabsl:
1321  case Builtin::BI__builtin_fabs:
1322  case Builtin::BI__builtin_fabsf:
1323  case Builtin::BI__builtin_fabsl:
1324  case Builtin::BI__builtin_fabsf128:
1325  return RValue::get(emitUnaryBuiltin(*this, E, Intrinsic::fabs));
1326 
1327  case Builtin::BIfloor:
1328  case Builtin::BIfloorf:
1329  case Builtin::BIfloorl:
1330  case Builtin::BI__builtin_floor:
1331  case Builtin::BI__builtin_floorf:
1332  case Builtin::BI__builtin_floorl:
1333  return RValue::get(emitUnaryBuiltin(*this, E, Intrinsic::floor));
1334 
1335  case Builtin::BIfma:
1336  case Builtin::BIfmaf:
1337  case Builtin::BIfmal:
1338  case Builtin::BI__builtin_fma:
1339  case Builtin::BI__builtin_fmaf:
1340  case Builtin::BI__builtin_fmal:
1341  return RValue::get(emitTernaryBuiltin(*this, E, Intrinsic::fma));
1342 
1343  case Builtin::BIfmax:
1344  case Builtin::BIfmaxf:
1345  case Builtin::BIfmaxl:
1346  case Builtin::BI__builtin_fmax:
1347  case Builtin::BI__builtin_fmaxf:
1348  case Builtin::BI__builtin_fmaxl:
1349  return RValue::get(emitBinaryBuiltin(*this, E, Intrinsic::maxnum));
1350 
1351  case Builtin::BIfmin:
1352  case Builtin::BIfminf:
1353  case Builtin::BIfminl:
1354  case Builtin::BI__builtin_fmin:
1355  case Builtin::BI__builtin_fminf:
1356  case Builtin::BI__builtin_fminl:
1357  return RValue::get(emitBinaryBuiltin(*this, E, Intrinsic::minnum));
1358 
1359  // fmod() is a special-case. It maps to the frem instruction rather than an
1360  // LLVM intrinsic.
1361  case Builtin::BIfmod:
1362  case Builtin::BIfmodf:
1363  case Builtin::BIfmodl:
1364  case Builtin::BI__builtin_fmod:
1365  case Builtin::BI__builtin_fmodf:
1366  case Builtin::BI__builtin_fmodl: {
1367  Value *Arg1 = EmitScalarExpr(E->getArg(0));
1368  Value *Arg2 = EmitScalarExpr(E->getArg(1));
1369  return RValue::get(Builder.CreateFRem(Arg1, Arg2, "fmod"));
1370  }
1371 
1372  case Builtin::BIlog:
1373  case Builtin::BIlogf:
1374  case Builtin::BIlogl:
1375  case Builtin::BI__builtin_log:
1376  case Builtin::BI__builtin_logf:
1377  case Builtin::BI__builtin_logl:
1378  return RValue::get(emitUnaryBuiltin(*this, E, Intrinsic::log));
1379 
1380  case Builtin::BIlog10:
1381  case Builtin::BIlog10f:
1382  case Builtin::BIlog10l:
1383  case Builtin::BI__builtin_log10:
1384  case Builtin::BI__builtin_log10f:
1385  case Builtin::BI__builtin_log10l:
1386  return RValue::get(emitUnaryBuiltin(*this, E, Intrinsic::log10));
1387 
1388  case Builtin::BIlog2:
1389  case Builtin::BIlog2f:
1390  case Builtin::BIlog2l:
1391  case Builtin::BI__builtin_log2:
1392  case Builtin::BI__builtin_log2f:
1393  case Builtin::BI__builtin_log2l:
1394  return RValue::get(emitUnaryBuiltin(*this, E, Intrinsic::log2));
1395 
1396  case Builtin::BInearbyint:
1397  case Builtin::BInearbyintf:
1398  case Builtin::BInearbyintl:
1399  case Builtin::BI__builtin_nearbyint:
1400  case Builtin::BI__builtin_nearbyintf:
1401  case Builtin::BI__builtin_nearbyintl:
1403 
1404  case Builtin::BIpow:
1405  case Builtin::BIpowf:
1406  case Builtin::BIpowl:
1407  case Builtin::BI__builtin_pow:
1408  case Builtin::BI__builtin_powf:
1409  case Builtin::BI__builtin_powl:
1410  return RValue::get(emitBinaryBuiltin(*this, E, Intrinsic::pow));
1411 
1412  case Builtin::BIrint:
1413  case Builtin::BIrintf:
1414  case Builtin::BIrintl:
1415  case Builtin::BI__builtin_rint:
1416  case Builtin::BI__builtin_rintf:
1417  case Builtin::BI__builtin_rintl:
1418  return RValue::get(emitUnaryBuiltin(*this, E, Intrinsic::rint));
1419 
1420  case Builtin::BIround:
1421  case Builtin::BIroundf:
1422  case Builtin::BIroundl:
1423  case Builtin::BI__builtin_round:
1424  case Builtin::BI__builtin_roundf:
1425  case Builtin::BI__builtin_roundl:
1426  return RValue::get(emitUnaryBuiltin(*this, E, Intrinsic::round));
1427 
1428  case Builtin::BIsin:
1429  case Builtin::BIsinf:
1430  case Builtin::BIsinl:
1431  case Builtin::BI__builtin_sin:
1432  case Builtin::BI__builtin_sinf:
1433  case Builtin::BI__builtin_sinl:
1434  return RValue::get(emitUnaryBuiltin(*this, E, Intrinsic::sin));
1435 
1436  case Builtin::BIsqrt:
1437  case Builtin::BIsqrtf:
1438  case Builtin::BIsqrtl:
1439  case Builtin::BI__builtin_sqrt:
1440  case Builtin::BI__builtin_sqrtf:
1441  case Builtin::BI__builtin_sqrtl:
1442  return RValue::get(emitUnaryBuiltin(*this, E, Intrinsic::sqrt));
1443 
1444  case Builtin::BItrunc:
1445  case Builtin::BItruncf:
1446  case Builtin::BItruncl:
1447  case Builtin::BI__builtin_trunc:
1448  case Builtin::BI__builtin_truncf:
1449  case Builtin::BI__builtin_truncl:
1450  return RValue::get(emitUnaryBuiltin(*this, E, Intrinsic::trunc));
1451 
1452  default:
1453  break;
1454  }
1455  }
1456 
1457  switch (BuiltinID) {
1458  default: break;
1459  case Builtin::BI__builtin___CFStringMakeConstantString:
1460  case Builtin::BI__builtin___NSStringMakeConstantString:
1461  return RValue::get(ConstantEmitter(*this).emitAbstract(E, E->getType()));
1462  case Builtin::BI__builtin_stdarg_start:
1463  case Builtin::BI__builtin_va_start:
1464  case Builtin::BI__va_start:
1465  case Builtin::BI__builtin_va_end:
1466  return RValue::get(
1467  EmitVAStartEnd(BuiltinID == Builtin::BI__va_start
1468  ? EmitScalarExpr(E->getArg(0))
1469  : EmitVAListRef(E->getArg(0)).getPointer(),
1470  BuiltinID != Builtin::BI__builtin_va_end));
1471  case Builtin::BI__builtin_va_copy: {
1472  Value *DstPtr = EmitVAListRef(E->getArg(0)).getPointer();
1473  Value *SrcPtr = EmitVAListRef(E->getArg(1)).getPointer();
1474 
1475  llvm::Type *Type = Int8PtrTy;
1476 
1477  DstPtr = Builder.CreateBitCast(DstPtr, Type);
1478  SrcPtr = Builder.CreateBitCast(SrcPtr, Type);
1479  return RValue::get(Builder.CreateCall(CGM.getIntrinsic(Intrinsic::vacopy),
1480  {DstPtr, SrcPtr}));
1481  }
1482  case Builtin::BI__builtin_abs:
1483  case Builtin::BI__builtin_labs:
1484  case Builtin::BI__builtin_llabs: {
1485  // X < 0 ? -X : X
1486  // The negation has 'nsw' because abs of INT_MIN is undefined.
1487  Value *ArgValue = EmitScalarExpr(E->getArg(0));
1488  Value *NegOp = Builder.CreateNSWNeg(ArgValue, "neg");
1489  Constant *Zero = llvm::Constant::getNullValue(ArgValue->getType());
1490  Value *CmpResult = Builder.CreateICmpSLT(ArgValue, Zero, "abscond");
1491  Value *Result = Builder.CreateSelect(CmpResult, NegOp, ArgValue, "abs");
1492  return RValue::get(Result);
1493  }
1494  case Builtin::BI__builtin_conj:
1495  case Builtin::BI__builtin_conjf:
1496  case Builtin::BI__builtin_conjl: {
1497  ComplexPairTy ComplexVal = EmitComplexExpr(E->getArg(0));
1498  Value *Real = ComplexVal.first;
1499  Value *Imag = ComplexVal.second;
1500  Value *Zero =
1501  Imag->getType()->isFPOrFPVectorTy()
1502  ? llvm::ConstantFP::getZeroValueForNegation(Imag->getType())
1503  : llvm::Constant::getNullValue(Imag->getType());
1504 
1505  Imag = Builder.CreateFSub(Zero, Imag, "sub");
1506  return RValue::getComplex(std::make_pair(Real, Imag));
1507  }
1508  case Builtin::BI__builtin_creal:
1509  case Builtin::BI__builtin_crealf:
1510  case Builtin::BI__builtin_creall:
1511  case Builtin::BIcreal:
1512  case Builtin::BIcrealf:
1513  case Builtin::BIcreall: {
1514  ComplexPairTy ComplexVal = EmitComplexExpr(E->getArg(0));
1515  return RValue::get(ComplexVal.first);
1516  }
1517 
1518  case Builtin::BI__builtin_dump_struct: {
1519  Value *Func = EmitScalarExpr(E->getArg(1)->IgnoreImpCasts());
1520  CharUnits Arg0Align = EmitPointerWithAlignment(E->getArg(0)).getAlignment();
1521 
1522  const Expr *Arg0 = E->getArg(0)->IgnoreImpCasts();
1523  QualType Arg0Type = Arg0->getType()->getPointeeType();
1524 
1525  Value *RecordPtr = EmitScalarExpr(Arg0);
1526  Value *Res = dumpRecord(*this, Arg0Type, RecordPtr, Arg0Align, Func, 0);
1527  return RValue::get(Res);
1528  }
1529 
1530  case Builtin::BI__builtin_cimag:
1531  case Builtin::BI__builtin_cimagf:
1532  case Builtin::BI__builtin_cimagl:
1533  case Builtin::BIcimag:
1534  case Builtin::BIcimagf:
1535  case Builtin::BIcimagl: {
1536  ComplexPairTy ComplexVal = EmitComplexExpr(E->getArg(0));
1537  return RValue::get(ComplexVal.second);
1538  }
1539 
1540  case Builtin::BI__builtin_clrsb:
1541  case Builtin::BI__builtin_clrsbl:
1542  case Builtin::BI__builtin_clrsbll: {
1543  // clrsb(x) -> clz(x < 0 ? ~x : x) - 1 or
1544  Value *ArgValue = EmitScalarExpr(E->getArg(0));
1545 
1546  llvm::Type *ArgType = ArgValue->getType();
1547  Value *F = CGM.getIntrinsic(Intrinsic::ctlz, ArgType);
1548 
1549  llvm::Type *ResultType = ConvertType(E->getType());
1550  Value *Zero = llvm::Constant::getNullValue(ArgType);
1551  Value *IsNeg = Builder.CreateICmpSLT(ArgValue, Zero, "isneg");
1552  Value *Inverse = Builder.CreateNot(ArgValue, "not");
1553  Value *Tmp = Builder.CreateSelect(IsNeg, Inverse, ArgValue);
1554  Value *Ctlz = Builder.CreateCall(F, {Tmp, Builder.getFalse()});
1555  Value *Result = Builder.CreateSub(Ctlz, llvm::ConstantInt::get(ArgType, 1));
1556  Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true,
1557  "cast");
1558  return RValue::get(Result);
1559  }
1560  case Builtin::BI__builtin_ctzs:
1561  case Builtin::BI__builtin_ctz:
1562  case Builtin::BI__builtin_ctzl:
1563  case Builtin::BI__builtin_ctzll: {
1564  Value *ArgValue = EmitCheckedArgForBuiltin(E->getArg(0), BCK_CTZPassedZero);
1565 
1566  llvm::Type *ArgType = ArgValue->getType();
1567  Value *F = CGM.getIntrinsic(Intrinsic::cttz, ArgType);
1568 
1569  llvm::Type *ResultType = ConvertType(E->getType());
1570  Value *ZeroUndef = Builder.getInt1(getTarget().isCLZForZeroUndef());
1571  Value *Result = Builder.CreateCall(F, {ArgValue, ZeroUndef});
1572  if (Result->getType() != ResultType)
1573  Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true,
1574  "cast");
1575  return RValue::get(Result);
1576  }
1577  case Builtin::BI__builtin_clzs:
1578  case Builtin::BI__builtin_clz:
1579  case Builtin::BI__builtin_clzl:
1580  case Builtin::BI__builtin_clzll: {
1581  Value *ArgValue = EmitCheckedArgForBuiltin(E->getArg(0), BCK_CLZPassedZero);
1582 
1583  llvm::Type *ArgType = ArgValue->getType();
1584  Value *F = CGM.getIntrinsic(Intrinsic::ctlz, ArgType);
1585 
1586  llvm::Type *ResultType = ConvertType(E->getType());
1587  Value *ZeroUndef = Builder.getInt1(getTarget().isCLZForZeroUndef());
1588  Value *Result = Builder.CreateCall(F, {ArgValue, ZeroUndef});
1589  if (Result->getType() != ResultType)
1590  Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true,
1591  "cast");
1592  return RValue::get(Result);
1593  }
1594  case Builtin::BI__builtin_ffs:
1595  case Builtin::BI__builtin_ffsl:
1596  case Builtin::BI__builtin_ffsll: {
1597  // ffs(x) -> x ? cttz(x) + 1 : 0
1598  Value *ArgValue = EmitScalarExpr(E->getArg(0));
1599 
1600  llvm::Type *ArgType = ArgValue->getType();
1601  Value *F = CGM.getIntrinsic(Intrinsic::cttz, ArgType);
1602 
1603  llvm::Type *ResultType = ConvertType(E->getType());
1604  Value *Tmp =
1605  Builder.CreateAdd(Builder.CreateCall(F, {ArgValue, Builder.getTrue()}),
1606  llvm::ConstantInt::get(ArgType, 1));
1607  Value *Zero = llvm::Constant::getNullValue(ArgType);
1608  Value *IsZero = Builder.CreateICmpEQ(ArgValue, Zero, "iszero");
1609  Value *Result = Builder.CreateSelect(IsZero, Zero, Tmp, "ffs");
1610  if (Result->getType() != ResultType)
1611  Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true,
1612  "cast");
1613  return RValue::get(Result);
1614  }
1615  case Builtin::BI__builtin_parity:
1616  case Builtin::BI__builtin_parityl:
1617  case Builtin::BI__builtin_parityll: {
1618  // parity(x) -> ctpop(x) & 1
1619  Value *ArgValue = EmitScalarExpr(E->getArg(0));
1620 
1621  llvm::Type *ArgType = ArgValue->getType();
1622  Value *F = CGM.getIntrinsic(Intrinsic::ctpop, ArgType);
1623 
1624  llvm::Type *ResultType = ConvertType(E->getType());
1625  Value *Tmp = Builder.CreateCall(F, ArgValue);
1626  Value *Result = Builder.CreateAnd(Tmp, llvm::ConstantInt::get(ArgType, 1));
1627  if (Result->getType() != ResultType)
1628  Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true,
1629  "cast");
1630  return RValue::get(Result);
1631  }
1632  case Builtin::BI__popcnt16:
1633  case Builtin::BI__popcnt:
1634  case Builtin::BI__popcnt64:
1635  case Builtin::BI__builtin_popcount:
1636  case Builtin::BI__builtin_popcountl:
1637  case Builtin::BI__builtin_popcountll: {
1638  Value *ArgValue = EmitScalarExpr(E->getArg(0));
1639 
1640  llvm::Type *ArgType = ArgValue->getType();
1641  Value *F = CGM.getIntrinsic(Intrinsic::ctpop, ArgType);
1642 
1643  llvm::Type *ResultType = ConvertType(E->getType());
1644  Value *Result = Builder.CreateCall(F, ArgValue);
1645  if (Result->getType() != ResultType)
1646  Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true,
1647  "cast");
1648  return RValue::get(Result);
1649  }
1650  case Builtin::BI_rotr8:
1651  case Builtin::BI_rotr16:
1652  case Builtin::BI_rotr:
1653  case Builtin::BI_lrotr:
1654  case Builtin::BI_rotr64: {
1655  Value *Val = EmitScalarExpr(E->getArg(0));
1656  Value *Shift = EmitScalarExpr(E->getArg(1));
1657 
1658  llvm::Type *ArgType = Val->getType();
1659  Shift = Builder.CreateIntCast(Shift, ArgType, false);
1660  unsigned ArgWidth = ArgType->getIntegerBitWidth();
1661  Value *Mask = llvm::ConstantInt::get(ArgType, ArgWidth - 1);
1662 
1663  Value *RightShiftAmt = Builder.CreateAnd(Shift, Mask);
1664  Value *RightShifted = Builder.CreateLShr(Val, RightShiftAmt);
1665  Value *LeftShiftAmt = Builder.CreateAnd(Builder.CreateNeg(Shift), Mask);
1666  Value *LeftShifted = Builder.CreateShl(Val, LeftShiftAmt);
1667  Value *Result = Builder.CreateOr(LeftShifted, RightShifted);
1668  return RValue::get(Result);
1669  }
1670  case Builtin::BI_rotl8:
1671  case Builtin::BI_rotl16:
1672  case Builtin::BI_rotl:
1673  case Builtin::BI_lrotl:
1674  case Builtin::BI_rotl64: {
1675  Value *Val = EmitScalarExpr(E->getArg(0));
1676  Value *Shift = EmitScalarExpr(E->getArg(1));
1677 
1678  llvm::Type *ArgType = Val->getType();
1679  Shift = Builder.CreateIntCast(Shift, ArgType, false);
1680  unsigned ArgWidth = ArgType->getIntegerBitWidth();
1681  Value *Mask = llvm::ConstantInt::get(ArgType, ArgWidth - 1);
1682 
1683  Value *LeftShiftAmt = Builder.CreateAnd(Shift, Mask);
1684  Value *LeftShifted = Builder.CreateShl(Val, LeftShiftAmt);
1685  Value *RightShiftAmt = Builder.CreateAnd(Builder.CreateNeg(Shift), Mask);
1686  Value *RightShifted = Builder.CreateLShr(Val, RightShiftAmt);
1687  Value *Result = Builder.CreateOr(LeftShifted, RightShifted);
1688  return RValue::get(Result);
1689  }
1690  case Builtin::BI__builtin_unpredictable: {
1691  // Always return the argument of __builtin_unpredictable. LLVM does not
1692  // handle this builtin. Metadata for this builtin should be added directly
1693  // to instructions such as branches or switches that use it.
1694  return RValue::get(EmitScalarExpr(E->getArg(0)));
1695  }
1696  case Builtin::BI__builtin_expect: {
1697  Value *ArgValue = EmitScalarExpr(E->getArg(0));
1698  llvm::Type *ArgType = ArgValue->getType();
1699 
1700  Value *ExpectedValue = EmitScalarExpr(E->getArg(1));
1701  // Don't generate llvm.expect on -O0 as the backend won't use it for
1702  // anything.
1703  // Note, we still IRGen ExpectedValue because it could have side-effects.
1704  if (CGM.getCodeGenOpts().OptimizationLevel == 0)
1705  return RValue::get(ArgValue);
1706 
1707  Value *FnExpect = CGM.getIntrinsic(Intrinsic::expect, ArgType);
1708  Value *Result =
1709  Builder.CreateCall(FnExpect, {ArgValue, ExpectedValue}, "expval");
1710  return RValue::get(Result);
1711  }
1712  case Builtin::BI__builtin_assume_aligned: {
1713  Value *PtrValue = EmitScalarExpr(E->getArg(0));
1714  Value *OffsetValue =
1715  (E->getNumArgs() > 2) ? EmitScalarExpr(E->getArg(2)) : nullptr;
1716 
1717  Value *AlignmentValue = EmitScalarExpr(E->getArg(1));
1718  ConstantInt *AlignmentCI = cast<ConstantInt>(AlignmentValue);
1719  unsigned Alignment = (unsigned) AlignmentCI->getZExtValue();
1720 
1721  EmitAlignmentAssumption(PtrValue, Alignment, OffsetValue);
1722  return RValue::get(PtrValue);
1723  }
1724  case Builtin::BI__assume:
1725  case Builtin::BI__builtin_assume: {
1726  if (E->getArg(0)->HasSideEffects(getContext()))
1727  return RValue::get(nullptr);
1728 
1729  Value *ArgValue = EmitScalarExpr(E->getArg(0));
1730  Value *FnAssume = CGM.getIntrinsic(Intrinsic::assume);
1731  return RValue::get(Builder.CreateCall(FnAssume, ArgValue));
1732  }
1733  case Builtin::BI__builtin_bswap16:
1734  case Builtin::BI__builtin_bswap32:
1735  case Builtin::BI__builtin_bswap64: {
1736  return RValue::get(emitUnaryBuiltin(*this, E, Intrinsic::bswap));
1737  }
1738  case Builtin::BI__builtin_bitreverse8:
1739  case Builtin::BI__builtin_bitreverse16:
1740  case Builtin::BI__builtin_bitreverse32:
1741  case Builtin::BI__builtin_bitreverse64: {
1742  return RValue::get(emitUnaryBuiltin(*this, E, Intrinsic::bitreverse));
1743  }
1744  case Builtin::BI__builtin_object_size: {
1745  unsigned Type =
1746  E->getArg(1)->EvaluateKnownConstInt(getContext()).getZExtValue();
1747  auto *ResType = cast<llvm::IntegerType>(ConvertType(E->getType()));
1748 
1749  // We pass this builtin onto the optimizer so that it can figure out the
1750  // object size in more complex cases.
1751  return RValue::get(emitBuiltinObjectSize(E->getArg(0), Type, ResType,
1752  /*EmittedE=*/nullptr));
1753  }
1754  case Builtin::BI__builtin_prefetch: {
1755  Value *Locality, *RW, *Address = EmitScalarExpr(E->getArg(0));
1756  // FIXME: Technically these constants should of type 'int', yes?
1757  RW = (E->getNumArgs() > 1) ? EmitScalarExpr(E->getArg(1)) :
1758  llvm::ConstantInt::get(Int32Ty, 0);
1759  Locality = (E->getNumArgs() > 2) ? EmitScalarExpr(E->getArg(2)) :
1760  llvm::ConstantInt::get(Int32Ty, 3);
1761  Value *Data = llvm::ConstantInt::get(Int32Ty, 1);
1762  Value *F = CGM.getIntrinsic(Intrinsic::prefetch);
1763  return RValue::get(Builder.CreateCall(F, {Address, RW, Locality, Data}));
1764  }
1765  case Builtin::BI__builtin_readcyclecounter: {
1766  Value *F = CGM.getIntrinsic(Intrinsic::readcyclecounter);
1767  return RValue::get(Builder.CreateCall(F));
1768  }
1769  case Builtin::BI__builtin___clear_cache: {
1770  Value *Begin = EmitScalarExpr(E->getArg(0));
1771  Value *End = EmitScalarExpr(E->getArg(1));
1772  Value *F = CGM.getIntrinsic(Intrinsic::clear_cache);
1773  return RValue::get(Builder.CreateCall(F, {Begin, End}));
1774  }
1775  case Builtin::BI__builtin_trap:
1776  return RValue::get(EmitTrapCall(Intrinsic::trap));
1777  case Builtin::BI__debugbreak:
1778  return RValue::get(EmitTrapCall(Intrinsic::debugtrap));
1779  case Builtin::BI__builtin_unreachable: {
1780  EmitUnreachable(E->getExprLoc());
1781 
1782  // We do need to preserve an insertion point.
1783  EmitBlock(createBasicBlock("unreachable.cont"));
1784 
1785  return RValue::get(nullptr);
1786  }
1787 
1788  case Builtin::BI__builtin_powi:
1789  case Builtin::BI__builtin_powif:
1790  case Builtin::BI__builtin_powil: {
1791  Value *Base = EmitScalarExpr(E->getArg(0));
1792  Value *Exponent = EmitScalarExpr(E->getArg(1));
1793  llvm::Type *ArgType = Base->getType();
1794  Value *F = CGM.getIntrinsic(Intrinsic::powi, ArgType);
1795  return RValue::get(Builder.CreateCall(F, {Base, Exponent}));
1796  }
1797 
1798  case Builtin::BI__builtin_isgreater:
1799  case Builtin::BI__builtin_isgreaterequal:
1800  case Builtin::BI__builtin_isless:
1801  case Builtin::BI__builtin_islessequal:
1802  case Builtin::BI__builtin_islessgreater:
1803  case Builtin::BI__builtin_isunordered: {
1804  // Ordered comparisons: we know the arguments to these are matching scalar
1805  // floating point values.
1806  Value *LHS = EmitScalarExpr(E->getArg(0));
1807  Value *RHS = EmitScalarExpr(E->getArg(1));
1808 
1809  switch (BuiltinID) {
1810  default: llvm_unreachable("Unknown ordered comparison");
1811  case Builtin::BI__builtin_isgreater:
1812  LHS = Builder.CreateFCmpOGT(LHS, RHS, "cmp");
1813  break;
1814  case Builtin::BI__builtin_isgreaterequal:
1815  LHS = Builder.CreateFCmpOGE(LHS, RHS, "cmp");
1816  break;
1817  case Builtin::BI__builtin_isless:
1818  LHS = Builder.CreateFCmpOLT(LHS, RHS, "cmp");
1819  break;
1820  case Builtin::BI__builtin_islessequal:
1821  LHS = Builder.CreateFCmpOLE(LHS, RHS, "cmp");
1822  break;
1823  case Builtin::BI__builtin_islessgreater:
1824  LHS = Builder.CreateFCmpONE(LHS, RHS, "cmp");
1825  break;
1826  case Builtin::BI__builtin_isunordered:
1827  LHS = Builder.CreateFCmpUNO(LHS, RHS, "cmp");
1828  break;
1829  }
1830  // ZExt bool to int type.
1831  return RValue::get(Builder.CreateZExt(LHS, ConvertType(E->getType())));
1832  }
1833  case Builtin::BI__builtin_isnan: {
1834  Value *V = EmitScalarExpr(E->getArg(0));
1835  V = Builder.CreateFCmpUNO(V, V, "cmp");
1836  return RValue::get(Builder.CreateZExt(V, ConvertType(E->getType())));
1837  }
1838 
1839  case Builtin::BIfinite:
1840  case Builtin::BI__finite:
1841  case Builtin::BIfinitef:
1842  case Builtin::BI__finitef:
1843  case Builtin::BIfinitel:
1844  case Builtin::BI__finitel:
1845  case Builtin::BI__builtin_isinf:
1846  case Builtin::BI__builtin_isfinite: {
1847  // isinf(x) --> fabs(x) == infinity
1848  // isfinite(x) --> fabs(x) != infinity
1849  // x != NaN via the ordered compare in either case.
1850  Value *V = EmitScalarExpr(E->getArg(0));
1851  Value *Fabs = EmitFAbs(*this, V);
1852  Constant *Infinity = ConstantFP::getInfinity(V->getType());
1853  CmpInst::Predicate Pred = (BuiltinID == Builtin::BI__builtin_isinf)
1854  ? CmpInst::FCMP_OEQ
1855  : CmpInst::FCMP_ONE;
1856  Value *FCmp = Builder.CreateFCmp(Pred, Fabs, Infinity, "cmpinf");
1857  return RValue::get(Builder.CreateZExt(FCmp, ConvertType(E->getType())));
1858  }
1859 
1860  case Builtin::BI__builtin_isinf_sign: {
1861  // isinf_sign(x) -> fabs(x) == infinity ? (signbit(x) ? -1 : 1) : 0
1862  Value *Arg = EmitScalarExpr(E->getArg(0));
1863  Value *AbsArg = EmitFAbs(*this, Arg);
1864  Value *IsInf = Builder.CreateFCmpOEQ(
1865  AbsArg, ConstantFP::getInfinity(Arg->getType()), "isinf");
1866  Value *IsNeg = EmitSignBit(*this, Arg);
1867 
1868  llvm::Type *IntTy = ConvertType(E->getType());
1869  Value *Zero = Constant::getNullValue(IntTy);
1870  Value *One = ConstantInt::get(IntTy, 1);
1871  Value *NegativeOne = ConstantInt::get(IntTy, -1);
1872  Value *SignResult = Builder.CreateSelect(IsNeg, NegativeOne, One);
1873  Value *Result = Builder.CreateSelect(IsInf, SignResult, Zero);
1874  return RValue::get(Result);
1875  }
1876 
1877  case Builtin::BI__builtin_isnormal: {
1878  // isnormal(x) --> x == x && fabsf(x) < infinity && fabsf(x) >= float_min
1879  Value *V = EmitScalarExpr(E->getArg(0));
1880  Value *Eq = Builder.CreateFCmpOEQ(V, V, "iseq");
1881 
1882  Value *Abs = EmitFAbs(*this, V);
1883  Value *IsLessThanInf =
1884  Builder.CreateFCmpULT(Abs, ConstantFP::getInfinity(V->getType()),"isinf");
1885  APFloat Smallest = APFloat::getSmallestNormalized(
1886  getContext().getFloatTypeSemantics(E->getArg(0)->getType()));
1887  Value *IsNormal =
1888  Builder.CreateFCmpUGE(Abs, ConstantFP::get(V->getContext(), Smallest),
1889  "isnormal");
1890  V = Builder.CreateAnd(Eq, IsLessThanInf, "and");
1891  V = Builder.CreateAnd(V, IsNormal, "and");
1892  return RValue::get(Builder.CreateZExt(V, ConvertType(E->getType())));
1893  }
1894 
1895  case Builtin::BI__builtin_fpclassify: {
1896  Value *V = EmitScalarExpr(E->getArg(5));
1897  llvm::Type *Ty = ConvertType(E->getArg(5)->getType());
1898 
1899  // Create Result
1900  BasicBlock *Begin = Builder.GetInsertBlock();
1901  BasicBlock *End = createBasicBlock("fpclassify_end", this->CurFn);
1902  Builder.SetInsertPoint(End);
1903  PHINode *Result =
1904  Builder.CreatePHI(ConvertType(E->getArg(0)->getType()), 4,
1905  "fpclassify_result");
1906 
1907  // if (V==0) return FP_ZERO
1908  Builder.SetInsertPoint(Begin);
1909  Value *IsZero = Builder.CreateFCmpOEQ(V, Constant::getNullValue(Ty),
1910  "iszero");
1911  Value *ZeroLiteral = EmitScalarExpr(E->getArg(4));
1912  BasicBlock *NotZero = createBasicBlock("fpclassify_not_zero", this->CurFn);
1913  Builder.CreateCondBr(IsZero, End, NotZero);
1914  Result->addIncoming(ZeroLiteral, Begin);
1915 
1916  // if (V != V) return FP_NAN
1917  Builder.SetInsertPoint(NotZero);
1918  Value *IsNan = Builder.CreateFCmpUNO(V, V, "cmp");
1919  Value *NanLiteral = EmitScalarExpr(E->getArg(0));
1920  BasicBlock *NotNan = createBasicBlock("fpclassify_not_nan", this->CurFn);
1921  Builder.CreateCondBr(IsNan, End, NotNan);
1922  Result->addIncoming(NanLiteral, NotZero);
1923 
1924  // if (fabs(V) == infinity) return FP_INFINITY
1925  Builder.SetInsertPoint(NotNan);
1926  Value *VAbs = EmitFAbs(*this, V);
1927  Value *IsInf =
1928  Builder.CreateFCmpOEQ(VAbs, ConstantFP::getInfinity(V->getType()),
1929  "isinf");
1930  Value *InfLiteral = EmitScalarExpr(E->getArg(1));
1931  BasicBlock *NotInf = createBasicBlock("fpclassify_not_inf", this->CurFn);
1932  Builder.CreateCondBr(IsInf, End, NotInf);
1933  Result->addIncoming(InfLiteral, NotNan);
1934 
1935  // if (fabs(V) >= MIN_NORMAL) return FP_NORMAL else FP_SUBNORMAL
1936  Builder.SetInsertPoint(NotInf);
1937  APFloat Smallest = APFloat::getSmallestNormalized(
1938  getContext().getFloatTypeSemantics(E->getArg(5)->getType()));
1939  Value *IsNormal =
1940  Builder.CreateFCmpUGE(VAbs, ConstantFP::get(V->getContext(), Smallest),
1941  "isnormal");
1942  Value *NormalResult =
1943  Builder.CreateSelect(IsNormal, EmitScalarExpr(E->getArg(2)),
1944  EmitScalarExpr(E->getArg(3)));
1945  Builder.CreateBr(End);
1946  Result->addIncoming(NormalResult, NotInf);
1947 
1948  // return Result
1949  Builder.SetInsertPoint(End);
1950  return RValue::get(Result);
1951  }
1952 
1953  case Builtin::BIalloca:
1954  case Builtin::BI_alloca:
1955  case Builtin::BI__builtin_alloca: {
1956  Value *Size = EmitScalarExpr(E->getArg(0));
1957  const TargetInfo &TI = getContext().getTargetInfo();
1958  // The alignment of the alloca should correspond to __BIGGEST_ALIGNMENT__.
1959  unsigned SuitableAlignmentInBytes =
1960  CGM.getContext()
1961  .toCharUnitsFromBits(TI.getSuitableAlign())
1962  .getQuantity();
1963  AllocaInst *AI = Builder.CreateAlloca(Builder.getInt8Ty(), Size);
1964  AI->setAlignment(SuitableAlignmentInBytes);
1965  return RValue::get(AI);
1966  }
1967 
1968  case Builtin::BI__builtin_alloca_with_align: {
1969  Value *Size = EmitScalarExpr(E->getArg(0));
1970  Value *AlignmentInBitsValue = EmitScalarExpr(E->getArg(1));
1971  auto *AlignmentInBitsCI = cast<ConstantInt>(AlignmentInBitsValue);
1972  unsigned AlignmentInBits = AlignmentInBitsCI->getZExtValue();
1973  unsigned AlignmentInBytes =
1974  CGM.getContext().toCharUnitsFromBits(AlignmentInBits).getQuantity();
1975  AllocaInst *AI = Builder.CreateAlloca(Builder.getInt8Ty(), Size);
1976  AI->setAlignment(AlignmentInBytes);
1977  return RValue::get(AI);
1978  }
1979 
1980  case Builtin::BIbzero:
1981  case Builtin::BI__builtin_bzero: {
1982  Address Dest = EmitPointerWithAlignment(E->getArg(0));
1983  Value *SizeVal = EmitScalarExpr(E->getArg(1));
1984  EmitNonNullArgCheck(RValue::get(Dest.getPointer()), E->getArg(0)->getType(),
1985  E->getArg(0)->getExprLoc(), FD, 0);
1986  Builder.CreateMemSet(Dest, Builder.getInt8(0), SizeVal, false);
1987  return RValue::get(nullptr);
1988  }
1989  case Builtin::BImemcpy:
1990  case Builtin::BI__builtin_memcpy: {
1991  Address Dest = EmitPointerWithAlignment(E->getArg(0));
1992  Address Src = EmitPointerWithAlignment(E->getArg(1));
1993  Value *SizeVal = EmitScalarExpr(E->getArg(2));
1994  EmitNonNullArgCheck(RValue::get(Dest.getPointer()), E->getArg(0)->getType(),
1995  E->getArg(0)->getExprLoc(), FD, 0);
1996  EmitNonNullArgCheck(RValue::get(Src.getPointer()), E->getArg(1)->getType(),
1997  E->getArg(1)->getExprLoc(), FD, 1);
1998  Builder.CreateMemCpy(Dest, Src, SizeVal, false);
1999  return RValue::get(Dest.getPointer());
2000  }
2001 
2002  case Builtin::BI__builtin_char_memchr:
2003  BuiltinID = Builtin::BI__builtin_memchr;
2004  break;
2005 
2006  case Builtin::BI__builtin___memcpy_chk: {
2007  // fold __builtin_memcpy_chk(x, y, cst1, cst2) to memcpy iff cst1<=cst2.
2008  llvm::APSInt Size, DstSize;
2009  if (!E->getArg(2)->EvaluateAsInt(Size, CGM.getContext()) ||
2010  !E->getArg(3)->EvaluateAsInt(DstSize, CGM.getContext()))
2011  break;
2012  if (Size.ugt(DstSize))
2013  break;
2014  Address Dest = EmitPointerWithAlignment(E->getArg(0));
2015  Address Src = EmitPointerWithAlignment(E->getArg(1));
2016  Value *SizeVal = llvm::ConstantInt::get(Builder.getContext(), Size);
2017  Builder.CreateMemCpy(Dest, Src, SizeVal, false);
2018  return RValue::get(Dest.getPointer());
2019  }
2020 
2021  case Builtin::BI__builtin_objc_memmove_collectable: {
2022  Address DestAddr = EmitPointerWithAlignment(E->getArg(0));
2023  Address SrcAddr = EmitPointerWithAlignment(E->getArg(1));
2024  Value *SizeVal = EmitScalarExpr(E->getArg(2));
2025  CGM.getObjCRuntime().EmitGCMemmoveCollectable(*this,
2026  DestAddr, SrcAddr, SizeVal);
2027  return RValue::get(DestAddr.getPointer());
2028  }
2029 
2030  case Builtin::BI__builtin___memmove_chk: {
2031  // fold __builtin_memmove_chk(x, y, cst1, cst2) to memmove iff cst1<=cst2.
2032  llvm::APSInt Size, DstSize;
2033  if (!E->getArg(2)->EvaluateAsInt(Size, CGM.getContext()) ||
2034  !E->getArg(3)->EvaluateAsInt(DstSize, CGM.getContext()))
2035  break;
2036  if (Size.ugt(DstSize))
2037  break;
2038  Address Dest = EmitPointerWithAlignment(E->getArg(0));
2039  Address Src = EmitPointerWithAlignment(E->getArg(1));
2040  Value *SizeVal = llvm::ConstantInt::get(Builder.getContext(), Size);
2041  Builder.CreateMemMove(Dest, Src, SizeVal, false);
2042  return RValue::get(Dest.getPointer());
2043  }
2044 
2045  case Builtin::BImemmove:
2046  case Builtin::BI__builtin_memmove: {
2047  Address Dest = EmitPointerWithAlignment(E->getArg(0));
2048  Address Src = EmitPointerWithAlignment(E->getArg(1));
2049  Value *SizeVal = EmitScalarExpr(E->getArg(2));
2050  EmitNonNullArgCheck(RValue::get(Dest.getPointer()), E->getArg(0)->getType(),
2051  E->getArg(0)->getExprLoc(), FD, 0);
2052  EmitNonNullArgCheck(RValue::get(Src.getPointer()), E->getArg(1)->getType(),
2053  E->getArg(1)->getExprLoc(), FD, 1);
2054  Builder.CreateMemMove(Dest, Src, SizeVal, false);
2055  return RValue::get(Dest.getPointer());
2056  }
2057  case Builtin::BImemset:
2058  case Builtin::BI__builtin_memset: {
2059  Address Dest = EmitPointerWithAlignment(E->getArg(0));
2060  Value *ByteVal = Builder.CreateTrunc(EmitScalarExpr(E->getArg(1)),
2061  Builder.getInt8Ty());
2062  Value *SizeVal = EmitScalarExpr(E->getArg(2));
2063  EmitNonNullArgCheck(RValue::get(Dest.getPointer()), E->getArg(0)->getType(),
2064  E->getArg(0)->getExprLoc(), FD, 0);
2065  Builder.CreateMemSet(Dest, ByteVal, SizeVal, false);
2066  return RValue::get(Dest.getPointer());
2067  }
2068  case Builtin::BI__builtin___memset_chk: {
2069  // fold __builtin_memset_chk(x, y, cst1, cst2) to memset iff cst1<=cst2.
2070  llvm::APSInt Size, DstSize;
2071  if (!E->getArg(2)->EvaluateAsInt(Size, CGM.getContext()) ||
2072  !E->getArg(3)->EvaluateAsInt(DstSize, CGM.getContext()))
2073  break;
2074  if (Size.ugt(DstSize))
2075  break;
2076  Address Dest = EmitPointerWithAlignment(E->getArg(0));
2077  Value *ByteVal = Builder.CreateTrunc(EmitScalarExpr(E->getArg(1)),
2078  Builder.getInt8Ty());
2079  Value *SizeVal = llvm::ConstantInt::get(Builder.getContext(), Size);
2080  Builder.CreateMemSet(Dest, ByteVal, SizeVal, false);
2081  return RValue::get(Dest.getPointer());
2082  }
2083  case Builtin::BI__builtin_wmemcmp: {
2084  // The MSVC runtime library does not provide a definition of wmemcmp, so we
2085  // need an inline implementation.
2086  if (!getTarget().getTriple().isOSMSVCRT())
2087  break;
2088 
2089  llvm::Type *WCharTy = ConvertType(getContext().WCharTy);
2090 
2091  Value *Dst = EmitScalarExpr(E->getArg(0));
2092  Value *Src = EmitScalarExpr(E->getArg(1));
2093  Value *Size = EmitScalarExpr(E->getArg(2));
2094 
2095  BasicBlock *Entry = Builder.GetInsertBlock();
2096  BasicBlock *CmpGT = createBasicBlock("wmemcmp.gt");
2097  BasicBlock *CmpLT = createBasicBlock("wmemcmp.lt");
2098  BasicBlock *Next = createBasicBlock("wmemcmp.next");
2099  BasicBlock *Exit = createBasicBlock("wmemcmp.exit");
2100  Value *SizeEq0 = Builder.CreateICmpEQ(Size, ConstantInt::get(SizeTy, 0));
2101  Builder.CreateCondBr(SizeEq0, Exit, CmpGT);
2102 
2103  EmitBlock(CmpGT);
2104  PHINode *DstPhi = Builder.CreatePHI(Dst->getType(), 2);
2105  DstPhi->addIncoming(Dst, Entry);
2106  PHINode *SrcPhi = Builder.CreatePHI(Src->getType(), 2);
2107  SrcPhi->addIncoming(Src, Entry);
2108  PHINode *SizePhi = Builder.CreatePHI(SizeTy, 2);
2109  SizePhi->addIncoming(Size, Entry);
2110  CharUnits WCharAlign =
2111  getContext().getTypeAlignInChars(getContext().WCharTy);
2112  Value *DstCh = Builder.CreateAlignedLoad(WCharTy, DstPhi, WCharAlign);
2113  Value *SrcCh = Builder.CreateAlignedLoad(WCharTy, SrcPhi, WCharAlign);
2114  Value *DstGtSrc = Builder.CreateICmpUGT(DstCh, SrcCh);
2115  Builder.CreateCondBr(DstGtSrc, Exit, CmpLT);
2116 
2117  EmitBlock(CmpLT);
2118  Value *DstLtSrc = Builder.CreateICmpULT(DstCh, SrcCh);
2119  Builder.CreateCondBr(DstLtSrc, Exit, Next);
2120 
2121  EmitBlock(Next);
2122  Value *NextDst = Builder.CreateConstInBoundsGEP1_32(WCharTy, DstPhi, 1);
2123  Value *NextSrc = Builder.CreateConstInBoundsGEP1_32(WCharTy, SrcPhi, 1);
2124  Value *NextSize = Builder.CreateSub(SizePhi, ConstantInt::get(SizeTy, 1));
2125  Value *NextSizeEq0 =
2126  Builder.CreateICmpEQ(NextSize, ConstantInt::get(SizeTy, 0));
2127  Builder.CreateCondBr(NextSizeEq0, Exit, CmpGT);
2128  DstPhi->addIncoming(NextDst, Next);
2129  SrcPhi->addIncoming(NextSrc, Next);
2130  SizePhi->addIncoming(NextSize, Next);
2131 
2132  EmitBlock(Exit);
2133  PHINode *Ret = Builder.CreatePHI(IntTy, 4);
2134  Ret->addIncoming(ConstantInt::get(IntTy, 0), Entry);
2135  Ret->addIncoming(ConstantInt::get(IntTy, 1), CmpGT);
2136  Ret->addIncoming(ConstantInt::get(IntTy, -1), CmpLT);
2137  Ret->addIncoming(ConstantInt::get(IntTy, 0), Next);
2138  return RValue::get(Ret);
2139  }
2140  case Builtin::BI__builtin_dwarf_cfa: {
2141  // The offset in bytes from the first argument to the CFA.
2142  //
2143  // Why on earth is this in the frontend? Is there any reason at
2144  // all that the backend can't reasonably determine this while
2145  // lowering llvm.eh.dwarf.cfa()?
2146  //
2147  // TODO: If there's a satisfactory reason, add a target hook for
2148  // this instead of hard-coding 0, which is correct for most targets.
2149  int32_t Offset = 0;
2150 
2151  Value *F = CGM.getIntrinsic(Intrinsic::eh_dwarf_cfa);
2152  return RValue::get(Builder.CreateCall(F,
2153  llvm::ConstantInt::get(Int32Ty, Offset)));
2154  }
2155  case Builtin::BI__builtin_return_address: {
2156  Value *Depth = ConstantEmitter(*this).emitAbstract(E->getArg(0),
2157  getContext().UnsignedIntTy);
2158  Value *F = CGM.getIntrinsic(Intrinsic::returnaddress);
2159  return RValue::get(Builder.CreateCall(F, Depth));
2160  }
2161  case Builtin::BI_ReturnAddress: {
2162  Value *F = CGM.getIntrinsic(Intrinsic::returnaddress);
2163  return RValue::get(Builder.CreateCall(F, Builder.getInt32(0)));
2164  }
2165  case Builtin::BI__builtin_frame_address: {
2166  Value *Depth = ConstantEmitter(*this).emitAbstract(E->getArg(0),
2167  getContext().UnsignedIntTy);
2168  Value *F = CGM.getIntrinsic(Intrinsic::frameaddress);
2169  return RValue::get(Builder.CreateCall(F, Depth));
2170  }
2171  case Builtin::BI__builtin_extract_return_addr: {
2172  Value *Address = EmitScalarExpr(E->getArg(0));
2173  Value *Result = getTargetHooks().decodeReturnAddress(*this, Address);
2174  return RValue::get(Result);
2175  }
2176  case Builtin::BI__builtin_frob_return_addr: {
2177  Value *Address = EmitScalarExpr(E->getArg(0));
2178  Value *Result = getTargetHooks().encodeReturnAddress(*this, Address);
2179  return RValue::get(Result);
2180  }
2181  case Builtin::BI__builtin_dwarf_sp_column: {
2182  llvm::IntegerType *Ty
2183  = cast<llvm::IntegerType>(ConvertType(E->getType()));
2184  int Column = getTargetHooks().getDwarfEHStackPointer(CGM);
2185  if (Column == -1) {
2186  CGM.ErrorUnsupported(E, "__builtin_dwarf_sp_column");
2187  return RValue::get(llvm::UndefValue::get(Ty));
2188  }
2189  return RValue::get(llvm::ConstantInt::get(Ty, Column, true));
2190  }
2191  case Builtin::BI__builtin_init_dwarf_reg_size_table: {
2192  Value *Address = EmitScalarExpr(E->getArg(0));
2193  if (getTargetHooks().initDwarfEHRegSizeTable(*this, Address))
2194  CGM.ErrorUnsupported(E, "__builtin_init_dwarf_reg_size_table");
2195  return RValue::get(llvm::UndefValue::get(ConvertType(E->getType())));
2196  }
2197  case Builtin::BI__builtin_eh_return: {
2198  Value *Int = EmitScalarExpr(E->getArg(0));
2199  Value *Ptr = EmitScalarExpr(E->getArg(1));
2200 
2201  llvm::IntegerType *IntTy = cast<llvm::IntegerType>(Int->getType());
2202  assert((IntTy->getBitWidth() == 32 || IntTy->getBitWidth() == 64) &&
2203  "LLVM's __builtin_eh_return only supports 32- and 64-bit variants");
2204  Value *F = CGM.getIntrinsic(IntTy->getBitWidth() == 32
2205  ? Intrinsic::eh_return_i32
2206  : Intrinsic::eh_return_i64);
2207  Builder.CreateCall(F, {Int, Ptr});
2208  Builder.CreateUnreachable();
2209 
2210  // We do need to preserve an insertion point.
2211  EmitBlock(createBasicBlock("builtin_eh_return.cont"));
2212 
2213  return RValue::get(nullptr);
2214  }
2215  case Builtin::BI__builtin_unwind_init: {
2216  Value *F = CGM.getIntrinsic(Intrinsic::eh_unwind_init);
2217  return RValue::get(Builder.CreateCall(F));
2218  }
2219  case Builtin::BI__builtin_extend_pointer: {
2220  // Extends a pointer to the size of an _Unwind_Word, which is
2221  // uint64_t on all platforms. Generally this gets poked into a
2222  // register and eventually used as an address, so if the
2223  // addressing registers are wider than pointers and the platform
2224  // doesn't implicitly ignore high-order bits when doing
2225  // addressing, we need to make sure we zext / sext based on
2226  // the platform's expectations.
2227  //
2228  // See: http://gcc.gnu.org/ml/gcc-bugs/2002-02/msg00237.html
2229 
2230  // Cast the pointer to intptr_t.
2231  Value *Ptr = EmitScalarExpr(E->getArg(0));
2232  Value *Result = Builder.CreatePtrToInt(Ptr, IntPtrTy, "extend.cast");
2233 
2234  // If that's 64 bits, we're done.
2235  if (IntPtrTy->getBitWidth() == 64)
2236  return RValue::get(Result);
2237 
2238  // Otherwise, ask the codegen data what to do.
2239  if (getTargetHooks().extendPointerWithSExt())
2240  return RValue::get(Builder.CreateSExt(Result, Int64Ty, "extend.sext"));
2241  else
2242  return RValue::get(Builder.CreateZExt(Result, Int64Ty, "extend.zext"));
2243  }
2244  case Builtin::BI__builtin_setjmp: {
2245  // Buffer is a void**.
2246  Address Buf = EmitPointerWithAlignment(E->getArg(0));
2247 
2248  // Store the frame pointer to the setjmp buffer.
2249  Value *FrameAddr =
2250  Builder.CreateCall(CGM.getIntrinsic(Intrinsic::frameaddress),
2251  ConstantInt::get(Int32Ty, 0));
2252  Builder.CreateStore(FrameAddr, Buf);
2253 
2254  // Store the stack pointer to the setjmp buffer.
2255  Value *StackAddr =
2256  Builder.CreateCall(CGM.getIntrinsic(Intrinsic::stacksave));
2257  Address StackSaveSlot =
2258  Builder.CreateConstInBoundsGEP(Buf, 2, getPointerSize());
2259  Builder.CreateStore(StackAddr, StackSaveSlot);
2260 
2261  // Call LLVM's EH setjmp, which is lightweight.
2262  Value *F = CGM.getIntrinsic(Intrinsic::eh_sjlj_setjmp);
2263  Buf = Builder.CreateBitCast(Buf, Int8PtrTy);
2264  return RValue::get(Builder.CreateCall(F, Buf.getPointer()));
2265  }
2266  case Builtin::BI__builtin_longjmp: {
2267  Value *Buf = EmitScalarExpr(E->getArg(0));
2268  Buf = Builder.CreateBitCast(Buf, Int8PtrTy);
2269 
2270  // Call LLVM's EH longjmp, which is lightweight.
2271  Builder.CreateCall(CGM.getIntrinsic(Intrinsic::eh_sjlj_longjmp), Buf);
2272 
2273  // longjmp doesn't return; mark this as unreachable.
2274  Builder.CreateUnreachable();
2275 
2276  // We do need to preserve an insertion point.
2277  EmitBlock(createBasicBlock("longjmp.cont"));
2278 
2279  return RValue::get(nullptr);
2280  }
2281  case Builtin::BI__sync_fetch_and_add:
2282  case Builtin::BI__sync_fetch_and_sub:
2283  case Builtin::BI__sync_fetch_and_or:
2284  case Builtin::BI__sync_fetch_and_and:
2285  case Builtin::BI__sync_fetch_and_xor:
2286  case Builtin::BI__sync_fetch_and_nand:
2287  case Builtin::BI__sync_add_and_fetch:
2288  case Builtin::BI__sync_sub_and_fetch:
2289  case Builtin::BI__sync_and_and_fetch:
2290  case Builtin::BI__sync_or_and_fetch:
2291  case Builtin::BI__sync_xor_and_fetch:
2292  case Builtin::BI__sync_nand_and_fetch:
2293  case Builtin::BI__sync_val_compare_and_swap:
2294  case Builtin::BI__sync_bool_compare_and_swap:
2295  case Builtin::BI__sync_lock_test_and_set:
2296  case Builtin::BI__sync_lock_release:
2297  case Builtin::BI__sync_swap:
2298  llvm_unreachable("Shouldn't make it through sema");
2299  case Builtin::BI__sync_fetch_and_add_1:
2300  case Builtin::BI__sync_fetch_and_add_2:
2301  case Builtin::BI__sync_fetch_and_add_4:
2302  case Builtin::BI__sync_fetch_and_add_8:
2303  case Builtin::BI__sync_fetch_and_add_16:
2304  return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::Add, E);
2305  case Builtin::BI__sync_fetch_and_sub_1:
2306  case Builtin::BI__sync_fetch_and_sub_2:
2307  case Builtin::BI__sync_fetch_and_sub_4:
2308  case Builtin::BI__sync_fetch_and_sub_8:
2309  case Builtin::BI__sync_fetch_and_sub_16:
2310  return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::Sub, E);
2311  case Builtin::BI__sync_fetch_and_or_1:
2312  case Builtin::BI__sync_fetch_and_or_2:
2313  case Builtin::BI__sync_fetch_and_or_4:
2314  case Builtin::BI__sync_fetch_and_or_8:
2315  case Builtin::BI__sync_fetch_and_or_16:
2316  return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::Or, E);
2317  case Builtin::BI__sync_fetch_and_and_1:
2318  case Builtin::BI__sync_fetch_and_and_2:
2319  case Builtin::BI__sync_fetch_and_and_4:
2320  case Builtin::BI__sync_fetch_and_and_8:
2321  case Builtin::BI__sync_fetch_and_and_16:
2322  return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::And, E);
2323  case Builtin::BI__sync_fetch_and_xor_1:
2324  case Builtin::BI__sync_fetch_and_xor_2:
2325  case Builtin::BI__sync_fetch_and_xor_4:
2326  case Builtin::BI__sync_fetch_and_xor_8:
2327  case Builtin::BI__sync_fetch_and_xor_16:
2328  return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::Xor, E);
2329  case Builtin::BI__sync_fetch_and_nand_1:
2330  case Builtin::BI__sync_fetch_and_nand_2:
2331  case Builtin::BI__sync_fetch_and_nand_4:
2332  case Builtin::BI__sync_fetch_and_nand_8:
2333  case Builtin::BI__sync_fetch_and_nand_16:
2334  return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::Nand, E);
2335 
2336  // Clang extensions: not overloaded yet.
2337  case Builtin::BI__sync_fetch_and_min:
2338  return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::Min, E);
2339  case Builtin::BI__sync_fetch_and_max:
2340  return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::Max, E);
2341  case Builtin::BI__sync_fetch_and_umin:
2342  return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::UMin, E);
2343  case Builtin::BI__sync_fetch_and_umax:
2344  return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::UMax, E);
2345 
2346  case Builtin::BI__sync_add_and_fetch_1:
2347  case Builtin::BI__sync_add_and_fetch_2:
2348  case Builtin::BI__sync_add_and_fetch_4:
2349  case Builtin::BI__sync_add_and_fetch_8:
2350  case Builtin::BI__sync_add_and_fetch_16:
2351  return EmitBinaryAtomicPost(*this, llvm::AtomicRMWInst::Add, E,
2352  llvm::Instruction::Add);
2353  case Builtin::BI__sync_sub_and_fetch_1:
2354  case Builtin::BI__sync_sub_and_fetch_2:
2355  case Builtin::BI__sync_sub_and_fetch_4:
2356  case Builtin::BI__sync_sub_and_fetch_8:
2357  case Builtin::BI__sync_sub_and_fetch_16:
2358  return EmitBinaryAtomicPost(*this, llvm::AtomicRMWInst::Sub, E,
2359  llvm::Instruction::Sub);
2360  case Builtin::BI__sync_and_and_fetch_1:
2361  case Builtin::BI__sync_and_and_fetch_2:
2362  case Builtin::BI__sync_and_and_fetch_4:
2363  case Builtin::BI__sync_and_and_fetch_8:
2364  case Builtin::BI__sync_and_and_fetch_16:
2367  case Builtin::BI__sync_or_and_fetch_1:
2368  case Builtin::BI__sync_or_and_fetch_2:
2369  case Builtin::BI__sync_or_and_fetch_4:
2370  case Builtin::BI__sync_or_and_fetch_8:
2371  case Builtin::BI__sync_or_and_fetch_16:
2372  return EmitBinaryAtomicPost(*this, llvm::AtomicRMWInst::Or, E,
2373  llvm::Instruction::Or);
2374  case Builtin::BI__sync_xor_and_fetch_1:
2375  case Builtin::BI__sync_xor_and_fetch_2:
2376  case Builtin::BI__sync_xor_and_fetch_4:
2377  case Builtin::BI__sync_xor_and_fetch_8:
2378  case Builtin::BI__sync_xor_and_fetch_16:
2379  return EmitBinaryAtomicPost(*this, llvm::AtomicRMWInst::Xor, E,
2380  llvm::Instruction::Xor);
2381  case Builtin::BI__sync_nand_and_fetch_1:
2382  case Builtin::BI__sync_nand_and_fetch_2:
2383  case Builtin::BI__sync_nand_and_fetch_4:
2384  case Builtin::BI__sync_nand_and_fetch_8:
2385  case Builtin::BI__sync_nand_and_fetch_16:
2386  return EmitBinaryAtomicPost(*this, llvm::AtomicRMWInst::Nand, E,
2387  llvm::Instruction::And, true);
2388 
2389  case Builtin::BI__sync_val_compare_and_swap_1:
2390  case Builtin::BI__sync_val_compare_and_swap_2:
2391  case Builtin::BI__sync_val_compare_and_swap_4:
2392  case Builtin::BI__sync_val_compare_and_swap_8:
2393  case Builtin::BI__sync_val_compare_and_swap_16:
2394  return RValue::get(MakeAtomicCmpXchgValue(*this, E, false));
2395 
2396  case Builtin::BI__sync_bool_compare_and_swap_1:
2397  case Builtin::BI__sync_bool_compare_and_swap_2:
2398  case Builtin::BI__sync_bool_compare_and_swap_4:
2399  case Builtin::BI__sync_bool_compare_and_swap_8:
2400  case Builtin::BI__sync_bool_compare_and_swap_16:
2401  return RValue::get(MakeAtomicCmpXchgValue(*this, E, true));
2402 
2403  case Builtin::BI__sync_swap_1:
2404  case Builtin::BI__sync_swap_2:
2405  case Builtin::BI__sync_swap_4:
2406  case Builtin::BI__sync_swap_8:
2407  case Builtin::BI__sync_swap_16:
2408  return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::Xchg, E);
2409 
2410  case Builtin::BI__sync_lock_test_and_set_1:
2411  case Builtin::BI__sync_lock_test_and_set_2:
2412  case Builtin::BI__sync_lock_test_and_set_4:
2413  case Builtin::BI__sync_lock_test_and_set_8:
2414  case Builtin::BI__sync_lock_test_and_set_16:
2415  return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::Xchg, E);
2416 
2417  case Builtin::BI__sync_lock_release_1:
2418  case Builtin::BI__sync_lock_release_2:
2419  case Builtin::BI__sync_lock_release_4:
2420  case Builtin::BI__sync_lock_release_8:
2421  case Builtin::BI__sync_lock_release_16: {
2422  Value *Ptr = EmitScalarExpr(E->getArg(0));
2423  QualType ElTy = E->getArg(0)->getType()->getPointeeType();
2424  CharUnits StoreSize = getContext().getTypeSizeInChars(ElTy);
2425  llvm::Type *ITy = llvm::IntegerType::get(getLLVMContext(),
2426  StoreSize.getQuantity() * 8);
2427  Ptr = Builder.CreateBitCast(Ptr, ITy->getPointerTo());
2428  llvm::StoreInst *Store =
2429  Builder.CreateAlignedStore(llvm::Constant::getNullValue(ITy), Ptr,
2430  StoreSize);
2431  Store->setAtomic(llvm::AtomicOrdering::Release);
2432  return RValue::get(nullptr);
2433  }
2434 
2435  case Builtin::BI__sync_synchronize: {
2436  // We assume this is supposed to correspond to a C++0x-style
2437  // sequentially-consistent fence (i.e. this is only usable for
2438  // synchronization, not device I/O or anything like that). This intrinsic
2439  // is really badly designed in the sense that in theory, there isn't
2440  // any way to safely use it... but in practice, it mostly works
2441  // to use it with non-atomic loads and stores to get acquire/release
2442  // semantics.
2443  Builder.CreateFence(llvm::AtomicOrdering::SequentiallyConsistent);
2444  return RValue::get(nullptr);
2445  }
2446 
2447  case Builtin::BI__builtin_nontemporal_load:
2448  return RValue::get(EmitNontemporalLoad(*this, E));
2449  case Builtin::BI__builtin_nontemporal_store:
2450  return RValue::get(EmitNontemporalStore(*this, E));
2451  case Builtin::BI__c11_atomic_is_lock_free:
2452  case Builtin::BI__atomic_is_lock_free: {
2453  // Call "bool __atomic_is_lock_free(size_t size, void *ptr)". For the
2454  // __c11 builtin, ptr is 0 (indicating a properly-aligned object), since
2455  // _Atomic(T) is always properly-aligned.
2456  const char *LibCallName = "__atomic_is_lock_free";
2457  CallArgList Args;
2458  Args.add(RValue::get(EmitScalarExpr(E->getArg(0))),
2459  getContext().getSizeType());
2460  if (BuiltinID == Builtin::BI__atomic_is_lock_free)
2461  Args.add(RValue::get(EmitScalarExpr(E->getArg(1))),
2462  getContext().VoidPtrTy);
2463  else
2464  Args.add(RValue::get(llvm::Constant::getNullValue(VoidPtrTy)),
2465  getContext().VoidPtrTy);
2466  const CGFunctionInfo &FuncInfo =
2467  CGM.getTypes().arrangeBuiltinFunctionCall(E->getType(), Args);
2468  llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FuncInfo);
2469  llvm::Constant *Func = CGM.CreateRuntimeFunction(FTy, LibCallName);
2470  return EmitCall(FuncInfo, CGCallee::forDirect(Func),
2471  ReturnValueSlot(), Args);
2472  }
2473 
2474  case Builtin::BI__atomic_test_and_set: {
2475  // Look at the argument type to determine whether this is a volatile
2476  // operation. The parameter type is always volatile.
2477  QualType PtrTy = E->getArg(0)->IgnoreImpCasts()->getType();
2478  bool Volatile =
2479  PtrTy->castAs<PointerType>()->getPointeeType().isVolatileQualified();
2480 
2481  Value *Ptr = EmitScalarExpr(E->getArg(0));
2482  unsigned AddrSpace = Ptr->getType()->getPointerAddressSpace();
2483  Ptr = Builder.CreateBitCast(Ptr, Int8Ty->getPointerTo(AddrSpace));
2484  Value *NewVal = Builder.getInt8(1);
2485  Value *Order = EmitScalarExpr(E->getArg(1));
2486  if (isa<llvm::ConstantInt>(Order)) {
2487  int ord = cast<llvm::ConstantInt>(Order)->getZExtValue();
2488  AtomicRMWInst *Result = nullptr;
2489  switch (ord) {
2490  case 0: // memory_order_relaxed
2491  default: // invalid order
2492  Result = Builder.CreateAtomicRMW(llvm::AtomicRMWInst::Xchg, Ptr, NewVal,
2493  llvm::AtomicOrdering::Monotonic);
2494  break;
2495  case 1: // memory_order_consume
2496  case 2: // memory_order_acquire
2497  Result = Builder.CreateAtomicRMW(llvm::AtomicRMWInst::Xchg, Ptr, NewVal,
2498  llvm::AtomicOrdering::Acquire);
2499  break;
2500  case 3: // memory_order_release
2501  Result = Builder.CreateAtomicRMW(llvm::AtomicRMWInst::Xchg, Ptr, NewVal,
2502  llvm::AtomicOrdering::Release);
2503  break;
2504  case 4: // memory_order_acq_rel
2505 
2506  Result = Builder.CreateAtomicRMW(llvm::AtomicRMWInst::Xchg, Ptr, NewVal,
2507  llvm::AtomicOrdering::AcquireRelease);
2508  break;
2509  case 5: // memory_order_seq_cst
2510  Result = Builder.CreateAtomicRMW(
2511  llvm::AtomicRMWInst::Xchg, Ptr, NewVal,
2512  llvm::AtomicOrdering::SequentiallyConsistent);
2513  break;
2514  }
2515  Result->setVolatile(Volatile);
2516  return RValue::get(Builder.CreateIsNotNull(Result, "tobool"));
2517  }
2518 
2519  llvm::BasicBlock *ContBB = createBasicBlock("atomic.continue", CurFn);
2520 
2521  llvm::BasicBlock *BBs[5] = {
2522  createBasicBlock("monotonic", CurFn),
2523  createBasicBlock("acquire", CurFn),
2524  createBasicBlock("release", CurFn),
2525  createBasicBlock("acqrel", CurFn),
2526  createBasicBlock("seqcst", CurFn)
2527  };
2528  llvm::AtomicOrdering Orders[5] = {
2529  llvm::AtomicOrdering::Monotonic, llvm::AtomicOrdering::Acquire,
2530  llvm::AtomicOrdering::Release, llvm::AtomicOrdering::AcquireRelease,
2531  llvm::AtomicOrdering::SequentiallyConsistent};
2532 
2533  Order = Builder.CreateIntCast(Order, Builder.getInt32Ty(), false);
2534  llvm::SwitchInst *SI = Builder.CreateSwitch(Order, BBs[0]);
2535 
2536  Builder.SetInsertPoint(ContBB);
2537  PHINode *Result = Builder.CreatePHI(Int8Ty, 5, "was_set");
2538 
2539  for (unsigned i = 0; i < 5; ++i) {
2540  Builder.SetInsertPoint(BBs[i]);
2541  AtomicRMWInst *RMW = Builder.CreateAtomicRMW(llvm::AtomicRMWInst::Xchg,
2542  Ptr, NewVal, Orders[i]);
2543  RMW->setVolatile(Volatile);
2544  Result->addIncoming(RMW, BBs[i]);
2545  Builder.CreateBr(ContBB);
2546  }
2547 
2548  SI->addCase(Builder.getInt32(0), BBs[0]);
2549  SI->addCase(Builder.getInt32(1), BBs[1]);
2550  SI->addCase(Builder.getInt32(2), BBs[1]);
2551  SI->addCase(Builder.getInt32(3), BBs[2]);
2552  SI->addCase(Builder.getInt32(4), BBs[3]);
2553  SI->addCase(Builder.getInt32(5), BBs[4]);
2554 
2555  Builder.SetInsertPoint(ContBB);
2556  return RValue::get(Builder.CreateIsNotNull(Result, "tobool"));
2557  }
2558 
2559  case Builtin::BI__atomic_clear: {
2560  QualType PtrTy = E->getArg(0)->IgnoreImpCasts()->getType();
2561  bool Volatile =
2562  PtrTy->castAs<PointerType>()->getPointeeType().isVolatileQualified();
2563 
2564  Address Ptr = EmitPointerWithAlignment(E->getArg(0));
2565  unsigned AddrSpace = Ptr.getPointer()->getType()->getPointerAddressSpace();
2566  Ptr = Builder.CreateBitCast(Ptr, Int8Ty->getPointerTo(AddrSpace));
2567  Value *NewVal = Builder.getInt8(0);
2568  Value *Order = EmitScalarExpr(E->getArg(1));
2569  if (isa<llvm::ConstantInt>(Order)) {
2570  int ord = cast<llvm::ConstantInt>(Order)->getZExtValue();
2571  StoreInst *Store = Builder.CreateStore(NewVal, Ptr, Volatile);
2572  switch (ord) {
2573  case 0: // memory_order_relaxed
2574  default: // invalid order
2575  Store->setOrdering(llvm::AtomicOrdering::Monotonic);
2576  break;
2577  case 3: // memory_order_release
2578  Store->setOrdering(llvm::AtomicOrdering::Release);
2579  break;
2580  case 5: // memory_order_seq_cst
2581  Store->setOrdering(llvm::AtomicOrdering::SequentiallyConsistent);
2582  break;
2583  }
2584  return RValue::get(nullptr);
2585  }
2586 
2587  llvm::BasicBlock *ContBB = createBasicBlock("atomic.continue", CurFn);
2588 
2589  llvm::BasicBlock *BBs[3] = {
2590  createBasicBlock("monotonic", CurFn),
2591  createBasicBlock("release", CurFn),
2592  createBasicBlock("seqcst", CurFn)
2593  };
2594  llvm::AtomicOrdering Orders[3] = {
2595  llvm::AtomicOrdering::Monotonic, llvm::AtomicOrdering::Release,
2596  llvm::AtomicOrdering::SequentiallyConsistent};
2597 
2598  Order = Builder.CreateIntCast(Order, Builder.getInt32Ty(), false);
2599  llvm::SwitchInst *SI = Builder.CreateSwitch(Order, BBs[0]);
2600 
2601  for (unsigned i = 0; i < 3; ++i) {
2602  Builder.SetInsertPoint(BBs[i]);
2603  StoreInst *Store = Builder.CreateStore(NewVal, Ptr, Volatile);
2604  Store->setOrdering(Orders[i]);
2605  Builder.CreateBr(ContBB);
2606  }
2607 
2608  SI->addCase(Builder.getInt32(0), BBs[0]);
2609  SI->addCase(Builder.getInt32(3), BBs[1]);
2610  SI->addCase(Builder.getInt32(5), BBs[2]);
2611 
2612  Builder.SetInsertPoint(ContBB);
2613  return RValue::get(nullptr);
2614  }
2615 
2616  case Builtin::BI__atomic_thread_fence:
2617  case Builtin::BI__atomic_signal_fence:
2618  case Builtin::BI__c11_atomic_thread_fence:
2619  case Builtin::BI__c11_atomic_signal_fence: {
2620  llvm::SyncScope::ID SSID;
2621  if (BuiltinID == Builtin::BI__atomic_signal_fence ||
2622  BuiltinID == Builtin::BI__c11_atomic_signal_fence)
2623  SSID = llvm::SyncScope::SingleThread;
2624  else
2625  SSID = llvm::SyncScope::System;
2626  Value *Order = EmitScalarExpr(E->getArg(0));
2627  if (isa<llvm::ConstantInt>(Order)) {
2628  int ord = cast<llvm::ConstantInt>(Order)->getZExtValue();
2629  switch (ord) {
2630  case 0: // memory_order_relaxed
2631  default: // invalid order
2632  break;
2633  case 1: // memory_order_consume
2634  case 2: // memory_order_acquire
2635  Builder.CreateFence(llvm::AtomicOrdering::Acquire, SSID);
2636  break;
2637  case 3: // memory_order_release
2638  Builder.CreateFence(llvm::AtomicOrdering::Release, SSID);
2639  break;
2640  case 4: // memory_order_acq_rel
2641  Builder.CreateFence(llvm::AtomicOrdering::AcquireRelease, SSID);
2642  break;
2643  case 5: // memory_order_seq_cst
2644  Builder.CreateFence(llvm::AtomicOrdering::SequentiallyConsistent, SSID);
2645  break;
2646  }
2647  return RValue::get(nullptr);
2648  }
2649 
2650  llvm::BasicBlock *AcquireBB, *ReleaseBB, *AcqRelBB, *SeqCstBB;
2651  AcquireBB = createBasicBlock("acquire", CurFn);
2652  ReleaseBB = createBasicBlock("release", CurFn);
2653  AcqRelBB = createBasicBlock("acqrel", CurFn);
2654  SeqCstBB = createBasicBlock("seqcst", CurFn);
2655  llvm::BasicBlock *ContBB = createBasicBlock("atomic.continue", CurFn);
2656 
2657  Order = Builder.CreateIntCast(Order, Builder.getInt32Ty(), false);
2658  llvm::SwitchInst *SI = Builder.CreateSwitch(Order, ContBB);
2659 
2660  Builder.SetInsertPoint(AcquireBB);
2661  Builder.CreateFence(llvm::AtomicOrdering::Acquire, SSID);
2662  Builder.CreateBr(ContBB);
2663  SI->addCase(Builder.getInt32(1), AcquireBB);
2664  SI->addCase(Builder.getInt32(2), AcquireBB);
2665 
2666  Builder.SetInsertPoint(ReleaseBB);
2667  Builder.CreateFence(llvm::AtomicOrdering::Release, SSID);
2668  Builder.CreateBr(ContBB);
2669  SI->addCase(Builder.getInt32(3), ReleaseBB);
2670 
2671  Builder.SetInsertPoint(AcqRelBB);
2672  Builder.CreateFence(llvm::AtomicOrdering::AcquireRelease, SSID);
2673  Builder.CreateBr(ContBB);
2674  SI->addCase(Builder.getInt32(4), AcqRelBB);
2675 
2676  Builder.SetInsertPoint(SeqCstBB);
2677  Builder.CreateFence(llvm::AtomicOrdering::SequentiallyConsistent, SSID);
2678  Builder.CreateBr(ContBB);
2679  SI->addCase(Builder.getInt32(5), SeqCstBB);
2680 
2681  Builder.SetInsertPoint(ContBB);
2682  return RValue::get(nullptr);
2683  }
2684 
2685  case Builtin::BI__builtin_signbit:
2686  case Builtin::BI__builtin_signbitf:
2687  case Builtin::BI__builtin_signbitl: {
2688  return RValue::get(
2689  Builder.CreateZExt(EmitSignBit(*this, EmitScalarExpr(E->getArg(0))),
2690  ConvertType(E->getType())));
2691  }
2692  case Builtin::BI__annotation: {
2693  // Re-encode each wide string to UTF8 and make an MDString.
2695  for (const Expr *Arg : E->arguments()) {
2696  const auto *Str = cast<StringLiteral>(Arg->IgnoreParenCasts());
2697  assert(Str->getCharByteWidth() == 2);
2698  StringRef WideBytes = Str->getBytes();
2699  std::string StrUtf8;
2700  if (!convertUTF16ToUTF8String(
2701  makeArrayRef(WideBytes.data(), WideBytes.size()), StrUtf8)) {
2702  CGM.ErrorUnsupported(E, "non-UTF16 __annotation argument");
2703  continue;
2704  }
2705  Strings.push_back(llvm::MDString::get(getLLVMContext(), StrUtf8));
2706  }
2707 
2708  // Build and MDTuple of MDStrings and emit the intrinsic call.
2709  llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::codeview_annotation, {});
2710  MDTuple *StrTuple = MDTuple::get(getLLVMContext(), Strings);
2711  Builder.CreateCall(F, MetadataAsValue::get(getLLVMContext(), StrTuple));
2712  return RValue::getIgnored();
2713  }
2714  case Builtin::BI__builtin_annotation: {
2715  llvm::Value *AnnVal = EmitScalarExpr(E->getArg(0));
2716  llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::annotation,
2717  AnnVal->getType());
2718 
2719  // Get the annotation string, go through casts. Sema requires this to be a
2720  // non-wide string literal, potentially casted, so the cast<> is safe.
2721  const Expr *AnnotationStrExpr = E->getArg(1)->IgnoreParenCasts();
2722  StringRef Str = cast<StringLiteral>(AnnotationStrExpr)->getString();
2723  return RValue::get(EmitAnnotationCall(F, AnnVal, Str, E->getExprLoc()));
2724  }
2725  case Builtin::BI__builtin_addcb:
2726  case Builtin::BI__builtin_addcs:
2727  case Builtin::BI__builtin_addc:
2728  case Builtin::BI__builtin_addcl:
2729  case Builtin::BI__builtin_addcll:
2730  case Builtin::BI__builtin_subcb:
2731  case Builtin::BI__builtin_subcs:
2732  case Builtin::BI__builtin_subc:
2733  case Builtin::BI__builtin_subcl:
2734  case Builtin::BI__builtin_subcll: {
2735 
2736  // We translate all of these builtins from expressions of the form:
2737  // int x = ..., y = ..., carryin = ..., carryout, result;
2738  // result = __builtin_addc(x, y, carryin, &carryout);
2739  //
2740  // to LLVM IR of the form:
2741  //
2742  // %tmp1 = call {i32, i1} @llvm.uadd.with.overflow.i32(i32 %x, i32 %y)
2743  // %tmpsum1 = extractvalue {i32, i1} %tmp1, 0
2744  // %carry1 = extractvalue {i32, i1} %tmp1, 1
2745  // %tmp2 = call {i32, i1} @llvm.uadd.with.overflow.i32(i32 %tmpsum1,
2746  // i32 %carryin)
2747  // %result = extractvalue {i32, i1} %tmp2, 0
2748  // %carry2 = extractvalue {i32, i1} %tmp2, 1
2749  // %tmp3 = or i1 %carry1, %carry2
2750  // %tmp4 = zext i1 %tmp3 to i32
2751  // store i32 %tmp4, i32* %carryout
2752 
2753  // Scalarize our inputs.
2754  llvm::Value *X = EmitScalarExpr(E->getArg(0));
2755  llvm::Value *Y = EmitScalarExpr(E->getArg(1));
2756  llvm::Value *Carryin = EmitScalarExpr(E->getArg(2));
2757  Address CarryOutPtr = EmitPointerWithAlignment(E->getArg(3));
2758 
2759  // Decide if we are lowering to a uadd.with.overflow or usub.with.overflow.
2760  llvm::Intrinsic::ID IntrinsicId;
2761  switch (BuiltinID) {
2762  default: llvm_unreachable("Unknown multiprecision builtin id.");
2763  case Builtin::BI__builtin_addcb:
2764  case Builtin::BI__builtin_addcs:
2765  case Builtin::BI__builtin_addc:
2766  case Builtin::BI__builtin_addcl:
2767  case Builtin::BI__builtin_addcll:
2768  IntrinsicId = llvm::Intrinsic::uadd_with_overflow;
2769  break;
2770  case Builtin::BI__builtin_subcb:
2771  case Builtin::BI__builtin_subcs:
2772  case Builtin::BI__builtin_subc:
2773  case Builtin::BI__builtin_subcl:
2774  case Builtin::BI__builtin_subcll:
2775  IntrinsicId = llvm::Intrinsic::usub_with_overflow;
2776  break;
2777  }
2778 
2779  // Construct our resulting LLVM IR expression.
2780  llvm::Value *Carry1;
2781  llvm::Value *Sum1 = EmitOverflowIntrinsic(*this, IntrinsicId,
2782  X, Y, Carry1);
2783  llvm::Value *Carry2;
2784  llvm::Value *Sum2 = EmitOverflowIntrinsic(*this, IntrinsicId,
2785  Sum1, Carryin, Carry2);
2786  llvm::Value *CarryOut = Builder.CreateZExt(Builder.CreateOr(Carry1, Carry2),
2787  X->getType());
2788  Builder.CreateStore(CarryOut, CarryOutPtr);
2789  return RValue::get(Sum2);
2790  }
2791 
2792  case Builtin::BI__builtin_add_overflow:
2793  case Builtin::BI__builtin_sub_overflow:
2794  case Builtin::BI__builtin_mul_overflow: {
2795  const clang::Expr *LeftArg = E->getArg(0);
2796  const clang::Expr *RightArg = E->getArg(1);
2797  const clang::Expr *ResultArg = E->getArg(2);
2798 
2799  clang::QualType ResultQTy =
2800  ResultArg->getType()->castAs<PointerType>()->getPointeeType();
2801 
2802  WidthAndSignedness LeftInfo =
2803  getIntegerWidthAndSignedness(CGM.getContext(), LeftArg->getType());
2804  WidthAndSignedness RightInfo =
2805  getIntegerWidthAndSignedness(CGM.getContext(), RightArg->getType());
2806  WidthAndSignedness ResultInfo =
2807  getIntegerWidthAndSignedness(CGM.getContext(), ResultQTy);
2808 
2809  // Handle mixed-sign multiplication as a special case, because adding
2810  // runtime or backend support for our generic irgen would be too expensive.
2811  if (isSpecialMixedSignMultiply(BuiltinID, LeftInfo, RightInfo, ResultInfo))
2812  return EmitCheckedMixedSignMultiply(*this, LeftArg, LeftInfo, RightArg,
2813  RightInfo, ResultArg, ResultQTy,
2814  ResultInfo);
2815 
2816  WidthAndSignedness EncompassingInfo =
2817  EncompassingIntegerType({LeftInfo, RightInfo, ResultInfo});
2818 
2819  llvm::Type *EncompassingLLVMTy =
2820  llvm::IntegerType::get(CGM.getLLVMContext(), EncompassingInfo.Width);
2821 
2822  llvm::Type *ResultLLVMTy = CGM.getTypes().ConvertType(ResultQTy);
2823 
2824  llvm::Intrinsic::ID IntrinsicId;
2825  switch (BuiltinID) {
2826  default:
2827  llvm_unreachable("Unknown overflow builtin id.");
2828  case Builtin::BI__builtin_add_overflow:
2829  IntrinsicId = EncompassingInfo.Signed
2830  ? llvm::Intrinsic::sadd_with_overflow
2831  : llvm::Intrinsic::uadd_with_overflow;
2832  break;
2833  case Builtin::BI__builtin_sub_overflow:
2834  IntrinsicId = EncompassingInfo.Signed
2835  ? llvm::Intrinsic::ssub_with_overflow
2836  : llvm::Intrinsic::usub_with_overflow;
2837  break;
2838  case Builtin::BI__builtin_mul_overflow:
2839  IntrinsicId = EncompassingInfo.Signed
2840  ? llvm::Intrinsic::smul_with_overflow
2841  : llvm::Intrinsic::umul_with_overflow;
2842  break;
2843  }
2844 
2845  llvm::Value *Left = EmitScalarExpr(LeftArg);
2846  llvm::Value *Right = EmitScalarExpr(RightArg);
2847  Address ResultPtr = EmitPointerWithAlignment(ResultArg);
2848 
2849  // Extend each operand to the encompassing type.
2850  Left = Builder.CreateIntCast(Left, EncompassingLLVMTy, LeftInfo.Signed);
2851  Right = Builder.CreateIntCast(Right, EncompassingLLVMTy, RightInfo.Signed);
2852 
2853  // Perform the operation on the extended values.
2854  llvm::Value *Overflow, *Result;
2855  Result = EmitOverflowIntrinsic(*this, IntrinsicId, Left, Right, Overflow);
2856 
2857  if (EncompassingInfo.Width > ResultInfo.Width) {
2858  // The encompassing type is wider than the result type, so we need to
2859  // truncate it.
2860  llvm::Value *ResultTrunc = Builder.CreateTrunc(Result, ResultLLVMTy);
2861 
2862  // To see if the truncation caused an overflow, we will extend
2863  // the result and then compare it to the original result.
2864  llvm::Value *ResultTruncExt = Builder.CreateIntCast(
2865  ResultTrunc, EncompassingLLVMTy, ResultInfo.Signed);
2866  llvm::Value *TruncationOverflow =
2867  Builder.CreateICmpNE(Result, ResultTruncExt);
2868 
2869  Overflow = Builder.CreateOr(Overflow, TruncationOverflow);
2870  Result = ResultTrunc;
2871  }
2872 
2873  // Finally, store the result using the pointer.
2874  bool isVolatile =
2875  ResultArg->getType()->getPointeeType().isVolatileQualified();
2876  Builder.CreateStore(EmitToMemory(Result, ResultQTy), ResultPtr, isVolatile);
2877 
2878  return RValue::get(Overflow);
2879  }
2880 
2881  case Builtin::BI__builtin_uadd_overflow:
2882  case Builtin::BI__builtin_uaddl_overflow:
2883  case Builtin::BI__builtin_uaddll_overflow:
2884  case Builtin::BI__builtin_usub_overflow:
2885  case Builtin::BI__builtin_usubl_overflow:
2886  case Builtin::BI__builtin_usubll_overflow:
2887  case Builtin::BI__builtin_umul_overflow:
2888  case Builtin::BI__builtin_umull_overflow:
2889  case Builtin::BI__builtin_umulll_overflow:
2890  case Builtin::BI__builtin_sadd_overflow:
2891  case Builtin::BI__builtin_saddl_overflow:
2892  case Builtin::BI__builtin_saddll_overflow:
2893  case Builtin::BI__builtin_ssub_overflow:
2894  case Builtin::BI__builtin_ssubl_overflow:
2895  case Builtin::BI__builtin_ssubll_overflow:
2896  case Builtin::BI__builtin_smul_overflow:
2897  case Builtin::BI__builtin_smull_overflow:
2898  case Builtin::BI__builtin_smulll_overflow: {
2899 
2900  // We translate all of these builtins directly to the relevant llvm IR node.
2901 
2902  // Scalarize our inputs.
2903  llvm::Value *X = EmitScalarExpr(E->getArg(0));
2904  llvm::Value *Y = EmitScalarExpr(E->getArg(1));
2905  Address SumOutPtr = EmitPointerWithAlignment(E->getArg(2));
2906 
2907  // Decide which of the overflow intrinsics we are lowering to:
2908  llvm::Intrinsic::ID IntrinsicId;
2909  switch (BuiltinID) {
2910  default: llvm_unreachable("Unknown overflow builtin id.");
2911  case Builtin::BI__builtin_uadd_overflow:
2912  case Builtin::BI__builtin_uaddl_overflow:
2913  case Builtin::BI__builtin_uaddll_overflow:
2914  IntrinsicId = llvm::Intrinsic::uadd_with_overflow;
2915  break;
2916  case Builtin::BI__builtin_usub_overflow:
2917  case Builtin::BI__builtin_usubl_overflow:
2918  case Builtin::BI__builtin_usubll_overflow:
2919  IntrinsicId = llvm::Intrinsic::usub_with_overflow;
2920  break;
2921  case Builtin::BI__builtin_umul_overflow:
2922  case Builtin::BI__builtin_umull_overflow:
2923  case Builtin::BI__builtin_umulll_overflow:
2924  IntrinsicId = llvm::Intrinsic::umul_with_overflow;
2925  break;
2926  case Builtin::BI__builtin_sadd_overflow:
2927  case Builtin::BI__builtin_saddl_overflow:
2928  case Builtin::BI__builtin_saddll_overflow:
2929  IntrinsicId = llvm::Intrinsic::sadd_with_overflow;
2930  break;
2931  case Builtin::BI__builtin_ssub_overflow:
2932  case Builtin::BI__builtin_ssubl_overflow:
2933  case Builtin::BI__builtin_ssubll_overflow:
2934  IntrinsicId = llvm::Intrinsic::ssub_with_overflow;
2935  break;
2936  case Builtin::BI__builtin_smul_overflow:
2937  case Builtin::BI__builtin_smull_overflow:
2938  case Builtin::BI__builtin_smulll_overflow:
2939  IntrinsicId = llvm::Intrinsic::smul_with_overflow;
2940  break;
2941  }
2942 
2943 
2944  llvm::Value *Carry;
2945  llvm::Value *Sum = EmitOverflowIntrinsic(*this, IntrinsicId, X, Y, Carry);
2946  Builder.CreateStore(Sum, SumOutPtr);
2947 
2948  return RValue::get(Carry);
2949  }
2950  case Builtin::BI__builtin_addressof:
2951  return RValue::get(EmitLValue(E->getArg(0)).getPointer());
2952  case Builtin::BI__builtin_operator_new:
2953  return EmitBuiltinNewDeleteCall(
2954  E->getCallee()->getType()->castAs<FunctionProtoType>(), E, false);
2955  case Builtin::BI__builtin_operator_delete:
2956  return EmitBuiltinNewDeleteCall(
2957  E->getCallee()->getType()->castAs<FunctionProtoType>(), E, true);
2958 
2959  case Builtin::BI__noop:
2960  // __noop always evaluates to an integer literal zero.
2961  return RValue::get(ConstantInt::get(IntTy, 0));
2962  case Builtin::BI__builtin_call_with_static_chain: {
2963  const CallExpr *Call = cast<CallExpr>(E->getArg(0));
2964  const Expr *Chain = E->getArg(1);
2965  return EmitCall(Call->getCallee()->getType(),
2966  EmitCallee(Call->getCallee()), Call, ReturnValue,
2967  EmitScalarExpr(Chain));
2968  }
2969  case Builtin::BI_InterlockedExchange8:
2970  case Builtin::BI_InterlockedExchange16:
2971  case Builtin::BI_InterlockedExchange:
2972  case Builtin::BI_InterlockedExchangePointer:
2973  return RValue::get(
2974  EmitMSVCBuiltinExpr(MSVCIntrin::_InterlockedExchange, E));
2975  case Builtin::BI_InterlockedCompareExchangePointer: {
2976  llvm::Type *RTy;
2977  llvm::IntegerType *IntType =
2978  IntegerType::get(getLLVMContext(),
2979  getContext().getTypeSize(E->getType()));
2980  llvm::Type *IntPtrType = IntType->getPointerTo();
2981 
2982  llvm::Value *Destination =
2983  Builder.CreateBitCast(EmitScalarExpr(E->getArg(0)), IntPtrType);
2984 
2985  llvm::Value *Exchange = EmitScalarExpr(E->getArg(1));
2986  RTy = Exchange->getType();
2987  Exchange = Builder.CreatePtrToInt(Exchange, IntType);
2988 
2989  llvm::Value *Comparand =
2990  Builder.CreatePtrToInt(EmitScalarExpr(E->getArg(2)), IntType);
2991 
2992  auto Result =
2993  Builder.CreateAtomicCmpXchg(Destination, Comparand, Exchange,
2994  AtomicOrdering::SequentiallyConsistent,
2995  AtomicOrdering::SequentiallyConsistent);
2996  Result->setVolatile(true);
2997 
2998  return RValue::get(Builder.CreateIntToPtr(Builder.CreateExtractValue(Result,
2999  0),
3000  RTy));
3001  }
3002  case Builtin::BI_InterlockedCompareExchange8:
3003  case Builtin::BI_InterlockedCompareExchange16:
3004  case Builtin::BI_InterlockedCompareExchange:
3005  case Builtin::BI_InterlockedCompareExchange64: {
3006  AtomicCmpXchgInst *CXI = Builder.CreateAtomicCmpXchg(
3007  EmitScalarExpr(E->getArg(0)),
3008  EmitScalarExpr(E->getArg(2)),
3009  EmitScalarExpr(E->getArg(1)),
3010  AtomicOrdering::SequentiallyConsistent,
3011  AtomicOrdering::SequentiallyConsistent);
3012  CXI->setVolatile(true);
3013  return RValue::get(Builder.CreateExtractValue(CXI, 0));
3014  }
3015  case Builtin::BI_InterlockedIncrement16:
3016  case Builtin::BI_InterlockedIncrement:
3017  return RValue::get(
3018  EmitMSVCBuiltinExpr(MSVCIntrin::_InterlockedIncrement, E));
3019  case Builtin::BI_InterlockedDecrement16:
3020  case Builtin::BI_InterlockedDecrement:
3021  return RValue::get(
3022  EmitMSVCBuiltinExpr(MSVCIntrin::_InterlockedDecrement, E));
3023  case Builtin::BI_InterlockedAnd8:
3024  case Builtin::BI_InterlockedAnd16:
3025  case Builtin::BI_InterlockedAnd:
3026  return RValue::get(EmitMSVCBuiltinExpr(MSVCIntrin::_InterlockedAnd, E));
3027  case Builtin::BI_InterlockedExchangeAdd8:
3028  case Builtin::BI_InterlockedExchangeAdd16:
3029  case Builtin::BI_InterlockedExchangeAdd:
3030  return RValue::get(
3031  EmitMSVCBuiltinExpr(MSVCIntrin::_InterlockedExchangeAdd, E));
3032  case Builtin::BI_InterlockedExchangeSub8:
3033  case Builtin::BI_InterlockedExchangeSub16:
3034  case Builtin::BI_InterlockedExchangeSub:
3035  return RValue::get(
3036  EmitMSVCBuiltinExpr(MSVCIntrin::_InterlockedExchangeSub, E));
3037  case Builtin::BI_InterlockedOr8:
3038  case Builtin::BI_InterlockedOr16:
3039  case Builtin::BI_InterlockedOr:
3040  return RValue::get(EmitMSVCBuiltinExpr(MSVCIntrin::_InterlockedOr, E));
3041  case Builtin::BI_InterlockedXor8:
3042  case Builtin::BI_InterlockedXor16:
3043  case Builtin::BI_InterlockedXor:
3044  return RValue::get(EmitMSVCBuiltinExpr(MSVCIntrin::_InterlockedXor, E));
3045 
3046  case Builtin::BI_bittest64:
3047  case Builtin::BI_bittest:
3048  case Builtin::BI_bittestandcomplement64:
3049  case Builtin::BI_bittestandcomplement:
3050  case Builtin::BI_bittestandreset64:
3051  case Builtin::BI_bittestandreset:
3052  case Builtin::BI_bittestandset64:
3053  case Builtin::BI_bittestandset:
3054  case Builtin::BI_interlockedbittestandreset:
3055  case Builtin::BI_interlockedbittestandreset64:
3056  case Builtin::BI_interlockedbittestandset64:
3057  case Builtin::BI_interlockedbittestandset:
3058  case Builtin::BI_interlockedbittestandset_acq:
3059  case Builtin::BI_interlockedbittestandset_rel:
3060  case Builtin::BI_interlockedbittestandset_nf:
3061  case Builtin::BI_interlockedbittestandreset_acq:
3062  case Builtin::BI_interlockedbittestandreset_rel:
3063  case Builtin::BI_interlockedbittestandreset_nf:
3064  return RValue::get(EmitBitTestIntrinsic(*this, BuiltinID, E));
3065 
3066  case Builtin::BI__exception_code:
3067  case Builtin::BI_exception_code:
3068  return RValue::get(EmitSEHExceptionCode());
3069  case Builtin::BI__exception_info:
3070  case Builtin::BI_exception_info:
3071  return RValue::get(EmitSEHExceptionInfo());
3072  case Builtin::BI__abnormal_termination:
3073  case Builtin::BI_abnormal_termination:
3074  return RValue::get(EmitSEHAbnormalTermination());
3075  case Builtin::BI_setjmpex:
3076  if (getTarget().getTriple().isOSMSVCRT())
3077  return EmitMSVCRTSetJmp(*this, MSVCSetJmpKind::_setjmpex, E);
3078  break;
3079  case Builtin::BI_setjmp:
3080  if (getTarget().getTriple().isOSMSVCRT()) {
3081  if (getTarget().getTriple().getArch() == llvm::Triple::x86)
3082  return EmitMSVCRTSetJmp(*this, MSVCSetJmpKind::_setjmp3, E);
3083  else if (getTarget().getTriple().getArch() == llvm::Triple::aarch64)
3084  return EmitMSVCRTSetJmp(*this, MSVCSetJmpKind::_setjmpex, E);
3085  return EmitMSVCRTSetJmp(*this, MSVCSetJmpKind::_setjmp, E);
3086  }
3087  break;
3088 
3089  case Builtin::BI__GetExceptionInfo: {
3090  if (llvm::GlobalVariable *GV =
3091  CGM.getCXXABI().getThrowInfo(FD->getParamDecl(0)->getType()))
3092  return RValue::get(llvm::ConstantExpr::getBitCast(GV, CGM.Int8PtrTy));
3093  break;
3094  }
3095 
3096  case Builtin::BI__fastfail:
3097  return RValue::get(EmitMSVCBuiltinExpr(MSVCIntrin::__fastfail, E));
3098 
3099  case Builtin::BI__builtin_coro_size: {
3100  auto & Context = getContext();
3101  auto SizeTy = Context.getSizeType();
3102  auto T = Builder.getIntNTy(Context.getTypeSize(SizeTy));
3103  Value *F = CGM.getIntrinsic(Intrinsic::coro_size, T);
3104  return RValue::get(Builder.CreateCall(F));
3105  }
3106 
3107  case Builtin::BI__builtin_coro_id:
3108  return EmitCoroutineIntrinsic(E, Intrinsic::coro_id);
3109  case Builtin::BI__builtin_coro_promise:
3110  return EmitCoroutineIntrinsic(E, Intrinsic::coro_promise);
3111  case Builtin::BI__builtin_coro_resume:
3112  return EmitCoroutineIntrinsic(E, Intrinsic::coro_resume);
3113  case Builtin::BI__builtin_coro_frame:
3114  return EmitCoroutineIntrinsic(E, Intrinsic::coro_frame);
3115  case Builtin::BI__builtin_coro_noop:
3116  return EmitCoroutineIntrinsic(E, Intrinsic::coro_noop);
3117  case Builtin::BI__builtin_coro_free:
3118  return EmitCoroutineIntrinsic(E, Intrinsic::coro_free);
3119  case Builtin::BI__builtin_coro_destroy:
3120  return EmitCoroutineIntrinsic(E, Intrinsic::coro_destroy);
3121  case Builtin::BI__builtin_coro_done:
3122  return EmitCoroutineIntrinsic(E, Intrinsic::coro_done);
3123  case Builtin::BI__builtin_coro_alloc:
3124  return EmitCoroutineIntrinsic(E, Intrinsic::coro_alloc);
3125  case Builtin::BI__builtin_coro_begin:
3126  return EmitCoroutineIntrinsic(E, Intrinsic::coro_begin);
3127  case Builtin::BI__builtin_coro_end:
3128  return EmitCoroutineIntrinsic(E, Intrinsic::coro_end);
3129  case Builtin::BI__builtin_coro_suspend:
3130  return EmitCoroutineIntrinsic(E, Intrinsic::coro_suspend);
3131  case Builtin::BI__builtin_coro_param:
3132  return EmitCoroutineIntrinsic(E, Intrinsic::coro_param);
3133 
3134  // OpenCL v2.0 s6.13.16.2, Built-in pipe read and write functions
3135  case Builtin::BIread_pipe:
3136  case Builtin::BIwrite_pipe: {
3137  Value *Arg0 = EmitScalarExpr(E->getArg(0)),
3138  *Arg1 = EmitScalarExpr(E->getArg(1));
3139  CGOpenCLRuntime OpenCLRT(CGM);
3140  Value *PacketSize = OpenCLRT.getPipeElemSize(E->getArg(0));
3141  Value *PacketAlign = OpenCLRT.getPipeElemAlign(E->getArg(0));
3142 
3143  // Type of the generic packet parameter.
3144  unsigned GenericAS =
3145  getContext().getTargetAddressSpace(LangAS::opencl_generic);
3146  llvm::Type *I8PTy = llvm::PointerType::get(
3147  llvm::Type::getInt8Ty(getLLVMContext()), GenericAS);
3148 
3149  // Testing which overloaded version we should generate the call for.
3150  if (2U == E->getNumArgs()) {
3151  const char *Name = (BuiltinID == Builtin::BIread_pipe) ? "__read_pipe_2"
3152  : "__write_pipe_2";
3153  // Creating a generic function type to be able to call with any builtin or
3154  // user defined type.
3155  llvm::Type *ArgTys[] = {Arg0->getType(), I8PTy, Int32Ty, Int32Ty};
3156  llvm::FunctionType *FTy = llvm::FunctionType::get(
3157  Int32Ty, llvm::ArrayRef<llvm::Type *>(ArgTys), false);
3158  Value *BCast = Builder.CreatePointerCast(Arg1, I8PTy);
3159  return RValue::get(
3160  Builder.CreateCall(CGM.CreateRuntimeFunction(FTy, Name),
3161  {Arg0, BCast, PacketSize, PacketAlign}));
3162  } else {
3163  assert(4 == E->getNumArgs() &&
3164  "Illegal number of parameters to pipe function");
3165  const char *Name = (BuiltinID == Builtin::BIread_pipe) ? "__read_pipe_4"
3166  : "__write_pipe_4";
3167 
3168  llvm::Type *ArgTys[] = {Arg0->getType(), Arg1->getType(), Int32Ty, I8PTy,
3169  Int32Ty, Int32Ty};
3170  Value *Arg2 = EmitScalarExpr(E->getArg(2)),
3171  *Arg3 = EmitScalarExpr(E->getArg(3));
3172  llvm::FunctionType *FTy = llvm::FunctionType::get(
3173  Int32Ty, llvm::ArrayRef<llvm::Type *>(ArgTys), false);
3174  Value *BCast = Builder.CreatePointerCast(Arg3, I8PTy);
3175  // We know the third argument is an integer type, but we may need to cast
3176  // it to i32.
3177  if (Arg2->getType() != Int32Ty)
3178  Arg2 = Builder.CreateZExtOrTrunc(Arg2, Int32Ty);
3179  return RValue::get(Builder.CreateCall(
3180  CGM.CreateRuntimeFunction(FTy, Name),
3181  {Arg0, Arg1, Arg2, BCast, PacketSize, PacketAlign}));
3182  }
3183  }
3184  // OpenCL v2.0 s6.13.16 ,s9.17.3.5 - Built-in pipe reserve read and write
3185  // functions
3186  case Builtin::BIreserve_read_pipe:
3187  case Builtin::BIreserve_write_pipe:
3188  case Builtin::BIwork_group_reserve_read_pipe:
3189  case Builtin::BIwork_group_reserve_write_pipe:
3190  case Builtin::BIsub_group_reserve_read_pipe:
3191  case Builtin::BIsub_group_reserve_write_pipe: {
3192  // Composing the mangled name for the function.
3193  const char *Name;
3194  if (BuiltinID == Builtin::BIreserve_read_pipe)
3195  Name = "__reserve_read_pipe";
3196  else if (BuiltinID == Builtin::BIreserve_write_pipe)
3197  Name = "__reserve_write_pipe";
3198  else if (BuiltinID == Builtin::BIwork_group_reserve_read_pipe)
3199  Name = "__work_group_reserve_read_pipe";
3200  else if (BuiltinID == Builtin::BIwork_group_reserve_write_pipe)
3201  Name = "__work_group_reserve_write_pipe";
3202  else if (BuiltinID == Builtin::BIsub_group_reserve_read_pipe)
3203  Name = "__sub_group_reserve_read_pipe";
3204  else
3205  Name = "__sub_group_reserve_write_pipe";
3206 
3207  Value *Arg0 = EmitScalarExpr(E->getArg(0)),
3208  *Arg1 = EmitScalarExpr(E->getArg(1));
3209  llvm::Type *ReservedIDTy = ConvertType(getContext().OCLReserveIDTy);
3210  CGOpenCLRuntime OpenCLRT(CGM);
3211  Value *PacketSize = OpenCLRT.getPipeElemSize(E->getArg(0));
3212  Value *PacketAlign = OpenCLRT.getPipeElemAlign(E->getArg(0));
3213 
3214  // Building the generic function prototype.
3215  llvm::Type *ArgTys[] = {Arg0->getType(), Int32Ty, Int32Ty, Int32Ty};
3216  llvm::FunctionType *FTy = llvm::FunctionType::get(
3217  ReservedIDTy, llvm::ArrayRef<llvm::Type *>(ArgTys), false);
3218  // We know the second argument is an integer type, but we may need to cast
3219  // it to i32.
3220  if (Arg1->getType() != Int32Ty)
3221  Arg1 = Builder.CreateZExtOrTrunc(Arg1, Int32Ty);
3222  return RValue::get(
3223  Builder.CreateCall(CGM.CreateRuntimeFunction(FTy, Name),
3224  {Arg0, Arg1, PacketSize, PacketAlign}));
3225  }
3226  // OpenCL v2.0 s6.13.16, s9.17.3.5 - Built-in pipe commit read and write
3227  // functions
3228  case Builtin::BIcommit_read_pipe:
3229  case Builtin::BIcommit_write_pipe:
3230  case Builtin::BIwork_group_commit_read_pipe:
3231  case Builtin::BIwork_group_commit_write_pipe:
3232  case Builtin::BIsub_group_commit_read_pipe:
3233  case Builtin::BIsub_group_commit_write_pipe: {
3234  const char *Name;
3235  if (BuiltinID == Builtin::BIcommit_read_pipe)
3236  Name = "__commit_read_pipe";
3237  else if (BuiltinID == Builtin::BIcommit_write_pipe)
3238  Name = "__commit_write_pipe";
3239  else if (BuiltinID == Builtin::BIwork_group_commit_read_pipe)
3240  Name = "__work_group_commit_read_pipe";
3241  else if (BuiltinID == Builtin::BIwork_group_commit_write_pipe)
3242  Name = "__work_group_commit_write_pipe";
3243  else if (BuiltinID == Builtin::BIsub_group_commit_read_pipe)
3244  Name = "__sub_group_commit_read_pipe";
3245  else
3246  Name = "__sub_group_commit_write_pipe";
3247 
3248  Value *Arg0 = EmitScalarExpr(E->getArg(0)),
3249  *Arg1 = EmitScalarExpr(E->getArg(1));
3250  CGOpenCLRuntime OpenCLRT(CGM);
3251  Value *PacketSize = OpenCLRT.getPipeElemSize(E->getArg(0));
3252  Value *PacketAlign = OpenCLRT.getPipeElemAlign(E->getArg(0));
3253 
3254  // Building the generic function prototype.
3255  llvm::Type *ArgTys[] = {Arg0->getType(), Arg1->getType(), Int32Ty, Int32Ty};
3256  llvm::FunctionType *FTy =
3257  llvm::FunctionType::get(llvm::Type::getVoidTy(getLLVMContext()),
3258  llvm::ArrayRef<llvm::Type *>(ArgTys), false);
3259 
3260  return RValue::get(
3261  Builder.CreateCall(CGM.CreateRuntimeFunction(FTy, Name),
3262  {Arg0, Arg1, PacketSize, PacketAlign}));
3263  }
3264  // OpenCL v2.0 s6.13.16.4 Built-in pipe query functions
3265  case Builtin::BIget_pipe_num_packets:
3266  case Builtin::BIget_pipe_max_packets: {
3267  const char *BaseName;
3268  const PipeType *PipeTy = E->getArg(0)->getType()->getAs<PipeType>();
3269  if (BuiltinID == Builtin::BIget_pipe_num_packets)
3270  BaseName = "__get_pipe_num_packets";
3271  else
3272  BaseName = "__get_pipe_max_packets";
3273  auto Name = std::string(BaseName) +
3274  std::string(PipeTy->isReadOnly() ? "_ro" : "_wo");
3275 
3276  // Building the generic function prototype.
3277  Value *Arg0 = EmitScalarExpr(E->getArg(0));
3278  CGOpenCLRuntime OpenCLRT(CGM);
3279  Value *PacketSize = OpenCLRT.getPipeElemSize(E->getArg(0));
3280  Value *PacketAlign = OpenCLRT.getPipeElemAlign(E->getArg(0));
3281  llvm::Type *ArgTys[] = {Arg0->getType(), Int32Ty, Int32Ty};
3282  llvm::FunctionType *FTy = llvm::FunctionType::get(
3283  Int32Ty, llvm::ArrayRef<llvm::Type *>(ArgTys), false);
3284 
3285  return RValue::get(Builder.CreateCall(CGM.CreateRuntimeFunction(FTy, Name),
3286  {Arg0, PacketSize, PacketAlign}));
3287  }
3288 
3289  // OpenCL v2.0 s6.13.9 - Address space qualifier functions.
3290  case Builtin::BIto_global:
3291  case Builtin::BIto_local:
3292  case Builtin::BIto_private: {
3293  auto Arg0 = EmitScalarExpr(E->getArg(0));
3294  auto NewArgT = llvm::PointerType::get(Int8Ty,
3295  CGM.getContext().getTargetAddressSpace(LangAS::opencl_generic));
3296  auto NewRetT = llvm::PointerType::get(Int8Ty,
3297  CGM.getContext().getTargetAddressSpace(
3299  auto FTy = llvm::FunctionType::get(NewRetT, {NewArgT}, false);
3300  llvm::Value *NewArg;
3301  if (Arg0->getType()->getPointerAddressSpace() !=
3302  NewArgT->getPointerAddressSpace())
3303  NewArg = Builder.CreateAddrSpaceCast(Arg0, NewArgT);
3304  else
3305  NewArg = Builder.CreateBitOrPointerCast(Arg0, NewArgT);
3306  auto NewName = std::string("__") + E->getDirectCallee()->getName().str();
3307  auto NewCall =
3308  Builder.CreateCall(CGM.CreateRuntimeFunction(FTy, NewName), {NewArg});
3309  return RValue::get(Builder.CreateBitOrPointerCast(NewCall,
3310  ConvertType(E->getType())));
3311  }
3312 
3313  // OpenCL v2.0, s6.13.17 - Enqueue kernel function.
3314  // It contains four different overload formats specified in Table 6.13.17.1.
3315  case Builtin::BIenqueue_kernel: {
3316  StringRef Name; // Generated function call name
3317  unsigned NumArgs = E->getNumArgs();
3318 
3319  llvm::Type *QueueTy = ConvertType(getContext().OCLQueueTy);
3320  llvm::Type *GenericVoidPtrTy = Builder.getInt8PtrTy(
3321  getContext().getTargetAddressSpace(LangAS::opencl_generic));
3322 
3323  llvm::Value *Queue = EmitScalarExpr(E->getArg(0));
3324  llvm::Value *Flags = EmitScalarExpr(E->getArg(1));
3325  LValue NDRangeL = EmitAggExprToLValue(E->getArg(2));
3326  llvm::Value *Range = NDRangeL.getAddress().getPointer();
3327  llvm::Type *RangeTy = NDRangeL.getAddress().getType();
3328 
3329  if (NumArgs == 4) {
3330  // The most basic form of the call with parameters:
3331  // queue_t, kernel_enqueue_flags_t, ndrange_t, block(void)
3332  Name = "__enqueue_kernel_basic";
3333  llvm::Type *ArgTys[] = {QueueTy, Int32Ty, RangeTy, GenericVoidPtrTy,
3334  GenericVoidPtrTy};
3335  llvm::FunctionType *FTy = llvm::FunctionType::get(
3336  Int32Ty, llvm::ArrayRef<llvm::Type *>(ArgTys), false);
3337 
3338  auto Info =
3339  CGM.getOpenCLRuntime().emitOpenCLEnqueuedBlock(*this, E->getArg(3));
3340  llvm::Value *Kernel =
3341  Builder.CreatePointerCast(Info.Kernel, GenericVoidPtrTy);
3342  llvm::Value *Block =
3343  Builder.CreatePointerCast(Info.BlockArg, GenericVoidPtrTy);
3344 
3345  AttrBuilder B;
3346  B.addAttribute(Attribute::ByVal);
3347  llvm::AttributeList ByValAttrSet =
3348  llvm::AttributeList::get(CGM.getModule().getContext(), 3U, B);
3349 
3350  auto RTCall =
3351  Builder.CreateCall(CGM.CreateRuntimeFunction(FTy, Name, ByValAttrSet),
3352  {Queue, Flags, Range, Kernel, Block});
3353  RTCall->setAttributes(ByValAttrSet);
3354  return RValue::get(RTCall);
3355  }
3356  assert(NumArgs >= 5 && "Invalid enqueue_kernel signature");
3357 
3358  // Create a temporary array to hold the sizes of local pointer arguments
3359  // for the block. \p First is the position of the first size argument.
3360  auto CreateArrayForSizeVar = [=](unsigned First)
3361  -> std::tuple<llvm::Value *, llvm::Value *, llvm::Value *> {
3362  llvm::APInt ArraySize(32, NumArgs - First);
3363  QualType SizeArrayTy = getContext().getConstantArrayType(
3364  getContext().getSizeType(), ArraySize, ArrayType::Normal,
3365  /*IndexTypeQuals=*/0);
3366  auto Tmp = CreateMemTemp(SizeArrayTy, "block_sizes");
3367  llvm::Value *TmpPtr = Tmp.getPointer();
3368  llvm::Value *TmpSize = EmitLifetimeStart(
3369  CGM.getDataLayout().getTypeAllocSize(Tmp.getElementType()), TmpPtr);
3370  llvm::Value *ElemPtr;
3371  // Each of the following arguments specifies the size of the corresponding
3372  // argument passed to the enqueued block.
3373  auto *Zero = llvm::ConstantInt::get(IntTy, 0);
3374  for (unsigned I = First; I < NumArgs; ++I) {
3375  auto *Index = llvm::ConstantInt::get(IntTy, I - First);
3376  auto *GEP = Builder.CreateGEP(TmpPtr, {Zero, Index});
3377  if (I == First)
3378  ElemPtr = GEP;
3379  auto *V =
3380  Builder.CreateZExtOrTrunc(EmitScalarExpr(E->getArg(I)), SizeTy);
3381  Builder.CreateAlignedStore(
3382  V, GEP, CGM.getDataLayout().getPrefTypeAlignment(SizeTy));
3383  }
3384  return std::tie(ElemPtr, TmpSize, TmpPtr);
3385  };
3386 
3387  // Could have events and/or varargs.
3388  if (E->getArg(3)->getType()->isBlockPointerType()) {
3389  // No events passed, but has variadic arguments.
3390  Name = "__enqueue_kernel_varargs";
3391  auto Info =
3392  CGM.getOpenCLRuntime().emitOpenCLEnqueuedBlock(*this, E->getArg(3));
3393  llvm::Value *Kernel =
3394  Builder.CreatePointerCast(Info.Kernel, GenericVoidPtrTy);
3395  auto *Block = Builder.CreatePointerCast(Info.BlockArg, GenericVoidPtrTy);
3396  llvm::Value *ElemPtr, *TmpSize, *TmpPtr;
3397  std::tie(ElemPtr, TmpSize, TmpPtr) = CreateArrayForSizeVar(4);
3398 
3399  // Create a vector of the arguments, as well as a constant value to
3400  // express to the runtime the number of variadic arguments.
3401  std::vector<llvm::Value *> Args = {
3402  Queue, Flags, Range,
3403  Kernel, Block, ConstantInt::get(IntTy, NumArgs - 4),
3404  ElemPtr};
3405  std::vector<llvm::Type *> ArgTys = {
3406  QueueTy, IntTy, RangeTy, GenericVoidPtrTy,
3407  GenericVoidPtrTy, IntTy, ElemPtr->getType()};
3408 
3409  llvm::FunctionType *FTy = llvm::FunctionType::get(
3410  Int32Ty, llvm::ArrayRef<llvm::Type *>(ArgTys), false);
3411  auto Call =
3412  RValue::get(Builder.CreateCall(CGM.CreateRuntimeFunction(FTy, Name),
3414  if (TmpSize)
3415  EmitLifetimeEnd(TmpSize, TmpPtr);
3416  return Call;
3417  }
3418  // Any calls now have event arguments passed.
3419  if (NumArgs >= 7) {
3420  llvm::Type *EventTy = ConvertType(getContext().OCLClkEventTy);
3421  llvm::Type *EventPtrTy = EventTy->getPointerTo(
3422  CGM.getContext().getTargetAddressSpace(LangAS::opencl_generic));
3423 
3424  llvm::Value *NumEvents =
3425  Builder.CreateZExtOrTrunc(EmitScalarExpr(E->getArg(3)), Int32Ty);
3426  llvm::Value *EventList =
3427  E->getArg(4)->getType()->isArrayType()
3428  ? EmitArrayToPointerDecay(E->getArg(4)).getPointer()
3429  : EmitScalarExpr(E->getArg(4));
3430  llvm::Value *ClkEvent = EmitScalarExpr(E->getArg(5));
3431  // Convert to generic address space.
3432  EventList = Builder.CreatePointerCast(EventList, EventPtrTy);
3433  ClkEvent = Builder.CreatePointerCast(ClkEvent, EventPtrTy);
3434  auto Info =
3435  CGM.getOpenCLRuntime().emitOpenCLEnqueuedBlock(*this, E->getArg(6));
3436  llvm::Value *Kernel =
3437  Builder.CreatePointerCast(Info.Kernel, GenericVoidPtrTy);
3438  llvm::Value *Block =
3439  Builder.CreatePointerCast(Info.BlockArg, GenericVoidPtrTy);
3440 
3441  std::vector<llvm::Type *> ArgTys = {
3442  QueueTy, Int32Ty, RangeTy, Int32Ty,
3443  EventPtrTy, EventPtrTy, GenericVoidPtrTy, GenericVoidPtrTy};
3444 
3445  std::vector<llvm::Value *> Args = {Queue, Flags, Range, NumEvents,
3446  EventList, ClkEvent, Kernel, Block};
3447 
3448  if (NumArgs == 7) {
3449  // Has events but no variadics.
3450  Name = "__enqueue_kernel_basic_events";
3451  llvm::FunctionType *FTy = llvm::FunctionType::get(
3452  Int32Ty, llvm::ArrayRef<llvm::Type *>(ArgTys), false);
3453  return RValue::get(
3454  Builder.CreateCall(CGM.CreateRuntimeFunction(FTy, Name),
3456  }
3457  // Has event info and variadics
3458  // Pass the number of variadics to the runtime function too.
3459  Args.push_back(ConstantInt::get(Int32Ty, NumArgs - 7));
3460  ArgTys.push_back(Int32Ty);
3461  Name = "__enqueue_kernel_events_varargs";
3462 
3463  llvm::Value *ElemPtr, *TmpSize, *TmpPtr;
3464  std::tie(ElemPtr, TmpSize, TmpPtr) = CreateArrayForSizeVar(7);
3465  Args.push_back(ElemPtr);
3466  ArgTys.push_back(ElemPtr->getType());
3467 
3468  llvm::FunctionType *FTy = llvm::FunctionType::get(
3469  Int32Ty, llvm::ArrayRef<llvm::Type *>(ArgTys), false);
3470  auto Call =
3471  RValue::get(Builder.CreateCall(CGM.CreateRuntimeFunction(FTy, Name),
3473  if (TmpSize)
3474  EmitLifetimeEnd(TmpSize, TmpPtr);
3475  return Call;
3476  }
3477  LLVM_FALLTHROUGH;
3478  }
3479  // OpenCL v2.0 s6.13.17.6 - Kernel query functions need bitcast of block
3480  // parameter.
3481  case Builtin::BIget_kernel_work_group_size: {
3482  llvm::Type *GenericVoidPtrTy = Builder.getInt8PtrTy(
3483  getContext().getTargetAddressSpace(LangAS::opencl_generic));
3484  auto Info =
3485  CGM.getOpenCLRuntime().emitOpenCLEnqueuedBlock(*this, E->getArg(0));
3486  Value *Kernel = Builder.CreatePointerCast(Info.Kernel, GenericVoidPtrTy);
3487  Value *Arg = Builder.CreatePointerCast(Info.BlockArg, GenericVoidPtrTy);
3488  return RValue::get(Builder.CreateCall(
3489  CGM.CreateRuntimeFunction(
3490  llvm::FunctionType::get(IntTy, {GenericVoidPtrTy, GenericVoidPtrTy},
3491  false),
3492  "__get_kernel_work_group_size_impl"),
3493  {Kernel, Arg}));
3494  }
3495  case Builtin::BIget_kernel_preferred_work_group_size_multiple: {
3496  llvm::Type *GenericVoidPtrTy = Builder.getInt8PtrTy(
3497  getContext().getTargetAddressSpace(LangAS::opencl_generic));
3498  auto Info =
3499  CGM.getOpenCLRuntime().emitOpenCLEnqueuedBlock(*this, E->getArg(0));
3500  Value *Kernel = Builder.CreatePointerCast(Info.Kernel, GenericVoidPtrTy);
3501  Value *Arg = Builder.CreatePointerCast(Info.BlockArg, GenericVoidPtrTy);
3502  return RValue::get(Builder.CreateCall(
3503  CGM.CreateRuntimeFunction(
3504  llvm::FunctionType::get(IntTy, {GenericVoidPtrTy, GenericVoidPtrTy},
3505  false),
3506  "__get_kernel_preferred_work_group_size_multiple_impl"),
3507  {Kernel, Arg}));
3508  }
3509  case Builtin::BIget_kernel_max_sub_group_size_for_ndrange:
3510  case Builtin::BIget_kernel_sub_group_count_for_ndrange: {
3511  llvm::Type *GenericVoidPtrTy = Builder.getInt8PtrTy(
3512  getContext().getTargetAddressSpace(LangAS::opencl_generic));
3513  LValue NDRangeL = EmitAggExprToLValue(E->getArg(0));
3514  llvm::Value *NDRange = NDRangeL.getAddress().getPointer();
3515  auto Info =
3516  CGM.getOpenCLRuntime().emitOpenCLEnqueuedBlock(*this, E->getArg(1));
3517  Value *Kernel = Builder.CreatePointerCast(Info.Kernel, GenericVoidPtrTy);
3518  Value *Block = Builder.CreatePointerCast(Info.BlockArg, GenericVoidPtrTy);
3519  const char *Name =
3520  BuiltinID == Builtin::BIget_kernel_max_sub_group_size_for_ndrange
3521  ? "__get_kernel_max_sub_group_size_for_ndrange_impl"
3522  : "__get_kernel_sub_group_count_for_ndrange_impl";
3523  return RValue::get(Builder.CreateCall(
3524  CGM.CreateRuntimeFunction(
3525  llvm::FunctionType::get(
3526  IntTy, {NDRange->getType(), GenericVoidPtrTy, GenericVoidPtrTy},
3527  false),
3528  Name),
3529  {NDRange, Kernel, Block}));
3530  }
3531 
3532  case Builtin::BI__builtin_store_half:
3533  case Builtin::BI__builtin_store_halff: {
3534  Value *Val = EmitScalarExpr(E->getArg(0));
3535  Address Address = EmitPointerWithAlignment(E->getArg(1));
3536  Value *HalfVal = Builder.CreateFPTrunc(Val, Builder.getHalfTy());
3537  return RValue::get(Builder.CreateStore(HalfVal, Address));
3538  }
3539  case Builtin::BI__builtin_load_half: {
3540  Address Address = EmitPointerWithAlignment(E->getArg(0));
3541  Value *HalfVal = Builder.CreateLoad(Address);
3542  return RValue::get(Builder.CreateFPExt(HalfVal, Builder.getDoubleTy()));
3543  }
3544  case Builtin::BI__builtin_load_halff: {
3545  Address Address = EmitPointerWithAlignment(E->getArg(0));
3546  Value *HalfVal = Builder.CreateLoad(Address);
3547  return RValue::get(Builder.CreateFPExt(HalfVal, Builder.getFloatTy()));
3548  }
3549  case Builtin::BIprintf:
3550  if (getTarget().getTriple().isNVPTX())
3551  return EmitNVPTXDevicePrintfCallExpr(E, ReturnValue);
3552  break;
3553  case Builtin::BI__builtin_canonicalize:
3554  case Builtin::BI__builtin_canonicalizef:
3555  case Builtin::BI__builtin_canonicalizel:
3556  return RValue::get(emitUnaryBuiltin(*this, E, Intrinsic::canonicalize));
3557 
3558  case Builtin::BI__builtin_thread_pointer: {
3559  if (!getContext().getTargetInfo().isTLSSupported())
3560  CGM.ErrorUnsupported(E, "__builtin_thread_pointer");
3561  // Fall through - it's already mapped to the intrinsic by GCCBuiltin.
3562  break;
3563  }
3564  case Builtin::BI__builtin_os_log_format:
3565  return emitBuiltinOSLogFormat(*E);
3566 
3567  case Builtin::BI__builtin_os_log_format_buffer_size: {
3569  analyze_os_log::computeOSLogBufferLayout(CGM.getContext(), E, Layout);
3570  return RValue::get(ConstantInt::get(ConvertType(E->getType()),
3571  Layout.size().getQuantity()));
3572  }
3573 
3574  case Builtin::BI__xray_customevent: {
3575  if (!ShouldXRayInstrumentFunction())
3576  return RValue::getIgnored();
3577 
3578  if (!CGM.getCodeGenOpts().XRayInstrumentationBundle.has(
3580  return RValue::getIgnored();
3581 
3582  if (const auto *XRayAttr = CurFuncDecl->getAttr<XRayInstrumentAttr>())
3583  if (XRayAttr->neverXRayInstrument() && !AlwaysEmitXRayCustomEvents())
3584  return RValue::getIgnored();
3585 
3586  Function *F = CGM.getIntrinsic(Intrinsic::xray_customevent);
3587  auto FTy = F->getFunctionType();
3588  auto Arg0 = E->getArg(0);
3589  auto Arg0Val = EmitScalarExpr(Arg0);
3590  auto Arg0Ty = Arg0->getType();
3591  auto PTy0 = FTy->getParamType(0);
3592  if (PTy0 != Arg0Val->getType()) {
3593  if (Arg0Ty->isArrayType())
3594  Arg0Val = EmitArrayToPointerDecay(Arg0).getPointer();
3595  else
3596  Arg0Val = Builder.CreatePointerCast(Arg0Val, PTy0);
3597  }
3598  auto Arg1 = EmitScalarExpr(E->getArg(1));
3599  auto PTy1 = FTy->getParamType(1);
3600  if (PTy1 != Arg1->getType())
3601  Arg1 = Builder.CreateTruncOrBitCast(Arg1, PTy1);
3602  return RValue::get(Builder.CreateCall(F, {Arg0Val, Arg1}));
3603  }
3604 
3605  case Builtin::BI__xray_typedevent: {
3606  // TODO: There should be a way to always emit events even if the current
3607  // function is not instrumented. Losing events in a stream can cripple
3608  // a trace.
3609  if (!ShouldXRayInstrumentFunction())
3610  return RValue::getIgnored();
3611 
3612  if (!CGM.getCodeGenOpts().XRayInstrumentationBundle.has(
3614  return RValue::getIgnored();
3615 
3616  if (const auto *XRayAttr = CurFuncDecl->getAttr<XRayInstrumentAttr>())
3617  if (XRayAttr->neverXRayInstrument() && !AlwaysEmitXRayTypedEvents())
3618  return RValue::getIgnored();
3619 
3620  Function *F = CGM.getIntrinsic(Intrinsic::xray_typedevent);
3621  auto FTy = F->getFunctionType();
3622  auto Arg0 = EmitScalarExpr(E->getArg(0));
3623  auto PTy0 = FTy->getParamType(0);
3624  if (PTy0 != Arg0->getType())
3625  Arg0 = Builder.CreateTruncOrBitCast(Arg0, PTy0);
3626  auto Arg1 = E->getArg(1);
3627  auto Arg1Val = EmitScalarExpr(Arg1);
3628  auto Arg1Ty = Arg1->getType();
3629  auto PTy1 = FTy->getParamType(1);
3630  if (PTy1 != Arg1Val->getType()) {
3631  if (Arg1Ty->isArrayType())
3632  Arg1Val = EmitArrayToPointerDecay(Arg1).getPointer();
3633  else
3634  Arg1Val = Builder.CreatePointerCast(Arg1Val, PTy1);
3635  }
3636  auto Arg2 = EmitScalarExpr(E->getArg(2));
3637  auto PTy2 = FTy->getParamType(2);
3638  if (PTy2 != Arg2->getType())
3639  Arg2 = Builder.CreateTruncOrBitCast(Arg2, PTy2);
3640  return RValue::get(Builder.CreateCall(F, {Arg0, Arg1Val, Arg2}));
3641  }
3642 
3643  case Builtin::BI__builtin_ms_va_start:
3644  case Builtin::BI__builtin_ms_va_end:
3645  return RValue::get(
3646  EmitVAStartEnd(EmitMSVAListRef(E->getArg(0)).getPointer(),
3647  BuiltinID == Builtin::BI__builtin_ms_va_start));
3648 
3649  case Builtin::BI__builtin_ms_va_copy: {
3650  // Lower this manually. We can't reliably determine whether or not any
3651  // given va_copy() is for a Win64 va_list from the calling convention
3652  // alone, because it's legal to do this from a System V ABI function.
3653  // With opaque pointer types, we won't have enough information in LLVM
3654  // IR to determine this from the argument types, either. Best to do it
3655  // now, while we have enough information.
3656  Address DestAddr = EmitMSVAListRef(E->getArg(0));
3657  Address SrcAddr = EmitMSVAListRef(E->getArg(1));
3658 
3659  llvm::Type *BPP = Int8PtrPtrTy;
3660 
3661  DestAddr = Address(Builder.CreateBitCast(DestAddr.getPointer(), BPP, "cp"),
3662  DestAddr.getAlignment());
3663  SrcAddr = Address(Builder.CreateBitCast(SrcAddr.getPointer(), BPP, "ap"),
3664  SrcAddr.getAlignment());
3665 
3666  Value *ArgPtr = Builder.CreateLoad(SrcAddr, "ap.val");
3667  return RValue::get(Builder.CreateStore(ArgPtr, DestAddr));
3668  }
3669  }
3670 
3671  // If this is an alias for a lib function (e.g. __builtin_sin), emit
3672  // the call using the normal call path, but using the unmangled
3673  // version of the function name.
3674  if (getContext().BuiltinInfo.isLibFunction(BuiltinID))
3675  return emitLibraryCall(*this, FD, E,
3676  CGM.getBuiltinLibFunction(FD, BuiltinID));
3677 
3678  // If this is a predefined lib function (e.g. malloc), emit the call
3679  // using exactly the normal call path.
3680  if (getContext().BuiltinInfo.isPredefinedLibFunction(BuiltinID))
3681  return emitLibraryCall(*this, FD, E,
3682  cast<llvm::Constant>(EmitScalarExpr(E->getCallee())));
3683 
3684  // Check that a call to a target specific builtin has the correct target
3685  // features.
3686  // This is down here to avoid non-target specific builtins, however, if
3687  // generic builtins start to require generic target features then we
3688  // can move this up to the beginning of the function.
3689  checkTargetFeatures(E, FD);
3690 
3691  if (unsigned VectorWidth = getContext().BuiltinInfo.getRequiredVectorWidth(BuiltinID))
3692  LargestVectorWidth = std::max(LargestVectorWidth, VectorWidth);
3693 
3694  // See if we have a target specific intrinsic.
3695  const char *Name = getContext().BuiltinInfo.getName(BuiltinID);
3696  Intrinsic::ID IntrinsicID = Intrinsic::not_intrinsic;
3697  StringRef Prefix =
3698  llvm::Triple::getArchTypePrefix(getTarget().getTriple().getArch());
3699  if (!Prefix.empty()) {
3700  IntrinsicID = Intrinsic::getIntrinsicForGCCBuiltin(Prefix.data(), Name);
3701  // NOTE we don't need to perform a compatibility flag check here since the
3702  // intrinsics are declared in Builtins*.def via LANGBUILTIN which filter the
3703  // MS builtins via ALL_MS_LANGUAGES and are filtered earlier.
3704  if (IntrinsicID == Intrinsic::not_intrinsic)
3705  IntrinsicID = Intrinsic::getIntrinsicForMSBuiltin(Prefix.data(), Name);
3706  }
3707 
3708  if (IntrinsicID != Intrinsic::not_intrinsic) {
3710 
3711  // Find out if any arguments are required to be integer constant
3712  // expressions.
3713  unsigned ICEArguments = 0;
3715  getContext().GetBuiltinType(BuiltinID, Error, &ICEArguments);
3716  assert(Error == ASTContext::GE_None && "Should not codegen an error");
3717 
3718  Function *F = CGM.getIntrinsic(IntrinsicID);
3719  llvm::FunctionType *FTy = F->getFunctionType();
3720 
3721  for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
3722  Value *ArgValue;
3723  // If this is a normal argument, just emit it as a scalar.
3724  if ((ICEArguments & (1 << i)) == 0) {
3725  ArgValue = EmitScalarExpr(E->getArg(i));
3726  } else {
3727  // If this is required to be a constant, constant fold it so that we
3728  // know that the generated intrinsic gets a ConstantInt.
3729  llvm::APSInt Result;
3730  bool IsConst = E->getArg(i)->isIntegerConstantExpr(Result,getContext());
3731  assert(IsConst && "Constant arg isn't actually constant?");
3732  (void)IsConst;
3733  ArgValue = llvm::ConstantInt::get(getLLVMContext(), Result);
3734  }
3735 
3736  // If the intrinsic arg type is different from the builtin arg type
3737  // we need to do a bit cast.
3738  llvm::Type *PTy = FTy->getParamType(i);
3739  if (PTy != ArgValue->getType()) {
3740  // XXX - vector of pointers?
3741  if (auto *PtrTy = dyn_cast<llvm::PointerType>(PTy)) {
3742  if (PtrTy->getAddressSpace() !=
3743  ArgValue->getType()->getPointerAddressSpace()) {
3744  ArgValue = Builder.CreateAddrSpaceCast(
3745  ArgValue,
3746  ArgValue->getType()->getPointerTo(PtrTy->getAddressSpace()));
3747  }
3748  }
3749 
3750  assert(PTy->canLosslesslyBitCastTo(FTy->getParamType(i)) &&
3751  "Must be able to losslessly bit cast to param");
3752  ArgValue = Builder.CreateBitCast(ArgValue, PTy);
3753  }
3754 
3755  Args.push_back(ArgValue);
3756  }
3757 
3758  Value *V = Builder.CreateCall(F, Args);
3759  QualType BuiltinRetType = E->getType();
3760 
3761  llvm::Type *RetTy = VoidTy;
3762  if (!BuiltinRetType->isVoidType())
3763  RetTy = ConvertType(BuiltinRetType);
3764 
3765  if (RetTy != V->getType()) {
3766  // XXX - vector of pointers?
3767  if (auto *PtrTy = dyn_cast<llvm::PointerType>(RetTy)) {
3768  if (PtrTy->getAddressSpace() != V->getType()->getPointerAddressSpace()) {
3769  V = Builder.CreateAddrSpaceCast(
3770  V, V->getType()->getPointerTo(PtrTy->getAddressSpace()));
3771  }
3772  }
3773 
3774  assert(V->getType()->canLosslesslyBitCastTo(RetTy) &&
3775  "Must be able to losslessly bit cast result type");
3776  V = Builder.CreateBitCast(V, RetTy);
3777  }
3778 
3779  return RValue::get(V);
3780  }
3781 
3782  // See if we have a target specific builtin that needs to be lowered.
3783  if (Value *V = EmitTargetBuiltinExpr(BuiltinID, E))
3784  return RValue::get(V);
3785 
3786  ErrorUnsupported(E, "builtin function");
3787 
3788  // Unknown builtin, for now just dump it out and return undef.
3789  return GetUndefRValue(E->getType());
3790 }
3791 
3793  unsigned BuiltinID, const CallExpr *E,
3794  llvm::Triple::ArchType Arch) {
3795  switch (Arch) {
3796  case llvm::Triple::arm:
3797  case llvm::Triple::armeb:
3798  case llvm::Triple::thumb:
3799  case llvm::Triple::thumbeb:
3800  return CGF->EmitARMBuiltinExpr(BuiltinID, E, Arch);
3801  case llvm::Triple::aarch64:
3802  case llvm::Triple::aarch64_be:
3803  return CGF->EmitAArch64BuiltinExpr(BuiltinID, E, Arch);
3804  case llvm::Triple::x86:
3805  case llvm::Triple::x86_64:
3806  return CGF->EmitX86BuiltinExpr(BuiltinID, E);
3807  case llvm::Triple::ppc:
3808  case llvm::Triple::ppc64:
3809  case llvm::Triple::ppc64le:
3810  return CGF->EmitPPCBuiltinExpr(BuiltinID, E);
3811  case llvm::Triple::r600:
3812  case llvm::Triple::amdgcn:
3813  return CGF->EmitAMDGPUBuiltinExpr(BuiltinID, E);
3814  case llvm::Triple::systemz:
3815  return CGF->EmitSystemZBuiltinExpr(BuiltinID, E);
3816  case llvm::Triple::nvptx:
3817  case llvm::Triple::nvptx64:
3818  return CGF->EmitNVPTXBuiltinExpr(BuiltinID, E);
3819  case llvm::Triple::wasm32:
3820  case llvm::Triple::wasm64:
3821  return CGF->EmitWebAssemblyBuiltinExpr(BuiltinID, E);
3822  case llvm::Triple::hexagon:
3823  return CGF->EmitHexagonBuiltinExpr(BuiltinID, E);
3824  default:
3825  return nullptr;
3826  }
3827 }
3828 
3830  const CallExpr *E) {
3831  if (getContext().BuiltinInfo.isAuxBuiltinID(BuiltinID)) {
3832  assert(getContext().getAuxTargetInfo() && "Missing aux target info");
3834  this, getContext().BuiltinInfo.getAuxBuiltinID(BuiltinID), E,
3835  getContext().getAuxTargetInfo()->getTriple().getArch());
3836  }
3837 
3838  return EmitTargetArchBuiltinExpr(this, BuiltinID, E,
3839  getTarget().getTriple().getArch());
3840 }
3841 
3842 static llvm::VectorType *GetNeonType(CodeGenFunction *CGF,
3843  NeonTypeFlags TypeFlags,
3844  bool HasLegalHalfType=true,
3845  bool V1Ty=false) {
3846  int IsQuad = TypeFlags.isQuad();
3847  switch (TypeFlags.getEltType()) {
3848  case NeonTypeFlags::Int8:
3849  case NeonTypeFlags::Poly8:
3850  return llvm::VectorType::get(CGF->Int8Ty, V1Ty ? 1 : (8 << IsQuad));
3851  case NeonTypeFlags::Int16:
3852  case NeonTypeFlags::Poly16:
3853  return llvm::VectorType::get(CGF->Int16Ty, V1Ty ? 1 : (4 << IsQuad));
3855  if (HasLegalHalfType)
3856  return llvm::VectorType::get(CGF->HalfTy, V1Ty ? 1 : (4 << IsQuad));
3857  else
3858  return llvm::VectorType::get(CGF->Int16Ty, V1Ty ? 1 : (4 << IsQuad));
3859  case NeonTypeFlags::Int32:
3860  return llvm::VectorType::get(CGF->Int32Ty, V1Ty ? 1 : (2 << IsQuad));
3861  case NeonTypeFlags::Int64:
3862  case NeonTypeFlags::Poly64:
3863  return llvm::VectorType::get(CGF->Int64Ty, V1Ty ? 1 : (1 << IsQuad));
3865  // FIXME: i128 and f128 doesn't get fully support in Clang and llvm.
3866  // There is a lot of i128 and f128 API missing.
3867  // so we use v16i8 to represent poly128 and get pattern matched.
3868  return llvm::VectorType::get(CGF->Int8Ty, 16);
3870  return llvm::VectorType::get(CGF->FloatTy, V1Ty ? 1 : (2 << IsQuad));
3872  return llvm::VectorType::get(CGF->DoubleTy, V1Ty ? 1 : (1 << IsQuad));
3873  }
3874  llvm_unreachable("Unknown vector element type!");
3875 }
3876 
3877 static llvm::VectorType *GetFloatNeonType(CodeGenFunction *CGF,
3878  NeonTypeFlags IntTypeFlags) {
3879  int IsQuad = IntTypeFlags.isQuad();
3880  switch (IntTypeFlags.getEltType()) {
3881  case NeonTypeFlags::Int16:
3882  return llvm::VectorType::get(CGF->HalfTy, (4 << IsQuad));
3883  case NeonTypeFlags::Int32:
3884  return llvm::VectorType::get(CGF->FloatTy, (2 << IsQuad));
3885  case NeonTypeFlags::Int64:
3886  return llvm::VectorType::get(CGF->DoubleTy, (1 << IsQuad));
3887  default:
3888  llvm_unreachable("Type can't be converted to floating-point!");
3889  }
3890 }
3891 
3893  unsigned nElts = V->getType()->getVectorNumElements();
3894  Value* SV = llvm::ConstantVector::getSplat(nElts, C);
3895  return Builder.CreateShuffleVector(V, V, SV, "lane");
3896 }
3897 
3899  const char *name,
3900  unsigned shift, bool rightshift) {
3901  unsigned j = 0;
3902  for (Function::const_arg_iterator ai = F->arg_begin(), ae = F->arg_end();
3903  ai != ae; ++ai, ++j)
3904  if (shift > 0 && shift == j)
3905  Ops[j] = EmitNeonShiftVector(Ops[j], ai->getType(), rightshift);
3906  else
3907  Ops[j] = Builder.CreateBitCast(Ops[j], ai->getType(), name);
3908 
3909  return Builder.CreateCall(F, Ops, name);
3910 }
3911 
3913  bool neg) {
3914  int SV = cast<ConstantInt>(V)->getSExtValue();
3915  return ConstantInt::get(Ty, neg ? -SV : SV);
3916 }
3917 
3918 // Right-shift a vector by a constant.
3920  llvm::Type *Ty, bool usgn,
3921  const char *name) {
3922  llvm::VectorType *VTy = cast<llvm::VectorType>(Ty);
3923 
3924  int ShiftAmt = cast<ConstantInt>(Shift)->getSExtValue();
3925  int EltSize = VTy->getScalarSizeInBits();
3926 
3927  Vec = Builder.CreateBitCast(Vec, Ty);
3928 
3929  // lshr/ashr are undefined when the shift amount is equal to the vector
3930  // element size.
3931  if (ShiftAmt == EltSize) {
3932  if (usgn) {
3933  // Right-shifting an unsigned value by its size yields 0.
3934  return llvm::ConstantAggregateZero::get(VTy);
3935  } else {
3936  // Right-shifting a signed value by its size is equivalent
3937  // to a shift of size-1.
3938  --ShiftAmt;
3939  Shift = ConstantInt::get(VTy->getElementType(), ShiftAmt);
3940  }
3941  }
3942 
3943  Shift = EmitNeonShiftVector(Shift, Ty, false);
3944  if (usgn)
3945  return Builder.CreateLShr(Vec, Shift, name);
3946  else
3947  return Builder.CreateAShr(Vec, Shift, name);
3948 }
3949 
3950 enum {
3951  AddRetType = (1 << 0),
3952  Add1ArgType = (1 << 1),
3953  Add2ArgTypes = (1 << 2),
3954 
3955  VectorizeRetType = (1 << 3),
3956  VectorizeArgTypes = (1 << 4),
3957 
3958  InventFloatType = (1 << 5),
3959  UnsignedAlts = (1 << 6),
3960 
3961  Use64BitVectors = (1 << 7),
3962  Use128BitVectors = (1 << 8),
3963 
3970 };
3971 
3972 namespace {
3973 struct NeonIntrinsicInfo {
3974  const char *NameHint;
3975  unsigned BuiltinID;
3976  unsigned LLVMIntrinsic;
3977  unsigned AltLLVMIntrinsic;
3978  unsigned TypeModifier;
3979 
3980  bool operator<(unsigned RHSBuiltinID) const {
3981  return BuiltinID < RHSBuiltinID;
3982  }
3983  bool operator<(const NeonIntrinsicInfo &TE) const {
3984  return BuiltinID < TE.BuiltinID;
3985  }
3986 };
3987 } // end anonymous namespace
3988 
3989 #define NEONMAP0(NameBase) \
3990  { #NameBase, NEON::BI__builtin_neon_ ## NameBase, 0, 0, 0 }
3991 
3992 #define NEONMAP1(NameBase, LLVMIntrinsic, TypeModifier) \
3993  { #NameBase, NEON:: BI__builtin_neon_ ## NameBase, \
3994  Intrinsic::LLVMIntrinsic, 0, TypeModifier }
3995 
3996 #define NEONMAP2(NameBase, LLVMIntrinsic, AltLLVMIntrinsic, TypeModifier) \
3997  { #NameBase, NEON:: BI__builtin_neon_ ## NameBase, \
3998  Intrinsic::LLVMIntrinsic, Intrinsic::AltLLVMIntrinsic, \
3999  TypeModifier }
4000 
4001 static const NeonIntrinsicInfo ARMSIMDIntrinsicMap [] = {
4002  NEONMAP2(vabd_v, arm_neon_vabdu, arm_neon_vabds, Add1ArgType | UnsignedAlts),
4003  NEONMAP2(vabdq_v, arm_neon_vabdu, arm_neon_vabds, Add1ArgType | UnsignedAlts),
4004  NEONMAP1(vabs_v, arm_neon_vabs, 0),
4005  NEONMAP1(vabsq_v, arm_neon_vabs, 0),
4006  NEONMAP0(vaddhn_v),
4007  NEONMAP1(vaesdq_v, arm_neon_aesd, 0),
4008  NEONMAP1(vaeseq_v, arm_neon_aese, 0),
4009  NEONMAP1(vaesimcq_v, arm_neon_aesimc, 0),
4010  NEONMAP1(vaesmcq_v, arm_neon_aesmc, 0),
4011  NEONMAP1(vbsl_v, arm_neon_vbsl, AddRetType),
4012  NEONMAP1(vbslq_v, arm_neon_vbsl, AddRetType),
4013  NEONMAP1(vcage_v, arm_neon_vacge, 0),
4014  NEONMAP1(vcageq_v, arm_neon_vacge, 0),
4015  NEONMAP1(vcagt_v, arm_neon_vacgt, 0),
4016  NEONMAP1(vcagtq_v, arm_neon_vacgt, 0),
4017  NEONMAP1(vcale_v, arm_neon_vacge, 0),
4018  NEONMAP1(vcaleq_v, arm_neon_vacge, 0),
4019  NEONMAP1(vcalt_v, arm_neon_vacgt, 0),
4020  NEONMAP1(vcaltq_v, arm_neon_vacgt, 0),
4021  NEONMAP0(vceqz_v),
4022  NEONMAP0(vceqzq_v),
4023  NEONMAP0(vcgez_v),
4024  NEONMAP0(vcgezq_v),
4025  NEONMAP0(vcgtz_v),
4026  NEONMAP0(vcgtzq_v),
4027  NEONMAP0(vclez_v),
4028  NEONMAP0(vclezq_v),
4029  NEONMAP1(vcls_v, arm_neon_vcls, Add1ArgType),
4030  NEONMAP1(vclsq_v, arm_neon_vcls, Add1ArgType),
4031  NEONMAP0(vcltz_v),
4032  NEONMAP0(vcltzq_v),
4033  NEONMAP1(vclz_v, ctlz, Add1ArgType),
4034  NEONMAP1(vclzq_v, ctlz, Add1ArgType),
4035  NEONMAP1(vcnt_v, ctpop, Add1ArgType),
4036  NEONMAP1(vcntq_v, ctpop, Add1ArgType),
4037  NEONMAP1(vcvt_f16_f32, arm_neon_vcvtfp2hf, 0),
4038  NEONMAP0(vcvt_f16_v),
4039  NEONMAP1(vcvt_f32_f16, arm_neon_vcvthf2fp, 0),
4040  NEONMAP0(vcvt_f32_v),
4041  NEONMAP2(vcvt_n_f16_v, arm_neon_vcvtfxu2fp, arm_neon_vcvtfxs2fp, 0),
4042  NEONMAP2(vcvt_n_f32_v, arm_neon_vcvtfxu2fp, arm_neon_vcvtfxs2fp, 0),
4043  NEONMAP1(vcvt_n_s16_v, arm_neon_vcvtfp2fxs, 0),
4044  NEONMAP1(vcvt_n_s32_v, arm_neon_vcvtfp2fxs, 0),
4045  NEONMAP1(vcvt_n_s64_v, arm_neon_vcvtfp2fxs, 0),
4046  NEONMAP1(vcvt_n_u16_v, arm_neon_vcvtfp2fxu, 0),
4047  NEONMAP1(vcvt_n_u32_v, arm_neon_vcvtfp2fxu, 0),
4048  NEONMAP1(vcvt_n_u64_v, arm_neon_vcvtfp2fxu, 0),
4049  NEONMAP0(vcvt_s16_v),
4050  NEONMAP0(vcvt_s32_v),
4051  NEONMAP0(vcvt_s64_v),
4052  NEONMAP0(vcvt_u16_v),
4053  NEONMAP0(vcvt_u32_v),
4054  NEONMAP0(vcvt_u64_v),
4055  NEONMAP1(vcvta_s16_v, arm_neon_vcvtas, 0),
4056  NEONMAP1(vcvta_s32_v, arm_neon_vcvtas, 0),
4057  NEONMAP1(vcvta_s64_v, arm_neon_vcvtas, 0),
4058  NEONMAP1(vcvta_u16_v, arm_neon_vcvtau, 0),
4059  NEONMAP1(vcvta_u32_v, arm_neon_vcvtau, 0),
4060  NEONMAP1(vcvta_u64_v, arm_neon_vcvtau, 0),
4061  NEONMAP1(vcvtaq_s16_v, arm_neon_vcvtas, 0),
4062  NEONMAP1(vcvtaq_s32_v, arm_neon_vcvtas, 0),
4063  NEONMAP1(vcvtaq_s64_v, arm_neon_vcvtas, 0),
4064  NEONMAP1(vcvtaq_u16_v, arm_neon_vcvtau, 0),
4065  NEONMAP1(vcvtaq_u32_v, arm_neon_vcvtau, 0),
4066  NEONMAP1(vcvtaq_u64_v, arm_neon_vcvtau, 0),
4067  NEONMAP1(vcvtm_s16_v, arm_neon_vcvtms, 0),
4068  NEONMAP1(vcvtm_s32_v, arm_neon_vcvtms, 0),
4069  NEONMAP1(vcvtm_s64_v, arm_neon_vcvtms, 0),
4070  NEONMAP1(vcvtm_u16_v, arm_neon_vcvtmu, 0),
4071  NEONMAP1(vcvtm_u32_v, arm_neon_vcvtmu, 0),
4072  NEONMAP1(vcvtm_u64_v, arm_neon_vcvtmu, 0),
4073  NEONMAP1(vcvtmq_s16_v, arm_neon_vcvtms, 0),
4074  NEONMAP1(vcvtmq_s32_v, arm_neon_vcvtms, 0),
4075  NEONMAP1(vcvtmq_s64_v, arm_neon_vcvtms, 0),
4076  NEONMAP1(vcvtmq_u16_v, arm_neon_vcvtmu, 0),
4077  NEONMAP1(vcvtmq_u32_v, arm_neon_vcvtmu, 0),
4078  NEONMAP1(vcvtmq_u64_v, arm_neon_vcvtmu, 0),
4079  NEONMAP1(vcvtn_s16_v, arm_neon_vcvtns, 0),
4080  NEONMAP1(vcvtn_s32_v, arm_neon_vcvtns, 0),
4081  NEONMAP1(vcvtn_s64_v, arm_neon_vcvtns, 0),
4082  NEONMAP1(vcvtn_u16_v, arm_neon_vcvtnu, 0),
4083  NEONMAP1(vcvtn_u32_v, arm_neon_vcvtnu, 0),
4084  NEONMAP1(vcvtn_u64_v, arm_neon_vcvtnu, 0),
4085  NEONMAP1(vcvtnq_s16_v, arm_neon_vcvtns, 0),
4086  NEONMAP1(vcvtnq_s32_v, arm_neon_vcvtns, 0),
4087  NEONMAP1(vcvtnq_s64_v, arm_neon_vcvtns, 0),
4088  NEONMAP1(vcvtnq_u16_v, arm_neon_vcvtnu, 0),
4089  NEONMAP1(vcvtnq_u32_v, arm_neon_vcvtnu, 0),
4090  NEONMAP1(vcvtnq_u64_v, arm_neon_vcvtnu, 0),
4091  NEONMAP1(vcvtp_s16_v, arm_neon_vcvtps, 0),
4092  NEONMAP1(vcvtp_s32_v, arm_neon_vcvtps, 0),
4093  NEONMAP1(vcvtp_s64_v, arm_neon_vcvtps, 0),
4094  NEONMAP1(vcvtp_u16_v, arm_neon_vcvtpu, 0),
4095  NEONMAP1(vcvtp_u32_v, arm_neon_vcvtpu, 0),
4096  NEONMAP1(vcvtp_u64_v, arm_neon_vcvtpu, 0),
4097  NEONMAP1(vcvtpq_s16_v, arm_neon_vcvtps, 0),
4098  NEONMAP1(vcvtpq_s32_v, arm_neon_vcvtps, 0),
4099  NEONMAP1(vcvtpq_s64_v, arm_neon_vcvtps, 0),
4100  NEONMAP1(vcvtpq_u16_v, arm_neon_vcvtpu, 0),
4101  NEONMAP1(vcvtpq_u32_v, arm_neon_vcvtpu, 0),
4102  NEONMAP1(vcvtpq_u64_v, arm_neon_vcvtpu, 0),
4103  NEONMAP0(vcvtq_f16_v),
4104  NEONMAP0(vcvtq_f32_v),
4105  NEONMAP2(vcvtq_n_f16_v, arm_neon_vcvtfxu2fp, arm_neon_vcvtfxs2fp, 0),
4106  NEONMAP2(vcvtq_n_f32_v, arm_neon_vcvtfxu2fp, arm_neon_vcvtfxs2fp, 0),
4107  NEONMAP1(vcvtq_n_s16_v, arm_neon_vcvtfp2fxs, 0),
4108  NEONMAP1(vcvtq_n_s32_v, arm_neon_vcvtfp2fxs, 0),
4109  NEONMAP1(vcvtq_n_s64_v, arm_neon_vcvtfp2fxs, 0),
4110  NEONMAP1(vcvtq_n_u16_v, arm_neon_vcvtfp2fxu, 0),
4111  NEONMAP1(vcvtq_n_u32_v, arm_neon_vcvtfp2fxu, 0),
4112  NEONMAP1(vcvtq_n_u64_v, arm_neon_vcvtfp2fxu, 0),
4113  NEONMAP0(vcvtq_s16_v),
4114  NEONMAP0(vcvtq_s32_v),
4115  NEONMAP0(vcvtq_s64_v),
4116  NEONMAP0(vcvtq_u16_v),
4117  NEONMAP0(vcvtq_u32_v),
4118  NEONMAP0(vcvtq_u64_v),
4119  NEONMAP2(vdot_v, arm_neon_udot, arm_neon_sdot, 0),
4120  NEONMAP2(vdotq_v, arm_neon_udot, arm_neon_sdot, 0),
4121  NEONMAP0(vext_v),
4122  NEONMAP0(vextq_v),
4123  NEONMAP0(vfma_v),
4124  NEONMAP0(vfmaq_v),
4125  NEONMAP2(vhadd_v, arm_neon_vhaddu, arm_neon_vhadds, Add1ArgType | UnsignedAlts),
4126  NEONMAP2(vhaddq_v, arm_neon_vhaddu, arm_neon_vhadds, Add1ArgType | UnsignedAlts),
4127  NEONMAP2(vhsub_v, arm_neon_vhsubu, arm_neon_vhsubs, Add1ArgType | UnsignedAlts),
4128  NEONMAP2(vhsubq_v, arm_neon_vhsubu, arm_neon_vhsubs, Add1ArgType | UnsignedAlts),
4129  NEONMAP0(vld1_dup_v),
4130  NEONMAP1(vld1_v, arm_neon_vld1, 0),
4131  NEONMAP1(vld1_x2_v, arm_neon_vld1x2, 0),
4132  NEONMAP1(vld1_x3_v, arm_neon_vld1x3, 0),
4133  NEONMAP1(vld1_x4_v, arm_neon_vld1x4, 0),
4134  NEONMAP0(vld1q_dup_v),
4135  NEONMAP1(vld1q_v, arm_neon_vld1, 0),
4136  NEONMAP1(vld1q_x2_v, arm_neon_vld1x2, 0),
4137  NEONMAP1(vld1q_x3_v, arm_neon_vld1x3, 0),
4138  NEONMAP1(vld1q_x4_v, arm_neon_vld1x4, 0),
4139  NEONMAP1(vld2_dup_v, arm_neon_vld2dup, 0),
4140  NEONMAP1(vld2_lane_v, arm_neon_vld2lane, 0),
4141  NEONMAP1(vld2_v, arm_neon_vld2, 0),
4142  NEONMAP1(vld2q_dup_v, arm_neon_vld2dup, 0),
4143  NEONMAP1(vld2q_lane_v, arm_neon_vld2lane, 0),
4144  NEONMAP1(vld2q_v, arm_neon_vld2, 0),
4145  NEONMAP1(vld3_dup_v, arm_neon_vld3dup, 0),
4146  NEONMAP1(vld3_lane_v, arm_neon_vld3lane, 0),
4147  NEONMAP1(vld3_v, arm_neon_vld3, 0),
4148  NEONMAP1(vld3q_dup_v, arm_neon_vld3dup, 0),
4149  NEONMAP1(vld3q_lane_v, arm_neon_vld3lane, 0),
4150  NEONMAP1(vld3q_v, arm_neon_vld3, 0),
4151  NEONMAP1(vld4_dup_v, arm_neon_vld4dup, 0),
4152  NEONMAP1(vld4_lane_v, arm_neon_vld4lane, 0),
4153  NEONMAP1(vld4_v, arm_neon_vld4, 0),
4154  NEONMAP1(vld4q_dup_v, arm_neon_vld4dup, 0),
4155  NEONMAP1(vld4q_lane_v, arm_neon_vld4lane, 0),
4156  NEONMAP1(vld4q_v, arm_neon_vld4, 0),
4157  NEONMAP2(vmax_v, arm_neon_vmaxu, arm_neon_vmaxs, Add1ArgType | UnsignedAlts),
4158  NEONMAP1(vmaxnm_v, arm_neon_vmaxnm, Add1ArgType),
4159  NEONMAP1(vmaxnmq_v, arm_neon_vmaxnm, Add1ArgType),
4160  NEONMAP2(vmaxq_v, arm_neon_vmaxu, arm_neon_vmaxs, Add1ArgType | UnsignedAlts),
4161  NEONMAP2(vmin_v, arm_neon_vminu, arm_neon_vmins, Add1ArgType | UnsignedAlts),
4162  NEONMAP1(vminnm_v, arm_neon_vminnm, Add1ArgType),
4163  NEONMAP1(vminnmq_v, arm_neon_vminnm, Add1ArgType),
4164  NEONMAP2(vminq_v, arm_neon_vminu, arm_neon_vmins, Add1ArgType | UnsignedAlts),
4165  NEONMAP0(vmovl_v),
4166  NEONMAP0(vmovn_v),
4167  NEONMAP1(vmul_v, arm_neon_vmulp, Add1ArgType),
4168  NEONMAP0(vmull_v),
4169  NEONMAP1(vmulq_v, arm_neon_vmulp, Add1ArgType),
4170  NEONMAP2(vpadal_v, arm_neon_vpadalu, arm_neon_vpadals, UnsignedAlts),
4171  NEONMAP2(vpadalq_v, arm_neon_vpadalu, arm_neon_vpadals, UnsignedAlts),
4172  NEONMAP1(vpadd_v, arm_neon_vpadd, Add1ArgType),
4173  NEONMAP2(vpaddl_v, arm_neon_vpaddlu, arm_neon_vpaddls, UnsignedAlts),
4174  NEONMAP2(vpaddlq_v, arm_neon_vpaddlu, arm_neon_vpaddls, UnsignedAlts),
4175  NEONMAP1(vpaddq_v, arm_neon_vpadd, Add1ArgType),
4176  NEONMAP2(vpmax_v, arm_neon_vpmaxu, arm_neon_vpmaxs, Add1ArgType | UnsignedAlts),
4177  NEONMAP2(vpmin_v, arm_neon_vpminu, arm_neon_vpmins, Add1ArgType | UnsignedAlts),
4178  NEONMAP1(vqabs_v, arm_neon_vqabs, Add1ArgType),
4179  NEONMAP1(vqabsq_v, arm_neon_vqabs, Add1ArgType),
4180  NEONMAP2(vqadd_v, arm_neon_vqaddu, arm_neon_vqadds, Add1ArgType | UnsignedAlts),
4181  NEONMAP2(vqaddq_v, arm_neon_vqaddu, arm_neon_vqadds, Add1ArgType | UnsignedAlts),
4182  NEONMAP2(vqdmlal_v, arm_neon_vqdmull, arm_neon_vqadds, 0),
4183  NEONMAP2(vqdmlsl_v, arm_neon_vqdmull, arm_neon_vqsubs, 0),
4184  NEONMAP1(vqdmulh_v, arm_neon_vqdmulh, Add1ArgType),
4185  NEONMAP1(vqdmulhq_v, arm_neon_vqdmulh, Add1ArgType),
4186  NEONMAP1(vqdmull_v, arm_neon_vqdmull, Add1ArgType),
4187  NEONMAP2(vqmovn_v, arm_neon_vqmovnu, arm_neon_vqmovns, Add1ArgType | UnsignedAlts),
4188  NEONMAP1(vqmovun_v, arm_neon_vqmovnsu, Add1ArgType),
4189  NEONMAP1(vqneg_v, arm_neon_vqneg, Add1ArgType),
4190  NEONMAP1(vqnegq_v, arm_neon_vqneg, Add1ArgType),
4191  NEONMAP1(vqrdmulh_v, arm_neon_vqrdmulh, Add1ArgType),
4192  NEONMAP1(vqrdmulhq_v, arm_neon_vqrdmulh, Add1ArgType),
4193  NEONMAP2(vqrshl_v, arm_neon_vqrshiftu, arm_neon_vqrshifts, Add1ArgType | UnsignedAlts),
4194  NEONMAP2(vqrshlq_v, arm_neon_vqrshiftu, arm_neon_vqrshifts, Add1ArgType | UnsignedAlts),
4195  NEONMAP2(vqshl_n_v, arm_neon_vqshiftu, arm_neon_vqshifts, UnsignedAlts),
4196  NEONMAP2(vqshl_v, arm_neon_vqshiftu, arm_neon_vqshifts, Add1ArgType | UnsignedAlts),
4197  NEONMAP2(vqshlq_n_v, arm_neon_vqshiftu, arm_neon_vqshifts, UnsignedAlts),
4198  NEONMAP2(vqshlq_v, arm_neon_vqshiftu, arm_neon_vqshifts, Add1ArgType | UnsignedAlts),
4199  NEONMAP1(vqshlu_n_v, arm_neon_vqshiftsu, 0),
4200  NEONMAP1(vqshluq_n_v, arm_neon_vqshiftsu, 0),
4201  NEONMAP2(vqsub_v, arm_neon_vqsubu, arm_neon_vqsubs, Add1ArgType | UnsignedAlts),
4202  NEONMAP2(vqsubq_v, arm_neon_vqsubu, arm_neon_vqsubs, Add1ArgType | UnsignedAlts),
4203  NEONMAP1(vraddhn_v, arm_neon_vraddhn, Add1ArgType),
4204  NEONMAP2(vrecpe_v, arm_neon_vrecpe, arm_neon_vrecpe, 0),
4205  NEONMAP2(vrecpeq_v, arm_neon_vrecpe, arm_neon_vrecpe, 0),
4206  NEONMAP1(vrecps_v, arm_neon_vrecps, Add1ArgType),
4207  NEONMAP1(vrecpsq_v, arm_neon_vrecps, Add1ArgType),
4208  NEONMAP2(vrhadd_v, arm_neon_vrhaddu, arm_neon_vrhadds, Add1ArgType | UnsignedAlts),
4209  NEONMAP2(vrhaddq_v, arm_neon_vrhaddu, arm_neon_vrhadds, Add1ArgType | UnsignedAlts),
4210  NEONMAP1(vrnd_v, arm_neon_vrintz, Add1ArgType),
4211  NEONMAP1(vrnda_v, arm_neon_vrinta, Add1ArgType),
4212  NEONMAP1(vrndaq_v, arm_neon_vrinta, Add1ArgType),
4213  NEONMAP0(vrndi_v),
4214  NEONMAP0(vrndiq_v),
4215  NEONMAP1(vrndm_v, arm_neon_vrintm, Add1ArgType),
4216  NEONMAP1(vrndmq_v, arm_neon_vrintm, Add1ArgType),
4217  NEONMAP1(vrndn_v, arm_neon_vrintn, Add1ArgType),
4218  NEONMAP1(vrndnq_v, arm_neon_vrintn, Add1ArgType),
4219  NEONMAP1(vrndp_v, arm_neon_vrintp, Add1ArgType),
4220  NEONMAP1(vrndpq_v, arm_neon_vrintp, Add1ArgType),
4221  NEONMAP1(vrndq_v, arm_neon_vrintz, Add1ArgType),
4222  NEONMAP1(vrndx_v, arm_neon_vrintx, Add1ArgType),
4223  NEONMAP1(vrndxq_v, arm_neon_vrintx, Add1ArgType),
4224  NEONMAP2(vrshl_v, arm_neon_vrshiftu, arm_neon_vrshifts, Add1ArgType | UnsignedAlts),
4225  NEONMAP2(vrshlq_v, arm_neon_vrshiftu, arm_neon_vrshifts, Add1ArgType | UnsignedAlts),
4226  NEONMAP2(vrshr_n_v, arm_neon_vrshiftu, arm_neon_vrshifts, UnsignedAlts),
4227  NEONMAP2(vrshrq_n_v, arm_neon_vrshiftu, arm_neon_vrshifts, UnsignedAlts),
4228  NEONMAP2(vrsqrte_v, arm_neon_vrsqrte, arm_neon_vrsqrte, 0),
4229  NEONMAP2(vrsqrteq_v, arm_neon_vrsqrte, arm_neon_vrsqrte, 0),
4230  NEONMAP1(vrsqrts_v, arm_neon_vrsqrts, Add1ArgType),
4231  NEONMAP1(vrsqrtsq_v, arm_neon_vrsqrts, Add1ArgType),
4232  NEONMAP1(vrsubhn_v, arm_neon_vrsubhn, Add1ArgType),
4233  NEONMAP1(vsha1su0q_v, arm_neon_sha1su0, 0),
4234  NEONMAP1(vsha1su1q_v, arm_neon_sha1su1, 0),
4235  NEONMAP1(vsha256h2q_v, arm_neon_sha256h2, 0),
4236  NEONMAP1(vsha256hq_v, arm_neon_sha256h, 0),
4237  NEONMAP1(vsha256su0q_v, arm_neon_sha256su0, 0),
4238  NEONMAP1(vsha256su1q_v, arm_neon_sha256su1, 0),
4239  NEONMAP0(vshl_n_v),
4240  NEONMAP2(vshl_v, arm_neon_vshiftu, arm_neon_vshifts, Add1ArgType | UnsignedAlts),
4241  NEONMAP0(vshll_n_v),
4242  NEONMAP0(vshlq_n_v),
4243  NEONMAP2(vshlq_v, arm_neon_vshiftu, arm_neon_vshifts, Add1ArgType | UnsignedAlts),
4244  NEONMAP0(vshr_n_v),
4245  NEONMAP0(vshrn_n_v),
4246  NEONMAP0(vshrq_n_v),
4247  NEONMAP1(vst1_v, arm_neon_vst1, 0),
4248  NEONMAP1(vst1_x2_v, arm_neon_vst1x2, 0),
4249  NEONMAP1(vst1_x3_v, arm_neon_vst1x3, 0),
4250  NEONMAP1(vst1_x4_v, arm_neon_vst1x4, 0),
4251  NEONMAP1(vst1q_v, arm_neon_vst1, 0),
4252  NEONMAP1(vst1q_x2_v, arm_neon_vst1x2, 0),
4253  NEONMAP1(vst1q_x3_v, arm_neon_vst1x3, 0),
4254  NEONMAP1(vst1q_x4_v, arm_neon_vst1x4, 0),
4255  NEONMAP1(vst2_lane_v, arm_neon_vst2lane, 0),
4256  NEONMAP1(vst2_v, arm_neon_vst2, 0),
4257  NEONMAP1(vst2q_lane_v, arm_neon_vst2lane, 0),
4258  NEONMAP1(vst2q_v, arm_neon_vst2, 0),
4259  NEONMAP1(vst3_lane_v, arm_neon_vst3lane, 0),
4260  NEONMAP1(vst3_v, arm_neon_vst3, 0),
4261  NEONMAP1(vst3q_lane_v, arm_neon_vst3lane, 0),
4262  NEONMAP1(vst3q_v, arm_neon_vst3, 0),
4263  NEONMAP1(vst4_lane_v, arm_neon_vst4lane, 0),
4264  NEONMAP1(vst4_v, arm_neon_vst4, 0),
4265  NEONMAP1(vst4q_lane_v, arm_neon_vst4lane, 0),
4266  NEONMAP1(vst4q_v, arm_neon_vst4, 0),
4267  NEONMAP0(vsubhn_v),
4268  NEONMAP0(vtrn_v),
4269  NEONMAP0(vtrnq_v),
4270  NEONMAP0(vtst_v),
4271  NEONMAP0(vtstq_v),
4272  NEONMAP0(vuzp_v),
4273  NEONMAP0(vuzpq_v),
4274  NEONMAP0(vzip_v),
4275  NEONMAP0(vzipq_v)
4276 };
4277 
4278 static const NeonIntrinsicInfo AArch64SIMDIntrinsicMap[] = {
4279  NEONMAP1(vabs_v, aarch64_neon_abs, 0),
4280  NEONMAP1(vabsq_v, aarch64_neon_abs, 0),
4281  NEONMAP0(vaddhn_v),
4282  NEONMAP1(vaesdq_v, aarch64_crypto_aesd, 0),
4283  NEONMAP1(vaeseq_v, aarch64_crypto_aese, 0),
4284  NEONMAP1(vaesimcq_v, aarch64_crypto_aesimc, 0),
4285  NEONMAP1(vaesmcq_v, aarch64_crypto_aesmc, 0),
4286  NEONMAP1(vcage_v, aarch64_neon_facge, 0),
4287  NEONMAP1(vcageq_v, aarch64_neon_facge, 0),
4288  NEONMAP1(vcagt_v, aarch64_neon_facgt, 0),
4289  NEONMAP1(vcagtq_v, aarch64_neon_facgt, 0),
4290  NEONMAP1(vcale_v, aarch64_neon_facge, 0),
4291  NEONMAP1(vcaleq_v, aarch64_neon_facge, 0),
4292  NEONMAP1(vcalt_v, aarch64_neon_facgt, 0),
4293  NEONMAP1(vcaltq_v, aarch64_neon_facgt, 0),
4294  NEONMAP0(vceqz_v),
4295  NEONMAP0(vceqzq_v),
4296  NEONMAP0(vcgez_v),
4297  NEONMAP0(vcgezq_v),
4298  NEONMAP0(vcgtz_v),
4299  NEONMAP0(vcgtzq_v),
4300  NEONMAP0(vclez_v),
4301  NEONMAP0(vclezq_v),
4302  NEONMAP1(vcls_v, aarch64_neon_cls, Add1ArgType),
4303  NEONMAP1(vclsq_v, aarch64_neon_cls, Add1ArgType),
4304  NEONMAP0(vcltz_v),
4305  NEONMAP0(vcltzq_v),
4306  NEONMAP1(vclz_v, ctlz, Add1ArgType),
4307  NEONMAP1(vclzq_v, ctlz, Add1ArgType),
4308  NEONMAP1(vcnt_v, ctpop, Add1ArgType),
4309  NEONMAP1(vcntq_v, ctpop, Add1ArgType),
4310  NEONMAP1(vcvt_f16_f32, aarch64_neon_vcvtfp2hf, 0),
4311  NEONMAP0(vcvt_f16_v),
4312  NEONMAP1(vcvt_f32_f16, aarch64_neon_vcvthf2fp, 0),
4313  NEONMAP0(vcvt_f32_v),
4314  NEONMAP2(vcvt_n_f16_v, aarch64_neon_vcvtfxu2fp, aarch64_neon_vcvtfxs2fp, 0),
4315  NEONMAP2(vcvt_n_f32_v, aarch64_neon_vcvtfxu2fp, aarch64_neon_vcvtfxs2fp, 0),
4316  NEONMAP2(vcvt_n_f64_v, aarch64_neon_vcvtfxu2fp, aarch64_neon_vcvtfxs2fp, 0),
4317  NEONMAP1(vcvt_n_s16_v, aarch64_neon_vcvtfp2fxs, 0),
4318  NEONMAP1(vcvt_n_s32_v, aarch64_neon_vcvtfp2fxs, 0),
4319  NEONMAP1(vcvt_n_s64_v, aarch64_neon_vcvtfp2fxs, 0),
4320  NEONMAP1(vcvt_n_u16_v, aarch64_neon_vcvtfp2fxu, 0),
4321  NEONMAP1(vcvt_n_u32_v, aarch64_neon_vcvtfp2fxu, 0),
4322  NEONMAP1(vcvt_n_u64_v, aarch64_neon_vcvtfp2fxu, 0),
4323  NEONMAP0(vcvtq_f16_v),
4324  NEONMAP0(vcvtq_f32_v),
4325  NEONMAP2(vcvtq_n_f16_v, aarch64_neon_vcvtfxu2fp, aarch64_neon_vcvtfxs2fp, 0),
4326  NEONMAP2(vcvtq_n_f32_v, aarch64_neon_vcvtfxu2fp, aarch64_neon_vcvtfxs2fp, 0),
4327  NEONMAP2(vcvtq_n_f64_v, aarch64_neon_vcvtfxu2fp, aarch64_neon_vcvtfxs2fp, 0),
4328  NEONMAP1(vcvtq_n_s16_v, aarch64_neon_vcvtfp2fxs, 0),
4329  NEONMAP1(vcvtq_n_s32_v, aarch64_neon_vcvtfp2fxs, 0),
4330  NEONMAP1(vcvtq_n_s64_v, aarch64_neon_vcvtfp2fxs, 0),
4331  NEONMAP1(vcvtq_n_u16_v, aarch64_neon_vcvtfp2fxu, 0),
4332  NEONMAP1(vcvtq_n_u32_v, aarch64_neon_vcvtfp2fxu, 0),
4333  NEONMAP1(vcvtq_n_u64_v, aarch64_neon_vcvtfp2fxu, 0),
4334  NEONMAP1(vcvtx_f32_v, aarch64_neon_fcvtxn, AddRetType | Add1ArgType),
4335  NEONMAP2(vdot_v, aarch64_neon_udot, aarch64_neon_sdot, 0),
4336  NEONMAP2(vdotq_v, aarch64_neon_udot, aarch64_neon_sdot, 0),
4337  NEONMAP0(vext_v),
4338  NEONMAP0(vextq_v),
4339  NEONMAP0(vfma_v),
4340  NEONMAP0(vfmaq_v),
4341  NEONMAP2(vhadd_v, aarch64_neon_uhadd, aarch64_neon_shadd, Add1ArgType | UnsignedAlts),
4342  NEONMAP2(vhaddq_v, aarch64_neon_uhadd, aarch64_neon_shadd, Add1ArgType | UnsignedAlts),
4343  NEONMAP2(vhsub_v, aarch64_neon_uhsub, aarch64_neon_shsub, Add1ArgType | UnsignedAlts),
4344  NEONMAP2(vhsubq_v, aarch64_neon_uhsub, aarch64_neon_shsub, Add1ArgType | UnsignedAlts),
4345  NEONMAP1(vld1_x2_v, aarch64_neon_ld1x2, 0),
4346  NEONMAP1(vld1_x3_v, aarch64_neon_ld1x3, 0),
4347  NEONMAP1(vld1_x4_v, aarch64_neon_ld1x4, 0),
4348  NEONMAP1(vld1q_x2_v, aarch64_neon_ld1x2, 0),
4349  NEONMAP1(vld1q_x3_v, aarch64_neon_ld1x3, 0),
4350  NEONMAP1(vld1q_x4_v, aarch64_neon_ld1x4, 0),
4351  NEONMAP0(vmovl_v),
4352  NEONMAP0(vmovn_v),
4353  NEONMAP1(vmul_v, aarch64_neon_pmul, Add1ArgType),
4354  NEONMAP1(vmulq_v, aarch64_neon_pmul, Add1ArgType),
4355  NEONMAP1(vpadd_v, aarch64_neon_addp, Add1ArgType),
4356  NEONMAP2(vpaddl_v, aarch64_neon_uaddlp, aarch64_neon_saddlp, UnsignedAlts),
4357  NEONMAP2(vpaddlq_v, aarch64_neon_uaddlp, aarch64_neon_saddlp, UnsignedAlts),
4358  NEONMAP1(vpaddq_v, aarch64_neon_addp, Add1ArgType),
4359  NEONMAP1(vqabs_v, aarch64_neon_sqabs, Add1ArgType),
4360  NEONMAP1(vqabsq_v, aarch64_neon_sqabs, Add1ArgType),
4361  NEONMAP2(vqadd_v, aarch64_neon_uqadd, aarch64_neon_sqadd, Add1ArgType | UnsignedAlts),
4362  NEONMAP2(vqaddq_v, aarch64_neon_uqadd, aarch64_neon_sqadd, Add1ArgType | UnsignedAlts),
4363  NEONMAP2(vqdmlal_v, aarch64_neon_sqdmull, aarch64_neon_sqadd, 0),
4364  NEONMAP2(vqdmlsl_v, aarch64_neon_sqdmull, aarch64_neon_sqsub, 0),
4365  NEONMAP1(vqdmulh_v, aarch64_neon_sqdmulh, Add1ArgType),
4366  NEONMAP1(vqdmulhq_v, aarch64_neon_sqdmulh, Add1ArgType),
4367  NEONMAP1(vqdmull_v, aarch64_neon_sqdmull, Add1ArgType),
4368  NEONMAP2(vqmovn_v, aarch64_neon_uqxtn, aarch64_neon_sqxtn, Add1ArgType | UnsignedAlts),
4369  NEONMAP1(vqmovun_v, aarch64_neon_sqxtun, Add1ArgType),
4370  NEONMAP1(vqneg_v, aarch64_neon_sqneg, Add1ArgType),
4371  NEONMAP1(vqnegq_v, aarch64_neon_sqneg, Add1ArgType),
4372  NEONMAP1(vqrdmulh_v, aarch64_neon_sqrdmulh, Add1ArgType),
4373  NEONMAP1(vqrdmulhq_v, aarch64_neon_sqrdmulh, Add1ArgType),
4374  NEONMAP2(vqrshl_v, aarch64_neon_uqrshl, aarch64_neon_sqrshl, Add1ArgType | UnsignedAlts),
4375  NEONMAP2(vqrshlq_v, aarch64_neon_uqrshl, aarch64_neon_sqrshl, Add1ArgType | UnsignedAlts),
4376  NEONMAP2(vqshl_n_v, aarch64_neon_uqshl, aarch64_neon_sqshl, UnsignedAlts),
4377  NEONMAP2(vqshl_v, aarch64_neon_uqshl, aarch64_neon_sqshl, Add1ArgType | UnsignedAlts),
4378  NEONMAP2(vqshlq_n_v, aarch64_neon_uqshl, aarch64_neon_sqshl,UnsignedAlts),
4379  NEONMAP2(vqshlq_v, aarch64_neon_uqshl, aarch64_neon_sqshl, Add1ArgType | UnsignedAlts),
4380  NEONMAP1(vqshlu_n_v, aarch64_neon_sqshlu, 0),
4381  NEONMAP1(vqshluq_n_v, aarch64_neon_sqshlu, 0),
4382  NEONMAP2(vqsub_v, aarch64_neon_uqsub, aarch64_neon_sqsub, Add1ArgType | UnsignedAlts),
4383  NEONMAP2(vqsubq_v, aarch64_neon_uqsub, aarch64_neon_sqsub, Add1ArgType | UnsignedAlts),
4384  NEONMAP1(vraddhn_v, aarch64_neon_raddhn, Add1ArgType),
4385  NEONMAP2(vrecpe_v, aarch64_neon_frecpe, aarch64_neon_urecpe, 0),
4386  NEONMAP2(vrecpeq_v, aarch64_neon_frecpe, aarch64_neon_urecpe, 0),
4387  NEONMAP1(vrecps_v, aarch64_neon_frecps, Add1ArgType),
4388  NEONMAP1(vrecpsq_v, aarch64_neon_frecps, Add1ArgType),
4389  NEONMAP2(vrhadd_v, aarch64_neon_urhadd, aarch64_neon_srhadd, Add1ArgType | UnsignedAlts),
4390  NEONMAP2(vrhaddq_v, aarch64_neon_urhadd, aarch64_neon_srhadd, Add1ArgType | UnsignedAlts),
4391  NEONMAP0(vrndi_v),
4392  NEONMAP0(vrndiq_v),
4393  NEONMAP2(vrshl_v, aarch64_neon_urshl, aarch64_neon_srshl, Add1ArgType | UnsignedAlts),
4394  NEONMAP2(vrshlq_v, aarch64_neon_urshl, aarch64_neon_srshl, Add1ArgType | UnsignedAlts),
4395  NEONMAP2(vrshr_n_v, aarch64_neon_urshl, aarch64_neon_srshl, UnsignedAlts),
4396  NEONMAP2(vrshrq_n_v, aarch64_neon_urshl, aarch64_neon_srshl, UnsignedAlts),
4397  NEONMAP2(vrsqrte_v, aarch64_neon_frsqrte, aarch64_neon_ursqrte, 0),
4398  NEONMAP2(vrsqrteq_v, aarch64_neon_frsqrte, aarch64_neon_ursqrte, 0),
4399  NEONMAP1(vrsqrts_v, aarch64_neon_frsqrts, Add1ArgType),
4400  NEONMAP1(vrsqrtsq_v, aarch64_neon_frsqrts, Add1ArgType),
4401  NEONMAP1(vrsubhn_v, aarch64_neon_rsubhn, Add1ArgType),
4402  NEONMAP1(vsha1su0q_v, aarch64_crypto_sha1su0, 0),
4403  NEONMAP1(vsha1su1q_v, aarch64_crypto_sha1su1, 0),
4404  NEONMAP1(vsha256h2q_v, aarch64_crypto_sha256h2, 0),
4405  NEONMAP1(vsha256hq_v, aarch64_crypto_sha256h, 0),
4406  NEONMAP1(vsha256su0q_v, aarch64_crypto_sha256su0, 0),
4407  NEONMAP1(vsha256su1q_v, aarch64_crypto_sha256su1, 0),
4408  NEONMAP0(vshl_n_v),
4409  NEONMAP2(vshl_v, aarch64_neon_ushl, aarch64_neon_sshl, Add1ArgType | UnsignedAlts),
4410  NEONMAP0(vshll_n_v),
4411  NEONMAP0(vshlq_n_v),
4412  NEONMAP2(vshlq_v, aarch64_neon_ushl, aarch64_neon_sshl, Add1ArgType | UnsignedAlts),
4413  NEONMAP0(vshr_n_v),
4414  NEONMAP0(vshrn_n_v),
4415  NEONMAP0(vshrq_n_v),
4416  NEONMAP1(vst1_x2_v, aarch64_neon_st1x2, 0),
4417  NEONMAP1(vst1_x3_v, aarch64_neon_st1x3, 0),
4418  NEONMAP1(vst1_x4_v, aarch64_neon_st1x4, 0),
4419  NEONMAP1(vst1q_x2_v, aarch64_neon_st1x2, 0),
4420  NEONMAP1(vst1q_x3_v, aarch64_neon_st1x3, 0),
4421  NEONMAP1(vst1q_x4_v, aarch64_neon_st1x4, 0),
4422  NEONMAP0(vsubhn_v),
4423  NEONMAP0(vtst_v),
4424  NEONMAP0(vtstq_v),
4425 };
4426 
4427 static const NeonIntrinsicInfo AArch64SISDIntrinsicMap[] = {
4428  NEONMAP1(vabdd_f64, aarch64_sisd_fabd, Add1ArgType),
4429  NEONMAP1(vabds_f32, aarch64_sisd_fabd, Add1ArgType),
4430  NEONMAP1(vabsd_s64, aarch64_neon_abs, Add1ArgType),
4431  NEONMAP1(vaddlv_s32, aarch64_neon_saddlv, AddRetType | Add1ArgType),
4432  NEONMAP1(vaddlv_u32, aarch64_neon_uaddlv, AddRetType | Add1ArgType),
4433  NEONMAP1(vaddlvq_s32, aarch64_neon_saddlv, AddRetType | Add1ArgType),
4434  NEONMAP1(vaddlvq_u32, aarch64_neon_uaddlv, AddRetType | Add1ArgType),
4435  NEONMAP1(vaddv_f32, aarch64_neon_faddv, AddRetType | Add1ArgType),
4436  NEONMAP1(vaddv_s32, aarch64_neon_saddv, AddRetType | Add1ArgType),
4437  NEONMAP1(vaddv_u32, aarch64_neon_uaddv, AddRetType | Add1ArgType),
4438  NEONMAP1(vaddvq_f32, aarch64_neon_faddv, AddRetType | Add1ArgType),
4439  NEONMAP1(vaddvq_f64, aarch64_neon_faddv, AddRetType | Add1ArgType),
4440  NEONMAP1(vaddvq_s32, aarch64_neon_saddv, AddRetType | Add1ArgType),
4441  NEONMAP1(vaddvq_s64, aarch64_neon_saddv, AddRetType | Add1ArgType),
4442  NEONMAP1(vaddvq_u32, aarch64_neon_uaddv, AddRetType | Add1ArgType),
4443  NEONMAP1(vaddvq_u64, aarch64_neon_uaddv, AddRetType | Add1ArgType),
4444  NEONMAP1(vcaged_f64, aarch64_neon_facge, AddRetType | Add1ArgType),
4445  NEONMAP1(vcages_f32, aarch64_neon_facge, AddRetType | Add1ArgType),
4446  NEONMAP1(vcagtd_f64, aarch64_neon_facgt, AddRetType | Add1ArgType),
4447  NEONMAP1(vcagts_f32, aarch64_neon_facgt, AddRetType | Add1ArgType),
4448  NEONMAP1(vcaled_f64, aarch64_neon_facge, AddRetType | Add1ArgType),
4449  NEONMAP1(vcales_f32, aarch64_neon_facge, AddRetType | Add1ArgType),
4450  NEONMAP1(vcaltd_f64, aarch64_neon_facgt, AddRetType | Add1ArgType),
4451  NEONMAP1(vcalts_f32, aarch64_neon_facgt, AddRetType | Add1ArgType),
4452  NEONMAP1(vcvtad_s64_f64, aarch64_neon_fcvtas, AddRetType | Add1ArgType),
4453  NEONMAP1(vcvtad_u64_f64, aarch64_neon_fcvtau, AddRetType | Add1ArgType),
4454  NEONMAP1(vcvtas_s32_f32, aarch64_neon_fcvtas, AddRetType | Add1ArgType),
4455  NEONMAP1(vcvtas_u32_f32, aarch64_neon_fcvtau, AddRetType | Add1ArgType),
4456  NEONMAP1(vcvtd_n_f64_s64, aarch64_neon_vcvtfxs2fp, AddRetType | Add1ArgType),
4457  NEONMAP1(vcvtd_n_f64_u64, aarch64_neon_vcvtfxu2fp, AddRetType | Add1ArgType),
4458  NEONMAP1(vcvtd_n_s64_f64, aarch64_neon_vcvtfp2fxs, AddRetType | Add1ArgType),
4459  NEONMAP1(vcvtd_n_u64_f64, aarch64_neon_vcvtfp2fxu, AddRetType | Add1ArgType),
4460  NEONMAP1(vcvtmd_s64_f64, aarch64_neon_fcvtms, AddRetType | Add1ArgType),
4461  NEONMAP1(vcvtmd_u64_f64, aarch64_neon_fcvtmu, AddRetType | Add1ArgType),
4462  NEONMAP1(vcvtms_s32_f32, aarch64_neon_fcvtms, AddRetType | Add1ArgType),
4463  NEONMAP1(vcvtms_u32_f32, aarch64_neon_fcvtmu, AddRetType | Add1ArgType),
4464  NEONMAP1(vcvtnd_s64_f64, aarch64_neon_fcvtns, AddRetType | Add1ArgType),
4465  NEONMAP1(vcvtnd_u64_f64, aarch64_neon_fcvtnu, AddRetType | Add1ArgType),
4466  NEONMAP1(vcvtns_s32_f32, aarch64_neon_fcvtns, AddRetType | Add1ArgType),
4467  NEONMAP1(vcvtns_u32_f32, aarch64_neon_fcvtnu, AddRetType | Add1ArgType),
4468  NEONMAP1(vcvtpd_s64_f64, aarch64_neon_fcvtps, AddRetType | Add1ArgType),
4469  NEONMAP1(vcvtpd_u64_f64, aarch64_neon_fcvtpu, AddRetType | Add1ArgType),
4470  NEONMAP1(vcvtps_s32_f32, aarch64_neon_fcvtps, AddRetType | Add1ArgType),
4471  NEONMAP1(vcvtps_u32_f32, aarch64_neon_fcvtpu, AddRetType | Add1ArgType),
4472  NEONMAP1(vcvts_n_f32_s32, aarch64_neon_vcvtfxs2fp, AddRetType | Add1ArgType),
4473  NEONMAP1(vcvts_n_f32_u32, aarch64_neon_vcvtfxu2fp, AddRetType | Add1ArgType),
4474  NEONMAP1(vcvts_n_s32_f32, aarch64_neon_vcvtfp2fxs, AddRetType | Add1ArgType),
4475  NEONMAP1(vcvts_n_u32_f32, aarch64_neon_vcvtfp2fxu, AddRetType | Add1ArgType),
4476  NEONMAP1(vcvtxd_f32_f64, aarch64_sisd_fcvtxn, 0),
4477  NEONMAP1(vmaxnmv_f32, aarch64_neon_fmaxnmv, AddRetType | Add1ArgType),
4478  NEONMAP1(vmaxnmvq_f32, aarch64_neon_fmaxnmv, AddRetType | Add1ArgType),
4479  NEONMAP1(vmaxnmvq_f64, aarch64_neon_fmaxnmv, AddRetType | Add1ArgType),
4480  NEONMAP1(vmaxv_f32, aarch64_neon_fmaxv, AddRetType | Add1ArgType),
4481  NEONMAP1(vmaxv_s32, aarch64_neon_smaxv, AddRetType | Add1ArgType),
4482  NEONMAP1(vmaxv_u32, aarch64_neon_umaxv, AddRetType | Add1ArgType),
4483  NEONMAP1(vmaxvq_f32, aarch64_neon_fmaxv, AddRetType | Add1ArgType),
4484  NEONMAP1(vmaxvq_f64, aarch64_neon_fmaxv, AddRetType | Add1ArgType),
4485  NEONMAP1(vmaxvq_s32, aarch64_neon_smaxv, AddRetType | Add1ArgType),
4486  NEONMAP1(vmaxvq_u32, aarch64_neon_umaxv, AddRetType | Add1ArgType),
4487  NEONMAP1(vminnmv_f32, aarch64_neon_fminnmv, AddRetType | Add1ArgType),
4488  NEONMAP1(vminnmvq_f32, aarch64_neon_fminnmv, AddRetType | Add1ArgType),
4489  NEONMAP1(vminnmvq_f64, aarch64_neon_fminnmv, AddRetType | Add1ArgType),
4490  NEONMAP1(vminv_f32, aarch64_neon_fminv, AddRetType | Add1ArgType),
4491  NEONMAP1(vminv_s32, aarch64_neon_sminv, AddRetType | Add1ArgType),
4492  NEONMAP1(vminv_u32, aarch64_neon_uminv, AddRetType | Add1ArgType),
4493  NEONMAP1(vminvq_f32, aarch64_neon_fminv, AddRetType | Add1ArgType),
4494  NEONMAP1(vminvq_f64, aarch64_neon_fminv, AddRetType | Add1ArgType),
4495  NEONMAP1(vminvq_s32, aarch64_neon_sminv, AddRetType | Add1ArgType),
4496  NEONMAP1(vminvq_u32, aarch64_neon_uminv, AddRetType | Add1ArgType),
4497  NEONMAP1(vmull_p64, aarch64_neon_pmull64, 0),
4498  NEONMAP1(vmulxd_f64, aarch64_neon_fmulx, Add1ArgType),
4499  NEONMAP1(vmulxs_f32, aarch64_neon_fmulx, Add1ArgType),
4500  NEONMAP1(vpaddd_s64, aarch64_neon_uaddv, AddRetType | Add1ArgType),
4501  NEONMAP1(vpaddd_u64, aarch64_neon_uaddv, AddRetType | Add1ArgType),
4502  NEONMAP1(vpmaxnmqd_f64, aarch64_neon_fmaxnmv, AddRetType | Add1ArgType),
4503  NEONMAP1(vpmaxnms_f32, aarch64_neon_fmaxnmv, AddRetType | Add1ArgType),
4504  NEONMAP1(vpmaxqd_f64, aarch64_neon_fmaxv, AddRetType | Add1ArgType),
4505  NEONMAP1(vpmaxs_f32, aarch64_neon_fmaxv, AddRetType | Add1ArgType),
4506  NEONMAP1(vpminnmqd_f64, aarch64_neon_fminnmv, AddRetType | Add1ArgType),
4507  NEONMAP1(vpminnms_f32, aarch64_neon_fminnmv, AddRetType | Add1ArgType),
4508  NEONMAP1(vpminqd_f64, aarch64_neon_fminv, AddRetType | Add1ArgType),
4509  NEONMAP1(vpmins_f32, aarch64_neon_fminv, AddRetType | Add1ArgType),
4510  NEONMAP1(vqabsb_s8, aarch64_neon_sqabs, Vectorize1ArgType | Use64BitVectors),
4511  NEONMAP1(vqabsd_s64, aarch64_neon_sqabs, Add1ArgType),
4512  NEONMAP1(vqabsh_s16, aarch64_neon_sqabs, Vectorize1ArgType | Use64BitVectors),
4513  NEONMAP1(vqabss_s32, aarch64_neon_sqabs, Add1ArgType),
4514  NEONMAP1(vqaddb_s8, aarch64_neon_sqadd, Vectorize1ArgType | Use64BitVectors),
4515  NEONMAP1(vqaddb_u8, aarch64_neon_uqadd, Vectorize1ArgType | Use64BitVectors),
4516  NEONMAP1(vqaddd_s64, aarch64_neon_sqadd, Add1ArgType),
4517  NEONMAP1(vqaddd_u64, aarch64_neon_uqadd, Add1ArgType),
4518  NEONMAP1(vqaddh_s16, aarch64_neon_sqadd, Vectorize1ArgType | Use64BitVectors),
4519  NEONMAP1(vqaddh_u16, aarch64_neon_uqadd, Vectorize1ArgType | Use64BitVectors),
4520  NEONMAP1(vqadds_s32, aarch64_neon_sqadd, Add1ArgType),
4521  NEONMAP1(vqadds_u32, aarch64_neon_uqadd, Add1ArgType),
4522  NEONMAP1(vqdmulhh_s16, aarch64_neon_sqdmulh, Vectorize1ArgType | Use64BitVectors),
4523  NEONMAP1(vqdmulhs_s32, aarch64_neon_sqdmulh, Add1ArgType),
4524  NEONMAP1(vqdmullh_s16, aarch64_neon_sqdmull, VectorRet | Use128BitVectors),
4525  NEONMAP1(vqdmulls_s32, aarch64_neon_sqdmulls_scalar, 0),
4526  NEONMAP1(vqmovnd_s64, aarch64_neon_scalar_sqxtn, AddRetType | Add1ArgType),
4527  NEONMAP1(vqmovnd_u64, aarch64_neon_scalar_uqxtn, AddRetType | Add1ArgType),
4528  NEONMAP1(vqmovnh_s16, aarch64_neon_sqxtn, VectorRet | Use64BitVectors),
4529  NEONMAP1(vqmovnh_u16, aarch64_neon_uqxtn, VectorRet | Use64BitVectors),
4530  NEONMAP1(vqmovns_s32, aarch64_neon_sqxtn, VectorRet | Use64BitVectors),
4531  NEONMAP1(vqmovns_u32, aarch64_neon_uqxtn, VectorRet | Use64BitVectors),
4532  NEONMAP1(vqmovund_s64, aarch64_neon_scalar_sqxtun, AddRetType | Add1ArgType),
4533  NEONMAP1(vqmovunh_s16, aarch64_neon_sqxtun, VectorRet | Use64BitVectors),
4534  NEONMAP1(vqmovuns_s32, aarch64_neon_sqxtun, VectorRet | Use64BitVectors),
4535  NEONMAP1(vqnegb_s8, aarch64_neon_sqneg, Vectorize1ArgType | Use64BitVectors),
4536  NEONMAP1(vqnegd_s64, aarch64_neon_sqneg, Add1ArgType),
4537  NEONMAP1(vqnegh_s16, aarch64_neon_sqneg, Vectorize1ArgType | Use64BitVectors),
4538  NEONMAP1(vqnegs_s32, aarch64_neon_sqneg, Add1ArgType),
4539  NEONMAP1(vqrdmulhh_s16, aarch64_neon_sqrdmulh, Vectorize1ArgType | Use64BitVectors),
4540  NEONMAP1(vqrdmulhs_s32, aarch64_neon_sqrdmulh, Add1ArgType),
4541  NEONMAP1(vqrshlb_s8, aarch64_neon_sqrshl, Vectorize1ArgType | Use64BitVectors),
4542  NEONMAP1(vqrshlb_u8, aarch64_neon_uqrshl, Vectorize1ArgType | Use64BitVectors),
4543  NEONMAP1(vqrshld_s64, aarch64_neon_sqrshl, Add1ArgType),
4544  NEONMAP1(vqrshld_u64, aarch64_neon_uqrshl, Add1ArgType),
4545  NEONMAP1(vqrshlh_s16, aarch64_neon_sqrshl, Vectorize1ArgType | Use64BitVectors),
4546  NEONMAP1(vqrshlh_u16, aarch64_neon_uqrshl, Vectorize1ArgType | Use64BitVectors),
4547  NEONMAP1(vqrshls_s32, aarch64_neon_sqrshl, Add1ArgType),
4548  NEONMAP1(vqrshls_u32, aarch64_neon_uqrshl, Add1ArgType),
4549  NEONMAP1(vqrshrnd_n_s64, aarch64_neon_sqrshrn, AddRetType),
4550  NEONMAP1(vqrshrnd_n_u64, aarch64_neon_uqrshrn, AddRetType),
4551  NEONMAP1(vqrshrnh_n_s16, aarch64_neon_sqrshrn, VectorRet | Use64BitVectors),
4552  NEONMAP1(vqrshrnh_n_u16, aarch64_neon_uqrshrn, VectorRet | Use64BitVectors),
4553  NEONMAP1(vqrshrns_n_s32, aarch64_neon_sqrshrn, VectorRet | Use64BitVectors),
4554  NEONMAP1(vqrshrns_n_u32, aarch64_neon_uqrshrn, VectorRet | Use64BitVectors),
4555  NEONMAP1(vqrshrund_n_s64, aarch64_neon_sqrshrun, AddRetType),
4556  NEONMAP1(vqrshrunh_n_s16, aarch64_neon_sqrshrun, VectorRet | Use64BitVectors),
4557  NEONMAP1(vqrshruns_n_s32, aarch64_neon_sqrshrun, VectorRet | Use64BitVectors),
4558  NEONMAP1(vqshlb_n_s8, aarch64_neon_sqshl, Vectorize1ArgType | Use64BitVectors),
4559  NEONMAP1(vqshlb_n_u8, aarch64_neon_uqshl, Vectorize1ArgType | Use64BitVectors),
4560  NEONMAP1(vqshlb_s8, aarch64_neon_sqshl, Vectorize1ArgType | Use64BitVectors),
4561  NEONMAP1(vqshlb_u8, aarch64_neon_uqshl, Vectorize1ArgType | Use64BitVectors),
4562  NEONMAP1(vqshld_s64, aarch64_neon_sqshl, Add1ArgType),
4563  NEONMAP1(vqshld_u64, aarch64_neon_uqshl, Add1ArgType),
4564  NEONMAP1(vqshlh_n_s16, aarch64_neon_sqshl, Vectorize1ArgType | Use64BitVectors),
4565  NEONMAP1(vqshlh_n_u16, aarch64_neon_uqshl, Vectorize1ArgType | Use64BitVectors),
4566  NEONMAP1(vqshlh_s16, aarch64_neon_sqshl, Vectorize1ArgType | Use64BitVectors),
4567  NEONMAP1(vqshlh_u16, aarch64_neon_uqshl, Vectorize1ArgType | Use64BitVectors),
4568  NEONMAP1(vqshls_n_s32, aarch64_neon_sqshl, Add1ArgType),
4569  NEONMAP1(vqshls_n_u32, aarch64_neon_uqshl, Add1ArgType),
4570  NEONMAP1(vqshls_s32, aarch64_neon_sqshl, Add1ArgType),
4571  NEONMAP1(vqshls_u32, aarch64_neon_uqshl, Add1ArgType),
4572  NEONMAP1(vqshlub_n_s8, aarch64_neon_sqshlu, Vectorize1ArgType | Use64BitVectors),
4573  NEONMAP1(vqshluh_n_s16, aarch64_neon_sqshlu, Vectorize1ArgType | Use64BitVectors),
4574  NEONMAP1(vqshlus_n_s32, aarch64_neon_sqshlu, Add1ArgType),
4575  NEONMAP1(vqshrnd_n_s64, aarch64_neon_sqshrn, AddRetType),
4576  NEONMAP1(vqshrnd_n_u64, aarch64_neon_uqshrn, AddRetType),
4577  NEONMAP1(vqshrnh_n_s16, aarch64_neon_sqshrn, VectorRet | Use64BitVectors),
4578  NEONMAP1(vqshrnh_n_u16, aarch64_neon_uqshrn, VectorRet | Use64BitVectors),
4579  NEONMAP1(vqshrns_n_s32, aarch64_neon_sqshrn, VectorRet | Use64BitVectors),
4580  NEONMAP1(vqshrns_n_u32, aarch64_neon_uqshrn, VectorRet | Use64BitVectors),
4581  NEONMAP1(vqshrund_n_s64, aarch64_neon_sqshrun, AddRetType),
4582  NEONMAP1(vqshrunh_n_s16, aarch64_neon_sqshrun, VectorRet | Use64BitVectors),
4583  NEONMAP1(vqshruns_n_s32, aarch64_neon_sqshrun, VectorRet | Use64BitVectors),
4584  NEONMAP1(vqsubb_s8, aarch64_neon_sqsub, Vectorize1ArgType | Use64BitVectors),
4585  NEONMAP1(vqsubb_u8, aarch64_neon_uqsub, Vectorize1ArgType | Use64BitVectors),
4586  NEONMAP1(vqsubd_s64, aarch64_neon_sqsub, Add1ArgType),
4587  NEONMAP1(vqsubd_u64, aarch64_neon_uqsub, Add1ArgType),
4588  NEONMAP1(vqsubh_s16, aarch64_neon_sqsub, Vectorize1ArgType | Use64BitVectors),
4589  NEONMAP1(vqsubh_u16, aarch64_neon_uqsub, Vectorize1ArgType | Use64BitVectors),
4590  NEONMAP1(vqsubs_s32, aarch64_neon_sqsub, Add1ArgType),
4591  NEONMAP1(vqsubs_u32, aarch64_neon_uqsub, Add1ArgType),
4592  NEONMAP1(vrecped_f64, aarch64_neon_frecpe, Add1ArgType),
4593  NEONMAP1(vrecpes_f32, aarch64_neon_frecpe, Add1ArgType),
4594  NEONMAP1(vrecpxd_f64, aarch64_neon_frecpx, Add1ArgType),
4595  NEONMAP1(vrecpxs_f32, aarch64_neon_frecpx, Add1ArgType),
4596  NEONMAP1(vrshld_s64, aarch64_neon_srshl, Add1ArgType),
4597  NEONMAP1(vrshld_u64, aarch64_neon_urshl, Add1ArgType),
4598  NEONMAP1(vrsqrted_f64, aarch64_neon_frsqrte, Add1ArgType),
4599  NEONMAP1(vrsqrtes_f32, aarch64_neon_frsqrte, Add1ArgType),
4600  NEONMAP1(vrsqrtsd_f64, aarch64_neon_frsqrts, Add1ArgType),
4601  NEONMAP1(vrsqrtss_f32, aarch64_neon_frsqrts, Add1ArgType),
4602  NEONMAP1(vsha1cq_u32, aarch64_crypto_sha1c, 0),
4603  NEONMAP1(vsha1h_u32, aarch64_crypto_sha1h, 0),
4604  NEONMAP1(vsha1mq_u32, aarch64_crypto_sha1m, 0),
4605  NEONMAP1(vsha1pq_u32, aarch64_crypto_sha1p, 0),
4606  NEONMAP1(vshld_s64, aarch64_neon_sshl, Add1ArgType),
4607  NEONMAP1(vshld_u64, aarch64_neon_ushl, Add1ArgType),
4608  NEONMAP1(vslid_n_s64, aarch64_neon_vsli, Vectorize1ArgType),
4609  NEONMAP1(vslid_n_u64, aarch64_neon_vsli, Vectorize1ArgType),
4610  NEONMAP1(vsqaddb_u8, aarch64_neon_usqadd, Vectorize1ArgType | Use64BitVectors),
4611  NEONMAP1(vsqaddd_u64, aarch64_neon_usqadd, Add1ArgType),
4612  NEONMAP1(vsqaddh_u16, aarch64_neon_usqadd, Vectorize1ArgType | Use64BitVectors),
4613  NEONMAP1(vsqadds_u32, aarch64_neon_usqadd, Add1ArgType),
4614  NEONMAP1(vsrid_n_s64, aarch64_neon_vsri, Vectorize1ArgType),
4615  NEONMAP1(vsrid_n_u64, aarch64_neon_vsri, Vectorize1ArgType),
4616  NEONMAP1(vuqaddb_s8, aarch64_neon_suqadd, Vectorize1ArgType | Use64BitVectors),
4617  NEONMAP1(vuqaddd_s64, aarch64_neon_suqadd, Add1ArgType),
4618  NEONMAP1(vuqaddh_s16, aarch64_neon_suqadd, Vectorize1ArgType | Use64BitVectors),
4619  NEONMAP1(vuqadds_s32, aarch64_neon_suqadd, Add1ArgType),
4620  // FP16 scalar intrinisics go here.
4621  NEONMAP1(vabdh_f16, aarch64_sisd_fabd, Add1ArgType),
4622  NEONMAP1(vcvtah_s32_f16, aarch64_neon_fcvtas, AddRetType | Add1ArgType),
4623  NEONMAP1(vcvtah_s64_f16, aarch64_neon_fcvtas, AddRetType | Add1ArgType),
4624  NEONMAP1(vcvtah_u32_f16, aarch64_neon_fcvtau, AddRetType | Add1ArgType),
4625  NEONMAP1(vcvtah_u64_f16, aarch64_neon_fcvtau, AddRetType | Add1ArgType),
4626  NEONMAP1(vcvth_n_f16_s32, aarch64_neon_vcvtfxs2fp, AddRetType | Add1ArgType),
4627  NEONMAP1(vcvth_n_f16_s64, aarch64_neon_vcvtfxs2fp, AddRetType | Add1ArgType),
4628  NEONMAP1(vcvth_n_f16_u32, aarch64_neon_vcvtfxu2fp, AddRetType | Add1ArgType),
4629  NEONMAP1(vcvth_n_f16_u64, aarch64_neon_vcvtfxu2fp, AddRetType | Add1ArgType),
4630  NEONMAP1(vcvth_n_s32_f16, aarch64_neon_vcvtfp2fxs, AddRetType | Add1ArgType),
4631  NEONMAP1(vcvth_n_s64_f16, aarch64_neon_vcvtfp2fxs, AddRetType | Add1ArgType),
4632  NEONMAP1(vcvth_n_u32_f16, aarch64_neon_vcvtfp2fxu, AddRetType | Add1ArgType),
4633  NEONMAP1(vcvth_n_u64_f16, aarch64_neon_vcvtfp2fxu, AddRetType | Add1ArgType),
4634  NEONMAP1(vcvtmh_s32_f16, aarch64_neon_fcvtms, AddRetType | Add1ArgType),
4635  NEONMAP1(vcvtmh_s64_f16, aarch64_neon_fcvtms, AddRetType | Add1ArgType),
4636  NEONMAP1(vcvtmh_u32_f16, aarch64_neon_fcvtmu, AddRetType | Add1ArgType),
4637  NEONMAP1(vcvtmh_u64_f16, aarch64_neon_fcvtmu, AddRetType | Add1ArgType),
4638  NEONMAP1(vcvtnh_s32_f16, aarch64_neon_fcvtns, AddRetType | Add1ArgType),
4639  NEONMAP1(vcvtnh_s64_f16, aarch64_neon_fcvtns, AddRetType | Add1ArgType),
4640  NEONMAP1(vcvtnh_u32_f16, aarch64_neon_fcvtnu, AddRetType | Add1ArgType),
4641  NEONMAP1(vcvtnh_u64_f16, aarch64_neon_fcvtnu, AddRetType | Add1ArgType),
4642  NEONMAP1(vcvtph_s32_f16, aarch64_neon_fcvtps, AddRetType | Add1ArgType),
4643  NEONMAP1(vcvtph_s64_f16, aarch64_neon_fcvtps, AddRetType | Add1ArgType),
4644  NEONMAP1(vcvtph_u32_f16, aarch64_neon_fcvtpu, AddRetType | Add1ArgType),
4645  NEONMAP1(vcvtph_u64_f16, aarch64_neon_fcvtpu, AddRetType | Add1ArgType),
4646  NEONMAP1(vmulxh_f16, aarch64_neon_fmulx, Add1ArgType),
4647  NEONMAP1(vrecpeh_f16, aarch64_neon_frecpe, Add1ArgType),
4648  NEONMAP1(vrecpxh_f16, aarch64_neon_frecpx, Add1ArgType),
4649  NEONMAP1(vrsqrteh_f16, aarch64_neon_frsqrte, Add1ArgType),
4650  NEONMAP1(vrsqrtsh_f16, aarch64_neon_frsqrts, Add1ArgType),
4651 };
4652 
4653 #undef NEONMAP0
4654 #undef NEONMAP1
4655 #undef NEONMAP2
4656 
4658 
4661 
4662 
4663 static const NeonIntrinsicInfo *
4665  unsigned BuiltinID, bool &MapProvenSorted) {
4666 
4667 #ifndef NDEBUG
4668  if (!MapProvenSorted) {
4669  assert(std::is_sorted(std::begin(IntrinsicMap), std::end(IntrinsicMap)));
4670  MapProvenSorted = true;
4671  }
4672 #endif
4673 
4674  const NeonIntrinsicInfo *Builtin =
4675  std::lower_bound(IntrinsicMap.begin(), IntrinsicMap.end(), BuiltinID);
4676 
4677  if (Builtin != IntrinsicMap.end() && Builtin->BuiltinID == BuiltinID)
4678  return Builtin;
4679 
4680  return nullptr;
4681 }
4682 
4683 Function *CodeGenFunction::LookupNeonLLVMIntrinsic(unsigned IntrinsicID,
4684  unsigned Modifier,
4685  llvm::Type *ArgType,
4686  const CallExpr *E) {
4687  int VectorSize = 0;
4688  if (Modifier & Use64BitVectors)
4689  VectorSize = 64;
4690  else if (Modifier & Use128BitVectors)
4691  VectorSize = 128;
4692 
4693  // Return type.
4695  if (Modifier & AddRetType) {
4696  llvm::Type *Ty = ConvertType(E->getCallReturnType(getContext()));
4697  if (Modifier & VectorizeRetType)
4698  Ty = llvm::VectorType::get(
4699  Ty, VectorSize ? VectorSize / Ty->getPrimitiveSizeInBits() : 1);
4700 
4701  Tys.push_back(Ty);
4702  }
4703 
4704  // Arguments.
4705  if (Modifier & VectorizeArgTypes) {
4706  int Elts = VectorSize ? VectorSize / ArgType->getPrimitiveSizeInBits() : 1;
4707  ArgType = llvm::VectorType::get(ArgType, Elts);
4708  }
4709 
4710  if (Modifier & (Add1ArgType | Add2ArgTypes))
4711  Tys.push_back(ArgType);
4712 
4713  if (Modifier & Add2ArgTypes)
4714  Tys.push_back(ArgType);
4715 
4716  if (Modifier & InventFloatType)
4717  Tys.push_back(FloatTy);
4718 
4719  return CGM.getIntrinsic(IntrinsicID, Tys);
4720 }
4721 
4723  const NeonIntrinsicInfo &SISDInfo,
4725  const CallExpr *E) {
4726  unsigned BuiltinID = SISDInfo.BuiltinID;
4727  unsigned int Int = SISDInfo.LLVMIntrinsic;
4728  unsigned Modifier = SISDInfo.TypeModifier;
4729  const char *s = SISDInfo.NameHint;
4730 
4731  switch (BuiltinID) {
4732  case NEON::BI__builtin_neon_vcled_s64:
4733  case NEON::BI__builtin_neon_vcled_u64:
4734  case NEON::BI__builtin_neon_vcles_f32:
4735  case NEON::BI__builtin_neon_vcled_f64:
4736  case NEON::BI__builtin_neon_vcltd_s64:
4737  case NEON::BI__builtin_neon_vcltd_u64:
4738  case NEON::BI__builtin_neon_vclts_f32:
4739  case NEON::BI__builtin_neon_vcltd_f64:
4740  case NEON::BI__builtin_neon_vcales_f32:
4741  case NEON::BI__builtin_neon_vcaled_f64:
4742  case NEON::BI__builtin_neon_vcalts_f32:
4743  case NEON::BI__builtin_neon_vcaltd_f64:
4744  // Only one direction of comparisons actually exist, cmle is actually a cmge
4745  // with swapped operands. The table gives us the right intrinsic but we
4746  // still need to do the swap.
4747  std::swap(Ops[0], Ops[1]);
4748  break;
4749  }
4750 
4751  assert(Int && "Generic code assumes a valid intrinsic");
4752 
4753  // Determine the type(s) of this overloaded AArch64 intrinsic.
4754  const Expr *Arg = E->getArg(0);
4755  llvm::Type *ArgTy = CGF.ConvertType(Arg->getType());
4756  Function *F = CGF.LookupNeonLLVMIntrinsic(Int, Modifier, ArgTy, E);
4757 
4758  int j = 0;
4759  ConstantInt *C0 = ConstantInt::get(CGF.SizeTy, 0);
4760  for (Function::const_arg_iterator ai = F->arg_begin(), ae = F->arg_end();
4761  ai != ae; ++ai, ++j) {
4762  llvm::Type *ArgTy = ai->getType();
4763  if (Ops[j]->getType()->getPrimitiveSizeInBits() ==
4764  ArgTy->getPrimitiveSizeInBits())
4765  continue;
4766 
4767  assert(ArgTy->isVectorTy() && !Ops[j]->getType()->isVectorTy());
4768  // The constant argument to an _n_ intrinsic always has Int32Ty, so truncate
4769  // it before inserting.
4770  Ops[j] =
4771  CGF.Builder.CreateTruncOrBitCast(Ops[j], ArgTy->getVectorElementType());
4772  Ops[j] =
4773  CGF.Builder.CreateInsertElement(UndefValue::get(ArgTy), Ops[j], C0);
4774  }
4775 
4776  Value *Result = CGF.EmitNeonCall(F, Ops, s);
4777  llvm::Type *ResultType = CGF.ConvertType(E->getType());
4778  if (ResultType->getPrimitiveSizeInBits() <
4779  Result->getType()->getPrimitiveSizeInBits())
4780  return CGF.Builder.CreateExtractElement(Result, C0);
4781 
4782  return CGF.Builder.CreateBitCast(Result, ResultType, s);
4783 }
4784 
4786  unsigned BuiltinID, unsigned LLVMIntrinsic, unsigned AltLLVMIntrinsic,
4787  const char *NameHint, unsigned Modifier, const CallExpr *E,
4788  SmallVectorImpl<llvm::Value *> &Ops, Address PtrOp0, Address PtrOp1,
4789  llvm::Triple::ArchType Arch) {
4790  // Get the last argument, which specifies the vector type.
4791  llvm::APSInt NeonTypeConst;
4792  const Expr *Arg = E->getArg(E->getNumArgs() - 1);
4793  if (!Arg->isIntegerConstantExpr(NeonTypeConst, getContext()))
4794  return nullptr;
4795 
4796  // Determine the type of this overloaded NEON intrinsic.
4797  NeonTypeFlags Type(NeonTypeConst.getZExtValue());
4798  bool Usgn = Type.isUnsigned();
4799  bool Quad = Type.isQuad();
4800  const bool HasLegalHalfType = getTarget().hasLegalHalfType();
4801 
4802  llvm::VectorType *VTy = GetNeonType(this, Type, HasLegalHalfType);
4803  llvm::Type *Ty = VTy;
4804  if (!Ty)
4805  return nullptr;
4806 
4807  auto getAlignmentValue32 = [&](Address addr) -> Value* {
4808  return Builder.getInt32(addr.getAlignment().getQuantity());
4809  };
4810 
4811  unsigned Int = LLVMIntrinsic;
4812  if ((Modifier & UnsignedAlts) && !Usgn)
4813  Int = AltLLVMIntrinsic;
4814 
4815  switch (BuiltinID) {
4816  default: break;
4817  case NEON::BI__builtin_neon_vabs_v:
4818  case NEON::BI__builtin_neon_vabsq_v:
4819  if (VTy->getElementType()->isFloatingPointTy())
4820  return EmitNeonCall(CGM.getIntrinsic(Intrinsic::fabs, Ty), Ops, "vabs");
4821  return EmitNeonCall(CGM.getIntrinsic(LLVMIntrinsic, Ty), Ops, "vabs");
4822  case NEON::BI__builtin_neon_vaddhn_v: {
4823  llvm::VectorType *SrcTy =
4824  llvm::VectorType::getExtendedElementVectorType(VTy);
4825 
4826  // %sum = add <4 x i32> %lhs, %rhs
4827  Ops[0] = Builder.CreateBitCast(Ops[0], SrcTy);
4828  Ops[1] = Builder.CreateBitCast(Ops[1], SrcTy);
4829  Ops[0] = Builder.CreateAdd(Ops[0], Ops[1], "vaddhn");
4830 
4831  // %high = lshr <4 x i32> %sum, <i32 16, i32 16, i32 16, i32 16>
4832  Constant *ShiftAmt =
4833  ConstantInt::get(SrcTy, SrcTy->getScalarSizeInBits() / 2);
4834  Ops[0] = Builder.CreateLShr(Ops[0], ShiftAmt, "vaddhn");
4835 
4836  // %res = trunc <4 x i32> %high to <4 x i16>
4837  return Builder.CreateTrunc(Ops[0], VTy, "vaddhn");
4838  }
4839  case NEON::BI__builtin_neon_vcale_v:
4840  case NEON::BI__builtin_neon_vcaleq_v:
4841  case NEON::BI__builtin_neon_vcalt_v:
4842  case NEON::BI__builtin_neon_vcaltq_v:
4843  std::swap(Ops[0], Ops[1]);
4844  LLVM_FALLTHROUGH;
4845  case NEON::BI__builtin_neon_vcage_v:
4846  case NEON::BI__builtin_neon_vcageq_v:
4847  case NEON::BI__builtin_neon_vcagt_v:
4848  case NEON::BI__builtin_neon_vcagtq_v: {
4849  llvm::Type *Ty;
4850  switch (VTy->getScalarSizeInBits()) {
4851  default: llvm_unreachable("unexpected type");
4852  case 32:
4853  Ty = FloatTy;
4854  break;
4855  case 64:
4856  Ty = DoubleTy;
4857  break;
4858  case 16:
4859  Ty = HalfTy;
4860  break;
4861  }
4862  llvm::Type *VecFlt = llvm::VectorType::get(Ty, VTy->getNumElements());
4863  llvm::Type *Tys[] = { VTy, VecFlt };
4864  Function *F = CGM.getIntrinsic(LLVMIntrinsic, Tys);
4865  return EmitNeonCall(F, Ops, NameHint);
4866  }
4867  case NEON::BI__builtin_neon_vceqz_v:
4868  case NEON::BI__builtin_neon_vceqzq_v:
4869  return EmitAArch64CompareBuiltinExpr(Ops[0], Ty, ICmpInst::FCMP_OEQ,
4870  ICmpInst::ICMP_EQ, "vceqz");
4871  case NEON::BI__builtin_neon_vcgez_v:
4872  case NEON::BI__builtin_neon_vcgezq_v:
4873  return EmitAArch64CompareBuiltinExpr(Ops[0], Ty, ICmpInst::FCMP_OGE,
4874  ICmpInst::ICMP_SGE, "vcgez");
4875  case NEON::BI__builtin_neon_vclez_v:
4876  case NEON::BI__builtin_neon_vclezq_v:
4877  return EmitAArch64CompareBuiltinExpr(Ops[0], Ty, ICmpInst::FCMP_OLE,
4878  ICmpInst::ICMP_SLE, "vclez");
4879  case NEON::BI__builtin_neon_vcgtz_v:
4880  case NEON::BI__builtin_neon_vcgtzq_v:
4881  return EmitAArch64CompareBuiltinExpr(Ops[0], Ty, ICmpInst::FCMP_OGT,
4882  ICmpInst::ICMP_SGT, "vcgtz");
4883  case NEON::BI__builtin_neon_vcltz_v:
4884  case NEON::BI__builtin_neon_vcltzq_v:
4885  return EmitAArch64CompareBuiltinExpr(Ops[0], Ty, ICmpInst::FCMP_OLT,
4886  ICmpInst::ICMP_SLT, "vcltz");
4887  case NEON::BI__builtin_neon_vclz_v:
4888  case NEON::BI__builtin_neon_vclzq_v:
4889  // We generate target-independent intrinsic, which needs a second argument
4890  // for whether or not clz of zero is undefined; on ARM it isn't.
4891  Ops.push_back(Builder.getInt1(getTarget().isCLZForZeroUndef()));
4892  break;
4893  case NEON::BI__builtin_neon_vcvt_f32_v:
4894  case NEON::BI__builtin_neon_vcvtq_f32_v:
4895  Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
4896  Ty = GetNeonType(this, NeonTypeFlags(NeonTypeFlags::Float32, false, Quad),
4897  HasLegalHalfType);
4898  return Usgn ? Builder.CreateUIToFP(Ops[0], Ty, "vcvt")
4899  : Builder.CreateSIToFP(Ops[0], Ty, "vcvt");
4900  case NEON::BI__builtin_neon_vcvt_f16_v:
4901  case NEON::BI__builtin_neon_vcvtq_f16_v:
4902  Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
4903  Ty = GetNeonType(this, NeonTypeFlags(NeonTypeFlags::Float16, false, Quad),
4904  HasLegalHalfType);
4905  return Usgn ? Builder.CreateUIToFP(Ops[0], Ty, "vcvt")
4906  : Builder.CreateSIToFP(Ops[0], Ty, "vcvt");
4907  case NEON::BI__builtin_neon_vcvt_n_f16_v:
4908  case NEON::BI__builtin_neon_vcvt_n_f32_v:
4909  case NEON::BI__builtin_neon_vcvt_n_f64_v:
4910  case NEON::BI__builtin_neon_vcvtq_n_f16_v:
4911  case NEON::BI__builtin_neon_vcvtq_n_f32_v:
4912  case NEON::BI__builtin_neon_vcvtq_n_f64_v: {
4913  llvm::Type *Tys[2] = { GetFloatNeonType(this, Type), Ty };
4914  Int = Usgn ? LLVMIntrinsic : AltLLVMIntrinsic;
4915  Function *F = CGM.getIntrinsic(Int, Tys);
4916  return EmitNeonCall(F, Ops, "vcvt_n");
4917  }
4918  case NEON::BI__builtin_neon_vcvt_n_s16_v:
4919  case NEON::BI__builtin_neon_vcvt_n_s32_v:
4920  case NEON::BI__builtin_neon_vcvt_n_u16_v:
4921  case NEON::BI__builtin_neon_vcvt_n_u32_v:
4922  case NEON::BI__builtin_neon_vcvt_n_s64_v:
4923  case NEON::BI__builtin_neon_vcvt_n_u64_v:
4924  case NEON::BI__builtin_neon_vcvtq_n_s16_v:
4925  case NEON::BI__builtin_neon_vcvtq_n_s32_v:
4926  case NEON::BI__builtin_neon_vcvtq_n_u16_v:
4927  case NEON::BI__builtin_neon_vcvtq_n_u32_v:
4928  case NEON::BI__builtin_neon_vcvtq_n_s64_v:
4929  case NEON::BI__builtin_neon_vcvtq_n_u64_v: {
4930  llvm::Type *Tys[2] = { Ty, GetFloatNeonType(this, Type) };
4931  Function *F = CGM.getIntrinsic(LLVMIntrinsic, Tys);
4932  return EmitNeonCall(F, Ops, "vcvt_n");
4933  }
4934  case NEON::BI__builtin_neon_vcvt_s32_v:
4935  case NEON::BI__builtin_neon_vcvt_u32_v:
4936  case NEON::BI__builtin_neon_vcvt_s64_v:
4937  case NEON::BI__builtin_neon_vcvt_u64_v:
4938  case NEON::BI__builtin_neon_vcvt_s16_v:
4939  case NEON::BI__builtin_neon_vcvt_u16_v:
4940  case NEON::BI__builtin_neon_vcvtq_s32_v:
4941  case NEON::BI__builtin_neon_vcvtq_u32_v:
4942  case NEON::BI__builtin_neon_vcvtq_s64_v:
4943  case NEON::BI__builtin_neon_vcvtq_u64_v:
4944  case NEON::BI__builtin_neon_vcvtq_s16_v:
4945  case NEON::BI__builtin_neon_vcvtq_u16_v: {
4946  Ops[0] = Builder.CreateBitCast(Ops[0], GetFloatNeonType(this, Type));
4947  return Usgn ? Builder.CreateFPToUI(Ops[0], Ty, "vcvt")
4948  : Builder.CreateFPToSI(Ops[0], Ty, "vcvt");
4949  }
4950  case NEON::BI__builtin_neon_vcvta_s16_v:
4951  case NEON::BI__builtin_neon_vcvta_s32_v:
4952  case NEON::BI__builtin_neon_vcvta_s64_v:
4953  case NEON::BI__builtin_neon_vcvta_u16_v:
4954  case NEON::BI__builtin_neon_vcvta_u32_v:
4955  case NEON::BI__builtin_neon_vcvta_u64_v:
4956  case NEON::BI__builtin_neon_vcvtaq_s16_v:
4957  case NEON::BI__builtin_neon_vcvtaq_s32_v:
4958  case NEON::BI__builtin_neon_vcvtaq_s64_v:
4959  case NEON::BI__builtin_neon_vcvtaq_u16_v:
4960  case NEON::BI__builtin_neon_vcvtaq_u32_v:
4961  case NEON::BI__builtin_neon_vcvtaq_u64_v:
4962  case NEON::BI__builtin_neon_vcvtn_s16_v:
4963  case NEON::BI__builtin_neon_vcvtn_s32_v:
4964  case NEON::BI__builtin_neon_vcvtn_s64_v:
4965  case NEON::BI__builtin_neon_vcvtn_u16_v:
4966  case NEON::BI__builtin_neon_vcvtn_u32_v:
4967  case NEON::BI__builtin_neon_vcvtn_u64_v:
4968  case NEON::BI__builtin_neon_vcvtnq_s16_v:
4969  case NEON::BI__builtin_neon_vcvtnq_s32_v:
4970  case NEON::BI__builtin_neon_vcvtnq_s64_v:
4971  case NEON::BI__builtin_neon_vcvtnq_u16_v:
4972  case NEON::BI__builtin_neon_vcvtnq_u32_v:
4973  case NEON::BI__builtin_neon_vcvtnq_u64_v:
4974  case NEON::BI__builtin_neon_vcvtp_s16_v:
4975  case NEON::BI__builtin_neon_vcvtp_s32_v:
4976  case NEON::BI__builtin_neon_vcvtp_s64_v:
4977  case NEON::BI__builtin_neon_vcvtp_u16_v:
4978  case NEON::BI__builtin_neon_vcvtp_u32_v:
4979  case NEON::BI__builtin_neon_vcvtp_u64_v:
4980  case NEON::BI__builtin_neon_vcvtpq_s16_v:
4981  case NEON::BI__builtin_neon_vcvtpq_s32_v:
4982  case NEON::BI__builtin_neon_vcvtpq_s64_v:
4983  case NEON::BI__builtin_neon_vcvtpq_u16_v:
4984  case NEON::BI__builtin_neon_vcvtpq_u32_v:
4985  case NEON::BI__builtin_neon_vcvtpq_u64_v:
4986  case NEON::BI__builtin_neon_vcvtm_s16_v:
4987  case NEON::BI__builtin_neon_vcvtm_s32_v:
4988  case NEON::BI__builtin_neon_vcvtm_s64_v:
4989  case NEON::BI__builtin_neon_vcvtm_u16_v:
4990  case NEON::BI__builtin_neon_vcvtm_u32_v:
4991  case NEON::BI__builtin_neon_vcvtm_u64_v:
4992  case NEON::BI__builtin_neon_vcvtmq_s16_v:
4993  case NEON::BI__builtin_neon_vcvtmq_s32_v:
4994  case NEON::BI__builtin_neon_vcvtmq_s64_v:
4995  case NEON::BI__builtin_neon_vcvtmq_u16_v:
4996  case NEON::BI__builtin_neon_vcvtmq_u32_v:
4997  case NEON::BI__builtin_neon_vcvtmq_u64_v: {
4998  llvm::Type *Tys[2] = { Ty, GetFloatNeonType(this, Type) };
4999  return EmitNeonCall(CGM.getIntrinsic(LLVMIntrinsic, Tys), Ops, NameHint);
5000  }
5001  case NEON::BI__builtin_neon_vext_v:
5002  case NEON::BI__builtin_neon_vextq_v: {
5003  int CV = cast<ConstantInt>(Ops[2])->getSExtValue();
5004  SmallVector<uint32_t, 16> Indices;
5005  for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i)
5006  Indices.push_back(i+CV);
5007 
5008  Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
5009  Ops[1] = Builder.CreateBitCast(Ops[1], Ty);
5010  return Builder.CreateShuffleVector(Ops[0], Ops[1], Indices, "vext");
5011  }
5012  case NEON::BI__builtin_neon_vfma_v:
5013  case NEON::BI__builtin_neon_vfmaq_v: {
5014  Value *F = CGM.getIntrinsic(Intrinsic::fma, Ty);
5015  Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
5016  Ops[1] = Builder.CreateBitCast(Ops[1], Ty);
5017  Ops[2] = Builder.CreateBitCast(Ops[2], Ty);
5018 
5019  // NEON intrinsic puts accumulator first, unlike the LLVM fma.
5020  return Builder.CreateCall(F, {Ops[1], Ops[2], Ops[0]});
5021  }
5022  case NEON::BI__builtin_neon_vld1_v:
5023  case NEON::BI__builtin_neon_vld1q_v: {
5024  llvm::Type *Tys[] = {Ty, Int8PtrTy};
5025  Ops.push_back(getAlignmentValue32(PtrOp0));
5026  return EmitNeonCall(CGM.getIntrinsic(LLVMIntrinsic, Tys), Ops, "vld1");
5027  }
5028  case NEON::BI__builtin_neon_vld1_x2_v:
5029  case NEON::BI__builtin_neon_vld1q_x2_v:
5030  case NEON::BI__builtin_neon_vld1_x3_v:
5031  case NEON::BI__builtin_neon_vld1q_x3_v:
5032  case NEON::BI__builtin_neon_vld1_x4_v:
5033  case NEON::BI__builtin_neon_vld1q_x4_v: {
5034  llvm::Type *PTy = llvm::PointerType::getUnqual(VTy->getVectorElementType());
5035  Ops[1] = Builder.CreateBitCast(Ops[1], PTy);
5036  llvm::Type *Tys[2] = { VTy, PTy };
5037  Function *F = CGM.getIntrinsic(LLVMIntrinsic, Tys);
5038  Ops[1] = Builder.CreateCall(F, Ops[1], "vld1xN");
5039  Ty = llvm::PointerType::getUnqual(Ops[1]->getType());
5040  Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
5041  return Builder.CreateDefaultAlignedStore(Ops[1], Ops[0]);
5042  }
5043  case NEON::BI__builtin_neon_vld2_v:
5044  case NEON::BI__builtin_neon_vld2q_v:
5045  case NEON::BI__builtin_neon_vld3_v:
5046  case NEON::BI__builtin_neon_vld3q_v:
5047  case NEON::BI__builtin_neon_vld4_v:
5048  case NEON::BI__builtin_neon_vld4q_v:
5049  case NEON::BI__builtin_neon_vld2_dup_v:
5050  case NEON::BI__builtin_neon_vld2q_dup_v:
5051  case NEON::BI__builtin_neon_vld3_dup_v:
5052  case NEON::BI__builtin_neon_vld3q_dup_v:
5053  case NEON::BI__builtin_neon_vld4_dup_v:
5054  case NEON::BI__builtin_neon_vld4q_dup_v: {
5055  llvm::Type *Tys[] = {Ty, Int8PtrTy};
5056  Function *F = CGM.getIntrinsic(LLVMIntrinsic, Tys);
5057  Value *Align = getAlignmentValue32(PtrOp1);
5058  Ops[1] = Builder.CreateCall(F, {Ops[1], Align}, NameHint);
5059  Ty = llvm::PointerType::getUnqual(Ops[1]->getType());
5060  Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
5061  return Builder.CreateDefaultAlignedStore(Ops[1], Ops[0]);
5062  }
5063  case NEON::BI__builtin_neon_vld1_dup_v:
5064  case NEON::BI__builtin_neon_vld1q_dup_v: {
5065  Value *V = UndefValue::get(Ty);
5066  Ty = llvm::PointerType::getUnqual(VTy->getElementType());
5067  PtrOp0 = Builder.CreateBitCast(PtrOp0, Ty);
5068  LoadInst *Ld = Builder.CreateLoad(PtrOp0);
5069  llvm::Constant *CI = ConstantInt::get(SizeTy, 0);
5070  Ops[0] = Builder.CreateInsertElement(V, Ld, CI);
5071  return EmitNeonSplat(Ops[0], CI);
5072  }
5073  case NEON::BI__builtin_neon_vld2_lane_v:
5074  case NEON::BI__builtin_neon_vld2q_lane_v:
5075  case NEON::BI__builtin_neon_vld3_lane_v:
5076  case NEON::BI__builtin_neon_vld3q_lane_v:
5077  case NEON::BI__builtin_neon_vld4_lane_v:
5078  case NEON::BI__builtin_neon_vld4q_lane_v: {
5079  llvm::Type *Tys[] = {Ty, Int8PtrTy};
5080  Function *F = CGM.getIntrinsic(LLVMIntrinsic, Tys);
5081  for (unsigned I = 2; I < Ops.size() - 1; ++I)
5082  Ops[I] = Builder.CreateBitCast(Ops[I], Ty);
5083  Ops.push_back(getAlignmentValue32(PtrOp1));
5084  Ops[1] = Builder.CreateCall(F, makeArrayRef(Ops).slice(1), NameHint);
5085  Ty = llvm::PointerType::getUnqual(Ops[1]->getType());
5086  Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
5087  return Builder.CreateDefaultAlignedStore(Ops[1], Ops[0]);
5088  }
5089  case NEON::BI__builtin_neon_vmovl_v: {
5090  llvm::Type *DTy =llvm::VectorType::getTruncatedElementVectorType(VTy);
5091  Ops[0] = Builder.CreateBitCast(Ops[0], DTy);
5092  if (Usgn)
5093  return Builder.CreateZExt(Ops[0], Ty, "vmovl");
5094  return Builder.CreateSExt(Ops[0], Ty, "vmovl");
5095  }
5096  case NEON::BI__builtin_neon_vmovn_v: {
5097  llvm::Type *QTy = llvm::VectorType::getExtendedElementVectorType(VTy);
5098  Ops[0] = Builder.CreateBitCast(Ops[0], QTy);
5099  return Builder.CreateTrunc(Ops[0], Ty, "vmovn");
5100  }
5101  case NEON::BI__builtin_neon_vmull_v:
5102  // FIXME: the integer vmull operations could be emitted in terms of pure
5103  // LLVM IR (2 exts followed by a mul). Unfortunately LLVM has a habit of
5104  // hoisting the exts outside loops. Until global ISel comes along that can
5105  // see through such movement this leads to bad CodeGen. So we need an
5106  // intrinsic for now.
5107  Int = Usgn ? Intrinsic::arm_neon_vmullu : Intrinsic::arm_neon_vmulls;
5108  Int = Type.isPoly() ? (unsigned)Intrinsic::arm_neon_vmullp : Int;
5109  return EmitNeonCall(CGM.getIntrinsic(Int, Ty), Ops, "vmull");
5110  case NEON::BI__builtin_neon_vpadal_v:
5111  case NEON::BI__builtin_neon_vpadalq_v: {
5112  // The source operand type has twice as many elements of half the size.
5113  unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
5114  llvm::Type *EltTy =
5115  llvm::IntegerType::get(getLLVMContext(), EltBits / 2);
5116  llvm::Type *NarrowTy =
5117  llvm::VectorType::get(EltTy, VTy->getNumElements() * 2);
5118  llvm::Type *Tys[2] = { Ty, NarrowTy };
5119  return EmitNeonCall(CGM.getIntrinsic(Int, Tys), Ops, NameHint);
5120  }
5121  case NEON::BI__builtin_neon_vpaddl_v:
5122  case NEON::BI__builtin_neon_vpaddlq_v: {
5123  // The source operand type has twice as many elements of half the size.
5124  unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
5125  llvm::Type *EltTy = llvm::IntegerType::get(getLLVMContext(), EltBits / 2);
5126  llvm::Type *NarrowTy =
5127  llvm::VectorType::get(EltTy, VTy->getNumElements() * 2);
5128  llvm::Type *Tys[2] = { Ty, NarrowTy };
5129  return EmitNeonCall(CGM.getIntrinsic(Int, Tys), Ops, "vpaddl");
5130  }
5131  case NEON::BI__builtin_neon_vqdmlal_v:
5132  case NEON::BI__builtin_neon_vqdmlsl_v: {
5133  SmallVector<Value *, 2> MulOps(Ops.begin() + 1, Ops.end());
5134  Ops[1] =
5135  EmitNeonCall(CGM.getIntrinsic(LLVMIntrinsic, Ty), MulOps, "vqdmlal");
5136  Ops.resize(2);
5137  return EmitNeonCall(CGM.getIntrinsic(AltLLVMIntrinsic, Ty), Ops, NameHint);
5138  }
5139  case NEON::BI__builtin_neon_vqshl_n_v:
5140  case NEON::BI__builtin_neon_vqshlq_n_v:
5141  return EmitNeonCall(CGM.getIntrinsic(Int, Ty), Ops, "vqshl_n",
5142  1, false);
5143  case NEON::BI__builtin_neon_vqshlu_n_v:
5144  case NEON::BI__builtin_neon_vqshluq_n_v:
5145  return EmitNeonCall(CGM.getIntrinsic(Int, Ty), Ops, "vqshlu_n",
5146  1, false);
5147  case NEON::BI__builtin_neon_vrecpe_v:
5148  case NEON::BI__builtin_neon_vrecpeq_v:
5149  case NEON::BI__builtin_neon_vrsqrte_v:
5150  case NEON::BI__builtin_neon_vrsqrteq_v:
5151  Int = Ty->isFPOrFPVectorTy() ? LLVMIntrinsic : AltLLVMIntrinsic;
5152  return EmitNeonCall(CGM.getIntrinsic(Int, Ty), Ops, NameHint);
5153  case NEON::BI__builtin_neon_vrndi_v:
5154  case NEON::BI__builtin_neon_vrndiq_v:
5155  Int = Intrinsic::nearbyint;
5156  return EmitNeonCall(CGM.getIntrinsic(Int, Ty), Ops, NameHint);
5157  case NEON::BI__builtin_neon_vrshr_n_v:
5158  case NEON::BI__builtin_neon_vrshrq_n_v:
5159  return EmitNeonCall(CGM.getIntrinsic(Int, Ty), Ops, "vrshr_n",
5160  1, true);
5161  case NEON::BI__builtin_neon_vshl_n_v:
5162  case NEON::BI__builtin_neon_vshlq_n_v:
5163  Ops[1] = EmitNeonShiftVector(Ops[1], Ty, false);
5164  return Builder.CreateShl(Builder.CreateBitCast(Ops[0],Ty), Ops[1],
5165  "vshl_n");
5166  case NEON::BI__builtin_neon_vshll_n_v: {
5167  llvm::Type *SrcTy = llvm::VectorType::getTruncatedElementVectorType(VTy);
5168  Ops[0] = Builder.CreateBitCast(Ops[0], SrcTy);
5169  if (Usgn)
5170  Ops[0] = Builder.CreateZExt(Ops[0], VTy);
5171  else
5172  Ops[0] = Builder.CreateSExt(Ops[0], VTy);
5173  Ops[1] = EmitNeonShiftVector(Ops[1], VTy, false);
5174  return Builder.CreateShl(Ops[0], Ops[1], "vshll_n");
5175  }
5176  case NEON::BI__builtin_neon_vshrn_n_v: {
5177  llvm::Type *SrcTy = llvm::VectorType::getExtendedElementVectorType(VTy);
5178  Ops[0] = Builder.CreateBitCast(Ops[0], SrcTy);
5179  Ops[1] = EmitNeonShiftVector(Ops[1], SrcTy, false);
5180  if (Usgn)
5181  Ops[0] = Builder.CreateLShr(Ops[0], Ops[1]);
5182  else
5183  Ops[0] = Builder.CreateAShr(Ops[0], Ops[1]);
5184  return Builder.CreateTrunc(Ops[0], Ty, "vshrn_n");
5185  }
5186  case NEON::BI__builtin_neon_vshr_n_v: