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"
28 /** The source language specific language disambiguator function. */
29 static DISAMBIGUATOR_FUNC language_disambuigator = NULL;
32 * Find the base address of an Sel node.
34 static ir_node *find_base_adr(ir_node *sel) {
35 ir_node *ptr = get_Sel_ptr(sel);
39 ptr = get_Sel_ptr(sel);
45 * Two address expressions have the same base address,
46 * check if there offsets are different.
48 * @param adr1 The first address.
49 * @param adr2 The second address.
51 static ir_alias_relation different_offsets(ir_node *adr1, ir_node *adr2) {
53 } /* different_offsets */
56 * Determine the alias relation by checking if adr1 and adr2 are pointer
59 static ir_alias_relation different_types(ir_node *adr1, ir_node *adr2)
61 ir_entity *ent1 = NULL, *ent2 = NULL;
63 if (is_SymConst(adr1) && get_SymConst_kind(adr1) == symconst_addr_ent)
64 ent1 = get_SymConst_entity(adr1);
65 else if (is_Sel(adr1))
66 ent1 = get_Sel_entity(adr1);
68 if (is_SymConst(adr2) && get_SymConst_kind(adr2) == symconst_addr_ent)
69 ent2 = get_SymConst_entity(adr2);
70 else if (is_Sel(adr2))
71 ent2 = get_Sel_entity(adr2);
73 if (ent1 != NULL && ent2 != NULL) {
74 ir_type *tp1 = get_entity_type(ent1);
75 ir_type *tp2 = get_entity_type(ent2);
78 if (is_Pointer_type(tp1) && is_Pointer_type(tp2)) {
79 /* do deref until no pointer types are found */
81 tp1 = get_pointer_points_to_type(tp1);
82 tp2 = get_pointer_points_to_type(tp2);
83 } while (is_Pointer_type(tp1) && is_Pointer_type(tp2));
86 if (get_type_tpop(tp1) != get_type_tpop(tp2)) {
87 /* different type structure */
90 if (is_Class_type(tp1)) {
91 /* check class hierarchy */
92 if (! is_SubClass_of(tp1, tp2) &&
93 ! is_SubClass_of(tp2, tp1))
102 } /* different_types */
105 * Determine the alias relation between two addresses.
107 static ir_alias_relation _get_alias_relation(
109 ir_node *adr1, ir_mode *mode1,
110 ir_node *adr2, ir_mode *mode2,
114 ir_entity *ent1, *ent2;
116 if (! get_opt_alias_analysis())
122 /* Two save some code, sort the addresses by its id's. Beware, this
123 might break some things, so better check here. */
124 assert(iro_SymConst < iro_Sel && "Code dependence breaked");
125 op1 = get_irn_opcode(adr1);
126 op2 = get_irn_opcode(adr2);
137 if (is_SymConst(adr1) && get_SymConst_kind(adr1) == symconst_addr_ent) {
138 /* first address is a global variable */
140 if (is_SymConst(adr2) && get_SymConst_kind(adr2) == symconst_addr_ent) {
141 /* both addresses are global variables and we know
142 they are different (R1 a) */
143 if (get_SymConst_entity(adr1) != get_SymConst_entity(adr2))
149 ir_node *base = find_base_adr(adr2);
151 if (is_SymConst(base) && get_SymConst_kind(base) == symconst_addr_ent) {
152 /* base address is a global var (R1 a) */
156 return different_offsets(adr1, adr2);
157 } else if (base == get_irg_frame(irg)) {
158 /* the second one is a local variable so they are always
161 } else if (base == get_irg_tls(irg)) {
162 /* the second one is a TLS variable so they are always
168 /* Here we are: the first is a global var, the second some pointer. */
169 ent1 = get_SymConst_entity(adr1);
170 if (get_entity_address_taken(ent1) == ir_address_not_taken) {
171 /* The address of the global variable was never taken, so
172 the pointer cannot match (R2). */
175 } else if (is_Sel(adr1)) {
176 /* the first address is a Sel */
177 ir_node *base1 = find_base_adr(adr1);
179 if (base1 == get_irg_frame(irg)) {
180 /* the first is a local variable */
182 /* the second address is a Sel */
183 ir_node *base2 = find_base_adr(adr2);
185 if (base2 == get_irg_frame(irg)) {
186 /* the second one is a local variable */
187 } else if (base2 == get_irg_tls(irg)) {
188 /* the second one is a TLS variable so they are always
193 } else if (base1 == get_irg_tls(irg)) {
194 /* the first is a TLS variable */
196 /* the second address is a Sel */
197 ir_node *base2 = find_base_adr(adr2);
199 if (base2 == get_irg_frame(irg)) {
200 /* the second one is a local variable so they are always
203 } else if (base2 == get_irg_tls(irg)) {
204 /* the second one is a TLS variable */
210 if (options & opt_strong_typed) {
212 ir_alias_relation rel = different_types(adr1, adr2);
213 if (rel != may_alias)
217 /* do we have a language specific memory disambiguator? */
218 if (language_disambuigator) {
219 ir_alias_relation rel = (*language_disambuigator)(irg, adr1, mode1, adr2, mode2);
220 if (rel != may_alias)
224 /* access points-to information here */
226 } /* _get_alias_relation */
229 * Determine the alias relation between two addresses.
231 ir_alias_relation get_alias_relation(
233 ir_node *adr1, ir_mode *mode1,
234 ir_node *adr2, ir_mode *mode2,
237 ir_alias_relation rel = _get_alias_relation(irg, adr1, mode1, adr2, mode2, options);
239 } /* get_alias_relation */
241 /* Set a source language specific memory disambiguator function. */
242 void set_language_memory_disambiguator(DISAMBIGUATOR_FUNC func) {
243 language_disambuigator = func;
244 } /* set_language_memory_disambiguator */
246 /** The result cache for the memory disambiguator. */
247 static set *result_cache = NULL;
249 /** An entry in the relation cache. */
250 typedef struct mem_disambig_entry {
251 ir_node *adr1; /**< The first address. */
252 ir_node *adr2; /**< The second address. */
253 ir_alias_relation result; /**< The alias relation result. */
254 } mem_disambig_entry;
256 #define HASH_ENTRY(adr1, adr2) (HASH_PTR(adr1) ^ HASH_PTR(adr2))
259 * Compare two relation cache entries.
261 static int cmp_mem_disambig_entry(const void *elt, const void *key, size_t size) {
262 const mem_disambig_entry *p1 = elt;
263 const mem_disambig_entry *p2 = key;
265 return p1->adr1 == p2->adr1 && p1->adr2 == p2->adr2;
266 } /* cmp_mem_disambig_entry */
269 * Initialize the relation cache.
271 void mem_disambig_init(void) {
272 result_cache = new_set(cmp_mem_disambig_entry, 8);
273 } /* mem_disambig_init */
276 * Determine the alias relation between two addresses.
278 ir_alias_relation get_alias_relation_ex(
280 ir_node *adr1, ir_mode *mode1,
281 ir_node *adr2, ir_mode *mode2,
284 mem_disambig_entry key, *entry;
286 if (! get_opt_alias_analysis())
289 if (get_irn_opcode(adr1) > get_irn_opcode(adr2)) {
297 entry = set_find(result_cache, &key, sizeof(key), HASH_ENTRY(adr1, adr2));
299 return entry->result;
301 key.result = get_alias_relation(irg, adr1, mode1, adr2, mode2, options);
303 set_insert(result_cache, &key, sizeof(key), HASH_ENTRY(adr1, adr2));
305 } /* get_alias_relation_ex */
307 /* Free the relation cache. */
308 void mem_disambig_term(void) {
310 del_set(result_cache);
313 } /* mem_disambig_term */
316 * Check the mode of a Load/Store with the mode of the entity
318 * If the mode of the entity and the Load/Store mode do not match, we
319 * have the bad reinterpret case:
322 * char b = *(char *)&i;
324 * We do NOT count this as one value and return address_taken
326 * However, we support an often used case. If the mode is two-complement
327 * we allow casts between signed/unsigned.
329 * @param mode the mode of the Load/Store
330 * @param ent_mode the mode of the accessed entity
332 * @return non-zero if the Load/Store is a hidden cast, zero else
334 static int is_hidden_cast(ir_mode *mode, ir_mode *ent_mode) {
335 if (ent_mode != mode) {
336 if (ent_mode == NULL ||
337 get_mode_size_bits(ent_mode) != get_mode_size_bits(mode) ||
338 get_mode_sort(ent_mode) != get_mode_sort(mode) ||
339 get_mode_arithmetic(ent_mode) != irma_twos_complement ||
340 get_mode_arithmetic(mode) != irma_twos_complement)
344 } /* is_hidden_cast */
347 * Determine the address_taken state of a node (or it's successor Sels).
349 * @param irn the node
351 static ir_address_taken_state find_address_taken_state(ir_node *irn) {
353 ir_mode *emode, *mode;
357 for (i = get_irn_n_outs(irn) - 1; i >= 0; --i) {
358 ir_node *succ = get_irn_out(irn, i);
360 switch (get_irn_opcode(succ)) {
362 /* check if this load is not a hidden conversion */
363 mode = get_Load_mode(succ);
364 ent = is_SymConst(irn) ? get_SymConst_entity(irn) : get_Sel_entity(irn);
365 emode = get_type_mode(get_entity_type(ent));
366 if (is_hidden_cast(mode, emode))
367 return ir_address_taken;
371 /* check that the node is not the Store's value */
372 value = get_Store_value(succ);
374 return ir_address_taken;
375 /* check if this Store is not a hidden conversion */
376 mode = get_irn_mode(value);
377 ent = is_SymConst(irn) ? get_SymConst_entity(irn) : get_Sel_entity(irn);
378 emode = get_type_mode(get_entity_type(ent));
379 if (is_hidden_cast(mode, emode))
380 return ir_address_taken;
384 /* Check the successor of irn. */
385 ir_address_taken_state res = find_address_taken_state(succ);
386 if (res != ir_address_not_taken)
392 /* Only the call address is not an address taker but
393 this is an uninteresting case, so we ignore it here. */
394 return ir_address_taken;
397 /* another op, the address may be taken */
398 return ir_address_taken_unknown;
401 /* All successors finished, the address is not taken. */
402 return ir_address_not_taken;
403 } /* find_address_taken_state */
406 * Update the "address taken" flag of all frame entities.
408 static void analyse_irg_address_taken(ir_graph *irg) {
409 ir_type *ft = get_irg_frame_type(irg);
413 /* set initial state to not_taken, as this is the "smallest" state */
414 for (i = get_class_n_members(ft) - 1; i >= 0; --i) {
415 entity *ent = get_class_member(ft, i);
417 set_entity_address_taken(ent, ir_address_not_taken);
420 assure_irg_outs(irg);
422 irg_frame = get_irg_frame(irg);
424 for (i = get_irn_n_outs(irg_frame) - 1; i >= 0; --i) {
425 ir_node *succ = get_irn_out(irg_frame, i);
426 ir_address_taken_state state;
429 ir_entity *ent = get_Sel_entity(succ);
431 if (get_entity_address_taken(ent) == ir_address_taken)
434 state = find_address_taken_state(succ);
435 if (state > get_entity_address_taken(ent))
436 set_entity_address_taken(ent, state);
440 irg->adr_taken_state = ir_address_taken_computed;
441 } /* analyse_address_taken */
443 /* Returns the current address taken state of the graph. */
444 ir_address_taken_computed_state get_irg_address_taken_state(const ir_graph *irg) {
445 return irg->adr_taken_state;
446 } /* get_irg_address_taken_state */
448 /* Sets the current address taken state of the graph. */
449 void set_irg_address_taken_state(ir_graph *irg, ir_address_taken_computed_state state) {
450 irg->adr_taken_state = state;
451 } /* set_irg_address_taken_state */
453 /* Assure that the address taken flag is computed for the given graph. */
454 void assure_irg_address_taken_computed(ir_graph *irg) {
455 if (irg->adr_taken_state == ir_address_taken_not_computed)
456 analyse_irg_address_taken(irg);
457 } /* assure_irg_address_taken_computed */
460 * Initialize the address_taken flag for a global type like type.
462 static void init_taken_flag(ir_type * tp) {
465 /* All external visible entities are at least
466 ir_address_taken_unknown. This is very conservative. */
467 for (i = get_compound_n_members(tp) - 1; i >= 0; --i) {
468 entity *ent = get_compound_member(tp, i);
469 ir_address_taken_state state;
471 state = get_entity_visibility(ent) == visibility_external_visible ?
472 ir_address_taken_unknown : ir_address_not_taken ;
473 set_entity_address_taken(ent, state);
475 } /* init_taken_flag */
478 * Print the address taken state of all entities of a given type for debugging.
480 static void print_address_taken_state(ir_type *tp) {
482 for (i = get_compound_n_members(tp) - 1; i >= 0; --i) {
483 entity *ent = get_compound_member(tp, i);
484 ir_address_taken_state state = get_entity_address_taken(ent);
486 if (state != ir_address_not_taken) {
487 assert(ir_address_not_taken <= state && state <= ir_address_taken);
488 ir_printf("%+F: %s\n", ent, get_address_taken_state_name(state));
491 } /* print_address_taken_state */
494 * Post-walker: check for global entity address
496 static void check_global_address(ir_node *irn, void *env) {
499 ir_address_taken_state state;
501 if (is_SymConst(irn) && get_SymConst_kind(irn) == symconst_addr_ent) {
503 ent = get_SymConst_entity(irn);
504 } else if (is_Sel(irn) && get_Sel_ptr(irn) == tls) {
505 /* A TLS variable. */
506 ent = get_SymConst_entity(irn);
510 if (get_entity_address_taken(ent) == ir_address_not_taken) {
511 /* Already at the maximum. */
514 state = find_address_taken_state(irn);
515 if (state > get_entity_address_taken(ent))
516 set_entity_address_taken(ent, state);
517 } /* check_global_address */
520 * Update the "address taken" flag of all global entities.
522 static void analyse_irp_globals_address_taken(void) {
525 init_taken_flag(get_glob_type());
526 init_taken_flag(get_tls_type());
528 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
529 ir_graph *irg = get_irp_irg(i);
531 assure_irg_outs(irg);
532 irg_walk_graph(irg, NULL, check_global_address, get_irg_tls(irg));
534 print_address_taken_state(get_glob_type());
535 print_address_taken_state(get_tls_type());
538 irp->globals_adr_taken_state = ir_address_taken_computed;
539 } /* analyse_irp_globals_address_taken */
541 /* Returns the current address taken state of the globals. */
542 ir_address_taken_computed_state get_irp_globals_address_taken_state(void) {
543 return irp->globals_adr_taken_state;
544 } /* get_irp_globals_address_taken_state */
546 /* Sets the current address taken state of the graph. */
547 void set_irp_globals_address_taken_state(ir_address_taken_computed_state state) {
548 irp->globals_adr_taken_state = state;
549 } /* set_irg_address_taken_state */
551 /* Assure that the address taken flag is computed for the globals. */
552 void assure_irp_globals_address_taken_computed(void) {
553 if (irp->globals_adr_taken_state == ir_address_taken_not_computed)
554 analyse_irp_globals_address_taken();
555 } /* assure_irp_globals_address_taken_computed */