Coverage Report

Created: 2019-07-24 05:18

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