This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Fix PR21595


This fixes __builtin_constant_p(&"Hello"[0]) for C++ by teaching
fold_builtin_constant_p about ARRAY_REF.

Bootstrapped and tested on i686-unknown-linux-gnu.

Ok for mainline?  Ok for 4.0.1?

Thanks,
Richard.
2005-05-16  Richard Guenther  <rguenth@gcc.gnu.org>

	PR middle-end/21595
	* builtins.c (fold_builtin_constant_p): Handle
	&"string cst"[0] as constant.

	* g++.dg/tree-ssa/builtin1.C: New testcase.


Index: builtins.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/builtins.c,v
retrieving revision 1.461
diff -c -3 -p -r1.461 builtins.c
*** builtins.c	3 May 2005 22:21:48 -0000	1.461
--- builtins.c	15 May 2005 21:44:37 -0000
*************** fold_builtin_constant_p (tree arglist)
*** 6338,6347 ****
    /* If we know this is a constant, emit the constant of one.  */
    if (CONSTANT_CLASS_P (arglist)
        || (TREE_CODE (arglist) == CONSTRUCTOR
! 	  && TREE_CONSTANT (arglist))
!       || (TREE_CODE (arglist) == ADDR_EXPR
! 	  && TREE_CODE (TREE_OPERAND (arglist, 0)) == STRING_CST))
      return integer_one_node;
  
    /* If this expression has side effects, show we don't know it to be a
       constant.  Likewise if it's a pointer or aggregate type since in
--- 6338,6354 ----
    /* If we know this is a constant, emit the constant of one.  */
    if (CONSTANT_CLASS_P (arglist)
        || (TREE_CODE (arglist) == CONSTRUCTOR
! 	  && TREE_CONSTANT (arglist)))
      return integer_one_node;
+   if (TREE_CODE (arglist) == ADDR_EXPR)
+     {
+        tree op = TREE_OPERAND (arglist, 0);
+        if (TREE_CODE (op) == STRING_CST
+ 	   || (TREE_CODE (op) == ARRAY_REF
+ 	       && integer_zerop (TREE_OPERAND (op, 1))
+ 	       && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
+ 	 return integer_one_node;
+     }
  
    /* If this expression has side effects, show we don't know it to be a
       constant.  Likewise if it's a pointer or aggregate type since in


// { dg-do link }

extern void link_error();

int main()
{
	if (! __builtin_constant_p (&"Hello"[0]))
		link_error();
}


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]