Index: docs/LanguageExtensions.rst =================================================================== --- docs/LanguageExtensions.rst +++ docs/LanguageExtensions.rst @@ -1739,6 +1739,70 @@ the bitpattern of an integer value; for example ``0b10110110`` becomes ``0b01101101``. +``__builtin_rotateleft`` +------------------------ + +* ``__builtin_rotateleft8`` +* ``__builtin_rotateleft16`` +* ``__builtin_rotateleft32`` +* ``__builtin_rotateleft64`` + +**Syntax**: + +.. code-block:: c++ + + __builtin_rotateleft32(x, y) + +**Examples**: + +.. code-block:: c++ + + uint8_t rot_x = __builtin_rotateleft8(x, y); + uint16_t rot_x = __builtin_rotateleft16(x, y); + uint32_t rot_x = __builtin_rotateleft32(x, y); + uint64_t rot_x = __builtin_rotateleft64(x, y); + +**Description**: + +The '``__builtin_rotateleft``' family of builtins is used to rotate +the bits in the first argument by the amount in the second argument. +For example, ``0b10000110`` rotated left by 11 becomes ``0b00110100``. +The shift value is treated as an unsigned amount modulo the size of +the arguments. Both arguments and the result have the bitwidth specified +by the name of the builtin. + +``__builtin_rotateright`` +------------------------ + +* ``__builtin_rotateright8`` +* ``__builtin_rotateright16`` +* ``__builtin_rotateright32`` +* ``__builtin_rotateright64`` + +**Syntax**: + +.. code-block:: c++ + + __builtin_rotateright32(x, y) + +**Examples**: + +.. code-block:: c++ + + uint8_t rot_x = __builtin_rotateright8(x, y); + uint16_t rot_x = __builtin_rotateright16(x, y); + uint32_t rot_x = __builtin_rotateright32(x, y); + uint64_t rot_x = __builtin_rotateright64(x, y); + +**Description**: + +The '``__builtin_rotateright``' family of builtins is used to rotate +the bits in the first argument by the amount in the second argument. +For example, ``0b10000110`` rotated right by 3 becomes ``0b11010000``. +The shift value is treated as an unsigned amount modulo the size of +the arguments. Both arguments and the result have the bitwidth specified +by the name of the builtin. + ``__builtin_unreachable`` ------------------------- Index: include/clang/Basic/Builtins.def =================================================================== --- include/clang/Basic/Builtins.def +++ include/clang/Basic/Builtins.def @@ -428,6 +428,15 @@ BUILTIN(__builtin_bitreverse32, "UiUi", "nc") BUILTIN(__builtin_bitreverse64, "ULLiULLi", "nc") +BUILTIN(__builtin_rotateleft8, "UcUcUc", "nc") +BUILTIN(__builtin_rotateleft16, "UsUsUs", "nc") +BUILTIN(__builtin_rotateleft32, "UiUiUi", "nc") +BUILTIN(__builtin_rotateleft64, "ULLiULLiULLi", "nc") +BUILTIN(__builtin_rotateright8, "UcUcUc", "nc") +BUILTIN(__builtin_rotateright16, "UsUsUs", "nc") +BUILTIN(__builtin_rotateright32, "UiUiUi", "nc") +BUILTIN(__builtin_rotateright64, "ULLiULLiULLi", "nc") + // Random GCC builtins BUILTIN(__builtin_constant_p, "i.", "nctu") BUILTIN(__builtin_classify_type, "i.", "nctu") Index: lib/CodeGen/CGBuiltin.cpp =================================================================== --- lib/CodeGen/CGBuiltin.cpp +++ lib/CodeGen/CGBuiltin.cpp @@ -1741,6 +1741,25 @@ case Builtin::BI__builtin_bitreverse64: { return RValue::get(emitUnaryBuiltin(*this, E, Intrinsic::bitreverse)); } + case Builtin::BI__builtin_rotateleft8: + case Builtin::BI__builtin_rotateleft16: + case Builtin::BI__builtin_rotateleft32: + case Builtin::BI__builtin_rotateleft64: { + llvm::Value *Src = EmitScalarExpr(E->getArg(0)); + llvm::Value *ShiftAmt = EmitScalarExpr(E->getArg(1)); + Value *F = CGM.getIntrinsic(Intrinsic::fshl, Src->getType()); + return RValue::get(Builder.CreateCall(F, { Src, Src, ShiftAmt })); + } + case Builtin::BI__builtin_rotateright8: + case Builtin::BI__builtin_rotateright16: + case Builtin::BI__builtin_rotateright32: + case Builtin::BI__builtin_rotateright64: { + llvm::Value *Src = EmitScalarExpr(E->getArg(0)); + llvm::Value *ShiftAmt = EmitScalarExpr(E->getArg(1)); + Value *F = CGM.getIntrinsic(Intrinsic::fshr, Src->getType()); + return RValue::get(Builder.CreateCall(F, { Src, Src, ShiftAmt })); + } + case Builtin::BI__builtin_object_size: { unsigned Type = E->getArg(1)->EvaluateKnownConstInt(getContext()).getZExtValue(); Index: test/CodeGen/builtin-rotate.c =================================================================== --- test/CodeGen/builtin-rotate.c +++ test/CodeGen/builtin-rotate.c @@ -0,0 +1,66 @@ +// RUN: %clang_cc1 %s -emit-llvm -o - | FileCheck %s + +unsigned char rotl8(unsigned char x, unsigned char y) { +// CHECK-LABEL: rotl8 +// CHECK: [[F:%.*]] = call i8 @llvm.fshl.i8(i8 [[X:%.*]], i8 [[X]], i8 [[Y:%.*]]) +// CHECK-NEXT: ret i8 [[F]] + + return __builtin_rotateleft8(x, y); +} + +short rotl16(short x, short y) { +// CHECK-LABEL: rotl16 +// CHECK: [[F:%.*]] = call i16 @llvm.fshl.i16(i16 [[X:%.*]], i16 [[X]], i16 [[Y:%.*]]) +// CHECK-NEXT: ret i16 [[F]] + + return __builtin_rotateleft16(x, y); +} + +int rotl32(int x, unsigned int y) { +// CHECK-LABEL: rotl32 +// CHECK: [[F:%.*]] = call i32 @llvm.fshl.i32(i32 [[X:%.*]], i32 [[X]], i32 [[Y:%.*]]) +// CHECK-NEXT: ret i32 [[F]] + + return __builtin_rotateleft32(x, y); +} + +unsigned long long rotl64(unsigned long long x, long long y) { +// CHECK-LABEL: rotl64 +// CHECK: [[F:%.*]] = call i64 @llvm.fshl.i64(i64 [[X:%.*]], i64 [[X]], i64 [[Y:%.*]]) +// CHECK-NEXT: ret i64 [[F]] + + return __builtin_rotateleft64(x, y); +} + +char rotr8(char x, char y) { +// CHECK-LABEL: rotr8 +// CHECK: [[F:%.*]] = call i8 @llvm.fshr.i8(i8 [[X:%.*]], i8 [[X]], i8 [[Y:%.*]]) +// CHECK-NEXT: ret i8 [[F]] + + return __builtin_rotateright8(x, y); +} + +unsigned short rotr16(unsigned short x, unsigned short y) { +// CHECK-LABEL: rotr16 +// CHECK: [[F:%.*]] = call i16 @llvm.fshr.i16(i16 [[X:%.*]], i16 [[X]], i16 [[Y:%.*]]) +// CHECK-NEXT: ret i16 [[F]] + + return __builtin_rotateright16(x, y); +} + +unsigned int rotr32(unsigned int x, int y) { +// CHECK-LABEL: rotr32 +// CHECK: [[F:%.*]] = call i32 @llvm.fshr.i32(i32 [[X:%.*]], i32 [[X]], i32 [[Y:%.*]]) +// CHECK-NEXT: ret i32 [[F]] + + return __builtin_rotateright32(x, y); +} + +long long rotr64(long long x, unsigned long long y) { +// CHECK-LABEL: rotr64 +// CHECK: [[F:%.*]] = call i64 @llvm.fshr.i64(i64 [[X:%.*]], i64 [[X]], i64 [[Y:%.*]]) +// CHECK-NEXT: ret i64 [[F]] + + return __builtin_rotateright64(x, y); +} +