From 2460c3fc733a618c65a032065f9e45a2232b2e3e Mon Sep 17 00:00:00 2001
From: Kostya Serebryany <kcc@google.com>
Date: Thu, 5 Dec 2013 15:03:02 +0000
Subject: [PATCH] [tsan] fix PR18146: sometimes a variable written into vptr
 could have an integer type (after other optimizations)

llvm-svn: 196507
---
 .../lib/Transforms/Instrumentation/ThreadSanitizer.cpp |  4 +++-
 .../Instrumentation/ThreadSanitizer/vptr_update.ll     | 10 ++++++++++
 2 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/Transforms/Instrumentation/ThreadSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/ThreadSanitizer.cpp
index baec1534e00e..5c1881782008 100644
--- a/llvm/lib/Transforms/Instrumentation/ThreadSanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/ThreadSanitizer.cpp
@@ -408,10 +408,12 @@ bool ThreadSanitizer::instrumentLoadOrStore(Instruction *I) {
     if (isa<VectorType>(StoredValue->getType()))
       StoredValue = IRB.CreateExtractElement(
           StoredValue, ConstantInt::get(IRB.getInt32Ty(), 0));
+    if (StoredValue->getType()->isIntegerTy())
+      StoredValue = IRB.CreateIntToPtr(StoredValue, IRB.getInt8PtrTy());
     // Call TsanVptrUpdate.
     IRB.CreateCall2(TsanVptrUpdate,
                     IRB.CreatePointerCast(Addr, IRB.getInt8PtrTy()),
-                    IRB.CreateBitCast(StoredValue, IRB.getInt8PtrTy()));
+                    IRB.CreatePointerCast(StoredValue, IRB.getInt8PtrTy()));
     NumInstrumentedVtableWrites++;
     return true;
   }
diff --git a/llvm/test/Instrumentation/ThreadSanitizer/vptr_update.ll b/llvm/test/Instrumentation/ThreadSanitizer/vptr_update.ll
index 46650040540d..83d28b6ee217 100644
--- a/llvm/test/Instrumentation/ThreadSanitizer/vptr_update.ll
+++ b/llvm/test/Instrumentation/ThreadSanitizer/vptr_update.ll
@@ -11,6 +11,16 @@ entry:
   ret void
 }
 
+define void @FooInt(i64* nocapture %a, i64 %b) nounwind uwtable sanitize_thread {
+entry:
+; CHECK-LABEL: @FooInt
+; CHECK: call void @__tsan_vptr_update
+; CHECK: ret void
+  store i64 %b, i64* %a, align 8, !tbaa !0
+  ret void
+}
+
+
 declare i32 @Func1()
 declare i32 @Func2()