1 /* Builtins definitions for RISC-V 'V' Extension for GNU compiler.
2 Copyright (C) 2022-2024 Free Software Foundation, Inc.
3 Contributed by Ju-Zhe Zhong (juzhe.zhong@rivai.ai), RiVAI Technologies Ltd.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #ifndef GCC_RISCV_VECTOR_BUILTINS_H
22 #define GCC_RISCV_VECTOR_BUILTINS_H
24 /* The full name of an RVV intrinsic function is the concatenation of:
26 - the base name ("vadd", etc.)
27 - the operand suffix ("_vv", "_vx", etc.)
28 - the type suffix ("_i32m1", "_i32mf2", etc.)
29 - the predication suffix ("_tamu", "_tumu", etc.)
31 Each piece of information is individually useful, so we retain this
32 classification throughout:
34 - function_base represents the base name.
36 - operand_type_index can be used as an index to get operand suffix.
38 - rvv_op_info can be used as an index to get argument suffix.
40 - predication_type_index can be used as an index to get predication suffix.
42 In addition to its unique full name, a function may have a shorter
43 overloaded alias. This alias removes pieces of the suffixes that
44 can be inferred from the arguments, such as by shortening the mode
45 suffix or dropping some of the type suffixes. The base name and the
46 predication suffix stay the same.
48 - The function_instance class describes contains all properties of each
49 individual function. Such these information will be used by
50 function_builder, function_base, function_shape, gimple_folder,
51 function_expander, etc.
53 - The function_builder class provides several helper function to add an
56 - The function_shape class describes how that instruction has been presented
57 at the language level:
59 1. Determine the function name for C and C++ overload function which can
60 be recognized by compiler at language level for each instruction
61 according to members of function_instance (base name, operand suffix,
62 type suffix, predication suffix, etc.).
64 2. Specify the arguments type and return type of each function to
65 describe how that instruction has presented at language level.
67 - The function_base describes how the underlying instruction behaves.
69 The static list of functions uses function_group to describe a group
70 of related functions. The function_builder class is responsible for
71 expanding this static description into a list of individual functions
72 and registering the associated built-in functions. function_instance
73 describes one of these individual functions in terms of the properties
76 The classes involved in compiling a function call are:
78 - function_resolver, which resolves an overloaded function call to a
79 specific function_instance and its associated function decl.
81 - function_checker, which checks whether the values of the arguments
82 conform to the RVV ISA specification.
84 - gimple_folder, which tries to fold a function call at the gimple level
86 - function_expander, which expands a function call into rtl instructions
88 function_resolver and function_checker operate at the language level
89 and so are associated with the function_shape. gimple_folder and
90 function_expander are concerned with the behavior of the function
91 and so are associated with the function_base. */
93 namespace riscv_vector
{
95 /* Flags that describe what a function might do, in addition to reading
96 its arguments and returning a result. */
97 static const unsigned int CP_READ_FPCR
= 1U << 0;
98 static const unsigned int CP_RAISE_FP_EXCEPTIONS
= 1U << 1;
99 static const unsigned int CP_READ_MEMORY
= 1U << 2;
100 static const unsigned int CP_WRITE_MEMORY
= 1U << 3;
101 static const unsigned int CP_READ_CSR
= 1U << 4;
102 static const unsigned int CP_WRITE_CSR
= 1U << 5;
104 /* Bit values used to identify required extensions for RVV intrinsics. */
105 #define RVV_REQUIRE_RV64BIT (1 << 0) /* Require RV64. */
106 #define RVV_REQUIRE_ELEN_64 (1 << 1) /* Require TARGET_VECTOR_ELEN_64. */
107 #define RVV_REQUIRE_ELEN_FP_32 (1 << 2) /* Require FP ELEN >= 32. */
108 #define RVV_REQUIRE_ELEN_FP_64 (1 << 3) /* Require FP ELEN >= 64. */
109 #define RVV_REQUIRE_FULL_V (1 << 4) /* Require Full 'V' extension. */
110 #define RVV_REQUIRE_MIN_VLEN_64 (1 << 5) /* Require TARGET_MIN_VLEN >= 64. */
111 #define RVV_REQUIRE_ELEN_FP_16 (1 << 6) /* Require FP ELEN >= 32. */
112 #define RVV_REQUIRE_ELEN_BF_16 (1 << 7) /* Require BF16. */
114 /* Enumerates the required extensions. */
117 VECTOR_EXT
, /* Vector extension */
118 ZVBB_EXT
, /* Crypto vector Zvbb sub-ext */
119 ZVBB_OR_ZVKB_EXT
, /* Crypto vector Zvbb or zvkb sub-ext */
120 ZVBC_EXT
, /* Crypto vector Zvbc sub-ext */
121 ZVKG_EXT
, /* Crypto vector Zvkg sub-ext */
122 ZVKNED_EXT
, /* Crypto vector Zvkned sub-ext */
123 ZVKNHA_OR_ZVKNHB_EXT
, /* Crypto vector Zvknh[ab] sub-ext */
124 ZVKNHB_EXT
, /* Crypto vector Zvknhb sub-ext */
125 ZVKSED_EXT
, /* Crypto vector Zvksed sub-ext */
126 ZVKSH_EXT
, /* Crypto vector Zvksh sub-ext */
127 XTHEADVECTOR_EXT
, /* XTheadVector extension */
128 ZVFBFMIN_EXT
, /* Zvfbfmin extension */
129 ZVFBFWMA_EXT
, /* Zvfbfwma extension */
130 /* Please update below to isa_name func when add or remove enum type(s). */
133 static inline const char * required_ext_to_isa_name (enum required_ext required
)
141 case ZVBB_OR_ZVKB_EXT
:
142 return "zvbb or zvkb";
149 case ZVKNHA_OR_ZVKNHB_EXT
:
150 return "zvknha or zvknhb";
157 case XTHEADVECTOR_EXT
:
158 return "xtheadvector";
170 static inline bool required_extensions_specified (enum required_ext required
)
175 return TARGET_VECTOR
;;
178 case ZVBB_OR_ZVKB_EXT
:
179 return TARGET_ZVBB
|| TARGET_ZVKB
;
185 return TARGET_ZVKNED
;
186 case ZVKNHA_OR_ZVKNHB_EXT
:
187 return TARGET_ZVKNHA
|| TARGET_ZVKNHB
;
189 return TARGET_ZVKNHB
;
191 return TARGET_ZVKSED
;
194 case XTHEADVECTOR_EXT
:
195 return TARGET_XTHEADVECTOR
;
197 return TARGET_ZVFBFMIN
;
199 return TARGET_ZVFBFWMA
;
207 /* Enumerates the RVV operand types. */
208 enum operand_type_index
211 #define DEF_RVV_OP_TYPE(NAME) OP_TYPE_##NAME,
212 #include "riscv-vector-builtins.def"
216 /* Enumerates the RVV types, together called
217 "vector types" for brevity. */
218 enum vector_type_index
220 #define DEF_RVV_TYPE(NAME, ABI_NAME, NCHARS, ARGS...) VECTOR_TYPE_##NAME,
221 #define DEF_RVV_TUPLE_TYPE(NAME, ABI_NAME, NCHARS, ARGS...) VECTOR_TYPE_##NAME,
222 #include "riscv-vector-builtins.def"
224 VECTOR_TYPE_INVALID
= NUM_VECTOR_TYPES
227 /* Enumerates the RVV governing predication types. */
228 enum predication_type_index
231 #define DEF_RVV_PRED_TYPE(NAME) PRED_TYPE_##NAME,
232 #include "riscv-vector-builtins.def"
236 /* Enumerates the RVV base types. */
239 #define DEF_RVV_BASE_TYPE(NAME, ARGS...) RVV_BASE_##NAME,
240 #include "riscv-vector-builtins.def"
244 /* Builtin types that are used to register RVV intrinsics. */
245 struct GTY (()) rvv_builtin_types_t
251 tree scalar_const_ptr
;
254 /* Builtin suffix that are used to register RVV intrinsics. */
255 struct rvv_builtin_suffixes
262 /* Builtin base type used to specify the type of builtin function
263 argument or return result. */
264 struct function_type_info
266 enum vector_type_index type_indexes
[NUM_BASE_TYPES
];
269 /* RVV Builtin argument information. */
270 struct rvv_arg_type_info
272 CONSTEXPR
rvv_arg_type_info (rvv_base_type base_type_in
)
273 : base_type (base_type_in
)
275 enum rvv_base_type base_type
;
277 tree
get_scalar_ptr_type (vector_type_index
) const;
278 tree
get_scalar_const_ptr_type (vector_type_index
) const;
279 vector_type_index
get_function_type_index (vector_type_index
) const;
280 tree
get_scalar_type (vector_type_index
) const;
281 tree
get_vector_type (vector_type_index
) const;
282 tree
get_tree_type (vector_type_index
) const;
283 tree
get_tuple_subpart_type (vector_type_index
) const;
286 /* Static information for each operand. */
289 enum vector_type_index index
;
290 uint64_t required_extensions
;
293 /* RVV Builtin operands information. */
296 const rvv_type_info
*types
;
297 const operand_type_index op
;
298 rvv_arg_type_info ret
;
299 const rvv_arg_type_info
*args
;
302 class registered_function
;
304 class function_shape
;
306 /* Static information about a set of functions. */
307 struct function_group_info
309 /* Return true if required extension is enabled */
310 bool match (required_ext ext_value
) const
315 return TARGET_VECTOR
;
318 case ZVBB_OR_ZVKB_EXT
:
319 return (TARGET_ZVBB
|| TARGET_ZVKB
);
325 return TARGET_ZVKNED
;
326 case ZVKNHA_OR_ZVKNHB_EXT
:
327 return (TARGET_ZVKNHA
|| TARGET_ZVKNHB
);
329 return TARGET_ZVKNHB
;
331 return TARGET_ZVKSED
;
334 case XTHEADVECTOR_EXT
:
335 return TARGET_XTHEADVECTOR
;
337 return TARGET_ZVFBFMIN
;
339 return TARGET_ZVFBFWMA
;
344 /* The base name, as a string. */
345 const char *base_name
;
347 /* Describes the behavior associated with the function base name. */
348 const function_base
*const *base
;
350 /* The shape of the functions, as described above the class definition.
351 It's possible to have entries with the same base name but different
353 const function_shape
*const *shape
;
355 /* A list of the available operand types, predication types,
356 and of the available operand datatype.
357 The function supports every combination of the two.
358 The list of predication is terminated by two NUM_PRED_TYPES,
359 while the list of operand info is terminated by NUM_BASE_TYPES.
360 The list of these type suffix is lexicographically ordered based
361 on the index value. */
362 const predication_type_index
*preds
;
363 const rvv_op_info ops_infos
;
364 /* The required extension value, using it to get the enabled flag. */
365 required_ext required_extensions
;
368 class GTY ((user
)) function_instance
371 function_instance (const char *, const function_base
*,
372 const function_shape
*, rvv_type_info
,
373 predication_type_index
, const rvv_op_info
*);
375 bool operator== (const function_instance
&) const;
376 bool operator!= (const function_instance
&) const;
377 hashval_t
hash () const;
379 unsigned int call_properties () const;
380 bool reads_global_state_p () const;
381 bool modifies_global_state_p () const;
382 bool could_trap_p () const;
384 /* Return true if return type or arguments are floating point type. */
385 bool any_type_float_p () const;
387 tree
get_return_type () const;
388 tree
get_arg_type (unsigned opno
) const;
390 /* The properties of the function. (The explicit "enum"s are required
392 const char *base_name
;
393 const function_base
*base
;
394 const function_shape
*shape
;
396 enum predication_type_index pred
;
397 const rvv_op_info
*op_info
;
400 /* A class for building and registering function decls. */
401 class function_builder
405 ~function_builder ();
407 void allocate_argument_types (const function_instance
&, vec
<tree
> &) const;
408 void apply_predication (const function_instance
&, tree
, vec
<tree
> &) const;
409 void add_unique_function (const function_instance
&, const function_shape
*,
410 tree
, vec
<tree
> &, enum required_ext
);
411 void add_overloaded_function (const function_instance
&,
412 const function_shape
*,
414 void register_function_group (const function_group_info
&);
415 void append_name (const char *);
416 void append_base_name (const char *);
417 void append_sew (int);
418 void append_nf (int);
419 char *finish_name ();
422 tree
get_attributes (const function_instance
&);
424 registered_function
&add_function (const function_instance
&, const char *,
425 tree
, tree
, bool, const char *,
426 const vec
<tree
> &, enum required_ext
,
429 /* True if we should create a separate decl for each instance of an
430 overloaded function, instead of using function_builder. */
431 bool m_direct_overloads
;
433 /* Used for building up function names. */
434 obstack m_string_obstack
;
437 /* A base class for handling calls to built-in functions. */
438 class function_call_info
: public function_instance
441 function_call_info (location_t
, const function_instance
&, tree
);
443 bool function_returns_void_p ();
445 /* The location of the call. */
448 /* The FUNCTION_DECL that is being called. */
452 /* Return true if the function has no return value. */
454 function_call_info::function_returns_void_p ()
456 return TREE_TYPE (TREE_TYPE (fndecl
)) == void_type_node
;
459 /* A class for folding a gimple function call. */
460 class gimple_folder
: public function_call_info
463 gimple_folder (const function_instance
&, tree
, gimple_stmt_iterator
*,
468 /* Where to insert extra statements that feed the final replacement. */
469 gimple_stmt_iterator
*gsi
;
471 /* The call we're folding. */
474 /* The result of the call, or null if none. */
478 /* A class for expanding a function call into RTL. */
479 class function_expander
: public function_call_info
482 function_expander (const function_instance
&, tree
, tree
, rtx
);
485 void add_input_operand (machine_mode
, rtx
);
486 void add_input_operand (unsigned);
487 void add_output_operand (machine_mode
, rtx
);
488 void add_all_one_mask_operand (machine_mode
);
489 void add_scalar_move_mask_operand (machine_mode
);
490 void add_vundef_operand (machine_mode
);
491 void add_fixed_operand (rtx
);
492 void add_integer_operand (rtx
);
493 void add_mem_operand (machine_mode
, unsigned);
495 machine_mode
vector_mode (void) const;
496 machine_mode
index_mode (void) const;
497 machine_mode
arg_mode (int) const;
498 machine_mode
mask_mode (void) const;
499 machine_mode
ret_mode (void) const;
501 rtx
use_exact_insn (insn_code
);
502 rtx
use_contiguous_load_insn (insn_code
);
503 rtx
use_contiguous_store_insn (insn_code
);
504 rtx
use_compare_insn (rtx_code
, insn_code
);
505 rtx
use_ternop_insn (bool, insn_code
);
506 rtx
use_widen_ternop_insn (insn_code
);
507 rtx
use_scalar_move_insn (insn_code
);
508 rtx
generate_insn (insn_code
);
510 /* The function call expression. */
513 /* For functions that return a value, this is the preferred location
514 of that value. It could be null or could have a different mode
515 from the function return type. */
518 /* The number of the operands. */
522 /* Used to build up the operands to an instruction. */
523 struct expand_operand m_ops
[MAX_RECOG_OPERANDS
];
526 /* Provides information about a particular function base name, and handles
527 tasks related to the base name. */
531 /* Return a set of CP_* flags that describe what the function might do,
532 in addition to reading its arguments and returning a result. */
533 virtual unsigned int call_properties (const function_instance
&) const;
535 /* Return true if intrinsics should apply vl operand. */
536 virtual bool apply_vl_p () const;
538 /* Return true if intrinsics should apply tail policy operand. */
539 virtual bool apply_tail_policy_p () const;
541 /* Return true if intrinsics should apply mask policy operand. */
542 virtual bool apply_mask_policy_p () const;
544 /* Return true if intrinsic can be overloaded. */
545 virtual bool can_be_overloaded_p (enum predication_type_index
) const;
547 /* Return true if intrinsics use mask predication. */
548 virtual bool use_mask_predication_p () const;
550 /* Return true if intrinsics has merge operand. */
551 virtual bool has_merge_operand_p () const;
553 /* Return true if intrinsics has rounding mode operand. */
554 virtual bool has_rounding_mode_operand_p () const;
556 /* Return true if intrinsics maybe require vxrm operand. */
557 virtual bool may_require_vxrm_p () const;
559 /* Return true if intrinsics maybe require frm operand. */
560 virtual bool may_require_frm_p () const;
562 /* Try to fold the given gimple call. Return the new gimple statement
563 on success, otherwise return null. */
564 virtual gimple
*fold (gimple_folder
&) const { return NULL
; }
566 /* Expand the given call into rtl. Return the result of the function,
567 or an arbitrary value if the function doesn't return a result. */
568 virtual rtx
expand (function_expander
&) const = 0;
571 /* A class for checking that the semantic constraints on a function call are
572 satisfied, such as arguments being integer constant expressions with
573 a particular range. The parent class's FNDECL is the decl that was
574 called in the original source, before overload resolution. */
575 class function_checker
: public function_call_info
578 function_checker (location_t
, const function_instance
&, tree
, tree
,
579 unsigned int, tree
*);
581 machine_mode
arg_mode (unsigned int) const;
582 machine_mode
ret_mode (void) const;
583 unsigned int arg_num (void) const;
586 bool require_immediate (unsigned int, HOST_WIDE_INT
, HOST_WIDE_INT
) const;
587 bool require_immediate_range_or (unsigned int, HOST_WIDE_INT
,
588 HOST_WIDE_INT
, HOST_WIDE_INT
) const;
591 bool require_immediate_range (unsigned int, HOST_WIDE_INT
,
592 HOST_WIDE_INT
) const;
593 void report_non_ice (unsigned int) const;
594 void report_out_of_range (unsigned int, HOST_WIDE_INT
, HOST_WIDE_INT
,
595 HOST_WIDE_INT
) const;
596 void report_out_of_range_and_not (unsigned int, HOST_WIDE_INT
, HOST_WIDE_INT
,
597 HOST_WIDE_INT
, HOST_WIDE_INT
) const;
599 /* The type of the resolved function. */
602 /* The arguments to the function. */
603 unsigned int m_nargs
;
607 /* Classifies functions into "shapes" base on:
609 - Base name of the intrinsic function.
611 - Operand types list.
613 - Argument type list.
615 - Predication type list. */
619 /* Shape the function name according to function_instance. */
620 virtual char *get_name (function_builder
&, const function_instance
&,
624 /* Define all functions associated with the given group. */
625 virtual void build (function_builder
&, const function_group_info
&) const
628 /* Check whether the given call is semantically valid. Return true
629 if it is, otherwise report an error and return false. */
630 virtual bool check (function_checker
&) const { return true; }
633 extern const char *const operand_suffixes
[NUM_OP_TYPES
];
634 extern const rvv_builtin_suffixes type_suffixes
[NUM_VECTOR_TYPES
+ 1];
635 extern const char *const predication_suffixes
[NUM_PRED_TYPES
];
636 extern rvv_builtin_types_t builtin_types
[NUM_VECTOR_TYPES
+ 1];
637 extern function_instance
get_read_vl_instance (void);
638 extern tree
get_read_vl_decl (void);
641 rvv_arg_type_info::get_scalar_type (vector_type_index type_idx
) const
643 return get_function_type_index (type_idx
) == VECTOR_TYPE_INVALID
645 : builtin_types
[get_function_type_index (type_idx
)].scalar
;
649 rvv_arg_type_info::get_vector_type (vector_type_index type_idx
) const
651 return get_function_type_index (type_idx
) == VECTOR_TYPE_INVALID
653 : builtin_types
[get_function_type_index (type_idx
)].vector
;
657 function_instance::operator!= (const function_instance
&other
) const
659 return !operator== (other
);
662 /* Expand the call and return its lhs. */
664 function_expander::expand ()
666 return base
->expand (*this);
669 /* Create op and add it into M_OPS and increase OPNO. */
671 function_expander::add_input_operand (machine_mode mode
, rtx op
)
673 create_input_operand (&m_ops
[opno
++], op
, mode
);
676 /* Create output and add it into M_OPS and increase OPNO. */
678 function_expander::add_output_operand (machine_mode mode
, rtx target
)
680 create_output_operand (&m_ops
[opno
++], target
, mode
);
683 /* Since we may normalize vop/vop_tu/vop_m/vop_tumu.. into a single patter.
684 We add a fake all true mask for the intrinsics that don't need a real mask.
687 function_expander::add_all_one_mask_operand (machine_mode mode
)
689 add_input_operand (mode
, CONSTM1_RTX (mode
));
692 /* Add mask operand for scalar move instruction. */
694 function_expander::add_scalar_move_mask_operand (machine_mode mode
)
696 add_input_operand (mode
, gen_scalar_move_mask (mode
));
699 /* Add an operand that must be X. The only way of legitimizing an
700 invalid X is to reload the address of a MEM. */
702 function_expander::add_fixed_operand (rtx x
)
704 create_fixed_operand (&m_ops
[opno
++], x
);
707 /* Add an integer operand X. */
709 function_expander::add_integer_operand (rtx x
)
711 create_integer_operand (&m_ops
[opno
++], INTVAL (x
));
714 /* Return the machine_mode of the corresponding vector type. */
716 function_expander::vector_mode (void) const
718 return TYPE_MODE (builtin_types
[type
.index
].vector
);
721 /* Return the machine_mode of the corresponding index type. */
723 function_expander::index_mode (void) const
725 return TYPE_MODE (op_info
->args
[1].get_tree_type (type
.index
));
728 /* Return the machine_mode of the corresponding arg type. */
730 function_expander::arg_mode (int idx
) const
732 return TYPE_MODE (op_info
->args
[idx
].get_tree_type (type
.index
));
735 /* Return the machine_mode of the corresponding return type. */
737 function_expander::ret_mode (void) const
739 return TYPE_MODE (op_info
->ret
.get_tree_type (type
.index
));
743 function_checker::arg_mode (unsigned int argno
) const
745 return TYPE_MODE (TREE_TYPE (m_args
[argno
]));
749 function_checker::ret_mode () const
751 return TYPE_MODE (TREE_TYPE (TREE_TYPE (fndecl
)));
755 function_checker::arg_num () const
760 /* Default implementation of function_base::call_properties, with conservatively
761 correct behavior for floating-point instructions. */
763 function_base::call_properties (const function_instance
&instance
) const
765 unsigned int flags
= 0;
766 if (instance
.any_type_float_p ())
767 return flags
| CP_READ_FPCR
| CP_RAISE_FP_EXCEPTIONS
;
771 /* We choose to apply vl operand by default since most of the intrinsics
774 function_base::apply_vl_p () const
779 /* We choose to apply tail policy operand by default since most of the
780 intrinsics has tail policy operand. */
782 function_base::apply_tail_policy_p () const
787 /* We choose to apply mask policy operand by default since most of the
788 intrinsics has mask policy operand. */
790 function_base::apply_mask_policy_p () const
795 /* We choose to return true by default since most of the intrinsics use
798 function_base::use_mask_predication_p () const
803 /* We choose to return true by default since most of the intrinsics use
804 has merge operand. */
806 function_base::has_merge_operand_p () const
811 /* We choose to return false by default since most of the intrinsics does
812 not have rounding mode operand. */
814 function_base::has_rounding_mode_operand_p () const
819 /* We choose to return false by default since most of the intrinsics does
820 not need frm operand. */
822 function_base::may_require_frm_p () const
827 /* We choose to return false by default since most of the intrinsics does
828 not need vxrm operand. */
830 function_base::may_require_vxrm_p () const
835 /* Since most of intrinsics can be overloaded, we set it true by default. */
837 function_base::can_be_overloaded_p (enum predication_type_index
) const
842 /* Return the single field in tuple type TYPE. */
844 tuple_type_field (tree type
)
846 for (tree field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
847 if (TREE_CODE (field
) == FIELD_DECL
)
852 } // end namespace riscv_vector