[llvm] [InstCombine] Improve handling of `not` and free inversion. (PR #66787)
via llvm-commits
llvm-commits at lists.llvm.org
Sat Nov 18 09:06:26 PST 2023
================
@@ -2198,12 +2198,14 @@ Instruction *InstCombinerImpl::visitSub(BinaryOperator &I) {
// (~X) - (~Y) --> Y - X
// This is placed after the other reassociations and explicitly excludes a
// sub-of-sub pattern to avoid infinite looping.
- if (isFreeToInvert(Op0, Op0->hasOneUse()) &&
- isFreeToInvert(Op1, Op1->hasOneUse()) &&
- !match(Op0, m_Sub(m_ImmConstant(), m_Value()))) {
- Value *NotOp0 = Builder.CreateNot(Op0);
- Value *NotOp1 = Builder.CreateNot(Op1);
- return BinaryOperator::CreateSub(NotOp1, NotOp0);
+ if (!match(Op0, m_Sub(m_ImmConstant(), m_Value())) &&
+ isFreeToInvert(Op0, Op0->hasOneUse())) {
+ if (Value *NotOp1 = getFreelyInverted(Op1, Op1->hasOneUse(), &Builder)) {
+ Value *NotOp0 = getFreelyInverted(Op0, Op0->hasOneUse(), &Builder);
+ assert(NotOp0 != nullptr &&
+ "isFreeToInvert desynced with getFreelyInverted");
+ return BinaryOperator::CreateSub(NotOp1, NotOp0);
+ }
----------------
goldsteinn wrote:
Adding tests for all binops we support with lhs/rhs pattern that is always freely invertible.
https://github.com/llvm/llvm-project/pull/66787
More information about the llvm-commits
mailing list