From ddc1b40f26b210a1650454e1ef232cf5f1b6775f Mon Sep 17 00:00:00 2001 From: Sanjay Patel Date: Mon, 1 Jul 2019 22:00:00 +0000 Subject: [InstCombine] reduce more checks for power-of-2-or-zero using ctpop Extends the transform from: rL364341 ...to include another (more common?) pattern that tests whether a value is a power-of-2 (including or excluding zero). llvm-svn: 364856 --- llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp') diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp index 664edc7c9e5..4c818193ced 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -3926,9 +3926,15 @@ Instruction *InstCombiner::foldICmpEquality(ICmpInst &I) { return new ICmpInst(Pred, A, B); // Canonicalize checking for a power-of-2-or-zero value: + // (A & (A-1)) == 0 --> ctpop(A) < 2 (two commuted variants) + // ((A-1) & A) != 0 --> ctpop(A) > 1 (two commuted variants) + if (!match(Op0, m_OneUse(m_c_And(m_Add(m_Value(A), m_AllOnes()), + m_Deferred(A)))) || + !match(Op1, m_ZeroInt())) + A = nullptr; + // (A & -A) == A --> ctpop(A) < 2 (four commuted variants) // (-A & A) != A --> ctpop(A) > 1 (four commuted variants) - A = nullptr; if (match(Op0, m_OneUse(m_c_And(m_Neg(m_Specific(Op1)), m_Specific(Op1))))) A = Op1; else if (match(Op1, -- cgit v1.2.3