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 tarval_is_null(tv) ? may_alias : no_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;
124 return tv1 == tv2 ? sure_alias : no_alias;
126 /* arg, modes may be different */
127 m1 = get_tarval_mode(tv1);
128 m2 = get_tarval_mode(tv2);
130 int size = get_mode_size_bits(m1) - get_mode_size_bits(m2);
133 /* m1 is a small mode, cast up */
134 m1 = mode_is_signed(m1) ? find_signed_mode(m2) : find_unsigned_mode(m2);
136 /* should NOT happen, but if it does we give up here */
139 tv1 = tarval_convert_to(tv1, m1);
140 } else if (size > 0) {
141 /* m2 is a small mode, cast up */
142 m2 = mode_is_signed(m2) ? find_signed_mode(m1) : find_unsigned_mode(m1);
144 /* should NOT happen, but if it does we give up here */
147 tv2 = tarval_convert_to(tv2, m2);
149 /* here the size should be identical, check for signed */
150 if (get_mode_sign(m1) != get_mode_sign(m2)) {
151 /* find the signed */
152 if (mode_is_signed(m2)) {
159 /* m1 is now the signed one */
160 if (tarval_cmp(tv1, get_tarval_null(m1)) & (pn_Cmp_Eq|pn_Cmp_Gt)) {
161 /* tv1 is signed, but >= 0, simply cast into unsigned */
162 tv1 = tarval_convert_to(tv1, m2);
164 tv_size = new_tarval_from_long(size, m2);
166 if (tarval_cmp(tv2, tv_size) & (pn_Cmp_Eq|pn_Cmp_Gt)) {
167 /* tv1 is negative and tv2 >= tv_size, so the difference is bigger than size */
170 /* tv_size > tv2, so we can subtract without overflow */
171 tv2 = tarval_sub(tv_size, tv2);
173 /* tv1 is < 0, so we can negate it */
174 tv1 = tarval_neg(tv1);
176 /* cast it into unsigned. for two-complement it does the right thing for MIN_INT */
177 tv1 = tarval_convert_to(tv1, m2);
179 /* now we can compare without overflow */
180 return tarval_cmp(tv1, tv2) & (pn_Cmp_Eq|pn_Cmp_Gt) ? no_alias : may_alias;
184 if (tarval_cmp(tv1, tv2) == pn_Cmp_Gt) {
189 /* tv1 is now the "smaller" one */
190 tv = tarval_sub(tv2, tv1);
191 tv_size = new_tarval_from_long(size, get_tarval_mode(tv));
192 return tarval_cmp(tv_size, tv) & (pn_Cmp_Eq|pn_Cmp_Lt) ? no_alias : may_alias;
195 /* Note: we rely here on the fact that normalization puts constants on the RIGHT side */
197 ir_node *l1 = get_Add_left(idx1);
198 ir_node *r1 = get_Add_right(idx1);
203 return check_const(r1, size);
206 /* both are Adds, check if they are of x + a == x + b kind */
207 ir_node *l2 = get_Add_left(idx2);
208 ir_node *r2 = get_Add_right(idx2);
211 return different_index(r1, r2, size);
213 return different_index(r1, l2, size);
215 return different_index(l1, l2, size);
217 return different_index(l1, r2, size);
221 ir_node *l2 = get_Add_left(idx2);
222 ir_node *r2 = get_Add_right(idx2);
227 return check_const(r2, size);
232 ir_node *l1 = get_Sub_left(idx1);
233 ir_node *r1 = get_Sub_right(idx1);
238 return check_const(r1, size);
242 /* both are Subs, check if they are of x - a == x - b kind */
243 ir_node *l2 = get_Sub_left(idx2);
246 ir_node *r2 = get_Sub_right(idx2);
247 return different_index(r1, r2, size);
252 ir_node *l2 = get_Sub_left(idx2);
253 ir_node *r2 = get_Sub_right(idx2);
258 return check_const(r2, size);
263 } /* different_index */
266 * Two Sel addresses have the same base address, check if there offsets are different.
268 * @param adr1 The first address.
269 * @param adr2 The second address.
271 static ir_alias_relation different_sel_offsets(ir_node *sel1, ir_node *sel2) {
272 /* seems to be broken */
274 ir_entity *ent1 = get_Sel_entity(sel1);
275 ir_entity *ent2 = get_Sel_entity(sel2);
276 int i, check_arr = 0;
281 ir_type *tp1 = get_entity_type(ent1);
282 ir_type *tp2 = get_entity_type(ent2);
286 else if (get_type_state(tp1) == layout_fixed && get_type_state(tp2) == layout_fixed &&
287 get_type_size_bits(tp1) == get_type_size_bits(tp2))
291 /* we select an entity of same size, check for indexes */
292 int n = get_Sel_n_indexs(sel1);
295 if (n > 0 && n == get_Sel_n_indexs(sel2)) {
296 /* same non-zero number of indexes, an array access, check */
297 for (i = 0; i < n; ++i) {
298 ir_node *idx1 = get_Sel_index(sel1, i);
299 ir_node *idx2 = get_Sel_index(sel2, i);
300 ir_alias_relation res = different_index(idx1, idx2, 0); /* we can safely IGNORE the size here if it's at least >0 */
302 if (res == may_alias)
304 else if (res == no_alias)
307 /* if we have at least one no_alias, there is no alias relation, else we have sure */
308 return have_no > 0 ? no_alias : sure_alias;
313 } /* different_sel_offsets */
316 * Determine the alias relation by checking if adr1 and adr2 are pointer
319 * @param adr1 The first address.
320 * @param adr2 The second address.
322 static ir_alias_relation different_types(ir_node *adr1, ir_node *adr2)
324 ir_entity *ent1 = NULL, *ent2 = NULL;
326 if (is_SymConst(adr1) && get_SymConst_kind(adr1) == symconst_addr_ent)
327 ent1 = get_SymConst_entity(adr1);
328 else if (is_Sel(adr1))
329 ent1 = get_Sel_entity(adr1);
331 if (is_SymConst(adr2) && get_SymConst_kind(adr2) == symconst_addr_ent)
332 ent2 = get_SymConst_entity(adr2);
333 else if (is_Sel(adr2))
334 ent2 = get_Sel_entity(adr2);
336 if (ent1 != NULL && ent2 != NULL) {
337 ir_type *tp1 = get_entity_type(ent1);
338 ir_type *tp2 = get_entity_type(ent2);
341 if (is_Pointer_type(tp1) && is_Pointer_type(tp2)) {
342 /* do deref until no pointer types are found */
344 tp1 = get_pointer_points_to_type(tp1);
345 tp2 = get_pointer_points_to_type(tp2);
346 } while (is_Pointer_type(tp1) && is_Pointer_type(tp2));
349 if (get_type_tpop(tp1) != get_type_tpop(tp2)) {
350 /* different type structure */
353 if (is_Class_type(tp1)) {
354 /* check class hierarchy */
355 if (! is_SubClass_of(tp1, tp2) &&
356 ! is_SubClass_of(tp2, tp1))
359 /* different types */
365 } /* different_types */
368 * Check if an offset is a constant and these constant is bigger or equal
371 static int check_const_offset(ir_node *offset, int size) {
372 ir_mode *mode = get_irn_mode(offset);
374 /* ok, we found an offset, check for constant */
375 if (is_Const(offset) && mode_is_int(mode)) {
376 tarval *tv = new_tarval_from_long(size, mode);
378 /* size <= offset ? */
379 if (tarval_cmp(tv, get_Const_tarval(offset)) & (pn_Cmp_Eq|pn_Cmp_Lt))
383 } /* check_const_offset */
386 * Check if we can determine that the two pointers always have an offset bigger then size
388 static ir_alias_relation _different_pointer(ir_node *adr1, ir_node *adr2, int size) {
392 /* first address is the result of a pointer addition */
393 ir_node *l1 = get_Add_left(adr1);
394 ir_node *r1 = get_Add_right(adr1);
397 found = check_const_offset(r1, size);
398 } else if (r1 == adr2) {
399 found = check_const_offset(l1, size);
400 } else if (is_Add(adr2)) {
401 /* second address is the result of a pointer addition */
402 ir_node *l2 = get_Add_left(adr2);
403 ir_node *r2 = get_Add_right(adr2);
406 return _different_pointer(r1, r2, size);
407 } else if (l1 == r2) {
408 return _different_pointer(r1, l2, size);
409 } else if (r1 == l2) {
410 return _different_pointer(l1, r2, size);
411 } else if (r1 == r2) {
412 return _different_pointer(l1, l2, 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 found = check_const_offset(r2, size);
422 } else if (r2 == adr1) {
423 found = check_const_offset(l2, size);
426 return different_index(adr1, adr2, size);
428 return found ? no_alias : may_alias;
429 } /* _different_pointer */
432 * Check if we can determine that the two pointers always have an offset bigger then the maximum size of mode1, mode2
434 static ir_alias_relation different_pointer(ir_node *adr1, ir_mode *mode1, ir_node *adr2, ir_mode *mode2) {
435 int size = get_mode_size_bytes(mode1);
436 int n = get_mode_size_bytes(mode2);
440 return _different_pointer(adr1, adr2, size);
441 } /* different_pointer */
444 * Returns non-zero if a node is a routine parameter.
446 * @param node the node to test
448 static int is_arg_Proj(ir_node *node) {
451 node = get_Proj_pred(node);
454 return pn_Start_T_args == get_Proj_proj(node) && is_Start(get_Proj_pred(node));
458 * Returns true if an address represents a global variable.
460 static INLINE int is_global_var(ir_node *irn) {
461 return is_SymConst(irn) && get_SymConst_kind(irn) == symconst_addr_ent;
462 } /* is_global_var */
465 * Determine the alias relation between two addresses.
467 static ir_alias_relation _get_alias_relation(
469 ir_node *adr1, ir_mode *mode1,
470 ir_node *adr2, ir_mode *mode2)
473 ir_entity *ent1, *ent2;
476 if (! get_opt_alias_analysis())
482 options = get_irg_memory_disambiguator_options(irg);
484 /* The Armageddon switch */
485 if (options & aa_opt_no_alias)
488 /* Two save some code, sort the addresses by its id's. Beware, this
489 might break some things, so better check here. */
490 assert(iro_SymConst < iro_Sel && iro_Sel < iro_Proj && "Code dependence broken");
491 op1 = get_irn_opcode(adr1);
492 op2 = get_irn_opcode(adr2);
503 if (is_global_var(adr1)) {
504 /* first address is a global variable */
506 if (is_global_var(adr2)) {
507 /* both addresses are global variables and we know
508 they are different (R1 a) */
509 if (get_SymConst_entity(adr1) != get_SymConst_entity(adr2))
512 /* equal entity addresses */
517 ir_node *base2 = find_base_adr(adr2, &ent2);
519 if (is_global_var(base2)) {
520 /* base2 address is a global var (R1 a) */
523 } else if (base2 == get_irg_frame(irg)) {
524 /* the second one is a local variable so they are always
527 } else if (base2 == get_irg_tls(irg)) {
528 /* the second one is a TLS variable so they are always
534 /* Here we are: the first is a global var, the second some pointer. */
535 ent1 = get_SymConst_entity(adr1);
536 if (get_entity_address_taken(ent1) == ir_address_not_taken) {
537 /* The address of the global variable was never taken, so
538 the pointer cannot match (R2). */
541 } else if (is_Sel(adr1)) {
542 /* the first address is a Sel */
543 ir_node *base1 = find_base_adr(adr1, &ent1);
545 if (base1 == get_irg_frame(irg)) {
546 /* first is a local variable ent1 */
548 /* the second address is a Sel */
549 ir_node *base2 = find_base_adr(adr2, &ent2);
551 if (base1 == base2) {
552 /* identical bases: both are local variables */
554 /* both addresses are local variables and we know
555 they are different (R1 a) */
559 return different_sel_offsets(adr1, adr2);
561 } else if (base2 == get_irg_tls(irg)) {
562 /* the second one is a TLS variable so they are always
565 } else if (is_arg_Proj(base2)) {
566 /* the second one is an offset from a parameter so they are
567 always different (R1 e) */
570 } else if (is_arg_Proj(adr2)) {
571 /* a local variable and a parameter are always different (R1 e) */
574 } else if (base1 == get_irg_tls(irg)) {
575 /* the first is a TLS variable */
577 /* the second address is a Sel */
578 ir_node *base2 = find_base_adr(adr2, &ent2);
582 /* both addresses are tls variables and we know
583 they are different (R1 a) */
586 return different_sel_offsets(adr1, adr2);
588 else if (base2 == get_irg_frame(irg)) {
589 /* the first one is a tls variable, the second a local one,
590 they are different (R1 d) */
594 } else if (is_arg_Proj(base1)) {
595 /* the first one is an offset from a parameter */
597 /* the second address is a Sel */
598 ir_node *base2 = find_base_adr(adr2, &ent2);
600 if (base2 == get_irg_frame(irg)) {
601 /* the second one is a local variable so they are always
606 } else if (is_global_var(base1)) {
607 /* the first one is a global variable */
608 ent1 = get_SymConst_entity(base1);
610 /* the second address is a Sel */
611 ir_node *base2 = find_base_adr(adr2, &ent2);
613 if (base1 == base2) {
614 /* same global var */
615 return different_sel_offsets(adr1, adr2);
617 else if (base2 == get_irg_frame(irg)) {
618 /* the second one is a local variable so they are always
621 } else if (base2 == get_irg_tls(irg)) {
622 /* the second one is a TLS variable so they are always
625 } else if (is_arg_Proj(base2)) {
626 if (get_entity_address_taken(ent1) == ir_address_not_taken) {
627 /* The address of the global variable was never taken, so
628 the pointer cannot match (R2). */
631 } else if (is_global_var(base2)) {
632 ent2 = get_SymConst_entity(base2);
633 /* both addresses are global variables and we know
634 they are different (R1 a) */
641 /* some pointers, check if they have the same base buf constant offset */
642 ir_alias_relation rel = different_pointer(adr1, mode1, adr2, mode2);
643 if (rel != may_alias)
648 if (options & aa_opt_type_based) { /* Type based alias analysis */
649 ir_alias_relation rel;
651 if (options & aa_opt_byte_type_may_alias) {
652 if (get_mode_size_bits(mode1) == 8 || get_mode_size_bits(mode2) == 8) {
653 /* One of the modes address a byte. Assume a may_alias and leave
654 the type based check. */
655 goto leave_type_based_alias;
658 /* cheap check: If the mode sizes did not match, the types MUST be different */
659 if (get_mode_size_bits(mode1) != get_mode_size_bits(mode2))
662 /* cheap test: if only one is a reference mode, no alias */
663 if (mode_is_reference(mode1) != mode_is_reference(mode2))
667 rel = different_types(adr1, adr2);
668 if (rel != may_alias)
670 leave_type_based_alias:;
673 /* do we have a language specific memory disambiguator? */
674 if (language_disambuigator) {
675 ir_alias_relation rel = (*language_disambuigator)(irg, adr1, mode1, adr2, mode2);
676 if (rel != may_alias)
680 /* access points-to information here */
682 } /* _get_alias_relation */
685 * Determine the alias relation between two addresses.
687 ir_alias_relation get_alias_relation(
689 ir_node *adr1, ir_mode *mode1,
690 ir_node *adr2, ir_mode *mode2)
692 ir_alias_relation rel = _get_alias_relation(irg, adr1, mode1, adr2, mode2);
694 } /* get_alias_relation */
696 /* Set a source language specific memory disambiguator function. */
697 void set_language_memory_disambiguator(DISAMBIGUATOR_FUNC func) {
698 language_disambuigator = func;
699 } /* set_language_memory_disambiguator */
701 /** The result cache for the memory disambiguator. */
702 static set *result_cache = NULL;
704 /** An entry in the relation cache. */
705 typedef struct mem_disambig_entry {
706 ir_node *adr1; /**< The first address. */
707 ir_node *adr2; /**< The second address. */
708 ir_alias_relation result; /**< The alias relation result. */
709 } mem_disambig_entry;
711 #define HASH_ENTRY(adr1, adr2) (HASH_PTR(adr1) ^ HASH_PTR(adr2))
714 * Compare two relation cache entries.
716 static int cmp_mem_disambig_entry(const void *elt, const void *key, size_t size) {
717 const mem_disambig_entry *p1 = elt;
718 const mem_disambig_entry *p2 = key;
721 return p1->adr1 == p2->adr1 && p1->adr2 == p2->adr2;
722 } /* cmp_mem_disambig_entry */
725 * Initialize the relation cache.
727 void mem_disambig_init(void) {
728 result_cache = new_set(cmp_mem_disambig_entry, 8);
729 } /* mem_disambig_init */
732 * Determine the alias relation between two addresses.
734 ir_alias_relation get_alias_relation_ex(
736 ir_node *adr1, ir_mode *mode1,
737 ir_node *adr2, ir_mode *mode2)
739 mem_disambig_entry key, *entry;
741 if (! get_opt_alias_analysis())
744 if (get_irn_opcode(adr1) > get_irn_opcode(adr2)) {
752 entry = set_find(result_cache, &key, sizeof(key), HASH_ENTRY(adr1, adr2));
754 return entry->result;
756 key.result = get_alias_relation(irg, adr1, mode1, adr2, mode2);
758 set_insert(result_cache, &key, sizeof(key), HASH_ENTRY(adr1, adr2));
760 } /* get_alias_relation_ex */
762 /* Free the relation cache. */
763 void mem_disambig_term(void) {
765 del_set(result_cache);
768 } /* mem_disambig_term */
771 * Check the mode of a Load/Store with the mode of the entity
773 * If the mode of the entity and the Load/Store mode do not match, we
774 * have the bad reinterpret case:
777 * char b = *(char *)&i;
779 * We do NOT count this as one value and return address_taken
781 * However, we support an often used case. If the mode is two-complement
782 * we allow casts between signed/unsigned.
784 * @param mode the mode of the Load/Store
785 * @param ent_mode the mode of the accessed entity
787 * @return non-zero if the Load/Store is a hidden cast, zero else
789 static int is_hidden_cast(ir_mode *mode, ir_mode *ent_mode) {
790 if (ent_mode != mode) {
791 if (ent_mode == NULL ||
792 get_mode_size_bits(ent_mode) != get_mode_size_bits(mode) ||
793 get_mode_sort(ent_mode) != get_mode_sort(mode) ||
794 get_mode_arithmetic(ent_mode) != irma_twos_complement ||
795 get_mode_arithmetic(mode) != irma_twos_complement)
799 } /* is_hidden_cast */
802 * Determine the address_taken state of a node (or it's successor Sels).
804 * @param irn the node
806 static ir_address_taken_state find_address_taken_state(ir_node *irn) {
808 ir_mode *emode, *mode;
812 for (i = get_irn_n_outs(irn) - 1; i >= 0; --i) {
813 ir_node *succ = get_irn_out(irn, i);
815 switch (get_irn_opcode(succ)) {
817 /* check if this load is not a hidden conversion */
818 mode = get_Load_mode(succ);
819 ent = is_SymConst(irn) ? get_SymConst_entity(irn) : get_Sel_entity(irn);
820 emode = get_type_mode(get_entity_type(ent));
821 if (is_hidden_cast(mode, emode))
822 return ir_address_taken;
826 /* check that the node is not the Store's value */
827 value = get_Store_value(succ);
829 return ir_address_taken;
830 /* check if this Store is not a hidden conversion */
831 mode = get_irn_mode(value);
832 ent = is_SymConst(irn) ? get_SymConst_entity(irn) : get_Sel_entity(irn);
833 emode = get_type_mode(get_entity_type(ent));
834 if (is_hidden_cast(mode, emode))
835 return ir_address_taken;
839 /* Check the successor of irn. */
840 ir_address_taken_state res = find_address_taken_state(succ);
841 if (res != ir_address_not_taken)
847 /* Only the call address is not an address taker but
848 this is an uninteresting case, so we ignore it here. */
849 for (j = get_Call_n_params(succ) - 1; j >= 0; --j) {
850 ir_node *param = get_Call_param(succ, j);
852 return ir_address_taken;
857 /* another op, the address may be taken */
858 return ir_address_taken_unknown;
861 /* All successors finished, the address is not taken. */
862 return ir_address_not_taken;
863 } /* find_address_taken_state */
866 * Update the "address taken" flag of all frame entities.
868 static void analyse_irg_address_taken(ir_graph *irg) {
869 ir_type *ft = get_irg_frame_type(irg);
873 /* set initial state to not_taken, as this is the "smallest" state */
874 for (i = get_class_n_members(ft) - 1; i >= 0; --i) {
875 ir_entity *ent = get_class_member(ft, i);
877 set_entity_address_taken(ent, ir_address_not_taken);
880 assure_irg_outs(irg);
882 irg_frame = get_irg_frame(irg);
884 for (i = get_irn_n_outs(irg_frame) - 1; i >= 0; --i) {
885 ir_node *succ = get_irn_out(irg_frame, i);
886 ir_address_taken_state state;
889 ir_entity *ent = get_Sel_entity(succ);
891 if (get_entity_address_taken(ent) == ir_address_taken)
894 state = find_address_taken_state(succ);
895 if (state > get_entity_address_taken(ent))
896 set_entity_address_taken(ent, state);
900 irg->adr_taken_state = ir_address_taken_computed;
901 } /* analyse_address_taken */
903 /* Returns the current address taken state of the graph. */
904 ir_address_taken_computed_state get_irg_address_taken_state(const ir_graph *irg) {
905 return irg->adr_taken_state;
906 } /* get_irg_address_taken_state */
908 /* Sets the current address taken state of the graph. */
909 void set_irg_address_taken_state(ir_graph *irg, ir_address_taken_computed_state state) {
910 irg->adr_taken_state = state;
911 } /* set_irg_address_taken_state */
913 /* Assure that the address taken flag is computed for the given graph. */
914 void assure_irg_address_taken_computed(ir_graph *irg) {
915 if (irg->adr_taken_state == ir_address_taken_not_computed)
916 analyse_irg_address_taken(irg);
917 } /* assure_irg_address_taken_computed */
921 * Initialize the address_taken flag for a global type like type.
923 static void init_taken_flag(ir_type * tp) {
926 /* All external visible entities are at least
927 ir_address_taken_unknown. This is very conservative. */
928 for (i = get_compound_n_members(tp) - 1; i >= 0; --i) {
929 ir_entity *ent = get_compound_member(tp, i);
930 ir_address_taken_state state;
932 state = get_entity_visibility(ent) == visibility_external_visible ?
933 ir_address_taken_unknown : ir_address_not_taken ;
934 set_entity_address_taken(ent, state);
936 } /* init_taken_flag */
939 * Mark all entities used in the initializer for the given entity as address taken
941 static void check_initializer(ir_entity *ent) {
945 /* do not check uninitialized values */
946 if (get_entity_variability(ent) == variability_uninitialized)
949 /* Beware: Methods initialized with "themself". This does not count as a taken
951 if (is_Method_type(get_entity_type(ent)))
954 if (is_atomic_entity(ent)) {
955 /* let's check if it's an address */
956 n = get_atomic_ent_value(ent);
957 if (is_SymConst(n) && get_SymConst_kind(n) == symconst_addr_ent) {
958 ir_entity *ent = get_SymConst_entity(n);
959 set_entity_address_taken(ent, ir_address_taken);
962 for (i = get_compound_ent_n_values(ent) - 1; i >= 0; --i) {
963 n = get_compound_ent_value(ent, i);
965 /* let's check if it's an address */
966 if (is_SymConst(n) && get_SymConst_kind(n) == symconst_addr_ent) {
967 ir_entity *ent = get_SymConst_entity(n);
968 set_entity_address_taken(ent, ir_address_taken);
972 } /* check_initializer */
976 * Mark all entities used in initializers as address taken
978 static void check_initializers(ir_type *tp) {
981 for (i = get_compound_n_members(tp) - 1; i >= 0; --i) {
982 ir_entity *ent = get_compound_member(tp, i);
984 check_initializer(ent);
986 } /* check_initializers */
990 * Print the address taken state of all entities of a given type for debugging.
992 static void print_address_taken_state(ir_type *tp) {
994 for (i = get_compound_n_members(tp) - 1; i >= 0; --i) {
995 ir_entity *ent = get_compound_member(tp, i);
996 ir_address_taken_state state = get_entity_address_taken(ent);
998 if (state != ir_address_not_taken) {
999 assert(ir_address_not_taken <= (int) state && state <= ir_address_taken);
1000 ir_printf("%+F: %s\n", ent, get_address_taken_state_name(state));
1003 } /* print_address_taken_state */
1004 #endif /* DEBUG_libfirm */
1007 * Post-walker: check for global entity address
1009 static void check_global_address(ir_node *irn, void *env) {
1012 ir_address_taken_state state;
1014 if (is_SymConst(irn) && get_SymConst_kind(irn) == symconst_addr_ent) {
1016 ent = get_SymConst_entity(irn);
1017 } else if (is_Sel(irn) && get_Sel_ptr(irn) == tls) {
1018 /* A TLS variable. */
1019 ent = get_Sel_entity(irn);
1023 if (get_entity_address_taken(ent) >= ir_address_taken) {
1024 /* Already at the maximum. */
1027 state = find_address_taken_state(irn);
1028 if (state > get_entity_address_taken(ent))
1029 set_entity_address_taken(ent, state);
1030 } /* check_global_address */
1033 * Update the "address taken" flag of all global entities.
1035 static void analyse_irp_globals_address_taken(void) {
1038 FIRM_DBG_REGISTER(dbg, "firm.ana.irmemory");
1040 init_taken_flag(get_glob_type());
1041 init_taken_flag(get_tls_type());
1043 check_initializers(get_glob_type());
1044 check_initializers(get_tls_type());
1046 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
1047 ir_graph *irg = get_irp_irg(i);
1049 assure_irg_outs(irg);
1050 irg_walk_graph(irg, NULL, check_global_address, get_irg_tls(irg));
1053 #ifdef DEBUG_libfirm
1054 if (firm_dbg_get_mask(dbg) & LEVEL_1) {
1055 print_address_taken_state(get_glob_type());
1056 print_address_taken_state(get_tls_type());
1058 #endif /* DEBUG_libfirm */
1061 irp->globals_adr_taken_state = ir_address_taken_computed;
1062 } /* analyse_irp_globals_address_taken */
1064 /* Returns the current address taken state of the globals. */
1065 ir_address_taken_computed_state get_irp_globals_address_taken_state(void) {
1066 return irp->globals_adr_taken_state;
1067 } /* get_irp_globals_address_taken_state */
1069 /* Sets the current address taken state of the graph. */
1070 void set_irp_globals_address_taken_state(ir_address_taken_computed_state state) {
1071 irp->globals_adr_taken_state = state;
1072 } /* set_irg_address_taken_state */
1074 /* Assure that the address taken flag is computed for the globals. */
1075 void assure_irp_globals_address_taken_computed(void) {
1076 if (irp->globals_adr_taken_state == ir_address_taken_not_computed)
1077 analyse_irp_globals_address_taken();
1078 } /* assure_irp_globals_address_taken_computed */
1081 DEBUG_ONLY(static firm_dbg_module_t *dbgcall = NULL;)
1084 * Copy the calling conventions from the entities to the call type.
1086 static void update_calls(ir_node *call, void *env) {
1088 if (is_Call(call)) {
1089 ir_node *ptr = get_Call_ptr(call);
1091 if (is_SymConst(ptr)) {
1092 ir_entity *ent = get_SymConst_entity(ptr);
1093 ir_type *mtp = get_entity_type(ent);
1094 ir_type *ctp = get_Call_type(call);
1096 if (mtp != ctp && get_method_additional_properties(mtp) & mtp_property_private) {
1097 set_method_additional_property(ctp, mtp_property_private);
1098 DB((dbgcall, LEVEL_1, "changed call to private method %+F\n", ent));
1104 /* Mark all private methods, i.e. those of which all call sites are known. */
1105 void mark_private_methods(void) {
1109 FIRM_DBG_REGISTER(dbgcall, "firm.opt.cc");
1111 assure_irp_globals_address_taken_computed();
1113 /* first step: change the calling conventions of the local non-escaped entities */
1114 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
1115 ir_graph *irg = get_irp_irg(i);
1116 ir_entity *ent = get_irg_entity(irg);
1117 ir_address_taken_state state = get_entity_address_taken(ent);
1119 if (get_entity_visibility(ent) == visibility_local &&
1120 state == ir_address_not_taken) {
1121 ir_type *mtp = get_entity_type(ent);
1123 set_method_additional_property(mtp, mtp_property_private);
1125 DB((dbgcall, LEVEL_1, "found private method %+F\n", ent));
1130 all_irg_walk(NULL, update_calls, NULL);