Skip to content

Commit

Permalink
Fix for Issue openhwgroup#77
Browse files Browse the repository at this point in the history
Since the constraint `m` should assume non-xcvmem specific operands,
we redefined TARGET_MEM_CONSTRAINT and defined 'm' to exclude XCVmem
specific operands.
Constraint `CVmp` defines addresses for post modify operands.
Constraint `CVmr` defines addresses for reg + reg operands.

Files Changed:

   * gcc/config/riscv/predicates.md (mem_post_inc): Prevent DI and
     DF mode mem.
     (mem_plus_reg): Likewise.
   * gcc/config/riscv/constraints.md: Add new constraints `m`,
     `CVmp` and `CVmr`.
   * gcc/config/riscv/riscv.md: Remove constraint `am`.
   * gcc/config/riscv/riscv.h: Redefine TARGET_MEM_CONSTRAINT to
     `w`.
   * gcc/testsuite/gcc.target/riscv/cv-mem-compile-1.c: New test.
   * gcc/testsuite/gcc.target/riscv/cv-mem-compile-2.c: Likewise.
   * gcc/testsuite/gcc.target/riscv/cv-mem-compile-3.c: Likewise.
  • Loading branch information
Mary Bennett committed Nov 2, 2023
1 parent b14af4e commit 4306fbe
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 30 deletions.
30 changes: 25 additions & 5 deletions gcc/config/riscv/constraints.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@

;; General constraints

(define_memory_constraint "m"
"An address that is not base reg + index reg or post modify."
(and (match_code "mem")
(and (match_test "memory_address_addr_space_p (GET_MODE (op), XEXP (op, 0),
MEM_ADDR_SPACE (op))")
(not (match_test "(GET_CODE (XEXP (op, 0)) == PLUS
&& GET_CODE (XEXP (XEXP (op, 0), 0)) == REG
&& GET_CODE (XEXP (XEXP (op, 0), 1)) == REG)
|| GET_CODE (XEXP (op, 0)) == POST_MODIFY")))))

(define_constraint "I"
"An I-type 12-bit signed immediate."
(and (match_code "const_int")
Expand Down Expand Up @@ -128,11 +138,6 @@
(and (match_code "mem")
(match_test "GET_CODE(XEXP(op,0)) == REG")))

(define_memory_constraint "am"
"An address that is held in a general-purpose register."
(and (match_code "mem")
(match_test "!(GET_CODE(XEXP(op,0)) == PLUS && GET_CODE(XEXP(XEXP(op,0),0)) == REG && GET_CODE(XEXP(XEXP(op,0),1)) == REG)")))

(define_constraint "S"
"A constraint that matches an absolute symbolic address."
(match_operand 0 "absolute_symbolic_operand"))
Expand Down Expand Up @@ -168,6 +173,21 @@
(and (match_test "IN_RANGE (ival, 0, 1073741823)")
(match_test "exact_log2 (ival + 1) != -1"))))

(define_constraint "CVmr"
"An address for reg+reg stores and loads"
(and (match_code "mem")
(match_test "GET_CODE (XEXP (op, 0)) == PLUS
&& GET_CODE (XEXP (XEXP (op, 0), 0)) == REG
&& GET_CODE (XEXP (XEXP (op, 0), 1)) == REG")))

(define_constraint "CVmp"
"An address for post-modify or reg+reg stores and loads"
(and (match_code "mem")
(match_test "(GET_CODE (XEXP (op, 0)) == PLUS
&& GET_CODE (XEXP (XEXP (op, 0), 0)) == REG
&& GET_CODE (XEXP (XEXP (op, 0), 1)) == REG)
|| GET_CODE (XEXP (op, 0)) == POST_MODIFY")))

(define_constraint "MVs10"
"A 10-bit unsigned immediate for CORE-V bitmanip."
(and (match_code "const_int")
Expand Down
44 changes: 22 additions & 22 deletions gcc/config/riscv/corev.md
Original file line number Diff line number Diff line change
Expand Up @@ -2983,23 +2983,23 @@
;; Post Increment Register-Immediate and Register-Register Load/Store
(define_insn "cv_load<mode>_postinc"
[(set (match_operand:ANYI 0 "register_operand" "=r")
(match_operand:ANYI 1 "mem_post_inc" "m"))]
(match_operand:ANYI 1 "mem_post_inc" "CVmp"))]
"TARGET_XCVMEM && riscv_legitimate_xcvmem_address_p (<MODE>mode, XEXP (operands[1], 0), (lra_in_progress || reload_completed))"
"cv.<load>\t%0,%1"
[(set_attr "type" "load")
(set_attr "mode" "<MODE>")])

(define_insn "cv_load_<optab><SHORT:mode>_postinc"
[(set (match_operand:SI 0 "register_operand" "=r")
(any_extend:SI (match_operand:SHORT 1 "mem_post_inc" "m")))]
(any_extend:SI (match_operand:SHORT 1 "mem_post_inc" "CVmp")))]
"TARGET_XCVMEM && riscv_legitimate_xcvmem_address_p (<MODE>mode, XEXP (operands[1], 0), (lra_in_progress || reload_completed))"
"cv.<load><u>\t%0,%1"
[(set_attr "type" "load")
(set_attr "mode" "<MODE>")])

(define_insn "cv_loadsf_postinc_hardfloat"
[(set (match_operand:SF 0 "register_operand" "=r")
(match_operand:SF 1 "mem_post_inc" "m"))]
(match_operand:SF 1 "mem_post_inc" "CVmp"))]
"TARGET_HARD_FLOAT && TARGET_XCVMEM
&& riscv_legitimate_xcvmem_address_p (SFmode, XEXP (operands[1], 0), (lra_in_progress || reload_completed))
&& (register_operand (operands[0], SFmode)
Expand All @@ -3010,7 +3010,7 @@

(define_insn "cv_loadsf_postinc_softfloat"
[(set (match_operand:SF 0 "register_operand" "=r")
(match_operand:SF 1 "mem_post_inc" "m"))]
(match_operand:SF 1 "mem_post_inc" "CVmp"))]
"!TARGET_HARD_FLOAT && TARGET_XCVMEM
&& riscv_legitimate_xcvmem_address_p (SFmode, XEXP (operands[1], 0), (lra_in_progress || reload_completed))
&& (register_operand (operands[0], SFmode)
Expand All @@ -3021,7 +3021,7 @@

(define_insn "cv_loadhf_postinc_hardfloat"
[(set (match_operand:HF 0 "register_operand" "=r")
(match_operand:HF 1 "mem_post_inc" "m"))]
(match_operand:HF 1 "mem_post_inc" "CVmp"))]
"TARGET_HARD_FLOAT && TARGET_XCVMEM
&& riscv_legitimate_xcvmem_address_p (HFmode, XEXP (operands[1], 0), (lra_in_progress || reload_completed))
&& (register_operand (operands[0], HFmode)
Expand All @@ -3032,7 +3032,7 @@

(define_insn "cv_loadhf_postinc_softfloat"
[(set (match_operand:HF 0 "register_operand" "=r")
(match_operand:HF 1 "mem_post_inc" "m"))]
(match_operand:HF 1 "mem_post_inc" "CVmp"))]
"!TARGET_HARD_FLOAT && TARGET_XCVMEM
&& riscv_legitimate_xcvmem_address_p (HFmode, XEXP (operands[1], 0), (lra_in_progress || reload_completed))
&& (register_operand (operands[0], HFmode)
Expand All @@ -3042,15 +3042,15 @@
(set_attr "mode" "HF")])

(define_insn "cv_store<mode>_postinc"
[(set (match_operand:ANYI 0 "mem_post_inc" "=m")
[(set (match_operand:ANYI 0 "mem_post_inc" "=CVmp")
(match_operand:ANYI 1 "register_operand" "r"))]
"TARGET_XCVMEM && riscv_legitimate_xcvmem_address_p (<MODE>mode, XEXP (operands[0], 0), (lra_in_progress || reload_completed))"
"cv.<store>\t%1,%0"
[(set_attr "type" "store")
(set_attr "mode" "<MODE>")])

(define_insn "cv_storesf_postinc_hardfloat"
[(set (match_operand:SF 0 "mem_post_inc" "=m")
[(set (match_operand:SF 0 "mem_post_inc" "=CVmp")
(match_operand:SF 1 "register_operand" "r"))]
"TARGET_HARD_FLOAT && TARGET_XCVMEM
&& riscv_legitimate_xcvmem_address_p (SFmode, XEXP (operands[0], 0), (lra_in_progress || reload_completed))
Expand All @@ -3061,7 +3061,7 @@
(set_attr "mode" "SF")])

(define_insn "cv_storesf_postinc_softfloat"
[(set (match_operand:SF 0 "mem_post_inc" "=m")
[(set (match_operand:SF 0 "mem_post_inc" "=CVmp")
(match_operand:SF 1 "register_operand" "r"))]
"!TARGET_HARD_FLOAT && TARGET_XCVMEM
&& riscv_legitimate_xcvmem_address_p (SFmode, XEXP (operands[0], 0), (lra_in_progress || reload_completed))
Expand All @@ -3072,7 +3072,7 @@
(set_attr "mode" "SF")])

(define_insn "cv_storehf_postinc_hardfloat"
[(set (match_operand:HF 0 "mem_post_inc" "=m")
[(set (match_operand:HF 0 "mem_post_inc" "=CVmp")
(match_operand:HF 1 "register_operand" "r"))]
"TARGET_HARD_FLOAT && TARGET_XCVMEM
&& riscv_legitimate_xcvmem_address_p (HFmode, XEXP (operands[0], 0), (lra_in_progress || reload_completed))
Expand All @@ -3083,7 +3083,7 @@
(set_attr "mode" "HF")])

(define_insn "cv_storehf_postinc_softfloat"
[(set (match_operand:HF 0 "mem_post_inc" "=m")
[(set (match_operand:HF 0 "mem_post_inc" "=CVmp")
(match_operand:HF 1 "register_operand" "r"))]
"!TARGET_HARD_FLOAT && TARGET_XCVMEM
&& riscv_legitimate_xcvmem_address_p (HFmode, XEXP (operands[0], 0), (lra_in_progress || reload_completed))
Expand All @@ -3096,23 +3096,23 @@
;; Normal Register-Register Load/Store
(define_insn "cv_load<mode>"
[(set (match_operand:ANYI 0 "register_operand" "=r")
(match_operand:ANYI 1 "mem_plus_reg" "m"))]
(match_operand:ANYI 1 "mem_plus_reg" "CVmr"))]
"TARGET_XCVMEM && riscv_legitimate_xcvmem_address_p (<MODE>mode, XEXP (operands[1], 0), (lra_in_progress || reload_completed))"
"cv.<load>\t%0,%1"
[(set_attr "type" "load")
(set_attr "mode" "<MODE>")])

(define_insn "cv_load_<optab><SHORT:mode>"
[(set (match_operand:SI 0 "register_operand" "=r")
(any_extend:SI (match_operand:SHORT 1 "mem_plus_reg" "m")))]
(any_extend:SI (match_operand:SHORT 1 "mem_plus_reg" "CVmr")))]
"TARGET_XCVMEM && riscv_legitimate_xcvmem_address_p (<MODE>mode, XEXP (operands[1], 0), (lra_in_progress || reload_completed))"
"cv.<load><u>\t%0,%1"
[(set_attr "type" "load")
(set_attr "mode" "<MODE>")])

(define_insn "cv_loadsf_hardfloat"
[(set (match_operand:SF 0 "register_operand" "=r")
(match_operand:SF 1 "mem_plus_reg" "m"))]
(match_operand:SF 1 "mem_plus_reg" "CVmr"))]
"TARGET_HARD_FLOAT && TARGET_XCVMEM
&& riscv_legitimate_xcvmem_address_p (SFmode, XEXP (operands[1], 0), (lra_in_progress || reload_completed))
&& (register_operand (operands[0], SFmode)
Expand All @@ -3123,7 +3123,7 @@

(define_insn "cv_loadsf_softfloat"
[(set (match_operand:SF 0 "register_operand" "=r")
(match_operand:SF 1 "mem_plus_reg" "m"))]
(match_operand:SF 1 "mem_plus_reg" "CVmr"))]
"!TARGET_HARD_FLOAT && TARGET_XCVMEM
&& riscv_legitimate_xcvmem_address_p (SFmode, XEXP (operands[1], 0), (lra_in_progress || reload_completed))
&& (register_operand (operands[0], SFmode)
Expand All @@ -3134,7 +3134,7 @@

(define_insn "cv_loadhf_hardfloat"
[(set (match_operand:HF 0 "register_operand" "=r")
(match_operand:HF 1 "mem_plus_reg" "m"))]
(match_operand:HF 1 "mem_plus_reg" "CVmr"))]
"TARGET_HARD_FLOAT && TARGET_XCVMEM
&& riscv_legitimate_xcvmem_address_p (HFmode, XEXP (operands[1], 0), (lra_in_progress || reload_completed))
&& (register_operand (operands[0], HFmode)
Expand All @@ -3145,7 +3145,7 @@

(define_insn "cv_loadhf_softfloat"
[(set (match_operand:HF 0 "register_operand" "=r")
(match_operand:HF 1 "mem_plus_reg" "m"))]
(match_operand:HF 1 "mem_plus_reg" "CVmr"))]
"!TARGET_HARD_FLOAT && TARGET_XCVMEM
&& riscv_legitimate_xcvmem_address_p (HFmode, XEXP (operands[1], 0), (lra_in_progress || reload_completed))
&& (register_operand (operands[0], HFmode)
Expand All @@ -3155,15 +3155,15 @@
(set_attr "mode" "HF")])

(define_insn "cv_store<mode>"
[(set (match_operand:ANYI 0 "mem_plus_reg" "=m")
[(set (match_operand:ANYI 0 "mem_plus_reg" "=CVmr")
(match_operand:ANYI 1 "register_operand" "r"))]
"TARGET_XCVMEM && riscv_legitimate_xcvmem_address_p (<MODE>mode, XEXP (operands[0], 0), (lra_in_progress || reload_completed))"
"cv.<store>\t%1,%0"
[(set_attr "type" "store")
(set_attr "mode" "<MODE>")])

(define_insn "cv_storesf_hardfloat"
[(set (match_operand:SF 0 "mem_plus_reg" "=m")
[(set (match_operand:SF 0 "mem_plus_reg" "=CVmr")
(match_operand:SF 1 "register_operand" " r"))]
"TARGET_HARD_FLOAT && TARGET_XCVMEM
&& riscv_legitimate_xcvmem_address_p (SFmode, XEXP (operands[0], 0), (lra_in_progress || reload_completed))
Expand All @@ -3174,7 +3174,7 @@
(set_attr "mode" "SF")])

(define_insn "cv_storesf_softfloat"
[(set (match_operand:SF 0 "mem_plus_reg" "=m")
[(set (match_operand:SF 0 "mem_plus_reg" "=CVmr")
(match_operand:SF 1 "register_operand" " r"))]
"!TARGET_HARD_FLOAT && TARGET_XCVMEM
&& riscv_legitimate_xcvmem_address_p (SFmode, XEXP (operands[0], 0), (lra_in_progress || reload_completed))
Expand All @@ -3185,7 +3185,7 @@
(set_attr "mode" "SF")])

(define_insn "cv_storehf_hardfloat"
[(set (match_operand:HF 0 "mem_plus_reg" "=m")
[(set (match_operand:HF 0 "mem_plus_reg" "=CVmr")
(match_operand:HF 1 "register_operand" " r"))]
"TARGET_HARD_FLOAT && TARGET_XCVMEM
&& riscv_legitimate_xcvmem_address_p (HFmode, XEXP (operands[0], 0), (lra_in_progress || reload_completed))
Expand All @@ -3196,7 +3196,7 @@
(set_attr "mode" "HF")])

(define_insn "cv_storehf_softfloat"
[(set (match_operand:HF 0 "mem_plus_reg" "=m")
[(set (match_operand:HF 0 "mem_plus_reg" "=CVmr")
(match_operand:HF 1 "register_operand" " r"))]
"!TARGET_HARD_FLOAT && TARGET_XCVMEM
&& riscv_legitimate_xcvmem_address_p (HFmode, XEXP (operands[0], 0), (lra_in_progress || reload_completed))
Expand Down
9 changes: 7 additions & 2 deletions gcc/config/riscv/predicates.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,16 @@

(define_predicate "mem_post_inc"
(and (match_code "mem")
(match_test "TARGET_XCVMEM && GET_CODE (XEXP (op, 0)) == POST_MODIFY")))
(match_test "TARGET_XCVMEM && GET_CODE (XEXP (op, 0)) == POST_MODIFY
&& GET_MODE (op) != DImode
&& GET_MODE (op) != DFmode
&& GET_MODE (op) != TImode
&& GET_MODE (op) != TFmode")))

(define_predicate "mem_plus_reg"
(and (match_code "mem")
(match_test "GET_CODE (XEXP (op, 0)) == PLUS
(match_test "TARGET_XCVMEM && GET_CODE (XEXP (op, 0)) == PLUS
&& GET_MODE_SIZE (GET_MODE (op)).to_constant () <= 4
&& REG_P (XEXP (XEXP (op, 0), 1))
&& REG_P (XEXP (XEXP (op, 0), 0))")))

Expand Down
2 changes: 2 additions & 0 deletions gcc/config/riscv/riscv.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ along with GCC; see the file COPYING3. If not see
#define RISCV_TUNE_STRING_DEFAULT "rocket"
#endif

#define TARGET_MEM_CONSTRAINT 'w'

extern const char *riscv_expand_arch (int argc, const char **argv);
extern const char *riscv_expand_arch_from_cpu (int argc, const char **argv);
extern const char *riscv_default_mtune (int argc, const char **argv);
Expand Down
2 changes: 1 addition & 1 deletion gcc/config/riscv/riscv.md
Original file line number Diff line number Diff line change
Expand Up @@ -2176,7 +2176,7 @@

(define_insn "*movsf_hardfloat"
[(set (match_operand:SF 0 "nonimmediate_nonpostinc" "=f, f,f,f,m,m,*f,*r, *r,*r,*m")
(match_operand:SF 1 "move_operand" " f,zfli,G,am,f,G,*r,*f,*G*r,*m,*r"))]
(match_operand:SF 1 "move_operand" " f,zfli,G,m,f,G,*r,*f,*G*r,*m,*r"))]
"TARGET_HARD_FLOAT
&& (register_operand (operands[0], SFmode)
|| reg_or_0_operand (operands[1], SFmode))"
Expand Down
17 changes: 17 additions & 0 deletions gcc/testsuite/gcc.target/riscv/cv-mem-compile-1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* { dg-do compile } */
/* { dg-options "-march=rv32i_xcvmem -mabi=ilp32 -g -O2" } */
/* { dg-skip-if "" { *-*-* } { "-O0" } { "" } } */

/* Test for illegal generation of pattern: `(mem:DF (plus:SI (reg reg)))`.
*/

struct {
double a[3];
} * b;
int c;

int
foo (void)
{
b[0].a[c] -= b[0].a[c];
}
18 changes: 18 additions & 0 deletions gcc/testsuite/gcc.target/riscv/cv-mem-compile-2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* { dg-do compile } */
/* { dg-options "-march=rv32i_xcvmem -mabi=ilp32 -g -O2" } */
/* { dg-skip-if "" { *-*-* } { "-O0" } { "" } } */

/* Test for illegal pattern: `(mem:DF (post_modify:SI (reg reg)))`.
*/

int bar (double);

int
foo (void)
{
double *b;
int c = 0;
for (;; c++)
bar (b[c]);
}

26 changes: 26 additions & 0 deletions gcc/testsuite/gcc.target/riscv/cv-mem-compile-3.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* { dg-do compile } */
/* { dg-options "-march=rv32i_xcvmem -mabi=ilp32 -g -O2 -Wno-int-conversion" } */
/* { dg-skip-if "" { *-*-* } { "-O0" } { "" } } */

/* Test for illegal assembly instruction `lbu reg,reg(reg)`.
*/

int a;
int bar (char);

int
foo (void)
{
short *d;
char *e = (char *)foo;
for (;;) {
char c = d++;
bar (c);
short b = e[0] + b;
if (b)
a = 5;
e += 2;
}
}

/* { dg-final { scan-assembler-not "lbu\t\[a-z\]\[0-99\],\[a-z\]\[0-99\]\\(\[a-z\]\[0-99\]\\)" } } */

0 comments on commit 4306fbe

Please sign in to comment.