This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: swap instruction generation
- From: naga raj <gnuuser dot raj at gmail dot com>
- To: Ian Lance Taylor <iant at google dot com>
- Cc: gcc-help at gcc dot gnu dot org
- Date: Wed, 21 Sep 2011 11:53:51 +0530
- Subject: Re: swap instruction generation
- References: <BANLkTikUN3NOPqePJU5YKpTyizWzasOyKQ@mail.gmail.com> <mcrzknnfkxw.fsf@google.com>
Hi Ian,
I am using Gcc-4.6.0 and I have used bswap RTL pattern for both SI
and HI modes to generate swapb & swaph instructions respectively.
(define_insn "bswapsi2"
[(set (match_operand:SI 0 "register_operand" "=r")
(bswap:SI (match_operand:SI 1 "register_operand" "r")))]
""
"swapb %0, %1"
)
(define_insn "bswaphi2"
[(set (match_operand:HI 0 "register_operand" "=r")
(bswap:HI (match_operand:HI 1 "register_operand" "r")))]
""
"swaph %0, %1"
)
I have written a sample example to generate these instructions..
int swapb(int n)
{
return ((((n) & 0xff000000) >> 24)
| (((n) & 0x00ff0000) >> 8)
| (((n) & 0x0000ff00) << 8)
| (((n) & 0x000000ff) << 24));
}
short int swaph(short int n)
{
return ((((n) & 0xff00) >> 8)
| (((n) & 0xff) << 8));
}
int main()
{
volatile int a=0x12345678;
volatile short int b=0x1234;
a=swapb(a);
b=swaph(b);
return 0;
}
with this example "swapb" instruction has generated but I am unable to
generate "swaph"(HI mode of bswap RTL pattern) instruction
I have tried all possibilities that I know.
Am I missing something or this approach is wrong.
Please guide me to generate swaph instruction.
Thanks in Advance,
Nag
On Tue, Apr 19, 2011 at 12:25 AM, Ian Lance Taylor <iant@google.com> wrote:
> naga raj <gnuuser.raj@gmail.com> writes:
>
>> ? I need to emit a swap instruction from a embedded target which is
>> using Gcc-4.1.2.
>>
>> ? I am trying to emit a instruction which will perform following operation
>>
>> ? R0=0x12345678;
>>
>> ? swap R1,R0
>>
>> ? Then after this R1 should contain R1= 0x00007856;
>
> Not 0x78563421?
>
>> ?As there is no bswap rtl expression in Gcc-4.1.2 I have tried various
>> alternatives like:
>>
>> ?1. Used rotate rtl expression
>>
>> ? 2. ashift,ashiftrt and or to generate the above instruction
>>
>>
>> ?but compiler is not generating(emitting) swap instruction.
>
> Current gcc has a bswap RTL pattern which swaps bytes in a word. ?It was
> added in gcc 4.3. ?That's the thing to use if your instruction actually
> swaps bytes in a word. ?For versions of gcc before that you will have to
> use an intrinsic function.
>
> Ian
>