Skip to content

[InstCombine] Miscompilation in foldGEPICmp #157714

@dtcxzyw

Description

@dtcxzyw

Reproducer: https://alive2.llvm.org/ce/z/SLybDz

define i1 @gep_mugtiple_ugt_inbounds_nusw(ptr %base, i64 %idx, i64 %idx2) {
  %gep1 = getelementptr inbounds i8, ptr %base, i64 %idx
  %gep2 = getelementptr nusw i8, ptr %gep1, i64 %idx2
  %cmp = icmp ugt ptr %gep2, %base
  ret i1 %cmp
}
=>
define i1 @gep_mugtiple_ugt_inbounds_nusw(ptr %base, i64 %idx, i64 %idx2) {
#0:
  %#1 = add i64 %idx, %idx2
  %cmp = icmp sgt i64 %#1, 0
  ret i1 %cmp
}
Transformation doesn't verify!

ERROR: Value mismatch

Example:
ptr %base = pointer(non-local, block_id=1, offset=0) / Address=#x000000000000000c
i64 %idx = #x7ffffffffffffffc (9223372036854775804)
i64 %idx2 = #x0000000000000005 (5)

Source:
ptr %gep1 = pointer(non-local, block_id=1, offset=9223372036854775804) / Address=#x8000000000000008
ptr %gep2 = pointer(non-local, block_id=1, offset=-9223372036854775807) / Address=#x800000000000000d
i1 %cmp = #x1 (1)

SOURCE MEMORY STATE
===================
NON-LOCAL BLOCKS:
Block 0 >	size: 0	align: 1	alloc type: 0	alive: false	address: #x0000000000000000
Block 1 >	size: 9223372036854775804	align: 1	alloc type: 0	alive: true	address: #x000000000000000c

Target:
i64 %#1 = #x8000000000000001 (9223372036854775809, -9223372036854775807)
i1 %cmp = #x0 (0)
Source value: #x1 (1)
Target value: #x0 (0)

cc @nikic

Metadata

Metadata

Assignees

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions