[PATCH] D154333: [CSKY] Optimize multiplication with immediates

Ben Shi via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 3 03:35:10 PDT 2023


benshi001 created this revision.
benshi001 added a reviewer: zixuan-wu.
Herald added a subscriber: hiraditya.
Herald added a project: All.
benshi001 requested review of this revision.
Herald added subscribers: llvm-commits, jacquesguan.
Herald added a project: LLVM.

Optimize "Rx * imm" for specific immediates to
([IXH32|IXW32|IXD32] (LSLI Rx, shift), Rx).


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D154333

Files:
  llvm/lib/Target/CSKY/CSKYISelLowering.cpp
  llvm/test/CodeGen/CSKY/mul-imm.ll


Index: llvm/test/CodeGen/CSKY/mul-imm.ll
===================================================================
--- llvm/test/CodeGen/CSKY/mul-imm.ll
+++ llvm/test/CodeGen/CSKY/mul-imm.ll
@@ -1,5 +1,5 @@
 ; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 2
-; RUN: llc -verify-machineinstrs -csky-no-aliases -mattr=+e2 < %s -mtriple=csky | FileCheck %s
+; RUN: llc -verify-machineinstrs -csky-no-aliases -mattr=+2e3 < %s -mtriple=csky | FileCheck %s
 ; RUN: llc -verify-machineinstrs -csky-no-aliases < %s -mtriple=csky | FileCheck %s --check-prefix=GENERIC
 
 ;; This file shows if a multiplication can be simplified to an addition/subtraction
@@ -71,9 +71,8 @@
 define i32 @mul_i32_131074(i32 %x) {
 ; CHECK-LABEL: mul_i32_131074:
 ; CHECK:       # %bb.0: # %entry
-; CHECK-NEXT:    movih32 a1, 2
-; CHECK-NEXT:    ori32 a1, a1, 2
-; CHECK-NEXT:    mult16 a0, a1
+; CHECK-NEXT:    lsli16 a1, a0, 17
+; CHECK-NEXT:    ixh32 a0, a1, a0
 ; CHECK-NEXT:    rts16
 ;
 ; GENERIC-LABEL: mul_i32_131074:
@@ -105,9 +104,8 @@
 define i32 @mul_i32_131076(i32 %x) {
 ; CHECK-LABEL: mul_i32_131076:
 ; CHECK:       # %bb.0: # %entry
-; CHECK-NEXT:    movih32 a1, 2
-; CHECK-NEXT:    ori32 a1, a1, 4
-; CHECK-NEXT:    mult16 a0, a1
+; CHECK-NEXT:    lsli16 a1, a0, 17
+; CHECK-NEXT:    ixw32 a0, a1, a0
 ; CHECK-NEXT:    rts16
 ;
 ; GENERIC-LABEL: mul_i32_131076:
@@ -135,9 +133,8 @@
 define i32 @mul_i32_131080(i32 %x) {
 ; CHECK-LABEL: mul_i32_131080:
 ; CHECK:       # %bb.0: # %entry
-; CHECK-NEXT:    movih32 a1, 2
-; CHECK-NEXT:    ori32 a1, a1, 8
-; CHECK-NEXT:    mult16 a0, a1
+; CHECK-NEXT:    lsli16 a1, a0, 17
+; CHECK-NEXT:    ixd32 a0, a1, a0
 ; CHECK-NEXT:    rts16
 ;
 ; GENERIC-LABEL: mul_i32_131080:
@@ -369,10 +366,9 @@
 ; CHECK-NEXT:    st32.w lr, (sp, 0) # 4-byte Folded Spill
 ; CHECK-NEXT:    .cfi_offset lr, -4
 ; CHECK-NEXT:    .cfi_def_cfa_offset 4
-; CHECK-NEXT:    lrw32 t0, [.LCPI15_0]
 ; CHECK-NEXT:    movi32 a2, 4097
 ; CHECK-NEXT:    movi16 a3, 0
-; CHECK-NEXT:    jsr16 t0
+; CHECK-NEXT:    jsri32 [.LCPI15_0]
 ; CHECK-NEXT:    ld32.w lr, (sp, 0) # 4-byte Folded Reload
 ; CHECK-NEXT:    addi16 sp, sp, 4
 ; CHECK-NEXT:    rts16
Index: llvm/lib/Target/CSKY/CSKYISelLowering.cpp
===================================================================
--- llvm/lib/Target/CSKY/CSKYISelLowering.cpp
+++ llvm/lib/Target/CSKY/CSKYISelLowering.cpp
@@ -1397,6 +1397,13 @@
     // unchanged on sub targets with MULT32, since not sure it is better.
     if (!Subtarget.hasE2() && (-1 - Imm).isPowerOf2())
       return true;
+    // Break (MULT x, imm) to ([IXH32|IXW32|IXD32] (LSLI32 x, i0), x) when
+    // imm=(1<<i0)+[2|4|8] and imm has to be composed via a MOVIH32/ORI32 pair.
+    if (Imm.ugt(0xffff) && ((Imm - 2).isPowerOf2() || (Imm - 4).isPowerOf2()) &&
+        Subtarget.hasE2())
+      return true;
+    if (Imm.ugt(0xffff) && (Imm - 8).isPowerOf2() && Subtarget.has2E3())
+       return true;
   }
 
   return false;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D154333.536708.patch
Type: text/x-patch
Size: 2984 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230703/aca18969/attachment.bin>


More information about the llvm-commits mailing list