1 /* Interprocedural reference lists.
2    Copyright (C) 2010-2014 Free Software Foundation, Inc.
3    Contributed by Jan Hubicka
4 
5 This file is part of GCC.
6 
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11 
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20 
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tree.h"
25 #include "ggc.h"
26 #include "target.h"
27 #include "cgraph.h"
28 #include "ipa-utils.h"
29 
30 static const char *ipa_ref_use_name[] = {"read","write","addr","alias"};
31 
32 /* Return ipa reference from REFERING_NODE or REFERING_VARPOOL_NODE
33    to REFERED_NODE or REFERED_VARPOOL_NODE. USE_TYPE specify type
34    of the use and STMT the statement (if it exists).  */
35 
36 struct ipa_ref *
ipa_record_reference(symtab_node * referring_node,symtab_node * referred_node,enum ipa_ref_use use_type,gimple stmt)37 ipa_record_reference (symtab_node *referring_node,
38 		      symtab_node *referred_node,
39 		      enum ipa_ref_use use_type, gimple stmt)
40 {
41   struct ipa_ref *ref, *ref2;
42   struct ipa_ref_list *list, *list2;
43   ipa_ref_t *old_references;
44 
45   gcc_checking_assert (!stmt || is_a <cgraph_node> (referring_node));
46   gcc_checking_assert (use_type != IPA_REF_ALIAS || !stmt);
47 
48   list = &referring_node->ref_list;
49   old_references = vec_safe_address (list->references);
50   vec_safe_grow (list->references, vec_safe_length (list->references) + 1);
51   ref = &list->references->last ();
52 
53   list2 = &referred_node->ref_list;
54   list2->referring.safe_push (ref);
55   ref->referred_index = list2->referring.length () - 1;
56   ref->referring = referring_node;
57   ref->referred = referred_node;
58   ref->stmt = stmt;
59   ref->lto_stmt_uid = 0;
60   ref->use = use_type;
61   ref->speculative = 0;
62 
63   /* If vector was moved in memory, update pointers.  */
64   if (old_references != list->references->address ())
65     {
66       int i;
67       for (i = 0; ipa_ref_list_reference_iterate (list, i, ref2); i++)
68 	ipa_ref_referred_ref_list (ref2)->referring[ref2->referred_index] = ref2;
69     }
70   return ref;
71 }
72 
73 /* If VAL is a reference to a function or a variable, add a reference from
74    REFERRING_NODE to the corresponding symbol table node.  USE_TYPE specify
75    type of the use and STMT the statement (if it exists).  Return the new
76    reference or NULL if none was created.  */
77 
78 struct ipa_ref *
ipa_maybe_record_reference(symtab_node * referring_node,tree val,enum ipa_ref_use use_type,gimple stmt)79 ipa_maybe_record_reference (symtab_node *referring_node, tree val,
80 			    enum ipa_ref_use use_type, gimple stmt)
81 {
82   STRIP_NOPS (val);
83   if (TREE_CODE (val) != ADDR_EXPR)
84     return NULL;
85   val = get_base_var (val);
86   if (val && (TREE_CODE (val) == FUNCTION_DECL
87 	       || TREE_CODE (val) == VAR_DECL))
88     {
89       symtab_node *referred = symtab_get_node (val);
90       gcc_checking_assert (referred);
91       return ipa_record_reference (referring_node, referred,
92 				   use_type, stmt);
93     }
94   return NULL;
95 }
96 
97 /* Remove reference REF.  */
98 
99 void
ipa_remove_reference(struct ipa_ref * ref)100 ipa_remove_reference (struct ipa_ref *ref)
101 {
102   struct ipa_ref_list *list = ipa_ref_referred_ref_list (ref);
103   struct ipa_ref_list *list2 = ipa_ref_referring_ref_list (ref);
104   vec<ipa_ref_t, va_gc> *old_references = list2->references;
105   struct ipa_ref *last;
106 
107   gcc_assert (list->referring[ref->referred_index] == ref);
108   last = list->referring.last ();
109   if (ref != last)
110     {
111       list->referring[ref->referred_index] = list->referring.last ();
112       list->referring[ref->referred_index]->referred_index
113 	  = ref->referred_index;
114     }
115   list->referring.pop ();
116 
117   last = &list2->references->last ();
118   if (ref != last)
119     {
120       *ref = *last;
121       ipa_ref_referred_ref_list (ref)->referring[ref->referred_index] = ref;
122     }
123   list2->references->pop ();
124   gcc_assert (list2->references == old_references);
125 }
126 
127 /* Remove all references in ref list LIST.  */
128 
129 void
ipa_remove_all_references(struct ipa_ref_list * list)130 ipa_remove_all_references (struct ipa_ref_list *list)
131 {
132   while (vec_safe_length (list->references))
133     ipa_remove_reference (&list->references->last ());
134   vec_free (list->references);
135 }
136 
137 /* Remove all references in ref list LIST.  */
138 
139 void
ipa_remove_all_referring(struct ipa_ref_list * list)140 ipa_remove_all_referring (struct ipa_ref_list *list)
141 {
142   while (list->referring.length ())
143     ipa_remove_reference (list->referring.last ());
144   list->referring.release ();
145 }
146 
147 /* Dump references in LIST to FILE.  */
148 
149 void
ipa_dump_references(FILE * file,struct ipa_ref_list * list)150 ipa_dump_references (FILE * file, struct ipa_ref_list *list)
151 {
152   struct ipa_ref *ref;
153   int i;
154   for (i = 0; ipa_ref_list_reference_iterate (list, i, ref); i++)
155     {
156       fprintf (file, "%s/%i (%s)",
157                ref->referred->asm_name (),
158                ref->referred->order,
159 	       ipa_ref_use_name [ref->use]);
160       if (ref->speculative)
161 	fprintf (file, " (speculative)");
162     }
163   fprintf (file, "\n");
164 }
165 
166 /* Dump referring in LIST to FILE.  */
167 
168 void
ipa_dump_referring(FILE * file,struct ipa_ref_list * list)169 ipa_dump_referring (FILE * file, struct ipa_ref_list *list)
170 {
171   struct ipa_ref *ref;
172   int i;
173   for (i = 0; ipa_ref_list_referring_iterate (list, i, ref); i++)
174     {
175       fprintf (file, "%s/%i (%s)",
176                ref->referring->asm_name (),
177                ref->referring->order,
178 	       ipa_ref_use_name [ref->use]);
179       if (ref->speculative)
180 	fprintf (file, " (speculative)");
181     }
182   fprintf (file, "\n");
183 }
184 
185 /* Clone reference REF to DEST_NODE and set its stmt to STMT.  */
186 
187 struct ipa_ref *
ipa_clone_ref(struct ipa_ref * ref,symtab_node * dest_node,gimple stmt)188 ipa_clone_ref (struct ipa_ref *ref,
189 	       symtab_node *dest_node,
190 	       gimple stmt)
191 {
192   bool speculative = ref->speculative;
193   unsigned int stmt_uid = ref->lto_stmt_uid;
194   struct ipa_ref *ref2;
195 
196   ref2 = ipa_record_reference (dest_node,
197 			       ref->referred,
198 			       ref->use, stmt);
199   ref2->speculative = speculative;
200   ref2->lto_stmt_uid = stmt_uid;
201   return ref2;
202 }
203 
204 /* Clone all references from SRC to DEST_NODE or DEST_VARPOOL_NODE.  */
205 
206 void
ipa_clone_references(symtab_node * dest_node,struct ipa_ref_list * src)207 ipa_clone_references (symtab_node *dest_node,
208 		      struct ipa_ref_list *src)
209 {
210   struct ipa_ref *ref, *ref2;
211   int i;
212   for (i = 0; ipa_ref_list_reference_iterate (src, i, ref); i++)
213     {
214       bool speculative = ref->speculative;
215       unsigned int stmt_uid = ref->lto_stmt_uid;
216 
217       ref2 = ipa_record_reference (dest_node,
218 				   ref->referred,
219 				   ref->use, ref->stmt);
220       ref2->speculative = speculative;
221       ref2->lto_stmt_uid = stmt_uid;
222     }
223 }
224 
225 /* Clone all referring from SRC to DEST_NODE or DEST_VARPOOL_NODE.  */
226 
227 void
ipa_clone_referring(symtab_node * dest_node,struct ipa_ref_list * src)228 ipa_clone_referring (symtab_node *dest_node,
229 		    struct ipa_ref_list *src)
230 {
231   struct ipa_ref *ref, *ref2;
232   int i;
233   for (i = 0; ipa_ref_list_referring_iterate (src, i, ref); i++)
234     {
235       bool speculative = ref->speculative;
236       unsigned int stmt_uid = ref->lto_stmt_uid;
237 
238       ref2 = ipa_record_reference (ref->referring,
239 				   dest_node,
240 				   ref->use, ref->stmt);
241       ref2->speculative = speculative;
242       ref2->lto_stmt_uid = stmt_uid;
243     }
244 }
245 
246 /* Return true when execution of REF can lead to return from
247    function. */
248 bool
ipa_ref_cannot_lead_to_return(struct ipa_ref * ref)249 ipa_ref_cannot_lead_to_return (struct ipa_ref *ref)
250 {
251   return cgraph_node_cannot_return (ipa_ref_referring_node (ref));
252 }
253 
254 /* Return true if list contains an alias.  */
255 bool
ipa_ref_has_aliases_p(struct ipa_ref_list * ref_list)256 ipa_ref_has_aliases_p (struct ipa_ref_list *ref_list)
257 {
258   struct ipa_ref *ref;
259   int i;
260 
261   for (i = 0; ipa_ref_list_referring_iterate (ref_list, i, ref); i++)
262     if (ref->use == IPA_REF_ALIAS)
263       return true;
264   return false;
265 }
266 
267 /* Find the structure describing a reference in REFERRING_NODE to REFERRED_NODE
268    and associated with statement STMT.  */
269 
270 struct ipa_ref *
ipa_find_reference(symtab_node * referring_node,symtab_node * referred_node,gimple stmt,unsigned int lto_stmt_uid)271 ipa_find_reference (symtab_node *referring_node, symtab_node *referred_node,
272 		    gimple stmt, unsigned int lto_stmt_uid)
273 {
274   struct ipa_ref *r = NULL;
275   int i;
276 
277   for (i = 0; ipa_ref_list_reference_iterate (&referring_node->ref_list, i, r); i++)
278     if (r->referred == referred_node
279 	&& !r->speculative
280 	&& ((stmt && r->stmt == stmt)
281 	    || (lto_stmt_uid && r->lto_stmt_uid == lto_stmt_uid)
282 	    || (!stmt && !lto_stmt_uid && !r->stmt && !r->lto_stmt_uid)))
283       return r;
284   return NULL;
285 }
286 
287 /* Remove all references from REFERRING_NODE that are associated with statement
288    STMT.  */
289 
290 void
ipa_remove_stmt_references(symtab_node * referring_node,gimple stmt)291 ipa_remove_stmt_references (symtab_node *referring_node, gimple stmt)
292 {
293   struct ipa_ref *r = NULL;
294   int i = 0;
295 
296   while (ipa_ref_list_reference_iterate (&referring_node->ref_list, i, r))
297     if (r->stmt == stmt)
298       ipa_remove_reference (r);
299     else
300       i++;
301 }
302 
303 /* Remove all stmt references in non-speculative references.
304    Those are not maintained during inlining & clonning.
305    The exception are speculative references that are updated along
306    with callgraph edges associated with them.  */
307 
308 void
ipa_clear_stmts_in_references(symtab_node * referring_node)309 ipa_clear_stmts_in_references (symtab_node *referring_node)
310 {
311   struct ipa_ref *r = NULL;
312   int i;
313 
314   for (i = 0; ipa_ref_list_reference_iterate (&referring_node->ref_list, i, r); i++)
315     if (!r->speculative)
316       {
317 	r->stmt = NULL;
318 	r->lto_stmt_uid = 0;
319       }
320 }
321