clang 23.0.0git
SemaPPC.cpp
Go to the documentation of this file.
1//===------ SemaPPC.cpp ------ PowerPC target-specific routines -----------===//
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 semantic analysis functions specific to PowerPC.
10//
11//===----------------------------------------------------------------------===//
12
13#include "clang/Sema/SemaPPC.h"
15#include "clang/AST/Attr.h"
16#include "clang/AST/CharUnits.h"
17#include "clang/AST/Decl.h"
18#include "clang/AST/Type.h"
23#include "clang/Sema/Sema.h"
24#include "llvm/ADT/APSInt.h"
25#include "llvm/TargetParser/PPCTargetParser.h"
26
27namespace clang {
28
30
32 const auto *ICE = dyn_cast<ImplicitCastExpr>(Arg->IgnoreParens());
33 if (!ICE)
34 return;
35
36 const auto *DR = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
37 if (!DR)
38 return;
39
40 const auto *PD = dyn_cast<ParmVarDecl>(DR->getDecl());
41 if (!PD || !PD->getType()->isRecordType())
42 return;
43
44 QualType ArgType = Arg->getType();
45 for (const FieldDecl *FD : ArgType->castAsRecordDecl()->fields()) {
46 if (const auto *AA = FD->getAttr<AlignedAttr>()) {
48 AA->getAlignment(getASTContext()));
49 if (Alignment.getQuantity() == 16) {
50 Diag(FD->getLocation(), diag::warn_not_xl_compatible) << FD;
51 Diag(Loc, diag::note_misaligned_member_used_here) << PD;
52 }
53 }
54 }
55}
56
57static bool isPPC_64Builtin(unsigned BuiltinID) {
58 // These builtins only work on PPC 64bit targets.
59 switch (BuiltinID) {
60 case PPC::BI__builtin_divde:
61 case PPC::BI__builtin_divdeu:
62 case PPC::BI__builtin_bpermd:
63 case PPC::BI__builtin_pdepd:
64 case PPC::BI__builtin_pextd:
65 case PPC::BI__builtin_ppc_cdtbcd:
66 case PPC::BI__builtin_ppc_cbcdtd:
67 case PPC::BI__builtin_ppc_addg6s:
68 case PPC::BI__builtin_ppc_ldarx:
69 case PPC::BI__builtin_ppc_stdcx:
70 case PPC::BI__builtin_ppc_tdw:
71 case PPC::BI__builtin_ppc_trapd:
72 case PPC::BI__builtin_ppc_cmpeqb:
73 case PPC::BI__builtin_ppc_setb:
74 case PPC::BI__builtin_ppc_mulhd:
75 case PPC::BI__builtin_ppc_mulhdu:
76 case PPC::BI__builtin_ppc_maddhd:
77 case PPC::BI__builtin_ppc_maddhdu:
78 case PPC::BI__builtin_ppc_maddld:
79 case PPC::BI__builtin_ppc_load8r:
80 case PPC::BI__builtin_ppc_store8r:
81 case PPC::BI__builtin_ppc_insert_exp:
82 case PPC::BI__builtin_ppc_extract_sig:
83 case PPC::BI__builtin_ppc_addex:
84 case PPC::BI__builtin_darn:
85 case PPC::BI__builtin_darn_raw:
86 case PPC::BI__builtin_ppc_compare_and_swaplp:
87 case PPC::BI__builtin_ppc_fetch_and_addlp:
88 case PPC::BI__builtin_ppc_fetch_and_andlp:
89 case PPC::BI__builtin_ppc_fetch_and_orlp:
90 case PPC::BI__builtin_ppc_fetch_and_swaplp:
91 case PPC::BI__builtin_amo_lwat:
92 case PPC::BI__builtin_amo_ldat:
93 case PPC::BI__builtin_amo_lwat_s:
94 case PPC::BI__builtin_amo_ldat_s:
95 case PPC::BI__builtin_amo_lwat_cond:
96 case PPC::BI__builtin_amo_ldat_cond:
97 case PPC::BI__builtin_amo_lwat_cond_s:
98 case PPC::BI__builtin_amo_ldat_cond_s:
99 case PPC::BI__builtin_amo_stwat:
100 case PPC::BI__builtin_amo_stdat:
101 case PPC::BI__builtin_amo_stwat_s:
102 case PPC::BI__builtin_amo_stdat_s:
103 case PPC::BI__builtin_amo_lwat_csne:
104 case PPC::BI__builtin_amo_ldat_csne:
105 case PPC::BI__builtin_amo_lwat_csne_s:
106 case PPC::BI__builtin_amo_ldat_csne_s:
107 return true;
108 }
109 return false;
110}
111
113 unsigned BuiltinID,
114 CallExpr *TheCall) {
115 ASTContext &Context = getASTContext();
116 bool IsTarget64Bit = TI.getTypeWidth(TI.getIntPtrType()) == 64;
117
118 if (isPPC_64Builtin(BuiltinID) && !IsTarget64Bit)
119 return Diag(TheCall->getBeginLoc(), diag::err_64_bit_builtin_32_bit_tgt)
120 << TheCall->getSourceRange();
121
122 // Common BCD type-validation helpers
123 // Emit error diagnostics and return true on success
124 // - IsTypeVecUChar: enforces vector unsigned char
125 // - IsIntType: enforces any integer type
126 // Lambdas centralize type checks for BCD builtin handlers
127
128 // Lambda 1: verify vector unsigned char type
129 auto IsTypeVecUChar = [&](QualType ArgTy, unsigned ArgIndex) -> bool {
130 QualType VecType = Context.getVectorType(Context.UnsignedCharTy, 16,
132 if (Context.hasSameType(ArgTy, VecType))
133 return true;
134
135 Diag(TheCall->getArg(ArgIndex)->getBeginLoc(),
136 diag::err_ppc_invalid_arg_type)
137 << ArgIndex << VecType << ArgTy;
138 return false;
139 };
140
141 // Lambda 2: verify integer type
142 auto IsIntType = [&](QualType ArgTy, unsigned ArgIndex) -> bool {
143 if (ArgTy->isIntegerType())
144 return true;
145
146 Diag(TheCall->getArg(ArgIndex)->getBeginLoc(),
147 diag::err_ppc_invalid_arg_type)
148 << ArgIndex << "integer" << ArgTy;
149 return false;
150 };
151
152 switch (BuiltinID) {
153 default:
154 return false;
155 case PPC::BI__builtin_ppc_bcdsetsign: {
156 // Arg0 must be vector unsigned char
157 if (!IsTypeVecUChar(TheCall->getArg(0)->getType(), 0))
158 return false;
159
160 // Restrict Arg1 constant range (0–1)
161 return SemaRef.BuiltinConstantArgRange(TheCall, 1, 0, 1);
162 }
163 case PPC::BI__builtin_ppc_national2packed:
164 case PPC::BI__builtin_ppc_packed2zoned:
165 case PPC::BI__builtin_ppc_zoned2packed:
166 return SemaRef.BuiltinConstantArgRange(TheCall, 1, 0, 1);
167 case PPC::BI__builtin_ppc_bcdshift:
168 case PPC::BI__builtin_ppc_bcdshiftround:
169 case PPC::BI__builtin_ppc_bcdtruncate: {
170
171 // Arg0 must be vector unsigned char
172 if (!IsTypeVecUChar(TheCall->getArg(0)->getType(), 0))
173 return false;
174
175 // Arg1 must be integer type
176 if (!IsIntType(TheCall->getArg(1)->getType(), 1))
177 return false;
178
179 // Restrict Arg2 constant range (0–1)
180 return SemaRef.BuiltinConstantArgRange(TheCall, 2, 0, 1);
181 }
182 case PPC::BI__builtin_altivec_crypto_vshasigmaw:
183 case PPC::BI__builtin_altivec_crypto_vshasigmad:
184 return SemaRef.BuiltinConstantArgRange(TheCall, 1, 0, 1) ||
185 SemaRef.BuiltinConstantArgRange(TheCall, 2, 0, 15);
186 case PPC::BI__builtin_altivec_dss:
187 return SemaRef.BuiltinConstantArgRange(TheCall, 0, 0, 3);
188 case PPC::BI__builtin_tbegin:
189 case PPC::BI__builtin_tend:
190 return SemaRef.BuiltinConstantArgRange(TheCall, 0, 0, 1);
191 case PPC::BI__builtin_tsr:
192 return SemaRef.BuiltinConstantArgRange(TheCall, 0, 0, 7);
193 case PPC::BI__builtin_tabortwc:
194 case PPC::BI__builtin_tabortdc:
195 return SemaRef.BuiltinConstantArgRange(TheCall, 0, 0, 31);
196 case PPC::BI__builtin_tabortwci:
197 case PPC::BI__builtin_tabortdci:
198 return SemaRef.BuiltinConstantArgRange(TheCall, 0, 0, 31) ||
199 SemaRef.BuiltinConstantArgRange(TheCall, 2, 0, 31);
200 // According to GCC 'Basic PowerPC Built-in Functions Available on ISA 2.05',
201 // __builtin_(un)pack_longdouble are available only if long double uses IBM
202 // extended double representation.
203 case PPC::BI__builtin_unpack_longdouble:
204 if (SemaRef.BuiltinConstantArgRange(TheCall, 1, 0, 1))
205 return true;
206 [[fallthrough]];
207 case PPC::BI__builtin_pack_longdouble:
208 if (&TI.getLongDoubleFormat() != &llvm::APFloat::PPCDoubleDouble())
209 return Diag(TheCall->getBeginLoc(), diag::err_ppc_builtin_requires_abi)
210 << "ibmlongdouble";
211 return false;
212 case PPC::BI__builtin_altivec_dst:
213 case PPC::BI__builtin_altivec_dstt:
214 case PPC::BI__builtin_altivec_dstst:
215 case PPC::BI__builtin_altivec_dststt:
216 return SemaRef.BuiltinConstantArgRange(TheCall, 2, 0, 3);
217 case PPC::BI__builtin_vsx_xxpermdi:
218 case PPC::BI__builtin_vsx_xxsldwi:
219 return BuiltinVSX(TheCall);
220 case PPC::BI__builtin_unpack_vector_int128:
221 return SemaRef.BuiltinConstantArgRange(TheCall, 1, 0, 1);
222 case PPC::BI__builtin_altivec_vgnb:
223 return SemaRef.BuiltinConstantArgRange(TheCall, 1, 2, 7);
224 case PPC::BI__builtin_vsx_xxeval:
225 return SemaRef.BuiltinConstantArgRange(TheCall, 3, 0, 255);
226 case PPC::BI__builtin_altivec_vsldbi:
227 return SemaRef.BuiltinConstantArgRange(TheCall, 2, 0, 7);
228 case PPC::BI__builtin_altivec_vsrdbi:
229 return SemaRef.BuiltinConstantArgRange(TheCall, 2, 0, 7);
230 case PPC::BI__builtin_vsx_xxpermx:
231 return SemaRef.BuiltinConstantArgRange(TheCall, 3, 0, 7);
232 case PPC::BI__builtin_ppc_tw:
233 case PPC::BI__builtin_ppc_tdw:
234 return SemaRef.BuiltinConstantArgRange(TheCall, 2, 1, 31);
235 case PPC::BI__builtin_ppc_cmprb:
236 return SemaRef.BuiltinConstantArgRange(TheCall, 0, 0, 1);
237 // For __rlwnm, __rlwimi and __rldimi, the last parameter mask must
238 // be a constant that represents a contiguous bit field.
239 case PPC::BI__builtin_ppc_rlwnm:
240 return SemaRef.ValueIsRunOfOnes(TheCall, 2);
241 case PPC::BI__builtin_ppc_rlwimi:
242 return SemaRef.BuiltinConstantArgRange(TheCall, 2, 0, 31) ||
243 SemaRef.ValueIsRunOfOnes(TheCall, 3);
244 case PPC::BI__builtin_ppc_rldimi:
245 return SemaRef.BuiltinConstantArgRange(TheCall, 2, 0, 63) ||
246 SemaRef.ValueIsRunOfOnes(TheCall, 3);
247 case PPC::BI__builtin_ppc_addex: {
248 if (SemaRef.BuiltinConstantArgRange(TheCall, 2, 0, 3))
249 return true;
250 // Output warning for reserved values 1 to 3.
251 int ArgValue =
252 TheCall->getArg(2)->getIntegerConstantExpr(Context)->getSExtValue();
253 if (ArgValue != 0)
254 Diag(TheCall->getBeginLoc(), diag::warn_argument_undefined_behaviour)
255 << ArgValue;
256 return false;
257 }
258 case PPC::BI__builtin_ppc_mtfsb0:
259 case PPC::BI__builtin_ppc_mtfsb1:
260 return SemaRef.BuiltinConstantArgRange(TheCall, 0, 0, 31);
261 case PPC::BI__builtin_ppc_mtfsf:
262 return SemaRef.BuiltinConstantArgRange(TheCall, 0, 0, 255);
263 case PPC::BI__builtin_ppc_mtfsfi:
264 return SemaRef.BuiltinConstantArgRange(TheCall, 0, 0, 7) ||
265 SemaRef.BuiltinConstantArgRange(TheCall, 1, 0, 15);
266 case PPC::BI__builtin_ppc_alignx:
267 return SemaRef.BuiltinConstantArgPower2(TheCall, 0);
268 case PPC::BI__builtin_ppc_rdlam:
269 return SemaRef.ValueIsRunOfOnes(TheCall, 2);
270 case PPC::BI__builtin_vsx_ldrmb:
271 case PPC::BI__builtin_vsx_strmb:
272 return SemaRef.BuiltinConstantArgRange(TheCall, 1, 1, 16);
273 case PPC::BI__builtin_altivec_vcntmbb:
274 case PPC::BI__builtin_altivec_vcntmbh:
275 case PPC::BI__builtin_altivec_vcntmbw:
276 case PPC::BI__builtin_altivec_vcntmbd:
277 return SemaRef.BuiltinConstantArgRange(TheCall, 1, 0, 1);
278 case PPC::BI__builtin_vsx_xxgenpcvbm:
279 case PPC::BI__builtin_vsx_xxgenpcvhm:
280 case PPC::BI__builtin_vsx_xxgenpcvwm:
281 case PPC::BI__builtin_vsx_xxgenpcvdm:
282 return SemaRef.BuiltinConstantArgRange(TheCall, 1, 0, 3);
283 case PPC::BI__builtin_ppc_test_data_class: {
284 // Check if the first argument of the __builtin_ppc_test_data_class call is
285 // valid. The argument must be 'float' or 'double' or '__float128'.
286 QualType ArgType = TheCall->getArg(0)->getType();
287 if (ArgType != QualType(Context.FloatTy) &&
288 ArgType != QualType(Context.DoubleTy) &&
289 ArgType != QualType(Context.Float128Ty))
290 return Diag(TheCall->getBeginLoc(),
291 diag::err_ppc_invalid_test_data_class_type);
292 return SemaRef.BuiltinConstantArgRange(TheCall, 1, 0, 127);
293 }
294 case PPC::BI__builtin_ppc_maxfe:
295 case PPC::BI__builtin_ppc_minfe:
296 case PPC::BI__builtin_ppc_maxfl:
297 case PPC::BI__builtin_ppc_minfl:
298 case PPC::BI__builtin_ppc_maxfs:
299 case PPC::BI__builtin_ppc_minfs: {
300 if (Context.getTargetInfo().getTriple().isOSAIX() &&
301 (BuiltinID == PPC::BI__builtin_ppc_maxfe ||
302 BuiltinID == PPC::BI__builtin_ppc_minfe))
303 return Diag(TheCall->getBeginLoc(), diag::err_target_unsupported_type)
304 << "builtin" << true << 128 << QualType(Context.LongDoubleTy)
305 << false << Context.getTargetInfo().getTriple().str();
306 // Argument type should be exact.
307 QualType ArgType = QualType(Context.LongDoubleTy);
308 if (BuiltinID == PPC::BI__builtin_ppc_maxfl ||
309 BuiltinID == PPC::BI__builtin_ppc_minfl)
310 ArgType = QualType(Context.DoubleTy);
311 else if (BuiltinID == PPC::BI__builtin_ppc_maxfs ||
312 BuiltinID == PPC::BI__builtin_ppc_minfs)
313 ArgType = QualType(Context.FloatTy);
314 for (unsigned I = 0, E = TheCall->getNumArgs(); I < E; ++I)
315 if (TheCall->getArg(I)->getType() != ArgType)
316 return Diag(TheCall->getBeginLoc(),
317 diag::err_typecheck_convert_incompatible)
318 << TheCall->getArg(I)->getType() << ArgType << 1 << 0 << 0;
319 return false;
320 }
321#define CUSTOM_BUILTIN(Name, Intr, Types, Acc, Feature) \
322 case PPC::BI__builtin_##Name: \
323 return BuiltinPPCMMACall(TheCall, BuiltinID, Types);
324#include "clang/Basic/BuiltinsPPC.def"
325 case PPC::BI__builtin_amo_lwat:
326 case PPC::BI__builtin_amo_ldat:
327 case PPC::BI__builtin_amo_lwat_s:
328 case PPC::BI__builtin_amo_ldat_s: {
329 llvm::APSInt Result;
330 if (SemaRef.BuiltinConstantArg(TheCall, 2, Result))
331 return true;
332 unsigned Val = Result.getZExtValue();
333
334 bool IsUnsigned = (BuiltinID == PPC::BI__builtin_amo_lwat ||
335 BuiltinID == PPC::BI__builtin_amo_ldat);
336
337 bool IsValid = IsUnsigned
338 ? llvm::is_contained({0u, 1u, 2u, 3u, 4u, 6u, 8u}, Val)
339 : llvm::is_contained({0u, 5u, 7u, 8u}, Val);
340
341 if (IsValid)
342 return false;
343
344 Expr *Arg = TheCall->getArg(2);
345 return SemaRef.Diag(Arg->getBeginLoc(), diag::err_argument_invalid_range)
346 << toString(Result, 10) << (IsUnsigned ? "0-4, 6" : "0, 5, 7") << "8"
347 << Arg->getSourceRange();
348 }
349 case PPC::BI__builtin_amo_lwat_cond:
350 case PPC::BI__builtin_amo_ldat_cond:
351 case PPC::BI__builtin_amo_lwat_cond_s:
352 case PPC::BI__builtin_amo_ldat_cond_s: {
353 llvm::APSInt Result;
354 if (SemaRef.BuiltinConstantArg(TheCall, 1, Result))
355 return true;
356 unsigned Val = Result.getZExtValue();
357 if (llvm::is_contained({24u, 25u, 28u}, Val))
358 return false;
359
360 Expr *Arg = TheCall->getArg(1);
361 return SemaRef.Diag(Arg->getBeginLoc(), diag::err_argument_invalid_range)
362 << toString(Result, 10) << "24, 25" << "28" << Arg->getSourceRange();
363 }
364 case PPC::BI__builtin_amo_stwat:
365 case PPC::BI__builtin_amo_stdat:
366 case PPC::BI__builtin_amo_stwat_s:
367 case PPC::BI__builtin_amo_stdat_s: {
368 llvm::APSInt Result;
369 if (SemaRef.BuiltinConstantArg(TheCall, 2, Result))
370 return true;
371 unsigned Val = Result.getZExtValue();
372
373 bool IsUnsigned = (BuiltinID == PPC::BI__builtin_amo_stwat ||
374 BuiltinID == PPC::BI__builtin_amo_stdat);
375
376 bool IsValid = IsUnsigned
377 ? llvm::is_contained({0u, 1u, 2u, 3u, 4u, 6u, 24u}, Val)
378 : llvm::is_contained({0u, 5u, 7u, 24u}, Val);
379
380 if (IsValid)
381 return false;
382
383 Expr *Arg = TheCall->getArg(2);
384 return SemaRef.Diag(Arg->getBeginLoc(), diag::err_argument_invalid_range)
385 << toString(Result, 10) << (IsUnsigned ? "0-4, 6" : "0, 5, 7")
386 << "24" << Arg->getSourceRange();
387 }
388 }
389 llvm_unreachable("must return from switch");
390}
391
392// Check if the given type is a non-pointer PPC MMA type. This function is used
393// in Sema to prevent invalid uses of restricted PPC MMA types.
395 ASTContext &Context = getASTContext();
396 if (Type->isPointerType() || Type->isArrayType())
397 return false;
398
399 QualType CoreType = Type.getCanonicalType().getUnqualifiedType();
400#define PPC_VECTOR_TYPE(Name, Id, Size) || CoreType == Context.Id##Ty
401 if (false
402#include "clang/Basic/PPCTypes.def"
403 ) {
404 Diag(TypeLoc, diag::err_ppc_invalid_use_mma_type);
405 return true;
406 }
407 return false;
408}
409
410/// DecodePPCMMATypeFromStr - This decodes one PPC MMA type descriptor from Str,
411/// advancing the pointer over the consumed characters. The decoded type is
412/// returned. If the decoded type represents a constant integer with a
413/// constraint on its value then Mask is set to that value. The type descriptors
414/// used in Str are specific to PPC MMA builtins and are documented in the file
415/// defining the PPC builtins.
416static QualType DecodePPCMMATypeFromStr(ASTContext &Context, const char *&Str,
417 unsigned &Mask) {
418 bool RequireICE = false;
420 switch (*Str++) {
421 case 'V':
422 return Context.getVectorType(Context.UnsignedCharTy, 16,
424 case 'i': {
425 char *End;
426 unsigned size = strtoul(Str, &End, 10);
427 assert(End != Str && "Missing constant parameter constraint");
428 Str = End;
429 Mask = size;
430 return Context.IntTy;
431 }
432 case 'W': {
433 char *End;
434 unsigned size = strtoul(Str, &End, 10);
435 assert(End != Str && "Missing PowerPC MMA type size");
436 Str = End;
438 switch (size) {
439#define PPC_VECTOR_TYPE(typeName, Id, size) \
440 case size: \
441 Type = Context.Id##Ty; \
442 break;
443#include "clang/Basic/PPCTypes.def"
444 default:
445 llvm_unreachable("Invalid PowerPC MMA vector type");
446 }
447 bool CheckVectorArgs = false;
448 while (!CheckVectorArgs) {
449 switch (*Str++) {
450 case '*':
451 Type = Context.getPointerType(Type);
452 break;
453 case 'C':
454 Type = Type.withConst();
455 break;
456 default:
457 CheckVectorArgs = true;
458 --Str;
459 break;
460 }
461 }
462 return Type;
463 }
464 default:
465 return Context.DecodeTypeStr(--Str, Context, Error, RequireICE, true);
466 }
467}
468
469bool SemaPPC::BuiltinPPCMMACall(CallExpr *TheCall, unsigned BuiltinID,
470 const char *TypeStr) {
471
472 assert((TypeStr[0] != '\0') &&
473 "Invalid types in PPC MMA builtin declaration");
474
475 ASTContext &Context = getASTContext();
476 unsigned Mask = 0;
477 unsigned ArgNum = 0;
478
479 // The first type in TypeStr is the type of the value returned by the
480 // builtin. So we first read that type and change the type of TheCall.
481 QualType type = DecodePPCMMATypeFromStr(Context, TypeStr, Mask);
482 TheCall->setType(type);
483
484 while (*TypeStr != '\0') {
485 Mask = 0;
486 QualType ExpectedType = DecodePPCMMATypeFromStr(Context, TypeStr, Mask);
487 if (ArgNum >= TheCall->getNumArgs()) {
488 ArgNum++;
489 break;
490 }
491
492 Expr *Arg = TheCall->getArg(ArgNum);
493 QualType PassedType = Arg->getType();
494 QualType StrippedRVType = PassedType.getCanonicalType();
495
496 // Strip Restrict/Volatile qualifiers.
497 if (StrippedRVType.isRestrictQualified() ||
498 StrippedRVType.isVolatileQualified())
499 StrippedRVType = StrippedRVType.getCanonicalType().getUnqualifiedType();
500
501 // The only case where the argument type and expected type are allowed to
502 // mismatch is if the argument type is a non-void pointer (or array) and
503 // expected type is a void pointer.
504 if (StrippedRVType != ExpectedType)
505 if (!(ExpectedType->isVoidPointerType() &&
506 (StrippedRVType->isPointerType() || StrippedRVType->isArrayType())))
507 return Diag(Arg->getBeginLoc(),
508 diag::err_typecheck_convert_incompatible)
509 << PassedType << ExpectedType << 1 << 0 << 0;
510
511 // If the value of the Mask is not 0, we have a constraint in the size of
512 // the integer argument so here we ensure the argument is a constant that
513 // is in the valid range.
514 if (Mask != 0 &&
515 SemaRef.BuiltinConstantArgRange(TheCall, ArgNum, 0, Mask, true))
516 return true;
517
518 ArgNum++;
519 }
520
521 // In case we exited early from the previous loop, there are other types to
522 // read from TypeStr. So we need to read them all to ensure we have the right
523 // number of arguments in TheCall and if it is not the case, to display a
524 // better error message.
525 while (*TypeStr != '\0') {
526 (void)DecodePPCMMATypeFromStr(Context, TypeStr, Mask);
527 ArgNum++;
528 }
529 if (SemaRef.checkArgCount(TheCall, ArgNum))
530 return true;
531
532 return false;
533}
534
536 unsigned ExpectedNumArgs = 3;
537 if (SemaRef.checkArgCount(TheCall, ExpectedNumArgs))
538 return true;
539
540 // Check the third argument is a compile time constant
541 if (!TheCall->getArg(2)->isIntegerConstantExpr(getASTContext()))
542 return Diag(TheCall->getBeginLoc(),
543 diag::err_vsx_builtin_nonconstant_argument)
544 << 3 /* argument index */ << TheCall->getDirectCallee()
545 << SourceRange(TheCall->getArg(2)->getBeginLoc(),
546 TheCall->getArg(2)->getEndLoc());
547
548 QualType Arg1Ty = TheCall->getArg(0)->getType();
549 QualType Arg2Ty = TheCall->getArg(1)->getType();
550
551 // Check the type of argument 1 and argument 2 are vectors.
552 SourceLocation BuiltinLoc = TheCall->getBeginLoc();
553 if ((!Arg1Ty->isVectorType() && !Arg1Ty->isDependentType()) ||
554 (!Arg2Ty->isVectorType() && !Arg2Ty->isDependentType())) {
555 return Diag(BuiltinLoc, diag::err_vec_builtin_non_vector)
556 << TheCall->getDirectCallee() << /*isMorethantwoArgs*/ false
557 << SourceRange(TheCall->getArg(0)->getBeginLoc(),
558 TheCall->getArg(1)->getEndLoc());
559 }
560
561 // Check the first two arguments are the same type.
562 if (!getASTContext().hasSameUnqualifiedType(Arg1Ty, Arg2Ty)) {
563 return Diag(BuiltinLoc, diag::err_vec_builtin_incompatible_vector)
564 << TheCall->getDirectCallee() << /*isMorethantwoArgs*/ false
565 << SourceRange(TheCall->getArg(0)->getBeginLoc(),
566 TheCall->getArg(1)->getEndLoc());
567 }
568
569 // When default clang type checking is turned off and the customized type
570 // checking is used, the returning type of the function must be explicitly
571 // set. Otherwise it is _Bool by default.
572 TheCall->setType(Arg1Ty);
573
574 return false;
575}
576
580 SourceLocation AttrLoc) {
581 using namespace DiagAttrParams;
582
583 assert(Params.size() == Locs.size() &&
584 "Mismatch between number of string parameters and locations");
585
587 bool HasDefault = false;
588 bool HasComma = false;
589 for (unsigned I = 0, E = Params.size(); I < E; ++I) {
590 const StringRef Param = Params[I].trim();
591 const SourceLocation &Loc = Locs[I];
592
593 if (Param.empty() || Param.ends_with(','))
594 return Diag(Loc, diag::warn_unsupported_target_attribute)
595 << Unsupported << None << "" << TargetClones;
596
597 if (Param.contains(','))
598 HasComma = true;
599
600 StringRef LHS;
601 StringRef RHS = Param;
602 do {
603 std::tie(LHS, RHS) = RHS.split(',');
604 LHS = LHS.trim();
605 const SourceLocation &CurLoc =
606 Loc.getLocWithOffset(LHS.data() - Param.data());
607
608 if (LHS.starts_with("cpu=")) {
609 StringRef CPUStr = LHS.drop_front(sizeof("cpu=") - 1);
610 if (!TargetInfo.isValidCPUName(CPUStr))
611 return Diag(CurLoc, diag::warn_unsupported_target_attribute)
612 << Unknown << CPU << CPUStr << TargetClones;
613 else if (!TargetInfo.validateCpuIs(CPUStr))
614 return Diag(CurLoc, diag::warn_unsupported_target_attribute)
615 << Unsupported << CPU << CPUStr << TargetClones;
616 } else if (LHS == "default") {
617 HasDefault = true;
618 } else {
619 // it's a feature string, but not supported yet.
620 return Diag(CurLoc, diag::warn_unsupported_target_attribute)
621 << Unsupported << None << LHS << TargetClones;
622 }
623 SmallString<64> CPU;
624 if (LHS.starts_with("cpu=")) {
625 CPU.append("cpu=");
626 CPU.append(
627 llvm::PPC::normalizeCPUName(LHS.drop_front(sizeof("cpu=") - 1)));
628 LHS = CPU.str();
629 }
630 if (llvm::is_contained(NewParams, LHS)) {
631 Diag(CurLoc, diag::warn_target_clone_duplicate_options);
632 continue;
633 }
634 NewParams.push_back(LHS);
635 } while (!RHS.empty());
636 }
637 if (HasComma && Params.size() > 1)
638 Diag(Locs[0], diag::warn_target_clone_mixed_values);
639
640 if (!HasDefault)
641 return Diag(AttrLoc, diag::err_target_clone_must_have_default);
642
643 return false;
644}
645} // namespace clang
Defines the clang::ASTContext interface.
static std::string toString(const clang::SanitizerSet &Sanitizers)
Produce a string containing comma-separated names of sanitizers in Sanitizers set.
This file declares semantic analysis functions specific to PowerPC.
Defines the clang::SourceLocation class and associated facilities.
Enumerates target-specific builtins in their own namespaces within namespace clang.
C Language Family Type Representation.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:226
const TargetInfo & getTargetInfo() const
Definition ASTContext.h:917
CharUnits toCharUnitsFromBits(int64_t BitSize) const
Convert a size in bits to a size in characters.
@ GE_None
No error.
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition Expr.h:2946
Expr * getArg(unsigned Arg)
getArg - Return the specified argument.
Definition Expr.h:3150
SourceLocation getBeginLoc() const
Definition Expr.h:3280
FunctionDecl * getDirectCallee()
If the callee is a FunctionDecl, return it. Otherwise return null.
Definition Expr.h:3129
unsigned getNumArgs() const
getNumArgs - Return the number of actual arguments to this call.
Definition Expr.h:3137
CharUnits - This is an opaque type for sizes expressed in character units.
Definition CharUnits.h:38
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
Definition CharUnits.h:185
This represents one expression.
Definition Expr.h:112
bool isIntegerConstantExpr(const ASTContext &Ctx) const
void setType(QualType t)
Definition Expr.h:145
Expr * IgnoreParens() LLVM_READONLY
Skip past any parentheses which might surround this expression until reaching a fixed point.
Definition Expr.cpp:3086
std::optional< llvm::APSInt > getIntegerConstantExpr(const ASTContext &Ctx) const
isIntegerConstantExpr - Return the value if this expression is a valid integer constant expression.
QualType getType() const
Definition Expr.h:144
Represents a member of a struct/union/class.
Definition Decl.h:3160
A (possibly-)qualified type.
Definition TypeBase.h:937
bool isVolatileQualified() const
Determine whether this type is volatile-qualified.
Definition TypeBase.h:8515
bool isRestrictQualified() const
Determine whether this type is restrict-qualified.
Definition TypeBase.h:8509
QualType getCanonicalType() const
Definition TypeBase.h:8483
QualType getUnqualifiedType() const
Retrieve the unqualified variant of the given type, removing as little sugar as possible.
Definition TypeBase.h:8525
SemaBase(Sema &S)
Definition SemaBase.cpp:7
ASTContext & getASTContext() const
Definition SemaBase.cpp:9
Sema & SemaRef
Definition SemaBase.h:40
SemaDiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID)
Emit a diagnostic.
Definition SemaBase.cpp:61
bool checkTargetClonesAttr(const SmallVectorImpl< StringRef > &Params, const SmallVectorImpl< SourceLocation > &Locs, SmallVectorImpl< SmallString< 64 > > &NewParams, SourceLocation AttrLoc)
Definition SemaPPC.cpp:577
bool CheckPPCBuiltinFunctionCall(const TargetInfo &TI, unsigned BuiltinID, CallExpr *TheCall)
Definition SemaPPC.cpp:112
void checkAIXMemberAlignment(SourceLocation Loc, const Expr *Arg)
Definition SemaPPC.cpp:31
SemaPPC(Sema &S)
Definition SemaPPC.cpp:29
bool BuiltinPPCMMACall(CallExpr *TheCall, unsigned BuiltinID, const char *TypeDesc)
BuiltinPPCMMACall - Check the call to a PPC MMA builtin for validity.
Definition SemaPPC.cpp:469
bool BuiltinVSX(CallExpr *TheCall)
Definition SemaPPC.cpp:535
bool CheckPPCMMAType(QualType Type, SourceLocation TypeLoc)
Definition SemaPPC.cpp:394
Sema - This implements semantic analysis and AST building for C.
Definition Sema.h:868
Encodes a location in the source.
SourceLocation getLocWithOffset(IntTy Offset) const
Return a source location with the specified offset from this SourceLocation.
A trivial tuple used to represent a source range.
SourceLocation getEndLoc() const LLVM_READONLY
Definition Stmt.cpp:367
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
Definition Stmt.cpp:343
SourceLocation getBeginLoc() const LLVM_READONLY
Definition Stmt.cpp:355
Exposes information about the current target.
Definition TargetInfo.h:227
virtual bool validateCpuIs(StringRef Name) const
unsigned getTypeWidth(IntType T) const
Return the width (in bits) of the specified integer type enum.
IntType getIntPtrType() const
Definition TargetInfo.h:415
const llvm::fltSemantics & getLongDoubleFormat() const
Definition TargetInfo.h:810
virtual bool isValidCPUName(StringRef Name) const
Determine whether this TargetInfo supports the given CPU name.
Base wrapper for a particular "section" of type source info.
Definition TypeLoc.h:59
The base class of the type hierarchy.
Definition TypeBase.h:1866
bool isArrayType() const
Definition TypeBase.h:8767
bool isPointerType() const
Definition TypeBase.h:8668
bool isIntegerType() const
isIntegerType() does not include complex integers (a GCC extension).
Definition TypeBase.h:9078
bool isDependentType() const
Whether this type is a dependent type, meaning that its definition somehow depends on a template para...
Definition TypeBase.h:2832
bool isVectorType() const
Definition TypeBase.h:8807
Defines the clang::TargetInfo interface.
Enums for the diagnostics of target, target_version and target_clones.
Definition Sema.h:854
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
The JSON file list parser is used to communicate input to InstallAPI.
static bool isPPC_64Builtin(unsigned BuiltinID)
Definition SemaPPC.cpp:57
@ Result
The result type of a method or function.
Definition TypeBase.h:905
llvm::Expected< QualType > ExpectedType
static QualType DecodePPCMMATypeFromStr(ASTContext &Context, const char *&Str, unsigned &Mask)
DecodePPCMMATypeFromStr - This decodes one PPC MMA type descriptor from Str, advancing the pointer ov...
Definition SemaPPC.cpp:416
@ AltiVecVector
is AltiVec vector
Definition TypeBase.h:4189
@ None
The alignment was not explicit in code.
Definition ASTContext.h:179