]> granicus.if.org Git - clang/commitdiff
add tbaa metadata to vtable pointer loads/stores
authorKostya Serebryany <kcc@google.com>
Mon, 26 Mar 2012 17:03:51 +0000 (17:03 +0000)
committerKostya Serebryany <kcc@google.com>
Mon, 26 Mar 2012 17:03:51 +0000 (17:03 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@153447 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/CGClass.cpp
lib/CodeGen/CodeGenModule.cpp
lib/CodeGen/CodeGenModule.h
lib/CodeGen/CodeGenTBAA.cpp
lib/CodeGen/CodeGenTBAA.h
test/CodeGen/tbaa-for-vptr.cpp [new file with mode: 0644]

index ee79c394ac0477a2f4fa896f4695c0fd3b04cb22..b452c1b7ab43c14e22531c62672d2bfb26e10cb8 100644 (file)
@@ -1514,7 +1514,8 @@ CodeGenFunction::InitializeVTablePointer(BaseSubobject Base,
   llvm::Type *AddressPointPtrTy =
     VTableAddressPoint->getType()->getPointerTo();
   VTableField = Builder.CreateBitCast(VTableField, AddressPointPtrTy);
-  Builder.CreateStore(VTableAddressPoint, VTableField);
+  llvm::StoreInst *Store = Builder.CreateStore(VTableAddressPoint, VTableField);
+  CGM.DecorateInstruction(Store, CGM.getTBAAInfoForVTablePtr());
 }
 
 void
@@ -1597,7 +1598,9 @@ void CodeGenFunction::InitializeVTablePointers(const CXXRecordDecl *RD) {
 llvm::Value *CodeGenFunction::GetVTablePtr(llvm::Value *This,
                                            llvm::Type *Ty) {
   llvm::Value *VTablePtrSrc = Builder.CreateBitCast(This, Ty->getPointerTo());
-  return Builder.CreateLoad(VTablePtrSrc, "vtable");
+  llvm::Instruction *VTable = Builder.CreateLoad(VTablePtrSrc, "vtable");
+  CGM.DecorateInstruction(VTable, CGM.getTBAAInfoForVTablePtr());
+  return VTable;
 }
 
 static const CXXRecordDecl *getMostDerivedClassDecl(const Expr *Base) {
index 1c8f5f65f93d07273382cd3648c15367e7232035..cc19d950fc8943f563c6fb6f0c55c2ecc3c4548a 100644 (file)
@@ -182,6 +182,12 @@ llvm::MDNode *CodeGenModule::getTBAAInfo(QualType QTy) {
   return TBAA->getTBAAInfo(QTy);
 }
 
+llvm::MDNode *CodeGenModule::getTBAAInfoForVTablePtr() {
+  if (!TBAA)
+    return 0;
+  return TBAA->getTBAAInfoForVTablePtr();
+}
+
 void CodeGenModule::DecorateInstruction(llvm::Instruction *Inst,
                                         llvm::MDNode *TBAAInfo) {
   Inst->setMetadata(llvm::LLVMContext::MD_tbaa, TBAAInfo);
index 8f9cdcd9a953648e8adde20ca2b0628fa43ea91c..5719afb6123aa27ccde19dfa487926bbfae67fea 100644 (file)
@@ -448,6 +448,7 @@ public:
   bool shouldUseTBAA() const { return TBAA != 0; }
 
   llvm::MDNode *getTBAAInfo(QualType QTy);
+  llvm::MDNode *getTBAAInfoForVTablePtr();
 
   bool isTypeConstant(QualType QTy, bool ExcludeCtorDtor);
 
index 148081e928efb9d1b04bd5d27aceb24a50a3589d..9ee3f1d2e67c5779d025a8037bfacbd08dd3f38d 100644 (file)
@@ -179,3 +179,7 @@ CodeGenTBAA::getTBAAInfo(QualType QTy) {
   // For now, handle any other kind of type conservatively.
   return MetadataCache[Ty] = getChar();
 }
+
+llvm::MDNode *CodeGenTBAA::getTBAAInfoForVTablePtr() {
+  return getTBAAInfoForNamedType("vtable pointer", getRoot());
+}
index 9fe51fb331414c1671abe215b9ca5b98244cab2d..8e08498b7e597b24893e869de62ccc50fee3f23c 100644 (file)
@@ -68,6 +68,10 @@ public:
   /// getTBAAInfo - Get the TBAA MDNode to be used for a dereference
   /// of the given type.
   llvm::MDNode *getTBAAInfo(QualType QTy);
+
+  /// getTBAAInfoForVTablePtr - Get the TBAA MDNode to be used for a
+  /// dereference of a vtable pointer.
+  llvm::MDNode *getTBAAInfoForVTablePtr();
 };
 
 }  // end namespace CodeGen
diff --git a/test/CodeGen/tbaa-for-vptr.cpp b/test/CodeGen/tbaa-for-vptr.cpp
new file mode 100644 (file)
index 0000000..c63c736
--- /dev/null
@@ -0,0 +1,19 @@
+// RUN: %clang_cc1 -emit-llvm -o - -O1 %s | FileCheck %s
+// Check that we generate TBAA for vtable pointer loads and stores.
+struct A {
+  virtual int foo() const ;
+  virtual ~A();
+};
+
+void CreateA() {
+  new A;
+}
+
+void CallFoo(A *a) {
+  a->foo();
+}
+
+// CHECK: %vtable = load {{.*}} !tbaa !0
+// CHECK: store {{.*}} !tbaa !0
+// CHECK: !0 = metadata !{metadata !"vtable pointer", metadata !1}
+// CHECK: !1 = metadata !{metadata !"Simple C/C++ TBAA", null}