]> granicus.if.org Git - clang/commitdiff
enable TBAA when -fthread-sanitizer is given, even with -O0 or -relaxed-aliasing
authorKostya Serebryany <kcc@google.com>
Tue, 24 Apr 2012 06:57:01 +0000 (06:57 +0000)
committerKostya Serebryany <kcc@google.com>
Tue, 24 Apr 2012 06:57:01 +0000 (06:57 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@155430 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/CodeGenModule.cpp
lib/CodeGen/CodeGenTBAA.cpp
lib/CodeGen/CodeGenTBAA.h
test/CodeGen/tbaa-for-vptr.cpp

index 9a55c0848006e707eaa4072bd1293e39eaa67a38..0b1ddc15be06741dfe711be7d39e39f225e07384 100644 (file)
@@ -102,9 +102,10 @@ CodeGenModule::CodeGenModule(ASTContext &C, const CodeGenOptions &CGO,
   if (LangOpts.CUDA)
     createCUDARuntime();
 
-  // Enable TBAA unless it's suppressed.
-  if (!CodeGenOpts.RelaxedAliasing && CodeGenOpts.OptimizationLevel > 0)
-    TBAA = new CodeGenTBAA(Context, VMContext, getLangOpts(),
+  // Enable TBAA unless it's suppressed. ThreadSanitizer needs TBAA even at O0.
+  if (LangOpts.ThreadSanitizer ||
+      (!CodeGenOpts.RelaxedAliasing && CodeGenOpts.OptimizationLevel > 0))
+    TBAA = new CodeGenTBAA(Context, VMContext, CodeGenOpts, getLangOpts(),
                            ABI.getMangleContext());
 
   // If debug info or coverage generation is enabled, create the CGDebugInfo
index a3cadcf39248b293eb1f293c3e29d2e2e3450b35..e9164dc3048a76df35f9dba04ef235843a4f9c48 100644 (file)
@@ -18,6 +18,7 @@
 #include "CodeGenTBAA.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/AST/Mangle.h"
+#include "clang/Frontend/CodeGenOptions.h"
 #include "llvm/LLVMContext.h"
 #include "llvm/Metadata.h"
 #include "llvm/Constants.h"
@@ -26,8 +27,10 @@ using namespace clang;
 using namespace CodeGen;
 
 CodeGenTBAA::CodeGenTBAA(ASTContext &Ctx, llvm::LLVMContext& VMContext,
+                         const CodeGenOptions &CGO,
                          const LangOptions &Features, MangleContext &MContext)
-  : Context(Ctx), VMContext(VMContext), Features(Features), MContext(MContext),
+  : Context(Ctx), VMContext(VMContext), CodeGenOpts(CGO),
+    Features(Features), MContext(MContext),
     MDHelper(VMContext), Root(0), Char(0) {
 }
 
@@ -74,6 +77,10 @@ static bool TypeHasMayAlias(QualType QTy) {
 
 llvm::MDNode *
 CodeGenTBAA::getTBAAInfo(QualType QTy) {
+  // At -O0 TBAA is not emitted for regular types.
+  if (CodeGenOpts.OptimizationLevel == 0 || CodeGenOpts.RelaxedAliasing)
+    return NULL;
+
   // If the type has the may_alias attribute (even on a typedef), it is
   // effectively in the general char alias class.
   if (TypeHasMayAlias(QTy))
index 4a9785287d01d1e667a6d2d4b8d9d1d3903e7f89..9463b6110cf8ec8d9f8ce03fa2fd0b07485918ba 100644 (file)
@@ -26,6 +26,7 @@ namespace llvm {
 
 namespace clang {
   class ASTContext;
+  class CodeGenOptions;
   class LangOptions;
   class MangleContext;
   class QualType;
@@ -39,6 +40,7 @@ namespace CodeGen {
 class CodeGenTBAA {
   ASTContext &Context;
   llvm::LLVMContext& VMContext;
+  const CodeGenOptions &CodeGenOpts;
   const LangOptions &Features;
   MangleContext &MContext;
 
@@ -61,6 +63,7 @@ class CodeGenTBAA {
 
 public:
   CodeGenTBAA(ASTContext &Ctx, llvm::LLVMContext &VMContext,
+              const CodeGenOptions &CGO,
               const LangOptions &Features,
               MangleContext &MContext);
   ~CodeGenTBAA();
index 5ce6bf32edf3890271a4fd6ffe70660c2bb7bc7b..b9a68fe0eae19edc15fc1e9b79137facdff48e8a 100644 (file)
@@ -1,5 +1,13 @@
+// RUN: %clang_cc1 -emit-llvm -o - -O0 -fthread-sanitizer %s | FileCheck %s
 // RUN: %clang_cc1 -emit-llvm -o - -O1 %s | FileCheck %s
+// RUN: %clang_cc1 -emit-llvm -o - -O1  -relaxed-aliasing -fthread-sanitizer %s | FileCheck %s
+//
+// RUN: %clang_cc1 -emit-llvm -o - -O0 %s | FileCheck %s --check-prefix=NOTBAA
+// RUN: %clang_cc1 -emit-llvm -o - -O2  -relaxed-aliasing %s | FileCheck %s --check-prefix=NOTBAA
+//
 // Check that we generate TBAA for vtable pointer loads and stores.
+// When -fthread-sanitizer is used TBAA should be generated at all opt levels
+// even if -relaxed-aliasing is present.
 struct A {
   virtual int foo() const ;
   virtual ~A();
@@ -15,5 +23,5 @@ void CallFoo(A *a) {
 
 // CHECK: %{{.*}} = load {{.*}} !tbaa !0
 // CHECK: store {{.*}} !tbaa !0
-// CHECK: !0 = metadata !{metadata !"vtable pointer", metadata !1}
-// CHECK: !1 = metadata !{metadata !"Simple C/C++ TBAA"}
+// CHECK: = metadata !{metadata !"vtable pointer", metadata !{{.*}}}
+// NOTBAA-NOT: = metadata !{metadata !"Simple C/C++ TBAA"}