llvm-project/llvm/test/Transforms/GlobalDCE/virtual-functions-derived-call.ll
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

78 lines
2.3 KiB
LLVM

; RUN: opt < %s -passes=globaldce -S | FileCheck %s
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
; struct A {
; A();
; virtual int foo();
; };
;
; struct B : A {
; B();
; virtual int foo();
; };
;
; A::A() {}
; B::B() {}
; int A::foo() { return 42; }
; int B::foo() { return 1337; }
;
; extern "C" int test(B *p) { return p->foo(); }
; The virtual call in test can only be dispatched to B::foo (or a more-derived
; class, if there was one), so A::foo can be removed.
%struct.A = type { ptr }
%struct.B = type { %struct.A }
; CHECK: @_ZTV1A = internal unnamed_addr constant { [3 x ptr] } zeroinitializer
@_ZTV1A = internal unnamed_addr constant { [3 x ptr] } { [3 x ptr] [ptr null, ptr null, ptr @_ZN1A3fooEv] }, align 8, !type !0, !type !1, !vcall_visibility !2
; CHECK: @_ZTV1B = internal unnamed_addr constant { [3 x ptr] } { [3 x ptr] [ptr null, ptr null, ptr @_ZN1B3fooEv] }
@_ZTV1B = internal unnamed_addr constant { [3 x ptr] } { [3 x ptr] [ptr null, ptr null, ptr @_ZN1B3fooEv] }, align 8, !type !0, !type !1, !type !3, !type !4, !vcall_visibility !2
; CHECK-NOT: define internal i32 @_ZN1A3fooEv(
define internal i32 @_ZN1A3fooEv(ptr nocapture readnone %this) {
entry:
ret i32 42
}
; CHECK: define internal i32 @_ZN1B3fooEv(
define internal i32 @_ZN1B3fooEv(ptr nocapture readnone %this) {
entry:
ret i32 1337
}
define hidden void @_ZN1AC2Ev(ptr nocapture %this) {
entry:
store ptr getelementptr inbounds inrange(-16, 8) ({ [3 x ptr] }, ptr @_ZTV1A, i64 0, i32 0, i64 2), ptr %this, align 8
ret void
}
define hidden void @_ZN1BC2Ev(ptr nocapture %this) {
entry:
store ptr getelementptr inbounds inrange(-16, 8) ({ [3 x ptr] }, ptr @_ZTV1B, i64 0, i32 0, i64 2), ptr %this, align 8
ret void
}
define hidden i32 @test(ptr %p) {
entry:
%vtable1 = load ptr, ptr %p, align 8
%0 = tail call { ptr, i1 } @llvm.type.checked.load(ptr %vtable1, i32 0, metadata !"_ZTS1B"), !nosanitize !10
%1 = extractvalue { ptr, i1 } %0, 0, !nosanitize !10
%call = tail call i32 %1(ptr %p)
ret i32 %call
}
declare { ptr, i1 } @llvm.type.checked.load(ptr, i32, metadata) #2
!llvm.module.flags = !{!5}
!0 = !{i64 16, !"_ZTS1A"}
!1 = !{i64 16, !"_ZTSM1AFivE.virtual"}
!2 = !{i64 2}
!3 = !{i64 16, !"_ZTS1B"}
!4 = !{i64 16, !"_ZTSM1BFivE.virtual"}
!5 = !{i32 1, !"Virtual Function Elim", i32 1}
!10 = !{}