| Issue |
241590
|
| Summary |
[GVN] Scalar PRE reuses a predecessor's value whose operands have stronger poison-generating flags
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
phongndo
|
### reproducer
alive: https://alive2.llvm.org/ce/z/y2JvyB
```llvm
define i64 @src(i1 %c, i64 %idx) {
entry:
br i1 %c, label %pA, label %pB
pA:
%ga = getelementptr inbounds i8, ptr null, i64 %idx
%ra = ptrtoint ptr %ga to i64
br label %join
pB:
%gb = getelementptr i8, ptr null, i64 %idx
br label %join
join:
%g = getelementptr i8, ptr null, i64 %idx
%r = ptrtoint ptr %g to i64
ret i64 %r
}
```
```console
$ opt -passes=gvn -S repro.ll
pA:
%ga = getelementptr inbounds i8, ptr null, i64 %idx
%ra = ptrtoint ptr %ga to i64
br label %join
pB:
%gb = getelementptr i8, ptr null, i64 %idx
%.pre = ptrtoint ptr %gb to i64
br label %join
join:
%r.pre-phi = phi i64 [ %.pre, %pB ], [ %ra, %pA ]
%g = getelementptr i8, ptr null, i64 %idx
ret i64 %r.pre-phi
ERROR: Target is more poisonous than source
i1 %c = #x1 (1)
i64 %idx = #x0000000000000001 (1)
Source value: #x0000000000000001 (1)
Target value: poison
```
On the `%pA` edge, `%r` (built from the plain GEP `%g`) is replaced by `%ra`, which is built from the `inbounds` GEP `%ga`.
### other shapes
- Both predecessors already have the value, so PRE only inserts a phi: https://alive2.llvm.org/ce/z/uhZrc3
- The reused value is an earlier `.pre-phi`, so the flags are two levels down: https://alive2.llvm.org/ce/z/cG5-mx
- Not specific to GEPs. `icmp samesign` miscompiles the same way (0 -> poison):
```llvm
define i32 @src(i1 %c, i32 %x, i32 %y) {
entry:
br i1 %c, label %pA, label %pB
pA:
%ca = icmp samesign ult i32 %x, %y
%ra = zext i1 %ca to i32
br label %join
pB:
%cb = icmp ult i32 %x, %y
br label %join
join:
%cc = icmp ult i32 %x, %y
%r = zext i1 %cc to i32
ret i32 %r
}
```
### cause
In `GVNPassImpl::performScalarPRE`, when a predecessor already has a leader `V` for the value number, the phi takes `V` and the code calls `patchReplacementInstruction(CurInst, V)`. That intersects the flags of `V` itself only. Value numbering ignores poison-generating flags, so a reused `V` can be built from instructions with stronger flags than the ones `CurInst` is built from (here `ptrtoint`/`zext` has nothing to weaken; the flags sit on its operand, or further up through a phi).
Possible directions: drop flags on the instructions `V` is built from (the chain can be arbitrarily deep), or skip PRE when a reused leader's operands are more poisonous than `CurInst`'s.
### related
#238215 (fixing #233091) handles the operand-leader case in `performScalarPREInsertion` (the inserted copy's operands). That patch does not change the output for any of the inputs above.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs