201 lines · plain
1; RUN: opt < %s -passes='cgscc(devirt<4>(inline)),function(sroa,early-cse)' -S | FileCheck %s2; RUN: opt < %s -passes='default<O3>' -S | FileCheck %s3 4; Check that DoNotOptimize is inlined into Test.5; CHECK: @_Z4Testv()6; CHECK-NOT: ret void7; CHECK: call void asm8; CHECK: ret void9 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 840@_ZTVN10__cxxabiv120__si_class_type_infoE = external dso_local global ptr41@_ZTS4Impl = linkonce_odr dso_local constant [6 x i8] c"4Impl\00", align 142@_ZTVN10__cxxabiv117__class_type_infoE = external dso_local global ptr43@_ZTS9Interface = linkonce_odr dso_local constant [11 x i8] c"9Interface\00", align 144@_ZTI9Interface = linkonce_odr dso_local constant { ptr, ptr } { ptr getelementptr inbounds (ptr, ptr @_ZTVN10__cxxabiv117__class_type_infoE, i64 2), ptr @_ZTS9Interface }, align 845@_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 846@_ZTV9Interface = linkonce_odr dso_local unnamed_addr constant { [3 x ptr] } { [3 x ptr] [ptr null, ptr @_ZTI9Interface, ptr @__cxa_pure_virtual] }, align 847 48define dso_local void @_Z4Testv() local_unnamed_addr {49entry:50 %o = alloca %class.Impl, align 851 call void @llvm.lifetime.start.p0(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(ptr nonnull %o)55 ret void56}57 58declare void @llvm.lifetime.start.p0(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 864 %f = getelementptr inbounds %class.Impl, ptr %this, i64 0, i32 165 store i32 3, ptr %f, align 866 ret void67}68 69define internal fastcc void @_ZL11IndirectRunR9Interface(ptr dereferenceable(8) %o) unnamed_addr {70entry:71 %vtable = load ptr, ptr %o, align 872 %0 = load ptr, ptr %vtable, align 873 call void %0(ptr nonnull %o)74 ret void75}76 77declare void @llvm.lifetime.end.p0(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 882 ret void83}84 85define linkonce_odr dso_local void @_ZN4Impl3RunEv(ptr %this) unnamed_addr align 2 {86entry:87 %ref.tmp = alloca ptr, align 888 call void @llvm.lifetime.start.p0(ptr nonnull %ref.tmp)89 store ptr %this, ptr %ref.tmp, align 890 call void @_Z13DoNotOptimizeIP4ImplEvRKT_(ptr nonnull dereferenceable(8) %ref.tmp)91 call void @llvm.lifetime.end.p0(ptr nonnull %ref.tmp)92 ret void93}94 95declare dso_local void @__cxa_pure_virtual() unnamed_addr96 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 void101}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 1109; CHECK: @_Z2g2v()110; CHECK-NOT: }111; CHECK: ret i32 2112;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 8133@_ZTS1A = linkonce_odr constant [3 x i8] c"1A\00", align 1134@_ZTI1A = linkonce_odr constant { ptr, ptr } { ptr getelementptr inbounds (ptr, ptr @_ZTVN10__cxxabiv117__class_type_infoE, i64 2), ptr @_ZTS1A }, align 8135 136define i32 @_Z1fP1AMS_FivE(ptr %a, i64 %fp.coerce0, i64 %fp.coerce1) {137entry:138 %0 = getelementptr inbounds i8, ptr %a, i64 %fp.coerce1139 %1 = and i64 %fp.coerce0, 1140 %memptr.isvirtual = icmp eq i64 %1, 0141 br i1 %memptr.isvirtual, label %memptr.nonvirtual, label %memptr.virtual142 143memptr.virtual: ; preds = %entry144 %vtable = load ptr, ptr %0, align 8145 %2 = add i64 %fp.coerce0, -1146 %3 = getelementptr i8, ptr %vtable, i64 %2147 %memptr.virtualfn = load ptr, ptr %3, align 8148 br label %memptr.end149 150memptr.nonvirtual: ; preds = %entry151 %memptr.nonvirtualfn = inttoptr i64 %fp.coerce0 to ptr152 br label %memptr.end153 154memptr.end: ; preds = %memptr.nonvirtual, %memptr.virtual155 %4 = phi ptr [ %memptr.virtualfn, %memptr.virtual ], [ %memptr.nonvirtualfn, %memptr.nonvirtual ]156 %call = call i32 %4(ptr %0)157 ret i32 %call158}159 160define i32 @_Z2g1v() {161entry:162 %a = alloca %struct.A, align 8163 call void @llvm.lifetime.start.p0(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(ptr nonnull %a)167 ret i32 %call168}169 170define linkonce_odr void @_ZN1AC1Ev(ptr %this) align 2 {171entry:172 call void @_ZN1AC2Ev(ptr %this)173 ret void174}175 176define i32 @_Z2g2v() {177entry:178 %a = alloca %struct.A, align 8179 call void @llvm.lifetime.start.p0(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(ptr nonnull %a)183 ret i32 %call184}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 8189 ret void190}191 192define linkonce_odr i32 @_ZN1A3vf1Ev(ptr %this) align 2 {193entry:194 ret i32 1195}196 197define linkonce_odr i32 @_ZN1A3vf2Ev(ptr %this) align 2 {198entry:199 ret i32 2200}201