[llvm] bc713b1 - [GlobalIsel][X86] fix legalization of G_CTLZ and G_CTPOP

Thorsten Schütt via llvm-commits llvm-commits at lists.llvm.org
Thu May 25 07:41:21 PDT 2023


Author: Thorsten Schütt
Date: 2023-05-25T16:41:14+02:00
New Revision: bc713b193fecd1566a333dc74dfbf5bc04cbc36e

URL: https://github.com/llvm/llvm-project/commit/bc713b193fecd1566a333dc74dfbf5bc04cbc36e
DIFF: https://github.com/llvm/llvm-project/commit/bc713b193fecd1566a333dc74dfbf5bc04cbc36e.diff

LOG: [GlobalIsel][X86] fix legalization of G_CTLZ and G_CTPOP

Note that the builders are protected by is64Bit().

More fine-grained availibility checks.

Reviewed By: RKSimon

Differential Revision: https://reviews.llvm.org/D150790

Added: 
    

Modified: 
    llvm/lib/Target/X86/X86LegalizerInfo.cpp
    llvm/test/CodeGen/X86/GlobalISel/legalize-ctpop.mir
    llvm/test/CodeGen/X86/GlobalISel/legalize-leading-zeros.mir

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/X86/X86LegalizerInfo.cpp b/llvm/lib/Target/X86/X86LegalizerInfo.cpp
index b5b700c4475d6..e583d22593f30 100644
--- a/llvm/lib/Target/X86/X86LegalizerInfo.cpp
+++ b/llvm/lib/Target/X86/X86LegalizerInfo.cpp
@@ -287,6 +287,23 @@ void X86LegalizerInfo::setLegalizerInfo64bit() {
                        LegacyLegalizeActions::Legal);
   LegacyInfo.setAction({G_MERGE_VALUES, 1, s128}, LegacyLegalizeActions::Legal);
   LegacyInfo.setAction({G_UNMERGE_VALUES, s128}, LegacyLegalizeActions::Legal);
+
+  if (Subtarget.hasPOPCNT()) {
+    // popcount
+    getActionDefinitionsBuilder(G_CTPOP)
+      .legalFor({{s16, s16}, {s32, s32}, {s64, s64}})
+      .widenScalarToNextPow2(1, /*Min=*/16)
+      .clampScalar(1, s16, s64);
+  }
+
+  if (Subtarget.hasLZCNT()) {
+    // count leading zeros (LZCNT)
+    getActionDefinitionsBuilder(G_CTLZ)
+      .legalFor({{s16, s16}, {s32, s32}, {s64, s64}})
+      .widenScalarToNextPow2(1, /*Min=*/16)
+      .clampScalar(1, s16, s64);
+  }
+
 }
 
 void X86LegalizerInfo::setLegalizerInfoSSE1() {
@@ -389,21 +406,6 @@ void X86LegalizerInfo::setLegalizerInfoSSE42() {
   if (!Subtarget.hasSSE42())
     return;
 
-  const LLT s16 = LLT::scalar(16);
-  const LLT s32 = LLT::scalar(32);
-  const LLT s64 = LLT::scalar(64);
-
-  // popcount
-  getActionDefinitionsBuilder(G_CTPOP)
-    .legalFor({{s16, s16}, {s32, s32}, {s64, s64}})
-    .widenScalarToNextPow2(1, /*Min=*/16)
-    .clampScalar(1, s16, s64);
-
-  // count leading zeros (LZCNT)
-  getActionDefinitionsBuilder(G_CTLZ)
-    .legalFor({{s16, s16}, {s32, s32}, {s64, s64}})
-    .widenScalarToNextPow2(1, /*Min=*/16)
-    .clampScalar(1, s16, s64);
 }
 
 void X86LegalizerInfo::setLegalizerInfoAVX() {

diff  --git a/llvm/test/CodeGen/X86/GlobalISel/legalize-ctpop.mir b/llvm/test/CodeGen/X86/GlobalISel/legalize-ctpop.mir
index ab3af64552a56..75ce295beed74 100644
--- a/llvm/test/CodeGen/X86/GlobalISel/legalize-ctpop.mir
+++ b/llvm/test/CodeGen/X86/GlobalISel/legalize-ctpop.mir
@@ -1,5 +1,5 @@
 # NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 2
-# RUN: llc -mtriple=x86_64-linux-gnu -mattr=+sse4.2 -run-pass=legalizer %s -o - | FileCheck %s
+# RUN: llc -mtriple=x86_64-linux-gnu -mattr=+popcnt -run-pass=legalizer %s -o - | FileCheck %s
 
 # test popcount for s16, s32, and s64
 

diff  --git a/llvm/test/CodeGen/X86/GlobalISel/legalize-leading-zeros.mir b/llvm/test/CodeGen/X86/GlobalISel/legalize-leading-zeros.mir
index 7e628482cc810..cb4bae122329c 100644
--- a/llvm/test/CodeGen/X86/GlobalISel/legalize-leading-zeros.mir
+++ b/llvm/test/CodeGen/X86/GlobalISel/legalize-leading-zeros.mir
@@ -1,5 +1,5 @@
 # NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 2
-# RUN: llc -mtriple=x86_64-linux-gnu -mattr=+sse4.2 -run-pass=legalizer %s -o - | FileCheck %s
+# RUN: llc -mtriple=x86_64-linux-gnu -mattr=+lzcnt -run-pass=legalizer %s -o - | FileCheck %s
 
 # test count leading zeros for s16, s32, and s64
 


        


More information about the llvm-commits mailing list