Skip to content

Commit 74ce711

Browse files
committed
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. llvm-svn: 373874
1 parent a30730f commit 74ce711

File tree

4 files changed

+24
-7
lines changed

4 files changed

+24
-7
lines changed

clang/include/clang/AST/OperationKinds.def

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,9 @@ CAST_OPERATION(BitCast)
6666
/// bool b; reinterpret_cast<char&>(b) = 'a';
6767
CAST_OPERATION(LValueBitCast)
6868

69-
/// CK_LValueToRValueBitCast - A conversion that causes us to reinterpret an
70-
/// lvalue as an rvalue of a different type. Created by __builtin_bit_cast.
69+
/// CK_LValueToRValueBitCast - A conversion that causes us to reinterpret the
70+
/// object representation of an lvalue as an rvalue. Created by
71+
/// __builtin_bit_cast.
7172
CAST_OPERATION(LValueToRValueBitCast)
7273

7374
/// CK_LValueToRValue - A conversion which causes the extraction of

clang/lib/Sema/SemaCast.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2835,11 +2835,6 @@ void CastOperation::CheckBuiltinBitCast() {
28352835
return;
28362836
}
28372837

2838-
if (Self.Context.hasSameUnqualifiedType(DestType, SrcType)) {
2839-
Kind = CK_NoOp;
2840-
return;
2841-
}
2842-
28432838
Kind = CK_LValueToRValueBitCast;
28442839
}
28452840

clang/test/CodeGenCXX/builtin-bit-cast-no-tbaa.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,10 @@ void test_scalar2() {
1515
// CHECK: load i32, i32* {{.*}}, align 4, !tbaa ![[MAY_ALIAS_TBAA]]
1616
}
1717

18+
int test_same_type(int &r) {
19+
// CHECK: load i32, i32* {{.*}}, align 4, !tbaa ![[MAY_ALIAS_TBAA]]
20+
return __builtin_bit_cast(int, r);
21+
}
22+
1823
// CHECK: ![[CHAR_TBAA:.*]] = !{!"omnipotent char", {{.*}}, i64 0}
1924
// CHECK: ![[MAY_ALIAS_TBAA]] = !{![[CHAR_TBAA]], ![[CHAR_TBAA]], i64 0}

clang/test/SemaCXX/constexpr-builtin-bit-cast.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,3 +381,19 @@ constexpr bool test_pad_buffer() {
381381
return x.a == z.a && x.b == z.b;
382382
}
383383
static_assert(test_pad_buffer());
384+
385+
constexpr unsigned char identity1a = 42;
386+
constexpr unsigned char identity1b = __builtin_bit_cast(unsigned char, identity1a);
387+
static_assert(identity1b == 42);
388+
389+
struct IdentityInStruct {
390+
unsigned char n;
391+
};
392+
constexpr IdentityInStruct identity2a = {42};
393+
constexpr unsigned char identity2b = __builtin_bit_cast(unsigned char, identity2a.n);
394+
395+
union IdentityInUnion {
396+
unsigned char n;
397+
};
398+
constexpr IdentityInUnion identity3a = {42};
399+
constexpr unsigned char identity3b = __builtin_bit_cast(unsigned char, identity3a.n);

0 commit comments

Comments
 (0)