Debian Codegraph @

clang/lib/Sema/SemaPPC.cpp

raw
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"
14 #include "clang/AST/ASTContext.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"
19 #include "clang/Basic/DiagnosticSema.h"
20 #include "clang/Basic/SourceLocation.h"
21 #include "clang/Basic/TargetBuiltins.h"
22 #include "clang/Basic/TargetInfo.h"
23 #include "clang/Sema/Sema.h"
24 #include "llvm/ADT/APSInt.h"
25
26 namespace clang {
27
28 SemaPPC::SemaPPC(Sema &S) : SemaBase(S) {}
29
30 void SemaPPC::checkAIXMemberAlignment(SourceLocation Loc, const Expr *Arg) {
31 const auto *ICE = dyn_cast<ImplicitCastExpr>(Arg->IgnoreParens());
32 if (!ICE)
33 return;
34
35 const auto *DR = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
36 if (!DR)
37 return;
38
39 const auto *PD = dyn_cast<ParmVarDecl>(DR->getDecl());
40 if (!PD || !PD->getType()->isRecordType())
41 return;
42
43 QualType ArgType = Arg->getType();
44 for (const FieldDecl *FD : ArgType->castAsRecordDecl()->fields()) {
45 if (const auto *AA = FD->getAttr<AlignedAttr>()) {
46 CharUnits Alignment = getASTContext().toCharUnitsFromBits(
47 AA->getAlignment(getASTContext()));
48 if (Alignment.getQuantity() == 16) {
49 Diag(FD->getLocation(), diag::warn_not_xl_compatible) << FD;
50 Diag(Loc, diag::note_misaligned_member_used_here) << PD;
51 }
52 }
53 }
54 }
55
56 static bool isPPC_64Builtin(unsigned BuiltinID) {
57 // These builtins only work on PPC 64bit targets.
58 switch (BuiltinID) {
59 case PPC::BI__builtin_divde:
60 case PPC::BI__builtin_divdeu:
61 case PPC::BI__builtin_bpermd:
62 case PPC::BI__builtin_pdepd:
63 case PPC::BI__builtin_pextd:
64 case PPC::BI__builtin_ppc_cdtbcd:
65 case PPC::BI__builtin_ppc_cbcdtd:
66 case PPC::BI__builtin_ppc_addg6s:
67 case PPC::BI__builtin_ppc_ldarx:
68 case PPC::BI__builtin_ppc_stdcx:
69 case PPC::BI__builtin_ppc_tdw:
70 case PPC::BI__builtin_ppc_trapd:
71 case PPC::BI__builtin_ppc_cmpeqb:
72 case PPC::BI__builtin_ppc_setb:
73 case PPC::BI__builtin_ppc_mulhd:
74 case PPC::BI__builtin_ppc_mulhdu:
75 case PPC::BI__builtin_ppc_maddhd:
76 case PPC::BI__builtin_ppc_maddhdu:
77 case PPC::BI__builtin_ppc_maddld:
78 case PPC::BI__builtin_ppc_load8r:
79 case PPC::BI__builtin_ppc_store8r:
80 case PPC::BI__builtin_ppc_insert_exp:
81 case PPC::BI__builtin_ppc_extract_sig:
82 case PPC::BI__builtin_ppc_addex:
83 case PPC::BI__builtin_darn:
84 case PPC::BI__builtin_darn_raw:
85 case PPC::BI__builtin_ppc_compare_and_swaplp:
86 case PPC::BI__builtin_ppc_fetch_and_addlp:
87 case PPC::BI__builtin_ppc_fetch_and_andlp:
88 case PPC::BI__builtin_ppc_fetch_and_orlp:
89 case PPC::BI__builtin_ppc_fetch_and_swaplp:
90 case PPC::BI__builtin_amo_lwat:
91 case PPC::BI__builtin_amo_ldat:
92 case PPC::BI__builtin_amo_lwat_s:
93 case PPC::BI__builtin_amo_ldat_s:
94 return true;
95 }
96 return false;
97 }
98
99 bool SemaPPC::CheckPPCBuiltinFunctionCall(const TargetInfo &TI,
100 unsigned BuiltinID,
101 CallExpr *TheCall) {
102 ASTContext &Context = getASTContext();
103 bool IsTarget64Bit = TI.getTypeWidth(TI.getIntPtrType()) == 64;
104
105 if (isPPC_64Builtin(BuiltinID) && !IsTarget64Bit)
106 return Diag(TheCall->getBeginLoc(), diag::err_64_bit_builtin_32_bit_tgt)
107 << TheCall->getSourceRange();
108
109 switch (BuiltinID) {
110 default:
111 return false;
112 case PPC::BI__builtin_ppc_bcdsetsign:
113 case PPC::BI__builtin_ppc_national2packed:
114 case PPC::BI__builtin_ppc_packed2zoned:
115 case PPC::BI__builtin_ppc_zoned2packed:
116 return SemaRef.BuiltinConstantArgRange(TheCall, 1, 0, 1);
117 case PPC::BI__builtin_altivec_crypto_vshasigmaw:
118 case PPC::BI__builtin_altivec_crypto_vshasigmad:
119 return SemaRef.BuiltinConstantArgRange(TheCall, 1, 0, 1) ||
120 SemaRef.BuiltinConstantArgRange(TheCall, 2, 0, 15);
121 case PPC::BI__builtin_altivec_dss:
122 return SemaRef.BuiltinConstantArgRange(TheCall, 0, 0, 3);
123 case PPC::BI__builtin_tbegin:
124 case PPC::BI__builtin_tend:
125 return SemaRef.BuiltinConstantArgRange(TheCall, 0, 0, 1);
126 case PPC::BI__builtin_tsr:
127 return SemaRef.BuiltinConstantArgRange(TheCall, 0, 0, 7);
128 case PPC::BI__builtin_tabortwc:
129 case PPC::BI__builtin_tabortdc:
130 return SemaRef.BuiltinConstantArgRange(TheCall, 0, 0, 31);
131 case PPC::BI__builtin_tabortwci:
132 case PPC::BI__builtin_tabortdci:
133 return SemaRef.BuiltinConstantArgRange(TheCall, 0, 0, 31) ||
134 SemaRef.BuiltinConstantArgRange(TheCall, 2, 0, 31);
135 // According to GCC 'Basic PowerPC Built-in Functions Available on ISA 2.05',
136 // __builtin_(un)pack_longdouble are available only if long double uses IBM
137 // extended double representation.
138 case PPC::BI__builtin_unpack_longdouble:
139 if (SemaRef.BuiltinConstantArgRange(TheCall, 1, 0, 1))
140 return true;
141 [[fallthrough]];
142 case PPC::BI__builtin_pack_longdouble:
143 if (&TI.getLongDoubleFormat() != &llvm::APFloat::PPCDoubleDouble())
144 return Diag(TheCall->getBeginLoc(), diag::err_ppc_builtin_requires_abi)
145 << "ibmlongdouble";
146 return false;
147 case PPC::BI__builtin_altivec_dst:
148 case PPC::BI__builtin_altivec_dstt:
149 case PPC::BI__builtin_altivec_dstst:
150 case PPC::BI__builtin_altivec_dststt:
151 return SemaRef.BuiltinConstantArgRange(TheCall, 2, 0, 3);
152 case PPC::BI__builtin_vsx_xxpermdi:
153 case PPC::BI__builtin_vsx_xxsldwi:
154 return BuiltinVSX(TheCall);
155 case PPC::BI__builtin_unpack_vector_int128:
156 return SemaRef.BuiltinConstantArgRange(TheCall, 1, 0, 1);
157 case PPC::BI__builtin_altivec_vgnb:
158 return SemaRef.BuiltinConstantArgRange(TheCall, 1, 2, 7);
159 case PPC::BI__builtin_vsx_xxeval:
160 return SemaRef.BuiltinConstantArgRange(TheCall, 3, 0, 255);
161 case PPC::BI__builtin_altivec_vsldbi:
162 return SemaRef.BuiltinConstantArgRange(TheCall, 2, 0, 7);
163 case PPC::BI__builtin_altivec_vsrdbi:
164 return SemaRef.BuiltinConstantArgRange(TheCall, 2, 0, 7);
165 case PPC::BI__builtin_vsx_xxpermx:
166 return SemaRef.BuiltinConstantArgRange(TheCall, 3, 0, 7);
167 case PPC::BI__builtin_ppc_tw:
168 case PPC::BI__builtin_ppc_tdw:
169 return SemaRef.BuiltinConstantArgRange(TheCall, 2, 1, 31);
170 case PPC::BI__builtin_ppc_cmprb:
171 return SemaRef.BuiltinConstantArgRange(TheCall, 0, 0, 1);
172 // For __rlwnm, __rlwimi and __rldimi, the last parameter mask must
173 // be a constant that represents a contiguous bit field.
174 case PPC::BI__builtin_ppc_rlwnm:
175 return SemaRef.ValueIsRunOfOnes(TheCall, 2);
176 case PPC::BI__builtin_ppc_rlwimi:
177 return SemaRef.BuiltinConstantArgRange(TheCall, 2, 0, 31) ||
178 SemaRef.ValueIsRunOfOnes(TheCall, 3);
179 case PPC::BI__builtin_ppc_rldimi:
180 return SemaRef.BuiltinConstantArgRange(TheCall, 2, 0, 63) ||
181 SemaRef.ValueIsRunOfOnes(TheCall, 3);
182 case PPC::BI__builtin_ppc_addex: {
183 if (SemaRef.BuiltinConstantArgRange(TheCall, 2, 0, 3))
184 return true;
185 // Output warning for reserved values 1 to 3.
186 int ArgValue =
187 TheCall->getArg(2)->getIntegerConstantExpr(Context)->getSExtValue();
188 if (ArgValue != 0)
189 Diag(TheCall->getBeginLoc(), diag::warn_argument_undefined_behaviour)
190 << ArgValue;
191 return false;
192 }
193 case PPC::BI__builtin_ppc_mtfsb0:
194 case PPC::BI__builtin_ppc_mtfsb1:
195 return SemaRef.BuiltinConstantArgRange(TheCall, 0, 0, 31);
196 case PPC::BI__builtin_ppc_mtfsf:
197 return SemaRef.BuiltinConstantArgRange(TheCall, 0, 0, 255);
198 case PPC::BI__builtin_ppc_mtfsfi:
199 return SemaRef.BuiltinConstantArgRange(TheCall, 0, 0, 7) ||
200 SemaRef.BuiltinConstantArgRange(TheCall, 1, 0, 15);
201 case PPC::BI__builtin_ppc_alignx:
202 return SemaRef.BuiltinConstantArgPower2(TheCall, 0);
203 case PPC::BI__builtin_ppc_rdlam:
204 return SemaRef.ValueIsRunOfOnes(TheCall, 2);
205 case PPC::BI__builtin_vsx_ldrmb:
206 case PPC::BI__builtin_vsx_strmb:
207 return SemaRef.BuiltinConstantArgRange(TheCall, 1, 1, 16);
208 case PPC::BI__builtin_altivec_vcntmbb:
209 case PPC::BI__builtin_altivec_vcntmbh:
210 case PPC::BI__builtin_altivec_vcntmbw:
211 case PPC::BI__builtin_altivec_vcntmbd:
212 return SemaRef.BuiltinConstantArgRange(TheCall, 1, 0, 1);
213 case PPC::BI__builtin_vsx_xxgenpcvbm:
214 case PPC::BI__builtin_vsx_xxgenpcvhm:
215 case PPC::BI__builtin_vsx_xxgenpcvwm:
216 case PPC::BI__builtin_vsx_xxgenpcvdm:
217 return SemaRef.BuiltinConstantArgRange(TheCall, 1, 0, 3);
218 case PPC::BI__builtin_ppc_test_data_class: {
219 // Check if the first argument of the __builtin_ppc_test_data_class call is
220 // valid. The argument must be 'float' or 'double' or '__float128'.
221 QualType ArgType = TheCall->getArg(0)->getType();
222 if (ArgType != QualType(Context.FloatTy) &&
223 ArgType != QualType(Context.DoubleTy) &&
224 ArgType != QualType(Context.Float128Ty))
225 return Diag(TheCall->getBeginLoc(),
226 diag::err_ppc_invalid_test_data_class_type);
227 return SemaRef.BuiltinConstantArgRange(TheCall, 1, 0, 127);
228 }
229 case PPC::BI__builtin_ppc_maxfe:
230 case PPC::BI__builtin_ppc_minfe:
231 case PPC::BI__builtin_ppc_maxfl:
232 case PPC::BI__builtin_ppc_minfl:
233 case PPC::BI__builtin_ppc_maxfs:
234 case PPC::BI__builtin_ppc_minfs: {
235 if (Context.getTargetInfo().getTriple().isOSAIX() &&
236 (BuiltinID == PPC::BI__builtin_ppc_maxfe ||
237 BuiltinID == PPC::BI__builtin_ppc_minfe))
238 return Diag(TheCall->getBeginLoc(), diag::err_target_unsupported_type)
239 << "builtin" << true << 128 << QualType(Context.LongDoubleTy)
240 << false << Context.getTargetInfo().getTriple().str();
241 // Argument type should be exact.
242 QualType ArgType = QualType(Context.LongDoubleTy);
243 if (BuiltinID == PPC::BI__builtin_ppc_maxfl ||
244 BuiltinID == PPC::BI__builtin_ppc_minfl)
245 ArgType = QualType(Context.DoubleTy);
246 else if (BuiltinID == PPC::BI__builtin_ppc_maxfs ||
247 BuiltinID == PPC::BI__builtin_ppc_minfs)
248 ArgType = QualType(Context.FloatTy);
249 for (unsigned I = 0, E = TheCall->getNumArgs(); I < E; ++I)
250 if (TheCall->getArg(I)->getType() != ArgType)
251 return Diag(TheCall->getBeginLoc(),
252 diag::err_typecheck_convert_incompatible)
253 << TheCall->getArg(I)->getType() << ArgType << 1 << 0 << 0;
254 return false;
255 }
256 #define CUSTOM_BUILTIN(Name, Intr, Types, Acc, Feature) \
257 case PPC::BI__builtin_##Name: \
258 return BuiltinPPCMMACall(TheCall, BuiltinID, Types);
259 #include "clang/Basic/BuiltinsPPC.def"
260 case PPC::BI__builtin_amo_lwat:
261 case PPC::BI__builtin_amo_ldat:
262 case PPC::BI__builtin_amo_lwat_s:
263 case PPC::BI__builtin_amo_ldat_s: {
264 llvm::APSInt Result;
265 if (SemaRef.BuiltinConstantArg(TheCall, 2, Result))
266 return true;
267 unsigned Val = Result.getZExtValue();
268
269 bool IsUnsigned = (BuiltinID == PPC::BI__builtin_amo_lwat ||
270 BuiltinID == PPC::BI__builtin_amo_ldat);
271
272 bool IsValid = IsUnsigned
273 ? llvm::is_contained({0u, 1u, 2u, 3u, 4u, 6u, 8u}, Val)
274 : llvm::is_contained({0u, 5u, 7u, 8u}, Val);
275
276 if (IsValid)
277 return false;
278
279 Expr *Arg = TheCall->getArg(2);
280 return SemaRef.Diag(Arg->getBeginLoc(), diag::err_argument_invalid_range)
281 << toString(Result, 10) << (IsUnsigned ? "0-4, 6" : "0, 5, 7") << "8"
282 << Arg->getSourceRange();
283 }
284 }
285 llvm_unreachable("must return from switch");
286 }
287
288 // Check if the given type is a non-pointer PPC MMA type. This function is used
289 // in Sema to prevent invalid uses of restricted PPC MMA types.
290 bool SemaPPC::CheckPPCMMAType(QualType Type, SourceLocation TypeLoc) {
291 ASTContext &Context = getASTContext();
292 if (Type->isPointerType() || Type->isArrayType())
293 return false;
294
295 QualType CoreType = Type.getCanonicalType().getUnqualifiedType();
296 #define PPC_VECTOR_TYPE(Name, Id, Size) || CoreType == Context.Id##Ty
297 if (false
298 #include "clang/Basic/PPCTypes.def"
299 ) {
300 Diag(TypeLoc, diag::err_ppc_invalid_use_mma_type);
301 return true;
302 }
303 return false;
304 }
305
306 /// DecodePPCMMATypeFromStr - This decodes one PPC MMA type descriptor from Str,
307 /// advancing the pointer over the consumed characters. The decoded type is
308 /// returned. If the decoded type represents a constant integer with a
309 /// constraint on its value then Mask is set to that value. The type descriptors
310 /// used in Str are specific to PPC MMA builtins and are documented in the file
311 /// defining the PPC builtins.
312 static QualType DecodePPCMMATypeFromStr(ASTContext &Context, const char *&Str,
313 unsigned &Mask) {
314 bool RequireICE = false;
315 ASTContext::GetBuiltinTypeError Error = ASTContext::GE_None;
316 switch (*Str++) {
317 case 'V':
318 return Context.getVectorType(Context.UnsignedCharTy, 16,
319 VectorKind::AltiVecVector);
320 case 'i': {
321 char *End;
322 unsigned size = strtoul(Str, &End, 10);
323 assert(End != Str && "Missing constant parameter constraint");
324 Str = End;
325 Mask = size;
326 return Context.IntTy;
327 }
328 case 'W': {
329 char *End;
330 unsigned size = strtoul(Str, &End, 10);
331 assert(End != Str && "Missing PowerPC MMA type size");
332 Str = End;
333 QualType Type;
334 switch (size) {
335 #define PPC_VECTOR_TYPE(typeName, Id, size) \
336 case size: \
337 Type = Context.Id##Ty; \
338 break;
339 #include "clang/Basic/PPCTypes.def"
340 default:
341 llvm_unreachable("Invalid PowerPC MMA vector type");
342 }
343 bool CheckVectorArgs = false;
344 while (!CheckVectorArgs) {
345 switch (*Str++) {
346 case '*':
347 Type = Context.getPointerType(Type);
348 break;
349 case 'C':
350 Type = Type.withConst();
351 break;
352 default:
353 CheckVectorArgs = true;
354 --Str;
355 break;
356 }
357 }
358 return Type;
359 }
360 default:
361 return Context.DecodeTypeStr(--Str, Context, Error, RequireICE, true);
362 }
363 }
364
365 bool SemaPPC::BuiltinPPCMMACall(CallExpr *TheCall, unsigned BuiltinID,
366 const char *TypeStr) {
367
368 assert((TypeStr[0] != '\0') &&
369 "Invalid types in PPC MMA builtin declaration");
370
371 ASTContext &Context = getASTContext();
372 unsigned Mask = 0;
373 unsigned ArgNum = 0;
374
375 // The first type in TypeStr is the type of the value returned by the
376 // builtin. So we first read that type and change the type of TheCall.
377 QualType type = DecodePPCMMATypeFromStr(Context, TypeStr, Mask);
378 TheCall->setType(type);
379
380 while (*TypeStr != '\0') {
381 Mask = 0;
382 QualType ExpectedType = DecodePPCMMATypeFromStr(Context, TypeStr, Mask);
383 if (ArgNum >= TheCall->getNumArgs()) {
384 ArgNum++;
385 break;
386 }
387
388 Expr *Arg = TheCall->getArg(ArgNum);
389 QualType PassedType = Arg->getType();
390 QualType StrippedRVType = PassedType.getCanonicalType();
391
392 // Strip Restrict/Volatile qualifiers.
393 if (StrippedRVType.isRestrictQualified() ||
394 StrippedRVType.isVolatileQualified())
395 StrippedRVType = StrippedRVType.getCanonicalType().getUnqualifiedType();
396
397 // The only case where the argument type and expected type are allowed to
398 // mismatch is if the argument type is a non-void pointer (or array) and
399 // expected type is a void pointer.
400 if (StrippedRVType != ExpectedType)
401 if (!(ExpectedType->isVoidPointerType() &&
402 (StrippedRVType->isPointerType() || StrippedRVType->isArrayType())))
403 return Diag(Arg->getBeginLoc(),
404 diag::err_typecheck_convert_incompatible)
405 << PassedType << ExpectedType << 1 << 0 << 0;
406
407 // If the value of the Mask is not 0, we have a constraint in the size of
408 // the integer argument so here we ensure the argument is a constant that
409 // is in the valid range.
410 if (Mask != 0 &&
411 SemaRef.BuiltinConstantArgRange(TheCall, ArgNum, 0, Mask, true))
412 return true;
413
414 ArgNum++;
415 }
416
417 // In case we exited early from the previous loop, there are other types to
418 // read from TypeStr. So we need to read them all to ensure we have the right
419 // number of arguments in TheCall and if it is not the case, to display a
420 // better error message.
421 while (*TypeStr != '\0') {
422 (void)DecodePPCMMATypeFromStr(Context, TypeStr, Mask);
423 ArgNum++;
424 }
425 if (SemaRef.checkArgCount(TheCall, ArgNum))
426 return true;
427
428 return false;
429 }
430
431 bool SemaPPC::BuiltinVSX(CallExpr *TheCall) {
432 unsigned ExpectedNumArgs = 3;
433 if (SemaRef.checkArgCount(TheCall, ExpectedNumArgs))
434 return true;
435
436 // Check the third argument is a compile time constant
437 if (!TheCall->getArg(2)->isIntegerConstantExpr(getASTContext()))
438 return Diag(TheCall->getBeginLoc(),
439 diag::err_vsx_builtin_nonconstant_argument)
440 << 3 /* argument index */ << TheCall->getDirectCallee()
441 << SourceRange(TheCall->getArg(2)->getBeginLoc(),
442 TheCall->getArg(2)->getEndLoc());
443
444 QualType Arg1Ty = TheCall->getArg(0)->getType();
445 QualType Arg2Ty = TheCall->getArg(1)->getType();
446
447 // Check the type of argument 1 and argument 2 are vectors.
448 SourceLocation BuiltinLoc = TheCall->getBeginLoc();
449 if ((!Arg1Ty->isVectorType() && !Arg1Ty->isDependentType()) ||
450 (!Arg2Ty->isVectorType() && !Arg2Ty->isDependentType())) {
451 return Diag(BuiltinLoc, diag::err_vec_builtin_non_vector)
452 << TheCall->getDirectCallee() << /*isMorethantwoArgs*/ false
453 << SourceRange(TheCall->getArg(0)->getBeginLoc(),
454 TheCall->getArg(1)->getEndLoc());
455 }
456
457 // Check the first two arguments are the same type.
458 if (!getASTContext().hasSameUnqualifiedType(Arg1Ty, Arg2Ty)) {
459 return Diag(BuiltinLoc, diag::err_vec_builtin_incompatible_vector)
460 << TheCall->getDirectCallee() << /*isMorethantwoArgs*/ false
461 << SourceRange(TheCall->getArg(0)->getBeginLoc(),
462 TheCall->getArg(1)->getEndLoc());
463 }
464
465 // When default clang type checking is turned off and the customized type
466 // checking is used, the returning type of the function must be explicitly
467 // set. Otherwise it is _Bool by default.
468 TheCall->setType(Arg1Ty);
469
470 return false;
471 }
472
473 } // namespace clang