From 5d7d2f0d2e7b137e68337b0b472130b61d9f305e Mon Sep 17 00:00:00 2001 From: Sanjay Patel Date: Mon, 23 Aug 2021 14:51:58 -0400 Subject: [PATCH] [InstCombine] improve efficiency of isFreeToInvert This is NFC-intended when viewed from outside the pass. I was trying to make sure that we don't infinite loop in subtract combines and noticed that we handle the non-canonical forms of add/sub here, but it should not be necessary. Coding it this way seems slightly clearer than mixing all 4 patterns as before. --- llvm/include/llvm/Transforms/InstCombine/InstCombiner.h | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/llvm/include/llvm/Transforms/InstCombine/InstCombiner.h b/llvm/include/llvm/Transforms/InstCombine/InstCombiner.h index 3f13df7..f763612 100644 --- a/llvm/include/llvm/Transforms/InstCombine/InstCombiner.h +++ b/llvm/include/llvm/Transforms/InstCombine/InstCombiner.h @@ -246,12 +246,13 @@ public: // If `V` is of the form `A + Constant` then `-1 - V` can be folded into // `(-1 - Constant) - A` if we are willing to invert all of the uses. - if (BinaryOperator *BO = dyn_cast(V)) - if (BO->getOpcode() == Instruction::Add || - BO->getOpcode() == Instruction::Sub) - if (match(BO, PatternMatch::m_c_BinOp(PatternMatch::m_Value(), - PatternMatch::m_ImmConstant()))) - return WillInvertAllUses; + if (match(V, m_Add(PatternMatch::m_Value(), PatternMatch::m_ImmConstant()))) + return WillInvertAllUses; + + // If `V` is of the form `Constant - A` then `-1 - V` can be folded into + // `A + (-1 - Constant)` if we are willing to invert all of the uses. + if (match(V, m_Sub(PatternMatch::m_ImmConstant(), PatternMatch::m_Value()))) + return WillInvertAllUses; // Selects with invertible operands are freely invertible if (match(V, -- 2.7.4