[tree-ssa] Optimize reads from constant strings
law@redhat.com
law@redhat.com
Thu Aug 14 15:32:00 GMT 2003
This fixes 20030807-9.c which looks like:
static void
bar ()
{
const char *label2 = (*"*.L_sfnames_b" == '*') + "*.L_sfnames_b";
oof (label2);
}
Obviously the condition is a compile-time constant since we're peeking at
an element in a constant string (and yes, this is derived from real-world
code -- gcc itself).
We fold the load in fold-const.c::fold. However, we can't do the same for
constant array accesses because we'd muck up &"blah"[2]. Try as I might
I've been unable to make similar constructs fail for INDIRECT_REFs.
* fold-const.c (fold, case INDIRECT_REF): Optimize reads from
constant strings.
Index: fold-const.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/fold-const.c,v
retrieving revision 1.213.2.41
diff -c -3 -p -r1.213.2.41 fold-const.c
*** fold-const.c 12 Aug 2003 02:14:52 -0000 1.213.2.41
--- fold-const.c 14 Aug 2003 15:26:20 -0000
*************** fold (tree expr)
*** 7675,7680 ****
--- 7675,7697 ----
}
return t;
+ case INDIRECT_REF:
+ {
+ tree exp1 = TREE_OPERAND (expr, 0);
+ tree index;
+ tree string = string_constant (exp1, &index);
+
+ /* Try to optimize reads from const strings. */
+ if (string
+ && TREE_CODE (string) == STRING_CST
+ && TREE_CODE (index) == INTEGER_CST
+ && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0)
+ return build_int_2 ((TREE_STRING_POINTER (string)
+ [TREE_INT_CST_LOW (index)]), 0);
+ return t;
+ }
+
+
default:
return t;
} /* switch (code) */
More information about the Gcc-patches
mailing list