Skip to content

Commit a9029a3

Browse files
committed
[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
1 parent d5f7ae7 commit a9029a3

File tree

2 files changed

+3
-1
lines changed

2 files changed

+3
-1
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7136,7 +7136,8 @@ Optional<int64_t> llvm::isPointerOffset(const Value *Ptr1, const Value *Ptr2,
71367136
// potentially variable) indices. After that they handle some constant
71377137
// offset, which determines their offset from each other. At this point, we
71387138
// handle no other case.
7139-
if (!GEP1 || !GEP2 || GEP1->getOperand(0) != GEP2->getOperand(0))
7139+
if (!GEP1 || !GEP2 || GEP1->getOperand(0) != GEP2->getOperand(0) ||
7140+
GEP1->getSourceElementType() != GEP2->getSourceElementType())
71407141
return None;
71417142

71427143
// Skip any common indices and track the GEP types.

llvm/test/Transforms/MemCpyOpt/opaque-ptr.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ define void @test_memset_memcpy(ptr %src, i64 %src_size, ptr noalias %dst, i64 %
2222
define void @test_different_gep_source_elements(ptr %src) {
2323
; CHECK-LABEL: @test_different_gep_source_elements(
2424
; CHECK-NEXT: [[PB:%.*]] = getelementptr [[B:%.*]], ptr [[SRC:%.*]], i64 0, i32 1
25+
; CHECK-NEXT: store i64 0, ptr [[PB]], align 4
2526
; CHECK-NEXT: [[PA:%.*]] = getelementptr [[A:%.*]], ptr [[SRC]], i64 0, i32 1
2627
; CHECK-NEXT: [[PA2:%.*]] = getelementptr [[A]], ptr [[SRC]], i64 0, i32 2
2728
; CHECK-NEXT: call void @llvm.memset.p0.i64(ptr align 4 [[PA]], i8 0, i64 16, i1 false)

0 commit comments

Comments
 (0)