]> granicus.if.org Git - clang/commitdiff
Fix behavior of __builtin_bit_cast when the From and To types are the
authorRichard Smith <richard-llvm@metafoo.co.uk>
Mon, 7 Oct 2019 02:45:12 +0000 (02:45 +0000)
committerRichard Smith <richard-llvm@metafoo.co.uk>
Mon, 7 Oct 2019 02:45:12 +0000 (02:45 +0000)
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.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@373874 91177308-0d34-0410-b5e6-96231b3b80d8

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

index 9af92c1ae7ff79b29b6e2a307d8cb171dc226b24..f29664e8eb338961045828e25910a900489a7cbb 100644 (file)
@@ -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
index 71e5e8e4286f7c37c2088fe361d854b02b178897..8c6abc448d977bbf7fdf1cefc3af1ff1b5628992 100644 (file)
@@ -2835,11 +2835,6 @@ void CastOperation::CheckBuiltinBitCast() {
     return;
   }
 
-  if (Self.Context.hasSameUnqualifiedType(DestType, SrcType)) {
-    Kind = CK_NoOp;
-    return;
-  }
-
   Kind = CK_LValueToRValueBitCast;
 }
 
index 3f0e490a5f94bfa63ed913c228cc5057b57cc263..b26e519bee3d42c93dd4e8c43c986aa1168090de 100644 (file)
@@ -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}
index 0a12e7eebe45ef359501a875f8b9704dd8cbf444..06771f8f3252a668ec4e480248dc7321b6693c13 100644 (file)
@@ -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);