66343ec90ddc7b963e4a799ccedb88c868e03920
[libfirm] / ir / ana / irmemory.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/ana/irmemory.c
4  * Purpose:     Memory disambiguator
5  * Author:      Michael Beck
6  * Modified by:
7  * Created:     27.12.2006
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 2006-2007 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12 #ifdef HAVE_CONFIG_H
13 #include "config.h"
14 #endif
15
16 #include <stdlib.h>
17
18 #include "irnode_t.h"
19 #include "irgraph_t.h"
20 #include "irprog_t.h"
21 #include "irmemory.h"
22 #include "irflag.h"
23 #include "hashptr.h"
24 #include "irflag.h"
25 #include "irouts.h"
26 #include "irgwalk.h"
27
28 /** The source language specific language disambiguator function. */
29 static DISAMBIGUATOR_FUNC language_disambuigator = NULL;
30
31 /**
32  * Find the base address of an Sel node.
33  */
34 static ir_node *find_base_adr(ir_node *sel) {
35         ir_node *ptr = get_Sel_ptr(sel);
36
37         while (is_Sel(ptr)) {
38                 sel = ptr;
39                 ptr = get_Sel_ptr(sel);
40         }
41         return ptr;
42 }  /* find_base_adr */
43
44 /**
45  * Two address expressions have the same base address,
46  * check if there offsets are different.
47  *
48  * @param adr1  The first address.
49  * @param adr2  The second address.
50  */
51 static ir_alias_relation different_offsets(ir_node *adr1, ir_node *adr2) {
52         return may_alias;
53 }  /* different_offsets */
54
55 /**
56  * Determine the alias relation by checking if adr1 and adr2 are pointer
57  * to different type.
58  */
59 static ir_alias_relation different_types(ir_node *adr1, ir_node *adr2)
60 {
61         ir_entity *ent1 = NULL, *ent2 = NULL;
62
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);
67
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);
72
73         if (ent1 != NULL && ent2 != NULL) {
74                 ir_type *tp1 = get_entity_type(ent1);
75                 ir_type *tp2 = get_entity_type(ent2);
76
77                 if (tp1 != tp2) {
78                         if (is_Pointer_type(tp1) && is_Pointer_type(tp2)) {
79                                 /* do deref until no pointer types are found */
80                                 do {
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));
84                         }
85
86                         if (get_type_tpop(tp1) != get_type_tpop(tp2)) {
87                                 /* different type structure */
88                                 return no_alias;
89                         }
90                         if (is_Class_type(tp1)) {
91                                 /* check class hierarchy */
92                                 if (! is_SubClass_of(tp1, tp2) &&
93                                         ! is_SubClass_of(tp2, tp1))
94                                         return no_alias;
95                         } else {
96                                 /* different types */
97                                 return no_alias;
98                         }
99                 }
100         }
101         return may_alias;
102 }  /* different_types */
103
104 /**
105  * Determine the alias relation between two addresses.
106  */
107 static ir_alias_relation _get_alias_relation(
108         ir_graph *irg,
109         ir_node *adr1, ir_mode *mode1,
110         ir_node *adr2, ir_mode *mode2,
111         unsigned options)
112 {
113         opcode op1, op2;
114         ir_entity *ent1, *ent2;
115
116         if (! get_opt_alias_analysis())
117                 return may_alias;
118
119         if (adr1 == adr2)
120                 return sure_alias;
121
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);
127
128         if (op1 > op2) {
129                 ir_node *t = adr1;
130                 ir_mode *m = mode1;
131                 adr1  = adr2;
132                 mode1 = mode2;
133                 adr2  = t;
134                 mode2 = m;
135         }
136
137         if (is_SymConst(adr1) && get_SymConst_kind(adr1) == symconst_addr_ent) {
138                 /* first address is a global variable */
139
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))
144                                 return no_alias;
145                         else
146                                 return sure_alias;
147                 }
148                 if (is_Sel(adr2)) {
149                         ir_node *base = find_base_adr(adr2);
150
151                         if (is_SymConst(base) && get_SymConst_kind(base) == symconst_addr_ent) {
152                                 /* base address is a global var (R1 a) */
153                                 if (adr1 != base)
154                                         return no_alias;
155                                 if (base == adr1)
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
159                                    different (R1 b) */
160                                 return no_alias;
161                         } else if (base == get_irg_tls(irg)) {
162                                 /* the second one is a TLS variable so they are always
163                                    different (R1 c) */
164                                 return no_alias;
165                         }
166                 }
167
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). */
173                         return no_alias;
174                 }
175         } else if (is_Sel(adr1)) {
176                 /* the first address is a Sel */
177                 ir_node *base1 = find_base_adr(adr1);
178
179                 if (base1 == get_irg_frame(irg)) {
180                         /* the first is a local variable */
181                         if (is_Sel(adr2)) {
182                                 /* the second address is a Sel */
183                                 ir_node *base2 = find_base_adr(adr2);
184
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
189                                        different (R1 d) */
190                                         return no_alias;
191                                 }
192                         }
193                 } else if (base1 == get_irg_tls(irg)) {
194                         /* the first is a TLS variable */
195                         if (is_Sel(adr2)) {
196                                 /* the second address is a Sel */
197                                 ir_node *base2 = find_base_adr(adr2);
198
199                                 if (base2 == get_irg_frame(irg)) {
200                                         /* the second one is a local variable so they are always
201                                        different (R1 d) */
202                                         return no_alias;
203                                 } else if (base2 == get_irg_tls(irg)) {
204                                         /* the second one is a TLS variable */
205                                 }
206                         }
207                 }
208         }
209
210         if (options & opt_strong_typed) {
211                 /* try rule R5 */
212                 ir_alias_relation rel = different_types(adr1, adr2);
213                 if (rel != may_alias)
214                         return rel;
215         }
216
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)
221                         return rel;
222         }
223
224         /* access points-to information here */
225         return may_alias;
226 }  /* _get_alias_relation */
227
228 /*
229  * Determine the alias relation between two addresses.
230  */
231 ir_alias_relation get_alias_relation(
232         ir_graph *irg,
233         ir_node *adr1, ir_mode *mode1,
234         ir_node *adr2, ir_mode *mode2,
235         unsigned options)
236 {
237         ir_alias_relation rel = _get_alias_relation(irg, adr1, mode1, adr2, mode2, options);
238         return rel;
239 }  /* get_alias_relation */
240
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 */
245
246 /** The result cache for the memory disambiguator. */
247 static set *result_cache = NULL;
248
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;
255
256 #define HASH_ENTRY(adr1, adr2)  (HASH_PTR(adr1) ^ HASH_PTR(adr2))
257
258 /**
259  * Compare two relation cache entries.
260  */
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;
264
265         return p1->adr1 == p2->adr1 && p1->adr2 == p2->adr2;
266 }  /* cmp_mem_disambig_entry */
267
268 /**
269  * Initialize the relation cache.
270  */
271 void mem_disambig_init(void) {
272         result_cache = new_set(cmp_mem_disambig_entry, 8);
273 }  /* mem_disambig_init */
274
275 /*
276  * Determine the alias relation between two addresses.
277  */
278 ir_alias_relation get_alias_relation_ex(
279         ir_graph *irg,
280         ir_node *adr1, ir_mode *mode1,
281         ir_node *adr2, ir_mode *mode2,
282         unsigned options)
283 {
284         mem_disambig_entry key, *entry;
285
286         if (! get_opt_alias_analysis())
287                 return may_alias;
288
289         if (get_irn_opcode(adr1) > get_irn_opcode(adr2)) {
290                 ir_node *t = adr1;
291                 adr1 = adr2;
292                 adr2 = t;
293         }
294
295         key.adr1 = adr1;
296         key.adr2 = adr2;
297         entry = set_find(result_cache, &key, sizeof(key), HASH_ENTRY(adr1, adr2));
298         if (entry)
299                 return entry->result;
300
301         key.result = get_alias_relation(irg, adr1, mode1, adr2, mode2, options);
302
303         set_insert(result_cache, &key, sizeof(key), HASH_ENTRY(adr1, adr2));
304         return key.result;
305 }  /* get_alias_relation_ex */
306
307 /* Free the relation cache. */
308 void mem_disambig_term(void) {
309         if (result_cache) {
310                 del_set(result_cache);
311                 result_cache = NULL;
312         }
313 }  /* mem_disambig_term */
314
315 /**
316  * Check the mode of a Load/Store with the mode of the entity
317  * that is accessed.
318  * If the mode of the entity and the Load/Store mode do not match, we
319  * have the bad reinterpret case:
320  *
321  * int i;
322  * char b = *(char *)&i;
323  *
324  * We do NOT count this as one value and return address_taken
325  * in that case.
326  * However, we support an often used case. If the mode is two-complement
327  * we allow casts between signed/unsigned.
328  *
329  * @param mode     the mode of the Load/Store
330  * @param ent_mode the mode of the accessed entity
331  *
332  * @return non-zero if the Load/Store is a hidden cast, zero else
333  */
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)
341                         return 1;
342         }
343         return 0;
344 }  /* is_hidden_cast */
345
346 /**
347  * Determine the address_taken state of a node (or it's successor Sels).
348  *
349  * @param irn  the node
350  */
351 static ir_address_taken_state find_address_taken_state(ir_node *irn) {
352         int     i;
353         ir_mode *emode, *mode;
354         ir_node *value;
355         ir_entity *ent;
356
357         for (i = get_irn_n_outs(irn) - 1; i >= 0; --i) {
358                 ir_node *succ = get_irn_out(irn, i);
359
360                 switch (get_irn_opcode(succ)) {
361                 case iro_Load:
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;
368                         break;
369
370                 case iro_Store:
371                         /* check that the node is not the Store's value */
372                         value = get_Store_value(succ);
373                         if (value == irn)
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;
381                         break;
382
383                 case iro_Sel: {
384                         /* Check the successor of irn. */
385                         ir_address_taken_state res = find_address_taken_state(succ);
386                         if (res != ir_address_not_taken)
387                                 return res;
388                         break;
389                 }
390
391                 case iro_Call:
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;
395
396                 default:
397                         /* another op, the address may be taken */
398                         return ir_address_taken_unknown;
399                 }
400         }
401         /* All successors finished, the address is not taken. */
402         return ir_address_not_taken;
403 }  /* find_address_taken_state */
404
405 /**
406  * Update the "address taken" flag of all frame entities.
407  */
408 static void analyse_irg_address_taken(ir_graph *irg) {
409         ir_type *ft = get_irg_frame_type(irg);
410         ir_node *irg_frame;
411         int i;
412
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);
416
417                 set_entity_address_taken(ent, ir_address_not_taken);
418         }
419
420         assure_irg_outs(irg);
421
422         irg_frame = get_irg_frame(irg);
423
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;
427
428             if (is_Sel(succ)) {
429                         ir_entity *ent = get_Sel_entity(succ);
430
431                         if (get_entity_address_taken(ent) == ir_address_taken)
432                                 continue;
433
434                         state = find_address_taken_state(succ);
435                         if (state > get_entity_address_taken(ent))
436                                 set_entity_address_taken(ent, state);
437                 }
438         }
439         /* now computed */
440         irg->adr_taken_state = ir_address_taken_computed;
441 }  /* analyse_address_taken */
442
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 */
447
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 */
452
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 */
458
459 /**
460  * Initialize the address_taken flag for a global type like type.
461  */
462 static void init_taken_flag(ir_type * tp) {
463         int i;
464
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;
470
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);
474         }
475 }  /* init_taken_flag */
476
477 /**
478  * Print the address taken state of all entities of a given type for debugging.
479  */
480 static void print_address_taken_state(ir_type *tp) {
481         int i;
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);
485
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));
489                 }
490         }
491 }  /* print_address_taken_state */
492
493 /**
494  * Post-walker: check for global entity address
495  */
496 static void check_global_address(ir_node *irn, void *env) {
497         ir_node *tls = env;
498         entity *ent;
499         ir_address_taken_state state;
500
501         if (is_SymConst(irn) && get_SymConst_kind(irn) == symconst_addr_ent) {
502                 /* A global. */
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);
507         } else
508                 return;
509
510         if (get_entity_address_taken(ent) == ir_address_not_taken) {
511                 /* Already at the maximum. */
512                 return;
513         }
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 */
518
519 /**
520  * Update the "address taken" flag of all global entities.
521  */
522 static void analyse_irp_globals_address_taken(void) {
523         int i;
524
525         init_taken_flag(get_glob_type());
526         init_taken_flag(get_tls_type());
527
528         for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
529                 ir_graph *irg = get_irp_irg(i);
530
531                 assure_irg_outs(irg);
532                 irg_walk_graph(irg, NULL, check_global_address, get_irg_tls(irg));
533         }
534         print_address_taken_state(get_glob_type());
535         print_address_taken_state(get_tls_type());
536
537         /* now computed */
538         irp->globals_adr_taken_state = ir_address_taken_computed;
539 }  /* analyse_irp_globals_address_taken */
540
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 */
545
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 */
550
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 */