Commit b9298273 by Jim Wilson Committed by Kito Cheng

RISC-V: Optimize si to di zero-extend followed by left shift.

This is potentially a sequence of 3 shifts, we which optimize to a sequence
of 2 shifts.  This can happen when unsigned int is used for array indexing.

	gcc/
	* config/riscv/riscv.md (zero_extendsidi2_shifted): New.

	gcc/testsuite/
	* gcc.target/riscv/zero-extend-5.c: New.
parent 4cc6b0da
...@@ -1817,6 +1817,28 @@ ...@@ -1817,6 +1817,28 @@
operands[2] = GEN_INT (ctz_hwi (INTVAL (operands[2]))); operands[2] = GEN_INT (ctz_hwi (INTVAL (operands[2])));
}) })
;; Handle SImode to DImode zero-extend combined with a left shift. This can
;; occur when unsigned int is used for array indexing. Split this into two
;; shifts. Otherwise we can get 3 shifts.
(define_insn_and_split "zero_extendsidi2_shifted"
[(set (match_operand:DI 0 "register_operand" "=r")
(and:DI (ashift:DI (match_operand:DI 1 "register_operand" "r")
(match_operand:QI 2 "immediate_operand" "I"))
(match_operand 3 "immediate_operand" "")))
(clobber (match_scratch:DI 4 "=&r"))]
"TARGET_64BIT
&& ((INTVAL (operands[3]) >> INTVAL (operands[2])) == 0xffffffff)"
"#"
"&& reload_completed"
[(set (match_dup 4)
(ashift:DI (match_dup 1) (const_int 32)))
(set (match_dup 0)
(lshiftrt:DI (match_dup 4) (match_dup 5)))]
"operands[5] = GEN_INT (32 - (INTVAL (operands [2])));"
[(set_attr "type" "shift")
(set_attr "mode" "DI")])
;; ;;
;; .................... ;; ....................
;; ;;
......
/* { dg-do compile { target { riscv64*-*-* } } } */
/* { dg-options "-march=rv64gc -mabi=lp64 -O2" } */
int
sub (unsigned int i, unsigned int j, unsigned int k, int *array)
{
return array[i] + array[j] + array[k];
}
/* { dg-final { scan-assembler-times "slli" 3 } } */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment