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