diff --git a/llvm/include/llvm/ADT/APInt.h b/llvm/include/llvm/ADT/APInt.h --- a/llvm/include/llvm/ADT/APInt.h +++ b/llvm/include/llvm/ADT/APInt.h @@ -430,6 +430,11 @@ return countPopulationSlowCase() == 1; } + /// Check if this APInt's negated value is a power of two greater than zero. + bool isNegatedPowerOf2() const { + return (-*this).isPowerOf2(); + } + /// Check if the APInt's value is returned by getSignMask. /// /// \returns true if this is the value returned by getSignMask. diff --git a/llvm/include/llvm/IR/PatternMatch.h b/llvm/include/llvm/IR/PatternMatch.h --- a/llvm/include/llvm/IR/PatternMatch.h +++ b/llvm/include/llvm/IR/PatternMatch.h @@ -549,7 +549,7 @@ } struct is_negated_power2 { - bool isValue(const APInt &C) { return (-C).isPowerOf2(); } + bool isValue(const APInt &C) { return C.isNegatedPowerOf2(); } }; /// Match a integer or vector negated power-of-2. /// For vectors, this includes constants with undefined elements. diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp --- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp @@ -3841,7 +3841,7 @@ } // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c - if (N1IsConst && !N1IsOpaqueConst && (-ConstValue1).isPowerOf2()) { + if (N1IsConst && !N1IsOpaqueConst && ConstValue1.isNegatedPowerOf2()) { unsigned Log2Val = (-ConstValue1).logBase2(); SDLoc DL(N); // FIXME: If the input is something that is easily negated (e.g. a @@ -4212,7 +4212,7 @@ return false; if (C->getAPIntValue().isPowerOf2()) return true; - if ((-C->getAPIntValue()).isPowerOf2()) + if (C->getAPIntValue().isNegatedPowerOf2()) return true; return false; }; diff --git a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp --- a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp @@ -4150,7 +4150,7 @@ N0.getOpcode() == ISD::AND && N0.hasOneUse()) { if (auto *AndRHS = dyn_cast(N0.getOperand(1))) { const APInt &AndRHSC = AndRHS->getAPIntValue(); - if ((-AndRHSC).isPowerOf2() && (AndRHSC & C1) == C1) { + if (AndRHSC.isNegatedPowerOf2() && (AndRHSC & C1) == C1) { unsigned ShiftBits = AndRHSC.countTrailingZeros(); if (!TLI.shouldAvoidTransformToShift(ShValTy, ShiftBits)) { SDValue Shift = diff --git a/llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp b/llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp --- a/llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp +++ b/llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp @@ -620,7 +620,7 @@ ConstantInt *Exponent = ConstantInt::get(DeltaType, IndexOffset.logBase2()); return Builder.CreateShl(ExtendedStride, Exponent); } - if ((-IndexOffset).isPowerOf2()) { + if (IndexOffset.isNegatedPowerOf2()) { // If (i - i') is a power of 2, Bump = -sext/trunc(S) << log(i' - i). ConstantInt *Exponent = ConstantInt::get(DeltaType, (-IndexOffset).logBase2()); diff --git a/llvm/unittests/ADT/APIntTest.cpp b/llvm/unittests/ADT/APIntTest.cpp --- a/llvm/unittests/ADT/APIntTest.cpp +++ b/llvm/unittests/ADT/APIntTest.cpp @@ -1785,6 +1785,27 @@ } } +TEST(APIntTest, isNegatedPowerOf2) { + EXPECT_FALSE(APInt(5, 0x00).isNegatedPowerOf2()); + EXPECT_TRUE(APInt(16, 0xfffe).isNegatedPowerOf2()); + EXPECT_TRUE(APInt(16, 0xfffc).isNegatedPowerOf2()); + EXPECT_TRUE(APInt(32, 0xffffffff).isNegatedPowerOf2()); + + for (int N : {1, 2, 3, 4, 7, 8, 16, 32, 64, 127, 128, 129, 256}) { + EXPECT_FALSE(APInt(N, 0).isNegatedPowerOf2()); + EXPECT_TRUE((-APInt::getSignedMinValue(N)).isNegatedPowerOf2()); + + APInt One(N, 1); + for (int I = 1; I < N - 1; ++I) { + EXPECT_FALSE(APInt::getOneBitSet(N, I).isNegatedPowerOf2()); + EXPECT_TRUE((-APInt::getOneBitSet(N, I)).isNegatedPowerOf2()); + + APInt MaskVal = One.shl(I); + EXPECT_TRUE((-MaskVal).isNegatedPowerOf2()); + } + } +} + // Test that self-move works with EXPENSIVE_CHECKS. It calls std::shuffle which // does self-move on some platforms. #ifdef EXPENSIVE_CHECKS