[llvm] [SeparateConstOffsetFromGEP] Preserve inbounds flag based on ValueTracking and NUW (PR #130617)
Fabian Ritter via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 17 06:14:02 PDT 2025
================
@@ -372,8 +372,8 @@ define ptr @trunk_explicit(ptr %ptr, i64 %idx) {
; CHECK-LABEL: define ptr @trunk_explicit(
; CHECK-SAME: ptr [[PTR:%.*]], i64 [[IDX:%.*]]) {
; CHECK-NEXT: entry:
-; CHECK-NEXT: [[TMP0:%.*]] = getelementptr [[STRUCT0:%.*]], ptr [[PTR]], i64 0, i32 3, i64 [[IDX]], i32 1
-; CHECK-NEXT: [[PTR21:%.*]] = getelementptr i8, ptr [[TMP0]], i64 3216
+; CHECK-NEXT: [[TMP0:%.*]] = getelementptr inbounds [[STRUCT0:%.*]], ptr [[PTR]], i64 0, i32 3, i64 [[IDX]], i32 1
+; CHECK-NEXT: [[PTR21:%.*]] = getelementptr inbounds i8, ptr [[TMP0]], i64 3216
----------------
ritter-x2a wrote:
Indeed, good catch, thanks! I think the reason for this is that `AllOffsetsNonNegative` is only updated [here](https://github.com/llvm/llvm-project/blob/7148a368cf74c0db1adabf62f8c60cccdf570cf0/llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp#L1117-L1118) for offsets that are extracted, but non-extracted offsets (like `%idx`) should also be considered.
@nikic do you happen to know if it's safe to assume that the offsets from a GEP's struct index components are non-negative, or do they also need to be checked explicitly? I haven't found anything about that in a quick search in the LangRef.
https://github.com/llvm/llvm-project/pull/130617
More information about the llvm-commits
mailing list