A question about integer promotion in GCC
Jie Zhang
zhangjie@magima.com.cn
Fri Sep 3 08:33:00 GMT 2004
Joseph S. Myers wrote:
> On Thu, 2 Sep 2004, Jie Zhang wrote:
>>According to this, shouldn't it be:
>>
>> return (int)x << 8 | (int)x >> 8;
>>
>
> The current function of tree dumps is for debugging the compiler, not as a
> representation of source. Various optimisations are performed on the
> trees generated, both in the process of generating them to avoid
> generating unnecessary garbage, and as part of fold(), before they get to
> the first tree dumps. (In this case, a right shift of a short can be
> represented directly on the short, whereas a left shift of a short
> cannot.)
>
I think optimizing "(int)x >> 8" to "(int)(x >> 8)" is not good, at least for RISCs, like MIPS. Now gcc (3.4.0) generates the following assembly with option -O0:
lhu $2,0($fp)
sll $2,$2,8
lhu $3,0($fp)
srl $3,$3,8
andi $3,$3,0xffff
or $2,$2,$3
IMO, the "andi" instruction is useless. I suspect it's due to this optimization. Without knowing it this assembly is confusing.
> There is a mood towards doing less such optimisations at parse time (and
I like this idea. It will make both the structure of GCC and the -O0 assembly output more clear.
regards
--
Jie
More information about the Gcc
mailing list