When pushing a sub nsw 0, %x negation into an expression, try to preserve the nsw flag for the cases where this is possible. Do this by passing the flag through recursive Negator::negate() calls.
Details
Diff Detail
- Repository
- rG LLVM Github Monorepo
Event Timeline
All of these except the -1 << X case work for NUW as well: https://alive2.llvm.org/ce/z/zzPZRQ
Maybe instead of a bool pass a bitmask of flags? Would make it easier to add NUW support in a followup.
llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp | ||
---|---|---|
261–268 | You can have NSW on this mul given that Op1 is a constant. | |
llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp | ||
241 | Nit: I would rename IsNSW to MayNSW or ParentNSW as that seems to more accurately represent the usage. |
llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp | ||
---|---|---|
306 | Why does freeze kill nsw? |
sub nuw 0, %x is only non-poison when %x is zero. For that reason, it's not really something that occurs in practice, so I don't think it's worth handling.
llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp | ||
---|---|---|
261–268 | I don't think you can add nsw on the mul if I got this right: https://alive2.llvm.org/ce/z/lSk99J Though it looks like passing nsw to the negator would be okay: https://alive2.llvm.org/ce/z/MrwT5K | |
llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp | ||
306 | Hm, looks like it's okay to keep nsw: https://alive2.llvm.org/ce/z/vnYqoi That's quite unexpected to me. |
llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp | ||
---|---|---|
261–268 | You have it right, the idea though is that since Op1 is a constant we can infer when it would be NSW i.e: |
llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp | ||
---|---|---|
261–268 | edit: Actually you can just propagate the NSW flag i.e: |
llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp | ||
---|---|---|
241 | The intention here was that IsNSW refers to whether the negation is nsw. |
You can have NSW on this mul given that Op1 is a constant.