================
@@ -9020,6 +9021,136 @@ static SDValue performVSELECTCombine(SDNode *N, 
SelectionDAG &DAG,
                      DL, VT, X, Shift);
 }
 
+static SDValue performMULCombine(SDNode *N, SelectionDAG &DAG,
+                                 TargetLowering::DAGCombinerInfo &DCI,
+                                 const LoongArchSubtarget &Subtarget) {
+  if (!DCI.isBeforeLegalize())
+    return SDValue();
+
+  SDLoc DL(N);
+  EVT ResTy = N->getValueType(0);
+  SDValue N0 = N->getOperand(0);
+  SDValue N1 = N->getOperand(1);
+
+  // Note: v2i128 is an unsupported MVT vector type (see
+  // MachineValueType.h::getVectorVT()), use NumElements and SizeInBits to
+  // identify it.
+  bool HasLSXOnly = Subtarget.hasExtLSX() && !Subtarget.hasExtLASX();
+  bool Isv2i128 = ResTy.isVector() && ResTy.getVectorNumElements() == 2 &&
+                  ResTy.getScalarSizeInBits() == 128;
+  if (ResTy != MVT::v8i16 && ResTy != MVT::v4i32 && ResTy != MVT::v2i64 &&
+      ResTy != MVT::i128 && ResTy != MVT::v16i16 && ResTy != MVT::v8i32 &&
+      ResTy != MVT::v4i64 && !Isv2i128)
+    return SDValue();
+  if (HasLSXOnly && (ResTy.is256BitVector() || Isv2i128))
+    return SDValue();
+
+  // Combine:
+  //   ti,tii,...,tx = extract_vector_elt t0, {0,2,4,.../1,3,5,...}
+  //   tj,tjj,...,ty = extract_vector_elt t1, {0,2,4,.../1,3,5,...}
+  //   tm = BUILD_VECTOR ti,tii,...,tx (Only when ResTy != MVT::i128)
+  //   tn = BUILD_VECTOR tj,tjj,...,ty (Only when ResTy != MVT::i128)
+  //   ta = {sign/zero}_extend tm
+  //   tb = {sign/zero}_extend tn
+  //   tr = mul ta, tb
+  // to:
+  //   tr = VMULW{EV/OD}[U/US] t0, t1
+  auto getExtType = [](unsigned Op0, unsigned Op1) -> int {
+    if (Op0 == ISD::SIGN_EXTEND && Op1 == ISD::SIGN_EXTEND)
+      return 0;
+    if (Op0 == ISD::ZERO_EXTEND && Op1 == ISD::ZERO_EXTEND)
+      return 1;
+    if (Op0 == ISD::ZERO_EXTEND && Op1 == ISD::SIGN_EXTEND)
+      return 2;
+    if (Op0 == ISD::SIGN_EXTEND && Op1 == ISD::ZERO_EXTEND)
+      return 3;
+    return -1;
+  };
+
+  int ExtType = getExtType(N0.getOpcode(), N1.getOpcode());
+  if (ExtType < 0)
+    return SDValue();
+
+  SDValue Src0 = N0.getOperand(0);
+  SDValue Src1 = N1.getOperand(0);
+  bool IsScalar = (ResTy == MVT::i128);
----------------
heiher wrote:

This path fires even when the i128 product is only used in GPRs:

```
define i128 @f(<2 x i64> %a, <2 x i64> %b) nounwind {
  %x = extractelement <2 x i64> %a, i32 0
  %y = extractelement <2 x i64> %b, i32 0
  %sx = sext i64 %x to i128
  %sy = sext i64 %y to i128
  %m = mul i128 %sx, %sy
  ret i128 %m
}
```

```asm
# before                        # after
vpickve2gr.d $a1, $vr0, 0       vmulwev.q.d  $vr0, $vr0, $vr1
vpickve2gr.d $a2, $vr1, 0       vpickve2gr.d $a0, $vr0, 0
mul.d        $a0, $a1, $a2      vpickve2gr.d $a1, $vr0, 1
mulh.d       $a1, $a1, $a2
```

vmulwev.q.d has 7-cycle latency, against 4 for mul.d/mulh.d. Microbenchmarks on 
a 3A6000 at 2.5 GHz, with the product accumulated as an i128:

|                                                  | before       | after       
              |
|--------------------------------------------------|--------------|---------------------------|
| Latency (result fed back into the next multiply) | 4.46 ns/iter | 5.63 
ns/iter (26% slower) |
| Throughput (independent multiplies)              | 1.20 ns/iter | 1.04 
ns/iter (14% faster) |


https://github.com/llvm/llvm-project/pull/161368
_______________________________________________
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits

Reply via email to