[all-commits] [llvm/llvm-project] 03ab85: [InstCombine] fold `gepi _, (srem x, y)` to `gepi ...
Kiva via All-commits
all-commits at lists.llvm.org
Mon Feb 9 06:26:30 PST 2026
Branch: refs/heads/main
Home: https://github.com/llvm/llvm-project
Commit: 03ab85cb873eee64da5a4a34c44955682bdc7e34
https://github.com/llvm/llvm-project/commit/03ab85cb873eee64da5a4a34c44955682bdc7e34
Author: Kiva <imkiva at islovely.icu>
Date: 2026-02-09 (Mon, 09 Feb 2026)
Changed paths:
M llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
A llvm/test/Transforms/InstCombine/gep-srem-to-and-deref.ll
Log Message:
-----------
[InstCombine] fold `gepi _, (srem x, y)` to `gepi _, (urem x, y)` if `y` is power-of-2 (#180148)
This PR adds a small, targeted InstCombine fold for the pattern:
```
%idx = srem i64 %x, 2^k
%p = getelementptr inbounds nuw i8, ptr %base, i64 %idx
```
When the GEP is inbounds + nuw, and the divisor is a non-zero
power-of-two constant, the signed remainder cannot produce a negative
offset without violating the inbounds/nuw constraints. In that case we
can canonicalize the index to a non-negative form and expose the common
power-of-two rewrite:
- Rewrite the GEP index from `srem %x, 2^k` to `urem %x, 2^k`
- Create a new GEP with the new index and replace the original GEP
- the `urem %x, 2^k` will further folds to `and %x (2^k-1)`
resulting the following pattern
```
%idx = and i64 %x, (2^k-1)
%p = getelementptr inbounds nuw i8, ptr %base, i64 %idx
```
Fixes #180097.
generalized alive2 proof: https://alive2.llvm.org/ce/z/8EBxug
To unsubscribe from these emails, change your notification settings at https://github.com/llvm/llvm-project/settings/notifications
More information about the All-commits
mailing list