Open zhengyang92 opened 7 months ago
I don't know how to generalize this pattern :( Any thoughts?
cc @nikic @goldsteinn
I don't know how to generalize this pattern :(
The generalization here is you can commute zext over nuw operations -- but we currently cannot preserve sub nuw x, C
due to canonicalization to add x, -C
. We would need a new poison flag for this. (This is a recurring problem, also a big issue in SCEV.)
Of course, we can work around it locally by explicitly proving no overflow, it just doesn't scale.
We would need a new poison flag for this.
Do you mean introducing "always-wrap" flags? Alive2: https://alive2.llvm.org/ce/z/6XgsFv
I don't think your proof is working correctly (you used sadd instead of uadd and it still passed?)
But yes, I believe "always wrap" is a possible, if somewhat unintuitive, way to formulate it.
(you used sadd instead of uadd and it still passed?)
Sorry, the return value should be %res
instead of %c
. So the proof is incorrect :(
@dtcxzyw Ah yes, it doesn't work if the negated value is zero: https://alive2.llvm.org/ce/z/Onv-xv Duh.
In that case something like nnuw
(negated no unsigned wrap)that specifically means that
add nnuw x, yis equivalent to
sub nuw x, -y`.
Generalized fold: https://alive2.llvm.org/ce/z/Z_nK2c
@dtcxzyw are you working on a patch for this? Otherwise I'm happy to make one.
@dtcxzyw are you working on a patch for this?
No.
We already implement the generalized fold for add. This is the best I can do regarding the actual compare version: https://alive2.llvm.org/ce/z/DbyC-d
https://alive2.llvm.org/ce/z/-mgzSg https://godbolt.org/z/xWreoG6x5
@regehr @dtcxzyw