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"
46 /** The debug handle. */
47 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
49 /** The source language specific language disambiguator function. */
50 static DISAMBIGUATOR_FUNC language_disambuigator = NULL;
52 /** The global memory disambiguator options. */
53 static unsigned global_mem_disamgig_opt = aa_opt_no_opt;
55 /* Returns a human readable name for an alias relation. */
56 const char *get_ir_alias_relation_name(ir_alias_relation rel) {
57 #define X(a) case a: return #a
62 default: assert(0); return "UNKNOWN";
67 /* Get the memory disambiguator options for a graph. */
68 unsigned get_irg_memory_disambiguator_options(ir_graph *irg) {
69 unsigned opt = irg->mem_disambig_opt;
70 if (opt & aa_opt_inherited)
71 return global_mem_disamgig_opt;
73 } /* get_irg_memory_disambiguator_options */
75 /* Set the memory disambiguator options for a graph. */
76 void set_irg_memory_disambiguator_options(ir_graph *irg, unsigned options) {
77 irg->mem_disambig_opt = options & ~aa_opt_inherited;
78 } /* set_irg_memory_disambiguator_options */
80 /* Set the global disambiguator options for all graphs not having local options. */
81 void set_irp_memory_disambiguator_options(unsigned options) {
82 global_mem_disamgig_opt = options;
83 } /* set_irp_memory_disambiguator_options */
86 * Find the base address and entity of an Sel node.
89 * @param pEnt after return points to the base entity.
91 * @return the base address.
93 static ir_node *find_base_adr(ir_node *sel, ir_entity **pEnt) {
94 ir_node *ptr = get_Sel_ptr(sel);
98 ptr = get_Sel_ptr(sel);
100 *pEnt = get_Sel_entity(sel);
102 } /* find_base_adr */
105 * Check if a given Const node is greater or equal a given size.
107 * @return ir_no_alias if the Const is greater, ir_may_alias else
109 static ir_alias_relation check_const(ir_node *cns, int size) {
110 tarval *tv = get_Const_tarval(cns);
114 return tarval_is_null(tv) ? ir_may_alias : ir_no_alias;
115 tv_size = new_tarval_from_long(size, get_tarval_mode(tv));
116 return tarval_cmp(tv_size, tv) & (pn_Cmp_Eq|pn_Cmp_Lt) ? ir_no_alias : ir_may_alias;
120 * Treat idx1 and idx2 as integer indexes and check if they differ always more than size.
122 * @return ir_sure_alias iff idx1 == idx2
123 * ir_no_alias iff they ALWAYS differ more than size
126 static ir_alias_relation different_index(ir_node *idx1, ir_node *idx2, int size) {
128 return ir_sure_alias;
129 if (is_Const(idx1) && is_Const(idx2)) {
130 /* both are const, we can compare them */
131 tarval *tv1 = get_Const_tarval(idx1);
132 tarval *tv2 = get_Const_tarval(idx2);
133 tarval *tv, *tv_size;
137 return tv1 == tv2 ? ir_sure_alias : ir_no_alias;
139 /* arg, modes may be different */
140 m1 = get_tarval_mode(tv1);
141 m2 = get_tarval_mode(tv2);
143 int size = get_mode_size_bits(m1) - get_mode_size_bits(m2);
146 /* m1 is a small mode, cast up */
147 m1 = mode_is_signed(m1) ? find_signed_mode(m2) : find_unsigned_mode(m2);
149 /* should NOT happen, but if it does we give up here */
152 tv1 = tarval_convert_to(tv1, m1);
153 } else if (size > 0) {
154 /* m2 is a small mode, cast up */
155 m2 = mode_is_signed(m2) ? find_signed_mode(m1) : find_unsigned_mode(m1);
157 /* should NOT happen, but if it does we give up here */
160 tv2 = tarval_convert_to(tv2, m2);
162 /* here the size should be identical, check for signed */
163 if (get_mode_sign(m1) != get_mode_sign(m2)) {
164 /* find the signed */
165 if (mode_is_signed(m2)) {
172 /* m1 is now the signed one */
173 if (tarval_cmp(tv1, get_tarval_null(m1)) & (pn_Cmp_Eq|pn_Cmp_Gt)) {
174 /* tv1 is signed, but >= 0, simply cast into unsigned */
175 tv1 = tarval_convert_to(tv1, m2);
177 tv_size = new_tarval_from_long(size, m2);
179 if (tarval_cmp(tv2, tv_size) & (pn_Cmp_Eq|pn_Cmp_Gt)) {
180 /* tv1 is negative and tv2 >= tv_size, so the difference is bigger than size */
183 /* tv_size > tv2, so we can subtract without overflow */
184 tv2 = tarval_sub(tv_size, tv2, NULL);
186 /* tv1 is < 0, so we can negate it */
187 tv1 = tarval_neg(tv1);
189 /* cast it into unsigned. for two-complement it does the right thing for MIN_INT */
190 tv1 = tarval_convert_to(tv1, m2);
192 /* now we can compare without overflow */
193 return tarval_cmp(tv1, tv2) & (pn_Cmp_Eq|pn_Cmp_Gt) ? ir_no_alias : ir_may_alias;
197 if (tarval_cmp(tv1, tv2) == pn_Cmp_Gt) {
202 /* tv1 is now the "smaller" one */
203 tv = tarval_sub(tv2, tv1, NULL);
204 tv_size = new_tarval_from_long(size, get_tarval_mode(tv));
205 return tarval_cmp(tv_size, tv) & (pn_Cmp_Eq|pn_Cmp_Lt) ? ir_no_alias : ir_may_alias;
208 /* Note: we rely here on the fact that normalization puts constants on the RIGHT side */
210 ir_node *l1 = get_Add_left(idx1);
211 ir_node *r1 = get_Add_right(idx1);
216 return check_const(r1, size);
219 /* both are Adds, check if they are of x + a == x + b kind */
220 ir_node *l2 = get_Add_left(idx2);
221 ir_node *r2 = get_Add_right(idx2);
224 return different_index(r1, r2, size);
226 return different_index(r1, l2, size);
228 return different_index(l1, l2, size);
230 return different_index(l1, r2, size);
234 ir_node *l2 = get_Add_left(idx2);
235 ir_node *r2 = get_Add_right(idx2);
240 return check_const(r2, size);
245 ir_node *l1 = get_Sub_left(idx1);
246 ir_node *r1 = get_Sub_right(idx1);
251 return check_const(r1, size);
255 /* both are Subs, check if they are of x - a == x - b kind */
256 ir_node *l2 = get_Sub_left(idx2);
259 ir_node *r2 = get_Sub_right(idx2);
260 return different_index(r1, r2, size);
265 ir_node *l2 = get_Sub_left(idx2);
266 ir_node *r2 = get_Sub_right(idx2);
271 return check_const(r2, size);
276 } /* different_index */
279 * Two Sel addresses have the same base address, check if there offsets are
282 * @param adr1 The first address.
283 * @param adr2 The second address.
285 static ir_alias_relation different_sel_offsets(ir_node *sel1, ir_node *sel2) {
286 /* seems to be broken */
290 ir_entity *ent1 = get_Sel_entity(sel1);
291 ir_entity *ent2 = get_Sel_entity(sel2);
292 int i, check_arr = 0;
297 ir_type *tp1 = get_entity_type(ent1);
298 ir_type *tp2 = get_entity_type(ent2);
302 else if (get_type_state(tp1) == layout_fixed && get_type_state(tp2) == layout_fixed &&
303 get_type_size_bits(tp1) == get_type_size_bits(tp2))
307 /* we select an entity of same size, check for indexes */
308 int n = get_Sel_n_indexs(sel1);
311 if (n > 0 && n == get_Sel_n_indexs(sel2)) {
312 /* same non-zero number of indexes, an array access, check */
313 for (i = 0; i < n; ++i) {
314 ir_node *idx1 = get_Sel_index(sel1, i);
315 ir_node *idx2 = get_Sel_index(sel2, i);
316 ir_alias_relation res = different_index(idx1, idx2, 0); /* we can safely IGNORE the size here if it's at least >0 */
318 if (res == may_alias)
320 else if (res == no_alias)
323 /* if we have at least one no_alias, there is no alias relation, else we have sure */
324 return have_no > 0 ? no_alias : sure_alias;
329 } /* different_sel_offsets */
332 * Determine the alias relation by checking if adr1 and adr2 are pointer
335 * @param adr1 The first address.
336 * @param adr2 The second address.
338 static ir_alias_relation different_types(ir_node *adr1, ir_node *adr2)
340 ir_entity *ent1 = NULL, *ent2 = NULL;
343 ent1 = get_Global_entity(adr1);
344 else if (is_Sel(adr1))
345 ent1 = get_Sel_entity(adr1);
348 ent2 = get_Global_entity(adr2);
349 else if (is_Sel(adr2))
350 ent2 = get_Sel_entity(adr2);
352 if (ent1 != NULL && ent2 != NULL) {
353 ir_type *tp1 = get_entity_type(ent1);
354 ir_type *tp2 = get_entity_type(ent2);
358 if (is_Pointer_type(tp1) && is_Pointer_type(tp2)) {
359 /* do deref until no pointer types are found */
361 tp1 = get_pointer_points_to_type(tp1);
362 tp2 = get_pointer_points_to_type(tp2);
363 } while (is_Pointer_type(tp1) && is_Pointer_type(tp2));
367 if (get_type_tpop(tp1) != get_type_tpop(tp2)) {
368 /* different type structure */
371 if (is_Class_type(tp1)) {
372 /* check class hierarchy */
373 if (! is_SubClass_of(tp1, tp2) &&
374 ! is_SubClass_of(tp2, tp1))
377 /* different types */
383 } /* different_types */
386 * Returns non-zero if a node is a result on a malloc-like routine.
388 * @param node the Proj node to test
390 static int is_malloc_Result(ir_node *node) {
391 node = get_Proj_pred(node);
394 node = get_Proj_pred(node);
397 node = get_Call_ptr(node);
398 if (is_Global(node)) {
399 ir_entity *ent = get_Global_entity(node);
401 if (get_entity_additional_properties(ent) & mtp_property_malloc)
406 } /* is_malloc_Result */
409 * Classify a base pointer.
411 * @param irg the graph of the pointer
412 * @param irn the node representing the base address
413 * @param ent the base entity of the base address iff any
415 ir_storage_class_class_t classify_pointer(ir_graph *irg, ir_node *irn, ir_entity *ent)
417 ir_storage_class_class_t res = ir_sc_pointer;
418 if (is_Global(irn)) {
419 ir_entity *entity = get_Global_entity(irn);
420 res = ir_sc_globalvar;
421 if (get_entity_address_taken(entity) == ir_address_not_taken)
422 res |= ir_sc_modifier_nottaken;
423 } else if (irn == get_irg_frame(irg)) {
424 res = ir_sc_localvar;
425 if (ent != NULL && get_entity_address_taken(ent) == ir_address_not_taken)
426 res |= ir_sc_modifier_nottaken;
427 } else if (is_arg_Proj(irn)) {
428 return ir_sc_argument;
429 } else if (irn == get_irg_tls(irg)) {
431 if (ent != NULL && get_entity_address_taken(ent) == ir_address_not_taken)
432 res |= ir_sc_modifier_nottaken;
433 } else if (is_Proj(irn) && is_malloc_Result(irn)) {
434 return ir_sc_malloced;
441 * If adr represents a Bitfield Sel, skip it
443 static ir_node *skip_Bitfield_Sels(ir_node *adr) {
445 ir_entity *ent = get_Sel_entity(adr);
446 ir_type *bf_type = get_entity_type(ent);
448 /* is it a bitfield type? */
449 if (is_Primitive_type(bf_type) && get_primitive_base_type(bf_type) != NULL)
450 adr = get_Sel_ptr(adr);
456 * Determine the alias relation between two addresses.
458 static ir_alias_relation _get_alias_relation(
460 ir_node *adr1, ir_mode *mode1,
461 ir_node *adr2, ir_mode *mode2)
463 ir_entity *ent1, *ent2;
469 ir_node *orig_adr1 = adr1;
470 ir_node *orig_adr2 = adr2;
472 ir_storage_class_class_t class1, class2;
473 int have_const_offsets;
475 if (! get_opt_alias_analysis())
479 return ir_sure_alias;
481 options = get_irg_memory_disambiguator_options(irg);
483 /* The Armageddon switch */
484 if (options & aa_opt_no_alias)
487 /* do the addresses have constants offsets?
488 * Note: nodes are normalized to have constants at right inputs,
489 * sub X, C is normalized to add X, -C
491 have_const_offsets = 1;
492 while (is_Add(adr1)) {
493 ir_node *add_right = get_Add_right(adr1);
494 if (is_Const(add_right)) {
495 tarval *tv = get_Const_tarval(add_right);
496 offset1 += get_tarval_long(tv);
497 adr1 = get_Add_left(adr1);
498 } else if (mode_is_reference(get_irn_mode(add_right))) {
500 have_const_offsets = 0;
502 adr1 = get_Add_left(adr1);
503 have_const_offsets = 0;
506 while (is_Add(adr2)) {
507 ir_node *add_right = get_Add_right(adr2);
508 if (is_Const(add_right)) {
509 tarval *tv = get_Const_tarval(add_right);
510 offset2 += get_tarval_long(tv);
511 adr2 = get_Add_left(adr2);
512 } else if (mode_is_reference(get_irn_mode(add_right))) {
514 have_const_offsets = 0;
516 adr2 = get_Add_left(adr2);
517 have_const_offsets = 0;
521 mode_size = get_mode_size_bytes(mode1);
522 if (get_mode_size_bytes(mode2) > mode_size) {
523 mode_size = get_mode_size_bytes(mode2);
526 /* same base address -> compare offsets if possible.
527 * FIXME: type long is not sufficient for this task ...
529 if (adr1 == adr2 && have_const_offsets) {
530 if ((unsigned long)labs(offset2 - offset1) >= mode_size)
533 return ir_sure_alias;
537 * Bitfields can be constructed as Sels from its base address.
538 * As they have different entities, the disambiguator would find that they are
539 * alias free. While this is true for it's values, it is false for the addresses
540 * (strictly speaking, the Sel's are NOT the addresses of the bitfields).
541 * So, skip those bitfield selecting Sel's.
543 adr1 = skip_Bitfield_Sels(adr1);
544 adr2 = skip_Bitfield_Sels(adr2);
552 base1 = find_base_adr(adr1, &ent1);
555 base2 = find_base_adr(adr2, &ent2);
558 /* same base address -> compare sel entities */
559 if (base1 == base2 && ent1 != NULL && ent2 != NULL) {
562 else if (have_const_offsets)
563 return different_sel_offsets(adr1, adr2);
566 /* Type based alias analysis */
567 if (options & aa_opt_type_based) {
568 ir_alias_relation rel;
570 if (options & aa_opt_byte_type_may_alias) {
571 if (get_mode_size_bits(mode1) == 8 || get_mode_size_bits(mode2) == 8) {
572 /* One of the modes address a byte. Assume a ir_may_alias and leave
573 the type based check. */
574 goto leave_type_based_alias;
577 /* cheap check: If the mode sizes did not match, the types MUST be different */
578 if (get_mode_size_bits(mode1) != get_mode_size_bits(mode2))
581 /* cheap test: if only one is a reference mode, no alias */
582 if (mode_is_reference(mode1) != mode_is_reference(mode2))
586 rel = different_types(orig_adr1, orig_adr2);
587 if (rel != ir_may_alias)
589 leave_type_based_alias:;
592 class1 = classify_pointer(irg, base1, ent1);
593 class2 = classify_pointer(irg, base2, ent2);
595 if (class1 == ir_sc_pointer) {
596 if (class2 & ir_sc_modifier_nottaken) {
601 } else if (class2 == ir_sc_pointer) {
602 if (class1 & ir_sc_modifier_nottaken) {
609 if (class1 != class2) {
613 if (class1 == ir_sc_globalvar) {
614 ir_entity *entity1 = get_SymConst_entity(base1);
615 ir_entity *entity2 = get_SymConst_entity(base2);
616 if (entity1 != entity2)
619 /* for some reason CSE didn't happen yet for the 2 SymConsts... */
623 /* do we have a language specific memory disambiguator? */
624 if (language_disambuigator) {
625 ir_alias_relation rel = (*language_disambuigator)(irg, orig_adr1, mode1, orig_adr2, mode2);
626 if (rel != ir_may_alias)
630 /* access points-to information here */
632 } /* _get_alias_relation */
635 * Determine the alias relation between two addresses.
637 ir_alias_relation get_alias_relation(
639 ir_node *adr1, ir_mode *mode1,
640 ir_node *adr2, ir_mode *mode2)
642 ir_alias_relation rel = _get_alias_relation(irg, adr1, mode1, adr2, mode2);
643 DB((dbg, LEVEL_1, "alias(%+F, %+F) = %s\n", adr1, adr2, get_ir_alias_relation_name(rel)));
645 } /* get_alias_relation */
647 /* Set a source language specific memory disambiguator function. */
648 void set_language_memory_disambiguator(DISAMBIGUATOR_FUNC func) {
649 language_disambuigator = func;
650 } /* set_language_memory_disambiguator */
652 /** The result cache for the memory disambiguator. */
653 static set *result_cache = NULL;
655 /** An entry in the relation cache. */
656 typedef struct mem_disambig_entry {
657 ir_node *adr1; /**< The first address. */
658 ir_node *adr2; /**< The second address. */
659 ir_alias_relation result; /**< The alias relation result. */
660 } mem_disambig_entry;
662 #define HASH_ENTRY(adr1, adr2) (HASH_PTR(adr1) ^ HASH_PTR(adr2))
665 * Compare two relation cache entries.
667 static int cmp_mem_disambig_entry(const void *elt, const void *key, size_t size) {
668 const mem_disambig_entry *p1 = elt;
669 const mem_disambig_entry *p2 = key;
672 return p1->adr1 == p2->adr1 && p1->adr2 == p2->adr2;
673 } /* cmp_mem_disambig_entry */
676 * Initialize the relation cache.
678 void mem_disambig_init(void) {
679 result_cache = new_set(cmp_mem_disambig_entry, 8);
680 } /* mem_disambig_init */
683 * Determine the alias relation between two addresses.
685 ir_alias_relation get_alias_relation_ex(
687 ir_node *adr1, ir_mode *mode1,
688 ir_node *adr2, ir_mode *mode2)
690 mem_disambig_entry key, *entry;
692 ir_fprintf(stderr, "%+F <-> %+F\n", adr1, adr2);
694 if (! get_opt_alias_analysis())
697 if (get_irn_opcode(adr1) > get_irn_opcode(adr2)) {
705 entry = set_find(result_cache, &key, sizeof(key), HASH_ENTRY(adr1, adr2));
707 return entry->result;
709 key.result = get_alias_relation(irg, adr1, mode1, adr2, mode2);
711 set_insert(result_cache, &key, sizeof(key), HASH_ENTRY(adr1, adr2));
713 } /* get_alias_relation_ex */
715 /* Free the relation cache. */
716 void mem_disambig_term(void) {
718 del_set(result_cache);
721 } /* mem_disambig_term */
724 * Check the mode of a Load/Store with the mode of the entity
726 * If the mode of the entity and the Load/Store mode do not match, we
727 * have the bad reinterpret case:
730 * char b = *(char *)&i;
732 * We do NOT count this as one value and return address_taken
734 * However, we support an often used case. If the mode is two-complement
735 * we allow casts between signed/unsigned.
737 * @param mode the mode of the Load/Store
738 * @param ent_mode the mode of the accessed entity
740 * @return non-zero if the Load/Store is a hidden cast, zero else
742 static int is_hidden_cast(ir_mode *mode, ir_mode *ent_mode) {
743 if (ent_mode != mode) {
744 if (ent_mode == NULL ||
745 get_mode_size_bits(ent_mode) != get_mode_size_bits(mode) ||
746 get_mode_sort(ent_mode) != get_mode_sort(mode) ||
747 get_mode_arithmetic(ent_mode) != irma_twos_complement ||
748 get_mode_arithmetic(mode) != irma_twos_complement)
752 } /* is_hidden_cast */
755 * Determine the address_taken state of a node (or it's successor Sels).
757 * @param irn the node
759 static ir_address_taken_state find_address_taken_state(ir_node *irn) {
761 ir_mode *emode, *mode;
766 for (i = get_irn_n_outs(irn) - 1; i >= 0; --i) {
767 ir_node *succ = get_irn_out(irn, i);
769 switch (get_irn_opcode(succ)) {
771 /* check if this load is not a hidden conversion */
772 mode = get_Load_mode(succ);
773 ent = is_SymConst(irn) ? get_SymConst_entity(irn) : get_Sel_entity(irn);
774 emode = get_type_mode(get_entity_type(ent));
775 if (is_hidden_cast(mode, emode))
776 return ir_address_taken;
780 /* check that the node is not the Store's value */
781 value = get_Store_value(succ);
783 return ir_address_taken;
784 /* check if this Store is not a hidden conversion */
785 mode = get_irn_mode(value);
786 ent = is_SymConst(irn) ? get_SymConst_entity(irn) : get_Sel_entity(irn);
787 emode = get_type_mode(get_entity_type(ent));
788 if (is_hidden_cast(mode, emode))
789 return ir_address_taken;
793 /* CopyB are like Loads/Stores */
794 ent = is_SymConst(irn) ? get_SymConst_entity(irn) : get_Sel_entity(irn);
795 tp = get_entity_type(ent);
796 if (tp != get_CopyB_type(succ)) {
797 /* bad, different types, might be a hidden conversion */
798 return ir_address_taken;
803 /* Check the successor of irn. */
804 ir_address_taken_state res = find_address_taken_state(succ);
805 if (res != ir_address_not_taken)
811 /* Only the call address is not an address taker but
812 this is an uninteresting case, so we ignore it here. */
813 for (j = get_Call_n_params(succ) - 1; j >= 0; --j) {
814 ir_node *param = get_Call_param(succ, j);
816 return ir_address_taken;
821 /* another op, the address may be taken */
822 return ir_address_taken_unknown;
825 /* All successors finished, the address is not taken. */
826 return ir_address_not_taken;
827 } /* find_address_taken_state */
830 * Update the "address taken" flag of all frame entities.
832 static void analyse_irg_address_taken(ir_graph *irg) {
833 ir_type *ft = get_irg_frame_type(irg);
837 /* set initial state to not_taken, as this is the "smallest" state */
838 for (i = get_class_n_members(ft) - 1; i >= 0; --i) {
839 ir_entity *ent = get_class_member(ft, i);
841 set_entity_address_taken(ent, ir_address_not_taken);
844 assure_irg_outs(irg);
846 irg_frame = get_irg_frame(irg);
848 for (i = get_irn_n_outs(irg_frame) - 1; i >= 0; --i) {
849 ir_node *succ = get_irn_out(irg_frame, i);
850 ir_address_taken_state state;
853 ir_entity *ent = get_Sel_entity(succ);
855 if (get_entity_address_taken(ent) == ir_address_taken)
858 state = find_address_taken_state(succ);
859 if (state > get_entity_address_taken(ent))
860 set_entity_address_taken(ent, state);
864 irg->adr_taken_state = ir_address_taken_computed;
865 } /* analyse_address_taken */
867 /* Returns the current address taken state of the graph. */
868 ir_address_taken_computed_state get_irg_address_taken_state(const ir_graph *irg) {
869 return irg->adr_taken_state;
870 } /* get_irg_address_taken_state */
872 /* Sets the current address taken state of the graph. */
873 void set_irg_address_taken_state(ir_graph *irg, ir_address_taken_computed_state state) {
874 irg->adr_taken_state = state;
875 } /* set_irg_address_taken_state */
877 /* Assure that the address taken flag is computed for the given graph. */
878 void assure_irg_address_taken_computed(ir_graph *irg) {
879 if (irg->adr_taken_state == ir_address_taken_not_computed)
880 analyse_irg_address_taken(irg);
881 } /* assure_irg_address_taken_computed */
885 * Initialize the address_taken flag for a global type like type.
887 static void init_taken_flag(ir_type * tp) {
890 /* All external visible entities are at least
891 ir_address_taken_unknown. This is very conservative. */
892 for (i = get_compound_n_members(tp) - 1; i >= 0; --i) {
893 ir_entity *ent = get_compound_member(tp, i);
894 ir_address_taken_state state;
896 state = get_entity_visibility(ent) == visibility_external_visible ?
897 ir_address_taken_unknown : ir_address_not_taken ;
898 set_entity_address_taken(ent, state);
900 } /* init_taken_flag */
902 static void check_initializer_nodes(ir_initializer_t *initializer)
904 switch (initializer->kind) {
905 case IR_INITIALIZER_CONST: {
906 ir_node *n = initializer->consti.value;
908 /* let's check if it's an address */
910 ir_entity *ent = get_Global_entity(n);
911 set_entity_address_taken(ent, ir_address_taken);
915 case IR_INITIALIZER_TARVAL:
916 case IR_INITIALIZER_NULL:
918 case IR_INITIALIZER_COMPOUND: {
921 for (i = 0; i < initializer->compound.n_initializers; ++i) {
922 ir_initializer_t *sub_initializer
923 = initializer->compound.initializers[i];
924 check_initializer_nodes(sub_initializer);
929 panic("invalid initialzier found");
933 * Mark all entities used in the initializer for the given entity as address taken
935 static void check_initializer(ir_entity *ent) {
939 /* do not check uninitialized values */
940 if (get_entity_variability(ent) == variability_uninitialized)
943 /* Beware: Methods initialized with "themself". This does not count as a taken
945 if (is_Method_type(get_entity_type(ent)))
948 if (ent->has_initializer) {
949 check_initializer_nodes(ent->attr.initializer);
950 } else if (is_atomic_entity(ent)) {
951 /* let's check if it's an address */
952 n = get_atomic_ent_value(ent);
954 ir_entity *ent = get_Global_entity(n);
955 set_entity_address_taken(ent, ir_address_taken);
958 for (i = get_compound_ent_n_values(ent) - 1; i >= 0; --i) {
959 n = get_compound_ent_value(ent, i);
961 /* let's check if it's an address */
963 ir_entity *ent = get_Global_entity(n);
964 set_entity_address_taken(ent, ir_address_taken);
968 } /* check_initializer */
972 * Mark all entities used in initializers as address taken
974 static void check_initializers(ir_type *tp) {
977 for (i = get_compound_n_members(tp) - 1; i >= 0; --i) {
978 ir_entity *ent = get_compound_member(tp, i);
980 check_initializer(ent);
982 } /* check_initializers */
986 * Print the address taken state of all entities of a given type for debugging.
988 static void print_address_taken_state(ir_type *tp) {
990 for (i = get_compound_n_members(tp) - 1; i >= 0; --i) {
991 ir_entity *ent = get_compound_member(tp, i);
992 ir_address_taken_state state = get_entity_address_taken(ent);
994 if (state != ir_address_not_taken) {
995 assert(ir_address_not_taken <= (int) state && state <= ir_address_taken);
996 ir_printf("%+F: %s\n", ent, get_address_taken_state_name(state));
999 } /* print_address_taken_state */
1000 #endif /* DEBUG_libfirm */
1003 * Post-walker: check for global entity address
1005 static void check_global_address(ir_node *irn, void *env) {
1008 ir_address_taken_state state;
1010 if (is_Global(irn)) {
1012 ent = get_Global_entity(irn);
1013 } else if (is_Sel(irn) && get_Sel_ptr(irn) == tls) {
1014 /* A TLS variable. */
1015 ent = get_Sel_entity(irn);
1019 if (get_entity_address_taken(ent) >= ir_address_taken) {
1020 /* Already at the maximum. */
1023 state = find_address_taken_state(irn);
1024 if (state > get_entity_address_taken(ent))
1025 set_entity_address_taken(ent, state);
1026 } /* check_global_address */
1029 * Update the "address taken" flag of all global entities.
1031 static void analyse_irp_globals_address_taken(void) {
1034 FIRM_DBG_REGISTER(dbg, "firm.ana.irmemory");
1036 init_taken_flag(get_glob_type());
1037 init_taken_flag(get_tls_type());
1039 check_initializers(get_glob_type());
1040 check_initializers(get_tls_type());
1042 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
1043 ir_graph *irg = get_irp_irg(i);
1045 assure_irg_outs(irg);
1046 irg_walk_graph(irg, NULL, check_global_address, get_irg_tls(irg));
1049 #ifdef DEBUG_libfirm
1050 if (firm_dbg_get_mask(dbg) & LEVEL_1) {
1051 print_address_taken_state(get_glob_type());
1052 print_address_taken_state(get_tls_type());
1054 #endif /* DEBUG_libfirm */
1057 irp->globals_adr_taken_state = ir_address_taken_computed;
1058 } /* analyse_irp_globals_address_taken */
1060 /* Returns the current address taken state of the globals. */
1061 ir_address_taken_computed_state get_irp_globals_address_taken_state(void) {
1062 return irp->globals_adr_taken_state;
1063 } /* get_irp_globals_address_taken_state */
1065 /* Sets the current address taken state of the graph. */
1066 void set_irp_globals_address_taken_state(ir_address_taken_computed_state state) {
1067 irp->globals_adr_taken_state = state;
1068 } /* set_irg_address_taken_state */
1070 /* Assure that the address taken flag is computed for the globals. */
1071 void assure_irp_globals_address_taken_computed(void) {
1072 if (irp->globals_adr_taken_state == ir_address_taken_not_computed)
1073 analyse_irp_globals_address_taken();
1074 } /* assure_irp_globals_address_taken_computed */
1077 #include <adt/pmap.h>
1078 #include "typerep.h"
1080 DEBUG_ONLY(static firm_dbg_module_t *dbgcall = NULL;)
1082 /** Maps method types to cloned method types. */
1083 static pmap *mtp_map;
1086 * Clone a method type if not already cloned.
1088 static ir_type *clone_type_and_cache(ir_type *tp) {
1089 static ident *prefix = NULL;
1091 pmap_entry *e = pmap_find(mtp_map, tp);
1097 prefix = new_id_from_chars("C", 1);
1099 res = clone_type_method(tp, prefix);
1100 pmap_insert(mtp_map, tp, res);
1101 DB((dbgcall, LEVEL_2, "cloned type %+F into %+F\n", tp, res));
1104 } /* clone_type_and_cache */
1107 * Copy the calling conventions from the entities to the call type.
1109 static void update_calls_to_private(ir_node *call, void *env) {
1111 if (is_Call(call)) {
1112 ir_node *ptr = get_Call_ptr(call);
1114 if (is_SymConst(ptr)) {
1115 ir_entity *ent = get_SymConst_entity(ptr);
1116 ir_type *ctp = get_Call_type(call);
1118 if (get_entity_additional_properties(ent) & mtp_property_private) {
1119 if ((get_method_additional_properties(ctp) & mtp_property_private) == 0) {
1120 ctp = clone_type_and_cache(ctp);
1121 set_method_additional_property(ctp, mtp_property_private);
1122 set_Call_type(call, ctp);
1123 DB((dbgcall, LEVEL_1, "changed call to private method %+F\n", ent));
1128 } /* update_calls_to_private */
1130 /* Mark all private methods, i.e. those of which all call sites are known. */
1131 void mark_private_methods(void) {
1135 FIRM_DBG_REGISTER(dbgcall, "firm.opt.cc");
1137 assure_irp_globals_address_taken_computed();
1139 mtp_map = pmap_create();
1141 /* first step: change the calling conventions of the local non-escaped entities */
1142 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
1143 ir_graph *irg = get_irp_irg(i);
1144 ir_entity *ent = get_irg_entity(irg);
1145 ir_address_taken_state state = get_entity_address_taken(ent);
1147 /* If an entity is sticky, it might be called from external
1148 places (like inline assembler), so do NOT mark it as private. */
1149 if (get_entity_visibility(ent) == visibility_local &&
1150 state == ir_address_not_taken &&
1151 get_entity_stickyness(ent) != stickyness_sticky) {
1152 ir_type *mtp = get_entity_type(ent);
1154 set_entity_additional_property(ent, mtp_property_private);
1155 DB((dbgcall, LEVEL_1, "found private method %+F\n", ent));
1156 if ((get_method_additional_properties(mtp) & mtp_property_private) == 0) {
1157 /* need a new type */
1158 mtp = clone_type_and_cache(mtp);
1159 set_entity_type(ent, mtp);
1160 set_method_additional_property(mtp, mtp_property_private);
1167 all_irg_walk(NULL, update_calls_to_private, NULL);
1169 pmap_destroy(mtp_map);
1170 } /* mark_private_methods */