Coverage Report

Created: 2021-01-26 06:56

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Sema/SemaChecking.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- SemaChecking.cpp - Extra Semantic Checking -------------------------===//
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 file implements extra semantic analysis beyond what is enforced
10
//  by the C type system.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "clang/AST/APValue.h"
15
#include "clang/AST/ASTContext.h"
16
#include "clang/AST/Attr.h"
17
#include "clang/AST/AttrIterator.h"
18
#include "clang/AST/CharUnits.h"
19
#include "clang/AST/Decl.h"
20
#include "clang/AST/DeclBase.h"
21
#include "clang/AST/DeclCXX.h"
22
#include "clang/AST/DeclObjC.h"
23
#include "clang/AST/DeclarationName.h"
24
#include "clang/AST/EvaluatedExprVisitor.h"
25
#include "clang/AST/Expr.h"
26
#include "clang/AST/ExprCXX.h"
27
#include "clang/AST/ExprObjC.h"
28
#include "clang/AST/ExprOpenMP.h"
29
#include "clang/AST/FormatString.h"
30
#include "clang/AST/NSAPI.h"
31
#include "clang/AST/NonTrivialTypeVisitor.h"
32
#include "clang/AST/OperationKinds.h"
33
#include "clang/AST/RecordLayout.h"
34
#include "clang/AST/Stmt.h"
35
#include "clang/AST/TemplateBase.h"
36
#include "clang/AST/Type.h"
37
#include "clang/AST/TypeLoc.h"
38
#include "clang/AST/UnresolvedSet.h"
39
#include "clang/Basic/AddressSpaces.h"
40
#include "clang/Basic/CharInfo.h"
41
#include "clang/Basic/Diagnostic.h"
42
#include "clang/Basic/IdentifierTable.h"
43
#include "clang/Basic/LLVM.h"
44
#include "clang/Basic/LangOptions.h"
45
#include "clang/Basic/OpenCLOptions.h"
46
#include "clang/Basic/OperatorKinds.h"
47
#include "clang/Basic/PartialDiagnostic.h"
48
#include "clang/Basic/SourceLocation.h"
49
#include "clang/Basic/SourceManager.h"
50
#include "clang/Basic/Specifiers.h"
51
#include "clang/Basic/SyncScope.h"
52
#include "clang/Basic/TargetBuiltins.h"
53
#include "clang/Basic/TargetCXXABI.h"
54
#include "clang/Basic/TargetInfo.h"
55
#include "clang/Basic/TypeTraits.h"
56
#include "clang/Lex/Lexer.h" // TODO: Extract static functions to fix layering.
57
#include "clang/Sema/Initialization.h"
58
#include "clang/Sema/Lookup.h"
59
#include "clang/Sema/Ownership.h"
60
#include "clang/Sema/Scope.h"
61
#include "clang/Sema/ScopeInfo.h"
62
#include "clang/Sema/Sema.h"
63
#include "clang/Sema/SemaInternal.h"
64
#include "llvm/ADT/APFloat.h"
65
#include "llvm/ADT/APInt.h"
66
#include "llvm/ADT/APSInt.h"
67
#include "llvm/ADT/ArrayRef.h"
68
#include "llvm/ADT/DenseMap.h"
69
#include "llvm/ADT/FoldingSet.h"
70
#include "llvm/ADT/None.h"
71
#include "llvm/ADT/Optional.h"
72
#include "llvm/ADT/STLExtras.h"
73
#include "llvm/ADT/SmallBitVector.h"
74
#include "llvm/ADT/SmallPtrSet.h"
75
#include "llvm/ADT/SmallString.h"
76
#include "llvm/ADT/SmallVector.h"
77
#include "llvm/ADT/StringRef.h"
78
#include "llvm/ADT/StringSet.h"
79
#include "llvm/ADT/StringSwitch.h"
80
#include "llvm/ADT/Triple.h"
81
#include "llvm/Support/AtomicOrdering.h"
82
#include "llvm/Support/Casting.h"
83
#include "llvm/Support/Compiler.h"
84
#include "llvm/Support/ConvertUTF.h"
85
#include "llvm/Support/ErrorHandling.h"
86
#include "llvm/Support/Format.h"
87
#include "llvm/Support/Locale.h"
88
#include "llvm/Support/MathExtras.h"
89
#include "llvm/Support/SaveAndRestore.h"
90
#include "llvm/Support/raw_ostream.h"
91
#include <algorithm>
92
#include <bitset>
93
#include <cassert>
94
#include <cstddef>
95
#include <cstdint>
96
#include <functional>
97
#include <limits>
98
#include <string>
99
#include <tuple>
100
#include <utility>
101
102
using namespace clang;
103
using namespace sema;
104
105
SourceLocation Sema::getLocationOfStringLiteralByte(const StringLiteral *SL,
106
24
                                                    unsigned ByteNo) const {
107
24
  return SL->getLocationOfByte(ByteNo, getSourceManager(), LangOpts,
108
24
                               Context.getTargetInfo());
109
24
}
110
111
/// Checks that a call expression's argument count is the desired number.
112
/// This is useful when doing custom type-checking.  Returns true on error.
113
12.8k
static bool checkArgCount(Sema &S, CallExpr *call, unsigned desiredArgCount) {
114
12.8k
  unsigned argCount = call->getNumArgs();
115
12.8k
  if (argCount == desiredArgCount) 
return false12.7k
;
116
117
79
  if (argCount < desiredArgCount)
118
42
    return S.Diag(call->getEndLoc(), diag::err_typecheck_call_too_few_args)
119
42
           << 0 /*function call*/ << desiredArgCount << argCount
120
42
           << call->getSourceRange();
121
122
  // Highlight all the excess arguments.
123
37
  SourceRange range(call->getArg(desiredArgCount)->getBeginLoc(),
124
37
                    call->getArg(argCount - 1)->getEndLoc());
125
126
37
  return S.Diag(range.getBegin(), diag::err_typecheck_call_too_many_args)
127
37
    << 0 /*function call*/ << desiredArgCount << argCount
128
37
    << call->getArg(1)->getSourceRange();
129
37
}
130
131
/// Check that the first argument to __builtin_annotation is an integer
132
/// and the second argument is a non-wide string literal.
133
12
static bool SemaBuiltinAnnotation(Sema &S, CallExpr *TheCall) {
134
12
  if (checkArgCount(S, TheCall, 2))
135
0
    return true;
136
137
  // First argument should be an integer.
138
12
  Expr *ValArg = TheCall->getArg(0);
139
12
  QualType Ty = ValArg->getType();
140
12
  if (!Ty->isIntegerType()) {
141
1
    S.Diag(ValArg->getBeginLoc(), diag::err_builtin_annotation_first_arg)
142
1
        << ValArg->getSourceRange();
143
1
    return true;
144
1
  }
145
146
  // Second argument should be a constant string.
147
11
  Expr *StrArg = TheCall->getArg(1)->IgnoreParenCasts();
148
11
  StringLiteral *Literal = dyn_cast<StringLiteral>(StrArg);
149
11
  if (!Literal || 
!Literal->isAscii()10
) {
150
2
    S.Diag(StrArg->getBeginLoc(), diag::err_builtin_annotation_second_arg)
151
2
        << StrArg->getSourceRange();
152
2
    return true;
153
2
  }
154
155
9
  TheCall->setType(Ty);
156
9
  return false;
157
9
}
158
159
31
static bool SemaBuiltinMSVCAnnotation(Sema &S, CallExpr *TheCall) {
160
  // We need at least one argument.
161
31
  if (TheCall->getNumArgs() < 1) {
162
3
    S.Diag(TheCall->getEndLoc(), diag::err_typecheck_call_too_few_args_at_least)
163
3
        << 0 << 1 << TheCall->getNumArgs()
164
3
        << TheCall->getCallee()->getSourceRange();
165
3
    return true;
166
3
  }
167
168
  // All arguments should be wide string literals.
169
49
  
for (Expr *Arg : TheCall->arguments())28
{
170
49
    auto *Literal = dyn_cast<StringLiteral>(Arg->IgnoreParenCasts());
171
49
    if (!Literal || 
!Literal->isWide()43
) {
172
6
      S.Diag(Arg->getBeginLoc(), diag::err_msvc_annotation_wide_str)
173
6
          << Arg->getSourceRange();
174
6
      return true;
175
6
    }
176
49
  }
177
178
22
  return false;
179
28
}
180
181
/// Check that the argument to __builtin_addressof is a glvalue, and set the
182
/// result type to the corresponding pointer type.
183
1.04k
static bool SemaBuiltinAddressof(Sema &S, CallExpr *TheCall) {
184
1.04k
  if (checkArgCount(S, TheCall, 1))
185
0
    return true;
186
187
1.04k
  ExprResult Arg(TheCall->getArg(0));
188
1.04k
  QualType ResultType = S.CheckAddressOfOperand(Arg, TheCall->getBeginLoc());
189
1.04k
  if (ResultType.isNull())
190
2
    return true;
191
192
1.04k
  TheCall->setArg(0, Arg.get());
193
1.04k
  TheCall->setType(ResultType);
194
1.04k
  return false;
195
1.04k
}
196
197
/// Check the number of arguments and set the result type to
198
/// the argument type.
199
29
static bool SemaBuiltinPreserveAI(Sema &S, CallExpr *TheCall) {
200
29
  if (checkArgCount(S, TheCall, 1))
201
1
    return true;
202
203
28
  TheCall->setType(TheCall->getArg(0)->getType());
204
28
  return false;
205
28
}
206
207
/// Check that the value argument for __builtin_is_aligned(value, alignment) and
208
/// __builtin_aligned_{up,down}(value, alignment) is an integer or a pointer
209
/// type (but not a function pointer) and that the alignment is a power-of-two.
210
281
static bool SemaBuiltinAlignment(Sema &S, CallExpr *TheCall, unsigned ID) {
211
281
  if (checkArgCount(S, TheCall, 2))
212
1
    return true;
213
214
280
  clang::Expr *Source = TheCall->getArg(0);
215
280
  bool IsBooleanAlignBuiltin = ID == Builtin::BI__builtin_is_aligned;
216
217
406
  auto IsValidIntegerType = [](QualType Ty) {
218
406
    return Ty->isIntegerType() && 
!Ty->isEnumeralType()386
&&
!Ty->isBooleanType()380
;
219
406
  };
220
280
  QualType SrcTy = Source->getType();
221
  // We should also be able to use it with arrays (but not functions!).
222
280
  if (SrcTy->canDecayToPointerType() && 
SrcTy->isArrayType()24
) {
223
21
    SrcTy = S.Context.getDecayedType(SrcTy);
224
21
  }
225
280
  if ((!SrcTy->isPointerType() && 
!IsValidIntegerType(SrcTy)150
) ||
226
259
      SrcTy->isFunctionPointerType()) {
227
    // FIXME: this is not quite the right error message since we don't allow
228
    // floating point types, or member pointers.
229
24
    S.Diag(Source->getExprLoc(), diag::err_typecheck_expect_scalar_operand)
230
24
        << SrcTy;
231
24
    return true;
232
24
  }
233
234
256
  clang::Expr *AlignOp = TheCall->getArg(1);
235
256
  if (!IsValidIntegerType(AlignOp->getType())) {
236
12
    S.Diag(AlignOp->getExprLoc(), diag::err_typecheck_expect_int)
237
12
        << AlignOp->getType();
238
12
    return true;
239
12
  }
240
244
  Expr::EvalResult AlignResult;
241
244
  unsigned MaxAlignmentBits = S.Context.getIntWidth(SrcTy) - 1;
242
  // We can't check validity of alignment if it is value dependent.
243
244
  if (!AlignOp->isValueDependent() &&
244
243
      AlignOp->EvaluateAsInt(AlignResult, S.Context,
245
196
                             Expr::SE_AllowSideEffects)) {
246
196
    llvm::APSInt AlignValue = AlignResult.Val.getInt();
247
196
    llvm::APSInt MaxValue(
248
196
        llvm::APInt::getOneBitSet(MaxAlignmentBits + 1, MaxAlignmentBits));
249
196
    if (AlignValue < 1) {
250
4
      S.Diag(AlignOp->getExprLoc(), diag::err_alignment_too_small) << 1;
251
4
      return true;
252
4
    }
253
192
    if (llvm::APSInt::compareValues(AlignValue, MaxValue) > 0) {
254
15
      S.Diag(AlignOp->getExprLoc(), diag::err_alignment_too_big)
255
15
          << MaxValue.toString(10);
256
15
      return true;
257
15
    }
258
177
    if (!AlignValue.isPowerOf2()) {
259
16
      S.Diag(AlignOp->getExprLoc(), diag::err_alignment_not_power_of_two);
260
16
      return true;
261
16
    }
262
161
    if (AlignValue == 1) {
263
3
      S.Diag(AlignOp->getExprLoc(), diag::warn_alignment_builtin_useless)
264
3
          << IsBooleanAlignBuiltin;
265
3
    }
266
161
  }
267
268
209
  ExprResult SrcArg = S.PerformCopyInitialization(
269
209
      InitializedEntity::InitializeParameter(S.Context, SrcTy, false),
270
209
      SourceLocation(), Source);
271
209
  if (SrcArg.isInvalid())
272
0
    return true;
273
209
  TheCall->setArg(0, SrcArg.get());
274
209
  ExprResult AlignArg =
275
209
      S.PerformCopyInitialization(InitializedEntity::InitializeParameter(
276
209
                                      S.Context, AlignOp->getType(), false),
277
209
                                  SourceLocation(), AlignOp);
278
209
  if (AlignArg.isInvalid())
279
0
    return true;
280
209
  TheCall->setArg(1, AlignArg.get());
281
  // For align_up/align_down, the return type is the same as the (potentially
282
  // decayed) argument type including qualifiers. For is_aligned(), the result
283
  // is always bool.
284
138
  TheCall->setType(IsBooleanAlignBuiltin ? 
S.Context.BoolTy71
: SrcTy);
285
209
  return false;
286
209
}
287
288
static bool SemaBuiltinOverflow(Sema &S, CallExpr *TheCall,
289
114
                                unsigned BuiltinID) {
290
114
  if (checkArgCount(S, TheCall, 3))
291
2
    return true;
292
293
  // First two arguments should be integers.
294
333
  
for (unsigned I = 0; 112
I < 2;
++I221
) {
295
223
    ExprResult Arg = S.DefaultFunctionArrayLvalueConversion(TheCall->getArg(I));
296
223
    if (Arg.isInvalid()) 
return true0
;
297
223
    TheCall->setArg(I, Arg.get());
298
299
223
    QualType Ty = Arg.get()->getType();
300
223
    if (!Ty->isIntegerType()) {
301
2
      S.Diag(Arg.get()->getBeginLoc(), diag::err_overflow_builtin_must_be_int)
302
2
          << Ty << Arg.get()->getSourceRange();
303
2
      return true;
304
2
    }
305
223
  }
306
307
  // Third argument should be a pointer to a non-const integer.
308
  // IRGen correctly handles volatile, restrict, and address spaces, and
309
  // the other qualifiers aren't possible.
310
110
  {
311
110
    ExprResult Arg = S.DefaultFunctionArrayLvalueConversion(TheCall->getArg(2));
312
110
    if (Arg.isInvalid()) 
return true0
;
313
110
    TheCall->setArg(2, Arg.get());
314
315
110
    QualType Ty = Arg.get()->getType();
316
110
    const auto *PtrTy = Ty->getAs<PointerType>();
317
110
    if (!PtrTy ||
318
109
        !PtrTy->getPointeeType()->isIntegerType() ||
319
108
        PtrTy->getPointeeType().isConstQualified()) {
320
3
      S.Diag(Arg.get()->getBeginLoc(),
321
3
             diag::err_overflow_builtin_must_be_ptr_int)
322
3
        << Ty << Arg.get()->getSourceRange();
323
3
      return true;
324
3
    }
325
107
  }
326
327
  // Disallow signed ExtIntType args larger than 128 bits to mul function until
328
  // we improve backend support.
329
107
  if (BuiltinID == Builtin::BI__builtin_mul_overflow) {
330
241
    for (unsigned I = 0; I < 3; 
++I180
) {
331
181
      const auto Arg = TheCall->getArg(I);
332
      // Third argument will be a pointer.
333
121
      auto Ty = I < 2 ? Arg->getType() : 
Arg->getType()->getPointeeType()60
;
334
181
      if (Ty->isExtIntType() && 
Ty->isSignedIntegerType()34
&&
335
31
          S.getASTContext().getIntWidth(Ty) > 128)
336
1
        return S.Diag(Arg->getBeginLoc(),
337
1
                      diag::err_overflow_builtin_ext_int_max_size)
338
1
               << 128;
339
181
    }
340
61
  }
341
342
106
  return false;
343
107
}
344
345
16
static bool SemaBuiltinCallWithStaticChain(Sema &S, CallExpr *BuiltinCall) {
346
16
  if (checkArgCount(S, BuiltinCall, 2))
347
0
    return true;
348
349
16
  SourceLocation BuiltinLoc = BuiltinCall->getBeginLoc();
350
16
  Expr *Builtin = BuiltinCall->getCallee()->IgnoreImpCasts();
351
16
  Expr *Call = BuiltinCall->getArg(0);
352
16
  Expr *Chain = BuiltinCall->getArg(1);
353
354
16
  if (Call->getStmtClass() != Stmt::CallExprClass) {
355
2
    S.Diag(BuiltinLoc, diag::err_first_argument_to_cwsc_not_call)
356
2
        << Call->getSourceRange();
357
2
    return true;
358
2
  }
359
360
14
  auto CE = cast<CallExpr>(Call);
361
14
  if (CE->getCallee()->getType()->isBlockPointerType()) {
362
1
    S.Diag(BuiltinLoc, diag::err_first_argument_to_cwsc_block_call)
363
1
        << Call->getSourceRange();
364
1
    return true;
365
1
  }
366
367
13
  const Decl *TargetDecl = CE->getCalleeDecl();
368
13
  if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(TargetDecl))
369
12
    if (FD->getBuiltinID()) {
370
1
      S.Diag(BuiltinLoc, diag::err_first_argument_to_cwsc_builtin_call)
371
1
          << Call->getSourceRange();
372
1
      return true;
373
1
    }
374
375
12
  if (isa<CXXPseudoDestructorExpr>(CE->getCallee()->IgnoreParens())) {
376
1
    S.Diag(BuiltinLoc, diag::err_first_argument_to_cwsc_pdtor_call)
377
1
        << Call->getSourceRange();
378
1
    return true;
379
1
  }
380
381
11
  ExprResult ChainResult = S.UsualUnaryConversions(Chain);
382
11
  if (ChainResult.isInvalid())
383
0
    return true;
384
11
  if (!ChainResult.get()->getType()->isPointerType()) {
385
1
    S.Diag(BuiltinLoc, diag::err_second_argument_to_cwsc_not_pointer)
386
1
        << Chain->getSourceRange();
387
1
    return true;
388
1
  }
389
390
10
  QualType ReturnTy = CE->getCallReturnType(S.Context);
391
10
  QualType ArgTys[2] = { ReturnTy, ChainResult.get()->getType() };
392
10
  QualType BuiltinTy = S.Context.getFunctionType(
393
10
      ReturnTy, ArgTys, FunctionProtoType::ExtProtoInfo());
394
10
  QualType BuiltinPtrTy = S.Context.getPointerType(BuiltinTy);
395
396
10
  Builtin =
397
10
      S.ImpCastExprToType(Builtin, BuiltinPtrTy, CK_BuiltinFnToFnPtr).get();
398
399
10
  BuiltinCall->setType(CE->getType());
400
10
  BuiltinCall->setValueKind(CE->getValueKind());
401
10
  BuiltinCall->setObjectKind(CE->getObjectKind());
402
10
  BuiltinCall->setCallee(Builtin);
403
10
  BuiltinCall->setArg(1, ChainResult.get());
404
405
10
  return false;
406
10
}
407
408
namespace {
409
410
class EstimateSizeFormatHandler
411
    : public analyze_format_string::FormatStringHandler {
412
  size_t Size;
413
414
public:
415
  EstimateSizeFormatHandler(StringRef Format)
416
      : Size(std::min(Format.find(0), Format.size()) +
417
329
             1 /* null byte always written by sprintf */) {}
418
419
  bool HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier &FS,
420
290
                             const char *, unsigned SpecifierLen) override {
421
422
290
    const size_t FieldWidth = computeFieldWidth(FS);
423
290
    const size_t Precision = computePrecision(FS);
424
425
    // The actual format.
426
290
    switch (FS.getConversionSpecifier().getKind()) {
427
    // Just a char.
428
16
    case analyze_format_string::ConversionSpecifier::cArg:
429
16
    case analyze_format_string::ConversionSpecifier::CArg:
430
16
      Size += std::max(FieldWidth, (size_t)1);
431
16
      break;
432
    // Just an integer.
433
98
    case analyze_format_string::ConversionSpecifier::dArg:
434
98
    case analyze_format_string::ConversionSpecifier::DArg:
435
122
    case analyze_format_string::ConversionSpecifier::iArg:
436
130
    case analyze_format_string::ConversionSpecifier::oArg:
437
130
    case analyze_format_string::ConversionSpecifier::OArg:
438
139
    case analyze_format_string::ConversionSpecifier::uArg:
439
139
    case analyze_format_string::ConversionSpecifier::UArg:
440
163
    case analyze_format_string::ConversionSpecifier::xArg:
441
171
    case analyze_format_string::ConversionSpecifier::XArg:
442
171
      Size += std::max(FieldWidth, Precision);
443
171
      break;
444
445
    // %g style conversion switches between %f or %e style dynamically.
446
    // %f always takes less space, so default to it.
447
0
    case analyze_format_string::ConversionSpecifier::gArg:
448
0
    case analyze_format_string::ConversionSpecifier::GArg:
449
450
    // Floating point number in the form '[+]ddd.ddd'.
451
40
    case analyze_format_string::ConversionSpecifier::fArg:
452
40
    case analyze_format_string::ConversionSpecifier::FArg:
453
40
      Size += std::max(FieldWidth, 1 /* integer part */ +
454
40
                                       (Precision ? 1 + Precision
455
0
                                                  : 0) /* period + decimal */);
456
40
      break;
457
458
    // Floating point number in the form '[-]d.ddde[+-]dd'.
459
16
    case analyze_format_string::ConversionSpecifier::eArg:
460
16
    case analyze_format_string::ConversionSpecifier::EArg:
461
16
      Size +=
462
16
          std::max(FieldWidth,
463
16
                   1 /* integer part */ +
464
12
                       (Precision ? 1 + Precision : 
04
) /* period + decimal */ +
465
16
                       1 /* e or E letter */ + 2 /* exponent */);
466
16
      break;
467
468
    // Floating point number in the form '[-]0xh.hhhhp±dd'.
469
0
    case analyze_format_string::ConversionSpecifier::aArg:
470
0
    case analyze_format_string::ConversionSpecifier::AArg:
471
0
      Size +=
472
0
          std::max(FieldWidth,
473
0
                   2 /* 0x */ + 1 /* integer part */ +
474
0
                       (Precision ? 1 + Precision : 0) /* period + decimal */ +
475
0
                       1 /* p or P letter */ + 1 /* + or - */ + 1 /* value */);
476
0
      break;
477
478
    // Just a string.
479
11
    case analyze_format_string::ConversionSpecifier::sArg:
480
11
    case analyze_format_string::ConversionSpecifier::SArg:
481
11
      Size += FieldWidth;
482
11
      break;
483
484
    // Just a pointer in the form '0xddd'.
485
16
    case analyze_format_string::ConversionSpecifier::pArg:
486
16
      Size += std::max(FieldWidth, 2 /* leading 0x */ + Precision);
487
16
      break;
488
489
    // A plain percent.
490
20
    case analyze_format_string::ConversionSpecifier::PercentArg:
491
20
      Size += 1;
492
20
      break;
493
494
0
    default:
495
0
      break;
496
290
    }
497
498
290
    Size += FS.hasPlusPrefix() || 
FS.hasSpacePrefix()258
;
499
500
290
    if (FS.hasAlternativeForm()) {
501
16
      switch (FS.getConversionSpecifier().getKind()) {
502
0
      default:
503
0
        break;
504
      // Force a leading '0'.
505
0
      case analyze_format_string::ConversionSpecifier::oArg:
506
0
        Size += 1;
507
0
        break;
508
      // Force a leading '0x'.
509
16
      case analyze_format_string::ConversionSpecifier::xArg:
510
16
      case analyze_format_string::ConversionSpecifier::XArg:
511
16
        Size += 2;
512
16
        break;
513
      // Force a period '.' before decimal, even if precision is 0.
514
0
      case analyze_format_string::ConversionSpecifier::aArg:
515
0
      case analyze_format_string::ConversionSpecifier::AArg:
516
0
      case analyze_format_string::ConversionSpecifier::eArg:
517
0
      case analyze_format_string::ConversionSpecifier::EArg:
518
0
      case analyze_format_string::ConversionSpecifier::fArg:
519
0
      case analyze_format_string::ConversionSpecifier::FArg:
520
0
      case analyze_format_string::ConversionSpecifier::gArg:
521
0
      case analyze_format_string::ConversionSpecifier::GArg:
522
0
        Size += (Precision ? 0 : 1);
523
0
        break;
524
290
      }
525
290
    }
526
290
    assert(SpecifierLen <= Size && "no underflow");
527
290
    Size -= SpecifierLen;
528
290
    return true;
529
290
  }
530
531
329
  size_t getSizeLowerBound() const { return Size; }
532
533
private:
534
290
  static size_t computeFieldWidth(const analyze_printf::PrintfSpecifier &FS) {
535
290
    const analyze_format_string::OptionalAmount &FW = FS.getFieldWidth();
536
290
    size_t FieldWidth = 0;
537
290
    if (FW.getHowSpecified() == analyze_format_string::OptionalAmount::Constant)
538
18
      FieldWidth = FW.getConstantAmount();
539
290
    return FieldWidth;
540
290
  }
541
542
290
  static size_t computePrecision(const analyze_printf::PrintfSpecifier &FS) {
543
290
    const analyze_format_string::OptionalAmount &FW = FS.getPrecision();
544
290
    size_t Precision = 0;
545
546
    // See man 3 printf for default precision value based on the specifier.
547
290
    switch (FW.getHowSpecified()) {
548
266
    case analyze_format_string::OptionalAmount::NotSpecified:
549
266
      switch (FS.getConversionSpecifier().getKind()) {
550
47
      default:
551
47
        break;
552
98
      case analyze_format_string::ConversionSpecifier::dArg: // %d
553
98
      case analyze_format_string::ConversionSpecifier::DArg: // %D
554
122
      case analyze_format_string::ConversionSpecifier::iArg: // %i
555
122
        Precision = 1;
556
122
        break;
557
8
      case analyze_format_string::ConversionSpecifier::oArg: // %d
558
8
      case analyze_format_string::ConversionSpecifier::OArg: // %D
559
17
      case analyze_format_string::ConversionSpecifier::uArg: // %d
560
17
      case analyze_format_string::ConversionSpecifier::UArg: // %D
561
41
      case analyze_format_string::ConversionSpecifier::xArg: // %d
562
49
      case analyze_format_string::ConversionSpecifier::XArg: // %D
563
49
        Precision = 1;
564
49
        break;
565
24
      case analyze_format_string::ConversionSpecifier::fArg: // %f
566
24
      case analyze_format_string::ConversionSpecifier::FArg: // %F
567
32
      case analyze_format_string::ConversionSpecifier::eArg: // %e
568
32
      case analyze_format_string::ConversionSpecifier::EArg: // %E
569
32
      case analyze_format_string::ConversionSpecifier::gArg: // %g
570
32
      case analyze_format_string::ConversionSpecifier::GArg: // %G
571
32
        Precision = 6;
572
32
        break;
573
16
      case analyze_format_string::ConversionSpecifier::pArg: // %d
574
16
        Precision = 1;
575
16
        break;
576
266
      }
577
266
      break;
578
24
    case analyze_format_string::OptionalAmount::Constant:
579
24
      Precision = FW.getConstantAmount();
580
24
      break;
581
0
    default:
582
0
      break;
583
290
    }
584
290
    return Precision;
585
290
  }
586
};
587
588
} // namespace
589
590
/// Check a call to BuiltinID for buffer overflows. If BuiltinID is a
591
/// __builtin_*_chk function, then use the object size argument specified in the
592
/// source. Otherwise, infer the object size using __builtin_object_size.
593
void Sema::checkFortifiedBuiltinMemoryFunction(FunctionDecl *FD,
594
2.90M
                                               CallExpr *TheCall) {
595
  // FIXME: There are some more useful checks we could be doing here:
596
  //  - Evaluate strlen of strcpy arguments, use as object size.
597
598
2.90M
  if (TheCall->isValueDependent() || 
TheCall->isTypeDependent()2.88M
||
599
2.88M
      isConstantEvaluated())
600
40.9k
    return;
601
602
2.86M
  unsigned BuiltinID = FD->getBuiltinID(/*ConsiderWrappers=*/true);
603
2.86M
  if (!BuiltinID)
604
1.42M
    return;
605
606
1.43M
  const TargetInfo &TI = getASTContext().getTargetInfo();
607
1.43M
  unsigned SizeTypeWidth = TI.getTypeWidth(TI.getSizeType());
608
609
1.43M
  unsigned DiagID = 0;
610
1.43M
  bool IsChkVariant = false;
611
1.43M
  Optional<llvm::APSInt> UsedSize;
612
1.43M
  unsigned SizeIndex, ObjectIndex;
613
1.43M
  switch (BuiltinID) {
614
1.43M
  default:
615
1.43M
    return;
616
149
  case Builtin::BIsprintf:
617
346
  case Builtin::BI__builtin___sprintf_chk: {
618
197
    size_t FormatIndex = BuiltinID == Builtin::BIsprintf ? 
1149
: 3;
619
346
    auto *FormatExpr = TheCall->getArg(FormatIndex)->IgnoreParenImpCasts();
620
621
346
    if (auto *Format = dyn_cast<StringLiteral>(FormatExpr)) {
622
623
329
      if (!Format->isAscii() && 
!Format->isUTF8()0
)
624
0
        return;
625
626
329
      StringRef FormatStrRef = Format->getString();
627
329
      EstimateSizeFormatHandler H(FormatStrRef);
628
329
      const char *FormatBytes = FormatStrRef.data();
629
329
      const ConstantArrayType *T =
630
329
          Context.getAsConstantArrayType(Format->getType());
631
329
      assert(T && "String literal not of constant array type!");
632
329
      size_t TypeSize = T->getSize().getZExtValue();
633
634
      // In case there's a null byte somewhere.
635
329
      size_t StrLen =
636
329
          std::min(std::max(TypeSize, size_t(1)) - 1, FormatStrRef.find(0));
637
329
      if (!analyze_format_string::ParsePrintfString(
638
329
              H, FormatBytes, FormatBytes + StrLen, getLangOpts(),
639
329
              Context.getTargetInfo(), false)) {
640
329
        DiagID = diag::warn_fortify_source_format_overflow;
641
329
        UsedSize = llvm::APSInt::getUnsigned(H.getSizeLowerBound())
642
329
                       .extOrTrunc(SizeTypeWidth);
643
329
        if (BuiltinID == Builtin::BI__builtin___sprintf_chk) {
644
195
          IsChkVariant = true;
645
195
          ObjectIndex = 2;
646
134
        } else {
647
134
          IsChkVariant = false;
648
134
          ObjectIndex = 0;
649
134
        }
650
329
        break;
651
329
      }
652
17
    }
653
17
    return;
654
17
  }
655
37
  case Builtin::BI__builtin___memcpy_chk:
656
50
  case Builtin::BI__builtin___memmove_chk:
657
74
  case Builtin::BI__builtin___memset_chk:
658
77
  case Builtin::BI__builtin___strlcat_chk:
659
92
  case Builtin::BI__builtin___strlcpy_chk:
660
112
  case Builtin::BI__builtin___strncat_chk:
661
128
  case Builtin::BI__builtin___strncpy_chk:
662
128
  case Builtin::BI__builtin___stpncpy_chk:
663
135
  case Builtin::BI__builtin___memccpy_chk:
664
159
  case Builtin::BI__builtin___mempcpy_chk: {
665
159
    DiagID = diag::warn_builtin_chk_overflow;
666
159
    IsChkVariant = true;
667
159
    SizeIndex = TheCall->getNumArgs() - 2;
668
159
    ObjectIndex = TheCall->getNumArgs() - 1;
669
159
    break;
670
135
  }
671
672
2
  case Builtin::BI__builtin___snprintf_chk:
673
11
  case Builtin::BI__builtin___vsnprintf_chk: {
674
11
    DiagID = diag::warn_builtin_chk_overflow;
675
11
    IsChkVariant = true;
676
11
    SizeIndex = 1;
677
11
    ObjectIndex = 3;
678
11
    break;
679
2
  }
680
681
94
  case Builtin::BIstrncat:
682
150
  case Builtin::BI__builtin_strncat:
683
191
  case Builtin::BIstrncpy:
684
218
  case Builtin::BI__builtin_strncpy:
685
218
  case Builtin::BIstpncpy:
686
226
  case Builtin::BI__builtin_stpncpy: {
687
    // Whether these functions overflow depends on the runtime strlen of the
688
    // string, not just the buffer size, so emitting the "always overflow"
689
    // diagnostic isn't quite right. We should still diagnose passing a buffer
690
    // size larger than the destination buffer though; this is a runtime abort
691
    // in _FORTIFY_SOURCE mode, and is quite suspicious otherwise.
692
226
    DiagID = diag::warn_fortify_source_size_mismatch;
693
226
    SizeIndex = TheCall->getNumArgs() - 1;
694
226
    ObjectIndex = 0;
695
226
    break;
696
218
  }
697
698
182
  case Builtin::BImemcpy:
699
381
  case Builtin::BI__builtin_memcpy:
700
488
  case Builtin::BImemmove:
701
543
  case Builtin::BI__builtin_memmove:
702
720
  case Builtin::BImemset:
703
809
  case Builtin::BI__builtin_memset:
704
831
  case Builtin::BImempcpy:
705
853
  case Builtin::BI__builtin_mempcpy: {
706
853
    DiagID = diag::warn_fortify_source_overflow;
707
853
    SizeIndex = TheCall->getNumArgs() - 1;
708
853
    ObjectIndex = 0;
709
853
    break;
710
831
  }
711
25
  case Builtin::BIsnprintf:
712
43
  case Builtin::BI__builtin_snprintf:
713
57
  case Builtin::BIvsnprintf:
714
65
  case Builtin::BI__builtin_vsnprintf: {
715
65
    DiagID = diag::warn_fortify_source_size_mismatch;
716
65
    SizeIndex = 1;
717
65
    ObjectIndex = 0;
718
65
    break;
719
1.64k
  }
720
1.64k
  }
721
722
1.64k
  llvm::APSInt ObjectSize;
723
  // For __builtin___*_chk, the object size is explicitly provided by the caller
724
  // (usually using __builtin_object_size). Use that value to check this call.
725
1.64k
  if (IsChkVariant) {
726
365
    Expr::EvalResult Result;
727
365
    Expr *SizeArg = TheCall->getArg(ObjectIndex);
728
365
    if (!SizeArg->EvaluateAsInt(Result, getASTContext()))
729
31
      return;
730
334
    ObjectSize = Result.Val.getInt();
731
732
  // Otherwise, try to evaluate an imaginary call to __builtin_object_size.
733
1.27k
  } else {
734
    // If the parameter has a pass_object_size attribute, then we should use its
735
    // (potentially) more strict checking mode. Otherwise, conservatively assume
736
    // type 0.
737
1.27k
    int BOSType = 0;
738
1.27k
    if (const auto *POS =
739
0
            FD->getParamDecl(ObjectIndex)->getAttr<PassObjectSizeAttr>())
740
0
      BOSType = POS->getType();
741
742
1.27k
    Expr *ObjArg = TheCall->getArg(ObjectIndex);
743
1.27k
    uint64_t Result;
744
1.27k
    if (!ObjArg->tryEvaluateObjectSize(Result, getASTContext(), BOSType))
745
532
      return;
746
    // Get the object size in the target's size_t width.
747
746
    ObjectSize = llvm::APSInt::getUnsigned(Result).extOrTrunc(SizeTypeWidth);
748
746
  }
749
750
  // Evaluate the number of bytes of the object that this call will use.
751
1.08k
  if (!UsedSize) {
752
758
    Expr::EvalResult Result;
753
758
    Expr *UsedSizeArg = TheCall->getArg(SizeIndex);
754
758
    if (!UsedSizeArg->EvaluateAsInt(Result, getASTContext()))
755
107
      return;
756
651
    UsedSize = Result.Val.getInt().extOrTrunc(SizeTypeWidth);
757
651
  }
758
759
973
  if (UsedSize.getValue().ule(ObjectSize))
760
739
    return;
761
762
234
  StringRef FunctionName = getASTContext().BuiltinInfo.getName(BuiltinID);
763
  // Skim off the details of whichever builtin was called to produce a better
764
  // diagnostic, as it's unlikley that the user wrote the __builtin explicitly.
765
234
  if (IsChkVariant) {
766
108
    FunctionName = FunctionName.drop_front(std::strlen("__builtin___"));
767
108
    FunctionName = FunctionName.drop_back(std::strlen("_chk"));
768
126
  } else if (FunctionName.startswith("__builtin_")) {
769
46
    FunctionName = FunctionName.drop_front(std::strlen("__builtin_"));
770
46
  }
771
772
234
  DiagRuntimeBehavior(TheCall->getBeginLoc(), TheCall,
773
234
                      PDiag(DiagID)
774
234
                          << FunctionName << ObjectSize.toString(/*Radix=*/10)
775
234
                          << UsedSize.getValue().toString(/*Radix=*/10));
776
234
}
777
778
static bool SemaBuiltinSEHScopeCheck(Sema &SemaRef, CallExpr *TheCall,
779
                                     Scope::ScopeFlags NeededScopeFlags,
780
39
                                     unsigned DiagID) {
781
  // Scopes aren't available during instantiation. Fortunately, builtin
782
  // functions cannot be template args so they cannot be formed through template
783
  // instantiation. Therefore checking once during the parse is sufficient.
784
39
  if (SemaRef.inTemplateInstantiation())
785
0
    return false;
786
787
39
  Scope *S = SemaRef.getCurScope();
788
74
  while (S && 
!S->isSEHExceptScope()62
)
789
35
    S = S->getParent();
790
39
  if (!S || 
!(S->getFlags() & NeededScopeFlags)27
) {
791
13
    auto *DRE = cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts());
792
13
    SemaRef.Diag(TheCall->getExprLoc(), DiagID)
793
13
        << DRE->getDecl()->getIdentifier();
794
13
    return true;
795
13
  }
796
797
26
  return false;
798
26
}
799
800
292
static inline bool isBlockPointer(Expr *Arg) {
801
292
  return Arg->getType()->isBlockPointerType();
802
292
}
803
804
/// OpenCL C v2.0, s6.13.17.2 - Checks that the block parameters are all local
805
/// void*, which is a requirement of device side enqueue.
806
191
static bool checkOpenCLBlockArgs(Sema &S, Expr *BlockArg) {
807
191
  const BlockPointerType *BPT =
808
191
      cast<BlockPointerType>(BlockArg->getType().getCanonicalType());
809
191
  ArrayRef<QualType> Params =
810
191
      BPT->getPointeeType()->castAs<FunctionProtoType>()->getParamTypes();
811
191
  unsigned ArgCounter = 0;
812
191
  bool IllegalParams = false;
813
  // Iterate through the block parameters until either one is found that is not
814
  // a local void*, or the block is valid.
815
191
  for (ArrayRef<QualType>::iterator I = Params.begin(), E = Params.end();
816
380
       I != E; 
++I, ++ArgCounter189
) {
817
189
    if (!(*I)->isPointerType() || 
!(*I)->getPointeeType()->isVoidType()173
||
818
153
        (*I)->getPointeeType().getQualifiers().getAddressSpace() !=
819
36
            LangAS::opencl_local) {
820
      // Get the location of the error. If a block literal has been passed
821
      // (BlockExpr) then we can point straight to the offending argument,
822
      // else we just point to the variable reference.
823
36
      SourceLocation ErrorLoc;
824
36
      if (isa<BlockExpr>(BlockArg)) {
825
16
        BlockDecl *BD = cast<BlockExpr>(BlockArg)->getBlockDecl();
826
16
        ErrorLoc = BD->getParamDecl(ArgCounter)->getBeginLoc();
827
20
      } else if (isa<DeclRefExpr>(BlockArg)) {
828
20
        ErrorLoc = cast<DeclRefExpr>(BlockArg)->getBeginLoc();
829
20
      }
830
36
      S.Diag(ErrorLoc,
831
36
             diag::err_opencl_enqueue_kernel_blocks_non_local_void_args);
832
36
      IllegalParams = true;
833
36
    }
834
189
  }
835
836
191
  return IllegalParams;
837
191
}
838
839
46
static bool checkOpenCLSubgroupExt(Sema &S, CallExpr *Call) {
840
46
  if (!S.getOpenCLOptions().isEnabled("cl_khr_subgroups")) {
841
8
    S.Diag(Call->getBeginLoc(), diag::err_opencl_requires_extension)
842
8
        << 1 << Call->getDirectCallee() << "cl_khr_subgroups";
843
8
    return true;
844
8
  }
845
38
  return false;
846
38
}
847
848
38
static bool SemaOpenCLBuiltinNDRangeAndBlock(Sema &S, CallExpr *TheCall) {
849
38
  if (checkArgCount(S, TheCall, 2))
850
0
    return true;
851
852
38
  if (checkOpenCLSubgroupExt(S, TheCall))
853
8
    return true;
854
855
  // First argument is an ndrange_t type.
856
30
  Expr *NDRangeArg = TheCall->getArg(0);
857
30
  if (NDRangeArg->getType().getUnqualifiedType().getAsString() != "ndrange_t") {
858
8
    S.Diag(NDRangeArg->getBeginLoc(), diag::err_opencl_builtin_expected_type)
859
8
        << TheCall->getDirectCallee() << "'ndrange_t'";
860
8
    return true;
861
8
  }
862
863
22
  Expr *BlockArg = TheCall->getArg(1);
864
22
  if (!isBlockPointer(BlockArg)) {
865
8
    S.Diag(BlockArg->getBeginLoc(), diag::err_opencl_builtin_expected_type)
866
8
        << TheCall->getDirectCallee() << "block";
867
8
    return true;
868
8
  }
869
14
  return checkOpenCLBlockArgs(S, BlockArg);
870
14
}
871
872
/// OpenCL C v2.0, s6.13.17.6 - Check the argument to the
873
/// get_kernel_work_group_size
874
/// and get_kernel_preferred_work_group_size_multiple builtin functions.
875
97
static bool SemaOpenCLBuiltinKernelWorkGroupSize(Sema &S, CallExpr *TheCall) {
876
97
  if (checkArgCount(S, TheCall, 1))
877
16
    return true;
878
879
81
  Expr *BlockArg = TheCall->getArg(0);
880
81
  if (!isBlockPointer(BlockArg)) {
881
8
    S.Diag(BlockArg->getBeginLoc(), diag::err_opencl_builtin_expected_type)
882
8
        << TheCall->getDirectCallee() << "block";
883
8
    return true;
884
8
  }
885
73
  return checkOpenCLBlockArgs(S, BlockArg);
886
73
}
887
888
/// Diagnose integer type and any valid implicit conversion to it.
889
static bool checkOpenCLEnqueueIntType(Sema &S, Expr *E,
890
                                      const QualType &IntType);
891
892
static bool checkOpenCLEnqueueLocalSizeArgs(Sema &S, CallExpr *TheCall,
893
66
                                            unsigned Start, unsigned End) {
894
66
  bool IllegalParams = false;
895
174
  for (unsigned I = Start; I <= End; 
++I108
)
896
108
    IllegalParams |= checkOpenCLEnqueueIntType(S, TheCall->getArg(I),
897
108
                                              S.Context.getSizeType());
898
66
  return IllegalParams;
899
66
}
900
901
/// OpenCL v2.0, s6.13.17.1 - Check that sizes are provided for all
902
/// 'local void*' parameter of passed block.
903
static bool checkOpenCLEnqueueVariadicArgs(Sema &S, CallExpr *TheCall,
904
                                           Expr *BlockArg,
905
74
                                           unsigned NumNonVarArgs) {
906
74
  const BlockPointerType *BPT =
907
74
      cast<BlockPointerType>(BlockArg->getType().getCanonicalType());
908
74
  unsigned NumBlockParams =
909
74
      BPT->getPointeeType()->castAs<FunctionProtoType>()->getNumParams();
910
74
  unsigned TotalNumArgs = TheCall->getNumArgs();
911
912
  // For each argument passed to the block, a corresponding uint needs to
913
  // be passed to describe the size of the local memory.
914
74
  if (TotalNumArgs != NumBlockParams + NumNonVarArgs) {
915
8
    S.Diag(TheCall->getBeginLoc(),
916
8
           diag::err_opencl_enqueue_kernel_local_size_args);
917
8
    return true;
918
8
  }
919
920
  // Check that the sizes of the local memory are specified by integers.
921
66
  return checkOpenCLEnqueueLocalSizeArgs(S, TheCall, NumNonVarArgs,
922
66
                                         TotalNumArgs - 1);
923
66
}
924
925
/// OpenCL C v2.0, s6.13.17 - Enqueue kernel function contains four different
926
/// overload formats specified in Table 6.13.17.1.
927
/// int enqueue_kernel(queue_t queue,
928
///                    kernel_enqueue_flags_t flags,
929
///                    const ndrange_t ndrange,
930
///                    void (^block)(void))
931
/// int enqueue_kernel(queue_t queue,
932
///                    kernel_enqueue_flags_t flags,
933
///                    const ndrange_t ndrange,
934
///                    uint num_events_in_wait_list,
935
///                    clk_event_t *event_wait_list,
936
///                    clk_event_t *event_ret,
937
///                    void (^block)(void))
938
/// int enqueue_kernel(queue_t queue,
939
///                    kernel_enqueue_flags_t flags,
940
///                    const ndrange_t ndrange,
941
///                    void (^block)(local void*, ...),
942
///                    uint size0, ...)
943
/// int enqueue_kernel(queue_t queue,
944
///                    kernel_enqueue_flags_t flags,
945
///                    const ndrange_t ndrange,
946
///                    uint num_events_in_wait_list,
947
///                    clk_event_t *event_wait_list,
948
///                    clk_event_t *event_ret,
949
///                    void (^block)(local void*, ...),
950
///                    uint size0, ...)
951
157
static bool SemaOpenCLBuiltinEnqueueKernel(Sema &S, CallExpr *TheCall) {
952
157
  unsigned NumArgs = TheCall->getNumArgs();
953
954
157
  if (NumArgs < 4) {
955
4
    S.Diag(TheCall->getBeginLoc(),
956
4
           diag::err_typecheck_call_too_few_args_at_least)
957
4
        << 0 << 4 << NumArgs;
958
4
    return true;
959
4
  }
960
961
153
  Expr *Arg0 = TheCall->getArg(0);
962
153
  Expr *Arg1 = TheCall->getArg(1);
963
153
  Expr *Arg2 = TheCall->getArg(2);
964
153
  Expr *Arg3 = TheCall->getArg(3);
965
966
  // First argument always needs to be a queue_t type.
967
153
  if (!Arg0->getType()->isQueueT()) {
968
4
    S.Diag(TheCall->getArg(0)->getBeginLoc(),
969
4
           diag::err_opencl_builtin_expected_type)
970
4
        << TheCall->getDirectCallee() << S.Context.OCLQueueTy;
971
4
    return true;
972
4
  }
973
974
  // Second argument always needs to be a kernel_enqueue_flags_t enum value.
975
149
  if (!Arg1->getType()->isIntegerType()) {
976
4
    S.Diag(TheCall->getArg(1)->getBeginLoc(),
977
4
           diag::err_opencl_builtin_expected_type)
978
4
        << TheCall->getDirectCallee() << "'kernel_enqueue_flags_t' (i.e. uint)";
979
4
    return true;
980
4
  }
981
982
  // Third argument is always an ndrange_t type.
983
145
  if (Arg2->getType().getUnqualifiedType().getAsString() != "ndrange_t") {
984
4
    S.Diag(TheCall->getArg(2)->getBeginLoc(),
985
4
           diag::err_opencl_builtin_expected_type)
986
4
        << TheCall->getDirectCallee() << "'ndrange_t'";
987
4
    return true;
988
4
  }
989
990
  // With four arguments, there is only one form that the function could be
991
  // called in: no events and no variable arguments.
992
141
  if (NumArgs == 4) {
993
    // check that the last argument is the right block type.
994
25
    if (!isBlockPointer(Arg3)) {
995
4
      S.Diag(Arg3->getBeginLoc(), diag::err_opencl_builtin_expected_type)
996
4
          << TheCall->getDirectCallee() << "block";
997
4
      return true;
998
4
    }
999
    // we have a block type, check the prototype
1000
21
    const BlockPointerType *BPT =
1001
21
        cast<BlockPointerType>(Arg3->getType().getCanonicalType());
1002
21
    if (BPT->getPointeeType()->castAs<FunctionProtoType>()->getNumParams() > 0) {
1003
4
      S.Diag(Arg3->getBeginLoc(),
1004
4
             diag::err_opencl_enqueue_kernel_blocks_no_args);
1005
4
      return true;
1006
4
    }
1007
17
    return false;
1008
17
  }
1009
  // we can have block + varargs.
1010
116
  if (isBlockPointer(Arg3))
1011
60
    return (checkOpenCLBlockArgs(S, Arg3) ||
1012
56
            checkOpenCLEnqueueVariadicArgs(S, TheCall, Arg3, 4));
1013
  // last two cases with either exactly 7 args or 7 args and varargs.
1014
56
  if (NumArgs >= 7) {
1015
    // check common block argument.
1016
48
    Expr *Arg6 = TheCall->getArg(6);
1017
48
    if (!isBlockPointer(Arg6)) {
1018
4
      S.Diag(Arg6->getBeginLoc(), diag::err_opencl_builtin_expected_type)
1019
4
          << TheCall->getDirectCallee() << "block";
1020
4
      return true;
1021
4
    }
1022
44
    if (checkOpenCLBlockArgs(S, Arg6))
1023
0
      return true;
1024
1025
    // Forth argument has to be any integer type.
1026
44
    if (!Arg3->getType()->isIntegerType()) {
1027
4
      S.Diag(TheCall->getArg(3)->getBeginLoc(),
1028
4
             diag::err_opencl_builtin_expected_type)
1029
4
          << TheCall->getDirectCallee() << "integer";
1030
4
      return true;
1031
4
    }
1032
    // check remaining common arguments.
1033
40
    Expr *Arg4 = TheCall->getArg(4);
1034
40
    Expr *Arg5 = TheCall->getArg(5);
1035
1036
    // Fifth argument is always passed as a pointer to clk_event_t.
1037
40
    if (!Arg4->isNullPointerConstant(S.Context,
1038
40
                                     Expr::NPC_ValueDependentIsNotNull) &&
1039
29
        !Arg4->getType()->getPointeeOrArrayElementType()->isClkEventT()) {
1040
4
      S.Diag(TheCall->getArg(4)->getBeginLoc(),
1041
4
             diag::err_opencl_builtin_expected_type)
1042
4
          << TheCall->getDirectCallee()
1043
4
          << S.Context.getPointerType(S.Context.OCLClkEventTy);
1044
4
      return true;
1045
4
    }
1046
1047
    // Sixth argument is always passed as a pointer to clk_event_t.
1048
36
    if (!Arg5->isNullPointerConstant(S.Context,
1049
36
                                     Expr::NPC_ValueDependentIsNotNull) &&
1050
25
        !(Arg5->getType()->isPointerType() &&
1051
25
          Arg5->getType()->getPointeeType()->isClkEventT())) {
1052
4
      S.Diag(TheCall->getArg(5)->getBeginLoc(),
1053
4
             diag::err_opencl_builtin_expected_type)
1054
4
          << TheCall->getDirectCallee()
1055
4
          << S.Context.getPointerType(S.Context.OCLClkEventTy);
1056
4
      return true;
1057
4
    }
1058
1059
32
    if (NumArgs == 7)
1060
14
      return false;
1061
1062
18
    return checkOpenCLEnqueueVariadicArgs(S, TheCall, Arg6, 7);
1063
18
  }
1064
1065
  // None of the specific case has been detected, give generic error
1066
8
  S.Diag(TheCall->getBeginLoc(),
1067
8
         diag::err_opencl_enqueue_kernel_incorrect_args);
1068
8
  return true;
1069
8
}
1070
1071
/// Returns OpenCL access qual.
1072
50
static OpenCLAccessAttr *getOpenCLArgAccess(const Decl *D) {
1073
50
    return D->getAttr<OpenCLAccessAttr>();
1074
50
}
1075
1076
/// Returns true if pipe element type is different from the pointer.
1077
56
static bool checkOpenCLPipeArg(Sema &S, CallExpr *Call) {
1078
56
  const Expr *Arg0 = Call->getArg(0);
1079
  // First argument type should always be pipe.
1080
56
  if (!Arg0->getType()->isPipeType()) {
1081
6
    S.Diag(Call->getBeginLoc(), diag::err_opencl_builtin_pipe_first_arg)
1082
6
        << Call->getDirectCallee() << Arg0->getSourceRange();
1083
6
    return true;
1084
6
  }
1085
50
  OpenCLAccessAttr *AccessQual =
1086
50
      getOpenCLArgAccess(cast<DeclRefExpr>(Arg0)->getDecl());
1087
  // Validates the access qualifier is compatible with the call.
1088
  // OpenCL v2.0 s6.13.16 - The access qualifiers for pipe should only be
1089
  // read_only and write_only, and assumed to be read_only if no qualifier is
1090
  // specified.
1091
50
  switch (Call->getDirectCallee()->getBuiltinID()) {
1092
13
  case Builtin::BIread_pipe:
1093
18
  case Builtin::BIreserve_read_pipe:
1094
20
  case Builtin::BIcommit_read_pipe:
1095
21
  case Builtin::BIwork_group_reserve_read_pipe:
1096
23
  case Builtin::BIsub_group_reserve_read_pipe:
1097
25
  case Builtin::BIwork_group_commit_read_pipe:
1098
27
  case Builtin::BIsub_group_commit_read_pipe:
1099
27
    if (!(!AccessQual || AccessQual->isReadOnly())) {
1100
4
      S.Diag(Arg0->getBeginLoc(),
1101
4
             diag::err_opencl_builtin_pipe_invalid_access_modifier)
1102
4
          << "read_only" << Arg0->getSourceRange();
1103
4
      return true;
1104
4
    }
1105
23
    break;
1106
11
  case Builtin::BIwrite_pipe:
1107
14
  case Builtin::BIreserve_write_pipe:
1108
16
  case Builtin::BIcommit_write_pipe:
1109
17
  case Builtin::BIwork_group_reserve_write_pipe:
1110
19
  case Builtin::BIsub_group_reserve_write_pipe:
1111
21
  case Builtin::BIwork_group_commit_write_pipe:
1112
23
  case Builtin::BIsub_group_commit_write_pipe:
1113
23
    if (!(AccessQual && AccessQual->isWriteOnly())) {
1114
4
      S.Diag(Arg0->getBeginLoc(),
1115
4
             diag::err_opencl_builtin_pipe_invalid_access_modifier)
1116
4
          << "write_only" << Arg0->getSourceRange();
1117
4
      return true;
1118
4
    }
1119
19
    break;
1120
0
  default:
1121
0
    break;
1122
42
  }
1123
42
  return false;
1124
42
}
1125
1126
/// Returns true if pipe element type is different from the pointer.
1127
16
static bool checkOpenCLPipePacketType(Sema &S, CallExpr *Call, unsigned Idx) {
1128
16
  const Expr *Arg0 = Call->getArg(0);
1129
16
  const Expr *ArgIdx = Call->getArg(Idx);
1130
16
  const PipeType *PipeTy = cast<PipeType>(Arg0->getType());
1131
16
  const QualType EltTy = PipeTy->getElementType();
1132
16
  const PointerType *ArgTy = ArgIdx->getType()->getAs<PointerType>();
1133
  // The Idx argument should be a pointer and the type of the pointer and
1134
  // the type of pipe element should also be the same.
1135
16
  if (!ArgTy ||
1136
14
      !S.Context.hasSameType(
1137
2
          EltTy, ArgTy->getPointeeType()->getCanonicalTypeInternal())) {
1138
2
    S.Diag(Call->getBeginLoc(), diag::err_opencl_builtin_pipe_invalid_arg)
1139
2
        << Call->getDirectCallee() << S.Context.getPointerType(EltTy)
1140
2
        << ArgIdx->getType() << ArgIdx->getSourceRange();
1141
2
    return true;
1142
2
  }
1143
14
  return false;
1144
14
}
1145
1146
// Performs semantic analysis for the read/write_pipe call.
1147
// \param S Reference to the semantic analyzer.
1148
// \param Call A pointer to the builtin call.
1149
// \return True if a semantic error has been found, false otherwise.
1150
28
static bool SemaBuiltinRWPipe(Sema &S, CallExpr *Call) {
1151
  // OpenCL v2.0 s6.13.16.2 - The built-in read/write
1152
  // functions have two forms.
1153
28
  switch (Call->getNumArgs()) {
1154
16
  case 2:
1155
16
    if (checkOpenCLPipeArg(S, Call))
1156
4
      return true;
1157
    // The call with 2 arguments should be
1158
    // read/write_pipe(pipe T, T*).
1159
    // Check packet type T.
1160
12
    if (checkOpenCLPipePacketType(S, Call, 1))
1161
2
      return true;
1162
10
    break;
1163
1164
10
  case 4: {
1165
10
    if (checkOpenCLPipeArg(S, Call))
1166
2
      return true;
1167
    // The call with 4 arguments should be
1168
    // read/write_pipe(pipe T, reserve_id_t, uint, T*).
1169
    // Check reserve_id_t.
1170
8
    if (!Call->getArg(1)->getType()->isReserveIDT()) {
1171
2
      S.Diag(Call->getBeginLoc(), diag::err_opencl_builtin_pipe_invalid_arg)
1172
2
          << Call->getDirectCallee() << S.Context.OCLReserveIDTy
1173
2
          << Call->getArg(1)->getType() << Call->getArg(1)->getSourceRange();
1174
2
      return true;
1175
2
    }
1176
1177
    // Check the index.
1178
6
    const Expr *Arg2 = Call->getArg(2);
1179
6
    if (!Arg2->getType()->isIntegerType() &&
1180
2
        !Arg2->getType()->isUnsignedIntegerType()) {
1181
2
      S.Diag(Call->getBeginLoc(), diag::err_opencl_builtin_pipe_invalid_arg)
1182
2
          << Call->getDirectCallee() << S.Context.UnsignedIntTy
1183
2
          << Arg2->getType() << Arg2->getSourceRange();
1184
2
      return true;
1185
2
    }
1186
1187
    // Check packet type T.
1188
4
    if (checkOpenCLPipePacketType(S, Call, 3))
1189
0
      return true;
1190
4
  } break;
1191
2
  default:
1192
2
    S.Diag(Call->getBeginLoc(), diag::err_opencl_builtin_pipe_arg_num)
1193
2
        << Call->getDirectCallee() << Call->getSourceRange();
1194
2
    return true;
1195
14
  }
1196
1197
14
  return false;
1198
14
}
1199
1200
// Performs a semantic analysis on the {work_group_/sub_group_
1201
//        /_}reserve_{read/write}_pipe
1202
// \param S Reference to the semantic analyzer.
1203
// \param Call The call to the builtin function to be analyzed.
1204
// \return True if a semantic error was found, false otherwise.
1205
16
static bool SemaBuiltinReserveRWPipe(Sema &S, CallExpr *Call) {
1206
16
  if (checkArgCount(S, Call, 2))
1207
0
    return true;
1208
1209
16
  if (checkOpenCLPipeArg(S, Call))
1210
4
    return true;
1211
1212
  // Check the reserve size.
1213
12
  if (!Call->getArg(1)->getType()->isIntegerType() &&
1214
2
      !Call->getArg(1)->getType()->isUnsignedIntegerType()) {
1215
2
    S.Diag(Call->getBeginLoc(), diag::err_opencl_builtin_pipe_invalid_arg)
1216
2
        << Call->getDirectCallee() << S.Context.UnsignedIntTy
1217
2
        << Call->getArg(1)->getType() << Call->getArg(1)->getSourceRange();
1218
2
    return true;
1219
2
  }
1220
1221
  // Since return type of reserve_read/write_pipe built-in function is
1222
  // reserve_id_t, which is not defined in the builtin def file , we used int
1223
  // as return type and need to override the return type of these functions.
1224
10
  Call->setType(S.Context.OCLReserveIDTy);
1225
1226
10
  return false;
1227
10
}
1228
1229
// Performs a semantic analysis on {work_group_/sub_group_
1230
//        /_}commit_{read/write}_pipe
1231
// \param S Reference to the semantic analyzer.
1232
// \param Call The call to the builtin function to be analyzed.
1233
// \return True if a semantic error was found, false otherwise.
1234
14
static bool SemaBuiltinCommitRWPipe(Sema &S, CallExpr *Call) {
1235
14
  if (checkArgCount(S, Call, 2))
1236
0
    return true;
1237
1238
14
  if (checkOpenCLPipeArg(S, Call))
1239
4
    return true;
1240
1241
  // Check reserve_id_t.
1242
10
  if (!Call->getArg(1)->getType()->isReserveIDT()) {
1243
2
    S.Diag(Call->getBeginLoc(), diag::err_opencl_builtin_pipe_invalid_arg)
1244
2
        << Call->getDirectCallee() << S.Context.OCLReserveIDTy
1245
2
        << Call->getArg(1)->getType() << Call->getArg(1)->getSourceRange();
1246
2
    return true;
1247
2
  }
1248
1249
8
  return false;
1250
8
}
1251
1252
// Performs a semantic analysis on the call to built-in Pipe
1253
//        Query Functions.
1254
// \param S Reference to the semantic analyzer.
1255
// \param Call The call to the builtin function to be analyzed.
1256
// \return True if a semantic error was found, false otherwise.
1257
10
static bool SemaBuiltinPipePackets(Sema &S, CallExpr *Call) {
1258
10
  if (checkArgCount(S, Call, 1))
1259
0
    return true;
1260
1261
10
  if (!Call->getArg(0)->getType()->isPipeType()) {
1262
2
    S.Diag(Call->getBeginLoc(), diag::err_opencl_builtin_pipe_first_arg)
1263
2
        << Call->getDirectCallee() << Call->getArg(0)->getSourceRange();
1264
2
    return true;
1265
2
  }
1266
1267
8
  return false;
1268
8
}
1269
1270
// OpenCL v2.0 s6.13.9 - Address space qualifier functions.
1271
// Performs semantic analysis for the to_global/local/private call.
1272
// \param S Reference to the semantic analyzer.
1273
// \param BuiltinID ID of the builtin function.
1274
// \param Call A pointer to the builtin call.
1275
// \return True if a semantic error has been found, false otherwise.
1276
static bool SemaOpenCLBuiltinToAddr(Sema &S, unsigned BuiltinID,
1277
32
                                    CallExpr *Call) {
1278
32
  if (checkArgCount(S, Call, 1))
1279
1
    return true;
1280
1281
31
  auto RT = Call->getArg(0)->getType();
1282
31
  if (!RT->isPointerType() || RT->getPointeeType()
1283
3
      .getAddressSpace() == LangAS::opencl_constant) {
1284
3
    S.Diag(Call->getBeginLoc(), diag::err_opencl_builtin_to_addr_invalid_arg)
1285
3
        << Call->getArg(0) << Call->getDirectCallee() << Call->getSourceRange();
1286
3
    return true;
1287
3
  }
1288
1289
28
  if (RT->getPointeeType().getAddressSpace() != LangAS::opencl_generic) {
1290
21
    S.Diag(Call->getArg(0)->getBeginLoc(),
1291
21
           diag::warn_opencl_generic_address_space_arg)
1292
21
        << Call->getDirectCallee()->getNameInfo().getAsString()
1293
21
        << Call->getArg(0)->getSourceRange();
1294
21
  }
1295
1296
28
  RT = RT->getPointeeType();
1297
28
  auto Qual = RT.getQualifiers();
1298
28
  switch (BuiltinID) {
1299
12
  case Builtin::BIto_global:
1300
12
    Qual.setAddressSpace(LangAS::opencl_global);
1301
12
    break;
1302
8
  case Builtin::BIto_local:
1303
8
    Qual.setAddressSpace(LangAS::opencl_local);
1304
8
    break;
1305
8
  case Builtin::BIto_private:
1306
8
    Qual.setAddressSpace(LangAS::opencl_private);
1307
8
    break;
1308
0
  default:
1309
0
    llvm_unreachable("Invalid builtin function");
1310
28
  }
1311
28
  Call->setType(S.Context.getPointerType(S.Context.getQualifiedType(
1312
28
      RT.getUnqualifiedType(), Qual)));
1313
1314
28
  return false;
1315
28
}
1316
1317
120
static ExprResult SemaBuiltinLaunder(Sema &S, CallExpr *TheCall) {
1318
120
  if (checkArgCount(S, TheCall, 1))
1319
2
    return ExprError();
1320
1321
  // Compute __builtin_launder's parameter type from the argument.
1322
  // The parameter type is:
1323
  //  * The type of the argument if it's not an array or function type,
1324
  //  Otherwise,
1325
  //  * The decayed argument type.
1326
118
  QualType ParamTy = [&]() {
1327
118
    QualType ArgTy = TheCall->getArg(0)->getType();
1328
118
    if (const ArrayType *Ty = ArgTy->getAsArrayTypeUnsafe())
1329
14
      return S.Context.getPointerType(Ty->getElementType());
1330
104
    if (ArgTy->isFunctionType()) {
1331
2
      return S.Context.getPointerType(ArgTy);
1332
2
    }
1333
102
    return ArgTy;
1334
102
  }();
1335
1336
118
  TheCall->setType(ParamTy);
1337
1338
118
  auto DiagSelect = [&]() -> llvm::Optional<unsigned> {
1339
118
    if (!ParamTy->isPointerType())
1340
11
      return 0;
1341
107
    if (ParamTy->isFunctionPointerType())
1342
5
      return 1;
1343
102
    if (ParamTy->isVoidPointerType())
1344
6
      return 2;
1345
96
    return llvm::Optional<unsigned>{};
1346
96
  }();
1347
118
  if (DiagSelect.hasValue()) {
1348
22
    S.Diag(TheCall->getBeginLoc(), diag::err_builtin_launder_invalid_arg)
1349
22
        << DiagSelect.getValue() << TheCall->getSourceRange();
1350
22
    return ExprError();
1351
22
  }
1352
1353
  // We either have an incomplete class type, or we have a class template
1354
  // whose instantiation has not been forced. Example:
1355
  //
1356
  //   template <class T> struct Foo { T value; };
1357
  //   Foo<int> *p = nullptr;
1358
  //   auto *d = __builtin_launder(p);
1359
96
  if (S.RequireCompleteType(TheCall->getBeginLoc(), ParamTy->getPointeeType(),
1360
96
                            diag::err_incomplete_type))
1361
2
    return ExprError();
1362
1363
94
  assert(ParamTy->getPointeeType()->isObjectType() &&
1364
94
         "Unhandled non-object pointer case");
1365
1366
94
  InitializedEntity Entity =
1367
94
      InitializedEntity::InitializeParameter(S.Context, ParamTy, false);
1368
94
  ExprResult Arg =
1369
94
      S.PerformCopyInitialization(Entity, SourceLocation(), TheCall->getArg(0));
1370
94
  if (Arg.isInvalid())
1371
0
    return ExprError();
1372
94
  TheCall->setArg(0, Arg.get());
1373
1374
94
  return TheCall;
1375
94
}
1376
1377
// Emit an error and return true if the current architecture is not in the list
1378
// of supported architectures.
1379
static bool
1380
CheckBuiltinTargetSupport(Sema &S, unsigned BuiltinID, CallExpr *TheCall,
1381
90
                          ArrayRef<llvm::Triple::ArchType> SupportedArchs) {
1382
90
  llvm::Triple::ArchType CurArch =
1383
90
      S.getASTContext().getTargetInfo().getTriple().getArch();
1384
90
  if (llvm::is_contained(SupportedArchs, CurArch))
1385
72
    return false;
1386
18
  S.Diag(TheCall->getBeginLoc(), diag::err_builtin_target_unsupported)
1387
18
      << TheCall->getSourceRange();
1388
18
  return true;
1389
18
}
1390
1391
static void CheckNonNullArgument(Sema &S, const Expr *ArgExpr,
1392
                                 SourceLocation CallSiteLoc);
1393
1394
bool Sema::CheckTSBuiltinFunctionCall(const TargetInfo &TI, unsigned BuiltinID,
1395
1.38M
                                      CallExpr *TheCall) {
1396
1.38M
  switch (TI.getTriple().getArch()) {
1397
5.89k
  default:
1398
    // Some builtins don't require additional checking, so just consider these
1399
    // acceptable.
1400
5.89k
    return false;
1401
18.4k
  case llvm::Triple::arm:
1402
19.0k
  case llvm::Triple::armeb:
1403
36.3k
  case llvm::Triple::thumb:
1404
36.6k
  case llvm::Triple::thumbeb:
1405
36.6k
    return CheckARMBuiltinFunctionCall(TI, BuiltinID, TheCall);
1406
317k
  case llvm::Triple::aarch64:
1407
318k
  case llvm::Triple::aarch64_32:
1408
332k
  case llvm::Triple::aarch64_be:
1409
332k
    return CheckAArch64BuiltinFunctionCall(TI, BuiltinID, TheCall);
1410
0
  case llvm::Triple::bpfeb:
1411
49
  case llvm::Triple::bpfel:
1412
49
    return CheckBPFBuiltinFunctionCall(BuiltinID, TheCall);
1413
1.80k
  case llvm::Triple::hexagon:
1414
1.80k
    return CheckHexagonBuiltinFunctionCall(BuiltinID, TheCall);
1415
1.04k
  case llvm::Triple::mips:
1416
1.04k
  case llvm::Triple::mipsel:
1417
1.05k
  case llvm::Triple::mips64:
1418
1.05k
  case llvm::Triple::mips64el:
1419
1.05k
    return CheckMipsBuiltinFunctionCall(TI, BuiltinID, TheCall);
1420
16.2k
  case llvm::Triple::systemz:
1421
16.2k
    return CheckSystemZBuiltinFunctionCall(BuiltinID, TheCall);
1422
162k
  case llvm::Triple::x86:
1423
907k
  case llvm::Triple::x86_64:
1424
907k
    return CheckX86BuiltinFunctionCall(TI, BuiltinID, TheCall);
1425
6.36k
  case llvm::Triple::ppc:
1426
7.44k
  case llvm::Triple::ppcle:
1427
49.7k
  case llvm::Triple::ppc64:
1428
82.7k
  case llvm::Triple::ppc64le:
1429
82.7k
    return CheckPPCBuiltinFunctionCall(TI, BuiltinID, TheCall);
1430
443
  case llvm::Triple::amdgcn:
1431
443
    return CheckAMDGCNBuiltinFunctionCall(BuiltinID, TheCall);
1432
1.38M
  }
1433
1.38M
}
1434
1435
ExprResult
1436
Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, unsigned BuiltinID,
1437
1.60M
                               CallExpr *TheCall) {
1438
1.60M
  ExprResult TheCallResult(TheCall);
1439
1440
  // Find out if any arguments are required to be integer constant expressions.
1441
1.60M
  unsigned ICEArguments = 0;
1442
1.60M
  ASTContext::GetBuiltinTypeError Error;
1443
1.60M
  Context.GetBuiltinType(BuiltinID, Error, &ICEArguments);
1444
1.60M
  if (Error != ASTContext::GE_None)
1445
4.90k
    ICEArguments = 0;  // Don't diagnose previously diagnosed errors.
1446
1447
  // If any arguments are required to be ICE's, check and diagnose.
1448
1.95M
  for (unsigned ArgNo = 0; ICEArguments != 0; 
++ArgNo348k
) {
1449
    // Skip arguments not required to be ICE's.
1450
349k
    if ((ICEArguments & (1 << ArgNo)) == 0) 
continue249k
;
1451
1452
100k
    llvm::APSInt Result;
1453
100k
    if (SemaBuiltinConstantArg(TheCall, ArgNo, Result))
1454
602
      return true;
1455
99.4k
    ICEArguments &= ~(1 << ArgNo);
1456
99.4k
  }
1457
1458
1.60M
  switch (BuiltinID) {
1459
420
  case Builtin::BI__builtin___CFStringMakeConstantString:
1460
420
    assert(TheCall->getNumArgs() == 1 &&
1461
420
           "Wrong # arguments to builtin CFStringMakeConstantString");
1462
420
    if (CheckObjCString(TheCall->getArg(0)))
1463
1
      return ExprError();
1464
419
    break;
1465
24
  case Builtin::BI__builtin_ms_va_start:
1466
33
  case Builtin::BI__builtin_stdarg_start:
1467
576
  case Builtin::BI__builtin_va_start:
1468
576
    if (SemaBuiltinVAStart(BuiltinID, TheCall))
1469
33
      return ExprError();
1470
543
    break;
1471
12
  case Builtin::BI__va_start: {
1472
12
    switch (Context.getTargetInfo().getTriple().getArch()) {
1473
2
    case llvm::Triple::aarch64:
1474
2
    case llvm::Triple::arm:
1475
10
    case llvm::Triple::thumb:
1476
10
      if (SemaBuiltinVAStartARMMicrosoft(TheCall))
1477
3
        return ExprError();
1478
7
      break;
1479
2
    default:
1480
2
      if (SemaBuiltinVAStart(BuiltinID, TheCall))
1481
0
        return ExprError();
1482
2
      break;
1483
9
    }
1484
9
    break;
1485
9
  }
1486
1487
  // The acquire, release, and no fence variants are ARM and AArch64 only.
1488
7
  case Builtin::BI_interlockedbittestandset_acq:
1489
14
  case Builtin::BI_interlockedbittestandset_rel:
1490
21
  case Builtin::BI_interlockedbittestandset_nf:
1491
28
  case Builtin::BI_interlockedbittestandreset_acq:
1492
35
  case Builtin::BI_interlockedbittestandreset_rel:
1493
42
  case Builtin::BI_interlockedbittestandreset_nf:
1494
42
    if (CheckBuiltinTargetSupport(
1495
42
            *this, BuiltinID, TheCall,
1496
42
            {llvm::Triple::arm, llvm::Triple::thumb, llvm::Triple::aarch64}))
1497
12
      return ExprError();
1498
30
    break;
1499
1500
  // The 64-bit bittest variants are x64, ARM, and AArch64 only.
1501
8
  case Builtin::BI_bittest64:
1502
16
  case Builtin::BI_bittestandcomplement64:
1503
24
  case Builtin::BI_bittestandreset64:
1504
32
  case Builtin::BI_bittestandset64:
1505
40
  case Builtin::BI_interlockedbittestandreset64:
1506
48
  case Builtin::BI_interlockedbittestandset64:
1507
48
    if (CheckBuiltinTargetSupport(*this, BuiltinID, TheCall,
1508
48
                                  {llvm::Triple::x86_64, llvm::Triple::arm,
1509
48
                                   llvm::Triple::thumb, llvm::Triple::aarch64}))
1510
6
      return ExprError();
1511
42
    break;
1512
1513
67
  case Builtin::BI__builtin_isgreater:
1514
129
  case Builtin::BI__builtin_isgreaterequal:
1515
195
  case Builtin::BI__builtin_isless:
1516
260
  case Builtin::BI__builtin_islessequal:
1517
322
  case Builtin::BI__builtin_islessgreater:
1518
409
  case Builtin::BI__builtin_isunordered:
1519
409
    if (SemaBuiltinUnorderedCompare(TheCall))
1520
4
      return ExprError();
1521
405
    break;
1522
62
  case Builtin::BI__builtin_fpclassify:
1523
62
    if (SemaBuiltinFPClassification(TheCall, 6))
1524
5
      return ExprError();
1525
57
    break;
1526
35
  case Builtin::BI__builtin_isfinite:
1527
65
  case Builtin::BI__builtin_isinf:
1528
110
  case Builtin::BI__builtin_isinf_sign:
1529
315
  case Builtin::BI__builtin_isnan:
1530
370
  case Builtin::BI__builtin_isnormal:
1531
384
  case Builtin::BI__builtin_signbit:
1532
394
  case Builtin::BI__builtin_signbitf:
1533
406
  case Builtin::BI__builtin_signbitl:
1534
406
    if (SemaBuiltinFPClassification(TheCall, 1))
1535
7
      return ExprError();
1536
399
    break;
1537
143k
  case Builtin::BI__builtin_shufflevector:
1538
143k
    return SemaBuiltinShuffleVector(TheCall);
1539
    // TheCall will be freed by the smart pointer here, but that's fine, since
1540
    // SemaBuiltinShuffleVector guts it, but then doesn't release it.
1541
268
  case Builtin::BI__builtin_prefetch:
1542
268
    if (SemaBuiltinPrefetch(TheCall))
1543
6
      return ExprError();
1544
262
    break;
1545
12
  case Builtin::BI__builtin_alloca_with_align:
1546
12
    if (SemaBuiltinAllocaWithAlign(TheCall))
1547
3
      return ExprError();
1548
9
    LLVM_FALLTHROUGH;
1549
60
  case Builtin::BI__builtin_alloca:
1550
60
    Diag(TheCall->getBeginLoc(), diag::warn_alloca)
1551
60
        << TheCall->getDirectCallee();
1552
60
    break;
1553
2
  case Builtin::BI__assume:
1554
738
  case Builtin::BI__builtin_assume:
1555
738
    if (SemaBuiltinAssume(TheCall))
1556
0
      return ExprError();
1557
738
    break;
1558
55
  case Builtin::BI__builtin_assume_aligned:
1559
55
    if (SemaBuiltinAssumeAligned(TheCall))
1560
6
      return ExprError();
1561
49
    break;
1562
295
  case Builtin::BI__builtin_dynamic_object_size:
1563
769
  case Builtin::BI__builtin_object_size:
1564
769
    if (SemaBuiltinConstantArgRange(TheCall, 1, 0, 3))
1565
9
      return ExprError();
1566
760
    break;
1567
12
  case Builtin::BI__builtin_longjmp:
1568
12
    if (SemaBuiltinLongjmp(TheCall))
1569
3
      return ExprError();
1570
9
    break;
1571
10
  case Builtin::BI__builtin_setjmp:
1572
10
    if (SemaBuiltinSetjmp(TheCall))
1573
3
      return ExprError();
1574
7
    break;
1575
88
  case Builtin::BI__builtin_classify_type:
1576
88
    if (checkArgCount(*this, TheCall, 1)) 
return true0
;
1577
88
    TheCall->setType(Context.IntTy);
1578
88
    break;
1579
42
  case Builtin::BI__builtin_complex:
1580
42
    if (SemaBuiltinComplex(TheCall))
1581
22
      return ExprError();
1582
20
    break;
1583
4.71k
  case Builtin::BI__builtin_constant_p: {
1584
4.71k
    if (checkArgCount(*this, TheCall, 1)) 
return true2
;
1585
4.71k
    ExprResult Arg = DefaultFunctionArrayLvalueConversion(TheCall->getArg(0));
1586
4.71k
    if (Arg.isInvalid()) 
return true0
;
1587
4.71k
    TheCall->setArg(0, Arg.get());
1588
4.71k
    TheCall->setType(Context.IntTy);
1589
4.71k
    break;
1590
4.71k
  }
1591
120
  case Builtin::BI__builtin_launder:
1592
120
    return SemaBuiltinLaunder(*this, TheCall);
1593
35
  case Builtin::BI__sync_fetch_and_add:
1594
35
  case Builtin::BI__sync_fetch_and_add_1:
1595
35
  case Builtin::BI__sync_fetch_and_add_2:
1596
40
  case Builtin::BI__sync_fetch_and_add_4:
1597
40
  case Builtin::BI__sync_fetch_and_add_8:
1598
40
  case Builtin::BI__sync_fetch_and_add_16:
1599
59
  case Builtin::BI__sync_fetch_and_sub:
1600
59
  case Builtin::BI__sync_fetch_and_sub_1:
1601
59
  case Builtin::BI__sync_fetch_and_sub_2:
1602
59
  case Builtin::BI__sync_fetch_and_sub_4:
1603
59
  case Builtin::BI__sync_fetch_and_sub_8:
1604
59
  case Builtin::BI__sync_fetch_and_sub_16:
1605
77
  case Builtin::BI__sync_fetch_and_or:
1606
77
  case Builtin::BI__sync_fetch_and_or_1:
1607
77
  case Builtin::BI__sync_fetch_and_or_2:
1608
77
  case Builtin::BI__sync_fetch_and_or_4:
1609
77
  case Builtin::BI__sync_fetch_and_or_8:
1610
77
  case Builtin::BI__sync_fetch_and_or_16:
1611
95
  case Builtin::BI__sync_fetch_and_and:
1612
95
  case Builtin::BI__sync_fetch_and_and_1:
1613
95
  case Builtin::BI__sync_fetch_and_and_2:
1614
95
  case Builtin::BI__sync_fetch_and_and_4:
1615
95
  case Builtin::BI__sync_fetch_and_and_8:
1616
95
  case Builtin::BI__sync_fetch_and_and_16:
1617
115
  case Builtin::BI__sync_fetch_and_xor:
1618
115
  case Builtin::BI__sync_fetch_and_xor_1:
1619
115
  case Builtin::BI__sync_fetch_and_xor_2:
1620
115
  case Builtin::BI__sync_fetch_and_xor_4:
1621
115
  case Builtin::BI__sync_fetch_and_xor_8:
1622
115
  case Builtin::BI__sync_fetch_and_xor_16:
1623
134
  case Builtin::BI__sync_fetch_and_nand:
1624
134
  case Builtin::BI__sync_fetch_and_nand_1:
1625
134
  case Builtin::BI__sync_fetch_and_nand_2:
1626
134
  case Builtin::BI__sync_fetch_and_nand_4:
1627
134
  case Builtin::BI__sync_fetch_and_nand_8:
1628
134
  case Builtin::BI__sync_fetch_and_nand_16:
1629
146
  case Builtin::BI__sync_add_and_fetch:
1630
146
  case Builtin::BI__sync_add_and_fetch_1:
1631
146
  case Builtin::BI__sync_add_and_fetch_2:
1632
146
  case Builtin::BI__sync_add_and_fetch_4:
1633
146
  case Builtin::BI__sync_add_and_fetch_8:
1634
146
  case Builtin::BI__sync_add_and_fetch_16:
1635
156
  case Builtin::BI__sync_sub_and_fetch:
1636
156
  case Builtin::BI__sync_sub_and_fetch_1:
1637
156
  case Builtin::BI__sync_sub_and_fetch_2:
1638
156
  case Builtin::BI__sync_sub_and_fetch_4:
1639
156
  case Builtin::BI__sync_sub_and_fetch_8:
1640
156
  case Builtin::BI__sync_sub_and_fetch_16:
1641
166
  case Builtin::BI__sync_and_and_fetch:
1642
166
  case Builtin::BI__sync_and_and_fetch_1:
1643
166
  case Builtin::BI__sync_and_and_fetch_2:
1644
166
  case Builtin::BI__sync_and_and_fetch_4:
1645
166
  case Builtin::BI__sync_and_and_fetch_8:
1646
166
  case Builtin::BI__sync_and_and_fetch_16:
1647
176
  case Builtin::BI__sync_or_and_fetch:
1648
176
  case Builtin::BI__sync_or_and_fetch_1:
1649
176
  case Builtin::BI__sync_or_and_fetch_2:
1650
176
  case Builtin::BI__sync_or_and_fetch_4:
1651
176
  case Builtin::BI__sync_or_and_fetch_8:
1652
176
  case Builtin::BI__sync_or_and_fetch_16:
1653
187
  case Builtin::BI__sync_xor_and_fetch:
1654
187
  case Builtin::BI__sync_xor_and_fetch_1:
1655
187
  case Builtin::BI__sync_xor_and_fetch_2:
1656
187
  case Builtin::BI__sync_xor_and_fetch_4:
1657
187
  case Builtin::BI__sync_xor_and_fetch_8:
1658
187
  case Builtin::BI__sync_xor_and_fetch_16:
1659
205
  case Builtin::BI__sync_nand_and_fetch:
1660
205
  case Builtin::BI__sync_nand_and_fetch_1:
1661
205
  case Builtin::BI__sync_nand_and_fetch_2:
1662
205
  case Builtin::BI__sync_nand_and_fetch_4:
1663
205
  case Builtin::BI__sync_nand_and_fetch_8:
1664
205
  case Builtin::BI__sync_nand_and_fetch_16:
1665
220
  case Builtin::BI__sync_val_compare_and_swap:
1666
220
  case Builtin::BI__sync_val_compare_and_swap_1:
1667
220
  case Builtin::BI__sync_val_compare_and_swap_2:
1668
220
  case Builtin::BI__sync_val_compare_and_swap_4:
1669
220
  case Builtin::BI__sync_val_compare_and_swap_8:
1670
220
  case Builtin::BI__sync_val_compare_and_swap_16:
1671
235
  case Builtin::BI__sync_bool_compare_and_swap:
1672
235
  case Builtin::BI__sync_bool_compare_and_swap_1:
1673
235
  case Builtin::BI__sync_bool_compare_and_swap_2:
1674
236
  case Builtin::BI__sync_bool_compare_and_swap_4:
1675
236
  case Builtin::BI__sync_bool_compare_and_swap_8:
1676
236
  case Builtin::BI__sync_bool_compare_and_swap_16:
1677
249
  case Builtin::BI__sync_lock_test_and_set:
1678
249
  case Builtin::BI__sync_lock_test_and_set_1:
1679
249
  case Builtin::BI__sync_lock_test_and_set_2:
1680
249
  case Builtin::BI__sync_lock_test_and_set_4:
1681
249
  case Builtin::BI__sync_lock_test_and_set_8:
1682
249
  case Builtin::BI__sync_lock_test_and_set_16:
1683
261
  case Builtin::BI__sync_lock_release:
1684
261
  case Builtin::BI__sync_lock_release_1:
1685
261
  case Builtin::BI__sync_lock_release_2:
1686
261
  case Builtin::BI__sync_lock_release_4:
1687
261
  case Builtin::BI__sync_lock_release_8:
1688
261
  case Builtin::BI__sync_lock_release_16:
1689
266
  case Builtin::BI__sync_swap:
1690
266
  case Builtin::BI__sync_swap_1:
1691
266
  case Builtin::BI__sync_swap_2:
1692
266
  case Builtin::BI__sync_swap_4:
1693
266
  case Builtin::BI__sync_swap_8:
1694
266
  case Builtin::BI__sync_swap_16:
1695
266
    return SemaBuiltinAtomicOverloaded(TheCallResult);
1696
3
  case Builtin::BI__sync_synchronize:
1697
3
    Diag(TheCall->getBeginLoc(), diag::warn_atomic_implicit_seq_cst)
1698
3
        << TheCall->getCallee()->getSourceRange();
1699
3
    break;
1700
760
  case Builtin::BI__builtin_nontemporal_load:
1701
3.95k
  case Builtin::BI__builtin_nontemporal_store:
1702
3.95k
    return SemaBuiltinNontemporalOverloaded(TheCallResult);
1703
11
  case Builtin::BI__builtin_memcpy_inline: {
1704
11
    clang::Expr *SizeOp = TheCall->getArg(2);
1705
    // We warn about copying to or from `nullptr` pointers when `size` is
1706
    // greater than 0. When `size` is value dependent we cannot evaluate its
1707
    // value so we bail out.
1708
11
    if (SizeOp->isValueDependent())
1709
1
      break;
1710
10
    if (!SizeOp->EvaluateKnownConstInt(Context).isNullValue()) {
1711
6
      CheckNonNullArgument(*this, TheCall->getArg(0), TheCall->getExprLoc());
1712
6
      CheckNonNullArgument(*this, TheCall->getArg(1), TheCall->getExprLoc());
1713
6
    }
1714
10
    break;
1715
10
  }
1716
10
#define BUILTIN(ID, TYPE, ATTRS)
1717
10
#define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
1718
5.35k
  case Builtin::BI##ID: \
1719
5.35k
    return SemaAtomicOpsOverloaded(TheCallResult, AtomicExpr::AO##ID);
1720
10
#include "clang/Basic/Builtins.def"
1721
31
  case Builtin::BI__annotation:
1722
31
    if (SemaBuiltinMSVCAnnotation(*this, TheCall))
1723
9
      return ExprError();
1724
22
    break;
1725
12
  case Builtin::BI__builtin_annotation:
1726
12
    if (SemaBuiltinAnnotation(*this, TheCall))
1727
3
      return ExprError();
1728
9
    break;
1729
1.04k
  case Builtin::BI__builtin_addressof:
1730
1.04k
    if (SemaBuiltinAddressof(*this, TheCall))
1731
2
      return ExprError();
1732
1.04k
    break;
1733
91
  case Builtin::BI__builtin_is_aligned:
1734
195
  case Builtin::BI__builtin_align_up:
1735
281
  case Builtin::BI__builtin_align_down:
1736
281
    if (SemaBuiltinAlignment(*this, TheCall, BuiltinID))
1737
72
      return ExprError();
1738
209
    break;
1739
37
  case Builtin::BI__builtin_add_overflow:
1740
53
  case Builtin::BI__builtin_sub_overflow:
1741
114
  case Builtin::BI__builtin_mul_overflow:
1742
114
    if (SemaBuiltinOverflow(*this, TheCall, BuiltinID))
1743
8
      return ExprError();
1744
106
    break;
1745
505
  case Builtin::BI__builtin_operator_new:
1746
995
  case Builtin::BI__builtin_operator_delete: {
1747
995
    bool IsDelete = BuiltinID == Builtin::BI__builtin_operator_delete;
1748
995
    ExprResult Res =
1749
995
        SemaBuiltinOperatorNewDeleteOverloaded(TheCallResult, IsDelete);
1750
995
    if (Res.isInvalid())
1751
56
      CorrectDelayedTyposInExpr(TheCallResult.get());
1752
995
    return Res;
1753
505
  }
1754
36
  case Builtin::BI__builtin_dump_struct: {
1755
    // We first want to ensure we are called with 2 arguments
1756
36
    if (checkArgCount(*this, TheCall, 2))
1757
2
      return ExprError();
1758
    // Ensure that the first argument is of type 'struct XX *'
1759
34
    const Expr *PtrArg = TheCall->getArg(0)->IgnoreParenImpCasts();
1760
34
    const QualType PtrArgType = PtrArg->getType();
1761
34
    if (!PtrArgType->isPointerType() ||
1762
32
        !PtrArgType->getPointeeType()->isRecordType()) {
1763
3
      Diag(PtrArg->getBeginLoc(), diag::err_typecheck_convert_incompatible)
1764
3
          << PtrArgType << "structure pointer" << 1 << 0 << 3 << 1 << PtrArgType
1765
3
          << "structure pointer";
1766
3
      return ExprError();
1767
3
    }
1768
1769
    // Ensure that the second argument is of type 'FunctionType'
1770
31
    const Expr *FnPtrArg = TheCall->getArg(1)->IgnoreImpCasts();
1771
31
    const QualType FnPtrArgType = FnPtrArg->getType();
1772
31
    if (!FnPtrArgType->isPointerType()) {
1773
1
      Diag(FnPtrArg->getBeginLoc(), diag::err_typecheck_convert_incompatible)
1774
1
          << FnPtrArgType << "'int (*)(const char *, ...)'" << 1 << 0 << 3 << 2
1775
1
          << FnPtrArgType << "'int (*)(const char *, ...)'";
1776
1
      return ExprError();
1777
1
    }
1778
1779
30
    const auto *FuncType =
1780
30
        FnPtrArgType->getPointeeType()->getAs<FunctionType>();
1781
1782
30
    if (!FuncType) {
1783
0
      Diag(FnPtrArg->getBeginLoc(), diag::err_typecheck_convert_incompatible)
1784
0
          << FnPtrArgType << "'int (*)(const char *, ...)'" << 1 << 0 << 3 << 2
1785
0
          << FnPtrArgType << "'int (*)(const char *, ...)'";
1786
0
      return ExprError();
1787
0
    }
1788
1789
30
    if (const auto *FT = dyn_cast<FunctionProtoType>(FuncType)) {
1790
29
      if (!FT->getNumParams()) {
1791
1
        Diag(FnPtrArg->getBeginLoc(), diag::err_typecheck_convert_incompatible)
1792
1
            << FnPtrArgType << "'int (*)(const char *, ...)'" << 1 << 0 << 3
1793
1
            << 2 << FnPtrArgType << "'int (*)(const char *, ...)'";
1794
1
        return ExprError();
1795
1
      }
1796
28
      QualType PT = FT->getParamType(0);
1797
28
      if (!FT->isVariadic() || 
FT->getReturnType() != Context.IntTy27
||
1798
26
          !PT->isPointerType() || 
!PT->getPointeeType()->isCharType()25
||
1799
25
          !PT->getPointeeType().isConstQualified()) {
1800
4
        Diag(FnPtrArg->getBeginLoc(), diag::err_typecheck_convert_incompatible)
1801
4
            << FnPtrArgType << "'int (*)(const char *, ...)'" << 1 << 0 << 3
1802
4
            << 2 << FnPtrArgType << "'int (*)(const char *, ...)'";
1803
4
        return ExprError();
1804
4
      }
1805
25
    }
1806
1807
25
    TheCall->setType(Context.IntTy);
1808
25
    break;
1809
25
  }
1810
36
  case Builtin::BI__builtin_expect_with_probability: {
1811
    // We first want to ensure we are called with 3 arguments
1812
36
    if (checkArgCount(*this, TheCall, 3))
1813
0
      return ExprError();
1814
    // then check probability is constant float in range [0.0, 1.0]
1815
36
    const Expr *ProbArg = TheCall->getArg(2);
1816
36
    SmallVector<PartialDiagnosticAt, 8> Notes;
1817
36
    Expr::EvalResult Eval;
1818
36
    Eval.Diag = &Notes;
1819
36
    if ((!ProbArg->EvaluateAsConstantExpr(Eval, Context)) ||
1820
34
        !Eval.Val.isFloat()) {
1821
2
      Diag(ProbArg->getBeginLoc(), diag::err_probability_not_constant_float)
1822
2
          << ProbArg->getSourceRange();
1823
2
      for (const PartialDiagnosticAt &PDiag : Notes)
1824
4
        Diag(PDiag.first, PDiag.second);
1825
2
      return ExprError();
1826
2
    }
1827
34
    llvm::APFloat Probability = Eval.Val.getFloat();
1828
34
    bool LoseInfo = false;
1829
34
    Probability.convert(llvm::APFloat::IEEEdouble(),
1830
34
                        llvm::RoundingMode::Dynamic, &LoseInfo);
1831
34
    if (!(Probability >= llvm::APFloat(0.0) &&
1832
28
          Probability <= llvm::APFloat(1.0))) {
1833
12
      Diag(ProbArg->getBeginLoc(), diag::err_probability_out_of_range)
1834
12
          << ProbArg->getSourceRange();
1835
12
      return ExprError();
1836
12
    }
1837
22
    break;
1838
22
  }
1839
29
  case Builtin::BI__builtin_preserve_access_index:
1840
29
    if (SemaBuiltinPreserveAI(*this, TheCall))
1841
1
      return ExprError();
1842
28
    break;
1843
16
  case Builtin::BI__builtin_call_with_static_chain:
1844
16
    if (SemaBuiltinCallWithStaticChain(*this, TheCall))
1845
6
      return ExprError();
1846
10
    break;
1847
6
  case Builtin::BI__exception_code:
1848
30
  case Builtin::BI_exception_code:
1849
30
    if (SemaBuiltinSEHScopeCheck(*this, TheCall, Scope::SEHExceptScope,
1850
30
                                 diag::err_seh___except_block))
1851
6
      return ExprError();
1852
24
    break;
1853
5
  case Builtin::BI__exception_info:
1854
9
  case Builtin::BI_exception_info:
1855
9
    if (SemaBuiltinSEHScopeCheck(*this, TheCall, Scope::SEHFilterScope,
1856
9
                                 diag::err_seh___except_filter))
1857
7
      return ExprError();
1858
2
    break;
1859
6
  case Builtin::BI__GetExceptionInfo:
1860
6
    if (checkArgCount(*this, TheCall, 1))
1861
0
      return ExprError();
1862
1863
6
    if (CheckCXXThrowOperand(
1864
6
            TheCall->getBeginLoc(),
1865
6
            Context.getExceptionObjectType(FDecl->getParamDecl(0)->getType()),
1866
6
            TheCall))
1867
0
      return ExprError();
1868
1869
6
    TheCall->setType(Context.VoidPtrTy);
1870
6
    break;
1871
  // OpenCL v2.0, s6.13.16 - Pipe functions
1872
15
  case Builtin::BIread_pipe:
1873
28
  case Builtin::BIwrite_pipe:
1874
    // Since those two functions are declared with var args, we need a semantic
1875
    // check for the argument.
1876
28
    if (SemaBuiltinRWPipe(*this, TheCall))
1877
14
      return ExprError();
1878
14
    break;
1879
5
  case Builtin::BIreserve_read_pipe:
1880
8
  case Builtin::BIreserve_write_pipe:
1881
10
  case Builtin::BIwork_group_reserve_read_pipe:
1882
12
  case Builtin::BIwork_group_reserve_write_pipe:
1883
12
    if (SemaBuiltinReserveRWPipe(*this, TheCall))
1884
4
      return ExprError();
1885
8
    break;
1886
2
  case Builtin::BIsub_group_reserve_read_pipe:
1887
4
  case Builtin::BIsub_group_reserve_write_pipe:
1888
4
    if (checkOpenCLSubgroupExt(*this, TheCall) ||
1889
4
        SemaBuiltinReserveRWPipe(*this, TheCall))
1890
2
      return ExprError();
1891
2
    break;
1892
3
  case Builtin::BIcommit_read_pipe:
1893
6
  case Builtin::BIcommit_write_pipe:
1894
8
  case Builtin::BIwork_group_commit_read_pipe:
1895
10
  case Builtin::BIwork_group_commit_write_pipe:
1896
10
    if (SemaBuiltinCommitRWPipe(*this, TheCall))
1897
4
      return ExprError();
1898
6
    break;
1899
2
  case Builtin::BIsub_group_commit_read_pipe:
1900
4
  case Builtin::BIsub_group_commit_write_pipe:
1901
4
    if (checkOpenCLSubgroupExt(*this, TheCall) ||
1902
4
        SemaBuiltinCommitRWPipe(*this, TheCall))
1903
2
      return ExprError();
1904
2
    break;
1905
5
  case Builtin::BIget_pipe_num_packets:
1906
10
  case Builtin::BIget_pipe_max_packets:
1907
10
    if (SemaBuiltinPipePackets(*this, TheCall))
1908
2
      return ExprError();
1909
8
    break;
1910
16
  case Builtin::BIto_global:
1911
24
  case Builtin::BIto_local:
1912
32
  case Builtin::BIto_private:
1913
32
    if (SemaOpenCLBuiltinToAddr(*this, BuiltinID, TheCall))
1914
4
      return ExprError();
1915
28
    break;
1916
  // OpenCL v2.0, s6.13.17 - Enqueue kernel functions.
1917
157
  case Builtin::BIenqueue_kernel:
1918
157
    if (SemaOpenCLBuiltinEnqueueKernel(*this, TheCall))
1919
68
      return ExprError();
1920
89
    break;
1921
47
  case Builtin::BIget_kernel_work_group_size:
1922
97
  case Builtin::BIget_kernel_preferred_work_group_size_multiple:
1923
97
    if (SemaOpenCLBuiltinKernelWorkGroupSize(*this, TheCall))
1924
56
      return ExprError();
1925
41
    break;
1926
19
  case Builtin::BIget_kernel_max_sub_group_size_for_ndrange:
1927
38
  case Builtin::BIget_kernel_sub_group_count_for_ndrange:
1928
38
    if (SemaOpenCLBuiltinNDRangeAndBlock(*this, TheCall))
1929
24
      return ExprError();
1930
14
    break;
1931
1.21k
  case Builtin::BI__builtin_os_log_format:
1932
1.21k
    Cleanup.setExprNeedsCleanups(true);
1933
1.21k
    LLVM_FALLTHROUGH;
1934
2.35k
  case Builtin::BI__builtin_os_log_format_buffer_size:
1935
2.35k
    if (SemaBuiltinOSLogFormat(TheCall))
1936
2
      return ExprError();
1937
2.35k
    break;
1938
16
  case Builtin::BI__builtin_frame_address:
1939
32
  case Builtin::BI__builtin_return_address: {
1940
32
    if (SemaBuiltinConstantArgRange(TheCall, 0, 0, 0xFFFF))
1941
4
      return ExprError();
1942
1943
    // -Wframe-address warning if non-zero passed to builtin
1944
    // return/frame address.
1945
28
    Expr::EvalResult Result;
1946
28
    if (!TheCall->getArg(0)->isValueDependent() &&
1947
27
        TheCall->getArg(0)->EvaluateAsInt(Result, getASTContext()) &&
1948
27
        Result.Val.getInt() != 0)
1949
8
      Diag(TheCall->getBeginLoc(), diag::warn_frame_address)
1950
8
          << ((BuiltinID == Builtin::BI__builtin_return_address)
1951
5
                  ? "__builtin_return_address"
1952
3
                  : "__builtin_frame_address")
1953
8
          << TheCall->getSourceRange();
1954
28
    break;
1955
28
  }
1956
1957
40
  case Builtin::BI__builtin_matrix_transpose:
1958
40
    return SemaBuiltinMatrixTranspose(TheCall, TheCallResult);
1959
1960
54
  case Builtin::BI__builtin_matrix_column_major_load:
1961
54
    return SemaBuiltinMatrixColumnMajorLoad(TheCall, TheCallResult);
1962
1963
43
  case Builtin::BI__builtin_matrix_column_major_store:
1964
43
    return SemaBuiltinMatrixColumnMajorStore(TheCall, TheCallResult);
1965
1.44M
  }
1966
1967
  // Since the target specific builtins for each arch overlap, only check those
1968
  // of the arch we are compiling for.
1969
1.44M
  if (Context.BuiltinInfo.isTSBuiltin(BuiltinID)) {
1970
1.38M
    if (Context.BuiltinInfo.isAuxBuiltinID(BuiltinID)) {
1971
4
      assert(Context.getAuxTargetInfo() &&
1972
4
             "Aux Target Builtin, but not an aux target?");
1973
1974
4
      if (CheckTSBuiltinFunctionCall(
1975
4
              *Context.getAuxTargetInfo(),
1976
4
              Context.BuiltinInfo.getAuxBuiltinID(BuiltinID), TheCall))
1977
1
        return ExprError();
1978
1.38M
    } else {
1979
1.38M
      if (CheckTSBuiltinFunctionCall(Context.getTargetInfo(), BuiltinID,
1980
1.38M
                                     TheCall))
1981
2.61k
        return ExprError();
1982
1.44M
    }
1983
1.38M
  }
1984
1985
1.44M
  return TheCallResult;
1986
1.44M
}
1987
1988
// Get the valid immediate range for the specified NEON type code.
1989
2.86k
static unsigned RFT(unsigned t, bool shift = false, bool ForceQuad = false) {
1990
2.86k
  NeonTypeFlags Type(t);
1991
2.50k
  int IsQuad = ForceQuad ? 
true359
: Type.isQuad();
1992
2.86k
  switch (Type.getEltType()) {
1993
254
  case NeonTypeFlags::Int8:
1994
302
  case NeonTypeFlags::Poly8:
1995
184
    return shift ? 
7118
: (8 << IsQuad) - 1;
1996
665
  case NeonTypeFlags::Int16:
1997
711
  case NeonTypeFlags::Poly16:
1998
360
    return shift ? 15 : 
(4 << IsQuad) - 1351
;
1999
1.02k
  case NeonTypeFlags::Int32:
2000
650
    return shift ? 
31378
: (2 << IsQuad) - 1;
2001
443
  case NeonTypeFlags::Int64:
2002
467
  case NeonTypeFlags::Poly64:
2003
326
    return shift ? 63 : 
(1 << IsQuad) - 1141
;
2004
0
  case NeonTypeFlags::Poly128:
2005
0
    return shift ? 127 : (1 << IsQuad) - 1;
2006
90
  case NeonTypeFlags::Float16:
2007
90
    assert(!shift && "cannot shift float types!");
2008
90
    return (4 << IsQuad) - 1;
2009
106
  case NeonTypeFlags::Float32:
2010
106
    assert(!shift && "cannot shift float types!");
2011
106
    return (2 << IsQuad) - 1;
2012
81
  case NeonTypeFlags::Float64:
2013
81
    assert(!shift && "cannot shift float types!");
2014
81
    return (1 << IsQuad) - 1;
2015
76
  case NeonTypeFlags::BFloat16:
2016
76
    assert(!shift && "cannot shift float types!");
2017
76
    return (4 << IsQuad) - 1;
2018
0
  }
2019
0
  llvm_unreachable("Invalid NeonTypeFlag!");
2020
0
}
2021
2022
/// getNeonEltType - Return the QualType corresponding to the elements of
2023
/// the vector type specified by the NeonTypeFlags.  This is used to check
2024
/// the pointer arguments for Neon load/store intrinsics.
2025
static QualType getNeonEltType(NeonTypeFlags Flags, ASTContext &Context,
2026
1.46k
                               bool IsPolyUnsigned, bool IsInt64Long) {
2027
1.46k
  switch (Flags.getEltType()) {
2028
222
  case NeonTypeFlags::Int8:
2029
135
    return Flags.isUnsigned() ? 
Context.UnsignedCharTy87
: Context.SignedCharTy;
2030
235
  case NeonTypeFlags::Int16:
2031
142
    return Flags.isUnsigned() ? 
Context.UnsignedShortTy93
: Context.ShortTy;
2032
235
  case NeonTypeFlags::Int32:
2033
142
    return Flags.isUnsigned() ? 
Context.UnsignedIntTy93
: Context.IntTy;
2034
196
  case NeonTypeFlags::Int64:
2035
196
    if (IsInt64Long)
2036
142
      return Flags.isUnsigned() ? 
Context.UnsignedLongTy47
:
Context.LongTy95
;
2037
54
    else
2038
54
      return Flags.isUnsigned() ? 
Context.UnsignedLongLongTy29
2039
25
                                : Context.LongLongTy;
2040
87
  case NeonTypeFlags::Poly8:
2041
47
    return IsPolyUnsigned ? Context.UnsignedCharTy : 
Context.SignedCharTy40
;
2042
93
  case NeonTypeFlags::Poly16:
2043
47
    return IsPolyUnsigned ? Context.UnsignedShortTy : 
Context.ShortTy46
;
2044
48
  case NeonTypeFlags::Poly64:
2045
48
    if (IsInt64Long)
2046
48
      return Context.UnsignedLongTy;
2047
0
    else
2048
0
      return Context.UnsignedLongLongTy;
2049
0
  case NeonTypeFlags::Poly128:
2050
0
    break;
2051
93
  case NeonTypeFlags::Float16:
2052
93
    return Context.HalfTy;
2053
95
  case NeonTypeFlags::Float32:
2054
95
    return Context.FloatTy;
2055
47
  case NeonTypeFlags::Float64:
2056
47
    return Context.DoubleTy;
2057
116
  case NeonTypeFlags::BFloat16:
2058
116
    return Context.BFloat16Ty;
2059
0
  }
2060
0
  llvm_unreachable("Invalid NeonTypeFlag!");
2061
0
}
2062
2063
331k
bool Sema::CheckSVEBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
2064
  // Range check SVE intrinsics that take immediate values.
2065
331k
  SmallVector<std::tuple<int,int,int>, 3> ImmChecks;
2066
2067
331k
  switch (BuiltinID) {
2068
328k
  default:
2069
328k
    return false;
2070
0
#define GET_SVE_IMMEDIATE_CHECK
2071
0
#include "clang/Basic/arm_sve_sema_rangechecks.inc"
2072
331k
#undef GET_SVE_IMMEDIATE_CHECK
2073
331k
  }
2074
2075
  // Perform all the immediate checks for this builtin call.
2076
3.56k
  bool HasError = false;
2077
3.99k
  for (auto &I : ImmChecks) {
2078
3.99k
    int ArgNum, CheckTy, ElementSizeInBits;
2079
3.99k
    std::tie(ArgNum, CheckTy, ElementSizeInBits) = I;
2080
2081
3.99k
    typedef bool(*OptionSetCheckFnTy)(int64_t Value);
2082
2083
    // Function that checks whether the operand (ArgNum) is an immediate
2084
    // that is one of the predefined values.
2085
3.99k
    auto CheckImmediateInSet = [&](OptionSetCheckFnTy CheckImm,
2086
484
                                   int ErrDiag) -> bool {
2087
      // We can't check the value of a dependent argument.
2088
484
      Expr *Arg = TheCall->getArg(ArgNum);
2089
484
      if (Arg->isTypeDependent() || Arg->isValueDependent())
2090
0
        return false;
2091
2092
      // Check constant-ness first.
2093
484
      llvm::APSInt Imm;
2094
484
      if (SemaBuiltinConstantArg(TheCall, ArgNum, Imm))
2095
0
        return true;
2096
2097
484
      if (!CheckImm(Imm.getSExtValue()))
2098
166
        return Diag(TheCall->getBeginLoc(), ErrDiag) << Arg->getSourceRange();
2099
318
      return false;
2100
318
    };
2101
2102
3.99k
    switch ((SVETypeFlags::ImmCheckType)CheckTy) {
2103
484
    case SVETypeFlags::ImmCheck0_31:
2104
484
      if (SemaBuiltinConstantArgRange(TheCall, ArgNum, 0, 31))
2105
0
        HasError = true;
2106
484
      break;
2107
271
    case SVETypeFlags::ImmCheck0_13:
2108
271
      if (SemaBuiltinConstantArgRange(TheCall, ArgNum, 0, 13))
2109
16
        HasError = true;
2110
271
      break;
2111
640
    case SVETypeFlags::ImmCheck1_16:
2112
640
      if (SemaBuiltinConstantArgRange(TheCall, ArgNum, 1, 16))
2113
352
        HasError = true;
2114
640
      break;
2115
32
    case SVETypeFlags::ImmCheck0_7:
2116
32
      if (SemaBuiltinConstantArgRange(TheCall, ArgNum, 0, 7))
2117
12
        HasError = true;
2118
32
      break;
2119
78
    case SVETypeFlags::ImmCheckExtract:
2120
78
      if (SemaBuiltinConstantArgRange(TheCall, ArgNum, 0,
2121
78
                                      (2048 / ElementSizeInBits) - 1))
2122
30
        HasError = true;
2123
78
      break;
2124
453
    case SVETypeFlags::ImmCheckShiftRight:
2125
453
      if (SemaBuiltinConstantArgRange(TheCall, ArgNum, 1, ElementSizeInBits))
2126
176
        HasError = true;
2127
453
      break;
2128
360
    case SVETypeFlags::ImmCheckShiftRightNarrow:
2129
360
      if (SemaBuiltinConstantArgRange(TheCall, ArgNum, 1,
2130
360
                                      ElementSizeInBits / 2))
2131
120
        HasError = true;
2132
360
      break;
2133
192
    case SVETypeFlags::ImmCheckShiftLeft:
2134
192
      if (SemaBuiltinConstantArgRange(TheCall, ArgNum, 0,
2135
192
                                      ElementSizeInBits - 1))
2136
64
        HasError = true;
2137
192
      break;
2138
492
    case SVETypeFlags::ImmCheckLaneIndex:
2139
492
      if (SemaBuiltinConstantArgRange(TheCall, ArgNum, 0,
2140
492
                                      (128 / (1 * ElementSizeInBits)) - 1))
2141
160
        HasError = true;
2142
492
      break;
2143
116
    case SVETypeFlags::ImmCheckLaneIndexCompRotate:
2144
116
      if (SemaBuiltinConstantArgRange(TheCall, ArgNum, 0,
2145
116
                                      (128 / (2 * ElementSizeInBits)) - 1))
2146
48
        HasError = true;
2147
116
      break;
2148
60
    case SVETypeFlags::ImmCheckLaneIndexDot:
2149
60
      if (SemaBuiltinConstantArgRange(TheCall, ArgNum, 0,
2150
60
                                      (128 / (4 * ElementSizeInBits)) - 1))
2151
16
        HasError = true;
2152
60
      break;
2153
136
    case SVETypeFlags::ImmCheckComplexRot90_270:
2154
136
      if (CheckImmediateInSet([](int64_t V) { return V == 90 || 
V == 27085
; },
2155
136
                              diag::err_rotation_argument_to_cadd))
2156
58
        HasError = true;
2157
136
      break;
2158
348
    case SVETypeFlags::ImmCheckComplexRotAll90:
2159
348
      if (CheckImmediateInSet(
2160
348
              [](int64_t V) {
2161
348
                return V == 0 || 
V == 90270
||
V == 180216
||
V == 270160
;
2162
348
              },
2163
348
              diag::err_rotation_argument_to_cmla))
2164
108
        HasError = true;
2165
348
      break;
2166
107
    case SVETypeFlags::ImmCheck0_1:
2167
107
      if (SemaBuiltinConstantArgRange(TheCall, ArgNum, 0, 1))
2168
44
        HasError = true;
2169
107
      break;
2170
111
    case SVETypeFlags::ImmCheck0_2:
2171
111
      if (SemaBuiltinConstantArgRange(TheCall, ArgNum, 0, 2))
2172
44
        HasError = true;
2173
111
      break;
2174
119
    case SVETypeFlags::ImmCheck0_3:
2175
119
      if (SemaBuiltinConstantArgRange(TheCall, ArgNum, 0, 3))
2176
44
        HasError = true;
2177
119
      break;
2178
3.99k
    }
2179
3.99k
  }
2180
2181
3.56k
  return HasError;
2182
3.56k
}
2183
2184
bool Sema::CheckNeonBuiltinFunctionCall(const TargetInfo &TI,
2185
368k
                                        unsigned BuiltinID, CallExpr *TheCall) {
2186
368k
  llvm::APSInt Result;
2187
368k
  uint64_t mask = 0;
2188
368k
  unsigned TV = 0;
2189
368k
  int PtrArgNum = -1;
2190
368k
  bool HasConstPtr = false;
2191
368k
  switch (BuiltinID) {
2192
0
#define GET_NEON_OVERLOAD_CHECK
2193
0
#include "clang/Basic/arm_neon.inc"
2194
368k
#include "clang/Basic/arm_fp16.inc"
2195
368k
#undef GET_NEON_OVERLOAD_CHECK
2196
368k
  }
2197
2198
  // For NEON intrinsics which are overloaded on vector element type, validate
2199
  // the immediate which specifies which variant to emit.
2200
368k
  unsigned ImmArg = TheCall->getNumArgs()-1;
2201
368k
  if (mask) {
2202
123k
    if (SemaBuiltinConstantArg(TheCall, ImmArg, Result))
2203
0
      return true;
2204
2205
123k
    TV = Result.getLimitedValue(64);
2206
123k
    if ((TV > 63) || (mask & (1ULL << TV)) == 0)
2207
0
      return Diag(TheCall->getBeginLoc(), diag::err_invalid_neon_type_code)
2208
0
             << TheCall->getArg(ImmArg)->getSourceRange();
2209
368k
  }
2210
2211
368k
  if (PtrArgNum >= 0) {
2212
    // Check that pointer arguments have the specified type.
2213
1.46k
    Expr *Arg = TheCall->getArg(PtrArgNum);
2214
1.46k
    if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg))
2215
1.46k
      Arg = ICE->getSubExpr();
2216
1.46k
    ExprResult RHS = DefaultFunctionArrayLvalueConversion(Arg);
2217
1.46k
    QualType RHSTy = RHS.get()->getType();
2218
2219
1.46k
    llvm::Triple::ArchType Arch = TI.getTriple().getArch();
2220
1.46k
    bool IsPolyUnsigned = Arch == llvm::Triple::aarch64 ||
2221
546
                          Arch == llvm::Triple::aarch64_32 ||
2222
546
                          Arch == llvm::Triple::aarch64_be;
2223
1.46k
    bool IsInt64Long = TI.getInt64Type() == TargetInfo::SignedLong;
2224
1.46k
    QualType EltTy =
2225
1.46k
        getNeonEltType(NeonTypeFlags(TV), Context, IsPolyUnsigned, IsInt64Long);
2226
1.46k
    if (HasConstPtr)
2227
824
      EltTy = EltTy.withConst();
2228
1.46k
    QualType LHSTy = Context.getPointerType(EltTy);
2229
1.46k
    AssignConvertType ConvTy;
2230
1.46k
    ConvTy = CheckSingleAssignmentConstraints(LHSTy, RHS);
2231
1.46k
    if (RHS.isInvalid())
2232
0
      return true;
2233
1.46k
    if (DiagnoseAssignmentResult(ConvTy, Arg->getBeginLoc(), LHSTy, RHSTy,
2234
1.46k
                                 RHS.get(), AA_Assigning))
2235
0
      return true;
2236
368k
  }
2237
2238
  // For NEON intrinsics which take an immediate value as part of the
2239
  // instruction, range check them here.
2240
368k
  unsigned i = 0, l = 0, u = 0;
2241
368k
  switch (BuiltinID) {
2242
365k
  default:
2243
365k
    return false;
2244
0
  #define GET_NEON_IMMEDIATE_CHECK
2245
365k
  #include "clang/Basic/arm_neon.inc"
2246
0
  #include "clang/Basic/arm_fp16.inc"
2247
368k
  #undef GET_NEON_IMMEDIATE_CHECK
2248
368k
  }
2249
2250
3.68k
  return SemaBuiltinConstantArgRange(TheCall, i, l, u + l);
2251
368k
}
2252
2253
36.3k
bool Sema::CheckMVEBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
2254
36.3k
  switch (BuiltinID) {
2255
35.3k
  default:
2256
35.3k
    return false;
2257
2
  #include "clang/Basic/arm_mve_builtin_sema.inc"
2258
36.3k
  }
2259
36.3k
}
2260
2261
bool Sema::CheckCDEBuiltinFunctionCall(const TargetInfo &TI, unsigned BuiltinID,
2262
36.3k
                                       CallExpr *TheCall) {
2263
36.3k
  bool Err = false;
2264
36.3k
  switch (BuiltinID) {
2265
36.1k
  default:
2266
36.1k
    return false;
2267
9
#include "clang/Basic/arm_cde_builtin_sema.inc"
2268
36.3k
  }
2269
2270
120
  if (Err)
2271
67
    return true;
2272
2273
53
  return CheckARMCoprocessorImmediate(TI, TheCall->getArg(0), /*WantCDE*/ true);
2274
53
}
2275
2276
bool Sema::CheckARMCoprocessorImmediate(const TargetInfo &TI,
2277
113
                                        const Expr *CoprocArg, bool WantCDE) {
2278
113
  if (isConstantEvaluated())
2279
0
    return false;
2280
2281
  // We can't check the value of a dependent argument.
2282
113
  if (CoprocArg->isTypeDependent() || CoprocArg->isValueDependent())
2283
0
    return false;
2284
2285
113
  llvm::APSInt CoprocNoAP = *CoprocArg->getIntegerConstantExpr(Context);
2286
113
  int64_t CoprocNo = CoprocNoAP.getExtValue();
2287
113
  assert(CoprocNo >= 0 && "Coprocessor immediate must be non-negative");
2288
2289
113
  uint32_t CDECoprocMask = TI.getARMCDECoprocMask();
2290
113
  bool IsCDECoproc = CoprocNo <= 7 && 
(CDECoprocMask & (1 << CoprocNo))97
;
2291
2292
113
  if (IsCDECoproc != WantCDE)
2293
19
    return Diag(CoprocArg->getBeginLoc(), diag::err_arm_invalid_coproc)
2294
19
           << (int)CoprocNo << (int)WantCDE << CoprocArg->getSourceRange();
2295
2296
94
  return false;
2297
94
}
2298
2299
bool Sema::CheckARMBuiltinExclusiveCall(unsigned BuiltinID, CallExpr *TheCall,
2300
241
                                        unsigned MaxWidth) {
2301
241
  assert((BuiltinID == ARM::BI__builtin_arm_ldrex ||
2302
241
          BuiltinID == ARM::BI__builtin_arm_ldaex ||
2303
241
          BuiltinID == ARM::BI__builtin_arm_strex ||
2304
241
          BuiltinID == ARM::BI__builtin_arm_stlex ||
2305
241
          BuiltinID == AArch64::BI__builtin_arm_ldrex ||
2306
241
          BuiltinID == AArch64::BI__builtin_arm_ldaex ||
2307
241
          BuiltinID == AArch64::BI__builtin_arm_strex ||
2308
241
          BuiltinID == AArch64::BI__builtin_arm_stlex) &&
2309
241
         "unexpected ARM builtin");
2310
241
  bool IsLdrex = BuiltinID == ARM::BI__builtin_arm_ldrex ||
2311
207
                 BuiltinID == ARM::BI__builtin_arm_ldaex ||
2312
184
                 BuiltinID == AArch64::BI__builtin_arm_ldrex ||
2313
145
                 BuiltinID == AArch64::BI__builtin_arm_ldaex;
2314
2315
241
  DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts());
2316
2317
  // Ensure that we have the proper number of arguments.
2318
241
  if (checkArgCount(*this, TheCall, IsLdrex ? 
1120
:
2121
))
2319
16
    return true;
2320
2321
  // Inspect the pointer argument of the atomic builtin.  This should always be
2322
  // a pointer type, whose element is an integral scalar or pointer type.
2323
  // Because it is a pointer type, we don't have to worry about any implicit
2324
  // casts here.
2325
225
  Expr *PointerArg = TheCall->getArg(IsLdrex ? 
0112
:
1113
);
2326
225
  ExprResult PointerArgRes = DefaultFunctionArrayLvalueConversion(PointerArg);
2327
225
  if (PointerArgRes.isInvalid())
2328
0
    return true;
2329
225
  PointerArg = PointerArgRes.get();
2330
2331
225
  const PointerType *pointerType = PointerArg->getType()->getAs<PointerType>();
2332
225
  if (!pointerType) {
2333
0
    Diag(DRE->getBeginLoc(), diag::err_atomic_builtin_must_be_pointer)
2334
0
        << PointerArg->getType() << PointerArg->getSourceRange();
2335
0
    return true;
2336
0
  }
2337
2338
  // ldrex takes a "const volatile T*" and strex takes a "volatile T*". Our next
2339
  // task is to insert the appropriate casts into the AST. First work out just
2340
  // what the appropriate type is.
2341
225
  QualType ValType = pointerType->getPointeeType();
2342
225
  QualType AddrType = ValType.getUnqualifiedType().withVolatile();
2343
225
  if (IsLdrex)
2344
112
    AddrType.addConst();
2345
2346
  // Issue a warning if the cast is dodgy.
2347
225
  CastKind CastNeeded = CK_NoOp;
2348
225
  if (!AddrType.isAtLeastAsQualifiedAs(ValType)) {
2349
4
    CastNeeded = CK_BitCast;
2350
4
    Diag(DRE->getBeginLoc(), diag::ext_typecheck_convert_discards_qualifiers)
2351
4
        << PointerArg->getType() << Context.getPointerType(AddrType)
2352
4
        << AA_Passing << PointerArg->getSourceRange();
2353
4
  }
2354
2355
  // Finally, do the cast and replace the argument with the corrected version.
2356
225
  AddrType = Context.getPointerType(AddrType);
2357
225
  PointerArgRes = ImpCastExprToType(PointerArg, AddrType, CastNeeded);
2358
225
  if (PointerArgRes.isInvalid())
2359
0
    return true;
2360
225
  PointerArg = PointerArgRes.get();
2361
2362
113
  TheCall->setArg(IsLdrex ? 
0112
: 1, PointerArg);
2363
2364
  // In general, we allow ints, floats and pointers to be loaded and stored.
2365
225
  if (!ValType->isIntegerType() && 
!ValType->isAnyPointerType()72
&&
2366
40
      !ValType->isBlockPointerType() && !ValType->isFloatingType()) {
2367
8
    Diag(DRE->getBeginLoc(), diag::err_atomic_builtin_must_be_pointer_intfltptr)
2368
8
        << PointerArg->getType() << PointerArg->getSourceRange();
2369
8
    return true;
2370
8
  }
2371
2372
  // But ARM doesn't have instructions to deal with 128-bit versions.
2373
217
  if (Context.getTypeSize(ValType) > MaxWidth) {
2374
4
    assert(MaxWidth == 64 && "Diagnostic unexpectedly inaccurate");
2375
4
    Diag(DRE->getBeginLoc(), diag::err_atomic_exclusive_builtin_pointer_size)
2376
4
        << PointerArg->getType() << PointerArg->getSourceRange();
2377
4
    return true;
2378
4
  }
2379
2380
213
  switch (ValType.getObjCLifetime()) {
2381
213
  case Qualifiers::OCL_None:
2382
213
  case Qualifiers::OCL_ExplicitNone:
2383
    // okay
2384
213
    break;
2385
2386
0
  case Qualifiers::OCL_Weak:
2387
0
  case Qualifiers::OCL_Strong:
2388
0
  case Qualifiers::OCL_Autoreleasing:
2389
0
    Diag(DRE->getBeginLoc(), diag::err_arc_atomic_ownership)
2390
0
        << ValType << PointerArg->getSourceRange();
2391
0
    return true;
2392
213
  }
2393
2394
213
  if (IsLdrex) {
2395
106
    TheCall->setType(ValType);
2396
106
    return false;
2397
106
  }
2398
2399
  // Initialize the argument to be stored.
2400
107
  ExprResult ValArg = TheCall->getArg(0);
2401
107
  InitializedEntity Entity = InitializedEntity::InitializeParameter(
2402
107
      Context, ValType, /*consume*/ false);
2403
107
  ValArg = PerformCopyInitialization(Entity, SourceLocation(), ValArg);
2404
107
  if (ValArg.isInvalid())
2405
4
    return true;
2406
103
  TheCall->setArg(0, ValArg.get());
2407
2408
  // __builtin_arm_strex always returns an int. It's marked as such in the .def,
2409
  // but the custom checker bypasses all default analysis.
2410
103
  TheCall->setType(Context.IntTy);
2411
103
  return false;
2412
103
}
2413
2414
bool Sema::CheckARMBuiltinFunctionCall(const TargetInfo &TI, unsigned BuiltinID,
2415
36.6k
                                       CallExpr *TheCall) {
2416
36.6k
  if (BuiltinID == ARM::BI__builtin_arm_ldrex ||
2417
36.5k
      BuiltinID == ARM::BI__builtin_arm_ldaex ||
2418
36.5k
      BuiltinID == ARM::BI__builtin_arm_strex ||
2419
36.5k
      BuiltinID == ARM::BI__builtin_arm_stlex) {
2420
115
    return CheckARMBuiltinExclusiveCall(BuiltinID, TheCall, 64);
2421
115
  }
2422
2423
36.4k
  if (BuiltinID == ARM::BI__builtin_arm_prefetch) {
2424
8
    return SemaBuiltinConstantArgRange(TheCall, 1, 0, 1) ||
2425
5
      SemaBuiltinConstantArgRange(TheCall, 2, 0, 1);
2426
8
  }
2427
2428
36.4k
  if (BuiltinID == ARM::BI__builtin_arm_rsr64 ||
2429
36.4k
      BuiltinID == ARM::BI__builtin_arm_wsr64)
2430
11
    return SemaBuiltinARMSpecialReg(BuiltinID, TheCall, 0, 3, false);
2431
2432
36.4k
  if (BuiltinID == ARM::BI__builtin_arm_rsr ||
2433
36.4k
      BuiltinID == ARM::BI__builtin_arm_rsrp ||
2434
36.4k
      BuiltinID == ARM::BI__builtin_arm_wsr ||
2435
36.4k
      BuiltinID == ARM::BI__builtin_arm_wsrp)
2436
22
    return SemaBuiltinARMSpecialReg(BuiltinID, TheCall, 0, 5, true);
2437
2438
36.4k
  if (CheckNeonBuiltinFunctionCall(TI, BuiltinID, TheCall))
2439
46
    return true;
2440
36.3k
  if (CheckMVEBuiltinFunctionCall(BuiltinID, TheCall))
2441
94
    return true;
2442
36.3k
  if (CheckCDEBuiltinFunctionCall(TI, BuiltinID, TheCall))
2443
68
    return true;
2444
2445
  // For intrinsics which take an immediate value as part of the instruction,
2446
  // range check them here.
2447
  // FIXME: VFP Intrinsics should error if VFP not present.
2448
36.2k
  switch (BuiltinID) {
2449
36.0k
  default: return false;
2450
13
  case ARM::BI__builtin_arm_ssat:
2451
13
    return SemaBuiltinConstantArgRange(TheCall, 1, 1, 32);
2452
11
  case ARM::BI__builtin_arm_usat:
2453
11
    return SemaBuiltinConstantArgRange(TheCall, 1, 0, 31);
2454
8
  case ARM::BI__builtin_arm_ssat16:
2455
8
    return SemaBuiltinConstantArgRange(TheCall, 1, 1, 16);
2456
6
  case ARM::BI__builtin_arm_usat16:
2457
6
    return SemaBuiltinConstantArgRange(TheCall, 1, 0, 15);
2458
18
  case ARM::BI__builtin_arm_vcvtr_f:
2459
20
  case ARM::BI__builtin_arm_vcvtr_d:
2460
20
    return SemaBuiltinConstantArgRange(TheCall, 1, 0, 1);
2461
8
  case ARM::BI__builtin_arm_dmb:
2462
12
  case ARM::BI__builtin_arm_dsb:
2463
16
  case ARM::BI__builtin_arm_isb:
2464
20
  case ARM::BI__builtin_arm_dbg:
2465
20
    return SemaBuiltinConstantArgRange(TheCall, 0, 0, 15);
2466
2
  case ARM::BI__builtin_arm_cdp:
2467
4
  case ARM::BI__builtin_arm_cdp2:
2468
6
  case ARM::BI__builtin_arm_mcr:
2469
8
  case ARM::BI__builtin_arm_mcr2:
2470
10
  case ARM::BI__builtin_arm_mrc:
2471
12
  case ARM::BI__builtin_arm_mrc2:
2472
16
  case ARM::BI__builtin_arm_mcrr:
2473
20
  case ARM::BI__builtin_arm_mcrr2:
2474
24
  case ARM::BI__builtin_arm_mrrc:
2475
28
  case ARM::BI__builtin_arm_mrrc2:
2476
32
  case ARM::BI__builtin_arm_ldc:
2477
36
  case ARM::BI__builtin_arm_ldcl:
2478
40
  case ARM::BI__builtin_arm_ldc2:
2479
44
  case ARM::BI__builtin_arm_ldc2l:
2480
48
  case ARM::BI__builtin_arm_stc:
2481
52
  case ARM::BI__builtin_arm_stcl:
2482
56
  case ARM::BI__builtin_arm_stc2:
2483
60
  case ARM::BI__builtin_arm_stc2l:
2484
60
    return SemaBuiltinConstantArgRange(TheCall, 0, 0, 15) ||
2485
60
           CheckARMCoprocessorImmediate(TI, TheCall->getArg(0),
2486
60
                                        /*WantCDE*/ false);
2487
36.2k
  }
2488
36.2k
}
2489
2490
bool Sema::CheckAArch64BuiltinFunctionCall(const TargetInfo &TI,
2491
                                           unsigned BuiltinID,
2492
332k
                                           CallExpr *TheCall) {
2493
332k
  if (BuiltinID == AArch64::BI__builtin_arm_ldrex ||
2494
332k
      BuiltinID == AArch64::BI__builtin_arm_ldaex ||
2495
332k
      BuiltinID == AArch64::BI__builtin_arm_strex ||
2496
332k
      BuiltinID == AArch64::BI__builtin_arm_stlex) {
2497
126
    return CheckARMBuiltinExclusiveCall(BuiltinID, TheCall, 128);
2498
126
  }
2499
2500
332k
  if (BuiltinID == AArch64::BI__builtin_arm_prefetch) {
2501
20
    return SemaBuiltinConstantArgRange(TheCall, 1, 0, 1) ||
2502
18
      SemaBuiltinConstantArgRange(TheCall, 2, 0, 2) ||
2503
16
      SemaBuiltinConstantArgRange(TheCall, 3, 0, 1) ||
2504
14
      SemaBuiltinConstantArgRange(TheCall, 4, 0, 1);
2505
20
  }
2506
2507
332k
  if (BuiltinID == AArch64::BI__builtin_arm_rsr64 ||
2508
332k
      BuiltinID == AArch64::BI__builtin_arm_wsr64)
2509
21
    return SemaBuiltinARMSpecialReg(BuiltinID, TheCall, 0, 5, true);
2510
2511
  // Memory Tagging Extensions (MTE) Intrinsics
2512
332k
  if (BuiltinID == AArch64::BI__builtin_arm_irg ||
2513
332k
      BuiltinID == AArch64::BI__builtin_arm_addg ||
2514
332k
      BuiltinID == AArch64::BI__builtin_arm_gmi ||
2515
332k
      BuiltinID == AArch64::BI__builtin_arm_ldg ||
2516
332k
      BuiltinID == AArch64::BI__builtin_arm_stg ||
2517
332k
      BuiltinID == AArch64::BI__builtin_arm_subp) {
2518
54
    return SemaBuiltinARMMemoryTaggingCall(BuiltinID, TheCall);
2519
54
  }
2520
2521
332k
  if (BuiltinID == AArch64::BI__builtin_arm_rsr ||
2522
332k
      BuiltinID == AArch64::BI__builtin_arm_rsrp ||
2523
332k
      BuiltinID == AArch64::BI__builtin_arm_wsr ||
2524
332k
      BuiltinID == AArch64::BI__builtin_arm_wsrp)
2525
34
    return SemaBuiltinARMSpecialReg(BuiltinID, TheCall, 0, 5, true);
2526
2527
  // Only check the valid encoding range. Any constant in this range would be
2528
  // converted to a register of the form S1_2_C3_C4_5. Let the hardware throw
2529
  // an exception for incorrect registers. This matches MSVC behavior.
2530
332k
  if (BuiltinID == AArch64::BI_ReadStatusReg ||
2531
332k
      BuiltinID == AArch64::BI_WriteStatusReg)
2532
82
    return SemaBuiltinConstantArgRange(TheCall, 0, 0, 0x7fff);
2533
2534
332k
  if (BuiltinID == AArch64::BI__getReg)
2535
4
    return SemaBuiltinConstantArgRange(TheCall, 0, 0, 31);
2536
2537
332k
  if (CheckNeonBuiltinFunctionCall(TI, BuiltinID, TheCall))
2538
350
    return true;
2539
2540
331k
  if (CheckSVEBuiltinFunctionCall(BuiltinID, TheCall))
2541
1.29k
    return true;
2542
2543
  // For intrinsics which take an immediate value as part of the instruction,
2544
  // range check them here.
2545
330k
  unsigned i = 0, l = 0, u = 0;
2546
330k
  switch (BuiltinID) {
2547
330k
  default: return false;
2548
7
  case AArch64::BI__builtin_arm_dmb:
2549
12
  case AArch64::BI__builtin_arm_dsb:
2550
17
  case AArch64::BI__builtin_arm_isb: l = 0; u = 15; break;
2551
6
  case AArch64::BI__builtin_arm_tcancel: l = 0; u = 65535; break;
2552
23
  }
2553
2554
23
  return SemaBuiltinConstantArgRange(TheCall, i, l, u + l);
2555
23
}
2556
2557
15
static bool isValidBPFPreserveFieldInfoArg(Expr *Arg) {
2558
15
  if (Arg->getType()->getAsPlaceholderType())
2559
0
    return false;
2560
2561
  // The first argument needs to be a record field access.
2562
  // If it is an array element access, we delay decision
2563
  // to BPF backend to check whether the access is a
2564
  // field access or not.
2565
15
  return (Arg->IgnoreParens()->getObjectKind() == OK_BitField ||
2566
10
          dyn_cast<MemberExpr>(Arg->IgnoreParens()) ||
2567
4
          dyn_cast<ArraySubscriptExpr>(Arg->IgnoreParens()));
2568
15
}
2569
2570
static bool isEltOfVectorTy(ASTContext &Context, CallExpr *Call, Sema &S,
2571
36
                            QualType VectorTy, QualType EltTy) {
2572
36
  QualType VectorEltTy = VectorTy->castAs<VectorType>()->getElementType();
2573
36
  if (!Context.hasSameType(VectorEltTy, EltTy)) {
2574
12
    S.Diag(Call->getBeginLoc(), diag::err_typecheck_call_different_arg_types)
2575
12
        << Call->getSourceRange() << VectorEltTy << EltTy;
2576
12
    return false;
2577
12
  }
2578
24
  return true;
2579
24
}
2580
2581
14
static bool isValidBPFPreserveTypeInfoArg(Expr *Arg) {
2582
14
  QualType ArgType = Arg->getType();
2583
14
  if (ArgType->getAsPlaceholderType())
2584
0
    return false;
2585
2586
  // for TYPE_EXISTENCE/TYPE_SIZEOF reloc type
2587
  // format:
2588
  //   1. __builtin_preserve_type_info(*(<type> *)0, flag);
2589
  //   2. <type> var;
2590
  //      __builtin_preserve_type_info(var, flag);
2591
14
  if (!dyn_cast<DeclRefExpr>(Arg->IgnoreParens()) &&
2592
8
      !dyn_cast<UnaryOperator>(Arg->IgnoreParens()))
2593
1
    return false;
2594
2595
  // Typedef type.
2596
13
  if (ArgType->getAs<TypedefType>())
2597
4
    return true;
2598
2599
  // Record type or Enum type.
2600
9
  const Type *Ty = ArgType->getUnqualifiedDesugaredType();
2601
9
  if (const auto *RT = Ty->getAs<RecordType>()) {
2602
5
    if (!RT->getDecl()->getDeclName().isEmpty())
2603
5
      return true;
2604
4
  } else if (const auto *ET = Ty->getAs<EnumType>()) {
2605
4
    if (!ET->getDecl()->getDeclName().isEmpty())
2606
4
      return true;
2607
0
  }
2608
2609
0
  return false;
2610
0
}
2611
2612
8
static bool isValidBPFPreserveEnumValueArg(Expr *Arg) {
2613
8
  QualType ArgType = Arg->getType();
2614
8
  if (ArgType->getAsPlaceholderType())
2615
0
    return false;
2616
2617
  // for ENUM_VALUE_EXISTENCE/ENUM_VALUE reloc type
2618
  // format:
2619
  //   __builtin_preserve_enum_value(*(<enum_type> *)<enum_value>,
2620
  //                                 flag);
2621
8
  const auto *UO = dyn_cast<UnaryOperator>(Arg->IgnoreParens());
2622
8
  if (!UO)
2623
0
    return false;
2624
2625
8
  const auto *CE = dyn_cast<CStyleCastExpr>(UO->getSubExpr());
2626
8
  if (!CE || CE->getCastKind() != CK_IntegralToPointer)
2627
1
    return false;
2628
2629
  // The integer must be from an EnumConstantDecl.
2630
7
  const auto *DR = dyn_cast<DeclRefExpr>(CE->getSubExpr());
2631
7
  if (!DR)
2632
0
    return false;
2633
2634
7
  const EnumConstantDecl *Enumerator =
2635
7
      dyn_cast<EnumConstantDecl>(DR->getDecl());
2636
7
  if (!Enumerator)
2637
0
    return false;
2638
2639
  // The type must be EnumType.
2640
7
  const Type *Ty = ArgType->getUnqualifiedDesugaredType();
2641
7
  const auto *ET = Ty->getAs<EnumType>();
2642
7
  if (!ET)
2643
0
    return false;
2644
2645
  // The enum value must be supported.
2646
11
  
for (auto *EDI : ET->getDecl()->enumerators())7
{
2647
11
    if (EDI == Enumerator)
2648
6
      return true;
2649
11
  }
2650
2651
1
  return false;
2652
7
}
2653
2654
bool Sema::CheckBPFBuiltinFunctionCall(unsigned BuiltinID,
2655
49
                                       CallExpr *TheCall) {
2656
49
  assert((BuiltinID == BPF::BI__builtin_preserve_field_info ||
2657
49
          BuiltinID == BPF::BI__builtin_btf_type_id ||
2658
49
          BuiltinID == BPF::BI__builtin_preserve_type_info ||
2659
49
          BuiltinID == BPF::BI__builtin_preserve_enum_value) &&
2660
49
         "unexpected BPF builtin");
2661
2662
49
  if (checkArgCount(*this, TheCall, 2))
2663
1
    return true;
2664
2665
  // The second argument needs to be a constant int
2666
48
  Expr *Arg = TheCall->getArg(1);
2667
48
  Optional<llvm::APSInt> Value = Arg->getIntegerConstantExpr(Context);
2668
48
  diag::kind kind;
2669
48
  if (!Value) {
2670
3
    if (BuiltinID == BPF::BI__builtin_preserve_field_info)
2671
2
      kind = diag::err_preserve_field_info_not_const;
2672
1
    else if (BuiltinID == BPF::BI__builtin_btf_type_id)
2673
1
      kind = diag::err_btf_type_id_not_const;
2674
0
    else if (BuiltinID == BPF::BI__builtin_preserve_type_info)
2675
0
      kind = diag::err_preserve_type_info_not_const;
2676
0
    else
2677
0
      kind = diag::err_preserve_enum_value_not_const;
2678
3
    Diag(Arg->getBeginLoc(), kind) << 2 << Arg->getSourceRange();
2679
3
    return true;
2680
3
  }
2681
2682
  // The first argument
2683
45
  Arg = TheCall->getArg(0);
2684
45
  bool InvalidArg = false;
2685
45
  bool ReturnUnsignedInt = true;
2686
45
  if (BuiltinID == BPF::BI__builtin_preserve_field_info) {
2687
15
    if (!isValidBPFPreserveFieldInfoArg(Arg)) {
2688
2
      InvalidArg = true;
2689
2
      kind = diag::err_preserve_field_info_not_field;
2690
2
    }
2691
30
  } else if (BuiltinID == BPF::BI__builtin_preserve_type_info) {
2692
14
    if (!isValidBPFPreserveTypeInfoArg(Arg)) {
2693
1
      InvalidArg = true;
2694
1
      kind = diag::err_preserve_type_info_invalid;
2695
1
    }
2696
16
  } else if (BuiltinID == BPF::BI__builtin_preserve_enum_value) {
2697
8
    if (!isValidBPFPreserveEnumValueArg(Arg)) {
2698
2
      InvalidArg = true;
2699
2
      kind = diag::err_preserve_enum_value_invalid;
2700
2
    }
2701
8
    ReturnUnsignedInt = false;
2702
8
  } else if (BuiltinID == BPF::BI__builtin_btf_type_id) {
2703
8
    ReturnUnsignedInt = false;
2704
8
  }
2705
2706
45
  if (InvalidArg) {
2707
5
    Diag(Arg->getBeginLoc(), kind) << 1 << Arg->getSourceRange();
2708
5
    return true;
2709
5
  }
2710
2711
40
  if (ReturnUnsignedInt)
2712
26
    TheCall->setType(Context.UnsignedIntTy);
2713
14
  else
2714
14
    TheCall->setType(Context.UnsignedLongTy);
2715
40
  return false;
2716
40
}
2717
2718
1.80k
bool Sema::CheckHexagonBuiltinArgument(unsigned BuiltinID, CallExpr *TheCall) {
2719
1.80k
  struct ArgInfo {
2720
1.80k
    uint8_t OpNum;
2721
1.80k
    bool IsSigned;
2722
1.80k
    uint8_t BitWidth;
2723
1.80k
    uint8_t Align;
2724
1.80k
  };
2725
1.80k
  struct BuiltinInfo {
2726
1.80k
    unsigned BuiltinID;
2727
1.80k
    ArgInfo Infos[2];
2728
1.80k
  };
2729
2730
1.80k
  static BuiltinInfo Infos[] = {
2731
1.80k
    { Hexagon::BI__builtin_circ_ldd,                  {{ 3, true,  4,  3 }} },
2732
1.80k
    { Hexagon::BI__builtin_circ_ldw,                  {{ 3, true,  4,  2 }} },
2733
1.80k
    { Hexagon::BI__builtin_circ_ldh,                  {{ 3, true,  4,  1 }} },
2734
1.80k
    { Hexagon::BI__builtin_circ_lduh,                 {{ 3, true,  4,  1 }} },
2735
1.80k
    { Hexagon::BI__builtin_circ_ldb,                  {{ 3, true,  4,  0 }} },
2736
1.80k
    { Hexagon::BI__builtin_circ_ldub,                 {{ 3, true,  4,  0 }} },
2737
1.80k
    { Hexagon::BI__builtin_circ_std,                  {{ 3, true,  4,  3 }} },
2738
1.80k
    { Hexagon::BI__builtin_circ_stw,                  {{ 3, true,  4,  2 }} },
2739
1.80k
    { Hexagon::BI__builtin_circ_sth,                  {{ 3, true,  4,  1 }} },
2740
1.80k
    { Hexagon::BI__builtin_circ_sthhi,                {{ 3, true,  4,  1 }} },
2741
1.80k
    { Hexagon::BI__builtin_circ_stb,                  {{ 3, true,  4,  0 }} },
2742
2743
1.80k
    { Hexagon::BI__builtin_HEXAGON_L2_loadrub_pci,    {{ 1, true,  4,  0 }} },
2744
1.80k
    { Hexagon::BI__builtin_HEXAGON_L2_loadrb_pci,     {{ 1, true,  4,  0 }} },
2745
1.80k
    { Hexagon::BI__builtin_HEXAGON_L2_loadruh_pci,    {{ 1, true,  4,  1 }} },
2746
1.80k
    { Hexagon::BI__builtin_HEXAGON_L2_loadrh_pci,     {{ 1, true,  4,  1 }} },
2747
1.80k
    { Hexagon::BI__builtin_HEXAGON_L2_loadri_pci,     {{ 1, true,  4,  2 }} },
2748
1.80k
    { Hexagon::BI__builtin_HEXAGON_L2_loadrd_pci,     {{ 1, true,  4,  3 }} },
2749
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_storerb_pci,    {{ 1, true,  4,  0 }} },
2750
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_storerh_pci,    {{ 1, true,  4,  1 }} },
2751
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_storerf_pci,    {{ 1, true,  4,  1 }} },
2752
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_storeri_pci,    {{ 1, true,  4,  2 }} },
2753
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_storerd_pci,    {{ 1, true,  4,  3 }} },
2754
2755
1.80k
    { Hexagon::BI__builtin_HEXAGON_A2_combineii,      {{ 1, true,  8,  0 }} },
2756
1.80k
    { Hexagon::BI__builtin_HEXAGON_A2_tfrih,          {{ 1, false, 16, 0 }} },
2757
1.80k
    { Hexagon::BI__builtin_HEXAGON_A2_tfril,          {{ 1, false, 16, 0 }} },
2758
1.80k
    { Hexagon::BI__builtin_HEXAGON_A2_tfrpi,          {{ 0, true,  8,  0 }} },
2759
1.80k
    { Hexagon::BI__builtin_HEXAGON_A4_bitspliti,      {{ 1, false, 5,  0 }} },
2760
1.80k
    { Hexagon::BI__builtin_HEXAGON_A4_cmpbeqi,        {{ 1, false, 8,  0 }} },
2761
1.80k
    { Hexagon::BI__builtin_HEXAGON_A4_cmpbgti,        {{ 1, true,  8,  0 }} },
2762
1.80k
    { Hexagon::BI__builtin_HEXAGON_A4_cround_ri,      {{ 1, false, 5,  0 }} },
2763
1.80k
    { Hexagon::BI__builtin_HEXAGON_A4_round_ri,       {{ 1, false, 5,  0 }} },
2764
1.80k
    { Hexagon::BI__builtin_HEXAGON_A4_round_ri_sat,   {{ 1, false, 5,  0 }} },
2765
1.80k
    { Hexagon::BI__builtin_HEXAGON_A4_vcmpbeqi,       {{ 1, false, 8,  0 }} },
2766
1.80k
    { Hexagon::BI__builtin_HEXAGON_A4_vcmpbgti,       {{ 1, true,  8,  0 }} },
2767
1.80k
    { Hexagon::BI__builtin_HEXAGON_A4_vcmpbgtui,      {{ 1, false, 7,  0 }} },
2768
1.80k
    { Hexagon::BI__builtin_HEXAGON_A4_vcmpheqi,       {{ 1, true,  8,  0 }} },
2769
1.80k
    { Hexagon::BI__builtin_HEXAGON_A4_vcmphgti,       {{ 1, true,  8,  0 }} },
2770
1.80k
    { Hexagon::BI__builtin_HEXAGON_A4_vcmphgtui,      {{ 1, false, 7,  0 }} },
2771
1.80k
    { Hexagon::BI__builtin_HEXAGON_A4_vcmpweqi,       {{ 1, true,  8,  0 }} },
2772
1.80k
    { Hexagon::BI__builtin_HEXAGON_A4_vcmpwgti,       {{ 1, true,  8,  0 }} },
2773
1.80k
    { Hexagon::BI__builtin_HEXAGON_A4_vcmpwgtui,      {{ 1, false, 7,  0 }} },
2774
1.80k
    { Hexagon::BI__builtin_HEXAGON_C2_bitsclri,       {{ 1, false, 6,  0 }} },
2775
1.80k
    { Hexagon::BI__builtin_HEXAGON_C2_muxii,          {{ 2, true,  8,  0 }} },
2776
1.80k
    { Hexagon::BI__builtin_HEXAGON_C4_nbitsclri,      {{ 1, false, 6,  0 }} },
2777
1.80k
    { Hexagon::BI__builtin_HEXAGON_F2_dfclass,        {{ 1, false, 5,  0 }} },
2778
1.80k
    { Hexagon::BI__builtin_HEXAGON_F2_dfimm_n,        {{ 0, false, 10, 0 }} },
2779
1.80k
    { Hexagon::BI__builtin_HEXAGON_F2_dfimm_p,        {{ 0, false, 10, 0 }} },
2780
1.80k
    { Hexagon::BI__builtin_HEXAGON_F2_sfclass,        {{ 1, false, 5,  0 }} },
2781
1.80k
    { Hexagon::BI__builtin_HEXAGON_F2_sfimm_n,        {{ 0, false, 10, 0 }} },
2782
1.80k
    { Hexagon::BI__builtin_HEXAGON_F2_sfimm_p,        {{ 0, false, 10, 0 }} },
2783
1.80k
    { Hexagon::BI__builtin_HEXAGON_M4_mpyri_addi,     {{ 2, false, 6,  0 }} },
2784
1.80k
    { Hexagon::BI__builtin_HEXAGON_M4_mpyri_addr_u2,  {{ 1, false, 6,  2 }} },
2785
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_addasl_rrri,    {{ 2, false, 3,  0 }} },
2786
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_asl_i_p_acc,    {{ 2, false, 6,  0 }} },
2787
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_asl_i_p_and,    {{ 2, false, 6,  0 }} },
2788
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_asl_i_p,        {{ 1, false, 6,  0 }} },
2789
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_asl_i_p_nac,    {{ 2, false, 6,  0 }} },
2790
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_asl_i_p_or,     {{ 2, false, 6,  0 }} },
2791
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_asl_i_p_xacc,   {{ 2, false, 6,  0 }} },
2792
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_asl_i_r_acc,    {{ 2, false, 5,  0 }} },
2793
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_asl_i_r_and,    {{ 2, false, 5,  0 }} },
2794
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_asl_i_r,        {{ 1, false, 5,  0 }} },
2795
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_asl_i_r_nac,    {{ 2, false, 5,  0 }} },
2796
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_asl_i_r_or,     {{ 2, false, 5,  0 }} },
2797
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_asl_i_r_sat,    {{ 1, false, 5,  0 }} },
2798
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_asl_i_r_xacc,   {{ 2, false, 5,  0 }} },
2799
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_asl_i_vh,       {{ 1, false, 4,  0 }} },
2800
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_asl_i_vw,       {{ 1, false, 5,  0 }} },
2801
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_asr_i_p_acc,    {{ 2, false, 6,  0 }} },
2802
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_asr_i_p_and,    {{ 2, false, 6,  0 }} },
2803
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_asr_i_p,        {{ 1, false, 6,  0 }} },
2804
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_asr_i_p_nac,    {{ 2, false, 6,  0 }} },
2805
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_asr_i_p_or,     {{ 2, false, 6,  0 }} },
2806
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_asr_i_p_rnd_goodsyntax,
2807
1.80k
                                                      {{ 1, false, 6,  0 }} },
2808
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_asr_i_p_rnd,    {{ 1, false, 6,  0 }} },
2809
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_asr_i_r_acc,    {{ 2, false, 5,  0 }} },
2810
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_asr_i_r_and,    {{ 2, false, 5,  0 }} },
2811
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_asr_i_r,        {{ 1, false, 5,  0 }} },
2812
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_asr_i_r_nac,    {{ 2, false, 5,  0 }} },
2813
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_asr_i_r_or,     {{ 2, false, 5,  0 }} },
2814
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_asr_i_r_rnd_goodsyntax,
2815
1.80k
                                                      {{ 1, false, 5,  0 }} },
2816
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_asr_i_r_rnd,    {{ 1, false, 5,  0 }} },
2817
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_asr_i_svw_trun, {{ 1, false, 5,  0 }} },
2818
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_asr_i_vh,       {{ 1, false, 4,  0 }} },
2819
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_asr_i_vw,       {{ 1, false, 5,  0 }} },
2820
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_clrbit_i,       {{ 1, false, 5,  0 }} },
2821
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_extractu,       {{ 1, false, 5,  0 },
2822
1.80k
                                                       { 2, false, 5,  0 }} },
2823
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_extractup,      {{ 1, false, 6,  0 },
2824
1.80k
                                                       { 2, false, 6,  0 }} },
2825
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_insert,         {{ 2, false, 5,  0 },
2826
1.80k
                                                       { 3, false, 5,  0 }} },
2827
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_insertp,        {{ 2, false, 6,  0 },
2828
1.80k
                                                       { 3, false, 6,  0 }} },
2829
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_p_acc,    {{ 2, false, 6,  0 }} },
2830
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_p_and,    {{ 2, false, 6,  0 }} },
2831
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_p,        {{ 1, false, 6,  0 }} },
2832
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_p_nac,    {{ 2, false, 6,  0 }} },
2833
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_p_or,     {{ 2, false, 6,  0 }} },
2834
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_p_xacc,   {{ 2, false, 6,  0 }} },
2835
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_r_acc,    {{ 2, false, 5,  0 }} },
2836
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_r_and,    {{ 2, false, 5,  0 }} },
2837
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_r,        {{ 1, false, 5,  0 }} },
2838
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_r_nac,    {{ 2, false, 5,  0 }} },
2839
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_r_or,     {{ 2, false, 5,  0 }} },
2840
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_r_xacc,   {{ 2, false, 5,  0 }} },
2841
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_vh,       {{ 1, false, 4,  0 }} },
2842
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_vw,       {{ 1, false, 5,  0 }} },
2843
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_setbit_i,       {{ 1, false, 5,  0 }} },
2844
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_tableidxb_goodsyntax,
2845
1.80k
                                                      {{ 2, false, 4,  0 },
2846
1.80k
                                                       { 3, false, 5,  0 }} },
2847
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_tableidxd_goodsyntax,
2848
1.80k
                                                      {{ 2, false, 4,  0 },
2849
1.80k
                                                       { 3, false, 5,  0 }} },
2850
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_tableidxh_goodsyntax,
2851
1.80k
                                                      {{ 2, false, 4,  0 },
2852
1.80k
                                                       { 3, false, 5,  0 }} },
2853
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_tableidxw_goodsyntax,
2854
1.80k
                                                      {{ 2, false, 4,  0 },
2855
1.80k
                                                       { 3, false, 5,  0 }} },
2856
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_togglebit_i,    {{ 1, false, 5,  0 }} },
2857
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_tstbit_i,       {{ 1, false, 5,  0 }} },
2858
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_valignib,       {{ 2, false, 3,  0 }} },
2859
1.80k
    { Hexagon::BI__builtin_HEXAGON_S2_vspliceib,      {{ 2, false, 3,  0 }} },
2860
1.80k
    { Hexagon::BI__builtin_HEXAGON_S4_addi_asl_ri,    {{ 2, false, 5,  0 }} },
2861
1.80k
    { Hexagon::BI__builtin_HEXAGON_S4_addi_lsr_ri,    {{ 2, false, 5,  0 }} },
2862
1.80k
    { Hexagon::BI__builtin_HEXAGON_S4_andi_asl_ri,    {{ 2, false, 5,  0 }} },
2863
1.80k
    { Hexagon::BI__builtin_HEXAGON_S4_andi_lsr_ri,    {{ 2, false, 5,  0 }} },
2864
1.80k
    { Hexagon::BI__builtin_HEXAGON_S4_clbaddi,        {{ 1, true , 6,  0 }} },
2865
1.80k
    { Hexagon::BI__builtin_HEXAGON_S4_clbpaddi,       {{ 1, true,  6,  0 }} },
2866
1.80k
    { Hexagon::BI__builtin_HEXAGON_S4_extract,        {{ 1, false, 5,  0 },
2867
1.80k
                                                       { 2, false, 5,  0 }} },
2868
1.80k
    { Hexagon::BI__builtin_HEXAGON_S4_extractp,       {{ 1, false, 6,  0 },
2869
1.80k
                                                       { 2, false, 6,  0 }} },
2870
1.80k
    { Hexagon::BI__builtin_HEXAGON_S4_lsli,           {{ 0, true,  6,  0 }} },
2871
1.80k
    { Hexagon::BI__builtin_HEXAGON_S4_ntstbit_i,      {{ 1, false, 5,  0 }} },
2872
1.80k
    { Hexagon::BI__builtin_HEXAGON_S4_ori_asl_ri,     {{ 2, false, 5,  0 }} },
2873
1.80k
    { Hexagon::BI__builtin_HEXAGON_S4_ori_lsr_ri,     {{ 2, false, 5,  0 }} },
2874
1.80k
    { Hexagon::BI__builtin_HEXAGON_S4_subi_asl_ri,    {{ 2, false, 5,  0 }} },
2875
1.80k
    { Hexagon::BI__builtin_HEXAGON_S4_subi_lsr_ri,    {{ 2, false, 5,  0 }} },
2876
1.80k
    { Hexagon::BI__builtin_HEXAGON_S4_vrcrotate_acc,  {{ 3, false, 2,  0 }} },
2877
1.80k
    { Hexagon::BI__builtin_HEXAGON_S4_vrcrotate,      {{ 2, false, 2,  0 }} },
2878
1.80k
    { Hexagon::BI__builtin_HEXAGON_S5_asrhub_rnd_sat_goodsyntax,
2879
1.80k
                                                      {{ 1, false, 4,  0 }} },
2880
1.80k
    { Hexagon::BI__builtin_HEXAGON_S5_asrhub_sat,     {{ 1, false, 4,  0 }} },
2881
1.80k
    { Hexagon::BI__builtin_HEXAGON_S5_vasrhrnd_goodsyntax,
2882
1.80k
                                                      {{ 1, false, 4,  0 }} },
2883
1.80k
    { Hexagon::BI__builtin_HEXAGON_S6_rol_i_p,        {{ 1, false, 6,  0 }} },
2884
1.80k
    { Hexagon::BI__builtin_HEXAGON_S6_rol_i_p_acc,    {{ 2, false, 6,  0 }} },
2885
1.80k
    { Hexagon::BI__builtin_HEXAGON_S6_rol_i_p_and,    {{ 2, false, 6,  0 }} },
2886
1.80k
    { Hexagon::BI__builtin_HEXAGON_S6_rol_i_p_nac,    {{ 2, false, 6,  0 }} },
2887
1.80k
    { Hexagon::BI__builtin_HEXAGON_S6_rol_i_p_or,     {{ 2, false, 6,  0 }} },
2888
1.80k
    { Hexagon::BI__builtin_HEXAGON_S6_rol_i_p_xacc,   {{ 2, false, 6,  0 }} },
2889
1.80k
    { Hexagon::BI__builtin_HEXAGON_S6_rol_i_r,        {{ 1, false, 5,  0 }} },
2890
1.80k
    { Hexagon::BI__builtin_HEXAGON_S6_rol_i_r_acc,    {{ 2, false, 5,  0 }} },
2891
1.80k
    { Hexagon::BI__builtin_HEXAGON_S6_rol_i_r_and,    {{ 2, false, 5,  0 }} },
2892
1.80k
    { Hexagon::BI__builtin_HEXAGON_S6_rol_i_r_nac,    {{ 2, false, 5,  0 }} },
2893
1.80k
    { Hexagon::BI__builtin_HEXAGON_S6_rol_i_r_or,     {{ 2, false, 5,  0 }} },
2894
1.80k
    { Hexagon::BI__builtin_HEXAGON_S6_rol_i_r_xacc,   {{ 2, false, 5,  0 }} },
2895
1.80k
    { Hexagon::BI__builtin_HEXAGON_V6_valignbi,       {{ 2, false, 3,  0 }} },
2896
1.80k
    { Hexagon::BI__builtin_HEXAGON_V6_valignbi_128B,  {{ 2, false, 3,  0 }} },
2897
1.80k
    { Hexagon::BI__builtin_HEXAGON_V6_vlalignbi,      {{ 2, false, 3,  0 }} },
2898
1.80k
    { Hexagon::BI__builtin_HEXAGON_V6_vlalignbi_128B, {{ 2, false, 3,  0 }} },
2899
1.80k
    { Hexagon::BI__builtin_HEXAGON_V6_vrmpybusi,      {{ 2, false, 1,  0 }} },
2900
1.80k
    { Hexagon::BI__builtin_HEXAGON_V6_vrmpybusi_128B, {{ 2, false, 1,  0 }} },
2901
1.80k
    { Hexagon::BI__builtin_HEXAGON_V6_vrmpybusi_acc,  {{ 3, false, 1,  0 }} },
2902
1.80k
    { Hexagon::BI__builtin_HEXAGON_V6_vrmpybusi_acc_128B,
2903
1.80k
                                                      {{ 3, false, 1,  0 }} },
2904
1.80k
    { Hexagon::BI__builtin_HEXAGON_V6_vrmpyubi,       {{ 2, false, 1,  0 }} },
2905
1.80k
    { Hexagon::BI__builtin_HEXAGON_V6_vrmpyubi_128B,  {{ 2, false, 1,  0 }} },
2906
1.80k
    { Hexagon::BI__builtin_HEXAGON_V6_vrmpyubi_acc,   {{ 3, false, 1,  0 }} },
2907
1.80k
    { Hexagon::BI__builtin_HEXAGON_V6_vrmpyubi_acc_128B,
2908
1.80k
                                                      {{ 3, false, 1,  0 }} },
2909
1.80k
    { Hexagon::BI__builtin_HEXAGON_V6_vrsadubi,       {{ 2, false, 1,  0 }} },
2910
1.80k
    { Hexagon::BI__builtin_HEXAGON_V6_vrsadubi_128B,  {{ 2, false, 1,  0 }} },
2911
1.80k
    { Hexagon::BI__builtin_HEXAGON_V6_vrsadubi_acc,   {{ 3, false, 1,  0 }} },
2912
1.80k
    { Hexagon::BI__builtin_HEXAGON_V6_vrsadubi_acc_128B,
2913
1.80k
                                                      {{ 3, false, 1,  0 }} },
2914
1.80k
  };
2915
2916
  // Use a dynamically initialized static to sort the table exactly once on
2917
  // first run.
2918
1.80k
  static const bool SortOnce =
2919
1.80k
      (llvm::sort(Infos,
2920
36.5k
                 [](const BuiltinInfo &LHS, const BuiltinInfo &RHS) {
2921
36.5k
                   return LHS.BuiltinID < RHS.BuiltinID;
2922
36.5k
                 }),
2923
1.80k
       true);
2924
1.80k
  (void)SortOnce;
2925
2926
1.80k
  const BuiltinInfo *F = llvm::partition_point(
2927
13.0k
      Infos, [=](const BuiltinInfo &BI) { return BI.BuiltinID < BuiltinID; });
2928
1.80k
  if (F == std::end(Infos) || 
F->BuiltinID != BuiltinID1.14k
)
2929
1.63k
    return false;
2930
2931
174
  bool Error = false;
2932
2933
348
  for (const ArgInfo &A : F->Infos) {
2934
    // Ignore empty ArgInfo elements.
2935
348
    if (A.BitWidth == 0)
2936
159
      continue;
2937
2938
189
    int32_t Min = A.IsSigned ? 
-(1 << (A.BitWidth - 1))39
:
0150
;
2939
150
    int32_t Max = (1 << (A.IsSigned ? 
A.BitWidth - 139
: A.BitWidth)) - 1;
2940
189
    if (!A.Align) {
2941
167
      Error |= SemaBuiltinConstantArgRange(TheCall, A.OpNum, Min, Max);
2942
22
    } else {
2943
22
      unsigned M = 1 << A.Align;
2944
22
      Min *= M;
2945
22
      Max *= M;
2946
22
      Error |= SemaBuiltinConstantArgRange(TheCall, A.OpNum, Min, Max) |
2947
22
               SemaBuiltinConstantArgMultiple(TheCall, A.OpNum, M);
2948
22
    }
2949
189
  }
2950
174
  return Error;
2951
174
}
2952
2953
bool Sema::CheckHexagonBuiltinFunctionCall(unsigned BuiltinID,
2954
1.80k
                                           CallExpr *TheCall) {
2955
1.80k
  return CheckHexagonBuiltinArgument(BuiltinID, TheCall);
2956
1.80k
}
2957
2958
bool Sema::CheckMipsBuiltinFunctionCall(const TargetInfo &TI,
2959
1.05k
                                        unsigned BuiltinID, CallExpr *TheCall) {
2960
1.05k
  return CheckMipsBuiltinCpu(TI, BuiltinID, TheCall) ||
2961
1.04k
         CheckMipsBuiltinArgument(BuiltinID, TheCall);
2962
1.05k
}
2963
2964
bool Sema::CheckMipsBuiltinCpu(const TargetInfo &TI, unsigned BuiltinID,
2965
1.05k
                               CallExpr *TheCall) {
2966
2967
1.05k
  if (Mips::BI__builtin_mips_addu_qb <= BuiltinID &&
2968
1.05k
      BuiltinID <= Mips::BI__builtin_mips_lwx) {
2969
102
    if (!TI.hasFeature("dsp"))
2970
2
      return Diag(TheCall->getBeginLoc(), diag::err_mips_builtin_requires_dsp);
2971
1.04k
  }
2972
2973
1.04k
  if (Mips::BI__builtin_mips_absq_s_qb <= BuiltinID &&
2974
949
      BuiltinID <= Mips::BI__builtin_mips_subuh_r_qb) {
2975
58
    if (!TI.hasFeature("dspr2"))
2976
2
      return Diag(TheCall->getBeginLoc(),
2977
2
                  diag::err_mips_builtin_requires_dspr2);
2978
1.04k
  }
2979
2980
1.04k
  if (Mips::BI__builtin_msa_add_a_b <= BuiltinID &&
2981
891
      BuiltinID <= Mips::BI__builtin_msa_xori_b) {
2982
891
    if (!TI.hasFeature("msa"))
2983
2
      return Diag(TheCall->getBeginLoc(), diag::err_mips_builtin_requires_msa);
2984
1.04k
  }
2985
2986
1.04k
  return false;
2987
1.04k
}
2988
2989
// CheckMipsBuiltinArgument - Checks the constant value passed to the
2990
// intrinsic is correct. The switch statement is ordered by DSP, MSA. The
2991
// ordering for DSP is unspecified. MSA is ordered by the data format used
2992
// by the underlying instruction i.e., df/m, df/n and then by size.
2993
//
2994
// FIXME: The size tests here should instead be tablegen'd along with the
2995
//        definitions from include/clang/Basic/BuiltinsMips.def.
2996
// FIXME: GCC is strict on signedness for some of these intrinsics, we should
2997
//        be too.
2998
1.04k
bool Sema::CheckMipsBuiltinArgument(unsigned BuiltinID, CallExpr *TheCall) {
2999
1.04k
  unsigned i = 0, l = 0, u = 0, m = 0;
3000
1.04k
  switch (BuiltinID) {
3001
556
  default: return false;
3002
5
  case Mips::BI__builtin_mips_wrdsp: i = 1; l = 0; u = 63; break;
3003
3
  case Mips::BI__builtin_mips_rddsp: i = 0; l = 0; u = 63; break;
3004
3
  case Mips::BI__builtin_mips_append: i = 2; l = 0; u = 31; break;
3005
3
  case Mips::BI__builtin_mips_balign: i = 2; l = 0; u = 3; break;
3006
3
  case Mips::BI__builtin_mips_precr_sra_ph_w: i = 2; l = 0; u = 31; break;
3007
3
  case Mips::BI__builtin_mips_precr_sra_r_ph_w: i = 2; l = 0; u = 31; break;
3008
3
  case Mips::BI__builtin_mips_prepend: i = 2; l = 0; u = 31; break;
3009
  // MSA intrinsics. Instructions (which the intrinsics maps to) which use the
3010
  // df/m field.
3011
  // These intrinsics take an unsigned 3 bit immediate.
3012
3
  case Mips::BI__builtin_msa_bclri_b:
3013
6
  case Mips::BI__builtin_msa_bnegi_b:
3014
9
  case Mips::BI__builtin_msa_bseti_b:
3015
12
  case Mips::BI__builtin_msa_sat_s_b:
3016
15
  case Mips::BI__builtin_msa_sat_u_b:
3017
18
  case Mips::BI__builtin_msa_slli_b:
3018
21
  case Mips::BI__builtin_msa_srai_b:
3019
24
  case Mips::BI__builtin_msa_srari_b:
3020
27
  case Mips::BI__builtin_msa_srli_b:
3021
30
  case Mips::BI__builtin_msa_srlri_b: i = 1; l = 0; u = 7; break;
3022
3
  case Mips::BI__builtin_msa_binsli_b:
3023
6
  case Mips::BI__builtin_msa_binsri_b: i = 2; l = 0; u = 7; break;
3024
  // These intrinsics take an unsigned 4 bit immediate.
3025
3
  case Mips::BI__builtin_msa_bclri_h:
3026
6
  case Mips::BI__builtin_msa_bnegi_h:
3027
9
  case Mips::BI__builtin_msa_bseti_h:
3028
12
  case Mips::BI__builtin_msa_sat_s_h:
3029
15
  case Mips::BI__builtin_msa_sat_u_h:
3030
18
  case Mips::BI__builtin_msa_slli_h:
3031
21
  case Mips::BI__builtin_msa_srai_h:
3032
24
  case Mips::BI__builtin_msa_srari_h:
3033
27
  case Mips::BI__builtin_msa_srli_h:
3034
30
  case Mips::BI__builtin_msa_srlri_h: i = 1; l = 0; u = 15; break;
3035
3
  case Mips::BI__builtin_msa_binsli_h:
3036
6
  case Mips::BI__builtin_msa_binsri_h: i = 2; l = 0; u = 15; break;
3037
  // These intrinsics take an unsigned 5 bit immediate.
3038
  // The first block of intrinsics actually have an unsigned 5 bit field,
3039
  // not a df/n field.
3040
2
  case Mips::BI__builtin_msa_cfcmsa:
3041
4
  case Mips::BI__builtin_msa_ctcmsa: i = 0; l = 0; u = 31; break;
3042
3
  case Mips::BI__builtin_msa_clei_u_b:
3043
6
  case Mips::BI__builtin_msa_clei_u_h:
3044
9
  case Mips::BI__builtin_msa_clei_u_w:
3045
12
  case Mips::BI__builtin_msa_clei_u_d:
3046
15
  case Mips::BI__builtin_msa_clti_u_b:
3047
18
  case Mips::BI__builtin_msa_clti_u_h:
3048
21
  case Mips::BI__builtin_msa_clti_u_w:
3049
24
  case Mips::BI__builtin_msa_clti_u_d:
3050
27
  case Mips::BI__builtin_msa_maxi_u_b:
3051
30
  case Mips::BI__builtin_msa_maxi_u_h:
3052
33
  case Mips::BI__builtin_msa_maxi_u_w:
3053
36
  case Mips::BI__builtin_msa_maxi_u_d:
3054
39
  case Mips::BI__builtin_msa_mini_u_b:
3055
42
  case Mips::BI__builtin_msa_mini_u_h:
3056
45
  case Mips::BI__builtin_msa_mini_u_w:
3057
48
  case Mips::BI__builtin_msa_mini_u_d:
3058
52
  case Mips::BI__builtin_msa_addvi_b:
3059
56
  case Mips::BI__builtin_msa_addvi_h:
3060
60
  case Mips::BI__builtin_msa_addvi_w:
3061
64
  case Mips::BI__builtin_msa_addvi_d:
3062
67
  case Mips::BI__builtin_msa_bclri_w:
3063
70
  case Mips::BI__builtin_msa_bnegi_w:
3064
73
  case Mips::BI__builtin_msa_bseti_w:
3065
76
  case Mips::BI__builtin_msa_sat_s_w:
3066
79
  case Mips::BI__builtin_msa_sat_u_w:
3067
82
  case Mips::BI__builtin_msa_slli_w:
3068
85
  case Mips::BI__builtin_msa_srai_w:
3069
88
  case Mips::BI__builtin_msa_srari_w:
3070
91
  case Mips::BI__builtin_msa_srli_w:
3071
94
  case Mips::BI__builtin_msa_srlri_w:
3072
97
  case Mips::BI__builtin_msa_subvi_b:
3073
100
  case Mips::BI__builtin_msa_subvi_h:
3074
103
  case Mips::BI__builtin_msa_subvi_w:
3075
106
  case Mips::BI__builtin_msa_subvi_d: i = 1; l = 0; u = 31; break;
3076
3
  case Mips::BI__builtin_msa_binsli_w:
3077
6
  case Mips::BI__builtin_msa_binsri_w: i = 2; l = 0; u = 31; break;
3078
  // These intrinsics take an unsigned 6 bit immediate.
3079
3
  case Mips::BI__builtin_msa_bclri_d:
3080
6
  case Mips::BI__builtin_msa_bnegi_d:
3081
9
  case Mips::BI__builtin_msa_bseti_d:
3082
12
  case Mips::BI__builtin_msa_sat_s_d:
3083
15
  case Mips::BI__builtin_msa_sat_u_d:
3084
18
  case Mips::BI__builtin_msa_slli_d:
3085
21
  case Mips::BI__builtin_msa_srai_d:
3086
24
  case Mips::BI__builtin_msa_srari_d:
3087
27
  case Mips::BI__builtin_msa_srli_d:
3088
30
  case Mips::BI__builtin_msa_srlri_d: i = 1; l = 0; u = 63; break;
3089
3
  case Mips::BI__builtin_msa_binsli_d:
3090
6
  case Mips::BI__builtin_msa_binsri_d: i = 2; l = 0; u = 63; break;
3091
  // These intrinsics take a signed 5 bit immediate.
3092
3
  case Mips::BI__builtin_msa_ceqi_b:
3093
6
  case Mips::BI__builtin_msa_ceqi_h:
3094
9
  case Mips::BI__builtin_msa_ceqi_w:
3095
12
  case Mips::BI__builtin_msa_ceqi_d:
3096
15
  case Mips::BI__builtin_msa_clti_s_b:
3097
18
  case Mips::BI__builtin_msa_clti_s_h:
3098
21
  case Mips::BI__builtin_msa_clti_s_w:
3099
24
  case Mips::BI__builtin_msa_clti_s_d:
3100
27
  case Mips::BI__builtin_msa_clei_s_b:
3101
30
  case Mips::BI__builtin_msa_clei_s_h:
3102
33
  case Mips::BI__builtin_msa_clei_s_w:
3103
36
  case Mips::BI__builtin_msa_clei_s_d:
3104
39
  case Mips::BI__builtin_msa_maxi_s_b:
3105
42
  case Mips::BI__builtin_msa_maxi_s_h:
3106
45
  case Mips::BI__builtin_msa_maxi_s_w:
3107
48
  case Mips::BI__builtin_msa_maxi_s_d:
3108
51
  case Mips::BI__builtin_msa_mini_s_b:
3109
54
  case Mips::BI__builtin_msa_mini_s_h:
3110
57
  case Mips::BI__builtin_msa_mini_s_w:
3111
60
  case Mips::BI__builtin_msa_mini_s_d: i = 1; l = -16; u = 15; break;
3112
  // These intrinsics take an unsigned 8 bit immediate.
3113
16
  case Mips::BI__builtin_msa_andi_b:
3114
26
  case Mips::BI__builtin_msa_nori_b:
3115
36
  case Mips::BI__builtin_msa_ori_b:
3116
39
  case Mips::BI__builtin_msa_shf_b:
3117
42
  case Mips::BI__builtin_msa_shf_h:
3118
45
  case Mips::BI__builtin_msa_shf_w:
3119
69
  case Mips::BI__builtin_msa_xori_b: i = 1; l = 0; u = 255; break;
3120
3
  case Mips::BI__builtin_msa_bseli_b:
3121
6
  case Mips::BI__builtin_msa_bmnzi_b:
3122
9
  case Mips::BI__builtin_msa_bmzi_b: i = 2; l = 0; u = 255; break;
3123
  // df/n format
3124
  // These intrinsics take an unsigned 4 bit immediate.
3125
3
  case Mips::BI__builtin_msa_copy_s_b:
3126
6
  case Mips::BI__builtin_msa_copy_u_b:
3127
9
  case Mips::BI__builtin_msa_insve_b:
3128
12
  case Mips::BI__builtin_msa_splati_b: i = 1; l = 0; u = 15; break;
3129
3
  case Mips::BI__builtin_msa_sldi_b: i = 2; l = 0; u = 15; break;
3130
  // These intrinsics take an unsigned 3 bit immediate.
3131
3
  case Mips::BI__builtin_msa_copy_s_h:
3132
6
  case Mips::BI__builtin_msa_copy_u_h:
3133
9
  case Mips::BI__builtin_msa_insve_h:
3134
12
  case Mips::BI__builtin_msa_splati_h: i = 1; l = 0; u = 7; break;
3135
3
  case Mips::BI__builtin_msa_sldi_h: i = 2; l = 0; u = 7; break;
3136
  // These intrinsics take an unsigned 2 bit immediate.
3137
3
  case Mips::BI__builtin_msa_copy_s_w:
3138
6
  case Mips::BI__builtin_msa_copy_u_w:
3139
9
  case Mips::BI__builtin_msa_insve_w:
3140
12
  case Mips::BI__builtin_msa_splati_w: i = 1; l = 0; u = 3; break;
3141
3
  case Mips::BI__builtin_msa_sldi_w: i = 2; l = 0; u = 3; break;
3142
  // These intrinsics take an unsigned 1 bit immediate.
3143
3
  case Mips::BI__builtin_msa_copy_s_d:
3144
6
  case Mips::BI__builtin_msa_copy_u_d:
3145
9
  case Mips::BI__builtin_msa_insve_d:
3146
12
  case Mips::BI__builtin_msa_splati_d: i = 1; l = 0; u = 1; break;
3147
3
  case Mips::BI__builtin_msa_sldi_d: i = 2; l = 0; u = 1; break;
3148
  // Memory offsets and immediate loads.
3149
  // These intrinsics take a signed 10 bit immediate.
3150
5
  case Mips::BI__builtin_msa_ldi_b: i = 0; l = -128; u = 255; break;
3151
3
  case Mips::BI__builtin_msa_ldi_h:
3152
6
  case Mips::BI__builtin_msa_ldi_w:
3153
9
  case Mips::BI__builtin_msa_ldi_d: i = 0; l = -512; u = 511; break;
3154
3
  case Mips::BI__builtin_msa_ld_b: i = 1; l = -512; u = 511; m = 1; break;
3155
4
  case Mips::BI__builtin_msa_ld_h: i = 1; l = -1024; u = 1022; m = 2; break;
3156
4
  case Mips::BI__builtin_msa_ld_w: i = 1; l = -2048; u = 2044; m = 4; break;
3157
4
  case Mips::BI__builtin_msa_ld_d: i = 1; l = -4096; u = 4088; m = 8; break;
3158
0
  case Mips::BI__builtin_msa_ldr_d: i = 1; l = -4096; u = 4088; m = 8; break;
3159
0
  case Mips::BI__builtin_msa_ldr_w: i = 1; l = -2048; u = 2044; m = 4; break;
3160
3
  case Mips::BI__builtin_msa_st_b: i = 2; l = -512; u = 511; m = 1; break;
3161
4
  case Mips::BI__builtin_msa_st_h: i = 2; l = -1024; u = 1022; m = 2; break;
3162
4
  case Mips::BI__builtin_msa_st_w: i = 2; l = -2048; u = 2044; m = 4; break;
3163
4
  case Mips::BI__builtin_msa_st_d: i = 2; l = -4096; u = 4088; m = 8; break;
3164
0
  case Mips::BI__builtin_msa_str_d: i = 2; l = -4096; u = 4088; m = 8; break;
3165
0
  case Mips::BI__builtin_msa_str_w: i = 2; l = -2048; u = 2044; m = 4; break;
3166
489
  }
3167
3168
489
  if (!m)
3169
459
    return SemaBuiltinConstantArgRange(TheCall, i, l, u);
3170
3171
30
  return SemaBuiltinConstantArgRange(TheCall, i, l, u) ||
3172
14
         SemaBuiltinConstantArgMultiple(TheCall, i, m);
3173
30
}
3174
3175
/// DecodePPCMMATypeFromStr - This decodes one PPC MMA type descriptor from Str,
3176
/// advancing the pointer over the consumed characters. The decoded type is
3177
/// returned. If the decoded type represents a constant integer with a
3178
/// constraint on its value then Mask is set to that value. The type descriptors
3179
/// used in Str are specific to PPC MMA builtins and are documented in the file
3180
/// defining the PPC builtins.
3181
static QualType DecodePPCMMATypeFromStr(ASTContext &Context, const char *&Str,
3182
441
                                        unsigned &Mask) {
3183
441
  bool RequireICE = false;
3184
441
  ASTContext::GetBuiltinTypeError Error = ASTContext::GE_None;
3185
441
  switch (*Str++) {
3186
122
  case 'V':
3187
122
    return Context.getVectorType(Context.UnsignedCharTy, 16,
3188
122
                                 VectorType::VectorKind::AltiVecVector);
3189
79
  case 'i': {
3190
79
    char *End;
3191
79
    unsigned size = strtoul(Str, &End, 10);
3192
79
    assert(End != Str && "Missing constant parameter constraint");
3193
79
    Str = End;
3194
79
    Mask = size;
3195
79
    return Context.IntTy;
3196
0
  }
3197
131
  case 'W': {
3198
131
    char *End;
3199
131
    unsigned size = strtoul(Str, &End, 10);
3200
131
    assert(End != Str && "Missing PowerPC MMA type size");
3201
131
    Str = End;
3202
131
    QualType Type;
3203
131
    switch (size) {
3204
0
  #define PPC_VECTOR_TYPE(typeName, Id, size) \
3205
131
    case size: Type = Context.Id##Ty; break;
3206
0
  #include "clang/Basic/PPCTypes.def"
3207
0
    default: llvm_unreachable("Invalid PowerPC MMA vector type");
3208
131
    }
3209
131
    bool CheckVectorArgs = false;
3210
376
    while (!CheckVectorArgs) {
3211
245
      switch (*Str++) {
3212
94
      case '*':
3213
94
        Type = Context.getPointerType(Type);
3214
94
        break;
3215
20
      case 'C':
3216
20
        Type = Type.withConst();
3217
20
        break;
3218
131
      default:
3219
131
        CheckVectorArgs = true;
3220
131
        --Str;
3221
131
        break;
3222
245
      }
3223
245
    }
3224
131
    return Type;
3225
131
  }
3226
109
  default:
3227
109
    return Context.DecodeTypeStr(--Str, Context, Error, RequireICE, true);
3228
441
  }
3229
441
}
3230
3231
bool Sema::CheckPPCBuiltinFunctionCall(const TargetInfo &TI, unsigned BuiltinID,
3232
82.7k
                                       CallExpr *TheCall) {
3233
82.7k
  unsigned i = 0, l = 0, u = 0;
3234
82.7k
  bool Is64BitBltin = BuiltinID == PPC::BI__builtin_divde ||
3235
82.7k
                      BuiltinID == PPC::BI__builtin_divdeu ||
3236
82.7k
                      BuiltinID == PPC::BI__builtin_bpermd;
3237
82.7k
  bool IsTarget64Bit = TI.getTypeWidth(TI.getIntPtrType()) == 64;
3238
82.7k
  bool IsBltinExtDiv = BuiltinID == PPC::BI__builtin_divwe ||
3239
82.7k
                       BuiltinID == PPC::BI__builtin_divweu ||
3240
82.7k
                       BuiltinID == PPC::BI__builtin_divde ||
3241
82.6k
                       BuiltinID == PPC::BI__builtin_divdeu;
3242
3243
82.7k
  if (Is64BitBltin && 
!IsTarget64Bit25
)
3244
3
    return Diag(TheCall->getBeginLoc(), diag::err_64_bit_builtin_32_bit_tgt)
3245
3
           << TheCall->getSourceRange();
3246
3247
82.7k
  if ((IsBltinExtDiv && 
!TI.hasFeature("extdiv")14
) ||
3248
82.7k
      (BuiltinID == PPC::BI__builtin_bpermd && 
!TI.hasFeature("bpermd")16
))
3249
5
    return Diag(TheCall->getBeginLoc(), diag::err_ppc_builtin_only_on_pwr7)
3250
5
           << TheCall->getSourceRange();
3251
3252
82.7k
  auto SemaVSXCheck = [&](CallExpr *TheCall) -> bool {
3253
16
    if (!TI.hasFeature("vsx"))
3254
2
      return Diag(TheCall->getBeginLoc(), diag::err_ppc_builtin_only_on_pwr7)
3255
2
             << TheCall->getSourceRange();
3256
14
    return false;
3257
14
  };
3258
3259
82.7k
  switch (BuiltinID) {
3260
82.2k
  default: return false;
3261
11
  case PPC::BI__builtin_altivec_crypto_vshasigmaw:
3262
22
  case PPC::BI__builtin_altivec_crypto_vshasigmad:
3263
22
    return SemaBuiltinConstantArgRange(TheCall, 1, 0, 1) ||
3264
18
           SemaBuiltinConstantArgRange(TheCall, 2, 0, 15);
3265
8
  case PPC::BI__builtin_altivec_dss:
3266
8
    return SemaBuiltinConstantArgRange(TheCall, 0, 0, 3);
3267
7
  case PPC::BI__builtin_tbegin:
3268
11
  case PPC::BI__builtin_tend: i = 0; l = 0; u = 1; break;
3269
2
  case PPC::BI__builtin_tsr: i = 0; l = 0; u = 7; break;
3270
2
  case PPC::BI__builtin_tabortwc:
3271
4
  case PPC::BI__builtin_tabortdc: i = 0; l = 0; u = 31; break;
3272
3
  case PPC::BI__builtin_tabortwci:
3273
6
  case PPC::BI__builtin_tabortdci:
3274
6
    return SemaBuiltinConstantArgRange(TheCall, 0, 0, 31) ||
3275
4
           SemaBuiltinConstantArgRange(TheCall, 2, 0, 31);
3276
7
  case PPC::BI__builtin_altivec_dst:
3277
14
  case PPC::BI__builtin_altivec_dstt:
3278
21
  case PPC::BI__builtin_altivec_dstst:
3279
28
  case PPC::BI__builtin_altivec_dststt:
3280
28
    return SemaBuiltinConstantArgRange(TheCall, 2, 0, 3);
3281
81
  case PPC::BI__builtin_vsx_xxpermdi:
3282
134
  case PPC::BI__builtin_vsx_xxsldwi:
3283
134
    return SemaBuiltinVSX(TheCall);
3284
12
  case PPC::BI__builtin_unpack_vector_int128:
3285
12
    return SemaVSXCheck(TheCall) ||
3286
11
           SemaBuiltinConstantArgRange(TheCall, 1, 0, 1);
3287
4
  case PPC::BI__builtin_pack_vector_int128:
3288
4
    return SemaVSXCheck(TheCall);
3289
6
  case PPC::BI__builtin_altivec_vgnb:
3290
6
     return SemaBuiltinConstantArgRange(TheCall, 1, 2, 7);
3291
26
  case PPC::BI__builtin_altivec_vec_replace_elt:
3292
50
  case PPC::BI__builtin_altivec_vec_replace_unaligned: {
3293
50
    QualType VecTy = TheCall->getArg(0)->getType();
3294
50
    QualType EltTy = TheCall->getArg(1)->getType();
3295
50
    unsigned Width = Context.getIntWidth(EltTy);
3296
50
    return SemaBuiltinConstantArgRange(TheCall, 2, 0, Width == 32 ? 
1226
:
824
) ||
3297
36
           !isEltOfVectorTy(Context, TheCall, *this, VecTy, EltTy);
3298
26
  }
3299
50
  case PPC::BI__builtin_vsx_xxeval:
3300
50
     return SemaBuiltinConstantArgRange(TheCall, 3, 0, 255);
3301
16
  case PPC::BI__builtin_altivec_vsldbi:
3302
16
     return SemaBuiltinConstantArgRange(TheCall, 2, 0, 7);
3303
16
  case PPC::BI__builtin_altivec_vsrdbi:
3304
16
     return SemaBuiltinConstantArgRange(TheCall, 2, 0, 7);
3305
20
  case PPC::BI__builtin_vsx_xxpermx:
3306
20
     return SemaBuiltinConstantArgRange(TheCall, 3, 0, 7);
3307
26
#define CUSTOM_BUILTIN(Name, Types, Acc) \
3308
97
  case PPC::BI__builtin_##Name: \
3309
97
    return SemaBuiltinPPCMMACall(TheCall, Types);
3310
26
#include "clang/Basic/BuiltinsPPC.def"
3311
82.7k
  }
3312
17
  return SemaBuiltinConstantArgRange(TheCall, i, l, u);
3313
82.7k
}
3314
3315
// Check if the given type is a non-pointer PPC MMA type. This function is used
3316
// in Sema to prevent invalid uses of restricted PPC MMA types.
3317
616k
bool Sema::CheckPPCMMAType(QualType Type, SourceLocation TypeLoc) {
3318
616k
  if (Type->isPointerType() || 
Type->isArrayType()587k
)
3319
31.0k
    return false;
3320
3321
584k
  QualType CoreType = Type.getCanonicalType().getUnqualifiedType();
3322
1.16M
#define PPC_VECTOR_TYPE(Name, Id, Size) || 
CoreType == Context.Id1.16M
##Ty
3323
584k
  if (false
3324
584k
#include "clang/Basic/PPCTypes.def"
3325
64
     ) {
3326
64
    Diag(TypeLoc, diag::err_ppc_invalid_use_mma_type);
3327
64
    return true;
3328
64
  }
3329
584k
  return false;
3330
584k
}
3331
3332
bool Sema::CheckAMDGCNBuiltinFunctionCall(unsigned BuiltinID,
3333
443
                                          CallExpr *TheCall) {
3334
  // position of memory order and scope arguments in the builtin
3335
443
  unsigned OrderIndex, ScopeIndex;
3336
443
  switch (BuiltinID) {
3337
14
  case AMDGPU::BI__builtin_amdgcn_atomic_inc32:
3338
28
  case AMDGPU::BI__builtin_amdgcn_atomic_inc64:
3339
46
  case AMDGPU::BI__builtin_amdgcn_atomic_dec32:
3340
64
  case AMDGPU::BI__builtin_amdgcn_atomic_dec64:
3341
64
    OrderIndex = 2;
3342
64
    ScopeIndex = 3;
3343
64
    break;
3344
11
  case AMDGPU::BI__builtin_amdgcn_fence:
3345
11
    OrderIndex = 0;
3346
11
    ScopeIndex = 1;
3347
11
    break;
3348
368
  default:
3349
368
    return false;
3350
75
  }
3351
3352
75
  ExprResult Arg = TheCall->getArg(OrderIndex);
3353
75
  auto ArgExpr = Arg.get();
3354
75
  Expr::EvalResult ArgResult;
3355
3356
75
  if (!ArgExpr->EvaluateAsInt(ArgResult, Context))
3357
0
    return Diag(ArgExpr->getExprLoc(), diag::err_typecheck_expect_int)
3358
0
           << ArgExpr->getType();
3359
75
  int ord = ArgResult.Val.getInt().getZExtValue();
3360
3361
  // Check valididty of memory ordering as per C11 / C++11's memody model.
3362
75
  switch (static_cast<llvm::AtomicOrderingCABI>(ord)) {
3363
17
  case llvm::AtomicOrderingCABI::acquire:
3364
25
  case llvm::AtomicOrderingCABI::release:
3365
28
  case llvm::AtomicOrderingCABI::acq_rel:
3366
65
  case llvm::AtomicOrderingCABI::seq_cst:
3367
65
    break;
3368
10
  default: {
3369
10
    return Diag(ArgExpr->getBeginLoc(),
3370
10
                diag::warn_atomic_op_has_invalid_memory_order)
3371
10
           << ArgExpr->getSourceRange();
3372
65
  }
3373
65
  }
3374
3375
65
  Arg = TheCall->getArg(ScopeIndex);
3376
65
  ArgExpr = Arg.get();
3377
65
  Expr::EvalResult ArgResult1;
3378
  // Check that sync scope is a constant literal
3379
65
  if (!ArgExpr->EvaluateAsConstantExpr(ArgResult1, Context))
3380
5
    return Diag(ArgExpr->getExprLoc(), diag::err_expr_not_string_literal)
3381
5
           << ArgExpr->getType();
3382
3383
60
  return false;
3384
60
}
3385
3386
bool Sema::CheckSystemZBuiltinFunctionCall(unsigned BuiltinID,
3387
16.2k
                                           CallExpr *TheCall) {
3388
16.2k
  if (BuiltinID == SystemZ::BI__builtin_tabort) {
3389
12
    Expr *Arg = TheCall->getArg(0);
3390
12
    if (Optional<llvm::APSInt> AbortCode = Arg->getIntegerConstantExpr(Context))
3391
8
      if (AbortCode->getSExtValue() >= 0 && 
AbortCode->getSExtValue() < 2566
)
3392
2
        return Diag(Arg->getBeginLoc(), diag::err_systemz_invalid_tabort_code)
3393
2
               << Arg->getSourceRange();
3394
16.2k
  }
3395
3396
  // For intrinsics which take an immediate value as part of the instruction,
3397
  // range check them here.
3398
16.2k
  unsigned i = 0, l = 0, u = 0;
3399
16.2k
  switch (BuiltinID) {
3400
13.0k
  default: return false;
3401
22
  case SystemZ::BI__builtin_s390_lcbb: i = 1; l = 0; u = 15; break;
3402
36
  case SystemZ::BI__builtin_s390_verimb:
3403
72
  case SystemZ::BI__builtin_s390_verimh:
3404
108
  case SystemZ::BI__builtin_s390_verimf:
3405
144
  case SystemZ::BI__builtin_s390_verimg: i = 3; l = 0; u = 255; break;
3406
184
  case SystemZ::BI__builtin_s390_vfaeb:
3407
368
  case SystemZ::BI__builtin_s390_vfaeh:
3408
552
  case SystemZ::BI__builtin_s390_vfaef:
3409
736
  case SystemZ::BI__builtin_s390_vfaebs:
3410
920
  case SystemZ::BI__builtin_s390_vfaehs:
3411
1.10k
  case SystemZ::BI__builtin_s390_vfaefs:
3412
1.19k
  case SystemZ::BI__builtin_s390_vfaezb:
3413
1.29k
  case SystemZ::BI__builtin_s390_vfaezh:
3414
1.38k
  case SystemZ::BI__builtin_s390_vfaezf:
3415
1.48k
  case SystemZ::BI__builtin_s390_vfaezbs:
3416
1.57k
  case SystemZ::BI__builtin_s390_vfaezhs:
3417
1.66k
  case SystemZ::BI__builtin_s390_vfaezfs: i = 2; l = 0; u = 15; break;
3418
107
  case SystemZ::BI__builtin_s390_vfisb:
3419
260
  case SystemZ::BI__builtin_s390_vfidb:
3420
260
    return SemaBuiltinConstantArgRange(TheCall, 1, 0, 15) ||
3421
256
           SemaBuiltinConstantArgRange(TheCall, 2, 0, 15);
3422
64
  case SystemZ::BI__builtin_s390_vftcisb:
3423
196
  case SystemZ::BI__builtin_s390_vftcidb: i = 1; l = 0; u = 4095; break;
3424
53
  case SystemZ::BI__builtin_s390_vlbb: i = 1; l = 0; u = 15; break;
3425
44
  case SystemZ::BI__builtin_s390_vpdi: i = 2; l = 0; u = 15; break;
3426
108
  case SystemZ::BI__builtin_s390_vsldb: i = 2; l = 0; u = 15; break;
3427
64
  case SystemZ::BI__builtin_s390_vstrcb:
3428
128
  case SystemZ::BI__builtin_s390_vstrch:
3429
192
  case SystemZ::BI__builtin_s390_vstrcf:
3430
226
  case SystemZ::BI__builtin_s390_vstrczb:
3431
260
  case SystemZ::BI__builtin_s390_vstrczh:
3432
294
  case SystemZ::BI__builtin_s390_vstrczf:
3433
358
  case SystemZ::BI__builtin_s390_vstrcbs:
3434
422
  case SystemZ::BI__builtin_s390_vstrchs:
3435
486
  case SystemZ::BI__builtin_s390_vstrcfs:
3436
520
  case SystemZ::BI__builtin_s390_vstrczbs:
3437
554
  case SystemZ::BI__builtin_s390_vstrczhs:
3438
588
  case SystemZ::BI__builtin_s390_vstrczfs: i = 3; l = 0; u = 15; break;
3439
14
  case SystemZ::BI__builtin_s390_vmslg: i = 3; l = 0; u = 15; break;
3440
16
  case SystemZ::BI__builtin_s390_vfminsb:
3441
32
  case SystemZ::BI__builtin_s390_vfmaxsb:
3442
49
  case SystemZ::BI__builtin_s390_vfmindb:
3443
65
  case SystemZ::BI__builtin_s390_vfmaxdb: i = 2; l = 0; u = 15; break;
3444
44
  case SystemZ::BI__builtin_s390_vsld: i = 2; l = 0; u = 7; break;
3445
44
  case SystemZ::BI__builtin_s390_vsrd: i = 2; l = 0; u = 7; break;
3446
2.99k
  }
3447
2.99k
  return SemaBuiltinConstantArgRange(TheCall, i, l, u);
3448
2.99k
}
3449
3450
/// SemaBuiltinCpuSupports - Handle __builtin_cpu_supports(char *).
3451
/// This checks that the target supports __builtin_cpu_supports and
3452
/// that the string argument is constant and valid.
3453
static bool SemaBuiltinCpuSupports(Sema &S, const TargetInfo &TI,
3454
44
                                   CallExpr *TheCall) {
3455
44
  Expr *Arg = TheCall->getArg(0);
3456
3457
  // Check if the argument is a string literal.
3458
44
  if (!isa<StringLiteral>(Arg->IgnoreParenImpCasts()))
3459
1
    return S.Diag(TheCall->getBeginLoc(), diag::err_expr_not_string_literal)
3460
1
           << Arg->getSourceRange();
3461
3462
  // Check the contents of the string.
3463
43
  StringRef Feature =
3464
43
      cast<StringLiteral>(Arg->IgnoreParenImpCasts())->getString();
3465
43
  if (!TI.validateCpuSupports(Feature))
3466
1
    return S.Diag(TheCall->getBeginLoc(), diag::err_invalid_cpu_supports)
3467
1
           << Arg->getSourceRange();
3468
42
  return false;
3469
42
}
3470
3471
/// SemaBuiltinCpuIs - Handle __builtin_cpu_is(char *).
3472
/// This checks that the target supports __builtin_cpu_is and
3473
/// that the string argument is constant and valid.
3474
57
static bool SemaBuiltinCpuIs(Sema &S, const TargetInfo &TI, CallExpr *TheCall) {
3475
57
  Expr *Arg = TheCall->getArg(0);
3476
3477
  // Check if the argument is a string literal.
3478
57
  if (!isa<StringLiteral>(Arg->IgnoreParenImpCasts()))
3479
0
    return S.Diag(TheCall->getBeginLoc(), diag::err_expr_not_string_literal)
3480
0
           << Arg->getSourceRange();
3481
3482
  // Check the contents of the string.
3483
57
  StringRef Feature =
3484
57
      cast<StringLiteral>(Arg->IgnoreParenImpCasts())->getString();
3485
57
  if (!TI.validateCpuIs(Feature))
3486
6
    return S.Diag(TheCall->getBeginLoc(), diag::err_invalid_cpu_is)
3487
6
           << Arg->getSourceRange();
3488
51
  return false;
3489
51
}
3490
3491
// Check if the rounding mode is legal.
3492
907k
bool Sema::CheckX86BuiltinRoundingOrSAE(unsigned BuiltinID, CallExpr *TheCall) {
3493
  // Indicates if this instruction has rounding control or just SAE.
3494
907k
  bool HasRC = false;
3495
3496
907k
  unsigned ArgNum = 0;
3497
907k
  switch (BuiltinID) {
3498
858k
  default:
3499
858k
    return false;
3500
252
  case X86::BI__builtin_ia32_vcvttsd2si32:
3501
456
  case X86::BI__builtin_ia32_vcvttsd2si64:
3502
706
  case X86::BI__builtin_ia32_vcvttsd2usi32:
3503
908
  case X86::BI__builtin_ia32_vcvttsd2usi64:
3504
1.16k
  case X86::BI__builtin_ia32_vcvttss2si32:
3505
1.36k
  case X86::BI__builtin_ia32_vcvttss2si64:
3506
1.61k
  case X86::BI__builtin_ia32_vcvttss2usi32:
3507
1.81k
  case X86::BI__builtin_ia32_vcvttss2usi64:
3508
1.81k
    ArgNum = 1;
3509
1.81k
    break;
3510
254
  case X86::BI__builtin_ia32_maxpd512:
3511
508
  case X86::BI__builtin_ia32_maxps512:
3512
762
  case X86::BI__builtin_ia32_minpd512:
3513
1.01k
  case X86::BI__builtin_ia32_minps512:
3514
1.01k
    ArgNum = 2;
3515
1.01k
    break;
3516
6
  case X86::BI__builtin_ia32_cvtps2pd512_mask:
3517
756
  case X86::BI__builtin_ia32_cvttpd2dq512_mask:
3518
1.50k
  case X86::BI__builtin_ia32_cvttpd2qq512_mask:
3519
2.25k
  case X86::BI__builtin_ia32_cvttpd2udq512_mask:
3520
3.00k
  case X86::BI__builtin_ia32_cvttpd2uqq512_mask:
3521
3.75k
  case X86::BI__builtin_ia32_cvttps2dq512_mask:
3522
4.49k
  case X86::BI__builtin_ia32_cvttps2qq512_mask:
3523
5.24k
  case X86::BI__builtin_ia32_cvttps2udq512_mask:
3524
5.99k
  case X86::BI__builtin_ia32_cvttps2uqq512_mask:
3525
6.00k
  case X86::BI__builtin_ia32_exp2pd_mask:
3526
6.00k
  case X86::BI__builtin_ia32_exp2ps_mask:
3527
6.75k
  case X86::BI__builtin_ia32_getexppd512_mask:
3528
7.50k
  case X86::BI__builtin_ia32_getexpps512_mask:
3529
7.51k
  case X86::BI__builtin_ia32_rcp28pd_mask:
3530
7.51k
  case X86::BI__builtin_ia32_rcp28ps_mask:
3531
7.52k
  case X86::BI__builtin_ia32_rsqrt28pd_mask:
3532
7.53k
  case X86::BI__builtin_ia32_rsqrt28ps_mask:
3533
7.53k
  case X86::BI__builtin_ia32_vcomisd:
3534
7.53k
  case X86::BI__builtin_ia32_vcomiss:
3535
8.30k
  case X86::BI__builtin_ia32_vcvtph2ps512_mask:
3536
8.30k
    ArgNum = 3;
3537
8.30k
    break;
3538
233
  case X86::BI__builtin_ia32_cmppd512_mask:
3539
468
  case X86::BI__builtin_ia32_cmpps512_mask:
3540
476
  case X86::BI__builtin_ia32_cmpsd_mask:
3541
484
  case X86::BI__builtin_ia32_cmpss_mask:
3542
986
  case X86::BI__builtin_ia32_cvtss2sd_round_mask:
3543
1.73k
  case X86::BI__builtin_ia32_getexpsd128_round_mask:
3544
2.48k
  case X86::BI__builtin_ia32_getexpss128_round_mask:
3545
2.49k
  case X86::BI__builtin_ia32_getmantpd512_mask:
3546
2.51k
  case X86::BI__builtin_ia32_getmantps512_mask:
3547
3.01k
  case X86::BI__builtin_ia32_maxsd_round_mask:
3548
3.51k
  case X86::BI__builtin_ia32_maxss_round_mask:
3549
4.01k
  case X86::BI__builtin_ia32_minsd_round_mask:
3550
4.51k
  case X86::BI__builtin_ia32_minss_round_mask:
3551
4.52k
  case X86::BI__builtin_ia32_rcp28sd_round_mask:
3552
4.53k
  case X86::BI__builtin_ia32_rcp28ss_round_mask:
3553
4.53k
  case X86::BI__builtin_ia32_reducepd512_mask:
3554
4.54k
  case X86::BI__builtin_ia32_reduceps512_mask:
3555
5.54k
  case X86::BI__builtin_ia32_rndscalepd_mask:
3556
6.54k
  case X86::BI__builtin_ia32_rndscaleps_mask:
3557
6.55k
  case X86::BI__builtin_ia32_rsqrt28sd_round_mask:
3558
6.55k
  case X86::BI__builtin_ia32_rsqrt28ss_round_mask:
3559
6.55k
    ArgNum = 4;
3560
6.55k
    break;
3561
8
  case X86::BI__builtin_ia32_fixupimmpd512_mask:
3562
12
  case X86::BI__builtin_ia32_fixupimmpd512_maskz:
3563
20
  case X86::BI__builtin_ia32_fixupimmps512_mask:
3564
24
  case X86::BI__builtin_ia32_fixupimmps512_maskz:
3565
32
  case X86::BI__builtin_ia32_fixupimmsd_mask:
3566
36
  case X86::BI__builtin_ia32_fixupimmsd_maskz:
3567
44
  case X86::BI__builtin_ia32_fixupimmss_mask:
3568
48
  case X86::BI__builtin_ia32_fixupimmss_maskz:
3569
60
  case X86::BI__builtin_ia32_getmantsd_round_mask:
3570
73
  case X86::BI__builtin_ia32_getmantss_round_mask:
3571
79
  case X86::BI__builtin_ia32_rangepd512_mask:
3572
85
  case X86::BI__builtin_ia32_rangeps512_mask:
3573
91
  case X86::BI__builtin_ia32_rangesd128_round_mask:
3574
97
  case X86::BI__builtin_ia32_rangess128_round_mask:
3575
103
  case X86::BI__builtin_ia32_reducesd_mask:
3576
109
  case X86::BI__builtin_ia32_reducess_mask:
3577
121
  case X86::BI__builtin_ia32_rndscalesd_round_mask:
3578
131
  case X86::BI__builtin_ia32_rndscaless_round_mask:
3579
131
    ArgNum = 5;
3580
131
    break;
3581
4
  case X86::BI__builtin_ia32_vcvtsd2si64:
3582
8
  case X86::BI__builtin_ia32_vcvtsd2si32:
3583
258
  case X86::BI__builtin_ia32_vcvtsd2usi32:
3584
460
  case X86::BI__builtin_ia32_vcvtsd2usi64:
3585
464
  case X86::BI__builtin_ia32_vcvtss2si32:
3586
468
  case X86::BI__builtin_ia32_vcvtss2si64:
3587
718
  case X86::BI__builtin_ia32_vcvtss2usi32:
3588
920
  case X86::BI__builtin_ia32_vcvtss2usi64:
3589
1.17k
  case X86::BI__builtin_ia32_sqrtpd512:
3590
1.42k
  case X86::BI__builtin_ia32_sqrtps512:
3591
1.42k
    ArgNum = 1;
3592
1.42k
    HasRC = true;
3593
1.42k
    break;
3594
6
  case X86::BI__builtin_ia32_addpd512:
3595
12
  case X86::BI__builtin_ia32_addps512:
3596
18
  case X86::BI__builtin_ia32_divpd512:
3597
24
  case X86::BI__builtin_ia32_divps512:
3598
30
  case X86::BI__builtin_ia32_mulpd512:
3599
36
  case X86::BI__builtin_ia32_mulps512:
3600
42
  case X86::BI__builtin_ia32_subpd512:
3601
48
  case X86::BI__builtin_ia32_subps512:
3602
52
  case X86::BI__builtin_ia32_cvtsi2sd64:
3603
56
  case X86::BI__builtin_ia32_cvtsi2ss32:
3604
60
  case X86::BI__builtin_ia32_cvtsi2ss64:
3605
62
  case X86::BI__builtin_ia32_cvtusi2sd64:
3606
64
  case X86::BI__builtin_ia32_cvtusi2ss32:
3607
66
  case X86::BI__builtin_ia32_cvtusi2ss64:
3608
66
    ArgNum = 2;
3609
66
    HasRC = true;
3610
66
    break;
3611
6
  case X86::BI__builtin_ia32_cvtdq2ps512_mask:
3612
12
  case X86::BI__builtin_ia32_cvtudq2ps512_mask:
3613
762
  case X86::BI__builtin_ia32_cvtpd2ps512_mask:
3614
1.51k
  case X86::BI__builtin_ia32_cvtpd2dq512_mask:
3615
2.25k
  case X86::BI__builtin_ia32_cvtpd2qq512_mask:
3616
3.00k
  case X86::BI__builtin_ia32_cvtpd2udq512_mask:
3617
3.75k
  case X86::BI__builtin_ia32_cvtpd2uqq512_mask:
3618
4.50k
  case X86::BI__builtin_ia32_cvtps2dq512_mask:
3619
5.25k
  case X86::BI__builtin_ia32_cvtps2qq512_mask:
3620
6.00k
  case X86::BI__builtin_ia32_cvtps2udq512_mask:
3621
6.75k
  case X86::BI__builtin_ia32_cvtps2uqq512_mask:
3622
6.75k
  case X86::BI__builtin_ia32_cvtqq2pd512_mask:
3623
7.50k
  case X86::BI__builtin_ia32_cvtqq2ps512_mask:
3624
7.50k
  case X86::BI__builtin_ia32_cvtuqq2pd512_mask:
3625
8.25k
  case X86::BI__builtin_ia32_cvtuqq2ps512_mask:
3626
8.25k
    ArgNum = 3;
3627
8.25k
    HasRC = true;
3628
8.25k
    break;
3629
6
  case X86::BI__builtin_ia32_addss_round_mask:
3630
12
  case X86::BI__builtin_ia32_addsd_round_mask:
3631
18
  case X86::BI__builtin_ia32_divss_round_mask:
3632
24
  case X86::BI__builtin_ia32_divsd_round_mask:
3633
30
  case X86::BI__builtin_ia32_mulss_round_mask:
3634
36
  case X86::BI__builtin_ia32_mulsd_round_mask:
3635
42
  case X86::BI__builtin_ia32_subss_round_mask:
3636
48
  case X86::BI__builtin_ia32_subsd_round_mask:
3637
798
  case X86::BI__builtin_ia32_scalefpd512_mask:
3638
1.54k
  case X86::BI__builtin_ia32_scalefps512_mask:
3639
2.29k
  case X86::BI__builtin_ia32_scalefsd_round_mask:
3640
3.04k
  case X86::BI__builtin_ia32_scalefss_round_mask:
3641
3.55k
  case X86::BI__builtin_ia32_cvtsd2ss_round_mask:
3642
4.05k
  case X86::BI__builtin_ia32_sqrtsd_round_mask:
3643
4.55k
  case X86::BI__builtin_ia32_sqrtss_round_mask:
3644
5.56k
  case X86::BI__builtin_ia32_vfmaddsd3_mask:
3645
6.56k
  case X86::BI__builtin_ia32_vfmaddsd3_maskz:
3646
7.06k
  case X86::BI__builtin_ia32_vfmaddsd3_mask3:
3647
8.07k
  case X86::BI__builtin_ia32_vfmaddss3_mask:
3648
9.07k
  case X86::BI__builtin_ia32_vfmaddss3_maskz:
3649
9.57k
  case X86::BI__builtin_ia32_vfmaddss3_mask3:
3650
11.5k
  case X86::BI__builtin_ia32_vfmaddpd512_mask:
3651
12.5k
  case X86::BI__builtin_ia32_vfmaddpd512_maskz:
3652
13.0k
  case X86::BI__builtin_ia32_vfmaddpd512_mask3:
3653
13.5k
  case X86::BI__builtin_ia32_vfmsubpd512_mask3:
3654
15.5k
  case X86::BI__builtin_ia32_vfmaddps512_mask:
3655
16.5k
  case X86::BI__builtin_ia32_vfmaddps512_maskz:
3656
17.0k
  case X86::BI__builtin_ia32_vfmaddps512_mask3:
3657
17.5k
  case X86::BI__builtin_ia32_vfmsubps512_mask3:
3658
18.5k
  case X86::BI__builtin_ia32_vfmaddsubpd512_mask:
3659
19.0k
  case X86::BI__builtin_ia32_vfmaddsubpd512_maskz:
3660
19.3k
  case X86::BI__builtin_ia32_vfmaddsubpd512_mask3:
3661
19.5k
  case X86::BI__builtin_ia32_vfmsubaddpd512_mask3:
3662
20.5k
  case X86::BI__builtin_ia32_vfmaddsubps512_mask:
3663
21.0k
  case X86::BI__builtin_ia32_vfmaddsubps512_maskz:
3664
21.3k
  case X86::BI__builtin_ia32_vfmaddsubps512_mask3:
3665
21.5k
  case X86::BI__builtin_ia32_vfmsubaddps512_mask3:
3666
21.5k
    ArgNum = 4;
3667
21.5k
    HasRC = true;
3668
21.5k
    break;
3669
49.1k
  }
3670
3671
49.1k
  llvm::APSInt Result;
3672
3673
  // We can't check the value of a dependent argument.
3674
49.1k
  Expr *Arg = TheCall->getArg(ArgNum);
3675
49.1k
  if (Arg->isTypeDependent() || Arg->isValueDependent())
3676
0
    return false;
3677
3678
  // Check constant-ness first.
3679
49.1k
  if (SemaBuiltinConstantArg(TheCall, ArgNum, Result))
3680
0
    return true;
3681
3682
  // Make sure rounding mode is either ROUND_CUR_DIRECTION or ROUND_NO_EXC bit
3683
  // is set. If the intrinsic has rounding control(bits 1:0), make sure its only
3684
  // combined with ROUND_NO_EXC. If the intrinsic does not have rounding
3685
  // control, allow ROUND_NO_EXC and ROUND_CUR_DIRECTION together.
3686
49.1k
  if (Result == 4/*ROUND_CUR_DIRECTION*/ ||
3687
731
      Result == 8/*ROUND_NO_EXC*/ ||
3688
390
      (!HasRC && 
Result == 124
/*ROUND_CUR_DIRECTION|ROUND_NO_EXC*/) ||
3689
389
      (HasRC && 
Result.getZExtValue() >= 8386
&&
Result.getZExtValue() <= 11386
))
3690
49.1k
    return false;
3691
3692
3
  return Diag(TheCall->getBeginLoc(), diag::err_x86_builtin_invalid_rounding)
3693
3
         << Arg->getSourceRange();
3694
3
}
3695
3696
// Check if the gather/scatter scale is legal.
3697
bool Sema::CheckX86BuiltinGatherScatterScale(unsigned BuiltinID,
3698
907k
                                             CallExpr *TheCall) {
3699
907k
  unsigned ArgNum = 0;
3700
907k
  switch (BuiltinID) {
3701
907k
  default:
3702
907k
    return false;
3703
2
  case X86::BI__builtin_ia32_gatherpfdpd:
3704
6
  case X86::BI__builtin_ia32_gatherpfdps:
3705
8
  case X86::BI__builtin_ia32_gatherpfqpd:
3706
10
  case X86::BI__builtin_ia32_gatherpfqps:
3707
12
  case X86::BI__builtin_ia32_scatterpfdpd:
3708
14
  case X86::BI__builtin_ia32_scatterpfdps:
3709
16
  case X86::BI__builtin_ia32_scatterpfqpd:
3710
18
  case X86::BI__builtin_ia32_scatterpfqps:
3711
18
    ArgNum = 3;
3712
18
    break;
3713
4
  case X86::BI__builtin_ia32_gatherd_pd:
3714
8
  case X86::BI__builtin_ia32_gatherd_pd256:
3715
12
  case X86::BI__builtin_ia32_gatherq_pd:
3716
16
  case X86::BI__builtin_ia32_gatherq_pd256:
3717
20
  case X86::BI__builtin_ia32_gatherd_ps:
3718
24
  case X86::BI__builtin_ia32_gatherd_ps256:
3719
28
  case X86::BI__builtin_ia32_gatherq_ps:
3720
32
  case X86::BI__builtin_ia32_gatherq_ps256:
3721
36
  case X86::BI__builtin_ia32_gatherd_q:
3722
40
  case X86::BI__builtin_ia32_gatherd_q256:
3723
44
  case X86::BI__builtin_ia32_gatherq_q:
3724
48
  case X86::BI__builtin_ia32_gatherq_q256:
3725
53
  case X86::BI__builtin_ia32_gatherd_d:
3726
57
  case X86::BI__builtin_ia32_gatherd_d256:
3727
61
  case X86::BI__builtin_ia32_gatherq_d:
3728
65
  case X86::BI__builtin_ia32_gatherq_d256:
3729
66
  case X86::BI__builtin_ia32_gather3div2df:
3730
67
  case X86::BI__builtin_ia32_gather3div2di:
3731
68
  case X86::BI__builtin_ia32_gather3div4df:
3732
69
  case X86::BI__builtin_ia32_gather3div4di:
3733
70
  case X86::BI__builtin_ia32_gather3div4sf:
3734
71
  case X86::BI__builtin_ia32_gather3div4si:
3735
72
  case X86::BI__builtin_ia32_gather3div8sf:
3736
73
  case X86::BI__builtin_ia32_gather3div8si:
3737
74
  case X86::BI__builtin_ia32_gather3siv2df:
3738
75
  case X86::BI__builtin_ia32_gather3siv2di:
3739
76
  case X86::BI__builtin_ia32_gather3siv4df:
3740
77
  case X86::BI__builtin_ia32_gather3siv4di:
3741
78
  case X86::BI__builtin_ia32_gather3siv4sf:
3742
79
  case X86::BI__builtin_ia32_gather3siv4si:
3743
80
  case X86::BI__builtin_ia32_gather3siv8sf:
3744
81
  case X86::BI__builtin_ia32_gather3siv8si:
3745
85
  case X86::BI__builtin_ia32_gathersiv8df:
3746
89
  case X86::BI__builtin_ia32_gathersiv16sf:
3747
93
  case X86::BI__builtin_ia32_gatherdiv8df:
3748
97
  case X86::BI__builtin_ia32_gatherdiv16sf:
3749
101
  case X86::BI__builtin_ia32_gathersiv8di:
3750
105
  case X86::BI__builtin_ia32_gathersiv16si:
3751
109
  case X86::BI__builtin_ia32_gatherdiv8di:
3752
113
  case X86::BI__builtin_ia32_gatherdiv16si:
3753
115
  case X86::BI__builtin_ia32_scatterdiv2df:
3754
117
  case X86::BI__builtin_ia32_scatterdiv2di:
3755
119
  case X86::BI__builtin_ia32_scatterdiv4df:
3756
121
  case X86::BI__builtin_ia32_scatterdiv4di:
3757
123
  case X86::BI__builtin_ia32_scatterdiv4sf:
3758
125
  case X86::BI__builtin_ia32_scatterdiv4si:
3759
127
  case X86::BI__builtin_ia32_scatterdiv8sf:
3760
129
  case X86::BI__builtin_ia32_scatterdiv8si:
3761
131
  case X86::BI__builtin_ia32_scattersiv2df:
3762
133
  case X86::BI__builtin_ia32_scattersiv2di:
3763
135
  case X86::BI__builtin_ia32_scattersiv4df:
3764
137
  case X86::BI__builtin_ia32_scattersiv4di:
3765
139
  case X86::BI__builtin_ia32_scattersiv4sf:
3766
141
  case X86::BI__builtin_ia32_scattersiv4si:
3767
143
  case X86::BI__builtin_ia32_scattersiv8sf:
3768
145
  case X86::BI__builtin_ia32_scattersiv8si:
3769
149
  case X86::BI__builtin_ia32_scattersiv8df:
3770
153
  case X86::BI__builtin_ia32_scattersiv16sf:
3771
157
  case X86::BI__builtin_ia32_scatterdiv8df:
3772
161
  case X86::BI__builtin_ia32_scatterdiv16sf:
3773
165
  case X86::BI__builtin_ia32_scattersiv8di:
3774
169
  case X86::BI__builtin_ia32_scattersiv16si:
3775
173
  case X86::BI__builtin_ia32_scatterdiv8di:
3776
177
  case X86::BI__builtin_ia32_scatterdiv16si:
3777
177
    ArgNum = 4;
3778
177
    break;
3779
195
  }
3780
3781
195
  llvm::APSInt Result;
3782
3783
  // We can't check the value of a dependent argument.
3784
195
  Expr *Arg = TheCall->getArg(ArgNum);
3785
195
  if (Arg->isTypeDependent() || Arg->isValueDependent())
3786
0
    return false;
3787
3788
  // Check constant-ness first.
3789
195
  if (SemaBuiltinConstantArg(TheCall, ArgNum, Result))
3790
0
    return true;
3791
3792
195
  if (Result == 1 || 
Result == 2186
||
Result == 42
||
Result == 82
)
3793
193
    return false;
3794
3795
2
  return Diag(TheCall->getBeginLoc(), diag::err_x86_builtin_invalid_scale)
3796
2
         << Arg->getSourceRange();
3797
2
}
3798
3799
enum { TileRegLow = 0, TileRegHigh = 7 };
3800
3801
bool Sema::CheckX86BuiltinTileArgumentsRange(CallExpr *TheCall,
3802
20
                                             ArrayRef<int> ArgNums) {
3803
41
  for (int ArgNum : ArgNums) {
3804
41
    if (SemaBuiltinConstantArgRange(TheCall, ArgNum, TileRegLow, TileRegHigh))
3805
7
      return true;
3806
41
  }
3807
13
  return false;
3808
20
}
3809
3810
bool Sema::CheckX86BuiltinTileDuplicate(CallExpr *TheCall,
3811
9
                                        ArrayRef<int> ArgNums) {
3812
  // Because the max number of tile register is TileRegHigh + 1, so here we use
3813
  // each bit to represent the usage of them in bitset.
3814
9
  std::bitset<TileRegHigh + 1> ArgValues;
3815
26
  for (int ArgNum : ArgNums) {
3816
26
    Expr *Arg = TheCall->getArg(ArgNum);
3817
26
    if (Arg->isTypeDependent() || Arg->isValueDependent())
3818
0
      continue;
3819
3820
26
    llvm::APSInt Result;
3821
26
    if (SemaBuiltinConstantArg(TheCall, ArgNum, Result))
3822
0
      return true;
3823
26
    int ArgExtValue = Result.getExtValue();
3824
26
    assert((ArgExtValue >= TileRegLow || ArgExtValue <= TileRegHigh) &&
3825
26
           "Incorrect tile register num.");
3826
26
    if (ArgValues.test(ArgExtValue))
3827
4
      return Diag(TheCall->getBeginLoc(),
3828
4
                  diag::err_x86_builtin_tile_arg_duplicate)
3829
4
             << TheCall->getArg(ArgNum)->getSourceRange();
3830
22
    ArgValues.set(ArgExtValue);
3831
22
  }
3832
5
  return false;
3833
9
}
3834
3835
bool Sema::CheckX86BuiltinTileRangeAndDuplicate(CallExpr *TheCall,
3836
12
                                                ArrayRef<int> ArgNums) {
3837
12
  return CheckX86BuiltinTileArgumentsRange(TheCall, ArgNums) ||
3838
9
         CheckX86BuiltinTileDuplicate(TheCall, ArgNums);
3839
12
}
3840
3841
907k
bool Sema::CheckX86BuiltinTileArguments(unsigned BuiltinID, CallExpr *TheCall) {
3842
907k
  switch (BuiltinID) {
3843
907k
  default:
3844
907k
    return false;
3845
2
  case X86::BI__builtin_ia32_tileloadd64:
3846
4
  case X86::BI__builtin_ia32_tileloaddt164:
3847
6
  case X86::BI__builtin_ia32_tilestored64:
3848
8
  case X86::BI__builtin_ia32_tilezero:
3849
8
    return CheckX86BuiltinTileArgumentsRange(TheCall, 0);
3850
3
  case X86::BI__builtin_ia32_tdpbssd:
3851
7
  case X86::BI__builtin_ia32_tdpbsud:
3852
8
  case X86::BI__builtin_ia32_tdpbusd:
3853
10
  case X86::BI__builtin_ia32_tdpbuud:
3854
12
  case X86::BI__builtin_ia32_tdpbf16ps:
3855
12
    return CheckX86BuiltinTileRangeAndDuplicate(TheCall, {0, 1, 2});
3856
907k
  }
3857
907k
}
3858
745k
static bool isX86_32Builtin(unsigned BuiltinID) {
3859
  // These builtins only work on x86-32 targets.
3860
745k
  switch (BuiltinID) {
3861
1
  case X86::BI__builtin_ia32_readeflags_u32:
3862
2
  case X86::BI__builtin_ia32_writeeflags_u32:
3863
2
    return true;
3864
745k
  }
3865
3866
745k
  return false;
3867
745k
}
3868
3869
bool Sema::CheckX86BuiltinFunctionCall(const TargetInfo &TI, unsigned BuiltinID,
3870
907k
                                       CallExpr *TheCall) {
3871
907k
  if (BuiltinID == X86::BI__builtin_cpu_supports)
3872
44
    return SemaBuiltinCpuSupports(*this, TI, TheCall);
3873
3874
907k
  if (BuiltinID == X86::BI__builtin_cpu_is)
3875
57
    return SemaBuiltinCpuIs(*this, TI, TheCall);
3876
3877
  // Check for 32-bit only builtins on a 64-bit target.
3878
907k
  const llvm::Triple &TT = TI.getTriple();
3879
907k
  if (TT.getArch() != llvm::Triple::x86 && 
isX86_32Builtin(BuiltinID)745k
)
3880
2
    return Diag(TheCall->getCallee()->getBeginLoc(),
3881
2
                diag::err_32_bit_builtin_64_bit_tgt);
3882
3883
  // If the intrinsic has rounding or SAE make sure its valid.
3884
907k
  if (CheckX86BuiltinRoundingOrSAE(BuiltinID, TheCall))
3885
3
    return true;
3886
3887
  // If the intrinsic has a gather/scatter scale immediate make sure its valid.
3888
907k
  if (CheckX86BuiltinGatherScatterScale(BuiltinID, TheCall))
3889
2
    return true;
3890
3891
  // If the intrinsic has a tile arguments, make sure they are valid.
3892
907k
  if (CheckX86BuiltinTileArguments(BuiltinID, TheCall))
3893
11
    return true;
3894
3895
  // For intrinsics which take an immediate value as part of the instruction,
3896
  // range check them here.
3897
907k
  int i = 0, l = 0, u = 0;
3898
907k
  switch (BuiltinID) {
3899
873k
  default:
3900
873k
    return false;
3901
571
  case X86::BI__builtin_ia32_vec_ext_v2si:
3902
573
  case X86::BI__builtin_ia32_vec_ext_v2di:
3903
2.81k
  case X86::BI__builtin_ia32_vextractf128_pd256:
3904
5.04k
  case X86::BI__builtin_ia32_vextractf128_ps256:
3905
5.30k
  case X86::BI__builtin_ia32_vextractf128_si256:
3906
5.30k
  case X86::BI__builtin_ia32_extract128i256:
3907
9.28k
  case X86::BI__builtin_ia32_extractf64x4_mask:
3908
9.28k
  case X86::BI__builtin_ia32_extracti64x4_mask:
3909
9.29k
  case X86::BI__builtin_ia32_extractf32x8_mask:
3910
9.29k
  case X86::BI__builtin_ia32_extracti32x8_mask:
3911
9.29k
  case X86::BI__builtin_ia32_extractf64x2_256_mask:
3912
9.30k
  case X86::BI__builtin_ia32_extracti64x2_256_mask:
3913
9.30k
  case X86::BI__builtin_ia32_extractf32x4_256_mask:
3914
9.30k
  case X86::BI__builtin_ia32_extracti32x4_256_mask:
3915
9.30k
    i = 1; l = 0; u = 1;
3916
9.30k
    break;
3917
2
  case X86::BI__builtin_ia32_vec_set_v2di:
3918
256
  case X86::BI__builtin_ia32_vinsertf128_pd256:
3919
510
  case X86::BI__builtin_ia32_vinsertf128_ps256:
3920
764
  case X86::BI__builtin_ia32_vinsertf128_si256:
3921
770
  case X86::BI__builtin_ia32_insert128i256:
3922
773
  case X86::BI__builtin_ia32_insertf32x8:
3923
776
  case X86::BI__builtin_ia32_inserti32x8:
3924
782
  case X86::BI__builtin_ia32_insertf64x4:
3925
788
  case X86::BI__builtin_ia32_inserti64x4:
3926
791
  case X86::BI__builtin_ia32_insertf64x2_256:
3927
794
  case X86::BI__builtin_ia32_inserti64x2_256:
3928
797
  case X86::BI__builtin_ia32_insertf32x4_256:
3929
800
  case X86::BI__builtin_ia32_inserti32x4_256:
3930
800
    i = 2; l = 0; u = 1;
3931
800
    break;
3932
6
  case X86::BI__builtin_ia32_vpermilpd:
3933
8
  case X86::BI__builtin_ia32_vec_ext_v4hi:
3934
11
  case X86::BI__builtin_ia32_vec_ext_v4si:
3935
13
  case X86::BI__builtin_ia32_vec_ext_v4sf:
3936
16
  case X86::BI__builtin_ia32_vec_ext_v4di:
3937
22
  case X86::BI__builtin_ia32_extractf32x4_mask:
3938
28
  case X86::BI__builtin_ia32_extracti32x4_mask:
3939
31
  case X86::BI__builtin_ia32_extractf64x2_512_mask:
3940
34
  case X86::BI__builtin_ia32_extracti64x2_512_mask:
3941
34
    i = 1; l = 0; u = 3;
3942
34
    break;
3943
0
  case X86::BI_mm_prefetch:
3944
5
  case X86::BI__builtin_ia32_vec_ext_v8hi:
3945
11
  case X86::BI__builtin_ia32_vec_ext_v8si:
3946
11
    i = 1; l = 0; u = 7;
3947
11
    break;
3948
2
  case X86::BI__builtin_ia32_sha1rnds4:
3949
4
  case X86::BI__builtin_ia32_blendpd:
3950
9
  case X86::BI__builtin_ia32_shufpd:
3951
11
  case X86::BI__builtin_ia32_vec_set_v4hi:
3952
13
  case X86::BI__builtin_ia32_vec_set_v4si:
3953
16
  case X86::BI__builtin_ia32_vec_set_v4di:
3954
19
  case X86::BI__builtin_ia32_shuf_f32x4_256:
3955
22
  case X86::BI__builtin_ia32_shuf_f64x2_256:
3956
25
  case X86::BI__builtin_ia32_shuf_i32x4_256:
3957
28
  case X86::BI__builtin_ia32_shuf_i64x2_256:
3958
31
  case X86::BI__builtin_ia32_insertf64x2_512:
3959
34
  case X86::BI__builtin_ia32_inserti64x2_512:
3960
40
  case X86::BI__builtin_ia32_insertf32x4:
3961
46
  case X86::BI__builtin_ia32_inserti32x4:
3962
46
    i = 2; l = 0; u = 3;
3963
46
    break;
3964
2
  case X86::BI__builtin_ia32_vpermil2pd:
3965
4
  case X86::BI__builtin_ia32_vpermil2pd256:
3966
6
  case X86::BI__builtin_ia32_vpermil2ps:
3967
8
  case X86::BI__builtin_ia32_vpermil2ps256:
3968
8
    i = 3; l = 0; u = 3;
3969
8
    break;
3970
1.02k
  case X86::BI__builtin_ia32_cmpb128_mask:
3971
2.04k
  case X86::BI__builtin_ia32_cmpw128_mask:
3972
3.05k
  case X86::BI__builtin_ia32_cmpd128_mask:
3973
4.06k
  case X86::BI__builtin_ia32_cmpq128_mask:
3974
5.08k
  case X86::BI__builtin_ia32_cmpb256_mask:
3975
6.10k
  case X86::BI__builtin_ia32_cmpw256_mask:
3976
7.11k
  case X86::BI__builtin_ia32_cmpd256_mask:
3977
8.11k
  case X86::BI__builtin_ia32_cmpq256_mask:
3978
9.13k
  case X86::BI__builtin_ia32_cmpb512_mask:
3979
10.1k
  case X86::BI__builtin_ia32_cmpw512_mask:
3980
11.1k
  case X86::BI__builtin_ia32_cmpd512_mask:
3981
12.2k
  case X86::BI__builtin_ia32_cmpq512_mask:
3982
12.2k
  case X86::BI__builtin_ia32_ucmpb128_mask:
3983
12.2k
  case X86::BI__builtin_ia32_ucmpw128_mask:
3984
12.2k
  case X86::BI__builtin_ia32_ucmpd128_mask:
3985
12.2k
  case X86::BI__builtin_ia32_ucmpq128_mask:
3986
12.3k
  case X86::BI__builtin_ia32_ucmpb256_mask:
3987
12.3k
  case X86::BI__builtin_ia32_ucmpw256_mask:
3988
12.3k
  case X86::BI__builtin_ia32_ucmpd256_mask:
3989
12.3k
  case X86::BI__builtin_ia32_ucmpq256_mask:
3990
12.4k
  case X86::BI__builtin_ia32_ucmpb512_mask:
3991
12.5k
  case X86::BI__builtin_ia32_ucmpw512_mask:
3992
12.7k
  case X86::BI__builtin_ia32_ucmpd512_mask:
3993
12.7k
  case X86::BI__builtin_ia32_ucmpq512_mask:
3994
13.6k
  case X86::BI__builtin_ia32_vpcomub:
3995
14.4k
  case X86::BI__builtin_ia32_vpcomuw:
3996
15.2k
  case X86::BI__builtin_ia32_vpcomud:
3997
16.1k
  case X86::BI__builtin_ia32_vpcomuq:
3998
16.9k
  case X86::BI__builtin_ia32_vpcomb:
3999
17.8k
  case X86::BI__builtin_ia32_vpcomw:
4000
18.6k
  case X86::BI__builtin_ia32_vpcomd:
4001
19.4k
  case X86::BI__builtin_ia32_vpcomq:
4002
19.4k
  case X86::BI__builtin_ia32_vec_set_v8hi:
4003
19.4k
  case X86::BI__builtin_ia32_vec_set_v8si:
4004
19.4k
    i = 2; l = 0; u = 7;
4005
19.4k
    break;
4006
6
  case X86::BI__builtin_ia32_vpermilpd256:
4007
25
  case X86::BI__builtin_ia32_roundps:
4008
32
  case X86::BI__builtin_ia32_roundpd:
4009
44
  case X86::BI__builtin_ia32_roundps256:
4010
56
  case X86::BI__builtin_ia32_roundpd256:
4011
59
  case X86::BI__builtin_ia32_getmantpd128_mask:
4012
62
  case X86::BI__builtin_ia32_getmantpd256_mask:
4013
65
  case X86::BI__builtin_ia32_getmantps128_mask:
4014
68
  case X86::BI__builtin_ia32_getmantps256_mask:
4015
80
  case X86::BI__builtin_ia32_getmantpd512_mask:
4016
92
  case X86::BI__builtin_ia32_getmantps512_mask:
4017
96
  case X86::BI__builtin_ia32_vec_ext_v16qi:
4018
99
  case X86::BI__builtin_ia32_vec_ext_v16hi:
4019
99
    i = 1; l = 0; u = 15;
4020
99
    break;
4021
2
  case X86::BI__builtin_ia32_pblendd128:
4022
4
  case X86::BI__builtin_ia32_blendps:
4023
7
  case X86::BI__builtin_ia32_blendpd256:
4024
12
  case X86::BI__builtin_ia32_shufpd256:
4025
19
  case X86::BI__builtin_ia32_roundss:
4026
26
  case X86::BI__builtin_ia32_roundsd:
4027
29
  case X86::BI__builtin_ia32_rangepd128_mask:
4028
32
  case X86::BI__builtin_ia32_rangepd256_mask:
4029
38
  case X86::BI__builtin_ia32_rangepd512_mask:
4030
41
  case X86::BI__builtin_ia32_rangeps128_mask:
4031
44
  case X86::BI__builtin_ia32_rangeps256_mask:
4032
50
  case X86::BI__builtin_ia32_rangeps512_mask:
4033
62
  case X86::BI__builtin_ia32_getmantsd_round_mask:
4034
74
  case X86::BI__builtin_ia32_getmantss_round_mask:
4035
76
  case X86::BI__builtin_ia32_vec_set_v16qi:
4036
79
  case X86::BI__builtin_ia32_vec_set_v16hi:
4037
79
    i = 2; l = 0; u = 15;
4038
79
    break;
4039
3
  case X86::BI__builtin_ia32_vec_ext_v32qi:
4040
3
    i = 1; l = 0; u = 31;
4041
3
    break;
4042
154
  case X86::BI__builtin_ia32_cmpps:
4043
183
  case X86::BI__builtin_ia32_cmpss:
4044
336
  case X86::BI__builtin_ia32_cmppd:
4045
365
  case X86::BI__builtin_ia32_cmpsd:
4046
498
  case X86::BI__builtin_ia32_cmpps256:
4047
633
  case X86::BI__builtin_ia32_cmppd256:
4048
761
  case X86::BI__builtin_ia32_cmpps128_mask:
4049
889
  case X86::BI__builtin_ia32_cmppd128_mask:
4050
1.01k
  case X86::BI__builtin_ia32_cmpps256_mask:
4051
1.14k
  case X86::BI__builtin_ia32_cmppd256_mask:
4052
1.37k
  case X86::BI__builtin_ia32_cmpps512_mask:
4053
1.61k
  case X86::BI__builtin_ia32_cmppd512_mask:
4054
1.62k
  case X86::BI__builtin_ia32_cmpsd_mask:
4055
1.62k
  case X86::BI__builtin_ia32_cmpss_mask:
4056
1.63k
  case X86::BI__builtin_ia32_vec_set_v32qi:
4057
1.63k
    i = 2; l = 0; u = 31;
4058
1.63k
    break;
4059
5
  case X86::BI__builtin_ia32_permdf256:
4060
10
  case X86::BI__builtin_ia32_permdi256:
4061
16
  case X86::BI__builtin_ia32_permdf512:
4062
22
  case X86::BI__builtin_ia32_permdi512:
4063
32
  case X86::BI__builtin_ia32_vpermilps:
4064
38
  case X86::BI__builtin_ia32_vpermilps256:
4065
44
  case X86::BI__builtin_ia32_vpermilpd512:
4066
50
  case X86::BI__builtin_ia32_vpermilps512:
4067
55
  case X86::BI__builtin_ia32_pshufd:
4068
59
  case X86::BI__builtin_ia32_pshufd256:
4069
65
  case X86::BI__builtin_ia32_pshufd512:
4070
72
  case X86::BI__builtin_ia32_pshufhw:
4071
78
  case X86::BI__builtin_ia32_pshufhw256:
4072
84
  case X86::BI__builtin_ia32_pshufhw512:
4073
109
  case X86::BI__builtin_ia32_pshuflw:
4074
115
  case X86::BI__builtin_ia32_pshuflw256:
4075
121
  case X86::BI__builtin_ia32_pshuflw512:
4076
125
  case X86::BI__builtin_ia32_vcvtps2ph:
4077
145
  case X86::BI__builtin_ia32_vcvtps2ph_mask:
4078
149
  case X86::BI__builtin_ia32_vcvtps2ph256:
4079
169
  case X86::BI__builtin_ia32_vcvtps2ph256_mask:
4080
197
  case X86::BI__builtin_ia32_vcvtps2ph512_mask:
4081
200
  case X86::BI__builtin_ia32_rndscaleps_128_mask:
4082
203
  case X86::BI__builtin_ia32_rndscalepd_128_mask:
4083
206
  case X86::BI__builtin_ia32_rndscaleps_256_mask:
4084
209
  case X86::BI__builtin_ia32_rndscalepd_256_mask:
4085
1.21k
  case X86::BI__builtin_ia32_rndscaleps_mask:
4086
2.21k
  case X86::BI__builtin_ia32_rndscalepd_mask:
4087
2.21k
  case X86::BI__builtin_ia32_reducepd128_mask:
4088
2.21k
  case X86::BI__builtin_ia32_reducepd256_mask:
4089
2.22k
  case X86::BI__builtin_ia32_reducepd512_mask:
4090
2.22k
  case X86::BI__builtin_ia32_reduceps128_mask:
4091
2.23k
  case X86::BI__builtin_ia32_reduceps256_mask:
4092
2.23k
  case X86::BI__builtin_ia32_reduceps512_mask:
4093
2.24k
  case X86::BI__builtin_ia32_prold512:
4094
2.24k
  case X86::BI__builtin_ia32_prolq512:
4095
2.25k
  case X86::BI__builtin_ia32_prold128:
4096
2.25k
  case X86::BI__builtin_ia32_prold256:
4097
2.25k
  case X86::BI__builtin_ia32_prolq128:
4098
2.26k
  case X86::BI__builtin_ia32_prolq256:
4099
2.26k
  case X86::BI__builtin_ia32_prord512:
4100
2.27k
  case X86::BI__builtin_ia32_prorq512:
4101
2.27k
  case X86::BI__builtin_ia32_prord128:
4102
2.27k
  case X86::BI__builtin_ia32_prord256:
4103
2.28k
  case X86::BI__builtin_ia32_prorq128:
4104
2.28k
  case X86::BI__builtin_ia32_prorq256:
4105
2.28k
  case X86::BI__builtin_ia32_fpclasspd128_mask:
4106
2.28k
  case X86::BI__builtin_ia32_fpclasspd256_mask:
4107
2.29k
  case X86::BI__builtin_ia32_fpclassps128_mask:
4108
2.29k
  case X86::BI__builtin_ia32_fpclassps256_mask:
4109
2.29k
  case X86::BI__builtin_ia32_fpclassps512_mask:
4110
2.29k
  case X86::BI__builtin_ia32_fpclasspd512_mask:
4111
2.29k
  case X86::BI__builtin_ia32_fpclasssd_mask:
4112
2.30k
  case X86::BI__builtin_ia32_fpclassss_mask:
4113
2.31k
  case X86::BI__builtin_ia32_pslldqi128_byteshift:
4114
2.31k
  case X86::BI__builtin_ia32_pslldqi256_byteshift:
4115
2.32k
  case X86::BI__builtin_ia32_pslldqi512_byteshift:
4116
2.33k
  case X86::BI__builtin_ia32_psrldqi128_byteshift:
4117
2.33k
  case X86::BI__builtin_ia32_psrldqi256_byteshift:
4118
2.33k
  case X86::BI__builtin_ia32_psrldqi512_byteshift:
4119
2.34k
  case X86::BI__builtin_ia32_kshiftliqi:
4120
2.34k
  case X86::BI__builtin_ia32_kshiftlihi:
4121
2.34k
  case X86::BI__builtin_ia32_kshiftlisi:
4122
2.34k
  case X86::BI__builtin_ia32_kshiftlidi:
4123
2.34k
  case X86::BI__builtin_ia32_kshiftriqi:
4124
2.34k
  case X86::BI__builtin_ia32_kshiftrihi:
4125
2.35k
  case X86::BI__builtin_ia32_kshiftrisi:
4126
2.35k
  case X86::BI__builtin_ia32_kshiftridi:
4127
2.35k
    i = 1; l = 0; u = 255;
4128
2.35k
    break;
4129
7
  case X86::BI__builtin_ia32_vperm2f128_pd256:
4130
14
  case X86::BI__builtin_ia32_vperm2f128_ps256:
4131
21
  case X86::BI__builtin_ia32_vperm2f128_si256:
4132
23
  case X86::BI__builtin_ia32_permti256:
4133
25
  case X86::BI__builtin_ia32_pblendw128:
4134
27
  case X86::BI__builtin_ia32_pblendw256:
4135
30
  case X86::BI__builtin_ia32_blendps256:
4136
32
  case X86::BI__builtin_ia32_pblendd256:
4137
45
  case X86::BI__builtin_ia32_palignr128:
4138
53
  case X86::BI__builtin_ia32_palignr256:
4139
59
  case X86::BI__builtin_ia32_palignr512:
4140
65
  case X86::BI__builtin_ia32_alignq512:
4141
71
  case X86::BI__builtin_ia32_alignd512:
4142
74
  case X86::BI__builtin_ia32_alignd128:
4143
77
  case X86::BI__builtin_ia32_alignd256:
4144
80
  case X86::BI__builtin_ia32_alignq128:
4145
83
  case X86::BI__builtin_ia32_alignq256:
4146
85
  case X86::BI__builtin_ia32_vcomisd:
4147
87
  case X86::BI__builtin_ia32_vcomiss:
4148
93
  case X86::BI__builtin_ia32_shuf_f32x4:
4149
99
  case X86::BI__builtin_ia32_shuf_f64x2:
4150
105
  case X86::BI__builtin_ia32_shuf_i32x4:
4151
111
  case X86::BI__builtin_ia32_shuf_i64x2:
4152
117
  case X86::BI__builtin_ia32_shufpd512:
4153
121
  case X86::BI__builtin_ia32_shufps:
4154
127
  case X86::BI__builtin_ia32_shufps256:
4155
133
  case X86::BI__builtin_ia32_shufps512:
4156
139
  case X86::BI__builtin_ia32_dbpsadbw128:
4157
145
  case X86::BI__builtin_ia32_dbpsadbw256:
4158
151
  case X86::BI__builtin_ia32_dbpsadbw512:
4159
155
  case X86::BI__builtin_ia32_vpshldd128:
4160
159
  case X86::BI__builtin_ia32_vpshldd256:
4161
163
  case X86::BI__builtin_ia32_vpshldd512:
4162
167
  case X86::BI__builtin_ia32_vpshldq128:
4163
171
  case X86::BI__builtin_ia32_vpshldq256:
4164
175
  case X86::BI__builtin_ia32_vpshldq512:
4165
179
  case X86::BI__builtin_ia32_vpshldw128:
4166
183
  case X86::BI__builtin_ia32_vpshldw256:
4167
187
  case X86::BI__builtin_ia32_vpshldw512:
4168
191
  case X86::BI__builtin_ia32_vpshrdd128:
4169
195
  case X86::BI__builtin_ia32_vpshrdd256:
4170
199
  case X86::BI__builtin_ia32_vpshrdd512:
4171
203
  case X86::BI__builtin_ia32_vpshrdq128:
4172
207
  case X86::BI__builtin_ia32_vpshrdq256:
4173
211
  case X86::BI__builtin_ia32_vpshrdq512:
4174
215
  case X86::BI__builtin_ia32_vpshrdw128:
4175
219
  case X86::BI__builtin_ia32_vpshrdw256:
4176
223
  case X86::BI__builtin_ia32_vpshrdw512:
4177
223
    i = 2; l = 0; u = 255;
4178
223
    break;
4179
8
  case X86::BI__builtin_ia32_fixupimmpd512_mask:
4180
12
  case X86::BI__builtin_ia32_fixupimmpd512_maskz:
4181
20
  case X86::BI__builtin_ia32_fixupimmps512_mask:
4182
24
  case X86::BI__builtin_ia32_fixupimmps512_maskz:
4183
32
  case X86::BI__builtin_ia32_fixupimmsd_mask:
4184
36
  case X86::BI__builtin_ia32_fixupimmsd_maskz:
4185
44
  case X86::BI__builtin_ia32_fixupimmss_mask:
4186
48
  case X86::BI__builtin_ia32_fixupimmss_maskz:
4187
50
  case X86::BI__builtin_ia32_fixupimmpd128_mask:
4188
51
  case X86::BI__builtin_ia32_fixupimmpd128_maskz:
4189
53
  case X86::BI__builtin_ia32_fixupimmpd256_mask:
4190
54
  case X86::BI__builtin_ia32_fixupimmpd256_maskz:
4191
56
  case X86::BI__builtin_ia32_fixupimmps128_mask:
4192
57
  case X86::BI__builtin_ia32_fixupimmps128_maskz:
4193
59
  case X86::BI__builtin_ia32_fixupimmps256_mask:
4194
60
  case X86::BI__builtin_ia32_fixupimmps256_maskz:
4195
64
  case X86::BI__builtin_ia32_pternlogd512_mask:
4196
66
  case X86::BI__builtin_ia32_pternlogd512_maskz:
4197
70
  case X86::BI__builtin_ia32_pternlogq512_mask:
4198
72
  case X86::BI__builtin_ia32_pternlogq512_maskz:
4199
74
  case X86::BI__builtin_ia32_pternlogd128_mask:
4200
75
  case X86::BI__builtin_ia32_pternlogd128_maskz:
4201
77
  case X86::BI__builtin_ia32_pternlogd256_mask:
4202
78
  case X86::BI__builtin_ia32_pternlogd256_maskz:
4203
80
  case X86::BI__builtin_ia32_pternlogq128_mask:
4204
81
  case X86::BI__builtin_ia32_pternlogq128_maskz:
4205
83
  case X86::BI__builtin_ia32_pternlogq256_mask:
4206
84
  case X86::BI__builtin_ia32_pternlogq256_maskz:
4207
84
    i = 3; l = 0; u = 255;
4208
84
    break;
4209
2
  case X86::BI__builtin_ia32_gatherpfdpd:
4210
5
  case X86::BI__builtin_ia32_gatherpfdps:
4211
7
  case X86::BI__builtin_ia32_gatherpfqpd:
4212
9
  case X86::BI__builtin_ia32_gatherpfqps:
4213
11
  case X86::BI__builtin_ia32_scatterpfdpd:
4214
13
  case X86::BI__builtin_ia32_scatterpfdps:
4215
15
  case X86::BI__builtin_ia32_scatterpfqpd:
4216
17
  case X86::BI__builtin_ia32_scatterpfqps:
4217
17
    i = 4; l = 2; u = 3;
4218
17
    break;
4219
6
  case X86::BI__builtin_ia32_reducesd_mask:
4220
12
  case X86::BI__builtin_ia32_reducess_mask:
4221
24
  case X86::BI__builtin_ia32_rndscalesd_round_mask:
4222
34
  case X86::BI__builtin_ia32_rndscaless_round_mask:
4223
34
    i = 4; l = 0; u = 255;
4224
34
    break;
4225
34.2k
  }
4226
4227
  // Note that we don't force a hard error on the range check here, allowing
4228
  // template-generated or macro-generated dead code to potentially have out-of-
4229
  // range values. These need to code generate, but don't need to necessarily
4230
  // make any sense. We use a warning that defaults to an error.
4231
34.2k
  return SemaBuiltinConstantArgRange(TheCall, i, l, u, /*RangeIsError*/ false);
4232
34.2k
}
4233
4234
/// Given a FunctionDecl's FormatAttr, attempts to populate the FomatStringInfo
4235
/// parameter with the FormatAttr's correct format_idx and firstDataArg.
4236
/// Returns true when the format fits the function and the FormatStringInfo has
4237
/// been populated.
4238
bool Sema::getFormatStringInfo(const FormatAttr *Format, bool IsCXXMember,
4239
18.7k
                               FormatStringInfo *FSI) {
4240
18.7k
  FSI->HasVAListArg = Format->getFirstArg() == 0;
4241
18.7k
  FSI->FormatIdx = Format->getFormatIdx() - 1;
4242
18.6k
  FSI->FirstDataArg = FSI->HasVAListArg ? 
0156
: Format->getFirstArg() - 1;
4243
4244
  // The way the format attribute works in GCC, the implicit this argument
4245
  // of member functions is counted. However, it doesn't appear in our own
4246
  // lists, so decrement format_idx in that case.
4247
18.7k
  if (IsCXXMember) {
4248
19
    if(FSI->FormatIdx == 0)
4249
0
      return false;
4250
19
    --FSI->FormatIdx;
4251
19
    if (FSI->FirstDataArg != 0)
4252
19
      --FSI->FirstDataArg;
4253
19
  }
4254
18.7k
  return true;
4255
18.7k
}
4256
4257
/// Checks if a the given expression evaluates to null.
4258
///
4259
/// Returns true if the value evaluates to null.
4260
30.8k
static bool CheckNonNullExpr(Sema &S, const Expr *Expr) {
4261
  // If the expression has non-null type, it doesn't evaluate to null.
4262
30.8k
  if (auto nullability
4263
2.61k
        = Expr->IgnoreImplicit()->getType()->getNullability(S.Context)) {
4264
2.61k
    if (*nullability == NullabilityKind::NonNull)
4265
2.49k
      return false;
4266
28.3k
  }
4267
4268
  // As a special case, transparent unions initialized with zero are
4269
  // considered null for the purposes of the nonnull attribute.
4270
28.3k
  if (const RecordType *UT = Expr->getType()->getAsUnionType()) {
4271
14
    if (UT->getDecl()->hasAttr<TransparentUnionAttr>())
4272
14
      if (const CompoundLiteralExpr *CLE =
4273
13
          dyn_cast<CompoundLiteralExpr>(Expr))
4274
13
        if (const InitListExpr *ILE =
4275
13
            dyn_cast<InitListExpr>(CLE->getInitializer()))
4276
13
          Expr = ILE->getInit(0);
4277
14
  }
4278
4279
28.3k
  bool Result;
4280
28.3k
  return (!Expr->isValueDependent() &&
4281
28.3k
          Expr->EvaluateAsBooleanCondition(Result, S.Context) &&
4282
4.88k
          !Result);
4283
28.3k
}
4284
4285
static void CheckNonNullArgument(Sema &S,
4286
                                 const Expr *ArgExpr,
4287
29.5k
                                 SourceLocation CallSiteLoc) {
4288
29.5k
  if (CheckNonNullExpr(S, ArgExpr))
4289
197
    S.DiagRuntimeBehavior(CallSiteLoc, ArgExpr,
4290
197
                          S.PDiag(diag::warn_null_arg)
4291
197
                              << ArgExpr->getSourceRange());
4292
29.5k
}
4293
4294
1.71k
bool Sema::GetFormatNSStringIdx(const FormatAttr *Format, unsigned &Idx) {
4295
1.71k
  FormatStringInfo FSI;
4296
1.71k
  if ((GetFormatStringType(Format) == FST_NSString) &&
4297
632
      getFormatStringInfo(Format, false, &FSI)) {
4298
632
    Idx = FSI.FormatIdx;
4299
632
    return true;
4300
632
  }
4301
1.08k
  return false;
4302
1.08k
}
4303
4304
/// Diagnose use of %s directive in an NSString which is being passed
4305
/// as formatting string to formatting method.
4306
static void
4307
DiagnoseCStringFormatDirectiveInCFAPI(Sema &S,
4308
                                        const NamedDecl *FDecl,
4309
                                        Expr **Args,
4310
246k
                                        unsigned NumArgs) {
4311
246k
  unsigned Idx = 0;
4312
246k
  bool Format = false;
4313
246k
  ObjCStringFormatFamily SFFamily = FDecl->getObjCFStringFormattingFamily();
4314
246k
  if (SFFamily == ObjCStringFormatFamily::SFF_CFString) {
4315
24
    Idx = 2;
4316
24
    Format = true;
4317
24
  }
4318
246k
  else
4319
246k
    for (const auto *I : FDecl->specific_attrs<FormatAttr>()) {
4320
1.69k
      if (S.GetFormatNSStringIdx(I, Idx)) {
4321
618
        Format = true;
4322
618
        break;
4323
618
      }
4324
1.69k
    }
4325
246k
  if (!Format || 
NumArgs <= Idx642
)
4326
245k
    return;
4327
638
  const Expr *FormatExpr = Args[Idx];
4328
638
  if (const CStyleCastExpr *CSCE = dyn_cast<CStyleCastExpr>(FormatExpr))
4329
9
    FormatExpr = CSCE->getSubExpr();
4330
638
  const StringLiteral *FormatString;
4331
638
  if (const ObjCStringLiteral *OSL =
4332
610
      dyn_cast<ObjCStringLiteral>(FormatExpr->IgnoreParenImpCasts()))
4333
610
    FormatString = OSL->getString();
4334
28
  else
4335
28
    FormatString = dyn_cast<StringLiteral>(FormatExpr->IgnoreParenImpCasts());
4336
638
  if (!FormatString)
4337
26
    return;
4338
612
  if (S.FormatStringHasSArg(FormatString)) {
4339
55
    S.Diag(FormatExpr->getExprLoc(), diag::warn_objc_cdirective_format_string)
4340
55
      << "%s" << 1 << 1;
4341
55
    S.Diag(FDecl->getLocation(), diag::note_entity_declared_at)
4342
55
      << FDecl->getDeclName();
4343
55
  }
4344
612
}
4345
4346
/// Determine whether the given type has a non-null nullability annotation.
4347
7.80M
static bool isNonNullType(ASTContext &ctx, QualType type) {
4348
7.80M
  if (auto nullability = type->getNullability(ctx))
4349
29.3k
    return *nullability == NullabilityKind::NonNull;
4350
4351
7.77M
  return false;
4352
7.77M
}
4353
4354
static void CheckNonNullArguments(Sema &S,
4355
                                  const NamedDecl *FDecl,
4356
                                  const FunctionProtoType *Proto,
4357
                                  ArrayRef<const Expr *> Args,
4358
3.33M
                                  SourceLocation CallSiteLoc) {
4359
3.33M
  assert((FDecl || Proto) && "Need a function declaration or prototype");
4360
4361
  // Already checked by by constant evaluator.
4362
3.33M
  if (S.isConstantEvaluated())
4363
6.60k
    return;
4364
  // Check the attributes attached to the method/function itself.
4365
3.33M
  llvm::SmallBitVector NonNullArgs;
4366
3.33M
  if (FDecl) {
4367
    // Handle the nonnull attribute on the function/method declaration itself.
4368
1.44k
    for (const auto *NonNull : FDecl->specific_attrs<NonNullAttr>()) {
4369
1.44k
      if (!NonNull->args_size()) {
4370
        // Easy case: all pointer arguments are nonnull.
4371
475
        for (const auto *Arg : Args)
4372
921
          if (S.isValidPointerAttrType(Arg->getType()))
4373
877
            CheckNonNullArgument(S, Arg, CallSiteLoc);
4374
475
        return;
4375
475
      }
4376
4377
985
      
for (const ParamIdx &Idx : NonNull->args())967
{
4378
985
        unsigned IdxAST = Idx.getASTIndex();
4379
985
        if (IdxAST >= Args.size())
4380
2
          continue;
4381
983
        if (NonNullArgs.empty())
4382
516
          NonNullArgs.resize(Args.size());
4383
983
        NonNullArgs.set(IdxAST);
4384
983
      }
4385
967
    }
4386
3.32M
  }
4387
4388
3.33M
  if (FDecl && 
(3.32M
isa<FunctionDecl>(FDecl)3.32M
||
isa<ObjCMethodDecl>(FDecl)31.7k
)) {
4389
    // Handle the nonnull attribute on the parameters of the
4390
    // function/method.
4391
3.32M
    ArrayRef<ParmVarDecl*> parms;
4392
3.32M
    if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(FDecl))
4393
3.29M
      parms = FD->parameters();
4394
24.7k
    else
4395
24.7k
      parms = cast<ObjCMethodDecl>(FDecl)->parameters();
4396
4397
3.32M
    unsigned ParamIndex = 0;
4398
3.32M
    for (ArrayRef<ParmVarDecl*>::iterator I = parms.begin(), E = parms.end();
4399
8.84M
         I != E; 
++I, ++ParamIndex5.52M
) {
4400
5.52M
      const ParmVarDecl *PVD = *I;
4401
5.52M
      if (PVD->hasAttr<NonNullAttr>() ||
4402
5.52M
          isNonNullType(S.Context, PVD->getType())) {
4403
21.1k
        if (NonNullArgs.empty())
4404
16.7k
          NonNullArgs.resize(Args.size());
4405
4406
21.1k
        NonNullArgs.set(ParamIndex);
4407
21.1k
      }
4408
5.52M
    }
4409
10.7k
  } else {
4410
    // If we have a non-function, non-method declaration but no
4411
    // function prototype, try to dig out the function prototype.
4412
10.7k
    if (!Proto) {
4413
103
      if (const ValueDecl *VD = dyn_cast<ValueDecl>(FDecl)) {
4414
103
        QualType type = VD->getType().getNonReferenceType();
4415
103
        if (auto pointerType = type->getAs<PointerType>())
4416
36
          type = pointerType->getPointeeType();
4417
67
        else if (auto blockType = type->getAs<BlockPointerType>())
4418
67
          type = blockType->getPointeeType();
4419
        // FIXME: data member pointers?
4420
4421
        // Dig out the function prototype, if there is one.
4422
103
        Proto = type->getAs<FunctionProtoType>();
4423
103
      }
4424
103
    }
4425
4426
    // Fill in non-null argument information from the nullability
4427
    // information on the parameter types (if we have them).
4428
10.7k
    if (Proto) {
4429
10.6k
      unsigned Index = 0;
4430
18.8k
      for (auto paramType : Proto->getParamTypes()) {
4431
18.8k
        if (isNonNullType(S.Context, paramType)) {
4432
1.60k
          if (NonNullArgs.empty())
4433
1.33k
            NonNullArgs.resize(Args.size());
4434
4435
1.60k
          NonNullArgs.set(Index);
4436
1.60k
        }
4437
4438
18.8k
        ++Index;
4439
18.8k
      }
4440
10.6k
    }
4441
10.7k
  }
4442
4443
  // Check for non-null arguments.
4444
3.33M
  for (unsigned ArgIndex = 0, ArgIndexEnd = NonNullArgs.size();
4445
3.36M
       ArgIndex != ArgIndexEnd; 
++ArgIndex30.6k
) {
4446
30.6k
    if (NonNullArgs[ArgIndex])
4447
22.8k
      CheckNonNullArgument(S, Args[ArgIndex], CallSiteLoc);
4448
30.6k
  }
4449
3.33M
}
4450
4451
/// Handles the checks for format strings, non-POD arguments to vararg
4452
/// functions, NULL arguments passed to non-NULL parameters, and diagnose_if
4453
/// attributes.
4454
void Sema::checkCall(NamedDecl *FDecl, const FunctionProtoType *Proto,
4455
                     const Expr *ThisArg, ArrayRef<const Expr *> Args,
4456
                     bool IsMemberFunction, SourceLocation Loc,
4457
3.55M
                     SourceRange Range, VariadicCallType CallType) {
4458
  // FIXME: We should check as much as we can in the template definition.
4459
3.55M
  if (CurContext->isDependentContext())
4460
215k
    return;
4461
4462
  // Printf and scanf checking.
4463
3.33M
  llvm::SmallBitVector CheckedVarArgs;
4464
3.33M
  if (FDecl) {
4465
18.1k
    for (const auto *I : FDecl->specific_attrs<FormatAttr>()) {
4466
      // Only create vector if there are format attributes.
4467
18.1k
      CheckedVarArgs.resize(Args.size());
4468
4469
18.1k
      CheckFormatArguments(I, Args, IsMemberFunction, CallType, Loc, Range,
4470
18.1k
                           CheckedVarArgs);
4471
18.1k
    }
4472
3.33M
  }
4473
4474
  // Refuse POD arguments that weren't caught by the format string
4475
  // checks above.
4476
3.33M
  auto *FD = dyn_cast_or_null<FunctionDecl>(FDecl);
4477
3.33M
  if (CallType != VariadicDoesNotApply &&
4478
53.2k
      (!FD || 
FD->getBuiltinID() != Builtin::BI__noop51.9k
)) {
4479
52.1k
    unsigned NumParams = Proto ? Proto->getNumParams()
4480
1.15k
                       : FDecl && isa<FunctionDecl>(FDecl)
4481
0
                           ? cast<FunctionDecl>(FDecl)->getNumParams()
4482
1.15k
                       : FDecl && isa<ObjCMethodDecl>(FDecl)
4483
1.15k
                           ? cast<ObjCMethodDecl>(FDecl)->param_size()
4484
0
                       : 0;
4485
4486
116k
    for (unsigned ArgIdx = NumParams; ArgIdx < Args.size(); 
++ArgIdx63.6k
) {
4487
      // Args[ArgIdx] can be null in malformed code.
4488
63.6k
      if (const Expr *Arg = Args[ArgIdx]) {
4489
63.6k
        if (CheckedVarArgs.empty() || 
!CheckedVarArgs[ArgIdx]23.3k
)
4490
60.1k
          checkVariadicArgument(Arg, CallType);
4491
63.6k
      }
4492
63.6k
    }
4493
53.2k
  }
4494
4495
3.33M
  if (FDecl || 
Proto3.76k
) {
4496
3.33M
    CheckNonNullArguments(*this, FDecl, Proto, Args, Loc);
4497
4498
    // Type safety checking.
4499
3.33M
    if (FDecl) {
4500
3.33M
      for (const auto *I : FDecl->specific_attrs<ArgumentWithTypeTagAttr>())
4501
404
        CheckArgumentWithTypeTag(I, Args, Loc);
4502
3.33M
    }
4503
3.33M
  }
4504
4505
3.33M
  if (FDecl && 
FDecl->hasAttr<AllocAlignAttr>()3.33M
) {
4506
229
    auto *AA = FDecl->getAttr<AllocAlignAttr>();
4507
229
    const Expr *Arg = Args[AA->getParamIndex().getASTIndex()];
4508
229
    if (!Arg->isValueDependent()) {
4509
229
      Expr::EvalResult Align;
4510
229
      if (Arg->EvaluateAsInt(Align, Context)) {
4511
202
        const llvm::APSInt &I = Align.Val.getInt();
4512
202
        if (!I.isPowerOf2())
4513
16
          Diag(Arg->getExprLoc(), diag::warn_alignment_not_power_of_two)
4514
16
              << Arg->getSourceRange();
4515
4516
202
        if (I > Sema::MaximumAlignment)
4517
3
          Diag(Arg->getExprLoc(), diag::warn_assume_aligned_too_great)
4518
3
              << Arg->getSourceRange() << Sema::MaximumAlignment;
4519
202
      }
4520
229
    }
4521
229
  }
4522
4523
3.33M
  if (FD)
4524
3.30M
    diagnoseArgDependentDiagnoseIfAttrs(FD, ThisArg, Args, Loc);
4525
3.33M
}
4526
4527
/// CheckConstructorCall - Check a constructor call for correctness and safety
4528
/// properties not enforced by the C type system.
4529
void Sema::CheckConstructorCall(FunctionDecl *FDecl,
4530
                                ArrayRef<const Expr *> Args,
4531
                                const FunctionProtoType *Proto,
4532
327k
                                SourceLocation Loc) {
4533
327k
  VariadicCallType CallType =
4534
327k
    Proto->isVariadic() ? 
VariadicConstructor100
: VariadicDoesNotApply;
4535
327k
  checkCall(FDecl, Proto, /*ThisArg=*/nullptr, Args, /*IsMemberFunction=*/true,
4536
327k
            Loc, SourceRange(), CallType);
4537
327k
}
4538
4539
/// CheckFunctionCall - Check a direct function call for various correctness
4540
/// and safety properties not strictly enforced by the C type system.
4541
bool Sema::CheckFunctionCall(FunctionDecl *FDecl, CallExpr *TheCall,
4542
3.12M
                             const FunctionProtoType *Proto) {
4543
3.12M
  bool IsMemberOperatorCall = isa<CXXOperatorCallExpr>(TheCall) &&
4544
37.7k
                              isa<CXXMethodDecl>(FDecl);
4545
3.12M
  bool IsMemberFunction = isa<CXXMemberCallExpr>(TheCall) ||
4546
2.94M
                          IsMemberOperatorCall;
4547
3.12M
  VariadicCallType CallType = getVariadicCallType(FDecl, Proto,
4548
3.12M
                                                  TheCall->getCallee());
4549
3.12M
  Expr** Args = TheCall->getArgs();
4550
3.12M
  unsigned NumArgs = TheCall->getNumArgs();
4551
4552
3.12M
  Expr *ImplicitThis = nullptr;
4553
3.12M
  if (IsMemberOperatorCall) {
4554
    // If this is a call to a member operator, hide the first argument
4555
    // from checkCall.
4556
    // FIXME: Our choice of AST representation here is less than ideal.
4557
37.5k
    ImplicitThis = Args[0];
4558
37.5k
    ++Args;
4559
37.5k
    --NumArgs;
4560
3.08M
  } else if (IsMemberFunction)
4561
182k
    ImplicitThis =
4562
182k
        cast<CXXMemberCallExpr>(TheCall)->getImplicitObjectArgument();
4563
4564
3.12M
  checkCall(FDecl, Proto, ImplicitThis, llvm::makeArrayRef(Args, NumArgs),
4565
3.12M
            IsMemberFunction, TheCall->getRParenLoc(),
4566
3.12M
            TheCall->getCallee()->getSourceRange(), CallType);
4567
4568
3.12M
  IdentifierInfo *FnInfo = FDecl->getIdentifier();
4569
  // None of the checks below are needed for functions that don't have
4570
  // simple names (e.g., C++ conversion functions).
4571
3.12M
  if (!FnInfo)
4572
53.9k
    return false;
4573
4574
3.06M
  CheckTCBEnforcement(TheCall, FDecl);
4575
4576
3.06M
  CheckAbsoluteValueFunction(TheCall, FDecl);
4577
3.06M
  CheckMaxUnsignedZero(TheCall, FDecl);
4578
4579
3.06M
  if (getLangOpts().ObjC)
4580
246k
    DiagnoseCStringFormatDirectiveInCFAPI(*this, FDecl, Args, NumArgs);
4581
4582
3.06M
  unsigned CMId = FDecl->getMemoryFunctionKind();
4583
4584
  // Handle memory setting and copying functions.
4585
3.06M
  switch (CMId) {
4586
3.06M
  case 0:
4587
3.06M
    return false;
4588
102
  case Builtin::BIstrlcpy: // fallthrough
4589
151
  case Builtin::BIstrlcat:
4590
151
    CheckStrlcpycatArguments(TheCall, FnInfo);
4591
151
    break;
4592
173
  case Builtin::BIstrncat:
4593
173
    CheckStrncatArguments(TheCall, FnInfo);
4594
173
    break;
4595
477
  case Builtin::BIfree:
4596
477
    CheckFreeArguments(TheCall);
4597
477
    break;
4598
8.37k
  default:
4599
8.37k
    CheckMemaccessArguments(TheCall, CMId, FnInfo);
4600
3.06M
  }
4601
4602
9.18k
  return false;
4603
3.06M
}
4604
4605
bool Sema::CheckObjCMethodCall(ObjCMethodDecl *Method, SourceLocation lbrac,
4606
24.8k
                               ArrayRef<const Expr *> Args) {
4607
24.8k
  VariadicCallType CallType =
4608
23.6k
      Method->isVariadic() ? 
VariadicMethod1.15k
: VariadicDoesNotApply;
4609
4610
24.8k
  checkCall(Method, nullptr, /*ThisArg=*/nullptr, Args,
4611
24.8k
            /*IsMemberFunction=*/false, lbrac, Method->getSourceRange(),
4612
24.8k
            CallType);
4613
4614
24.8k
  return false;
4615
24.8k
}
4616
4617
bool Sema::CheckPointerCall(NamedDecl *NDecl, CallExpr *TheCall,
4618
7.85k
                            const FunctionProtoType *Proto) {
4619
7.85k
  QualType Ty;
4620
7.85k
  if (const auto *V = dyn_cast<VarDecl>(NDecl))
4621
4.18k
    Ty = V->getType().getNonReferenceType();
4622
3.66k
  else if (const auto *F = dyn_cast<FieldDecl>(NDecl))
4623
3.63k
    Ty = F->getType().getNonReferenceType();
4624
35
  else
4625
35
    return false;
4626
4627
7.81k
  if (!Ty->isBlockPointerType() && 
!Ty->isFunctionPointerType()7.12k
&&
4628
11
      !Ty->isFunctionProtoType())
4629
3
    return false;
4630
4631
7.81k
  VariadicCallType CallType;
4632
7.81k
  if (!Proto || 
!Proto->isVariadic()7.71k
) {
4633
7.73k
    CallType = VariadicDoesNotApply;
4634
77
  } else if (Ty->isBlockPointerType()) {
4635
35
    CallType = VariadicBlock;
4636
42
  } else { // Ty->isFunctionPointerType()
4637
42
    CallType = VariadicFunction;
4638
42
  }
4639
4640
7.81k
  checkCall(NDecl, Proto, /*ThisArg=*/nullptr,
4641
7.81k
            llvm::makeArrayRef(TheCall->getArgs(), TheCall->getNumArgs()),
4642
7.81k
            /*IsMemberFunction=*/false, TheCall->getRParenLoc(),
4643
7.81k
            TheCall->getCallee()->getSourceRange(), CallType);
4644
4645
7.81k
  return false;
4646
7.81k
}
4647
4648
/// Checks function calls when a FunctionDecl or a NamedDecl is not available,
4649
/// such as function pointers returned from functions.
4650
3.99k
bool Sema::CheckOtherCall(CallExpr *TheCall, const FunctionProtoType *Proto) {
4651
3.99k
  VariadicCallType CallType = getVariadicCallType(/*FDecl=*/nullptr, Proto,
4652
3.99k
                                                  TheCall->getCallee());
4653
3.99k
  checkCall(/*FDecl=*/nullptr, Proto, /*ThisArg=*/nullptr,
4654
3.99k
            llvm::makeArrayRef(TheCall->getArgs(), TheCall->getNumArgs()),
4655
3.99k
            /*IsMemberFunction=*/false, TheCall->getRParenLoc(),
4656
3.99k
            TheCall->getCallee()->getSourceRange(), CallType);
4657
4658
3.99k
  return false;
4659
3.99k
}
4660
4661
2.47k
static bool isValidOrderingForOp(int64_t Ordering, AtomicExpr::AtomicOp Op) {
4662
2.47k
  if (!llvm::isValidAtomicOrderingCABI(Ordering))
4663
2
    return false;
4664
4665
2.47k
  auto OrderingCABI = (llvm::AtomicOrderingCABI)Ordering;
4666
2.47k
  switch (Op) {
4667
0
  case AtomicExpr::AO__c11_atomic_init:
4668
0
  case AtomicExpr::AO__opencl_atomic_init:
4669
0
    llvm_unreachable("There is no ordering argument for an init");
4670
4671
58
  case AtomicExpr::AO__c11_atomic_load:
4672
116
  case AtomicExpr::AO__opencl_atomic_load:
4673
553
  case AtomicExpr::AO__atomic_load_n:
4674
664
  case AtomicExpr::AO__atomic_load:
4675
664
    return OrderingCABI != llvm::AtomicOrderingCABI::release &&
4676
657
           OrderingCABI != llvm::AtomicOrderingCABI::acq_rel;
4677
4678
54
  case AtomicExpr::AO__c11_atomic_store:
4679
96
  case AtomicExpr::AO__opencl_atomic_store:
4680
195
  case AtomicExpr::AO__atomic_store:
4681
222
  case AtomicExpr::AO__atomic_store_n:
4682
222
    return OrderingCABI != llvm::AtomicOrderingCABI::consume &&
4683
219
           OrderingCABI != llvm::AtomicOrderingCABI::acquire &&
4684
214
           OrderingCABI != llvm::AtomicOrderingCABI::acq_rel;
4685
4686
1.58k
  default:
4687
1.58k
    return true;
4688
2.47k
  }
4689
2.47k
}
4690
4691
ExprResult Sema::SemaAtomicOpsOverloaded(ExprResult TheCallResult,
4692
5.35k
                                         AtomicExpr::AtomicOp Op) {
4693
5.35k
  CallExpr *TheCall = cast<CallExpr>(TheCallResult.get());
4694
5.35k
  DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts());
4695
5.35k
  MultiExprArg Args{TheCall->getArgs(), TheCall->getNumArgs()};
4696
5.35k
  return BuildAtomicExpr({TheCall->getBeginLoc(), TheCall->getEndLoc()},
4697
5.35k
                         DRE->getSourceRange(), TheCall->getRParenLoc(), Args,
4698
5.35k
                         Op);
4699
5.35k
}
4700
4701
ExprResult Sema::BuildAtomicExpr(SourceRange CallRange, SourceRange ExprRange,
4702
                                 SourceLocation RParenLoc, MultiExprArg Args,
4703
                                 AtomicExpr::AtomicOp Op,
4704
5.35k
                                 AtomicArgumentOrder ArgOrder) {
4705
  // All the non-OpenCL operations take one of the following forms.
4706
  // The OpenCL operations take the __c11 forms with one extra argument for
4707
  // synchronization scope.
4708
5.35k
  enum {
4709
    // C    __c11_atomic_init(A *, C)
4710
5.35k
    Init,
4711
4712
    // C    __c11_atomic_load(A *, int)
4713
5.35k
    Load,
4714
4715
    // void __atomic_load(A *, CP, int)
4716
5.35k
    LoadCopy,
4717
4718
    // void __atomic_store(A *, CP, int)
4719
5.35k
    Copy,
4720
4721
    // C    __c11_atomic_add(A *, M, int)
4722
5.35k
    Arithmetic,
4723
4724
    // C    __atomic_exchange_n(A *, CP, int)
4725
5.35k
    Xchg,
4726
4727
    // void __atomic_exchange(A *, C *, CP, int)
4728
5.35k
    GNUXchg,
4729
4730
    // bool __c11_atomic_compare_exchange_strong(A *, C *, CP, int, int)
4731
5.35k
    C11CmpXchg,
4732
4733
    // bool __atomic_compare_exchange(A *, C *, CP, bool, int, int)
4734
5.35k
    GNUCmpXchg
4735
5.35k
  } Form = Init;
4736
4737
5.35k
  const unsigned NumForm = GNUCmpXchg + 1;
4738
5.35k
  const unsigned NumArgs[] = { 2, 2, 3, 3, 3, 3, 4, 5, 6 };
4739
5.35k
  const unsigned NumVals[] = { 1, 0, 1, 1, 1, 1, 2, 2, 3 };
4740
  // where:
4741
  //   C is an appropriate type,
4742
  //   A is volatile _Atomic(C) for __c11 builtins and is C for GNU builtins,
4743
  //   CP is C for __c11 builtins and GNU _n builtins and is C * otherwise,
4744
  //   M is C if C is an integer, and ptrdiff_t if C is a pointer, and
4745
  //   the int parameters are for orderings.
4746
4747
5.35k
  static_assert(sizeof(NumArgs)/sizeof(NumArgs[0]) == NumForm
4748
5.35k
      && sizeof(NumVals)/sizeof(NumVals[0]) == NumForm,
4749
5.35k
      "need to update code for modified forms");
4750
5.35k
  static_assert(AtomicExpr::AO__c11_atomic_init == 0 &&
4751
5.35k
                    AtomicExpr::AO__c11_atomic_fetch_min + 1 ==
4752
5.35k
                        AtomicExpr::AO__atomic_load,
4753
5.35k
                "need to update code for modified C11 atomics");
4754
5.35k
  bool IsOpenCL = Op >= AtomicExpr::AO__opencl_atomic_init &&
4755
328
                  Op <= AtomicExpr::AO__opencl_atomic_fetch_max;
4756
5.35k
  bool IsC11 = (Op >= AtomicExpr::AO__c11_atomic_init &&
4757
5.35k
               Op <= AtomicExpr::AO__c11_atomic_fetch_min) ||
4758
2.13k
               IsOpenCL;
4759
5.35k
  bool IsN = Op == AtomicExpr::AO__atomic_load_n ||
4760
4.91k
             Op == AtomicExpr::AO__atomic_store_n ||
4761
4.88k
             Op == AtomicExpr::AO__atomic_exchange_n ||
4762
4.86k
             Op == AtomicExpr::AO__atomic_compare_exchange_n;
4763
5.35k
  bool IsAddSub = false;
4764
4765
5.35k
  switch (Op) {
4766
35
  case AtomicExpr::AO__c11_atomic_init:
4767
49
  case AtomicExpr::AO__opencl_atomic_init:
4768
49
    Form = Init;
4769
49
    break;
4770
4771
953
  case AtomicExpr::AO__c11_atomic_load:
4772
1.02k
  case AtomicExpr::AO__opencl_atomic_load:
4773
1.46k
  case AtomicExpr::AO__atomic_load_n:
4774
1.46k
    Form = Load;
4775
1.46k
    break;
4776
4777
113
  case AtomicExpr::AO__atomic_load:
4778
113
    Form = LoadCopy;
4779
113
    break;
4780
4781
955
  case AtomicExpr::AO__c11_atomic_store:
4782
1.00k
  case AtomicExpr::AO__opencl_atomic_store:
4783
1.10k
  case AtomicExpr::AO__atomic_store:
4784
1.13k
  case AtomicExpr::AO__atomic_store_n:
4785
1.13k
    Form = Copy;
4786
1.13k
    break;
4787
4788
175
  case AtomicExpr::AO__c11_atomic_fetch_add:
4789
277
  case AtomicExpr::AO__c11_atomic_fetch_sub:
4790
295
  case AtomicExpr::AO__opencl_atomic_fetch_add:
4791
305
  case AtomicExpr::AO__opencl_atomic_fetch_sub:
4792
336
  case AtomicExpr::AO__atomic_fetch_add:
4793
368
  case AtomicExpr::AO__atomic_fetch_sub:
4794
1.18k
  case AtomicExpr::AO__atomic_add_fetch:
4795
1.19k
  case AtomicExpr::AO__atomic_sub_fetch:
4796
1.19k
    IsAddSub = true;
4797
1.19k
    LLVM_FALLTHROUGH;
4798
1.21k
  case AtomicExpr::AO__c11_atomic_fetch_and:
4799
1.23k
  case AtomicExpr::AO__c11_atomic_fetch_or:
4800
1.24k
  case AtomicExpr::AO__c11_atomic_fetch_xor:
4801
1.26k
  case AtomicExpr::AO__opencl_atomic_fetch_and:
4802
1.27k
  case AtomicExpr::AO__opencl_atomic_fetch_or:
4803
1.28k
  case AtomicExpr::AO__opencl_atomic_fetch_xor:
4804
1.29k
  case AtomicExpr::AO__atomic_fetch_and:
4805
1.31k
  case AtomicExpr::AO__atomic_fetch_or:
4806
1.32k
  case AtomicExpr::AO__atomic_fetch_xor:
4807
1.33k
  case AtomicExpr::AO__atomic_fetch_nand:
4808
1.34k
  case AtomicExpr::AO__atomic_and_fetch:
4809
1.35k
  case AtomicExpr::AO__atomic_or_fetch:
4810
1.36k
  case AtomicExpr::AO__atomic_xor_fetch:
4811
1.37k
  case AtomicExpr::AO__atomic_nand_fetch:
4812
1.38k
  case AtomicExpr::AO__c11_atomic_fetch_min:
4813
1.40k
  case AtomicExpr::AO__c11_atomic_fetch_max:
4814
1.41k
  case AtomicExpr::AO__opencl_atomic_fetch_min:
4815
1.42k
  case AtomicExpr::AO__opencl_atomic_fetch_max:
4816
1.44k
  case AtomicExpr::AO__atomic_min_fetch:
4817
1.45k
  case AtomicExpr::AO__atomic_max_fetch:
4818
1.46k
  case AtomicExpr::AO__atomic_fetch_min:
4819
1.47k
  case AtomicExpr::AO__atomic_fetch_max:
4820
1.47k
    Form = Arithmetic;
4821
1.47k
    break;
4822
4823
850
  case AtomicExpr::AO__c11_atomic_exchange:
4824
866
  case AtomicExpr::AO__opencl_atomic_exchange:
4825
884
  case AtomicExpr::AO__atomic_exchange_n:
4826
884
    Form = Xchg;
4827
884
    break;
4828
4829
32
  case AtomicExpr::AO__atomic_exchange:
4830
32
    Form = GNUXchg;
4831
32
    break;
4832
4833
44
  case AtomicExpr::AO__c11_atomic_compare_exchange_strong:
4834
68
  case AtomicExpr::AO__c11_atomic_compare_exchange_weak:
4835
98
  case AtomicExpr::AO__opencl_atomic_compare_exchange_strong:
4836
127
  case AtomicExpr::AO__opencl_atomic_compare_exchange_weak:
4837
127
    Form = C11CmpXchg;
4838
127
    break;
4839
4840
39
  case AtomicExpr::AO__atomic_compare_exchange:
4841
70
  case AtomicExpr::AO__atomic_compare_exchange_n:
4842
70
    Form = GNUCmpXchg;
4843
70
    break;
4844
5.35k
  }
4845
4846
5.35k
  unsigned AdjustedNumArgs = NumArgs[Form];
4847
5.35k
  if (IsOpenCL && 
Op != AtomicExpr::AO__opencl_atomic_init301
)
4848
287
    ++AdjustedNumArgs;
4849
  // Check we have the right number of arguments.
4850
5.35k
  if (Args.size() < AdjustedNumArgs) {
4851
5
    Diag(CallRange.getEnd(), diag::err_typecheck_call_too_few_args)
4852
5
        << 0 << AdjustedNumArgs << static_cast<unsigned>(Args.size())
4853
5
        << ExprRange;
4854
5
    return ExprError();
4855
5.35k
  } else if (Args.size() > AdjustedNumArgs) {
4856
4
    Diag(Args[AdjustedNumArgs]->getBeginLoc(),
4857
4
         diag::err_typecheck_call_too_many_args)
4858
4
        << 0 << AdjustedNumArgs << static_cast<unsigned>(Args.size())
4859
4
        << ExprRange;
4860
4
    return ExprError();
4861
4
  }
4862
4863
  // Inspect the first argument of the atomic operation.
4864
5.34k
  Expr *Ptr = Args[0];
4865
5.34k
  ExprResult ConvertedPtr = DefaultFunctionArrayLvalueConversion(Ptr);
4866
5.34k
  if (ConvertedPtr.isInvalid())
4867
1
    return ExprError();
4868
4869
5.34k
  Ptr = ConvertedPtr.get();
4870
5.34k
  const PointerType *pointerType = Ptr->getType()->getAs<PointerType>();
4871
5.34k
  if (!pointerType) {
4872
3
    Diag(ExprRange.getBegin(), diag::err_atomic_builtin_must_be_pointer)
4873
3
        << Ptr->getType() << Ptr->getSourceRange();
4874
3
    return ExprError();
4875
3
  }
4876
4877
  // For a __c11 builtin, this should be a pointer to an _Atomic type.
4878
5.34k
  QualType AtomTy = pointerType->getPointeeType(); // 'A'
4879
5.34k
  QualType ValType = AtomTy; // 'C'
4880
5.34k
  if (IsC11) {
4881
3.50k
    if (!AtomTy->isAtomicType()) {
4882
10
      Diag(ExprRange.getBegin(), diag::err_atomic_op_needs_atomic)
4883
10
          << Ptr->getType() << Ptr->getSourceRange();
4884
10
      return ExprError();
4885
10
    }
4886
3.49k
    if ((Form != Load && 
Form != LoadCopy2.47k
&&
AtomTy.isConstQualified()2.47k
) ||
4887
3.48k
        AtomTy.getAddressSpace() == LangAS::opencl_constant) {
4888
19
      Diag(ExprRange.getBegin(), diag::err_atomic_op_needs_non_const_atomic)
4889
15
          << (AtomTy.isConstQualified() ? 0 : 
14
) << Ptr->getType()
4890
19
          << Ptr->getSourceRange();
4891
19
      return ExprError();
4892
19
    }
4893
3.48k
    ValType = AtomTy->castAs<AtomicType>()->getValueType();
4894
1.83k
  } else if (Form != Load && 
Form != LoadCopy1.39k
) {
4895
1.28k
    if (ValType.isConstQualified()) {
4896
6
      Diag(ExprRange.getBegin(), diag::err_atomic_op_needs_non_const_pointer)
4897
6
          << Ptr->getType() << Ptr->getSourceRange();
4898
6
      return ExprError();
4899
6
    }
4900
5.30k
  }
4901
4902
  // For an arithmetic operation, the implied arithmetic must be well-formed.
4903
5.30k
  if (Form == Arithmetic) {
4904
    // gcc does not enforce these rules for GNU atomics, but we do so for sanity.
4905
1.47k
    if (IsAddSub && 
!ValType->isIntegerType()1.19k
4906
20
        && !ValType->isPointerType()) {
4907
6
      Diag(ExprRange.getBegin(), diag::err_atomic_op_needs_atomic_int_or_ptr)
4908
6
          << IsC11 << Ptr->getType() << Ptr->getSourceRange();
4909
6
      return ExprError();
4910
6
    }
4911
1.46k
    if (!IsAddSub && 
!ValType->isIntegerType()280
) {
4912
14
      Diag(ExprRange.getBegin(), diag::err_atomic_op_needs_atomic_int)
4913
14
          << IsC11 << Ptr->getType() << Ptr->getSourceRange();
4914
14
      return ExprError();
4915
14
    }
4916
1.45k
    if (IsC11 && 
ValType->isPointerType()431
&&
4917
8
        RequireCompleteType(Ptr->getBeginLoc(), ValType->getPointeeType(),
4918
1
                            diag::err_incomplete_type)) {
4919
1
      return ExprError();
4920
1
    }
4921
3.83k
  } else if (IsN && 
!ValType->isIntegerType()516
&&
!ValType->isPointerType()12
) {
4922
    // For __atomic_*_n operations, the value type must be a scalar integral or
4923
    // pointer type which is 1, 2, 4, 8 or 16 bytes in length.
4924
5
    Diag(ExprRange.getBegin(), diag::err_atomic_op_needs_atomic_int_or_ptr)
4925
5
        << IsC11 << Ptr->getType() << Ptr->getSourceRange();
4926
5
    return ExprError();
4927
5
  }
4928
4929
5.28k
  if (!IsC11 && 
!AtomTy.isTriviallyCopyableType(Context)1.81k
&&
4930
2
      !AtomTy->isScalarType()) {
4931
    // For GNU atomics, require a trivially-copyable type. This is not part of
4932
    // the GNU atomics specification, but we enforce it for sanity.
4933
2
    Diag(ExprRange.getBegin(), diag::err_atomic_op_needs_trivial_copy)
4934
2
        << Ptr->getType() << Ptr->getSourceRange();
4935
2
    return ExprError();
4936
2
  }
4937
4938
5.28k
  switch (ValType.getObjCLifetime()) {
4939
5.28k
  case Qualifiers::OCL_None:
4940
5.28k
  case Qualifiers::OCL_ExplicitNone:
4941
    // okay
4942
5.28k
    break;
4943
4944
0
  case Qualifiers::OCL_Weak:
4945
0
  case Qualifiers::OCL_Strong:
4946
0
  case Qualifiers::OCL_Autoreleasing:
4947
    // FIXME: Can this happen? By this point, ValType should be known
4948
    // to be trivially copyable.
4949
0
    Diag(ExprRange.getBegin(), diag::err_arc_atomic_ownership)
4950
0
        << ValType << Ptr->getSourceRange();
4951
0
    return ExprError();
4952
5.28k
  }
4953
4954
  // All atomic operations have an overload which takes a pointer to a volatile
4955
  // 'A'.  We shouldn't let the volatile-ness of the pointee-type inject itself
4956
  // into the result or the other operands. Similarly atomic_load takes a
4957
  // pointer to a const 'A'.
4958
5.28k
  ValType.removeLocalVolatile();
4959
5.28k
  ValType.removeLocalConst();
4960
5.28k
  QualType ResultType = ValType;
4961
5.28k
  if (Form == Copy || 
Form == LoadCopy4.16k
||
Form == GNUXchg4.05k
||
4962
4.02k
      Form == Init)
4963
1.29k
    ResultType = Context.VoidTy;
4964
3.98k
  else if (Form == C11CmpXchg || 
Form == GNUCmpXchg3.85k
)
4965
194
    ResultType = Context.BoolTy;
4966
4967
  // The type of a parameter passed 'by value'. In the GNU atomics, such
4968
  // arguments are actually passed as pointers.
4969
5.28k
  QualType ByValType = ValType; // 'CP'
4970
5.28k
  bool IsPassedByAddress = false;
4971
5.28k
  if (!IsC11 && 
!IsN1.81k
) {
4972
1.30k
    ByValType = Ptr->getType();
4973
1.30k
    IsPassedByAddress = true;
4974
1.30k
  }
4975
4976
5.28k
  SmallVector<Expr *, 5> APIOrderedArgs;
4977
5.28k
  if (ArgOrder == Sema::AtomicArgumentOrder::AST) {
4978
4
    APIOrderedArgs.push_back(Args[0]);
4979
4
    switch (Form) {
4980
0
    case Init:
4981
0
    case Load:
4982
0
      APIOrderedArgs.push_back(Args[1]); // Val1/Order
4983
0
      break;
4984
0
    case LoadCopy:
4985
2
    case Copy:
4986
2
    case Arithmetic:
4987
2
    case Xchg:
4988
2
      APIOrderedArgs.push_back(Args[2]); // Val1
4989
2
      APIOrderedArgs.push_back(Args[1]); // Order
4990
2
      break;
4991
0
    case GNUXchg:
4992
0
      APIOrderedArgs.push_back(Args[2]); // Val1
4993
0
      APIOrderedArgs.push_back(Args[3]); // Val2
4994
0
      APIOrderedArgs.push_back(Args[1]); // Order
4995
0
      break;
4996
0
    case C11CmpXchg:
4997
0
      APIOrderedArgs.push_back(Args[2]); // Val1
4998
0
      APIOrderedArgs.push_back(Args[4]); // Val2
4999
0
      APIOrderedArgs.push_back(Args[1]); // Order
5000
0
      APIOrderedArgs.push_back(Args[3]); // OrderFail
5001
0
      break;
5002
2
    case GNUCmpXchg:
5003
2
      APIOrderedArgs.push_back(Args[2]); // Val1
5004
2
      APIOrderedArgs.push_back(Args[4]); // Val2
5005
2
      APIOrderedArgs.push_back(Args[5]); // Weak
5006
2
      APIOrderedArgs.push_back(Args[1]); // Order
5007
2
      APIOrderedArgs.push_back(Args[3]); // OrderFail
5008
2
      break;
5009
5.27k
    }
5010
5.27k
  } else
5011
5.27k
    APIOrderedArgs.append(Args.begin(), Args.end());
5012
5013
  // The first argument's non-CV pointer type is used to deduce the type of
5014
  // subsequent arguments, except for:
5015
  //  - weak flag (always converted to bool)
5016
  //  - memory order (always converted to int)
5017
  //  - scope  (always converted to int)
5018
20.3k
  
for (unsigned i = 0; 5.28k
i != APIOrderedArgs.size();
++i15.0k
) {
5019
15.0k
    QualType Ty;
5020
15.0k
    if (i < NumVals[Form] + 1) {
5021
9.39k
      switch (i) {
5022
5.28k
      case 0:
5023
        // The first argument is always a pointer. It has a fixed type.
5024
        // It is always dereferenced, a nullptr is undefined.
5025
5.28k
        CheckNonNullArgument(*this, APIOrderedArgs[i], ExprRange.getBegin());
5026
        // Nothing else to do: we already know all we want about this pointer.
5027
5.28k
        continue;
5028
3.82k
      case 1:
5029
        // The second argument is the non-atomic operand. For arithmetic, this
5030
        // is always passed by value, and for a compare_exchange it is always
5031
        // passed by address. For the rest, GNU uses by-address and C11 uses
5032
        // by-value.
5033
3.82k
        assert(Form != Load);
5034
3.82k
        if (Form == Init || 
(3.78k
Form == Arithmetic3.78k
&&
ValType->isIntegerType()1.45k
))
5035
1.47k
          Ty = ValType;
5036
2.34k
        else if (Form == Copy || 
Form == Xchg1.22k
) {
5037
1.99k
          if (IsPassedByAddress) {
5038
            // The value pointer is always dereferenced, a nullptr is undefined.
5039
99
            CheckNonNullArgument(*this, APIOrderedArgs[i],
5040
99
                                 ExprRange.getBegin());
5041
99
          }
5042
1.99k
          Ty = ByValType;
5043
349
        } else if (Form == Arithmetic)
5044
13
          Ty = Context.getPointerDiffType();
5045
336
        else {
5046
336
          Expr *ValArg = APIOrderedArgs[i];
5047
          // The value pointer is always dereferenced, a nullptr is undefined.
5048
336
          CheckNonNullArgument(*this, ValArg, ExprRange.getBegin());
5049
336
          LangAS AS = LangAS::Default;
5050
          // Keep address space of non-atomic pointer type.
5051
336
          if (const PointerType *PtrTy =
5052
331
                  ValArg->getType()->getAs<PointerType>()) {
5053
331
            AS = PtrTy->getPointeeType().getAddressSpace();
5054
331
          }
5055
336
          Ty = Context.getPointerType(
5056
336
              Context.getAddrSpaceQualType(ValType.getUnqualifiedType(), AS));
5057
336
        }
5058
3.82k
        break;
5059
225
      case 2:
5060
        // The third argument to compare_exchange / GNU exchange is the desired
5061
        // value, either by-value (for the C11 and *_n variant) or as a pointer.
5062
225
        if (IsPassedByAddress)
5063
69
          CheckNonNullArgument(*this, APIOrderedArgs[i], ExprRange.getBegin());
5064
225
        Ty = ByValType;
5065
225
        break;
5066
68
      case 3:
5067
        // The fourth argument to GNU compare_exchange is a 'weak' flag.
5068
68
        Ty = Context.BoolTy;
5069
68
        break;
5070
5.69k
      }
5071
5.69k
    } else {
5072
      // The order(s) and scope are always converted to int.
5073
5.69k
      Ty = Context.IntTy;
5074
5.69k
    }
5075
5076
9.81k
    InitializedEntity Entity =
5077
9.81k
        InitializedEntity::InitializeParameter(Context, Ty, false);
5078
9.81k
    ExprResult Arg = APIOrderedArgs[i];
5079
9.81k
    Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg);
5080
9.81k
    if (Arg.isInvalid())
5081
4
      return true;
5082
9.80k
    APIOrderedArgs[i] = Arg.get();
5083
9.80k
  }
5084
5085
  // Permute the arguments into a 'consistent' order.
5086
5.27k
  SmallVector<Expr*, 5> SubExprs;
5087
5.27k
  SubExprs.push_back(Ptr);
5088
5.27k
  switch (Form) {
5089
39
  case Init:
5090
    // Note, AtomicExpr::getVal1() has a special case for this atomic.
5091
39
    SubExprs.push_back(APIOrderedArgs[1]); // Val1
5092
39
    break;
5093
1.45k
  case Load:
5094
1.45k
    SubExprs.push_back(APIOrderedArgs[1]); // Order
5095
1.45k
    break;
5096
111
  case LoadCopy:
5097
1.22k
  case Copy:
5098
2.68k
  case Arithmetic:
5099
3.56k
  case Xchg:
5100
3.56k
    SubExprs.push_back(APIOrderedArgs[2]); // Order
5101
3.56k
    SubExprs.push_back(APIOrderedArgs[1]); // Val1
5102
3.56k
    break;
5103
31
  case GNUXchg:
5104
    // Note, AtomicExpr::getVal2() has a special case for this atomic.
5105
31
    SubExprs.push_back(APIOrderedArgs[3]); // Order
5106
31
    SubExprs.push_back(APIOrderedArgs[1]); // Val1
5107
31
    SubExprs.push_back(APIOrderedArgs[2]); // Val2
5108
31
    break;
5109
126
  case C11CmpXchg:
5110
126
    SubExprs.push_back(APIOrderedArgs[3]); // Order
5111
126
    SubExprs.push_back(APIOrderedArgs[1]); // Val1
5112
126
    SubExprs.push_back(APIOrderedArgs[4]); // OrderFail
5113
126
    SubExprs.push_back(APIOrderedArgs[2]); // Val2
5114
126
    break;
5115
68
  case GNUCmpXchg:
5116
68
    SubExprs.push_back(APIOrderedArgs[4]); // Order
5117
68
    SubExprs.push_back(APIOrderedArgs[1]); // Val1
5118
68
    SubExprs.push_back(APIOrderedArgs[5]); // OrderFail
5119
68
    SubExprs.push_back(APIOrderedArgs[2]); // Val2
5120
68
    SubExprs.push_back(APIOrderedArgs[3]); // Weak
5121
68
    break;
5122
5.27k
  }
5123
5124
5.27k
  if (SubExprs.size() >= 2 && Form != Init) {
5125
5.23k
    if (Optional<llvm::APSInt> Result =
5126
2.47k
            SubExprs[1]->getIntegerConstantExpr(Context))
5127
2.47k
      if (!isValidOrderingForOp(Result->getSExtValue(), Op))
5128
27
        Diag(SubExprs[1]->getBeginLoc(),
5129
27
             diag::warn_atomic_op_has_invalid_memory_order)
5130
27
            << SubExprs[1]->getSourceRange();
5131
5.23k
  }
5132
5133
5.27k
  if (auto ScopeModel = AtomicExpr::getScopeModel(Op)) {
5134
257
    auto *Scope = Args[Args.size() - 1];
5135
257
    if (Optional<llvm::APSInt> Result =
5136
245
            Scope->getIntegerConstantExpr(Context)) {
5137
245
      if (!ScopeModel->isValid(Result->getZExtValue()))
5138
4
        Diag(Scope->getBeginLoc(), diag::err_atomic_op_has_invalid_synch_scope)
5139
4
            << Scope->getSourceRange();
5140
245
    }
5141
257
    SubExprs.push_back(Scope);
5142
257
  }
5143
5144
5.27k
  AtomicExpr *AE = new (Context)
5145
5.27k
      AtomicExpr(ExprRange.getBegin(), SubExprs, ResultType, Op, RParenLoc);
5146
5147
5.27k
  if ((Op == AtomicExpr::AO__c11_atomic_load ||
5148
4.32k
       Op == AtomicExpr::AO__c11_atomic_store ||
5149
3.37k
       Op == AtomicExpr::AO__opencl_atomic_load ||
5150
3.31k
       Op == AtomicExpr::AO__opencl_atomic_store ) &&
5151
2.00k
      Context.AtomicUsesUnsupportedLibcall(AE))
5152
2
    Diag(AE->getBeginLoc(), diag::err_atomic_load_store_uses_lib)
5153
2
        << ((Op == AtomicExpr::AO__c11_atomic_load ||
5154
1
             Op == AtomicExpr::AO__opencl_atomic_load)
5155
1
                ? 0
5156
1
                : 1);
5157
5158
5.27k
  if (ValType->isExtIntType()) {
5159
2
    Diag(Ptr->getExprLoc(), diag::err_atomic_builtin_ext_int_prohibit);