Re: [patch] Apply conditional down cast to cgraph.h et.al.

2012-11-05 Thread Eric Botcazou
 Done and committed.

That's great, thanks!

-- 
Eric Botcazou


Re: [patch] Apply conditional down cast to cgraph.h et.al.

2012-11-02 Thread Eric Botcazou
 Index: gcc/ChangeLog
 
 2012-10-31  Lawrence Crowl  cr...@google.com
 
   * is-a.h: New.
   (is_a T (U*)): New.  Test for is-a relationship.
   (as_a T (U*)): New.  Treat as a derived type.
   (dyn_cast T (U*)): New.  Conditionally cast based on is_a.
   * cgraph.h (varpool_node): Rename to varpool_node_for_decl.
   Adjust callers to match.
   (is_a_helper cgraph_node::test (symtab_node_def *)): New.
   (is_a_helper varpool_node::test (symtab_node_def *)): New.
   (symtab_node_def::try_function): New.  Change most calls to
   symtab_function_p with calls to dyn_cast cgraph_node (p).
   (symtab_node_def::try_variable): New.  Change most calls to
   symtab_variable_p with calls to dyn_cast varpool_node (p).
   (symtab_function_p): Remove.  Change callers to use
 is_a cgraph_node (p) instead.
   (symtab_variable_p): Remove.  Change callers to use
 is_a varpool_node (p) instead.
   * cgraph.c (cgraph_node_for_asm): Remove redundant call to
   symtab_node_for_asm.
   * cgraphunit.c (symbol_finalized_and_needed): New.
   (symbol_finalized): New.
   (cgraph_analyze_functions): Split complicated conditionals out into
   above new functions.
   * Makefile.in (CGRAPH_H): Add is-a.h as used by cgraph.h.

The installed patch touches the ada/, cp/ and lto/ subdirectories without 
modifying their ChangeLog files.  Please add the missing entries.

[Some people, like me, do use these ChangeLogs to synchronize trees]

-- 
Eric Botcazou


Re: [patch] Apply conditional down cast to cgraph.h et.al.

2012-11-02 Thread Lawrence Crowl
On 11/2/12, Eric Botcazou ebotca...@adacore.com wrote:
 Index: gcc/ChangeLog

 2012-10-31  Lawrence Crowl  cr...@google.com

  * is-a.h: New.
  (is_a T (U*)): New.  Test for is-a relationship.
  (as_a T (U*)): New.  Treat as a derived type.
  (dyn_cast T (U*)): New.  Conditionally cast based on is_a.
  * cgraph.h (varpool_node): Rename to varpool_node_for_decl.
  Adjust callers to match.
  (is_a_helper cgraph_node::test (symtab_node_def *)): New.
  (is_a_helper varpool_node::test (symtab_node_def *)): New.
  (symtab_node_def::try_function): New.  Change most calls to
  symtab_function_p with calls to dyn_cast cgraph_node (p).
  (symtab_node_def::try_variable): New.  Change most calls to
  symtab_variable_p with calls to dyn_cast varpool_node (p).
  (symtab_function_p): Remove.  Change callers to use
 is_a cgraph_node (p) instead.
  (symtab_variable_p): Remove.  Change callers to use
 is_a varpool_node (p) instead.
  * cgraph.c (cgraph_node_for_asm): Remove redundant call to
  symtab_node_for_asm.
  * cgraphunit.c (symbol_finalized_and_needed): New.
  (symbol_finalized): New.
  (cgraph_analyze_functions): Split complicated conditionals out into
  above new functions.
  * Makefile.in (CGRAPH_H): Add is-a.h as used by cgraph.h.

 The installed patch touches the ada/, cp/ and lto/ subdirectories without
 modifying their ChangeLog files.  Please add the missing entries.

 [Some people, like me, do use these ChangeLogs to synchronize trees]

Done and committed.


Update ChangeLog files as requested for cgraph change to checked down cast.


Index: gcc/cp/ChangeLog

2012-10-31  Lawrence Crowl  cr...@google.com

* decl2.c (var_finalized_p): Rename varpool_node to
varpool_node_for_decl.
(maybe_emit_vtables): Likewise.

Index: gcc/ada/ChangeLog

2012-10-31  Lawrence Crowl  cr...@google.com

* gcc-interface/utils.c (gnat_write_global_declarations):
Rename varpool_node to varpool_node_for_decl.

Index: gcc/lto/ChangeLog

2012-10-31  Lawrence Crowl  cr...@google.com

* lto.c (lto_wpa_write_files): Change symtab checking to a checked
down-cast via dyn_cast.
* lto-partition.c (add_symbol_to_partition_1): Likewise.
(undo_partition): Likewise.
(lto_balanced_map): Likewise.
(get_symbol_class): Likewise and via is_a.
(lto_balanced_map): Change symtab checking to is_a.


-- 
Lawrence Crowl


Re: [patch] Apply conditional down cast to cgraph.h et.al.

2012-10-30 Thread Diego Novillo

On 2012-10-29 15:01 , Lawrence Crowl wrote:

On 10/27/12, Marc Glisse marc.gli...@inria.fr wrote:

On Fri, 26 Oct 2012, Lawrence Crowl wrote:

2012-10-26  Lawrence Crowl  cr...@google.com


missing ''


Fixed.


* is-a.h: New.
(is_a T (U*)): New.  Test for is-a relationship.
(as_a T (U*)): New.  Treat as a derived type.
(dyn_cast T (U*)): New.  Conditionally cast based on is_a.


I can't find this file in the patch...


I forgot to svn add.  Updated patch here.


This patch implements generic type query and conversion functions,
and applies them to the use of cgraph_node, varpool_node, and symtab_node.

The functions are:

bool is_a TYPE (pointer)
   Tests whether the pointer actually points to a more derived TYPE.

TYPE *as_a TYPE (pointer)
   Converts pointer to a TYPE*.

TYPE *dyn_cast TYPE (pointer)
   Converts pointer to TYPE* if and only if is_a TYPE pointer.
   Otherwise, returns NULL.
   This function is essentially a checked down cast.

These functions reduce compile time and increase type safety when treating a
generic item as a more specific item.  In essence, the code change is from

   if (symtab_function_p (node))
 {
   struct cgraph_node *cnode = cgraph (node);
   
 }

to

   if (cgraph_node *cnode = dyn_cast cgraph_node (node))
 {
   
 }

The necessary conditional test defines a variable that holds a known good
pointer to the specific item and avoids subsequent conversion calls and
the assertion checks that may come with them.

When, the property test is embedded within a larger condition, the variable
declaration gets pulled out of the condition.  (This leaves some room for
using the variable inappropriately.)

   if (symtab_variable_p (node)
varpool (node)-finalized)
 varpool_analyze_node (varpool (node));

becomes

   varpool_node *vnode = dyn_cast varpool_node (node);
   if (vnode  vnode-finalized)
 varpool_analyze_node (vnode);

Note that we have converted two sets of assertions in the calls to varpool
into safe and efficient use of a variable.


There are remaining calls to symtab_function_p and symtab_variable_p that
do not involve a pointer to a more specific type.  These have been converted
to calls to a functions is_a cgraph_node and is_a varpool_node.  The
original predicate functions have been removed.

The cgraph.h header defined both a struct and a function with the name
varpool_node.  This name overloading can cause some unintuitive error messages
when, as is common in C++, one omits the struct keyword when using the type.
I have renamed the function to varpool_node_for_decl.

Tested on x86_64.


Okay for trunk?
Index: gcc/ChangeLog

2012-10-29  Lawrence Crowl  cr...@google.com

* is-a.h: New.
(is_a T (U*)): New.  Test for is-a relationship.
(as_a T (U*)): New.  Treat as a derived type.
(dyn_cast T (U*)): New.  Conditionally cast based on is_a.
* cgraph.h (varpool_node): Rename to varpool_node_for_decl.
Adjust callers to match.
(is_a_helper cgraph_node::test (symtab_node_def *)): New.
(is_a_helper varpool_node::test (symtab_node_def *)): New.
(symtab_node_def::try_function): New.  Change most calls to
symtab_function_p with calls to dyn_cast cgraph_node (p).
(symtab_node_def::try_variable): New.  Change most calls to
symtab_variable_p with calls to dyn_cast varpool_node (p).
(symtab_function_p): Remove.  Change callers to use
 is_a cgraph_node (p) instead.
(symtab_variable_p): Remove.  Change callers to use
 is_a varpool_node (p) instead.
* cgraph.c (cgraph_node_for_asm): Remove redundant call to
symtab_node_for_asm.
* cgraphunit.c (symbol_finalized_and_needed): New.
(symbol_finalized): New.
(cgraph_analyze_functions): Split complicated conditionals out into
above new functions.
* Makefile.in (CGRAPH_H): Add is-a.h as used by cgraph.h.


Thanks.  I really like this cleanup.  I have a few questions and 
comments below.  Honza, the patch looks OK to me but it touches a bunch 
of cgraph code, could you please go over it?




Index: gcc/is-a.h
===
--- gcc/is-a.h  (revision 0)
+++ gcc/is-a.h  (revision 0)
@@ -0,0 +1,118 @@
+/* Dynamic testing for abstract is-a relationships.
+   Copyright (C) 2012 Free Software Foundation, Inc.
+   Contributed by Lawrence Crowl.
+
+This file is part of GCC.
+
+GCC is free software; you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free
+Software Foundation; either version 3, or (at your option) any later
+version.
+
+GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+for more details.
+
+You should have 

Re: [patch] Apply conditional down cast to cgraph.h et.al.

2012-10-30 Thread Richard Biener
On Tue, Oct 30, 2012 at 1:20 PM, Diego Novillo dnovi...@google.com wrote:
 On 2012-10-29 15:01 , Lawrence Crowl wrote:

 On 10/27/12, Marc Glisse marc.gli...@inria.fr wrote:

 On Fri, 26 Oct 2012, Lawrence Crowl wrote:

 2012-10-26  Lawrence Crowl  cr...@google.com


 missing ''


 Fixed.

 * is-a.h: New.
 (is_a T (U*)): New.  Test for is-a relationship.
 (as_a T (U*)): New.  Treat as a derived type.
 (dyn_cast T (U*)): New.  Conditionally cast based on is_a.


 I can't find this file in the patch...


 I forgot to svn add.  Updated patch here.


 This patch implements generic type query and conversion functions,
 and applies them to the use of cgraph_node, varpool_node, and symtab_node.

 The functions are:

 bool is_a TYPE (pointer)
Tests whether the pointer actually points to a more derived TYPE.

 TYPE *as_a TYPE (pointer)
Converts pointer to a TYPE*.

 TYPE *dyn_cast TYPE (pointer)
Converts pointer to TYPE* if and only if is_a TYPE pointer.
Otherwise, returns NULL.
This function is essentially a checked down cast.

 These functions reduce compile time and increase type safety when treating
 a
 generic item as a more specific item.  In essence, the code change is from

if (symtab_function_p (node))
  {
struct cgraph_node *cnode = cgraph (node);

  }

 to

if (cgraph_node *cnode = dyn_cast cgraph_node (node))
  {

  }

 The necessary conditional test defines a variable that holds a known good
 pointer to the specific item and avoids subsequent conversion calls and
 the assertion checks that may come with them.

 When, the property test is embedded within a larger condition, the
 variable
 declaration gets pulled out of the condition.  (This leaves some room for
 using the variable inappropriately.)

if (symtab_variable_p (node)
 varpool (node)-finalized)
  varpool_analyze_node (varpool (node));

 becomes

varpool_node *vnode = dyn_cast varpool_node (node);
if (vnode  vnode-finalized)
  varpool_analyze_node (vnode);

 Note that we have converted two sets of assertions in the calls to varpool
 into safe and efficient use of a variable.


 There are remaining calls to symtab_function_p and symtab_variable_p that
 do not involve a pointer to a more specific type.  These have been
 converted
 to calls to a functions is_a cgraph_node and is_a varpool_node.  The
 original predicate functions have been removed.

 The cgraph.h header defined both a struct and a function with the name
 varpool_node.  This name overloading can cause some unintuitive error
 messages
 when, as is common in C++, one omits the struct keyword when using the
 type.
 I have renamed the function to varpool_node_for_decl.

 Tested on x86_64.


 Okay for trunk?
 Index: gcc/ChangeLog

 2012-10-29  Lawrence Crowl  cr...@google.com

 * is-a.h: New.
 (is_a T (U*)): New.  Test for is-a relationship.
 (as_a T (U*)): New.  Treat as a derived type.
 (dyn_cast T (U*)): New.  Conditionally cast based on is_a.
 * cgraph.h (varpool_node): Rename to varpool_node_for_decl.
 Adjust callers to match.
 (is_a_helper cgraph_node::test (symtab_node_def *)): New.
 (is_a_helper varpool_node::test (symtab_node_def *)): New.
 (symtab_node_def::try_function): New.  Change most calls to
 symtab_function_p with calls to dyn_cast cgraph_node (p).
 (symtab_node_def::try_variable): New.  Change most calls to
 symtab_variable_p with calls to dyn_cast varpool_node (p).
 (symtab_function_p): Remove.  Change callers to use
  is_a cgraph_node (p) instead.
 (symtab_variable_p): Remove.  Change callers to use
  is_a varpool_node (p) instead.
 * cgraph.c (cgraph_node_for_asm): Remove redundant call to
 symtab_node_for_asm.
 * cgraphunit.c (symbol_finalized_and_needed): New.
 (symbol_finalized): New.
 (cgraph_analyze_functions): Split complicated conditionals out
 into
 above new functions.
 * Makefile.in (CGRAPH_H): Add is-a.h as used by cgraph.h.


 Thanks.  I really like this cleanup.  I have a few questions and comments
 below.  Honza, the patch looks OK to me but it touches a bunch of cgraph
 code, could you please go over it?



 Index: gcc/is-a.h
 ===
 --- gcc/is-a.h  (revision 0)
 +++ gcc/is-a.h  (revision 0)
 @@ -0,0 +1,118 @@
 +/* Dynamic testing for abstract is-a relationships.
 +   Copyright (C) 2012 Free Software Foundation, Inc.
 +   Contributed by Lawrence Crowl.
 +
 +This file is part of GCC.
 +
 +GCC is free software; you can redistribute it and/or modify it under
 +the terms of the GNU General Public License as published by the Free
 +Software Foundation; either version 3, or (at your option) any later
 +version.
 +
 +GCC is distributed in the hope that it will be useful, but WITHOUT 

Re: [patch] Apply conditional down cast to cgraph.h et.al.

2012-10-30 Thread Jan Hubicka
 2012-10-29  Lawrence Crowl  cr...@google.com
 
   * is-a.h: New.
   (is_a T (U*)): New.  Test for is-a relationship.
   (as_a T (U*)): New.  Treat as a derived type.
   (dyn_cast T (U*)): New.  Conditionally cast based on is_a.
   * cgraph.h (varpool_node): Rename to varpool_node_for_decl.
   Adjust callers to match.
   (is_a_helper cgraph_node::test (symtab_node_def *)): New.
   (is_a_helper varpool_node::test (symtab_node_def *)): New.
   (symtab_node_def::try_function): New.  Change most calls to
   symtab_function_p with calls to dyn_cast cgraph_node (p).
   (symtab_node_def::try_variable): New.  Change most calls to
   symtab_variable_p with calls to dyn_cast varpool_node (p).
   (symtab_function_p): Remove.  Change callers to use
 is_a cgraph_node (p) instead.
   (symtab_variable_p): Remove.  Change callers to use
 is_a varpool_node (p) instead.
   * cgraph.c (cgraph_node_for_asm): Remove redundant call to
   symtab_node_for_asm.
   * cgraphunit.c (symbol_finalized_and_needed): New.
   (symbol_finalized): New.
   (cgraph_analyze_functions): Split complicated conditionals out into
   above new functions.
   * Makefile.in (CGRAPH_H): Add is-a.h as used by cgraph.h.
 

the patch is OK,
thanks!
Honza


Re: [patch] Apply conditional down cast to cgraph.h et.al.

2012-10-30 Thread Lawrence Crowl
On 10/30/12, Diego Novillo dnovi...@google.com wrote:
 On 2012-10-29 15:01 , Lawrence Crowl wrote:
 On 10/27/12, Marc Glisse marc.gli...@inria.fr wrote:
 On Fri, 26 Oct 2012, Lawrence Crowl wrote:
 2012-10-26  Lawrence Crowl  cr...@google.com

 missing ''

 Fixed.

* is-a.h: New.
(is_a T (U*)): New.  Test for is-a relationship.
(as_a T (U*)): New.  Treat as a derived type.
(dyn_cast T (U*)): New.  Conditionally cast based on is_a.

 I can't find this file in the patch...

 I forgot to svn add.  Updated patch here.


 This patch implements generic type query and conversion functions,
 and applies them to the use of cgraph_node, varpool_node, and
 symtab_node.

 The functions are:

 bool is_a TYPE (pointer)
Tests whether the pointer actually points to a more derived TYPE.

 TYPE *as_a TYPE (pointer)
Converts pointer to a TYPE*.

 TYPE *dyn_cast TYPE (pointer)
Converts pointer to TYPE* if and only if is_a TYPE pointer.
Otherwise, returns NULL.
This function is essentially a checked down cast.

 These functions reduce compile time and increase type safety when treating
 a
 generic item as a more specific item.  In essence, the code change is
 from

if (symtab_function_p (node))
  {
struct cgraph_node *cnode = cgraph (node);

  }

 to

if (cgraph_node *cnode = dyn_cast cgraph_node (node))
  {

  }

 The necessary conditional test defines a variable that holds a known good
 pointer to the specific item and avoids subsequent conversion calls and
 the assertion checks that may come with them.

 When, the property test is embedded within a larger condition, the
 variable
 declaration gets pulled out of the condition.  (This leaves some room for
 using the variable inappropriately.)

if (symtab_variable_p (node)
 varpool (node)-finalized)
  varpool_analyze_node (varpool (node));

 becomes

varpool_node *vnode = dyn_cast varpool_node (node);
if (vnode  vnode-finalized)
  varpool_analyze_node (vnode);

 Note that we have converted two sets of assertions in the calls to
 varpool
 into safe and efficient use of a variable.


 There are remaining calls to symtab_function_p and symtab_variable_p that
 do not involve a pointer to a more specific type.  These have been
 converted
 to calls to a functions is_a cgraph_node and is_a varpool_node.  The
 original predicate functions have been removed.

 The cgraph.h header defined both a struct and a function with the name
 varpool_node.  This name overloading can cause some unintuitive error
 messages
 when, as is common in C++, one omits the struct keyword when using the
 type.
 I have renamed the function to varpool_node_for_decl.

 Tested on x86_64.


 Okay for trunk?
 Index: gcc/ChangeLog

 2012-10-29  Lawrence Crowl  cr...@google.com

  * is-a.h: New.
  (is_a T (U*)): New.  Test for is-a relationship.
  (as_a T (U*)): New.  Treat as a derived type.
  (dyn_cast T (U*)): New.  Conditionally cast based on is_a.
  * cgraph.h (varpool_node): Rename to varpool_node_for_decl.
  Adjust callers to match.
  (is_a_helper cgraph_node::test (symtab_node_def *)): New.
  (is_a_helper varpool_node::test (symtab_node_def *)): New.
  (symtab_node_def::try_function): New.  Change most calls to
  symtab_function_p with calls to dyn_cast cgraph_node (p).
  (symtab_node_def::try_variable): New.  Change most calls to
  symtab_variable_p with calls to dyn_cast varpool_node (p).
  (symtab_function_p): Remove.  Change callers to use
  is_a cgraph_node (p) instead.
  (symtab_variable_p): Remove.  Change callers to use
  is_a varpool_node (p) instead.
  * cgraph.c (cgraph_node_for_asm): Remove redundant call to
  symtab_node_for_asm.
  * cgraphunit.c (symbol_finalized_and_needed): New.
  (symbol_finalized): New.
  (cgraph_analyze_functions): Split complicated conditionals out into
  above new functions.
  * Makefile.in (CGRAPH_H): Add is-a.h as used by cgraph.h.

 Thanks.  I really like this cleanup.  I have a few questions and
 comments below.  Honza, the patch looks OK to me but it touches a bunch
 of cgraph code, could you please go over it?


 Index: gcc/is-a.h
 ===
 --- gcc/is-a.h   (revision 0)
 +++ gcc/is-a.h   (revision 0)
 @@ -0,0 +1,118 @@
 +/* Dynamic testing for abstract is-a relationships.
 +   Copyright (C) 2012 Free Software Foundation, Inc.
 +   Contributed by Lawrence Crowl.
 +
 +This file is part of GCC.
 +
 +GCC is free software; you can redistribute it and/or modify it under
 +the terms of the GNU General Public License as published by the Free
 +Software Foundation; either version 3, or (at your option) any later
 +version.
 +
 +GCC is distributed in the hope that it will be useful, but WITHOUT ANY
 +WARRANTY; without even the implied warranty of MERCHANTABILITY or
 +FITNESS FOR A PARTICULAR 

Re: [patch] Apply conditional down cast to cgraph.h et.al.

2012-10-30 Thread Diego Novillo
On Tue, Oct 30, 2012 at 4:53 PM, Lawrence Crowl cr...@googlers.com wrote:
 On 10/30/12, Diego Novillo dnovi...@google.com wrote:

 So, to use these three functions, the user must define this single
 'is_a_helper' routine?  Nothing else?

 You need to distinguish which kind user.  Someone just wanting
 to convert does not need to know about the is_a_helper stuff.
 Someone wanting to extend the set of type relationships needs to
 provide one or two template specializations.  I've modified the
 in-header documentation to better reflect the distinction.

Great.

 I originally had

   if (cgraph_node *ce = dyn_cast cgraph_node (e))
 if (!DECL_BUILT_IN (e-symbol.decl))
   lto_cgraph_replace_node (ce, cgraph (prevailing));

 but folks objected to increasing the nesting, and asked that I
 change to the pre-declare form.

Ah, yeah.  I remember that.  OK, so we can now use both forms, right?


Thanks.  Diego.


Re: [patch] Apply conditional down cast to cgraph.h et.al.

2012-10-30 Thread Lawrence Crowl
On 10/30/12, Diego Novillo dnovi...@google.com wrote:
 On Tue, Oct 30, 2012 at 4:53 PM, Lawrence Crowl cr...@googlers.com wrote:
 On 10/30/12, Diego Novillo dnovi...@google.com wrote:

 So, to use these three functions, the user must define this single
 'is_a_helper' routine?  Nothing else?

 You need to distinguish which kind user.  Someone just wanting
 to convert does not need to know about the is_a_helper stuff.
 Someone wanting to extend the set of type relationships needs to
 provide one or two template specializations.  I've modified the
 in-header documentation to better reflect the distinction.

 Great.

 I originally had

   if (cgraph_node *ce = dyn_cast cgraph_node (e))
 if (!DECL_BUILT_IN (e-symbol.decl))
   lto_cgraph_replace_node (ce, cgraph (prevailing));

 but folks objected to increasing the nesting, and asked that I
 change to the pre-declare form.

 Ah, yeah.  I remember that.  OK, so we can now use both forms, right?

Yes.

I will commit the patch as soon as the merge and test is complete.

-- 
Lawrence Crowl


Re: [patch] Apply conditional down cast to cgraph.h et.al.

2012-10-30 Thread Xinliang David Li
Good cleanup! Simple and consistent interfaces are usually
self-documenting, and this one is a good step forward.

thanks,

David

On Tue, Oct 30, 2012 at 2:09 PM, Lawrence Crowl cr...@googlers.com wrote:
 On 10/30/12, Diego Novillo dnovi...@google.com wrote:
 On Tue, Oct 30, 2012 at 4:53 PM, Lawrence Crowl cr...@googlers.com wrote:
 On 10/30/12, Diego Novillo dnovi...@google.com wrote:

 So, to use these three functions, the user must define this single
 'is_a_helper' routine?  Nothing else?

 You need to distinguish which kind user.  Someone just wanting
 to convert does not need to know about the is_a_helper stuff.
 Someone wanting to extend the set of type relationships needs to
 provide one or two template specializations.  I've modified the
 in-header documentation to better reflect the distinction.

 Great.

 I originally had

   if (cgraph_node *ce = dyn_cast cgraph_node (e))
 if (!DECL_BUILT_IN (e-symbol.decl))
   lto_cgraph_replace_node (ce, cgraph (prevailing));

 but folks objected to increasing the nesting, and asked that I
 change to the pre-declare form.

 Ah, yeah.  I remember that.  OK, so we can now use both forms, right?

 Yes.

 I will commit the patch as soon as the merge and test is complete.

 --
 Lawrence Crowl


Re: [patch] Apply conditional down cast to cgraph.h et.al.

2012-10-29 Thread Lawrence Crowl
On 10/27/12, Marc Glisse marc.gli...@inria.fr wrote:
 On Fri, 26 Oct 2012, Lawrence Crowl wrote:
  2012-10-26  Lawrence Crowl  cr...@google.com

 missing ''

Fixed.

  * is-a.h: New.
  (is_a T (U*)): New.  Test for is-a relationship.
  (as_a T (U*)): New.  Treat as a derived type.
  (dyn_cast T (U*)): New.  Conditionally cast based on is_a.

 I can't find this file in the patch...

I forgot to svn add.  Updated patch here.


This patch implements generic type query and conversion functions,
and applies them to the use of cgraph_node, varpool_node, and symtab_node.

The functions are:

bool is_a TYPE (pointer)
  Tests whether the pointer actually points to a more derived TYPE.

TYPE *as_a TYPE (pointer)
  Converts pointer to a TYPE*.

TYPE *dyn_cast TYPE (pointer)
  Converts pointer to TYPE* if and only if is_a TYPE pointer.
  Otherwise, returns NULL.
  This function is essentially a checked down cast.

These functions reduce compile time and increase type safety when treating a
generic item as a more specific item.  In essence, the code change is from

  if (symtab_function_p (node))
{
  struct cgraph_node *cnode = cgraph (node);
  
}

to

  if (cgraph_node *cnode = dyn_cast cgraph_node (node))
{
  
}

The necessary conditional test defines a variable that holds a known good
pointer to the specific item and avoids subsequent conversion calls and
the assertion checks that may come with them.

When, the property test is embedded within a larger condition, the variable
declaration gets pulled out of the condition.  (This leaves some room for
using the variable inappropriately.)

  if (symtab_variable_p (node)
   varpool (node)-finalized)
varpool_analyze_node (varpool (node));

becomes

  varpool_node *vnode = dyn_cast varpool_node (node);
  if (vnode  vnode-finalized)
varpool_analyze_node (vnode);

Note that we have converted two sets of assertions in the calls to varpool
into safe and efficient use of a variable.


There are remaining calls to symtab_function_p and symtab_variable_p that
do not involve a pointer to a more specific type.  These have been converted
to calls to a functions is_a cgraph_node and is_a varpool_node.  The
original predicate functions have been removed.

The cgraph.h header defined both a struct and a function with the name
varpool_node.  This name overloading can cause some unintuitive error messages
when, as is common in C++, one omits the struct keyword when using the type.
I have renamed the function to varpool_node_for_decl.

Tested on x86_64.


Okay for trunk?
Index: gcc/ChangeLog

2012-10-29  Lawrence Crowl  cr...@google.com

* is-a.h: New.
(is_a T (U*)): New.  Test for is-a relationship.
(as_a T (U*)): New.  Treat as a derived type.
(dyn_cast T (U*)): New.  Conditionally cast based on is_a.
* cgraph.h (varpool_node): Rename to varpool_node_for_decl.
Adjust callers to match.
(is_a_helper cgraph_node::test (symtab_node_def *)): New.
(is_a_helper varpool_node::test (symtab_node_def *)): New.
(symtab_node_def::try_function): New.  Change most calls to
symtab_function_p with calls to dyn_cast cgraph_node (p).
(symtab_node_def::try_variable): New.  Change most calls to
symtab_variable_p with calls to dyn_cast varpool_node (p).
(symtab_function_p): Remove.  Change callers to use
is_a cgraph_node (p) instead.
(symtab_variable_p): Remove.  Change callers to use
is_a varpool_node (p) instead.
* cgraph.c (cgraph_node_for_asm): Remove redundant call to
symtab_node_for_asm.
* cgraphunit.c (symbol_finalized_and_needed): New.
(symbol_finalized): New.
(cgraph_analyze_functions): Split complicated conditionals out into
above new functions.
* Makefile.in (CGRAPH_H): Add is-a.h as used by cgraph.h.

Index: gcc/is-a.h
===
--- gcc/is-a.h  (revision 0)
+++ gcc/is-a.h  (revision 0)
@@ -0,0 +1,118 @@
+/* Dynamic testing for abstract is-a relationships.
+   Copyright (C) 2012 Free Software Foundation, Inc.
+   Contributed by Lawrence Crowl.
+
+This file is part of GCC.
+
+GCC is free software; you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free
+Software Foundation; either version 3, or (at your option) any later
+version.
+
+GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with GCC; see the file COPYING3.  If not see
+http://www.gnu.org/licenses/.  */
+
+
+/*
+
+Suppose you have a symtab_node_def *ptr, AKA symtab_node ptr.  You can
+test whether it points to a 'derived'