[llvm] 85b77b1 - [GlobalISel][X86] Add G_IMPLICIT_DEF / G_CONSTANT legalization handling

Simon Pilgrim via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 6 03:45:40 PDT 2023


Author: Simon Pilgrim
Date: 2023-06-06T11:45:22+01:00
New Revision: 85b77b13e3bcccffeb84b09365e0ab96565467fa

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

LOG: [GlobalISel][X86] Add G_IMPLICIT_DEF / G_CONSTANT legalization handling

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/X86/X86LegalizerInfo.cpp b/llvm/lib/Target/X86/X86LegalizerInfo.cpp
index 86e014225a5f7..ed4f74e50d421 100644
--- a/llvm/lib/Target/X86/X86LegalizerInfo.cpp
+++ b/llvm/lib/Target/X86/X86LegalizerInfo.cpp
@@ -95,6 +95,24 @@ X86LegalizerInfo::X86LegalizerInfo(const X86Subtarget &STI,
   const LLT v16s32 = LLT::fixed_vector(16, 32);
   const LLT v8s64 = LLT::fixed_vector(8, 64);
 
+  // implicit/constants
+  getActionDefinitionsBuilder(G_IMPLICIT_DEF)
+      .legalIf([=](const LegalityQuery &Query) -> bool {
+        // 32/64-bits needs support for s64/s128 to handle cases:
+        // s64 = EXTEND (G_IMPLICIT_DEF s32) -> s64 = G_IMPLICIT_DEF
+        // s128 = EXTEND (G_IMPLICIT_DEF s32/s64) -> s128 = G_IMPLICIT_DEF
+        return typeInSet(0, {p0, s1, s8, s16, s32, s64})(Query) ||
+               (Is64Bit && typeInSet(0, {s128})(Query));
+      });
+
+  getActionDefinitionsBuilder(G_CONSTANT)
+      .legalIf([=](const LegalityQuery &Query) -> bool {
+        return typeInSet(0, {p0, s8, s16, s32})(Query) ||
+               (Is64Bit && typeInSet(0, {s64})(Query));
+      })
+      .widenScalarToNextPow2(0, /*Min=*/8)
+      .clampScalar(0, s8, sMaxScalar);
+
   // integer addition/subtraction
   getActionDefinitionsBuilder({G_ADD, G_SUB})
       .legalIf([=](const LegalityQuery &Query) -> bool {
@@ -292,9 +310,6 @@ X86LegalizerInfo::X86LegalizerInfo(const X86Subtarget &STI,
   LegacyInfo.setLegalizeScalarToDifferentSizeStrategy(
       G_PTR_ADD, 1,
       LegacyLegalizerInfo::widenToLargerTypesUnsupportedOtherwise);
-  LegacyInfo.setLegalizeScalarToDifferentSizeStrategy(
-      G_CONSTANT, 0,
-      LegacyLegalizerInfo::widenToLargerTypesAndNarrowToLargest);
 
   getActionDefinitionsBuilder({G_MEMCPY, G_MEMMOVE, G_MEMSET}).libcall();
 
@@ -318,9 +333,6 @@ void X86LegalizerInfo::setLegalizerInfo32bit() {
 
   auto &LegacyInfo = getLegacyLegalizerInfo();
 
-  for (auto Ty : {p0, s1, s8, s16, s32})
-    LegacyInfo.setAction({G_IMPLICIT_DEF, Ty}, LegacyLegalizeActions::Legal);
-
   for (auto Ty : {s8, s16, s32, p0})
     LegacyInfo.setAction({G_PHI, Ty}, LegacyLegalizeActions::Legal);
 
@@ -347,11 +359,6 @@ void X86LegalizerInfo::setLegalizerInfo32bit() {
   // Control-flow
   LegacyInfo.setAction({G_BRCOND, s1}, LegacyLegalizeActions::Legal);
 
-  // Constants
-  for (auto Ty : {s8, s16, s32, p0})
-    LegacyInfo.setAction({TargetOpcode::G_CONSTANT, Ty},
-                         LegacyLegalizeActions::Legal);
-
   getActionDefinitionsBuilder(G_SEXT_INREG).lower();
 
   // Merge/Unmerge
@@ -378,11 +385,6 @@ void X86LegalizerInfo::setLegalizerInfo64bit() {
 
   auto &LegacyInfo = getLegacyLegalizerInfo();
 
-  LegacyInfo.setAction({G_IMPLICIT_DEF, s64}, LegacyLegalizeActions::Legal);
-  // Need to have that, as tryFoldImplicitDef will create this pattern:
-  // s128 = EXTEND (G_IMPLICIT_DEF s32/s64) -> s128 = G_IMPLICIT_DEF
-  LegacyInfo.setAction({G_IMPLICIT_DEF, s128}, LegacyLegalizeActions::Legal);
-
   LegacyInfo.setAction({G_PHI, s64}, LegacyLegalizeActions::Legal);
 
   for (unsigned MemOp : {G_LOAD, G_STORE})
@@ -391,10 +393,6 @@ void X86LegalizerInfo::setLegalizerInfo64bit() {
   // Pointer-handling
   LegacyInfo.setAction({G_PTR_ADD, 1, s64}, LegacyLegalizeActions::Legal);
 
-  // Constants
-  LegacyInfo.setAction({TargetOpcode::G_CONSTANT, s64},
-                       LegacyLegalizeActions::Legal);
-
   getActionDefinitionsBuilder(G_SITOFP)
     .legalForCartesianProduct({s32, s64})
       .clampScalar(1, s32, s64)

diff  --git a/llvm/test/CodeGen/X86/GlobalISel/legalize-undef.mir b/llvm/test/CodeGen/X86/GlobalISel/legalize-undef.mir
index b295fce450059..37028db243d15 100644
--- a/llvm/test/CodeGen/X86/GlobalISel/legalize-undef.mir
+++ b/llvm/test/CodeGen/X86/GlobalISel/legalize-undef.mir
@@ -31,10 +31,12 @@ body: |
     ; X32-NEXT: G_STORE [[DEF2]](s16), [[DEF]](p0) :: (store (s16))
     ; X32-NEXT: [[DEF3:%[0-9]+]]:_(s32) = G_IMPLICIT_DEF
     ; X32-NEXT: G_STORE [[DEF3]](s32), [[DEF]](p0) :: (store (s32))
-    ; X32-NEXT: G_STORE [[DEF3]](s32), [[DEF]](p0) :: (store (s32), align 8)
+    ; X32-NEXT: [[DEF4:%[0-9]+]]:_(s64) = G_IMPLICIT_DEF
+    ; X32-NEXT: [[UV:%[0-9]+]]:_(s32), [[UV1:%[0-9]+]]:_(s32) = G_UNMERGE_VALUES [[DEF4]](s64)
+    ; X32-NEXT: G_STORE [[UV]](s32), [[DEF]](p0) :: (store (s32), align 8)
     ; X32-NEXT: [[C1:%[0-9]+]]:_(s32) = G_CONSTANT i32 4
     ; X32-NEXT: [[PTR_ADD:%[0-9]+]]:_(p0) = G_PTR_ADD [[DEF]], [[C1]](s32)
-    ; X32-NEXT: G_STORE [[DEF3]](s32), [[PTR_ADD]](p0) :: (store (s32) into unknown-address + 4)
+    ; X32-NEXT: G_STORE [[UV1]](s32), [[PTR_ADD]](p0) :: (store (s32) into unknown-address + 4)
     %5:_(p0) = G_IMPLICIT_DEF
     %0:_(s1) = G_IMPLICIT_DEF
     G_STORE %0, %5 ::(store (s1))


        


More information about the llvm-commits mailing list