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
35 #include "irgraph_t.h"
37 #include "irmemory_t.h"
48 /** The debug handle. */
49 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
51 /** The source language specific language disambiguator function. */
52 static DISAMBIGUATOR_FUNC language_disambuigator = NULL;
54 /** The global memory disambiguator options. */
55 static unsigned global_mem_disamgig_opt = aa_opt_no_opt;
57 /* Returns a human readable name for an alias relation. */
58 const char *get_ir_alias_relation_name(ir_alias_relation rel) {
59 #define X(a) case a: return #a
64 default: assert(0); return "UNKNOWN";
69 /* Get the memory disambiguator options for a graph. */
70 unsigned get_irg_memory_disambiguator_options(ir_graph *irg) {
71 unsigned opt = irg->mem_disambig_opt;
72 if (opt & aa_opt_inherited)
73 return global_mem_disamgig_opt;
75 } /* get_irg_memory_disambiguator_options */
77 /* Set the memory disambiguator options for a graph. */
78 void set_irg_memory_disambiguator_options(ir_graph *irg, unsigned options) {
79 irg->mem_disambig_opt = options & ~aa_opt_inherited;
80 } /* set_irg_memory_disambiguator_options */
82 /* Set the global disambiguator options for all graphs not having local options. */
83 void set_irp_memory_disambiguator_options(unsigned options) {
84 global_mem_disamgig_opt = options;
85 } /* set_irp_memory_disambiguator_options */
88 * Find the base address and entity of an Sel node.
91 * @param pEnt after return points to the base entity.
93 * @return the base address.
95 static ir_node *find_base_adr(ir_node *sel, ir_entity **pEnt) {
96 ir_node *ptr = get_Sel_ptr(sel);
100 ptr = get_Sel_ptr(sel);
102 *pEnt = get_Sel_entity(sel);
104 } /* find_base_adr */
107 * Check if a given Const node is greater or equal a given size.
109 * @param cns a Const node
110 * @param size a integer size
112 * @return ir_no_alias if the Const is greater, ir_may_alias else
114 static ir_alias_relation check_const(ir_node *cns, int size) {
115 tarval *tv = get_Const_tarval(cns);
119 return tarval_is_null(tv) ? ir_may_alias : ir_no_alias;
120 tv_size = new_tarval_from_long(size, get_tarval_mode(tv));
121 return tarval_cmp(tv_size, tv) & (pn_Cmp_Eq|pn_Cmp_Lt) ? ir_no_alias : ir_may_alias;
125 * Treat idx1 and idx2 as integer indexes and check if they differ always more than size.
127 * @param idx1 a node representing the first index
128 * @param idx2 a node representing the second index
129 * @param size an integer size
131 * @return ir_sure_alias iff idx1 == idx2
132 * ir_no_alias iff they ALWAYS differ more than size
135 static ir_alias_relation different_index(ir_node *idx1, ir_node *idx2, int size) {
137 return ir_sure_alias;
138 if (is_Const(idx1) && is_Const(idx2)) {
139 /* both are const, we can compare them */
140 tarval *tv1 = get_Const_tarval(idx1);
141 tarval *tv2 = get_Const_tarval(idx2);
142 tarval *tv, *tv_size;
146 return tv1 == tv2 ? ir_sure_alias : ir_no_alias;
148 /* arg, modes may be different */
149 m1 = get_tarval_mode(tv1);
150 m2 = get_tarval_mode(tv2);
152 int size = get_mode_size_bits(m1) - get_mode_size_bits(m2);
155 /* m1 is a small mode, cast up */
156 m1 = mode_is_signed(m1) ? find_signed_mode(m2) : find_unsigned_mode(m2);
158 /* should NOT happen, but if it does we give up here */
161 tv1 = tarval_convert_to(tv1, m1);
162 } else if (size > 0) {
163 /* m2 is a small mode, cast up */
164 m2 = mode_is_signed(m2) ? find_signed_mode(m1) : find_unsigned_mode(m1);
166 /* should NOT happen, but if it does we give up here */
169 tv2 = tarval_convert_to(tv2, m2);
171 /* here the size should be identical, check for signed */
172 if (get_mode_sign(m1) != get_mode_sign(m2)) {
173 /* find the signed */
174 if (mode_is_signed(m2)) {
181 /* m1 is now the signed one */
182 if (tarval_cmp(tv1, get_tarval_null(m1)) & (pn_Cmp_Eq|pn_Cmp_Gt)) {
183 /* tv1 is signed, but >= 0, simply cast into unsigned */
184 tv1 = tarval_convert_to(tv1, m2);
186 tv_size = new_tarval_from_long(size, m2);
188 if (tarval_cmp(tv2, tv_size) & (pn_Cmp_Eq|pn_Cmp_Gt)) {
189 /* tv1 is negative and tv2 >= tv_size, so the difference is bigger than size */
192 /* tv_size > tv2, so we can subtract without overflow */
193 tv2 = tarval_sub(tv_size, tv2, NULL);
195 /* tv1 is < 0, so we can negate it */
196 tv1 = tarval_neg(tv1);
198 /* cast it into unsigned. for two-complement it does the right thing for MIN_INT */
199 tv1 = tarval_convert_to(tv1, m2);
201 /* now we can compare without overflow */
202 return tarval_cmp(tv1, tv2) & (pn_Cmp_Eq|pn_Cmp_Gt) ? ir_no_alias : ir_may_alias;
206 if (tarval_cmp(tv1, tv2) == pn_Cmp_Gt) {
211 /* tv1 is now the "smaller" one */
212 tv = tarval_sub(tv2, tv1, NULL);
213 tv_size = new_tarval_from_long(size, get_tarval_mode(tv));
214 return tarval_cmp(tv_size, tv) & (pn_Cmp_Eq|pn_Cmp_Lt) ? ir_no_alias : ir_may_alias;
217 /* Note: we rely here on the fact that normalization puts constants on the RIGHT side */
219 ir_node *l1 = get_Add_left(idx1);
220 ir_node *r1 = get_Add_right(idx1);
225 return check_const(r1, size);
228 /* both are Adds, check if they are of x + a == x + b kind */
229 ir_node *l2 = get_Add_left(idx2);
230 ir_node *r2 = get_Add_right(idx2);
233 return different_index(r1, r2, size);
235 return different_index(r1, l2, size);
237 return different_index(l1, l2, size);
239 return different_index(l1, r2, size);
243 ir_node *l2 = get_Add_left(idx2);
244 ir_node *r2 = get_Add_right(idx2);
249 return check_const(r2, size);
254 ir_node *l1 = get_Sub_left(idx1);
255 ir_node *r1 = get_Sub_right(idx1);
260 return check_const(r1, size);
264 /* both are Subs, check if they are of x - a == x - b kind */
265 ir_node *l2 = get_Sub_left(idx2);
268 ir_node *r2 = get_Sub_right(idx2);
269 return different_index(r1, r2, size);
274 ir_node *l2 = get_Sub_left(idx2);
275 ir_node *r2 = get_Sub_right(idx2);
280 return check_const(r2, size);
285 } /* different_index */
288 * Two Sel addresses have the same base address, check if there offsets are
291 * @param adr1 The first address.
292 * @param adr2 The second address.
294 static ir_alias_relation different_sel_offsets(ir_node *sel1, ir_node *sel2) {
295 /* seems to be broken */
299 ir_entity *ent1 = get_Sel_entity(sel1);
300 ir_entity *ent2 = get_Sel_entity(sel2);
301 int i, check_arr = 0;
306 ir_type *tp1 = get_entity_type(ent1);
307 ir_type *tp2 = get_entity_type(ent2);
311 else if (get_type_state(tp1) == layout_fixed && get_type_state(tp2) == layout_fixed &&
312 get_type_size_bits(tp1) == get_type_size_bits(tp2))
316 /* we select an entity of same size, check for indexes */
317 int n = get_Sel_n_indexs(sel1);
320 if (n > 0 && n == get_Sel_n_indexs(sel2)) {
321 /* same non-zero number of indexes, an array access, check */
322 for (i = 0; i < n; ++i) {
323 ir_node *idx1 = get_Sel_index(sel1, i);
324 ir_node *idx2 = get_Sel_index(sel2, i);
325 ir_alias_relation res = different_index(idx1, idx2, 0); /* we can safely IGNORE the size here if it's at least >0 */
327 if (res == may_alias)
329 else if (res == no_alias)
332 /* if we have at least one no_alias, there is no alias relation, else we have sure */
333 return have_no > 0 ? no_alias : sure_alias;
338 } /* different_sel_offsets */
341 * Determine the alias relation by checking if adr1 and adr2 are pointer
344 * @param adr1 The first address.
345 * @param adr2 The second address.
347 static ir_alias_relation different_types(ir_node *adr1, ir_node *adr2)
349 ir_entity *ent1 = NULL, *ent2 = NULL;
352 ent1 = get_Global_entity(adr1);
353 else if (is_Sel(adr1))
354 ent1 = get_Sel_entity(adr1);
357 ent2 = get_Global_entity(adr2);
358 else if (is_Sel(adr2))
359 ent2 = get_Sel_entity(adr2);
361 if (ent1 != NULL && ent2 != NULL) {
362 ir_type *tp1 = get_entity_type(ent1);
363 ir_type *tp2 = get_entity_type(ent2);
366 /* do deref until no pointer types are found */
367 while (is_Pointer_type(tp1) && is_Pointer_type(tp2)) {
368 tp1 = get_pointer_points_to_type(tp1);
369 tp2 = get_pointer_points_to_type(tp2);
372 if (get_type_tpop(tp1) != get_type_tpop(tp2)) {
373 /* different type structure */
376 if (is_Class_type(tp1)) {
377 /* check class hierarchy */
378 if (! is_SubClass_of(tp1, tp2) &&
379 ! is_SubClass_of(tp2, tp1))
382 /* different types */
388 } /* different_types */
391 * Returns non-zero if a node is a result on a malloc-like routine.
393 * @param node the Proj node to test
395 static int is_malloc_Result(ir_node *node) {
396 node = get_Proj_pred(node);
399 node = get_Proj_pred(node);
402 node = get_Call_ptr(node);
403 if (is_Global(node)) {
404 ir_entity *ent = get_Global_entity(node);
406 if (get_entity_additional_properties(ent) & mtp_property_malloc)
411 } /* is_malloc_Result */
414 * Classify a base pointer.
416 * @param irg the graph of the pointer
417 * @param irn the node representing the base address
418 * @param ent the base entity of the base address iff any
420 ir_storage_class_class_t classify_pointer(ir_graph *irg, ir_node *irn, ir_entity *ent)
422 ir_storage_class_class_t res = ir_sc_pointer;
423 if (is_Global(irn)) {
424 ir_entity *entity = get_Global_entity(irn);
425 res = ir_sc_globalvar;
426 if (! (get_entity_usage(entity) & ir_usage_address_taken))
427 res |= ir_sc_modifier_nottaken;
428 } else if (irn == get_irg_frame(irg)) {
429 res = ir_sc_localvar;
430 if (ent != NULL && !(get_entity_usage(ent) & ir_usage_address_taken))
431 res |= ir_sc_modifier_nottaken;
432 } else if (is_arg_Proj(irn)) {
433 return ir_sc_argument;
434 } else if (irn == get_irg_tls(irg)) {
436 if (ent != NULL && !(get_entity_usage(ent) & ir_usage_address_taken))
437 res |= ir_sc_modifier_nottaken;
438 } else if (is_Proj(irn) && is_malloc_Result(irn)) {
439 return ir_sc_malloced;
446 * If adr represents a Bitfield Sel, skip it
448 static ir_node *skip_Bitfield_Sels(ir_node *adr) {
450 ir_entity *ent = get_Sel_entity(adr);
451 ir_type *bf_type = get_entity_type(ent);
453 /* is it a bitfield type? */
454 if (is_Primitive_type(bf_type) && get_primitive_base_type(bf_type) != NULL)
455 adr = get_Sel_ptr(adr);
461 * Determine the alias relation between two addresses.
463 * @param irg the graph of both memory operations
464 * @param addr1 pointer address of the first memory operation
465 * @param mode1 the mode of the accessed data through addr1
466 * @param addr2 pointer address of the second memory operation
467 * @param mode2 the mode of the accessed data through addr2
469 * @return found memory relation
471 static ir_alias_relation _get_alias_relation(
473 ir_node *adr1, ir_mode *mode1,
474 ir_node *adr2, ir_mode *mode2)
476 ir_entity *ent1, *ent2;
482 ir_node *orig_adr1 = adr1;
483 ir_node *orig_adr2 = adr2;
485 ir_storage_class_class_t class1, class2;
486 int have_const_offsets;
488 if (! get_opt_alias_analysis())
492 return ir_sure_alias;
494 options = get_irg_memory_disambiguator_options(irg);
496 /* The Armageddon switch */
497 if (options & aa_opt_no_alias)
500 /* do the addresses have constants offsets?
501 * Note: nodes are normalized to have constants at right inputs,
502 * sub X, C is normalized to add X, -C
504 have_const_offsets = 1;
505 while (is_Add(adr1)) {
506 ir_node *add_right = get_Add_right(adr1);
507 if (is_Const(add_right) && !mode_is_reference(get_irn_mode(add_right))) {
508 tarval *tv = get_Const_tarval(add_right);
509 offset1 += get_tarval_long(tv);
510 adr1 = get_Add_left(adr1);
511 } else if (mode_is_reference(get_irn_mode(add_right))) {
513 have_const_offsets = 0;
515 adr1 = get_Add_left(adr1);
516 have_const_offsets = 0;
519 while (is_Add(adr2)) {
520 ir_node *add_right = get_Add_right(adr2);
521 if (is_Const(add_right) && !mode_is_reference(get_irn_mode(add_right))) {
522 tarval *tv = get_Const_tarval(add_right);
523 offset2 += get_tarval_long(tv);
524 adr2 = get_Add_left(adr2);
525 } else if (mode_is_reference(get_irn_mode(add_right))) {
527 have_const_offsets = 0;
529 adr2 = get_Add_left(adr2);
530 have_const_offsets = 0;
534 mode_size = get_mode_size_bytes(mode1);
535 if (get_mode_size_bytes(mode2) > mode_size) {
536 mode_size = get_mode_size_bytes(mode2);
539 /* same base address -> compare offsets if possible.
540 * FIXME: type long is not sufficient for this task ...
542 if (adr1 == adr2 && have_const_offsets) {
543 if ((unsigned long)labs(offset2 - offset1) >= mode_size)
546 return ir_sure_alias;
550 * Bitfields can be constructed as Sels from its base address.
551 * As they have different entities, the disambiguator would find that they are
552 * alias free. While this is true for it's values, it is false for the addresses
553 * (strictly speaking, the Sel's are NOT the addresses of the bitfields).
554 * So, skip those bitfield selecting Sel's.
556 adr1 = skip_Bitfield_Sels(adr1);
557 adr2 = skip_Bitfield_Sels(adr2);
565 base1 = find_base_adr(adr1, &ent1);
568 base2 = find_base_adr(adr2, &ent2);
571 /* same base address -> compare Sel entities */
572 if (base1 == base2 && ent1 != NULL && ent2 != NULL) {
575 else if (have_const_offsets)
576 return different_sel_offsets(adr1, adr2);
579 class1 = classify_pointer(irg, base1, ent1);
580 class2 = classify_pointer(irg, base2, ent2);
582 if (class1 == ir_sc_pointer) {
583 if (class2 & ir_sc_modifier_nottaken) {
584 /* a pointer and an object whose objects was never taken */
587 } else if (class2 == ir_sc_pointer) {
588 if (class1 & ir_sc_modifier_nottaken) {
589 /* a pointer and an object whose objects was never taken */
592 } else if (class1 != class2) {
593 /* two objects from different memory spaces */
596 /* both classes are equal */
597 if (class1 == ir_sc_globalvar) {
598 ir_entity *entity1 = get_SymConst_entity(base1);
599 ir_entity *entity2 = get_SymConst_entity(base2);
600 if (entity1 != entity2)
603 /* for some reason CSE didn't happen yet for the 2 SymConsts... */
608 /* Type based alias analysis */
609 if (options & aa_opt_type_based) {
610 ir_alias_relation rel;
612 if (options & aa_opt_byte_type_may_alias) {
613 if (get_mode_size_bits(mode1) == 8 || get_mode_size_bits(mode2) == 8) {
614 /* One of the modes address a byte. Assume a ir_may_alias and leave
615 the type based check. */
616 goto leave_type_based_alias;
619 /* cheap check: If the mode sizes did not match, the types MUST be different */
620 if (get_mode_size_bits(mode1) != get_mode_size_bits(mode2))
623 /* cheap test: if only one is a reference mode, no alias */
624 if (mode_is_reference(mode1) != mode_is_reference(mode2))
627 /* cheap test: if arithmetic is different, no alias */
628 if (get_mode_arithmetic(mode1) != get_mode_arithmetic(mode2))
632 rel = different_types(orig_adr1, orig_adr2);
633 if (rel != ir_may_alias)
635 leave_type_based_alias:;
638 /* do we have a language specific memory disambiguator? */
639 if (language_disambuigator) {
640 ir_alias_relation rel = (*language_disambuigator)(irg, orig_adr1, mode1, orig_adr2, mode2);
641 if (rel != ir_may_alias)
645 /* access points-to information here */
647 } /* _get_alias_relation */
650 * Determine the alias relation between two addresses.
652 ir_alias_relation get_alias_relation(
654 ir_node *adr1, ir_mode *mode1,
655 ir_node *adr2, ir_mode *mode2)
657 ir_alias_relation rel = _get_alias_relation(irg, adr1, mode1, adr2, mode2);
658 DB((dbg, LEVEL_1, "alias(%+F, %+F) = %s\n", adr1, adr2, get_ir_alias_relation_name(rel)));
660 } /* get_alias_relation */
662 /* Set a source language specific memory disambiguator function. */
663 void set_language_memory_disambiguator(DISAMBIGUATOR_FUNC func) {
664 language_disambuigator = func;
665 } /* set_language_memory_disambiguator */
667 /** The result cache for the memory disambiguator. */
668 static set *result_cache = NULL;
670 /** An entry in the relation cache. */
671 typedef struct mem_disambig_entry {
672 ir_node *adr1; /**< The first address. */
673 ir_node *adr2; /**< The second address. */
674 ir_alias_relation result; /**< The alias relation result. */
675 } mem_disambig_entry;
677 #define HASH_ENTRY(adr1, adr2) (HASH_PTR(adr1) ^ HASH_PTR(adr2))
680 * Compare two relation cache entries.
682 static int cmp_mem_disambig_entry(const void *elt, const void *key, size_t size) {
683 const mem_disambig_entry *p1 = elt;
684 const mem_disambig_entry *p2 = key;
687 return p1->adr1 == p2->adr1 && p1->adr2 == p2->adr2;
688 } /* cmp_mem_disambig_entry */
691 * Initialize the relation cache.
693 void mem_disambig_init(void) {
694 result_cache = new_set(cmp_mem_disambig_entry, 8);
695 } /* mem_disambig_init */
698 * Determine the alias relation between two addresses.
700 ir_alias_relation get_alias_relation_ex(
702 ir_node *adr1, ir_mode *mode1,
703 ir_node *adr2, ir_mode *mode2)
705 mem_disambig_entry key, *entry;
707 ir_fprintf(stderr, "%+F <-> %+F\n", adr1, adr2);
709 if (! get_opt_alias_analysis())
712 if (get_irn_opcode(adr1) > get_irn_opcode(adr2)) {
720 entry = set_find(result_cache, &key, sizeof(key), HASH_ENTRY(adr1, adr2));
722 return entry->result;
724 key.result = get_alias_relation(irg, adr1, mode1, adr2, mode2);
726 set_insert(result_cache, &key, sizeof(key), HASH_ENTRY(adr1, adr2));
728 } /* get_alias_relation_ex */
730 /* Free the relation cache. */
731 void mem_disambig_term(void) {
733 del_set(result_cache);
736 } /* mem_disambig_term */
739 * Check the mode of a Load/Store with the mode of the entity
741 * If the mode of the entity and the Load/Store mode do not match, we
742 * have the bad reinterpret case:
745 * char b = *(char *)&i;
747 * We do NOT count this as one value and return address_taken
749 * However, we support an often used case. If the mode is two-complement
750 * we allow casts between signed/unsigned.
752 * @param mode the mode of the Load/Store
753 * @param ent_mode the mode of the accessed entity
755 * @return non-zero if the Load/Store is a hidden cast, zero else
757 static int is_hidden_cast(ir_mode *mode, ir_mode *ent_mode) {
758 if (ent_mode == NULL)
761 if (ent_mode != mode) {
762 if (ent_mode == NULL ||
763 get_mode_size_bits(ent_mode) != get_mode_size_bits(mode) ||
764 get_mode_sort(ent_mode) != get_mode_sort(mode) ||
765 get_mode_arithmetic(ent_mode) != irma_twos_complement ||
766 get_mode_arithmetic(mode) != irma_twos_complement)
770 } /* is_hidden_cast */
773 * Determine the usage state of a node (or its successor Sels).
775 * @param irn the node
777 static ir_entity_usage determine_entity_usage(const ir_node *irn, ir_entity *entity) {
779 ir_mode *emode, *mode;
782 ir_entity_usage res = 0;
784 for (i = get_irn_n_outs(irn) - 1; i >= 0; --i) {
785 ir_node *succ = get_irn_out(irn, i);
787 switch (get_irn_opcode(succ)) {
789 assert(irn == get_Load_ptr(succ));
790 res |= ir_usage_read;
792 /* check if this load is not a hidden conversion */
793 mode = get_Load_mode(succ);
794 emode = get_type_mode(get_entity_type(entity));
795 if (is_hidden_cast(mode, emode))
796 res |= ir_usage_reinterpret_cast;
800 /* check that the node is not the Store's value */
801 if (irn == get_Store_value(succ)) {
802 res |= ir_usage_unknown;
804 if (irn == get_Store_ptr(succ)) {
805 res |= ir_usage_write;
807 /* check if this Store is not a hidden conversion */
808 value = get_Store_value(succ);
809 mode = get_irn_mode(value);
810 emode = get_type_mode(get_entity_type(entity));
811 if (is_hidden_cast(mode, emode))
812 res |= ir_usage_reinterpret_cast;
814 assert(irn != get_Store_mem(succ));
818 /* CopyB are like Loads/Stores */
819 tp = get_entity_type(entity);
820 if (tp != get_CopyB_type(succ)) {
821 /* bad, different types, might be a hidden conversion */
822 res |= ir_usage_reinterpret_cast;
824 if (irn == get_CopyB_dst(succ)) {
825 res |= ir_usage_write;
827 assert(irn == get_CopyB_src(succ));
828 res |= ir_usage_read;
835 /* Check the successor of irn. */
836 res |= determine_entity_usage(succ, entity);
841 if (irn == get_Call_ptr(succ)) {
842 /* TODO: we could check for reinterpret casts here...
843 * But I doubt anyone is interested in that bit for
844 * function entities and I'm too lazy to write the code now.
846 res |= ir_usage_read;
848 assert(irn != get_Call_mem(succ));
849 res |= ir_usage_unknown;
855 /* TODO implement marker algo */
859 /* another op, we don't know anything */
860 res |= ir_usage_unknown;
869 * Update the usage flags of all frame entities.
871 static void analyse_irg_entity_usage(ir_graph *irg) {
872 ir_type *ft = get_irg_frame_type(irg);
876 /* set initial state to not_taken, as this is the "smallest" state */
877 for (i = get_class_n_members(ft) - 1; i >= 0; --i) {
878 ir_entity *ent = get_class_member(ft, i);
879 ir_entity_usage flags =
880 get_entity_stickyness(ent) == stickyness_sticky ? ir_usage_unknown : 0;
882 set_entity_usage(ent, flags);
885 assure_irg_outs(irg);
887 irg_frame = get_irg_frame(irg);
889 for (i = get_irn_n_outs(irg_frame) - 1; i >= 0; --i) {
890 ir_node *succ = get_irn_out(irg_frame, i);
892 ir_entity_usage flags;
897 entity = get_Sel_entity(succ);
898 flags = get_entity_usage(entity);
899 flags |= determine_entity_usage(succ, entity);
900 set_entity_usage(entity, flags);
904 irg->entity_usage_state = ir_entity_usage_computed;
907 ir_entity_usage_computed_state get_irg_entity_usage_state(const ir_graph *irg) {
908 return irg->entity_usage_state;
911 void set_irg_entity_usage_state(ir_graph *irg, ir_entity_usage_computed_state state) {
912 irg->entity_usage_state = state;
915 void assure_irg_entity_usage_computed(ir_graph *irg) {
916 if (irg->entity_usage_state != ir_entity_usage_not_computed)
919 analyse_irg_entity_usage(irg);
924 * Initialize the entity_usage flag for a global type like type.
926 static void init_entity_usage(ir_type * tp) {
929 /* We have to be conservative: All external visible entities are unknown */
930 for (i = get_compound_n_members(tp) - 1; i >= 0; --i) {
931 ir_entity *entity = get_compound_member(tp, i);
932 ir_entity_usage flags = 0;
934 if (get_entity_visibility(entity) == visibility_external_visible ||
935 get_entity_visibility(entity) == visibility_external_allocated ||
936 get_entity_stickyness(entity) == stickyness_sticky) {
937 flags |= ir_usage_unknown;
940 set_entity_usage(entity, flags);
944 static void check_initializer_nodes(ir_initializer_t *initializer)
946 switch (initializer->kind) {
947 case IR_INITIALIZER_CONST: {
948 ir_node *n = initializer->consti.value;
950 /* let's check if it's an address */
952 ir_entity *ent = get_Global_entity(n);
953 set_entity_usage(ent, ir_usage_unknown);
957 case IR_INITIALIZER_TARVAL:
958 case IR_INITIALIZER_NULL:
960 case IR_INITIALIZER_COMPOUND: {
963 for (i = 0; i < initializer->compound.n_initializers; ++i) {
964 ir_initializer_t *sub_initializer
965 = initializer->compound.initializers[i];
966 check_initializer_nodes(sub_initializer);
971 panic("invalid initializer found");
972 } /* check_initializer_nodes */
975 * Mark all entities used in the initializer for the given entity as address taken.
977 * @param ent the entity
979 static void check_initializer(ir_entity *ent) {
983 /* do not check uninitialized values */
984 if (get_entity_variability(ent) == variability_uninitialized)
987 /* Beware: Methods are always initialized with "themself". This does not
988 count as a taken address. */
989 if (is_Method_type(get_entity_type(ent)))
992 if (ent->has_initializer) {
993 check_initializer_nodes(ent->attr.initializer);
994 } else if (is_atomic_entity(ent)) {
995 /* let's check if it's an address */
996 n = get_atomic_ent_value(ent);
998 ir_entity *ent = get_Global_entity(n);
999 set_entity_usage(ent, ir_usage_unknown);
1002 for (i = get_compound_ent_n_values(ent) - 1; i >= 0; --i) {
1003 n = get_compound_ent_value(ent, i);
1005 /* let's check if it's an address */
1007 ir_entity *ent = get_Global_entity(n);
1008 set_entity_usage(ent, ir_usage_unknown);
1012 } /* check_initializer */
1016 * Mark all entities used in initializers as address taken.
1018 * @param tp a compound type
1020 static void check_initializers(ir_type *tp) {
1023 for (i = get_compound_n_members(tp) - 1; i >= 0; --i) {
1024 ir_entity *ent = get_compound_member(tp, i);
1026 check_initializer(ent);
1028 } /* check_initializers */
1030 #ifdef DEBUG_libfirm
1032 * Print the entity usage flags of all entities of a given type for debugging.
1034 * @param tp a compound type
1036 static void print_entity_usage_flags(ir_type *tp) {
1038 for (i = get_compound_n_members(tp) - 1; i >= 0; --i) {
1039 ir_entity *ent = get_compound_member(tp, i);
1040 ir_entity_usage flags = get_entity_usage(ent);
1044 ir_printf("%+F:", ent);
1045 if (flags & ir_usage_address_taken)
1046 printf(" address_taken");
1047 if (flags & ir_usage_read)
1049 if (flags & ir_usage_write)
1051 if (flags & ir_usage_reinterpret_cast)
1052 printf(" reinterp_cast");
1056 #endif /* DEBUG_libfirm */
1059 * Post-walker: check for global entity address
1061 static void check_global_address(ir_node *irn, void *env) {
1064 ir_entity_usage flags;
1066 if (is_Global(irn)) {
1068 ent = get_Global_entity(irn);
1069 } else if (is_Sel(irn) && get_Sel_ptr(irn) == tls) {
1070 /* A TLS variable. */
1071 ent = get_Sel_entity(irn);
1075 flags = get_entity_usage(ent);
1076 flags |= determine_entity_usage(irn, ent);
1077 set_entity_usage(ent, flags);
1078 } /* check_global_address */
1081 * Update the entity usage flags of all global entities.
1083 static void analyse_irp_globals_entity_usage(void) {
1087 for (s = IR_SEGMENT_FIRST; s < IR_SEGMENT_COUNT; ++s) {
1088 ir_type *type = get_segment_type(s);
1089 init_entity_usage(type);
1092 for (s = IR_SEGMENT_FIRST; s < IR_SEGMENT_COUNT; ++s) {
1093 ir_type *type = get_segment_type(s);
1094 check_initializers(type);
1097 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
1098 ir_graph *irg = get_irp_irg(i);
1100 assure_irg_outs(irg);
1101 irg_walk_graph(irg, NULL, check_global_address, get_irg_tls(irg));
1104 #ifdef DEBUG_libfirm
1105 if (firm_dbg_get_mask(dbg) & LEVEL_1) {
1107 for (s = IR_SEGMENT_FIRST; s < IR_SEGMENT_COUNT; ++s) {
1108 print_entity_usage_flags(get_segment_type(s));
1111 #endif /* DEBUG_libfirm */
1114 irp->globals_entity_usage_state = ir_entity_usage_computed;
1117 /* Returns the current address taken state of the globals. */
1118 ir_entity_usage_computed_state get_irp_globals_entity_usage_state(void) {
1119 return irp->globals_entity_usage_state;
1122 /* Sets the current address taken state of the graph. */
1123 void set_irp_globals_entity_usage_state(ir_entity_usage_computed_state state) {
1124 irp->globals_entity_usage_state = state;
1127 /* Assure that the address taken flag is computed for the globals. */
1128 void assure_irp_globals_entity_usage_computed(void) {
1129 if (irp->globals_entity_usage_state != ir_entity_usage_not_computed)
1132 analyse_irp_globals_entity_usage();
1135 void firm_init_memory_disambiguator(void) {
1136 FIRM_DBG_REGISTER(dbg, "firm.ana.irmemory");
1140 #include <adt/pmap.h>
1141 #include "typerep.h"
1143 DEBUG_ONLY(static firm_dbg_module_t *dbgcall = NULL;)
1145 /** Maps method types to cloned method types. */
1146 static pmap *mtp_map;
1149 * Clone a method type if not already cloned.
1151 * @param tp the type to clone
1153 static ir_type *clone_type_and_cache(ir_type *tp) {
1154 static ident *prefix = NULL;
1156 pmap_entry *e = pmap_find(mtp_map, tp);
1162 prefix = new_id_from_chars("C", 1);
1164 res = clone_type_method(tp, prefix);
1165 pmap_insert(mtp_map, tp, res);
1166 DB((dbgcall, LEVEL_2, "cloned type %+F into %+F\n", tp, res));
1169 } /* clone_type_and_cache */
1172 * Walker: clone all call types of Calls to methods having the
1173 * mtp_property_private property set.
1175 static void update_calls_to_private(ir_node *call, void *env) {
1177 if (is_Call(call)) {
1178 ir_node *ptr = get_Call_ptr(call);
1180 if (is_SymConst(ptr)) {
1181 ir_entity *ent = get_SymConst_entity(ptr);
1182 ir_type *ctp = get_Call_type(call);
1184 if (get_entity_additional_properties(ent) & mtp_property_private) {
1185 if ((get_method_additional_properties(ctp) & mtp_property_private) == 0) {
1186 ctp = clone_type_and_cache(ctp);
1187 set_method_additional_property(ctp, mtp_property_private);
1188 set_Call_type(call, ctp);
1189 DB((dbgcall, LEVEL_1, "changed call to private method %+F\n", ent));
1194 } /* update_calls_to_private */
1196 /* Mark all private methods, i.e. those of which all call sites are known. */
1197 void mark_private_methods(void) {
1201 FIRM_DBG_REGISTER(dbgcall, "firm.opt.cc");
1203 assure_irp_globals_entity_usage_computed();
1205 mtp_map = pmap_create();
1207 /* first step: change the calling conventions of the local non-escaped entities */
1208 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
1209 ir_graph *irg = get_irp_irg(i);
1210 ir_entity *ent = get_irg_entity(irg);
1211 ir_entity_usage flags = get_entity_usage(ent);
1213 /* If an entity is sticky, it might be called from external
1214 places (like inline assembler), so do NOT mark it as private. */
1215 if (get_entity_visibility(ent) == visibility_local &&
1216 !(flags & ir_usage_address_taken) &&
1217 get_entity_stickyness(ent) != stickyness_sticky) {
1218 ir_type *mtp = get_entity_type(ent);
1220 set_entity_additional_property(ent, mtp_property_private);
1221 DB((dbgcall, LEVEL_1, "found private method %+F\n", ent));
1222 if ((get_method_additional_properties(mtp) & mtp_property_private) == 0) {
1223 /* need a new type */
1224 mtp = clone_type_and_cache(mtp);
1225 set_entity_type(ent, mtp);
1226 set_method_additional_property(mtp, mtp_property_private);
1233 all_irg_walk(NULL, update_calls_to_private, NULL);
1235 pmap_destroy(mtp_map);
1236 } /* mark_private_methods */