2 * Copyright (C) 1995-2007 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"
45 /** The debug handle. */
46 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
48 /** The source language specific language disambiguator function. */
49 static DISAMBIGUATOR_FUNC language_disambuigator = NULL;
51 /** The global memory disambiguator options. */
52 static unsigned global_mem_disamgig_opt = aa_opt_no_opt;
54 /* Returns a human readable name for an alias relation. */
55 const char *get_ir_alias_relation_name(ir_alias_relation rel) {
56 #define X(a) case a: return #a
61 default: assert(0); return "UNKNOWN";
66 /* Get the memory disambiguator options for a graph. */
67 unsigned get_irg_memory_disambiguator_options(ir_graph *irg) {
68 unsigned opt = irg->mem_disambig_opt;
69 if (opt & aa_opt_inherited)
70 return global_mem_disamgig_opt;
72 } /* get_irg_memory_disambiguator_options */
74 /* Set the memory disambiguator options for a graph. */
75 void set_irg_memory_disambiguator_options(ir_graph *irg, unsigned options) {
76 irg->mem_disambig_opt = options & ~aa_opt_inherited;
77 } /* set_irg_memory_disambiguator_options */
79 /* Set the global disambiguator options for all graphs not having local options. */
80 void set_irp_memory_disambiguator_options(unsigned options) {
81 global_mem_disamgig_opt = options;
82 } /* set_irp_memory_disambiguator_options */
85 * Find the base address and entity of an Sel node.
88 * @param pEnt after return points to the base entity.
90 * @return the base address.
92 static ir_node *find_base_adr(ir_node *sel, ir_entity **pEnt) {
93 ir_node *ptr = get_Sel_ptr(sel);
97 ptr = get_Sel_ptr(sel);
99 *pEnt = get_Sel_entity(sel);
101 } /* find_base_adr */
104 * Check if a given Const node is greater or equal a given size.
106 * @return no_alias if the Const is greater, may_alias else
108 static ir_alias_relation check_const(ir_node *cns, int size) {
109 tarval *tv = get_Const_tarval(cns);
113 return tarval_is_null(tv) ? may_alias : no_alias;
114 tv_size = new_tarval_from_long(size, get_tarval_mode(tv));
115 return tarval_cmp(tv_size, tv) & (pn_Cmp_Eq|pn_Cmp_Lt) ? no_alias : may_alias;
119 * Treat idx1 and idx2 as integer indexes and check if they differ always more than size.
121 * @return sure_alias iff idx1 == idx2
122 * no_alias iff they ALWAYS differ more than size
125 static ir_alias_relation different_index(ir_node *idx1, ir_node *idx2, int size) {
128 if (is_Const(idx1) && is_Const(idx2)) {
129 /* both are const, we can compare them */
130 tarval *tv1 = get_Const_tarval(idx1);
131 tarval *tv2 = get_Const_tarval(idx2);
132 tarval *tv, *tv_size;
136 return tv1 == tv2 ? sure_alias : no_alias;
138 /* arg, modes may be different */
139 m1 = get_tarval_mode(tv1);
140 m2 = get_tarval_mode(tv2);
142 int size = get_mode_size_bits(m1) - get_mode_size_bits(m2);
145 /* m1 is a small mode, cast up */
146 m1 = mode_is_signed(m1) ? find_signed_mode(m2) : find_unsigned_mode(m2);
148 /* should NOT happen, but if it does we give up here */
151 tv1 = tarval_convert_to(tv1, m1);
152 } else if (size > 0) {
153 /* m2 is a small mode, cast up */
154 m2 = mode_is_signed(m2) ? find_signed_mode(m1) : find_unsigned_mode(m1);
156 /* should NOT happen, but if it does we give up here */
159 tv2 = tarval_convert_to(tv2, m2);
161 /* here the size should be identical, check for signed */
162 if (get_mode_sign(m1) != get_mode_sign(m2)) {
163 /* find the signed */
164 if (mode_is_signed(m2)) {
171 /* m1 is now the signed one */
172 if (tarval_cmp(tv1, get_tarval_null(m1)) & (pn_Cmp_Eq|pn_Cmp_Gt)) {
173 /* tv1 is signed, but >= 0, simply cast into unsigned */
174 tv1 = tarval_convert_to(tv1, m2);
176 tv_size = new_tarval_from_long(size, m2);
178 if (tarval_cmp(tv2, tv_size) & (pn_Cmp_Eq|pn_Cmp_Gt)) {
179 /* tv1 is negative and tv2 >= tv_size, so the difference is bigger than size */
182 /* tv_size > tv2, so we can subtract without overflow */
183 tv2 = tarval_sub(tv_size, tv2);
185 /* tv1 is < 0, so we can negate it */
186 tv1 = tarval_neg(tv1);
188 /* cast it into unsigned. for two-complement it does the right thing for MIN_INT */
189 tv1 = tarval_convert_to(tv1, m2);
191 /* now we can compare without overflow */
192 return tarval_cmp(tv1, tv2) & (pn_Cmp_Eq|pn_Cmp_Gt) ? no_alias : may_alias;
196 if (tarval_cmp(tv1, tv2) == pn_Cmp_Gt) {
201 /* tv1 is now the "smaller" one */
202 tv = tarval_sub(tv2, tv1);
203 tv_size = new_tarval_from_long(size, get_tarval_mode(tv));
204 return tarval_cmp(tv_size, tv) & (pn_Cmp_Eq|pn_Cmp_Lt) ? no_alias : may_alias;
207 /* Note: we rely here on the fact that normalization puts constants on the RIGHT side */
209 ir_node *l1 = get_Add_left(idx1);
210 ir_node *r1 = get_Add_right(idx1);
215 return check_const(r1, size);
218 /* both are Adds, check if they are of x + a == x + b kind */
219 ir_node *l2 = get_Add_left(idx2);
220 ir_node *r2 = get_Add_right(idx2);
223 return different_index(r1, r2, size);
225 return different_index(r1, l2, size);
227 return different_index(l1, l2, size);
229 return different_index(l1, r2, size);
233 ir_node *l2 = get_Add_left(idx2);
234 ir_node *r2 = get_Add_right(idx2);
239 return check_const(r2, size);
244 ir_node *l1 = get_Sub_left(idx1);
245 ir_node *r1 = get_Sub_right(idx1);
250 return check_const(r1, size);
254 /* both are Subs, check if they are of x - a == x - b kind */
255 ir_node *l2 = get_Sub_left(idx2);
258 ir_node *r2 = get_Sub_right(idx2);
259 return different_index(r1, r2, size);
264 ir_node *l2 = get_Sub_left(idx2);
265 ir_node *r2 = get_Sub_right(idx2);
270 return check_const(r2, size);
275 } /* different_index */
278 * Two Sel addresses have the same base address, check if there offsets are different.
280 * @param adr1 The first address.
281 * @param adr2 The second address.
283 static ir_alias_relation different_sel_offsets(ir_node *sel1, ir_node *sel2) {
284 /* seems to be broken */
286 ir_entity *ent1 = get_Sel_entity(sel1);
287 ir_entity *ent2 = get_Sel_entity(sel2);
288 int i, check_arr = 0;
293 ir_type *tp1 = get_entity_type(ent1);
294 ir_type *tp2 = get_entity_type(ent2);
298 else if (get_type_state(tp1) == layout_fixed && get_type_state(tp2) == layout_fixed &&
299 get_type_size_bits(tp1) == get_type_size_bits(tp2))
303 /* we select an entity of same size, check for indexes */
304 int n = get_Sel_n_indexs(sel1);
307 if (n > 0 && n == get_Sel_n_indexs(sel2)) {
308 /* same non-zero number of indexes, an array access, check */
309 for (i = 0; i < n; ++i) {
310 ir_node *idx1 = get_Sel_index(sel1, i);
311 ir_node *idx2 = get_Sel_index(sel2, i);
312 ir_alias_relation res = different_index(idx1, idx2, 0); /* we can safely IGNORE the size here if it's at least >0 */
314 if (res == may_alias)
316 else if (res == no_alias)
319 /* if we have at least one no_alias, there is no alias relation, else we have sure */
320 return have_no > 0 ? no_alias : sure_alias;
325 } /* different_sel_offsets */
328 * Determine the alias relation by checking if adr1 and adr2 are pointer
331 * @param adr1 The first address.
332 * @param adr2 The second address.
334 static ir_alias_relation different_types(ir_node *adr1, ir_node *adr2)
336 ir_entity *ent1 = NULL, *ent2 = NULL;
338 if (is_SymConst(adr1) && get_SymConst_kind(adr1) == symconst_addr_ent)
339 ent1 = get_SymConst_entity(adr1);
340 else if (is_Sel(adr1))
341 ent1 = get_Sel_entity(adr1);
343 if (is_SymConst(adr2) && get_SymConst_kind(adr2) == symconst_addr_ent)
344 ent2 = get_SymConst_entity(adr2);
345 else if (is_Sel(adr2))
346 ent2 = get_Sel_entity(adr2);
348 if (ent1 != NULL && ent2 != NULL) {
349 ir_type *tp1 = get_entity_type(ent1);
350 ir_type *tp2 = get_entity_type(ent2);
353 if (is_Pointer_type(tp1) && is_Pointer_type(tp2)) {
354 /* do deref until no pointer types are found */
356 tp1 = get_pointer_points_to_type(tp1);
357 tp2 = get_pointer_points_to_type(tp2);
358 } while (is_Pointer_type(tp1) && is_Pointer_type(tp2));
361 if (get_type_tpop(tp1) != get_type_tpop(tp2)) {
362 /* different type structure */
365 if (is_Class_type(tp1)) {
366 /* check class hierarchy */
367 if (! is_SubClass_of(tp1, tp2) &&
368 ! is_SubClass_of(tp2, tp1))
371 /* different types */
377 } /* different_types */
380 * Check if an offset is a constant and these constant is bigger or equal
383 static int check_const_offset(ir_node *offset, int size) {
384 ir_mode *mode = get_irn_mode(offset);
386 /* ok, we found an offset, check for constant */
387 if (is_Const(offset) && mode_is_int(mode)) {
388 tarval *tv = new_tarval_from_long(size, mode);
390 /* size <= offset ? */
391 if (tarval_cmp(tv, get_Const_tarval(offset)) & (pn_Cmp_Eq|pn_Cmp_Lt))
395 } /* check_const_offset */
398 * Check if we can determine that the two pointers always have an offset bigger then size
400 static ir_alias_relation _different_pointer(ir_node *adr1, ir_node *adr2, int size) {
404 /* first address is the result of a pointer addition */
405 ir_node *l1 = get_Add_left(adr1);
406 ir_node *r1 = get_Add_right(adr1);
409 found = check_const_offset(r1, size);
410 } else if (r1 == adr2) {
411 found = check_const_offset(l1, size);
412 } else if (is_Add(adr2)) {
413 /* second address is the result of a pointer addition */
414 ir_node *l2 = get_Add_left(adr2);
415 ir_node *r2 = get_Add_right(adr2);
418 return _different_pointer(r1, r2, size);
419 } else if (l1 == r2) {
420 return _different_pointer(r1, l2, size);
421 } else if (r1 == l2) {
422 return _different_pointer(l1, r2, size);
423 } else if (r1 == r2) {
424 return _different_pointer(l1, l2, size);
427 } else if (is_Add(adr2)) {
428 /* second address is the result of a pointer addition */
429 ir_node *l2 = get_Add_left(adr2);
430 ir_node *r2 = get_Add_right(adr2);
433 found = check_const_offset(r2, size);
434 } else if (r2 == adr1) {
435 found = check_const_offset(l2, size);
438 return different_index(adr1, adr2, size);
440 return found ? no_alias : may_alias;
441 } /* _different_pointer */
444 * Check if we can determine that the two pointers always have an offset bigger then the maximum size of mode1, mode2
446 static ir_alias_relation different_pointer(ir_node *adr1, ir_mode *mode1, ir_node *adr2, ir_mode *mode2) {
447 int size = get_mode_size_bytes(mode1);
448 int n = get_mode_size_bytes(mode2);
452 return _different_pointer(adr1, adr2, size);
453 } /* different_pointer */
456 * Returns non-zero if a node is a routine parameter.
458 * @param node the node to test
460 static int is_arg_Proj(ir_node *node) {
463 node = get_Proj_pred(node);
466 return pn_Start_T_args == get_Proj_proj(node) && is_Start(get_Proj_pred(node));
470 * Returns true if an address represents a global variable.
472 static INLINE int is_global_var(ir_node *irn) {
473 return is_SymConst(irn) && get_SymConst_kind(irn) == symconst_addr_ent;
474 } /* is_global_var */
477 * Determine the alias relation between two addresses.
479 static ir_alias_relation _get_alias_relation(
481 ir_node *adr1, ir_mode *mode1,
482 ir_node *adr2, ir_mode *mode2)
485 ir_entity *ent1, *ent2;
488 if (! get_opt_alias_analysis())
494 options = get_irg_memory_disambiguator_options(irg);
496 /* The Armageddon switch */
497 if (options & aa_opt_no_alias)
500 /* Two save some code, sort the addresses by its id's. Beware, this
501 might break some things, so better check here. */
502 assert(iro_SymConst < iro_Sel && iro_Sel < iro_Proj && "Code dependence broken");
503 op1 = get_irn_opcode(adr1);
504 op2 = get_irn_opcode(adr2);
515 if (is_global_var(adr1)) {
516 /* first address is a global variable */
518 if (is_global_var(adr2)) {
519 /* both addresses are global variables and we know
520 they are different (R1 a) */
521 if (get_SymConst_entity(adr1) != get_SymConst_entity(adr2))
524 /* equal entity addresses */
529 ir_node *base2 = find_base_adr(adr2, &ent2);
531 if (is_global_var(base2)) {
532 /* base2 address is a global var (R1 a) */
535 } else if (base2 == get_irg_frame(irg)) {
536 /* the second one is a local variable so they are always
539 } else if (base2 == get_irg_tls(irg)) {
540 /* the second one is a TLS variable so they are always
546 /* Here we are: the first is a global var, the second some pointer. */
547 ent1 = get_SymConst_entity(adr1);
548 if (get_entity_address_taken(ent1) == ir_address_not_taken) {
549 /* The address of the global variable was never taken, so
550 the pointer cannot match (R2). */
553 } else if (is_Sel(adr1)) {
554 /* the first address is a Sel */
555 ir_node *base1 = find_base_adr(adr1, &ent1);
557 if (base1 == get_irg_frame(irg)) {
558 /* first is a local variable ent1 */
560 /* the second address is a Sel */
561 ir_node *base2 = find_base_adr(adr2, &ent2);
563 if (base1 == base2) {
564 /* identical bases: both are local variables */
566 /* both addresses are local variables and we know
567 they are different (R1 a) */
571 return different_sel_offsets(adr1, adr2);
573 } else if (base2 == get_irg_tls(irg)) {
574 /* the second one is a TLS variable so they are always
577 } else if (is_arg_Proj(base2)) {
578 /* the second one is an offset from a parameter so they are
579 always different (R1 e) */
582 } else if (is_arg_Proj(adr2)) {
583 /* a local variable and a parameter are always different (R1 e) */
586 } else if (base1 == get_irg_tls(irg)) {
587 /* the first is a TLS variable */
589 /* the second address is a Sel */
590 ir_node *base2 = find_base_adr(adr2, &ent2);
594 /* both addresses are tls variables and we know
595 they are different (R1 a) */
598 return different_sel_offsets(adr1, adr2);
600 else if (base2 == get_irg_frame(irg)) {
601 /* the first one is a tls variable, the second a local one,
602 they are different (R1 d) */
606 } else if (is_arg_Proj(base1)) {
607 /* the first one is an offset from a parameter */
609 /* the second address is a Sel */
610 ir_node *base2 = find_base_adr(adr2, &ent2);
612 if (base2 == get_irg_frame(irg)) {
613 /* the second one is a local variable so they are always
618 } else if (is_global_var(base1)) {
619 /* the first one is a global variable */
620 ent1 = get_SymConst_entity(base1);
622 /* the second address is a Sel */
623 ir_node *base2 = find_base_adr(adr2, &ent2);
625 if (base1 == base2) {
626 /* same global var */
627 return different_sel_offsets(adr1, adr2);
629 else if (base2 == get_irg_frame(irg)) {
630 /* the second one is a local variable so they are always
633 } else if (base2 == get_irg_tls(irg)) {
634 /* the second one is a TLS variable so they are always
637 } else if (is_arg_Proj(base2)) {
638 if (get_entity_address_taken(ent1) == ir_address_not_taken) {
639 /* The address of the global variable was never taken, so
640 the pointer cannot match (R2). */
643 } else if (is_global_var(base2)) {
644 ent2 = get_SymConst_entity(base2);
645 /* both addresses are global variables and we know
646 they are different (R1 a) */
653 /* some pointers, check if they have the same base buf constant offset */
654 ir_alias_relation rel = different_pointer(adr1, mode1, adr2, mode2);
655 if (rel != may_alias)
660 if (options & aa_opt_type_based) { /* Type based alias analysis */
661 ir_alias_relation rel;
663 if (options & aa_opt_byte_type_may_alias) {
664 if (get_mode_size_bits(mode1) == 8 || get_mode_size_bits(mode2) == 8) {
665 /* One of the modes address a byte. Assume a may_alias and leave
666 the type based check. */
667 goto leave_type_based_alias;
670 /* cheap check: If the mode sizes did not match, the types MUST be different */
671 if (get_mode_size_bits(mode1) != get_mode_size_bits(mode2))
674 /* cheap test: if only one is a reference mode, no alias */
675 if (mode_is_reference(mode1) != mode_is_reference(mode2))
679 rel = different_types(adr1, adr2);
680 if (rel != may_alias)
682 leave_type_based_alias:;
685 /* do we have a language specific memory disambiguator? */
686 if (language_disambuigator) {
687 ir_alias_relation rel = (*language_disambuigator)(irg, adr1, mode1, adr2, mode2);
688 if (rel != may_alias)
692 /* access points-to information here */
694 } /* _get_alias_relation */
697 * Determine the alias relation between two addresses.
699 ir_alias_relation get_alias_relation(
701 ir_node *adr1, ir_mode *mode1,
702 ir_node *adr2, ir_mode *mode2)
704 ir_alias_relation rel = _get_alias_relation(irg, adr1, mode1, adr2, mode2);
705 DB((dbg, LEVEL_1, "alias(%+F, %+f) = %s\n", adr1, adr2, get_ir_alias_relation_name(rel)));
707 } /* get_alias_relation */
709 /* Set a source language specific memory disambiguator function. */
710 void set_language_memory_disambiguator(DISAMBIGUATOR_FUNC func) {
711 language_disambuigator = func;
712 } /* set_language_memory_disambiguator */
714 /** The result cache for the memory disambiguator. */
715 static set *result_cache = NULL;
717 /** An entry in the relation cache. */
718 typedef struct mem_disambig_entry {
719 ir_node *adr1; /**< The first address. */
720 ir_node *adr2; /**< The second address. */
721 ir_alias_relation result; /**< The alias relation result. */
722 } mem_disambig_entry;
724 #define HASH_ENTRY(adr1, adr2) (HASH_PTR(adr1) ^ HASH_PTR(adr2))
727 * Compare two relation cache entries.
729 static int cmp_mem_disambig_entry(const void *elt, const void *key, size_t size) {
730 const mem_disambig_entry *p1 = elt;
731 const mem_disambig_entry *p2 = key;
734 return p1->adr1 == p2->adr1 && p1->adr2 == p2->adr2;
735 } /* cmp_mem_disambig_entry */
738 * Initialize the relation cache.
740 void mem_disambig_init(void) {
741 result_cache = new_set(cmp_mem_disambig_entry, 8);
742 } /* mem_disambig_init */
745 * Determine the alias relation between two addresses.
747 ir_alias_relation get_alias_relation_ex(
749 ir_node *adr1, ir_mode *mode1,
750 ir_node *adr2, ir_mode *mode2)
752 mem_disambig_entry key, *entry;
754 if (! get_opt_alias_analysis())
757 if (get_irn_opcode(adr1) > get_irn_opcode(adr2)) {
765 entry = set_find(result_cache, &key, sizeof(key), HASH_ENTRY(adr1, adr2));
767 return entry->result;
769 key.result = get_alias_relation(irg, adr1, mode1, adr2, mode2);
771 set_insert(result_cache, &key, sizeof(key), HASH_ENTRY(adr1, adr2));
773 } /* get_alias_relation_ex */
775 /* Free the relation cache. */
776 void mem_disambig_term(void) {
778 del_set(result_cache);
781 } /* mem_disambig_term */
784 * Check the mode of a Load/Store with the mode of the entity
786 * If the mode of the entity and the Load/Store mode do not match, we
787 * have the bad reinterpret case:
790 * char b = *(char *)&i;
792 * We do NOT count this as one value and return address_taken
794 * However, we support an often used case. If the mode is two-complement
795 * we allow casts between signed/unsigned.
797 * @param mode the mode of the Load/Store
798 * @param ent_mode the mode of the accessed entity
800 * @return non-zero if the Load/Store is a hidden cast, zero else
802 static int is_hidden_cast(ir_mode *mode, ir_mode *ent_mode) {
803 if (ent_mode != mode) {
804 if (ent_mode == NULL ||
805 get_mode_size_bits(ent_mode) != get_mode_size_bits(mode) ||
806 get_mode_sort(ent_mode) != get_mode_sort(mode) ||
807 get_mode_arithmetic(ent_mode) != irma_twos_complement ||
808 get_mode_arithmetic(mode) != irma_twos_complement)
812 } /* is_hidden_cast */
815 * Determine the address_taken state of a node (or it's successor Sels).
817 * @param irn the node
819 static ir_address_taken_state find_address_taken_state(ir_node *irn) {
821 ir_mode *emode, *mode;
825 for (i = get_irn_n_outs(irn) - 1; i >= 0; --i) {
826 ir_node *succ = get_irn_out(irn, i);
828 switch (get_irn_opcode(succ)) {
830 /* check if this load is not a hidden conversion */
831 mode = get_Load_mode(succ);
832 ent = is_SymConst(irn) ? get_SymConst_entity(irn) : get_Sel_entity(irn);
833 emode = get_type_mode(get_entity_type(ent));
834 if (is_hidden_cast(mode, emode))
835 return ir_address_taken;
839 /* check that the node is not the Store's value */
840 value = get_Store_value(succ);
842 return ir_address_taken;
843 /* check if this Store is not a hidden conversion */
844 mode = get_irn_mode(value);
845 ent = is_SymConst(irn) ? get_SymConst_entity(irn) : get_Sel_entity(irn);
846 emode = get_type_mode(get_entity_type(ent));
847 if (is_hidden_cast(mode, emode))
848 return ir_address_taken;
852 /* Check the successor of irn. */
853 ir_address_taken_state res = find_address_taken_state(succ);
854 if (res != ir_address_not_taken)
860 /* Only the call address is not an address taker but
861 this is an uninteresting case, so we ignore it here. */
862 for (j = get_Call_n_params(succ) - 1; j >= 0; --j) {
863 ir_node *param = get_Call_param(succ, j);
865 return ir_address_taken;
870 /* another op, the address may be taken */
871 return ir_address_taken_unknown;
874 /* All successors finished, the address is not taken. */
875 return ir_address_not_taken;
876 } /* find_address_taken_state */
879 * Update the "address taken" flag of all frame entities.
881 static void analyse_irg_address_taken(ir_graph *irg) {
882 ir_type *ft = get_irg_frame_type(irg);
886 /* set initial state to not_taken, as this is the "smallest" state */
887 for (i = get_class_n_members(ft) - 1; i >= 0; --i) {
888 ir_entity *ent = get_class_member(ft, i);
890 set_entity_address_taken(ent, ir_address_not_taken);
893 assure_irg_outs(irg);
895 irg_frame = get_irg_frame(irg);
897 for (i = get_irn_n_outs(irg_frame) - 1; i >= 0; --i) {
898 ir_node *succ = get_irn_out(irg_frame, i);
899 ir_address_taken_state state;
902 ir_entity *ent = get_Sel_entity(succ);
904 if (get_entity_address_taken(ent) == ir_address_taken)
907 state = find_address_taken_state(succ);
908 if (state > get_entity_address_taken(ent))
909 set_entity_address_taken(ent, state);
913 irg->adr_taken_state = ir_address_taken_computed;
914 } /* analyse_address_taken */
916 /* Returns the current address taken state of the graph. */
917 ir_address_taken_computed_state get_irg_address_taken_state(const ir_graph *irg) {
918 return irg->adr_taken_state;
919 } /* get_irg_address_taken_state */
921 /* Sets the current address taken state of the graph. */
922 void set_irg_address_taken_state(ir_graph *irg, ir_address_taken_computed_state state) {
923 irg->adr_taken_state = state;
924 } /* set_irg_address_taken_state */
926 /* Assure that the address taken flag is computed for the given graph. */
927 void assure_irg_address_taken_computed(ir_graph *irg) {
928 if (irg->adr_taken_state == ir_address_taken_not_computed)
929 analyse_irg_address_taken(irg);
930 } /* assure_irg_address_taken_computed */
934 * Initialize the address_taken flag for a global type like type.
936 static void init_taken_flag(ir_type * tp) {
939 /* All external visible entities are at least
940 ir_address_taken_unknown. This is very conservative. */
941 for (i = get_compound_n_members(tp) - 1; i >= 0; --i) {
942 ir_entity *ent = get_compound_member(tp, i);
943 ir_address_taken_state state;
945 state = get_entity_visibility(ent) == visibility_external_visible ?
946 ir_address_taken_unknown : ir_address_not_taken ;
947 set_entity_address_taken(ent, state);
949 } /* init_taken_flag */
952 * Mark all entities used in the initializer for the given entity as address taken
954 static void check_initializer(ir_entity *ent) {
958 /* do not check uninitialized values */
959 if (get_entity_variability(ent) == variability_uninitialized)
962 /* Beware: Methods initialized with "themself". This does not count as a taken
964 if (is_Method_type(get_entity_type(ent)))
967 if (is_atomic_entity(ent)) {
968 /* let's check if it's an address */
969 n = get_atomic_ent_value(ent);
970 if (is_SymConst(n) && get_SymConst_kind(n) == symconst_addr_ent) {
971 ir_entity *ent = get_SymConst_entity(n);
972 set_entity_address_taken(ent, ir_address_taken);
975 for (i = get_compound_ent_n_values(ent) - 1; i >= 0; --i) {
976 n = get_compound_ent_value(ent, i);
978 /* let's check if it's an address */
979 if (is_SymConst(n) && get_SymConst_kind(n) == symconst_addr_ent) {
980 ir_entity *ent = get_SymConst_entity(n);
981 set_entity_address_taken(ent, ir_address_taken);
985 } /* check_initializer */
989 * Mark all entities used in initializers as address taken
991 static void check_initializers(ir_type *tp) {
994 for (i = get_compound_n_members(tp) - 1; i >= 0; --i) {
995 ir_entity *ent = get_compound_member(tp, i);
997 check_initializer(ent);
999 } /* check_initializers */
1001 #ifdef DEBUG_libfirm
1003 * Print the address taken state of all entities of a given type for debugging.
1005 static void print_address_taken_state(ir_type *tp) {
1007 for (i = get_compound_n_members(tp) - 1; i >= 0; --i) {
1008 ir_entity *ent = get_compound_member(tp, i);
1009 ir_address_taken_state state = get_entity_address_taken(ent);
1011 if (state != ir_address_not_taken) {
1012 assert(ir_address_not_taken <= (int) state && state <= ir_address_taken);
1013 ir_printf("%+F: %s\n", ent, get_address_taken_state_name(state));
1016 } /* print_address_taken_state */
1017 #endif /* DEBUG_libfirm */
1020 * Post-walker: check for global entity address
1022 static void check_global_address(ir_node *irn, void *env) {
1025 ir_address_taken_state state;
1027 if (is_SymConst(irn) && get_SymConst_kind(irn) == symconst_addr_ent) {
1029 ent = get_SymConst_entity(irn);
1030 } else if (is_Sel(irn) && get_Sel_ptr(irn) == tls) {
1031 /* A TLS variable. */
1032 ent = get_Sel_entity(irn);
1036 if (get_entity_address_taken(ent) >= ir_address_taken) {
1037 /* Already at the maximum. */
1040 state = find_address_taken_state(irn);
1041 if (state > get_entity_address_taken(ent))
1042 set_entity_address_taken(ent, state);
1043 } /* check_global_address */
1046 * Update the "address taken" flag of all global entities.
1048 static void analyse_irp_globals_address_taken(void) {
1051 FIRM_DBG_REGISTER(dbg, "firm.ana.irmemory");
1053 init_taken_flag(get_glob_type());
1054 init_taken_flag(get_tls_type());
1056 check_initializers(get_glob_type());
1057 check_initializers(get_tls_type());
1059 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
1060 ir_graph *irg = get_irp_irg(i);
1062 assure_irg_outs(irg);
1063 irg_walk_graph(irg, NULL, check_global_address, get_irg_tls(irg));
1066 #ifdef DEBUG_libfirm
1067 if (firm_dbg_get_mask(dbg) & LEVEL_1) {
1068 print_address_taken_state(get_glob_type());
1069 print_address_taken_state(get_tls_type());
1071 #endif /* DEBUG_libfirm */
1074 irp->globals_adr_taken_state = ir_address_taken_computed;
1075 } /* analyse_irp_globals_address_taken */
1077 /* Returns the current address taken state of the globals. */
1078 ir_address_taken_computed_state get_irp_globals_address_taken_state(void) {
1079 return irp->globals_adr_taken_state;
1080 } /* get_irp_globals_address_taken_state */
1082 /* Sets the current address taken state of the graph. */
1083 void set_irp_globals_address_taken_state(ir_address_taken_computed_state state) {
1084 irp->globals_adr_taken_state = state;
1085 } /* set_irg_address_taken_state */
1087 /* Assure that the address taken flag is computed for the globals. */
1088 void assure_irp_globals_address_taken_computed(void) {
1089 if (irp->globals_adr_taken_state == ir_address_taken_not_computed)
1090 analyse_irp_globals_address_taken();
1091 } /* assure_irp_globals_address_taken_computed */
1094 #include <adt/pmap.h>
1095 #include "typerep.h"
1097 DEBUG_ONLY(static firm_dbg_module_t *dbgcall = NULL;)
1099 /** Maps method types to cloned method types. */
1100 static pmap *mtp_map;
1103 * Clone a method type if not already cloned.
1105 static ir_type *clone_type_and_cache(ir_type *tp) {
1106 static ident *prefix = NULL;
1108 pmap_entry *e = pmap_find(mtp_map, tp);
1114 prefix = new_id_from_chars("C", 1);
1116 res = clone_type_method(tp, prefix);
1117 pmap_insert(mtp_map, tp, res);
1120 } /* clone_type_and_cache */
1123 * Copy the calling conventions from the entities to the call type.
1125 static void update_calls_to_private(ir_node *call, void *env) {
1127 if (is_Call(call)) {
1128 ir_node *ptr = get_Call_ptr(call);
1130 if (is_SymConst(ptr)) {
1131 ir_entity *ent = get_SymConst_entity(ptr);
1132 ir_type *mtp = get_entity_type(ent);
1133 ir_type *ctp = get_Call_type(call);
1135 if ((get_method_additional_properties(ctp) & mtp_property_private) == 0) {
1136 ctp = clone_type_and_cache(ctp);
1137 set_method_additional_property(ctp, mtp_property_private);
1138 set_Call_type(call, ctp);
1139 DB((dbgcall, LEVEL_1, "changed call to private method %+F\n", ent));
1143 } /* update_calls_to_private */
1145 /* Mark all private methods, i.e. those of which all call sites are known. */
1146 void mark_private_methods(void) {
1150 FIRM_DBG_REGISTER(dbgcall, "firm.opt.cc");
1152 assure_irp_globals_address_taken_computed();
1154 mtp_map = pmap_create();
1156 /* first step: change the calling conventions of the local non-escaped entities */
1157 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
1158 ir_graph *irg = get_irp_irg(i);
1159 ir_entity *ent = get_irg_entity(irg);
1160 ir_address_taken_state state = get_entity_address_taken(ent);
1162 if (get_entity_visibility(ent) == visibility_local &&
1163 state == ir_address_not_taken) {
1164 ir_type *mtp = get_entity_type(ent);
1166 set_entity_additional_property(ent, mtp_property_private);
1167 if ((get_method_additional_properties(mtp) & mtp_property_private) == 0) {
1168 /* need a new type */
1169 mtp = clone_type_and_cache(mtp);
1170 set_entity_type(ent, mtp);
1171 set_method_additional_property(mtp, mtp_property_private);
1174 DB((dbgcall, LEVEL_1, "found private method %+F\n", ent));
1179 all_irg_walk(NULL, update_calls_to_private, NULL);
1181 pmap_destroy(mtp_map);
1182 } /* mark_private_methods */