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"
44 /** The source language specific language disambiguator function. */
45 static DISAMBIGUATOR_FUNC language_disambuigator = NULL;
47 /** The global memory disambiguator options. */
48 static unsigned global_mem_disamgig_opt = aa_opt_no_opt;
50 /* Get the memory disambiguator options for a graph. */
51 unsigned get_irg_memory_disambiguator_options(ir_graph *irg) {
52 unsigned opt = irg->mem_disambig_opt;
53 if (opt & aa_opt_inherited)
54 return global_mem_disamgig_opt;
56 } /* get_irg_memory_disambiguator_options */
58 /* Set the memory disambiguator options for a graph. */
59 void set_irg_memory_disambiguator_options(ir_graph *irg, unsigned options) {
60 irg->mem_disambig_opt = options & ~aa_opt_inherited;
61 } /* set_irg_memory_disambiguator_options */
63 /* Set the global disambiguator options for all graphs not having local options. */
64 void set_irp_memory_disambiguator_options(unsigned options) {
65 global_mem_disamgig_opt = options;
66 } /* set_irp_memory_disambiguator_options */
69 * Find the base address and entity of an Sel node.
72 * @param pEnt after return points to the base entity.
74 * @return the base address.
76 static ir_node *find_base_adr(ir_node *sel, ir_entity **pEnt) {
77 ir_node *ptr = get_Sel_ptr(sel);
81 ptr = get_Sel_ptr(sel);
83 *pEnt = get_Sel_entity(sel);
88 * Two address expressions have the same base address,
89 * check if there offsets are different.
91 * @param adr1 The first address.
92 * @param adr2 The second address.
94 static ir_alias_relation different_offsets(ir_node *adr1, ir_node *adr2) {
96 } /* different_offsets */
99 * Determine the alias relation by checking if adr1 and adr2 are pointer
102 * @param adr1 The first address.
103 * @param adr2 The second address.
105 static ir_alias_relation different_types(ir_node *adr1, ir_node *adr2)
107 ir_entity *ent1 = NULL, *ent2 = NULL;
109 if (is_SymConst(adr1) && get_SymConst_kind(adr1) == symconst_addr_ent)
110 ent1 = get_SymConst_entity(adr1);
111 else if (is_Sel(adr1))
112 ent1 = get_Sel_entity(adr1);
114 if (is_SymConst(adr2) && get_SymConst_kind(adr2) == symconst_addr_ent)
115 ent2 = get_SymConst_entity(adr2);
116 else if (is_Sel(adr2))
117 ent2 = get_Sel_entity(adr2);
119 if (ent1 != NULL && ent2 != NULL) {
120 ir_type *tp1 = get_entity_type(ent1);
121 ir_type *tp2 = get_entity_type(ent2);
124 if (is_Pointer_type(tp1) && is_Pointer_type(tp2)) {
125 /* do deref until no pointer types are found */
127 tp1 = get_pointer_points_to_type(tp1);
128 tp2 = get_pointer_points_to_type(tp2);
129 } while (is_Pointer_type(tp1) && is_Pointer_type(tp2));
132 if (get_type_tpop(tp1) != get_type_tpop(tp2)) {
133 /* different type structure */
136 if (is_Class_type(tp1)) {
137 /* check class hierarchy */
138 if (! is_SubClass_of(tp1, tp2) &&
139 ! is_SubClass_of(tp2, tp1))
142 /* different types */
148 } /* different_types */
151 * Returns non-zero if a node is a routine parameter.
153 * @param node the node to test
155 static int is_arg_Proj(ir_node *node) {
158 node = get_Proj_pred(node);
161 return pn_Start_T_args == get_Proj_proj(node) && is_Start(get_Proj_pred(node));
165 * Determine the alias relation between two addresses.
167 static ir_alias_relation _get_alias_relation(
169 ir_node *adr1, ir_mode *mode1,
170 ir_node *adr2, ir_mode *mode2)
173 ir_entity *ent1, *ent2;
176 if (! get_opt_alias_analysis())
182 options = get_irg_memory_disambiguator_options(irg);
184 /* The Armageddon switch */
185 if (options & aa_opt_no_alias)
188 /* Two save some code, sort the addresses by its id's. Beware, this
189 might break some things, so better check here. */
190 assert(iro_SymConst < iro_Sel && iro_Sel < iro_Proj && "Code dependence breaked");
191 op1 = get_irn_opcode(adr1);
192 op2 = get_irn_opcode(adr2);
203 if (is_SymConst(adr1) && get_SymConst_kind(adr1) == symconst_addr_ent) {
204 /* first address is a global variable */
206 if (is_SymConst(adr2) && get_SymConst_kind(adr2) == symconst_addr_ent) {
207 /* both addresses are global variables and we know
208 they are different (R1 a) */
209 if (get_SymConst_entity(adr1) != get_SymConst_entity(adr2))
212 /* equal addresses */
217 ir_node *base = find_base_adr(adr2, &ent2);
219 if (is_SymConst(base) && get_SymConst_kind(base) == symconst_addr_ent) {
220 /* base address is a global var (R1 a) */
224 return different_offsets(adr1, adr2);
225 } else if (base == get_irg_frame(irg)) {
226 /* the second one is a local variable so they are always
229 } else if (base == get_irg_tls(irg)) {
230 /* the second one is a TLS variable so they are always
236 /* Here we are: the first is a global var, the second some pointer. */
237 ent1 = get_SymConst_entity(adr1);
238 if (get_entity_address_taken(ent1) == ir_address_not_taken) {
239 /* The address of the global variable was never taken, so
240 the pointer cannot match (R2). */
243 } else if (is_Sel(adr1)) {
244 /* the first address is a Sel */
245 ir_node *base1 = find_base_adr(adr1, &ent1);
247 if (base1 == get_irg_frame(irg)) {
248 /* the first is a local variable */
250 /* the second address is a Sel */
251 ir_node *base2 = find_base_adr(adr2, &ent2);
253 if (base2 == get_irg_frame(irg)) {
254 /* both addresses are local variables and we know
255 they are different (R1 a) */
259 return different_offsets(adr1, adr2);
260 } else if (base2 == get_irg_tls(irg)) {
261 /* the second one is a TLS variable so they are always
264 } else if (is_arg_Proj(base2)) {
265 /* the second one is an offset from a parameter so they are
266 always different (R1 e) */
269 } else if (is_arg_Proj(adr2)) {
270 /* a local variable and a parameter are always different (R1 e) */
273 } else if (base1 == get_irg_tls(irg)) {
274 /* the first is a TLS variable */
276 /* the second address is a Sel */
277 ir_node *base2 = find_base_adr(adr2, &ent2);
279 if (base2 == get_irg_frame(irg)) {
280 /* the second one is a local variable so they are always
283 } else if (base2 == get_irg_tls(irg)) {
284 /* both addresses are TLS variables and we know
285 they are different (R1 a) */
289 return different_offsets(adr1, adr2);
292 } else if (is_arg_Proj(base1)) {
293 /* the first one is an offset from a parameter */
295 /* the second address is a Sel */
296 ir_node *base2 = find_base_adr(adr2, &ent2);
298 if (base2 == get_irg_frame(irg)) {
299 /* the second one is a local variable so they are always
307 if (options & aa_opt_type_based) { /* Type based alias analysis */
308 ir_alias_relation rel;
310 if (options & aa_opt_byte_type_may_alias) {
311 if (get_mode_size_bits(mode1) == 8 || get_mode_size_bits(mode2) == 8) {
312 /* One of the modes address a byte. Assume a may_alias and leave
313 the type based check. */
314 goto leave_type_based_alias;
317 /* cheap check: If the mode sizes did not match, the types MUST be different */
318 if (get_mode_size_bits(mode1) != get_mode_size_bits(mode2))
322 rel = different_types(adr1, adr2);
323 if (rel != may_alias)
325 leave_type_based_alias:;
328 /* do we have a language specific memory disambiguator? */
329 if (language_disambuigator) {
330 ir_alias_relation rel = (*language_disambuigator)(irg, adr1, mode1, adr2, mode2);
331 if (rel != may_alias)
335 /* access points-to information here */
337 } /* _get_alias_relation */
340 * Determine the alias relation between two addresses.
342 ir_alias_relation get_alias_relation(
344 ir_node *adr1, ir_mode *mode1,
345 ir_node *adr2, ir_mode *mode2)
347 ir_alias_relation rel = _get_alias_relation(irg, adr1, mode1, adr2, mode2);
349 } /* get_alias_relation */
351 /* Set a source language specific memory disambiguator function. */
352 void set_language_memory_disambiguator(DISAMBIGUATOR_FUNC func) {
353 language_disambuigator = func;
354 } /* set_language_memory_disambiguator */
356 /** The result cache for the memory disambiguator. */
357 static set *result_cache = NULL;
359 /** An entry in the relation cache. */
360 typedef struct mem_disambig_entry {
361 ir_node *adr1; /**< The first address. */
362 ir_node *adr2; /**< The second address. */
363 ir_alias_relation result; /**< The alias relation result. */
364 } mem_disambig_entry;
366 #define HASH_ENTRY(adr1, adr2) (HASH_PTR(adr1) ^ HASH_PTR(adr2))
369 * Compare two relation cache entries.
371 static int cmp_mem_disambig_entry(const void *elt, const void *key, size_t size) {
372 const mem_disambig_entry *p1 = elt;
373 const mem_disambig_entry *p2 = key;
375 return p1->adr1 == p2->adr1 && p1->adr2 == p2->adr2;
376 } /* cmp_mem_disambig_entry */
379 * Initialize the relation cache.
381 void mem_disambig_init(void) {
382 result_cache = new_set(cmp_mem_disambig_entry, 8);
383 } /* mem_disambig_init */
386 * Determine the alias relation between two addresses.
388 ir_alias_relation get_alias_relation_ex(
390 ir_node *adr1, ir_mode *mode1,
391 ir_node *adr2, ir_mode *mode2)
393 mem_disambig_entry key, *entry;
395 if (! get_opt_alias_analysis())
398 if (get_irn_opcode(adr1) > get_irn_opcode(adr2)) {
406 entry = set_find(result_cache, &key, sizeof(key), HASH_ENTRY(adr1, adr2));
408 return entry->result;
410 key.result = get_alias_relation(irg, adr1, mode1, adr2, mode2);
412 set_insert(result_cache, &key, sizeof(key), HASH_ENTRY(adr1, adr2));
414 } /* get_alias_relation_ex */
416 /* Free the relation cache. */
417 void mem_disambig_term(void) {
419 del_set(result_cache);
422 } /* mem_disambig_term */
425 * Check the mode of a Load/Store with the mode of the entity
427 * If the mode of the entity and the Load/Store mode do not match, we
428 * have the bad reinterpret case:
431 * char b = *(char *)&i;
433 * We do NOT count this as one value and return address_taken
435 * However, we support an often used case. If the mode is two-complement
436 * we allow casts between signed/unsigned.
438 * @param mode the mode of the Load/Store
439 * @param ent_mode the mode of the accessed entity
441 * @return non-zero if the Load/Store is a hidden cast, zero else
443 static int is_hidden_cast(ir_mode *mode, ir_mode *ent_mode) {
444 if (ent_mode != mode) {
445 if (ent_mode == NULL ||
446 get_mode_size_bits(ent_mode) != get_mode_size_bits(mode) ||
447 get_mode_sort(ent_mode) != get_mode_sort(mode) ||
448 get_mode_arithmetic(ent_mode) != irma_twos_complement ||
449 get_mode_arithmetic(mode) != irma_twos_complement)
453 } /* is_hidden_cast */
456 * Determine the address_taken state of a node (or it's successor Sels).
458 * @param irn the node
460 static ir_address_taken_state find_address_taken_state(ir_node *irn) {
462 ir_mode *emode, *mode;
466 for (i = get_irn_n_outs(irn) - 1; i >= 0; --i) {
467 ir_node *succ = get_irn_out(irn, i);
469 switch (get_irn_opcode(succ)) {
471 /* check if this load is not a hidden conversion */
472 mode = get_Load_mode(succ);
473 ent = is_SymConst(irn) ? get_SymConst_entity(irn) : get_Sel_entity(irn);
474 emode = get_type_mode(get_entity_type(ent));
475 if (is_hidden_cast(mode, emode))
476 return ir_address_taken;
480 /* check that the node is not the Store's value */
481 value = get_Store_value(succ);
483 return ir_address_taken;
484 /* check if this Store is not a hidden conversion */
485 mode = get_irn_mode(value);
486 ent = is_SymConst(irn) ? get_SymConst_entity(irn) : get_Sel_entity(irn);
487 emode = get_type_mode(get_entity_type(ent));
488 if (is_hidden_cast(mode, emode))
489 return ir_address_taken;
493 /* Check the successor of irn. */
494 ir_address_taken_state res = find_address_taken_state(succ);
495 if (res != ir_address_not_taken)
501 /* Only the call address is not an address taker but
502 this is an uninteresting case, so we ignore it here. */
503 return ir_address_taken;
506 /* another op, the address may be taken */
507 return ir_address_taken_unknown;
510 /* All successors finished, the address is not taken. */
511 return ir_address_not_taken;
512 } /* find_address_taken_state */
515 * Update the "address taken" flag of all frame entities.
517 static void analyse_irg_address_taken(ir_graph *irg) {
518 ir_type *ft = get_irg_frame_type(irg);
522 /* set initial state to not_taken, as this is the "smallest" state */
523 for (i = get_class_n_members(ft) - 1; i >= 0; --i) {
524 ir_entity *ent = get_class_member(ft, i);
526 set_entity_address_taken(ent, ir_address_not_taken);
529 assure_irg_outs(irg);
531 irg_frame = get_irg_frame(irg);
533 for (i = get_irn_n_outs(irg_frame) - 1; i >= 0; --i) {
534 ir_node *succ = get_irn_out(irg_frame, i);
535 ir_address_taken_state state;
538 ir_entity *ent = get_Sel_entity(succ);
540 if (get_entity_address_taken(ent) == ir_address_taken)
543 state = find_address_taken_state(succ);
544 if (state > get_entity_address_taken(ent))
545 set_entity_address_taken(ent, state);
549 irg->adr_taken_state = ir_address_taken_computed;
550 } /* analyse_address_taken */
552 /* Returns the current address taken state of the graph. */
553 ir_address_taken_computed_state get_irg_address_taken_state(const ir_graph *irg) {
554 return irg->adr_taken_state;
555 } /* get_irg_address_taken_state */
557 /* Sets the current address taken state of the graph. */
558 void set_irg_address_taken_state(ir_graph *irg, ir_address_taken_computed_state state) {
559 irg->adr_taken_state = state;
560 } /* set_irg_address_taken_state */
562 /* Assure that the address taken flag is computed for the given graph. */
563 void assure_irg_address_taken_computed(ir_graph *irg) {
564 if (irg->adr_taken_state == ir_address_taken_not_computed)
565 analyse_irg_address_taken(irg);
566 } /* assure_irg_address_taken_computed */
569 * Initialize the address_taken flag for a global type like type.
571 static void init_taken_flag(ir_type * tp) {
574 /* All external visible entities are at least
575 ir_address_taken_unknown. This is very conservative. */
576 for (i = get_compound_n_members(tp) - 1; i >= 0; --i) {
577 ir_entity *ent = get_compound_member(tp, i);
578 ir_address_taken_state state;
580 state = get_entity_visibility(ent) == visibility_external_visible ?
581 ir_address_taken_unknown : ir_address_not_taken ;
582 set_entity_address_taken(ent, state);
584 } /* init_taken_flag */
588 * Print the address taken state of all entities of a given type for debugging.
590 static void print_address_taken_state(ir_type *tp) {
592 for (i = get_compound_n_members(tp) - 1; i >= 0; --i) {
593 ir_entity *ent = get_compound_member(tp, i);
594 ir_address_taken_state state = get_entity_address_taken(ent);
596 if (state != ir_address_not_taken) {
597 assert(ir_address_not_taken <= state && state <= ir_address_taken);
598 ir_printf("%+F: %s\n", ent, get_address_taken_state_name(state));
601 } /* print_address_taken_state */
605 * Post-walker: check for global entity address
607 static void check_global_address(ir_node *irn, void *env) {
610 ir_address_taken_state state;
612 if (is_SymConst(irn) && get_SymConst_kind(irn) == symconst_addr_ent) {
614 ent = get_SymConst_entity(irn);
615 } else if (is_Sel(irn) && get_Sel_ptr(irn) == tls) {
616 /* A TLS variable. */
617 ent = get_Sel_entity(irn);
621 if (get_entity_address_taken(ent) >= ir_address_taken) {
622 /* Already at the maximum. */
625 state = find_address_taken_state(irn);
626 if (state > get_entity_address_taken(ent))
627 set_entity_address_taken(ent, state);
628 } /* check_global_address */
631 * Update the "address taken" flag of all global entities.
633 static void analyse_irp_globals_address_taken(void) {
636 init_taken_flag(get_glob_type());
637 init_taken_flag(get_tls_type());
639 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
640 ir_graph *irg = get_irp_irg(i);
642 assure_irg_outs(irg);
643 irg_walk_graph(irg, NULL, check_global_address, get_irg_tls(irg));
645 //print_address_taken_state(get_glob_type());
646 //print_address_taken_state(get_tls_type());
649 irp->globals_adr_taken_state = ir_address_taken_computed;
650 } /* analyse_irp_globals_address_taken */
652 /* Returns the current address taken state of the globals. */
653 ir_address_taken_computed_state get_irp_globals_address_taken_state(void) {
654 return irp->globals_adr_taken_state;
655 } /* get_irp_globals_address_taken_state */
657 /* Sets the current address taken state of the graph. */
658 void set_irp_globals_address_taken_state(ir_address_taken_computed_state state) {
659 irp->globals_adr_taken_state = state;
660 } /* set_irg_address_taken_state */
662 /* Assure that the address taken flag is computed for the globals. */
663 void assure_irp_globals_address_taken_computed(void) {
664 if (irp->globals_adr_taken_state == ir_address_taken_not_computed)
665 analyse_irp_globals_address_taken();
666 } /* assure_irp_globals_address_taken_computed */