Don't force target of modulo into a distinct register.
The define_insns for the modulo operation currently force the target
register
to a distinct reg in preparation for a possible future peephole combining
div/mod. But this can lead to cases of a needless copy being inserted. Fixed
with the following patch.
Bootstrapped and regression tested on powerpc64le.
Ok for master?
-Pat
2023-02-27 Pat Haugen <pthau...@linux.ibm.com>
gcc/
* config/rs6000/rs6000.md (*mod<mode>3, umod<mode>3): Add
non-earlyclobber alternative.
gcc/testsuite/
* gcc.target/powerpc/mod-no_copy.c: New.
diff --git a/gcc/config/rs6000/rs6000.md b/gcc/config/rs6000/rs6000.md
index 81bffb04ceb..44f7dd509cb 100644
--- a/gcc/config/rs6000/rs6000.md
+++ b/gcc/config/rs6000/rs6000.md
@@ -3437,9 +3437,9 @@ (define_expand "mod<mode>3"
;; In order to enable using a peephole2 for combining div/mod to
eliminate the
;; mod, prefer putting the result of mod into a different register
(define_insn "*mod<mode>3"
- [(set (match_operand:GPR 0 "gpc_reg_operand" "=&r")
- (mod:GPR (match_operand:GPR 1 "gpc_reg_operand" "r")
- (match_operand:GPR 2 "gpc_reg_operand" "r")))]
+ [(set (match_operand:GPR 0 "gpc_reg_operand" "=&r,r")
+ (mod:GPR (match_operand:GPR 1 "gpc_reg_operand" "r,r")
+ (match_operand:GPR 2 "gpc_reg_operand" "r,r")))]
"TARGET_MODULO"
"mods<wd> %0,%1,%2"
[(set_attr "type" "div")
@@ -3447,9 +3447,9 @@ (define_insn "*mod<mode>3"
(define_insn "umod<mode>3"
- [(set (match_operand:GPR 0 "gpc_reg_operand" "=&r")
- (umod:GPR (match_operand:GPR 1 "gpc_reg_operand" "r")
- (match_operand:GPR 2 "gpc_reg_operand" "r")))]
+ [(set (match_operand:GPR 0 "gpc_reg_operand" "=&r,r")
+ (umod:GPR (match_operand:GPR 1 "gpc_reg_operand" "r,r")
+ (match_operand:GPR 2 "gpc_reg_operand" "r,r")))]
"TARGET_MODULO"
"modu<wd> %0,%1,%2"
[(set_attr "type" "div")
diff --git a/gcc/testsuite/gcc.target/powerpc/mod-no_copy.c
b/gcc/testsuite/gcc.target/powerpc/mod-no_copy.c
new file mode 100644
index 00000000000..91e3003b3fc
--- /dev/null
+++ b/gcc/testsuite/gcc.target/powerpc/mod-no_copy.c
@@ -0,0 +1,17 @@
+/* { dg-do compile { target { powerpc*-*-* } } } */
+/* { dg-require-effective-target powerpc_p9modulo_ok } */
+/* { dg-options "-mdejagnu-cpu=power9 -O2" } */
+
+/* Verify r3 is used as source and target, no copy inserted. */
+
+long foo (long a, long b)
+{
+ return (a % b);
+}
+
+unsigned long foo2 (unsigned long a, unsigned long b)
+{
+ return (a % b);
+}
+
+/* { dg-final { scan-assembler-not {\mmr\M} } } */