From 44e875ad5b2ce26826dd53f9e7d1a71436c86212 Mon Sep 17 00:00:00 2001 From: Kuba Brecka Date: Mon, 7 Nov 2016 19:09:56 +0000 Subject: [tsan] Cast floating-point types correctly when instrumenting atomic accesses, LLVM part Although rare, atomic accesses to floating-point types seem to be valid, i.e. `%a = load atomic float ...`. The TSan instrumentation pass however tries to emit inttoptr, which is incorrect, we should use a bitcast here. Anyway, IRBuilder already has a convenient helper function for this. Differential Revision: https://reviews.llvm.org/D26266 llvm-svn: 286135 --- .../Transforms/Instrumentation/ThreadSanitizer.cpp | 23 ++++++---------------- 1 file changed, 6 insertions(+), 17 deletions(-) (limited to 'llvm/lib/Transforms') diff --git a/llvm/lib/Transforms/Instrumentation/ThreadSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/ThreadSanitizer.cpp index 41d0b53672d..355f69c9aae 100644 --- a/llvm/lib/Transforms/Instrumentation/ThreadSanitizer.cpp +++ b/llvm/lib/Transforms/Instrumentation/ThreadSanitizer.cpp @@ -549,11 +549,6 @@ bool ThreadSanitizer::instrumentMemIntrinsic(Instruction *I) { return false; } -static Value *createIntOrPtrToIntCast(Value *V, Type* Ty, IRBuilder<> &IRB) { - return isa(V->getType()) ? - IRB.CreatePtrToInt(V, Ty) : IRB.CreateIntCast(V, Ty, false); -} - // Both llvm and ThreadSanitizer atomic operations are based on C++11/C1x // standards. For background see C++11 standard. A slightly older, publicly // available draft of the standard (not entirely up-to-date, but close enough @@ -576,15 +571,9 @@ bool ThreadSanitizer::instrumentAtomic(Instruction *I, const DataLayout &DL) { Value *Args[] = {IRB.CreatePointerCast(Addr, PtrTy), createOrdering(&IRB, LI->getOrdering())}; Type *OrigTy = cast(Addr->getType())->getElementType(); - if (Ty == OrigTy) { - Instruction *C = CallInst::Create(TsanAtomicLoad[Idx], Args); - ReplaceInstWithInst(I, C); - } else { - // We are loading a pointer, so we need to cast the return value. - Value *C = IRB.CreateCall(TsanAtomicLoad[Idx], Args); - Instruction *Cast = CastInst::Create(Instruction::IntToPtr, C, OrigTy); - ReplaceInstWithInst(I, Cast); - } + Value *C = IRB.CreateCall(TsanAtomicLoad[Idx], Args); + Value *Cast = IRB.CreateBitOrPointerCast(C, OrigTy); + I->replaceAllUsesWith(Cast); } else if (StoreInst *SI = dyn_cast(I)) { Value *Addr = SI->getPointerOperand(); int Idx = getMemoryAccessFuncIndex(Addr, DL); @@ -595,7 +584,7 @@ bool ThreadSanitizer::instrumentAtomic(Instruction *I, const DataLayout &DL) { Type *Ty = Type::getIntNTy(IRB.getContext(), BitSize); Type *PtrTy = Ty->getPointerTo(); Value *Args[] = {IRB.CreatePointerCast(Addr, PtrTy), - createIntOrPtrToIntCast(SI->getValueOperand(), Ty, IRB), + IRB.CreateBitOrPointerCast(SI->getValueOperand(), Ty), createOrdering(&IRB, SI->getOrdering())}; CallInst *C = CallInst::Create(TsanAtomicStore[Idx], Args); ReplaceInstWithInst(I, C); @@ -626,9 +615,9 @@ bool ThreadSanitizer::instrumentAtomic(Instruction *I, const DataLayout &DL) { Type *Ty = Type::getIntNTy(IRB.getContext(), BitSize); Type *PtrTy = Ty->getPointerTo(); Value *CmpOperand = - createIntOrPtrToIntCast(CASI->getCompareOperand(), Ty, IRB); + IRB.CreateBitOrPointerCast(CASI->getCompareOperand(), Ty); Value *NewOperand = - createIntOrPtrToIntCast(CASI->getNewValOperand(), Ty, IRB); + IRB.CreateBitOrPointerCast(CASI->getNewValOperand(), Ty); Value *Args[] = {IRB.CreatePointerCast(Addr, PtrTy), CmpOperand, NewOperand, -- cgit v1.2.3