| 1 |
/*========================== begin_copyright_notice ============================ |
| 2 |
|
| 3 |
Copyright (C) 2025 Intel Corporation |
| 4 |
|
| 5 |
SPDX-License-Identifier: MIT |
| 6 |
|
| 7 |
============================= end_copyright_notice ===========================*/ |
| 8 |
|
| 9 |
/*========================== begin_copyright_notice ============================ |
| 10 |
|
| 11 |
Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 12 |
See https://llvm.org/LICENSE.txt for license information. |
| 13 |
SPDX-License-Identifier: Apache-2.0 with LLVM-exception |
| 14 |
|
| 15 |
============================= end_copyright_notice ===========================*/ |
| 16 |
|
| 17 |
From a1cd60f3f84be880c71dc84e5b4a106d7bc0f532 Mon Sep 17 00:00:00 2001 |
| 18 |
From: Paulo Matos <[email protected]> |
| 19 |
Date: Thu, 1 Jun 2023 15:27:33 +0200 |
| 20 |
Subject: [PATCH] [InstCombine] Disable generation of fshl/fshr for rotates |
| 21 |
|
| 22 |
Disable conversion of funnel shifts (fshl/fshr) into rotates |
| 23 |
unless one of the operands is known to be a constant value. |
| 24 |
|
| 25 |
Reviewed By: nikic |
| 26 |
|
| 27 |
Differential Revision: https://reviews.llvm.org/D150670 |
| 28 |
--- |
| 29 |
.../InstCombineSimplifyDemanded.cpp | 23 ++++++++++++++++--- |
| 30 |
1 file changed, 20 insertions(+), 3 deletions(-) |
| 31 |
|
| 32 |
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp |
| 33 |
index 77d675422966..dca00e897693 100644 |
| 34 |
--- a/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp |
| 35 |
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp |
| 36 |
@@ -935,9 +935,26 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Value *V, APInt DemandedMask, |
| 37 |
|
| 38 |
APInt DemandedMaskLHS(DemandedMask.lshr(ShiftAmt)); |
| 39 |
APInt DemandedMaskRHS(DemandedMask.shl(BitWidth - ShiftAmt)); |
| 40 |
- if (SimplifyDemandedBits(I, 0, DemandedMaskLHS, LHSKnown, Depth + 1) || |
| 41 |
- SimplifyDemandedBits(I, 1, DemandedMaskRHS, RHSKnown, Depth + 1)) |
| 42 |
- return I; |
| 43 |
+ if (I->getOperand(0) != I->getOperand(1)) { |
| 44 |
+ if (SimplifyDemandedBits(I, 0, DemandedMaskLHS, LHSKnown, |
| 45 |
+ Depth + 1) || |
| 46 |
+ SimplifyDemandedBits(I, 1, DemandedMaskRHS, RHSKnown, Depth + 1)) |
| 47 |
+ return I; |
| 48 |
+ } else { // fshl is a rotate |
| 49 |
+ // Avoid converting rotate into funnel shift. |
| 50 |
+ // Only simplify if one operand is constant. |
| 51 |
+ KnownBits LHSKnown = computeKnownBits(I->getOperand(0), Depth + 1, I); |
| 52 |
+ if (DemandedMaskLHS.isSubsetOf(LHSKnown.Zero | LHSKnown.One)) { |
| 53 |
+ replaceOperand(*I, 0, Constant::getIntegerValue(VTy, LHSKnown.One)); |
| 54 |
+ return I; |
| 55 |
+ } |
| 56 |
+ |
| 57 |
+ KnownBits RHSKnown = computeKnownBits(I->getOperand(1), Depth + 1, I); |
| 58 |
+ if (DemandedMaskRHS.isSubsetOf(RHSKnown.Zero | RHSKnown.One)) { |
| 59 |
+ replaceOperand(*I, 1, Constant::getIntegerValue(VTy, RHSKnown.One)); |
| 60 |
+ return I; |
| 61 |
+ } |
| 62 |
+ } |
| 63 |
|
| 64 |
Known.Zero = LHSKnown.Zero.shl(ShiftAmt) | |
| 65 |
RHSKnown.Zero.lshr(BitWidth - ShiftAmt); |
| 66 |
-- |
| 67 |
2.43.0 |
| 68 |
|