Skip to content

Commit 6191538

Browse files
committed
arcv: apex: Add "i" suffix to resolved immediate-format instructions.
When an APEX instruction does not have a user-defined format specified via pragma, its format is resolved based on operand flags and opcode. In these cases, this patch appends an "i" suffix to the instruction name to distinguish it from the XD format. While XD instructions and immediate-based formats (XI, XS, XC) may have the same number of operands, XD uses only registers, whereas the others include at least one immediate operand. Signed-off-by: Luis Silva <[email protected]>
1 parent b9dd27f commit 6191538

File tree

4 files changed

+58
-22
lines changed

4 files changed

+58
-22
lines changed

gcc/config/riscv/arcv-apex.md

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,13 @@
5151
""
5252
{
5353
const char *str = arcv_apex_get_insn_name (operands[0]);
54+
const char *suffix = arcv_apex_get_insn_suffix (operands[0]);
5455
switch (which_alternative)
5556
{
5657
case 0:
57-
return xasprintf ("%si\t%d # 'XI' `insn src0` volatile",
58+
return xasprintf ("%s%s\t%d # 'XI' `insn src0` volatile",
5859
str,
60+
suffix,
5961
(int) INTVAL (operands[1]));
6062
case 1:
6163
return xasprintf ("%s\t%s # 'XD' `insn src0` volatile",
@@ -96,11 +98,13 @@
9698
""
9799
{
98100
const char *str = arcv_apex_get_insn_name (operands[0]);
101+
const char *suffix = arcv_apex_get_insn_suffix (operands[0]);
99102
switch (which_alternative)
100103
{
101104
case 0:
102-
return xasprintf ("%si\t%s,%d # 'XS' `insn src0, src1` volatile",
105+
return xasprintf ("%s%s\t%s,%d # 'XS' `insn src0, src1` volatile",
103106
str,
107+
suffix,
104108
reg_names[REGNO (operands[1])],
105109
(int) INTVAL (operands[2]));
106110
case 1:
@@ -213,11 +217,13 @@
213217
""
214218
{
215219
const char *str = arcv_apex_get_insn_name (operands[1]);
220+
const char *suffix = arcv_apex_get_insn_suffix (operands[1]);
216221
switch (which_alternative)
217222
{
218223
case 0:
219-
return xasprintf ("%si\t%s,%d # 'XI' `insn des, src0` volatile",
224+
return xasprintf ("%s%s\t%s,%d # 'XI' `insn des, src0` volatile",
220225
str,
226+
suffix,
221227
reg_names[REGNO (operands[0])],
222228
(int) INTVAL (operands[2]));
223229
case 1:
@@ -262,11 +268,13 @@
262268
""
263269
{
264270
const char *str = arcv_apex_get_insn_name (operands[1]);
271+
const char *suffix = arcv_apex_get_insn_suffix (operands[1]);
265272
switch (which_alternative)
266273
{
267274
case 0:
268-
return xasprintf ("%si\t%s,%d # 'XI' `insn des, src0`",
275+
return xasprintf ("%s%s\t%s,%d # 'XI' `insn des, src0`",
269276
str,
277+
suffix,
270278
reg_names[REGNO (operands[0])],
271279
(int) INTVAL (operands[2]));
272280
case 1:
@@ -312,17 +320,20 @@
312320
""
313321
{
314322
const char *str = arcv_apex_get_insn_name (operands[1]);
323+
const char *suffix = arcv_apex_get_insn_suffix (operands[1]);
315324
switch (which_alternative)
316325
{
317326
case 0:
318-
return xasprintf ("%si\t%s,%s,%d # 'XS' `insn dest, src0, imm/src1` volatile",
327+
return xasprintf ("%s%s\t%s,%s,%d # 'XS' `insn dest, src0, imm/src1` volatile",
319328
str,
329+
suffix,
320330
reg_names[REGNO (operands[0])],
321331
reg_names[REGNO (operands[2])],
322332
(int) INTVAL (operands[3]));
323333
case 1:
324334
return xasprintf ("%s%s\t%s,%s,%d # 'XC' `insn dest/src0, imm` volatile",
325335
str,
336+
suffix,
326337
reg_names[REGNO (operands[0])],
327338
reg_names[REGNO (operands[2])],
328339
(int) INTVAL (operands[3]));
@@ -372,17 +383,20 @@
372383
""
373384
{
374385
const char *str = arcv_apex_get_insn_name (operands[1]);
386+
const char *suffix = arcv_apex_get_insn_suffix (operands[1]);
375387
switch (which_alternative)
376388
{
377389
case 0:
378-
return xasprintf ("%si\t%s,%s,%d # 'XS' `insn dest, src0, imm/src1`",
390+
return xasprintf ("%s%s\t%s,%s,%d # 'XS' `insn dest, src0, imm/src1`",
379391
str,
392+
suffix,
380393
reg_names[REGNO (operands[0])],
381394
reg_names[REGNO (operands[2])],
382395
(int) INTVAL (operands[3]));
383396
case 1:
384397
return xasprintf ("%s%s\t%s,%s,%d # 'XC' `insn dest/src0, imm`",
385398
str,
399+
suffix,
386400
reg_names[REGNO (operands[0])],
387401
reg_names[REGNO (operands[2])],
388402
(int) INTVAL (operands[3]));

gcc/config/riscv/riscv-builtins.cc

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,10 @@ struct arcv_apex_builtin_description {
256256
/* Specifies the instruction format. See "apex_insn_format" enum
257257
for more details. */
258258
unsigned int insn_formats;
259+
260+
/* Suffix added to the instruction name when the format is resolved
261+
(e.g., XS, XI, or XC); set to "i" if resolved, otherwise "". */
262+
const char *insn_suffix;
259263
};
260264

261265
/* The XD-type has 8 function bits encoding up to 256 instructions.
@@ -616,6 +620,18 @@ arcv_apex_get_insn_name (rtx op)
616620
return arcv_apex_builtins[subcode].insn_name;
617621
}
618622

623+
/* Return the instruction suffix for an APEX subcode operand.
624+
625+
This suffix is used to mark instructions whose format was resolved
626+
(e.g., XS, XI, or XC) rather than explicitly specified via pragma. */
627+
628+
const char*
629+
arcv_apex_get_insn_suffix (rtx op)
630+
{
631+
unsigned int subcode = UINTVAL (op);
632+
return arcv_apex_builtins[subcode].insn_suffix;
633+
}
634+
619635
/* Checks if the APEX builtin instruction identified by the subcode
620636
supports the given instruction format.
621637
@@ -734,10 +750,6 @@ arcv_apex_set_insn_operand_flags (unsigned int insn_format, tree fndecl)
734750
static unsigned int
735751
arcv_apex_resolve_insn_format (unsigned int insn_format, unsigned int opcode)
736752
{
737-
/* Return early if a instruction format was defined by the used. */
738-
if ((insn_format & 0xF) != APEX_NONE)
739-
return insn_format;
740-
741753
/* Extract the operand flags (DEST, SRC0, SRC1) from bits 5–7.
742754
These bits encode the operand signature used for format selection. */
743755
unsigned int insn_operands = insn_format >> 5;
@@ -951,15 +963,21 @@ arcv_apex_init_builtin (tree fndecl, const char *fn_name,
951963

952964
/* Resolve the instruction format:
953965
If the user did not specify an instruction format at pragma level,
954-
infer the concrete format based on opcode and operand flags; otherwise,
955-
leave it as is. */
956-
insn_formats = arcv_apex_resolve_insn_format (insn_formats, opcode);
966+
infer the concrete format based on opcode and operand flags. Mark
967+
the insn. name with an "i" suffix for resolved XS/XI/XC formats;
968+
otherwise, leave it as is. */
969+
const char *insn_suffix = "";
970+
if ((insn_formats & 0xF) == APEX_NONE)
971+
{
972+
insn_formats = arcv_apex_resolve_insn_format (insn_formats, opcode);
973+
insn_suffix = "i";
974+
}
957975

958976
/* Validate the format is allowed for this instruction. */
959977
arcv_apex_validate_insn_format (fn_name, insn_formats, opcode);
960978

961979
/* Print .extInstruction section about APEX instruction. */
962-
arcv_apex_print_insn_section (insn_name, opcode, insn_formats);
980+
arcv_apex_print_insn_section (insn_name, insn_suffix, opcode, insn_formats);
963981

964982
/* Determine the internal instruction code (icode). */
965983
enum insn_code icode = arcv_apex_get_icode (insn_formats);
@@ -971,7 +989,7 @@ arcv_apex_init_builtin (tree fndecl, const char *fn_name,
971989

972990
/* Store APEX insn information. */
973991
arcv_apex_builtins[arcv_apex_builtin_index]
974-
= { icode, fn_name, insn_name, builtin_type, insn_formats };
992+
= { icode, fn_name, insn_name, builtin_type, insn_formats, insn_suffix };
975993

976994
/* Modify the prototype type as built-in. */
977995
fndecl->function_decl.built_in_class = BUILT_IN_MD;

gcc/config/riscv/riscv-protos.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -807,8 +807,10 @@ extern bool riscv_is_micro_arch (enum riscv_microarchitecture_type);
807807

808808
extern bool arcv_micro_arch_supports_fusion_p (void);
809809

810-
extern void arcv_apex_print_insn_section (const char *, int, unsigned int);
810+
extern void arcv_apex_print_insn_section (const char *, const char *,
811+
int, unsigned int);
811812
extern const char* arcv_apex_get_insn_name (rtx);
813+
extern const char* arcv_apex_get_insn_suffix (rtx);
812814
extern bool arcv_apex_format_supports_p (unsigned int, unsigned int);
813815
extern void arcv_apex_init_builtin (tree, const char *, const char *,
814816
unsigned int, int);

gcc/config/riscv/riscv.cc

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11013,8 +11013,8 @@ riscv_asm_output_variant_cc (FILE *stream, const tree decl, const char *name)
1101311013
and source1. */
1101411014

1101511015
void
11016-
arcv_apex_print_insn_section (const char *insn_name, int opcode,
11017-
unsigned int insn_format)
11016+
arcv_apex_print_insn_section (const char *insn_name, const char *insn_suffix,
11017+
int opcode, unsigned int insn_format)
1101811018
{
1101911019
/* Extract the operand flags (DEST, SRC0, SRC1) from bits 5–7.
1102011020
These bits encode the operand signature used for format selection. */
@@ -11038,7 +11038,8 @@ arcv_apex_print_insn_section (const char *insn_name, int opcode,
1103811038
if ((insn_format & (APEX_XS | APEX_XC))
1103911039
&& (insn_operands == APEX_DEST_FTYPE_SRC0_SRC1))
1104011040
{
11041-
fprintf (asm_out_file, "\t.extInstruction %si,%d", insn_name, opcode);
11041+
fprintf (asm_out_file, "\t.extInstruction %s%s,%d",
11042+
insn_name, insn_suffix, opcode);
1104211043
if (insn_format & APEX_XS)
1104311044
fputs (",XS", asm_out_file);
1104411045
if (insn_format & APEX_XC)
@@ -11049,7 +11050,8 @@ arcv_apex_print_insn_section (const char *insn_name, int opcode,
1104911050
/* Print XI format with dest, src0 operands; append ",void" if no dest. */
1105011051
if (insn_format & APEX_XI)
1105111052
{
11052-
fprintf (asm_out_file, "\t.extInstruction %si,%d,XI", insn_name, opcode);
11053+
fprintf (asm_out_file, "\t.extInstruction %s%s,%d,XI",
11054+
insn_name, insn_suffix, opcode);
1105311055
if ((insn_format & APEX_DEST) == 0)
1105411056
fputs (",void", asm_out_file);
1105511057
fputc ('\n', asm_out_file);
@@ -11059,8 +11061,8 @@ arcv_apex_print_insn_section (const char *insn_name, int opcode,
1105911061
if ((insn_format & APEX_XS)
1106011062
&& (insn_operands == APEX_VOID_FTYPE_SRC0_SRC1))
1106111063
{
11062-
fprintf (asm_out_file, "\t.extInstruction %si,%d,XS,void\n",
11063-
insn_name, opcode);
11064+
fprintf (asm_out_file, "\t.extInstruction %s%s,%d,XS,void\n",
11065+
insn_name, insn_suffix, opcode);
1106411066
}
1106511067
}
1106611068

0 commit comments

Comments
 (0)