Use CreateNSWSub instead of CreateSub if possible
Details
Diff Detail
Unit Tests
Event Timeline
This is correct if both subs are nsw: https://alive2.llvm.org/ce/z/r6UV8Q But not if only one of them is: https://alive2.llvm.org/ce/z/3_2BDj
Only neg is nsw also can createNSWSub
Neg by sub 0 case:
https://alive2.llvm.org/ce/z/KZLr8T
https://alive2.llvm.org/ce/z/lHJc-f
https://alive2.llvm.org/ce/z/pgH3Wd
https://alive2.llvm.org/ce/z/ZwVCtN
Neg by mul -1 case:
https://alive2.llvm.org/ce/z/BwMPtA
https://alive2.llvm.org/ce/z/Vn4pjh
https://alive2.llvm.org/ce/z/rYSowr
https://alive2.llvm.org/ce/z/fA587w
Must we do this? I was really hoping we wouldn't.
Does this actually unblock some measurable improvement?
Consider: https://alive2.llvm.org/ce/z/XYfMSm
Both subs have NSW, so i believe the code will now produce: https://alive2.llvm.org/ce/z/vUseE6
llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp | ||
---|---|---|
247 | CreateSub(I->getOperand(1), I->getOperand(0), I->getName() + ".neg", false, I->hasNoSignedWrap() && HasNSW); ? |
It comes from here https://reviews.llvm.org/D122013
Maybe we can enable it when IsTrulyNegation ? Abandon is also OK for me.
Maybe we can enable it when IsTrulyNegation ?
Oh, hm, that is also a required precondition apparently: https://alive2.llvm.org/ce/z/c9i8Go
But even then, the really big problem for NSW preservation is that every instruction inbetween that we manage to negate through affects the NSW: https://alive2.llvm.org/ce/z/4A--JC
Abandon is also OK for me.
Thanks for the detail explaination. So if we really want this work, it should be
bool NSW = I->hasNoSignedWrap() && HasNSW && IsTrulyNegation && Depth == 0; return Builder.CreateSub(I->getOperand(1), I->getOperand(0), I->getName() + ".neg", false, NSW);
or we need to trace every recursive instruction is also NSW, am I right?
Thanks again @lebedev.ri. I will update the code to
bool NSW = I->hasNoSignedWrap() && HasNSW && IsTrulyNegation && Depth == 0; return Builder.CreateSub(I->getOperand(1), I->getOperand(0), I->getName() + ".neg", false, NSW);
but if there is no other comments I prefer to abandon it.
I'm fine with abandoning the code change. I just wanted to make sure we were not miscompiling in D122013.
I would commit the tests anyway with some comments or a link back to this review (in case this comes up again in the future in some other context).
CreateSub(I->getOperand(1), I->getOperand(0), I->getName() + ".neg", false, I->hasNoSignedWrap() && HasNSW); ?