Re: Optimizing nested if statements?

Terry Greyzck <terryg@qwest.net>
21 Jan 2003 00:13:09 -0500

          From comp.compilers

Related articles
Optimizing nested if statements? sperugin@csgrad.cs.vt.edu (Saverio Perugini) (2003-01-17)
Re: Optimizing nested if statements? christian.bau@cbau.freeserve.co.uk (Christian Bau) (2003-01-21)
Re: Optimizing nested if statements? terryg@qwest.net (Terry Greyzck) (2003-01-21)
Re: Optimizing nested if statements? vbdis@aol.com (2003-01-21)
Re: Optimizing nested if statements? sperugin@csgrad.cs.vt.edu (Saverio Perugini) (2003-01-25)
Re: Optimizing nested if statements? Martin.Ward@durham.ac.uk (2003-01-26)
Re: Optimizing nested if statements? sperugin@csgrad.cs.vt.edu (Saverio Perugini) (2003-02-06)
| List of all articles for this month |
From: Terry Greyzck <terryg@qwest.net>
Newsgroups: comp.compilers
Date: 21 Jan 2003 00:13:09 -0500
Organization: Compilers Central
References: 03-01-092
Keywords: optimize
Posted-Date: 21 Jan 2003 00:13:09 EST

Saverio Perugini <sperugin@csgrad.cs.vt.edu> wrote:


>Hello,
>
>Consider the following code:
>
>if (a) {
> if (b) {
> if (c) {
> ...
> } else if (d) {
> ...
> }
> }
>} else if (e) {
> ...
> }
>
>This code is semantically equivalent to
>the following piece of code.
>
>if (a) {
> if (b && c) {
> ...
> } else if (d) {
> ...
> }
>} else if (e) {
> ...
> }


Note that in the original code, condition (d) is not evaluated if !(b) & !(c).
In the modified code, (d) is evaluated for !(b) & !(c), where it was not
before. Condition (d) could contain invalid pointer references, volatile
expressions, and other horrors, so performing the transformation could
very well lead to an incorrect program.


Side effects and bad data aside, the transformed case will evaluate
the (d) code for !(b) & (c) & (d) while the original would not. This is
not a legal transformation.


To better illustrate, expanding the '&&' in the second code fragment
gives the following; note how it differs from the original.


if (a) {
    if (b) {
        if (c) {
            ...
        }
    }
    else if (d) {
        ...
    }
}
else if (e) {
    ...
}


Terry Greyzck
terryg qwest net . @
http://www.greyzck.com


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.