[GISelValueTracking] Add test case for G_PTRTOINT

While we can only reason about the index/address, the G_PTRTOINT
operations returns all representation bits, so we can't assume the
remaining ones are all zeroes. This behaviour was clarified as part of
the discussion in https://discourse.llvm.org/t/clarifiying-the-semantics-of-ptrtoint/83987/54.
The LangRef semantics of ptrtoint being a full representation bitcast
were documented in https://github.com/llvm/llvm-project/pull/139349.

Prior to 77c8d214131e951e3d3a07b45a7436f54988d6f3 we were incorrectly
assuming known zeroes beyond the index size even if the input was
completely unknown. This commit adds a test case for G_PTRTOINT which
was omitted from that change.

See https://github.com/llvm/llvm-project/issues/139598

Reviewed By: arsenm

Pull Request: https://github.com/llvm/llvm-project/pull/139608
This commit is contained in:
Alexander Richardson 2025-06-11 10:47:17 -07:00 committed by GitHub
parent 66f533e7e3
commit c2f0af514b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -0,0 +1,110 @@
# NOTE: Assertions have been autogenerated by utils/update_givaluetracking_test_checks.py UTC_ARGS: --version 5
# RUN: llc -mtriple=amdgcn -passes="print<gisel-value-tracking>" %s -filetype=null 2>&1 | FileCheck %s
## Check that we don't incorrectly assume known zeroes for and extend of a truncated ptrtoint
## Test case for https://github.com/llvm/llvm-project/issues/139598
---
## We should see 128 unknown bits.
name: PtrToInt
body: |
bb.0:
; CHECK-LABEL: name: @PtrToInt
; CHECK-NEXT: %0:_ KnownBits:???????????????????????????????? SignBits:1
; CHECK-NEXT: %1:_ KnownBits:???????????????????????????????? SignBits:1
; CHECK-NEXT: %2:_ KnownBits:???????????????????????????????? SignBits:1
; CHECK-NEXT: %3:_ KnownBits:???????????????????????????????? SignBits:1
; CHECK-NEXT: %4:_ KnownBits:???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? SignBits:1
; CHECK-NEXT: %5:_ KnownBits:???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? SignBits:1
%0:_(s32) = COPY $vgpr0
%1:_(s32) = COPY $vgpr1
%2:_(s32) = COPY $vgpr2
%3:_(s32) = COPY $vgpr3
%4:_(p8) = G_MERGE_VALUES %0(s32), %1(s32), %2(s32), %3(s32)
%5:_(s128) = G_PTRTOINT %4(p8)
...
---
## We should see 128 high zeroes followed by 128 unknown bits for extending ptrtoint.
name: PtrToIntExt
body: |
bb.0:
; CHECK-LABEL: name: @PtrToIntExt
; CHECK-NEXT: %0:_ KnownBits:???????????????????????????????? SignBits:1
; CHECK-NEXT: %1:_ KnownBits:???????????????????????????????? SignBits:1
; CHECK-NEXT: %2:_ KnownBits:???????????????????????????????? SignBits:1
; CHECK-NEXT: %3:_ KnownBits:???????????????????????????????? SignBits:1
; CHECK-NEXT: %4:_ KnownBits:???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? SignBits:1
; CHECK-NEXT: %5:_ KnownBits:00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? SignBits:128
%0:_(s32) = COPY $vgpr0
%1:_(s32) = COPY $vgpr1
%2:_(s32) = COPY $vgpr2
%3:_(s32) = COPY $vgpr3
%4:_(p8) = G_MERGE_VALUES %0(s32), %1(s32), %2(s32), %3(s32)
%5:_(s256) = G_PTRTOINT %4(p8)
...
---
## We should see 48 unknown bits for truncating ptrtoint.
name: PtrToIntTrunc
body: |
bb.0:
; CHECK-LABEL: name: @PtrToIntTrunc
; CHECK-NEXT: %0:_ KnownBits:???????????????????????????????? SignBits:1
; CHECK-NEXT: %1:_ KnownBits:???????????????????????????????? SignBits:1
; CHECK-NEXT: %2:_ KnownBits:???????????????????????????????? SignBits:1
; CHECK-NEXT: %3:_ KnownBits:???????????????????????????????? SignBits:1
; CHECK-NEXT: %4:_ KnownBits:???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? SignBits:1
; CHECK-NEXT: %5:_ KnownBits:???????????????????????????????????????????????? SignBits:1
%0:_(s32) = COPY $vgpr0
%1:_(s32) = COPY $vgpr1
%2:_(s32) = COPY $vgpr2
%3:_(s32) = COPY $vgpr3
%4:_(p8) = G_MERGE_VALUES %0(s32), %1(s32), %2(s32), %3(s32)
%5:_(s48) = G_PTRTOINT %4(p8)
...
---
## This is the test for issue 139598: Truncating and then extending the
## G_PTRTOINT result was filling all bits above the index bitwidth with known
## zeroes even though the incoming value is completely unknown and G_PTRTOINT.
## is lowered to a bitwise copy.
## We should see all zero high bits with 48 unknown bits.
name: PtrToIntTruncExplicitExt
body: |
bb.0:
; CHECK-LABEL: name: @PtrToIntTruncExplicitExt
; CHECK-NEXT: %0:_ KnownBits:???????????????????????????????? SignBits:1
; CHECK-NEXT: %1:_ KnownBits:???????????????????????????????? SignBits:1
; CHECK-NEXT: %2:_ KnownBits:???????????????????????????????? SignBits:1
; CHECK-NEXT: %3:_ KnownBits:???????????????????????????????? SignBits:1
; CHECK-NEXT: %4:_ KnownBits:???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? SignBits:1
; CHECK-NEXT: %5:_ KnownBits:???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? SignBits:1
; CHECK-NEXT: %6:_ KnownBits:???????????????????????????????????????????????? SignBits:1
; CHECK-NEXT: %7:_ KnownBits:0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000???????????????????????????????????????????????? SignBits:208
%0:_(s32) = COPY $vgpr0
%1:_(s32) = COPY $vgpr1
%2:_(s32) = COPY $vgpr2
%3:_(s32) = COPY $vgpr3
%4:_(p8) = G_MERGE_VALUES %0(s32), %1(s32), %2(s32), %3(s32)
%5:_(s128) = G_PTRTOINT %4(p8)
%6:_(s48) = G_TRUNC %5(s128)
%7:_(s256) = G_ZEXT %6(s48)
...
---
## Same test again but this time have the G_PTRTOINT do the truncation.
## We should see all zero high bits with 48 unknown bits.
name: PtrToIntTruncImplicitExt
body: |
bb.0:
; CHECK-LABEL: name: @PtrToIntTruncImplicitExt
; CHECK-NEXT: %0:_ KnownBits:???????????????????????????????? SignBits:1
; CHECK-NEXT: %1:_ KnownBits:???????????????????????????????? SignBits:1
; CHECK-NEXT: %2:_ KnownBits:???????????????????????????????? SignBits:1
; CHECK-NEXT: %3:_ KnownBits:???????????????????????????????? SignBits:1
; CHECK-NEXT: %4:_ KnownBits:???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? SignBits:1
; CHECK-NEXT: %5:_ KnownBits:???????????????????????????????????????????????? SignBits:1
; CHECK-NEXT: %6:_ KnownBits:0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000???????????????????????????????????????????????? SignBits:208
%0:_(s32) = COPY $vgpr0
%1:_(s32) = COPY $vgpr1
%2:_(s32) = COPY $vgpr2
%3:_(s32) = COPY $vgpr3
%4:_(p8) = G_MERGE_VALUES %0(s32), %1(s32), %2(s32), %3(s32)
%5:_(s48) = G_PTRTOINT %4(p8)
%6:_(s256) = G_ZEXT %5(s48)
...