[RFC] Possible folding opportunities for string built-ins

Martin Liška mliska@suse.cz
Wed Oct 12 13:48:00 GMT 2016


Hi.

As you probably mentioned, simple folding improvement has grown to multiple patches
and multiple iterations. Apart from that, I also noticed that we do not do the best
for couple of cases and I would like to have a feedback if it worth to improve or not?

$ cat /tmp/string-folding-missing.c 
const char global_1[4] = {'a', 'b', 'c', 'd' };
const char global_2[6] = "abcdefghijk";

int main()
{
  const char local1[] = "asdfasdfasdf";

  /* Case 1 */
  __builtin_memchr (global_1, 'c', 5);

  /* Case 2 */
  __builtin_memchr (global_2, 'c', 5);

  /* Case 3 */
  __builtin_memchr (local1, 'a', 5);

  return 0;
}

Cases:
1) Currently, calling c_getstr (which calls string_constant) can't handle CONSTRUCTOR. Potential
solution can be to create on demand STRING_CST, however as string_constant is called multiple times,
it can be overkill.
2) /tmp/xxxxx.c:2:26: warning: initializer-string for array of chars is too long
 const char global_2[6] = "abcdefghijk";
Here I'm not sure whether one can consider global_2 == "abcdef" (w/o trailing zero char) or not?
If so, adding new output argument (string_length) to string_constant can be solution.
3) Currently, ctor_for_folding return error_mark_node for local variables. I'm wondering whether returning
DECL_INITIAL for these would be doable? Will it make any issue for LTO?

Last question is whether one can aggressively fold strcasecmp in a host compiler? Or are there any situations
where results depends on locale?

Thanks for thoughts.
Martin



More information about the Gcc-patches mailing list