Coverage Report

Created: 2020-02-18 08:44

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