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;
33 * Find the base address of an Sel node.
35 static ir_node *find_base_adr(ir_node *sel) {
36 ir_node *ptr = get_Sel_ptr(sel);
40 ptr = get_Sel_ptr(sel);
46 * Two address expressions have the same base address,
47 * check if there offsets are different.
49 * @param adr1 The first address.
50 * @param adr2 The second address.
52 static ir_alias_relation different_offsets(ir_node *adr1, ir_node *adr2) {
54 } /* different_offsets */
57 * Determine the alias relation by checking if adr1 and adr2 are pointer
60 static ir_alias_relation different_types(ir_node *adr1, ir_node *adr2)
62 ir_entity *ent1 = NULL, *ent2 = NULL;
64 if (is_SymConst(adr1) && get_SymConst_kind(adr1) == symconst_addr_ent)
65 ent1 = get_SymConst_entity(adr1);
66 else if (is_Sel(adr1))
67 ent1 = get_Sel_entity(adr1);
69 if (is_SymConst(adr2) && get_SymConst_kind(adr2) == symconst_addr_ent)
70 ent2 = get_SymConst_entity(adr2);
71 else if (is_Sel(adr2))
72 ent2 = get_Sel_entity(adr2);
74 if (ent1 != NULL && ent2 != NULL) {
75 ir_type *tp1 = get_entity_type(ent1);
76 ir_type *tp2 = get_entity_type(ent2);
79 if (is_Pointer_type(tp1) && is_Pointer_type(tp2)) {
80 /* do deref until no pointer types are found */
82 tp1 = get_pointer_points_to_type(tp1);
83 tp2 = get_pointer_points_to_type(tp2);
84 } while (is_Pointer_type(tp1) && is_Pointer_type(tp2));
87 if (get_type_tpop(tp1) != get_type_tpop(tp2)) {
88 /* different type structure */
91 if (is_Class_type(tp1)) {
92 /* check class hierarchy */
93 if (! is_SubClass_of(tp1, tp2) &&
94 ! is_SubClass_of(tp2, tp1))
103 } /* different_types */
106 * Determine the alias relation between two addresses.
108 static ir_alias_relation _get_alias_relation(
110 ir_node *adr1, ir_mode *mode1,
111 ir_node *adr2, ir_mode *mode2,
117 if (! get_opt_alias_analysis())
123 /* Two save some code, sort the addresses by its id's. Beware, this
124 might break some things, so better check here. */
125 assert(iro_SymConst < iro_Sel && "Code dependence breaked");
126 op1 = get_irn_opcode(adr1);
127 op2 = get_irn_opcode(adr2);
138 if (is_SymConst(adr1) && get_SymConst_kind(adr1) == symconst_addr_ent) {
139 /* first address is a global variable */
141 if (is_SymConst(adr2) && get_SymConst_kind(adr2) == symconst_addr_ent) {
142 /* both addresses are global variables and we know
143 they are different (R1 a) */
144 if (get_SymConst_entity(adr1) != get_SymConst_entity(adr2))
150 ir_node *base = find_base_adr(adr2);
152 if (is_SymConst(base) && get_SymConst_kind(base) == symconst_addr_ent) {
153 /* base address is a global var (R1 a) */
157 return different_offsets(adr1, adr2);
158 } else if (base == get_irg_frame(irg)) {
159 /* the second one is a local variable so they are always
162 } else if (base == get_irg_tls(irg)) {
163 /* the second one is a TLS variable so they are always
169 /* Here we are: the first is a global var, the second some pointer. */
170 ent1 = get_SymConst_entity(adr1);
171 if (get_entity_address_taken(ent1) == ir_address_not_taken) {
172 /* The address of the global variable was never taken, so
173 the pointer cannot match (R2). */
176 } else if (is_Sel(adr1)) {
177 /* the first address is a Sel */
178 ir_node *base1 = find_base_adr(adr1);
180 if (base1 == get_irg_frame(irg)) {
181 /* the first is a local variable */
183 /* the second address is a Sel */
184 ir_node *base2 = find_base_adr(adr2);
186 if (base2 == get_irg_frame(irg)) {
187 /* the second one is a local variable */
188 } else if (base2 == get_irg_tls(irg)) {
189 /* the second one is a TLS variable so they are always
194 } else if (base1 == get_irg_tls(irg)) {
195 /* the first is a TLS variable */
197 /* the second address is a Sel */
198 ir_node *base2 = find_base_adr(adr2);
200 if (base2 == get_irg_frame(irg)) {
201 /* the second one is a local variable so they are always
204 } else if (base2 == get_irg_tls(irg)) {
205 /* the second one is a TLS variable */
211 if (options & opt_strong_typed) {
213 ir_alias_relation rel = different_types(adr1, adr2);
214 if (rel != may_alias)
218 /* do we have a language specific memory disambiguator? */
219 if (language_disambuigator) {
220 ir_alias_relation rel = (*language_disambuigator)(irg, adr1, mode1, adr2, mode2);
221 if (rel != may_alias)
225 /* access points-to information here */
227 } /* _get_alias_relation */
230 * Determine the alias relation between two addresses.
232 ir_alias_relation get_alias_relation(
234 ir_node *adr1, ir_mode *mode1,
235 ir_node *adr2, ir_mode *mode2,
238 ir_alias_relation rel = _get_alias_relation(irg, adr1, mode1, adr2, mode2, options);
240 } /* get_alias_relation */
242 /* Set a source language specific memory disambiguator function. */
243 void set_language_memory_disambiguator(DISAMBIGUATOR_FUNC func) {
244 language_disambuigator = func;
245 } /* set_language_memory_disambiguator */
247 /** The result cache for the memory disambiguator. */
248 static set *result_cache = NULL;
250 /** An entry in the relation cache. */
251 typedef struct mem_disambig_entry {
252 ir_node *adr1; /**< The first address. */
253 ir_node *adr2; /**< The second address. */
254 ir_alias_relation result; /**< The alias relation result. */
255 } mem_disambig_entry;
257 #define HASH_ENTRY(adr1, adr2) (HASH_PTR(adr1) ^ HASH_PTR(adr2))
260 * Compare two relation cache entries.
262 static int cmp_mem_disambig_entry(const void *elt, const void *key, size_t size) {
263 const mem_disambig_entry *p1 = elt;
264 const mem_disambig_entry *p2 = key;
266 return p1->adr1 == p2->adr1 && p1->adr2 == p2->adr2;
267 } /* cmp_mem_disambig_entry */
270 * Initialize the relation cache.
272 void mem_disambig_init(void) {
273 result_cache = new_set(cmp_mem_disambig_entry, 8);
274 } /* mem_disambig_init */
277 * Determine the alias relation between two addresses.
279 ir_alias_relation get_alias_relation_ex(
281 ir_node *adr1, ir_mode *mode1,
282 ir_node *adr2, ir_mode *mode2,
285 mem_disambig_entry key, *entry;
287 if (! get_opt_alias_analysis())
290 if (get_irn_opcode(adr1) > get_irn_opcode(adr2)) {
298 entry = set_find(result_cache, &key, sizeof(key), HASH_ENTRY(adr1, adr2));
300 return entry->result;
302 key.result = get_alias_relation(irg, adr1, mode1, adr2, mode2, options);
304 set_insert(result_cache, &key, sizeof(key), HASH_ENTRY(adr1, adr2));
306 } /* get_alias_relation_ex */
308 /* Free the relation cache. */
309 void mem_disambig_term(void) {
311 del_set(result_cache);
314 } /* mem_disambig_term */
317 * Check the mode of a Load/Store with the mode of the entity
319 * If the mode of the entity and the Load/Store mode do not match, we
320 * have the bad reinterpret case:
323 * char b = *(char *)&i;
325 * We do NOT count this as one value and return address_taken
327 * However, we support an often used case. If the mode is two-complement
328 * we allow casts between signed/unsigned.
330 * @param mode the mode of the Load/Store
331 * @param ent_mode the mode of the accessed entity
333 * @return non-zero if the Load/Store is a hidden cast, zero else
335 static int is_hidden_cast(ir_mode *mode, ir_mode *ent_mode) {
336 if (ent_mode != mode) {
337 if (ent_mode == NULL ||
338 get_mode_size_bits(ent_mode) != get_mode_size_bits(mode) ||
339 get_mode_sort(ent_mode) != get_mode_sort(mode) ||
340 get_mode_arithmetic(ent_mode) != irma_twos_complement ||
341 get_mode_arithmetic(mode) != irma_twos_complement)
345 } /* is_hidden_cast */
348 * Determine the address_taken state of a node (or it's successor Sels).
350 * @param irn the node
352 static ir_address_taken_state find_address_taken_state(ir_node *irn) {
354 ir_mode *emode, *mode;
358 for (i = get_irn_n_outs(irn) - 1; i >= 0; --i) {
359 ir_node *succ = get_irn_out(irn, i);
361 switch (get_irn_opcode(succ)) {
363 /* check if this load is not a hidden conversion */
364 mode = get_Load_mode(succ);
365 ent = is_SymConst(irn) ? get_SymConst_entity(irn) : get_Sel_entity(irn);
366 emode = get_type_mode(get_entity_type(ent));
367 if (is_hidden_cast(mode, emode))
368 return ir_address_taken;
372 /* check that the node is not the Store's value */
373 value = get_Store_value(succ);
375 return ir_address_taken;
376 /* check if this Store is not a hidden conversion */
377 mode = get_irn_mode(value);
378 ent = is_SymConst(irn) ? get_SymConst_entity(irn) : get_Sel_entity(irn);
379 emode = get_type_mode(get_entity_type(ent));
380 if (is_hidden_cast(mode, emode))
381 return ir_address_taken;
385 /* Check the successor of irn. */
386 ir_address_taken_state res = find_address_taken_state(succ);
387 if (res != ir_address_not_taken)
393 /* Only the call address is not an address taker but
394 this is an uninteresting case, so we ignore it here. */
395 return ir_address_taken;
398 /* another op, the address may be taken */
399 return ir_address_taken_unknown;
402 /* All successors finished, the address is not taken. */
403 return ir_address_not_taken;
404 } /* find_address_taken_state */
407 * Update the "address taken" flag of all frame entities.
409 static void analyse_irg_address_taken(ir_graph *irg) {
410 ir_type *ft = get_irg_frame_type(irg);
414 /* set initial state to not_taken, as this is the "smallest" state */
415 for (i = get_class_n_members(ft) - 1; i >= 0; --i) {
416 entity *ent = get_class_member(ft, i);
418 set_entity_address_taken(ent, ir_address_not_taken);
421 assure_irg_outs(irg);
423 irg_frame = get_irg_frame(irg);
425 for (i = get_irn_n_outs(irg_frame) - 1; i >= 0; --i) {
426 ir_node *succ = get_irn_out(irg_frame, i);
427 ir_address_taken_state state;
430 ir_entity *ent = get_Sel_entity(succ);
432 if (get_entity_address_taken(ent) == ir_address_taken)
435 state = find_address_taken_state(succ);
436 if (state > get_entity_address_taken(ent))
437 set_entity_address_taken(ent, state);
441 irg->adr_taken_state = ir_address_taken_computed;
442 } /* analyse_address_taken */
444 /* Returns the current address taken state of the graph. */
445 ir_address_taken_computed_state get_irg_address_taken_state(const ir_graph *irg) {
446 return irg->adr_taken_state;
447 } /* get_irg_address_taken_state */
449 /* Sets the current address taken state of the graph. */
450 void set_irg_address_taken_state(ir_graph *irg, ir_address_taken_computed_state state) {
451 irg->adr_taken_state = state;
452 } /* set_irg_address_taken_state */
454 /* Assure that the address taken flag is computed for the given graph. */
455 void assure_irg_address_taken_computed(ir_graph *irg) {
456 if (irg->adr_taken_state == ir_address_taken_not_computed)
457 analyse_irg_address_taken(irg);
458 } /* assure_irg_address_taken_computed */
461 * Initialize the address_taken flag for a global type like type.
463 static void init_taken_flag(ir_type * tp) {
466 /* All external visible entities are at least
467 ir_address_taken_unknown. This is very conservative. */
468 for (i = get_compound_n_members(tp) - 1; i >= 0; --i) {
469 entity *ent = get_compound_member(tp, i);
470 ir_address_taken_state state;
472 state = get_entity_visibility(ent) == visibility_external_visible ?
473 ir_address_taken_unknown : ir_address_not_taken ;
474 set_entity_address_taken(ent, state);
476 } /* init_taken_flag */
479 * Print the address taken state of all entities of a given type for debugging.
481 static void print_address_taken_state(ir_type *tp) {
483 for (i = get_compound_n_members(tp) - 1; i >= 0; --i) {
484 entity *ent = get_compound_member(tp, i);
485 ir_address_taken_state state = get_entity_address_taken(ent);
487 if (state != ir_address_not_taken) {
488 assert(ir_address_not_taken <= state && state <= ir_address_taken);
489 ir_printf("%+F: %s\n", ent, get_address_taken_state_name(state));
492 } /* print_address_taken_state */
495 * Post-walker: check for global entity address
497 static void check_global_address(ir_node *irn, void *env) {
500 ir_address_taken_state state;
502 if (is_SymConst(irn) && get_SymConst_kind(irn) == symconst_addr_ent) {
504 ent = get_SymConst_entity(irn);
505 } else if (is_Sel(irn) && get_Sel_ptr(irn) == tls) {
506 /* A TLS variable. */
507 ent = get_SymConst_entity(irn);
511 if (get_entity_address_taken(ent) == ir_address_not_taken) {
512 /* Already at the maximum. */
515 state = find_address_taken_state(irn);
516 if (state > get_entity_address_taken(ent))
517 set_entity_address_taken(ent, state);
518 } /* check_global_address */
521 * Update the "address taken" flag of all global entities.
523 static void analyse_irp_globals_address_taken(void) {
526 init_taken_flag(get_glob_type());
527 init_taken_flag(get_tls_type());
529 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
530 ir_graph *irg = get_irp_irg(i);
532 assure_irg_outs(irg);
533 irg_walk_graph(irg, NULL, check_global_address, get_irg_tls(irg));
535 print_address_taken_state(get_glob_type());
536 print_address_taken_state(get_tls_type());
539 irp->globals_adr_taken_state = ir_address_taken_computed;
540 } /* analyse_irp_globals_address_taken */
542 /* Returns the current address taken state of the globals. */
543 ir_address_taken_computed_state get_irp_globals_address_taken_state(void) {
544 return irp->globals_adr_taken_state;
545 } /* get_irp_globals_address_taken_state */
547 /* Sets the current address taken state of the graph. */
548 void set_irp_globals_address_taken_state(ir_address_taken_computed_state state) {
549 irp->globals_adr_taken_state = state;
550 } /* set_irg_address_taken_state */
552 /* Assure that the address taken flag is computed for the globals. */
553 void assure_irp_globals_address_taken_computed(void) {
554 if (irp->globals_adr_taken_state == ir_address_taken_not_computed)
555 analyse_irp_globals_address_taken();
556 } /* assure_irp_globals_address_taken_computed */