r373874 - Fix behavior of __builtin_bit_cast when the From and To types are the

Richard Smith via cfe-commits cfe-commits at lists.llvm.org
Sun Oct 6 19:45:12 PDT 2019


Author: rsmith
Date: Sun Oct  6 19:45:12 2019
New Revision: 373874

URL: http://llvm.org/viewvc/llvm-project?rev=373874&view=rev
Log:
Fix behavior of __builtin_bit_cast when the From and To types are the
same.

We were missing the lvalue-to-rvalue conversion entirely in this case,
and in fact still need the full CK_LValueToRValueBitCast conversion to
perform a load with no TBAA.

Modified:
    cfe/trunk/include/clang/AST/OperationKinds.def
    cfe/trunk/lib/Sema/SemaCast.cpp
    cfe/trunk/test/CodeGenCXX/builtin-bit-cast-no-tbaa.cpp
    cfe/trunk/test/SemaCXX/constexpr-builtin-bit-cast.cpp

Modified: cfe/trunk/include/clang/AST/OperationKinds.def
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/OperationKinds.def?rev=373874&r1=373873&r2=373874&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/OperationKinds.def (original)
+++ cfe/trunk/include/clang/AST/OperationKinds.def Sun Oct  6 19:45:12 2019
@@ -66,8 +66,9 @@ CAST_OPERATION(BitCast)
 ///    bool b; reinterpret_cast<char&>(b) = 'a';
 CAST_OPERATION(LValueBitCast)
 
-/// CK_LValueToRValueBitCast - A conversion that causes us to reinterpret an
-/// lvalue as an rvalue of a different type. Created by __builtin_bit_cast.
+/// CK_LValueToRValueBitCast - A conversion that causes us to reinterpret the
+/// object representation of an lvalue as an rvalue. Created by
+/// __builtin_bit_cast.
 CAST_OPERATION(LValueToRValueBitCast)
 
 /// CK_LValueToRValue - A conversion which causes the extraction of

Modified: cfe/trunk/lib/Sema/SemaCast.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaCast.cpp?rev=373874&r1=373873&r2=373874&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaCast.cpp (original)
+++ cfe/trunk/lib/Sema/SemaCast.cpp Sun Oct  6 19:45:12 2019
@@ -2835,11 +2835,6 @@ void CastOperation::CheckBuiltinBitCast(
     return;
   }
 
-  if (Self.Context.hasSameUnqualifiedType(DestType, SrcType)) {
-    Kind = CK_NoOp;
-    return;
-  }
-
   Kind = CK_LValueToRValueBitCast;
 }
 

Modified: cfe/trunk/test/CodeGenCXX/builtin-bit-cast-no-tbaa.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/builtin-bit-cast-no-tbaa.cpp?rev=373874&r1=373873&r2=373874&view=diff
==============================================================================
--- cfe/trunk/test/CodeGenCXX/builtin-bit-cast-no-tbaa.cpp (original)
+++ cfe/trunk/test/CodeGenCXX/builtin-bit-cast-no-tbaa.cpp Sun Oct  6 19:45:12 2019
@@ -15,5 +15,10 @@ void test_scalar2() {
   // CHECK: load i32, i32* {{.*}}, align 4, !tbaa ![[MAY_ALIAS_TBAA]]
 }
 
+int test_same_type(int &r) {
+  // CHECK: load i32, i32* {{.*}}, align 4, !tbaa ![[MAY_ALIAS_TBAA]]
+  return __builtin_bit_cast(int, r);
+}
+
 // CHECK: ![[CHAR_TBAA:.*]] = !{!"omnipotent char", {{.*}}, i64 0}
 // CHECK: ![[MAY_ALIAS_TBAA]] = !{![[CHAR_TBAA]], ![[CHAR_TBAA]], i64 0}

Modified: cfe/trunk/test/SemaCXX/constexpr-builtin-bit-cast.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/constexpr-builtin-bit-cast.cpp?rev=373874&r1=373873&r2=373874&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/constexpr-builtin-bit-cast.cpp (original)
+++ cfe/trunk/test/SemaCXX/constexpr-builtin-bit-cast.cpp Sun Oct  6 19:45:12 2019
@@ -381,3 +381,19 @@ constexpr bool test_pad_buffer() {
   return x.a == z.a && x.b == z.b;
 }
 static_assert(test_pad_buffer());
+
+constexpr unsigned char identity1a = 42;
+constexpr unsigned char identity1b = __builtin_bit_cast(unsigned char, identity1a);
+static_assert(identity1b == 42);
+
+struct IdentityInStruct {
+  unsigned char n;
+};
+constexpr IdentityInStruct identity2a = {42};
+constexpr unsigned char identity2b = __builtin_bit_cast(unsigned char, identity2a.n);
+
+union IdentityInUnion {
+  unsigned char n;
+};
+constexpr IdentityInUnion identity3a = {42};
+constexpr unsigned char identity3b = __builtin_bit_cast(unsigned char, identity3a.n);




More information about the cfe-commits mailing list