* fold-const.c (fold): Optimize strlen comparisons against zero.

From-SVN: r47029
This commit is contained in:
Roger Sayle 2001-11-14 23:36:24 +00:00 committed by Richard Henderson
parent 81bc01c265
commit d59b3b679c
2 changed files with 31 additions and 0 deletions

View File

@ -1,3 +1,7 @@
2001-11-14 Roger Sayle <roger@eyesopen.com>
* fold-const.c (fold): Optimize strlen comparisons against zero.
2001-11-14 David O'Brien <obrien@FreeBSD.org>
* config.gcc (sparc64-wrs-vxworks*, sparc-*-netbsd*, sparc-*-openbsd*,

View File

@ -6899,6 +6899,33 @@ fold (expr)
fold (build (code, type, imag0, imag1))));
}
/* Optimize comparisons of strlen vs zero to a compare of the
first character of the string vs zero. To wit,
strlen(ptr) == 0 => *ptr == 0
strlen(ptr) != 0 => *ptr != 0
Other cases should reduce to one of these two (or a constant)
due to the return value of strlen being unsigned. */
if ((code == EQ_EXPR || code == NE_EXPR)
&& integer_zerop (arg1)
&& TREE_CODE (arg0) == CALL_EXPR
&& TREE_CODE (TREE_OPERAND (arg0, 0)) == ADDR_EXPR)
{
tree fndecl = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
tree arglist;
if (TREE_CODE (fndecl) == FUNCTION_DECL
&& DECL_BUILT_IN (fndecl)
&& DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_MD
&& DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
&& (arglist = TREE_OPERAND (arg0, 1))
&& TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) == POINTER_TYPE
&& ! TREE_CHAIN (arglist))
return fold (build (code, type,
build1 (INDIRECT_REF, char_type_node,
TREE_VALUE(arglist)),
integer_zero_node));
}
/* From here on, the only cases we handle are when the result is
known to be a constant.