[llvm] a9029a3 - [OpaquePtr][ValueTracking] Check GEP source element type in isPointerOffset()

Arthur Eubanks via llvm-commits llvm-commits at lists.llvm.org
Sun Feb 13 10:35:50 PST 2022


Author: Arthur Eubanks
Date: 2022-02-13T10:35:38-08:00
New Revision: a9029a33ffe5cd458b51b18f7faf5d1c844f4bb4

URL: https://github.com/llvm/llvm-project/commit/a9029a33ffe5cd458b51b18f7faf5d1c844f4bb4
DIFF: https://github.com/llvm/llvm-project/commit/a9029a33ffe5cd458b51b18f7faf5d1c844f4bb4.diff

LOG: [OpaquePtr][ValueTracking] Check GEP source element type in isPointerOffset()

Fixes a MemCpyOpt miscompile with opaque pointers.
This function can be further cleaned up, but let's just fix the miscompile first.

Reviewed By: #opaque-pointers, nikic

Differential Revision: https://reviews.llvm.org/D119652

Added: 
    

Modified: 
    llvm/lib/Analysis/ValueTracking.cpp
    llvm/test/Transforms/MemCpyOpt/opaque-ptr.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 236db5ef26f81..2fd4565de0e73 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -7136,7 +7136,8 @@ Optional<int64_t> llvm::isPointerOffset(const Value *Ptr1, const Value *Ptr2,
   // potentially variable) indices.  After that they handle some constant
   // offset, which determines their offset from each other.  At this point, we
   // handle no other case.
-  if (!GEP1 || !GEP2 || GEP1->getOperand(0) != GEP2->getOperand(0))
+  if (!GEP1 || !GEP2 || GEP1->getOperand(0) != GEP2->getOperand(0) ||
+      GEP1->getSourceElementType() != GEP2->getSourceElementType())
     return None;
 
   // Skip any common indices and track the GEP types.

diff  --git a/llvm/test/Transforms/MemCpyOpt/opaque-ptr.ll b/llvm/test/Transforms/MemCpyOpt/opaque-ptr.ll
index 235addc3df877..470775c05f4c9 100644
--- a/llvm/test/Transforms/MemCpyOpt/opaque-ptr.ll
+++ b/llvm/test/Transforms/MemCpyOpt/opaque-ptr.ll
@@ -22,6 +22,7 @@ define void @test_memset_memcpy(ptr %src, i64 %src_size, ptr noalias %dst, i64 %
 define void @test_
diff erent_gep_source_elements(ptr %src) {
 ; CHECK-LABEL: @test_
diff erent_gep_source_elements(
 ; CHECK-NEXT:    [[PB:%.*]] = getelementptr [[B:%.*]], ptr [[SRC:%.*]], i64 0, i32 1
+; CHECK-NEXT:    store i64 0, ptr [[PB]], align 4
 ; CHECK-NEXT:    [[PA:%.*]] = getelementptr [[A:%.*]], ptr [[SRC]], i64 0, i32 1
 ; CHECK-NEXT:    [[PA2:%.*]] = getelementptr [[A]], ptr [[SRC]], i64 0, i32 2
 ; CHECK-NEXT:    call void @llvm.memset.p0.i64(ptr align 4 [[PA]], i8 0, i64 16, i1 false)


        


More information about the llvm-commits mailing list