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 /* Get the memory disambiguator options for a graph. */
55 unsigned get_irg_memory_disambiguator_options(ir_graph *irg) {
56 unsigned opt = irg->mem_disambig_opt;
57 if (opt & aa_opt_inherited)
58 return global_mem_disamgig_opt;
60 } /* get_irg_memory_disambiguator_options */
62 /* Set the memory disambiguator options for a graph. */
63 void set_irg_memory_disambiguator_options(ir_graph *irg, unsigned options) {
64 irg->mem_disambig_opt = options & ~aa_opt_inherited;
65 } /* set_irg_memory_disambiguator_options */
67 /* Set the global disambiguator options for all graphs not having local options. */
68 void set_irp_memory_disambiguator_options(unsigned options) {
69 global_mem_disamgig_opt = options;
70 } /* set_irp_memory_disambiguator_options */
73 * Find the base address and entity of an Sel node.
76 * @param pEnt after return points to the base entity.
78 * @return the base address.
80 static ir_node *find_base_adr(ir_node *sel, ir_entity **pEnt) {
81 ir_node *ptr = get_Sel_ptr(sel);
85 ptr = get_Sel_ptr(sel);
87 *pEnt = get_Sel_entity(sel);
92 * Check if a given Const node is greater or equal a given size.
94 * @return no_alias if the Const is greater, may_alias else
96 static ir_alias_relation check_const(ir_node *cns, int size) {
97 tarval *tv = get_Const_tarval(cns);
101 return classify_tarval(tv) != TV_CLASSIFY_NULL ? no_alias : may_alias;
102 tv_size = new_tarval_from_long(size, get_tarval_mode(tv));
103 return tarval_cmp(tv_size, tv) & (pn_Cmp_Eq|pn_Cmp_Lt) ? no_alias : may_alias;
107 * Treat idx1 and idx2 as integer indexes and check if they differ always more than size.
109 * @return sure_alias iff idx1 == idx2
110 * no_alias iff they ALWAYS differ more than size
113 static ir_alias_relation different_index(ir_node *idx1, ir_node *idx2, int size) {
116 if (is_Const(idx1) && is_Const(idx2)) {
117 /* both are const, we can compare them */
118 tarval *tv1 = get_Const_tarval(idx1);
119 tarval *tv2 = get_Const_tarval(idx2);
120 tarval *tv, *tv_size;
123 return tv1 == tv2 ? sure_alias : no_alias;
125 if (tarval_cmp(tv1, tv2) == pn_Cmp_Gt) {
130 /* tv1 is now the "smaller" one */
131 tv = tarval_sub(tv2, tv1);
132 tv_size = new_tarval_from_long(size, get_tarval_mode(tv));
133 return tarval_cmp(tv_size, tv) & (pn_Cmp_Eq|pn_Cmp_Lt) ? no_alias : may_alias;
136 /* Note: we rely here on the fact that normalization puts constants on the RIGHT side */
138 ir_node *l1 = get_Add_left(idx1);
139 ir_node *r1 = get_Add_right(idx1);
144 return check_const(r1, size);
147 /* both are Adds, check if they are of x + a == x + b kind */
148 ir_node *l2 = get_Add_left(idx2);
149 ir_node *r2 = get_Add_right(idx2);
152 return different_index(r1, r2, size);
154 return different_index(r1, l2, size);
156 return different_index(l1, l2, size);
158 return different_index(l1, r2, size);
162 ir_node *l2 = get_Add_left(idx2);
163 ir_node *r2 = get_Add_right(idx2);
168 return check_const(r2, size);
173 ir_node *l1 = get_Sub_left(idx1);
174 ir_node *r1 = get_Sub_right(idx1);
179 return check_const(r1, size);
183 /* both are Subs, check if they are of x - a == x - b kind */
184 ir_node *l2 = get_Sub_left(idx2);
187 ir_node *r2 = get_Sub_right(idx2);
188 return different_index(r1, r2, size);
193 ir_node *l2 = get_Sub_left(idx2);
194 ir_node *r2 = get_Sub_right(idx2);
199 return check_const(r2, size);
204 } /* different_index */
207 * Two Sel addresses have the same base address, check if there offsets are different.
209 * @param adr1 The first address.
210 * @param adr2 The second address.
212 static ir_alias_relation different_sel_offsets(ir_node *sel1, ir_node *sel2) {
213 ir_entity *ent1 = get_Sel_entity(sel1);
214 ir_entity *ent2 = get_Sel_entity(sel2);
215 int i, check_arr = 0;
220 ir_type *tp1 = get_entity_type(ent1);
221 ir_type *tp2 = get_entity_type(ent2);
225 else if (get_type_state(tp1) == layout_fixed && get_type_state(tp2) == layout_fixed &&
226 get_type_size_bits(tp1) == get_type_size_bits(tp2))
230 /* we select an entity of same size, check for indexes */
231 int n = get_Sel_n_indexs(sel1);
234 if (n > 0 && n == get_Sel_n_indexs(sel2)) {
235 /* same non-zero number of indexes, an array access, check */
236 for (i = 0; i < n; ++i) {
237 ir_node *idx1 = get_Sel_index(sel1, i);
238 ir_node *idx2 = get_Sel_index(sel2, i);
239 ir_alias_relation res = different_index(idx1, idx2, 0); /* we can safely IGNORE the size here if it's at least >0 */
241 if (res == may_alias)
243 else if (res == no_alias)
246 /* if we have at least one no_alias, there is no alias relation, else we have sure */
247 return have_no > 0 ? no_alias : sure_alias;
251 } /* different_sel_offsets */
254 * Determine the alias relation by checking if adr1 and adr2 are pointer
257 * @param adr1 The first address.
258 * @param adr2 The second address.
260 static ir_alias_relation different_types(ir_node *adr1, ir_node *adr2)
262 ir_entity *ent1 = NULL, *ent2 = NULL;
264 if (is_SymConst(adr1) && get_SymConst_kind(adr1) == symconst_addr_ent)
265 ent1 = get_SymConst_entity(adr1);
266 else if (is_Sel(adr1))
267 ent1 = get_Sel_entity(adr1);
269 if (is_SymConst(adr2) && get_SymConst_kind(adr2) == symconst_addr_ent)
270 ent2 = get_SymConst_entity(adr2);
271 else if (is_Sel(adr2))
272 ent2 = get_Sel_entity(adr2);
274 if (ent1 != NULL && ent2 != NULL) {
275 ir_type *tp1 = get_entity_type(ent1);
276 ir_type *tp2 = get_entity_type(ent2);
279 if (is_Pointer_type(tp1) && is_Pointer_type(tp2)) {
280 /* do deref until no pointer types are found */
282 tp1 = get_pointer_points_to_type(tp1);
283 tp2 = get_pointer_points_to_type(tp2);
284 } while (is_Pointer_type(tp1) && is_Pointer_type(tp2));
287 if (get_type_tpop(tp1) != get_type_tpop(tp2)) {
288 /* different type structure */
291 if (is_Class_type(tp1)) {
292 /* check class hierarchy */
293 if (! is_SubClass_of(tp1, tp2) &&
294 ! is_SubClass_of(tp2, tp1))
297 /* different types */
303 } /* different_types */
306 * Check if an offset is a constant and these constant is bigger or equal
309 static int check_const_offset(ir_node *offset, int size) {
310 ir_mode *mode = get_irn_mode(offset);
312 /* ok, we found an offset, check for constant */
313 if (is_Const(offset) && mode_is_int(mode)) {
314 tarval *tv = new_tarval_from_long(size, mode);
316 /* size <= offset ? */
317 if (tarval_cmp(tv, get_Const_tarval(offset)) & (pn_Cmp_Eq|pn_Cmp_Lt))
321 } /* check_const_offset */
324 * Check if we can determine that the two pointers always have an offset bigger then size
326 static ir_alias_relation _different_pointer(ir_node *adr1, ir_node *adr2, int size) {
330 /* first address is the result of a pointer addition */
331 ir_node *l1 = get_Add_left(adr1);
332 ir_node *r1 = get_Add_right(adr1);
335 found = check_const_offset(r1, size);
336 } else if (r1 == adr2) {
337 found = check_const_offset(l1, size);
338 } else if (is_Add(adr2)) {
339 /* second address is the result of a pointer addition */
340 ir_node *l2 = get_Add_left(adr2);
341 ir_node *r2 = get_Add_right(adr2);
344 return _different_pointer(r1, r2, size);
345 } else if (l1 == r2) {
346 return _different_pointer(r1, l2, size);
347 } else if (r1 == l2) {
348 return _different_pointer(l1, r2, size);
349 } else if (r1 == r2) {
350 return _different_pointer(l1, l2, size);
353 } else if (is_Add(adr2)) {
354 /* second address is the result of a pointer addition */
355 ir_node *l2 = get_Add_left(adr2);
356 ir_node *r2 = get_Add_right(adr2);
359 found = check_const_offset(r2, size);
360 } else if (r2 == adr1) {
361 found = check_const_offset(l2, size);
364 return different_index(adr1, adr2, size);
366 return found ? no_alias : may_alias;
367 } /* _different_pointer */
370 * Check if we can determine that the two pointers always have an offset bigger then the maximum size of mode1, mode2
372 static ir_alias_relation different_pointer(ir_node *adr1, ir_mode *mode1, ir_node *adr2, ir_mode *mode2) {
373 int size = get_mode_size_bytes(mode1);
374 int n = get_mode_size_bytes(mode2);
378 return _different_pointer(adr1, adr2, size);
379 } /* different_pointer */
382 * Returns non-zero if a node is a routine parameter.
384 * @param node the node to test
386 static int is_arg_Proj(ir_node *node) {
389 node = get_Proj_pred(node);
392 return pn_Start_T_args == get_Proj_proj(node) && is_Start(get_Proj_pred(node));
396 * Returns true if an address represents a global variable.
398 static INLINE int is_global_var(ir_node *irn) {
399 return is_SymConst(irn) && get_SymConst_kind(irn) == symconst_addr_ent;
400 } /* is_global_var */
403 * Determine the alias relation between two addresses.
405 static ir_alias_relation _get_alias_relation(
407 ir_node *adr1, ir_mode *mode1,
408 ir_node *adr2, ir_mode *mode2)
411 ir_entity *ent1, *ent2;
414 if (! get_opt_alias_analysis())
420 options = get_irg_memory_disambiguator_options(irg);
422 /* The Armageddon switch */
423 if (options & aa_opt_no_alias)
426 /* Two save some code, sort the addresses by its id's. Beware, this
427 might break some things, so better check here. */
428 assert(iro_SymConst < iro_Sel && iro_Sel < iro_Proj && "Code dependence breaked");
429 op1 = get_irn_opcode(adr1);
430 op2 = get_irn_opcode(adr2);
441 if (is_global_var(adr1)) {
442 /* first address is a global variable */
444 if (is_global_var(adr2)) {
445 /* both addresses are global variables and we know
446 they are different (R1 a) */
447 if (get_SymConst_entity(adr1) != get_SymConst_entity(adr2))
450 /* equal entity addresses */
455 ir_node *base2 = find_base_adr(adr2, &ent2);
457 if (is_global_var(base2)) {
458 /* base2 address is a global var (R1 a) */
461 } else if (base2 == get_irg_frame(irg)) {
462 /* the second one is a local variable so they are always
465 } else if (base2 == get_irg_tls(irg)) {
466 /* the second one is a TLS variable so they are always
472 /* Here we are: the first is a global var, the second some pointer. */
473 ent1 = get_SymConst_entity(adr1);
474 if (get_entity_address_taken(ent1) == ir_address_not_taken) {
475 /* The address of the global variable was never taken, so
476 the pointer cannot match (R2). */
479 } else if (is_Sel(adr1)) {
480 /* the first address is a Sel */
481 ir_node *base1 = find_base_adr(adr1, &ent1);
483 if (base1 == get_irg_frame(irg)) {
484 /* first is a local variable ent1 */
486 /* the second address is a Sel */
487 ir_node *base2 = find_base_adr(adr2, &ent2);
489 if (base1 == base2) {
490 /* identical bases: check for different offsets */
491 return different_sel_offsets(adr1, adr2);
492 } else if (base2 == get_irg_frame(irg)) {
493 /* both addresses are local variables and we know
494 they are different (R1 a) */
497 } else if (base2 == get_irg_tls(irg)) {
498 /* the second one is a TLS variable so they are always
501 } else if (is_arg_Proj(base2)) {
502 /* the second one is an offset from a parameter so they are
503 always different (R1 e) */
506 } else if (is_arg_Proj(adr2)) {
507 /* a local variable and a parameter are always different (R1 e) */
510 } else if (base1 == get_irg_tls(irg)) {
511 /* the first is a TLS variable */
513 /* the second address is a Sel */
514 ir_node *base2 = find_base_adr(adr2, &ent2);
517 return different_sel_offsets(adr1, adr2);
518 else if (base2 == get_irg_frame(irg)) {
519 /* the second one is a local variable so they are always
522 } else if (base2 == get_irg_tls(irg)) {
523 /* both addresses are TLS variables and we know
524 they are different (R1 a) */
529 } else if (is_arg_Proj(base1)) {
530 /* the first one is an offset from a parameter */
532 /* the second address is a Sel */
533 ir_node *base2 = find_base_adr(adr2, &ent2);
535 if (base2 == get_irg_frame(irg)) {
536 /* the second one is a local variable so they are always
541 } else if (is_global_var(base1)) {
542 /* the first one is a global variable */
543 ent1 = get_SymConst_entity(base1);
545 /* the second address is a Sel */
546 ir_node *base2 = find_base_adr(adr2, &ent2);
549 return different_sel_offsets(adr1, adr2);
550 else if (base2 == get_irg_frame(irg)) {
551 /* the second one is a local variable so they are always
554 } else if (base2 == get_irg_tls(irg)) {
555 /* the second one is a TLS variable so they are always
558 } else if (is_arg_Proj(base2)) {
559 if (get_entity_address_taken(ent1) == ir_address_not_taken) {
560 /* The address of the global variable was never taken, so
561 the pointer cannot match (R2). */
564 } else if (is_global_var(base2)) {
565 ent2 = get_SymConst_entity(base2);
566 /* both addresses are global variables and we know
567 they are different (R1 a) */
574 /* some pointers, check if they have the same base buf constant offset */
575 ir_alias_relation rel = different_pointer(adr1, mode1, adr2, mode2);
576 if (rel != may_alias)
581 if (options & aa_opt_type_based) { /* Type based alias analysis */
582 ir_alias_relation rel;
584 if (options & aa_opt_byte_type_may_alias) {
585 if (get_mode_size_bits(mode1) == 8 || get_mode_size_bits(mode2) == 8) {
586 /* One of the modes address a byte. Assume a may_alias and leave
587 the type based check. */
588 goto leave_type_based_alias;
591 /* cheap check: If the mode sizes did not match, the types MUST be different */
592 if (get_mode_size_bits(mode1) != get_mode_size_bits(mode2))
596 rel = different_types(adr1, adr2);
597 if (rel != may_alias)
599 leave_type_based_alias:;
602 /* do we have a language specific memory disambiguator? */
603 if (language_disambuigator) {
604 ir_alias_relation rel = (*language_disambuigator)(irg, adr1, mode1, adr2, mode2);
605 if (rel != may_alias)
609 /* access points-to information here */
611 } /* _get_alias_relation */
614 * Determine the alias relation between two addresses.
616 ir_alias_relation get_alias_relation(
618 ir_node *adr1, ir_mode *mode1,
619 ir_node *adr2, ir_mode *mode2)
621 ir_alias_relation rel = _get_alias_relation(irg, adr1, mode1, adr2, mode2);
623 } /* get_alias_relation */
625 /* Set a source language specific memory disambiguator function. */
626 void set_language_memory_disambiguator(DISAMBIGUATOR_FUNC func) {
627 language_disambuigator = func;
628 } /* set_language_memory_disambiguator */
630 /** The result cache for the memory disambiguator. */
631 static set *result_cache = NULL;
633 /** An entry in the relation cache. */
634 typedef struct mem_disambig_entry {
635 ir_node *adr1; /**< The first address. */
636 ir_node *adr2; /**< The second address. */
637 ir_alias_relation result; /**< The alias relation result. */
638 } mem_disambig_entry;
640 #define HASH_ENTRY(adr1, adr2) (HASH_PTR(adr1) ^ HASH_PTR(adr2))
643 * Compare two relation cache entries.
645 static int cmp_mem_disambig_entry(const void *elt, const void *key, size_t size) {
646 const mem_disambig_entry *p1 = elt;
647 const mem_disambig_entry *p2 = key;
650 return p1->adr1 == p2->adr1 && p1->adr2 == p2->adr2;
651 } /* cmp_mem_disambig_entry */
654 * Initialize the relation cache.
656 void mem_disambig_init(void) {
657 result_cache = new_set(cmp_mem_disambig_entry, 8);
658 } /* mem_disambig_init */
661 * Determine the alias relation between two addresses.
663 ir_alias_relation get_alias_relation_ex(
665 ir_node *adr1, ir_mode *mode1,
666 ir_node *adr2, ir_mode *mode2)
668 mem_disambig_entry key, *entry;
670 if (! get_opt_alias_analysis())
673 if (get_irn_opcode(adr1) > get_irn_opcode(adr2)) {
681 entry = set_find(result_cache, &key, sizeof(key), HASH_ENTRY(adr1, adr2));
683 return entry->result;
685 key.result = get_alias_relation(irg, adr1, mode1, adr2, mode2);
687 set_insert(result_cache, &key, sizeof(key), HASH_ENTRY(adr1, adr2));
689 } /* get_alias_relation_ex */
691 /* Free the relation cache. */
692 void mem_disambig_term(void) {
694 del_set(result_cache);
697 } /* mem_disambig_term */
700 * Check the mode of a Load/Store with the mode of the entity
702 * If the mode of the entity and the Load/Store mode do not match, we
703 * have the bad reinterpret case:
706 * char b = *(char *)&i;
708 * We do NOT count this as one value and return address_taken
710 * However, we support an often used case. If the mode is two-complement
711 * we allow casts between signed/unsigned.
713 * @param mode the mode of the Load/Store
714 * @param ent_mode the mode of the accessed entity
716 * @return non-zero if the Load/Store is a hidden cast, zero else
718 static int is_hidden_cast(ir_mode *mode, ir_mode *ent_mode) {
719 if (ent_mode != mode) {
720 if (ent_mode == NULL ||
721 get_mode_size_bits(ent_mode) != get_mode_size_bits(mode) ||
722 get_mode_sort(ent_mode) != get_mode_sort(mode) ||
723 get_mode_arithmetic(ent_mode) != irma_twos_complement ||
724 get_mode_arithmetic(mode) != irma_twos_complement)
728 } /* is_hidden_cast */
731 * Determine the address_taken state of a node (or it's successor Sels).
733 * @param irn the node
735 static ir_address_taken_state find_address_taken_state(ir_node *irn) {
737 ir_mode *emode, *mode;
741 for (i = get_irn_n_outs(irn) - 1; i >= 0; --i) {
742 ir_node *succ = get_irn_out(irn, i);
744 switch (get_irn_opcode(succ)) {
746 /* check if this load is not a hidden conversion */
747 mode = get_Load_mode(succ);
748 ent = is_SymConst(irn) ? get_SymConst_entity(irn) : get_Sel_entity(irn);
749 emode = get_type_mode(get_entity_type(ent));
750 if (is_hidden_cast(mode, emode))
751 return ir_address_taken;
755 /* check that the node is not the Store's value */
756 value = get_Store_value(succ);
758 return ir_address_taken;
759 /* check if this Store is not a hidden conversion */
760 mode = get_irn_mode(value);
761 ent = is_SymConst(irn) ? get_SymConst_entity(irn) : get_Sel_entity(irn);
762 emode = get_type_mode(get_entity_type(ent));
763 if (is_hidden_cast(mode, emode))
764 return ir_address_taken;
768 /* Check the successor of irn. */
769 ir_address_taken_state res = find_address_taken_state(succ);
770 if (res != ir_address_not_taken)
776 /* Only the call address is not an address taker but
777 this is an uninteresting case, so we ignore it here. */
778 for (j = get_Call_n_params(succ) - 1; j >= 0; --j) {
779 ir_node *param = get_Call_param(succ, j);
781 return ir_address_taken;
786 /* another op, the address may be taken */
787 return ir_address_taken_unknown;
790 /* All successors finished, the address is not taken. */
791 return ir_address_not_taken;
792 } /* find_address_taken_state */
795 * Update the "address taken" flag of all frame entities.
797 static void analyse_irg_address_taken(ir_graph *irg) {
798 ir_type *ft = get_irg_frame_type(irg);
802 /* set initial state to not_taken, as this is the "smallest" state */
803 for (i = get_class_n_members(ft) - 1; i >= 0; --i) {
804 ir_entity *ent = get_class_member(ft, i);
806 set_entity_address_taken(ent, ir_address_not_taken);
809 assure_irg_outs(irg);
811 irg_frame = get_irg_frame(irg);
813 for (i = get_irn_n_outs(irg_frame) - 1; i >= 0; --i) {
814 ir_node *succ = get_irn_out(irg_frame, i);
815 ir_address_taken_state state;
818 ir_entity *ent = get_Sel_entity(succ);
820 if (get_entity_address_taken(ent) == ir_address_taken)
823 state = find_address_taken_state(succ);
824 if (state > get_entity_address_taken(ent))
825 set_entity_address_taken(ent, state);
829 irg->adr_taken_state = ir_address_taken_computed;
830 } /* analyse_address_taken */
832 /* Returns the current address taken state of the graph. */
833 ir_address_taken_computed_state get_irg_address_taken_state(const ir_graph *irg) {
834 return irg->adr_taken_state;
835 } /* get_irg_address_taken_state */
837 /* Sets the current address taken state of the graph. */
838 void set_irg_address_taken_state(ir_graph *irg, ir_address_taken_computed_state state) {
839 irg->adr_taken_state = state;
840 } /* set_irg_address_taken_state */
842 /* Assure that the address taken flag is computed for the given graph. */
843 void assure_irg_address_taken_computed(ir_graph *irg) {
844 if (irg->adr_taken_state == ir_address_taken_not_computed)
845 analyse_irg_address_taken(irg);
846 } /* assure_irg_address_taken_computed */
850 * Initialize the address_taken flag for a global type like type.
852 static void init_taken_flag(ir_type * tp) {
855 /* All external visible entities are at least
856 ir_address_taken_unknown. This is very conservative. */
857 for (i = get_compound_n_members(tp) - 1; i >= 0; --i) {
858 ir_entity *ent = get_compound_member(tp, i);
859 ir_address_taken_state state;
861 state = get_entity_visibility(ent) == visibility_external_visible ?
862 ir_address_taken_unknown : ir_address_not_taken ;
863 set_entity_address_taken(ent, state);
865 } /* init_taken_flag */
868 * Mark all entities used in the initializer for the given entity as address taken
870 static void check_initializer(ir_entity *ent) {
874 /* do not check uninitialized values */
875 if (get_entity_variability(ent) == variability_uninitialized)
878 if (is_atomic_entity(ent)) {
879 /* let's check if it's an address */
880 n = get_atomic_ent_value(ent);
881 if (is_SymConst(n) && get_SymConst_kind(n) == symconst_addr_ent) {
882 ir_entity *ent = get_SymConst_entity(n);
883 set_entity_address_taken(ent, ir_address_taken);
886 for (i = get_compound_ent_n_values(ent) - 1; i >= 0; --i) {
887 n = get_compound_ent_value(ent, i);
889 /* let's check if it's an address */
890 if (is_SymConst(n) && get_SymConst_kind(n) == symconst_addr_ent) {
891 ir_entity *ent = get_SymConst_entity(n);
892 set_entity_address_taken(ent, ir_address_taken);
896 } /* check_initializer */
900 * Mark all entities used in initializers as address taken
902 static void check_initializers(ir_type *tp) {
905 for (i = get_compound_n_members(tp) - 1; i >= 0; --i) {
906 ir_entity *ent = get_compound_member(tp, i);
908 check_initializer(ent);
910 } /* check_initializers */
914 * Print the address taken state of all entities of a given type for debugging.
916 static void print_address_taken_state(ir_type *tp) {
918 for (i = get_compound_n_members(tp) - 1; i >= 0; --i) {
919 ir_entity *ent = get_compound_member(tp, i);
920 ir_address_taken_state state = get_entity_address_taken(ent);
922 if (state != ir_address_not_taken) {
923 assert(ir_address_not_taken <= (int) state && state <= ir_address_taken);
924 ir_printf("%+F: %s\n", ent, get_address_taken_state_name(state));
927 } /* print_address_taken_state */
928 #endif /* DEBUG_libfirm */
931 * Post-walker: check for global entity address
933 static void check_global_address(ir_node *irn, void *env) {
936 ir_address_taken_state state;
938 if (is_SymConst(irn) && get_SymConst_kind(irn) == symconst_addr_ent) {
940 ent = get_SymConst_entity(irn);
941 } else if (is_Sel(irn) && get_Sel_ptr(irn) == tls) {
942 /* A TLS variable. */
943 ent = get_Sel_entity(irn);
947 if (get_entity_address_taken(ent) >= ir_address_taken) {
948 /* Already at the maximum. */
951 state = find_address_taken_state(irn);
952 if (state > get_entity_address_taken(ent))
953 set_entity_address_taken(ent, state);
954 } /* check_global_address */
957 * Update the "address taken" flag of all global entities.
959 static void analyse_irp_globals_address_taken(void) {
962 FIRM_DBG_REGISTER(dbg, "firm.ana.irmemory");
964 init_taken_flag(get_glob_type());
965 init_taken_flag(get_tls_type());
967 check_initializers(get_glob_type());
968 check_initializers(get_tls_type());
970 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
971 ir_graph *irg = get_irp_irg(i);
973 assure_irg_outs(irg);
974 irg_walk_graph(irg, NULL, check_global_address, get_irg_tls(irg));
978 if (firm_dbg_get_mask(dbg) & LEVEL_1) {
979 print_address_taken_state(get_glob_type());
980 print_address_taken_state(get_tls_type());
982 #endif /* DEBUG_libfirm */
985 irp->globals_adr_taken_state = ir_address_taken_computed;
986 } /* analyse_irp_globals_address_taken */
988 /* Returns the current address taken state of the globals. */
989 ir_address_taken_computed_state get_irp_globals_address_taken_state(void) {
990 return irp->globals_adr_taken_state;
991 } /* get_irp_globals_address_taken_state */
993 /* Sets the current address taken state of the graph. */
994 void set_irp_globals_address_taken_state(ir_address_taken_computed_state state) {
995 irp->globals_adr_taken_state = state;
996 } /* set_irg_address_taken_state */
998 /* Assure that the address taken flag is computed for the globals. */
999 void assure_irp_globals_address_taken_computed(void) {
1000 if (irp->globals_adr_taken_state == ir_address_taken_not_computed)
1001 analyse_irp_globals_address_taken();
1002 } /* assure_irp_globals_address_taken_computed */
1005 DEBUG_ONLY(static firm_dbg_module_t *dbgcall = NULL;)
1008 * Copy the calling conventions from the entities to the call type.
1010 static void update_calls(ir_node *call, void *env) {
1012 if (is_Call(call)) {
1013 ir_node *ptr = get_Call_ptr(call);
1015 if (is_SymConst(ptr)) {
1016 ir_entity *ent = get_SymConst_entity(ptr);
1017 ir_type *mtp = get_entity_type(ent);
1018 ir_type *ctp = get_Call_type(call);
1020 if (mtp != ctp && get_method_additional_properties(mtp) & mtp_property_private) {
1021 set_method_additional_property(ctp, mtp_property_private);
1022 DB((dbgcall, LEVEL_1, "changed call to private method %+F\n", ent));
1028 /* Mark all private methods, i.e. those of which all call sites are known. */
1029 void mark_private_methods(void) {
1033 FIRM_DBG_REGISTER(dbgcall, "firm.opt.cc");
1035 assure_irp_globals_address_taken_computed();
1037 /* first step: change the calling conventions of the local non-escaped entities */
1038 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
1039 ir_graph *irg = get_irp_irg(i);
1040 ir_entity *ent = get_irg_entity(irg);
1041 ir_address_taken_state state = get_entity_address_taken(ent);
1043 if (get_entity_visibility(ent) == visibility_local &&
1044 state == ir_address_not_taken) {
1045 ir_type *mtp = get_entity_type(ent);
1047 set_method_additional_property(mtp, mtp_property_private);
1049 DB((dbgcall, LEVEL_1, "found private method %+F\n", ent));
1054 all_irg_walk(NULL, update_calls, NULL);