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 no_alias if the Const is greater, 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) ? may_alias : 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) ? no_alias : may_alias;
120 * Treat idx1 and idx2 as integer indexes and check if they differ always more than size.
122 * @return sure_alias iff idx1 == idx2
123 * no_alias iff they ALWAYS differ more than size
126 static ir_alias_relation different_index(ir_node *idx1, ir_node *idx2, int size) {
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 ? sure_alias : 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) ? no_alias : 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) ? no_alias : 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 different.
281 * @param adr1 The first address.
282 * @param adr2 The second address.
284 static ir_alias_relation different_sel_offsets(ir_node *sel1, ir_node *sel2) {
285 /* seems to be broken */
289 ir_entity *ent1 = get_Sel_entity(sel1);
290 ir_entity *ent2 = get_Sel_entity(sel2);
291 int i, check_arr = 0;
296 ir_type *tp1 = get_entity_type(ent1);
297 ir_type *tp2 = get_entity_type(ent2);
301 else if (get_type_state(tp1) == layout_fixed && get_type_state(tp2) == layout_fixed &&
302 get_type_size_bits(tp1) == get_type_size_bits(tp2))
306 /* we select an entity of same size, check for indexes */
307 int n = get_Sel_n_indexs(sel1);
310 if (n > 0 && n == get_Sel_n_indexs(sel2)) {
311 /* same non-zero number of indexes, an array access, check */
312 for (i = 0; i < n; ++i) {
313 ir_node *idx1 = get_Sel_index(sel1, i);
314 ir_node *idx2 = get_Sel_index(sel2, i);
315 ir_alias_relation res = different_index(idx1, idx2, 0); /* we can safely IGNORE the size here if it's at least >0 */
317 if (res == may_alias)
319 else if (res == no_alias)
322 /* if we have at least one no_alias, there is no alias relation, else we have sure */
323 return have_no > 0 ? no_alias : sure_alias;
328 } /* different_sel_offsets */
331 * Determine the alias relation by checking if adr1 and adr2 are pointer
334 * @param adr1 The first address.
335 * @param adr2 The second address.
337 static ir_alias_relation different_types(ir_node *adr1, ir_node *adr2)
339 ir_entity *ent1 = NULL, *ent2 = NULL;
341 if (is_SymConst_addr_ent(adr1))
342 ent1 = get_SymConst_entity(adr1);
343 else if (is_Sel(adr1))
344 ent1 = get_Sel_entity(adr1);
346 if (is_SymConst_addr_ent(adr2))
347 ent2 = get_SymConst_entity(adr2);
348 else if (is_Sel(adr2))
349 ent2 = get_Sel_entity(adr2);
351 if (ent1 != NULL && ent2 != NULL) {
352 ir_type *tp1 = get_entity_type(ent1);
353 ir_type *tp2 = get_entity_type(ent2);
356 if (is_Pointer_type(tp1) && is_Pointer_type(tp2)) {
357 /* do deref until no pointer types are found */
359 tp1 = get_pointer_points_to_type(tp1);
360 tp2 = get_pointer_points_to_type(tp2);
361 } while (is_Pointer_type(tp1) && is_Pointer_type(tp2));
364 if (get_type_tpop(tp1) != get_type_tpop(tp2)) {
365 /* different type structure */
368 if (is_Class_type(tp1)) {
369 /* check class hierarchy */
370 if (! is_SubClass_of(tp1, tp2) &&
371 ! is_SubClass_of(tp2, tp1))
374 /* different types */
380 } /* different_types */
383 * Check if an offset is a constant and these constant is bigger or equal
386 static int check_const_offset(ir_node *offset, int size) {
387 ir_mode *mode = get_irn_mode(offset);
389 /* ok, we found an offset, check for constant */
390 if (is_Const(offset) && mode_is_int(mode)) {
391 tarval *tv = new_tarval_from_long(size, mode);
393 /* size <= offset ? */
394 if (tarval_cmp(tv, get_Const_tarval(offset)) & (pn_Cmp_Eq|pn_Cmp_Lt))
398 } /* check_const_offset */
401 * Check if we can determine that the two pointers always have an offset bigger then size
403 static ir_alias_relation _different_pointer(ir_node *adr1, ir_node *adr2, int size) {
407 /* first address is the result of a pointer addition */
408 ir_node *l1 = get_Add_left(adr1);
409 ir_node *r1 = get_Add_right(adr1);
412 found = check_const_offset(r1, size);
413 } else if (r1 == adr2) {
414 found = check_const_offset(l1, size);
415 } else if (is_Add(adr2)) {
416 /* second address is the result of a pointer addition */
417 ir_node *l2 = get_Add_left(adr2);
418 ir_node *r2 = get_Add_right(adr2);
421 return _different_pointer(r1, r2, size);
422 } else if (l1 == r2) {
423 return _different_pointer(r1, l2, size);
424 } else if (r1 == l2) {
425 return _different_pointer(l1, r2, size);
426 } else if (r1 == r2) {
427 return _different_pointer(l1, l2, size);
430 } else if (is_Add(adr2)) {
431 /* second address is the result of a pointer addition */
432 ir_node *l2 = get_Add_left(adr2);
433 ir_node *r2 = get_Add_right(adr2);
436 found = check_const_offset(r2, size);
437 } else if (r2 == adr1) {
438 found = check_const_offset(l2, size);
441 return different_index(adr1, adr2, size);
443 return found ? no_alias : may_alias;
444 } /* _different_pointer */
447 * Check if we can determine that the two pointers always have an offset bigger then the maximum size of mode1, mode2
449 static ir_alias_relation different_pointer(ir_node *adr1, ir_mode *mode1, ir_node *adr2, ir_mode *mode2) {
450 int size = get_mode_size_bytes(mode1);
451 int n = get_mode_size_bytes(mode2);
455 return _different_pointer(adr1, adr2, size);
456 } /* different_pointer */
459 * Returns non-zero if a node is a routine parameter.
461 * @param node the node to test
463 static int is_arg_Proj(ir_node *node) {
466 node = get_Proj_pred(node);
469 return pn_Start_T_args == get_Proj_proj(node) && is_Start(get_Proj_pred(node));
473 * Returns true if an address represents a global variable.
475 * @param irn the node representing the address
477 static INLINE int is_global_var(ir_node *irn) {
478 return is_SymConst_addr_ent(irn);
479 } /* is_global_var */
482 * Determine the alias relation between two addresses.
484 static ir_alias_relation _get_alias_relation(
486 ir_node *adr1, ir_mode *mode1,
487 ir_node *adr2, ir_mode *mode2)
490 ir_entity *ent1, *ent2;
493 if (! get_opt_alias_analysis())
499 options = get_irg_memory_disambiguator_options(irg);
501 /* The Armageddon switch */
502 if (options & aa_opt_no_alias)
505 /* Two save some code, sort the addresses by its id's. Beware, this
506 might break some things, so better check here. */
507 assert(iro_SymConst < iro_Sel && iro_Sel < iro_Proj && "Code dependence broken");
508 op1 = get_irn_opcode(adr1);
509 op2 = get_irn_opcode(adr2);
520 if (is_global_var(adr1)) {
521 /* first address is a global variable */
523 if (is_global_var(adr2)) {
524 /* both addresses are global variables and we know
525 they are different (R1 a) */
526 if (get_SymConst_entity(adr1) != get_SymConst_entity(adr2))
529 /* equal entity addresses */
534 ir_node *base2 = find_base_adr(adr2, &ent2);
536 if (is_global_var(base2)) {
537 /* base2 address is a global var (R1 a) */
540 } else if (base2 == get_irg_frame(irg)) {
541 /* the second one is a local variable so they are always
544 } else if (base2 == get_irg_tls(irg)) {
545 /* the second one is a TLS variable so they are always
551 /* Here we are: the first is a global var, the second some pointer. */
552 ent1 = get_SymConst_entity(adr1);
553 if (get_entity_address_taken(ent1) == ir_address_not_taken) {
554 /* The address of the global variable was never taken, so
555 the pointer cannot match (R2). */
558 } else if (is_Sel(adr1)) {
559 /* the first address is a Sel */
560 ir_node *base1 = find_base_adr(adr1, &ent1);
562 if (base1 == get_irg_frame(irg)) {
563 /* first is a local variable ent1 */
565 /* the second address is a Sel */
566 ir_node *base2 = find_base_adr(adr2, &ent2);
568 if (base1 == base2) {
569 /* identical bases: both are local variables */
571 /* both addresses are local variables and we know
572 they are different (R1 a) */
576 return different_sel_offsets(adr1, adr2);
578 } else if (base2 == get_irg_tls(irg)) {
579 /* the second one is a TLS variable so they are always
582 } else if (is_arg_Proj(base2)) {
583 /* the second one is an offset from a parameter so they are
584 always different (R1 e) */
587 } else if (is_arg_Proj(adr2)) {
588 /* a local variable and a parameter are always different (R1 e) */
591 } else if (base1 == get_irg_tls(irg)) {
592 /* the first is a TLS variable */
594 /* the second address is a Sel */
595 ir_node *base2 = find_base_adr(adr2, &ent2);
599 /* both addresses are tls variables and we know
600 they are different (R1 a) */
603 return different_sel_offsets(adr1, adr2);
605 else if (base2 == get_irg_frame(irg)) {
606 /* the first one is a tls variable, the second a local one,
607 they are different (R1 d) */
611 } else if (is_arg_Proj(base1)) {
612 /* the first one is an offset from a parameter */
614 /* the second address is a Sel */
615 ir_node *base2 = find_base_adr(adr2, &ent2);
617 if (base2 == get_irg_frame(irg)) {
618 /* the second one is a local variable so they are always
623 } else if (is_global_var(base1)) {
624 /* the first one is a global variable */
625 ent1 = get_SymConst_entity(base1);
627 /* the second address is a Sel */
628 ir_node *base2 = find_base_adr(adr2, &ent2);
630 if (base1 == base2) {
631 /* same global var */
632 return different_sel_offsets(adr1, adr2);
634 else if (base2 == get_irg_frame(irg)) {
635 /* the second one is a local variable so they are always
638 } else if (base2 == get_irg_tls(irg)) {
639 /* the second one is a TLS variable so they are always
642 } else if (is_arg_Proj(base2)) {
643 if (get_entity_address_taken(ent1) == ir_address_not_taken) {
644 /* The address of the global variable was never taken, so
645 the pointer cannot match (R2). */
648 } else if (is_global_var(base2)) {
649 ent2 = get_SymConst_entity(base2);
650 /* both addresses are global variables and we know
651 they are different (R1 a) */
658 /* some pointers, check if they have the same base buf constant offset */
659 ir_alias_relation rel = different_pointer(adr1, mode1, adr2, mode2);
660 if (rel != may_alias)
665 if (options & aa_opt_type_based) { /* Type based alias analysis */
666 ir_alias_relation rel;
668 if (options & aa_opt_byte_type_may_alias) {
669 if (get_mode_size_bits(mode1) == 8 || get_mode_size_bits(mode2) == 8) {
670 /* One of the modes address a byte. Assume a may_alias and leave
671 the type based check. */
672 goto leave_type_based_alias;
675 /* cheap check: If the mode sizes did not match, the types MUST be different */
676 if (get_mode_size_bits(mode1) != get_mode_size_bits(mode2))
679 /* cheap test: if only one is a reference mode, no alias */
680 if (mode_is_reference(mode1) != mode_is_reference(mode2))
684 rel = different_types(adr1, adr2);
685 if (rel != may_alias)
687 leave_type_based_alias:;
690 /* do we have a language specific memory disambiguator? */
691 if (language_disambuigator) {
692 ir_alias_relation rel = (*language_disambuigator)(irg, adr1, mode1, adr2, mode2);
693 if (rel != may_alias)
697 /* access points-to information here */
699 } /* _get_alias_relation */
702 * Determine the alias relation between two addresses.
704 ir_alias_relation get_alias_relation(
706 ir_node *adr1, ir_mode *mode1,
707 ir_node *adr2, ir_mode *mode2)
709 ir_alias_relation rel = _get_alias_relation(irg, adr1, mode1, adr2, mode2);
710 DB((dbg, LEVEL_1, "alias(%+F, %+f) = %s\n", adr1, adr2, get_ir_alias_relation_name(rel)));
712 } /* get_alias_relation */
714 /* Set a source language specific memory disambiguator function. */
715 void set_language_memory_disambiguator(DISAMBIGUATOR_FUNC func) {
716 language_disambuigator = func;
717 } /* set_language_memory_disambiguator */
719 /** The result cache for the memory disambiguator. */
720 static set *result_cache = NULL;
722 /** An entry in the relation cache. */
723 typedef struct mem_disambig_entry {
724 ir_node *adr1; /**< The first address. */
725 ir_node *adr2; /**< The second address. */
726 ir_alias_relation result; /**< The alias relation result. */
727 } mem_disambig_entry;
729 #define HASH_ENTRY(adr1, adr2) (HASH_PTR(adr1) ^ HASH_PTR(adr2))
732 * Compare two relation cache entries.
734 static int cmp_mem_disambig_entry(const void *elt, const void *key, size_t size) {
735 const mem_disambig_entry *p1 = elt;
736 const mem_disambig_entry *p2 = key;
739 return p1->adr1 == p2->adr1 && p1->adr2 == p2->adr2;
740 } /* cmp_mem_disambig_entry */
743 * Initialize the relation cache.
745 void mem_disambig_init(void) {
746 result_cache = new_set(cmp_mem_disambig_entry, 8);
747 } /* mem_disambig_init */
750 * Determine the alias relation between two addresses.
752 ir_alias_relation get_alias_relation_ex(
754 ir_node *adr1, ir_mode *mode1,
755 ir_node *adr2, ir_mode *mode2)
757 mem_disambig_entry key, *entry;
759 if (! get_opt_alias_analysis())
762 if (get_irn_opcode(adr1) > get_irn_opcode(adr2)) {
770 entry = set_find(result_cache, &key, sizeof(key), HASH_ENTRY(adr1, adr2));
772 return entry->result;
774 key.result = get_alias_relation(irg, adr1, mode1, adr2, mode2);
776 set_insert(result_cache, &key, sizeof(key), HASH_ENTRY(adr1, adr2));
778 } /* get_alias_relation_ex */
780 /* Free the relation cache. */
781 void mem_disambig_term(void) {
783 del_set(result_cache);
786 } /* mem_disambig_term */
789 * Check the mode of a Load/Store with the mode of the entity
791 * If the mode of the entity and the Load/Store mode do not match, we
792 * have the bad reinterpret case:
795 * char b = *(char *)&i;
797 * We do NOT count this as one value and return address_taken
799 * However, we support an often used case. If the mode is two-complement
800 * we allow casts between signed/unsigned.
802 * @param mode the mode of the Load/Store
803 * @param ent_mode the mode of the accessed entity
805 * @return non-zero if the Load/Store is a hidden cast, zero else
807 static int is_hidden_cast(ir_mode *mode, ir_mode *ent_mode) {
808 if (ent_mode != mode) {
809 if (ent_mode == NULL ||
810 get_mode_size_bits(ent_mode) != get_mode_size_bits(mode) ||
811 get_mode_sort(ent_mode) != get_mode_sort(mode) ||
812 get_mode_arithmetic(ent_mode) != irma_twos_complement ||
813 get_mode_arithmetic(mode) != irma_twos_complement)
817 } /* is_hidden_cast */
820 * Determine the address_taken state of a node (or it's successor Sels).
822 * @param irn the node
824 static ir_address_taken_state find_address_taken_state(ir_node *irn) {
826 ir_mode *emode, *mode;
830 for (i = get_irn_n_outs(irn) - 1; i >= 0; --i) {
831 ir_node *succ = get_irn_out(irn, i);
833 switch (get_irn_opcode(succ)) {
835 /* check if this load is not a hidden conversion */
836 mode = get_Load_mode(succ);
837 ent = is_SymConst(irn) ? get_SymConst_entity(irn) : get_Sel_entity(irn);
838 emode = get_type_mode(get_entity_type(ent));
839 if (is_hidden_cast(mode, emode))
840 return ir_address_taken;
844 /* check that the node is not the Store's value */
845 value = get_Store_value(succ);
847 return ir_address_taken;
848 /* check if this Store is not a hidden conversion */
849 mode = get_irn_mode(value);
850 ent = is_SymConst(irn) ? get_SymConst_entity(irn) : get_Sel_entity(irn);
851 emode = get_type_mode(get_entity_type(ent));
852 if (is_hidden_cast(mode, emode))
853 return ir_address_taken;
857 /* Check the successor of irn. */
858 ir_address_taken_state res = find_address_taken_state(succ);
859 if (res != ir_address_not_taken)
865 /* Only the call address is not an address taker but
866 this is an uninteresting case, so we ignore it here. */
867 for (j = get_Call_n_params(succ) - 1; j >= 0; --j) {
868 ir_node *param = get_Call_param(succ, j);
870 return ir_address_taken;
875 /* another op, the address may be taken */
876 return ir_address_taken_unknown;
879 /* All successors finished, the address is not taken. */
880 return ir_address_not_taken;
881 } /* find_address_taken_state */
884 * Update the "address taken" flag of all frame entities.
886 static void analyse_irg_address_taken(ir_graph *irg) {
887 ir_type *ft = get_irg_frame_type(irg);
891 /* set initial state to not_taken, as this is the "smallest" state */
892 for (i = get_class_n_members(ft) - 1; i >= 0; --i) {
893 ir_entity *ent = get_class_member(ft, i);
895 set_entity_address_taken(ent, ir_address_not_taken);
898 assure_irg_outs(irg);
900 irg_frame = get_irg_frame(irg);
902 for (i = get_irn_n_outs(irg_frame) - 1; i >= 0; --i) {
903 ir_node *succ = get_irn_out(irg_frame, i);
904 ir_address_taken_state state;
907 ir_entity *ent = get_Sel_entity(succ);
909 if (get_entity_address_taken(ent) == ir_address_taken)
912 state = find_address_taken_state(succ);
913 if (state > get_entity_address_taken(ent))
914 set_entity_address_taken(ent, state);
918 irg->adr_taken_state = ir_address_taken_computed;
919 } /* analyse_address_taken */
921 /* Returns the current address taken state of the graph. */
922 ir_address_taken_computed_state get_irg_address_taken_state(const ir_graph *irg) {
923 return irg->adr_taken_state;
924 } /* get_irg_address_taken_state */
926 /* Sets the current address taken state of the graph. */
927 void set_irg_address_taken_state(ir_graph *irg, ir_address_taken_computed_state state) {
928 irg->adr_taken_state = state;
929 } /* set_irg_address_taken_state */
931 /* Assure that the address taken flag is computed for the given graph. */
932 void assure_irg_address_taken_computed(ir_graph *irg) {
933 if (irg->adr_taken_state == ir_address_taken_not_computed)
934 analyse_irg_address_taken(irg);
935 } /* assure_irg_address_taken_computed */
939 * Initialize the address_taken flag for a global type like type.
941 static void init_taken_flag(ir_type * tp) {
944 /* All external visible entities are at least
945 ir_address_taken_unknown. This is very conservative. */
946 for (i = get_compound_n_members(tp) - 1; i >= 0; --i) {
947 ir_entity *ent = get_compound_member(tp, i);
948 ir_address_taken_state state;
950 state = get_entity_visibility(ent) == visibility_external_visible ?
951 ir_address_taken_unknown : ir_address_not_taken ;
952 set_entity_address_taken(ent, state);
954 } /* init_taken_flag */
956 static void check_initializer_nodes(ir_initializer_t *initializer)
958 switch(initializer->kind) {
959 case IR_INITIALIZER_CONST: {
960 ir_node *n = initializer->consti.value;
962 /* let's check if it's an address */
963 if (is_SymConst_addr_ent(n)) {
964 ir_entity *ent = get_SymConst_entity(n);
965 set_entity_address_taken(ent, ir_address_taken);
969 case IR_INITIALIZER_TARVAL:
970 case IR_INITIALIZER_NULL:
972 case IR_INITIALIZER_COMPOUND: {
975 for(i = 0; i < initializer->compound.n_initializers; ++i) {
976 ir_initializer_t *sub_initializer
977 = initializer->compound.initializers[i];
978 check_initializer_nodes(sub_initializer);
983 panic("invalid initialzier found");
987 * Mark all entities used in the initializer for the given entity as address taken
989 static void check_initializer(ir_entity *ent) {
993 /* do not check uninitialized values */
994 if (get_entity_variability(ent) == variability_uninitialized)
997 /* Beware: Methods initialized with "themself". This does not count as a taken
999 if (is_Method_type(get_entity_type(ent)))
1002 if (ent->has_initializer) {
1003 check_initializer_nodes(ent->attr.initializer);
1004 } else if (is_atomic_entity(ent)) {
1005 /* let's check if it's an address */
1006 n = get_atomic_ent_value(ent);
1007 if (is_SymConst_addr_ent(n)) {
1008 ir_entity *ent = get_SymConst_entity(n);
1009 set_entity_address_taken(ent, ir_address_taken);
1012 for (i = get_compound_ent_n_values(ent) - 1; i >= 0; --i) {
1013 n = get_compound_ent_value(ent, i);
1015 /* let's check if it's an address */
1016 if (is_SymConst_addr_ent(n)) {
1017 ir_entity *ent = get_SymConst_entity(n);
1018 set_entity_address_taken(ent, ir_address_taken);
1022 } /* check_initializer */
1026 * Mark all entities used in initializers as address taken
1028 static void check_initializers(ir_type *tp) {
1031 for (i = get_compound_n_members(tp) - 1; i >= 0; --i) {
1032 ir_entity *ent = get_compound_member(tp, i);
1034 check_initializer(ent);
1036 } /* check_initializers */
1038 #ifdef DEBUG_libfirm
1040 * Print the address taken state of all entities of a given type for debugging.
1042 static void print_address_taken_state(ir_type *tp) {
1044 for (i = get_compound_n_members(tp) - 1; i >= 0; --i) {
1045 ir_entity *ent = get_compound_member(tp, i);
1046 ir_address_taken_state state = get_entity_address_taken(ent);
1048 if (state != ir_address_not_taken) {
1049 assert(ir_address_not_taken <= (int) state && state <= ir_address_taken);
1050 ir_printf("%+F: %s\n", ent, get_address_taken_state_name(state));
1053 } /* print_address_taken_state */
1054 #endif /* DEBUG_libfirm */
1057 * Post-walker: check for global entity address
1059 static void check_global_address(ir_node *irn, void *env) {
1062 ir_address_taken_state state;
1064 if (is_SymConst_addr_ent(irn)) {
1066 ent = get_SymConst_entity(irn);
1067 } else if (is_Sel(irn) && get_Sel_ptr(irn) == tls) {
1068 /* A TLS variable. */
1069 ent = get_Sel_entity(irn);
1073 if (get_entity_address_taken(ent) >= ir_address_taken) {
1074 /* Already at the maximum. */
1077 state = find_address_taken_state(irn);
1078 if (state > get_entity_address_taken(ent))
1079 set_entity_address_taken(ent, state);
1080 } /* check_global_address */
1083 * Update the "address taken" flag of all global entities.
1085 static void analyse_irp_globals_address_taken(void) {
1088 FIRM_DBG_REGISTER(dbg, "firm.ana.irmemory");
1090 init_taken_flag(get_glob_type());
1091 init_taken_flag(get_tls_type());
1093 check_initializers(get_glob_type());
1094 check_initializers(get_tls_type());
1096 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
1097 ir_graph *irg = get_irp_irg(i);
1099 assure_irg_outs(irg);
1100 irg_walk_graph(irg, NULL, check_global_address, get_irg_tls(irg));
1103 #ifdef DEBUG_libfirm
1104 if (firm_dbg_get_mask(dbg) & LEVEL_1) {
1105 print_address_taken_state(get_glob_type());
1106 print_address_taken_state(get_tls_type());
1108 #endif /* DEBUG_libfirm */
1111 irp->globals_adr_taken_state = ir_address_taken_computed;
1112 } /* analyse_irp_globals_address_taken */
1114 /* Returns the current address taken state of the globals. */
1115 ir_address_taken_computed_state get_irp_globals_address_taken_state(void) {
1116 return irp->globals_adr_taken_state;
1117 } /* get_irp_globals_address_taken_state */
1119 /* Sets the current address taken state of the graph. */
1120 void set_irp_globals_address_taken_state(ir_address_taken_computed_state state) {
1121 irp->globals_adr_taken_state = state;
1122 } /* set_irg_address_taken_state */
1124 /* Assure that the address taken flag is computed for the globals. */
1125 void assure_irp_globals_address_taken_computed(void) {
1126 if (irp->globals_adr_taken_state == ir_address_taken_not_computed)
1127 analyse_irp_globals_address_taken();
1128 } /* assure_irp_globals_address_taken_computed */
1131 #include <adt/pmap.h>
1132 #include "typerep.h"
1134 DEBUG_ONLY(static firm_dbg_module_t *dbgcall = NULL;)
1136 /** Maps method types to cloned method types. */
1137 static pmap *mtp_map;
1140 * Clone a method type if not already cloned.
1142 static ir_type *clone_type_and_cache(ir_type *tp) {
1143 static ident *prefix = NULL;
1145 pmap_entry *e = pmap_find(mtp_map, tp);
1151 prefix = new_id_from_chars("C", 1);
1153 res = clone_type_method(tp, prefix);
1154 pmap_insert(mtp_map, tp, res);
1155 DB((dbgcall, LEVEL_2, "cloned type %+F into %+F\n", tp, res));
1158 } /* clone_type_and_cache */
1161 * Copy the calling conventions from the entities to the call type.
1163 static void update_calls_to_private(ir_node *call, void *env) {
1165 if (is_Call(call)) {
1166 ir_node *ptr = get_Call_ptr(call);
1168 if (is_SymConst(ptr)) {
1169 ir_entity *ent = get_SymConst_entity(ptr);
1170 ir_type *ctp = get_Call_type(call);
1172 if (get_entity_additional_properties(ent) & mtp_property_private) {
1173 if ((get_method_additional_properties(ctp) & mtp_property_private) == 0) {
1174 ctp = clone_type_and_cache(ctp);
1175 set_method_additional_property(ctp, mtp_property_private);
1176 set_Call_type(call, ctp);
1177 DB((dbgcall, LEVEL_1, "changed call to private method %+F\n", ent));
1182 } /* update_calls_to_private */
1184 /* Mark all private methods, i.e. those of which all call sites are known. */
1185 void mark_private_methods(void) {
1189 FIRM_DBG_REGISTER(dbgcall, "firm.opt.cc");
1191 assure_irp_globals_address_taken_computed();
1193 mtp_map = pmap_create();
1195 /* first step: change the calling conventions of the local non-escaped entities */
1196 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
1197 ir_graph *irg = get_irp_irg(i);
1198 ir_entity *ent = get_irg_entity(irg);
1199 ir_address_taken_state state = get_entity_address_taken(ent);
1201 if (get_entity_visibility(ent) == visibility_local &&
1202 state == ir_address_not_taken) {
1203 ir_type *mtp = get_entity_type(ent);
1205 set_entity_additional_property(ent, mtp_property_private);
1206 DB((dbgcall, LEVEL_1, "found private method %+F\n", ent));
1207 if ((get_method_additional_properties(mtp) & mtp_property_private) == 0) {
1208 /* need a new type */
1209 mtp = clone_type_and_cache(mtp);
1210 set_entity_type(ent, mtp);
1211 set_method_additional_property(mtp, mtp_property_private);
1218 all_irg_walk(NULL, update_calls_to_private, NULL);
1220 pmap_destroy(mtp_map);
1221 } /* mark_private_methods */