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]

Re: PATCH to strip unnecessary TARGET_EXPRs for c++/27371


Patch:
2006-09-06  Jason Merrill  <jason@redhat.com>

	PR c++/27371
	* tree-inline.c (copy_result_decl_to_var): New fn.
	(declare_return_variable): Use it.  Call declare_inline_vars here.
	(expand_call_inline): Not here.
	* cp/cvt.c (convert_to_void): Strip useless TARGET_EXPR.
	* cp/cp-tree.h (TARGET_EXPR_IMPLICIT_P): New macro.
	* cp/tree.c (build_cplus_new): Set it.

Index: tree-inline.c
===================================================================
*** tree-inline.c	(revision 116735)
--- tree-inline.c	(working copy)
*************** static void declare_inline_vars (tree, t
*** 120,125 ****
--- 120,126 ----
  static void remap_save_expr (tree *, void *, int *);
  static void add_lexical_block (tree current_block, tree new_block);
  static tree copy_decl_to_var (tree, copy_body_data *);
+ static tree copy_result_decl_to_var (tree, copy_body_data *);
  static tree copy_decl_no_change (tree, copy_body_data *);
  static tree copy_decl_maybe_to_var (tree, copy_body_data *);
  
*************** declare_return_variable (copy_body_data 
*** 1261,1267 ****
  
    gcc_assert (TREE_CODE (TYPE_SIZE_UNIT (callee_type)) == INTEGER_CST);
  
!   var = copy_decl_to_var (result, id);
  
    DECL_SEEN_IN_BIND_EXPR_P (var) = 1;
    DECL_STRUCT_FUNCTION (caller)->unexpanded_var_list
--- 1262,1268 ----
  
    gcc_assert (TREE_CODE (TYPE_SIZE_UNIT (callee_type)) == INTEGER_CST);
  
!   var = copy_result_decl_to_var (result, id);
  
    DECL_SEEN_IN_BIND_EXPR_P (var) = 1;
    DECL_STRUCT_FUNCTION (caller)->unexpanded_var_list
*************** declare_return_variable (copy_body_data 
*** 1272,1277 ****
--- 1273,1280 ----
       not be visible to the user.  */
    TREE_NO_WARNING (var) = 1;
  
+   declare_inline_vars (id->block, var);
+ 
    /* Build the use expr.  If the return type of the function was
       promoted, convert it back to the expected type.  */
    use = var;
*************** declare_return_variable (copy_body_data 
*** 1280,1285 ****
--- 1283,1291 ----
      
    STRIP_USELESS_TYPE_CONVERSION (use);
  
+   if (DECL_BY_REFERENCE (result))
+     var = build_fold_addr_expr (var);
+ 
   done:
    /* Register the VAR_DECL as the equivalent for the RESULT_DECL; that
       way, when the RESULT_DECL is encountered, it will be
*************** expand_call_inline (basic_block bb, tree
*** 1926,1932 ****
    bool successfully_inlined = FALSE;
    tree t_step;
    tree var;
-   tree decl;
  
    /* See what we've got.  */
    id = (copy_body_data *) data;
--- 1932,1937 ----
*************** expand_call_inline (basic_block bb, tree
*** 2103,2113 ****
      modify_dest = NULL;
  
    /* Declare the return variable for the function.  */
!   decl = declare_return_variable (id, return_slot_addr,
! 			          modify_dest, &use_retvar);
!   /* Do this only if declare_return_variable created a new one.  */
!   if (decl && !return_slot_addr && decl != modify_dest)
!     declare_inline_vars (id->block, decl);
  
    /* This is it.  Duplicate the callee body.  Assume callee is
       pre-gimplified.  Note that we must not alter the caller
--- 2108,2115 ----
      modify_dest = NULL;
  
    /* Declare the return variable for the function.  */
!   declare_return_variable (id, return_slot_addr,
! 			   modify_dest, &use_retvar);
  
    /* This is it.  Duplicate the callee body.  Assume callee is
       pre-gimplified.  Note that we must not alter the caller
*************** copy_decl_to_var (tree decl, copy_body_d
*** 2630,2635 ****
--- 2632,2665 ----
    return copy_decl_for_dup_finish (id, decl, copy);
  }
  
+ /* Like copy_decl_to_var, but create a return slot object instead of a
+    pointer variable for return by invisible reference.  */
+ 
+ static tree
+ copy_result_decl_to_var (tree decl, copy_body_data *id)
+ {
+   tree copy, type;
+ 
+   gcc_assert (TREE_CODE (decl) == PARM_DECL
+ 	      || TREE_CODE (decl) == RESULT_DECL);
+ 
+   type = TREE_TYPE (decl);
+   if (DECL_BY_REFERENCE (decl))
+     type = TREE_TYPE (type);
+ 
+   copy = build_decl (VAR_DECL, DECL_NAME (decl), type);
+   TREE_READONLY (copy) = TREE_READONLY (decl);
+   TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (decl);
+   if (!DECL_BY_REFERENCE (decl))
+     {
+       TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (decl);
+       DECL_COMPLEX_GIMPLE_REG_P (copy) = DECL_COMPLEX_GIMPLE_REG_P (decl);
+     }
+ 
+   return copy_decl_for_dup_finish (id, decl, copy);
+ }
+ 
+ 
  static tree
  copy_decl_no_change (tree decl, copy_body_data *id)
  {
Index: cp/cvt.c
===================================================================
*** cp/cvt.c	(revision 116735)
--- cp/cvt.c	(working copy)
*************** convert_to_void (tree expr, const char *
*** 892,897 ****
--- 892,916 ----
  	break;
        }
  
+     case TARGET_EXPR:
+       /* Don't bother with the temporary object returned from a function if
+ 	 we don't use it and don't need to destroy it.  We'll still
+ 	 allocate space for it in expand_call or declare_return_variable,
+ 	 but we don't need to track it through all the tree phases.  */
+       if (TARGET_EXPR_IMPLICIT_P (expr)
+ 	  && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (expr)))
+ 	{
+ 	  tree init = TARGET_EXPR_INITIAL (expr);
+ 	  if (TREE_CODE (init) == AGGR_INIT_EXPR
+ 	      && !AGGR_INIT_VIA_CTOR_P (init))
+ 	    {
+ 	      tree fn = TREE_OPERAND (init, 0);
+ 	      expr = build3 (CALL_EXPR, TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
+ 			     fn, TREE_OPERAND (init, 1), NULL_TREE);
+ 	    }
+ 	}
+       break;
+ 
      default:;
      }
    {
Index: cp/tree.c
===================================================================
*** cp/tree.c	(revision 116735)
--- cp/tree.c	(working copy)
*************** build_cplus_new (tree type, tree init)
*** 312,317 ****
--- 312,318 ----
      rval = init;
  
    rval = build_target_expr (slot, rval);
+   TARGET_EXPR_IMPLICIT_P (rval) = 1;
  
    return rval;
  }
Index: cp/cp-tree.h
===================================================================
*** cp/cp-tree.h	(revision 116735)
--- cp/cp-tree.h	(working copy)
*************** struct diagnostic_context;
*** 55,60 ****
--- 55,61 ----
        OMP_ATOMIC_DEPENDENT_P (in OMP_ATOMIC)
        OMP_FOR_GIMPLIFYING_P (in OMP_FOR)
        BASELINK_QUALIFIED_P (in BASELINK)
+       TARGET_EXPR_IMPLICIT_P (in TARGET_EXPR)
     1: IDENTIFIER_VIRTUAL_P (in IDENTIFIER_NODE)
        TI_PENDING_TEMPLATE_FLAG.
        TEMPLATE_PARMS_FOR_INLINE.
*************** extern void decl_shadowed_for_var_insert
*** 3091,3096 ****
--- 3092,3102 ----
     expression statement.  */
  #define EXPR_STMT_EXPR(NODE)	TREE_OPERAND (EXPR_STMT_CHECK (NODE), 0)
  
+ /* True if this TARGET_EXPR was created by build_cplus_new, and so we can
+    discard it if it isn't useful.  */
+ #define TARGET_EXPR_IMPLICIT_P(NODE) \
+   TREE_LANG_FLAG_0 (TARGET_EXPR_CHECK (NODE))
+ 
  /* An enumeration of the kind of tags that C++ accepts.  */
  enum tag_types {
    none_type = 0, /* Not a tag type.  */
Index: testsuite/g++.dg/warn/unused-result1.C
===================================================================
*** testsuite/g++.dg/warn/unused-result1.C	(revision 0)
--- testsuite/g++.dg/warn/unused-result1.C	(revision 0)
***************
*** 0 ****
--- 1,10 ----
+ // PR c++/27371
+ 
+ class QByteArray {
+ public:
+   QByteArray(const QByteArray &);
+ };
+ class QString {
+   QByteArray toLocal8Bit() const __attribute__ ((warn_unused_result));
+   void fooWarnHere() const { toLocal8Bit(); } // { dg-warning "ignoring" }
+ };

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