[llvm] [DAG] Improved handling of ISD::ROTL and ISD::ROTR in isKnownToBeAPowerOfTwo (PR #182744)

Mir Immad via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 2 01:55:30 PST 2026


https://github.com/mirimmad updated https://github.com/llvm/llvm-project/pull/182744

>From 15e6b6014648ab68faa0a7cfd46a406bd98c1e0a Mon Sep 17 00:00:00 2001
From: Immad Mir <mirimmad17 at gmail.com>
Date: Sun, 22 Feb 2026 19:11:03 +0530
Subject: [PATCH 1/6] use 'OrZero' in 'isKnownToBeAPowerOfTwo'

---
 llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
index 084700c50bd06..9f3bd9f15d1d7 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
@@ -4753,8 +4753,7 @@ bool SelectionDAG::isKnownToBeAPowerOfTwo(SDValue Val,
 
   case ISD::ROTL:
   case ISD::ROTR:
-    return isKnownToBeAPowerOfTwo(Val.getOperand(0), /*OrZero=*/false,
-                                  Depth + 1);
+    return isKnownToBeAPowerOfTwo(Val.getOperand(0), false, Depth + 1);
 
   case ISD::SMIN:
   case ISD::SMAX:

>From 1bd6086c369f613b1c79551e365c41ef5dc101fd Mon Sep 17 00:00:00 2001
From: Immad Mir <mirimmad17 at gmail.com>
Date: Sun, 22 Feb 2026 19:26:42 +0530
Subject: [PATCH 2/6] Add a test for ISD::ROTL and ISD::ROTR

---
 .../AArch64/AArch64SelectionDAGTest.cpp       | 49 +++++++++++++++++++
 1 file changed, 49 insertions(+)

diff --git a/llvm/unittests/Target/AArch64/AArch64SelectionDAGTest.cpp b/llvm/unittests/Target/AArch64/AArch64SelectionDAGTest.cpp
index b0c48e8c97995..36f266a8bdff3 100644
--- a/llvm/unittests/Target/AArch64/AArch64SelectionDAGTest.cpp
+++ b/llvm/unittests/Target/AArch64/AArch64SelectionDAGTest.cpp
@@ -1172,6 +1172,55 @@ TEST_F(AArch64SelectionDAGTest,
   EXPECT_EQ(SplatIdx, 0);
 }
 
+TEST_F(AArch64SelectionDAGTest, KnownToBeAPowerOfTwo_ROTL) {
+  SDLoc Loc;
+  auto IntVT = EVT::getIntegerVT(Context, 32);
+
+  // Power-of-two values
+  auto Pow2_4 = DAG->getConstant(4, Loc, IntVT);
+  auto Pow2_8 = DAG->getConstant(8, Loc, IntVT);
+
+  // Non-power-of-two values
+  auto NonPow2_5 = DAG->getConstant(5, Loc, IntVT);
+  auto NonPow2_0 = DAG->getConstant(0, Loc, IntVT);
+
+  auto RotAmount = DAG->getConstant(3, Loc, IntVT);
+
+  auto RotlPow2_4 = DAG->getNode(ISD::ROTL, Loc, IntVT, Pow2_4, RotAmount);
+  auto RotlPow2_8 = DAG->getNode(ISD::ROTL, Loc, IntVT, Pow2_8, RotAmount);
+  auto RotlNonPow2_5 =
+      DAG->getNode(ISD::ROTL, Loc, IntVT, NonPow2_5, RotAmount);
+  auto RotlZero = DAG->getNode(ISD::ROTL, Loc, IntVT, NonPow2_0, RotAmount);
+
+  EXPECT_TRUE(DAG->isKnownToBeAPowerOfTwo(RotlPow2_4));
+  EXPECT_TRUE(DAG->isKnownToBeAPowerOfTwo(RotlPow2_8));
+
+  EXPECT_FALSE(DAG->isKnownToBeAPowerOfTwo(RotlNonPow2_5));
+
+  EXPECT_FALSE(DAG->isKnownToBeAPowerOfTwo(RotlZero));
+  EXPECT_TRUE(DAG->isKnownToBeAPowerOfTwo(RotlZero, /*OrZero=*/true));
+}
+
+TEST_F(AArch64SelectionDAGTest, KnownToBeAPowerOfTwo_ROTR) {
+  SDLoc Loc;
+  auto IntVT = EVT::getIntegerVT(Context, 32);
+
+  auto Pow2_16 = DAG->getConstant(16, Loc, IntVT);
+  auto NonPow2_6 = DAG->getConstant(6, Loc, IntVT);
+  auto Zero = DAG->getConstant(0, Loc, IntVT);
+
+  auto RotAmount = DAG->getConstant(5, Loc, IntVT);
+
+  auto RotrPow2 = DAG->getNode(ISD::ROTR, Loc, IntVT, Pow2_16, RotAmount);
+  auto RotrNonPow2 = DAG->getNode(ISD::ROTR, Loc, IntVT, NonPow2_6, RotAmount);
+  auto RotrZero = DAG->getNode(ISD::ROTR, Loc, IntVT, Zero, RotAmount);
+
+  EXPECT_TRUE(DAG->isKnownToBeAPowerOfTwo(RotrPow2));
+  EXPECT_FALSE(DAG->isKnownToBeAPowerOfTwo(RotrNonPow2));
+  EXPECT_FALSE(DAG->isKnownToBeAPowerOfTwo(RotrZero));
+  EXPECT_TRUE(DAG->isKnownToBeAPowerOfTwo(RotrZero, /*OrZero=*/true));
+}
+
 TEST_F(AArch64SelectionDAGTest, getRepeatedSequence_Patterns) {
   TargetLowering TL(*TM, *STI);
 

>From f105d54a20e673420cb9bccc23f44b74929fec59 Mon Sep 17 00:00:00 2001
From: Immad Mir <mirimmad17 at gmail.com>
Date: Sun, 22 Feb 2026 23:07:34 +0530
Subject: [PATCH 3/6] Add missing 'DemandedElts' and 'OrZero' arguments to
 'isKnownToBeAPowerOfTwo' and associated tests

---
 .../lib/CodeGen/SelectionDAG/SelectionDAG.cpp |  2 +-
 .../AArch64/AArch64SelectionDAGTest.cpp       | 21 +++++++++++++++++++
 2 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
index 9f3bd9f15d1d7..de2ee2ee59622 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
@@ -4753,7 +4753,7 @@ bool SelectionDAG::isKnownToBeAPowerOfTwo(SDValue Val,
 
   case ISD::ROTL:
   case ISD::ROTR:
-    return isKnownToBeAPowerOfTwo(Val.getOperand(0), false, Depth + 1);
+    return isKnownToBeAPowerOfTwo(Val.getOperand(0), DemandedElts, OrZero, Depth + 1);
 
   case ISD::SMIN:
   case ISD::SMAX:
diff --git a/llvm/unittests/Target/AArch64/AArch64SelectionDAGTest.cpp b/llvm/unittests/Target/AArch64/AArch64SelectionDAGTest.cpp
index 36f266a8bdff3..e5f61576f6ffd 100644
--- a/llvm/unittests/Target/AArch64/AArch64SelectionDAGTest.cpp
+++ b/llvm/unittests/Target/AArch64/AArch64SelectionDAGTest.cpp
@@ -1199,6 +1199,17 @@ TEST_F(AArch64SelectionDAGTest, KnownToBeAPowerOfTwo_ROTL) {
 
   EXPECT_FALSE(DAG->isKnownToBeAPowerOfTwo(RotlZero));
   EXPECT_TRUE(DAG->isKnownToBeAPowerOfTwo(RotlZero, /*OrZero=*/true));
+
+    // Also verify DemandedElts is forwarded through ROTL for vector lanes.
+    auto VecVT = EVT::getVectorVT(Context, IntVT, 2, /*IsScalable=*/false);
+    auto PowAndZero = DAG->getBuildVector(VecVT, Loc, {Pow2_4, NonPow2_0});
+    auto RotAmountVec = DAG->getBuildVector(VecVT, Loc, {RotAmount, RotAmount});
+    auto RotlPowAndZero =
+      DAG->getNode(ISD::ROTL, Loc, VecVT, PowAndZero, RotAmountVec);
+    APInt DemandAll(2, 3);
+    EXPECT_FALSE(DAG->isKnownToBeAPowerOfTwo(RotlPowAndZero, DemandAll));
+    EXPECT_TRUE(
+      DAG->isKnownToBeAPowerOfTwo(RotlPowAndZero, DemandAll, /*OrZero=*/true));
 }
 
 TEST_F(AArch64SelectionDAGTest, KnownToBeAPowerOfTwo_ROTR) {
@@ -1219,6 +1230,16 @@ TEST_F(AArch64SelectionDAGTest, KnownToBeAPowerOfTwo_ROTR) {
   EXPECT_FALSE(DAG->isKnownToBeAPowerOfTwo(RotrNonPow2));
   EXPECT_FALSE(DAG->isKnownToBeAPowerOfTwo(RotrZero));
   EXPECT_TRUE(DAG->isKnownToBeAPowerOfTwo(RotrZero, /*OrZero=*/true));
+
+    auto VecVT = EVT::getVectorVT(Context, IntVT, 2, /*IsScalable=*/false);
+    auto PowAndZero = DAG->getBuildVector(VecVT, Loc, {Pow2_16, Zero});
+    auto RotAmountVec = DAG->getBuildVector(VecVT, Loc, {RotAmount, RotAmount});
+    auto RotrPowAndZero =
+      DAG->getNode(ISD::ROTR, Loc, VecVT, PowAndZero, RotAmountVec);
+    APInt DemandAll(2, 3);
+    EXPECT_FALSE(DAG->isKnownToBeAPowerOfTwo(RotrPowAndZero, DemandAll));
+    EXPECT_TRUE(
+      DAG->isKnownToBeAPowerOfTwo(RotrPowAndZero, DemandAll, /*OrZero=*/true));
 }
 
 TEST_F(AArch64SelectionDAGTest, getRepeatedSequence_Patterns) {

>From 092f13453ff7c8bafc9d723d16335140e6e67b84 Mon Sep 17 00:00:00 2001
From: Immad Mir <mirimmad17 at gmail.com>
Date: Tue, 24 Feb 2026 16:48:59 +0530
Subject: [PATCH 4/6] Remove 'auto' and add a test for vector case

---
 .../lib/CodeGen/SelectionDAG/SelectionDAG.cpp |  3 +-
 llvm/test/CodeGen/X86/known-pow2.ll           | 31 ++++++++
 .../AArch64/AArch64SelectionDAGTest.cpp       | 73 ++++++++++---------
 3 files changed, 71 insertions(+), 36 deletions(-)

diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
index de2ee2ee59622..bb2a8accc4f24 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
@@ -4753,7 +4753,8 @@ bool SelectionDAG::isKnownToBeAPowerOfTwo(SDValue Val,
 
   case ISD::ROTL:
   case ISD::ROTR:
-    return isKnownToBeAPowerOfTwo(Val.getOperand(0), DemandedElts, OrZero, Depth + 1);
+    return isKnownToBeAPowerOfTwo(Val.getOperand(0), DemandedElts, OrZero,
+                                  Depth + 1);
 
   case ISD::SMIN:
   case ISD::SMAX:
diff --git a/llvm/test/CodeGen/X86/known-pow2.ll b/llvm/test/CodeGen/X86/known-pow2.ll
index 2457f3344592c..d5c6ac773d496 100644
--- a/llvm/test/CodeGen/X86/known-pow2.ll
+++ b/llvm/test/CodeGen/X86/known-pow2.ll
@@ -11,6 +11,8 @@ declare i32 @llvm.smin.i32(i32, i32)
 declare i32 @llvm.smax.i32(i32, i32)
 declare i32 @llvm.fshl.i32(i32, i32, i32)
 declare i32 @llvm.fshr.i32(i32, i32, i32)
+declare <2 x i32> @llvm.fshl.v2i32(<2 x i32>, <2 x i32>, <2 x i32>)
+declare <2 x i32> @llvm.fshr.v2i32(<2 x i32>, <2 x i32>, <2 x i32>)
 
 define <4 x i32> @pow2_non_splat_vec(<4 x i32> %x) {
 ; CHECK-LABEL: pow2_non_splat_vec:
@@ -1006,3 +1008,32 @@ define i32 @pow2_blsi_sub(i32 %x, i32 %a) {
   %r = and i32 %x_sub_y, %y
   ret i32 %r
 }
+
+define <2 x i32> @pow2_rotl_vec() {
+; CHECK-LABEL: pow2_rotl_vec:
+; CHECK:       # %bb.0:
+; CHECK:       xmm0 = [32,0,0,0]
+; CHECK:       retq
+entry:
+  ; build vector <4,0>
+  %v0 = insertelement <2 x i32> undef, i32 4, i32 0
+  %v1 = insertelement <2 x i32> %v0, i32 0, i32 1
+  %amt0 = insertelement <2 x i32> undef, i32 3, i32 0
+  %amt1 = insertelement <2 x i32> %amt0, i32 3, i32 1
+  %r = call <2 x i32> @llvm.fshl.v2i32(<2 x i32> %v1, <2 x i32> %v1, <2 x i32> %amt1)
+  ret <2 x i32> %r
+}
+
+define <2 x i32> @pow2_rotr_vec() {
+; CHECK-LABEL: pow2_rotr_vec:
+; CHECK:       # %bb.0:
+; CHECK:       xmm0 = [2147483648,0,0,0]
+; CHECK:       retq
+entry:
+  %v0 = insertelement <2 x i32> undef, i32 16, i32 0
+  %v1 = insertelement <2 x i32> %v0, i32 0, i32 1
+  %amt0 = insertelement <2 x i32> undef, i32 5, i32 0
+  %amt1 = insertelement <2 x i32> %amt0, i32 5, i32 1
+  %r = call <2 x i32> @llvm.fshr.v2i32(<2 x i32> %v1, <2 x i32> %v1, <2 x i32> %amt1)
+  ret <2 x i32> %r
+}
diff --git a/llvm/unittests/Target/AArch64/AArch64SelectionDAGTest.cpp b/llvm/unittests/Target/AArch64/AArch64SelectionDAGTest.cpp
index e5f61576f6ffd..d599cf14adc07 100644
--- a/llvm/unittests/Target/AArch64/AArch64SelectionDAGTest.cpp
+++ b/llvm/unittests/Target/AArch64/AArch64SelectionDAGTest.cpp
@@ -1174,23 +1174,24 @@ TEST_F(AArch64SelectionDAGTest,
 
 TEST_F(AArch64SelectionDAGTest, KnownToBeAPowerOfTwo_ROTL) {
   SDLoc Loc;
-  auto IntVT = EVT::getIntegerVT(Context, 32);
-
   // Power-of-two values
-  auto Pow2_4 = DAG->getConstant(4, Loc, IntVT);
-  auto Pow2_8 = DAG->getConstant(8, Loc, IntVT);
+  SDValue Pow2_4 = DAG->getConstant(4, Loc, MVT::i32);
+  SDValue Pow2_8 = DAG->getConstant(8, Loc, MVT::i32);
 
   // Non-power-of-two values
-  auto NonPow2_5 = DAG->getConstant(5, Loc, IntVT);
-  auto NonPow2_0 = DAG->getConstant(0, Loc, IntVT);
+  SDValue NonPow2_5 = DAG->getConstant(5, Loc, MVT::i32);
+  SDValue NonPow2_0 = DAG->getConstant(0, Loc, MVT::i32);
 
-  auto RotAmount = DAG->getConstant(3, Loc, IntVT);
+  SDValue RotAmount = DAG->getConstant(3, Loc, MVT::i32);
 
-  auto RotlPow2_4 = DAG->getNode(ISD::ROTL, Loc, IntVT, Pow2_4, RotAmount);
-  auto RotlPow2_8 = DAG->getNode(ISD::ROTL, Loc, IntVT, Pow2_8, RotAmount);
-  auto RotlNonPow2_5 =
-      DAG->getNode(ISD::ROTL, Loc, IntVT, NonPow2_5, RotAmount);
-  auto RotlZero = DAG->getNode(ISD::ROTL, Loc, IntVT, NonPow2_0, RotAmount);
+  SDValue RotlPow2_4 =
+      DAG->getNode(ISD::ROTL, Loc, MVT::i32, Pow2_4, RotAmount);
+  SDValue RotlPow2_8 =
+      DAG->getNode(ISD::ROTL, Loc, MVT::i32, Pow2_8, RotAmount);
+  SDValue RotlNonPow2_5 =
+      DAG->getNode(ISD::ROTL, Loc, MVT::i32, NonPow2_5, RotAmount);
+  SDValue RotlZero =
+      DAG->getNode(ISD::ROTL, Loc, MVT::i32, NonPow2_0, RotAmount);
 
   EXPECT_TRUE(DAG->isKnownToBeAPowerOfTwo(RotlPow2_4));
   EXPECT_TRUE(DAG->isKnownToBeAPowerOfTwo(RotlPow2_8));
@@ -1200,45 +1201,47 @@ TEST_F(AArch64SelectionDAGTest, KnownToBeAPowerOfTwo_ROTL) {
   EXPECT_FALSE(DAG->isKnownToBeAPowerOfTwo(RotlZero));
   EXPECT_TRUE(DAG->isKnownToBeAPowerOfTwo(RotlZero, /*OrZero=*/true));
 
-    // Also verify DemandedElts is forwarded through ROTL for vector lanes.
-    auto VecVT = EVT::getVectorVT(Context, IntVT, 2, /*IsScalable=*/false);
-    auto PowAndZero = DAG->getBuildVector(VecVT, Loc, {Pow2_4, NonPow2_0});
-    auto RotAmountVec = DAG->getBuildVector(VecVT, Loc, {RotAmount, RotAmount});
-    auto RotlPowAndZero =
+  // Also verify DemandedElts is forwarded through ROTL for vector lanes.
+  MVT::SimpleValueType VecVT = MVT::v2i32;
+  SDValue PowAndZero = DAG->getBuildVector(VecVT, Loc, {Pow2_4, NonPow2_0});
+  SDValue RotAmountVec =
+      DAG->getBuildVector(VecVT, Loc, {RotAmount, RotAmount});
+  SDValue RotlPowAndZero =
       DAG->getNode(ISD::ROTL, Loc, VecVT, PowAndZero, RotAmountVec);
-    APInt DemandAll(2, 3);
-    EXPECT_FALSE(DAG->isKnownToBeAPowerOfTwo(RotlPowAndZero, DemandAll));
-    EXPECT_TRUE(
+  APInt DemandAll(2, 3);
+  EXPECT_FALSE(DAG->isKnownToBeAPowerOfTwo(RotlPowAndZero, DemandAll));
+  EXPECT_TRUE(
       DAG->isKnownToBeAPowerOfTwo(RotlPowAndZero, DemandAll, /*OrZero=*/true));
 }
 
 TEST_F(AArch64SelectionDAGTest, KnownToBeAPowerOfTwo_ROTR) {
   SDLoc Loc;
-  auto IntVT = EVT::getIntegerVT(Context, 32);
 
-  auto Pow2_16 = DAG->getConstant(16, Loc, IntVT);
-  auto NonPow2_6 = DAG->getConstant(6, Loc, IntVT);
-  auto Zero = DAG->getConstant(0, Loc, IntVT);
+  SDValue Pow2_16 = DAG->getConstant(16, Loc, MVT::i32);
+  SDValue NonPow2_6 = DAG->getConstant(6, Loc, MVT::i32);
+  SDValue Zero = DAG->getConstant(0, Loc, MVT::i32);
 
-  auto RotAmount = DAG->getConstant(5, Loc, IntVT);
+  SDValue RotAmount = DAG->getConstant(5, Loc, MVT::i32);
 
-  auto RotrPow2 = DAG->getNode(ISD::ROTR, Loc, IntVT, Pow2_16, RotAmount);
-  auto RotrNonPow2 = DAG->getNode(ISD::ROTR, Loc, IntVT, NonPow2_6, RotAmount);
-  auto RotrZero = DAG->getNode(ISD::ROTR, Loc, IntVT, Zero, RotAmount);
+  SDValue RotrPow2 = DAG->getNode(ISD::ROTR, Loc, MVT::i32, Pow2_16, RotAmount);
+  SDValue RotrNonPow2 =
+      DAG->getNode(ISD::ROTR, Loc, MVT::i32, NonPow2_6, RotAmount);
+  SDValue RotrZero = DAG->getNode(ISD::ROTR, Loc, MVT::i32, Zero, RotAmount);
 
   EXPECT_TRUE(DAG->isKnownToBeAPowerOfTwo(RotrPow2));
   EXPECT_FALSE(DAG->isKnownToBeAPowerOfTwo(RotrNonPow2));
   EXPECT_FALSE(DAG->isKnownToBeAPowerOfTwo(RotrZero));
   EXPECT_TRUE(DAG->isKnownToBeAPowerOfTwo(RotrZero, /*OrZero=*/true));
 
-    auto VecVT = EVT::getVectorVT(Context, IntVT, 2, /*IsScalable=*/false);
-    auto PowAndZero = DAG->getBuildVector(VecVT, Loc, {Pow2_16, Zero});
-    auto RotAmountVec = DAG->getBuildVector(VecVT, Loc, {RotAmount, RotAmount});
-    auto RotrPowAndZero =
+  MVT::SimpleValueType VecVT = MVT::v2i32;
+  SDValue PowAndZero = DAG->getBuildVector(VecVT, Loc, {Pow2_16, Zero});
+  SDValue RotAmountVec =
+      DAG->getBuildVector(VecVT, Loc, {RotAmount, RotAmount});
+  SDValue RotrPowAndZero =
       DAG->getNode(ISD::ROTR, Loc, VecVT, PowAndZero, RotAmountVec);
-    APInt DemandAll(2, 3);
-    EXPECT_FALSE(DAG->isKnownToBeAPowerOfTwo(RotrPowAndZero, DemandAll));
-    EXPECT_TRUE(
+  APInt DemandAll(2, 3);
+  EXPECT_FALSE(DAG->isKnownToBeAPowerOfTwo(RotrPowAndZero, DemandAll));
+  EXPECT_TRUE(
       DAG->isKnownToBeAPowerOfTwo(RotrPowAndZero, DemandAll, /*OrZero=*/true));
 }
 

>From f3a380ff71a5730d138ce1aaec6968935c1638f3 Mon Sep 17 00:00:00 2001
From: Immad Mir <mirimmad17 at gmail.com>
Date: Wed, 25 Feb 2026 16:04:34 +0530
Subject: [PATCH 5/6] Use 'SELECT' when creating ROTL/ROTR nodes Use
 zeroinitializer instead of undef.

---
 llvm/test/CodeGen/X86/known-pow2.ll           |  8 +-
 .../AArch64/AArch64SelectionDAGTest.cpp       | 91 ++++++++++++++-----
 2 files changed, 70 insertions(+), 29 deletions(-)

diff --git a/llvm/test/CodeGen/X86/known-pow2.ll b/llvm/test/CodeGen/X86/known-pow2.ll
index d5c6ac773d496..91af45a3f357c 100644
--- a/llvm/test/CodeGen/X86/known-pow2.ll
+++ b/llvm/test/CodeGen/X86/known-pow2.ll
@@ -1016,9 +1016,9 @@ define <2 x i32> @pow2_rotl_vec() {
 ; CHECK:       retq
 entry:
   ; build vector <4,0>
-  %v0 = insertelement <2 x i32> undef, i32 4, i32 0
+  %v0 = insertelement <2 x i32> zeroinitializer, i32 4, i32 0
   %v1 = insertelement <2 x i32> %v0, i32 0, i32 1
-  %amt0 = insertelement <2 x i32> undef, i32 3, i32 0
+  %amt0 = insertelement <2 x i32> zeroinitializer, i32 3, i32 0
   %amt1 = insertelement <2 x i32> %amt0, i32 3, i32 1
   %r = call <2 x i32> @llvm.fshl.v2i32(<2 x i32> %v1, <2 x i32> %v1, <2 x i32> %amt1)
   ret <2 x i32> %r
@@ -1030,9 +1030,9 @@ define <2 x i32> @pow2_rotr_vec() {
 ; CHECK:       xmm0 = [2147483648,0,0,0]
 ; CHECK:       retq
 entry:
-  %v0 = insertelement <2 x i32> undef, i32 16, i32 0
+  %v0 = insertelement <2 x i32> zeroinitializer, i32 16, i32 0
   %v1 = insertelement <2 x i32> %v0, i32 0, i32 1
-  %amt0 = insertelement <2 x i32> undef, i32 5, i32 0
+  %amt0 = insertelement <2 x i32> zeroinitializer, i32 5, i32 0
   %amt1 = insertelement <2 x i32> %amt0, i32 5, i32 1
   %r = call <2 x i32> @llvm.fshr.v2i32(<2 x i32> %v1, <2 x i32> %v1, <2 x i32> %amt1)
   ret <2 x i32> %r
diff --git a/llvm/unittests/Target/AArch64/AArch64SelectionDAGTest.cpp b/llvm/unittests/Target/AArch64/AArch64SelectionDAGTest.cpp
index d599cf14adc07..4addfe97d98c1 100644
--- a/llvm/unittests/Target/AArch64/AArch64SelectionDAGTest.cpp
+++ b/llvm/unittests/Target/AArch64/AArch64SelectionDAGTest.cpp
@@ -1177,55 +1177,83 @@ TEST_F(AArch64SelectionDAGTest, KnownToBeAPowerOfTwo_ROTL) {
   // Power-of-two values
   SDValue Pow2_4 = DAG->getConstant(4, Loc, MVT::i32);
   SDValue Pow2_8 = DAG->getConstant(8, Loc, MVT::i32);
-
   // Non-power-of-two values
-  SDValue NonPow2_5 = DAG->getConstant(5, Loc, MVT::i32);
+  SDValue NonPow2_3 = DAG->getConstant(3, Loc, MVT::i32);
+  SDValue NonPow2_9 = DAG->getConstant(9, Loc, MVT::i32);
+
+  // Zero value
   SDValue NonPow2_0 = DAG->getConstant(0, Loc, MVT::i32);
 
   SDValue RotAmount = DAG->getConstant(3, Loc, MVT::i32);
 
-  SDValue RotlPow2_4 =
-      DAG->getNode(ISD::ROTL, Loc, MVT::i32, Pow2_4, RotAmount);
-  SDValue RotlPow2_8 =
-      DAG->getNode(ISD::ROTL, Loc, MVT::i32, Pow2_8, RotAmount);
-  SDValue RotlNonPow2_5 =
-      DAG->getNode(ISD::ROTL, Loc, MVT::i32, NonPow2_5, RotAmount);
-  SDValue RotlZero =
-      DAG->getNode(ISD::ROTL, Loc, MVT::i32, NonPow2_0, RotAmount);
+  SDValue Cond = DAG->getCopyFromReg(DAG->getEntryNode(), Loc, 1, MVT::i1);
 
-  EXPECT_TRUE(DAG->isKnownToBeAPowerOfTwo(RotlPow2_4));
-  EXPECT_TRUE(DAG->isKnownToBeAPowerOfTwo(RotlPow2_8));
+  SDValue Sel_pow2 =
+      DAG->getNode(ISD::SELECT, Loc, MVT::i32, Cond, Pow2_4, Pow2_8);
+  SDValue Sel_non_pow2 =
+      DAG->getNode(ISD::SELECT, Loc, MVT::i32, Cond, NonPow2_3, NonPow2_9);
 
-  EXPECT_FALSE(DAG->isKnownToBeAPowerOfTwo(RotlNonPow2_5));
+  SDValue RotlPow2 =
+      DAG->getNode(ISD::ROTL, Loc, MVT::i32, Sel_pow2, RotAmount);
+  SDValue RotlNonPow2 =
+      DAG->getNode(ISD::ROTL, Loc, MVT::i32, Sel_non_pow2, RotAmount);
+  SDValue RotlZero =
+      DAG->getNode(ISD::ROTL, Loc, MVT::i32, NonPow2_0, RotAmount);
 
+  EXPECT_TRUE(DAG->isKnownToBeAPowerOfTwo(RotlPow2));
+  EXPECT_FALSE(DAG->isKnownToBeAPowerOfTwo(RotlNonPow2));
   EXPECT_FALSE(DAG->isKnownToBeAPowerOfTwo(RotlZero));
   EXPECT_TRUE(DAG->isKnownToBeAPowerOfTwo(RotlZero, /*OrZero=*/true));
 
   // Also verify DemandedElts is forwarded through ROTL for vector lanes.
   MVT::SimpleValueType VecVT = MVT::v2i32;
   SDValue PowAndZero = DAG->getBuildVector(VecVT, Loc, {Pow2_4, NonPow2_0});
+  SDValue PowAndNonPow = DAG->getBuildVector(VecVT, Loc, {Pow2_8, NonPow2_0});
+
   SDValue RotAmountVec =
       DAG->getBuildVector(VecVT, Loc, {RotAmount, RotAmount});
-  SDValue RotlPowAndZero =
-      DAG->getNode(ISD::ROTL, Loc, VecVT, PowAndZero, RotAmountVec);
+
+  SDValue VecCond = DAG->getCopyFromReg(DAG->getEntryNode(), Loc, 2, MVT::v2i1);
+  SDValue Sel_pow2_vec =
+      DAG->getNode(ISD::VSELECT, Loc, VecVT, VecCond, PowAndZero, PowAndNonPow);
+
+  SDValue Rotl =
+      DAG->getNode(ISD::ROTL, Loc, VecVT, Sel_pow2_vec, RotAmountVec);
+
   APInt DemandAll(2, 3);
-  EXPECT_FALSE(DAG->isKnownToBeAPowerOfTwo(RotlPowAndZero, DemandAll));
-  EXPECT_TRUE(
-      DAG->isKnownToBeAPowerOfTwo(RotlPowAndZero, DemandAll, /*OrZero=*/true));
+  EXPECT_FALSE(DAG->isKnownToBeAPowerOfTwo(Rotl, DemandAll));
+  EXPECT_TRUE(DAG->isKnownToBeAPowerOfTwo(Rotl, DemandAll, /*OrZero=*/true));
+
+  APInt DemandLo(2, 1);
+  EXPECT_TRUE(DAG->isKnownToBeAPowerOfTwo(Rotl, DemandLo));
+
+  APInt DemandHi(2, 2);
+  EXPECT_FALSE(DAG->isKnownToBeAPowerOfTwo(Rotl, DemandHi));
+  EXPECT_TRUE(DAG->isKnownToBeAPowerOfTwo(Rotl, DemandHi, /*OrZero=*/true));
 }
 
 TEST_F(AArch64SelectionDAGTest, KnownToBeAPowerOfTwo_ROTR) {
   SDLoc Loc;
 
   SDValue Pow2_16 = DAG->getConstant(16, Loc, MVT::i32);
+  SDValue Pow2_8 = DAG->getConstant(8, Loc, MVT::i32);
   SDValue NonPow2_6 = DAG->getConstant(6, Loc, MVT::i32);
+  SDValue NonPow2_9 = DAG->getConstant(9, Loc, MVT::i32);
   SDValue Zero = DAG->getConstant(0, Loc, MVT::i32);
 
   SDValue RotAmount = DAG->getConstant(5, Loc, MVT::i32);
 
-  SDValue RotrPow2 = DAG->getNode(ISD::ROTR, Loc, MVT::i32, Pow2_16, RotAmount);
+  SDValue Cond = DAG->getCopyFromReg(DAG->getEntryNode(), Loc, 3, MVT::i1);
+
+  SDValue Sel_pow2 =
+      DAG->getNode(ISD::SELECT, Loc, MVT::i32, Cond, Pow2_16, Pow2_8);
+  SDValue Sel_non_pow2 =
+      DAG->getNode(ISD::SELECT, Loc, MVT::i32, Cond, NonPow2_6, NonPow2_9);
+
+  SDValue RotrPow2 =
+      DAG->getNode(ISD::ROTR, Loc, MVT::i32, Sel_pow2, RotAmount);
   SDValue RotrNonPow2 =
-      DAG->getNode(ISD::ROTR, Loc, MVT::i32, NonPow2_6, RotAmount);
+      DAG->getNode(ISD::ROTR, Loc, MVT::i32, Sel_non_pow2, RotAmount);
   SDValue RotrZero = DAG->getNode(ISD::ROTR, Loc, MVT::i32, Zero, RotAmount);
 
   EXPECT_TRUE(DAG->isKnownToBeAPowerOfTwo(RotrPow2));
@@ -1235,14 +1263,27 @@ TEST_F(AArch64SelectionDAGTest, KnownToBeAPowerOfTwo_ROTR) {
 
   MVT::SimpleValueType VecVT = MVT::v2i32;
   SDValue PowAndZero = DAG->getBuildVector(VecVT, Loc, {Pow2_16, Zero});
+  SDValue PowAndNonPow = DAG->getBuildVector(VecVT, Loc, {Pow2_8, Zero});
+
   SDValue RotAmountVec =
       DAG->getBuildVector(VecVT, Loc, {RotAmount, RotAmount});
-  SDValue RotrPowAndZero =
-      DAG->getNode(ISD::ROTR, Loc, VecVT, PowAndZero, RotAmountVec);
+
+  SDValue VecCond = DAG->getCopyFromReg(DAG->getEntryNode(), Loc, 4, MVT::v2i1);
+  SDValue Sel_pow2_vec =
+      DAG->getNode(ISD::VSELECT, Loc, VecVT, VecCond, PowAndZero, PowAndNonPow);
+
+  SDValue Rotr =
+      DAG->getNode(ISD::ROTR, Loc, VecVT, Sel_pow2_vec, RotAmountVec);
   APInt DemandAll(2, 3);
-  EXPECT_FALSE(DAG->isKnownToBeAPowerOfTwo(RotrPowAndZero, DemandAll));
-  EXPECT_TRUE(
-      DAG->isKnownToBeAPowerOfTwo(RotrPowAndZero, DemandAll, /*OrZero=*/true));
+  EXPECT_FALSE(DAG->isKnownToBeAPowerOfTwo(Rotr, DemandAll));
+  EXPECT_TRUE(DAG->isKnownToBeAPowerOfTwo(Rotr, DemandAll, /*OrZero=*/true));
+
+  APInt DemandLo(2, 1);
+  EXPECT_TRUE(DAG->isKnownToBeAPowerOfTwo(Rotr, DemandLo));
+
+  APInt DemandHi(2, 2);
+  EXPECT_FALSE(DAG->isKnownToBeAPowerOfTwo(Rotr, DemandHi));
+  EXPECT_TRUE(DAG->isKnownToBeAPowerOfTwo(Rotr, DemandHi, /*OrZero=*/true));
 }
 
 TEST_F(AArch64SelectionDAGTest, getRepeatedSequence_Patterns) {

>From 793fb623fa7f68aed2e7425ad58ae1bd86bbbce5 Mon Sep 17 00:00:00 2001
From: Immad Mir <mirimmad17 at gmail.com>
Date: Mon, 2 Mar 2026 14:52:24 +0530
Subject: [PATCH 6/6] Add test for handling DemndElts for vectors in ROTL and
 ROTR

---
 ...Add-fno-inline-functions-called-once.patch | 152 ++++++++++++++++++
 ...o-argument-in-isKnownToBeAPowerOfTwo.patch |  26 +++
 examples/hello                                | Bin 0 -> 16016 bytes
 examples/hello.c                              |   7 +
 examples/inline.c                             |  14 ++
 examples/inline.ll                            |  30 ++++
 examples/inline10.ll                          |  30 ++++
 examples/inline2.ll                           |  43 +++++
 examples/inline3.ll                           |  43 +++++
 examples/inline4.ll                           |  30 ++++
 llvm/test/CodeGen/X86/known-pow2.ll           | 137 ++++++++++++++--
 rotl.ll                                       |  23 +++
 rotl.s                                        |  39 +++++
 13 files changed, 557 insertions(+), 17 deletions(-)
 create mode 100644 0001-Add-fno-inline-functions-called-once.patch
 create mode 100644 0002-use-OrZero-argument-in-isKnownToBeAPowerOfTwo.patch
 create mode 100755 examples/hello
 create mode 100644 examples/hello.c
 create mode 100644 examples/inline.c
 create mode 100644 examples/inline.ll
 create mode 100644 examples/inline10.ll
 create mode 100644 examples/inline2.ll
 create mode 100644 examples/inline3.ll
 create mode 100644 examples/inline4.ll
 create mode 100644 rotl.ll
 create mode 100644 rotl.s

diff --git a/0001-Add-fno-inline-functions-called-once.patch b/0001-Add-fno-inline-functions-called-once.patch
new file mode 100644
index 0000000000000..1340b806c5e37
--- /dev/null
+++ b/0001-Add-fno-inline-functions-called-once.patch
@@ -0,0 +1,152 @@
+From 2558e7ab7e85a0f943ffbef7f2bd0f463c6ea398 Mon Sep 17 00:00:00 2001
+From: Immad Mir <mirimmad17 at gmail.com>
+Date: Tue, 17 Feb 2026 11:26:40 +0530
+Subject: [PATCH 1/2] Add -fno-inline-functions-called-once
+
+---
+ clang/include/clang/Options/Options.td     |  7 +++++-
+ clang/lib/Driver/ToolChains/Clang.cpp      |  6 +++++
+ clang/test/CodeGen/no_inline_called_once.c | 26 ++++++++++++++++++++++
+ clang/test/Driver/clang_f_opts.c           |  6 ++---
+ llvm/lib/Analysis/InlineCost.cpp           | 17 ++++++++++++--
+ 5 files changed, 56 insertions(+), 6 deletions(-)
+ create mode 100644 clang/test/CodeGen/no_inline_called_once.c
+
+diff --git a/clang/include/clang/Options/Options.td b/clang/include/clang/Options/Options.td
+index 4ac812e92e2c..2aac64fec582 100644
+--- a/clang/include/clang/Options/Options.td
++++ b/clang/include/clang/Options/Options.td
+@@ -7238,8 +7238,13 @@ defm merge_constants : BooleanFFlag<"merge-constants">, Group<clang_ignored_gcc_
+ defm modulo_sched : BooleanFFlag<"modulo-sched">, Group<clang_ignored_gcc_optimization_f_Group>;
+ defm modulo_sched_allow_regmoves : BooleanFFlag<"modulo-sched-allow-regmoves">,
+     Group<clang_ignored_gcc_optimization_f_Group>;
++//defm inline_functions_called_once : BooleanFFlag<"inline-functions-called-once">,
++ //   Group<clang_ignored_gcc_optimization_f_Group>;
+ defm inline_functions_called_once : BooleanFFlag<"inline-functions-called-once">,
+-    Group<clang_ignored_gcc_optimization_f_Group>;
++      Group<f_clang_Group>,
++      Visibility<[ClangOption, CC1Option]>,
++      HelpText<"Control Inlining of TU-Local functions called exactly once "
++      "(Use -fno-inline-functions-called-once to inhibit it)">;
+ def finline_limit_EQ : Joined<["-"], "finline-limit=">, Group<clang_ignored_gcc_optimization_f_Group>;
+ defm finline_limit : BooleanFFlag<"inline-limit">, Group<clang_ignored_gcc_optimization_f_Group>;
+ defm inline_small_functions : BooleanFFlag<"inline-small-functions">,
+diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
+index 195f05d61464..4aae32e4f2cf 100644
+--- a/clang/lib/Driver/ToolChains/Clang.cpp
++++ b/clang/lib/Driver/ToolChains/Clang.cpp
+@@ -5072,6 +5072,12 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
+                                                 << TripleStr;
+   }
+ 
++  if (Args.hasArg(options::OPT_fno_inline_functions_called_once)) {
++    CmdArgs.push_back("-mllvm");
++    CmdArgs.push_back("-no-inline-functions-called-once");
++  }
++
++
+   // Push all default warning arguments that are specific to
+   // the given target.  These come before user provided warning options
+   // are provided.
+diff --git a/clang/test/CodeGen/no_inline_called_once.c b/clang/test/CodeGen/no_inline_called_once.c
+new file mode 100644
+index 000000000000..7be46f398412
+--- /dev/null
++++ b/clang/test/CodeGen/no_inline_called_once.c
+@@ -0,0 +1,26 @@
++// RUN: %clang -O2 -S -emit-llvm %s -fno-inline-functions-called-once -o - | FileCheck %s --check-prefix=NOINLINE
++
++// Test purpose:
++// 1) Ensure there is a surviving call to bad_function (i.e., it was not inlined)
++// 2) Ensure bad_function’s definition exists in the IR
++
++// NOINLINE-DAG: call{{.*}}@bad_function{{.*}}
++// NOINLINE-DAG: define internal{{.*}}@bad_function{{.*}}
++
++volatile int G;
++
++static void bad_function(void){
++  // Volatile side effect ensures the call can’t be removed by DCE
++  G++;
++}
++
++static void test(void){
++  // Single TU-local caller of bad_function
++  bad_function();
++}
++
++int main(void){
++  // Make the caller reachable so it survives global DCE
++  test();
++  return 0;
++}
+diff --git a/clang/test/Driver/clang_f_opts.c b/clang/test/Driver/clang_f_opts.c
+index 5871f1580d6b..921cbf3b1a60 100644
+--- a/clang/test/Driver/clang_f_opts.c
++++ b/clang/test/Driver/clang_f_opts.c
+@@ -283,7 +283,7 @@
+ // RUN:     -fgcse-las                                                        \
+ // RUN:     -fgcse-sm                                                         \
+ // RUN:     -fipa-cp                                                          \
+-// RUN:     -finline-functions-called-once                                    \
++
+ // RUN:     -fmodulo-sched                                                    \
+ // RUN:     -fmodulo-sched-allow-regmoves                                     \
+ // RUN:     -fpeel-loops                                                      \
+@@ -355,7 +355,7 @@
+ // RUN: -fgcse-las                                                            \
+ // RUN: -fgcse-sm                                                             \
+ // RUN: -fipa-cp                                                              \
+-// RUN: -finline-functions-called-once                                        \
++
+ // RUN: -fmodulo-sched                                                        \
+ // RUN: -fmodulo-sched-allow-regmoves                                         \
+ // RUN: -fpeel-loops                                                          \
+@@ -414,7 +414,7 @@
+ // CHECK-WARNING-DAG: optimization flag '-fgcse-las' is not supported
+ // CHECK-WARNING-DAG: optimization flag '-fgcse-sm' is not supported
+ // CHECK-WARNING-DAG: optimization flag '-fipa-cp' is not supported
+-// CHECK-WARNING-DAG: optimization flag '-finline-functions-called-once' is not supported
++
+ // CHECK-WARNING-DAG: optimization flag '-fmodulo-sched' is not supported
+ // CHECK-WARNING-DAG: optimization flag '-fmodulo-sched-allow-regmoves' is not supported
+ // CHECK-WARNING-DAG: optimization flag '-fpeel-loops' is not supported
+diff --git a/llvm/lib/Analysis/InlineCost.cpp b/llvm/lib/Analysis/InlineCost.cpp
+index e0054e3ed6ee..f449a4dad03d 100644
+--- a/llvm/lib/Analysis/InlineCost.cpp
++++ b/llvm/lib/Analysis/InlineCost.cpp
+@@ -154,6 +154,11 @@ static cl::opt<int> CallPenalty(
+     "inline-call-penalty", cl::Hidden, cl::init(25),
+     cl::desc("Call penalty that is applied per callsite when inlining"));
+ 
++static cl::opt<bool> NoInlineFunctionsCalledOnce(
++    "no-inline-functions-called-once", cl::Hidden, cl::init(false),
++    cl::desc("Disable inlining of functions with internal linkage that are "
++             "called only once"));
++
+ static cl::opt<size_t>
+     StackSizeThreshold("inline-max-stacksize", cl::Hidden,
+                        cl::init(std::numeric_limits<size_t>::max()),
+@@ -2193,9 +2198,17 @@ void InlineCostCallAnalyzer::updateThreshold(CallBase &Call, Function &Callee) {
+   // If there is only one call of the function, and it has internal linkage,
+   // the cost of inlining it drops dramatically. It may seem odd to update
+   // Cost in updateThreshold, but the bonus depends on the logic in this method.
++  // When -fno-inline-functions-called-once is enabled, we disable this bonus
++  // and instead apply a large cost penalty to prevent inlining of such
++  // functions, allowing them to be preserved as separate entities.
+   if (isSoleCallToLocalFunction(Call, F)) {
+-    addCost(-LastCallToStaticBonus);
+-    StaticBonusApplied = LastCallToStaticBonus;
++    if (NoInlineFunctionsCalledOnce) {
++      addCost(INT_MAX);
++      //StaticBonusApplied = 0;
++    } else {
++      addCost(-LastCallToStaticBonus);
++      StaticBonusApplied = LastCallToStaticBonus;
++    }
+   }
+ }
+ 
+-- 
+2.34.1
+
diff --git a/0002-use-OrZero-argument-in-isKnownToBeAPowerOfTwo.patch b/0002-use-OrZero-argument-in-isKnownToBeAPowerOfTwo.patch
new file mode 100644
index 0000000000000..9d3e75d3e1832
--- /dev/null
+++ b/0002-use-OrZero-argument-in-isKnownToBeAPowerOfTwo.patch
@@ -0,0 +1,26 @@
+From b291b91ff3894e929503f925488d3bea21ed09b2 Mon Sep 17 00:00:00 2001
+From: Immad Mir <mirimmad17 at gmail.com>
+Date: Sun, 22 Feb 2026 18:08:46 +0530
+Subject: [PATCH 2/2] use 'OrZero' argument in isKnownToBeAPowerOfTwo
+
+---
+ llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp | 3 +--
+ 1 file changed, 1 insertion(+), 2 deletions(-)
+
+diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
+index 581553d41cb6..01b2b5407ced 100644
+--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
++++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
+@@ -4733,8 +4733,7 @@ bool SelectionDAG::isKnownToBeAPowerOfTwo(SDValue Val,
+ 
+   case ISD::ROTL:
+   case ISD::ROTR:
+-    return isKnownToBeAPowerOfTwo(Val.getOperand(0), /*OrZero=*/false,
+-                                  Depth + 1);
++    return isKnownToBeAPowerOfTwo(Val.getOperand(0), OrZero, Depth + 1);
+ 
+   case ISD::SMIN:
+   case ISD::SMAX:
+-- 
+2.34.1
+
diff --git a/examples/hello b/examples/hello
new file mode 100755
index 0000000000000000000000000000000000000000..e2c232df1baa04d2fcb6a37d1c1e7ea299984450
GIT binary patch
literal 16016
zcmeHOU2Ggz6~12AjYE^zacgiBNJb?=4&d?bdhNKaOxC{{M~M>>2Nj8SI=egTUDzM9
zJKNe30;9BusA`)^Q1L*esyxuXKtMc#a8(pq2_Yhca37FN)k-b`>QX>1ATQ_4oNqHb
ztOWw8FU*y8zI(oN&b?>O+?l<zd+(Q~PtQc!+7wE=`h;TXT(`$0MbUj+Dg;S2s`k?H
zkou6imE>Do&eJD7Agx!{BF)$-d^;ig^>L*Ie$b;85D_x6-%e$-!_xqjoCo{4Dv`kh
zYtjJ_e4{^2dJN#tga-QkI5!&up25agGa8bYbAs?=k^Q#BZ%h1u>k=mr`Q)7NXH at bT
zM2&P at H20`f(C<F+3nJ`+$o+*koXmgI-#PKyBMk=e;}Rbb{e6=BI4}Qc!DT)#{q1xe
z`Xv8ANv|3#<WnPw!9r%BkT2Di29`!g21XKkwXDb85I8RGgU8g#xr-{YqE?xMKlCiK
zo(rcKmmAla_1<}XnN6gw)D8RRMVHv&z<u|$(48VqYr)?|Tr2zR4)(Pjc#fCb`j=vw
z=_Sj|=1W!~f5}#+NeN7wRmZA0X3@%*)M6!Ha<YnPPUDC=d3ttY(v0hIeYjB{N+@%7
z{*0NiEA~RZ>e!X}Gn0jK$)2}T1#WerST0Gcrf1sHkiv`5wP>dgANjgO)G<Pgi`s$w
zZoVQz!u{Z>r|B|8)CrQg;C{t at 65a3dn{q#b-;@UXlb#+t<TYuKuQBHiW!=YPOt5al
z$7Ad>zv<)U5y!OJ^6}m{a%t6<^l%q}+}a)y9>WZT83;2FW+2Q!n1L_ at VFv!MGw at E=
zp}!fIf8S|5*753~QpTgJPNe<|<MK~BUvl$OAAXJK`q9_v*mXEbHI{R%dvm=`<>>cG
z&R#_lxxLsQX0P0Mz_{|7aryPlbMw=&)z~Y><6}3-rrw(-^FL?xuEUSGF2uh;jPZEK
za}0NHIlUCh4A&`?-SzdZ!z*m~k|5bHyY|BshGW~tl}+QtKb|mN+-f)4t{XSDoja(3
zUTL7SzMkzmJayBb<69XUCbg=4?4ogb>`yvXjVo_BdyL1%-cS6-F*>V_3>~g_>?OJF
ze(Jl?Kl{J=FexRTi{wiA>;CEVKR8SZ?>qaHx~4XMxm~ZvRw<F)_j4v2d2VEu&BGFA
zAk09RfiMGM2Eq)483;2FW+2Q!n1L_ at VFuot4DkPAhFvI>-$!q{2ix!Mr*8Rwr#m+5
z_0JF{2>DHYijZETRpRY>{hO?(50|z}=T+NMciX`|yE?DZA6(4yfA(8%)$1Q&gQ;Ea
zKV(O!nxM}$;x$6GuX|=+->I(K9`0OGCl1{E(V-*m--_(d{XS22_%DskxlM;YS1FF8
zOpNdA{z_zW at 2w9~$1H~>%s`ldFau!*!VH8N2s037Ak09RfiMGM2HXrZ)-TF>MXW{S
zHIZEQc+glNxmV<ztb4puWUPxkC^Fu?4v38Pk^H{K1^T~i*UKy~$znvTVKn$<lM2=@
zJ}Y{99d*l-g0~6w3HAyC#7(KB$6!q*?D at Tt3$KUd@&OOjngjyAPh`ZsDt5G3)GcW5
z?{)8dh;&Wq-(L6pHspNhA7WQVp6RvKmDfc7uE>qJrTzCBu&zAV?}&^O at L11!a&qz>
zt^Z=GR&r`uEUpjf(Scaa6=PqF$MtAJj~^3-N*Aosg7%PIspiWiEl#$2bRq9({W-^3
ztll#?NJ6fb($nSQV4?7kcNkc#ls{{y9c^fMI5l#7Bpc7#iP+t#Xgm`eiYGGZbTna)
z#*b&>Bg0lKGh|~3Bh)73{NJM9t~Ze+ at b%=C!C3FEiNWoJt?UmGA5r_%N+2)n$NaSb
z&+Cer$NFFr-7-tZHg%_pCOsMBgI9>R(XS`Ce>|;HU!V%NgZmf!S6%zt8|#$8f14_;
z;@|1KzU#Krqh8j~!cA<|&W~LCUKJcS*Il>$3hVjN%B!wD-EB!Q9==7;YCZZMN{H5M
z>Pn|vzE5e%W at SI|-KwvVPiO~;?}>CNJiovXGY`RshO`rG|0404&kofL@%tlv3ik#0
z&oj at f%*9D=ceO%%H(mG9BrH9gBff{OFZSEGznPKhQL)E3f!9-FpOoja`&$?(pC+E;
z3EDqLyas0<{*d^)d@`!PCcY;^PuswFRu|G0C#IKa3BHxen~t at hh^RWXY*tUJo6Gf0
zr)Z{m!G4u2Gi7t3P)=C|GvkyiRnw|1DZ1Ae3$|lt^k{1f-b0W#txCmOHtmvASytJK
zRkY1atyo+p7oTL3rPE}Z<DW7qAk&;VKYnJ~oSvI9X`jH<r{~7c%uW(bdoj#uL$t=!
zd1anFeRg8}w0U-B=EC&6IX^yedYU|f+ZU49t?=)5DA*axv6OofvgM)S${QUhDWvyy
zFp$u^ox#jh%Vy3hWq7N@>{+tP<V$9)YG)Lsf(~Y?S`}Y+PlajH_6VFL at 0)138KQY>
zMsqlvL8bKSa?!C;gigi79BQ;_!>%kUy;OE=eW6s->G8a1SDa;`#wTV6cr>`WoK?*!
zJ+oY*_B?bdo<_a|ngo+{6}w=ugVYuaj?&$jbgJqLWfC2GiH<xf$f)e5SGRLA4ssc4
z1d`|K4G7Nzb?U(?=F`-D+2NKcIKE6e<x}z3Pmx9%Zh`*^d0s at f5IwWK(kEZ=K+KCo
z?b at Qw_+k3+Tm=53;t$kF=P#J2fE%HT-~T at G2Wl`B#PbRC6jlC8a})HxB>uo7Vu<-$
zu>U#I^IQ%7n9l*97k{4bah~9hdDW+g<1XQk`62L0()kOw;V%!6z;iG7W4;BX#d&T)
z{ICOlj<h_NgN*qcFe?7R_=Q%NsKPmgKjxFbRWU^W!SjDa{3pZ?^HU)5#Qr>o4f;Pp
z9Os1Na|@p1f|$2M&9VB+6%zdZn)o%)Z%p+-^Xh8>|D+fI5igp7416ZwkMBibZ{UKX
zf3)|lfIq(ffq1WheenEW5Pw`hrHQ2=-a~|SuQlq7-=hzQ2!G6nfY{f_wp at b!|Cl(x
z|ER`Rw69Zh!OgE%^Nz?HxL2scV_a_A#{3ZbAA|OgfxjZ(E}uW<qniUnQh}W(-IT%q
zmJ~ep&_Cvh*w4DoCr1V26Mndk-yj{Ez#seP)&|*2seTU{NB9GOM;gDsN{VUJLrp{g
z{DAL}!tbvoJxfje5ogdJ^1D>wwh%w&V^Q%(Kd_f{KvdgQV@}K)Jp8}jV7v<a{fM#W
zH+ at +ed{6 at 8?;)NCBOW|gb`yu#{r>$4Px=iy5 at kT7xo>O#4*p`IvMb;a5Y;~c^HkS`

literal 0
HcmV?d00001

diff --git a/examples/hello.c b/examples/hello.c
new file mode 100644
index 0000000000000..2b964866633a3
--- /dev/null
+++ b/examples/hello.c
@@ -0,0 +1,7 @@
+#include <stdio.h>
+
+int main() {
+
+	printf("Hello\n");
+	return 0;
+}
diff --git a/examples/inline.c b/examples/inline.c
new file mode 100644
index 0000000000000..251431d21546d
--- /dev/null
+++ b/examples/inline.c
@@ -0,0 +1,14 @@
+volatile int G;
+
+static void bad_function(void) {
+	G++;
+}
+
+static void test(void) {
+	bad_function();
+}
+
+int main() {
+	test();
+	return 0;
+}
diff --git a/examples/inline.ll b/examples/inline.ll
new file mode 100644
index 0000000000000..889e3a3a08c53
--- /dev/null
+++ b/examples/inline.ll
@@ -0,0 +1,30 @@
+; ModuleID = 'inline.c'
+source_filename = "inline.c"
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+ at G = dso_local global i32 0, align 4
+
+; Function Attrs: nofree norecurse nounwind memory(readwrite, argmem: none, target_mem0: none, target_mem1: none) uwtable
+define dso_local noundef i32 @main() local_unnamed_addr #0 {
+  %1 = load volatile i32, ptr @G, align 4, !tbaa !5
+  %2 = add nsw i32 %1, 1
+  store volatile i32 %2, ptr @G, align 4, !tbaa !5
+  ret i32 0
+}
+
+attributes #0 = { nofree norecurse nounwind memory(readwrite, argmem: none, target_mem0: none, target_mem1: none) uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
+
+!llvm.module.flags = !{!0, !1, !2, !3}
+!llvm.ident = !{!4}
+!llvm.errno.tbaa = !{!5}
+
+!0 = !{i32 1, !"wchar_size", i32 4}
+!1 = !{i32 8, !"PIC Level", i32 2}
+!2 = !{i32 7, !"PIE Level", i32 2}
+!3 = !{i32 7, !"uwtable", i32 2}
+!4 = !{!"clang version 23.0.0git (https://github.com/llvm/llvm-project 355b676f2fe419b02d1324dcc04e827d265a1d3e)"}
+!5 = !{!6, !6, i64 0}
+!6 = !{!"int", !7, i64 0}
+!7 = !{!"omnipotent char", !8, i64 0}
+!8 = !{!"Simple C/C++ TBAA"}
diff --git a/examples/inline10.ll b/examples/inline10.ll
new file mode 100644
index 0000000000000..889e3a3a08c53
--- /dev/null
+++ b/examples/inline10.ll
@@ -0,0 +1,30 @@
+; ModuleID = 'inline.c'
+source_filename = "inline.c"
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+ at G = dso_local global i32 0, align 4
+
+; Function Attrs: nofree norecurse nounwind memory(readwrite, argmem: none, target_mem0: none, target_mem1: none) uwtable
+define dso_local noundef i32 @main() local_unnamed_addr #0 {
+  %1 = load volatile i32, ptr @G, align 4, !tbaa !5
+  %2 = add nsw i32 %1, 1
+  store volatile i32 %2, ptr @G, align 4, !tbaa !5
+  ret i32 0
+}
+
+attributes #0 = { nofree norecurse nounwind memory(readwrite, argmem: none, target_mem0: none, target_mem1: none) uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
+
+!llvm.module.flags = !{!0, !1, !2, !3}
+!llvm.ident = !{!4}
+!llvm.errno.tbaa = !{!5}
+
+!0 = !{i32 1, !"wchar_size", i32 4}
+!1 = !{i32 8, !"PIC Level", i32 2}
+!2 = !{i32 7, !"PIE Level", i32 2}
+!3 = !{i32 7, !"uwtable", i32 2}
+!4 = !{!"clang version 23.0.0git (https://github.com/llvm/llvm-project 355b676f2fe419b02d1324dcc04e827d265a1d3e)"}
+!5 = !{!6, !6, i64 0}
+!6 = !{!"int", !7, i64 0}
+!7 = !{!"omnipotent char", !8, i64 0}
+!8 = !{!"Simple C/C++ TBAA"}
diff --git a/examples/inline2.ll b/examples/inline2.ll
new file mode 100644
index 0000000000000..ec8832c0c941a
--- /dev/null
+++ b/examples/inline2.ll
@@ -0,0 +1,43 @@
+; ModuleID = 'inline.c'
+source_filename = "inline.c"
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+ at G = dso_local global i32 0, align 4
+
+; Function Attrs: nofree norecurse nounwind memory(readwrite, target_mem0: none, target_mem1: none) uwtable
+define dso_local noundef i32 @main() local_unnamed_addr #0 {
+  tail call fastcc void @test()
+  ret i32 0
+}
+
+; Function Attrs: nofree norecurse nounwind memory(readwrite, target_mem0: none, target_mem1: none) uwtable
+define internal fastcc void @test() unnamed_addr #0 {
+  tail call fastcc void @bad_function()
+  ret void
+}
+
+; Function Attrs: nofree norecurse nounwind memory(readwrite, argmem: none, target_mem0: none, target_mem1: none) uwtable
+define internal fastcc void @bad_function() unnamed_addr #1 {
+  %1 = load volatile i32, ptr @G, align 4, !tbaa !5
+  %2 = add nsw i32 %1, 1
+  store volatile i32 %2, ptr @G, align 4, !tbaa !5
+  ret void
+}
+
+attributes #0 = { nofree norecurse nounwind memory(readwrite, target_mem0: none, target_mem1: none) uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
+attributes #1 = { nofree norecurse nounwind memory(readwrite, argmem: none, target_mem0: none, target_mem1: none) uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
+
+!llvm.module.flags = !{!0, !1, !2, !3}
+!llvm.ident = !{!4}
+!llvm.errno.tbaa = !{!5}
+
+!0 = !{i32 1, !"wchar_size", i32 4}
+!1 = !{i32 8, !"PIC Level", i32 2}
+!2 = !{i32 7, !"PIE Level", i32 2}
+!3 = !{i32 7, !"uwtable", i32 2}
+!4 = !{!"clang version 23.0.0git (https://github.com/llvm/llvm-project 355b676f2fe419b02d1324dcc04e827d265a1d3e)"}
+!5 = !{!6, !6, i64 0}
+!6 = !{!"int", !7, i64 0}
+!7 = !{!"omnipotent char", !8, i64 0}
+!8 = !{!"Simple C/C++ TBAA"}
diff --git a/examples/inline3.ll b/examples/inline3.ll
new file mode 100644
index 0000000000000..ec8832c0c941a
--- /dev/null
+++ b/examples/inline3.ll
@@ -0,0 +1,43 @@
+; ModuleID = 'inline.c'
+source_filename = "inline.c"
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+ at G = dso_local global i32 0, align 4
+
+; Function Attrs: nofree norecurse nounwind memory(readwrite, target_mem0: none, target_mem1: none) uwtable
+define dso_local noundef i32 @main() local_unnamed_addr #0 {
+  tail call fastcc void @test()
+  ret i32 0
+}
+
+; Function Attrs: nofree norecurse nounwind memory(readwrite, target_mem0: none, target_mem1: none) uwtable
+define internal fastcc void @test() unnamed_addr #0 {
+  tail call fastcc void @bad_function()
+  ret void
+}
+
+; Function Attrs: nofree norecurse nounwind memory(readwrite, argmem: none, target_mem0: none, target_mem1: none) uwtable
+define internal fastcc void @bad_function() unnamed_addr #1 {
+  %1 = load volatile i32, ptr @G, align 4, !tbaa !5
+  %2 = add nsw i32 %1, 1
+  store volatile i32 %2, ptr @G, align 4, !tbaa !5
+  ret void
+}
+
+attributes #0 = { nofree norecurse nounwind memory(readwrite, target_mem0: none, target_mem1: none) uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
+attributes #1 = { nofree norecurse nounwind memory(readwrite, argmem: none, target_mem0: none, target_mem1: none) uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
+
+!llvm.module.flags = !{!0, !1, !2, !3}
+!llvm.ident = !{!4}
+!llvm.errno.tbaa = !{!5}
+
+!0 = !{i32 1, !"wchar_size", i32 4}
+!1 = !{i32 8, !"PIC Level", i32 2}
+!2 = !{i32 7, !"PIE Level", i32 2}
+!3 = !{i32 7, !"uwtable", i32 2}
+!4 = !{!"clang version 23.0.0git (https://github.com/llvm/llvm-project 355b676f2fe419b02d1324dcc04e827d265a1d3e)"}
+!5 = !{!6, !6, i64 0}
+!6 = !{!"int", !7, i64 0}
+!7 = !{!"omnipotent char", !8, i64 0}
+!8 = !{!"Simple C/C++ TBAA"}
diff --git a/examples/inline4.ll b/examples/inline4.ll
new file mode 100644
index 0000000000000..889e3a3a08c53
--- /dev/null
+++ b/examples/inline4.ll
@@ -0,0 +1,30 @@
+; ModuleID = 'inline.c'
+source_filename = "inline.c"
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+ at G = dso_local global i32 0, align 4
+
+; Function Attrs: nofree norecurse nounwind memory(readwrite, argmem: none, target_mem0: none, target_mem1: none) uwtable
+define dso_local noundef i32 @main() local_unnamed_addr #0 {
+  %1 = load volatile i32, ptr @G, align 4, !tbaa !5
+  %2 = add nsw i32 %1, 1
+  store volatile i32 %2, ptr @G, align 4, !tbaa !5
+  ret i32 0
+}
+
+attributes #0 = { nofree norecurse nounwind memory(readwrite, argmem: none, target_mem0: none, target_mem1: none) uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
+
+!llvm.module.flags = !{!0, !1, !2, !3}
+!llvm.ident = !{!4}
+!llvm.errno.tbaa = !{!5}
+
+!0 = !{i32 1, !"wchar_size", i32 4}
+!1 = !{i32 8, !"PIC Level", i32 2}
+!2 = !{i32 7, !"PIE Level", i32 2}
+!3 = !{i32 7, !"uwtable", i32 2}
+!4 = !{!"clang version 23.0.0git (https://github.com/llvm/llvm-project 355b676f2fe419b02d1324dcc04e827d265a1d3e)"}
+!5 = !{!6, !6, i64 0}
+!6 = !{!"int", !7, i64 0}
+!7 = !{!"omnipotent char", !8, i64 0}
+!8 = !{!"Simple C/C++ TBAA"}
diff --git a/llvm/test/CodeGen/X86/known-pow2.ll b/llvm/test/CodeGen/X86/known-pow2.ll
index 91af45a3f357c..9ba1de7cebfd4 100644
--- a/llvm/test/CodeGen/X86/known-pow2.ll
+++ b/llvm/test/CodeGen/X86/known-pow2.ll
@@ -1009,31 +1009,134 @@ define i32 @pow2_blsi_sub(i32 %x, i32 %a) {
   ret i32 %r
 }
 
-define <2 x i32> @pow2_rotl_vec() {
-; CHECK-LABEL: pow2_rotl_vec:
+; Negative test: Y = a | 1 is always odd/non-zero but not pow2, fold should not trigger.
+define i32 @pow2_blsi_add_fail(i32 %x, i32 %a) {
+; CHECK-LABEL: pow2_blsi_add_fail:
 ; CHECK:       # %bb.0:
 ; CHECK:       xmm0 = [32,0,0,0]
 ; CHECK:       retq
 entry:
-  ; build vector <4,0>
-  %v0 = insertelement <2 x i32> zeroinitializer, i32 4, i32 0
-  %v1 = insertelement <2 x i32> %v0, i32 0, i32 1
-  %amt0 = insertelement <2 x i32> zeroinitializer, i32 3, i32 0
-  %amt1 = insertelement <2 x i32> %amt0, i32 3, i32 1
-  %r = call <2 x i32> @llvm.fshl.v2i32(<2 x i32> %v1, <2 x i32> %v1, <2 x i32> %amt1)
-  ret <2 x i32> %r
+  %cmp = icmp sgt <2 x i32> zeroinitializer, %a0
+
+  %powvec = select <2 x i1> %cmp,
+                     <2 x i32> <i32 1024, i32 1024>,
+                     <2 x i32> <i32 4096, i32 4096>
+
+  %base = extractelement <2 x i32> %powvec, i32 0
+
+  %d = call i32 @llvm.fshl.i32(i32 %base,
+                               i32 %base,
+                               i32 %rotamt)
+
+  %and = and i32 %x, %d
+  %r = icmp eq i32 %and, %d
+
+  ret i1 %r
 }
 
 define <2 x i32> @pow2_rotr_vec() {
 ; CHECK-LABEL: pow2_rotr_vec:
 ; CHECK:       # %bb.0:
-; CHECK:       xmm0 = [2147483648,0,0,0]
-; CHECK:       retq
+; CHECK-NEXT:    movl %esi, %eax
+; CHECK-NEXT:    negl %eax
+; CHECK-NEXT:    andl %esi, %eax
+; CHECK-NEXT:    notl %edi
+; CHECK-NEXT:    andl %edi, %eax
+; CHECK-NEXT:    retq
+  %neg_a = sub i32 0, %a
+  %y = and i32 %a, %neg_a
+  %x_add_y = add i32 %x, %y
+  %r = and i32 %x_add_y, %y
+  ret i32 %r
+}
+
+; Test that (X - Y) & Y --> ~X & Y when Y = a & -a (pow2-or-zero).
+define i32 @pow2_blsi_sub(i32 %x, i32 %a) {
+; CHECK-LABEL: pow2_blsi_sub:
+; CHECK:       # %bb.0:
+; CHECK-NEXT:    movl %esi, %eax
+; CHECK-NEXT:    negl %eax
+; CHECK-NEXT:    andl %esi, %eax
+; CHECK-NEXT:    notl %edi
+; CHECK-NEXT:    andl %edi, %eax
+; CHECK-NEXT:    retq
+  %neg_a = sub i32 0, %a
+  %y = and i32 %a, %neg_a
+  %x_sub_y = sub i32 %x, %y
+  %r = and i32 %x_sub_y, %y
+  ret i32 %r
+}
+
+
+
+define i1 @pow2_rotl_extract_vec(<2 x i32> %a0, i32 %rotamt, i32 %x) {
+; CHECK-LABEL: pow2_rotl_extract_vec:
+; CHECK:       # %bb.0: # %entry
+; CHECK-NEXT:    movl %edi, %ecx
+; CHECK-NEXT:    pxor %xmm1, %xmm1
+; CHECK-NEXT:    pcmpgtd %xmm0, %xmm1
+; CHECK-NEXT:    movdqa %xmm1, %xmm0
+; CHECK-NEXT:    pandn {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm0
+; CHECK-NEXT:    pand {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm1
+; CHECK-NEXT:    por %xmm0, %xmm1
+; CHECK-NEXT:    movd %xmm1, %eax
+; CHECK-NEXT:    # kill: def $cl killed $cl killed $ecx
+; CHECK-NEXT:    roll %cl, %eax
+; CHECK-NEXT:    notl %esi
+; CHECK-NEXT:    testl %esi, %eax
+; CHECK-NEXT:    sete %al
+; CHECK-NEXT:    retq
 entry:
-  %v0 = insertelement <2 x i32> zeroinitializer, i32 16, i32 0
-  %v1 = insertelement <2 x i32> %v0, i32 0, i32 1
-  %amt0 = insertelement <2 x i32> zeroinitializer, i32 5, i32 0
-  %amt1 = insertelement <2 x i32> %amt0, i32 5, i32 1
-  %r = call <2 x i32> @llvm.fshr.v2i32(<2 x i32> %v1, <2 x i32> %v1, <2 x i32> %amt1)
-  ret <2 x i32> %r
+  %cmp = icmp sgt <2 x i32> zeroinitializer, %a0
+
+  %powvec = select <2 x i1> %cmp,
+                     <2 x i32> <i32 1024, i32 1024>,
+                     <2 x i32> <i32 4096, i32 4096>
+
+  %base = extractelement <2 x i32> %powvec, i32 0
+
+  %d = call i32 @llvm.fshl.i32(i32 %base,
+                               i32 %base,
+                               i32 %rotamt)
+
+  %and = and i32 %x, %d
+  %r = icmp eq i32 %and, %d
+
+  ret i1 %r
+}
+
+define i1 @pow2_rotr_extract_vec(<2 x i32> %a0, i32 %rotamt, i32 %x) {
+; CHECK-LABEL: pow2_rotr_extract_vec:
+; CHECK:       # %bb.0: # %entry
+; CHECK-NEXT:    movl %edi, %ecx
+; CHECK-NEXT:    pxor %xmm1, %xmm1
+; CHECK-NEXT:    pcmpgtd %xmm0, %xmm1
+; CHECK-NEXT:    movdqa %xmm1, %xmm0
+; CHECK-NEXT:    pandn {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm0
+; CHECK-NEXT:    pand {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm1
+; CHECK-NEXT:    por %xmm0, %xmm1
+; CHECK-NEXT:    movd %xmm1, %eax
+; CHECK-NEXT:    # kill: def $cl killed $cl killed $ecx
+; CHECK-NEXT:    rorl %cl, %eax
+; CHECK-NEXT:    notl %esi
+; CHECK-NEXT:    testl %esi, %eax
+; CHECK-NEXT:    sete %al
+; CHECK-NEXT:    retq
+entry:
+
+  %cmp = icmp sgt <2 x i32> zeroinitializer, %a0
+  %powvec = select <2 x i1> %cmp,
+                     <2 x i32> <i32 1024, i32 1024>,
+                     <2 x i32> <i32 4096, i32 4096>
+
+  %base = extractelement <2 x i32> %powvec, i32 0
+
+  %d = call i32 @llvm.fshr.i32(i32 %base,
+                               i32 %base,
+                               i32 %rotamt)
+
+  %and = and i32 %x, %d
+  %r = icmp eq i32 %and, %d
+
+  ret i1 %r
 }
diff --git a/rotl.ll b/rotl.ll
new file mode 100644
index 0000000000000..a3b747bbcfcb4
--- /dev/null
+++ b/rotl.ll
@@ -0,0 +1,23 @@
+declare <2 x i32> @llvm.fshl.v2i32(<2 x i32>, <2 x i32>, <2 x i32>)
+declare i32 @llvm.fshl.i32(i32, i32, i32)
+
+define <2 x i32> @pow2_rotl_vec(<2 x i32> %y) {
+entry:
+  %d = call <2 x i32> @llvm.fshl.v2i32(
+        <2 x i32> <i32 1048576, i32 1048576>,
+        <2 x i32> <i32 1048576, i32 1048576>,
+        <2 x i32> %y)
+
+ ;%and = and <2 x i32> %x, %d
+  ;%cmp = icmp ne <2 x i32> %and, zeroinitializer
+ ;ret <2 x i1> %cmp
+ ret <2 x i32> %d
+}
+
+
+define i32 @pow2_rotl(i32 %x, i32 %y) {
+ %d = call i32 @llvm.fshl.i32(i32 1048576, i32 1048576, i32 %y)
+ ret i32  %d
+}
+
+
diff --git a/rotl.s b/rotl.s
new file mode 100644
index 0000000000000..b0b62877dd773
--- /dev/null
+++ b/rotl.s
@@ -0,0 +1,39 @@
+	.text
+	.file	"rotl.ll"
+	.section	.rodata.cst4,"aM", at progbits,4
+	.p2align	2, 0x0                          # -- Begin function pow2_rotl_vec
+.LCPI0_0:
+	.long	1048576                         # 0x100000
+	.text
+	.globl	pow2_rotl_vec
+	.p2align	4, 0x90
+	.type	pow2_rotl_vec, at function
+pow2_rotl_vec:                          # @pow2_rotl_vec
+	.cfi_startproc
+# %bb.0:                                # %entry
+                                        # kill: def $xmm0 killed $xmm0 def $zmm0
+	vpbroadcastd	.LCPI0_0(%rip), %xmm1   # xmm1 = [1048576,1048576,1048576,1048576]
+	vprolvd	%zmm0, %zmm1, %zmm0
+                                        # kill: def $xmm0 killed $xmm0 killed $zmm0
+	vzeroupper
+	retq
+.Lfunc_end0:
+	.size	pow2_rotl_vec, .Lfunc_end0-pow2_rotl_vec
+	.cfi_endproc
+                                        # -- End function
+	.globl	pow2_rotl                       # -- Begin function pow2_rotl
+	.p2align	4, 0x90
+	.type	pow2_rotl, at function
+pow2_rotl:                              # @pow2_rotl
+	.cfi_startproc
+# %bb.0:
+	movl	%esi, %ecx
+	movl	$1048576, %eax                  # imm = 0x100000
+                                        # kill: def $cl killed $cl killed $ecx
+	roll	%cl, %eax
+	retq
+.Lfunc_end1:
+	.size	pow2_rotl, .Lfunc_end1-pow2_rotl
+	.cfi_endproc
+                                        # -- End function
+	.section	".note.GNU-stack","", at progbits



More information about the llvm-commits mailing list