From a7b898b49abb90dd289d7c9adf5f9a6350787347 Mon Sep 17 00:00:00 2001 From: Roman Lebedev Date: Tue, 20 Dec 2022 19:55:09 +0300 Subject: [PATCH] [InstCombine] Disallow constant expressions in `not` canonicalization As per post-commit feedback - we generally do not like Constant Expressions, and trying to deal with them leads to inconsistent results that may very well be non-optimal. So just don't. --- .../Transforms/InstCombine/InstCombineAndOrXor.cpp | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp index 7843435..93bb120 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp @@ -3679,9 +3679,10 @@ bool InstCombinerImpl::sinkNotIntoLogicalOp(Instruction &I) { // And can the operands be adapted? for (Value *Op : {Op0, Op1}) if (!(InstCombiner::isFreeToInvert(Op, /*WillInvertAllUses=*/true) && - (isa(Op) || - InstCombiner::canFreelyInvertAllUsersOf(cast(Op), - /*IgnoredUser=*/&I)))) + (match(Op, m_ImmConstant()) || + (isa(Op) && + InstCombiner::canFreelyInvertAllUsersOf(cast(Op), + /*IgnoredUser=*/&I))))) return false; for (Value **Op : {&Op0, &Op1}) { @@ -3733,15 +3734,18 @@ bool InstCombinerImpl::sinkNotIntoOtherHandOfLogicalOp(Instruction &I) { Value **OpToInvert = nullptr; if (match(Op0, m_Not(m_Value(NotOp0))) && InstCombiner::isFreeToInvert(Op1, /*WillInvertAllUses=*/true) && - (isa(Op1) || InstCombiner::canFreelyInvertAllUsersOf( - cast(Op1), /*IgnoredUser=*/&I))) { + (match(Op1, m_ImmConstant()) || + (isa(Op1) && + InstCombiner::canFreelyInvertAllUsersOf(cast(Op1), + /*IgnoredUser=*/&I)))) { Op0 = NotOp0; OpToInvert = &Op1; } else if (match(Op1, m_Not(m_Value(NotOp1))) && InstCombiner::isFreeToInvert(Op0, /*WillInvertAllUses=*/true) && - (isa(Op0) || - InstCombiner::canFreelyInvertAllUsersOf(cast(Op0), - /*IgnoredUser=*/&I))) { + (match(Op0, m_ImmConstant()) || + (isa(Op0) && + InstCombiner::canFreelyInvertAllUsersOf(cast(Op0), + /*IgnoredUser=*/&I)))) { Op1 = NotOp1; OpToInvert = &Op0; } else -- 2.7.4