fix warning
[libfirm] / ir / ana / irmemory.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/ana/irmemory.c
4  * Purpose:     Memory disambiguator
5  * Author:      Michael Beck
6  * Modified by:
7  * Created:     27.12.2006
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 2006-2007 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12 #ifdef HAVE_CONFIG_H
13 #include "config.h"
14 #endif
15
16 #include <stdlib.h>
17
18 #include "irnode_t.h"
19 #include "irgraph_t.h"
20 #include "irprog_t.h"
21 #include "irmemory.h"
22 #include "irflag.h"
23 #include "hashptr.h"
24 #include "irflag.h"
25 #include "irouts.h"
26 #include "irgwalk.h"
27 #include "irprintf.h"
28
29 /** The source language specific language disambiguator function. */
30 static DISAMBIGUATOR_FUNC language_disambuigator = NULL;
31
32 /**
33  * Find the base address of an Sel node.
34  */
35 static ir_node *find_base_adr(ir_node *sel) {
36         ir_node *ptr = get_Sel_ptr(sel);
37
38         while (is_Sel(ptr)) {
39                 sel = ptr;
40                 ptr = get_Sel_ptr(sel);
41         }
42         return ptr;
43 }  /* find_base_adr */
44
45 /**
46  * Two address expressions have the same base address,
47  * check if there offsets are different.
48  *
49  * @param adr1  The first address.
50  * @param adr2  The second address.
51  */
52 static ir_alias_relation different_offsets(ir_node *adr1, ir_node *adr2) {
53         return may_alias;
54 }  /* different_offsets */
55
56 /**
57  * Determine the alias relation by checking if adr1 and adr2 are pointer
58  * to different type.
59  */
60 static ir_alias_relation different_types(ir_node *adr1, ir_node *adr2)
61 {
62         ir_entity *ent1 = NULL, *ent2 = NULL;
63
64         if (is_SymConst(adr1) && get_SymConst_kind(adr1) == symconst_addr_ent)
65                 ent1 = get_SymConst_entity(adr1);
66         else if (is_Sel(adr1))
67                 ent1 = get_Sel_entity(adr1);
68
69         if (is_SymConst(adr2) && get_SymConst_kind(adr2) == symconst_addr_ent)
70                 ent2 = get_SymConst_entity(adr2);
71         else if (is_Sel(adr2))
72                 ent2 = get_Sel_entity(adr2);
73
74         if (ent1 != NULL && ent2 != NULL) {
75                 ir_type *tp1 = get_entity_type(ent1);
76                 ir_type *tp2 = get_entity_type(ent2);
77
78                 if (tp1 != tp2) {
79                         if (is_Pointer_type(tp1) && is_Pointer_type(tp2)) {
80                                 /* do deref until no pointer types are found */
81                                 do {
82                                         tp1 = get_pointer_points_to_type(tp1);
83                                         tp2 = get_pointer_points_to_type(tp2);
84                                 } while (is_Pointer_type(tp1) && is_Pointer_type(tp2));
85                         }
86
87                         if (get_type_tpop(tp1) != get_type_tpop(tp2)) {
88                                 /* different type structure */
89                                 return no_alias;
90                         }
91                         if (is_Class_type(tp1)) {
92                                 /* check class hierarchy */
93                                 if (! is_SubClass_of(tp1, tp2) &&
94                                         ! is_SubClass_of(tp2, tp1))
95                                         return no_alias;
96                         } else {
97                                 /* different types */
98                                 return no_alias;
99                         }
100                 }
101         }
102         return may_alias;
103 }  /* different_types */
104
105 /**
106  * Determine the alias relation between two addresses.
107  */
108 static ir_alias_relation _get_alias_relation(
109         ir_graph *irg,
110         ir_node *adr1, ir_mode *mode1,
111         ir_node *adr2, ir_mode *mode2,
112         unsigned options)
113 {
114         opcode op1, op2;
115         ir_entity *ent1;
116
117         if (! get_opt_alias_analysis())
118                 return may_alias;
119
120         if (adr1 == adr2)
121                 return sure_alias;
122
123         /* Two save some code, sort the addresses by its id's. Beware, this
124            might break some things, so better check here. */
125         assert(iro_SymConst < iro_Sel && "Code dependence breaked");
126         op1 = get_irn_opcode(adr1);
127         op2 = get_irn_opcode(adr2);
128
129         if (op1 > op2) {
130                 ir_node *t = adr1;
131                 ir_mode *m = mode1;
132                 adr1  = adr2;
133                 mode1 = mode2;
134                 adr2  = t;
135                 mode2 = m;
136         }
137
138         if (is_SymConst(adr1) && get_SymConst_kind(adr1) == symconst_addr_ent) {
139                 /* first address is a global variable */
140
141                 if (is_SymConst(adr2) && get_SymConst_kind(adr2) == symconst_addr_ent) {
142                         /* both addresses are global variables and we know
143                            they are different (R1 a) */
144                         if (get_SymConst_entity(adr1) != get_SymConst_entity(adr2))
145                                 return no_alias;
146                         else
147                                 return sure_alias;
148                 }
149                 if (is_Sel(adr2)) {
150                         ir_node *base = find_base_adr(adr2);
151
152                         if (is_SymConst(base) && get_SymConst_kind(base) == symconst_addr_ent) {
153                                 /* base address is a global var (R1 a) */
154                                 if (adr1 != base)
155                                         return no_alias;
156                                 if (base == adr1)
157                                         return different_offsets(adr1, adr2);
158                         } else if (base == get_irg_frame(irg)) {
159                                 /* the second one is a local variable so they are always
160                                    different (R1 b) */
161                                 return no_alias;
162                         } else if (base == get_irg_tls(irg)) {
163                                 /* the second one is a TLS variable so they are always
164                                    different (R1 c) */
165                                 return no_alias;
166                         }
167                 }
168
169                 /* Here we are: the first is a global var, the second some pointer. */
170                 ent1 = get_SymConst_entity(adr1);
171                 if (get_entity_address_taken(ent1) == ir_address_not_taken) {
172                         /* The address of the global variable was never taken, so
173                            the pointer cannot match (R2). */
174                         return no_alias;
175                 }
176         } else if (is_Sel(adr1)) {
177                 /* the first address is a Sel */
178                 ir_node *base1 = find_base_adr(adr1);
179
180                 if (base1 == get_irg_frame(irg)) {
181                         /* the first is a local variable */
182                         if (is_Sel(adr2)) {
183                                 /* the second address is a Sel */
184                                 ir_node *base2 = find_base_adr(adr2);
185
186                                 if (base2 == get_irg_frame(irg)) {
187                                         /* the second one is a local variable */
188                                 } else if (base2 == get_irg_tls(irg)) {
189                                         /* the second one is a TLS variable so they are always
190                                        different (R1 d) */
191                                         return no_alias;
192                                 }
193                         }
194                 } else if (base1 == get_irg_tls(irg)) {
195                         /* the first is a TLS variable */
196                         if (is_Sel(adr2)) {
197                                 /* the second address is a Sel */
198                                 ir_node *base2 = find_base_adr(adr2);
199
200                                 if (base2 == get_irg_frame(irg)) {
201                                         /* the second one is a local variable so they are always
202                                        different (R1 d) */
203                                         return no_alias;
204                                 } else if (base2 == get_irg_tls(irg)) {
205                                         /* the second one is a TLS variable */
206                                 }
207                         }
208                 }
209         }
210
211         if (options & opt_strong_typed) {
212                 /* try rule R5 */
213                 ir_alias_relation rel = different_types(adr1, adr2);
214                 if (rel != may_alias)
215                         return rel;
216         }
217
218         /* do we have a language specific memory disambiguator? */
219         if (language_disambuigator) {
220                 ir_alias_relation rel = (*language_disambuigator)(irg, adr1, mode1, adr2, mode2);
221                 if (rel != may_alias)
222                         return rel;
223         }
224
225         /* access points-to information here */
226         return may_alias;
227 }  /* _get_alias_relation */
228
229 /*
230  * Determine the alias relation between two addresses.
231  */
232 ir_alias_relation get_alias_relation(
233         ir_graph *irg,
234         ir_node *adr1, ir_mode *mode1,
235         ir_node *adr2, ir_mode *mode2,
236         unsigned options)
237 {
238         ir_alias_relation rel = _get_alias_relation(irg, adr1, mode1, adr2, mode2, options);
239         return rel;
240 }  /* get_alias_relation */
241
242 /* Set a source language specific memory disambiguator function. */
243 void set_language_memory_disambiguator(DISAMBIGUATOR_FUNC func) {
244         language_disambuigator = func;
245 }  /* set_language_memory_disambiguator */
246
247 /** The result cache for the memory disambiguator. */
248 static set *result_cache = NULL;
249
250 /** An entry in the relation cache. */
251 typedef struct mem_disambig_entry {
252         ir_node           *adr1;    /**< The first address. */
253         ir_node           *adr2;    /**< The second address. */
254         ir_alias_relation result;   /**< The alias relation result. */
255 } mem_disambig_entry;
256
257 #define HASH_ENTRY(adr1, adr2)  (HASH_PTR(adr1) ^ HASH_PTR(adr2))
258
259 /**
260  * Compare two relation cache entries.
261  */
262 static int cmp_mem_disambig_entry(const void *elt, const void *key, size_t size) {
263         const mem_disambig_entry *p1 = elt;
264         const mem_disambig_entry *p2 = key;
265
266         return p1->adr1 == p2->adr1 && p1->adr2 == p2->adr2;
267 }  /* cmp_mem_disambig_entry */
268
269 /**
270  * Initialize the relation cache.
271  */
272 void mem_disambig_init(void) {
273         result_cache = new_set(cmp_mem_disambig_entry, 8);
274 }  /* mem_disambig_init */
275
276 /*
277  * Determine the alias relation between two addresses.
278  */
279 ir_alias_relation get_alias_relation_ex(
280         ir_graph *irg,
281         ir_node *adr1, ir_mode *mode1,
282         ir_node *adr2, ir_mode *mode2,
283         unsigned options)
284 {
285         mem_disambig_entry key, *entry;
286
287         if (! get_opt_alias_analysis())
288                 return may_alias;
289
290         if (get_irn_opcode(adr1) > get_irn_opcode(adr2)) {
291                 ir_node *t = adr1;
292                 adr1 = adr2;
293                 adr2 = t;
294         }
295
296         key.adr1 = adr1;
297         key.adr2 = adr2;
298         entry = set_find(result_cache, &key, sizeof(key), HASH_ENTRY(adr1, adr2));
299         if (entry)
300                 return entry->result;
301
302         key.result = get_alias_relation(irg, adr1, mode1, adr2, mode2, options);
303
304         set_insert(result_cache, &key, sizeof(key), HASH_ENTRY(adr1, adr2));
305         return key.result;
306 }  /* get_alias_relation_ex */
307
308 /* Free the relation cache. */
309 void mem_disambig_term(void) {
310         if (result_cache) {
311                 del_set(result_cache);
312                 result_cache = NULL;
313         }
314 }  /* mem_disambig_term */
315
316 /**
317  * Check the mode of a Load/Store with the mode of the entity
318  * that is accessed.
319  * If the mode of the entity and the Load/Store mode do not match, we
320  * have the bad reinterpret case:
321  *
322  * int i;
323  * char b = *(char *)&i;
324  *
325  * We do NOT count this as one value and return address_taken
326  * in that case.
327  * However, we support an often used case. If the mode is two-complement
328  * we allow casts between signed/unsigned.
329  *
330  * @param mode     the mode of the Load/Store
331  * @param ent_mode the mode of the accessed entity
332  *
333  * @return non-zero if the Load/Store is a hidden cast, zero else
334  */
335 static int is_hidden_cast(ir_mode *mode, ir_mode *ent_mode) {
336         if (ent_mode != mode) {
337                 if (ent_mode == NULL ||
338                         get_mode_size_bits(ent_mode) != get_mode_size_bits(mode) ||
339                         get_mode_sort(ent_mode) != get_mode_sort(mode) ||
340                         get_mode_arithmetic(ent_mode) != irma_twos_complement ||
341                         get_mode_arithmetic(mode) != irma_twos_complement)
342                         return 1;
343         }
344         return 0;
345 }  /* is_hidden_cast */
346
347 /**
348  * Determine the address_taken state of a node (or it's successor Sels).
349  *
350  * @param irn  the node
351  */
352 static ir_address_taken_state find_address_taken_state(ir_node *irn) {
353         int     i;
354         ir_mode *emode, *mode;
355         ir_node *value;
356         ir_entity *ent;
357
358         for (i = get_irn_n_outs(irn) - 1; i >= 0; --i) {
359                 ir_node *succ = get_irn_out(irn, i);
360
361                 switch (get_irn_opcode(succ)) {
362                 case iro_Load:
363                         /* check if this load is not a hidden conversion */
364                         mode = get_Load_mode(succ);
365                         ent = is_SymConst(irn) ? get_SymConst_entity(irn) : get_Sel_entity(irn);
366                         emode = get_type_mode(get_entity_type(ent));
367                         if (is_hidden_cast(mode, emode))
368                                 return ir_address_taken;
369                         break;
370
371                 case iro_Store:
372                         /* check that the node is not the Store's value */
373                         value = get_Store_value(succ);
374                         if (value == irn)
375                                 return ir_address_taken;
376                         /* check if this Store is not a hidden conversion */
377                         mode = get_irn_mode(value);
378                         ent = is_SymConst(irn) ? get_SymConst_entity(irn) : get_Sel_entity(irn);
379                         emode = get_type_mode(get_entity_type(ent));
380                         if (is_hidden_cast(mode, emode))
381                                 return ir_address_taken;
382                         break;
383
384                 case iro_Sel: {
385                         /* Check the successor of irn. */
386                         ir_address_taken_state res = find_address_taken_state(succ);
387                         if (res != ir_address_not_taken)
388                                 return res;
389                         break;
390                 }
391
392                 case iro_Call:
393                         /* Only the call address is not an address taker but
394                            this is an uninteresting case, so we ignore it here. */
395                         return ir_address_taken;
396
397                 default:
398                         /* another op, the address may be taken */
399                         return ir_address_taken_unknown;
400                 }
401         }
402         /* All successors finished, the address is not taken. */
403         return ir_address_not_taken;
404 }  /* find_address_taken_state */
405
406 /**
407  * Update the "address taken" flag of all frame entities.
408  */
409 static void analyse_irg_address_taken(ir_graph *irg) {
410         ir_type *ft = get_irg_frame_type(irg);
411         ir_node *irg_frame;
412         int i;
413
414         /* set initial state to not_taken, as this is the "smallest" state */
415         for (i = get_class_n_members(ft) - 1; i >= 0; --i) {
416                 entity *ent = get_class_member(ft, i);
417
418                 set_entity_address_taken(ent, ir_address_not_taken);
419         }
420
421         assure_irg_outs(irg);
422
423         irg_frame = get_irg_frame(irg);
424
425         for (i = get_irn_n_outs(irg_frame) - 1; i >= 0; --i) {
426                 ir_node *succ = get_irn_out(irg_frame, i);
427                 ir_address_taken_state state;
428
429             if (is_Sel(succ)) {
430                         ir_entity *ent = get_Sel_entity(succ);
431
432                         if (get_entity_address_taken(ent) == ir_address_taken)
433                                 continue;
434
435                         state = find_address_taken_state(succ);
436                         if (state > get_entity_address_taken(ent))
437                                 set_entity_address_taken(ent, state);
438                 }
439         }
440         /* now computed */
441         irg->adr_taken_state = ir_address_taken_computed;
442 }  /* analyse_address_taken */
443
444 /* Returns the current address taken state of the graph. */
445 ir_address_taken_computed_state get_irg_address_taken_state(const ir_graph *irg) {
446         return irg->adr_taken_state;
447 }  /* get_irg_address_taken_state */
448
449 /* Sets the current address taken state of the graph. */
450 void set_irg_address_taken_state(ir_graph *irg, ir_address_taken_computed_state state) {
451         irg->adr_taken_state = state;
452 }  /* set_irg_address_taken_state */
453
454 /* Assure that the address taken flag is computed for the given graph. */
455 void assure_irg_address_taken_computed(ir_graph *irg) {
456         if (irg->adr_taken_state == ir_address_taken_not_computed)
457                 analyse_irg_address_taken(irg);
458 }  /* assure_irg_address_taken_computed */
459
460 /**
461  * Initialize the address_taken flag for a global type like type.
462  */
463 static void init_taken_flag(ir_type * tp) {
464         int i;
465
466         /* All external visible entities are at least
467            ir_address_taken_unknown. This is very conservative. */
468         for (i = get_compound_n_members(tp) - 1; i >= 0; --i) {
469                 entity *ent = get_compound_member(tp, i);
470                 ir_address_taken_state state;
471
472                 state = get_entity_visibility(ent) == visibility_external_visible ?
473                                 ir_address_taken_unknown : ir_address_not_taken ;
474                 set_entity_address_taken(ent, state);
475         }
476 }  /* init_taken_flag */
477
478 /**
479  * Print the address taken state of all entities of a given type for debugging.
480  */
481 static void print_address_taken_state(ir_type *tp) {
482         int i;
483         for (i = get_compound_n_members(tp) - 1; i >= 0; --i) {
484                 entity *ent = get_compound_member(tp, i);
485                 ir_address_taken_state state = get_entity_address_taken(ent);
486
487                 if (state != ir_address_not_taken) {
488                         assert(ir_address_not_taken <= state && state <= ir_address_taken);
489                         ir_printf("%+F: %s\n", ent, get_address_taken_state_name(state));
490                 }
491         }
492 }  /* print_address_taken_state */
493
494 /**
495  * Post-walker: check for global entity address
496  */
497 static void check_global_address(ir_node *irn, void *env) {
498         ir_node *tls = env;
499         entity *ent;
500         ir_address_taken_state state;
501
502         if (is_SymConst(irn) && get_SymConst_kind(irn) == symconst_addr_ent) {
503                 /* A global. */
504                 ent = get_SymConst_entity(irn);
505         } else if (is_Sel(irn) && get_Sel_ptr(irn) == tls) {
506                 /* A TLS variable. */
507                 ent = get_SymConst_entity(irn);
508         } else
509                 return;
510
511         if (get_entity_address_taken(ent) == ir_address_not_taken) {
512                 /* Already at the maximum. */
513                 return;
514         }
515         state = find_address_taken_state(irn);
516         if (state > get_entity_address_taken(ent))
517                 set_entity_address_taken(ent, state);
518 }  /* check_global_address */
519
520 /**
521  * Update the "address taken" flag of all global entities.
522  */
523 static void analyse_irp_globals_address_taken(void) {
524         int i;
525
526         init_taken_flag(get_glob_type());
527         init_taken_flag(get_tls_type());
528
529         for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
530                 ir_graph *irg = get_irp_irg(i);
531
532                 assure_irg_outs(irg);
533                 irg_walk_graph(irg, NULL, check_global_address, get_irg_tls(irg));
534         }
535         print_address_taken_state(get_glob_type());
536         print_address_taken_state(get_tls_type());
537
538         /* now computed */
539         irp->globals_adr_taken_state = ir_address_taken_computed;
540 }  /* analyse_irp_globals_address_taken */
541
542 /* Returns the current address taken state of the globals. */
543 ir_address_taken_computed_state get_irp_globals_address_taken_state(void) {
544         return irp->globals_adr_taken_state;
545 }  /* get_irp_globals_address_taken_state */
546
547 /* Sets the current address taken state of the graph. */
548 void set_irp_globals_address_taken_state(ir_address_taken_computed_state state) {
549         irp->globals_adr_taken_state = state;
550 }  /* set_irg_address_taken_state */
551
552 /* Assure that the address taken flag is computed for the globals. */
553 void assure_irp_globals_address_taken_computed(void) {
554         if (irp->globals_adr_taken_state == ir_address_taken_not_computed)
555                 analyse_irp_globals_address_taken();
556 }  /* assure_irp_globals_address_taken_computed */