3 * File name: ir/ana/irmemory.c
4 * Purpose: Memory disambiguator
9 * Copyright: (c) 2006-2007 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
19 #include "irgraph_t.h"
29 /** The source language specific language disambiguator function. */
30 static DISAMBIGUATOR_FUNC language_disambuigator = NULL;
32 /** The global memory disambiguator options. */
33 static unsigned global_mem_disamgig_opt = aa_opt_no_opt;
35 /* Get the memory disambiguator options for a graph. */
36 unsigned get_irg_memory_disambiguator_options(ir_graph *irg) {
37 unsigned opt = irg->mem_disambig_opt;
38 if (opt & aa_opt_inherited)
39 return global_mem_disamgig_opt;
41 } /* get_irg_memory_disambiguator_options */
43 /* Set the memory disambiguator options for a graph. */
44 void set_irg_memory_disambiguator_options(ir_graph *irg, unsigned options) {
45 irg->mem_disambig_opt = options & ~aa_opt_inherited;
46 } /* set_irg_memory_disambiguator_options */
48 /* Set the global disambiguator options for all graphs not having local options. */
49 void set_irp_memory_disambiguator_options(unsigned options) {
50 global_mem_disamgig_opt = options;
51 } /* set_irp_memory_disambiguator_options */
54 * Find the base address and entity of an Sel node.
57 * @param pEnt after return points to the base entity.
59 * @return the base address.
61 static ir_node *find_base_adr(ir_node *sel, ir_entity **pEnt) {
62 ir_node *ptr = get_Sel_ptr(sel);
66 ptr = get_Sel_ptr(sel);
68 *pEnt = get_Sel_entity(sel);
73 * Two address expressions have the same base address,
74 * check if there offsets are different.
76 * @param adr1 The first address.
77 * @param adr2 The second address.
79 static ir_alias_relation different_offsets(ir_node *adr1, ir_node *adr2) {
81 } /* different_offsets */
84 * Determine the alias relation by checking if adr1 and adr2 are pointer
87 * @param adr1 The first address.
88 * @param adr2 The second address.
90 static ir_alias_relation different_types(ir_node *adr1, ir_node *adr2)
92 ir_entity *ent1 = NULL, *ent2 = NULL;
94 if (is_SymConst(adr1) && get_SymConst_kind(adr1) == symconst_addr_ent)
95 ent1 = get_SymConst_entity(adr1);
96 else if (is_Sel(adr1))
97 ent1 = get_Sel_entity(adr1);
99 if (is_SymConst(adr2) && get_SymConst_kind(adr2) == symconst_addr_ent)
100 ent2 = get_SymConst_entity(adr2);
101 else if (is_Sel(adr2))
102 ent2 = get_Sel_entity(adr2);
104 if (ent1 != NULL && ent2 != NULL) {
105 ir_type *tp1 = get_entity_type(ent1);
106 ir_type *tp2 = get_entity_type(ent2);
109 if (is_Pointer_type(tp1) && is_Pointer_type(tp2)) {
110 /* do deref until no pointer types are found */
112 tp1 = get_pointer_points_to_type(tp1);
113 tp2 = get_pointer_points_to_type(tp2);
114 } while (is_Pointer_type(tp1) && is_Pointer_type(tp2));
117 if (get_type_tpop(tp1) != get_type_tpop(tp2)) {
118 /* different type structure */
121 if (is_Class_type(tp1)) {
122 /* check class hierarchy */
123 if (! is_SubClass_of(tp1, tp2) &&
124 ! is_SubClass_of(tp2, tp1))
127 /* different types */
133 } /* different_types */
136 * Determine the alias relation between two addresses.
138 static ir_alias_relation _get_alias_relation(
140 ir_node *adr1, ir_mode *mode1,
141 ir_node *adr2, ir_mode *mode2)
144 ir_entity *ent1, *ent2;
147 if (! get_opt_alias_analysis())
153 options = get_irg_memory_disambiguator_options(irg);
155 /* The Armageddon switch */
156 if (options & aa_opt_no_alias)
159 /* Two save some code, sort the addresses by its id's. Beware, this
160 might break some things, so better check here. */
161 assert(iro_SymConst < iro_Sel && "Code dependence breaked");
162 op1 = get_irn_opcode(adr1);
163 op2 = get_irn_opcode(adr2);
174 if (is_SymConst(adr1) && get_SymConst_kind(adr1) == symconst_addr_ent) {
175 /* first address is a global variable */
177 if (is_SymConst(adr2) && get_SymConst_kind(adr2) == symconst_addr_ent) {
178 /* both addresses are global variables and we know
179 they are different (R1 a) */
180 if (get_SymConst_entity(adr1) != get_SymConst_entity(adr2))
183 /* equal addresses */
188 ir_node *base = find_base_adr(adr2, &ent2);
190 if (is_SymConst(base) && get_SymConst_kind(base) == symconst_addr_ent) {
191 /* base address is a global var (R1 a) */
195 return different_offsets(adr1, adr2);
196 } else if (base == get_irg_frame(irg)) {
197 /* the second one is a local variable so they are always
200 } else if (base == get_irg_tls(irg)) {
201 /* the second one is a TLS variable so they are always
207 /* Here we are: the first is a global var, the second some pointer. */
208 ent1 = get_SymConst_entity(adr1);
209 if (get_entity_address_taken(ent1) == ir_address_not_taken) {
210 /* The address of the global variable was never taken, so
211 the pointer cannot match (R2). */
214 } else if (is_Sel(adr1)) {
215 /* the first address is a Sel */
216 ir_node *base1 = find_base_adr(adr1, &ent1);
218 if (base1 == get_irg_frame(irg)) {
219 /* the first is a local variable */
221 /* the second address is a Sel */
222 ir_node *base2 = find_base_adr(adr2, &ent2);
224 if (base2 == get_irg_frame(irg)) {
225 /* both addresses are local variables and we know
226 they are different (R1 a) */
230 return different_offsets(adr1, adr2);
231 } else if (base2 == get_irg_tls(irg)) {
232 /* the second one is a TLS variable so they are always
237 } else if (base1 == get_irg_tls(irg)) {
238 /* the first is a TLS variable */
240 /* the second address is a Sel */
241 ir_node *base2 = find_base_adr(adr2, &ent2);
243 if (base2 == get_irg_frame(irg)) {
244 /* the second one is a local variable so they are always
247 } else if (base2 == get_irg_tls(irg)) {
248 /* both addresses are TLS variables and we know
249 they are different (R1 a) */
253 return different_offsets(adr1, adr2);
259 if (options & aa_opt_type_based) {
260 ir_alias_relation rel;
262 if (options & aa_opt_byte_type_may_alias) {
263 if (get_mode_size_bits(mode1) == 8 || get_mode_size_bits(mode2) == 8) {
264 /* One of the modes address a byte. Assume a may_alias ant leave
265 the type based check. */
266 goto leave_type_based_alias;
269 /* cheap check: If the mode sizes did not match, the types MUST be different */
270 if (get_mode_size_bits(mode1) != get_mode_size_bits(mode2))
274 rel = different_types(adr1, adr2);
275 if (rel != may_alias)
277 leave_type_based_alias:;
280 /* do we have a language specific memory disambiguator? */
281 if (language_disambuigator) {
282 ir_alias_relation rel = (*language_disambuigator)(irg, adr1, mode1, adr2, mode2);
283 if (rel != may_alias)
287 /* access points-to information here */
289 } /* _get_alias_relation */
292 * Determine the alias relation between two addresses.
294 ir_alias_relation get_alias_relation(
296 ir_node *adr1, ir_mode *mode1,
297 ir_node *adr2, ir_mode *mode2)
299 ir_alias_relation rel = _get_alias_relation(irg, adr1, mode1, adr2, mode2);
301 } /* get_alias_relation */
303 /* Set a source language specific memory disambiguator function. */
304 void set_language_memory_disambiguator(DISAMBIGUATOR_FUNC func) {
305 language_disambuigator = func;
306 } /* set_language_memory_disambiguator */
308 /** The result cache for the memory disambiguator. */
309 static set *result_cache = NULL;
311 /** An entry in the relation cache. */
312 typedef struct mem_disambig_entry {
313 ir_node *adr1; /**< The first address. */
314 ir_node *adr2; /**< The second address. */
315 ir_alias_relation result; /**< The alias relation result. */
316 } mem_disambig_entry;
318 #define HASH_ENTRY(adr1, adr2) (HASH_PTR(adr1) ^ HASH_PTR(adr2))
321 * Compare two relation cache entries.
323 static int cmp_mem_disambig_entry(const void *elt, const void *key, size_t size) {
324 const mem_disambig_entry *p1 = elt;
325 const mem_disambig_entry *p2 = key;
327 return p1->adr1 == p2->adr1 && p1->adr2 == p2->adr2;
328 } /* cmp_mem_disambig_entry */
331 * Initialize the relation cache.
333 void mem_disambig_init(void) {
334 result_cache = new_set(cmp_mem_disambig_entry, 8);
335 } /* mem_disambig_init */
338 * Determine the alias relation between two addresses.
340 ir_alias_relation get_alias_relation_ex(
342 ir_node *adr1, ir_mode *mode1,
343 ir_node *adr2, ir_mode *mode2)
345 mem_disambig_entry key, *entry;
347 if (! get_opt_alias_analysis())
350 if (get_irn_opcode(adr1) > get_irn_opcode(adr2)) {
358 entry = set_find(result_cache, &key, sizeof(key), HASH_ENTRY(adr1, adr2));
360 return entry->result;
362 key.result = get_alias_relation(irg, adr1, mode1, adr2, mode2);
364 set_insert(result_cache, &key, sizeof(key), HASH_ENTRY(adr1, adr2));
366 } /* get_alias_relation_ex */
368 /* Free the relation cache. */
369 void mem_disambig_term(void) {
371 del_set(result_cache);
374 } /* mem_disambig_term */
377 * Check the mode of a Load/Store with the mode of the entity
379 * If the mode of the entity and the Load/Store mode do not match, we
380 * have the bad reinterpret case:
383 * char b = *(char *)&i;
385 * We do NOT count this as one value and return address_taken
387 * However, we support an often used case. If the mode is two-complement
388 * we allow casts between signed/unsigned.
390 * @param mode the mode of the Load/Store
391 * @param ent_mode the mode of the accessed entity
393 * @return non-zero if the Load/Store is a hidden cast, zero else
395 static int is_hidden_cast(ir_mode *mode, ir_mode *ent_mode) {
396 if (ent_mode != mode) {
397 if (ent_mode == NULL ||
398 get_mode_size_bits(ent_mode) != get_mode_size_bits(mode) ||
399 get_mode_sort(ent_mode) != get_mode_sort(mode) ||
400 get_mode_arithmetic(ent_mode) != irma_twos_complement ||
401 get_mode_arithmetic(mode) != irma_twos_complement)
405 } /* is_hidden_cast */
408 * Determine the address_taken state of a node (or it's successor Sels).
410 * @param irn the node
412 static ir_address_taken_state find_address_taken_state(ir_node *irn) {
414 ir_mode *emode, *mode;
418 for (i = get_irn_n_outs(irn) - 1; i >= 0; --i) {
419 ir_node *succ = get_irn_out(irn, i);
421 switch (get_irn_opcode(succ)) {
423 /* check if this load is not a hidden conversion */
424 mode = get_Load_mode(succ);
425 ent = is_SymConst(irn) ? get_SymConst_entity(irn) : get_Sel_entity(irn);
426 emode = get_type_mode(get_entity_type(ent));
427 if (is_hidden_cast(mode, emode))
428 return ir_address_taken;
432 /* check that the node is not the Store's value */
433 value = get_Store_value(succ);
435 return ir_address_taken;
436 /* check if this Store is not a hidden conversion */
437 mode = get_irn_mode(value);
438 ent = is_SymConst(irn) ? get_SymConst_entity(irn) : get_Sel_entity(irn);
439 emode = get_type_mode(get_entity_type(ent));
440 if (is_hidden_cast(mode, emode))
441 return ir_address_taken;
445 /* Check the successor of irn. */
446 ir_address_taken_state res = find_address_taken_state(succ);
447 if (res != ir_address_not_taken)
453 /* Only the call address is not an address taker but
454 this is an uninteresting case, so we ignore it here. */
455 return ir_address_taken;
458 /* another op, the address may be taken */
459 return ir_address_taken_unknown;
462 /* All successors finished, the address is not taken. */
463 return ir_address_not_taken;
464 } /* find_address_taken_state */
467 * Update the "address taken" flag of all frame entities.
469 static void analyse_irg_address_taken(ir_graph *irg) {
470 ir_type *ft = get_irg_frame_type(irg);
474 /* set initial state to not_taken, as this is the "smallest" state */
475 for (i = get_class_n_members(ft) - 1; i >= 0; --i) {
476 ir_entity *ent = get_class_member(ft, i);
478 set_entity_address_taken(ent, ir_address_not_taken);
481 assure_irg_outs(irg);
483 irg_frame = get_irg_frame(irg);
485 for (i = get_irn_n_outs(irg_frame) - 1; i >= 0; --i) {
486 ir_node *succ = get_irn_out(irg_frame, i);
487 ir_address_taken_state state;
490 ir_entity *ent = get_Sel_entity(succ);
492 if (get_entity_address_taken(ent) == ir_address_taken)
495 state = find_address_taken_state(succ);
496 if (state > get_entity_address_taken(ent))
497 set_entity_address_taken(ent, state);
501 irg->adr_taken_state = ir_address_taken_computed;
502 } /* analyse_address_taken */
504 /* Returns the current address taken state of the graph. */
505 ir_address_taken_computed_state get_irg_address_taken_state(const ir_graph *irg) {
506 return irg->adr_taken_state;
507 } /* get_irg_address_taken_state */
509 /* Sets the current address taken state of the graph. */
510 void set_irg_address_taken_state(ir_graph *irg, ir_address_taken_computed_state state) {
511 irg->adr_taken_state = state;
512 } /* set_irg_address_taken_state */
514 /* Assure that the address taken flag is computed for the given graph. */
515 void assure_irg_address_taken_computed(ir_graph *irg) {
516 if (irg->adr_taken_state == ir_address_taken_not_computed)
517 analyse_irg_address_taken(irg);
518 } /* assure_irg_address_taken_computed */
521 * Initialize the address_taken flag for a global type like type.
523 static void init_taken_flag(ir_type * tp) {
526 /* All external visible entities are at least
527 ir_address_taken_unknown. This is very conservative. */
528 for (i = get_compound_n_members(tp) - 1; i >= 0; --i) {
529 ir_entity *ent = get_compound_member(tp, i);
530 ir_address_taken_state state;
532 state = get_entity_visibility(ent) == visibility_external_visible ?
533 ir_address_taken_unknown : ir_address_not_taken ;
534 set_entity_address_taken(ent, state);
536 } /* init_taken_flag */
539 * Print the address taken state of all entities of a given type for debugging.
541 static void print_address_taken_state(ir_type *tp) {
543 for (i = get_compound_n_members(tp) - 1; i >= 0; --i) {
544 ir_entity *ent = get_compound_member(tp, i);
545 ir_address_taken_state state = get_entity_address_taken(ent);
547 if (state != ir_address_not_taken) {
548 assert(ir_address_not_taken <= state && state <= ir_address_taken);
549 ir_printf("%+F: %s\n", ent, get_address_taken_state_name(state));
552 } /* print_address_taken_state */
555 * Post-walker: check for global entity address
557 static void check_global_address(ir_node *irn, void *env) {
560 ir_address_taken_state state;
562 if (is_SymConst(irn) && get_SymConst_kind(irn) == symconst_addr_ent) {
564 ent = get_SymConst_entity(irn);
565 } else if (is_Sel(irn) && get_Sel_ptr(irn) == tls) {
566 /* A TLS variable. */
567 ent = get_SymConst_entity(irn);
571 if (get_entity_address_taken(ent) == ir_address_not_taken) {
572 /* Already at the maximum. */
575 state = find_address_taken_state(irn);
576 if (state > get_entity_address_taken(ent))
577 set_entity_address_taken(ent, state);
578 } /* check_global_address */
581 * Update the "address taken" flag of all global entities.
583 static void analyse_irp_globals_address_taken(void) {
586 init_taken_flag(get_glob_type());
587 init_taken_flag(get_tls_type());
589 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
590 ir_graph *irg = get_irp_irg(i);
592 assure_irg_outs(irg);
593 irg_walk_graph(irg, NULL, check_global_address, get_irg_tls(irg));
595 //print_address_taken_state(get_glob_type());
596 //print_address_taken_state(get_tls_type());
599 irp->globals_adr_taken_state = ir_address_taken_computed;
600 } /* analyse_irp_globals_address_taken */
602 /* Returns the current address taken state of the globals. */
603 ir_address_taken_computed_state get_irp_globals_address_taken_state(void) {
604 return irp->globals_adr_taken_state;
605 } /* get_irp_globals_address_taken_state */
607 /* Sets the current address taken state of the graph. */
608 void set_irp_globals_address_taken_state(ir_address_taken_computed_state state) {
609 irp->globals_adr_taken_state = state;
610 } /* set_irg_address_taken_state */
612 /* Assure that the address taken flag is computed for the globals. */
613 void assure_irp_globals_address_taken_computed(void) {
614 if (irp->globals_adr_taken_state == ir_address_taken_not_computed)
615 analyse_irp_globals_address_taken();
616 } /* assure_irp_globals_address_taken_computed */