2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief Memory disambiguator
23 * @author Michael Beck
34 #include "irgraph_t.h"
36 #include "irmemory_t.h"
49 /** The debug handle. */
50 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
51 DEBUG_ONLY(static firm_dbg_module_t *dbgcall = NULL;)
53 /** The source language specific language disambiguator function. */
54 static DISAMBIGUATOR_FUNC language_disambuigator = NULL;
56 /** The global memory disambiguator options. */
57 static unsigned global_mem_disamgig_opt = aa_opt_no_opt;
59 /* Returns a human readable name for an alias relation. */
60 const char *get_ir_alias_relation_name(ir_alias_relation rel) {
61 #define X(a) case a: return #a
66 default: assert(0); return "UNKNOWN";
71 /* Get the memory disambiguator options for a graph. */
72 unsigned get_irg_memory_disambiguator_options(const ir_graph *irg) {
73 unsigned opt = irg->mem_disambig_opt;
74 if (opt & aa_opt_inherited)
75 return global_mem_disamgig_opt;
77 } /* get_irg_memory_disambiguator_options */
79 /* Set the memory disambiguator options for a graph. */
80 void set_irg_memory_disambiguator_options(ir_graph *irg, unsigned options) {
81 irg->mem_disambig_opt = options & ~aa_opt_inherited;
82 } /* set_irg_memory_disambiguator_options */
84 /* Set the global disambiguator options for all graphs not having local options. */
85 void set_irp_memory_disambiguator_options(unsigned options) {
86 global_mem_disamgig_opt = options;
87 } /* set_irp_memory_disambiguator_options */
90 * Find the base address and entity of an Sel node.
93 * @param pEnt after return points to the base entity.
95 * @return the base address.
97 static ir_node *find_base_adr(const ir_node *sel, ir_entity **pEnt) {
98 ir_node *ptr = get_Sel_ptr(sel);
100 while (is_Sel(ptr)) {
102 ptr = get_Sel_ptr(sel);
104 *pEnt = get_Sel_entity(sel);
106 } /* find_base_adr */
109 * Check if a given Const node is greater or equal a given size.
111 * @param cns a Const node
112 * @param size a integer size
114 * @return ir_no_alias if the Const is greater, ir_may_alias else
116 static ir_alias_relation check_const(const ir_node *cns, int size) {
117 tarval *tv = get_Const_tarval(cns);
121 return tarval_is_null(tv) ? ir_may_alias : ir_no_alias;
122 tv_size = new_tarval_from_long(size, get_tarval_mode(tv));
123 return tarval_cmp(tv_size, tv) & (pn_Cmp_Eq|pn_Cmp_Lt) ? ir_no_alias : ir_may_alias;
127 * Treat idx1 and idx2 as integer indexes and check if they differ always more than size.
129 * @param idx1 a node representing the first index
130 * @param idx2 a node representing the second index
131 * @param size an integer size
133 * @return ir_sure_alias iff idx1 == idx2
134 * ir_no_alias iff they ALWAYS differ more than size
137 static ir_alias_relation different_index(const ir_node *idx1, const ir_node *idx2, int size) {
139 return ir_sure_alias;
140 if (is_Const(idx1) && is_Const(idx2)) {
141 /* both are const, we can compare them */
142 tarval *tv1 = get_Const_tarval(idx1);
143 tarval *tv2 = get_Const_tarval(idx2);
144 tarval *tv, *tv_size;
148 return tv1 == tv2 ? ir_sure_alias : ir_no_alias;
150 /* arg, modes may be different */
151 m1 = get_tarval_mode(tv1);
152 m2 = get_tarval_mode(tv2);
154 int size = get_mode_size_bits(m1) - get_mode_size_bits(m2);
157 /* m1 is a small mode, cast up */
158 m1 = mode_is_signed(m1) ? find_signed_mode(m2) : find_unsigned_mode(m2);
160 /* should NOT happen, but if it does we give up here */
163 tv1 = tarval_convert_to(tv1, m1);
164 } else if (size > 0) {
165 /* m2 is a small mode, cast up */
166 m2 = mode_is_signed(m2) ? find_signed_mode(m1) : find_unsigned_mode(m1);
168 /* should NOT happen, but if it does we give up here */
171 tv2 = tarval_convert_to(tv2, m2);
173 /* here the size should be identical, check for signed */
174 if (get_mode_sign(m1) != get_mode_sign(m2)) {
175 /* find the signed */
176 if (mode_is_signed(m2)) {
183 /* m1 is now the signed one */
184 if (tarval_cmp(tv1, get_tarval_null(m1)) & (pn_Cmp_Eq|pn_Cmp_Gt)) {
185 /* tv1 is signed, but >= 0, simply cast into unsigned */
186 tv1 = tarval_convert_to(tv1, m2);
188 tv_size = new_tarval_from_long(size, m2);
190 if (tarval_cmp(tv2, tv_size) & (pn_Cmp_Eq|pn_Cmp_Gt)) {
191 /* tv1 is negative and tv2 >= tv_size, so the difference is bigger than size */
194 /* tv_size > tv2, so we can subtract without overflow */
195 tv2 = tarval_sub(tv_size, tv2, NULL);
197 /* tv1 is < 0, so we can negate it */
198 tv1 = tarval_neg(tv1);
200 /* cast it into unsigned. for two-complement it does the right thing for MIN_INT */
201 tv1 = tarval_convert_to(tv1, m2);
203 /* now we can compare without overflow */
204 return tarval_cmp(tv1, tv2) & (pn_Cmp_Eq|pn_Cmp_Gt) ? ir_no_alias : ir_may_alias;
208 if (tarval_cmp(tv1, tv2) == pn_Cmp_Gt) {
213 /* tv1 is now the "smaller" one */
214 tv = tarval_sub(tv2, tv1, NULL);
215 tv_size = new_tarval_from_long(size, get_tarval_mode(tv));
216 return tarval_cmp(tv_size, tv) & (pn_Cmp_Eq|pn_Cmp_Lt) ? ir_no_alias : ir_may_alias;
219 /* Note: we rely here on the fact that normalization puts constants on the RIGHT side */
221 ir_node *l1 = get_Add_left(idx1);
222 ir_node *r1 = get_Add_right(idx1);
227 return check_const(r1, size);
230 /* both are Adds, check if they are of x + a == x + b kind */
231 ir_node *l2 = get_Add_left(idx2);
232 ir_node *r2 = get_Add_right(idx2);
235 return different_index(r1, r2, size);
237 return different_index(r1, l2, size);
239 return different_index(l1, l2, size);
241 return different_index(l1, r2, size);
245 ir_node *l2 = get_Add_left(idx2);
246 ir_node *r2 = get_Add_right(idx2);
251 return check_const(r2, size);
256 ir_node *l1 = get_Sub_left(idx1);
257 ir_node *r1 = get_Sub_right(idx1);
262 return check_const(r1, size);
266 /* both are Subs, check if they are of x - a == x - b kind */
267 ir_node *l2 = get_Sub_left(idx2);
270 ir_node *r2 = get_Sub_right(idx2);
271 return different_index(r1, r2, size);
276 ir_node *l2 = get_Sub_left(idx2);
277 ir_node *r2 = get_Sub_right(idx2);
282 return check_const(r2, size);
287 } /* different_index */
290 * Two Sel addresses have the same base address, check if there offsets are
293 * @param adr1 The first address.
294 * @param adr2 The second address.
296 static ir_alias_relation different_sel_offsets(const ir_node *sel1, const ir_node *sel2) {
297 /* seems to be broken */
301 ir_entity *ent1 = get_Sel_entity(sel1);
302 ir_entity *ent2 = get_Sel_entity(sel2);
303 int i, check_arr = 0;
308 ir_type *tp1 = get_entity_type(ent1);
309 ir_type *tp2 = get_entity_type(ent2);
313 else if (get_type_state(tp1) == layout_fixed && get_type_state(tp2) == layout_fixed &&
314 get_type_size_bits(tp1) == get_type_size_bits(tp2))
318 /* we select an entity of same size, check for indexes */
319 int n = get_Sel_n_indexs(sel1);
322 if (n > 0 && n == get_Sel_n_indexs(sel2)) {
323 /* same non-zero number of indexes, an array access, check */
324 for (i = 0; i < n; ++i) {
325 ir_node *idx1 = get_Sel_index(sel1, i);
326 ir_node *idx2 = get_Sel_index(sel2, i);
327 ir_alias_relation res = different_index(idx1, idx2, 0); /* we can safely IGNORE the size here if it's at least >0 */
329 if (res == may_alias)
331 else if (res == no_alias)
334 /* if we have at least one no_alias, there is no alias relation, else we have sure */
335 return have_no > 0 ? no_alias : sure_alias;
339 (void) different_index;
342 } /* different_sel_offsets */
345 * Determine the alias relation by checking if adr1 and adr2 are pointer
348 * @param adr1 The first address.
349 * @param adr2 The second address.
351 static ir_alias_relation different_types(const ir_node *adr1, const ir_node *adr2)
353 ir_entity *ent1 = NULL, *ent2 = NULL;
356 ent1 = get_Global_entity(adr1);
357 else if (is_Sel(adr1))
358 ent1 = get_Sel_entity(adr1);
361 ent2 = get_Global_entity(adr2);
362 else if (is_Sel(adr2))
363 ent2 = get_Sel_entity(adr2);
365 if (ent1 != NULL && ent2 != NULL) {
366 ir_type *tp1 = get_entity_type(ent1);
367 ir_type *tp2 = get_entity_type(ent2);
370 /* do deref until no pointer types are found */
371 while (is_Pointer_type(tp1) && is_Pointer_type(tp2)) {
372 tp1 = get_pointer_points_to_type(tp1);
373 tp2 = get_pointer_points_to_type(tp2);
376 if (get_type_tpop(tp1) != get_type_tpop(tp2)) {
377 /* different type structure */
380 if (is_Class_type(tp1)) {
381 /* check class hierarchy */
382 if (! is_SubClass_of(tp1, tp2) &&
383 ! is_SubClass_of(tp2, tp1))
386 /* different types */
392 } /* different_types */
395 * Returns non-zero if a node is a result on a malloc-like routine.
397 * @param node the Proj node to test
399 static int is_malloc_Result(const ir_node *node) {
400 node = get_Proj_pred(node);
403 node = get_Proj_pred(node);
406 node = get_Call_ptr(node);
407 if (is_Global(node)) {
408 ir_entity *ent = get_Global_entity(node);
410 if (get_entity_additional_properties(ent) & mtp_property_malloc)
415 } /* is_malloc_Result */
418 * Classify a base pointer.
420 * @param irg the graph of the pointer
421 * @param irn the node representing the base address
422 * @param ent the base entity of the base address iff any
424 ir_storage_class_class_t classify_pointer(const ir_graph *irg, const ir_node *irn, const ir_entity *ent)
426 ir_storage_class_class_t res = ir_sc_pointer;
427 if (is_Global(irn)) {
428 ir_entity *entity = get_Global_entity(irn);
429 res = ir_sc_globalvar;
430 if (! (get_entity_usage(entity) & ir_usage_address_taken))
431 res |= ir_sc_modifier_nottaken;
432 } else if (irn == get_irg_frame(irg)) {
433 res = ir_sc_localvar;
434 if (ent != NULL && !(get_entity_usage(ent) & ir_usage_address_taken))
435 res |= ir_sc_modifier_nottaken;
436 } else if (irn == get_irg_tls(irg)) {
438 if (ent != NULL && !(get_entity_usage(ent) & ir_usage_address_taken))
439 res |= ir_sc_modifier_nottaken;
440 } else if (is_Proj(irn) && is_malloc_Result(irn)) {
441 return ir_sc_malloced;
442 } else if (is_Const(irn)) {
443 return ir_sc_globaladdr;
444 } else if (is_arg_Proj(irn)) {
445 res |= ir_sc_modifier_argument;
452 * If adr represents a Bitfield Sel, skip it
454 static const ir_node *skip_Bitfield_Sels(const ir_node *adr) {
456 ir_entity *ent = get_Sel_entity(adr);
457 ir_type *bf_type = get_entity_type(ent);
459 /* is it a bitfield type? */
460 if (is_Primitive_type(bf_type) && get_primitive_base_type(bf_type) != NULL)
461 adr = get_Sel_ptr(adr);
467 * Determine the alias relation between two addresses.
469 * @param irg the graph of both memory operations
470 * @param addr1 pointer address of the first memory operation
471 * @param mode1 the mode of the accessed data through addr1
472 * @param addr2 pointer address of the second memory operation
473 * @param mode2 the mode of the accessed data through addr2
475 * @return found memory relation
477 static ir_alias_relation _get_alias_relation(
479 const ir_node *adr1, const ir_mode *mode1,
480 const ir_node *adr2, const ir_mode *mode2)
482 ir_entity *ent1, *ent2;
486 const ir_node *base1;
487 const ir_node *base2;
488 const ir_node *orig_adr1 = adr1;
489 const ir_node *orig_adr2 = adr2;
491 ir_storage_class_class_t class1, class2, mod1, mod2;
492 int have_const_offsets;
494 if (! get_opt_alias_analysis())
498 return ir_sure_alias;
500 options = get_irg_memory_disambiguator_options(irg);
502 /* The Armageddon switch */
503 if (options & aa_opt_no_alias)
506 /* do the addresses have constants offsets?
507 * Note: nodes are normalized to have constants at right inputs,
508 * sub X, C is normalized to add X, -C
510 have_const_offsets = 1;
511 while (is_Add(adr1)) {
512 ir_node *add_right = get_Add_right(adr1);
513 if (is_Const(add_right) && !mode_is_reference(get_irn_mode(add_right))) {
514 tarval *tv = get_Const_tarval(add_right);
515 offset1 += get_tarval_long(tv);
516 adr1 = get_Add_left(adr1);
517 } else if (mode_is_reference(get_irn_mode(add_right))) {
519 have_const_offsets = 0;
521 adr1 = get_Add_left(adr1);
522 have_const_offsets = 0;
525 while (is_Add(adr2)) {
526 ir_node *add_right = get_Add_right(adr2);
527 if (is_Const(add_right) && !mode_is_reference(get_irn_mode(add_right))) {
528 tarval *tv = get_Const_tarval(add_right);
529 offset2 += get_tarval_long(tv);
530 adr2 = get_Add_left(adr2);
531 } else if (mode_is_reference(get_irn_mode(add_right))) {
533 have_const_offsets = 0;
535 adr2 = get_Add_left(adr2);
536 have_const_offsets = 0;
540 mode_size = get_mode_size_bytes(mode1);
541 if (get_mode_size_bytes(mode2) > mode_size) {
542 mode_size = get_mode_size_bytes(mode2);
545 /* same base address -> compare offsets if possible.
546 * FIXME: type long is not sufficient for this task ...
548 if (adr1 == adr2 && have_const_offsets) {
549 if ((unsigned long)labs(offset2 - offset1) >= mode_size)
552 return ir_sure_alias;
556 * Bitfields can be constructed as Sels from its base address.
557 * As they have different entities, the disambiguator would find that they are
558 * alias free. While this is true for it's values, it is false for the addresses
559 * (strictly speaking, the Sel's are NOT the addresses of the bitfields).
560 * So, skip those bitfield selecting Sel's.
562 adr1 = skip_Bitfield_Sels(adr1);
563 adr2 = skip_Bitfield_Sels(adr2);
571 base1 = find_base_adr(adr1, &ent1);
574 base2 = find_base_adr(adr2, &ent2);
577 /* same base address -> compare Sel entities */
578 if (base1 == base2 && ent1 != NULL && ent2 != NULL) {
581 else if (have_const_offsets)
582 return different_sel_offsets(adr1, adr2);
585 mod1 = classify_pointer(irg, base1, ent1);
586 mod2 = classify_pointer(irg, base2, ent2);
588 class1 = GET_BASE_SC(mod1);
589 class2 = GET_BASE_SC(mod2);
591 if (class1 == ir_sc_pointer || class2 == ir_sc_pointer) {
592 /* swap pointer class to class1 */
593 if (class2 == ir_sc_pointer) {
594 ir_storage_class_class_t temp = mod1;
597 class1 = GET_BASE_SC(mod1);
598 class2 = GET_BASE_SC(mod2);
600 /* a pointer and an object whose address was never taken */
601 if (mod2 & ir_sc_modifier_nottaken) {
604 if (mod1 & ir_sc_modifier_argument) {
605 if ( (options & aa_opt_no_alias_args)
606 && (mod2 & ir_sc_modifier_argument))
608 if ( (options & aa_opt_no_alias_args_global)
609 && (class2 == ir_sc_globalvar
610 || class2 == ir_sc_tls
611 || class2 == ir_sc_globaladdr))
614 } else if (class1 != class2) {
615 /* two objects from different memory spaces */
618 /* both classes are equal */
619 if (class1 == ir_sc_globalvar) {
620 ir_entity *entity1 = get_SymConst_entity(base1);
621 ir_entity *entity2 = get_SymConst_entity(base2);
622 if (entity1 != entity2)
625 /* for some reason CSE didn't happen yet for the 2 SymConsts... */
627 } else if (class1 == ir_sc_globaladdr) {
628 tarval *tv = get_Const_tarval(base1);
629 offset1 += get_tarval_long(tv);
630 tv = get_Const_tarval(base2);
631 offset2 += get_tarval_long(tv);
633 if ((unsigned long)labs(offset2 - offset1) >= mode_size)
636 return ir_sure_alias;
640 /* Type based alias analysis */
641 if (options & aa_opt_type_based) {
642 ir_alias_relation rel;
644 if (options & aa_opt_byte_type_may_alias) {
645 if (get_mode_size_bits(mode1) == 8 || get_mode_size_bits(mode2) == 8) {
646 /* One of the modes address a byte. Assume a ir_may_alias and leave
647 the type based check. */
648 goto leave_type_based_alias;
651 /* cheap check: If the mode sizes did not match, the types MUST be different */
652 if (get_mode_size_bits(mode1) != get_mode_size_bits(mode2))
655 /* cheap test: if only one is a reference mode, no alias */
656 if (mode_is_reference(mode1) != mode_is_reference(mode2))
659 /* cheap test: if arithmetic is different, no alias */
660 if (get_mode_arithmetic(mode1) != get_mode_arithmetic(mode2))
664 rel = different_types(orig_adr1, orig_adr2);
665 if (rel != ir_may_alias)
667 leave_type_based_alias:;
670 /* do we have a language specific memory disambiguator? */
671 if (language_disambuigator != NULL) {
672 ir_alias_relation rel = language_disambuigator(irg, orig_adr1, mode1, orig_adr2, mode2);
673 if (rel != ir_may_alias)
677 /* access points-to information here */
679 } /* _get_alias_relation */
682 * Determine the alias relation between two addresses.
684 ir_alias_relation get_alias_relation(
686 const ir_node *adr1, const ir_mode *mode1,
687 const ir_node *adr2, const ir_mode *mode2)
689 ir_alias_relation rel = _get_alias_relation(irg, adr1, mode1, adr2, mode2);
690 DB((dbg, LEVEL_1, "alias(%+F, %+F) = %s\n", adr1, adr2, get_ir_alias_relation_name(rel)));
692 } /* get_alias_relation */
694 /* Set a source language specific memory disambiguator function. */
695 void set_language_memory_disambiguator(DISAMBIGUATOR_FUNC func) {
696 language_disambuigator = func;
697 } /* set_language_memory_disambiguator */
699 /** The result cache for the memory disambiguator. */
700 static set *result_cache = NULL;
702 /** An entry in the relation cache. */
703 typedef struct mem_disambig_entry {
704 const ir_node *adr1; /**< The first address. */
705 const ir_mode *mode1; /**< The first address mode. */
706 const ir_node *adr2; /**< The second address. */
707 const ir_mode *mode2; /**< The second address mode. */
708 ir_alias_relation result; /**< The alias relation result. */
709 } mem_disambig_entry;
711 #define HASH_ENTRY(adr1, adr2) (HASH_PTR(adr1) ^ HASH_PTR(adr2))
714 * Compare two relation cache entries.
716 static int cmp_mem_disambig_entry(const void *elt, const void *key, size_t size) {
717 const mem_disambig_entry *p1 = elt;
718 const mem_disambig_entry *p2 = key;
721 return p1->adr1 == p2->adr1 && p1->adr2 == p2->adr2 &&
722 p1->mode1 == p2->mode1 && p1->mode2 == p2->mode2;
723 } /* cmp_mem_disambig_entry */
726 * Initialize the relation cache.
728 void mem_disambig_init(void) {
729 result_cache = new_set(cmp_mem_disambig_entry, 8);
730 } /* mem_disambig_init */
733 * Determine the alias relation between two addresses.
735 ir_alias_relation get_alias_relation_ex(
737 const ir_node *adr1, const ir_mode *mode1,
738 const ir_node *adr2, const ir_mode *mode2)
740 mem_disambig_entry key, *entry;
742 ir_fprintf(stderr, "%+F <-> %+F\n", adr1, adr2);
744 if (! get_opt_alias_analysis())
747 if (get_irn_opcode(adr1) > get_irn_opcode(adr2)) {
748 const ir_node *t = adr1;
757 entry = set_find(result_cache, &key, sizeof(key), HASH_ENTRY(adr1, adr2));
759 return entry->result;
761 key.result = get_alias_relation(irg, adr1, mode1, adr2, mode2);
763 set_insert(result_cache, &key, sizeof(key), HASH_ENTRY(adr1, adr2));
765 } /* get_alias_relation_ex */
767 /* Free the relation cache. */
768 void mem_disambig_term(void) {
769 if (result_cache != NULL) {
770 del_set(result_cache);
773 } /* mem_disambig_term */
776 * Check the mode of a Load/Store with the mode of the entity
778 * If the mode of the entity and the Load/Store mode do not match, we
779 * have the bad reinterpret case:
782 * char b = *(char *)&i;
784 * We do NOT count this as one value and return address_taken
786 * However, we support an often used case. If the mode is two-complement
787 * we allow casts between signed/unsigned.
789 * @param mode the mode of the Load/Store
790 * @param ent_mode the mode of the accessed entity
792 * @return non-zero if the Load/Store is a hidden cast, zero else
794 static int is_hidden_cast(const ir_mode *mode, const ir_mode *ent_mode) {
795 if (ent_mode == NULL)
798 if (ent_mode != mode) {
799 if (ent_mode == NULL ||
800 get_mode_size_bits(ent_mode) != get_mode_size_bits(mode) ||
801 get_mode_sort(ent_mode) != get_mode_sort(mode) ||
802 get_mode_arithmetic(ent_mode) != irma_twos_complement ||
803 get_mode_arithmetic(mode) != irma_twos_complement)
807 } /* is_hidden_cast */
810 * Determine the usage state of a node (or its successor Sels).
812 * @param irn the node
814 static ir_entity_usage determine_entity_usage(const ir_node *irn, ir_entity *entity) {
816 ir_mode *emode, *mode;
819 ir_entity_usage res = 0;
821 for (i = get_irn_n_outs(irn) - 1; i >= 0; --i) {
822 ir_node *succ = get_irn_out(irn, i);
824 switch (get_irn_opcode(succ)) {
826 /* beware: irn might be a Id node here, so irn might be not
827 equal to get_Load_ptr(succ) */
828 res |= ir_usage_read;
830 /* check if this load is not a hidden conversion */
831 mode = get_Load_mode(succ);
832 emode = get_type_mode(get_entity_type(entity));
833 if (is_hidden_cast(mode, emode))
834 res |= ir_usage_reinterpret_cast;
838 /* check that the node is not the Store's value */
839 if (irn == get_Store_value(succ)) {
840 res |= ir_usage_unknown;
842 if (irn == get_Store_ptr(succ)) {
843 res |= ir_usage_write;
845 /* check if this Store is not a hidden conversion */
846 value = get_Store_value(succ);
847 mode = get_irn_mode(value);
848 emode = get_type_mode(get_entity_type(entity));
849 if (is_hidden_cast(mode, emode))
850 res |= ir_usage_reinterpret_cast;
852 assert(irn != get_Store_mem(succ));
856 /* CopyB are like Loads/Stores */
857 tp = get_entity_type(entity);
858 if (tp != get_CopyB_type(succ)) {
859 /* bad, different types, might be a hidden conversion */
860 res |= ir_usage_reinterpret_cast;
862 if (irn == get_CopyB_dst(succ)) {
863 res |= ir_usage_write;
865 assert(irn == get_CopyB_src(succ));
866 res |= ir_usage_read;
872 /* Check the successor of irn. */
873 res |= determine_entity_usage(succ, entity);
876 ir_entity *entity = get_Sel_entity(succ);
877 /* this analysis can't handle unions correctly */
878 if (is_Union_type(get_entity_owner(entity))) {
879 res |= ir_usage_unknown;
882 /* Check the successor of irn. */
883 res |= determine_entity_usage(succ, entity);
888 if (irn == get_Call_ptr(succ)) {
889 /* TODO: we could check for reinterpret casts here...
890 * But I doubt anyone is interested in that bit for
891 * function entities and I'm too lazy to write the code now.
893 res |= ir_usage_read;
895 assert(irn != get_Call_mem(succ));
896 res |= ir_usage_unknown;
900 /* skip identities */
902 res |= determine_entity_usage(succ, entity);
908 for (input_nr = get_Tuple_n_preds(succ) - 1; input_nr >= 0;
910 ir_node *pred = get_Tuple_pred(succ, input_nr);
913 /* we found one input */
914 for (k = get_irn_n_outs(succ) - 1; k >= 0; --k) {
915 ir_node *proj = get_irn_out(succ, k);
917 if (is_Proj(proj) && get_Proj_proj(proj) == input_nr) {
918 res |= determine_entity_usage(proj, entity);
928 /* another op, we don't know anything (we could do more advanced
929 * things like a dataflow analysis here) */
930 res |= ir_usage_unknown;
939 * Update the usage flags of all frame entities.
941 static void analyse_irg_entity_usage(ir_graph *irg) {
942 ir_type *ft = get_irg_frame_type(irg);
944 int i, j, k, static_link_arg;
946 /* set initial state to not_taken, as this is the "smallest" state */
947 for (i = get_class_n_members(ft) - 1; i >= 0; --i) {
948 ir_entity *ent = get_class_member(ft, i);
950 /* methods can only be analyzed globally */
951 if (! is_method_entity(ent)) {
952 ir_entity_usage flags = 0;
953 if (get_entity_linkage(ent) & IR_LINKAGE_HIDDEN_USER)
954 flags = ir_usage_unknown;
955 set_entity_usage(ent, flags);
959 assure_irg_outs(irg);
961 irg_frame = get_irg_frame(irg);
963 for (i = get_irn_n_outs(irg_frame) - 1; i >= 0; --i) {
964 ir_node *succ = get_irn_out(irg_frame, i);
966 ir_entity_usage flags;
971 entity = get_Sel_entity(succ);
972 flags = get_entity_usage(entity);
973 flags |= determine_entity_usage(succ, entity);
974 set_entity_usage(entity, flags);
977 /* check inner functions accessing outer frame */
979 for (i = get_class_n_members(ft) - 1; i >= 0; --i) {
980 ir_entity *ent = get_class_member(ft, i);
984 if (! is_method_entity(ent))
987 inner_irg = get_entity_irg(ent);
988 if (inner_irg == NULL)
991 assure_irg_outs(inner_irg);
992 args = get_irg_args(inner_irg);
993 for (j = get_irn_n_outs(args) - 1; j >= 0; --j) {
994 ir_node *arg = get_irn_out(args, j);
996 if (get_Proj_proj(arg) == static_link_arg) {
997 for (k = get_irn_n_outs(arg) - 1; k >= 0; --k) {
998 ir_node *succ = get_irn_out(arg, k);
1001 ir_entity *entity = get_Sel_entity(succ);
1003 if (get_entity_owner(entity) == ft) {
1004 /* found an access to the outer frame */
1005 ir_entity_usage flags;
1007 flags = get_entity_usage(entity);
1008 flags |= determine_entity_usage(succ, entity);
1009 set_entity_usage(entity, flags);
1019 irg->entity_usage_state = ir_entity_usage_computed;
1022 ir_entity_usage_computed_state get_irg_entity_usage_state(const ir_graph *irg) {
1023 return irg->entity_usage_state;
1026 void set_irg_entity_usage_state(ir_graph *irg, ir_entity_usage_computed_state state) {
1027 irg->entity_usage_state = state;
1030 void assure_irg_entity_usage_computed(ir_graph *irg)
1032 if (irg->entity_usage_state != ir_entity_usage_not_computed)
1035 analyse_irg_entity_usage(irg);
1040 * Initialize the entity_usage flag for a global type like type.
1042 static void init_entity_usage(ir_type *tp)
1046 /* We have to be conservative: All external visible entities are unknown */
1047 for (i = get_compound_n_members(tp) - 1; i >= 0; --i) {
1048 ir_entity *ent = get_compound_member(tp, i);
1049 ir_entity_usage flags = ir_usage_none;
1051 if (entity_is_externally_visible(ent)) {
1052 flags |= ir_usage_unknown;
1054 set_entity_usage(ent, flags);
1059 * Mark all entities used in the initializer as unknown usage.
1061 * @param initializer the initializer to check
1063 static void check_initializer_nodes(ir_initializer_t *initializer)
1068 switch (initializer->kind) {
1069 case IR_INITIALIZER_CONST:
1070 /* let's check if it's an address */
1071 n = initializer->consti.value;
1073 ir_entity *ent = get_Global_entity(n);
1074 set_entity_usage(ent, ir_usage_unknown);
1077 case IR_INITIALIZER_TARVAL:
1078 case IR_INITIALIZER_NULL:
1080 case IR_INITIALIZER_COMPOUND:
1081 for (i = 0; i < initializer->compound.n_initializers; ++i) {
1082 ir_initializer_t *sub_initializer
1083 = initializer->compound.initializers[i];
1084 check_initializer_nodes(sub_initializer);
1088 panic("invalid initializer found");
1089 } /* check_initializer_nodes */
1092 * Mark all entities used in the initializer for the given entity as unknown
1095 * @param ent the entity
1097 static void check_initializer(ir_entity *ent)
1102 /* Beware: Methods are always initialized with "themself". This does not
1103 * count as a taken address.
1104 * TODO: this initialisation with "themself" is wrong and should be removed
1106 if (is_Method_type(get_entity_type(ent)))
1109 if (ent->initializer != NULL) {
1110 check_initializer_nodes(ent->initializer);
1111 } else if (entity_has_compound_ent_values(ent)) {
1112 for (i = get_compound_ent_n_values(ent) - 1; i >= 0; --i) {
1113 n = get_compound_ent_value(ent, i);
1115 /* let's check if it's an address */
1117 ir_entity *ent = get_Global_entity(n);
1118 set_entity_usage(ent, ir_usage_unknown);
1126 * Mark all entities used in initializers as unknown usage.
1128 * @param tp a compound type
1130 static void check_initializers(ir_type *tp) {
1133 for (i = get_compound_n_members(tp) - 1; i >= 0; --i) {
1134 ir_entity *ent = get_compound_member(tp, i);
1136 check_initializer(ent);
1138 } /* check_initializers */
1140 #ifdef DEBUG_libfirm
1142 * Print the entity usage flags of all entities of a given type for debugging.
1144 * @param tp a compound type
1146 static void print_entity_usage_flags(ir_type *tp) {
1148 for (i = get_compound_n_members(tp) - 1; i >= 0; --i) {
1149 ir_entity *ent = get_compound_member(tp, i);
1150 ir_entity_usage flags = get_entity_usage(ent);
1154 ir_printf("%+F:", ent);
1155 if (flags & ir_usage_address_taken)
1156 printf(" address_taken");
1157 if (flags & ir_usage_read)
1159 if (flags & ir_usage_write)
1161 if (flags & ir_usage_reinterpret_cast)
1162 printf(" reinterp_cast");
1166 #endif /* DEBUG_libfirm */
1169 * Post-walker: check for global entity address
1171 static void check_global_address(ir_node *irn, void *env) {
1174 ir_entity_usage flags;
1176 if (is_Global(irn)) {
1178 ent = get_Global_entity(irn);
1179 } else if (is_Sel(irn) && get_Sel_ptr(irn) == tls) {
1180 /* A TLS variable. */
1181 ent = get_Sel_entity(irn);
1185 flags = get_entity_usage(ent);
1186 flags |= determine_entity_usage(irn, ent);
1187 set_entity_usage(ent, flags);
1188 } /* check_global_address */
1191 * Update the entity usage flags of all global entities.
1193 static void analyse_irp_globals_entity_usage(void) {
1197 for (s = IR_SEGMENT_FIRST; s <= IR_SEGMENT_LAST; ++s) {
1198 ir_type *type = get_segment_type(s);
1199 init_entity_usage(type);
1202 for (s = IR_SEGMENT_FIRST; s <= IR_SEGMENT_LAST; ++s) {
1203 ir_type *type = get_segment_type(s);
1204 check_initializers(type);
1207 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
1208 ir_graph *irg = get_irp_irg(i);
1210 assure_irg_outs(irg);
1211 irg_walk_graph(irg, NULL, check_global_address, get_irg_tls(irg));
1214 #ifdef DEBUG_libfirm
1215 if (firm_dbg_get_mask(dbg) & LEVEL_1) {
1217 for (s = IR_SEGMENT_FIRST; s <= IR_SEGMENT_LAST; ++s) {
1218 print_entity_usage_flags(get_segment_type(s));
1221 #endif /* DEBUG_libfirm */
1224 irp->globals_entity_usage_state = ir_entity_usage_computed;
1227 /* Returns the current address taken state of the globals. */
1228 ir_entity_usage_computed_state get_irp_globals_entity_usage_state(void) {
1229 return irp->globals_entity_usage_state;
1232 /* Sets the current address taken state of the graph. */
1233 void set_irp_globals_entity_usage_state(ir_entity_usage_computed_state state) {
1234 irp->globals_entity_usage_state = state;
1237 /* Assure that the address taken flag is computed for the globals. */
1238 void assure_irp_globals_entity_usage_computed(void) {
1239 if (irp->globals_entity_usage_state != ir_entity_usage_not_computed)
1242 analyse_irp_globals_entity_usage();
1245 void firm_init_memory_disambiguator(void) {
1246 FIRM_DBG_REGISTER(dbg, "firm.ana.irmemory");
1247 FIRM_DBG_REGISTER(dbgcall, "firm.opt.cc");
1251 /** Maps method types to cloned method types. */
1252 static pmap *mtp_map;
1255 * Clone a method type if not already cloned.
1257 * @param tp the type to clone
1259 static ir_type *clone_type_and_cache(ir_type *tp)
1262 pmap_entry *e = pmap_find(mtp_map, tp);
1267 res = clone_type_method(tp);
1268 pmap_insert(mtp_map, tp, res);
1271 } /* clone_type_and_cache */
1274 * Walker: clone all call types of Calls to methods having the
1275 * mtp_property_private property set.
1277 static void update_calls_to_private(ir_node *call, void *env) {
1279 if (is_Call(call)) {
1280 ir_node *ptr = get_Call_ptr(call);
1282 if (is_SymConst(ptr)) {
1283 ir_entity *ent = get_SymConst_entity(ptr);
1284 ir_type *ctp = get_Call_type(call);
1286 if (get_entity_additional_properties(ent) & mtp_property_private) {
1287 if ((get_method_additional_properties(ctp) & mtp_property_private) == 0) {
1288 ctp = clone_type_and_cache(ctp);
1289 set_method_additional_property(ctp, mtp_property_private);
1290 set_Call_type(call, ctp);
1291 DB((dbgcall, LEVEL_1, "changed call to private method %+F using cloned type %+F\n", ent, ctp));
1296 } /* update_calls_to_private */
1298 /* Mark all private methods, i.e. those of which all call sites are known. */
1299 void mark_private_methods(void)
1304 assure_irp_globals_entity_usage_computed();
1306 mtp_map = pmap_create();
1308 /* first step: change the calling conventions of the local non-escaped entities */
1309 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
1310 ir_graph *irg = get_irp_irg(i);
1311 ir_entity *ent = get_irg_entity(irg);
1312 ir_entity_usage flags = get_entity_usage(ent);
1314 if (!entity_is_externally_visible(ent) &&
1315 !(flags & ir_usage_address_taken)) {
1316 ir_type *mtp = get_entity_type(ent);
1318 set_entity_additional_property(ent, mtp_property_private);
1319 DB((dbgcall, LEVEL_1, "found private method %+F\n", ent));
1320 if ((get_method_additional_properties(mtp) & mtp_property_private) == 0) {
1321 /* need a new type */
1322 mtp = clone_type_and_cache(mtp);
1323 set_method_additional_property(mtp, mtp_property_private);
1324 set_entity_type(ent, mtp);
1325 DB((dbgcall, LEVEL_2, "changed entity type of %+F to %+F\n", ent, mtp));
1332 all_irg_walk(NULL, update_calls_to_private, NULL);
1334 pmap_destroy(mtp_map);
1335 } /* mark_private_methods */
1337 /* create a pass for mark_private_methods() */
1338 ir_prog_pass_t *mark_private_methods_pass(const char *name) {
1339 return def_prog_pass(name ? name : "mark_private_methods", mark_private_methods);
1340 } /* mark_private_methods_pass */