Re: if() body folding, #pragma rarely_used

Rogier Wolff (R.E.Wolff@BitWizard.nl)
Thu, 11 Sep 1997 18:03:52 +0200 (MET DST)


Janos Farkas wrote:
>
>
> So, if that was too short; my problem can be seen with the following code:
>
> ret = 0;
> x=kmalloc(whatever);
> if (!x) {
> printk("no x\n");
> return ret; /* common */
> }
>
> bh = bread(whatever);
> if (!bh) {
> printk("no bh\n");
> kfree(x); /* common */
> return ret; /* common */
> }
>
> ret = dosomething()
>
> brelse(bh);
> kfree(x); /* common */
> return ret; /* common */
>
> It's not really a good example, since nesting if's is quite easy in the
> above case; but if somehow I'm forced to the above, GCC won't notice
> that the parts marked /* common */ are the same and can be folded to
> the end of the function.

A long, long time ago, I was debugging a Turbo C program:

status = read_a_line ();
while (status = ok) {
/* process line */
status = read_a_line ();
}

You could do this with a do/while, but then you'd have to
conditionalize the process line part. Anyway, it turns out that
turbo C recognizes the common code and jumped right into the
middle of the loop:

goto nextline:
while (status = ok) {
/* process line */
nextline:
status = read_a_line ();
}

It scared the hell out of me when I was debugging this, it didn't
execute the last instructions before the loop, and started somewhere
in the loop. Took a while to figure out that it had rewritten my code
a little and that it was still as I intended.

Moral: Yes it is possible to detect common pieces of code and to
emit those pieces only once.....

Roger.

-- 
** R.E.Wolff@BitWizard.nl ** +31-15-2137555 ** http://www.BitWizard.nl/ **
Florida -- A 39 year old construction worker woke up this morning when a
109-car freigt train drove over him. According to the police the man was 
drunk. The man himself claims he slipped while walking the dog. 080897