llvm-project/clang/test/CodeGenCXX/dynamic-cast-exact.cpp
Nikita Popov 0f46e31cfb
[IR] Change representation of getelementptr inrange (#84341)
As part of the migration to ptradd
(https://discourse.llvm.org/t/rfc-replacing-getelementptr-with-ptradd/68699),
we need to change the representation of the `inrange` attribute, which
is used for vtable splitting.

Currently, inrange is specified as follows:

```
getelementptr inbounds ({ [4 x ptr], [4 x ptr] }, ptr @vt, i64 0, inrange i32 1, i64 2)
```

The `inrange` is placed on a GEP index, and all accesses must be "in
range" of that index. The new representation is as follows:

```
getelementptr inbounds inrange(-16, 16) ({ [4 x ptr], [4 x ptr] }, ptr @vt, i64 0, i32 1, i64 2)
```

This specifies which offsets are "in range" of the GEP result. The new
representation will continue working when canonicalizing to ptradd
representation:

```
getelementptr inbounds inrange(-16, 16) (i8, ptr @vt, i64 48)
```

The inrange offsets are relative to the return value of the GEP. An
alternative design could make them relative to the source pointer
instead. The result-relative format was chosen on the off-chance that we
want to extend support to non-constant GEPs in the future, in which case
this variant is more expressive.

This implementation "upgrades" the old inrange representation in bitcode
by simply dropping it. This is a very niche feature, and I don't think
trying to upgrade it is worthwhile. Let me know if you disagree.
2024-03-20 10:59:45 +01:00

88 lines
3.5 KiB
C++

// RUN: %clang_cc1 -I%S %s -triple x86_64-apple-darwin10 -emit-llvm -fcxx-exceptions -fexceptions -std=c++11 -o - -O1 -disable-llvm-passes | FileCheck %s --implicit-check-not='call {{.*}} @__dynamic_cast'
struct Offset { virtual ~Offset(); };
struct A { virtual ~A(); };
struct B final : Offset, A { };
struct C { virtual ~C(); int c; };
struct D : A { int d; };
struct E : A { int e; };
struct F : virtual A { int f; };
struct G : virtual A { int g; };
struct H final : C, D, E, F, G { int h; };
// CHECK-LABEL: @_Z7inexactP1A
C *inexact(A *a) {
// CHECK: call {{.*}} @__dynamic_cast
return dynamic_cast<C*>(a);
}
// CHECK-LABEL: @_Z12exact_singleP1A
B *exact_single(A *a) {
// CHECK: %[[PTR_NULL:.*]] = icmp eq ptr %[[PTR:.*]], null
// CHECK: br i1 %[[PTR_NULL]], label %[[LABEL_FAILED:.*]], label %[[LABEL_NOTNULL:.*]]
// CHECK: [[LABEL_NOTNULL]]:
// CHECK: %[[VPTR:.*]] = load ptr, ptr %[[PTR]]
// CHECK: %[[MATCH:.*]] = icmp eq ptr %[[VPTR]], getelementptr inbounds inrange(-16, 16) ({ [4 x ptr], [4 x ptr] }, ptr @_ZTV1B, i32 0, i32 1, i32 2)
// CHECK: %[[RESULT:.*]] = getelementptr inbounds i8, ptr %[[PTR]], i64 -8
// CHECK: br i1 %[[MATCH]], label %[[LABEL_END:.*]], label %[[LABEL_FAILED]]
// CHECK: [[LABEL_FAILED]]:
// CHECK: br label %[[LABEL_END]]
// CHECK: [[LABEL_END]]:
// CHECK: phi ptr [ %[[RESULT]], %[[LABEL_NOTNULL]] ], [ null, %[[LABEL_FAILED]] ]
return dynamic_cast<B*>(a);
}
// CHECK-LABEL: @_Z9exact_refR1A
B &exact_ref(A &a) {
// CHECK: %[[PTR_NULL:.*]] = icmp eq ptr %[[PTR:.*]], null
// CHECK: br i1 %[[PTR_NULL]], label %[[LABEL_FAILED:.*]], label %[[LABEL_NOTNULL:.*]]
// CHECK: [[LABEL_NOTNULL]]:
// CHECK: %[[VPTR:.*]] = load ptr, ptr %[[PTR]]
// CHECK: %[[MATCH:.*]] = icmp eq ptr %[[VPTR]], getelementptr inbounds inrange(-16, 16) ({ [4 x ptr], [4 x ptr] }, ptr @_ZTV1B, i32 0, i32 1, i32 2)
// CHECK: %[[RESULT:.*]] = getelementptr inbounds i8, ptr %[[PTR]], i64 -8
// CHECK: br i1 %[[MATCH]], label %[[LABEL_END:.*]], label %[[LABEL_FAILED]]
// CHECK: [[LABEL_FAILED]]:
// CHECK: call {{.*}} @__cxa_bad_cast
// CHECK: unreachable
// CHECK: [[LABEL_END]]:
// CHECK: ret ptr %[[RESULT]]
return dynamic_cast<B&>(a);
}
// CHECK-LABEL: @_Z11exact_multiP1A
H *exact_multi(A *a) {
// CHECK: %[[PTR_NULL:.*]] = icmp eq ptr %[[PTR:.*]], null
// CHECK: br i1 %[[PTR_NULL]], label %[[LABEL_FAILED:.*]], label %[[LABEL_NOTNULL:.*]]
// CHECK: [[LABEL_NOTNULL]]:
// CHECK: %[[VPTR:.*]] = load ptr, ptr %[[PTR]]
// CHECK: %[[OFFSET_TO_TOP_SLOT:.*]] = getelementptr inbounds i64, ptr %[[VPTR]], i64 -2
// CHECK: %[[OFFSET_TO_TOP:.*]] = load i64, ptr %[[OFFSET_TO_TOP_SLOT]]
// CHECK: %[[RESULT:.*]] = getelementptr inbounds i8, ptr %[[PTR]], i64 %[[OFFSET_TO_TOP]]
// CHECK: %[[DERIVED_VPTR:.*]] = load ptr, ptr %[[RESULT]]
// CHECK: %[[MATCH:.*]] = icmp eq ptr %[[DERIVED_VPTR]], getelementptr inbounds inrange(-24, 16) ({ [5 x ptr], [4 x ptr], [4 x ptr], [6 x ptr], [6 x ptr] }, ptr @_ZTV1H, i32 0, i32 0, i32 3)
// CHECK: br i1 %[[MATCH]], label %[[LABEL_END:.*]], label %[[LABEL_FAILED]]
// CHECK: [[LABEL_FAILED]]:
// CHECK: br label %[[LABEL_END]]
// CHECK: [[LABEL_END]]:
// CHECK: phi ptr [ %[[RESULT]], %[[LABEL_NOTNULL]] ], [ null, %[[LABEL_FAILED]] ]
return dynamic_cast<H*>(a);
}
namespace GH64088 {
// Ensure we mark the B vtable as used here, because we're going to emit a
// reference to it.
// CHECK: define {{.*}} @_ZN7GH640881BD0
struct A { virtual ~A(); };
struct B final : A { virtual ~B() = default; };
B *cast(A *p) { return dynamic_cast<B*>(p); }
}