[PATCH] D156832: [GlobalISel] Handle trunc(sext x) in artifact combiner

Vladislav Dzhidzhoev via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 1 15:41:28 PDT 2023


dzhidzhoev created this revision.
dzhidzhoev added reviewers: arsenm, aemerson, dsanders, gargaroff, Petar.Avramovic.
Herald added subscribers: kerbowa, jvesely.
Herald added a project: All.
dzhidzhoev requested review of this revision.
Herald added subscribers: llvm-commits, wdng.
Herald added a project: LLVM.

trunc(sext x) -> x pattern is handled in artifact combiner to avoid
extra copy instructions in https://reviews.llvm.org/D156831.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D156832

Files:
  llvm/include/llvm/CodeGen/GlobalISel/LegalizationArtifactCombiner.h
  llvm/test/CodeGen/AMDGPU/GlobalISel/artifact-combiner-trunc.mir


Index: llvm/test/CodeGen/AMDGPU/GlobalISel/artifact-combiner-trunc.mir
===================================================================
--- llvm/test/CodeGen/AMDGPU/GlobalISel/artifact-combiner-trunc.mir
+++ llvm/test/CodeGen/AMDGPU/GlobalISel/artifact-combiner-trunc.mir
@@ -145,3 +145,18 @@
     %2:_(s32) = G_TRUNC %1
     $vgpr0 = COPY %2
 ...
+
+---
+name: trunc_sext
+
+body: |
+  bb.0:
+    ; Test that trunc(sext) is replaced with sext source.
+    ; CHECK-LABEL: name: trunc_sext
+    ; CHECK: [[DEF:%[0-9]+]]:_(s32) = G_IMPLICIT_DEF
+    ; CHECK-NEXT: $vgpr0 = COPY [[DEF]](s32)
+    %0:_(s32) = G_IMPLICIT_DEF
+    %1:_(s64) = G_SEXT %0
+    %2:_(s32) = G_TRUNC %1
+    $vgpr0 = COPY %2
+...
Index: llvm/include/llvm/CodeGen/GlobalISel/LegalizationArtifactCombiner.h
===================================================================
--- llvm/include/llvm/CodeGen/GlobalISel/LegalizationArtifactCombiner.h
+++ llvm/include/llvm/CodeGen/GlobalISel/LegalizationArtifactCombiner.h
@@ -325,6 +325,21 @@
       return true;
     }
 
+    // trunc(sext x) -> x
+    Register SextSrc;
+    if (mi_match(SrcReg, MRI, m_GSExt(m_Reg(SextSrc)))) {
+      LLT DstTy = MRI.getType(DstReg);
+      LLT SextSrcTy = MRI.getType(SextSrc);
+      if (DstTy == SextSrcTy) {
+        LLVM_DEBUG(dbgs() << ".. Combine MI: " << MI;);
+
+        replaceRegOrBuildCopy(DstReg, SextSrc, MRI, Builder, UpdatedDefs, Observer);
+        UpdatedDefs.push_back(DstReg);
+        markInstAndDefDead(MI, *MRI.getVRegDef(SrcReg), DeadInsts);
+        return true;
+      }
+    }
+
     return false;
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D156832.546250.patch
Type: text/x-patch
Size: 1590 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230801/569fe152/attachment.bin>


More information about the llvm-commits mailing list