[tree-ssa] Minor improvement to folding
law@redhat.com
law@redhat.com
Thu Feb 26 20:00:00 GMT 2004
I had this bootstrapped and tested a few days ago, but was offline and
thus unable to officially submit the change.
This change basically allows us to determine that the address of two distinct,
non-weak variables can not be the same. This is something that was in the
tree-ssa testsuite at one point, but was later dropped.
As it turns out this is somewhat useful to do -- it actually triggers
a few times in GCC itself. Typically in code like this:
char x;
char y;
bar (char *p)
{
if (p != &x)
{
outer then code
}
else
{
if (p == &y)
inner then code
else
inner else code
}
}
In the outer ELSE clause we know that p == &x. So we're able to const-prop
that value resulting in:
char x;
char y;
bar (char *p)
{
if (p != &x)
{
outer then code
}
else
{
if (&x == &y)
inner then code
else
inner else code
}
}
And we know &x can never be equal to &y, so we we know the result of the
inner IF and can simplify to:
char x;
char y;
bar (char *p)
{
if (p != &x)
{
outer then code
}
else
{
inner else code
}
}
This was actually a pleasant surprise. I had expected this to never trigger
in real code.
Bootstrapped and regression tested i686-pc-linux-gnu.
* fold-const.c (fold): An equality comparison of the address of
two non-weak variables has known compile-time result.
Index: fold-const.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/fold-const.c,v
retrieving revision 1.213.2.72
diff -c -p -r1.213.2.72 fold-const.c
*** fold-const.c 19 Feb 2004 17:00:39 -0000 1.213.2.72
--- fold-const.c 26 Feb 2004 18:51:56 -0000
*************** fold (tree expr)
*** 7181,7186 ****
--- 7181,7204 ----
return integer_one_node;
}
+ /* If this is an equality comparison of the address of two non-weak
+ symbols, then we know the result. */
+ if ((code == EQ_EXPR || code == NE_EXPR)
+ && TREE_CODE (arg0) == ADDR_EXPR
+ && DECL_P (TREE_OPERAND (arg0, 0))
+ && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
+ && TREE_CODE (arg1) == ADDR_EXPR
+ && DECL_P (TREE_OPERAND (arg1, 0))
+ && ! DECL_WEAK (TREE_OPERAND (arg1, 0)))
+ {
+ if (code == EQ_EXPR)
+ return (operand_equal_p (arg0, arg1, 0)
+ ? integer_one_node : integer_zero_node);
+ else
+ return (operand_equal_p (arg0, arg1, 0)
+ ? integer_zero_node : integer_one_node);
+ }
+
if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
{
tree targ0 = strip_float_extensions (arg0);
More information about the Gcc-patches
mailing list