]> granicus.if.org Git - clang/commitdiff
Decorating virtual functions load with invariant.load
authorPiotr Padlewski <prazek@google.com>
Thu, 1 Oct 2015 03:50:41 +0000 (03:50 +0000)
committerPiotr Padlewski <prazek@google.com>
Thu, 1 Oct 2015 03:50:41 +0000 (03:50 +0000)
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
test/CodeGenCXX/virtual-function-calls.cpp

index 8392f6a7ba2e1cd709ab606653d1080e1181d94a..37b49dc6086e290941f1e4478e34da97030f3f60 100644 (file)
@@ -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<llvm::Metadata *>()));
+  return Inst;
 }
 
 llvm::Value *ItaniumCXXABI::EmitVirtualDestructorCall(
index 0a6fc6b3f284f844955795d018b55e3097dc7530..09dfbb1c63122c46432896024c6f0c9e408cfac5 100644 (file)
@@ -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]] = !{}