xref: /llvm-project/llvm/test/Transforms/Inline/devirtualize-4.ll (revision 0f46e31cfbf415fcd3d3ce121bef94e92c6ccfc8)
1; RUN: opt < %s -passes='cgscc(devirt<4>(inline)),function(sroa,early-cse)' -S | FileCheck %s
2; RUN: opt < %s -passes='default<O3>' -S | FileCheck %s
3
4; Check that DoNotOptimize is inlined into Test.
5; CHECK: @_Z4Testv()
6; CHECK-NOT: ret void
7; CHECK: call void asm
8; CHECK: ret void
9
10;template <class T>
11;void DoNotOptimize(const T& var) {
12;  asm volatile("" : "+m"(const_cast<T&>(var)));
13;}
14;
15;class Interface {
16; public:
17;  virtual void Run() = 0;
18;};
19;
20;class Impl : public Interface {
21; public:
22;  Impl() : f(3) {}
23;  void Run() { DoNotOptimize(this); }
24;
25; private:
26;  int f;
27;};
28;
29;static void IndirectRun(Interface& o) { o.Run(); }
30;
31;void Test() {
32;  Impl o;
33;  IndirectRun(o);
34;}
35
36%class.Impl = type <{ %class.Interface, i32, [4 x i8] }>
37%class.Interface = type { ptr }
38
39@_ZTV4Impl = linkonce_odr dso_local unnamed_addr constant { [3 x ptr] } { [3 x ptr] [ptr null, ptr @_ZTI4Impl, ptr @_ZN4Impl3RunEv] }, align 8
40@_ZTVN10__cxxabiv120__si_class_type_infoE = external dso_local global ptr
41@_ZTS4Impl = linkonce_odr dso_local constant [6 x i8] c"4Impl\00", align 1
42@_ZTVN10__cxxabiv117__class_type_infoE = external dso_local global ptr
43@_ZTS9Interface = linkonce_odr dso_local constant [11 x i8] c"9Interface\00", align 1
44@_ZTI9Interface = linkonce_odr dso_local constant { ptr, ptr } { ptr getelementptr inbounds (ptr, ptr @_ZTVN10__cxxabiv117__class_type_infoE, i64 2), ptr @_ZTS9Interface }, align 8
45@_ZTI4Impl = linkonce_odr dso_local constant { ptr, ptr, ptr } { ptr getelementptr inbounds (ptr, ptr @_ZTVN10__cxxabiv120__si_class_type_infoE, i64 2), ptr @_ZTS4Impl, ptr @_ZTI9Interface }, align 8
46@_ZTV9Interface = linkonce_odr dso_local unnamed_addr constant { [3 x ptr] } { [3 x ptr] [ptr null, ptr @_ZTI9Interface, ptr @__cxa_pure_virtual] }, align 8
47
48define dso_local void @_Z4Testv() local_unnamed_addr {
49entry:
50  %o = alloca %class.Impl, align 8
51  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %o)
52  call void @_ZN4ImplC2Ev(ptr nonnull %o)
53  call fastcc void @_ZL11IndirectRunR9Interface(ptr nonnull dereferenceable(8) %o)
54  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %o)
55  ret void
56}
57
58declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture)
59
60define linkonce_odr dso_local void @_ZN4ImplC2Ev(ptr %this) unnamed_addr align 2 {
61entry:
62  call void @_ZN9InterfaceC2Ev(ptr %this)
63  store ptr getelementptr inbounds inrange(-16, 8) ({ [3 x ptr] }, ptr @_ZTV4Impl, i64 0, i32 0, i64 2), ptr %this, align 8
64  %f = getelementptr inbounds %class.Impl, ptr %this, i64 0, i32 1
65  store i32 3, ptr %f, align 8
66  ret void
67}
68
69define internal fastcc void @_ZL11IndirectRunR9Interface(ptr dereferenceable(8) %o) unnamed_addr {
70entry:
71  %vtable = load ptr, ptr %o, align 8
72  %0 = load ptr, ptr %vtable, align 8
73  call void %0(ptr nonnull %o)
74  ret void
75}
76
77declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture)
78
79define linkonce_odr dso_local void @_ZN9InterfaceC2Ev(ptr %this) unnamed_addr align 2 {
80entry:
81  store ptr getelementptr inbounds inrange(-16, 8) ({ [3 x ptr] }, ptr @_ZTV9Interface, i64 0, i32 0, i64 2), ptr %this, align 8
82  ret void
83}
84
85define linkonce_odr dso_local void @_ZN4Impl3RunEv(ptr %this) unnamed_addr align 2 {
86entry:
87  %ref.tmp = alloca ptr, align 8
88  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %ref.tmp)
89  store ptr %this, ptr %ref.tmp, align 8
90  call void @_Z13DoNotOptimizeIP4ImplEvRKT_(ptr nonnull dereferenceable(8) %ref.tmp)
91  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %ref.tmp)
92  ret void
93}
94
95declare dso_local void @__cxa_pure_virtual() unnamed_addr
96
97define linkonce_odr dso_local void @_Z13DoNotOptimizeIP4ImplEvRKT_(ptr dereferenceable(8) %var) local_unnamed_addr {
98entry:
99  call void asm sideeffect "", "=*m,*m,~{dirflag},~{fpsr},~{flags}"(ptr elementtype(ptr) nonnull %var, ptr elementtype(ptr) nonnull %var)
100  ret void
101}
102
103
104; Based on clang/test/CodeGenCXX/member-function-pointer-calls.cpp.
105; Check that vf1 and vf2 are inlined into g1 and g2.
106; CHECK: @_Z2g1v()
107; CHECK-NOT: }
108; CHECK: ret i32 1
109; CHECK: @_Z2g2v()
110; CHECK-NOT: }
111; CHECK: ret i32 2
112;
113;struct A {
114;  virtual int vf1() { return 1; }
115;  virtual int vf2() { return 2; }
116;};
117;
118;int f(A* a, int (A::*fp)()) {
119;  return (a->*fp)();
120;}
121;int g1() {
122;  A a;
123;  return f(&a, &A::vf1);
124;}
125;int g2() {
126;  A a;
127;  return f(&a, &A::vf2);
128;}
129
130%struct.A = type { ptr }
131
132@_ZTV1A = linkonce_odr unnamed_addr constant { [4 x ptr] } { [4 x ptr] [ptr null, ptr @_ZTI1A, ptr @_ZN1A3vf1Ev, ptr @_ZN1A3vf2Ev] }, align 8
133@_ZTS1A = linkonce_odr constant [3 x i8] c"1A\00", align 1
134@_ZTI1A = linkonce_odr constant { ptr, ptr } { ptr getelementptr inbounds (ptr, ptr @_ZTVN10__cxxabiv117__class_type_infoE, i64 2), ptr @_ZTS1A }, align 8
135
136define i32 @_Z1fP1AMS_FivE(ptr %a, i64 %fp.coerce0, i64 %fp.coerce1) {
137entry:
138  %0 = getelementptr inbounds i8, ptr %a, i64 %fp.coerce1
139  %1 = and i64 %fp.coerce0, 1
140  %memptr.isvirtual = icmp eq i64 %1, 0
141  br i1 %memptr.isvirtual, label %memptr.nonvirtual, label %memptr.virtual
142
143memptr.virtual:                                   ; preds = %entry
144  %vtable = load ptr, ptr %0, align 8
145  %2 = add i64 %fp.coerce0, -1
146  %3 = getelementptr i8, ptr %vtable, i64 %2
147  %memptr.virtualfn = load ptr, ptr %3, align 8
148  br label %memptr.end
149
150memptr.nonvirtual:                                ; preds = %entry
151  %memptr.nonvirtualfn = inttoptr i64 %fp.coerce0 to ptr
152  br label %memptr.end
153
154memptr.end:                                       ; preds = %memptr.nonvirtual, %memptr.virtual
155  %4 = phi ptr [ %memptr.virtualfn, %memptr.virtual ], [ %memptr.nonvirtualfn, %memptr.nonvirtual ]
156  %call = call i32 %4(ptr %0)
157  ret i32 %call
158}
159
160define i32 @_Z2g1v() {
161entry:
162  %a = alloca %struct.A, align 8
163  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %a)
164  call void @_ZN1AC1Ev(ptr nonnull %a)
165  %call = call i32 @_Z1fP1AMS_FivE(ptr nonnull %a, i64 1, i64 0)
166  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %a)
167  ret i32 %call
168}
169
170define linkonce_odr void @_ZN1AC1Ev(ptr %this) align 2 {
171entry:
172  call void @_ZN1AC2Ev(ptr %this)
173  ret void
174}
175
176define i32 @_Z2g2v() {
177entry:
178  %a = alloca %struct.A, align 8
179  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %a)
180  call void @_ZN1AC1Ev(ptr nonnull %a)
181  %call = call i32 @_Z1fP1AMS_FivE(ptr nonnull %a, i64 9, i64 0)
182  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %a)
183  ret i32 %call
184}
185
186define linkonce_odr void @_ZN1AC2Ev(ptr %this) align 2 {
187entry:
188  store ptr getelementptr inbounds inrange(-16, 8) ({ [4 x ptr] }, ptr @_ZTV1A, i64 0, i32 0, i64 2), ptr %this, align 8
189  ret void
190}
191
192define linkonce_odr i32 @_ZN1A3vf1Ev(ptr %this) align 2 {
193entry:
194  ret i32 1
195}
196
197define linkonce_odr i32 @_ZN1A3vf2Ev(ptr %this) align 2 {
198entry:
199  ret i32 2
200}
201