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 "CodeGenFunction.h"
15#include "CGObjCRuntime.h"
16#include "CodeGenModule.h"
17#include "TargetInfo.h"
18#include "clang/AST/ASTContext.h"
19#include "clang/AST/Decl.h"
20#include "clang/Basic/TargetBuiltins.h"
21#include "clang/Basic/TargetInfo.h"
22#include "clang/CodeGen/CGFunctionInfo.h"
23#include "llvm/IR/DataLayout.h"
24#include "llvm/IR/Intrinsics.h"
25
26using namespace clang;
27using namespace CodeGen;
28using namespace llvm;
29
30/// getBuiltinLibFunction - Given a builtin id for a function like
31/// "__builtin_fabsf", return a Function* for "fabsf".
32llvm::Value *CodeGenModule::getBuiltinLibFunction(const FunctionDecl *FD,
33                                                  unsigned BuiltinID) {
34  assert(Context.BuiltinInfo.isLibFunction(BuiltinID));
35
36  // Get the name, skip over the __builtin_ prefix (if necessary).
37  StringRef Name;
38  GlobalDecl D(FD);
39
40  // If the builtin has been declared explicitly with an assembler label,
41  // use the mangled name. This differs from the plain label on platforms
42  // that prefix labels.
43  if (FD->hasAttr<AsmLabelAttr>())
44    Name = getMangledName(D);
45  else
46    Name = Context.BuiltinInfo.GetName(BuiltinID) + 10;
47
48  llvm::FunctionType *Ty =
49    cast<llvm::FunctionType>(getTypes().ConvertType(FD->getType()));
50
51  return GetOrCreateLLVMFunction(Name, Ty, D, /*ForVTable=*/false);
52}
53
54/// Emit the conversions required to turn the given value into an
55/// integer of the given size.
56static Value *EmitToInt(CodeGenFunction &CGF, llvm::Value *V,
57                        QualType T, llvm::IntegerType *IntType) {
58  V = CGF.EmitToMemory(V, T);
59
60  if (V->getType()->isPointerTy())
61    return CGF.Builder.CreatePtrToInt(V, IntType);
62
63  assert(V->getType() == IntType);
64  return V;
65}
66
67static Value *EmitFromInt(CodeGenFunction &CGF, llvm::Value *V,
68                          QualType T, llvm::Type *ResultType) {
69  V = CGF.EmitFromMemory(V, T);
70
71  if (ResultType->isPointerTy())
72    return CGF.Builder.CreateIntToPtr(V, ResultType);
73
74  assert(V->getType() == ResultType);
75  return V;
76}
77
78/// Utility to insert an atomic instruction based on Instrinsic::ID
79/// and the expression node.
80static RValue EmitBinaryAtomic(CodeGenFunction &CGF,
81                               llvm::AtomicRMWInst::BinOp Kind,
82                               const CallExpr *E) {
83  QualType T = E->getType();
84  assert(E->getArg(0)->getType()->isPointerType());
85  assert(CGF.getContext().hasSameUnqualifiedType(T,
86                                  E->getArg(0)->getType()->getPointeeType()));
87  assert(CGF.getContext().hasSameUnqualifiedType(T, E->getArg(1)->getType()));
88
89  llvm::Value *DestPtr = CGF.EmitScalarExpr(E->getArg(0));
90  unsigned AddrSpace = DestPtr->getType()->getPointerAddressSpace();
91
92  llvm::IntegerType *IntType =
93    llvm::IntegerType::get(CGF.getLLVMContext(),
94                           CGF.getContext().getTypeSize(T));
95  llvm::Type *IntPtrType = IntType->getPointerTo(AddrSpace);
96
97  llvm::Value *Args[2];
98  Args[0] = CGF.Builder.CreateBitCast(DestPtr, IntPtrType);
99  Args[1] = CGF.EmitScalarExpr(E->getArg(1));
100  llvm::Type *ValueType = Args[1]->getType();
101  Args[1] = EmitToInt(CGF, Args[1], T, IntType);
102
103  llvm::Value *Result =
104      CGF.Builder.CreateAtomicRMW(Kind, Args[0], Args[1],
105                                  llvm::SequentiallyConsistent);
106  Result = EmitFromInt(CGF, Result, T, ValueType);
107  return RValue::get(Result);
108}
109
110/// Utility to insert an atomic instruction based Instrinsic::ID and
111/// the expression node, where the return value is the result of the
112/// operation.
113static RValue EmitBinaryAtomicPost(CodeGenFunction &CGF,
114                                   llvm::AtomicRMWInst::BinOp Kind,
115                                   const CallExpr *E,
116                                   Instruction::BinaryOps Op) {
117  QualType T = E->getType();
118  assert(E->getArg(0)->getType()->isPointerType());
119  assert(CGF.getContext().hasSameUnqualifiedType(T,
120                                  E->getArg(0)->getType()->getPointeeType()));
121  assert(CGF.getContext().hasSameUnqualifiedType(T, E->getArg(1)->getType()));
122
123  llvm::Value *DestPtr = CGF.EmitScalarExpr(E->getArg(0));
124  unsigned AddrSpace = DestPtr->getType()->getPointerAddressSpace();
125
126  llvm::IntegerType *IntType =
127    llvm::IntegerType::get(CGF.getLLVMContext(),
128                           CGF.getContext().getTypeSize(T));
129  llvm::Type *IntPtrType = IntType->getPointerTo(AddrSpace);
130
131  llvm::Value *Args[2];
132  Args[1] = CGF.EmitScalarExpr(E->getArg(1));
133  llvm::Type *ValueType = Args[1]->getType();
134  Args[1] = EmitToInt(CGF, Args[1], T, IntType);
135  Args[0] = CGF.Builder.CreateBitCast(DestPtr, IntPtrType);
136
137  llvm::Value *Result =
138      CGF.Builder.CreateAtomicRMW(Kind, Args[0], Args[1],
139                                  llvm::SequentiallyConsistent);
140  Result = CGF.Builder.CreateBinOp(Op, Result, Args[1]);
141  Result = EmitFromInt(CGF, Result, T, ValueType);
142  return RValue::get(Result);
143}
144
145/// EmitFAbs - Emit a call to fabs/fabsf/fabsl, depending on the type of ValTy,
146/// which must be a scalar floating point type.
147static Value *EmitFAbs(CodeGenFunction &CGF, Value *V, QualType ValTy) {
148  const BuiltinType *ValTyP = ValTy->getAs<BuiltinType>();
149  assert(ValTyP && "isn't scalar fp type!");
150
151  StringRef FnName;
152  switch (ValTyP->getKind()) {
153  default: llvm_unreachable("Isn't a scalar fp type!");
154  case BuiltinType::Float:      FnName = "fabsf"; break;
155  case BuiltinType::Double:     FnName = "fabs"; break;
156  case BuiltinType::LongDouble: FnName = "fabsl"; break;
157  }
158
159  // The prototype is something that takes and returns whatever V's type is.
160  llvm::FunctionType *FT = llvm::FunctionType::get(V->getType(), V->getType(),
161                                                   false);
162  llvm::Value *Fn = CGF.CGM.CreateRuntimeFunction(FT, FnName);
163
164  return CGF.EmitNounwindRuntimeCall(Fn, V, "abs");
165}
166
167static RValue emitLibraryCall(CodeGenFunction &CGF, const FunctionDecl *Fn,
168                              const CallExpr *E, llvm::Value *calleeValue) {
169  return CGF.EmitCall(E->getCallee()->getType(), calleeValue, E->getLocStart(),
170                      ReturnValueSlot(), E->arg_begin(), E->arg_end(), Fn);
171}
172
173/// \brief Emit a call to llvm.{sadd,uadd,ssub,usub,smul,umul}.with.overflow.*
174/// depending on IntrinsicID.
175///
176/// \arg CGF The current codegen function.
177/// \arg IntrinsicID The ID for the Intrinsic we wish to generate.
178/// \arg X The first argument to the llvm.*.with.overflow.*.
179/// \arg Y The second argument to the llvm.*.with.overflow.*.
180/// \arg Carry The carry returned by the llvm.*.with.overflow.*.
181/// \returns The result (i.e. sum/product) returned by the intrinsic.
182static llvm::Value *EmitOverflowIntrinsic(CodeGenFunction &CGF,
183                                          const llvm::Intrinsic::ID IntrinsicID,
184                                          llvm::Value *X, llvm::Value *Y,
185                                          llvm::Value *&Carry) {
186  // Make sure we have integers of the same width.
187  assert(X->getType() == Y->getType() &&
188         "Arguments must be the same type. (Did you forget to make sure both "
189         "arguments have the same integer width?)");
190
191  llvm::Value *Callee = CGF.CGM.getIntrinsic(IntrinsicID, X->getType());
192  llvm::Value *Tmp = CGF.Builder.CreateCall2(Callee, X, Y);
193  Carry = CGF.Builder.CreateExtractValue(Tmp, 1);
194  return CGF.Builder.CreateExtractValue(Tmp, 0);
195}
196
197RValue CodeGenFunction::EmitBuiltinExpr(const FunctionDecl *FD,
198                                        unsigned BuiltinID, const CallExpr *E) {
199  // See if we can constant fold this builtin.  If so, don't emit it at all.
200  Expr::EvalResult Result;
201  if (E->EvaluateAsRValue(Result, CGM.getContext()) &&
202      !Result.hasSideEffects()) {
203    if (Result.Val.isInt())
204      return RValue::get(llvm::ConstantInt::get(getLLVMContext(),
205                                                Result.Val.getInt()));
206    if (Result.Val.isFloat())
207      return RValue::get(llvm::ConstantFP::get(getLLVMContext(),
208                                               Result.Val.getFloat()));
209  }
210
211  switch (BuiltinID) {
212  default: break;  // Handle intrinsics and libm functions below.
213  case Builtin::BI__builtin___CFStringMakeConstantString:
214  case Builtin::BI__builtin___NSStringMakeConstantString:
215    return RValue::get(CGM.EmitConstantExpr(E, E->getType(), nullptr));
216  case Builtin::BI__builtin_stdarg_start:
217  case Builtin::BI__builtin_va_start:
218  case Builtin::BI__va_start:
219  case Builtin::BI__builtin_va_end: {
220    Value *ArgValue = (BuiltinID == Builtin::BI__va_start)
221                          ? EmitScalarExpr(E->getArg(0))
222                          : EmitVAListRef(E->getArg(0));
223    llvm::Type *DestType = Int8PtrTy;
224    if (ArgValue->getType() != DestType)
225      ArgValue = Builder.CreateBitCast(ArgValue, DestType,
226                                       ArgValue->getName().data());
227
228    Intrinsic::ID inst = (BuiltinID == Builtin::BI__builtin_va_end) ?
229      Intrinsic::vaend : Intrinsic::vastart;
230    return RValue::get(Builder.CreateCall(CGM.getIntrinsic(inst), ArgValue));
231  }
232  case Builtin::BI__builtin_va_copy: {
233    Value *DstPtr = EmitVAListRef(E->getArg(0));
234    Value *SrcPtr = EmitVAListRef(E->getArg(1));
235
236    llvm::Type *Type = Int8PtrTy;
237
238    DstPtr = Builder.CreateBitCast(DstPtr, Type);
239    SrcPtr = Builder.CreateBitCast(SrcPtr, Type);
240    return RValue::get(Builder.CreateCall2(CGM.getIntrinsic(Intrinsic::vacopy),
241                                           DstPtr, SrcPtr));
242  }
243  case Builtin::BI__builtin_abs:
244  case Builtin::BI__builtin_labs:
245  case Builtin::BI__builtin_llabs: {
246    Value *ArgValue = EmitScalarExpr(E->getArg(0));
247
248    Value *NegOp = Builder.CreateNeg(ArgValue, "neg");
249    Value *CmpResult =
250    Builder.CreateICmpSGE(ArgValue,
251                          llvm::Constant::getNullValue(ArgValue->getType()),
252                                                            "abscond");
253    Value *Result =
254      Builder.CreateSelect(CmpResult, ArgValue, NegOp, "abs");
255
256    return RValue::get(Result);
257  }
258
259  case Builtin::BI__builtin_conj:
260  case Builtin::BI__builtin_conjf:
261  case Builtin::BI__builtin_conjl: {
262    ComplexPairTy ComplexVal = EmitComplexExpr(E->getArg(0));
263    Value *Real = ComplexVal.first;
264    Value *Imag = ComplexVal.second;
265    Value *Zero =
266      Imag->getType()->isFPOrFPVectorTy()
267        ? llvm::ConstantFP::getZeroValueForNegation(Imag->getType())
268        : llvm::Constant::getNullValue(Imag->getType());
269
270    Imag = Builder.CreateFSub(Zero, Imag, "sub");
271    return RValue::getComplex(std::make_pair(Real, Imag));
272  }
273  case Builtin::BI__builtin_creal:
274  case Builtin::BI__builtin_crealf:
275  case Builtin::BI__builtin_creall:
276  case Builtin::BIcreal:
277  case Builtin::BIcrealf:
278  case Builtin::BIcreall: {
279    ComplexPairTy ComplexVal = EmitComplexExpr(E->getArg(0));
280    return RValue::get(ComplexVal.first);
281  }
282
283  case Builtin::BI__builtin_cimag:
284  case Builtin::BI__builtin_cimagf:
285  case Builtin::BI__builtin_cimagl:
286  case Builtin::BIcimag:
287  case Builtin::BIcimagf:
288  case Builtin::BIcimagl: {
289    ComplexPairTy ComplexVal = EmitComplexExpr(E->getArg(0));
290    return RValue::get(ComplexVal.second);
291  }
292
293  case Builtin::BI__builtin_ctzs:
294  case Builtin::BI__builtin_ctz:
295  case Builtin::BI__builtin_ctzl:
296  case Builtin::BI__builtin_ctzll: {
297    Value *ArgValue = EmitScalarExpr(E->getArg(0));
298
299    llvm::Type *ArgType = ArgValue->getType();
300    Value *F = CGM.getIntrinsic(Intrinsic::cttz, ArgType);
301
302    llvm::Type *ResultType = ConvertType(E->getType());
303    Value *ZeroUndef = Builder.getInt1(getTarget().isCLZForZeroUndef());
304    Value *Result = Builder.CreateCall2(F, ArgValue, ZeroUndef);
305    if (Result->getType() != ResultType)
306      Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true,
307                                     "cast");
308    return RValue::get(Result);
309  }
310  case Builtin::BI__builtin_clzs:
311  case Builtin::BI__builtin_clz:
312  case Builtin::BI__builtin_clzl:
313  case Builtin::BI__builtin_clzll: {
314    Value *ArgValue = EmitScalarExpr(E->getArg(0));
315
316    llvm::Type *ArgType = ArgValue->getType();
317    Value *F = CGM.getIntrinsic(Intrinsic::ctlz, ArgType);
318
319    llvm::Type *ResultType = ConvertType(E->getType());
320    Value *ZeroUndef = Builder.getInt1(getTarget().isCLZForZeroUndef());
321    Value *Result = Builder.CreateCall2(F, ArgValue, ZeroUndef);
322    if (Result->getType() != ResultType)
323      Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true,
324                                     "cast");
325    return RValue::get(Result);
326  }
327  case Builtin::BI__builtin_ffs:
328  case Builtin::BI__builtin_ffsl:
329  case Builtin::BI__builtin_ffsll: {
330    // ffs(x) -> x ? cttz(x) + 1 : 0
331    Value *ArgValue = EmitScalarExpr(E->getArg(0));
332
333    llvm::Type *ArgType = ArgValue->getType();
334    Value *F = CGM.getIntrinsic(Intrinsic::cttz, ArgType);
335
336    llvm::Type *ResultType = ConvertType(E->getType());
337    Value *Tmp = Builder.CreateAdd(Builder.CreateCall2(F, ArgValue,
338                                                       Builder.getTrue()),
339                                   llvm::ConstantInt::get(ArgType, 1));
340    Value *Zero = llvm::Constant::getNullValue(ArgType);
341    Value *IsZero = Builder.CreateICmpEQ(ArgValue, Zero, "iszero");
342    Value *Result = Builder.CreateSelect(IsZero, Zero, Tmp, "ffs");
343    if (Result->getType() != ResultType)
344      Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true,
345                                     "cast");
346    return RValue::get(Result);
347  }
348  case Builtin::BI__builtin_parity:
349  case Builtin::BI__builtin_parityl:
350  case Builtin::BI__builtin_parityll: {
351    // parity(x) -> ctpop(x) & 1
352    Value *ArgValue = EmitScalarExpr(E->getArg(0));
353
354    llvm::Type *ArgType = ArgValue->getType();
355    Value *F = CGM.getIntrinsic(Intrinsic::ctpop, ArgType);
356
357    llvm::Type *ResultType = ConvertType(E->getType());
358    Value *Tmp = Builder.CreateCall(F, ArgValue);
359    Value *Result = Builder.CreateAnd(Tmp, llvm::ConstantInt::get(ArgType, 1));
360    if (Result->getType() != ResultType)
361      Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true,
362                                     "cast");
363    return RValue::get(Result);
364  }
365  case Builtin::BI__builtin_popcount:
366  case Builtin::BI__builtin_popcountl:
367  case Builtin::BI__builtin_popcountll: {
368    Value *ArgValue = EmitScalarExpr(E->getArg(0));
369
370    llvm::Type *ArgType = ArgValue->getType();
371    Value *F = CGM.getIntrinsic(Intrinsic::ctpop, ArgType);
372
373    llvm::Type *ResultType = ConvertType(E->getType());
374    Value *Result = Builder.CreateCall(F, ArgValue);
375    if (Result->getType() != ResultType)
376      Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true,
377                                     "cast");
378    return RValue::get(Result);
379  }
380  case Builtin::BI__builtin_expect: {
381    Value *ArgValue = EmitScalarExpr(E->getArg(0));
382    llvm::Type *ArgType = ArgValue->getType();
383
384    Value *FnExpect = CGM.getIntrinsic(Intrinsic::expect, ArgType);
385    Value *ExpectedValue = EmitScalarExpr(E->getArg(1));
386
387    Value *Result = Builder.CreateCall2(FnExpect, ArgValue, ExpectedValue,
388                                        "expval");
389    return RValue::get(Result);
390  }
391  case Builtin::BI__builtin_bswap16:
392  case Builtin::BI__builtin_bswap32:
393  case Builtin::BI__builtin_bswap64: {
394    Value *ArgValue = EmitScalarExpr(E->getArg(0));
395    llvm::Type *ArgType = ArgValue->getType();
396    Value *F = CGM.getIntrinsic(Intrinsic::bswap, ArgType);
397    return RValue::get(Builder.CreateCall(F, ArgValue));
398  }
399  case Builtin::BI__builtin_object_size: {
400    // We rely on constant folding to deal with expressions with side effects.
401    assert(!E->getArg(0)->HasSideEffects(getContext()) &&
402           "should have been constant folded");
403
404    // We pass this builtin onto the optimizer so that it can
405    // figure out the object size in more complex cases.
406    llvm::Type *ResType = ConvertType(E->getType());
407
408    // LLVM only supports 0 and 2, make sure that we pass along that
409    // as a boolean.
410    Value *Ty = EmitScalarExpr(E->getArg(1));
411    ConstantInt *CI = dyn_cast<ConstantInt>(Ty);
412    assert(CI);
413    uint64_t val = CI->getZExtValue();
414    CI = ConstantInt::get(Builder.getInt1Ty(), (val & 0x2) >> 1);
415    // FIXME: Get right address space.
416    llvm::Type *Tys[] = { ResType, Builder.getInt8PtrTy(0) };
417    Value *F = CGM.getIntrinsic(Intrinsic::objectsize, Tys);
418    return RValue::get(Builder.CreateCall2(F, EmitScalarExpr(E->getArg(0)),CI));
419  }
420  case Builtin::BI__builtin_prefetch: {
421    Value *Locality, *RW, *Address = EmitScalarExpr(E->getArg(0));
422    // FIXME: Technically these constants should of type 'int', yes?
423    RW = (E->getNumArgs() > 1) ? EmitScalarExpr(E->getArg(1)) :
424      llvm::ConstantInt::get(Int32Ty, 0);
425    Locality = (E->getNumArgs() > 2) ? EmitScalarExpr(E->getArg(2)) :
426      llvm::ConstantInt::get(Int32Ty, 3);
427    Value *Data = llvm::ConstantInt::get(Int32Ty, 1);
428    Value *F = CGM.getIntrinsic(Intrinsic::prefetch);
429    return RValue::get(Builder.CreateCall4(F, Address, RW, Locality, Data));
430  }
431  case Builtin::BI__builtin_readcyclecounter: {
432    Value *F = CGM.getIntrinsic(Intrinsic::readcyclecounter);
433    return RValue::get(Builder.CreateCall(F));
434  }
435  case Builtin::BI__builtin___clear_cache: {
436    Value *Begin = EmitScalarExpr(E->getArg(0));
437    Value *End = EmitScalarExpr(E->getArg(1));
438    Value *F = CGM.getIntrinsic(Intrinsic::clear_cache);
439    return RValue::get(Builder.CreateCall2(F, Begin, End));
440  }
441  case Builtin::BI__builtin_trap: {
442    Value *F = CGM.getIntrinsic(Intrinsic::trap);
443    return RValue::get(Builder.CreateCall(F));
444  }
445  case Builtin::BI__debugbreak: {
446    Value *F = CGM.getIntrinsic(Intrinsic::debugtrap);
447    return RValue::get(Builder.CreateCall(F));
448  }
449  case Builtin::BI__builtin_unreachable: {
450    if (SanOpts->Unreachable)
451      EmitCheck(Builder.getFalse(), "builtin_unreachable",
452                EmitCheckSourceLocation(E->getExprLoc()),
453                ArrayRef<llvm::Value *>(), CRK_Unrecoverable);
454    else
455      Builder.CreateUnreachable();
456
457    // We do need to preserve an insertion point.
458    EmitBlock(createBasicBlock("unreachable.cont"));
459
460    return RValue::get(nullptr);
461  }
462
463  case Builtin::BI__builtin_powi:
464  case Builtin::BI__builtin_powif:
465  case Builtin::BI__builtin_powil: {
466    Value *Base = EmitScalarExpr(E->getArg(0));
467    Value *Exponent = EmitScalarExpr(E->getArg(1));
468    llvm::Type *ArgType = Base->getType();
469    Value *F = CGM.getIntrinsic(Intrinsic::powi, ArgType);
470    return RValue::get(Builder.CreateCall2(F, Base, Exponent));
471  }
472
473  case Builtin::BI__builtin_isgreater:
474  case Builtin::BI__builtin_isgreaterequal:
475  case Builtin::BI__builtin_isless:
476  case Builtin::BI__builtin_islessequal:
477  case Builtin::BI__builtin_islessgreater:
478  case Builtin::BI__builtin_isunordered: {
479    // Ordered comparisons: we know the arguments to these are matching scalar
480    // floating point values.
481    Value *LHS = EmitScalarExpr(E->getArg(0));
482    Value *RHS = EmitScalarExpr(E->getArg(1));
483
484    switch (BuiltinID) {
485    default: llvm_unreachable("Unknown ordered comparison");
486    case Builtin::BI__builtin_isgreater:
487      LHS = Builder.CreateFCmpOGT(LHS, RHS, "cmp");
488      break;
489    case Builtin::BI__builtin_isgreaterequal:
490      LHS = Builder.CreateFCmpOGE(LHS, RHS, "cmp");
491      break;
492    case Builtin::BI__builtin_isless:
493      LHS = Builder.CreateFCmpOLT(LHS, RHS, "cmp");
494      break;
495    case Builtin::BI__builtin_islessequal:
496      LHS = Builder.CreateFCmpOLE(LHS, RHS, "cmp");
497      break;
498    case Builtin::BI__builtin_islessgreater:
499      LHS = Builder.CreateFCmpONE(LHS, RHS, "cmp");
500      break;
501    case Builtin::BI__builtin_isunordered:
502      LHS = Builder.CreateFCmpUNO(LHS, RHS, "cmp");
503      break;
504    }
505    // ZExt bool to int type.
506    return RValue::get(Builder.CreateZExt(LHS, ConvertType(E->getType())));
507  }
508  case Builtin::BI__builtin_isnan: {
509    Value *V = EmitScalarExpr(E->getArg(0));
510    V = Builder.CreateFCmpUNO(V, V, "cmp");
511    return RValue::get(Builder.CreateZExt(V, ConvertType(E->getType())));
512  }
513
514  case Builtin::BI__builtin_isinf: {
515    // isinf(x) --> fabs(x) == infinity
516    Value *V = EmitScalarExpr(E->getArg(0));
517    V = EmitFAbs(*this, V, E->getArg(0)->getType());
518
519    V = Builder.CreateFCmpOEQ(V, ConstantFP::getInfinity(V->getType()),"isinf");
520    return RValue::get(Builder.CreateZExt(V, ConvertType(E->getType())));
521  }
522
523  // TODO: BI__builtin_isinf_sign
524  //   isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0
525
526  case Builtin::BI__builtin_isnormal: {
527    // isnormal(x) --> x == x && fabsf(x) < infinity && fabsf(x) >= float_min
528    Value *V = EmitScalarExpr(E->getArg(0));
529    Value *Eq = Builder.CreateFCmpOEQ(V, V, "iseq");
530
531    Value *Abs = EmitFAbs(*this, V, E->getArg(0)->getType());
532    Value *IsLessThanInf =
533      Builder.CreateFCmpULT(Abs, ConstantFP::getInfinity(V->getType()),"isinf");
534    APFloat Smallest = APFloat::getSmallestNormalized(
535                   getContext().getFloatTypeSemantics(E->getArg(0)->getType()));
536    Value *IsNormal =
537      Builder.CreateFCmpUGE(Abs, ConstantFP::get(V->getContext(), Smallest),
538                            "isnormal");
539    V = Builder.CreateAnd(Eq, IsLessThanInf, "and");
540    V = Builder.CreateAnd(V, IsNormal, "and");
541    return RValue::get(Builder.CreateZExt(V, ConvertType(E->getType())));
542  }
543
544  case Builtin::BI__builtin_isfinite: {
545    // isfinite(x) --> x == x && fabs(x) != infinity;
546    Value *V = EmitScalarExpr(E->getArg(0));
547    Value *Eq = Builder.CreateFCmpOEQ(V, V, "iseq");
548
549    Value *Abs = EmitFAbs(*this, V, E->getArg(0)->getType());
550    Value *IsNotInf =
551      Builder.CreateFCmpUNE(Abs, ConstantFP::getInfinity(V->getType()),"isinf");
552
553    V = Builder.CreateAnd(Eq, IsNotInf, "and");
554    return RValue::get(Builder.CreateZExt(V, ConvertType(E->getType())));
555  }
556
557  case Builtin::BI__builtin_fpclassify: {
558    Value *V = EmitScalarExpr(E->getArg(5));
559    llvm::Type *Ty = ConvertType(E->getArg(5)->getType());
560
561    // Create Result
562    BasicBlock *Begin = Builder.GetInsertBlock();
563    BasicBlock *End = createBasicBlock("fpclassify_end", this->CurFn);
564    Builder.SetInsertPoint(End);
565    PHINode *Result =
566      Builder.CreatePHI(ConvertType(E->getArg(0)->getType()), 4,
567                        "fpclassify_result");
568
569    // if (V==0) return FP_ZERO
570    Builder.SetInsertPoint(Begin);
571    Value *IsZero = Builder.CreateFCmpOEQ(V, Constant::getNullValue(Ty),
572                                          "iszero");
573    Value *ZeroLiteral = EmitScalarExpr(E->getArg(4));
574    BasicBlock *NotZero = createBasicBlock("fpclassify_not_zero", this->CurFn);
575    Builder.CreateCondBr(IsZero, End, NotZero);
576    Result->addIncoming(ZeroLiteral, Begin);
577
578    // if (V != V) return FP_NAN
579    Builder.SetInsertPoint(NotZero);
580    Value *IsNan = Builder.CreateFCmpUNO(V, V, "cmp");
581    Value *NanLiteral = EmitScalarExpr(E->getArg(0));
582    BasicBlock *NotNan = createBasicBlock("fpclassify_not_nan", this->CurFn);
583    Builder.CreateCondBr(IsNan, End, NotNan);
584    Result->addIncoming(NanLiteral, NotZero);
585
586    // if (fabs(V) == infinity) return FP_INFINITY
587    Builder.SetInsertPoint(NotNan);
588    Value *VAbs = EmitFAbs(*this, V, E->getArg(5)->getType());
589    Value *IsInf =
590      Builder.CreateFCmpOEQ(VAbs, ConstantFP::getInfinity(V->getType()),
591                            "isinf");
592    Value *InfLiteral = EmitScalarExpr(E->getArg(1));
593    BasicBlock *NotInf = createBasicBlock("fpclassify_not_inf", this->CurFn);
594    Builder.CreateCondBr(IsInf, End, NotInf);
595    Result->addIncoming(InfLiteral, NotNan);
596
597    // if (fabs(V) >= MIN_NORMAL) return FP_NORMAL else FP_SUBNORMAL
598    Builder.SetInsertPoint(NotInf);
599    APFloat Smallest = APFloat::getSmallestNormalized(
600        getContext().getFloatTypeSemantics(E->getArg(5)->getType()));
601    Value *IsNormal =
602      Builder.CreateFCmpUGE(VAbs, ConstantFP::get(V->getContext(), Smallest),
603                            "isnormal");
604    Value *NormalResult =
605      Builder.CreateSelect(IsNormal, EmitScalarExpr(E->getArg(2)),
606                           EmitScalarExpr(E->getArg(3)));
607    Builder.CreateBr(End);
608    Result->addIncoming(NormalResult, NotInf);
609
610    // return Result
611    Builder.SetInsertPoint(End);
612    return RValue::get(Result);
613  }
614
615  case Builtin::BIalloca:
616  case Builtin::BI_alloca:
617  case Builtin::BI__builtin_alloca: {
618    Value *Size = EmitScalarExpr(E->getArg(0));
619    return RValue::get(Builder.CreateAlloca(Builder.getInt8Ty(), Size));
620  }
621  case Builtin::BIbzero:
622  case Builtin::BI__builtin_bzero: {
623    std::pair<llvm::Value*, unsigned> Dest =
624        EmitPointerWithAlignment(E->getArg(0));
625    Value *SizeVal = EmitScalarExpr(E->getArg(1));
626    Builder.CreateMemSet(Dest.first, Builder.getInt8(0), SizeVal,
627                         Dest.second, false);
628    return RValue::get(Dest.first);
629  }
630  case Builtin::BImemcpy:
631  case Builtin::BI__builtin_memcpy: {
632    std::pair<llvm::Value*, unsigned> Dest =
633        EmitPointerWithAlignment(E->getArg(0));
634    std::pair<llvm::Value*, unsigned> Src =
635        EmitPointerWithAlignment(E->getArg(1));
636    Value *SizeVal = EmitScalarExpr(E->getArg(2));
637    unsigned Align = std::min(Dest.second, Src.second);
638    Builder.CreateMemCpy(Dest.first, Src.first, SizeVal, Align, false);
639    return RValue::get(Dest.first);
640  }
641
642  case Builtin::BI__builtin___memcpy_chk: {
643    // fold __builtin_memcpy_chk(x, y, cst1, cst2) to memcpy iff cst1<=cst2.
644    llvm::APSInt Size, DstSize;
645    if (!E->getArg(2)->EvaluateAsInt(Size, CGM.getContext()) ||
646        !E->getArg(3)->EvaluateAsInt(DstSize, CGM.getContext()))
647      break;
648    if (Size.ugt(DstSize))
649      break;
650    std::pair<llvm::Value*, unsigned> Dest =
651        EmitPointerWithAlignment(E->getArg(0));
652    std::pair<llvm::Value*, unsigned> Src =
653        EmitPointerWithAlignment(E->getArg(1));
654    Value *SizeVal = llvm::ConstantInt::get(Builder.getContext(), Size);
655    unsigned Align = std::min(Dest.second, Src.second);
656    Builder.CreateMemCpy(Dest.first, Src.first, SizeVal, Align, false);
657    return RValue::get(Dest.first);
658  }
659
660  case Builtin::BI__builtin_objc_memmove_collectable: {
661    Value *Address = EmitScalarExpr(E->getArg(0));
662    Value *SrcAddr = EmitScalarExpr(E->getArg(1));
663    Value *SizeVal = EmitScalarExpr(E->getArg(2));
664    CGM.getObjCRuntime().EmitGCMemmoveCollectable(*this,
665                                                  Address, SrcAddr, SizeVal);
666    return RValue::get(Address);
667  }
668
669  case Builtin::BI__builtin___memmove_chk: {
670    // fold __builtin_memmove_chk(x, y, cst1, cst2) to memmove iff cst1<=cst2.
671    llvm::APSInt Size, DstSize;
672    if (!E->getArg(2)->EvaluateAsInt(Size, CGM.getContext()) ||
673        !E->getArg(3)->EvaluateAsInt(DstSize, CGM.getContext()))
674      break;
675    if (Size.ugt(DstSize))
676      break;
677    std::pair<llvm::Value*, unsigned> Dest =
678        EmitPointerWithAlignment(E->getArg(0));
679    std::pair<llvm::Value*, unsigned> Src =
680        EmitPointerWithAlignment(E->getArg(1));
681    Value *SizeVal = llvm::ConstantInt::get(Builder.getContext(), Size);
682    unsigned Align = std::min(Dest.second, Src.second);
683    Builder.CreateMemMove(Dest.first, Src.first, SizeVal, Align, false);
684    return RValue::get(Dest.first);
685  }
686
687  case Builtin::BImemmove:
688  case Builtin::BI__builtin_memmove: {
689    std::pair<llvm::Value*, unsigned> Dest =
690        EmitPointerWithAlignment(E->getArg(0));
691    std::pair<llvm::Value*, unsigned> Src =
692        EmitPointerWithAlignment(E->getArg(1));
693    Value *SizeVal = EmitScalarExpr(E->getArg(2));
694    unsigned Align = std::min(Dest.second, Src.second);
695    Builder.CreateMemMove(Dest.first, Src.first, SizeVal, Align, false);
696    return RValue::get(Dest.first);
697  }
698  case Builtin::BImemset:
699  case Builtin::BI__builtin_memset: {
700    std::pair<llvm::Value*, unsigned> Dest =
701        EmitPointerWithAlignment(E->getArg(0));
702    Value *ByteVal = Builder.CreateTrunc(EmitScalarExpr(E->getArg(1)),
703                                         Builder.getInt8Ty());
704    Value *SizeVal = EmitScalarExpr(E->getArg(2));
705    Builder.CreateMemSet(Dest.first, ByteVal, SizeVal, Dest.second, false);
706    return RValue::get(Dest.first);
707  }
708  case Builtin::BI__builtin___memset_chk: {
709    // fold __builtin_memset_chk(x, y, cst1, cst2) to memset iff cst1<=cst2.
710    llvm::APSInt Size, DstSize;
711    if (!E->getArg(2)->EvaluateAsInt(Size, CGM.getContext()) ||
712        !E->getArg(3)->EvaluateAsInt(DstSize, CGM.getContext()))
713      break;
714    if (Size.ugt(DstSize))
715      break;
716    std::pair<llvm::Value*, unsigned> Dest =
717        EmitPointerWithAlignment(E->getArg(0));
718    Value *ByteVal = Builder.CreateTrunc(EmitScalarExpr(E->getArg(1)),
719                                         Builder.getInt8Ty());
720    Value *SizeVal = llvm::ConstantInt::get(Builder.getContext(), Size);
721    Builder.CreateMemSet(Dest.first, ByteVal, SizeVal, Dest.second, false);
722    return RValue::get(Dest.first);
723  }
724  case Builtin::BI__builtin_dwarf_cfa: {
725    // The offset in bytes from the first argument to the CFA.
726    //
727    // Why on earth is this in the frontend?  Is there any reason at
728    // all that the backend can't reasonably determine this while
729    // lowering llvm.eh.dwarf.cfa()?
730    //
731    // TODO: If there's a satisfactory reason, add a target hook for
732    // this instead of hard-coding 0, which is correct for most targets.
733    int32_t Offset = 0;
734
735    Value *F = CGM.getIntrinsic(Intrinsic::eh_dwarf_cfa);
736    return RValue::get(Builder.CreateCall(F,
737                                      llvm::ConstantInt::get(Int32Ty, Offset)));
738  }
739  case Builtin::BI__builtin_return_address: {
740    Value *Depth = EmitScalarExpr(E->getArg(0));
741    Depth = Builder.CreateIntCast(Depth, Int32Ty, false);
742    Value *F = CGM.getIntrinsic(Intrinsic::returnaddress);
743    return RValue::get(Builder.CreateCall(F, Depth));
744  }
745  case Builtin::BI__builtin_frame_address: {
746    Value *Depth = EmitScalarExpr(E->getArg(0));
747    Depth = Builder.CreateIntCast(Depth, Int32Ty, false);
748    Value *F = CGM.getIntrinsic(Intrinsic::frameaddress);
749    return RValue::get(Builder.CreateCall(F, Depth));
750  }
751  case Builtin::BI__builtin_extract_return_addr: {
752    Value *Address = EmitScalarExpr(E->getArg(0));
753    Value *Result = getTargetHooks().decodeReturnAddress(*this, Address);
754    return RValue::get(Result);
755  }
756  case Builtin::BI__builtin_frob_return_addr: {
757    Value *Address = EmitScalarExpr(E->getArg(0));
758    Value *Result = getTargetHooks().encodeReturnAddress(*this, Address);
759    return RValue::get(Result);
760  }
761  case Builtin::BI__builtin_dwarf_sp_column: {
762    llvm::IntegerType *Ty
763      = cast<llvm::IntegerType>(ConvertType(E->getType()));
764    int Column = getTargetHooks().getDwarfEHStackPointer(CGM);
765    if (Column == -1) {
766      CGM.ErrorUnsupported(E, "__builtin_dwarf_sp_column");
767      return RValue::get(llvm::UndefValue::get(Ty));
768    }
769    return RValue::get(llvm::ConstantInt::get(Ty, Column, true));
770  }
771  case Builtin::BI__builtin_init_dwarf_reg_size_table: {
772    Value *Address = EmitScalarExpr(E->getArg(0));
773    if (getTargetHooks().initDwarfEHRegSizeTable(*this, Address))
774      CGM.ErrorUnsupported(E, "__builtin_init_dwarf_reg_size_table");
775    return RValue::get(llvm::UndefValue::get(ConvertType(E->getType())));
776  }
777  case Builtin::BI__builtin_eh_return: {
778    Value *Int = EmitScalarExpr(E->getArg(0));
779    Value *Ptr = EmitScalarExpr(E->getArg(1));
780
781    llvm::IntegerType *IntTy = cast<llvm::IntegerType>(Int->getType());
782    assert((IntTy->getBitWidth() == 32 || IntTy->getBitWidth() == 64) &&
783           "LLVM's __builtin_eh_return only supports 32- and 64-bit variants");
784    Value *F = CGM.getIntrinsic(IntTy->getBitWidth() == 32
785                                  ? Intrinsic::eh_return_i32
786                                  : Intrinsic::eh_return_i64);
787    Builder.CreateCall2(F, Int, Ptr);
788    Builder.CreateUnreachable();
789
790    // We do need to preserve an insertion point.
791    EmitBlock(createBasicBlock("builtin_eh_return.cont"));
792
793    return RValue::get(nullptr);
794  }
795  case Builtin::BI__builtin_unwind_init: {
796    Value *F = CGM.getIntrinsic(Intrinsic::eh_unwind_init);
797    return RValue::get(Builder.CreateCall(F));
798  }
799  case Builtin::BI__builtin_extend_pointer: {
800    // Extends a pointer to the size of an _Unwind_Word, which is
801    // uint64_t on all platforms.  Generally this gets poked into a
802    // register and eventually used as an address, so if the
803    // addressing registers are wider than pointers and the platform
804    // doesn't implicitly ignore high-order bits when doing
805    // addressing, we need to make sure we zext / sext based on
806    // the platform's expectations.
807    //
808    // See: http://gcc.gnu.org/ml/gcc-bugs/2002-02/msg00237.html
809
810    // Cast the pointer to intptr_t.
811    Value *Ptr = EmitScalarExpr(E->getArg(0));
812    Value *Result = Builder.CreatePtrToInt(Ptr, IntPtrTy, "extend.cast");
813
814    // If that's 64 bits, we're done.
815    if (IntPtrTy->getBitWidth() == 64)
816      return RValue::get(Result);
817
818    // Otherwise, ask the codegen data what to do.
819    if (getTargetHooks().extendPointerWithSExt())
820      return RValue::get(Builder.CreateSExt(Result, Int64Ty, "extend.sext"));
821    else
822      return RValue::get(Builder.CreateZExt(Result, Int64Ty, "extend.zext"));
823  }
824  case Builtin::BI__builtin_setjmp: {
825    // Buffer is a void**.
826    Value *Buf = EmitScalarExpr(E->getArg(0));
827
828    // Store the frame pointer to the setjmp buffer.
829    Value *FrameAddr =
830      Builder.CreateCall(CGM.getIntrinsic(Intrinsic::frameaddress),
831                         ConstantInt::get(Int32Ty, 0));
832    Builder.CreateStore(FrameAddr, Buf);
833
834    // Store the stack pointer to the setjmp buffer.
835    Value *StackAddr =
836      Builder.CreateCall(CGM.getIntrinsic(Intrinsic::stacksave));
837    Value *StackSaveSlot =
838      Builder.CreateGEP(Buf, ConstantInt::get(Int32Ty, 2));
839    Builder.CreateStore(StackAddr, StackSaveSlot);
840
841    // Call LLVM's EH setjmp, which is lightweight.
842    Value *F = CGM.getIntrinsic(Intrinsic::eh_sjlj_setjmp);
843    Buf = Builder.CreateBitCast(Buf, Int8PtrTy);
844    return RValue::get(Builder.CreateCall(F, Buf));
845  }
846  case Builtin::BI__builtin_longjmp: {
847    Value *Buf = EmitScalarExpr(E->getArg(0));
848    Buf = Builder.CreateBitCast(Buf, Int8PtrTy);
849
850    // Call LLVM's EH longjmp, which is lightweight.
851    Builder.CreateCall(CGM.getIntrinsic(Intrinsic::eh_sjlj_longjmp), Buf);
852
853    // longjmp doesn't return; mark this as unreachable.
854    Builder.CreateUnreachable();
855
856    // We do need to preserve an insertion point.
857    EmitBlock(createBasicBlock("longjmp.cont"));
858
859    return RValue::get(nullptr);
860  }
861  case Builtin::BI__sync_fetch_and_add:
862  case Builtin::BI__sync_fetch_and_sub:
863  case Builtin::BI__sync_fetch_and_or:
864  case Builtin::BI__sync_fetch_and_and:
865  case Builtin::BI__sync_fetch_and_xor:
866  case Builtin::BI__sync_add_and_fetch:
867  case Builtin::BI__sync_sub_and_fetch:
868  case Builtin::BI__sync_and_and_fetch:
869  case Builtin::BI__sync_or_and_fetch:
870  case Builtin::BI__sync_xor_and_fetch:
871  case Builtin::BI__sync_val_compare_and_swap:
872  case Builtin::BI__sync_bool_compare_and_swap:
873  case Builtin::BI__sync_lock_test_and_set:
874  case Builtin::BI__sync_lock_release:
875  case Builtin::BI__sync_swap:
876    llvm_unreachable("Shouldn't make it through sema");
877  case Builtin::BI__sync_fetch_and_add_1:
878  case Builtin::BI__sync_fetch_and_add_2:
879  case Builtin::BI__sync_fetch_and_add_4:
880  case Builtin::BI__sync_fetch_and_add_8:
881  case Builtin::BI__sync_fetch_and_add_16:
882    return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::Add, E);
883  case Builtin::BI__sync_fetch_and_sub_1:
884  case Builtin::BI__sync_fetch_and_sub_2:
885  case Builtin::BI__sync_fetch_and_sub_4:
886  case Builtin::BI__sync_fetch_and_sub_8:
887  case Builtin::BI__sync_fetch_and_sub_16:
888    return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::Sub, E);
889  case Builtin::BI__sync_fetch_and_or_1:
890  case Builtin::BI__sync_fetch_and_or_2:
891  case Builtin::BI__sync_fetch_and_or_4:
892  case Builtin::BI__sync_fetch_and_or_8:
893  case Builtin::BI__sync_fetch_and_or_16:
894    return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::Or, E);
895  case Builtin::BI__sync_fetch_and_and_1:
896  case Builtin::BI__sync_fetch_and_and_2:
897  case Builtin::BI__sync_fetch_and_and_4:
898  case Builtin::BI__sync_fetch_and_and_8:
899  case Builtin::BI__sync_fetch_and_and_16:
900    return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::And, E);
901  case Builtin::BI__sync_fetch_and_xor_1:
902  case Builtin::BI__sync_fetch_and_xor_2:
903  case Builtin::BI__sync_fetch_and_xor_4:
904  case Builtin::BI__sync_fetch_and_xor_8:
905  case Builtin::BI__sync_fetch_and_xor_16:
906    return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::Xor, E);
907
908  // Clang extensions: not overloaded yet.
909  case Builtin::BI__sync_fetch_and_min:
910    return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::Min, E);
911  case Builtin::BI__sync_fetch_and_max:
912    return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::Max, E);
913  case Builtin::BI__sync_fetch_and_umin:
914    return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::UMin, E);
915  case Builtin::BI__sync_fetch_and_umax:
916    return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::UMax, E);
917
918  case Builtin::BI__sync_add_and_fetch_1:
919  case Builtin::BI__sync_add_and_fetch_2:
920  case Builtin::BI__sync_add_and_fetch_4:
921  case Builtin::BI__sync_add_and_fetch_8:
922  case Builtin::BI__sync_add_and_fetch_16:
923    return EmitBinaryAtomicPost(*this, llvm::AtomicRMWInst::Add, E,
924                                llvm::Instruction::Add);
925  case Builtin::BI__sync_sub_and_fetch_1:
926  case Builtin::BI__sync_sub_and_fetch_2:
927  case Builtin::BI__sync_sub_and_fetch_4:
928  case Builtin::BI__sync_sub_and_fetch_8:
929  case Builtin::BI__sync_sub_and_fetch_16:
930    return EmitBinaryAtomicPost(*this, llvm::AtomicRMWInst::Sub, E,
931                                llvm::Instruction::Sub);
932  case Builtin::BI__sync_and_and_fetch_1:
933  case Builtin::BI__sync_and_and_fetch_2:
934  case Builtin::BI__sync_and_and_fetch_4:
935  case Builtin::BI__sync_and_and_fetch_8:
936  case Builtin::BI__sync_and_and_fetch_16:
937    return EmitBinaryAtomicPost(*this, llvm::AtomicRMWInst::And, E,
938                                llvm::Instruction::And);
939  case Builtin::BI__sync_or_and_fetch_1:
940  case Builtin::BI__sync_or_and_fetch_2:
941  case Builtin::BI__sync_or_and_fetch_4:
942  case Builtin::BI__sync_or_and_fetch_8:
943  case Builtin::BI__sync_or_and_fetch_16:
944    return EmitBinaryAtomicPost(*this, llvm::AtomicRMWInst::Or, E,
945                                llvm::Instruction::Or);
946  case Builtin::BI__sync_xor_and_fetch_1:
947  case Builtin::BI__sync_xor_and_fetch_2:
948  case Builtin::BI__sync_xor_and_fetch_4:
949  case Builtin::BI__sync_xor_and_fetch_8:
950  case Builtin::BI__sync_xor_and_fetch_16:
951    return EmitBinaryAtomicPost(*this, llvm::AtomicRMWInst::Xor, E,
952                                llvm::Instruction::Xor);
953
954  case Builtin::BI__sync_val_compare_and_swap_1:
955  case Builtin::BI__sync_val_compare_and_swap_2:
956  case Builtin::BI__sync_val_compare_and_swap_4:
957  case Builtin::BI__sync_val_compare_and_swap_8:
958  case Builtin::BI__sync_val_compare_and_swap_16: {
959    QualType T = E->getType();
960    llvm::Value *DestPtr = EmitScalarExpr(E->getArg(0));
961    unsigned AddrSpace = DestPtr->getType()->getPointerAddressSpace();
962
963    llvm::IntegerType *IntType =
964      llvm::IntegerType::get(getLLVMContext(),
965                             getContext().getTypeSize(T));
966    llvm::Type *IntPtrType = IntType->getPointerTo(AddrSpace);
967
968    Value *Args[3];
969    Args[0] = Builder.CreateBitCast(DestPtr, IntPtrType);
970    Args[1] = EmitScalarExpr(E->getArg(1));
971    llvm::Type *ValueType = Args[1]->getType();
972    Args[1] = EmitToInt(*this, Args[1], T, IntType);
973    Args[2] = EmitToInt(*this, EmitScalarExpr(E->getArg(2)), T, IntType);
974
975    Value *Result = Builder.CreateAtomicCmpXchg(Args[0], Args[1], Args[2],
976                                                llvm::SequentiallyConsistent,
977                                                llvm::SequentiallyConsistent);
978    Result = Builder.CreateExtractValue(Result, 0);
979    Result = EmitFromInt(*this, Result, T, ValueType);
980    return RValue::get(Result);
981  }
982
983  case Builtin::BI__sync_bool_compare_and_swap_1:
984  case Builtin::BI__sync_bool_compare_and_swap_2:
985  case Builtin::BI__sync_bool_compare_and_swap_4:
986  case Builtin::BI__sync_bool_compare_and_swap_8:
987  case Builtin::BI__sync_bool_compare_and_swap_16: {
988    QualType T = E->getArg(1)->getType();
989    llvm::Value *DestPtr = EmitScalarExpr(E->getArg(0));
990    unsigned AddrSpace = DestPtr->getType()->getPointerAddressSpace();
991
992    llvm::IntegerType *IntType =
993      llvm::IntegerType::get(getLLVMContext(),
994                             getContext().getTypeSize(T));
995    llvm::Type *IntPtrType = IntType->getPointerTo(AddrSpace);
996
997    Value *Args[3];
998    Args[0] = Builder.CreateBitCast(DestPtr, IntPtrType);
999    Args[1] = EmitToInt(*this, EmitScalarExpr(E->getArg(1)), T, IntType);
1000    Args[2] = EmitToInt(*this, EmitScalarExpr(E->getArg(2)), T, IntType);
1001
1002    Value *Pair = Builder.CreateAtomicCmpXchg(Args[0], Args[1], Args[2],
1003                                              llvm::SequentiallyConsistent,
1004                                              llvm::SequentiallyConsistent);
1005    Value *Result = Builder.CreateExtractValue(Pair, 1);
1006    // zext bool to int.
1007    Result = Builder.CreateZExt(Result, ConvertType(E->getType()));
1008    return RValue::get(Result);
1009  }
1010
1011  case Builtin::BI__sync_swap_1:
1012  case Builtin::BI__sync_swap_2:
1013  case Builtin::BI__sync_swap_4:
1014  case Builtin::BI__sync_swap_8:
1015  case Builtin::BI__sync_swap_16:
1016    return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::Xchg, E);
1017
1018  case Builtin::BI__sync_lock_test_and_set_1:
1019  case Builtin::BI__sync_lock_test_and_set_2:
1020  case Builtin::BI__sync_lock_test_and_set_4:
1021  case Builtin::BI__sync_lock_test_and_set_8:
1022  case Builtin::BI__sync_lock_test_and_set_16:
1023    return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::Xchg, E);
1024
1025  case Builtin::BI__sync_lock_release_1:
1026  case Builtin::BI__sync_lock_release_2:
1027  case Builtin::BI__sync_lock_release_4:
1028  case Builtin::BI__sync_lock_release_8:
1029  case Builtin::BI__sync_lock_release_16: {
1030    Value *Ptr = EmitScalarExpr(E->getArg(0));
1031    QualType ElTy = E->getArg(0)->getType()->getPointeeType();
1032    CharUnits StoreSize = getContext().getTypeSizeInChars(ElTy);
1033    llvm::Type *ITy = llvm::IntegerType::get(getLLVMContext(),
1034                                             StoreSize.getQuantity() * 8);
1035    Ptr = Builder.CreateBitCast(Ptr, ITy->getPointerTo());
1036    llvm::StoreInst *Store =
1037      Builder.CreateStore(llvm::Constant::getNullValue(ITy), Ptr);
1038    Store->setAlignment(StoreSize.getQuantity());
1039    Store->setAtomic(llvm::Release);
1040    return RValue::get(nullptr);
1041  }
1042
1043  case Builtin::BI__sync_synchronize: {
1044    // We assume this is supposed to correspond to a C++0x-style
1045    // sequentially-consistent fence (i.e. this is only usable for
1046    // synchonization, not device I/O or anything like that). This intrinsic
1047    // is really badly designed in the sense that in theory, there isn't
1048    // any way to safely use it... but in practice, it mostly works
1049    // to use it with non-atomic loads and stores to get acquire/release
1050    // semantics.
1051    Builder.CreateFence(llvm::SequentiallyConsistent);
1052    return RValue::get(nullptr);
1053  }
1054
1055  case Builtin::BI__c11_atomic_is_lock_free:
1056  case Builtin::BI__atomic_is_lock_free: {
1057    // Call "bool __atomic_is_lock_free(size_t size, void *ptr)". For the
1058    // __c11 builtin, ptr is 0 (indicating a properly-aligned object), since
1059    // _Atomic(T) is always properly-aligned.
1060    const char *LibCallName = "__atomic_is_lock_free";
1061    CallArgList Args;
1062    Args.add(RValue::get(EmitScalarExpr(E->getArg(0))),
1063             getContext().getSizeType());
1064    if (BuiltinID == Builtin::BI__atomic_is_lock_free)
1065      Args.add(RValue::get(EmitScalarExpr(E->getArg(1))),
1066               getContext().VoidPtrTy);
1067    else
1068      Args.add(RValue::get(llvm::Constant::getNullValue(VoidPtrTy)),
1069               getContext().VoidPtrTy);
1070    const CGFunctionInfo &FuncInfo =
1071        CGM.getTypes().arrangeFreeFunctionCall(E->getType(), Args,
1072                                               FunctionType::ExtInfo(),
1073                                               RequiredArgs::All);
1074    llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FuncInfo);
1075    llvm::Constant *Func = CGM.CreateRuntimeFunction(FTy, LibCallName);
1076    return EmitCall(FuncInfo, Func, ReturnValueSlot(), Args);
1077  }
1078
1079  case Builtin::BI__atomic_test_and_set: {
1080    // Look at the argument type to determine whether this is a volatile
1081    // operation. The parameter type is always volatile.
1082    QualType PtrTy = E->getArg(0)->IgnoreImpCasts()->getType();
1083    bool Volatile =
1084        PtrTy->castAs<PointerType>()->getPointeeType().isVolatileQualified();
1085
1086    Value *Ptr = EmitScalarExpr(E->getArg(0));
1087    unsigned AddrSpace = Ptr->getType()->getPointerAddressSpace();
1088    Ptr = Builder.CreateBitCast(Ptr, Int8Ty->getPointerTo(AddrSpace));
1089    Value *NewVal = Builder.getInt8(1);
1090    Value *Order = EmitScalarExpr(E->getArg(1));
1091    if (isa<llvm::ConstantInt>(Order)) {
1092      int ord = cast<llvm::ConstantInt>(Order)->getZExtValue();
1093      AtomicRMWInst *Result = nullptr;
1094      switch (ord) {
1095      case 0:  // memory_order_relaxed
1096      default: // invalid order
1097        Result = Builder.CreateAtomicRMW(llvm::AtomicRMWInst::Xchg,
1098                                         Ptr, NewVal,
1099                                         llvm::Monotonic);
1100        break;
1101      case 1:  // memory_order_consume
1102      case 2:  // memory_order_acquire
1103        Result = Builder.CreateAtomicRMW(llvm::AtomicRMWInst::Xchg,
1104                                         Ptr, NewVal,
1105                                         llvm::Acquire);
1106        break;
1107      case 3:  // memory_order_release
1108        Result = Builder.CreateAtomicRMW(llvm::AtomicRMWInst::Xchg,
1109                                         Ptr, NewVal,
1110                                         llvm::Release);
1111        break;
1112      case 4:  // memory_order_acq_rel
1113        Result = Builder.CreateAtomicRMW(llvm::AtomicRMWInst::Xchg,
1114                                         Ptr, NewVal,
1115                                         llvm::AcquireRelease);
1116        break;
1117      case 5:  // memory_order_seq_cst
1118        Result = Builder.CreateAtomicRMW(llvm::AtomicRMWInst::Xchg,
1119                                         Ptr, NewVal,
1120                                         llvm::SequentiallyConsistent);
1121        break;
1122      }
1123      Result->setVolatile(Volatile);
1124      return RValue::get(Builder.CreateIsNotNull(Result, "tobool"));
1125    }
1126
1127    llvm::BasicBlock *ContBB = createBasicBlock("atomic.continue", CurFn);
1128
1129    llvm::BasicBlock *BBs[5] = {
1130      createBasicBlock("monotonic", CurFn),
1131      createBasicBlock("acquire", CurFn),
1132      createBasicBlock("release", CurFn),
1133      createBasicBlock("acqrel", CurFn),
1134      createBasicBlock("seqcst", CurFn)
1135    };
1136    llvm::AtomicOrdering Orders[5] = {
1137      llvm::Monotonic, llvm::Acquire, llvm::Release,
1138      llvm::AcquireRelease, llvm::SequentiallyConsistent
1139    };
1140
1141    Order = Builder.CreateIntCast(Order, Builder.getInt32Ty(), false);
1142    llvm::SwitchInst *SI = Builder.CreateSwitch(Order, BBs[0]);
1143
1144    Builder.SetInsertPoint(ContBB);
1145    PHINode *Result = Builder.CreatePHI(Int8Ty, 5, "was_set");
1146
1147    for (unsigned i = 0; i < 5; ++i) {
1148      Builder.SetInsertPoint(BBs[i]);
1149      AtomicRMWInst *RMW = Builder.CreateAtomicRMW(llvm::AtomicRMWInst::Xchg,
1150                                                   Ptr, NewVal, Orders[i]);
1151      RMW->setVolatile(Volatile);
1152      Result->addIncoming(RMW, BBs[i]);
1153      Builder.CreateBr(ContBB);
1154    }
1155
1156    SI->addCase(Builder.getInt32(0), BBs[0]);
1157    SI->addCase(Builder.getInt32(1), BBs[1]);
1158    SI->addCase(Builder.getInt32(2), BBs[1]);
1159    SI->addCase(Builder.getInt32(3), BBs[2]);
1160    SI->addCase(Builder.getInt32(4), BBs[3]);
1161    SI->addCase(Builder.getInt32(5), BBs[4]);
1162
1163    Builder.SetInsertPoint(ContBB);
1164    return RValue::get(Builder.CreateIsNotNull(Result, "tobool"));
1165  }
1166
1167  case Builtin::BI__atomic_clear: {
1168    QualType PtrTy = E->getArg(0)->IgnoreImpCasts()->getType();
1169    bool Volatile =
1170        PtrTy->castAs<PointerType>()->getPointeeType().isVolatileQualified();
1171
1172    Value *Ptr = EmitScalarExpr(E->getArg(0));
1173    unsigned AddrSpace = Ptr->getType()->getPointerAddressSpace();
1174    Ptr = Builder.CreateBitCast(Ptr, Int8Ty->getPointerTo(AddrSpace));
1175    Value *NewVal = Builder.getInt8(0);
1176    Value *Order = EmitScalarExpr(E->getArg(1));
1177    if (isa<llvm::ConstantInt>(Order)) {
1178      int ord = cast<llvm::ConstantInt>(Order)->getZExtValue();
1179      StoreInst *Store = Builder.CreateStore(NewVal, Ptr, Volatile);
1180      Store->setAlignment(1);
1181      switch (ord) {
1182      case 0:  // memory_order_relaxed
1183      default: // invalid order
1184        Store->setOrdering(llvm::Monotonic);
1185        break;
1186      case 3:  // memory_order_release
1187        Store->setOrdering(llvm::Release);
1188        break;
1189      case 5:  // memory_order_seq_cst
1190        Store->setOrdering(llvm::SequentiallyConsistent);
1191        break;
1192      }
1193      return RValue::get(nullptr);
1194    }
1195
1196    llvm::BasicBlock *ContBB = createBasicBlock("atomic.continue", CurFn);
1197
1198    llvm::BasicBlock *BBs[3] = {
1199      createBasicBlock("monotonic", CurFn),
1200      createBasicBlock("release", CurFn),
1201      createBasicBlock("seqcst", CurFn)
1202    };
1203    llvm::AtomicOrdering Orders[3] = {
1204      llvm::Monotonic, llvm::Release, llvm::SequentiallyConsistent
1205    };
1206
1207    Order = Builder.CreateIntCast(Order, Builder.getInt32Ty(), false);
1208    llvm::SwitchInst *SI = Builder.CreateSwitch(Order, BBs[0]);
1209
1210    for (unsigned i = 0; i < 3; ++i) {
1211      Builder.SetInsertPoint(BBs[i]);
1212      StoreInst *Store = Builder.CreateStore(NewVal, Ptr, Volatile);
1213      Store->setAlignment(1);
1214      Store->setOrdering(Orders[i]);
1215      Builder.CreateBr(ContBB);
1216    }
1217
1218    SI->addCase(Builder.getInt32(0), BBs[0]);
1219    SI->addCase(Builder.getInt32(3), BBs[1]);
1220    SI->addCase(Builder.getInt32(5), BBs[2]);
1221
1222    Builder.SetInsertPoint(ContBB);
1223    return RValue::get(nullptr);
1224  }
1225
1226  case Builtin::BI__atomic_thread_fence:
1227  case Builtin::BI__atomic_signal_fence:
1228  case Builtin::BI__c11_atomic_thread_fence:
1229  case Builtin::BI__c11_atomic_signal_fence: {
1230    llvm::SynchronizationScope Scope;
1231    if (BuiltinID == Builtin::BI__atomic_signal_fence ||
1232        BuiltinID == Builtin::BI__c11_atomic_signal_fence)
1233      Scope = llvm::SingleThread;
1234    else
1235      Scope = llvm::CrossThread;
1236    Value *Order = EmitScalarExpr(E->getArg(0));
1237    if (isa<llvm::ConstantInt>(Order)) {
1238      int ord = cast<llvm::ConstantInt>(Order)->getZExtValue();
1239      switch (ord) {
1240      case 0:  // memory_order_relaxed
1241      default: // invalid order
1242        break;
1243      case 1:  // memory_order_consume
1244      case 2:  // memory_order_acquire
1245        Builder.CreateFence(llvm::Acquire, Scope);
1246        break;
1247      case 3:  // memory_order_release
1248        Builder.CreateFence(llvm::Release, Scope);
1249        break;
1250      case 4:  // memory_order_acq_rel
1251        Builder.CreateFence(llvm::AcquireRelease, Scope);
1252        break;
1253      case 5:  // memory_order_seq_cst
1254        Builder.CreateFence(llvm::SequentiallyConsistent, Scope);
1255        break;
1256      }
1257      return RValue::get(nullptr);
1258    }
1259
1260    llvm::BasicBlock *AcquireBB, *ReleaseBB, *AcqRelBB, *SeqCstBB;
1261    AcquireBB = createBasicBlock("acquire", CurFn);
1262    ReleaseBB = createBasicBlock("release", CurFn);
1263    AcqRelBB = createBasicBlock("acqrel", CurFn);
1264    SeqCstBB = createBasicBlock("seqcst", CurFn);
1265    llvm::BasicBlock *ContBB = createBasicBlock("atomic.continue", CurFn);
1266
1267    Order = Builder.CreateIntCast(Order, Builder.getInt32Ty(), false);
1268    llvm::SwitchInst *SI = Builder.CreateSwitch(Order, ContBB);
1269
1270    Builder.SetInsertPoint(AcquireBB);
1271    Builder.CreateFence(llvm::Acquire, Scope);
1272    Builder.CreateBr(ContBB);
1273    SI->addCase(Builder.getInt32(1), AcquireBB);
1274    SI->addCase(Builder.getInt32(2), AcquireBB);
1275
1276    Builder.SetInsertPoint(ReleaseBB);
1277    Builder.CreateFence(llvm::Release, Scope);
1278    Builder.CreateBr(ContBB);
1279    SI->addCase(Builder.getInt32(3), ReleaseBB);
1280
1281    Builder.SetInsertPoint(AcqRelBB);
1282    Builder.CreateFence(llvm::AcquireRelease, Scope);
1283    Builder.CreateBr(ContBB);
1284    SI->addCase(Builder.getInt32(4), AcqRelBB);
1285
1286    Builder.SetInsertPoint(SeqCstBB);
1287    Builder.CreateFence(llvm::SequentiallyConsistent, Scope);
1288    Builder.CreateBr(ContBB);
1289    SI->addCase(Builder.getInt32(5), SeqCstBB);
1290
1291    Builder.SetInsertPoint(ContBB);
1292    return RValue::get(nullptr);
1293  }
1294
1295    // Library functions with special handling.
1296  case Builtin::BIsqrt:
1297  case Builtin::BIsqrtf:
1298  case Builtin::BIsqrtl: {
1299    // Transform a call to sqrt* into a @llvm.sqrt.* intrinsic call, but only
1300    // in finite- or unsafe-math mode (the intrinsic has different semantics
1301    // for handling negative numbers compared to the library function, so
1302    // -fmath-errno=0 is not enough).
1303    if (!FD->hasAttr<ConstAttr>())
1304      break;
1305    if (!(CGM.getCodeGenOpts().UnsafeFPMath ||
1306          CGM.getCodeGenOpts().NoNaNsFPMath))
1307      break;
1308    Value *Arg0 = EmitScalarExpr(E->getArg(0));
1309    llvm::Type *ArgType = Arg0->getType();
1310    Value *F = CGM.getIntrinsic(Intrinsic::sqrt, ArgType);
1311    return RValue::get(Builder.CreateCall(F, Arg0));
1312  }
1313
1314  case Builtin::BIpow:
1315  case Builtin::BIpowf:
1316  case Builtin::BIpowl: {
1317    // Transform a call to pow* into a @llvm.pow.* intrinsic call.
1318    if (!FD->hasAttr<ConstAttr>())
1319      break;
1320    Value *Base = EmitScalarExpr(E->getArg(0));
1321    Value *Exponent = EmitScalarExpr(E->getArg(1));
1322    llvm::Type *ArgType = Base->getType();
1323    Value *F = CGM.getIntrinsic(Intrinsic::pow, ArgType);
1324    return RValue::get(Builder.CreateCall2(F, Base, Exponent));
1325  }
1326
1327  case Builtin::BIfma:
1328  case Builtin::BIfmaf:
1329  case Builtin::BIfmal:
1330  case Builtin::BI__builtin_fma:
1331  case Builtin::BI__builtin_fmaf:
1332  case Builtin::BI__builtin_fmal: {
1333    // Rewrite fma to intrinsic.
1334    Value *FirstArg = EmitScalarExpr(E->getArg(0));
1335    llvm::Type *ArgType = FirstArg->getType();
1336    Value *F = CGM.getIntrinsic(Intrinsic::fma, ArgType);
1337    return RValue::get(Builder.CreateCall3(F, FirstArg,
1338                                              EmitScalarExpr(E->getArg(1)),
1339                                              EmitScalarExpr(E->getArg(2))));
1340  }
1341
1342  case Builtin::BI__builtin_signbit:
1343  case Builtin::BI__builtin_signbitf:
1344  case Builtin::BI__builtin_signbitl: {
1345    LLVMContext &C = CGM.getLLVMContext();
1346
1347    Value *Arg = EmitScalarExpr(E->getArg(0));
1348    llvm::Type *ArgTy = Arg->getType();
1349    if (ArgTy->isPPC_FP128Ty())
1350      break; // FIXME: I'm not sure what the right implementation is here.
1351    int ArgWidth = ArgTy->getPrimitiveSizeInBits();
1352    llvm::Type *ArgIntTy = llvm::IntegerType::get(C, ArgWidth);
1353    Value *BCArg = Builder.CreateBitCast(Arg, ArgIntTy);
1354    Value *ZeroCmp = llvm::Constant::getNullValue(ArgIntTy);
1355    Value *Result = Builder.CreateICmpSLT(BCArg, ZeroCmp);
1356    return RValue::get(Builder.CreateZExt(Result, ConvertType(E->getType())));
1357  }
1358  case Builtin::BI__builtin_annotation: {
1359    llvm::Value *AnnVal = EmitScalarExpr(E->getArg(0));
1360    llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::annotation,
1361                                      AnnVal->getType());
1362
1363    // Get the annotation string, go through casts. Sema requires this to be a
1364    // non-wide string literal, potentially casted, so the cast<> is safe.
1365    const Expr *AnnotationStrExpr = E->getArg(1)->IgnoreParenCasts();
1366    StringRef Str = cast<StringLiteral>(AnnotationStrExpr)->getString();
1367    return RValue::get(EmitAnnotationCall(F, AnnVal, Str, E->getExprLoc()));
1368  }
1369  case Builtin::BI__builtin_addcb:
1370  case Builtin::BI__builtin_addcs:
1371  case Builtin::BI__builtin_addc:
1372  case Builtin::BI__builtin_addcl:
1373  case Builtin::BI__builtin_addcll:
1374  case Builtin::BI__builtin_subcb:
1375  case Builtin::BI__builtin_subcs:
1376  case Builtin::BI__builtin_subc:
1377  case Builtin::BI__builtin_subcl:
1378  case Builtin::BI__builtin_subcll: {
1379
1380    // We translate all of these builtins from expressions of the form:
1381    //   int x = ..., y = ..., carryin = ..., carryout, result;
1382    //   result = __builtin_addc(x, y, carryin, &carryout);
1383    //
1384    // to LLVM IR of the form:
1385    //
1386    //   %tmp1 = call {i32, i1} @llvm.uadd.with.overflow.i32(i32 %x, i32 %y)
1387    //   %tmpsum1 = extractvalue {i32, i1} %tmp1, 0
1388    //   %carry1 = extractvalue {i32, i1} %tmp1, 1
1389    //   %tmp2 = call {i32, i1} @llvm.uadd.with.overflow.i32(i32 %tmpsum1,
1390    //                                                       i32 %carryin)
1391    //   %result = extractvalue {i32, i1} %tmp2, 0
1392    //   %carry2 = extractvalue {i32, i1} %tmp2, 1
1393    //   %tmp3 = or i1 %carry1, %carry2
1394    //   %tmp4 = zext i1 %tmp3 to i32
1395    //   store i32 %tmp4, i32* %carryout
1396
1397    // Scalarize our inputs.
1398    llvm::Value *X = EmitScalarExpr(E->getArg(0));
1399    llvm::Value *Y = EmitScalarExpr(E->getArg(1));
1400    llvm::Value *Carryin = EmitScalarExpr(E->getArg(2));
1401    std::pair<llvm::Value*, unsigned> CarryOutPtr =
1402      EmitPointerWithAlignment(E->getArg(3));
1403
1404    // Decide if we are lowering to a uadd.with.overflow or usub.with.overflow.
1405    llvm::Intrinsic::ID IntrinsicId;
1406    switch (BuiltinID) {
1407    default: llvm_unreachable("Unknown multiprecision builtin id.");
1408    case Builtin::BI__builtin_addcb:
1409    case Builtin::BI__builtin_addcs:
1410    case Builtin::BI__builtin_addc:
1411    case Builtin::BI__builtin_addcl:
1412    case Builtin::BI__builtin_addcll:
1413      IntrinsicId = llvm::Intrinsic::uadd_with_overflow;
1414      break;
1415    case Builtin::BI__builtin_subcb:
1416    case Builtin::BI__builtin_subcs:
1417    case Builtin::BI__builtin_subc:
1418    case Builtin::BI__builtin_subcl:
1419    case Builtin::BI__builtin_subcll:
1420      IntrinsicId = llvm::Intrinsic::usub_with_overflow;
1421      break;
1422    }
1423
1424    // Construct our resulting LLVM IR expression.
1425    llvm::Value *Carry1;
1426    llvm::Value *Sum1 = EmitOverflowIntrinsic(*this, IntrinsicId,
1427                                              X, Y, Carry1);
1428    llvm::Value *Carry2;
1429    llvm::Value *Sum2 = EmitOverflowIntrinsic(*this, IntrinsicId,
1430                                              Sum1, Carryin, Carry2);
1431    llvm::Value *CarryOut = Builder.CreateZExt(Builder.CreateOr(Carry1, Carry2),
1432                                               X->getType());
1433    llvm::StoreInst *CarryOutStore = Builder.CreateStore(CarryOut,
1434                                                         CarryOutPtr.first);
1435    CarryOutStore->setAlignment(CarryOutPtr.second);
1436    return RValue::get(Sum2);
1437  }
1438  case Builtin::BI__builtin_uadd_overflow:
1439  case Builtin::BI__builtin_uaddl_overflow:
1440  case Builtin::BI__builtin_uaddll_overflow:
1441  case Builtin::BI__builtin_usub_overflow:
1442  case Builtin::BI__builtin_usubl_overflow:
1443  case Builtin::BI__builtin_usubll_overflow:
1444  case Builtin::BI__builtin_umul_overflow:
1445  case Builtin::BI__builtin_umull_overflow:
1446  case Builtin::BI__builtin_umulll_overflow:
1447  case Builtin::BI__builtin_sadd_overflow:
1448  case Builtin::BI__builtin_saddl_overflow:
1449  case Builtin::BI__builtin_saddll_overflow:
1450  case Builtin::BI__builtin_ssub_overflow:
1451  case Builtin::BI__builtin_ssubl_overflow:
1452  case Builtin::BI__builtin_ssubll_overflow:
1453  case Builtin::BI__builtin_smul_overflow:
1454  case Builtin::BI__builtin_smull_overflow:
1455  case Builtin::BI__builtin_smulll_overflow: {
1456
1457    // We translate all of these builtins directly to the relevant llvm IR node.
1458
1459    // Scalarize our inputs.
1460    llvm::Value *X = EmitScalarExpr(E->getArg(0));
1461    llvm::Value *Y = EmitScalarExpr(E->getArg(1));
1462    std::pair<llvm::Value *, unsigned> SumOutPtr =
1463      EmitPointerWithAlignment(E->getArg(2));
1464
1465    // Decide which of the overflow intrinsics we are lowering to:
1466    llvm::Intrinsic::ID IntrinsicId;
1467    switch (BuiltinID) {
1468    default: llvm_unreachable("Unknown security overflow builtin id.");
1469    case Builtin::BI__builtin_uadd_overflow:
1470    case Builtin::BI__builtin_uaddl_overflow:
1471    case Builtin::BI__builtin_uaddll_overflow:
1472      IntrinsicId = llvm::Intrinsic::uadd_with_overflow;
1473      break;
1474    case Builtin::BI__builtin_usub_overflow:
1475    case Builtin::BI__builtin_usubl_overflow:
1476    case Builtin::BI__builtin_usubll_overflow:
1477      IntrinsicId = llvm::Intrinsic::usub_with_overflow;
1478      break;
1479    case Builtin::BI__builtin_umul_overflow:
1480    case Builtin::BI__builtin_umull_overflow:
1481    case Builtin::BI__builtin_umulll_overflow:
1482      IntrinsicId = llvm::Intrinsic::umul_with_overflow;
1483      break;
1484    case Builtin::BI__builtin_sadd_overflow:
1485    case Builtin::BI__builtin_saddl_overflow:
1486    case Builtin::BI__builtin_saddll_overflow:
1487      IntrinsicId = llvm::Intrinsic::sadd_with_overflow;
1488      break;
1489    case Builtin::BI__builtin_ssub_overflow:
1490    case Builtin::BI__builtin_ssubl_overflow:
1491    case Builtin::BI__builtin_ssubll_overflow:
1492      IntrinsicId = llvm::Intrinsic::ssub_with_overflow;
1493      break;
1494    case Builtin::BI__builtin_smul_overflow:
1495    case Builtin::BI__builtin_smull_overflow:
1496    case Builtin::BI__builtin_smulll_overflow:
1497      IntrinsicId = llvm::Intrinsic::smul_with_overflow;
1498      break;
1499    }
1500
1501
1502    llvm::Value *Carry;
1503    llvm::Value *Sum = EmitOverflowIntrinsic(*this, IntrinsicId, X, Y, Carry);
1504    llvm::StoreInst *SumOutStore = Builder.CreateStore(Sum, SumOutPtr.first);
1505    SumOutStore->setAlignment(SumOutPtr.second);
1506
1507    return RValue::get(Carry);
1508  }
1509  case Builtin::BI__builtin_addressof:
1510    return RValue::get(EmitLValue(E->getArg(0)).getAddress());
1511  case Builtin::BI__builtin_operator_new:
1512    return EmitBuiltinNewDeleteCall(FD->getType()->castAs<FunctionProtoType>(),
1513                                    E->getArg(0), false);
1514  case Builtin::BI__builtin_operator_delete:
1515    return EmitBuiltinNewDeleteCall(FD->getType()->castAs<FunctionProtoType>(),
1516                                    E->getArg(0), true);
1517  case Builtin::BI__noop:
1518    return RValue::get(nullptr);
1519  case Builtin::BI_InterlockedExchange:
1520  case Builtin::BI_InterlockedExchangePointer:
1521    return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::Xchg, E);
1522  case Builtin::BI_InterlockedCompareExchangePointer: {
1523    llvm::Type *RTy;
1524    llvm::IntegerType *IntType =
1525      IntegerType::get(getLLVMContext(),
1526                       getContext().getTypeSize(E->getType()));
1527    llvm::Type *IntPtrType = IntType->getPointerTo();
1528
1529    llvm::Value *Destination =
1530      Builder.CreateBitCast(EmitScalarExpr(E->getArg(0)), IntPtrType);
1531
1532    llvm::Value *Exchange = EmitScalarExpr(E->getArg(1));
1533    RTy = Exchange->getType();
1534    Exchange = Builder.CreatePtrToInt(Exchange, IntType);
1535
1536    llvm::Value *Comparand =
1537      Builder.CreatePtrToInt(EmitScalarExpr(E->getArg(2)), IntType);
1538
1539    auto Result = Builder.CreateAtomicCmpXchg(Destination, Comparand, Exchange,
1540                                              SequentiallyConsistent,
1541                                              SequentiallyConsistent);
1542    Result->setVolatile(true);
1543
1544    return RValue::get(Builder.CreateIntToPtr(Builder.CreateExtractValue(Result,
1545                                                                         0),
1546                                              RTy));
1547  }
1548  case Builtin::BI_InterlockedCompareExchange: {
1549    AtomicCmpXchgInst *CXI = Builder.CreateAtomicCmpXchg(
1550        EmitScalarExpr(E->getArg(0)),
1551        EmitScalarExpr(E->getArg(2)),
1552        EmitScalarExpr(E->getArg(1)),
1553        SequentiallyConsistent,
1554        SequentiallyConsistent);
1555      CXI->setVolatile(true);
1556      return RValue::get(Builder.CreateExtractValue(CXI, 0));
1557  }
1558  case Builtin::BI_InterlockedIncrement: {
1559    AtomicRMWInst *RMWI = Builder.CreateAtomicRMW(
1560      AtomicRMWInst::Add,
1561      EmitScalarExpr(E->getArg(0)),
1562      ConstantInt::get(Int32Ty, 1),
1563      llvm::SequentiallyConsistent);
1564    RMWI->setVolatile(true);
1565    return RValue::get(Builder.CreateAdd(RMWI, ConstantInt::get(Int32Ty, 1)));
1566  }
1567  case Builtin::BI_InterlockedDecrement: {
1568    AtomicRMWInst *RMWI = Builder.CreateAtomicRMW(
1569      AtomicRMWInst::Sub,
1570      EmitScalarExpr(E->getArg(0)),
1571      ConstantInt::get(Int32Ty, 1),
1572      llvm::SequentiallyConsistent);
1573    RMWI->setVolatile(true);
1574    return RValue::get(Builder.CreateSub(RMWI, ConstantInt::get(Int32Ty, 1)));
1575  }
1576  case Builtin::BI_InterlockedExchangeAdd: {
1577    AtomicRMWInst *RMWI = Builder.CreateAtomicRMW(
1578      AtomicRMWInst::Add,
1579      EmitScalarExpr(E->getArg(0)),
1580      EmitScalarExpr(E->getArg(1)),
1581      llvm::SequentiallyConsistent);
1582    RMWI->setVolatile(true);
1583    return RValue::get(RMWI);
1584  }
1585  }
1586
1587  // If this is an alias for a lib function (e.g. __builtin_sin), emit
1588  // the call using the normal call path, but using the unmangled
1589  // version of the function name.
1590  if (getContext().BuiltinInfo.isLibFunction(BuiltinID))
1591    return emitLibraryCall(*this, FD, E,
1592                           CGM.getBuiltinLibFunction(FD, BuiltinID));
1593
1594  // If this is a predefined lib function (e.g. malloc), emit the call
1595  // using exactly the normal call path.
1596  if (getContext().BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1597    return emitLibraryCall(*this, FD, E, EmitScalarExpr(E->getCallee()));
1598
1599  // See if we have a target specific intrinsic.
1600  const char *Name = getContext().BuiltinInfo.GetName(BuiltinID);
1601  Intrinsic::ID IntrinsicID = Intrinsic::not_intrinsic;
1602  if (const char *Prefix =
1603          llvm::Triple::getArchTypePrefix(getTarget().getTriple().getArch())) {
1604    IntrinsicID = Intrinsic::getIntrinsicForGCCBuiltin(Prefix, Name);
1605    // NOTE we dont need to perform a compatibility flag check here since the
1606    // intrinsics are declared in Builtins*.def via LANGBUILTIN which filter the
1607    // MS builtins via ALL_MS_LANGUAGES and are filtered earlier.
1608    if (IntrinsicID == Intrinsic::not_intrinsic)
1609      IntrinsicID = Intrinsic::getIntrinsicForMSBuiltin(Prefix, Name);
1610  }
1611
1612  if (IntrinsicID != Intrinsic::not_intrinsic) {
1613    SmallVector<Value*, 16> Args;
1614
1615    // Find out if any arguments are required to be integer constant
1616    // expressions.
1617    unsigned ICEArguments = 0;
1618    ASTContext::GetBuiltinTypeError Error;
1619    getContext().GetBuiltinType(BuiltinID, Error, &ICEArguments);
1620    assert(Error == ASTContext::GE_None && "Should not codegen an error");
1621
1622    Function *F = CGM.getIntrinsic(IntrinsicID);
1623    llvm::FunctionType *FTy = F->getFunctionType();
1624
1625    for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
1626      Value *ArgValue;
1627      // If this is a normal argument, just emit it as a scalar.
1628      if ((ICEArguments & (1 << i)) == 0) {
1629        ArgValue = EmitScalarExpr(E->getArg(i));
1630      } else {
1631        // If this is required to be a constant, constant fold it so that we
1632        // know that the generated intrinsic gets a ConstantInt.
1633        llvm::APSInt Result;
1634        bool IsConst = E->getArg(i)->isIntegerConstantExpr(Result,getContext());
1635        assert(IsConst && "Constant arg isn't actually constant?");
1636        (void)IsConst;
1637        ArgValue = llvm::ConstantInt::get(getLLVMContext(), Result);
1638      }
1639
1640      // If the intrinsic arg type is different from the builtin arg type
1641      // we need to do a bit cast.
1642      llvm::Type *PTy = FTy->getParamType(i);
1643      if (PTy != ArgValue->getType()) {
1644        assert(PTy->canLosslesslyBitCastTo(FTy->getParamType(i)) &&
1645               "Must be able to losslessly bit cast to param");
1646        ArgValue = Builder.CreateBitCast(ArgValue, PTy);
1647      }
1648
1649      Args.push_back(ArgValue);
1650    }
1651
1652    Value *V = Builder.CreateCall(F, Args);
1653    QualType BuiltinRetType = E->getType();
1654
1655    llvm::Type *RetTy = VoidTy;
1656    if (!BuiltinRetType->isVoidType())
1657      RetTy = ConvertType(BuiltinRetType);
1658
1659    if (RetTy != V->getType()) {
1660      assert(V->getType()->canLosslesslyBitCastTo(RetTy) &&
1661             "Must be able to losslessly bit cast result type");
1662      V = Builder.CreateBitCast(V, RetTy);
1663    }
1664
1665    return RValue::get(V);
1666  }
1667
1668  // See if we have a target specific builtin that needs to be lowered.
1669  if (Value *V = EmitTargetBuiltinExpr(BuiltinID, E))
1670    return RValue::get(V);
1671
1672  ErrorUnsupported(E, "builtin function");
1673
1674  // Unknown builtin, for now just dump it out and return undef.
1675  return GetUndefRValue(E->getType());
1676}
1677
1678Value *CodeGenFunction::EmitTargetBuiltinExpr(unsigned BuiltinID,
1679                                              const CallExpr *E) {
1680  switch (getTarget().getTriple().getArch()) {
1681  case llvm::Triple::arm:
1682  case llvm::Triple::armeb:
1683  case llvm::Triple::thumb:
1684  case llvm::Triple::thumbeb:
1685    return EmitARMBuiltinExpr(BuiltinID, E);
1686  case llvm::Triple::aarch64:
1687  case llvm::Triple::aarch64_be:
1688  case llvm::Triple::arm64:
1689  case llvm::Triple::arm64_be:
1690    return EmitAArch64BuiltinExpr(BuiltinID, E);
1691  case llvm::Triple::x86:
1692  case llvm::Triple::x86_64:
1693    return EmitX86BuiltinExpr(BuiltinID, E);
1694  case llvm::Triple::ppc:
1695  case llvm::Triple::ppc64:
1696  case llvm::Triple::ppc64le:
1697    return EmitPPCBuiltinExpr(BuiltinID, E);
1698  case llvm::Triple::r600:
1699    return EmitR600BuiltinExpr(BuiltinID, E);
1700  default:
1701    return nullptr;
1702  }
1703}
1704
1705static llvm::VectorType *GetNeonType(CodeGenFunction *CGF,
1706                                     NeonTypeFlags TypeFlags,
1707                                     bool V1Ty=false) {
1708  int IsQuad = TypeFlags.isQuad();
1709  switch (TypeFlags.getEltType()) {
1710  case NeonTypeFlags::Int8:
1711  case NeonTypeFlags::Poly8:
1712    return llvm::VectorType::get(CGF->Int8Ty, V1Ty ? 1 : (8 << IsQuad));
1713  case NeonTypeFlags::Int16:
1714  case NeonTypeFlags::Poly16:
1715  case NeonTypeFlags::Float16:
1716    return llvm::VectorType::get(CGF->Int16Ty, V1Ty ? 1 : (4 << IsQuad));
1717  case NeonTypeFlags::Int32:
1718    return llvm::VectorType::get(CGF->Int32Ty, V1Ty ? 1 : (2 << IsQuad));
1719  case NeonTypeFlags::Int64:
1720  case NeonTypeFlags::Poly64:
1721    return llvm::VectorType::get(CGF->Int64Ty, V1Ty ? 1 : (1 << IsQuad));
1722  case NeonTypeFlags::Poly128:
1723    // FIXME: i128 and f128 doesn't get fully support in Clang and llvm.
1724    // There is a lot of i128 and f128 API missing.
1725    // so we use v16i8 to represent poly128 and get pattern matched.
1726    return llvm::VectorType::get(CGF->Int8Ty, 16);
1727  case NeonTypeFlags::Float32:
1728    return llvm::VectorType::get(CGF->FloatTy, V1Ty ? 1 : (2 << IsQuad));
1729  case NeonTypeFlags::Float64:
1730    return llvm::VectorType::get(CGF->DoubleTy, V1Ty ? 1 : (1 << IsQuad));
1731  }
1732  llvm_unreachable("Unknown vector element type!");
1733}
1734
1735Value *CodeGenFunction::EmitNeonSplat(Value *V, Constant *C) {
1736  unsigned nElts = cast<llvm::VectorType>(V->getType())->getNumElements();
1737  Value* SV = llvm::ConstantVector::getSplat(nElts, C);
1738  return Builder.CreateShuffleVector(V, V, SV, "lane");
1739}
1740
1741Value *CodeGenFunction::EmitNeonCall(Function *F, SmallVectorImpl<Value*> &Ops,
1742                                     const char *name,
1743                                     unsigned shift, bool rightshift) {
1744  unsigned j = 0;
1745  for (Function::const_arg_iterator ai = F->arg_begin(), ae = F->arg_end();
1746       ai != ae; ++ai, ++j)
1747    if (shift > 0 && shift == j)
1748      Ops[j] = EmitNeonShiftVector(Ops[j], ai->getType(), rightshift);
1749    else
1750      Ops[j] = Builder.CreateBitCast(Ops[j], ai->getType(), name);
1751
1752  return Builder.CreateCall(F, Ops, name);
1753}
1754
1755Value *CodeGenFunction::EmitNeonShiftVector(Value *V, llvm::Type *Ty,
1756                                            bool neg) {
1757  int SV = cast<ConstantInt>(V)->getSExtValue();
1758
1759  llvm::VectorType *VTy = cast<llvm::VectorType>(Ty);
1760  llvm::Constant *C = ConstantInt::get(VTy->getElementType(), neg ? -SV : SV);
1761  return llvm::ConstantVector::getSplat(VTy->getNumElements(), C);
1762}
1763
1764// \brief Right-shift a vector by a constant.
1765Value *CodeGenFunction::EmitNeonRShiftImm(Value *Vec, Value *Shift,
1766                                          llvm::Type *Ty, bool usgn,
1767                                          const char *name) {
1768  llvm::VectorType *VTy = cast<llvm::VectorType>(Ty);
1769
1770  int ShiftAmt = cast<ConstantInt>(Shift)->getSExtValue();
1771  int EltSize = VTy->getScalarSizeInBits();
1772
1773  Vec = Builder.CreateBitCast(Vec, Ty);
1774
1775  // lshr/ashr are undefined when the shift amount is equal to the vector
1776  // element size.
1777  if (ShiftAmt == EltSize) {
1778    if (usgn) {
1779      // Right-shifting an unsigned value by its size yields 0.
1780      llvm::Constant *Zero = ConstantInt::get(VTy->getElementType(), 0);
1781      return llvm::ConstantVector::getSplat(VTy->getNumElements(), Zero);
1782    } else {
1783      // Right-shifting a signed value by its size is equivalent
1784      // to a shift of size-1.
1785      --ShiftAmt;
1786      Shift = ConstantInt::get(VTy->getElementType(), ShiftAmt);
1787    }
1788  }
1789
1790  Shift = EmitNeonShiftVector(Shift, Ty, false);
1791  if (usgn)
1792    return Builder.CreateLShr(Vec, Shift, name);
1793  else
1794    return Builder.CreateAShr(Vec, Shift, name);
1795}
1796
1797/// GetPointeeAlignment - Given an expression with a pointer type, find the
1798/// alignment of the type referenced by the pointer.  Skip over implicit
1799/// casts.
1800std::pair<llvm::Value*, unsigned>
1801CodeGenFunction::EmitPointerWithAlignment(const Expr *Addr) {
1802  assert(Addr->getType()->isPointerType());
1803  Addr = Addr->IgnoreParens();
1804  if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Addr)) {
1805    if ((ICE->getCastKind() == CK_BitCast || ICE->getCastKind() == CK_NoOp) &&
1806        ICE->getSubExpr()->getType()->isPointerType()) {
1807      std::pair<llvm::Value*, unsigned> Ptr =
1808          EmitPointerWithAlignment(ICE->getSubExpr());
1809      Ptr.first = Builder.CreateBitCast(Ptr.first,
1810                                        ConvertType(Addr->getType()));
1811      return Ptr;
1812    } else if (ICE->getCastKind() == CK_ArrayToPointerDecay) {
1813      LValue LV = EmitLValue(ICE->getSubExpr());
1814      unsigned Align = LV.getAlignment().getQuantity();
1815      if (!Align) {
1816        // FIXME: Once LValues are fixed to always set alignment,
1817        // zap this code.
1818        QualType PtTy = ICE->getSubExpr()->getType();
1819        if (!PtTy->isIncompleteType())
1820          Align = getContext().getTypeAlignInChars(PtTy).getQuantity();
1821        else
1822          Align = 1;
1823      }
1824      return std::make_pair(LV.getAddress(), Align);
1825    }
1826  }
1827  if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(Addr)) {
1828    if (UO->getOpcode() == UO_AddrOf) {
1829      LValue LV = EmitLValue(UO->getSubExpr());
1830      unsigned Align = LV.getAlignment().getQuantity();
1831      if (!Align) {
1832        // FIXME: Once LValues are fixed to always set alignment,
1833        // zap this code.
1834        QualType PtTy = UO->getSubExpr()->getType();
1835        if (!PtTy->isIncompleteType())
1836          Align = getContext().getTypeAlignInChars(PtTy).getQuantity();
1837        else
1838          Align = 1;
1839      }
1840      return std::make_pair(LV.getAddress(), Align);
1841    }
1842  }
1843
1844  unsigned Align = 1;
1845  QualType PtTy = Addr->getType()->getPointeeType();
1846  if (!PtTy->isIncompleteType())
1847    Align = getContext().getTypeAlignInChars(PtTy).getQuantity();
1848
1849  return std::make_pair(EmitScalarExpr(Addr), Align);
1850}
1851
1852enum {
1853  AddRetType = (1 << 0),
1854  Add1ArgType = (1 << 1),
1855  Add2ArgTypes = (1 << 2),
1856
1857  VectorizeRetType = (1 << 3),
1858  VectorizeArgTypes = (1 << 4),
1859
1860  InventFloatType = (1 << 5),
1861  UnsignedAlts = (1 << 6),
1862
1863  Use64BitVectors = (1 << 7),
1864  Use128BitVectors = (1 << 8),
1865
1866  Vectorize1ArgType = Add1ArgType | VectorizeArgTypes,
1867  VectorRet = AddRetType | VectorizeRetType,
1868  VectorRetGetArgs01 =
1869      AddRetType | Add2ArgTypes | VectorizeRetType | VectorizeArgTypes,
1870  FpCmpzModifiers =
1871      AddRetType | VectorizeRetType | Add1ArgType | InventFloatType
1872};
1873
1874 struct NeonIntrinsicInfo {
1875  unsigned BuiltinID;
1876  unsigned LLVMIntrinsic;
1877  unsigned AltLLVMIntrinsic;
1878  const char *NameHint;
1879  unsigned TypeModifier;
1880
1881  bool operator<(unsigned RHSBuiltinID) const {
1882    return BuiltinID < RHSBuiltinID;
1883  }
1884};
1885
1886#define NEONMAP0(NameBase) \
1887  { NEON::BI__builtin_neon_ ## NameBase, 0, 0, #NameBase, 0 }
1888
1889#define NEONMAP1(NameBase, LLVMIntrinsic, TypeModifier) \
1890  { NEON:: BI__builtin_neon_ ## NameBase, \
1891      Intrinsic::LLVMIntrinsic, 0, #NameBase, TypeModifier }
1892
1893#define NEONMAP2(NameBase, LLVMIntrinsic, AltLLVMIntrinsic, TypeModifier) \
1894  { NEON:: BI__builtin_neon_ ## NameBase, \
1895      Intrinsic::LLVMIntrinsic, Intrinsic::AltLLVMIntrinsic, \
1896      #NameBase, TypeModifier }
1897
1898static NeonIntrinsicInfo ARMSIMDIntrinsicMap [] = {
1899  NEONMAP2(vabd_v, arm_neon_vabdu, arm_neon_vabds, Add1ArgType | UnsignedAlts),
1900  NEONMAP2(vabdq_v, arm_neon_vabdu, arm_neon_vabds, Add1ArgType | UnsignedAlts),
1901  NEONMAP1(vabs_v, arm_neon_vabs, 0),
1902  NEONMAP1(vabsq_v, arm_neon_vabs, 0),
1903  NEONMAP0(vaddhn_v),
1904  NEONMAP1(vaesdq_v, arm_neon_aesd, 0),
1905  NEONMAP1(vaeseq_v, arm_neon_aese, 0),
1906  NEONMAP1(vaesimcq_v, arm_neon_aesimc, 0),
1907  NEONMAP1(vaesmcq_v, arm_neon_aesmc, 0),
1908  NEONMAP1(vbsl_v, arm_neon_vbsl, AddRetType),
1909  NEONMAP1(vbslq_v, arm_neon_vbsl, AddRetType),
1910  NEONMAP1(vcage_v, arm_neon_vacge, 0),
1911  NEONMAP1(vcageq_v, arm_neon_vacge, 0),
1912  NEONMAP1(vcagt_v, arm_neon_vacgt, 0),
1913  NEONMAP1(vcagtq_v, arm_neon_vacgt, 0),
1914  NEONMAP1(vcale_v, arm_neon_vacge, 0),
1915  NEONMAP1(vcaleq_v, arm_neon_vacge, 0),
1916  NEONMAP1(vcalt_v, arm_neon_vacgt, 0),
1917  NEONMAP1(vcaltq_v, arm_neon_vacgt, 0),
1918  NEONMAP1(vcls_v, arm_neon_vcls, Add1ArgType),
1919  NEONMAP1(vclsq_v, arm_neon_vcls, Add1ArgType),
1920  NEONMAP1(vclz_v, ctlz, Add1ArgType),
1921  NEONMAP1(vclzq_v, ctlz, Add1ArgType),
1922  NEONMAP1(vcnt_v, ctpop, Add1ArgType),
1923  NEONMAP1(vcntq_v, ctpop, Add1ArgType),
1924  NEONMAP1(vcvt_f16_v, arm_neon_vcvtfp2hf, 0),
1925  NEONMAP1(vcvt_f32_f16, arm_neon_vcvthf2fp, 0),
1926  NEONMAP0(vcvt_f32_v),
1927  NEONMAP2(vcvt_n_f32_v, arm_neon_vcvtfxu2fp, arm_neon_vcvtfxs2fp, 0),
1928  NEONMAP1(vcvt_n_s32_v, arm_neon_vcvtfp2fxs, 0),
1929  NEONMAP1(vcvt_n_s64_v, arm_neon_vcvtfp2fxs, 0),
1930  NEONMAP1(vcvt_n_u32_v, arm_neon_vcvtfp2fxu, 0),
1931  NEONMAP1(vcvt_n_u64_v, arm_neon_vcvtfp2fxu, 0),
1932  NEONMAP0(vcvt_s32_v),
1933  NEONMAP0(vcvt_s64_v),
1934  NEONMAP0(vcvt_u32_v),
1935  NEONMAP0(vcvt_u64_v),
1936  NEONMAP1(vcvta_s32_v, arm_neon_vcvtas, 0),
1937  NEONMAP1(vcvta_s64_v, arm_neon_vcvtas, 0),
1938  NEONMAP1(vcvta_u32_v, arm_neon_vcvtau, 0),
1939  NEONMAP1(vcvta_u64_v, arm_neon_vcvtau, 0),
1940  NEONMAP1(vcvtaq_s32_v, arm_neon_vcvtas, 0),
1941  NEONMAP1(vcvtaq_s64_v, arm_neon_vcvtas, 0),
1942  NEONMAP1(vcvtaq_u32_v, arm_neon_vcvtau, 0),
1943  NEONMAP1(vcvtaq_u64_v, arm_neon_vcvtau, 0),
1944  NEONMAP1(vcvtm_s32_v, arm_neon_vcvtms, 0),
1945  NEONMAP1(vcvtm_s64_v, arm_neon_vcvtms, 0),
1946  NEONMAP1(vcvtm_u32_v, arm_neon_vcvtmu, 0),
1947  NEONMAP1(vcvtm_u64_v, arm_neon_vcvtmu, 0),
1948  NEONMAP1(vcvtmq_s32_v, arm_neon_vcvtms, 0),
1949  NEONMAP1(vcvtmq_s64_v, arm_neon_vcvtms, 0),
1950  NEONMAP1(vcvtmq_u32_v, arm_neon_vcvtmu, 0),
1951  NEONMAP1(vcvtmq_u64_v, arm_neon_vcvtmu, 0),
1952  NEONMAP1(vcvtn_s32_v, arm_neon_vcvtns, 0),
1953  NEONMAP1(vcvtn_s64_v, arm_neon_vcvtns, 0),
1954  NEONMAP1(vcvtn_u32_v, arm_neon_vcvtnu, 0),
1955  NEONMAP1(vcvtn_u64_v, arm_neon_vcvtnu, 0),
1956  NEONMAP1(vcvtnq_s32_v, arm_neon_vcvtns, 0),
1957  NEONMAP1(vcvtnq_s64_v, arm_neon_vcvtns, 0),
1958  NEONMAP1(vcvtnq_u32_v, arm_neon_vcvtnu, 0),
1959  NEONMAP1(vcvtnq_u64_v, arm_neon_vcvtnu, 0),
1960  NEONMAP1(vcvtp_s32_v, arm_neon_vcvtps, 0),
1961  NEONMAP1(vcvtp_s64_v, arm_neon_vcvtps, 0),
1962  NEONMAP1(vcvtp_u32_v, arm_neon_vcvtpu, 0),
1963  NEONMAP1(vcvtp_u64_v, arm_neon_vcvtpu, 0),
1964  NEONMAP1(vcvtpq_s32_v, arm_neon_vcvtps, 0),
1965  NEONMAP1(vcvtpq_s64_v, arm_neon_vcvtps, 0),
1966  NEONMAP1(vcvtpq_u32_v, arm_neon_vcvtpu, 0),
1967  NEONMAP1(vcvtpq_u64_v, arm_neon_vcvtpu, 0),
1968  NEONMAP0(vcvtq_f32_v),
1969  NEONMAP2(vcvtq_n_f32_v, arm_neon_vcvtfxu2fp, arm_neon_vcvtfxs2fp, 0),
1970  NEONMAP1(vcvtq_n_s32_v, arm_neon_vcvtfp2fxs, 0),
1971  NEONMAP1(vcvtq_n_s64_v, arm_neon_vcvtfp2fxs, 0),
1972  NEONMAP1(vcvtq_n_u32_v, arm_neon_vcvtfp2fxu, 0),
1973  NEONMAP1(vcvtq_n_u64_v, arm_neon_vcvtfp2fxu, 0),
1974  NEONMAP0(vcvtq_s32_v),
1975  NEONMAP0(vcvtq_s64_v),
1976  NEONMAP0(vcvtq_u32_v),
1977  NEONMAP0(vcvtq_u64_v),
1978  NEONMAP0(vext_v),
1979  NEONMAP0(vextq_v),
1980  NEONMAP0(vfma_v),
1981  NEONMAP0(vfmaq_v),
1982  NEONMAP2(vhadd_v, arm_neon_vhaddu, arm_neon_vhadds, Add1ArgType | UnsignedAlts),
1983  NEONMAP2(vhaddq_v, arm_neon_vhaddu, arm_neon_vhadds, Add1ArgType | UnsignedAlts),
1984  NEONMAP2(vhsub_v, arm_neon_vhsubu, arm_neon_vhsubs, Add1ArgType | UnsignedAlts),
1985  NEONMAP2(vhsubq_v, arm_neon_vhsubu, arm_neon_vhsubs, Add1ArgType | UnsignedAlts),
1986  NEONMAP0(vld1_dup_v),
1987  NEONMAP1(vld1_v, arm_neon_vld1, 0),
1988  NEONMAP0(vld1q_dup_v),
1989  NEONMAP1(vld1q_v, arm_neon_vld1, 0),
1990  NEONMAP1(vld2_lane_v, arm_neon_vld2lane, 0),
1991  NEONMAP1(vld2_v, arm_neon_vld2, 0),
1992  NEONMAP1(vld2q_lane_v, arm_neon_vld2lane, 0),
1993  NEONMAP1(vld2q_v, arm_neon_vld2, 0),
1994  NEONMAP1(vld3_lane_v, arm_neon_vld3lane, 0),
1995  NEONMAP1(vld3_v, arm_neon_vld3, 0),
1996  NEONMAP1(vld3q_lane_v, arm_neon_vld3lane, 0),
1997  NEONMAP1(vld3q_v, arm_neon_vld3, 0),
1998  NEONMAP1(vld4_lane_v, arm_neon_vld4lane, 0),
1999  NEONMAP1(vld4_v, arm_neon_vld4, 0),
2000  NEONMAP1(vld4q_lane_v, arm_neon_vld4lane, 0),
2001  NEONMAP1(vld4q_v, arm_neon_vld4, 0),
2002  NEONMAP2(vmax_v, arm_neon_vmaxu, arm_neon_vmaxs, Add1ArgType | UnsignedAlts),
2003  NEONMAP2(vmaxq_v, arm_neon_vmaxu, arm_neon_vmaxs, Add1ArgType | UnsignedAlts),
2004  NEONMAP2(vmin_v, arm_neon_vminu, arm_neon_vmins, Add1ArgType | UnsignedAlts),
2005  NEONMAP2(vminq_v, arm_neon_vminu, arm_neon_vmins, Add1ArgType | UnsignedAlts),
2006  NEONMAP0(vmovl_v),
2007  NEONMAP0(vmovn_v),
2008  NEONMAP1(vmul_v, arm_neon_vmulp, Add1ArgType),
2009  NEONMAP0(vmull_v),
2010  NEONMAP1(vmulq_v, arm_neon_vmulp, Add1ArgType),
2011  NEONMAP2(vpadal_v, arm_neon_vpadalu, arm_neon_vpadals, UnsignedAlts),
2012  NEONMAP2(vpadalq_v, arm_neon_vpadalu, arm_neon_vpadals, UnsignedAlts),
2013  NEONMAP1(vpadd_v, arm_neon_vpadd, Add1ArgType),
2014  NEONMAP2(vpaddl_v, arm_neon_vpaddlu, arm_neon_vpaddls, UnsignedAlts),
2015  NEONMAP2(vpaddlq_v, arm_neon_vpaddlu, arm_neon_vpaddls, UnsignedAlts),
2016  NEONMAP1(vpaddq_v, arm_neon_vpadd, Add1ArgType),
2017  NEONMAP2(vpmax_v, arm_neon_vpmaxu, arm_neon_vpmaxs, Add1ArgType | UnsignedAlts),
2018  NEONMAP2(vpmin_v, arm_neon_vpminu, arm_neon_vpmins, Add1ArgType | UnsignedAlts),
2019  NEONMAP1(vqabs_v, arm_neon_vqabs, Add1ArgType),
2020  NEONMAP1(vqabsq_v, arm_neon_vqabs, Add1ArgType),
2021  NEONMAP2(vqadd_v, arm_neon_vqaddu, arm_neon_vqadds, Add1ArgType | UnsignedAlts),
2022  NEONMAP2(vqaddq_v, arm_neon_vqaddu, arm_neon_vqadds, Add1ArgType | UnsignedAlts),
2023  NEONMAP2(vqdmlal_v, arm_neon_vqdmull, arm_neon_vqadds, 0),
2024  NEONMAP2(vqdmlsl_v, arm_neon_vqdmull, arm_neon_vqsubs, 0),
2025  NEONMAP1(vqdmulh_v, arm_neon_vqdmulh, Add1ArgType),
2026  NEONMAP1(vqdmulhq_v, arm_neon_vqdmulh, Add1ArgType),
2027  NEONMAP1(vqdmull_v, arm_neon_vqdmull, Add1ArgType),
2028  NEONMAP2(vqmovn_v, arm_neon_vqmovnu, arm_neon_vqmovns, Add1ArgType | UnsignedAlts),
2029  NEONMAP1(vqmovun_v, arm_neon_vqmovnsu, Add1ArgType),
2030  NEONMAP1(vqneg_v, arm_neon_vqneg, Add1ArgType),
2031  NEONMAP1(vqnegq_v, arm_neon_vqneg, Add1ArgType),
2032  NEONMAP1(vqrdmulh_v, arm_neon_vqrdmulh, Add1ArgType),
2033  NEONMAP1(vqrdmulhq_v, arm_neon_vqrdmulh, Add1ArgType),
2034  NEONMAP2(vqrshl_v, arm_neon_vqrshiftu, arm_neon_vqrshifts, Add1ArgType | UnsignedAlts),
2035  NEONMAP2(vqrshlq_v, arm_neon_vqrshiftu, arm_neon_vqrshifts, Add1ArgType | UnsignedAlts),
2036  NEONMAP2(vqshl_n_v, arm_neon_vqshiftu, arm_neon_vqshifts, UnsignedAlts),
2037  NEONMAP2(vqshl_v, arm_neon_vqshiftu, arm_neon_vqshifts, Add1ArgType | UnsignedAlts),
2038  NEONMAP2(vqshlq_n_v, arm_neon_vqshiftu, arm_neon_vqshifts, UnsignedAlts),
2039  NEONMAP2(vqshlq_v, arm_neon_vqshiftu, arm_neon_vqshifts, Add1ArgType | UnsignedAlts),
2040  NEONMAP2(vqsub_v, arm_neon_vqsubu, arm_neon_vqsubs, Add1ArgType | UnsignedAlts),
2041  NEONMAP2(vqsubq_v, arm_neon_vqsubu, arm_neon_vqsubs, Add1ArgType | UnsignedAlts),
2042  NEONMAP1(vraddhn_v, arm_neon_vraddhn, Add1ArgType),
2043  NEONMAP2(vrecpe_v, arm_neon_vrecpe, arm_neon_vrecpe, 0),
2044  NEONMAP2(vrecpeq_v, arm_neon_vrecpe, arm_neon_vrecpe, 0),
2045  NEONMAP1(vrecps_v, arm_neon_vrecps, Add1ArgType),
2046  NEONMAP1(vrecpsq_v, arm_neon_vrecps, Add1ArgType),
2047  NEONMAP2(vrhadd_v, arm_neon_vrhaddu, arm_neon_vrhadds, Add1ArgType | UnsignedAlts),
2048  NEONMAP2(vrhaddq_v, arm_neon_vrhaddu, arm_neon_vrhadds, Add1ArgType | UnsignedAlts),
2049  NEONMAP2(vrshl_v, arm_neon_vrshiftu, arm_neon_vrshifts, Add1ArgType | UnsignedAlts),
2050  NEONMAP2(vrshlq_v, arm_neon_vrshiftu, arm_neon_vrshifts, Add1ArgType | UnsignedAlts),
2051  NEONMAP2(vrsqrte_v, arm_neon_vrsqrte, arm_neon_vrsqrte, 0),
2052  NEONMAP2(vrsqrteq_v, arm_neon_vrsqrte, arm_neon_vrsqrte, 0),
2053  NEONMAP1(vrsqrts_v, arm_neon_vrsqrts, Add1ArgType),
2054  NEONMAP1(vrsqrtsq_v, arm_neon_vrsqrts, Add1ArgType),
2055  NEONMAP1(vrsubhn_v, arm_neon_vrsubhn, Add1ArgType),
2056  NEONMAP1(vsha1su0q_v, arm_neon_sha1su0, 0),
2057  NEONMAP1(vsha1su1q_v, arm_neon_sha1su1, 0),
2058  NEONMAP1(vsha256h2q_v, arm_neon_sha256h2, 0),
2059  NEONMAP1(vsha256hq_v, arm_neon_sha256h, 0),
2060  NEONMAP1(vsha256su0q_v, arm_neon_sha256su0, 0),
2061  NEONMAP1(vsha256su1q_v, arm_neon_sha256su1, 0),
2062  NEONMAP0(vshl_n_v),
2063  NEONMAP2(vshl_v, arm_neon_vshiftu, arm_neon_vshifts, Add1ArgType | UnsignedAlts),
2064  NEONMAP0(vshll_n_v),
2065  NEONMAP0(vshlq_n_v),
2066  NEONMAP2(vshlq_v, arm_neon_vshiftu, arm_neon_vshifts, Add1ArgType | UnsignedAlts),
2067  NEONMAP0(vshr_n_v),
2068  NEONMAP0(vshrn_n_v),
2069  NEONMAP0(vshrq_n_v),
2070  NEONMAP1(vst1_v, arm_neon_vst1, 0),
2071  NEONMAP1(vst1q_v, arm_neon_vst1, 0),
2072  NEONMAP1(vst2_lane_v, arm_neon_vst2lane, 0),
2073  NEONMAP1(vst2_v, arm_neon_vst2, 0),
2074  NEONMAP1(vst2q_lane_v, arm_neon_vst2lane, 0),
2075  NEONMAP1(vst2q_v, arm_neon_vst2, 0),
2076  NEONMAP1(vst3_lane_v, arm_neon_vst3lane, 0),
2077  NEONMAP1(vst3_v, arm_neon_vst3, 0),
2078  NEONMAP1(vst3q_lane_v, arm_neon_vst3lane, 0),
2079  NEONMAP1(vst3q_v, arm_neon_vst3, 0),
2080  NEONMAP1(vst4_lane_v, arm_neon_vst4lane, 0),
2081  NEONMAP1(vst4_v, arm_neon_vst4, 0),
2082  NEONMAP1(vst4q_lane_v, arm_neon_vst4lane, 0),
2083  NEONMAP1(vst4q_v, arm_neon_vst4, 0),
2084  NEONMAP0(vsubhn_v),
2085  NEONMAP0(vtrn_v),
2086  NEONMAP0(vtrnq_v),
2087  NEONMAP0(vtst_v),
2088  NEONMAP0(vtstq_v),
2089  NEONMAP0(vuzp_v),
2090  NEONMAP0(vuzpq_v),
2091  NEONMAP0(vzip_v),
2092  NEONMAP0(vzipq_v)
2093};
2094
2095static NeonIntrinsicInfo AArch64SIMDIntrinsicMap[] = {
2096  NEONMAP1(vabs_v, aarch64_neon_abs, 0),
2097  NEONMAP1(vabsq_v, aarch64_neon_abs, 0),
2098  NEONMAP0(vaddhn_v),
2099  NEONMAP1(vaesdq_v, aarch64_crypto_aesd, 0),
2100  NEONMAP1(vaeseq_v, aarch64_crypto_aese, 0),
2101  NEONMAP1(vaesimcq_v, aarch64_crypto_aesimc, 0),
2102  NEONMAP1(vaesmcq_v, aarch64_crypto_aesmc, 0),
2103  NEONMAP1(vcage_v, aarch64_neon_facge, 0),
2104  NEONMAP1(vcageq_v, aarch64_neon_facge, 0),
2105  NEONMAP1(vcagt_v, aarch64_neon_facgt, 0),
2106  NEONMAP1(vcagtq_v, aarch64_neon_facgt, 0),
2107  NEONMAP1(vcale_v, aarch64_neon_facge, 0),
2108  NEONMAP1(vcaleq_v, aarch64_neon_facge, 0),
2109  NEONMAP1(vcalt_v, aarch64_neon_facgt, 0),
2110  NEONMAP1(vcaltq_v, aarch64_neon_facgt, 0),
2111  NEONMAP1(vcls_v, aarch64_neon_cls, Add1ArgType),
2112  NEONMAP1(vclsq_v, aarch64_neon_cls, Add1ArgType),
2113  NEONMAP1(vclz_v, ctlz, Add1ArgType),
2114  NEONMAP1(vclzq_v, ctlz, Add1ArgType),
2115  NEONMAP1(vcnt_v, ctpop, Add1ArgType),
2116  NEONMAP1(vcntq_v, ctpop, Add1ArgType),
2117  NEONMAP1(vcvt_f16_v, aarch64_neon_vcvtfp2hf, 0),
2118  NEONMAP1(vcvt_f32_f16, aarch64_neon_vcvthf2fp, 0),
2119  NEONMAP0(vcvt_f32_v),
2120  NEONMAP2(vcvt_n_f32_v, aarch64_neon_vcvtfxu2fp, aarch64_neon_vcvtfxs2fp, 0),
2121  NEONMAP2(vcvt_n_f64_v, aarch64_neon_vcvtfxu2fp, aarch64_neon_vcvtfxs2fp, 0),
2122  NEONMAP1(vcvt_n_s32_v, aarch64_neon_vcvtfp2fxs, 0),
2123  NEONMAP1(vcvt_n_s64_v, aarch64_neon_vcvtfp2fxs, 0),
2124  NEONMAP1(vcvt_n_u32_v, aarch64_neon_vcvtfp2fxu, 0),
2125  NEONMAP1(vcvt_n_u64_v, aarch64_neon_vcvtfp2fxu, 0),
2126  NEONMAP0(vcvtq_f32_v),
2127  NEONMAP2(vcvtq_n_f32_v, aarch64_neon_vcvtfxu2fp, aarch64_neon_vcvtfxs2fp, 0),
2128  NEONMAP2(vcvtq_n_f64_v, aarch64_neon_vcvtfxu2fp, aarch64_neon_vcvtfxs2fp, 0),
2129  NEONMAP1(vcvtq_n_s32_v, aarch64_neon_vcvtfp2fxs, 0),
2130  NEONMAP1(vcvtq_n_s64_v, aarch64_neon_vcvtfp2fxs, 0),
2131  NEONMAP1(vcvtq_n_u32_v, aarch64_neon_vcvtfp2fxu, 0),
2132  NEONMAP1(vcvtq_n_u64_v, aarch64_neon_vcvtfp2fxu, 0),
2133  NEONMAP1(vcvtx_f32_v, aarch64_neon_fcvtxn, AddRetType | Add1ArgType),
2134  NEONMAP0(vext_v),
2135  NEONMAP0(vextq_v),
2136  NEONMAP0(vfma_v),
2137  NEONMAP0(vfmaq_v),
2138  NEONMAP2(vhadd_v, aarch64_neon_uhadd, aarch64_neon_shadd, Add1ArgType | UnsignedAlts),
2139  NEONMAP2(vhaddq_v, aarch64_neon_uhadd, aarch64_neon_shadd, Add1ArgType | UnsignedAlts),
2140  NEONMAP2(vhsub_v, aarch64_neon_uhsub, aarch64_neon_shsub, Add1ArgType | UnsignedAlts),
2141  NEONMAP2(vhsubq_v, aarch64_neon_uhsub, aarch64_neon_shsub, Add1ArgType | UnsignedAlts),
2142  NEONMAP0(vmovl_v),
2143  NEONMAP0(vmovn_v),
2144  NEONMAP1(vmul_v, aarch64_neon_pmul, Add1ArgType),
2145  NEONMAP1(vmulq_v, aarch64_neon_pmul, Add1ArgType),
2146  NEONMAP1(vpadd_v, aarch64_neon_addp, Add1ArgType),
2147  NEONMAP2(vpaddl_v, aarch64_neon_uaddlp, aarch64_neon_saddlp, UnsignedAlts),
2148  NEONMAP2(vpaddlq_v, aarch64_neon_uaddlp, aarch64_neon_saddlp, UnsignedAlts),
2149  NEONMAP1(vpaddq_v, aarch64_neon_addp, Add1ArgType),
2150  NEONMAP1(vqabs_v, aarch64_neon_sqabs, Add1ArgType),
2151  NEONMAP1(vqabsq_v, aarch64_neon_sqabs, Add1ArgType),
2152  NEONMAP2(vqadd_v, aarch64_neon_uqadd, aarch64_neon_sqadd, Add1ArgType | UnsignedAlts),
2153  NEONMAP2(vqaddq_v, aarch64_neon_uqadd, aarch64_neon_sqadd, Add1ArgType | UnsignedAlts),
2154  NEONMAP2(vqdmlal_v, aarch64_neon_sqdmull, aarch64_neon_sqadd, 0),
2155  NEONMAP2(vqdmlsl_v, aarch64_neon_sqdmull, aarch64_neon_sqsub, 0),
2156  NEONMAP1(vqdmulh_v, aarch64_neon_sqdmulh, Add1ArgType),
2157  NEONMAP1(vqdmulhq_v, aarch64_neon_sqdmulh, Add1ArgType),
2158  NEONMAP1(vqdmull_v, aarch64_neon_sqdmull, Add1ArgType),
2159  NEONMAP2(vqmovn_v, aarch64_neon_uqxtn, aarch64_neon_sqxtn, Add1ArgType | UnsignedAlts),
2160  NEONMAP1(vqmovun_v, aarch64_neon_sqxtun, Add1ArgType),
2161  NEONMAP1(vqneg_v, aarch64_neon_sqneg, Add1ArgType),
2162  NEONMAP1(vqnegq_v, aarch64_neon_sqneg, Add1ArgType),
2163  NEONMAP1(vqrdmulh_v, aarch64_neon_sqrdmulh, Add1ArgType),
2164  NEONMAP1(vqrdmulhq_v, aarch64_neon_sqrdmulh, Add1ArgType),
2165  NEONMAP2(vqrshl_v, aarch64_neon_uqrshl, aarch64_neon_sqrshl, Add1ArgType | UnsignedAlts),
2166  NEONMAP2(vqrshlq_v, aarch64_neon_uqrshl, aarch64_neon_sqrshl, Add1ArgType | UnsignedAlts),
2167  NEONMAP2(vqshl_n_v, aarch64_neon_uqshl, aarch64_neon_sqshl, UnsignedAlts),
2168  NEONMAP2(vqshl_v, aarch64_neon_uqshl, aarch64_neon_sqshl, Add1ArgType | UnsignedAlts),
2169  NEONMAP2(vqshlq_n_v, aarch64_neon_uqshl, aarch64_neon_sqshl,UnsignedAlts),
2170  NEONMAP2(vqshlq_v, aarch64_neon_uqshl, aarch64_neon_sqshl, Add1ArgType | UnsignedAlts),
2171  NEONMAP2(vqsub_v, aarch64_neon_uqsub, aarch64_neon_sqsub, Add1ArgType | UnsignedAlts),
2172  NEONMAP2(vqsubq_v, aarch64_neon_uqsub, aarch64_neon_sqsub, Add1ArgType | UnsignedAlts),
2173  NEONMAP1(vraddhn_v, aarch64_neon_raddhn, Add1ArgType),
2174  NEONMAP2(vrecpe_v, aarch64_neon_frecpe, aarch64_neon_urecpe, 0),
2175  NEONMAP2(vrecpeq_v, aarch64_neon_frecpe, aarch64_neon_urecpe, 0),
2176  NEONMAP1(vrecps_v, aarch64_neon_frecps, Add1ArgType),
2177  NEONMAP1(vrecpsq_v, aarch64_neon_frecps, Add1ArgType),
2178  NEONMAP2(vrhadd_v, aarch64_neon_urhadd, aarch64_neon_srhadd, Add1ArgType | UnsignedAlts),
2179  NEONMAP2(vrhaddq_v, aarch64_neon_urhadd, aarch64_neon_srhadd, Add1ArgType | UnsignedAlts),
2180  NEONMAP2(vrshl_v, aarch64_neon_urshl, aarch64_neon_srshl, Add1ArgType | UnsignedAlts),
2181  NEONMAP2(vrshlq_v, aarch64_neon_urshl, aarch64_neon_srshl, Add1ArgType | UnsignedAlts),
2182  NEONMAP2(vrsqrte_v, aarch64_neon_frsqrte, aarch64_neon_ursqrte, 0),
2183  NEONMAP2(vrsqrteq_v, aarch64_neon_frsqrte, aarch64_neon_ursqrte, 0),
2184  NEONMAP1(vrsqrts_v, aarch64_neon_frsqrts, Add1ArgType),
2185  NEONMAP1(vrsqrtsq_v, aarch64_neon_frsqrts, Add1ArgType),
2186  NEONMAP1(vrsubhn_v, aarch64_neon_rsubhn, Add1ArgType),
2187  NEONMAP1(vsha1su0q_v, aarch64_crypto_sha1su0, 0),
2188  NEONMAP1(vsha1su1q_v, aarch64_crypto_sha1su1, 0),
2189  NEONMAP1(vsha256h2q_v, aarch64_crypto_sha256h2, 0),
2190  NEONMAP1(vsha256hq_v, aarch64_crypto_sha256h, 0),
2191  NEONMAP1(vsha256su0q_v, aarch64_crypto_sha256su0, 0),
2192  NEONMAP1(vsha256su1q_v, aarch64_crypto_sha256su1, 0),
2193  NEONMAP0(vshl_n_v),
2194  NEONMAP2(vshl_v, aarch64_neon_ushl, aarch64_neon_sshl, Add1ArgType | UnsignedAlts),
2195  NEONMAP0(vshll_n_v),
2196  NEONMAP0(vshlq_n_v),
2197  NEONMAP2(vshlq_v, aarch64_neon_ushl, aarch64_neon_sshl, Add1ArgType | UnsignedAlts),
2198  NEONMAP0(vshr_n_v),
2199  NEONMAP0(vshrn_n_v),
2200  NEONMAP0(vshrq_n_v),
2201  NEONMAP0(vsubhn_v),
2202  NEONMAP0(vtst_v),
2203  NEONMAP0(vtstq_v),
2204};
2205
2206static NeonIntrinsicInfo AArch64SISDIntrinsicMap[] = {
2207  NEONMAP1(vabdd_f64, aarch64_sisd_fabd, Add1ArgType),
2208  NEONMAP1(vabds_f32, aarch64_sisd_fabd, Add1ArgType),
2209  NEONMAP1(vabsd_s64, aarch64_neon_abs, Add1ArgType),
2210  NEONMAP1(vaddlv_s32, aarch64_neon_saddlv, AddRetType | Add1ArgType),
2211  NEONMAP1(vaddlv_u32, aarch64_neon_uaddlv, AddRetType | Add1ArgType),
2212  NEONMAP1(vaddlvq_s32, aarch64_neon_saddlv, AddRetType | Add1ArgType),
2213  NEONMAP1(vaddlvq_u32, aarch64_neon_uaddlv, AddRetType | Add1ArgType),
2214  NEONMAP1(vaddv_f32, aarch64_neon_faddv, AddRetType | Add1ArgType),
2215  NEONMAP1(vaddv_s32, aarch64_neon_saddv, AddRetType | Add1ArgType),
2216  NEONMAP1(vaddv_u32, aarch64_neon_uaddv, AddRetType | Add1ArgType),
2217  NEONMAP1(vaddvq_f32, aarch64_neon_faddv, AddRetType | Add1ArgType),
2218  NEONMAP1(vaddvq_f64, aarch64_neon_faddv, AddRetType | Add1ArgType),
2219  NEONMAP1(vaddvq_s32, aarch64_neon_saddv, AddRetType | Add1ArgType),
2220  NEONMAP1(vaddvq_s64, aarch64_neon_saddv, AddRetType | Add1ArgType),
2221  NEONMAP1(vaddvq_u32, aarch64_neon_uaddv, AddRetType | Add1ArgType),
2222  NEONMAP1(vaddvq_u64, aarch64_neon_uaddv, AddRetType | Add1ArgType),
2223  NEONMAP1(vcaged_f64, aarch64_neon_facge, AddRetType | Add1ArgType),
2224  NEONMAP1(vcages_f32, aarch64_neon_facge, AddRetType | Add1ArgType),
2225  NEONMAP1(vcagtd_f64, aarch64_neon_facgt, AddRetType | Add1ArgType),
2226  NEONMAP1(vcagts_f32, aarch64_neon_facgt, AddRetType | Add1ArgType),
2227  NEONMAP1(vcaled_f64, aarch64_neon_facge, AddRetType | Add1ArgType),
2228  NEONMAP1(vcales_f32, aarch64_neon_facge, AddRetType | Add1ArgType),
2229  NEONMAP1(vcaltd_f64, aarch64_neon_facgt, AddRetType | Add1ArgType),
2230  NEONMAP1(vcalts_f32, aarch64_neon_facgt, AddRetType | Add1ArgType),
2231  NEONMAP1(vcvtad_s64_f64, aarch64_neon_fcvtas, AddRetType | Add1ArgType),
2232  NEONMAP1(vcvtad_u64_f64, aarch64_neon_fcvtau, AddRetType | Add1ArgType),
2233  NEONMAP1(vcvtas_s32_f32, aarch64_neon_fcvtas, AddRetType | Add1ArgType),
2234  NEONMAP1(vcvtas_u32_f32, aarch64_neon_fcvtau, AddRetType | Add1ArgType),
2235  NEONMAP1(vcvtd_n_f64_s64, aarch64_neon_vcvtfxs2fp, AddRetType | Add1ArgType),
2236  NEONMAP1(vcvtd_n_f64_u64, aarch64_neon_vcvtfxu2fp, AddRetType | Add1ArgType),
2237  NEONMAP1(vcvtd_n_s64_f64, aarch64_neon_vcvtfp2fxs, AddRetType | Add1ArgType),
2238  NEONMAP1(vcvtd_n_u64_f64, aarch64_neon_vcvtfp2fxu, AddRetType | Add1ArgType),
2239  NEONMAP1(vcvtmd_s64_f64, aarch64_neon_fcvtms, AddRetType | Add1ArgType),
2240  NEONMAP1(vcvtmd_u64_f64, aarch64_neon_fcvtmu, AddRetType | Add1ArgType),
2241  NEONMAP1(vcvtms_s32_f32, aarch64_neon_fcvtms, AddRetType | Add1ArgType),
2242  NEONMAP1(vcvtms_u32_f32, aarch64_neon_fcvtmu, AddRetType | Add1ArgType),
2243  NEONMAP1(vcvtnd_s64_f64, aarch64_neon_fcvtns, AddRetType | Add1ArgType),
2244  NEONMAP1(vcvtnd_u64_f64, aarch64_neon_fcvtnu, AddRetType | Add1ArgType),
2245  NEONMAP1(vcvtns_s32_f32, aarch64_neon_fcvtns, AddRetType | Add1ArgType),
2246  NEONMAP1(vcvtns_u32_f32, aarch64_neon_fcvtnu, AddRetType | Add1ArgType),
2247  NEONMAP1(vcvtpd_s64_f64, aarch64_neon_fcvtps, AddRetType | Add1ArgType),
2248  NEONMAP1(vcvtpd_u64_f64, aarch64_neon_fcvtpu, AddRetType | Add1ArgType),
2249  NEONMAP1(vcvtps_s32_f32, aarch64_neon_fcvtps, AddRetType | Add1ArgType),
2250  NEONMAP1(vcvtps_u32_f32, aarch64_neon_fcvtpu, AddRetType | Add1ArgType),
2251  NEONMAP1(vcvts_n_f32_s32, aarch64_neon_vcvtfxs2fp, AddRetType | Add1ArgType),
2252  NEONMAP1(vcvts_n_f32_u32, aarch64_neon_vcvtfxu2fp, AddRetType | Add1ArgType),
2253  NEONMAP1(vcvts_n_s32_f32, aarch64_neon_vcvtfp2fxs, AddRetType | Add1ArgType),
2254  NEONMAP1(vcvts_n_u32_f32, aarch64_neon_vcvtfp2fxu, AddRetType | Add1ArgType),
2255  NEONMAP1(vcvtxd_f32_f64, aarch64_sisd_fcvtxn, 0),
2256  NEONMAP1(vmaxnmv_f32, aarch64_neon_fmaxnmv, AddRetType | Add1ArgType),
2257  NEONMAP1(vmaxnmvq_f32, aarch64_neon_fmaxnmv, AddRetType | Add1ArgType),
2258  NEONMAP1(vmaxnmvq_f64, aarch64_neon_fmaxnmv, AddRetType | Add1ArgType),
2259  NEONMAP1(vmaxv_f32, aarch64_neon_fmaxv, AddRetType | Add1ArgType),
2260  NEONMAP1(vmaxv_s32, aarch64_neon_smaxv, AddRetType | Add1ArgType),
2261  NEONMAP1(vmaxv_u32, aarch64_neon_umaxv, AddRetType | Add1ArgType),
2262  NEONMAP1(vmaxvq_f32, aarch64_neon_fmaxv, AddRetType | Add1ArgType),
2263  NEONMAP1(vmaxvq_f64, aarch64_neon_fmaxv, AddRetType | Add1ArgType),
2264  NEONMAP1(vmaxvq_s32, aarch64_neon_smaxv, AddRetType | Add1ArgType),
2265  NEONMAP1(vmaxvq_u32, aarch64_neon_umaxv, AddRetType | Add1ArgType),
2266  NEONMAP1(vminnmv_f32, aarch64_neon_fminnmv, AddRetType | Add1ArgType),
2267  NEONMAP1(vminnmvq_f32, aarch64_neon_fminnmv, AddRetType | Add1ArgType),
2268  NEONMAP1(vminnmvq_f64, aarch64_neon_fminnmv, AddRetType | Add1ArgType),
2269  NEONMAP1(vminv_f32, aarch64_neon_fminv, AddRetType | Add1ArgType),
2270  NEONMAP1(vminv_s32, aarch64_neon_sminv, AddRetType | Add1ArgType),
2271  NEONMAP1(vminv_u32, aarch64_neon_uminv, AddRetType | Add1ArgType),
2272  NEONMAP1(vminvq_f32, aarch64_neon_fminv, AddRetType | Add1ArgType),
2273  NEONMAP1(vminvq_f64, aarch64_neon_fminv, AddRetType | Add1ArgType),
2274  NEONMAP1(vminvq_s32, aarch64_neon_sminv, AddRetType | Add1ArgType),
2275  NEONMAP1(vminvq_u32, aarch64_neon_uminv, AddRetType | Add1ArgType),
2276  NEONMAP1(vmull_p64, aarch64_neon_pmull64, 0),
2277  NEONMAP1(vmulxd_f64, aarch64_neon_fmulx, Add1ArgType),
2278  NEONMAP1(vmulxs_f32, aarch64_neon_fmulx, Add1ArgType),
2279  NEONMAP1(vpaddd_s64, aarch64_neon_uaddv, AddRetType | Add1ArgType),
2280  NEONMAP1(vpaddd_u64, aarch64_neon_uaddv, AddRetType | Add1ArgType),
2281  NEONMAP1(vpmaxnmqd_f64, aarch64_neon_fmaxnmv, AddRetType | Add1ArgType),
2282  NEONMAP1(vpmaxnms_f32, aarch64_neon_fmaxnmv, AddRetType | Add1ArgType),
2283  NEONMAP1(vpmaxqd_f64, aarch64_neon_fmaxv, AddRetType | Add1ArgType),
2284  NEONMAP1(vpmaxs_f32, aarch64_neon_fmaxv, AddRetType | Add1ArgType),
2285  NEONMAP1(vpminnmqd_f64, aarch64_neon_fminnmv, AddRetType | Add1ArgType),
2286  NEONMAP1(vpminnms_f32, aarch64_neon_fminnmv, AddRetType | Add1ArgType),
2287  NEONMAP1(vpminqd_f64, aarch64_neon_fminv, AddRetType | Add1ArgType),
2288  NEONMAP1(vpmins_f32, aarch64_neon_fminv, AddRetType | Add1ArgType),
2289  NEONMAP1(vqabsb_s8, aarch64_neon_sqabs, Vectorize1ArgType | Use64BitVectors),
2290  NEONMAP1(vqabsd_s64, aarch64_neon_sqabs, Add1ArgType),
2291  NEONMAP1(vqabsh_s16, aarch64_neon_sqabs, Vectorize1ArgType | Use64BitVectors),
2292  NEONMAP1(vqabss_s32, aarch64_neon_sqabs, Add1ArgType),
2293  NEONMAP1(vqaddb_s8, aarch64_neon_sqadd, Vectorize1ArgType | Use64BitVectors),
2294  NEONMAP1(vqaddb_u8, aarch64_neon_uqadd, Vectorize1ArgType | Use64BitVectors),
2295  NEONMAP1(vqaddd_s64, aarch64_neon_sqadd, Add1ArgType),
2296  NEONMAP1(vqaddd_u64, aarch64_neon_uqadd, Add1ArgType),
2297  NEONMAP1(vqaddh_s16, aarch64_neon_sqadd, Vectorize1ArgType | Use64BitVectors),
2298  NEONMAP1(vqaddh_u16, aarch64_neon_uqadd, Vectorize1ArgType | Use64BitVectors),
2299  NEONMAP1(vqadds_s32, aarch64_neon_sqadd, Add1ArgType),
2300  NEONMAP1(vqadds_u32, aarch64_neon_uqadd, Add1ArgType),
2301  NEONMAP1(vqdmulhh_s16, aarch64_neon_sqdmulh, Vectorize1ArgType | Use64BitVectors),
2302  NEONMAP1(vqdmulhs_s32, aarch64_neon_sqdmulh, Add1ArgType),
2303  NEONMAP1(vqdmullh_s16, aarch64_neon_sqdmull, VectorRet | Use128BitVectors),
2304  NEONMAP1(vqdmulls_s32, aarch64_neon_sqdmulls_scalar, 0),
2305  NEONMAP1(vqmovnd_s64, aarch64_neon_scalar_sqxtn, AddRetType | Add1ArgType),
2306  NEONMAP1(vqmovnd_u64, aarch64_neon_scalar_uqxtn, AddRetType | Add1ArgType),
2307  NEONMAP1(vqmovnh_s16, aarch64_neon_sqxtn, VectorRet | Use64BitVectors),
2308  NEONMAP1(vqmovnh_u16, aarch64_neon_uqxtn, VectorRet | Use64BitVectors),
2309  NEONMAP1(vqmovns_s32, aarch64_neon_sqxtn, VectorRet | Use64BitVectors),
2310  NEONMAP1(vqmovns_u32, aarch64_neon_uqxtn, VectorRet | Use64BitVectors),
2311  NEONMAP1(vqmovund_s64, aarch64_neon_scalar_sqxtun, AddRetType | Add1ArgType),
2312  NEONMAP1(vqmovunh_s16, aarch64_neon_sqxtun, VectorRet | Use64BitVectors),
2313  NEONMAP1(vqmovuns_s32, aarch64_neon_sqxtun, VectorRet | Use64BitVectors),
2314  NEONMAP1(vqnegb_s8, aarch64_neon_sqneg, Vectorize1ArgType | Use64BitVectors),
2315  NEONMAP1(vqnegd_s64, aarch64_neon_sqneg, Add1ArgType),
2316  NEONMAP1(vqnegh_s16, aarch64_neon_sqneg, Vectorize1ArgType | Use64BitVectors),
2317  NEONMAP1(vqnegs_s32, aarch64_neon_sqneg, Add1ArgType),
2318  NEONMAP1(vqrdmulhh_s16, aarch64_neon_sqrdmulh, Vectorize1ArgType | Use64BitVectors),
2319  NEONMAP1(vqrdmulhs_s32, aarch64_neon_sqrdmulh, Add1ArgType),
2320  NEONMAP1(vqrshlb_s8, aarch64_neon_sqrshl, Vectorize1ArgType | Use64BitVectors),
2321  NEONMAP1(vqrshlb_u8, aarch64_neon_uqrshl, Vectorize1ArgType | Use64BitVectors),
2322  NEONMAP1(vqrshld_s64, aarch64_neon_sqrshl, Add1ArgType),
2323  NEONMAP1(vqrshld_u64, aarch64_neon_uqrshl, Add1ArgType),
2324  NEONMAP1(vqrshlh_s16, aarch64_neon_sqrshl, Vectorize1ArgType | Use64BitVectors),
2325  NEONMAP1(vqrshlh_u16, aarch64_neon_uqrshl, Vectorize1ArgType | Use64BitVectors),
2326  NEONMAP1(vqrshls_s32, aarch64_neon_sqrshl, Add1ArgType),
2327  NEONMAP1(vqrshls_u32, aarch64_neon_uqrshl, Add1ArgType),
2328  NEONMAP1(vqrshrnd_n_s64, aarch64_neon_sqrshrn, AddRetType),
2329  NEONMAP1(vqrshrnd_n_u64, aarch64_neon_uqrshrn, AddRetType),
2330  NEONMAP1(vqrshrnh_n_s16, aarch64_neon_sqrshrn, VectorRet | Use64BitVectors),
2331  NEONMAP1(vqrshrnh_n_u16, aarch64_neon_uqrshrn, VectorRet | Use64BitVectors),
2332  NEONMAP1(vqrshrns_n_s32, aarch64_neon_sqrshrn, VectorRet | Use64BitVectors),
2333  NEONMAP1(vqrshrns_n_u32, aarch64_neon_uqrshrn, VectorRet | Use64BitVectors),
2334  NEONMAP1(vqrshrund_n_s64, aarch64_neon_sqrshrun, AddRetType),
2335  NEONMAP1(vqrshrunh_n_s16, aarch64_neon_sqrshrun, VectorRet | Use64BitVectors),
2336  NEONMAP1(vqrshruns_n_s32, aarch64_neon_sqrshrun, VectorRet | Use64BitVectors),
2337  NEONMAP1(vqshlb_n_s8, aarch64_neon_sqshl, Vectorize1ArgType | Use64BitVectors),
2338  NEONMAP1(vqshlb_n_u8, aarch64_neon_uqshl, Vectorize1ArgType | Use64BitVectors),
2339  NEONMAP1(vqshlb_s8, aarch64_neon_sqshl, Vectorize1ArgType | Use64BitVectors),
2340  NEONMAP1(vqshlb_u8, aarch64_neon_uqshl, Vectorize1ArgType | Use64BitVectors),
2341  NEONMAP1(vqshld_s64, aarch64_neon_sqshl, Add1ArgType),
2342  NEONMAP1(vqshld_u64, aarch64_neon_uqshl, Add1ArgType),
2343  NEONMAP1(vqshlh_n_s16, aarch64_neon_sqshl, Vectorize1ArgType | Use64BitVectors),
2344  NEONMAP1(vqshlh_n_u16, aarch64_neon_uqshl, Vectorize1ArgType | Use64BitVectors),
2345  NEONMAP1(vqshlh_s16, aarch64_neon_sqshl, Vectorize1ArgType | Use64BitVectors),
2346  NEONMAP1(vqshlh_u16, aarch64_neon_uqshl, Vectorize1ArgType | Use64BitVectors),
2347  NEONMAP1(vqshls_n_s32, aarch64_neon_sqshl, Add1ArgType),
2348  NEONMAP1(vqshls_n_u32, aarch64_neon_uqshl, Add1ArgType),
2349  NEONMAP1(vqshls_s32, aarch64_neon_sqshl, Add1ArgType),
2350  NEONMAP1(vqshls_u32, aarch64_neon_uqshl, Add1ArgType),
2351  NEONMAP1(vqshlub_n_s8, aarch64_neon_sqshlu, Vectorize1ArgType | Use64BitVectors),
2352  NEONMAP1(vqshluh_n_s16, aarch64_neon_sqshlu, Vectorize1ArgType | Use64BitVectors),
2353  NEONMAP1(vqshlus_n_s32, aarch64_neon_sqshlu, Add1ArgType),
2354  NEONMAP1(vqshrnd_n_s64, aarch64_neon_sqshrn, AddRetType),
2355  NEONMAP1(vqshrnd_n_u64, aarch64_neon_uqshrn, AddRetType),
2356  NEONMAP1(vqshrnh_n_s16, aarch64_neon_sqshrn, VectorRet | Use64BitVectors),
2357  NEONMAP1(vqshrnh_n_u16, aarch64_neon_uqshrn, VectorRet | Use64BitVectors),
2358  NEONMAP1(vqshrns_n_s32, aarch64_neon_sqshrn, VectorRet | Use64BitVectors),
2359  NEONMAP1(vqshrns_n_u32, aarch64_neon_uqshrn, VectorRet | Use64BitVectors),
2360  NEONMAP1(vqshrund_n_s64, aarch64_neon_sqshrun, AddRetType),
2361  NEONMAP1(vqshrunh_n_s16, aarch64_neon_sqshrun, VectorRet | Use64BitVectors),
2362  NEONMAP1(vqshruns_n_s32, aarch64_neon_sqshrun, VectorRet | Use64BitVectors),
2363  NEONMAP1(vqsubb_s8, aarch64_neon_sqsub, Vectorize1ArgType | Use64BitVectors),
2364  NEONMAP1(vqsubb_u8, aarch64_neon_uqsub, Vectorize1ArgType | Use64BitVectors),
2365  NEONMAP1(vqsubd_s64, aarch64_neon_sqsub, Add1ArgType),
2366  NEONMAP1(vqsubd_u64, aarch64_neon_uqsub, Add1ArgType),
2367  NEONMAP1(vqsubh_s16, aarch64_neon_sqsub, Vectorize1ArgType | Use64BitVectors),
2368  NEONMAP1(vqsubh_u16, aarch64_neon_uqsub, Vectorize1ArgType | Use64BitVectors),
2369  NEONMAP1(vqsubs_s32, aarch64_neon_sqsub, Add1ArgType),
2370  NEONMAP1(vqsubs_u32, aarch64_neon_uqsub, Add1ArgType),
2371  NEONMAP1(vrecped_f64, aarch64_neon_frecpe, Add1ArgType),
2372  NEONMAP1(vrecpes_f32, aarch64_neon_frecpe, Add1ArgType),
2373  NEONMAP1(vrecpxd_f64, aarch64_neon_frecpx, Add1ArgType),
2374  NEONMAP1(vrecpxs_f32, aarch64_neon_frecpx, Add1ArgType),
2375  NEONMAP1(vrshld_s64, aarch64_neon_srshl, Add1ArgType),
2376  NEONMAP1(vrshld_u64, aarch64_neon_urshl, Add1ArgType),
2377  NEONMAP1(vrsqrted_f64, aarch64_neon_frsqrte, Add1ArgType),
2378  NEONMAP1(vrsqrtes_f32, aarch64_neon_frsqrte, Add1ArgType),
2379  NEONMAP1(vrsqrtsd_f64, aarch64_neon_frsqrts, Add1ArgType),
2380  NEONMAP1(vrsqrtss_f32, aarch64_neon_frsqrts, Add1ArgType),
2381  NEONMAP1(vsha1cq_u32, aarch64_crypto_sha1c, 0),
2382  NEONMAP1(vsha1h_u32, aarch64_crypto_sha1h, 0),
2383  NEONMAP1(vsha1mq_u32, aarch64_crypto_sha1m, 0),
2384  NEONMAP1(vsha1pq_u32, aarch64_crypto_sha1p, 0),
2385  NEONMAP1(vshld_s64, aarch64_neon_sshl, Add1ArgType),
2386  NEONMAP1(vshld_u64, aarch64_neon_ushl, Add1ArgType),
2387  NEONMAP1(vslid_n_s64, aarch64_neon_vsli, Vectorize1ArgType),
2388  NEONMAP1(vslid_n_u64, aarch64_neon_vsli, Vectorize1ArgType),
2389  NEONMAP1(vsqaddb_u8, aarch64_neon_usqadd, Vectorize1ArgType | Use64BitVectors),
2390  NEONMAP1(vsqaddd_u64, aarch64_neon_usqadd, Add1ArgType),
2391  NEONMAP1(vsqaddh_u16, aarch64_neon_usqadd, Vectorize1ArgType | Use64BitVectors),
2392  NEONMAP1(vsqadds_u32, aarch64_neon_usqadd, Add1ArgType),
2393  NEONMAP1(vsrid_n_s64, aarch64_neon_vsri, Vectorize1ArgType),
2394  NEONMAP1(vsrid_n_u64, aarch64_neon_vsri, Vectorize1ArgType),
2395  NEONMAP1(vuqaddb_s8, aarch64_neon_suqadd, Vectorize1ArgType | Use64BitVectors),
2396  NEONMAP1(vuqaddd_s64, aarch64_neon_suqadd, Add1ArgType),
2397  NEONMAP1(vuqaddh_s16, aarch64_neon_suqadd, Vectorize1ArgType | Use64BitVectors),
2398  NEONMAP1(vuqadds_s32, aarch64_neon_suqadd, Add1ArgType),
2399};
2400
2401#undef NEONMAP0
2402#undef NEONMAP1
2403#undef NEONMAP2
2404
2405static bool NEONSIMDIntrinsicsProvenSorted = false;
2406
2407static bool AArch64SIMDIntrinsicsProvenSorted = false;
2408static bool AArch64SISDIntrinsicsProvenSorted = false;
2409
2410
2411static const NeonIntrinsicInfo *
2412findNeonIntrinsicInMap(ArrayRef<NeonIntrinsicInfo> IntrinsicMap,
2413                       unsigned BuiltinID, bool &MapProvenSorted) {
2414
2415#ifndef NDEBUG
2416  if (!MapProvenSorted) {
2417    // FIXME: use std::is_sorted once C++11 is allowed
2418    for (unsigned i = 0; i < IntrinsicMap.size() - 1; ++i)
2419      assert(IntrinsicMap[i].BuiltinID <= IntrinsicMap[i + 1].BuiltinID);
2420    MapProvenSorted = true;
2421  }
2422#endif
2423
2424  const NeonIntrinsicInfo *Builtin =
2425      std::lower_bound(IntrinsicMap.begin(), IntrinsicMap.end(), BuiltinID);
2426
2427  if (Builtin != IntrinsicMap.end() && Builtin->BuiltinID == BuiltinID)
2428    return Builtin;
2429
2430  return nullptr;
2431}
2432
2433Function *CodeGenFunction::LookupNeonLLVMIntrinsic(unsigned IntrinsicID,
2434                                                   unsigned Modifier,
2435                                                   llvm::Type *ArgType,
2436                                                   const CallExpr *E) {
2437  int VectorSize = 0;
2438  if (Modifier & Use64BitVectors)
2439    VectorSize = 64;
2440  else if (Modifier & Use128BitVectors)
2441    VectorSize = 128;
2442
2443  // Return type.
2444  SmallVector<llvm::Type *, 3> Tys;
2445  if (Modifier & AddRetType) {
2446    llvm::Type *Ty = ConvertType(E->getCallReturnType());
2447    if (Modifier & VectorizeRetType)
2448      Ty = llvm::VectorType::get(
2449          Ty, VectorSize ? VectorSize / Ty->getPrimitiveSizeInBits() : 1);
2450
2451    Tys.push_back(Ty);
2452  }
2453
2454  // Arguments.
2455  if (Modifier & VectorizeArgTypes) {
2456    int Elts = VectorSize ? VectorSize / ArgType->getPrimitiveSizeInBits() : 1;
2457    ArgType = llvm::VectorType::get(ArgType, Elts);
2458  }
2459
2460  if (Modifier & (Add1ArgType | Add2ArgTypes))
2461    Tys.push_back(ArgType);
2462
2463  if (Modifier & Add2ArgTypes)
2464    Tys.push_back(ArgType);
2465
2466  if (Modifier & InventFloatType)
2467    Tys.push_back(FloatTy);
2468
2469  return CGM.getIntrinsic(IntrinsicID, Tys);
2470}
2471
2472static Value *EmitCommonNeonSISDBuiltinExpr(CodeGenFunction &CGF,
2473                                            const NeonIntrinsicInfo &SISDInfo,
2474                                            SmallVectorImpl<Value *> &Ops,
2475                                            const CallExpr *E) {
2476  unsigned BuiltinID = SISDInfo.BuiltinID;
2477  unsigned int Int = SISDInfo.LLVMIntrinsic;
2478  unsigned Modifier = SISDInfo.TypeModifier;
2479  const char *s = SISDInfo.NameHint;
2480
2481  switch (BuiltinID) {
2482  case NEON::BI__builtin_neon_vcled_s64:
2483  case NEON::BI__builtin_neon_vcled_u64:
2484  case NEON::BI__builtin_neon_vcles_f32:
2485  case NEON::BI__builtin_neon_vcled_f64:
2486  case NEON::BI__builtin_neon_vcltd_s64:
2487  case NEON::BI__builtin_neon_vcltd_u64:
2488  case NEON::BI__builtin_neon_vclts_f32:
2489  case NEON::BI__builtin_neon_vcltd_f64:
2490  case NEON::BI__builtin_neon_vcales_f32:
2491  case NEON::BI__builtin_neon_vcaled_f64:
2492  case NEON::BI__builtin_neon_vcalts_f32:
2493  case NEON::BI__builtin_neon_vcaltd_f64:
2494    // Only one direction of comparisons actually exist, cmle is actually a cmge
2495    // with swapped operands. The table gives us the right intrinsic but we
2496    // still need to do the swap.
2497    std::swap(Ops[0], Ops[1]);
2498    break;
2499  }
2500
2501  assert(Int && "Generic code assumes a valid intrinsic");
2502
2503  // Determine the type(s) of this overloaded AArch64 intrinsic.
2504  const Expr *Arg = E->getArg(0);
2505  llvm::Type *ArgTy = CGF.ConvertType(Arg->getType());
2506  Function *F = CGF.LookupNeonLLVMIntrinsic(Int, Modifier, ArgTy, E);
2507
2508  int j = 0;
2509  ConstantInt *C0 = ConstantInt::get(CGF.SizeTy, 0);
2510  for (Function::const_arg_iterator ai = F->arg_begin(), ae = F->arg_end();
2511       ai != ae; ++ai, ++j) {
2512    llvm::Type *ArgTy = ai->getType();
2513    if (Ops[j]->getType()->getPrimitiveSizeInBits() ==
2514             ArgTy->getPrimitiveSizeInBits())
2515      continue;
2516
2517    assert(ArgTy->isVectorTy() && !Ops[j]->getType()->isVectorTy());
2518    // The constant argument to an _n_ intrinsic always has Int32Ty, so truncate
2519    // it before inserting.
2520    Ops[j] =
2521        CGF.Builder.CreateTruncOrBitCast(Ops[j], ArgTy->getVectorElementType());
2522    Ops[j] =
2523        CGF.Builder.CreateInsertElement(UndefValue::get(ArgTy), Ops[j], C0);
2524  }
2525
2526  Value *Result = CGF.EmitNeonCall(F, Ops, s);
2527  llvm::Type *ResultType = CGF.ConvertType(E->getType());
2528  if (ResultType->getPrimitiveSizeInBits() <
2529      Result->getType()->getPrimitiveSizeInBits())
2530    return CGF.Builder.CreateExtractElement(Result, C0);
2531
2532  return CGF.Builder.CreateBitCast(Result, ResultType, s);
2533}
2534
2535Value *CodeGenFunction::EmitCommonNeonBuiltinExpr(
2536    unsigned BuiltinID, unsigned LLVMIntrinsic, unsigned AltLLVMIntrinsic,
2537    const char *NameHint, unsigned Modifier, const CallExpr *E,
2538    SmallVectorImpl<llvm::Value *> &Ops, llvm::Value *Align) {
2539  // Get the last argument, which specifies the vector type.
2540  llvm::APSInt NeonTypeConst;
2541  const Expr *Arg = E->getArg(E->getNumArgs() - 1);
2542  if (!Arg->isIntegerConstantExpr(NeonTypeConst, getContext()))
2543    return nullptr;
2544
2545  // Determine the type of this overloaded NEON intrinsic.
2546  NeonTypeFlags Type(NeonTypeConst.getZExtValue());
2547  bool Usgn = Type.isUnsigned();
2548  bool Quad = Type.isQuad();
2549
2550  llvm::VectorType *VTy = GetNeonType(this, Type);
2551  llvm::Type *Ty = VTy;
2552  if (!Ty)
2553    return nullptr;
2554
2555  unsigned Int = LLVMIntrinsic;
2556  if ((Modifier & UnsignedAlts) && !Usgn)
2557    Int = AltLLVMIntrinsic;
2558
2559  switch (BuiltinID) {
2560  default: break;
2561  case NEON::BI__builtin_neon_vabs_v:
2562  case NEON::BI__builtin_neon_vabsq_v:
2563    if (VTy->getElementType()->isFloatingPointTy())
2564      return EmitNeonCall(CGM.getIntrinsic(Intrinsic::fabs, Ty), Ops, "vabs");
2565    return EmitNeonCall(CGM.getIntrinsic(LLVMIntrinsic, Ty), Ops, "vabs");
2566  case NEON::BI__builtin_neon_vaddhn_v: {
2567    llvm::VectorType *SrcTy =
2568        llvm::VectorType::getExtendedElementVectorType(VTy);
2569
2570    // %sum = add <4 x i32> %lhs, %rhs
2571    Ops[0] = Builder.CreateBitCast(Ops[0], SrcTy);
2572    Ops[1] = Builder.CreateBitCast(Ops[1], SrcTy);
2573    Ops[0] = Builder.CreateAdd(Ops[0], Ops[1], "vaddhn");
2574
2575    // %high = lshr <4 x i32> %sum, <i32 16, i32 16, i32 16, i32 16>
2576    Constant *ShiftAmt = ConstantInt::get(SrcTy->getElementType(),
2577                                       SrcTy->getScalarSizeInBits() / 2);
2578    ShiftAmt = ConstantVector::getSplat(VTy->getNumElements(), ShiftAmt);
2579    Ops[0] = Builder.CreateLShr(Ops[0], ShiftAmt, "vaddhn");
2580
2581    // %res = trunc <4 x i32> %high to <4 x i16>
2582    return Builder.CreateTrunc(Ops[0], VTy, "vaddhn");
2583  }
2584  case NEON::BI__builtin_neon_vcale_v:
2585  case NEON::BI__builtin_neon_vcaleq_v:
2586  case NEON::BI__builtin_neon_vcalt_v:
2587  case NEON::BI__builtin_neon_vcaltq_v:
2588    std::swap(Ops[0], Ops[1]);
2589  case NEON::BI__builtin_neon_vcage_v:
2590  case NEON::BI__builtin_neon_vcageq_v:
2591  case NEON::BI__builtin_neon_vcagt_v:
2592  case NEON::BI__builtin_neon_vcagtq_v: {
2593    llvm::Type *VecFlt = llvm::VectorType::get(
2594        VTy->getScalarSizeInBits() == 32 ? FloatTy : DoubleTy,
2595        VTy->getNumElements());
2596    llvm::Type *Tys[] = { VTy, VecFlt };
2597    Function *F = CGM.getIntrinsic(LLVMIntrinsic, Tys);
2598    return EmitNeonCall(F, Ops, NameHint);
2599  }
2600  case NEON::BI__builtin_neon_vclz_v:
2601  case NEON::BI__builtin_neon_vclzq_v:
2602    // We generate target-independent intrinsic, which needs a second argument
2603    // for whether or not clz of zero is undefined; on ARM it isn't.
2604    Ops.push_back(Builder.getInt1(getTarget().isCLZForZeroUndef()));
2605    break;
2606  case NEON::BI__builtin_neon_vcvt_f32_v:
2607  case NEON::BI__builtin_neon_vcvtq_f32_v:
2608    Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
2609    Ty = GetNeonType(this, NeonTypeFlags(NeonTypeFlags::Float32, false, Quad));
2610    return Usgn ? Builder.CreateUIToFP(Ops[0], Ty, "vcvt")
2611                : Builder.CreateSIToFP(Ops[0], Ty, "vcvt");
2612  case NEON::BI__builtin_neon_vcvt_n_f32_v:
2613  case NEON::BI__builtin_neon_vcvt_n_f64_v:
2614  case NEON::BI__builtin_neon_vcvtq_n_f32_v:
2615  case NEON::BI__builtin_neon_vcvtq_n_f64_v: {
2616    bool Double =
2617      (cast<llvm::IntegerType>(VTy->getElementType())->getBitWidth() == 64);
2618    llvm::Type *FloatTy =
2619        GetNeonType(this, NeonTypeFlags(Double ? NeonTypeFlags::Float64
2620                                               : NeonTypeFlags::Float32,
2621                                        false, Quad));
2622    llvm::Type *Tys[2] = { FloatTy, Ty };
2623    Int = Usgn ? LLVMIntrinsic : AltLLVMIntrinsic;
2624    Function *F = CGM.getIntrinsic(Int, Tys);
2625    return EmitNeonCall(F, Ops, "vcvt_n");
2626  }
2627  case NEON::BI__builtin_neon_vcvt_n_s32_v:
2628  case NEON::BI__builtin_neon_vcvt_n_u32_v:
2629  case NEON::BI__builtin_neon_vcvt_n_s64_v:
2630  case NEON::BI__builtin_neon_vcvt_n_u64_v:
2631  case NEON::BI__builtin_neon_vcvtq_n_s32_v:
2632  case NEON::BI__builtin_neon_vcvtq_n_u32_v:
2633  case NEON::BI__builtin_neon_vcvtq_n_s64_v:
2634  case NEON::BI__builtin_neon_vcvtq_n_u64_v: {
2635    bool Double =
2636      (cast<llvm::IntegerType>(VTy->getElementType())->getBitWidth() == 64);
2637    llvm::Type *FloatTy =
2638        GetNeonType(this, NeonTypeFlags(Double ? NeonTypeFlags::Float64
2639                                               : NeonTypeFlags::Float32,
2640                                        false, Quad));
2641    llvm::Type *Tys[2] = { Ty, FloatTy };
2642    Function *F = CGM.getIntrinsic(LLVMIntrinsic, Tys);
2643    return EmitNeonCall(F, Ops, "vcvt_n");
2644  }
2645  case NEON::BI__builtin_neon_vcvt_s32_v:
2646  case NEON::BI__builtin_neon_vcvt_u32_v:
2647  case NEON::BI__builtin_neon_vcvt_s64_v:
2648  case NEON::BI__builtin_neon_vcvt_u64_v:
2649  case NEON::BI__builtin_neon_vcvtq_s32_v:
2650  case NEON::BI__builtin_neon_vcvtq_u32_v:
2651  case NEON::BI__builtin_neon_vcvtq_s64_v:
2652  case NEON::BI__builtin_neon_vcvtq_u64_v: {
2653    bool Double =
2654      (cast<llvm::IntegerType>(VTy->getElementType())->getBitWidth() == 64);
2655    llvm::Type *FloatTy =
2656        GetNeonType(this, NeonTypeFlags(Double ? NeonTypeFlags::Float64
2657                                               : NeonTypeFlags::Float32,
2658                                        false, Quad));
2659    Ops[0] = Builder.CreateBitCast(Ops[0], FloatTy);
2660    return Usgn ? Builder.CreateFPToUI(Ops[0], Ty, "vcvt")
2661                : Builder.CreateFPToSI(Ops[0], Ty, "vcvt");
2662  }
2663  case NEON::BI__builtin_neon_vcvta_s32_v:
2664  case NEON::BI__builtin_neon_vcvta_s64_v:
2665  case NEON::BI__builtin_neon_vcvta_u32_v:
2666  case NEON::BI__builtin_neon_vcvta_u64_v:
2667  case NEON::BI__builtin_neon_vcvtaq_s32_v:
2668  case NEON::BI__builtin_neon_vcvtaq_s64_v:
2669  case NEON::BI__builtin_neon_vcvtaq_u32_v:
2670  case NEON::BI__builtin_neon_vcvtaq_u64_v:
2671  case NEON::BI__builtin_neon_vcvtn_s32_v:
2672  case NEON::BI__builtin_neon_vcvtn_s64_v:
2673  case NEON::BI__builtin_neon_vcvtn_u32_v:
2674  case NEON::BI__builtin_neon_vcvtn_u64_v:
2675  case NEON::BI__builtin_neon_vcvtnq_s32_v:
2676  case NEON::BI__builtin_neon_vcvtnq_s64_v:
2677  case NEON::BI__builtin_neon_vcvtnq_u32_v:
2678  case NEON::BI__builtin_neon_vcvtnq_u64_v:
2679  case NEON::BI__builtin_neon_vcvtp_s32_v:
2680  case NEON::BI__builtin_neon_vcvtp_s64_v:
2681  case NEON::BI__builtin_neon_vcvtp_u32_v:
2682  case NEON::BI__builtin_neon_vcvtp_u64_v:
2683  case NEON::BI__builtin_neon_vcvtpq_s32_v:
2684  case NEON::BI__builtin_neon_vcvtpq_s64_v:
2685  case NEON::BI__builtin_neon_vcvtpq_u32_v:
2686  case NEON::BI__builtin_neon_vcvtpq_u64_v:
2687  case NEON::BI__builtin_neon_vcvtm_s32_v:
2688  case NEON::BI__builtin_neon_vcvtm_s64_v:
2689  case NEON::BI__builtin_neon_vcvtm_u32_v:
2690  case NEON::BI__builtin_neon_vcvtm_u64_v:
2691  case NEON::BI__builtin_neon_vcvtmq_s32_v:
2692  case NEON::BI__builtin_neon_vcvtmq_s64_v:
2693  case NEON::BI__builtin_neon_vcvtmq_u32_v:
2694  case NEON::BI__builtin_neon_vcvtmq_u64_v: {
2695    bool Double =
2696      (cast<llvm::IntegerType>(VTy->getElementType())->getBitWidth() == 64);
2697    llvm::Type *InTy =
2698      GetNeonType(this,
2699                  NeonTypeFlags(Double ? NeonTypeFlags::Float64
2700                                : NeonTypeFlags::Float32, false, Quad));
2701    llvm::Type *Tys[2] = { Ty, InTy };
2702    return EmitNeonCall(CGM.getIntrinsic(LLVMIntrinsic, Tys), Ops, NameHint);
2703  }
2704  case NEON::BI__builtin_neon_vext_v:
2705  case NEON::BI__builtin_neon_vextq_v: {
2706    int CV = cast<ConstantInt>(Ops[2])->getSExtValue();
2707    SmallVector<Constant*, 16> Indices;
2708    for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i)
2709      Indices.push_back(ConstantInt::get(Int32Ty, i+CV));
2710
2711    Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
2712    Ops[1] = Builder.CreateBitCast(Ops[1], Ty);
2713    Value *SV = llvm::ConstantVector::get(Indices);
2714    return Builder.CreateShuffleVector(Ops[0], Ops[1], SV, "vext");
2715  }
2716  case NEON::BI__builtin_neon_vfma_v:
2717  case NEON::BI__builtin_neon_vfmaq_v: {
2718    Value *F = CGM.getIntrinsic(Intrinsic::fma, Ty);
2719    Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
2720    Ops[1] = Builder.CreateBitCast(Ops[1], Ty);
2721    Ops[2] = Builder.CreateBitCast(Ops[2], Ty);
2722
2723    // NEON intrinsic puts accumulator first, unlike the LLVM fma.
2724    return Builder.CreateCall3(F, Ops[1], Ops[2], Ops[0]);
2725  }
2726  case NEON::BI__builtin_neon_vld1_v:
2727  case NEON::BI__builtin_neon_vld1q_v:
2728    Ops.push_back(Align);
2729    return EmitNeonCall(CGM.getIntrinsic(LLVMIntrinsic, Ty), Ops, "vld1");
2730  case NEON::BI__builtin_neon_vld2_v:
2731  case NEON::BI__builtin_neon_vld2q_v:
2732  case NEON::BI__builtin_neon_vld3_v:
2733  case NEON::BI__builtin_neon_vld3q_v:
2734  case NEON::BI__builtin_neon_vld4_v:
2735  case NEON::BI__builtin_neon_vld4q_v: {
2736    Function *F = CGM.getIntrinsic(LLVMIntrinsic, Ty);
2737    Ops[1] = Builder.CreateCall2(F, Ops[1], Align, NameHint);
2738    Ty = llvm::PointerType::getUnqual(Ops[1]->getType());
2739    Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
2740    return Builder.CreateStore(Ops[1], Ops[0]);
2741  }
2742  case NEON::BI__builtin_neon_vld1_dup_v:
2743  case NEON::BI__builtin_neon_vld1q_dup_v: {
2744    Value *V = UndefValue::get(Ty);
2745    Ty = llvm::PointerType::getUnqual(VTy->getElementType());
2746    Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
2747    LoadInst *Ld = Builder.CreateLoad(Ops[0]);
2748    Ld->setAlignment(cast<ConstantInt>(Align)->getZExtValue());
2749    llvm::Constant *CI = ConstantInt::get(SizeTy, 0);
2750    Ops[0] = Builder.CreateInsertElement(V, Ld, CI);
2751    return EmitNeonSplat(Ops[0], CI);
2752  }
2753  case NEON::BI__builtin_neon_vld2_lane_v:
2754  case NEON::BI__builtin_neon_vld2q_lane_v:
2755  case NEON::BI__builtin_neon_vld3_lane_v:
2756  case NEON::BI__builtin_neon_vld3q_lane_v:
2757  case NEON::BI__builtin_neon_vld4_lane_v:
2758  case NEON::BI__builtin_neon_vld4q_lane_v: {
2759    Function *F = CGM.getIntrinsic(LLVMIntrinsic, Ty);
2760    for (unsigned I = 2; I < Ops.size() - 1; ++I)
2761      Ops[I] = Builder.CreateBitCast(Ops[I], Ty);
2762    Ops.push_back(Align);
2763    Ops[1] = Builder.CreateCall(F, makeArrayRef(Ops).slice(1), NameHint);
2764    Ty = llvm::PointerType::getUnqual(Ops[1]->getType());
2765    Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
2766    return Builder.CreateStore(Ops[1], Ops[0]);
2767  }
2768  case NEON::BI__builtin_neon_vmovl_v: {
2769    llvm::Type *DTy =llvm::VectorType::getTruncatedElementVectorType(VTy);
2770    Ops[0] = Builder.CreateBitCast(Ops[0], DTy);
2771    if (Usgn)
2772      return Builder.CreateZExt(Ops[0], Ty, "vmovl");
2773    return Builder.CreateSExt(Ops[0], Ty, "vmovl");
2774  }
2775  case NEON::BI__builtin_neon_vmovn_v: {
2776    llvm::Type *QTy = llvm::VectorType::getExtendedElementVectorType(VTy);
2777    Ops[0] = Builder.CreateBitCast(Ops[0], QTy);
2778    return Builder.CreateTrunc(Ops[0], Ty, "vmovn");
2779  }
2780  case NEON::BI__builtin_neon_vmull_v:
2781    // FIXME: the integer vmull operations could be emitted in terms of pure
2782    // LLVM IR (2 exts followed by a mul). Unfortunately LLVM has a habit of
2783    // hoisting the exts outside loops. Until global ISel comes along that can
2784    // see through such movement this leads to bad CodeGen. So we need an
2785    // intrinsic for now.
2786    Int = Usgn ? Intrinsic::arm_neon_vmullu : Intrinsic::arm_neon_vmulls;
2787    Int = Type.isPoly() ? (unsigned)Intrinsic::arm_neon_vmullp : Int;
2788    return EmitNeonCall(CGM.getIntrinsic(Int, Ty), Ops, "vmull");
2789  case NEON::BI__builtin_neon_vpadal_v:
2790  case NEON::BI__builtin_neon_vpadalq_v: {
2791    // The source operand type has twice as many elements of half the size.
2792    unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
2793    llvm::Type *EltTy =
2794      llvm::IntegerType::get(getLLVMContext(), EltBits / 2);
2795    llvm::Type *NarrowTy =
2796      llvm::VectorType::get(EltTy, VTy->getNumElements() * 2);
2797    llvm::Type *Tys[2] = { Ty, NarrowTy };
2798    return EmitNeonCall(CGM.getIntrinsic(Int, Tys), Ops, NameHint);
2799  }
2800  case NEON::BI__builtin_neon_vpaddl_v:
2801  case NEON::BI__builtin_neon_vpaddlq_v: {
2802    // The source operand type has twice as many elements of half the size.
2803    unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
2804    llvm::Type *EltTy = llvm::IntegerType::get(getLLVMContext(), EltBits / 2);
2805    llvm::Type *NarrowTy =
2806      llvm::VectorType::get(EltTy, VTy->getNumElements() * 2);
2807    llvm::Type *Tys[2] = { Ty, NarrowTy };
2808    return EmitNeonCall(CGM.getIntrinsic(Int, Tys), Ops, "vpaddl");
2809  }
2810  case NEON::BI__builtin_neon_vqdmlal_v:
2811  case NEON::BI__builtin_neon_vqdmlsl_v: {
2812    SmallVector<Value *, 2> MulOps(Ops.begin() + 1, Ops.end());
2813    Value *Mul = EmitNeonCall(CGM.getIntrinsic(LLVMIntrinsic, Ty),
2814                              MulOps, "vqdmlal");
2815
2816    SmallVector<Value *, 2> AccumOps;
2817    AccumOps.push_back(Ops[0]);
2818    AccumOps.push_back(Mul);
2819    return EmitNeonCall(CGM.getIntrinsic(AltLLVMIntrinsic, Ty),
2820                        AccumOps, NameHint);
2821  }
2822  case NEON::BI__builtin_neon_vqshl_n_v:
2823  case NEON::BI__builtin_neon_vqshlq_n_v:
2824    return EmitNeonCall(CGM.getIntrinsic(Int, Ty), Ops, "vqshl_n",
2825                        1, false);
2826  case NEON::BI__builtin_neon_vrecpe_v:
2827  case NEON::BI__builtin_neon_vrecpeq_v:
2828  case NEON::BI__builtin_neon_vrsqrte_v:
2829  case NEON::BI__builtin_neon_vrsqrteq_v:
2830    Int = Ty->isFPOrFPVectorTy() ? LLVMIntrinsic : AltLLVMIntrinsic;
2831    return EmitNeonCall(CGM.getIntrinsic(Int, Ty), Ops, NameHint);
2832
2833  case NEON::BI__builtin_neon_vshl_n_v:
2834  case NEON::BI__builtin_neon_vshlq_n_v:
2835    Ops[1] = EmitNeonShiftVector(Ops[1], Ty, false);
2836    return Builder.CreateShl(Builder.CreateBitCast(Ops[0],Ty), Ops[1],
2837                             "vshl_n");
2838  case NEON::BI__builtin_neon_vshll_n_v: {
2839    llvm::Type *SrcTy = llvm::VectorType::getTruncatedElementVectorType(VTy);
2840    Ops[0] = Builder.CreateBitCast(Ops[0], SrcTy);
2841    if (Usgn)
2842      Ops[0] = Builder.CreateZExt(Ops[0], VTy);
2843    else
2844      Ops[0] = Builder.CreateSExt(Ops[0], VTy);
2845    Ops[1] = EmitNeonShiftVector(Ops[1], VTy, false);
2846    return Builder.CreateShl(Ops[0], Ops[1], "vshll_n");
2847  }
2848  case NEON::BI__builtin_neon_vshrn_n_v: {
2849    llvm::Type *SrcTy = llvm::VectorType::getExtendedElementVectorType(VTy);
2850    Ops[0] = Builder.CreateBitCast(Ops[0], SrcTy);
2851    Ops[1] = EmitNeonShiftVector(Ops[1], SrcTy, false);
2852    if (Usgn)
2853      Ops[0] = Builder.CreateLShr(Ops[0], Ops[1]);
2854    else
2855      Ops[0] = Builder.CreateAShr(Ops[0], Ops[1]);
2856    return Builder.CreateTrunc(Ops[0], Ty, "vshrn_n");
2857  }
2858  case NEON::BI__builtin_neon_vshr_n_v:
2859  case NEON::BI__builtin_neon_vshrq_n_v:
2860    return EmitNeonRShiftImm(Ops[0], Ops[1], Ty, Usgn, "vshr_n");
2861  case NEON::BI__builtin_neon_vst1_v:
2862  case NEON::BI__builtin_neon_vst1q_v:
2863  case NEON::BI__builtin_neon_vst2_v:
2864  case NEON::BI__builtin_neon_vst2q_v:
2865  case NEON::BI__builtin_neon_vst3_v:
2866  case NEON::BI__builtin_neon_vst3q_v:
2867  case NEON::BI__builtin_neon_vst4_v:
2868  case NEON::BI__builtin_neon_vst4q_v:
2869  case NEON::BI__builtin_neon_vst2_lane_v:
2870  case NEON::BI__builtin_neon_vst2q_lane_v:
2871  case NEON::BI__builtin_neon_vst3_lane_v:
2872  case NEON::BI__builtin_neon_vst3q_lane_v:
2873  case NEON::BI__builtin_neon_vst4_lane_v:
2874  case NEON::BI__builtin_neon_vst4q_lane_v:
2875    Ops.push_back(Align);
2876    return EmitNeonCall(CGM.getIntrinsic(Int, Ty), Ops, "");
2877  case NEON::BI__builtin_neon_vsubhn_v: {
2878    llvm::VectorType *SrcTy =
2879        llvm::VectorType::getExtendedElementVectorType(VTy);
2880
2881    // %sum = add <4 x i32> %lhs, %rhs
2882    Ops[0] = Builder.CreateBitCast(Ops[0], SrcTy);
2883    Ops[1] = Builder.CreateBitCast(Ops[1], SrcTy);
2884    Ops[0] = Builder.CreateSub(Ops[0], Ops[1], "vsubhn");
2885
2886    // %high = lshr <4 x i32> %sum, <i32 16, i32 16, i32 16, i32 16>
2887    Constant *ShiftAmt = ConstantInt::get(SrcTy->getElementType(),
2888                                       SrcTy->getScalarSizeInBits() / 2);
2889    ShiftAmt = ConstantVector::getSplat(VTy->getNumElements(), ShiftAmt);
2890    Ops[0] = Builder.CreateLShr(Ops[0], ShiftAmt, "vsubhn");
2891
2892    // %res = trunc <4 x i32> %high to <4 x i16>
2893    return Builder.CreateTrunc(Ops[0], VTy, "vsubhn");
2894  }
2895  case NEON::BI__builtin_neon_vtrn_v:
2896  case NEON::BI__builtin_neon_vtrnq_v: {
2897    Ops[0] = Builder.CreateBitCast(Ops[0], llvm::PointerType::getUnqual(Ty));
2898    Ops[1] = Builder.CreateBitCast(Ops[1], Ty);
2899    Ops[2] = Builder.CreateBitCast(Ops[2], Ty);
2900    Value *SV = nullptr;
2901
2902    for (unsigned vi = 0; vi != 2; ++vi) {
2903      SmallVector<Constant*, 16> Indices;
2904      for (unsigned i = 0, e = VTy->getNumElements(); i != e; i += 2) {
2905        Indices.push_back(Builder.getInt32(i+vi));
2906        Indices.push_back(Builder.getInt32(i+e+vi));
2907      }
2908      Value *Addr = Builder.CreateConstInBoundsGEP1_32(Ops[0], vi);
2909      SV = llvm::ConstantVector::get(Indices);
2910      SV = Builder.CreateShuffleVector(Ops[1], Ops[2], SV, "vtrn");
2911      SV = Builder.CreateStore(SV, Addr);
2912    }
2913    return SV;
2914  }
2915  case NEON::BI__builtin_neon_vtst_v:
2916  case NEON::BI__builtin_neon_vtstq_v: {
2917    Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
2918    Ops[1] = Builder.CreateBitCast(Ops[1], Ty);
2919    Ops[0] = Builder.CreateAnd(Ops[0], Ops[1]);
2920    Ops[0] = Builder.CreateICmp(ICmpInst::ICMP_NE, Ops[0],
2921                                ConstantAggregateZero::get(Ty));
2922    return Builder.CreateSExt(Ops[0], Ty, "vtst");
2923  }
2924  case NEON::BI__builtin_neon_vuzp_v:
2925  case NEON::BI__builtin_neon_vuzpq_v: {
2926    Ops[0] = Builder.CreateBitCast(Ops[0], llvm::PointerType::getUnqual(Ty));
2927    Ops[1] = Builder.CreateBitCast(Ops[1], Ty);
2928    Ops[2] = Builder.CreateBitCast(Ops[2], Ty);
2929    Value *SV = nullptr;
2930
2931    for (unsigned vi = 0; vi != 2; ++vi) {
2932      SmallVector<Constant*, 16> Indices;
2933      for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i)
2934        Indices.push_back(ConstantInt::get(Int32Ty, 2*i+vi));
2935
2936      Value *Addr = Builder.CreateConstInBoundsGEP1_32(Ops[0], vi);
2937      SV = llvm::ConstantVector::get(Indices);
2938      SV = Builder.CreateShuffleVector(Ops[1], Ops[2], SV, "vuzp");
2939      SV = Builder.CreateStore(SV, Addr);
2940    }
2941    return SV;
2942  }
2943  case NEON::BI__builtin_neon_vzip_v:
2944  case NEON::BI__builtin_neon_vzipq_v: {
2945    Ops[0] = Builder.CreateBitCast(Ops[0], llvm::PointerType::getUnqual(Ty));
2946    Ops[1] = Builder.CreateBitCast(Ops[1], Ty);
2947    Ops[2] = Builder.CreateBitCast(Ops[2], Ty);
2948    Value *SV = nullptr;
2949
2950    for (unsigned vi = 0; vi != 2; ++vi) {
2951      SmallVector<Constant*, 16> Indices;
2952      for (unsigned i = 0, e = VTy->getNumElements(); i != e; i += 2) {
2953        Indices.push_back(ConstantInt::get(Int32Ty, (i + vi*e) >> 1));
2954        Indices.push_back(ConstantInt::get(Int32Ty, ((i + vi*e) >> 1)+e));
2955      }
2956      Value *Addr = Builder.CreateConstInBoundsGEP1_32(Ops[0], vi);
2957      SV = llvm::ConstantVector::get(Indices);
2958      SV = Builder.CreateShuffleVector(Ops[1], Ops[2], SV, "vzip");
2959      SV = Builder.CreateStore(SV, Addr);
2960    }
2961    return SV;
2962  }
2963  }
2964
2965  assert(Int && "Expected valid intrinsic number");
2966
2967  // Determine the type(s) of this overloaded AArch64 intrinsic.
2968  Function *F = LookupNeonLLVMIntrinsic(Int, Modifier, Ty, E);
2969
2970  Value *Result = EmitNeonCall(F, Ops, NameHint);
2971  llvm::Type *ResultType = ConvertType(E->getType());
2972  // AArch64 intrinsic one-element vector type cast to
2973  // scalar type expected by the builtin
2974  return Builder.CreateBitCast(Result, ResultType, NameHint);
2975}
2976
2977Value *CodeGenFunction::EmitAArch64CompareBuiltinExpr(
2978    Value *Op, llvm::Type *Ty, const CmpInst::Predicate Fp,
2979    const CmpInst::Predicate Ip, const Twine &Name) {
2980  llvm::Type *OTy = Op->getType();
2981
2982  // FIXME: this is utterly horrific. We should not be looking at previous
2983  // codegen context to find out what needs doing. Unfortunately TableGen
2984  // currently gives us exactly the same calls for vceqz_f32 and vceqz_s32
2985  // (etc).
2986  if (BitCastInst *BI = dyn_cast<BitCastInst>(Op))
2987    OTy = BI->getOperand(0)->getType();
2988
2989  Op = Builder.CreateBitCast(Op, OTy);
2990  if (OTy->getScalarType()->isFloatingPointTy()) {
2991    Op = Builder.CreateFCmp(Fp, Op, Constant::getNullValue(OTy));
2992  } else {
2993    Op = Builder.CreateICmp(Ip, Op, Constant::getNullValue(OTy));
2994  }
2995  return Builder.CreateSExt(Op, Ty, Name);
2996}
2997
2998static Value *packTBLDVectorList(CodeGenFunction &CGF, ArrayRef<Value *> Ops,
2999                                 Value *ExtOp, Value *IndexOp,
3000                                 llvm::Type *ResTy, unsigned IntID,
3001                                 const char *Name) {
3002  SmallVector<Value *, 2> TblOps;
3003  if (ExtOp)
3004    TblOps.push_back(ExtOp);
3005
3006  // Build a vector containing sequential number like (0, 1, 2, ..., 15)
3007  SmallVector<Constant*, 16> Indices;
3008  llvm::VectorType *TblTy = cast<llvm::VectorType>(Ops[0]->getType());
3009  for (unsigned i = 0, e = TblTy->getNumElements(); i != e; ++i) {
3010    Indices.push_back(ConstantInt::get(CGF.Int32Ty, 2*i));
3011    Indices.push_back(ConstantInt::get(CGF.Int32Ty, 2*i+1));
3012  }
3013  Value *SV = llvm::ConstantVector::get(Indices);
3014
3015  int PairPos = 0, End = Ops.size() - 1;
3016  while (PairPos < End) {
3017    TblOps.push_back(CGF.Builder.CreateShuffleVector(Ops[PairPos],
3018                                                     Ops[PairPos+1], SV, Name));
3019    PairPos += 2;
3020  }
3021
3022  // If there's an odd number of 64-bit lookup table, fill the high 64-bit
3023  // of the 128-bit lookup table with zero.
3024  if (PairPos == End) {
3025    Value *ZeroTbl = ConstantAggregateZero::get(TblTy);
3026    TblOps.push_back(CGF.Builder.CreateShuffleVector(Ops[PairPos],
3027                                                     ZeroTbl, SV, Name));
3028  }
3029
3030  Function *TblF;
3031  TblOps.push_back(IndexOp);
3032  TblF = CGF.CGM.getIntrinsic(IntID, ResTy);
3033
3034  return CGF.EmitNeonCall(TblF, TblOps, Name);
3035}
3036
3037Value *CodeGenFunction::EmitARMBuiltinExpr(unsigned BuiltinID,
3038                                           const CallExpr *E) {
3039  unsigned HintID = static_cast<unsigned>(-1);
3040  switch (BuiltinID) {
3041  default: break;
3042  case ARM::BI__builtin_arm_yield:
3043  case ARM::BI__yield:
3044    HintID = 1;
3045    break;
3046  case ARM::BI__builtin_arm_wfe:
3047  case ARM::BI__wfe:
3048    HintID = 2;
3049    break;
3050  case ARM::BI__builtin_arm_wfi:
3051  case ARM::BI__wfi:
3052    HintID = 3;
3053    break;
3054  case ARM::BI__builtin_arm_sev:
3055  case ARM::BI__sev:
3056    HintID = 4;
3057    break;
3058  case ARM::BI__builtin_arm_sevl:
3059  case ARM::BI__sevl:
3060    HintID = 5;
3061    break;
3062  }
3063
3064  if (HintID != static_cast<unsigned>(-1)) {
3065    Function *F = CGM.getIntrinsic(Intrinsic::arm_hint);
3066    return Builder.CreateCall(F, llvm::ConstantInt::get(Int32Ty, HintID));
3067  }
3068
3069  if (BuiltinID == ARM::BI__builtin_arm_rbit) {
3070    return Builder.CreateCall(CGM.getIntrinsic(Intrinsic::arm_rbit),
3071                                               EmitScalarExpr(E->getArg(0)),
3072                              "rbit");
3073  }
3074
3075  if (BuiltinID == ARM::BI__clear_cache) {
3076    assert(E->getNumArgs() == 2 && "__clear_cache takes 2 arguments");
3077    const FunctionDecl *FD = E->getDirectCallee();
3078    SmallVector<Value*, 2> Ops;
3079    for (unsigned i = 0; i < 2; i++)
3080      Ops.push_back(EmitScalarExpr(E->getArg(i)));
3081    llvm::Type *Ty = CGM.getTypes().ConvertType(FD->getType());
3082    llvm::FunctionType *FTy = cast<llvm::FunctionType>(Ty);
3083    StringRef Name = FD->getName();
3084    return EmitNounwindRuntimeCall(CGM.CreateRuntimeFunction(FTy, Name), Ops);
3085  }
3086
3087  if (BuiltinID == ARM::BI__builtin_arm_ldrexd ||
3088      ((BuiltinID == ARM::BI__builtin_arm_ldrex ||
3089        BuiltinID == ARM::BI__builtin_arm_ldaex) &&
3090       getContext().getTypeSize(E->getType()) == 64) ||
3091      BuiltinID == ARM::BI__ldrexd) {
3092    Function *F;
3093
3094    switch (BuiltinID) {
3095    default: llvm_unreachable("unexpected builtin");
3096    case ARM::BI__builtin_arm_ldaex:
3097      F = CGM.getIntrinsic(Intrinsic::arm_ldaexd);
3098      break;
3099    case ARM::BI__builtin_arm_ldrexd:
3100    case ARM::BI__builtin_arm_ldrex:
3101    case ARM::BI__ldrexd:
3102      F = CGM.getIntrinsic(Intrinsic::arm_ldrexd);
3103      break;
3104    }
3105
3106    Value *LdPtr = EmitScalarExpr(E->getArg(0));
3107    Value *Val = Builder.CreateCall(F, Builder.CreateBitCast(LdPtr, Int8PtrTy),
3108                                    "ldrexd");
3109
3110    Value *Val0 = Builder.CreateExtractValue(Val, 1);
3111    Value *Val1 = Builder.CreateExtractValue(Val, 0);
3112    Val0 = Builder.CreateZExt(Val0, Int64Ty);
3113    Val1 = Builder.CreateZExt(Val1, Int64Ty);
3114
3115    Value *ShiftCst = llvm::ConstantInt::get(Int64Ty, 32);
3116    Val = Builder.CreateShl(Val0, ShiftCst, "shl", true /* nuw */);
3117    Val = Builder.CreateOr(Val, Val1);
3118    return Builder.CreateBitCast(Val, ConvertType(E->getType()));
3119  }
3120
3121  if (BuiltinID == ARM::BI__builtin_arm_ldrex ||
3122      BuiltinID == ARM::BI__builtin_arm_ldaex) {
3123    Value *LoadAddr = EmitScalarExpr(E->getArg(0));
3124
3125    QualType Ty = E->getType();
3126    llvm::Type *RealResTy = ConvertType(Ty);
3127    llvm::Type *IntResTy = llvm::IntegerType::get(getLLVMContext(),
3128                                                  getContext().getTypeSize(Ty));
3129    LoadAddr = Builder.CreateBitCast(LoadAddr, IntResTy->getPointerTo());
3130
3131    Function *F = CGM.getIntrinsic(BuiltinID == ARM::BI__builtin_arm_ldaex
3132                                       ? Intrinsic::arm_ldaex
3133                                       : Intrinsic::arm_ldrex,
3134                                   LoadAddr->getType());
3135    Value *Val = Builder.CreateCall(F, LoadAddr, "ldrex");
3136
3137    if (RealResTy->isPointerTy())
3138      return Builder.CreateIntToPtr(Val, RealResTy);
3139    else {
3140      Val = Builder.CreateTruncOrBitCast(Val, IntResTy);
3141      return Builder.CreateBitCast(Val, RealResTy);
3142    }
3143  }
3144
3145  if (BuiltinID == ARM::BI__builtin_arm_strexd ||
3146      ((BuiltinID == ARM::BI__builtin_arm_stlex ||
3147        BuiltinID == ARM::BI__builtin_arm_strex) &&
3148       getContext().getTypeSize(E->getArg(0)->getType()) == 64)) {
3149    Function *F = CGM.getIntrinsic(BuiltinID == ARM::BI__builtin_arm_stlex
3150                                       ? Intrinsic::arm_stlexd
3151                                       : Intrinsic::arm_strexd);
3152    llvm::Type *STy = llvm::StructType::get(Int32Ty, Int32Ty, NULL);
3153
3154    Value *Tmp = CreateMemTemp(E->getArg(0)->getType());
3155    Value *Val = EmitScalarExpr(E->getArg(0));
3156    Builder.CreateStore(Val, Tmp);
3157
3158    Value *LdPtr = Builder.CreateBitCast(Tmp,llvm::PointerType::getUnqual(STy));
3159    Val = Builder.CreateLoad(LdPtr);
3160
3161    Value *Arg0 = Builder.CreateExtractValue(Val, 0);
3162    Value *Arg1 = Builder.CreateExtractValue(Val, 1);
3163    Value *StPtr = Builder.CreateBitCast(EmitScalarExpr(E->getArg(1)), Int8PtrTy);
3164    return Builder.CreateCall3(F, Arg0, Arg1, StPtr, "strexd");
3165  }
3166
3167  if (BuiltinID == ARM::BI__builtin_arm_strex ||
3168      BuiltinID == ARM::BI__builtin_arm_stlex) {
3169    Value *StoreVal = EmitScalarExpr(E->getArg(0));
3170    Value *StoreAddr = EmitScalarExpr(E->getArg(1));
3171
3172    QualType Ty = E->getArg(0)->getType();
3173    llvm::Type *StoreTy = llvm::IntegerType::get(getLLVMContext(),
3174                                                 getContext().getTypeSize(Ty));
3175    StoreAddr = Builder.CreateBitCast(StoreAddr, StoreTy->getPointerTo());
3176
3177    if (StoreVal->getType()->isPointerTy())
3178      StoreVal = Builder.CreatePtrToInt(StoreVal, Int32Ty);
3179    else {
3180      StoreVal = Builder.CreateBitCast(StoreVal, StoreTy);
3181      StoreVal = Builder.CreateZExtOrBitCast(StoreVal, Int32Ty);
3182    }
3183
3184    Function *F = CGM.getIntrinsic(BuiltinID == ARM::BI__builtin_arm_stlex
3185                                       ? Intrinsic::arm_stlex
3186                                       : Intrinsic::arm_strex,
3187                                   StoreAddr->getType());
3188    return Builder.CreateCall2(F, StoreVal, StoreAddr, "strex");
3189  }
3190
3191  if (BuiltinID == ARM::BI__builtin_arm_clrex) {
3192    Function *F = CGM.getIntrinsic(Intrinsic::arm_clrex);
3193    return Builder.CreateCall(F);
3194  }
3195
3196  // CRC32
3197  Intrinsic::ID CRCIntrinsicID = Intrinsic::not_intrinsic;
3198  switch (BuiltinID) {
3199  case ARM::BI__builtin_arm_crc32b:
3200    CRCIntrinsicID = Intrinsic::arm_crc32b; break;
3201  case ARM::BI__builtin_arm_crc32cb:
3202    CRCIntrinsicID = Intrinsic::arm_crc32cb; break;
3203  case ARM::BI__builtin_arm_crc32h:
3204    CRCIntrinsicID = Intrinsic::arm_crc32h; break;
3205  case ARM::BI__builtin_arm_crc32ch:
3206    CRCIntrinsicID = Intrinsic::arm_crc32ch; break;
3207  case ARM::BI__builtin_arm_crc32w:
3208  case ARM::BI__builtin_arm_crc32d:
3209    CRCIntrinsicID = Intrinsic::arm_crc32w; break;
3210  case ARM::BI__builtin_arm_crc32cw:
3211  case ARM::BI__builtin_arm_crc32cd:
3212    CRCIntrinsicID = Intrinsic::arm_crc32cw; break;
3213  }
3214
3215  if (CRCIntrinsicID != Intrinsic::not_intrinsic) {
3216    Value *Arg0 = EmitScalarExpr(E->getArg(0));
3217    Value *Arg1 = EmitScalarExpr(E->getArg(1));
3218
3219    // crc32{c,}d intrinsics are implemnted as two calls to crc32{c,}w
3220    // intrinsics, hence we need different codegen for these cases.
3221    if (BuiltinID == ARM::BI__builtin_arm_crc32d ||
3222        BuiltinID == ARM::BI__builtin_arm_crc32cd) {
3223      Value *C1 = llvm::ConstantInt::get(Int64Ty, 32);
3224      Value *Arg1a = Builder.CreateTruncOrBitCast(Arg1, Int32Ty);
3225      Value *Arg1b = Builder.CreateLShr(Arg1, C1);
3226      Arg1b = Builder.CreateTruncOrBitCast(Arg1b, Int32Ty);
3227
3228      Function *F = CGM.getIntrinsic(CRCIntrinsicID);
3229      Value *Res = Builder.CreateCall2(F, Arg0, Arg1a);
3230      return Builder.CreateCall2(F, Res, Arg1b);
3231    } else {
3232      Arg1 = Builder.CreateZExtOrBitCast(Arg1, Int32Ty);
3233
3234      Function *F = CGM.getIntrinsic(CRCIntrinsicID);
3235      return Builder.CreateCall2(F, Arg0, Arg1);
3236    }
3237  }
3238
3239  SmallVector<Value*, 4> Ops;
3240  llvm::Value *Align = nullptr;
3241  for (unsigned i = 0, e = E->getNumArgs() - 1; i != e; i++) {
3242    if (i == 0) {
3243      switch (BuiltinID) {
3244      case NEON::BI__builtin_neon_vld1_v:
3245      case NEON::BI__builtin_neon_vld1q_v:
3246      case NEON::BI__builtin_neon_vld1q_lane_v:
3247      case NEON::BI__builtin_neon_vld1_lane_v:
3248      case NEON::BI__builtin_neon_vld1_dup_v:
3249      case NEON::BI__builtin_neon_vld1q_dup_v:
3250      case NEON::BI__builtin_neon_vst1_v:
3251      case NEON::BI__builtin_neon_vst1q_v:
3252      case NEON::BI__builtin_neon_vst1q_lane_v:
3253      case NEON::BI__builtin_neon_vst1_lane_v:
3254      case NEON::BI__builtin_neon_vst2_v:
3255      case NEON::BI__builtin_neon_vst2q_v:
3256      case NEON::BI__builtin_neon_vst2_lane_v:
3257      case NEON::BI__builtin_neon_vst2q_lane_v:
3258      case NEON::BI__builtin_neon_vst3_v:
3259      case NEON::BI__builtin_neon_vst3q_v:
3260      case NEON::BI__builtin_neon_vst3_lane_v:
3261      case NEON::BI__builtin_neon_vst3q_lane_v:
3262      case NEON::BI__builtin_neon_vst4_v:
3263      case NEON::BI__builtin_neon_vst4q_v:
3264      case NEON::BI__builtin_neon_vst4_lane_v:
3265      case NEON::BI__builtin_neon_vst4q_lane_v:
3266        // Get the alignment for the argument in addition to the value;
3267        // we'll use it later.
3268        std::pair<llvm::Value*, unsigned> Src =
3269            EmitPointerWithAlignment(E->getArg(0));
3270        Ops.push_back(Src.first);
3271        Align = Builder.getInt32(Src.second);
3272        continue;
3273      }
3274    }
3275    if (i == 1) {
3276      switch (BuiltinID) {
3277      case NEON::BI__builtin_neon_vld2_v:
3278      case NEON::BI__builtin_neon_vld2q_v:
3279      case NEON::BI__builtin_neon_vld3_v:
3280      case NEON::BI__builtin_neon_vld3q_v:
3281      case NEON::BI__builtin_neon_vld4_v:
3282      case NEON::BI__builtin_neon_vld4q_v:
3283      case NEON::BI__builtin_neon_vld2_lane_v:
3284      case NEON::BI__builtin_neon_vld2q_lane_v:
3285      case NEON::BI__builtin_neon_vld3_lane_v:
3286      case NEON::BI__builtin_neon_vld3q_lane_v:
3287      case NEON::BI__builtin_neon_vld4_lane_v:
3288      case NEON::BI__builtin_neon_vld4q_lane_v:
3289      case NEON::BI__builtin_neon_vld2_dup_v:
3290      case NEON::BI__builtin_neon_vld3_dup_v:
3291      case NEON::BI__builtin_neon_vld4_dup_v:
3292        // Get the alignment for the argument in addition to the value;
3293        // we'll use it later.
3294        std::pair<llvm::Value*, unsigned> Src =
3295            EmitPointerWithAlignment(E->getArg(1));
3296        Ops.push_back(Src.first);
3297        Align = Builder.getInt32(Src.second);
3298        continue;
3299      }
3300    }
3301    Ops.push_back(EmitScalarExpr(E->getArg(i)));
3302  }
3303
3304  switch (BuiltinID) {
3305  default: break;
3306  // vget_lane and vset_lane are not overloaded and do not have an extra
3307  // argument that specifies the vector type.
3308  case NEON::BI__builtin_neon_vget_lane_i8:
3309  case NEON::BI__builtin_neon_vget_lane_i16:
3310  case NEON::BI__builtin_neon_vget_lane_i32:
3311  case NEON::BI__builtin_neon_vget_lane_i64:
3312  case NEON::BI__builtin_neon_vget_lane_f32:
3313  case NEON::BI__builtin_neon_vgetq_lane_i8:
3314  case NEON::BI__builtin_neon_vgetq_lane_i16:
3315  case NEON::BI__builtin_neon_vgetq_lane_i32:
3316  case NEON::BI__builtin_neon_vgetq_lane_i64:
3317  case NEON::BI__builtin_neon_vgetq_lane_f32:
3318    return Builder.CreateExtractElement(Ops[0], EmitScalarExpr(E->getArg(1)),
3319                                        "vget_lane");
3320  case NEON::BI__builtin_neon_vset_lane_i8:
3321  case NEON::BI__builtin_neon_vset_lane_i16:
3322  case NEON::BI__builtin_neon_vset_lane_i32:
3323  case NEON::BI__builtin_neon_vset_lane_i64:
3324  case NEON::BI__builtin_neon_vset_lane_f32:
3325  case NEON::BI__builtin_neon_vsetq_lane_i8:
3326  case NEON::BI__builtin_neon_vsetq_lane_i16:
3327  case NEON::BI__builtin_neon_vsetq_lane_i32:
3328  case NEON::BI__builtin_neon_vsetq_lane_i64:
3329  case NEON::BI__builtin_neon_vsetq_lane_f32:
3330    Ops.push_back(EmitScalarExpr(E->getArg(2)));
3331    return Builder.CreateInsertElement(Ops[1], Ops[0], Ops[2], "vset_lane");
3332
3333  // Non-polymorphic crypto instructions also not overloaded
3334  case NEON::BI__builtin_neon_vsha1h_u32:
3335    Ops.push_back(EmitScalarExpr(E->getArg(0)));
3336    return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_sha1h), Ops,
3337                        "vsha1h");
3338  case NEON::BI__builtin_neon_vsha1cq_u32:
3339    Ops.push_back(EmitScalarExpr(E->getArg(2)));
3340    return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_sha1c), Ops,
3341                        "vsha1h");
3342  case NEON::BI__builtin_neon_vsha1pq_u32:
3343    Ops.push_back(EmitScalarExpr(E->getArg(2)));
3344    return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_sha1p), Ops,
3345                        "vsha1h");
3346  case NEON::BI__builtin_neon_vsha1mq_u32:
3347    Ops.push_back(EmitScalarExpr(E->getArg(2)));
3348    return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_sha1m), Ops,
3349                        "vsha1h");
3350  }
3351
3352  // Get the last argument, which specifies the vector type.
3353  llvm::APSInt Result;
3354  const Expr *Arg = E->getArg(E->getNumArgs()-1);
3355  if (!Arg->isIntegerConstantExpr(Result, getContext()))
3356    return nullptr;
3357
3358  if (BuiltinID == ARM::BI__builtin_arm_vcvtr_f ||
3359      BuiltinID == ARM::BI__builtin_arm_vcvtr_d) {
3360    // Determine the overloaded type of this builtin.
3361    llvm::Type *Ty;
3362    if (BuiltinID == ARM::BI__builtin_arm_vcvtr_f)
3363      Ty = FloatTy;
3364    else
3365      Ty = DoubleTy;
3366
3367    // Determine whether this is an unsigned conversion or not.
3368    bool usgn = Result.getZExtValue() == 1;
3369    unsigned Int = usgn ? Intrinsic::arm_vcvtru : Intrinsic::arm_vcvtr;
3370
3371    // Call the appropriate intrinsic.
3372    Function *F = CGM.getIntrinsic(Int, Ty);
3373    return Builder.CreateCall(F, Ops, "vcvtr");
3374  }
3375
3376  // Determine the type of this overloaded NEON intrinsic.
3377  NeonTypeFlags Type(Result.getZExtValue());
3378  bool usgn = Type.isUnsigned();
3379  bool rightShift = false;
3380
3381  llvm::VectorType *VTy = GetNeonType(this, Type);
3382  llvm::Type *Ty = VTy;
3383  if (!Ty)
3384    return nullptr;
3385
3386  // Many NEON builtins have identical semantics and uses in ARM and
3387  // AArch64. Emit these in a single function.
3388  ArrayRef<NeonIntrinsicInfo> IntrinsicMap(ARMSIMDIntrinsicMap);
3389  const NeonIntrinsicInfo *Builtin = findNeonIntrinsicInMap(
3390      IntrinsicMap, BuiltinID, NEONSIMDIntrinsicsProvenSorted);
3391  if (Builtin)
3392    return EmitCommonNeonBuiltinExpr(
3393        Builtin->BuiltinID, Builtin->LLVMIntrinsic, Builtin->AltLLVMIntrinsic,
3394        Builtin->NameHint, Builtin->TypeModifier, E, Ops, Align);
3395
3396  unsigned Int;
3397  switch (BuiltinID) {
3398  default: return nullptr;
3399  case NEON::BI__builtin_neon_vld1q_lane_v:
3400    // Handle 64-bit integer elements as a special case.  Use shuffles of
3401    // one-element vectors to avoid poor code for i64 in the backend.
3402    if (VTy->getElementType()->isIntegerTy(64)) {
3403      // Extract the other lane.
3404      Ops[1] = Builder.CreateBitCast(Ops[1], Ty);
3405      int Lane = cast<ConstantInt>(Ops[2])->getZExtValue();
3406      Value *SV = llvm::ConstantVector::get(ConstantInt::get(Int32Ty, 1-Lane));
3407      Ops[1] = Builder.CreateShuffleVector(Ops[1], Ops[1], SV);
3408      // Load the value as a one-element vector.
3409      Ty = llvm::VectorType::get(VTy->getElementType(), 1);
3410      Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vld1, Ty);
3411      Value *Ld = Builder.CreateCall2(F, Ops[0], Align);
3412      // Combine them.
3413      SmallVector<Constant*, 2> Indices;
3414      Indices.push_back(ConstantInt::get(Int32Ty, 1-Lane));
3415      Indices.push_back(ConstantInt::get(Int32Ty, Lane));
3416      SV = llvm::ConstantVector::get(Indices);
3417      return Builder.CreateShuffleVector(Ops[1], Ld, SV, "vld1q_lane");
3418    }
3419    // fall through
3420  case NEON::BI__builtin_neon_vld1_lane_v: {
3421    Ops[1] = Builder.CreateBitCast(Ops[1], Ty);
3422    Ty = llvm::PointerType::getUnqual(VTy->getElementType());
3423    Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
3424    LoadInst *Ld = Builder.CreateLoad(Ops[0]);
3425    Ld->setAlignment(cast<ConstantInt>(Align)->getZExtValue());
3426    return Builder.CreateInsertElement(Ops[1], Ld, Ops[2], "vld1_lane");
3427  }
3428  case NEON::BI__builtin_neon_vld2_dup_v:
3429  case NEON::BI__builtin_neon_vld3_dup_v:
3430  case NEON::BI__builtin_neon_vld4_dup_v: {
3431    // Handle 64-bit elements as a special-case.  There is no "dup" needed.
3432    if (VTy->getElementType()->getPrimitiveSizeInBits() == 64) {
3433      switch (BuiltinID) {
3434      case NEON::BI__builtin_neon_vld2_dup_v:
3435        Int = Intrinsic::arm_neon_vld2;
3436        break;
3437      case NEON::BI__builtin_neon_vld3_dup_v:
3438        Int = Intrinsic::arm_neon_vld3;
3439        break;
3440      case NEON::BI__builtin_neon_vld4_dup_v:
3441        Int = Intrinsic::arm_neon_vld4;
3442        break;
3443      default: llvm_unreachable("unknown vld_dup intrinsic?");
3444      }
3445      Function *F = CGM.getIntrinsic(Int, Ty);
3446      Ops[1] = Builder.CreateCall2(F, Ops[1], Align, "vld_dup");
3447      Ty = llvm::PointerType::getUnqual(Ops[1]->getType());
3448      Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
3449      return Builder.CreateStore(Ops[1], Ops[0]);
3450    }
3451    switch (BuiltinID) {
3452    case NEON::BI__builtin_neon_vld2_dup_v:
3453      Int = Intrinsic::arm_neon_vld2lane;
3454      break;
3455    case NEON::BI__builtin_neon_vld3_dup_v:
3456      Int = Intrinsic::arm_neon_vld3lane;
3457      break;
3458    case NEON::BI__builtin_neon_vld4_dup_v:
3459      Int = Intrinsic::arm_neon_vld4lane;
3460      break;
3461    default: llvm_unreachable("unknown vld_dup intrinsic?");
3462    }
3463    Function *F = CGM.getIntrinsic(Int, Ty);
3464    llvm::StructType *STy = cast<llvm::StructType>(F->getReturnType());
3465
3466    SmallVector<Value*, 6> Args;
3467    Args.push_back(Ops[1]);
3468    Args.append(STy->getNumElements(), UndefValue::get(Ty));
3469
3470    llvm::Constant *CI = ConstantInt::get(Int32Ty, 0);
3471    Args.push_back(CI);
3472    Args.push_back(Align);
3473
3474    Ops[1] = Builder.CreateCall(F, Args, "vld_dup");
3475    // splat lane 0 to all elts in each vector of the result.
3476    for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
3477      Value *Val = Builder.CreateExtractValue(Ops[1], i);
3478      Value *Elt = Builder.CreateBitCast(Val, Ty);
3479      Elt = EmitNeonSplat(Elt, CI);
3480      Elt = Builder.CreateBitCast(Elt, Val->getType());
3481      Ops[1] = Builder.CreateInsertValue(Ops[1], Elt, i);
3482    }
3483    Ty = llvm::PointerType::getUnqual(Ops[1]->getType());
3484    Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
3485    return Builder.CreateStore(Ops[1], Ops[0]);
3486  }
3487  case NEON::BI__builtin_neon_vqrshrn_n_v:
3488    Int =
3489      usgn ? Intrinsic::arm_neon_vqrshiftnu : Intrinsic::arm_neon_vqrshiftns;
3490    return EmitNeonCall(CGM.getIntrinsic(Int, Ty), Ops, "vqrshrn_n",
3491                        1, true);
3492  case NEON::BI__builtin_neon_vqrshrun_n_v:
3493    return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqrshiftnsu, Ty),
3494                        Ops, "vqrshrun_n", 1, true);
3495  case NEON::BI__builtin_neon_vqshlu_n_v:
3496  case NEON::BI__builtin_neon_vqshluq_n_v:
3497    return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqshiftsu, Ty),
3498                        Ops, "vqshlu", 1, false);
3499  case NEON::BI__builtin_neon_vqshrn_n_v:
3500    Int = usgn ? Intrinsic::arm_neon_vqshiftnu : Intrinsic::arm_neon_vqshiftns;
3501    return EmitNeonCall(CGM.getIntrinsic(Int, Ty), Ops, "vqshrn_n",
3502                        1, true);
3503  case NEON::BI__builtin_neon_vqshrun_n_v:
3504    return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqshiftnsu, Ty),
3505                        Ops, "vqshrun_n", 1, true);
3506  case NEON::BI__builtin_neon_vrecpe_v:
3507  case NEON::BI__builtin_neon_vrecpeq_v:
3508    return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vrecpe, Ty),
3509                        Ops, "vrecpe");
3510  case NEON::BI__builtin_neon_vrshrn_n_v:
3511    return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vrshiftn, Ty),
3512                        Ops, "vrshrn_n", 1, true);
3513  case NEON::BI__builtin_neon_vrshr_n_v:
3514  case NEON::BI__builtin_neon_vrshrq_n_v:
3515    Int = usgn ? Intrinsic::arm_neon_vrshiftu : Intrinsic::arm_neon_vrshifts;
3516    return EmitNeonCall(CGM.getIntrinsic(Int, Ty), Ops, "vrshr_n", 1, true);
3517  case NEON::BI__builtin_neon_vrsra_n_v:
3518  case NEON::BI__builtin_neon_vrsraq_n_v:
3519    Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
3520    Ops[1] = Builder.CreateBitCast(Ops[1], Ty);
3521    Ops[2] = EmitNeonShiftVector(Ops[2], Ty, true);
3522    Int = usgn ? Intrinsic::arm_neon_vrshiftu : Intrinsic::arm_neon_vrshifts;
3523    Ops[1] = Builder.CreateCall2(CGM.getIntrinsic(Int, Ty), Ops[1], Ops[2]);
3524    return Builder.CreateAdd(Ops[0], Ops[1], "vrsra_n");
3525  case NEON::BI__builtin_neon_vsri_n_v:
3526  case NEON::BI__builtin_neon_vsriq_n_v:
3527    rightShift = true;
3528  case NEON::BI__builtin_neon_vsli_n_v:
3529  case NEON::BI__builtin_neon_vsliq_n_v:
3530    Ops[2] = EmitNeonShiftVector(Ops[2], Ty, rightShift);
3531    return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vshiftins, Ty),
3532                        Ops, "vsli_n");
3533  case NEON::BI__builtin_neon_vsra_n_v:
3534  case NEON::BI__builtin_neon_vsraq_n_v:
3535    Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
3536    Ops[1] = EmitNeonRShiftImm(Ops[1], Ops[2], Ty, usgn, "vsra_n");
3537    return Builder.CreateAdd(Ops[0], Ops[1]);
3538  case NEON::BI__builtin_neon_vst1q_lane_v:
3539    // Handle 64-bit integer elements as a special case.  Use a shuffle to get
3540    // a one-element vector and avoid poor code for i64 in the backend.
3541    if (VTy->getElementType()->isIntegerTy(64)) {
3542      Ops[1] = Builder.CreateBitCast(Ops[1], Ty);
3543      Value *SV = llvm::ConstantVector::get(cast<llvm::Constant>(Ops[2]));
3544      Ops[1] = Builder.CreateShuffleVector(Ops[1], Ops[1], SV);
3545      Ops[2] = Align;
3546      return Builder.CreateCall(CGM.getIntrinsic(Intrinsic::arm_neon_vst1,
3547                                                 Ops[1]->getType()), Ops);
3548    }
3549    // fall through
3550  case NEON::BI__builtin_neon_vst1_lane_v: {
3551    Ops[1] = Builder.CreateBitCast(Ops[1], Ty);
3552    Ops[1] = Builder.CreateExtractElement(Ops[1], Ops[2]);
3553    Ty = llvm::PointerType::getUnqual(Ops[1]->getType());
3554    StoreInst *St = Builder.CreateStore(Ops[1],
3555                                        Builder.CreateBitCast(Ops[0], Ty));
3556    St->setAlignment(cast<ConstantInt>(Align)->getZExtValue());
3557    return St;
3558  }
3559  case NEON::BI__builtin_neon_vtbl1_v:
3560    return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbl1),
3561                        Ops, "vtbl1");
3562  case NEON::BI__builtin_neon_vtbl2_v:
3563    return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbl2),
3564                        Ops, "vtbl2");
3565  case NEON::BI__builtin_neon_vtbl3_v:
3566    return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbl3),
3567                        Ops, "vtbl3");
3568  case NEON::BI__builtin_neon_vtbl4_v:
3569    return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbl4),
3570                        Ops, "vtbl4");
3571  case NEON::BI__builtin_neon_vtbx1_v:
3572    return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbx1),
3573                        Ops, "vtbx1");
3574  case NEON::BI__builtin_neon_vtbx2_v:
3575    return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbx2),
3576                        Ops, "vtbx2");
3577  case NEON::BI__builtin_neon_vtbx3_v:
3578    return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbx3),
3579                        Ops, "vtbx3");
3580  case NEON::BI__builtin_neon_vtbx4_v:
3581    return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbx4),
3582                        Ops, "vtbx4");
3583  }
3584}
3585
3586static Value *EmitAArch64TblBuiltinExpr(CodeGenFunction &CGF, unsigned BuiltinID,
3587                                      const CallExpr *E,
3588                                      SmallVectorImpl<Value *> &Ops) {
3589  unsigned int Int = 0;
3590  const char *s = nullptr;
3591
3592  switch (BuiltinID) {
3593  default:
3594    return nullptr;
3595  case NEON::BI__builtin_neon_vtbl1_v:
3596  case NEON::BI__builtin_neon_vqtbl1_v:
3597  case NEON::BI__builtin_neon_vqtbl1q_v:
3598  case NEON::BI__builtin_neon_vtbl2_v:
3599  case NEON::BI__builtin_neon_vqtbl2_v:
3600  case NEON::BI__builtin_neon_vqtbl2q_v:
3601  case NEON::BI__builtin_neon_vtbl3_v:
3602  case NEON::BI__builtin_neon_vqtbl3_v:
3603  case NEON::BI__builtin_neon_vqtbl3q_v:
3604  case NEON::BI__builtin_neon_vtbl4_v:
3605  case NEON::BI__builtin_neon_vqtbl4_v:
3606  case NEON::BI__builtin_neon_vqtbl4q_v:
3607    break;
3608  case NEON::BI__builtin_neon_vtbx1_v:
3609  case NEON::BI__builtin_neon_vqtbx1_v:
3610  case NEON::BI__builtin_neon_vqtbx1q_v:
3611  case NEON::BI__builtin_neon_vtbx2_v:
3612  case NEON::BI__builtin_neon_vqtbx2_v:
3613  case NEON::BI__builtin_neon_vqtbx2q_v:
3614  case NEON::BI__builtin_neon_vtbx3_v:
3615  case NEON::BI__builtin_neon_vqtbx3_v:
3616  case NEON::BI__builtin_neon_vqtbx3q_v:
3617  case NEON::BI__builtin_neon_vtbx4_v:
3618  case NEON::BI__builtin_neon_vqtbx4_v:
3619  case NEON::BI__builtin_neon_vqtbx4q_v:
3620    break;
3621  }
3622
3623  assert(E->getNumArgs() >= 3);
3624
3625  // Get the last argument, which specifies the vector type.
3626  llvm::APSInt Result;
3627  const Expr *Arg = E->getArg(E->getNumArgs() - 1);
3628  if (!Arg->isIntegerConstantExpr(Result, CGF.getContext()))
3629    return nullptr;
3630
3631  // Determine the type of this overloaded NEON intrinsic.
3632  NeonTypeFlags Type(Result.getZExtValue());
3633  llvm::VectorType *VTy = GetNeonType(&CGF, Type);
3634  llvm::Type *Ty = VTy;
3635  if (!Ty)
3636    return nullptr;
3637
3638  unsigned nElts = VTy->getNumElements();
3639
3640  CodeGen::CGBuilderTy &Builder = CGF.Builder;
3641
3642  // AArch64 scalar builtins are not overloaded, they do not have an extra
3643  // argument that specifies the vector type, need to handle each case.
3644  SmallVector<Value *, 2> TblOps;
3645  switch (BuiltinID) {
3646  case NEON::BI__builtin_neon_vtbl1_v: {
3647    TblOps.push_back(Ops[0]);
3648    return packTBLDVectorList(CGF, TblOps, nullptr, Ops[1], Ty,
3649                              Intrinsic::aarch64_neon_tbl1, "vtbl1");
3650  }
3651  case NEON::BI__builtin_neon_vtbl2_v: {
3652    TblOps.push_back(Ops[0]);
3653    TblOps.push_back(Ops[1]);
3654    return packTBLDVectorList(CGF, TblOps, nullptr, Ops[2], Ty,
3655                              Intrinsic::aarch64_neon_tbl1, "vtbl1");
3656  }
3657  case NEON::BI__builtin_neon_vtbl3_v: {
3658    TblOps.push_back(Ops[0]);
3659    TblOps.push_back(Ops[1]);
3660    TblOps.push_back(Ops[2]);
3661    return packTBLDVectorList(CGF, TblOps, nullptr, Ops[3], Ty,
3662                              Intrinsic::aarch64_neon_tbl2, "vtbl2");
3663  }
3664  case NEON::BI__builtin_neon_vtbl4_v: {
3665    TblOps.push_back(Ops[0]);
3666    TblOps.push_back(Ops[1]);
3667    TblOps.push_back(Ops[2]);
3668    TblOps.push_back(Ops[3]);
3669    return packTBLDVectorList(CGF, TblOps, nullptr, Ops[4], Ty,
3670                              Intrinsic::aarch64_neon_tbl2, "vtbl2");
3671  }
3672  case NEON::BI__builtin_neon_vtbx1_v: {
3673    TblOps.push_back(Ops[1]);
3674    Value *TblRes = packTBLDVectorList(CGF, TblOps, nullptr, Ops[2], Ty,
3675                                       Intrinsic::aarch64_neon_tbl1, "vtbl1");
3676
3677    llvm::Constant *Eight = ConstantInt::get(VTy->getElementType(), 8);
3678    Value* EightV = llvm::ConstantVector::getSplat(nElts, Eight);
3679    Value *CmpRes = Builder.CreateICmp(ICmpInst::ICMP_UGE, Ops[2], EightV);
3680    CmpRes = Builder.CreateSExt(CmpRes, Ty);
3681
3682    Value *EltsFromInput = Builder.CreateAnd(CmpRes, Ops[0]);
3683    Value *EltsFromTbl = Builder.CreateAnd(Builder.CreateNot(CmpRes), TblRes);
3684    return Builder.CreateOr(EltsFromInput, EltsFromTbl, "vtbx");
3685  }
3686  case NEON::BI__builtin_neon_vtbx2_v: {
3687    TblOps.push_back(Ops[1]);
3688    TblOps.push_back(Ops[2]);
3689    return packTBLDVectorList(CGF, TblOps, Ops[0], Ops[3], Ty,
3690                              Intrinsic::aarch64_neon_tbx1, "vtbx1");
3691  }
3692  case NEON::BI__builtin_neon_vtbx3_v: {
3693    TblOps.push_back(Ops[1]);
3694    TblOps.push_back(Ops[2]);
3695    TblOps.push_back(Ops[3]);
3696    Value *TblRes = packTBLDVectorList(CGF, TblOps, nullptr, Ops[4], Ty,
3697                                       Intrinsic::aarch64_neon_tbl2, "vtbl2");
3698
3699    llvm::Constant *TwentyFour = ConstantInt::get(VTy->getElementType(), 24);
3700    Value* TwentyFourV = llvm::ConstantVector::getSplat(nElts, TwentyFour);
3701    Value *CmpRes = Builder.CreateICmp(ICmpInst::ICMP_UGE, Ops[4],
3702                                           TwentyFourV);
3703    CmpRes = Builder.CreateSExt(CmpRes, Ty);
3704
3705    Value *EltsFromInput = Builder.CreateAnd(CmpRes, Ops[0]);
3706    Value *EltsFromTbl = Builder.CreateAnd(Builder.CreateNot(CmpRes), TblRes);
3707    return Builder.CreateOr(EltsFromInput, EltsFromTbl, "vtbx");
3708  }
3709  case NEON::BI__builtin_neon_vtbx4_v: {
3710    TblOps.push_back(Ops[1]);
3711    TblOps.push_back(Ops[2]);
3712    TblOps.push_back(Ops[3]);
3713    TblOps.push_back(Ops[4]);
3714    return packTBLDVectorList(CGF, TblOps, Ops[0], Ops[5], Ty,
3715                              Intrinsic::aarch64_neon_tbx2, "vtbx2");
3716  }
3717  case NEON::BI__builtin_neon_vqtbl1_v:
3718  case NEON::BI__builtin_neon_vqtbl1q_v:
3719    Int = Intrinsic::aarch64_neon_tbl1; s = "vtbl1"; break;
3720  case NEON::BI__builtin_neon_vqtbl2_v:
3721  case NEON::BI__builtin_neon_vqtbl2q_v: {
3722    Int = Intrinsic::aarch64_neon_tbl2; s = "vtbl2"; break;
3723  case NEON::BI__builtin_neon_vqtbl3_v:
3724  case NEON::BI__builtin_neon_vqtbl3q_v:
3725    Int = Intrinsic::aarch64_neon_tbl3; s = "vtbl3"; break;
3726  case NEON::BI__builtin_neon_vqtbl4_v:
3727  case NEON::BI__builtin_neon_vqtbl4q_v:
3728    Int = Intrinsic::aarch64_neon_tbl4; s = "vtbl4"; break;
3729  case NEON::BI__builtin_neon_vqtbx1_v:
3730  case NEON::BI__builtin_neon_vqtbx1q_v:
3731    Int = Intrinsic::aarch64_neon_tbx1; s = "vtbx1"; break;
3732  case NEON::BI__builtin_neon_vqtbx2_v:
3733  case NEON::BI__builtin_neon_vqtbx2q_v:
3734    Int = Intrinsic::aarch64_neon_tbx2; s = "vtbx2"; break;
3735  case NEON::BI__builtin_neon_vqtbx3_v:
3736  case NEON::BI__builtin_neon_vqtbx3q_v:
3737    Int = Intrinsic::aarch64_neon_tbx3; s = "vtbx3"; break;
3738  case NEON::BI__builtin_neon_vqtbx4_v:
3739  case NEON::BI__builtin_neon_vqtbx4q_v:
3740    Int = Intrinsic::aarch64_neon_tbx4; s = "vtbx4"; break;
3741  }
3742  }
3743
3744  if (!Int)
3745    return nullptr;
3746
3747  Function *F = CGF.CGM.getIntrinsic(Int, Ty);
3748  return CGF.EmitNeonCall(F, Ops, s);
3749}
3750
3751Value *CodeGenFunction::vectorWrapScalar16(Value *Op) {
3752  llvm::Type *VTy = llvm::VectorType::get(Int16Ty, 4);
3753  Op = Builder.CreateBitCast(Op, Int16Ty);
3754  Value *V = UndefValue::get(VTy);
3755  llvm::Constant *CI = ConstantInt::get(SizeTy, 0);
3756  Op = Builder.CreateInsertElement(V, Op, CI);
3757  return Op;
3758}
3759
3760Value *CodeGenFunction::vectorWrapScalar8(Value *Op) {
3761  llvm::Type *VTy = llvm::VectorType::get(Int8Ty, 8);
3762  Op = Builder.CreateBitCast(Op, Int8Ty);
3763  Value *V = UndefValue::get(VTy);
3764  llvm::Constant *CI = ConstantInt::get(SizeTy, 0);
3765  Op = Builder.CreateInsertElement(V, Op, CI);
3766  return Op;
3767}
3768
3769Value *CodeGenFunction::
3770emitVectorWrappedScalar8Intrinsic(unsigned Int, SmallVectorImpl<Value*> &Ops,
3771                                  const char *Name) {
3772  // i8 is not a legal types for AArch64, so we can't just use
3773  // a normal overloaded intrinsic call for these scalar types. Instead
3774  // we'll build 64-bit vectors w/ lane zero being our input values and
3775  // perform the operation on that. The back end can pattern match directly
3776  // to the scalar instruction.
3777  Ops[0] = vectorWrapScalar8(Ops[0]);
3778  Ops[1] = vectorWrapScalar8(Ops[1]);
3779  llvm::Type *VTy = llvm::VectorType::get(Int8Ty, 8);
3780  Value *V = EmitNeonCall(CGM.getIntrinsic(Int, VTy), Ops, Name);
3781  Constant *CI = ConstantInt::get(SizeTy, 0);
3782  return Builder.CreateExtractElement(V, CI, "lane0");
3783}
3784
3785Value *CodeGenFunction::
3786emitVectorWrappedScalar16Intrinsic(unsigned Int, SmallVectorImpl<Value*> &Ops,
3787                                   const char *Name) {
3788  // i16 is not a legal types for AArch64, so we can't just use
3789  // a normal overloaded intrinsic call for these scalar types. Instead
3790  // we'll build 64-bit vectors w/ lane zero being our input values and
3791  // perform the operation on that. The back end can pattern match directly
3792  // to the scalar instruction.
3793  Ops[0] = vectorWrapScalar16(Ops[0]);
3794  Ops[1] = vectorWrapScalar16(Ops[1]);
3795  llvm::Type *VTy = llvm::VectorType::get(Int16Ty, 4);
3796  Value *V = EmitNeonCall(CGM.getIntrinsic(Int, VTy), Ops, Name);
3797  Constant *CI = ConstantInt::get(SizeTy, 0);
3798  return Builder.CreateExtractElement(V, CI, "lane0");
3799}
3800
3801Value *CodeGenFunction::EmitAArch64BuiltinExpr(unsigned BuiltinID,
3802                                               const CallExpr *E) {
3803  if (BuiltinID == AArch64::BI__builtin_arm_rbit) {
3804    assert((getContext().getTypeSize(E->getType()) == 32) &&
3805           "rbit of unusual size!");
3806    llvm::Value *Arg = EmitScalarExpr(E->getArg(0));
3807    return Builder.CreateCall(
3808        CGM.getIntrinsic(Intrinsic::aarch64_rbit, Arg->getType()), Arg, "rbit");
3809  }
3810  if (BuiltinID == AArch64::BI__builtin_arm_rbit64) {
3811    assert((getContext().getTypeSize(E->getType()) == 64) &&
3812           "rbit of unusual size!");
3813    llvm::Value *Arg = EmitScalarExpr(E->getArg(0));
3814    return Builder.CreateCall(
3815        CGM.getIntrinsic(Intrinsic::aarch64_rbit, Arg->getType()), Arg, "rbit");
3816  }
3817
3818  if (BuiltinID == AArch64::BI__clear_cache) {
3819    assert(E->getNumArgs() == 2 && "__clear_cache takes 2 arguments");
3820    const FunctionDecl *FD = E->getDirectCallee();
3821    SmallVector<Value*, 2> Ops;
3822    for (unsigned i = 0; i < 2; i++)
3823      Ops.push_back(EmitScalarExpr(E->getArg(i)));
3824    llvm::Type *Ty = CGM.getTypes().ConvertType(FD->getType());
3825    llvm::FunctionType *FTy = cast<llvm::FunctionType>(Ty);
3826    StringRef Name = FD->getName();
3827    return EmitNounwindRuntimeCall(CGM.CreateRuntimeFunction(FTy, Name), Ops);
3828  }
3829
3830  if ((BuiltinID == AArch64::BI__builtin_arm_ldrex ||
3831      BuiltinID == AArch64::BI__builtin_arm_ldaex) &&
3832      getContext().getTypeSize(E->getType()) == 128) {
3833    Function *F = CGM.getIntrinsic(BuiltinID == AArch64::BI__builtin_arm_ldaex
3834                                       ? Intrinsic::aarch64_ldaxp
3835                                       : Intrinsic::aarch64_ldxp);
3836
3837    Value *LdPtr = EmitScalarExpr(E->getArg(0));
3838    Value *Val = Builder.CreateCall(F, Builder.CreateBitCast(LdPtr, Int8PtrTy),
3839                                    "ldxp");
3840
3841    Value *Val0 = Builder.CreateExtractValue(Val, 1);
3842    Value *Val1 = Builder.CreateExtractValue(Val, 0);
3843    llvm::Type *Int128Ty = llvm::IntegerType::get(getLLVMContext(), 128);
3844    Val0 = Builder.CreateZExt(Val0, Int128Ty);
3845    Val1 = Builder.CreateZExt(Val1, Int128Ty);
3846
3847    Value *ShiftCst = llvm::ConstantInt::get(Int128Ty, 64);
3848    Val = Builder.CreateShl(Val0, ShiftCst, "shl", true /* nuw */);
3849    Val = Builder.CreateOr(Val, Val1);
3850    return Builder.CreateBitCast(Val, ConvertType(E->getType()));
3851  } else if (BuiltinID == AArch64::BI__builtin_arm_ldrex ||
3852             BuiltinID == AArch64::BI__builtin_arm_ldaex) {
3853    Value *LoadAddr = EmitScalarExpr(E->getArg(0));
3854
3855    QualType Ty = E->getType();
3856    llvm::Type *RealResTy = ConvertType(Ty);
3857    llvm::Type *IntResTy = llvm::IntegerType::get(getLLVMContext(),
3858                                                  getContext().getTypeSize(Ty));
3859    LoadAddr = Builder.CreateBitCast(LoadAddr, IntResTy->getPointerTo());
3860
3861    Function *F = CGM.getIntrinsic(BuiltinID == AArch64::BI__builtin_arm_ldaex
3862                                       ? Intrinsic::aarch64_ldaxr
3863                                       : Intrinsic::aarch64_ldxr,
3864                                   LoadAddr->getType());
3865    Value *Val = Builder.CreateCall(F, LoadAddr, "ldxr");
3866
3867    if (RealResTy->isPointerTy())
3868      return Builder.CreateIntToPtr(Val, RealResTy);
3869
3870    Val = Builder.CreateTruncOrBitCast(Val, IntResTy);
3871    return Builder.CreateBitCast(Val, RealResTy);
3872  }
3873
3874  if ((BuiltinID == AArch64::BI__builtin_arm_strex ||
3875       BuiltinID == AArch64::BI__builtin_arm_stlex) &&
3876      getContext().getTypeSize(E->getArg(0)->getType()) == 128) {
3877    Function *F = CGM.getIntrinsic(BuiltinID == AArch64::BI__builtin_arm_stlex
3878                                       ? Intrinsic::aarch64_stlxp
3879                                       : Intrinsic::aarch64_stxp);
3880    llvm::Type *STy = llvm::StructType::get(Int64Ty, Int64Ty, NULL);
3881
3882    Value *One = llvm::ConstantInt::get(Int32Ty, 1);
3883    Value *Tmp = Builder.CreateAlloca(ConvertType(E->getArg(0)->getType()),
3884                                      One);
3885    Value *Val = EmitScalarExpr(E->getArg(0));
3886    Builder.CreateStore(Val, Tmp);
3887
3888    Value *LdPtr = Builder.CreateBitCast(Tmp,llvm::PointerType::getUnqual(STy));
3889    Val = Builder.CreateLoad(LdPtr);
3890
3891    Value *Arg0 = Builder.CreateExtractValue(Val, 0);
3892    Value *Arg1 = Builder.CreateExtractValue(Val, 1);
3893    Value *StPtr = Builder.CreateBitCast(EmitScalarExpr(E->getArg(1)),
3894                                         Int8PtrTy);
3895    return Builder.CreateCall3(F, Arg0, Arg1, StPtr, "stxp");
3896  } else if (BuiltinID == AArch64::BI__builtin_arm_strex ||
3897             BuiltinID == AArch64::BI__builtin_arm_stlex) {
3898    Value *StoreVal = EmitScalarExpr(E->getArg(0));
3899    Value *StoreAddr = EmitScalarExpr(E->getArg(1));
3900
3901    QualType Ty = E->getArg(0)->getType();
3902    llvm::Type *StoreTy = llvm::IntegerType::get(getLLVMContext(),
3903                                                 getContext().getTypeSize(Ty));
3904    StoreAddr = Builder.CreateBitCast(StoreAddr, StoreTy->getPointerTo());
3905
3906    if (StoreVal->getType()->isPointerTy())
3907      StoreVal = Builder.CreatePtrToInt(StoreVal, Int64Ty);
3908    else {
3909      StoreVal = Builder.CreateBitCast(StoreVal, StoreTy);
3910      StoreVal = Builder.CreateZExtOrBitCast(StoreVal, Int64Ty);
3911    }
3912
3913    Function *F = CGM.getIntrinsic(BuiltinID == AArch64::BI__builtin_arm_stlex
3914                                       ? Intrinsic::aarch64_stlxr
3915                                       : Intrinsic::aarch64_stxr,
3916                                   StoreAddr->getType());
3917    return Builder.CreateCall2(F, StoreVal, StoreAddr, "stxr");
3918  }
3919
3920  if (BuiltinID == AArch64::BI__builtin_arm_clrex) {
3921    Function *F = CGM.getIntrinsic(Intrinsic::aarch64_clrex);
3922    return Builder.CreateCall(F);
3923  }
3924
3925  // CRC32
3926  Intrinsic::ID CRCIntrinsicID = Intrinsic::not_intrinsic;
3927  switch (BuiltinID) {
3928  case AArch64::BI__builtin_arm_crc32b:
3929    CRCIntrinsicID = Intrinsic::aarch64_crc32b; break;
3930  case AArch64::BI__builtin_arm_crc32cb:
3931    CRCIntrinsicID = Intrinsic::aarch64_crc32cb; break;
3932  case AArch64::BI__builtin_arm_crc32h:
3933    CRCIntrinsicID = Intrinsic::aarch64_crc32h; break;
3934  case AArch64::BI__builtin_arm_crc32ch:
3935    CRCIntrinsicID = Intrinsic::aarch64_crc32ch; break;
3936  case AArch64::BI__builtin_arm_crc32w:
3937    CRCIntrinsicID = Intrinsic::aarch64_crc32w; break;
3938  case AArch64::BI__builtin_arm_crc32cw:
3939    CRCIntrinsicID = Intrinsic::aarch64_crc32cw; break;
3940  case AArch64::BI__builtin_arm_crc32d:
3941    CRCIntrinsicID = Intrinsic::aarch64_crc32x; break;
3942  case AArch64::BI__builtin_arm_crc32cd:
3943    CRCIntrinsicID = Intrinsic::aarch64_crc32cx; break;
3944  }
3945
3946  if (CRCIntrinsicID != Intrinsic::not_intrinsic) {
3947    Value *Arg0 = EmitScalarExpr(E->getArg(0));
3948    Value *Arg1 = EmitScalarExpr(E->getArg(1));
3949    Function *F = CGM.getIntrinsic(CRCIntrinsicID);
3950
3951    llvm::Type *DataTy = F->getFunctionType()->getParamType(1);
3952    Arg1 = Builder.CreateZExtOrBitCast(Arg1, DataTy);
3953
3954    return Builder.CreateCall2(F, Arg0, Arg1);
3955  }
3956
3957  llvm::SmallVector<Value*, 4> Ops;
3958  for (unsigned i = 0, e = E->getNumArgs() - 1; i != e; i++)
3959    Ops.push_back(EmitScalarExpr(E->getArg(i)));
3960
3961  ArrayRef<NeonIntrinsicInfo> SISDMap(AArch64SISDIntrinsicMap);
3962  const NeonIntrinsicInfo *Builtin = findNeonIntrinsicInMap(
3963      SISDMap, BuiltinID, AArch64SISDIntrinsicsProvenSorted);
3964
3965  if (Builtin) {
3966    Ops.push_back(EmitScalarExpr(E->getArg(E->getNumArgs() - 1)));
3967    Value *Result = EmitCommonNeonSISDBuiltinExpr(*this, *Builtin, Ops, E);
3968    assert(Result && "SISD intrinsic should have been handled");
3969    return Result;
3970  }
3971
3972  llvm::APSInt Result;
3973  const Expr *Arg = E->getArg(E->getNumArgs()-1);
3974  NeonTypeFlags Type(0);
3975  if (Arg->isIntegerConstantExpr(Result, getContext()))
3976    // Determine the type of this overloaded NEON intrinsic.
3977    Type = NeonTypeFlags(Result.getZExtValue());
3978
3979  bool usgn = Type.isUnsigned();
3980  bool quad = Type.isQuad();
3981
3982  // Handle non-overloaded intrinsics first.
3983  switch (BuiltinID) {
3984  default: break;
3985  case NEON::BI__builtin_neon_vldrq_p128: {
3986    llvm::Type *Int128PTy = llvm::Type::getIntNPtrTy(getLLVMContext(), 128);
3987    Value *Ptr = Builder.CreateBitCast(EmitScalarExpr(E->getArg(0)), Int128PTy);
3988    return Builder.CreateLoad(Ptr);
3989  }
3990  case NEON::BI__builtin_neon_vstrq_p128: {
3991    llvm::Type *Int128PTy = llvm::Type::getIntNPtrTy(getLLVMContext(), 128);
3992    Value *Ptr = Builder.CreateBitCast(Ops[0], Int128PTy);
3993    return Builder.CreateStore(EmitScalarExpr(E->getArg(1)), Ptr);
3994  }
3995  case NEON::BI__builtin_neon_vcvts_u32_f32:
3996  case NEON::BI__builtin_neon_vcvtd_u64_f64:
3997    usgn = true;
3998    // FALL THROUGH
3999  case NEON::BI__builtin_neon_vcvts_s32_f32:
4000  case NEON::BI__builtin_neon_vcvtd_s64_f64: {
4001    Ops.push_back(EmitScalarExpr(E->getArg(0)));
4002    bool Is64 = Ops[0]->getType()->getPrimitiveSizeInBits() == 64;
4003    llvm::Type *InTy = Is64 ?