xref: /llvm-project/llvm/test/Transforms/WholeProgramDevirt/unique-retval-same-vtable.ll (revision 0ecf7965929c4477c8b9821c68e0ad66b012d8fe)
1; Test for PR45393: Two virtual functions that return unique i1 values
2; in the same vtable. Both calls are optimized to a comparison of
3; this's vptr against the address of the vtable. When nesting these
4; checks, LLVM would previously assume the nested check always fails,
5; but that assumption does not hold if both checks refer to the same vtable.
6; This tests checks that this case is handled correctly.
7;
8; RUN: opt -S -passes='wholeprogramdevirt,default<O2>' -wholeprogramdevirt-summary-action=import \
9; RUN:   -wholeprogramdevirt-read-summary=%p/Inputs/unique-retval-same-vtable.yaml \
10; RUN:   -o - %s | FileCheck %s
11;
12; Check that C::f() contains both possible return values.
13; CHECK-LABEL: define {{.*}} @_ZNK1C1fEv
14; CHECK-NOT: }
15; CHECK: 20074028
16; CHECK-NOT: }
17; CHECK: 1008434
18
19target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
20target triple = "x86_64-unknown-linux-gnu"
21
22%class.C = type { ptr }
23
24define hidden i32 @_ZNK1C1fEv(ptr %this) {
25entry:
26  %vtable = load ptr, ptr %this
27  %0 = tail call i1 @llvm.type.test(ptr %vtable, metadata !"_ZTS1C")
28  tail call void @llvm.assume(i1 %0)
29  %vfn = getelementptr inbounds ptr, ptr %vtable, i64 2
30  %1 = load ptr, ptr %vfn
31  %call = tail call zeroext i1 %1(ptr %this)
32  br i1 %call, label %if.then, label %return
33
34if.then:
35  %vtable2 = load ptr, ptr %this
36  %2 = tail call i1 @llvm.type.test(ptr %vtable2, metadata !"_ZTS1C")
37  tail call void @llvm.assume(i1 %2)
38  %vfn3 = getelementptr inbounds ptr, ptr %vtable2, i64 3
39  %3 = load ptr, ptr %vfn3
40  ; The method being called here and the method being called before
41  ; the branch above both return true in the same vtable and only that
42  ; vtable. Therefore, if this call is reached, we must select
43  ; 20074028. Earlier versions of LLVM mistakenly concluded that
44  ; this code *never* selects 200744028.
45  %call4 = tail call zeroext i1 %3(ptr nonnull %this)
46  %. = select i1 %call4, i32 20074028, i32 3007762
47  br label %return
48
49return:
50  %retval.0 = phi i32 [ %., %if.then ], [ 1008434, %entry ]
51  ret i32 %retval.0
52}
53
54declare i1 @llvm.type.test(ptr, metadata)
55
56declare void @llvm.assume(i1)
57