------- Additional Comments From dberlin at gcc dot gnu dot org 2005-01-15
00:22 -------
Subject: Re: New: missed optimization with
ifs and deferencing
On Thu, 2005-01-13 at 21:38 +0000, pinskia at gcc dot gnu dot org wrote:
> I found this while looking into PR 8361 for missed optimization.
> The following two programs should produce the same asm:
> int f(int k, int i1, int j1)
> {
> int *f1;
> if(k)
> f1 = &i1;
> else
> f1 = &j1;
> return *f1;
> }
The only way you are going to get this to optimize is to make it see we
are derefencing a phi whose arguments are all is_gimple_min_invariant,
substitute the value in, merge it, and then use the that at the
derefence point.
This is actually just a special case of noticing that we are really
doing *&<something> in both cases, and removing the *& part, while
leaving the rest.
IE transform it like so
Step 0:
f (k, i1, j1)
{
int * f1;
int D.1116;
<bb 0>:
if (k_2 != 0) goto <L2>; else goto <L4>;
<L4>:;
# f1_1 = PHI <&i1(0), &j1(1)>;
<L2>:;
# VUSE <i1_7>;
# VUSE <j1_8>;
D.1116_3 = *f1_1;
return D.1116_3;
}
Step 1 (new blocks added for clarity)
f (k, i1, j1)
{
int * f1;
int D.1116;
<bb 0>:
if (k_2 != 0) goto <L3>; else goto <L4>;
<L4>:;
newtemp_2 = i1;
goto L2
<L3>
newtemp_1 = j1;
f1_1 = PHI<&i1(0), &j1(1)>
<L2>:;
# VUSE <i1_7>;
# VUSE <j1_8>;
D.1116_3 = *f1_1;
return D.1116_3;
}
Step 2:
f (k, i1, j1)
{
int * f1;
int D.1116;
<bb 0>:
if (k_2 != 0) goto <L3>; else goto <L4>;
<L4>:;
newtemp_2 = i1;
goto L2
<L3>
newtemp_1 = j1;
newphi = PHI<newtemp_2(0), newtemp_1(1)>
<L2>:;
# VUSE <i1_7>;
# VUSE <j1_8>;
D.1116_3 = *f1_1;
return D.1116_3;
}
Step 3:
f (k, i1, j1)
{
int * f1;
int D.1116;
<bb 0>:
if (k_2 != 0) goto <L3>; else goto <L4>;
<L4>:;
newtemp_2 = i1;
goto L2
<L3>
newtemp_1 = j1;
newphi = PHI<newtemp_2(0), newtemp_1(1)>
<L2>:;
D.1116_3 = newphi;
return D.1116_3;
}
Notice that all i've done is simply match up that the dereference was of
a phi node whoes arguments are all &something.
In that specific case, we can remove all the addressing operations and
the dereference use, and just replace it with the values.
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=19431