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);
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);
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);
357 if (is_Pointer_type(tp1) && is_Pointer_type(tp2)) {
358 /* do deref until no pointer types are found */
360 tp1 = get_pointer_points_to_type(tp1);
361 tp2 = get_pointer_points_to_type(tp2);
362 } while (is_Pointer_type(tp1) && is_Pointer_type(tp2));
365 if (get_type_tpop(tp1) != get_type_tpop(tp2)) {
366 /* different type structure */
369 if (is_Class_type(tp1)) {
370 /* check class hierarchy */
371 if (! is_SubClass_of(tp1, tp2) &&
372 ! is_SubClass_of(tp2, tp1))
375 /* different types */
381 } /* different_types */
384 * Returns non-zero if a node is a routine parameter.
386 * @param node the Proj node to test
388 static int is_arg_Proj(ir_node *node) {
391 node = get_Proj_pred(node);
394 return pn_Start_T_args == get_Proj_proj(node) && is_Start(get_Proj_pred(node));
398 * Returns non-zero if a node is a result on a malloc-like routine.
400 * @param node the Proj node to test
402 static int is_malloc_Result(ir_node *node) {
403 node = get_Proj_pred(node);
406 node = get_Proj_pred(node);
409 node = get_Call_ptr(node);
410 if (is_Global(node)) {
411 ir_entity *ent = get_Global_entity(node);
413 if (get_entity_additional_properties(ent) & mtp_property_malloc)
418 } /* is_malloc_Result */
421 * Classify a base pointer.
423 * @param irg the graph of the pointer
424 * @param irn the node representing the base address
425 * @param ent the base entity of the base address iff any
427 ir_storage_class_class_t classify_pointer(ir_graph *irg, ir_node *irn, ir_entity *ent)
429 ir_storage_class_class_t res = ir_sc_pointer;
430 if (is_Global(irn)) {
431 ir_entity *entity = get_Global_entity(irn);
432 res = ir_sc_globalvar;
433 if (get_entity_address_taken(entity) == ir_address_not_taken)
434 res |= ir_sc_modifier_nottaken;
435 } else if (irn == get_irg_frame(irg)) {
436 res = ir_sc_localvar;
437 if (ent != NULL && get_entity_address_taken(ent) == ir_address_not_taken)
438 res |= ir_sc_modifier_nottaken;
439 } else if (is_arg_Proj(irn)) {
440 return ir_sc_argument;
441 } else if (irn == get_irg_tls(irg)) {
443 if (ent != NULL && get_entity_address_taken(ent) == ir_address_not_taken)
444 res |= ir_sc_modifier_nottaken;
445 } else if (is_Proj(irn) && is_malloc_Result(irn)) {
446 return ir_sc_malloced;
453 * If adr represents a Bitfield Sel, skip it
455 static ir_node *skip_Bitfield_Sels(ir_node *adr) {
457 ir_entity *ent = get_Sel_entity(adr);
458 ir_type *bf_type = get_entity_type(ent);
460 /* is it a bitfield type? */
461 if (is_Primitive_type(bf_type) && get_primitive_base_type(bf_type) != NULL)
462 adr = get_Sel_ptr(adr);
468 * Determine the alias relation between two addresses.
470 static ir_alias_relation _get_alias_relation(
472 ir_node *adr1, ir_mode *mode1,
473 ir_node *adr2, ir_mode *mode2)
475 ir_entity *ent1, *ent2;
481 ir_node *orig_adr1 = adr1;
482 ir_node *orig_adr2 = adr2;
484 ir_storage_class_class_t class1, class2;
485 int have_const_offsets;
487 if (! get_opt_alias_analysis())
491 return ir_sure_alias;
493 options = get_irg_memory_disambiguator_options(irg);
495 /* The Armageddon switch */
496 if (options & aa_opt_no_alias)
499 /* do the addresses have constants offsets?
500 * Note: nodes are normalized to have constants at right inputs,
501 * sub X, C is normalized to add X, -C
503 have_const_offsets = 1;
504 while (is_Add(adr1)) {
505 ir_node *add_right = get_Add_right(adr1);
506 if (is_Const(add_right)) {
507 tarval *tv = get_Const_tarval(add_right);
508 offset1 += get_tarval_long(tv);
509 adr1 = get_Add_left(adr1);
510 } else if (mode_is_reference(get_irn_mode(add_right))) {
512 have_const_offsets = 0;
514 adr1 = get_Add_left(adr1);
515 have_const_offsets = 0;
518 while (is_Add(adr2)) {
519 ir_node *add_right = get_Add_right(adr2);
520 if (is_Const(add_right)) {
521 tarval *tv = get_Const_tarval(add_right);
522 offset2 += get_tarval_long(tv);
523 adr2 = get_Add_left(adr2);
524 } else if (mode_is_reference(get_irn_mode(add_right))) {
526 have_const_offsets = 0;
528 adr2 = get_Add_left(adr2);
529 have_const_offsets = 0;
533 mode_size = get_mode_size_bytes(mode1);
534 if (get_mode_size_bytes(mode2) > mode_size) {
535 mode_size = get_mode_size_bytes(mode2);
538 /* same base address -> compare offsets if possible.
539 * FIXME: type long is not sufficient for this task ...
541 if (adr1 == adr2 && have_const_offsets) {
542 if ((unsigned long)labs(offset2 - offset1) >= mode_size)
545 return ir_sure_alias;
549 * Bitfields can be constructed as Sels from its base address.
550 * As they have different entities, the disambiguator would find that they are
551 * alias free. While this is true for it's values, it is false for the addresses
552 * (strictly speaking, the Sel's are NOT the addresses of the bitfields).
553 * So, skip those bitfield selecting Sel's.
555 adr1 = skip_Bitfield_Sels(adr1);
556 adr2 = skip_Bitfield_Sels(adr2);
564 base1 = find_base_adr(adr1, &ent1);
567 base2 = find_base_adr(adr2, &ent2);
570 /* same base address -> compare sel entities */
571 if (base1 == base2 && ent1 != NULL && ent2 != NULL) {
574 else if (have_const_offsets)
575 return different_sel_offsets(adr1, adr2);
578 class1 = classify_pointer(irg, base1, ent1);
579 class2 = classify_pointer(irg, base2, ent2);
581 if (class1 == ir_sc_pointer) {
582 if (class2 & ir_sc_modifier_nottaken) {
587 } else if (class2 == ir_sc_pointer) {
588 if (class1 & ir_sc_modifier_nottaken) {
595 if (class1 != class2) {
599 if (class1 == ir_sc_globalvar) {
600 ir_entity *entity1 = get_SymConst_entity(base1);
601 ir_entity *entity2 = get_SymConst_entity(base2);
602 if (entity1 != entity2)
605 /* for some reason CSE didn't happen yet for the 2 SymConsts... */
609 /* Type based alias analysis */
610 if (options & aa_opt_type_based) {
611 ir_alias_relation rel;
613 if (options & aa_opt_byte_type_may_alias) {
614 if (get_mode_size_bits(mode1) == 8 || get_mode_size_bits(mode2) == 8) {
615 /* One of the modes address a byte. Assume a ir_may_alias and leave
616 the type based check. */
617 goto leave_type_based_alias;
620 /* cheap check: If the mode sizes did not match, the types MUST be different */
621 if (get_mode_size_bits(mode1) != get_mode_size_bits(mode2))
624 /* cheap test: if only one is a reference mode, no alias */
625 if (mode_is_reference(mode1) != mode_is_reference(mode2))
629 rel = different_types(orig_adr1, orig_adr2);
630 if (rel != ir_may_alias)
632 leave_type_based_alias:;
635 /* do we have a language specific memory disambiguator? */
636 if (language_disambuigator) {
637 ir_alias_relation rel = (*language_disambuigator)(irg, orig_adr1, mode1, orig_adr2, mode2);
638 if (rel != ir_may_alias)
642 /* access points-to information here */
644 } /* _get_alias_relation */
647 * Determine the alias relation between two addresses.
649 ir_alias_relation get_alias_relation(
651 ir_node *adr1, ir_mode *mode1,
652 ir_node *adr2, ir_mode *mode2)
654 ir_alias_relation rel = _get_alias_relation(irg, adr1, mode1, adr2, mode2);
655 DB((dbg, LEVEL_1, "alias(%+F, %+F) = %s\n", adr1, adr2, get_ir_alias_relation_name(rel)));
657 } /* get_alias_relation */
659 /* Set a source language specific memory disambiguator function. */
660 void set_language_memory_disambiguator(DISAMBIGUATOR_FUNC func) {
661 language_disambuigator = func;
662 } /* set_language_memory_disambiguator */
664 /** The result cache for the memory disambiguator. */
665 static set *result_cache = NULL;
667 /** An entry in the relation cache. */
668 typedef struct mem_disambig_entry {
669 ir_node *adr1; /**< The first address. */
670 ir_node *adr2; /**< The second address. */
671 ir_alias_relation result; /**< The alias relation result. */
672 } mem_disambig_entry;
674 #define HASH_ENTRY(adr1, adr2) (HASH_PTR(adr1) ^ HASH_PTR(adr2))
677 * Compare two relation cache entries.
679 static int cmp_mem_disambig_entry(const void *elt, const void *key, size_t size) {
680 const mem_disambig_entry *p1 = elt;
681 const mem_disambig_entry *p2 = key;
684 return p1->adr1 == p2->adr1 && p1->adr2 == p2->adr2;
685 } /* cmp_mem_disambig_entry */
688 * Initialize the relation cache.
690 void mem_disambig_init(void) {
691 result_cache = new_set(cmp_mem_disambig_entry, 8);
692 } /* mem_disambig_init */
695 * Determine the alias relation between two addresses.
697 ir_alias_relation get_alias_relation_ex(
699 ir_node *adr1, ir_mode *mode1,
700 ir_node *adr2, ir_mode *mode2)
702 mem_disambig_entry key, *entry;
704 ir_fprintf(stderr, "%+F <-> %+F\n", adr1, adr2);
706 if (! get_opt_alias_analysis())
709 if (get_irn_opcode(adr1) > get_irn_opcode(adr2)) {
717 entry = set_find(result_cache, &key, sizeof(key), HASH_ENTRY(adr1, adr2));
719 return entry->result;
721 key.result = get_alias_relation(irg, adr1, mode1, adr2, mode2);
723 set_insert(result_cache, &key, sizeof(key), HASH_ENTRY(adr1, adr2));
725 } /* get_alias_relation_ex */
727 /* Free the relation cache. */
728 void mem_disambig_term(void) {
730 del_set(result_cache);
733 } /* mem_disambig_term */
736 * Check the mode of a Load/Store with the mode of the entity
738 * If the mode of the entity and the Load/Store mode do not match, we
739 * have the bad reinterpret case:
742 * char b = *(char *)&i;
744 * We do NOT count this as one value and return address_taken
746 * However, we support an often used case. If the mode is two-complement
747 * we allow casts between signed/unsigned.
749 * @param mode the mode of the Load/Store
750 * @param ent_mode the mode of the accessed entity
752 * @return non-zero if the Load/Store is a hidden cast, zero else
754 static int is_hidden_cast(ir_mode *mode, ir_mode *ent_mode) {
755 if (ent_mode != mode) {
756 if (ent_mode == NULL ||
757 get_mode_size_bits(ent_mode) != get_mode_size_bits(mode) ||
758 get_mode_sort(ent_mode) != get_mode_sort(mode) ||
759 get_mode_arithmetic(ent_mode) != irma_twos_complement ||
760 get_mode_arithmetic(mode) != irma_twos_complement)
764 } /* is_hidden_cast */
767 * Determine the address_taken state of a node (or it's successor Sels).
769 * @param irn the node
771 static ir_address_taken_state find_address_taken_state(ir_node *irn) {
773 ir_mode *emode, *mode;
778 for (i = get_irn_n_outs(irn) - 1; i >= 0; --i) {
779 ir_node *succ = get_irn_out(irn, i);
781 switch (get_irn_opcode(succ)) {
783 /* check if this load is not a hidden conversion */
784 mode = get_Load_mode(succ);
785 ent = is_SymConst(irn) ? get_SymConst_entity(irn) : get_Sel_entity(irn);
786 emode = get_type_mode(get_entity_type(ent));
787 if (is_hidden_cast(mode, emode))
788 return ir_address_taken;
792 /* check that the node is not the Store's value */
793 value = get_Store_value(succ);
795 return ir_address_taken;
796 /* check if this Store is not a hidden conversion */
797 mode = get_irn_mode(value);
798 ent = is_SymConst(irn) ? get_SymConst_entity(irn) : get_Sel_entity(irn);
799 emode = get_type_mode(get_entity_type(ent));
800 if (is_hidden_cast(mode, emode))
801 return ir_address_taken;
805 /* CopyB are like Loads/Stores */
806 ent = is_SymConst(irn) ? get_SymConst_entity(irn) : get_Sel_entity(irn);
807 tp = get_entity_type(ent);
808 if (tp != get_CopyB_type(succ)) {
809 /* bad, different types, might be a hidden conversion */
810 return ir_address_taken;
815 /* Check the successor of irn. */
816 ir_address_taken_state res = find_address_taken_state(succ);
817 if (res != ir_address_not_taken)
823 /* Only the call address is not an address taker but
824 this is an uninteresting case, so we ignore it here. */
825 for (j = get_Call_n_params(succ) - 1; j >= 0; --j) {
826 ir_node *param = get_Call_param(succ, j);
828 return ir_address_taken;
833 /* another op, the address may be taken */
834 return ir_address_taken_unknown;
837 /* All successors finished, the address is not taken. */
838 return ir_address_not_taken;
839 } /* find_address_taken_state */
842 * Update the "address taken" flag of all frame entities.
844 static void analyse_irg_address_taken(ir_graph *irg) {
845 ir_type *ft = get_irg_frame_type(irg);
849 /* set initial state to not_taken, as this is the "smallest" state */
850 for (i = get_class_n_members(ft) - 1; i >= 0; --i) {
851 ir_entity *ent = get_class_member(ft, i);
853 set_entity_address_taken(ent, ir_address_not_taken);
856 assure_irg_outs(irg);
858 irg_frame = get_irg_frame(irg);
860 for (i = get_irn_n_outs(irg_frame) - 1; i >= 0; --i) {
861 ir_node *succ = get_irn_out(irg_frame, i);
862 ir_address_taken_state state;
865 ir_entity *ent = get_Sel_entity(succ);
867 if (get_entity_address_taken(ent) == ir_address_taken)
870 state = find_address_taken_state(succ);
871 if (state > get_entity_address_taken(ent))
872 set_entity_address_taken(ent, state);
876 irg->adr_taken_state = ir_address_taken_computed;
877 } /* analyse_address_taken */
879 /* Returns the current address taken state of the graph. */
880 ir_address_taken_computed_state get_irg_address_taken_state(const ir_graph *irg) {
881 return irg->adr_taken_state;
882 } /* get_irg_address_taken_state */
884 /* Sets the current address taken state of the graph. */
885 void set_irg_address_taken_state(ir_graph *irg, ir_address_taken_computed_state state) {
886 irg->adr_taken_state = state;
887 } /* set_irg_address_taken_state */
889 /* Assure that the address taken flag is computed for the given graph. */
890 void assure_irg_address_taken_computed(ir_graph *irg) {
891 if (irg->adr_taken_state == ir_address_taken_not_computed)
892 analyse_irg_address_taken(irg);
893 } /* assure_irg_address_taken_computed */
897 * Initialize the address_taken flag for a global type like type.
899 static void init_taken_flag(ir_type * tp) {
902 /* All external visible entities are at least
903 ir_address_taken_unknown. This is very conservative. */
904 for (i = get_compound_n_members(tp) - 1; i >= 0; --i) {
905 ir_entity *ent = get_compound_member(tp, i);
906 ir_address_taken_state state;
908 state = get_entity_visibility(ent) == visibility_external_visible ?
909 ir_address_taken_unknown : ir_address_not_taken ;
910 set_entity_address_taken(ent, state);
912 } /* init_taken_flag */
914 static void check_initializer_nodes(ir_initializer_t *initializer)
916 switch (initializer->kind) {
917 case IR_INITIALIZER_CONST: {
918 ir_node *n = initializer->consti.value;
920 /* let's check if it's an address */
922 ir_entity *ent = get_Global_entity(n);
923 set_entity_address_taken(ent, ir_address_taken);
927 case IR_INITIALIZER_TARVAL:
928 case IR_INITIALIZER_NULL:
930 case IR_INITIALIZER_COMPOUND: {
933 for (i = 0; i < initializer->compound.n_initializers; ++i) {
934 ir_initializer_t *sub_initializer
935 = initializer->compound.initializers[i];
936 check_initializer_nodes(sub_initializer);
941 panic("invalid initialzier found");
945 * Mark all entities used in the initializer for the given entity as address taken
947 static void check_initializer(ir_entity *ent) {
951 /* do not check uninitialized values */
952 if (get_entity_variability(ent) == variability_uninitialized)
955 /* Beware: Methods initialized with "themself". This does not count as a taken
957 if (is_Method_type(get_entity_type(ent)))
960 if (ent->has_initializer) {
961 check_initializer_nodes(ent->attr.initializer);
962 } else if (is_atomic_entity(ent)) {
963 /* let's check if it's an address */
964 n = get_atomic_ent_value(ent);
966 ir_entity *ent = get_Global_entity(n);
967 set_entity_address_taken(ent, ir_address_taken);
970 for (i = get_compound_ent_n_values(ent) - 1; i >= 0; --i) {
971 n = get_compound_ent_value(ent, i);
973 /* let's check if it's an address */
975 ir_entity *ent = get_Global_entity(n);
976 set_entity_address_taken(ent, ir_address_taken);
980 } /* check_initializer */
984 * Mark all entities used in initializers as address taken
986 static void check_initializers(ir_type *tp) {
989 for (i = get_compound_n_members(tp) - 1; i >= 0; --i) {
990 ir_entity *ent = get_compound_member(tp, i);
992 check_initializer(ent);
994 } /* check_initializers */
998 * Print the address taken state of all entities of a given type for debugging.
1000 static void print_address_taken_state(ir_type *tp) {
1002 for (i = get_compound_n_members(tp) - 1; i >= 0; --i) {
1003 ir_entity *ent = get_compound_member(tp, i);
1004 ir_address_taken_state state = get_entity_address_taken(ent);
1006 if (state != ir_address_not_taken) {
1007 assert(ir_address_not_taken <= (int) state && state <= ir_address_taken);
1008 ir_printf("%+F: %s\n", ent, get_address_taken_state_name(state));
1011 } /* print_address_taken_state */
1012 #endif /* DEBUG_libfirm */
1015 * Post-walker: check for global entity address
1017 static void check_global_address(ir_node *irn, void *env) {
1020 ir_address_taken_state state;
1022 if (is_Global(irn)) {
1024 ent = get_Global_entity(irn);
1025 } else if (is_Sel(irn) && get_Sel_ptr(irn) == tls) {
1026 /* A TLS variable. */
1027 ent = get_Sel_entity(irn);
1031 if (get_entity_address_taken(ent) >= ir_address_taken) {
1032 /* Already at the maximum. */
1035 state = find_address_taken_state(irn);
1036 if (state > get_entity_address_taken(ent))
1037 set_entity_address_taken(ent, state);
1038 } /* check_global_address */
1041 * Update the "address taken" flag of all global entities.
1043 static void analyse_irp_globals_address_taken(void) {
1046 FIRM_DBG_REGISTER(dbg, "firm.ana.irmemory");
1048 init_taken_flag(get_glob_type());
1049 init_taken_flag(get_tls_type());
1051 check_initializers(get_glob_type());
1052 check_initializers(get_tls_type());
1054 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
1055 ir_graph *irg = get_irp_irg(i);
1057 assure_irg_outs(irg);
1058 irg_walk_graph(irg, NULL, check_global_address, get_irg_tls(irg));
1061 #ifdef DEBUG_libfirm
1062 if (firm_dbg_get_mask(dbg) & LEVEL_1) {
1063 print_address_taken_state(get_glob_type());
1064 print_address_taken_state(get_tls_type());
1066 #endif /* DEBUG_libfirm */
1069 irp->globals_adr_taken_state = ir_address_taken_computed;
1070 } /* analyse_irp_globals_address_taken */
1072 /* Returns the current address taken state of the globals. */
1073 ir_address_taken_computed_state get_irp_globals_address_taken_state(void) {
1074 return irp->globals_adr_taken_state;
1075 } /* get_irp_globals_address_taken_state */
1077 /* Sets the current address taken state of the graph. */
1078 void set_irp_globals_address_taken_state(ir_address_taken_computed_state state) {
1079 irp->globals_adr_taken_state = state;
1080 } /* set_irg_address_taken_state */
1082 /* Assure that the address taken flag is computed for the globals. */
1083 void assure_irp_globals_address_taken_computed(void) {
1084 if (irp->globals_adr_taken_state == ir_address_taken_not_computed)
1085 analyse_irp_globals_address_taken();
1086 } /* assure_irp_globals_address_taken_computed */
1089 #include <adt/pmap.h>
1090 #include "typerep.h"
1092 DEBUG_ONLY(static firm_dbg_module_t *dbgcall = NULL;)
1094 /** Maps method types to cloned method types. */
1095 static pmap *mtp_map;
1098 * Clone a method type if not already cloned.
1100 static ir_type *clone_type_and_cache(ir_type *tp) {
1101 static ident *prefix = NULL;
1103 pmap_entry *e = pmap_find(mtp_map, tp);
1109 prefix = new_id_from_chars("C", 1);
1111 res = clone_type_method(tp, prefix);
1112 pmap_insert(mtp_map, tp, res);
1113 DB((dbgcall, LEVEL_2, "cloned type %+F into %+F\n", tp, res));
1116 } /* clone_type_and_cache */
1119 * Copy the calling conventions from the entities to the call type.
1121 static void update_calls_to_private(ir_node *call, void *env) {
1123 if (is_Call(call)) {
1124 ir_node *ptr = get_Call_ptr(call);
1126 if (is_SymConst(ptr)) {
1127 ir_entity *ent = get_SymConst_entity(ptr);
1128 ir_type *ctp = get_Call_type(call);
1130 if (get_entity_additional_properties(ent) & mtp_property_private) {
1131 if ((get_method_additional_properties(ctp) & mtp_property_private) == 0) {
1132 ctp = clone_type_and_cache(ctp);
1133 set_method_additional_property(ctp, mtp_property_private);
1134 set_Call_type(call, ctp);
1135 DB((dbgcall, LEVEL_1, "changed call to private method %+F\n", ent));
1140 } /* update_calls_to_private */
1142 /* Mark all private methods, i.e. those of which all call sites are known. */
1143 void mark_private_methods(void) {
1147 FIRM_DBG_REGISTER(dbgcall, "firm.opt.cc");
1149 assure_irp_globals_address_taken_computed();
1151 mtp_map = pmap_create();
1153 /* first step: change the calling conventions of the local non-escaped entities */
1154 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
1155 ir_graph *irg = get_irp_irg(i);
1156 ir_entity *ent = get_irg_entity(irg);
1157 ir_address_taken_state state = get_entity_address_taken(ent);
1159 if (get_entity_visibility(ent) == visibility_local &&
1160 state == ir_address_not_taken) {
1161 ir_type *mtp = get_entity_type(ent);
1163 set_entity_additional_property(ent, mtp_property_private);
1164 DB((dbgcall, LEVEL_1, "found private method %+F\n", ent));
1165 if ((get_method_additional_properties(mtp) & mtp_property_private) == 0) {
1166 /* need a new type */
1167 mtp = clone_type_and_cache(mtp);
1168 set_entity_type(ent, mtp);
1169 set_method_additional_property(mtp, mtp_property_private);
1176 all_irg_walk(NULL, update_calls_to_private, NULL);
1178 pmap_destroy(mtp_map);
1179 } /* mark_private_methods */