From 7471e95be84dbb530c04d87303bcfb49276d7afc Mon Sep 17 00:00:00 2001 From: Piotr Padlewski Date: Thu, 1 Oct 2015 03:50:41 +0000 Subject: [PATCH] Decorating virtual functions load with invariant.load http://reviews.llvm.org/D13279 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@248982 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/ItaniumCXXABI.cpp | 11 ++++++++++- test/CodeGenCXX/virtual-function-calls.cpp | 7 ++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/CodeGen/ItaniumCXXABI.cpp b/lib/CodeGen/ItaniumCXXABI.cpp index 8392f6a7ba..37b49dc608 100644 --- a/lib/CodeGen/ItaniumCXXABI.cpp +++ b/lib/CodeGen/ItaniumCXXABI.cpp @@ -1609,7 +1609,16 @@ llvm::Value *ItaniumCXXABI::getVirtualFunctionPointer(CodeGenFunction &CGF, uint64_t VTableIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(GD); llvm::Value *VFuncPtr = CGF.Builder.CreateConstInBoundsGEP1_64(VTable, VTableIndex, "vfn"); - return CGF.Builder.CreateAlignedLoad(VFuncPtr, CGF.getPointerAlign()); + auto *Inst = CGF.Builder.CreateAlignedLoad(VFuncPtr, CGF.getPointerAlign()); + + // It's safe to add "invariant.load" without -fstrict-vtable-pointers, but it + // would not help in devirtualization. + if (CGM.getCodeGenOpts().OptimizationLevel > 0 && + CGM.getCodeGenOpts().StrictVTablePointers) + Inst->setMetadata(llvm::LLVMContext::MD_invariant_load, + llvm::MDNode::get(CGM.getLLVMContext(), + llvm::ArrayRef())); + return Inst; } llvm::Value *ItaniumCXXABI::EmitVirtualDestructorCall( diff --git a/test/CodeGenCXX/virtual-function-calls.cpp b/test/CodeGenCXX/virtual-function-calls.cpp index 0a6fc6b3f2..09dfbb1c63 100644 --- a/test/CodeGenCXX/virtual-function-calls.cpp +++ b/test/CodeGenCXX/virtual-function-calls.cpp @@ -1,4 +1,5 @@ // RUN: %clang_cc1 %s -triple %itanium_abi_triple -std=c++11 -emit-llvm -o - | FileCheck %s +// RUN: %clang_cc1 %s -triple %itanium_abi_triple -std=c++11 -emit-llvm -o - -fstrict-vtable-pointers -O1 | FileCheck --check-prefix=CHECK-INVARIANT %s // PR5021 namespace PR5021 { @@ -42,10 +43,14 @@ namespace VirtualNoreturn { [[noreturn]] virtual void f(); }; - // CHECK: @_ZN15VirtualNoreturn1f + // CHECK-LABEL: @_ZN15VirtualNoreturn1f + // CHECK-INVARIANT-LABEL: define void @_ZN15VirtualNoreturn1f void f(A *p) { p->f(); // CHECK: call {{.*}}void %{{[^#]*$}} // CHECK-NOT: unreachable + // CHECK-INVARIANT: load {{.*}} align 8, !invariant.load ![[EMPTY_NODE:[0-9]]] } } + +// CHECK-INVARIANT: ![[EMPTY_NODE]] = !{} -- 2.40.0