RISC-V: Add unary C/C++ API support
authorJu-Zhe Zhong <juzhe.zhong@rivai.ai>
Fri, 3 Feb 2023 23:21:15 +0000 (07:21 +0800)
committerKito Cheng <kito.cheng@sifive.com>
Sun, 12 Feb 2023 02:59:05 +0000 (10:59 +0800)
gcc/ChangeLog:

* config/riscv/iterators.md: Add neg and not.
* config/riscv/riscv-vector-builtins-bases.cc (class unop): New class.
(BASE): Ditto.
* config/riscv/riscv-vector-builtins-bases.h: Ditto.
* config/riscv/riscv-vector-builtins-functions.def (vadd): Rename binop
into alu.
(vsub): Ditto.
(vand): Ditto.
(vor): Ditto.
(vxor): Ditto.
(vsll): Ditto.
(vsra): Ditto.
(vsrl): Ditto.
(vmin): Ditto.
(vmax): Ditto.
(vminu): Ditto.
(vmaxu): Ditto.
(vmul): Ditto.
(vdiv): Ditto.
(vrem): Ditto.
(vdivu): Ditto.
(vremu): Ditto.
(vrsub): Ditto.
(vneg): Ditto.
(vnot): Ditto.
* config/riscv/riscv-vector-builtins-shapes.cc (struct binop_def): Ditto.
(struct alu_def): Ditto.
(SHAPE): Ditto.
* config/riscv/riscv-vector-builtins-shapes.h: Ditto.
* config/riscv/riscv-vector-builtins.cc: Support unary C/C/++.
* config/riscv/vector-iterators.md: New iterator.
* config/riscv/vector.md (@pred_<optab><mode>): New pattern

gcc/config/riscv/iterators.md
gcc/config/riscv/riscv-vector-builtins-bases.cc
gcc/config/riscv/riscv-vector-builtins-bases.h
gcc/config/riscv/riscv-vector-builtins-functions.def
gcc/config/riscv/riscv-vector-builtins-shapes.cc
gcc/config/riscv/riscv-vector-builtins-shapes.h
gcc/config/riscv/riscv-vector-builtins.cc
gcc/config/riscv/vector-iterators.md
gcc/config/riscv/vector.md

index 9561403419b0415dc148e614fb534bb2e5aebc44..6013f58db6e78a142bde0a1e7bf90baa3b38557a 100644 (file)
                         (smax "smax")
                         (umin "umin")
                         (umax "umax")
-                        (mult "mul")])
+                        (mult "mul")
+                        (not "one_cmpl")
+                        (neg "neg")])
 
 ;; <or_optab> code attributes
 (define_code_attr or_optab [(ior "ior")
                        (smax "max")
                        (umin "minu")
                        (umax "maxu")
-                       (mult "mul")])
+                       (mult "mul")
+                       (not "not")
+                       (neg "neg")])
 
 ; atomics code attribute
 (define_code_attr atomic_optab
index 0d54694398d961b5bd97d71f14797f3df39a0b66..0d86bbcd6b1c8ff58304501ba41cdf2f360e31f3 100644 (file)
@@ -186,6 +186,17 @@ public:
   }
 };
 
+/* Implements vneg/vnot.  */
+template<rtx_code CODE>
+class unop : public function_base
+{
+public:
+  rtx expand (function_expander &e) const override
+  {
+    return e.use_exact_insn (code_for_pred (CODE, e.vector_mode ()));
+  }
+};
+
 static CONSTEXPR const vsetvl<false> vsetvl_obj;
 static CONSTEXPR const vsetvl<true> vsetvlmax_obj;
 static CONSTEXPR const loadstore<false, LST_UNIT_STRIDE, false> vle_obj;
@@ -228,6 +239,8 @@ static CONSTEXPR const binop<DIV> vdiv_obj;
 static CONSTEXPR const binop<MOD> vrem_obj;
 static CONSTEXPR const binop<UDIV> vdivu_obj;
 static CONSTEXPR const binop<UMOD> vremu_obj;
+static CONSTEXPR const unop<NEG> vneg_obj;
+static CONSTEXPR const unop<NOT> vnot_obj;
 
 /* Declare the function base NAME, pointing it to an instance
    of class <NAME>_obj.  */
@@ -276,5 +289,7 @@ BASE (vdiv)
 BASE (vrem)
 BASE (vdivu)
 BASE (vremu)
+BASE (vneg)
+BASE (vnot)
 
 } // end namespace riscv_vector
index a8b65dee6fc70d27a1f0cc1311d4a44197f5c106..72ee25655b250815e604bd6fa74fe7988787b76c 100644 (file)
@@ -66,6 +66,8 @@ extern const function_base *const vdiv;
 extern const function_base *const vrem;
 extern const function_base *const vdivu;
 extern const function_base *const vremu;
+extern const function_base *const vneg;
+extern const function_base *const vnot;
 }
 
 } // end namespace riscv_vector
index d5df5c3d43341dc180deadbe1962c60a831a9474..b94e780e91634f3fac5b924c7028885076db0564 100644 (file)
@@ -63,40 +63,42 @@ DEF_RVV_FUNCTION (vsoxei16, indexed_loadstore, none_m_preds, all_v_scalar_ptr_ui
 DEF_RVV_FUNCTION (vsoxei32, indexed_loadstore, none_m_preds, all_v_scalar_ptr_uint32_index_ops)
 DEF_RVV_FUNCTION (vsoxei64, indexed_loadstore, none_m_preds, all_v_scalar_ptr_uint64_index_ops)
 /* 11. Vector Integer Arithmetic Instructions.  */
-DEF_RVV_FUNCTION (vadd, binop, full_preds, iu_vvv_ops)
-DEF_RVV_FUNCTION (vsub, binop, full_preds, iu_vvv_ops)
-DEF_RVV_FUNCTION (vand, binop, full_preds, iu_vvv_ops)
-DEF_RVV_FUNCTION (vor, binop, full_preds, iu_vvv_ops)
-DEF_RVV_FUNCTION (vxor, binop, full_preds, iu_vvv_ops)
-DEF_RVV_FUNCTION (vsll, binop, full_preds, iu_shift_vvv_ops)
-DEF_RVV_FUNCTION (vsra, binop, full_preds, iu_shift_vvv_ops)
-DEF_RVV_FUNCTION (vsrl, binop, full_preds, iu_shift_vvv_ops)
-DEF_RVV_FUNCTION (vmin, binop, full_preds, i_vvv_ops)
-DEF_RVV_FUNCTION (vmax, binop, full_preds, i_vvv_ops)
-DEF_RVV_FUNCTION (vminu, binop, full_preds, u_vvv_ops)
-DEF_RVV_FUNCTION (vmaxu, binop, full_preds, u_vvv_ops)
-DEF_RVV_FUNCTION (vmul, binop, full_preds, iu_vvv_ops)
-DEF_RVV_FUNCTION (vdiv, binop, full_preds, i_vvv_ops)
-DEF_RVV_FUNCTION (vrem, binop, full_preds, i_vvv_ops)
-DEF_RVV_FUNCTION (vdivu, binop, full_preds, u_vvv_ops)
-DEF_RVV_FUNCTION (vremu, binop, full_preds, u_vvv_ops)
-DEF_RVV_FUNCTION (vadd, binop, full_preds, iu_vvx_ops)
-DEF_RVV_FUNCTION (vsub, binop, full_preds, iu_vvx_ops)
-DEF_RVV_FUNCTION (vrsub, binop, full_preds, iu_vvx_ops)
-DEF_RVV_FUNCTION (vand, binop, full_preds, iu_vvx_ops)
-DEF_RVV_FUNCTION (vor, binop, full_preds, iu_vvx_ops)
-DEF_RVV_FUNCTION (vxor, binop, full_preds, iu_vvx_ops)
-DEF_RVV_FUNCTION (vsll, binop, full_preds, iu_shift_vvx_ops)
-DEF_RVV_FUNCTION (vsra, binop, full_preds, iu_shift_vvx_ops)
-DEF_RVV_FUNCTION (vsrl, binop, full_preds, iu_shift_vvx_ops)
-DEF_RVV_FUNCTION (vmin, binop, full_preds, i_vvx_ops)
-DEF_RVV_FUNCTION (vmax, binop, full_preds, i_vvx_ops)
-DEF_RVV_FUNCTION (vminu, binop, full_preds, u_vvx_ops)
-DEF_RVV_FUNCTION (vmaxu, binop, full_preds, u_vvx_ops)
-DEF_RVV_FUNCTION (vmul, binop, full_preds, iu_vvx_ops)
-DEF_RVV_FUNCTION (vdiv, binop, full_preds, i_vvx_ops)
-DEF_RVV_FUNCTION (vrem, binop, full_preds, i_vvx_ops)
-DEF_RVV_FUNCTION (vdivu, binop, full_preds, u_vvx_ops)
-DEF_RVV_FUNCTION (vremu, binop, full_preds, u_vvx_ops)
+DEF_RVV_FUNCTION (vadd, alu, full_preds, iu_vvv_ops)
+DEF_RVV_FUNCTION (vsub, alu, full_preds, iu_vvv_ops)
+DEF_RVV_FUNCTION (vand, alu, full_preds, iu_vvv_ops)
+DEF_RVV_FUNCTION (vor, alu, full_preds, iu_vvv_ops)
+DEF_RVV_FUNCTION (vxor, alu, full_preds, iu_vvv_ops)
+DEF_RVV_FUNCTION (vsll, alu, full_preds, iu_shift_vvv_ops)
+DEF_RVV_FUNCTION (vsra, alu, full_preds, iu_shift_vvv_ops)
+DEF_RVV_FUNCTION (vsrl, alu, full_preds, iu_shift_vvv_ops)
+DEF_RVV_FUNCTION (vmin, alu, full_preds, i_vvv_ops)
+DEF_RVV_FUNCTION (vmax, alu, full_preds, i_vvv_ops)
+DEF_RVV_FUNCTION (vminu, alu, full_preds, u_vvv_ops)
+DEF_RVV_FUNCTION (vmaxu, alu, full_preds, u_vvv_ops)
+DEF_RVV_FUNCTION (vmul, alu, full_preds, iu_vvv_ops)
+DEF_RVV_FUNCTION (vdiv, alu, full_preds, i_vvv_ops)
+DEF_RVV_FUNCTION (vrem, alu, full_preds, i_vvv_ops)
+DEF_RVV_FUNCTION (vdivu, alu, full_preds, u_vvv_ops)
+DEF_RVV_FUNCTION (vremu, alu, full_preds, u_vvv_ops)
+DEF_RVV_FUNCTION (vadd, alu, full_preds, iu_vvx_ops)
+DEF_RVV_FUNCTION (vsub, alu, full_preds, iu_vvx_ops)
+DEF_RVV_FUNCTION (vrsub, alu, full_preds, iu_vvx_ops)
+DEF_RVV_FUNCTION (vand, alu, full_preds, iu_vvx_ops)
+DEF_RVV_FUNCTION (vor, alu, full_preds, iu_vvx_ops)
+DEF_RVV_FUNCTION (vxor, alu, full_preds, iu_vvx_ops)
+DEF_RVV_FUNCTION (vsll, alu, full_preds, iu_shift_vvx_ops)
+DEF_RVV_FUNCTION (vsra, alu, full_preds, iu_shift_vvx_ops)
+DEF_RVV_FUNCTION (vsrl, alu, full_preds, iu_shift_vvx_ops)
+DEF_RVV_FUNCTION (vmin, alu, full_preds, i_vvx_ops)
+DEF_RVV_FUNCTION (vmax, alu, full_preds, i_vvx_ops)
+DEF_RVV_FUNCTION (vminu, alu, full_preds, u_vvx_ops)
+DEF_RVV_FUNCTION (vmaxu, alu, full_preds, u_vvx_ops)
+DEF_RVV_FUNCTION (vmul, alu, full_preds, iu_vvx_ops)
+DEF_RVV_FUNCTION (vdiv, alu, full_preds, i_vvx_ops)
+DEF_RVV_FUNCTION (vrem, alu, full_preds, i_vvx_ops)
+DEF_RVV_FUNCTION (vdivu, alu, full_preds, u_vvx_ops)
+DEF_RVV_FUNCTION (vremu, alu, full_preds, u_vvx_ops)
+DEF_RVV_FUNCTION (vneg, alu, full_preds, iu_v_ops)
+DEF_RVV_FUNCTION (vnot, alu, full_preds, iu_v_ops)
 
 #undef DEF_RVV_FUNCTION
index c8bca3598ac771edd7af2ec0d9ecc4f2d50685ac..d6dc4c7049ecabaae912aff09cb425e5e0506784 100644 (file)
@@ -185,8 +185,8 @@ struct indexed_loadstore_def : public function_shape
   }
 };
 
-/* binop_def class.  */
-struct binop_def : public build_base
+/* alu_def class.  */
+struct alu_def : public build_base
 {
   char *get_name (function_builder &b, const function_instance &instance,
                  bool overloaded_p) const override
@@ -213,6 +213,6 @@ SHAPE(vsetvl, vsetvl)
 SHAPE(vsetvl, vsetvlmax)
 SHAPE(loadstore, loadstore)
 SHAPE(indexed_loadstore, indexed_loadstore)
-SHAPE(binop, binop)
+SHAPE(alu, alu)
 
 } // end namespace riscv_vector
index 2903c2dfead4f9accaec91ed3afeac5500222bda..b4f6eaa0bba1d7bc878dd5592e1c2a12ce4d4b6b 100644 (file)
@@ -28,7 +28,7 @@ extern const function_shape *const vsetvl;
 extern const function_shape *const vsetvlmax;
 extern const function_shape *const loadstore;
 extern const function_shape *const indexed_loadstore;
-extern const function_shape *const binop;
+extern const function_shape *const alu;
 }
 
 } // end namespace riscv_vector
index 70df5d1ce87b2e5625788b3bb461e8b1504f5aae..95a949aab7c6f6a97464c42c19834a7f5ff87271 100644 (file)
@@ -260,6 +260,10 @@ static CONSTEXPR const rvv_arg_type_info shift_vv_args[]
   = {rvv_arg_type_info (RVV_BASE_vector),
      rvv_arg_type_info (RVV_BASE_shift_vector), rvv_arg_type_info_end};
 
+/* A list of args for vector_type func (vector_type) function.  */
+static CONSTEXPR const rvv_arg_type_info v_args[]
+  = {rvv_arg_type_info (RVV_BASE_vector), rvv_arg_type_info_end};
+
 /* A list of args for vector_type func (vector_type, size) function.  */
 static CONSTEXPR const rvv_arg_type_info vector_size_args[]
   = {rvv_arg_type_info (RVV_BASE_vector), rvv_arg_type_info (RVV_BASE_size),
@@ -469,6 +473,14 @@ static CONSTEXPR const rvv_op_info iu_shift_vvx_ops
      rvv_arg_type_info (RVV_BASE_vector), /* Return type */
      vector_size_args /* Args */};
 
+/* A static operand information for vector_type func (vector_type)
+ * function registration. */
+static CONSTEXPR const rvv_op_info iu_v_ops
+  = {iu_ops,                   /* Types */
+     OP_TYPE_v,                        /* Suffix */
+     rvv_arg_type_info (RVV_BASE_vector), /* Return type */
+     v_args /* Args */};
+
 /* A list of all RVV intrinsic functions.  */
 static function_group_info function_groups[] = {
 #define DEF_RVV_FUNCTION(NAME, SHAPE, PREDS, OPS_INFO)                         \
index ed9da0c35a6034c1b617eef947ad7460b2a9eb87..9cc60da91f4250d34833d9155d10ac3bd67ea8b2 100644 (file)
   smax umax smin umin mult div udiv mod umod
 ])
 
+(define_code_iterator any_int_unop [neg not])
+
 (define_code_iterator any_commutative_binop [plus and ior xor
   smax umax smin umin mult
 ])
index 02ded957f995aa4ee65ef62a60c7bc079c35f8b5..73c839f45c3ce109970e5863c218e9a539183117 100644 (file)
   "vrsub.vx\t%0,%3,%4%p1"
   [(set_attr "type" "vialu")
    (set_attr "mode" "<MODE>")])
+
+;; -------------------------------------------------------------------------------
+;; ---- Predicated integer unary operations
+;; -------------------------------------------------------------------------------
+;; Includes:
+;; - vneg.v/vnot.v
+;; -------------------------------------------------------------------------------
+
+(define_insn "@pred_<optab><mode>"
+  [(set (match_operand:VI 0 "register_operand"           "=vd, vr")
+       (if_then_else:VI
+         (unspec:<VM>
+           [(match_operand:<VM> 1 "vector_mask_operand" " vm,Wc1")
+            (match_operand 4 "vector_length_operand"    " rK, rK")
+            (match_operand 5 "const_int_operand"        "  i,  i")
+            (match_operand 6 "const_int_operand"        "  i,  i")
+            (match_operand 7 "const_int_operand"        "  i,  i")
+            (reg:SI VL_REGNUM)
+            (reg:SI VTYPE_REGNUM)] UNSPEC_VPREDICATE)
+         (any_int_unop:VI
+           (match_operand:VI 3 "register_operand"       " vr, vr"))
+         (match_operand:VI 2 "vector_merge_operand"     "0vu,0vu")))]
+  "TARGET_VECTOR"
+  "v<insn>.v\t%0,%3%p1"
+  [(set_attr "type" "vialu")
+   (set_attr "mode" "<MODE>")
+   (set_attr "vl_op_idx" "4")
+   (set (attr "ta") (symbol_ref "riscv_vector::get_ta(operands[5])"))
+   (set (attr "ma") (symbol_ref "riscv_vector::get_ta(operands[6])"))
+   (set (attr "avl_type") (symbol_ref "INTVAL (operands[7])"))])