llvm / llvm/llvm-project

gep(nonnull, -1) can not wrap

Open
#211,663 3 comments 0 reactions 0 assignees View on GitHub
llvm:instcombine missed-optimization
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

Missing transformation:

```llvm
define ptr @src(ptr nonnull %p) {
%subone = getelementptr i8, ptr %p, i64 -1
ret ptr %subone
}

define ptr @tgt(ptr nonnull %p) {
%subone = getelementptr nusw i8, ptr %p, i64 -1
ret ptr %subone
}
```

Alive2: https://alive2.llvm.org/ce/z/EtFUxx
This currently doesn't happen: https://godbolt.org/z/vK1noa7ar

This information can help eliminate redundant bounds checks.

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the missing GEP transformation from the issue's LLVM IR with the Alive2 example and the linked Godbolt case. Trace the compiler's handling of a nonnull pointer and a -1 GEP; done means the source form can be transformed to the shown nusw form without invalidating the pointer guarantees.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.