Coverage Report

Created: 2022-01-25 06:29

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