523 changes: 233 additions & 290 deletions clang/test/CodeGenCXX/auto-var-init.cpp

Large diffs are not rendered by default.

158 changes: 68 additions & 90 deletions clang/test/CodeGenCXX/microsoft-abi-dynamic-cast.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %clang_cc1 -no-opaque-pointers -emit-llvm -O1 -o - -fexceptions -triple=i386-pc-win32 %s | FileCheck %s
// RUN: %clang_cc1 -emit-llvm -O1 -o - -fexceptions -triple=i386-pc-win32 %s | FileCheck %s

struct S { char a; };
struct V { virtual void f(); };
Expand All @@ -7,120 +7,99 @@ struct B : S, virtual V {};
struct T {};

T* test0() { return dynamic_cast<T*>((B*)0); }
// CHECK-LABEL: define dso_local noalias noundef %struct.T* @"?test0@@YAPAUT@@XZ"()
// CHECK: ret %struct.T* null
// CHECK-LABEL: define dso_local noalias noundef ptr @"?test0@@YAPAUT@@XZ"()
// CHECK: ret ptr null

T* test1(V* x) { return &dynamic_cast<T&>(*x); }
// CHECK-LABEL: define dso_local noundef %struct.T* @"?test1@@YAPAUT@@PAUV@@@Z"(%struct.V* noundef %x)
// CHECK: [[CAST:%.*]] = bitcast %struct.V* %x to i8*
// CHECK-NEXT: [[CALL:%.*]] = tail call i8* @__RTDynamicCast(i8* [[CAST]], i32 0, i8* nonnull bitcast (%rtti.TypeDescriptor7* @"??_R0?AUV@@@8" to i8*), i8* nonnull bitcast (%rtti.TypeDescriptor7* @"??_R0?AUT@@@8" to i8*), i32 1)
// CHECK-NEXT: [[RET:%.*]] = bitcast i8* [[CALL]] to %struct.T*
// CHECK-NEXT: ret %struct.T* [[RET]]
// CHECK-LABEL: define dso_local noundef ptr @"?test1@@YAPAUT@@PAUV@@@Z"(ptr noundef %x)
// CHECK: [[CALL:%.*]] = tail call ptr @__RTDynamicCast(ptr %x, i32 0, ptr nonnull @"??_R0?AUV@@@8", ptr nonnull @"??_R0?AUT@@@8", i32 1)
// CHECK-NEXT: ret ptr [[CALL]]

T* test2(A* x) { return &dynamic_cast<T&>(*x); }
// CHECK-LABEL: define dso_local noundef %struct.T* @"?test2@@YAPAUT@@PAUA@@@Z"(%struct.A* noundef %x)
// CHECK: [[CAST:%.*]] = bitcast %struct.A* %x to i8*
// CHECK-NEXT: [[VBPTRPTR:%.*]] = getelementptr %struct.A, %struct.A* %x, i32 0, i32 0
// CHECK-NEXT: [[VBTBL:%.*]] = load i32*, i32** [[VBPTRPTR]], align 4
// CHECK-NEXT: [[VBOFFP:%.*]] = getelementptr inbounds i32, i32* [[VBTBL]], i32 1
// CHECK-NEXT: [[VBOFFS:%.*]] = load i32, i32* [[VBOFFP]], align 4
// CHECK-NEXT: [[ADJ:%.*]] = getelementptr inbounds i8, i8* [[CAST]], i32 [[VBOFFS]]
// CHECK-NEXT: [[CALL:%.*]] = tail call i8* @__RTDynamicCast(i8* [[ADJ]], i32 [[VBOFFS]], i8* nonnull bitcast (%rtti.TypeDescriptor7* @"??_R0?AUA@@@8" to i8*), i8* nonnull bitcast (%rtti.TypeDescriptor7* @"??_R0?AUT@@@8" to i8*), i32 1)
// CHECK-NEXT: [[RET:%.*]] = bitcast i8* [[CALL]] to %struct.T*
// CHECK-NEXT: ret %struct.T* [[RET]]
// CHECK-LABEL: define dso_local noundef ptr @"?test2@@YAPAUT@@PAUA@@@Z"(ptr noundef %x)
// CHECK: [[VBTBL:%.*]] = load ptr, ptr %x, align 4
// CHECK-NEXT: [[VBOFFP:%.*]] = getelementptr inbounds i32, ptr [[VBTBL]], i32 1
// CHECK-NEXT: [[VBOFFS:%.*]] = load i32, ptr [[VBOFFP]], align 4
// CHECK-NEXT: [[ADJ:%.*]] = getelementptr inbounds i8, ptr %x, i32 [[VBOFFS]]
// CHECK-NEXT: [[CALL:%.*]] = tail call ptr @__RTDynamicCast(ptr nonnull [[ADJ]], i32 [[VBOFFS]], ptr nonnull @"??_R0?AUA@@@8", ptr nonnull @"??_R0?AUT@@@8", i32 1)
// CHECK-NEXT: ret ptr [[CALL]]

T* test3(B* x) { return &dynamic_cast<T&>(*x); }
// CHECK-LABEL: define dso_local noundef %struct.T* @"?test3@@YAPAUT@@PAUB@@@Z"(%struct.B* noundef %x)
// CHECK: [[VOIDP:%.*]] = getelementptr %struct.B, %struct.B* %x, i32 0, i32 0, i32 0
// CHECK-NEXT: [[VBPTR:%.*]] = getelementptr inbounds i8, i8* [[VOIDP]], i32 4
// CHECK-NEXT: [[VBPTRPTR:%.*]] = bitcast i8* [[VBPTR:%.*]] to i32**
// CHECK-NEXT: [[VBTBL:%.*]] = load i32*, i32** [[VBPTRPTR]], align 4
// CHECK-NEXT: [[VBOFFP:%.*]] = getelementptr inbounds i32, i32* [[VBTBL]], i32 1
// CHECK-NEXT: [[VBOFFS:%.*]] = load i32, i32* [[VBOFFP]], align 4
// CHECK-LABEL: define dso_local noundef ptr @"?test3@@YAPAUT@@PAUB@@@Z"(ptr noundef %x)
// CHECK: [[VBPTR:%.*]] = getelementptr inbounds i8, ptr %x, i32 4
// CHECK-NEXT: [[VBTBL:%.*]] = load ptr, ptr [[VBPTR:%.*]], align 4
// CHECK-NEXT: [[VBOFFP:%.*]] = getelementptr inbounds i32, ptr [[VBTBL]], i32 1
// CHECK-NEXT: [[VBOFFS:%.*]] = load i32, ptr [[VBOFFP]], align 4
// CHECK-NEXT: [[DELTA:%.*]] = add nsw i32 [[VBOFFS]], 4
// CHECK-NEXT: [[ADJ:%.*]] = getelementptr inbounds i8, i8* [[VOIDP]], i32 [[DELTA]]
// CHECK-NEXT: [[CALL:%.*]] = tail call i8* @__RTDynamicCast(i8* [[ADJ]], i32 [[DELTA]], i8* nonnull bitcast (%rtti.TypeDescriptor7* @"??_R0?AUB@@@8" to i8*), i8* nonnull bitcast (%rtti.TypeDescriptor7* @"??_R0?AUT@@@8" to i8*), i32 1)
// CHECK-NEXT: [[RET:%.*]] = bitcast i8* [[CALL]] to %struct.T*
// CHECK-NEXT: ret %struct.T* [[RET]]
// CHECK-NEXT: [[ADJ:%.*]] = getelementptr inbounds i8, ptr %x, i32 [[DELTA]]
// CHECK-NEXT: [[CALL:%.*]] = tail call ptr @__RTDynamicCast(ptr [[ADJ]], i32 [[DELTA]], ptr nonnull @"??_R0?AUB@@@8", ptr nonnull @"??_R0?AUT@@@8", i32 1)
// CHECK-NEXT: ret ptr [[CALL]]

T* test4(V* x) { return dynamic_cast<T*>(x); }
// CHECK-LABEL: define dso_local noundef %struct.T* @"?test4@@YAPAUT@@PAUV@@@Z"(%struct.V* noundef %x)
// CHECK: [[CAST:%.*]] = bitcast %struct.V* %x to i8*
// CHECK-NEXT: [[CALL:%.*]] = tail call i8* @__RTDynamicCast(i8* [[CAST]], i32 0, i8* nonnull bitcast (%rtti.TypeDescriptor7* @"??_R0?AUV@@@8" to i8*), i8* nonnull bitcast (%rtti.TypeDescriptor7* @"??_R0?AUT@@@8" to i8*), i32 0)
// CHECK-NEXT: [[RET:%.*]] = bitcast i8* [[CALL]] to %struct.T*
// CHECK-NEXT: ret %struct.T* [[RET]]
// CHECK-LABEL: define dso_local noundef ptr @"?test4@@YAPAUT@@PAUV@@@Z"(ptr noundef %x)
// CHECK: [[CALL:%.*]] = tail call ptr @__RTDynamicCast(ptr %x, i32 0, ptr nonnull @"??_R0?AUV@@@8", ptr nonnull @"??_R0?AUT@@@8", i32 0)
// CHECK-NEXT: ret ptr [[CALL]]

T* test5(A* x) { return dynamic_cast<T*>(x); }
// CHECK-LABEL: define dso_local noundef %struct.T* @"?test5@@YAPAUT@@PAUA@@@Z"(%struct.A* noundef %x)
// CHECK: [[CHECK:%.*]] = icmp eq %struct.A* %x, null
// CHECK-LABEL: define dso_local noundef ptr @"?test5@@YAPAUT@@PAUA@@@Z"(ptr noundef %x)
// CHECK: [[CHECK:%.*]] = icmp eq ptr %x, null
// CHECK-NEXT: br i1 [[CHECK]]
// CHECK: [[VOIDP:%.*]] = bitcast %struct.A* %x to i8*
// CHECK-NEXT: [[VBPTRPTR:%.*]] = getelementptr %struct.A, %struct.A* %x, i32 0, i32 0
// CHECK-NEXT: [[VBTBL:%.*]] = load i32*, i32** [[VBPTRPTR]], align 4
// CHECK-NEXT: [[VBOFFP:%.*]] = getelementptr inbounds i32, i32* [[VBTBL]], i32 1
// CHECK-NEXT: [[VBOFFS:%.*]] = load i32, i32* [[VBOFFP]], align 4
// CHECK-NEXT: [[ADJ:%.*]] = getelementptr inbounds i8, i8* [[VOIDP]], i32 [[VBOFFS]]
// CHECK-NEXT: [[CALL:%.*]] = tail call i8* @__RTDynamicCast(i8* nonnull [[ADJ]], i32 [[VBOFFS]], i8* {{.*}}bitcast (%rtti.TypeDescriptor7* @"??_R0?AUA@@@8" to i8*), i8* {{.*}}bitcast (%rtti.TypeDescriptor7* @"??_R0?AUT@@@8" to i8*), i32 0)
// CHECK-NEXT: [[RES:%.*]] = bitcast i8* [[CALL]] to %struct.T*
// CHECK: [[VBTBL:%.*]] = load ptr, ptr %x, align 4
// CHECK-NEXT: [[VBOFFP:%.*]] = getelementptr inbounds i32, ptr [[VBTBL]], i32 1
// CHECK-NEXT: [[VBOFFS:%.*]] = load i32, ptr [[VBOFFP]], align 4
// CHECK-NEXT: [[ADJ:%.*]] = getelementptr inbounds i8, ptr %x, i32 [[VBOFFS]]
// CHECK-NEXT: [[CALL:%.*]] = tail call ptr @__RTDynamicCast(ptr nonnull [[ADJ]], i32 [[VBOFFS]], ptr {{.*}}@"??_R0?AUA@@@8", ptr {{.*}}@"??_R0?AUT@@@8", i32 0)
// CHECK-NEXT: br label
// CHECK: [[RET:%.*]] = phi %struct.T*
// CHECK-NEXT: ret %struct.T* [[RET]]
// CHECK: [[RET:%.*]] = phi ptr
// CHECK-NEXT: ret ptr [[RET]]

T* test6(B* x) { return dynamic_cast<T*>(x); }
// CHECK-LABEL: define dso_local noundef %struct.T* @"?test6@@YAPAUT@@PAUB@@@Z"(%struct.B* noundef %x)
// CHECK: [[CHECK:%.*]] = icmp eq %struct.B* %x, null
// CHECK-LABEL: define dso_local noundef ptr @"?test6@@YAPAUT@@PAUB@@@Z"(ptr noundef %x)
// CHECK: [[CHECK:%.*]] = icmp eq ptr %x, null
// CHECK-NEXT: br i1 [[CHECK]]
// CHECK: [[CAST:%.*]] = getelementptr %struct.B, %struct.B* %x, i32 0, i32 0, i32 0
// CHECK-NEXT: [[VBPTR:%.*]] = getelementptr inbounds i8, i8* [[CAST]], i32 4
// CHECK-NEXT: [[VBPTRPTR:%.*]] = bitcast i8* [[VBPTR]] to i32**
// CHECK-NEXT: [[VBTBL:%.*]] = load i32*, i32** [[VBPTRPTR]], align 4
// CHECK-NEXT: [[VBOFFP:%.*]] = getelementptr inbounds i32, i32* [[VBTBL]], i32 1
// CHECK-NEXT: [[VBOFFS:%.*]] = load i32, i32* [[VBOFFP]], align 4
// CHECK: [[VBPTR:%.*]] = getelementptr inbounds i8, ptr %x, i32 4
// CHECK-NEXT: [[VBTBL:%.*]] = load ptr, ptr [[VBPTR]], align 4
// CHECK-NEXT: [[VBOFFP:%.*]] = getelementptr inbounds i32, ptr [[VBTBL]], i32 1
// CHECK-NEXT: [[VBOFFS:%.*]] = load i32, ptr [[VBOFFP]], align 4
// CHECK-NEXT: [[DELTA:%.*]] = add nsw i32 [[VBOFFS]], 4
// CHECK-NEXT: [[ADJ:%.*]] = getelementptr inbounds i8, i8* [[CAST]], i32 [[DELTA]]
// CHECK-NEXT: [[CALL:%.*]] = tail call i8* @__RTDynamicCast(i8* [[ADJ]], i32 [[DELTA]], i8* {{.*}}bitcast (%rtti.TypeDescriptor7* @"??_R0?AUB@@@8" to i8*), i8* {{.*}}bitcast (%rtti.TypeDescriptor7* @"??_R0?AUT@@@8" to i8*), i32 0)
// CHECK-NEXT: [[RES:%.*]] = bitcast i8* [[CALL]] to %struct.T*
// CHECK-NEXT: [[ADJ:%.*]] = getelementptr inbounds i8, ptr %x, i32 [[DELTA]]
// CHECK-NEXT: [[CALL:%.*]] = tail call ptr @__RTDynamicCast(ptr nonnull [[ADJ]], i32 [[DELTA]], ptr {{.*}}@"??_R0?AUB@@@8", ptr {{.*}}@"??_R0?AUT@@@8", i32 0)
// CHECK-NEXT: br label
// CHECK: [[RET:%.*]] = phi %struct.T*
// CHECK-NEXT: ret %struct.T* [[RET]]
// CHECK: [[RET:%.*]] = phi ptr
// CHECK-NEXT: ret ptr [[RET]]

void* test7(V* x) { return dynamic_cast<void*>(x); }
// CHECK-LABEL: define dso_local noundef i8* @"?test7@@YAPAXPAUV@@@Z"(%struct.V* noundef %x)
// CHECK: [[CAST:%.*]] = bitcast %struct.V* %x to i8*
// CHECK-NEXT: [[RET:%.*]] = tail call i8* @__RTCastToVoid(i8* [[CAST]])
// CHECK-NEXT: ret i8* [[RET]]
// CHECK-LABEL: define dso_local noundef ptr @"?test7@@YAPAXPAUV@@@Z"(ptr noundef %x)
// CHECK: [[RET:%.*]] = tail call ptr @__RTCastToVoid(ptr %x)
// CHECK-NEXT: ret ptr [[RET]]

void* test8(A* x) { return dynamic_cast<void*>(x); }
// CHECK-LABEL: define dso_local noundef i8* @"?test8@@YAPAXPAUA@@@Z"(%struct.A* noundef %x)
// CHECK: [[CHECK:%.*]] = icmp eq %struct.A* %x, null
// CHECK-LABEL: define dso_local noundef ptr @"?test8@@YAPAXPAUA@@@Z"(ptr noundef %x)
// CHECK: [[CHECK:%.*]] = icmp eq ptr %x, null
// CHECK-NEXT: br i1 [[CHECK]]
// CHECK: [[VOIDP:%.*]] = bitcast %struct.A* %x to i8*
// CHECK-NEXT: [[VBPTRPTR:%.*]] = getelementptr %struct.A, %struct.A* %x, i32 0, i32 0
// CHECK-NEXT: [[VBTBL:%.*]] = load i32*, i32** [[VBPTRPTR]], align 4
// CHECK-NEXT: [[VBOFFP:%.*]] = getelementptr inbounds i32, i32* [[VBTBL]], i32 1
// CHECK-NEXT: [[VBOFFS:%.*]] = load i32, i32* [[VBOFFP]], align 4
// CHECK-NEXT: [[ADJ:%.*]] = getelementptr inbounds i8, i8* [[VOIDP]], i32 [[VBOFFS]]
// CHECK-NEXT: [[RES:%.*]] = tail call i8* @__RTCastToVoid(i8* nonnull [[ADJ]])
// CHECK: [[VBTBL:%.*]] = load ptr, ptr %x, align 4
// CHECK-NEXT: [[VBOFFP:%.*]] = getelementptr inbounds i32, ptr [[VBTBL]], i32 1
// CHECK-NEXT: [[VBOFFS:%.*]] = load i32, ptr [[VBOFFP]], align 4
// CHECK-NEXT: [[ADJ:%.*]] = getelementptr inbounds i8, ptr %x, i32 [[VBOFFS]]
// CHECK-NEXT: [[RES:%.*]] = tail call ptr @__RTCastToVoid(ptr nonnull [[ADJ]])
// CHECK-NEXT: br label
// CHECK: [[RET:%.*]] = phi i8*
// CHECK-NEXT: ret i8* [[RET]]
// CHECK: [[RET:%.*]] = phi ptr
// CHECK-NEXT: ret ptr [[RET]]

void* test9(B* x) { return dynamic_cast<void*>(x); }
// CHECK-LABEL: define dso_local noundef i8* @"?test9@@YAPAXPAUB@@@Z"(%struct.B* noundef %x)
// CHECK: [[CHECK:%.*]] = icmp eq %struct.B* %x, null
// CHECK-LABEL: define dso_local noundef ptr @"?test9@@YAPAXPAUB@@@Z"(ptr noundef %x)
// CHECK: [[CHECK:%.*]] = icmp eq ptr %x, null
// CHECK-NEXT: br i1 [[CHECK]]
// CHECK: [[CAST:%.*]] = getelementptr %struct.B, %struct.B* %x, i32 0, i32 0, i32 0
// CHECK-NEXT: [[VBPTR:%.*]] = getelementptr inbounds i8, i8* [[CAST]], i32 4
// CHECK-NEXT: [[VBPTRPTR:%.*]] = bitcast i8* [[VBPTR]] to i32**
// CHECK-NEXT: [[VBTBL:%.*]] = load i32*, i32** [[VBPTRPTR]], align 4
// CHECK-NEXT: [[VBOFFP:%.*]] = getelementptr inbounds i32, i32* [[VBTBL]], i32 1
// CHECK-NEXT: [[VBOFFS:%.*]] = load i32, i32* [[VBOFFP]], align 4
// CHECK: [[VBPTR:%.*]] = getelementptr inbounds i8, ptr %x, i32 4
// CHECK-NEXT: [[VBTBL:%.*]] = load ptr, ptr [[VBPTR]], align 4
// CHECK-NEXT: [[VBOFFP:%.*]] = getelementptr inbounds i32, ptr [[VBTBL]], i32 1
// CHECK-NEXT: [[VBOFFS:%.*]] = load i32, ptr [[VBOFFP]], align 4
// CHECK-NEXT: [[DELTA:%.*]] = add nsw i32 [[VBOFFS]], 4
// CHECK-NEXT: [[ADJ:%.*]] = getelementptr inbounds i8, i8* [[CAST]], i32 [[DELTA]]
// CHECK-NEXT: [[CALL:%.*]] = tail call i8* @__RTCastToVoid(i8* [[ADJ]])
// CHECK-NEXT: [[ADJ:%.*]] = getelementptr inbounds i8, ptr %x, i32 [[DELTA]]
// CHECK-NEXT: [[CALL:%.*]] = tail call ptr @__RTCastToVoid(ptr nonnull [[ADJ]])
// CHECK-NEXT: br label
// CHECK: [[RET:%.*]] = phi i8*
// CHECK-NEXT: ret i8* [[RET]]
// CHECK: [[RET:%.*]] = phi ptr
// CHECK-NEXT: ret ptr [[RET]]

namespace PR25606 {
struct Cleanup {
Expand All @@ -134,10 +113,9 @@ S3 *f(S2 &s) {
Cleanup c;
return dynamic_cast<S3 *>(&s);
}
// CHECK-LABEL: define dso_local noundef %"struct.PR25606::S3"* @"?f@PR25606@@YAPAUS3@1@AAUS2@1@@Z"(
// CHECK: [[CALL:%.*]] = invoke i8* @__RTDynamicCast
// CHECK-LABEL: define dso_local noundef ptr @"?f@PR25606@@YAPAUS3@1@AAUS2@1@@Z"(
// CHECK: [[CALL:%.*]] = invoke ptr @__RTDynamicCast

// CHECK: [[BC:%.*]] = bitcast i8* [[CALL]] to %"struct.PR25606::S3"*
// CHECK: call x86_thiscallcc void @"??1Cleanup@PR25606@@QAE@XZ"(
// CHECK: ret %"struct.PR25606::S3"* [[BC]]
// CHECK: ret ptr [[CALL]]
}
53 changes: 25 additions & 28 deletions clang/test/CodeGenCXX/microsoft-abi-typeid.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %clang_cc1 -no-opaque-pointers -emit-llvm -O1 -o - -triple=i386-pc-win32 %s -fexceptions -fcxx-exceptions | FileCheck %s
// RUN: %clang_cc1 -emit-llvm -O1 -o - -triple=i386-pc-win32 %s -fexceptions -fcxx-exceptions | FileCheck %s

struct type_info;
namespace std { using ::type_info; }
Expand All @@ -12,45 +12,42 @@ extern int b;
A* fn();

const std::type_info* test0_typeid() { return &typeid(int); }
// CHECK-LABEL: define dso_local noundef nonnull %struct.type_info* @"?test0_typeid@@YAPBUtype_info@@XZ"()
// CHECK: ret %struct.type_info* bitcast (%rtti.TypeDescriptor2* @"??_R0H@8" to %struct.type_info*)
// CHECK-LABEL: define dso_local noundef nonnull ptr @"?test0_typeid@@YAPBUtype_info@@XZ"()
// CHECK: ret ptr @"??_R0H@8"

const std::type_info* test1_typeid() { return &typeid(A); }
// CHECK-LABEL: define dso_local noundef nonnull %struct.type_info* @"?test1_typeid@@YAPBUtype_info@@XZ"()
// CHECK: ret %struct.type_info* bitcast (%rtti.TypeDescriptor7* @"??_R0?AUA@@@8" to %struct.type_info*)
// CHECK-LABEL: define dso_local noundef nonnull ptr @"?test1_typeid@@YAPBUtype_info@@XZ"()
// CHECK: ret ptr @"??_R0?AUA@@@8"

const std::type_info* test2_typeid() { return &typeid(&a); }
// CHECK-LABEL: define dso_local noundef nonnull %struct.type_info* @"?test2_typeid@@YAPBUtype_info@@XZ"()
// CHECK: ret %struct.type_info* bitcast (%rtti.TypeDescriptor7* @"??_R0PAUA@@@8" to %struct.type_info*)
// CHECK-LABEL: define dso_local noundef nonnull ptr @"?test2_typeid@@YAPBUtype_info@@XZ"()
// CHECK: ret ptr @"??_R0PAUA@@@8"

const std::type_info* test3_typeid() { return &typeid(*fn()); }
// CHECK-LABEL: define dso_local noundef %struct.type_info* @"?test3_typeid@@YAPBUtype_info@@XZ"()
// CHECK: [[CALL:%.*]] = tail call noundef %struct.A* @"?fn@@YAPAUA@@XZ"()
// CHECK-NEXT: [[CMP:%.*]] = icmp eq %struct.A* [[CALL]], null
// CHECK-LABEL: define dso_local noundef ptr @"?test3_typeid@@YAPBUtype_info@@XZ"()
// CHECK: [[CALL:%.*]] = tail call noundef ptr @"?fn@@YAPAUA@@XZ"()
// CHECK-NEXT: [[CMP:%.*]] = icmp eq ptr [[CALL]], null
// CHECK-NEXT: br i1 [[CMP]]
// CHECK: call i8* @__RTtypeid(i8* null)
// CHECK: call ptr @__RTtypeid(ptr null)
// CHECK-NEXT: unreachable
// CHECK: [[THIS:%.*]] = bitcast %struct.A* [[CALL]] to i8*
// CHECK-NEXT: [[VBTBLP:%.*]] = getelementptr %struct.A, %struct.A* [[CALL]], i32 0, i32 0
// CHECK-NEXT: [[VBTBL:%.*]] = load i32*, i32** [[VBTBLP]], align 4
// CHECK-NEXT: [[VBSLOT:%.*]] = getelementptr inbounds i32, i32* [[VBTBL]], i32 1
// CHECK-NEXT: [[VBASE_OFFS:%.*]] = load i32, i32* [[VBSLOT]], align 4
// CHECK-NEXT: [[ADJ:%.*]] = getelementptr inbounds i8, i8* [[THIS]], i32 [[VBASE_OFFS]]
// CHECK-NEXT: [[RT:%.*]] = tail call i8* @__RTtypeid(i8* nonnull [[ADJ]])
// CHECK-NEXT: [[RET:%.*]] = bitcast i8* [[RT]] to %struct.type_info*
// CHECK-NEXT: ret %struct.type_info* [[RET]]
// CHECK: [[VBTBL:%.*]] = load ptr, ptr [[CALL]], align 4
// CHECK-NEXT: [[VBSLOT:%.*]] = getelementptr inbounds i32, ptr [[VBTBL]], i32 1
// CHECK-NEXT: [[VBASE_OFFS:%.*]] = load i32, ptr [[VBSLOT]], align 4
// CHECK-NEXT: [[ADJ:%.*]] = getelementptr inbounds i8, ptr [[CALL]], i32 [[VBASE_OFFS]]
// CHECK-NEXT: [[RT:%.*]] = tail call ptr @__RTtypeid(ptr nonnull [[ADJ]])
// CHECK-NEXT: ret ptr [[RT]]

const std::type_info* test4_typeid() { return &typeid(b); }
// CHECK: define dso_local noundef nonnull %struct.type_info* @"?test4_typeid@@YAPBUtype_info@@XZ"()
// CHECK: ret %struct.type_info* bitcast (%rtti.TypeDescriptor2* @"??_R0H@8" to %struct.type_info*)
// CHECK: define dso_local noundef nonnull ptr @"?test4_typeid@@YAPBUtype_info@@XZ"()
// CHECK: ret ptr @"??_R0H@8"

const std::type_info* test5_typeid() { return &typeid(v); }
// CHECK: define dso_local noundef nonnull %struct.type_info* @"?test5_typeid@@YAPBUtype_info@@XZ"()
// CHECK: ret %struct.type_info* bitcast (%rtti.TypeDescriptor7* @"??_R0?AUV@@@8" to %struct.type_info*)
// CHECK: define dso_local noundef nonnull ptr @"?test5_typeid@@YAPBUtype_info@@XZ"()
// CHECK: ret ptr @"??_R0?AUV@@@8"

const std::type_info *test6_typeid() { return &typeid((V &)v); }
// CHECK: define dso_local noundef nonnull %struct.type_info* @"?test6_typeid@@YAPBUtype_info@@XZ"()
// CHECK: ret %struct.type_info* bitcast (%rtti.TypeDescriptor7* @"??_R0?AUV@@@8" to %struct.type_info*)
// CHECK: define dso_local noundef nonnull ptr @"?test6_typeid@@YAPBUtype_info@@XZ"()
// CHECK: ret ptr @"??_R0?AUV@@@8"

namespace PR26329 {
struct Polymorphic {
Expand All @@ -67,6 +64,6 @@ void f(const Polymorphic &poly) {
}
// CHECK-LABEL: define dso_local void @"?f@PR26329@@YAXABUPolymorphic@1@@Z"(
// CHECK: %[[cs:.*]] = catchswitch within none [label %{{.*}}] unwind to caller
// CHECK: %[[cp:.*]] = catchpad within %[[cs]] [i8* null, i32 64, i8* null]
// CHECK: invoke i8* @__RTtypeid(i8* {{.*}}) [ "funclet"(token %[[cp]]) ]
// CHECK: %[[cp:.*]] = catchpad within %[[cs]] [ptr null, i32 64, ptr null]
// CHECK: invoke ptr @__RTtypeid(ptr {{.*}}) [ "funclet"(token %[[cp]]) ]
}
55 changes: 0 additions & 55 deletions llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2554,57 +2554,6 @@ Instruction *InstCombinerImpl::optimizeBitCastFromPhi(CastInst &CI,
return RetVal;
}

static Instruction *convertBitCastToGEP(BitCastInst &CI, IRBuilderBase &Builder,
const DataLayout &DL) {
Value *Src = CI.getOperand(0);
PointerType *SrcPTy = cast<PointerType>(Src->getType());
PointerType *DstPTy = cast<PointerType>(CI.getType());

// Bitcasts involving opaque pointers cannot be converted into a GEP.
if (SrcPTy->isOpaque() || DstPTy->isOpaque())
return nullptr;

Type *DstElTy = DstPTy->getNonOpaquePointerElementType();
Type *SrcElTy = SrcPTy->getNonOpaquePointerElementType();

// When the type pointed to is not sized the cast cannot be
// turned into a gep.
if (!SrcElTy->isSized())
return nullptr;

// If the source and destination are pointers, and this cast is equivalent
// to a getelementptr X, 0, 0, 0... turn it into the appropriate gep.
// This can enhance SROA and other transforms that want type-safe pointers.
unsigned NumZeros = 0;
while (SrcElTy && SrcElTy != DstElTy) {
SrcElTy = GetElementPtrInst::getTypeAtIndex(SrcElTy, (uint64_t)0);
++NumZeros;
}

// If we found a path from the src to dest, create the getelementptr now.
if (SrcElTy == DstElTy) {
SmallVector<Value *, 8> Idxs(NumZeros + 1, Builder.getInt32(0));
GetElementPtrInst *GEP = GetElementPtrInst::Create(
SrcPTy->getNonOpaquePointerElementType(), Src, Idxs);

// If the source pointer is dereferenceable, then assume it points to an
// allocated object and apply "inbounds" to the GEP.
bool CanBeNull, CanBeFreed;
if (Src->getPointerDereferenceableBytes(DL, CanBeNull, CanBeFreed)) {
// In a non-default address space (not 0), a null pointer can not be
// assumed inbounds, so ignore that case (dereferenceable_or_null).
// The reason is that 'null' is not treated differently in these address
// spaces, and we consequently ignore the 'gep inbounds' special case
// for 'null' which allows 'inbounds' on 'null' if the indices are
// zeros.
if (SrcPTy->getAddressSpace() == 0 || !CanBeNull)
GEP->setIsInBounds();
}
return GEP;
}
return nullptr;
}

Instruction *InstCombinerImpl::visitBitCast(BitCastInst &CI) {
// If the operands are integer typed then apply the integer transforms,
// otherwise just apply the common ones.
Expand All @@ -2617,10 +2566,6 @@ Instruction *InstCombinerImpl::visitBitCast(BitCastInst &CI) {
if (DestTy == Src->getType())
return replaceInstUsesWith(CI, Src);

if (isa<PointerType>(SrcTy) && isa<PointerType>(DestTy))
if (Instruction *I = convertBitCastToGEP(CI, Builder, DL))
return I;

if (FixedVectorType *DestVTy = dyn_cast<FixedVectorType>(DestTy)) {
// Beware: messing with this target-specific oddity may cause trouble.
if (DestVTy->getNumElements() == 1 && SrcTy->isX86_MMXTy()) {
Expand Down