llvm-project/llvm/test/Transforms/InstSimplify/ConstProp/inttoptr-gep-index-width.ll
Nikita Popov 8758e5fe47
[ConstantFolding] Fix handling of index width != pointer width (#130608)
Per LangRef:

> The offsets are then added to the low bits of the base address up to
the index type width, with silently-wrapping two’s complement
arithmetic. If the pointer size is larger than the index size, this
means that the bits outside the index type width will not be affected.

The transform as implemented was doubly wrong, because it just truncated
the original base pointer to the index width, losing the top bits
entirely. Make sure we preserve the bits and use wrapping arithmetic
within the low bits.
2025-03-11 09:02:34 +01:00

15 lines
452 B
LLVM

; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
; RUN: opt -S -passes=instsimplify < %s | FileCheck %s
target datalayout = "p:16:16:16:8"
; The GEP should only modify the low 8 bits of the pointer.
define ptr @test() {
; CHECK-LABEL: define ptr @test() {
; CHECK-NEXT: ret ptr inttoptr (i16 -256 to ptr)
;
%base = inttoptr i16 -1 to ptr
%gep = getelementptr i8, ptr %base, i8 1
ret ptr %gep
}