diff --git a/llvm/lib/Transforms/IPO/Inliner.cpp b/llvm/lib/Transforms/IPO/Inliner.cpp --- a/llvm/lib/Transforms/IPO/Inliner.cpp +++ b/llvm/lib/Transforms/IPO/Inliner.cpp @@ -35,6 +35,7 @@ #include "llvm/Analysis/TargetLibraryInfo.h" #include "llvm/Analysis/TargetTransformInfo.h" #include "llvm/Transforms/Utils/Local.h" +#include "llvm/Transforms/Utils/CallPromotionUtils.h" #include "llvm/IR/Attributes.h" #include "llvm/IR/BasicBlock.h" #include "llvm/IR/CallSite.h" @@ -1096,10 +1097,20 @@ if (!IFI.InlinedCallSites.empty()) { int NewHistoryID = InlineHistory.size(); InlineHistory.push_back({&Callee, InlineHistoryID}); - for (CallSite &CS : reverse(IFI.InlinedCallSites)) - if (Function *NewCallee = CS.getCalledFunction()) + for (CallSite &CS : reverse(IFI.InlinedCallSites)) { + Function *NewCallee = CS.getCalledFunction(); + if (!NewCallee) { + // Try to promote an indirect (virtual) call without waiting for the + // post-inline cleanup and the next DevirtSCCRepeatedPass iteration + // because the next iteration may not happen and we may miss + // inlining it. + if (tryPromoteCall(CS)) + NewCallee = CS.getCalledFunction(); + } + if (NewCallee) if (!NewCallee->isDeclaration()) Calls.push_back({CS, NewHistoryID}); + } } if (InlinerFunctionImportStats != InlinerFunctionImportStatsOpts::No) diff --git a/llvm/test/Transforms/Inline/devirtualize-4.ll b/llvm/test/Transforms/Inline/devirtualize-4.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Transforms/Inline/devirtualize-4.ll @@ -0,0 +1,109 @@ +; RUN: opt < %s -passes='cgscc(devirt<4>(inline))' -S | FileCheck %s +; RUN: opt < %s -passes='default' -S | FileCheck %s + +; Check that DoNotOptimize is inlined into Test. +; CHECK: @_Z4Testv() +; CHECK-NOT: ret void +; CHECK: call void asm +; CHECK: ret void + +;template +;void DoNotOptimize(const T& var) { +; asm volatile("" : "+m"(const_cast(var))); +;} +; +;class Interface { +; public: +; virtual void Run() = 0; +;}; +; +;class Impl : public Interface { +; public: +; Impl() : f(3) {} +; void Run() { DoNotOptimize(this); } +; +; private: +; int f; +;}; +; +;static void IndirectRun(Interface& o) { o.Run(); } +; +;void Test() { +; Impl o; +; IndirectRun(o); +;} + +%class.Impl = type <{ %class.Interface, i32, [4 x i8] }> +%class.Interface = type { i32 (...)** } + +@_ZTV4Impl = linkonce_odr dso_local unnamed_addr constant { [3 x i8*] } { [3 x i8*] [i8* null, i8* bitcast ({ i8*, i8*, i8* }* @_ZTI4Impl to i8*), i8* bitcast (void (%class.Impl*)* @_ZN4Impl3RunEv to i8*)] }, align 8 +@_ZTVN10__cxxabiv120__si_class_type_infoE = external dso_local global i8* +@_ZTS4Impl = linkonce_odr dso_local constant [6 x i8] c"4Impl\00", align 1 +@_ZTVN10__cxxabiv117__class_type_infoE = external dso_local global i8* +@_ZTS9Interface = linkonce_odr dso_local constant [11 x i8] c"9Interface\00", align 1 +@_ZTI9Interface = linkonce_odr dso_local constant { i8*, i8* } { i8* bitcast (i8** getelementptr inbounds (i8*, i8** @_ZTVN10__cxxabiv117__class_type_infoE, i64 2) to i8*), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @_ZTS9Interface, i32 0, i32 0) }, align 8 +@_ZTI4Impl = linkonce_odr dso_local constant { i8*, i8*, i8* } { i8* bitcast (i8** getelementptr inbounds (i8*, i8** @_ZTVN10__cxxabiv120__si_class_type_infoE, i64 2) to i8*), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @_ZTS4Impl, i32 0, i32 0), i8* bitcast ({ i8*, i8* }* @_ZTI9Interface to i8*) }, align 8 +@_ZTV9Interface = linkonce_odr dso_local unnamed_addr constant { [3 x i8*] } { [3 x i8*] [i8* null, i8* bitcast ({ i8*, i8* }* @_ZTI9Interface to i8*), i8* bitcast (void ()* @__cxa_pure_virtual to i8*)] }, align 8 + +define dso_local void @_Z4Testv() local_unnamed_addr { +entry: + %o = alloca %class.Impl, align 8 + %0 = bitcast %class.Impl* %o to i8* + call void @llvm.lifetime.start.p0i8(i64 16, i8* nonnull %0) + call void @_ZN4ImplC2Ev(%class.Impl* nonnull %o) + %1 = getelementptr inbounds %class.Impl, %class.Impl* %o, i64 0, i32 0 + call fastcc void @_ZL11IndirectRunR9Interface(%class.Interface* nonnull dereferenceable(8) %1) + call void @llvm.lifetime.end.p0i8(i64 16, i8* nonnull %0) + ret void +} + +declare void @llvm.lifetime.start.p0i8(i64 immarg, i8* nocapture) + +define linkonce_odr dso_local void @_ZN4ImplC2Ev(%class.Impl* %this) unnamed_addr align 2 { +entry: + %0 = getelementptr %class.Impl, %class.Impl* %this, i64 0, i32 0 + call void @_ZN9InterfaceC2Ev(%class.Interface* %0) + %1 = getelementptr %class.Impl, %class.Impl* %this, i64 0, i32 0, i32 0 + store i32 (...)** bitcast (i8** getelementptr inbounds ({ [3 x i8*] }, { [3 x i8*] }* @_ZTV4Impl, i64 0, inrange i32 0, i64 2) to i32 (...)**), i32 (...)*** %1, align 8 + %f = getelementptr inbounds %class.Impl, %class.Impl* %this, i64 0, i32 1 + store i32 3, i32* %f, align 8 + ret void +} + +define internal fastcc void @_ZL11IndirectRunR9Interface(%class.Interface* dereferenceable(8) %o) unnamed_addr { +entry: + %0 = bitcast %class.Interface* %o to void (%class.Interface*)*** + %vtable = load void (%class.Interface*)**, void (%class.Interface*)*** %0, align 8 + %1 = load void (%class.Interface*)*, void (%class.Interface*)** %vtable, align 8 + call void %1(%class.Interface* nonnull %o) + ret void +} + +declare void @llvm.lifetime.end.p0i8(i64 immarg, i8* nocapture) + +define linkonce_odr dso_local void @_ZN9InterfaceC2Ev(%class.Interface* %this) unnamed_addr align 2 { +entry: + %0 = getelementptr %class.Interface, %class.Interface* %this, i64 0, i32 0 + store i32 (...)** bitcast (i8** getelementptr inbounds ({ [3 x i8*] }, { [3 x i8*] }* @_ZTV9Interface, i64 0, inrange i32 0, i64 2) to i32 (...)**), i32 (...)*** %0, align 8 + ret void +} + +define linkonce_odr dso_local void @_ZN4Impl3RunEv(%class.Impl* %this) unnamed_addr align 2 { +entry: + %ref.tmp = alloca %class.Impl*, align 8 + %0 = bitcast %class.Impl** %ref.tmp to i8* + call void @llvm.lifetime.start.p0i8(i64 8, i8* nonnull %0) + store %class.Impl* %this, %class.Impl** %ref.tmp, align 8 + call void @_Z13DoNotOptimizeIP4ImplEvRKT_(%class.Impl** nonnull dereferenceable(8) %ref.tmp) + call void @llvm.lifetime.end.p0i8(i64 8, i8* nonnull %0) + ret void +} + +declare dso_local void @__cxa_pure_virtual() unnamed_addr + +define linkonce_odr dso_local void @_Z13DoNotOptimizeIP4ImplEvRKT_(%class.Impl** dereferenceable(8) %var) local_unnamed_addr { +entry: + call void asm sideeffect "", "=*m,*m,~{dirflag},~{fpsr},~{flags}"(%class.Impl** nonnull %var, %class.Impl** nonnull %var) + ret void +} +