[PATCH] D137844: [DAG] Fold zext/sext into masked loads with multiple truncate uses

Benjamin Maxwell via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 11 07:33:37 PST 2022


benmxwl-arm created this revision.
benmxwl-arm added reviewers: peterwaller-arm, dtemirbulatov, MattDevereau, c-rhodes.
Herald added subscribers: ctetreau, ecnelises, hiraditya, kristof.beyls.
Herald added a project: All.
benmxwl-arm requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

This adjusts tryToFoldExtOfMaskedLoad() to allow folding the
sign/zero extensions into masked loads with multiple users
(rather than just one), so long as all the users are simple
truncates to types smaller than the initial load.

Doing this prevents some redundant mov and and instructions
being emitted for some SVE code on AArch64, due to the zero
extension information being lost.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D137844

Files:
  llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
  llvm/test/CodeGen/AArch64/sve-load-compare-store.ll
  llvm/unnecessary_inversion.isel.txt


Index: llvm/unnecessary_inversion.isel.txt
===================================================================
--- /dev/null
+++ llvm/unnecessary_inversion.isel.txt
@@ -0,0 +1 @@
+/home/benmax01/git/llvm-project/build/bin/llc: error: /home/benmax01/git/llvm-project/build/bin/llc: unnecessary_inversion.ll: error: Could not open input file: No such file or directory
Index: llvm/test/CodeGen/AArch64/sve-load-compare-store.ll
===================================================================
--- llvm/test/CodeGen/AArch64/sve-load-compare-store.ll
+++ llvm/test/CodeGen/AArch64/sve-load-compare-store.ll
@@ -6,9 +6,7 @@
 ; CHECK:       // %bb.0: // %entry
 ; CHECK-NEXT:    ptrue p0.b
 ; CHECK-NEXT:    ld1h { z0.s }, p0/z, [x0]
-; CHECK-NEXT:    mov z1.d, z0.d
-; CHECK-NEXT:    and z1.s, z1.s, #0xffff
-; CHECK-NEXT:    cmphs p0.s, p0/z, z1.s, #0
+; CHECK-NEXT:    cmphs p0.s, p0/z, z0.s, #0
 ; CHECK-NEXT:    st1b { z0.s }, p0, [x1]
 ; CHECK-NEXT:    ret
 entry:
Index: llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
===================================================================
--- llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -12097,13 +12097,29 @@
                                         SDNode *N, SDValue N0,
                                         ISD::LoadExtType ExtLoadType,
                                         ISD::NodeType ExtOpc) {
-  if (!N0.hasOneUse())
-    return SDValue();
 
   MaskedLoadSDNode *Ld = dyn_cast<MaskedLoadSDNode>(N0);
   if (!Ld || Ld->getExtensionType() != ISD::NON_EXTLOAD)
     return SDValue();
 
+  auto AllUsesCanBeReplaced = [&](SDValue V) {
+    for (SDNode::use_iterator UI = V->use_begin(), UE = V->use_end(); UI != UE;
+         ++UI) {
+      SDNode *User = *UI;
+      // Skip chain uses and the extension dag node N
+      if (UI.getUse().getResNo() != 0 || User == N)
+        continue;
+      // If all other uses of the load are just truncates we're fine to just
+      // truncate the extended type.
+      if (User->getOpcode() != ISD::TRUNCATE)
+        return false;
+    }
+    return true;
+  };
+
+  if (!AllUsesCanBeReplaced(N0))
+    return SDValue();
+
   if (!TLI.isLoadExtLegalOrCustom(ExtLoadType, VT, Ld->getValueType(0)))
     return SDValue();
 
@@ -12630,9 +12646,8 @@
                              ISD::ZEXTLOAD, ISD::ZERO_EXTEND))
     return foldedExt;
 
-  if (SDValue foldedExt =
-      tryToFoldExtOfMaskedLoad(DAG, TLI, VT, N, N0, ISD::ZEXTLOAD,
-                               ISD::ZERO_EXTEND))
+  if (SDValue foldedExt = tryToFoldExtOfMaskedLoad(
+          DAG, TLI, VT, N, N0, ISD::ZEXTLOAD, ISD::ZERO_EXTEND))
     return foldedExt;
 
   // fold (zext (load x)) to multiple smaller zextloads.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D137844.474764.patch
Type: text/x-patch
Size: 2744 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221111/eb44c208/attachment.bin>


More information about the llvm-commits mailing list