public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* How to write shift and add pattern?
@ 2009-08-28 19:56 Mohamed Shafi
  2009-08-28 23:49 ` Richard Henderson
  0 siblings, 1 reply; 8+ messages in thread
From: Mohamed Shafi @ 2009-08-28 19:56 UTC (permalink / raw)
  To: GCC

Hello all,

I am trying to port a 32bit arch in GCC 4.4.0. My target has support
for 1bit, 2bit shift and add operations. I tried to write patterns for
this , but gcc is not generating those. The following are the patterns
that i have written in md file:

(define_insn "shift_add_<mode>"
 [(set (match_operand:SI 0 "register_operand" "")
       (plus:SI (match_operand:SI 3 "register_operand" "")
                 (ashift:SI (match_operand:SI 1 "register_operand" "")
                             (match_operand:SI 2 "immediate_operand" ""))))]
 ""
 "shadd1\\t%1, %0"
)

(define_insn "shift_add1_<mode>"
 [(set (match_operand:SI 0 "register_operand" "")
       (plus:SI (ashift:SI (match_operand:SI 1 "register_operand" "")
                             (match_operand:SI 2 "immediate_operand" ""))
                 (match_operand:SI 3 "register_operand" "")))]
 ""
 "shadd1\\t%1, %0"
)

(define_insn "shift_n_add_<mode>"
 [(set (match_operand:SI 1 "register_operand" "")
       (ashift:SI (match_dup 1)
                   (match_operand:SI 2 "immediate_operand" "")))
  (set (match_operand:SI 0 "register_operand" "")
       (plus:SI (match_dup 0)
                 (match_dup 1)))]
 ""
 "shadd2\\t%1, %0"
)


As you can see i have tried combinations. Since i was looking for
pattern matching i didnt bother to write according to the target.
Thought i will do that after i get a matching pattern. When i debugged
GCC was generating patterns with multiply. But that gets discarded
since md file doesnt have those patterns. How can i make GCC generate
shift and add pattern? Is GCC generating patterns with multiply due to
cost issues? I havent mentioned any cost details.

Regards,
Shafi

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How to write shift and add pattern?
  2009-08-28 19:56 How to write shift and add pattern? Mohamed Shafi
@ 2009-08-28 23:49 ` Richard Henderson
  2009-11-06 13:30   ` Mohamed Shafi
  0 siblings, 1 reply; 8+ messages in thread
From: Richard Henderson @ 2009-08-28 23:49 UTC (permalink / raw)
  To: Mohamed Shafi; +Cc: GCC

On 08/28/2009 06:51 AM, Mohamed Shafi wrote:
> Hello all,
>
> I am trying to port a 32bit arch in GCC 4.4.0. My target has support
> for 1bit, 2bit shift and add operations. I tried to write patterns for
> this , but gcc is not generating those. The following are the patterns
> that i have written in md file:
>
> (define_insn "shift_add_<mode>"
>   [(set (match_operand:SI 0 "register_operand" "")
>         (plus:SI (match_operand:SI 3 "register_operand" "")
>                   (ashift:SI (match_operand:SI 1 "register_operand" "")
>                               (match_operand:SI 2 "immediate_operand" ""))))]
>   ""
>   "shadd1\\t%1, %0"
> )
...
 > Is GCC generating patterns with multiply due to
 > cost issues? I havent mentioned any cost details.

No, it's merely using multiply because someone way back when
decided that should be the canonical way to represent this.
We canonicalize patterns so that the target file doesn't have
to match both shifts and multiplies.

So your insn should look like:

(define_insn "*shadd1_si"
   [(set (match_operand:SI 0 "register_operand" "r")
         (plus:SI (mult:SI (match_operand:SI "register_operand" "r")
                           (const_int 2))
                  (match_operand:SI "register_operand" "0")))]
   ""
   "shadd1 %1,%0")

This should match even if your target doesn't support a
hardware multiply insn.

See also the shift-add patterns on the Alpha port.  There we
have 2 & 3 bit shifts.  Search for const48_operand to find
those patterns easily.


r~

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How to write shift and add pattern?
  2009-08-28 23:49 ` Richard Henderson
@ 2009-11-06 13:30   ` Mohamed Shafi
  2009-11-06 15:34     ` Ian Lance Taylor
  2009-11-06 16:17     ` Richard Henderson
  0 siblings, 2 replies; 8+ messages in thread
From: Mohamed Shafi @ 2009-11-06 13:30 UTC (permalink / raw)
  To: Richard Henderson; +Cc: GCC

2009/8/28 Richard Henderson <rth@redhat.com>:
> On 08/28/2009 06:51 AM, Mohamed Shafi wrote:
>>
>> Hello all,
>>
>> I am trying to port a 32bit arch in GCC 4.4.0. My target has support
>> for 1bit, 2bit shift and add operations. I tried to write patterns for
>> this , but gcc is not generating those. The following are the patterns
>> that i have written in md file:
>>
>> (define_insn "shift_add_<mode>"
>>  [(set (match_operand:SI 0 "register_operand" "")
>>        (plus:SI (match_operand:SI 3 "register_operand" "")
>>                  (ashift:SI (match_operand:SI 1 "register_operand" "")
>>                              (match_operand:SI 2 "immediate_operand"
>> ""))))]
>>  ""
>>  "shadd1\\t%1, %0"
>> )
>
> ...
>> Is GCC generating patterns with multiply due to
>> cost issues? I havent mentioned any cost details.
>
> No, it's merely using multiply because someone way back when
> decided that should be the canonical way to represent this.
> We canonicalize patterns so that the target file doesn't have
> to match both shifts and multiplies.
>
> So your insn should look like:
>
> (define_insn "*shadd1_si"
>  [(set (match_operand:SI 0 "register_operand" "r")
>        (plus:SI (mult:SI (match_operand:SI "register_operand" "r")
>                          (const_int 2))
>                 (match_operand:SI "register_operand" "0")))]
>  ""
>  "shadd1 %1,%0")
>
> This should match even if your target doesn't support a
> hardware multiply insn.
>
> See also the shift-add patterns on the Alpha port.  There we
> have 2 & 3 bit shifts.  Search for const48_operand to find
> those patterns easily.
>
    The target that i am working on has 1 & 2 bit shift-add patterns.
GCC is not generating shift-add patterns when the shift count is 1. It
is currently generating add operations. What should be done to
generate shift-add pattern instead of add-add pattern?

Another issue is that shift-add pattern will work only with address
registers. So if i have constraints for only address register in the
pattern there will be cases when the normal shift and add instructions
are more profitable than reloading into address registers and doing
sift-add instruction. Am i right ? So what i did was to have both
address register and data registers as constraints and then have a
define split after reload to split shift-add to shift and add when
registers are data registers. But the problem is i cant make the
allocator to allocate address registers for simple cases.

For the following program

extern int a, b;

int foo ()
{
  a = a + (b << 2) ;
  return a;
}

the complier should generate with address registers so that shift-add
pattern can be used. But i cant make the compiler to generate those.
It is generating with data registers. Here is the pattern that i have
written:


(define_insn "*saddl"
  [(set (match_operand:SI 0 "register_operand" "=r,d")
	(plus:SI (mult:SI (match_operand:SI 1 "register_operand" "r,d")
			  (match_operand:SI 2 "const24_operand" "J,J"))
		 (match_operand:SI 3 "register_operand" "0,0")))]

How can i do this. Will the constraint modifiers '?' or '!' help?
How can make GCC generate shift and add sequence when the shift count is 1?

Regards,
Shafi

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How to write shift and add pattern?
  2009-11-06 13:30   ` Mohamed Shafi
@ 2009-11-06 15:34     ` Ian Lance Taylor
  2009-11-09 12:53       ` Mohamed Shafi
  2009-11-06 16:17     ` Richard Henderson
  1 sibling, 1 reply; 8+ messages in thread
From: Ian Lance Taylor @ 2009-11-06 15:34 UTC (permalink / raw)
  To: Mohamed Shafi; +Cc: Richard Henderson, GCC

Mohamed Shafi <shafitvm@gmail.com> writes:

> It is generating with data registers. Here is the pattern that i have
> written:
>
>
> (define_insn "*saddl"
>   [(set (match_operand:SI 0 "register_operand" "=r,d")
> 	(plus:SI (mult:SI (match_operand:SI 1 "register_operand" "r,d")
> 			  (match_operand:SI 2 "const24_operand" "J,J"))
> 		 (match_operand:SI 3 "register_operand" "0,0")))]
>
> How can i do this. Will the constraint modifiers '?' or '!' help?
> How can make GCC generate shift and add sequence when the shift count is 1?

Does 'd' represent a data register?  I assume that 'r' is a general
register, as it always is.  What is the constraint character for an
address register?  You don't seem to have an alternative here for
address registers, so I'm not surprised that the compiler isn't
picking it.  No doubt I misunderstand something.

Ian

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How to write shift and add pattern?
  2009-11-06 13:30   ` Mohamed Shafi
  2009-11-06 15:34     ` Ian Lance Taylor
@ 2009-11-06 16:17     ` Richard Henderson
  2009-11-09 12:49       ` Mohamed Shafi
  1 sibling, 1 reply; 8+ messages in thread
From: Richard Henderson @ 2009-11-06 16:17 UTC (permalink / raw)
  To: Mohamed Shafi; +Cc: GCC

On 11/06/2009 05:29 AM, Mohamed Shafi wrote:
>      The target that i am working on has 1&  2 bit shift-add patterns.
> GCC is not generating shift-add patterns when the shift count is 1. It
> is currently generating add operations. What should be done to
> generate shift-add pattern instead of add-add pattern?

I'm not sure.  You may have to resort to matching

   (set (match_operand 0 "register_operand" "")
        (plus (plus (match_operand 1 "register_operand" "")
		   (match_dup 1))
              (match_operand 2 "register_operand" ""))))

But you should debug make_compound_operation first to
figure out what's going on for your port, because it's
working for x86_64:

	long foo(long a, long b) { return a*2 + b; }

	leaq	(%rsi,%rdi,2), %rax	# 8	*lea_2_rex64
	ret				# 26	return_internal


r~

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How to write shift and add pattern?
  2009-11-06 16:17     ` Richard Henderson
@ 2009-11-09 12:49       ` Mohamed Shafi
  0 siblings, 0 replies; 8+ messages in thread
From: Mohamed Shafi @ 2009-11-09 12:49 UTC (permalink / raw)
  To: Richard Henderson; +Cc: GCC

2009/11/6 Richard Henderson <rth@redhat.com>:
> On 11/06/2009 05:29 AM, Mohamed Shafi wrote:
>>
>>     The target that i am working on has 1&  2 bit shift-add patterns.
>> GCC is not generating shift-add patterns when the shift count is 1. It
>> is currently generating add operations. What should be done to
>> generate shift-add pattern instead of add-add pattern?
>
> I'm not sure.  You may have to resort to matching
>
>  (set (match_operand 0 "register_operand" "")
>       (plus (plus (match_operand 1 "register_operand" "")
>                   (match_dup 1))
>             (match_operand 2 "register_operand" ""))))
>
> But you should debug make_compound_operation first to
> figure out what's going on for your port, because it's
> working for x86_64:
>
>        long foo(long a, long b) { return a*2 + b; }
>
>        leaq    (%rsi,%rdi,2), %rax     # 8     *lea_2_rex64
>        ret                             # 26    return_internal
>
>
> r~
>

   I have fixed this. The culprit was the cost factor. I added the
case in targetm.rtx_costs and now it works properly. But i am having
issues with the reload.

Regards,
Shafi

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How to write shift and add pattern?
  2009-11-06 15:34     ` Ian Lance Taylor
@ 2009-11-09 12:53       ` Mohamed Shafi
  2009-11-09 15:26         ` Ian Lance Taylor
  0 siblings, 1 reply; 8+ messages in thread
From: Mohamed Shafi @ 2009-11-09 12:53 UTC (permalink / raw)
  To: Ian Lance Taylor; +Cc: Richard Henderson, GCC

2009/11/6 Ian Lance Taylor <iant@google.com>:
> Mohamed Shafi <shafitvm@gmail.com> writes:
>
>> It is generating with data registers. Here is the pattern that i have
>> written:
>>
>>
>> (define_insn "*saddl"
>>   [(set (match_operand:SI 0 "register_operand" "=r,d")
>>       (plus:SI (mult:SI (match_operand:SI 1 "register_operand" "r,d")
>>                         (match_operand:SI 2 "const24_operand" "J,J"))
>>                (match_operand:SI 3 "register_operand" "0,0")))]
>>
>> How can i do this. Will the constraint modifiers '?' or '!' help?
>> How can make GCC generate shift and add sequence when the shift count is 1?
>
> Does 'd' represent a data register?  I assume that 'r' is a general
> register, as it always is.  What is the constraint character for an
> address register?  You don't seem to have an alternative here for
> address registers, so I'm not surprised that the compiler isn't
> picking it.  No doubt I misunderstand something.
>
   Ok the constrain for address register is 'a'. Thats typo in the
pattern that i given here. The proper pattern is

 (define_insn "*saddl"
   [(set (match_operand:SI 0 "register_operand" "=a,d")
       (plus:SI (mult:SI (match_operand:SI 1 "register_operand" "a,d")
                         (match_operand:SI 2 "const24_operand" "J,J"))
                (match_operand:SI 3 "register_operand" "0,0")))]

So how can i choose the address registers over data registers if that
is more profitable?

Regards,
Shafi

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How to write shift and add pattern?
  2009-11-09 12:53       ` Mohamed Shafi
@ 2009-11-09 15:26         ` Ian Lance Taylor
  0 siblings, 0 replies; 8+ messages in thread
From: Ian Lance Taylor @ 2009-11-09 15:26 UTC (permalink / raw)
  To: Mohamed Shafi; +Cc: Richard Henderson, GCC

Mohamed Shafi <shafitvm@gmail.com> writes:

>    Ok the constrain for address register is 'a'. Thats typo in the
> pattern that i given here. The proper pattern is
>
>  (define_insn "*saddl"
>    [(set (match_operand:SI 0 "register_operand" "=a,d")
>        (plus:SI (mult:SI (match_operand:SI 1 "register_operand" "a,d")
>                          (match_operand:SI 2 "const24_operand" "J,J"))
>                 (match_operand:SI 3 "register_operand" "0,0")))]
>
> So how can i choose the address registers over data registers if that
> is more profitable?

If it is cheaper to move two values from data registers to address
registers than it is to use two data registers for this instruction,
then you should omit the d,d,J alternative.

If it is cheaper to move one value from a data register to an address
register than it is to use two data registers for this instruction,
then you should use !, as in "=a,!d".

Otherwise you should use ?, as in "=a,?d".  You can additional ?'s for
additional tuning if appropriate.

Ian

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2009-11-09 15:26 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-08-28 19:56 How to write shift and add pattern? Mohamed Shafi
2009-08-28 23:49 ` Richard Henderson
2009-11-06 13:30   ` Mohamed Shafi
2009-11-06 15:34     ` Ian Lance Taylor
2009-11-09 12:53       ` Mohamed Shafi
2009-11-09 15:26         ` Ian Lance Taylor
2009-11-06 16:17     ` Richard Henderson
2009-11-09 12:49       ` Mohamed Shafi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).