[llvm] [DAG] isKnownToBeAPowerOfTwo - Power of 2 value is known to be power of 2 after BSWAP/BITREVERSE (PR #182207)

Manuel Dun via llvm-commits llvm-commits at lists.llvm.org
Sat Feb 21 09:57:14 PST 2026


================
@@ -914,6 +914,32 @@ TEST_F(AArch64SelectionDAGTest, KnownToBeAPowerOfTwo_Constants) {
   EXPECT_TRUE(DAG->isKnownToBeAPowerOfTwo(SplatBig, /*OrZero=*/true));
 }
 
+TEST_F(AArch64SelectionDAGTest, KnownToBeAPowerOfTwo_BSWAP_BITREVERSE) {
+
+  SDLoc Loc;
+  SDValue Cst4 = DAG->getConstant(4, Loc, MVT::i32);
+  SDValue BSwapOp = DAG->getNode(ISD::BSWAP, Loc, MVT::i32, Cst4);
----------------
manueldun wrote:

To address this I tried to use an operator that cant be folded to a constant (eg. bitcast) and use the result to a bswap, but  isKnownToBeAPowerOfTwo does not return true with and without the changes.
This is what I tried:
```
  SDLoc Loc;
  SDValue Cst4 = DAG->getConstant(4, Loc, MVT::i32);
  EVT InTypes[] = {MVT::i32,MVT::i32};
  SDVTList IntTypes{InTypes,2};
  SDValue CastOp = DAG->getNode(ISD::BITCAST,Loc,IntTypes,Cst4);
  SDValue BSwapOp = DAG->getNode(ISD::BSWAP, Loc, MVT::i32, CastOp);
  EXPECT_TRUE(DAG->isKnownToBeAPowerOfTwo(BSwapOp, /*OrZero=*/false));
```
As you can see, I cast an i32 to an i32 (a noop)
I wonder if something similar to this aproach would work.

https://github.com/llvm/llvm-project/pull/182207


More information about the llvm-commits mailing list