[PATCH] D136014: [AArch64] Improve codegen for shifted mask op

chenglin.bi via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Oct 15 03:05:39 PDT 2022


bcl5980 created this revision.
bcl5980 added reviewers: dmgreen, efriedma, spatel, paulwalker-arm.
Herald added subscribers: hiraditya, kristof.beyls.
Herald added a project: All.
bcl5980 requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

The special case for bit extraction pattern is  `((x >> C) & mask) << C`. 
It can be combined to `x & (mask << C)` by return true in isDesirableToCommuteWithShift.


https://reviews.llvm.org/D136014

Files:
  llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
  llvm/test/CodeGen/AArch64/pr56427.ll


Index: llvm/test/CodeGen/AArch64/pr56427.ll
===================================================================
--- /dev/null
+++ llvm/test/CodeGen/AArch64/pr56427.ll
@@ -0,0 +1,13 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
+; RUN: llc < %s -mtriple=aarch64-none-linux-gnu | FileCheck %s
+
+define i64 @src(i64 %x) {
+; CHECK-LABEL: src:
+; CHECK:       // %bb.0:
+; CHECK-NEXT:    and x0, x0, #0x7fff8
+; CHECK-NEXT:    ret
+  %s1 = lshr i64 %x, 3
+  %a = and i64 %s1, 65535
+  %s2 = shl i64 %a, 3
+  ret i64 %s2
+}
Index: llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
===================================================================
--- llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
+++ llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
@@ -14381,15 +14381,24 @@
   SDValue ShiftLHS = N->getOperand(0);
   EVT VT = N->getValueType(0);
 
-  // If ShiftLHS is unsigned bit extraction: ((x >> C) & mask), then do not combine
-  // it with shift 'N' to let it be lowered to UBFX.
+  // If ShiftLHS is unsigned bit extraction: ((x >> C) & mask), then do not
+  // combine it with shift 'N' to let it be lowered to UBFX.
+  // The special case is ((x >> C) & mask) << C.
+  // It can be combine to x & (mask << C) by return true
   if (ShiftLHS.getOpcode() == ISD::AND && (VT == MVT::i32 || VT == MVT::i64) &&
       isa<ConstantSDNode>(ShiftLHS.getOperand(1))) {
     uint64_t TruncMask = ShiftLHS.getConstantOperandVal(1);
-    if (isMask_64(TruncMask) &&
-        ShiftLHS.getOperand(0).getOpcode() == ISD::SRL &&
-        isa<ConstantSDNode>(ShiftLHS.getOperand(0).getOperand(1)))
-      return false;
+    if (isMask_64(TruncMask)) {
+      SDValue AndLHS = ShiftLHS.getOperand(0);
+      if (AndLHS.getOpcode() == ISD::SRL) {
+        if (auto *SRLC = dyn_cast<ConstantSDNode>(AndLHS.getOperand(1))) {
+          if (N->getOpcode() == ISD::SHL)
+            if (auto *SHLC = dyn_cast<ConstantSDNode>(N->getOperand(1)))
+              return SRLC->getAPIntValue() == SHLC->getAPIntValue();
+          return false;
+        }
+      }
+    }
   }
   return true;
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D136014.468003.patch
Type: text/x-patch
Size: 2111 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221015/af1fa9af/attachment.bin>


More information about the llvm-commits mailing list