[llvm] [GlobalOpt] Look through non-PointerType constant expressions. (PR #125205)
Florian Hahn via llvm-commits
llvm-commits at lists.llvm.org
Fri Jan 31 06:58:42 PST 2025
fhahn wrote:
> I don't think this is safe. We generally cannot analyze past a ptrtoint cast due to its provenance exposure side effect.
>
With the patch, we only look through it to determine if the base object may be read or written. Are there any provenance issues that may lead as to miss reads/writes?
> Why does your change result in this ptrtoint+add+inttoptr arithmetic being introduced in the first place?
It is not removed/folded in the same way, because we simplify to a compare earlier. Here is roughly what's going on
We start out with
```
bb:
%.pre.i.i.i = ptrtoint ptr %17 to i64
%.pre56.i.i.i = sub i64 ptrtoint (ptr getelementptr inbounds nuw (i8, ptr @g, i64 12560) to i64), %.pre.i.i.i
%20 = sdiv exact i64 %.pre56.i.i.i, 40
switch i64 %20, label %34 [
i64 3, label %21
i64 2, label %26
i64 1, label %31
]
```
With the patch, SimplifyCFG converts this to icmps
```
bb:
%.pre.i.i.i = ptrtoint ptr %17 to i64
%.pre56.i.i.i = sub i64 ptrtoint (ptr getelementptr inbounds nuw (i8, ptr @g, i64 12560) to i64), %.pre.i.i.i
%20 = sdiv exact i64 %.pre56.i.i.i, 40
%cond = icmp eq i64 %20, 2
br i1 %cond, label %21, label %28
```
Then instcombine folds this to
```
%cond = icmp eq ptr %17, inttoptr (i64 add (i64 ptrtoint (ptr getelementptr inbounds nuw (i8, ptr @_ZN5clang7targetsL7AVRMcusE, i64 12560) to i64), i64 -80) to ptr)
```
Without the patch, the switch will only get simplified later
```
.bb:
%.pre.i.i.i.i = ptrtoint ptr %.ptr34 to i64
%.pre56.i.i.i.i = sub i64 ptrtoint (ptr getelementptr inbounds nuw (i8, ptr @g, i64 12560) to i64), %.pre.i.i.i.i
%17 = sdiv exact i64 %.pre56.i.i.i.i, 40
%cond = icmp eq i64 %17, 2
br i1 %cond, label %18, label …
```
And later instcombine removes the `ptrtoint`:
```
%cond = icmp eq i64 %.02950.i.i.i.i.idx, 12320
```
https://github.com/llvm/llvm-project/pull/125205
More information about the llvm-commits
mailing list