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"
48 /** The debug handle. */
49 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
50 DEBUG_ONLY(static firm_dbg_module_t *dbgcall = NULL;)
52 /** The source language specific language disambiguator function. */
53 static DISAMBIGUATOR_FUNC language_disambuigator = NULL;
55 /** The global memory disambiguator options. */
56 static unsigned global_mem_disamgig_opt = aa_opt_no_opt;
58 /* Returns a human readable name for an alias relation. */
59 const char *get_ir_alias_relation_name(ir_alias_relation rel) {
60 #define X(a) case a: return #a
65 default: assert(0); return "UNKNOWN";
70 /* Get the memory disambiguator options for a graph. */
71 unsigned get_irg_memory_disambiguator_options(const ir_graph *irg) {
72 unsigned opt = irg->mem_disambig_opt;
73 if (opt & aa_opt_inherited)
74 return global_mem_disamgig_opt;
76 } /* get_irg_memory_disambiguator_options */
78 /* Set the memory disambiguator options for a graph. */
79 void set_irg_memory_disambiguator_options(ir_graph *irg, unsigned options) {
80 irg->mem_disambig_opt = options & ~aa_opt_inherited;
81 } /* set_irg_memory_disambiguator_options */
83 /* Set the global disambiguator options for all graphs not having local options. */
84 void set_irp_memory_disambiguator_options(unsigned options) {
85 global_mem_disamgig_opt = options;
86 } /* set_irp_memory_disambiguator_options */
89 * Find the base address and entity of an Sel node.
92 * @param pEnt after return points to the base entity.
94 * @return the base address.
96 static ir_node *find_base_adr(const ir_node *sel, ir_entity **pEnt) {
97 ir_node *ptr = get_Sel_ptr(sel);
101 ptr = get_Sel_ptr(sel);
103 *pEnt = get_Sel_entity(sel);
105 } /* find_base_adr */
108 * Check if a given Const node is greater or equal a given size.
110 * @param cns a Const node
111 * @param size a integer size
113 * @return ir_no_alias if the Const is greater, ir_may_alias else
115 static ir_alias_relation check_const(const ir_node *cns, int size) {
116 tarval *tv = get_Const_tarval(cns);
120 return tarval_is_null(tv) ? ir_may_alias : ir_no_alias;
121 tv_size = new_tarval_from_long(size, get_tarval_mode(tv));
122 return tarval_cmp(tv_size, tv) & (pn_Cmp_Eq|pn_Cmp_Lt) ? ir_no_alias : ir_may_alias;
126 * Treat idx1 and idx2 as integer indexes and check if they differ always more than size.
128 * @param idx1 a node representing the first index
129 * @param idx2 a node representing the second index
130 * @param size an integer size
132 * @return ir_sure_alias iff idx1 == idx2
133 * ir_no_alias iff they ALWAYS differ more than size
136 static ir_alias_relation different_index(const ir_node *idx1, const ir_node *idx2, int size) {
138 return ir_sure_alias;
139 if (is_Const(idx1) && is_Const(idx2)) {
140 /* both are const, we can compare them */
141 tarval *tv1 = get_Const_tarval(idx1);
142 tarval *tv2 = get_Const_tarval(idx2);
143 tarval *tv, *tv_size;
147 return tv1 == tv2 ? ir_sure_alias : ir_no_alias;
149 /* arg, modes may be different */
150 m1 = get_tarval_mode(tv1);
151 m2 = get_tarval_mode(tv2);
153 int size = get_mode_size_bits(m1) - get_mode_size_bits(m2);
156 /* m1 is a small mode, cast up */
157 m1 = mode_is_signed(m1) ? find_signed_mode(m2) : find_unsigned_mode(m2);
159 /* should NOT happen, but if it does we give up here */
162 tv1 = tarval_convert_to(tv1, m1);
163 } else if (size > 0) {
164 /* m2 is a small mode, cast up */
165 m2 = mode_is_signed(m2) ? find_signed_mode(m1) : find_unsigned_mode(m1);
167 /* should NOT happen, but if it does we give up here */
170 tv2 = tarval_convert_to(tv2, m2);
172 /* here the size should be identical, check for signed */
173 if (get_mode_sign(m1) != get_mode_sign(m2)) {
174 /* find the signed */
175 if (mode_is_signed(m2)) {
182 /* m1 is now the signed one */
183 if (tarval_cmp(tv1, get_tarval_null(m1)) & (pn_Cmp_Eq|pn_Cmp_Gt)) {
184 /* tv1 is signed, but >= 0, simply cast into unsigned */
185 tv1 = tarval_convert_to(tv1, m2);
187 tv_size = new_tarval_from_long(size, m2);
189 if (tarval_cmp(tv2, tv_size) & (pn_Cmp_Eq|pn_Cmp_Gt)) {
190 /* tv1 is negative and tv2 >= tv_size, so the difference is bigger than size */
193 /* tv_size > tv2, so we can subtract without overflow */
194 tv2 = tarval_sub(tv_size, tv2, NULL);
196 /* tv1 is < 0, so we can negate it */
197 tv1 = tarval_neg(tv1);
199 /* cast it into unsigned. for two-complement it does the right thing for MIN_INT */
200 tv1 = tarval_convert_to(tv1, m2);
202 /* now we can compare without overflow */
203 return tarval_cmp(tv1, tv2) & (pn_Cmp_Eq|pn_Cmp_Gt) ? ir_no_alias : ir_may_alias;
207 if (tarval_cmp(tv1, tv2) == pn_Cmp_Gt) {
212 /* tv1 is now the "smaller" one */
213 tv = tarval_sub(tv2, tv1, NULL);
214 tv_size = new_tarval_from_long(size, get_tarval_mode(tv));
215 return tarval_cmp(tv_size, tv) & (pn_Cmp_Eq|pn_Cmp_Lt) ? ir_no_alias : ir_may_alias;
218 /* Note: we rely here on the fact that normalization puts constants on the RIGHT side */
220 ir_node *l1 = get_Add_left(idx1);
221 ir_node *r1 = get_Add_right(idx1);
226 return check_const(r1, size);
229 /* both are Adds, check if they are of x + a == x + b kind */
230 ir_node *l2 = get_Add_left(idx2);
231 ir_node *r2 = get_Add_right(idx2);
234 return different_index(r1, r2, size);
236 return different_index(r1, l2, size);
238 return different_index(l1, l2, size);
240 return different_index(l1, r2, size);
244 ir_node *l2 = get_Add_left(idx2);
245 ir_node *r2 = get_Add_right(idx2);
250 return check_const(r2, size);
255 ir_node *l1 = get_Sub_left(idx1);
256 ir_node *r1 = get_Sub_right(idx1);
261 return check_const(r1, size);
265 /* both are Subs, check if they are of x - a == x - b kind */
266 ir_node *l2 = get_Sub_left(idx2);
269 ir_node *r2 = get_Sub_right(idx2);
270 return different_index(r1, r2, size);
275 ir_node *l2 = get_Sub_left(idx2);
276 ir_node *r2 = get_Sub_right(idx2);
281 return check_const(r2, size);
286 } /* different_index */
289 * Two Sel addresses have the same base address, check if there offsets are
292 * @param adr1 The first address.
293 * @param adr2 The second address.
295 static ir_alias_relation different_sel_offsets(const ir_node *sel1, const ir_node *sel2) {
296 /* seems to be broken */
300 ir_entity *ent1 = get_Sel_entity(sel1);
301 ir_entity *ent2 = get_Sel_entity(sel2);
302 int i, check_arr = 0;
307 ir_type *tp1 = get_entity_type(ent1);
308 ir_type *tp2 = get_entity_type(ent2);
312 else if (get_type_state(tp1) == layout_fixed && get_type_state(tp2) == layout_fixed &&
313 get_type_size_bits(tp1) == get_type_size_bits(tp2))
317 /* we select an entity of same size, check for indexes */
318 int n = get_Sel_n_indexs(sel1);
321 if (n > 0 && n == get_Sel_n_indexs(sel2)) {
322 /* same non-zero number of indexes, an array access, check */
323 for (i = 0; i < n; ++i) {
324 ir_node *idx1 = get_Sel_index(sel1, i);
325 ir_node *idx2 = get_Sel_index(sel2, i);
326 ir_alias_relation res = different_index(idx1, idx2, 0); /* we can safely IGNORE the size here if it's at least >0 */
328 if (res == may_alias)
330 else if (res == no_alias)
333 /* if we have at least one no_alias, there is no alias relation, else we have sure */
334 return have_no > 0 ? no_alias : sure_alias;
338 (void) different_index;
341 } /* different_sel_offsets */
344 * Determine the alias relation by checking if adr1 and adr2 are pointer
347 * @param adr1 The first address.
348 * @param adr2 The second address.
350 static ir_alias_relation different_types(const ir_node *adr1, const ir_node *adr2)
352 ir_entity *ent1 = NULL, *ent2 = NULL;
355 ent1 = get_Global_entity(adr1);
356 else if (is_Sel(adr1))
357 ent1 = get_Sel_entity(adr1);
360 ent2 = get_Global_entity(adr2);
361 else if (is_Sel(adr2))
362 ent2 = get_Sel_entity(adr2);
364 if (ent1 != NULL && ent2 != NULL) {
365 ir_type *tp1 = get_entity_type(ent1);
366 ir_type *tp2 = get_entity_type(ent2);
369 /* do deref until no pointer types are found */
370 while (is_Pointer_type(tp1) && is_Pointer_type(tp2)) {
371 tp1 = get_pointer_points_to_type(tp1);
372 tp2 = get_pointer_points_to_type(tp2);
375 if (get_type_tpop(tp1) != get_type_tpop(tp2)) {
376 /* different type structure */
379 if (is_Class_type(tp1)) {
380 /* check class hierarchy */
381 if (! is_SubClass_of(tp1, tp2) &&
382 ! is_SubClass_of(tp2, tp1))
385 /* different types */
391 } /* different_types */
394 * Returns non-zero if a node is a result on a malloc-like routine.
396 * @param node the Proj node to test
398 static int is_malloc_Result(const ir_node *node) {
399 node = get_Proj_pred(node);
402 node = get_Proj_pred(node);
405 node = get_Call_ptr(node);
406 if (is_Global(node)) {
407 ir_entity *ent = get_Global_entity(node);
409 if (get_entity_additional_properties(ent) & mtp_property_malloc)
414 } /* is_malloc_Result */
417 * Classify a base pointer.
419 * @param irg the graph of the pointer
420 * @param irn the node representing the base address
421 * @param ent the base entity of the base address iff any
423 ir_storage_class_class_t classify_pointer(const ir_graph *irg, const ir_node *irn, const ir_entity *ent)
425 ir_storage_class_class_t res = ir_sc_pointer;
426 if (is_Global(irn)) {
427 ir_entity *entity = get_Global_entity(irn);
428 res = ir_sc_globalvar;
429 if (! (get_entity_usage(entity) & ir_usage_address_taken))
430 res |= ir_sc_modifier_nottaken;
431 } else if (irn == get_irg_frame(irg)) {
432 res = ir_sc_localvar;
433 if (ent != NULL && !(get_entity_usage(ent) & ir_usage_address_taken))
434 res |= ir_sc_modifier_nottaken;
435 } else if (irn == get_irg_tls(irg)) {
437 if (ent != NULL && !(get_entity_usage(ent) & ir_usage_address_taken))
438 res |= ir_sc_modifier_nottaken;
439 } else if (is_Proj(irn) && is_malloc_Result(irn)) {
440 return ir_sc_malloced;
441 } else if (is_Const(irn)) {
442 return ir_sc_globaladdr;
443 } else if (is_arg_Proj(irn)) {
444 res |= ir_sc_modifier_argument;
451 * If adr represents a Bitfield Sel, skip it
453 static const ir_node *skip_Bitfield_Sels(const ir_node *adr) {
455 ir_entity *ent = get_Sel_entity(adr);
456 ir_type *bf_type = get_entity_type(ent);
458 /* is it a bitfield type? */
459 if (is_Primitive_type(bf_type) && get_primitive_base_type(bf_type) != NULL)
460 adr = get_Sel_ptr(adr);
466 * Determine the alias relation between two addresses.
468 * @param irg the graph of both memory operations
469 * @param addr1 pointer address of the first memory operation
470 * @param mode1 the mode of the accessed data through addr1
471 * @param addr2 pointer address of the second memory operation
472 * @param mode2 the mode of the accessed data through addr2
474 * @return found memory relation
476 static ir_alias_relation _get_alias_relation(
478 const ir_node *adr1, const ir_mode *mode1,
479 const ir_node *adr2, const ir_mode *mode2)
481 ir_entity *ent1, *ent2;
485 const ir_node *base1;
486 const ir_node *base2;
487 const ir_node *orig_adr1 = adr1;
488 const ir_node *orig_adr2 = adr2;
490 ir_storage_class_class_t class1, class2, mod1, mod2;
491 int have_const_offsets;
493 if (! get_opt_alias_analysis())
497 return ir_sure_alias;
499 options = get_irg_memory_disambiguator_options(irg);
501 /* The Armageddon switch */
502 if (options & aa_opt_no_alias)
505 /* do the addresses have constants offsets?
506 * Note: nodes are normalized to have constants at right inputs,
507 * sub X, C is normalized to add X, -C
509 have_const_offsets = 1;
510 while (is_Add(adr1)) {
511 ir_node *add_right = get_Add_right(adr1);
512 if (is_Const(add_right) && !mode_is_reference(get_irn_mode(add_right))) {
513 tarval *tv = get_Const_tarval(add_right);
514 offset1 += get_tarval_long(tv);
515 adr1 = get_Add_left(adr1);
516 } else if (mode_is_reference(get_irn_mode(add_right))) {
518 have_const_offsets = 0;
520 adr1 = get_Add_left(adr1);
521 have_const_offsets = 0;
524 while (is_Add(adr2)) {
525 ir_node *add_right = get_Add_right(adr2);
526 if (is_Const(add_right) && !mode_is_reference(get_irn_mode(add_right))) {
527 tarval *tv = get_Const_tarval(add_right);
528 offset2 += get_tarval_long(tv);
529 adr2 = get_Add_left(adr2);
530 } else if (mode_is_reference(get_irn_mode(add_right))) {
532 have_const_offsets = 0;
534 adr2 = get_Add_left(adr2);
535 have_const_offsets = 0;
539 mode_size = get_mode_size_bytes(mode1);
540 if (get_mode_size_bytes(mode2) > mode_size) {
541 mode_size = get_mode_size_bytes(mode2);
544 /* same base address -> compare offsets if possible.
545 * FIXME: type long is not sufficient for this task ...
547 if (adr1 == adr2 && have_const_offsets) {
548 if ((unsigned long)labs(offset2 - offset1) >= mode_size)
551 return ir_sure_alias;
555 * Bitfields can be constructed as Sels from its base address.
556 * As they have different entities, the disambiguator would find that they are
557 * alias free. While this is true for it's values, it is false for the addresses
558 * (strictly speaking, the Sel's are NOT the addresses of the bitfields).
559 * So, skip those bitfield selecting Sel's.
561 adr1 = skip_Bitfield_Sels(adr1);
562 adr2 = skip_Bitfield_Sels(adr2);
570 base1 = find_base_adr(adr1, &ent1);
573 base2 = find_base_adr(adr2, &ent2);
576 /* same base address -> compare Sel entities */
577 if (base1 == base2 && ent1 != NULL && ent2 != NULL) {
580 else if (have_const_offsets)
581 return different_sel_offsets(adr1, adr2);
584 mod1 = classify_pointer(irg, base1, ent1);
585 mod2 = classify_pointer(irg, base2, ent2);
587 class1 = GET_BASE_SC(mod1);
588 class2 = GET_BASE_SC(mod2);
590 if (class1 == ir_sc_pointer || class2 == ir_sc_pointer) {
591 /* swap pointer class to class1 */
592 if (class2 == ir_sc_pointer) {
593 ir_storage_class_class_t temp = mod1;
596 class1 = GET_BASE_SC(mod1);
597 class2 = GET_BASE_SC(mod2);
599 /* a pointer and an object whose address was never taken */
600 if (mod2 & ir_sc_modifier_nottaken) {
603 if (mod1 & ir_sc_modifier_argument) {
604 if ( (options & aa_opt_no_alias_args)
605 && (mod2 & ir_sc_modifier_argument))
607 if ( (options & aa_opt_no_alias_args_global)
608 && (class2 == ir_sc_globalvar
609 || class2 == ir_sc_tls
610 || class2 == ir_sc_globaladdr))
613 } else if (class1 != class2) {
614 /* two objects from different memory spaces */
617 /* both classes are equal */
618 if (class1 == ir_sc_globalvar) {
619 ir_entity *entity1 = get_SymConst_entity(base1);
620 ir_entity *entity2 = get_SymConst_entity(base2);
621 if (entity1 != entity2)
624 /* for some reason CSE didn't happen yet for the 2 SymConsts... */
626 } else if (class1 == ir_sc_globaladdr) {
627 tarval *tv = get_Const_tarval(base1);
628 offset1 += get_tarval_long(tv);
629 tv = get_Const_tarval(base2);
630 offset2 += get_tarval_long(tv);
632 if ((unsigned long)labs(offset2 - offset1) >= mode_size)
635 return ir_sure_alias;
639 /* Type based alias analysis */
640 if (options & aa_opt_type_based) {
641 ir_alias_relation rel;
643 if (options & aa_opt_byte_type_may_alias) {
644 if (get_mode_size_bits(mode1) == 8 || get_mode_size_bits(mode2) == 8) {
645 /* One of the modes address a byte. Assume a ir_may_alias and leave
646 the type based check. */
647 goto leave_type_based_alias;
650 /* cheap check: If the mode sizes did not match, the types MUST be different */
651 if (get_mode_size_bits(mode1) != get_mode_size_bits(mode2))
654 /* cheap test: if only one is a reference mode, no alias */
655 if (mode_is_reference(mode1) != mode_is_reference(mode2))
658 /* cheap test: if arithmetic is different, no alias */
659 if (get_mode_arithmetic(mode1) != get_mode_arithmetic(mode2))
663 rel = different_types(orig_adr1, orig_adr2);
664 if (rel != ir_may_alias)
666 leave_type_based_alias:;
669 /* do we have a language specific memory disambiguator? */
670 if (language_disambuigator != NULL) {
671 ir_alias_relation rel = language_disambuigator(irg, orig_adr1, mode1, orig_adr2, mode2);
672 if (rel != ir_may_alias)
676 /* access points-to information here */
678 } /* _get_alias_relation */
681 * Determine the alias relation between two addresses.
683 ir_alias_relation get_alias_relation(
685 const ir_node *adr1, const ir_mode *mode1,
686 const ir_node *adr2, const ir_mode *mode2)
688 ir_alias_relation rel = _get_alias_relation(irg, adr1, mode1, adr2, mode2);
689 DB((dbg, LEVEL_1, "alias(%+F, %+F) = %s\n", adr1, adr2, get_ir_alias_relation_name(rel)));
691 } /* get_alias_relation */
693 /* Set a source language specific memory disambiguator function. */
694 void set_language_memory_disambiguator(DISAMBIGUATOR_FUNC func) {
695 language_disambuigator = func;
696 } /* set_language_memory_disambiguator */
698 /** The result cache for the memory disambiguator. */
699 static set *result_cache = NULL;
701 /** An entry in the relation cache. */
702 typedef struct mem_disambig_entry {
703 const ir_node *adr1; /**< The first address. */
704 const ir_mode *mode1; /**< The first address mode. */
705 const ir_node *adr2; /**< The second address. */
706 const ir_mode *mode2; /**< The second address mode. */
707 ir_alias_relation result; /**< The alias relation result. */
708 } mem_disambig_entry;
710 #define HASH_ENTRY(adr1, adr2) (HASH_PTR(adr1) ^ HASH_PTR(adr2))
713 * Compare two relation cache entries.
715 static int cmp_mem_disambig_entry(const void *elt, const void *key, size_t size) {
716 const mem_disambig_entry *p1 = elt;
717 const mem_disambig_entry *p2 = key;
720 return p1->adr1 == p2->adr1 && p1->adr2 == p2->adr2 &&
721 p1->mode1 == p2->mode1 && p1->mode2 == p2->mode2;
722 } /* cmp_mem_disambig_entry */
725 * Initialize the relation cache.
727 void mem_disambig_init(void) {
728 result_cache = new_set(cmp_mem_disambig_entry, 8);
729 } /* mem_disambig_init */
732 * Determine the alias relation between two addresses.
734 ir_alias_relation get_alias_relation_ex(
736 const ir_node *adr1, const ir_mode *mode1,
737 const ir_node *adr2, const ir_mode *mode2)
739 mem_disambig_entry key, *entry;
741 ir_fprintf(stderr, "%+F <-> %+F\n", adr1, adr2);
743 if (! get_opt_alias_analysis())
746 if (get_irn_opcode(adr1) > get_irn_opcode(adr2)) {
747 const ir_node *t = adr1;
756 entry = set_find(result_cache, &key, sizeof(key), HASH_ENTRY(adr1, adr2));
758 return entry->result;
760 key.result = get_alias_relation(irg, adr1, mode1, adr2, mode2);
762 set_insert(result_cache, &key, sizeof(key), HASH_ENTRY(adr1, adr2));
764 } /* get_alias_relation_ex */
766 /* Free the relation cache. */
767 void mem_disambig_term(void) {
768 if (result_cache != NULL) {
769 del_set(result_cache);
772 } /* mem_disambig_term */
775 * Check the mode of a Load/Store with the mode of the entity
777 * If the mode of the entity and the Load/Store mode do not match, we
778 * have the bad reinterpret case:
781 * char b = *(char *)&i;
783 * We do NOT count this as one value and return address_taken
785 * However, we support an often used case. If the mode is two-complement
786 * we allow casts between signed/unsigned.
788 * @param mode the mode of the Load/Store
789 * @param ent_mode the mode of the accessed entity
791 * @return non-zero if the Load/Store is a hidden cast, zero else
793 static int is_hidden_cast(const ir_mode *mode, const ir_mode *ent_mode) {
794 if (ent_mode == NULL)
797 if (ent_mode != mode) {
798 if (ent_mode == NULL ||
799 get_mode_size_bits(ent_mode) != get_mode_size_bits(mode) ||
800 get_mode_sort(ent_mode) != get_mode_sort(mode) ||
801 get_mode_arithmetic(ent_mode) != irma_twos_complement ||
802 get_mode_arithmetic(mode) != irma_twos_complement)
806 } /* is_hidden_cast */
809 * Determine the usage state of a node (or its successor Sels).
811 * @param irn the node
813 static ir_entity_usage determine_entity_usage(const ir_node *irn, ir_entity *entity) {
815 ir_mode *emode, *mode;
818 ir_entity_usage res = 0;
820 for (i = get_irn_n_outs(irn) - 1; i >= 0; --i) {
821 ir_node *succ = get_irn_out(irn, i);
823 switch (get_irn_opcode(succ)) {
825 /* beware: irn might be a Id node here, so irn might be not
826 equal to get_Load_ptr(succ) */
827 res |= ir_usage_read;
829 /* check if this load is not a hidden conversion */
830 mode = get_Load_mode(succ);
831 emode = get_type_mode(get_entity_type(entity));
832 if (is_hidden_cast(mode, emode))
833 res |= ir_usage_reinterpret_cast;
837 /* check that the node is not the Store's value */
838 if (irn == get_Store_value(succ)) {
839 res |= ir_usage_unknown;
841 if (irn == get_Store_ptr(succ)) {
842 res |= ir_usage_write;
844 /* check if this Store is not a hidden conversion */
845 value = get_Store_value(succ);
846 mode = get_irn_mode(value);
847 emode = get_type_mode(get_entity_type(entity));
848 if (is_hidden_cast(mode, emode))
849 res |= ir_usage_reinterpret_cast;
851 assert(irn != get_Store_mem(succ));
855 /* CopyB are like Loads/Stores */
856 tp = get_entity_type(entity);
857 if (tp != get_CopyB_type(succ)) {
858 /* bad, different types, might be a hidden conversion */
859 res |= ir_usage_reinterpret_cast;
861 if (irn == get_CopyB_dst(succ)) {
862 res |= ir_usage_write;
864 assert(irn == get_CopyB_src(succ));
865 res |= ir_usage_read;
871 /* Check the successor of irn. */
872 res |= determine_entity_usage(succ, entity);
875 ir_entity *entity = get_Sel_entity(succ);
876 /* this analysis can't handle unions correctly */
877 if (is_Union_type(get_entity_owner(entity))) {
878 res |= ir_usage_unknown;
881 /* Check the successor of irn. */
882 res |= determine_entity_usage(succ, entity);
887 if (irn == get_Call_ptr(succ)) {
888 /* TODO: we could check for reinterpret casts here...
889 * But I doubt anyone is interested in that bit for
890 * function entities and I'm too lazy to write the code now.
892 res |= ir_usage_read;
894 assert(irn != get_Call_mem(succ));
895 res |= ir_usage_unknown;
899 /* skip identities */
901 res |= determine_entity_usage(succ, entity);
907 for (input_nr = get_Tuple_n_preds(succ) - 1; input_nr >= 0;
909 ir_node *pred = get_Tuple_pred(succ, input_nr);
912 /* we found one input */
913 for (k = get_irn_n_outs(succ) - 1; k >= 0; --k) {
914 ir_node *proj = get_irn_out(succ, k);
916 if (is_Proj(proj) && get_Proj_proj(proj) == input_nr) {
917 res |= determine_entity_usage(proj, entity);
927 /* another op, we don't know anything (we could do more advanced
928 * things like a dataflow analysis here) */
929 res |= ir_usage_unknown;
938 * Update the usage flags of all frame entities.
940 static void analyse_irg_entity_usage(ir_graph *irg) {
941 ir_type *ft = get_irg_frame_type(irg);
943 int i, j, k, static_link_arg;
945 /* set initial state to not_taken, as this is the "smallest" state */
946 for (i = get_class_n_members(ft) - 1; i >= 0; --i) {
947 ir_entity *ent = get_class_member(ft, i);
949 /* methods can only be analyzed globally */
950 if (! is_method_entity(ent)) {
951 ir_entity_usage flags =
952 get_entity_stickyness(ent) == stickyness_sticky ? ir_usage_unknown : 0;
953 set_entity_usage(ent, flags);
957 assure_irg_outs(irg);
959 irg_frame = get_irg_frame(irg);
961 for (i = get_irn_n_outs(irg_frame) - 1; i >= 0; --i) {
962 ir_node *succ = get_irn_out(irg_frame, i);
964 ir_entity_usage flags;
969 entity = get_Sel_entity(succ);
970 flags = get_entity_usage(entity);
971 flags |= determine_entity_usage(succ, entity);
972 set_entity_usage(entity, flags);
975 /* check inner functions accessing outer frame */
977 for (i = get_class_n_members(ft) - 1; i >= 0; --i) {
978 ir_entity *ent = get_class_member(ft, i);
982 if (! is_method_entity(ent))
984 if (get_entity_peculiarity(ent) == peculiarity_description)
987 inner_irg = get_entity_irg(ent);
988 assure_irg_outs(inner_irg);
989 args = get_irg_args(inner_irg);
990 for (j = get_irn_n_outs(args) - 1; j >= 0; --j) {
991 ir_node *arg = get_irn_out(args, j);
993 if (get_Proj_proj(arg) == static_link_arg) {
994 for (k = get_irn_n_outs(arg) - 1; k >= 0; --k) {
995 ir_node *succ = get_irn_out(arg, k);
998 ir_entity *entity = get_Sel_entity(succ);
1000 if (get_entity_owner(entity) == ft) {
1001 /* found an access to the outer frame */
1002 ir_entity_usage flags;
1004 flags = get_entity_usage(entity);
1005 flags |= determine_entity_usage(succ, entity);
1006 set_entity_usage(entity, flags);
1016 irg->entity_usage_state = ir_entity_usage_computed;
1019 ir_entity_usage_computed_state get_irg_entity_usage_state(const ir_graph *irg) {
1020 return irg->entity_usage_state;
1023 void set_irg_entity_usage_state(ir_graph *irg, ir_entity_usage_computed_state state) {
1024 irg->entity_usage_state = state;
1027 void assure_irg_entity_usage_computed(ir_graph *irg) {
1028 if (irg->entity_usage_state != ir_entity_usage_not_computed)
1031 analyse_irg_entity_usage(irg);
1036 * Initialize the entity_usage flag for a global type like type.
1038 static void init_entity_usage(ir_type *tp) {
1041 /* We have to be conservative: All external visible entities are unknown */
1042 for (i = get_compound_n_members(tp) - 1; i >= 0; --i) {
1043 ir_entity *ent = get_compound_member(tp, i);
1044 ir_entity_usage flags = ir_usage_none;
1045 ir_visibility vis = get_entity_visibility(ent);
1047 if (vis == visibility_external_visible ||
1048 vis == visibility_external_allocated ||
1049 get_entity_stickyness(ent) == stickyness_sticky) {
1050 flags |= ir_usage_unknown;
1052 set_entity_usage(ent, flags);
1057 * Mark all entities used in the initializer as unknown usage.
1059 * @param initializer the initializer to check
1061 static void check_initializer_nodes(ir_initializer_t *initializer)
1066 switch (initializer->kind) {
1067 case IR_INITIALIZER_CONST:
1068 /* let's check if it's an address */
1069 n = initializer->consti.value;
1071 ir_entity *ent = get_Global_entity(n);
1072 set_entity_usage(ent, ir_usage_unknown);
1075 case IR_INITIALIZER_TARVAL:
1076 case IR_INITIALIZER_NULL:
1078 case IR_INITIALIZER_COMPOUND:
1079 for (i = 0; i < initializer->compound.n_initializers; ++i) {
1080 ir_initializer_t *sub_initializer
1081 = initializer->compound.initializers[i];
1082 check_initializer_nodes(sub_initializer);
1086 panic("invalid initializer found");
1087 } /* check_initializer_nodes */
1090 * Mark all entities used in the initializer for the given entity as unknown
1093 * @param ent the entity
1095 static void check_initializer(ir_entity *ent) {
1099 /* do not check uninitialized values */
1100 if (get_entity_variability(ent) == variability_uninitialized)
1103 /* Beware: Methods are always initialized with "themself". This does not
1104 count as a taken address. */
1105 if (is_Method_type(get_entity_type(ent)))
1108 if (ent->has_initializer) {
1109 check_initializer_nodes(ent->attr.initializer);
1110 } else if (is_atomic_entity(ent)) {
1111 /* let's check if it's an address */
1112 n = get_atomic_ent_value(ent);
1114 ir_entity *ent = get_Global_entity(n);
1115 set_entity_usage(ent, ir_usage_unknown);
1118 for (i = get_compound_ent_n_values(ent) - 1; i >= 0; --i) {
1119 n = get_compound_ent_value(ent, i);
1121 /* let's check if it's an address */
1123 ir_entity *ent = get_Global_entity(n);
1124 set_entity_usage(ent, ir_usage_unknown);
1128 } /* check_initializer */
1132 * Mark all entities used in initializers as unknown usage.
1134 * @param tp a compound type
1136 static void check_initializers(ir_type *tp) {
1139 for (i = get_compound_n_members(tp) - 1; i >= 0; --i) {
1140 ir_entity *ent = get_compound_member(tp, i);
1142 check_initializer(ent);
1144 } /* check_initializers */
1146 #ifdef DEBUG_libfirm
1148 * Print the entity usage flags of all entities of a given type for debugging.
1150 * @param tp a compound type
1152 static void print_entity_usage_flags(ir_type *tp) {
1154 for (i = get_compound_n_members(tp) - 1; i >= 0; --i) {
1155 ir_entity *ent = get_compound_member(tp, i);
1156 ir_entity_usage flags = get_entity_usage(ent);
1160 ir_printf("%+F:", ent);
1161 if (flags & ir_usage_address_taken)
1162 printf(" address_taken");
1163 if (flags & ir_usage_read)
1165 if (flags & ir_usage_write)
1167 if (flags & ir_usage_reinterpret_cast)
1168 printf(" reinterp_cast");
1172 #endif /* DEBUG_libfirm */
1175 * Post-walker: check for global entity address
1177 static void check_global_address(ir_node *irn, void *env) {
1180 ir_entity_usage flags;
1182 if (is_Global(irn)) {
1184 ent = get_Global_entity(irn);
1185 } else if (is_Sel(irn) && get_Sel_ptr(irn) == tls) {
1186 /* A TLS variable. */
1187 ent = get_Sel_entity(irn);
1191 flags = get_entity_usage(ent);
1192 flags |= determine_entity_usage(irn, ent);
1193 set_entity_usage(ent, flags);
1194 } /* check_global_address */
1197 * Update the entity usage flags of all global entities.
1199 static void analyse_irp_globals_entity_usage(void) {
1203 for (s = IR_SEGMENT_FIRST; s < IR_SEGMENT_COUNT; ++s) {
1204 ir_type *type = get_segment_type(s);
1205 init_entity_usage(type);
1208 for (s = IR_SEGMENT_FIRST; s < IR_SEGMENT_COUNT; ++s) {
1209 ir_type *type = get_segment_type(s);
1210 check_initializers(type);
1213 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
1214 ir_graph *irg = get_irp_irg(i);
1216 assure_irg_outs(irg);
1217 irg_walk_graph(irg, NULL, check_global_address, get_irg_tls(irg));
1220 #ifdef DEBUG_libfirm
1221 if (firm_dbg_get_mask(dbg) & LEVEL_1) {
1223 for (s = IR_SEGMENT_FIRST; s < IR_SEGMENT_COUNT; ++s) {
1224 print_entity_usage_flags(get_segment_type(s));
1227 #endif /* DEBUG_libfirm */
1230 irp->globals_entity_usage_state = ir_entity_usage_computed;
1233 /* Returns the current address taken state of the globals. */
1234 ir_entity_usage_computed_state get_irp_globals_entity_usage_state(void) {
1235 return irp->globals_entity_usage_state;
1238 /* Sets the current address taken state of the graph. */
1239 void set_irp_globals_entity_usage_state(ir_entity_usage_computed_state state) {
1240 irp->globals_entity_usage_state = state;
1243 /* Assure that the address taken flag is computed for the globals. */
1244 void assure_irp_globals_entity_usage_computed(void) {
1245 if (irp->globals_entity_usage_state != ir_entity_usage_not_computed)
1248 analyse_irp_globals_entity_usage();
1251 void firm_init_memory_disambiguator(void) {
1252 FIRM_DBG_REGISTER(dbg, "firm.ana.irmemory");
1253 FIRM_DBG_REGISTER(dbgcall, "firm.opt.cc");
1257 /** Maps method types to cloned method types. */
1258 static pmap *mtp_map;
1261 * Clone a method type if not already cloned.
1263 * @param tp the type to clone
1265 static ir_type *clone_type_and_cache(ir_type *tp) {
1266 static ident *prefix = NULL;
1268 pmap_entry *e = pmap_find(mtp_map, tp);
1274 prefix = new_id_from_chars("C", 1);
1276 res = clone_type_method(tp, prefix);
1277 pmap_insert(mtp_map, tp, res);
1278 DB((dbgcall, LEVEL_2, "cloned type %+F into %+F\n", tp, res));
1281 } /* clone_type_and_cache */
1284 * Walker: clone all call types of Calls to methods having the
1285 * mtp_property_private property set.
1287 static void update_calls_to_private(ir_node *call, void *env) {
1289 if (is_Call(call)) {
1290 ir_node *ptr = get_Call_ptr(call);
1292 if (is_SymConst(ptr)) {
1293 ir_entity *ent = get_SymConst_entity(ptr);
1294 ir_type *ctp = get_Call_type(call);
1296 if (get_entity_additional_properties(ent) & mtp_property_private) {
1297 if ((get_method_additional_properties(ctp) & mtp_property_private) == 0) {
1298 ctp = clone_type_and_cache(ctp);
1299 set_method_additional_property(ctp, mtp_property_private);
1300 set_Call_type(call, ctp);
1301 DB((dbgcall, LEVEL_1, "changed call to private method %+F\n", ent));
1306 } /* update_calls_to_private */
1308 /* Mark all private methods, i.e. those of which all call sites are known. */
1309 void mark_private_methods(void) {
1313 assure_irp_globals_entity_usage_computed();
1315 mtp_map = pmap_create();
1317 /* first step: change the calling conventions of the local non-escaped entities */
1318 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
1319 ir_graph *irg = get_irp_irg(i);
1320 ir_entity *ent = get_irg_entity(irg);
1321 ir_entity_usage flags = get_entity_usage(ent);
1323 /* If an entity is sticky, it might be called from external
1324 places (like inline assembler), so do NOT mark it as private. */
1325 if (get_entity_visibility(ent) == visibility_local &&
1326 !(flags & ir_usage_address_taken) &&
1327 get_entity_stickyness(ent) != stickyness_sticky) {
1328 ir_type *mtp = get_entity_type(ent);
1330 set_entity_additional_property(ent, mtp_property_private);
1331 DB((dbgcall, LEVEL_1, "found private method %+F\n", ent));
1332 if ((get_method_additional_properties(mtp) & mtp_property_private) == 0) {
1333 /* need a new type */
1334 mtp = clone_type_and_cache(mtp);
1335 set_entity_type(ent, mtp);
1336 set_method_additional_property(mtp, mtp_property_private);
1343 all_irg_walk(NULL, update_calls_to_private, NULL);
1345 pmap_destroy(mtp_map);
1346 } /* mark_private_methods */