[PATCH] D146032: [DAG] Fold (bitcast (logicop (bitcast x), (c))) -> (and x, (bitcast c)) iff the current logicop type is illegal

Simon Pilgrim via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 14 04:03:51 PDT 2023


RKSimon created this revision.
RKSimon added reviewers: arsenm, foad, spatel.
Herald added subscribers: kosarev, StephenFan, ecnelises, kerbowa, hiraditya, jvesely.
Herald added a project: All.
RKSimon requested review of this revision.
Herald added a subscriber: wdng.
Herald added a project: LLVM.

Try to remove extra bitcasts around logicops if we're dealing with illegal types

Fixes the regressions in D145939 <https://reviews.llvm.org/D145939>


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D146032

Files:
  llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
  llvm/test/CodeGen/AMDGPU/fneg.ll


Index: llvm/test/CodeGen/AMDGPU/fneg.ll
===================================================================
--- llvm/test/CodeGen/AMDGPU/fneg.ll
+++ llvm/test/CodeGen/AMDGPU/fneg.ll
@@ -218,13 +218,8 @@
   ret half %fadd
 }
 
-; FIXME: This is terrible
 ; FUNC-LABEL: {{^}}s_fneg_v2i16:
-; SI: s_and_b32 s5, s4, 0xffff0000
-; SI: s_xor_b32 s4, s4, 0x8000
-; SI: s_and_b32 s4, s4, 0xffff
-; SI: s_or_b32 s4, s4, s5
-; SI: s_add_i32 s4, s4, 0x80000000
+; SI: s_xor_b32 s4, s4, 0x80008000
 
 ; VI: s_lshr_b32 s5, s4, 16
 ; VI: s_xor_b32 s4, s4, 0x8000
Index: llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
===================================================================
--- llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -14668,6 +14668,22 @@
   if (N0.getOpcode() == ISD::BITCAST)
     return DAG.getBitcast(VT, N0.getOperand(0));
 
+  // fold (conv (and (conv x), (c))) -> (and x, (conv c))
+  // iff the current bitwise op type isn't legal
+  if (ISD::isBitwiseLogicOp(N0.getOpcode()) && VT.isInteger() &&
+      !TLI.isTypeLegal(N0.getOperand(0).getValueType())) {
+    auto IsFreeBitcast = [VT](SDValue V) {
+      return (V.getOpcode() == ISD::BITCAST &&
+              V.getOperand(0).getValueType() == VT) ||
+             (ISD::isBuildVectorOfConstantSDNodes(V.getNode()) &&
+              V->hasOneUse());
+    };
+    if (IsFreeBitcast(N0.getOperand(0)) && IsFreeBitcast(N0.getOperand(1)))
+      return DAG.getNode(N0.getOpcode(), SDLoc(N), VT,
+                         DAG.getBitcast(VT, N0.getOperand(0)),
+                         DAG.getBitcast(VT, N0.getOperand(1)));
+  }
+
   // fold (conv (load x)) -> (load (conv*)x)
   // If the resultant load doesn't need a higher alignment than the original!
   if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D146032.505018.patch
Type: text/x-patch
Size: 1830 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230314/7b82b328/attachment.bin>


More information about the llvm-commits mailing list