Coverage Report

Created: 2021-08-24 07:12

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