[clang] [clang] Implement __builtin_stdc_rotate_left, __builtin_stdc_rotate_right (PR #160259)
Erich Keane via cfe-commits
cfe-commits at lists.llvm.org
Mon Oct 6 11:41:02 PDT 2025
================
@@ -2267,6 +2267,67 @@ static bool BuiltinCountZeroBitsGeneric(Sema &S, CallExpr *TheCall) {
return false;
}
+/// Checks that __builtin_stdc_rotate_{left,right} was called with two
+/// arguments, that the first argument is an unsigned integer type, and that
+/// the second argument is an integer type.
+static bool BuiltinRotateGeneric(Sema &S, CallExpr *TheCall) {
+ if (S.checkArgCount(TheCall, 2))
+ return true;
+
+ ExprResult Arg0Res = S.DefaultLvalueConversion(TheCall->getArg(0));
+ if (Arg0Res.isInvalid())
+ return true;
+
+ Expr *Arg0 = Arg0Res.get();
+ TheCall->setArg(0, Arg0);
+
+ QualType Arg0Ty = Arg0->getType();
+ if (!Arg0Ty->isUnsignedIntegerType() && Arg0Ty->isRecordType()) {
+ ExprResult ConvArg0Res = S.PerformImplicitConversion(
+ TheCall->getArg(0), S.Context.UnsignedIntTy, AssignmentAction::Passing);
+ if (ConvArg0Res.isUsable()) {
+ Arg0 = ConvArg0Res.get();
+ Arg0Ty = Arg0->getType();
+ TheCall->setArg(0, Arg0);
+ }
+ }
+
+ if (!Arg0Ty->isUnsignedIntegerType()) {
+ S.Diag(Arg0->getBeginLoc(), diag::err_builtin_invalid_arg_type)
+ << 1 << /* scalar */ 1 << /* unsigned integer ty */ 3 << /* no fp */ 0
+ << Arg0Ty;
+ return true;
+ }
+
+ ExprResult Arg1Res = S.DefaultLvalueConversion(TheCall->getArg(1));
+ if (Arg1Res.isInvalid())
+ return true;
+
+ Expr *Arg1 = Arg1Res.get();
+ TheCall->setArg(1, Arg1);
+
+ QualType Arg1Ty = Arg1->getType();
+
+ if (!Arg1Ty->isIntegerType() && Arg1Ty->isRecordType()) {
----------------
erichkeane wrote:
My thoughts here are that the conversion operator would take care of these getting cast to unsigned-int. So something like:
```
auto maybeRotated = __builtin_stdc_rotate_left(thing_to_rotate, shouldShiftOnce());
```
Float I have a less compelling reason for, but I DO wonder if there is a reason to exclude anything that would have a valid unsigned representation.
https://github.com/llvm/llvm-project/pull/160259
More information about the cfe-commits
mailing list