BugFix: TLS variables are Sels, not SymConst
[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 #include "irprintf.h"
28
29 /** The source language specific language disambiguator function. */
30 static DISAMBIGUATOR_FUNC language_disambuigator = NULL;
31
32 /** The global memory disambiguator options. */
33 static unsigned global_mem_disamgig_opt = aa_opt_no_opt;
34
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;
40         return opt;
41 }  /* get_irg_memory_disambiguator_options */
42
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 */
47
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 */
52
53 /**
54  * Find the base address and entity of an Sel node.
55  *
56  * @param sel  the node
57  * @param pEnt after return points to the base entity.
58  *
59  * @return the base address.
60  */
61 static ir_node *find_base_adr(ir_node *sel, ir_entity **pEnt) {
62         ir_node *ptr = get_Sel_ptr(sel);
63
64         while (is_Sel(ptr)) {
65                 sel = ptr;
66                 ptr = get_Sel_ptr(sel);
67         }
68         *pEnt = get_Sel_entity(sel);
69         return ptr;
70 }  /* find_base_adr */
71
72 /**
73  * Two address expressions have the same base address,
74  * check if there offsets are different.
75  *
76  * @param adr1  The first address.
77  * @param adr2  The second address.
78  */
79 static ir_alias_relation different_offsets(ir_node *adr1, ir_node *adr2) {
80         return may_alias;
81 }  /* different_offsets */
82
83 /**
84  * Determine the alias relation by checking if adr1 and adr2 are pointer
85  * to different type.
86  *
87  * @param adr1    The first address.
88  * @param adr2    The second address.
89  */
90 static ir_alias_relation different_types(ir_node *adr1, ir_node *adr2)
91 {
92         ir_entity *ent1 = NULL, *ent2 = NULL;
93
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);
98
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);
103
104         if (ent1 != NULL && ent2 != NULL) {
105                 ir_type *tp1 = get_entity_type(ent1);
106                 ir_type *tp2 = get_entity_type(ent2);
107
108                 if (tp1 != tp2) {
109                         if (is_Pointer_type(tp1) && is_Pointer_type(tp2)) {
110                                 /* do deref until no pointer types are found */
111                                 do {
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));
115                         }
116
117                         if (get_type_tpop(tp1) != get_type_tpop(tp2)) {
118                                 /* different type structure */
119                                 return no_alias;
120                         }
121                         if (is_Class_type(tp1)) {
122                                 /* check class hierarchy */
123                                 if (! is_SubClass_of(tp1, tp2) &&
124                                         ! is_SubClass_of(tp2, tp1))
125                                         return no_alias;
126                         } else {
127                                 /* different types */
128                                 return no_alias;
129                         }
130                 }
131         }
132         return may_alias;
133 }  /* different_types */
134
135 /**
136  * Returns non-zero if a node is a routine parameter.
137  *
138  * @param node  the node to test
139  */
140 static int is_arg_Proj(ir_node *node) {
141         if (! is_Proj(node))
142                 return 0;
143         node = get_Proj_pred(node);
144         if (! is_Proj(node))
145                 return 0;
146         return pn_Start_T_args == get_Proj_proj(node) && is_Start(get_Proj_pred(node));
147 }  /* is_arg_Proj */
148
149 /**
150  * Determine the alias relation between two addresses.
151  */
152 static ir_alias_relation _get_alias_relation(
153         ir_graph *irg,
154         ir_node *adr1, ir_mode *mode1,
155         ir_node *adr2, ir_mode *mode2)
156 {
157         ir_opcode op1, op2;
158         ir_entity *ent1, *ent2;
159         unsigned options;
160
161         if (! get_opt_alias_analysis())
162                 return may_alias;
163
164         if (adr1 == adr2)
165                 return sure_alias;
166
167         options = get_irg_memory_disambiguator_options(irg);
168
169         /* The Armageddon switch */
170         if (options & aa_opt_no_alias)
171                 return no_alias;
172
173         /* Two save some code, sort the addresses by its id's. Beware, this
174            might break some things, so better check here. */
175         assert(iro_SymConst < iro_Sel && iro_Sel < iro_Proj && "Code dependence breaked");
176         op1 = get_irn_opcode(adr1);
177         op2 = get_irn_opcode(adr2);
178
179         if (op1 > op2) {
180                 ir_node *t = adr1;
181                 ir_mode *m = mode1;
182                 adr1  = adr2;
183                 mode1 = mode2;
184                 adr2  = t;
185                 mode2 = m;
186         }
187
188         if (is_SymConst(adr1) && get_SymConst_kind(adr1) == symconst_addr_ent) {
189                 /* first address is a global variable */
190
191                 if (is_SymConst(adr2) && get_SymConst_kind(adr2) == symconst_addr_ent) {
192                         /* both addresses are global variables and we know
193                            they are different (R1 a) */
194                         if (get_SymConst_entity(adr1) != get_SymConst_entity(adr2))
195                                 return no_alias;
196                         else {
197                                 /* equal addresses */
198                                 return sure_alias;
199                         }
200                 }
201                 if (is_Sel(adr2)) {
202                         ir_node *base = find_base_adr(adr2, &ent2);
203
204                         if (is_SymConst(base) && get_SymConst_kind(base) == symconst_addr_ent) {
205                                 /* base address is a global var (R1 a) */
206                                 if (adr1 != base)
207                                         return no_alias;
208                                 if (base == adr1)
209                                         return different_offsets(adr1, adr2);
210                         } else if (base == get_irg_frame(irg)) {
211                                 /* the second one is a local variable so they are always
212                                    different (R1 b) */
213                                 return no_alias;
214                         } else if (base == get_irg_tls(irg)) {
215                                 /* the second one is a TLS variable so they are always
216                                    different (R1 c) */
217                                 return no_alias;
218                         }
219                 }
220
221                 /* Here we are: the first is a global var, the second some pointer. */
222                 ent1 = get_SymConst_entity(adr1);
223                 if (get_entity_address_taken(ent1) == ir_address_not_taken) {
224                         /* The address of the global variable was never taken, so
225                            the pointer cannot match (R2). */
226                         return no_alias;
227                 }
228         } else if (is_Sel(adr1)) {
229                 /* the first address is a Sel */
230                 ir_node *base1 = find_base_adr(adr1, &ent1);
231
232                 if (base1 == get_irg_frame(irg)) {
233                         /* the first is a local variable */
234                         if (is_Sel(adr2)) {
235                                 /* the second address is a Sel */
236                                 ir_node *base2 = find_base_adr(adr2, &ent2);
237
238                                 if (base2 == get_irg_frame(irg)) {
239                                         /* both addresses are local variables and we know
240                                            they are different (R1 a) */
241                                         if (ent1 != ent2)
242                                                 return no_alias;
243                                         else
244                                                 return different_offsets(adr1, adr2);
245                                 } else if (base2 == get_irg_tls(irg)) {
246                                         /* the second one is a TLS variable so they are always
247                                        different (R1 d) */
248                                         return no_alias;
249                                 } else if (is_arg_Proj(base2)) {
250                                         /* the second one is an offset from a parameter so they are
251                                            always different (R1 e) */
252                                         return no_alias;
253                                 }
254                         } else if (is_arg_Proj(adr2)) {
255                                 /* a local variable and a parameter are always different (R1 e) */
256                                 return no_alias;
257                         }
258                 } else if (base1 == get_irg_tls(irg)) {
259                         /* the first is a TLS variable */
260                         if (is_Sel(adr2)) {
261                                 /* the second address is a Sel */
262                                 ir_node *base2 = find_base_adr(adr2, &ent2);
263
264                                 if (base2 == get_irg_frame(irg)) {
265                                         /* the second one is a local variable so they are always
266                                        different (R1 d) */
267                                         return no_alias;
268                                 } else if (base2 == get_irg_tls(irg)) {
269                                         /* both addresses are TLS variables and we know
270                                            they are different (R1 a) */
271                                         if (ent1 != ent2)
272                                                 return no_alias;
273                                         else
274                                                 return different_offsets(adr1, adr2);
275                                 }
276                         }
277                 } else if (is_arg_Proj(base1)) {
278                         /* the first one is an offset from a parameter */
279                         if (is_Sel(adr2)) {
280                                 /* the second address is a Sel */
281                                 ir_node *base2 = find_base_adr(adr2, &ent2);
282
283                                 if (base2 == get_irg_frame(irg)) {
284                                         /* the second one is a local variable so they are always
285                                        different (R1 e) */
286                                         return no_alias;
287                                 }
288                         }
289                 }
290         }
291
292         if (options & aa_opt_type_based) { /* Type based alias analysis */
293                 ir_alias_relation rel;
294
295                 if (options & aa_opt_byte_type_may_alias) {
296                         if (get_mode_size_bits(mode1) == 8 || get_mode_size_bits(mode2) == 8) {
297                                 /* One of the modes address a byte. Assume a may_alias and leave
298                                    the type based check. */
299                                 goto leave_type_based_alias;
300                         }
301                 }
302                 /* cheap check: If the mode sizes did not match, the types MUST be different */
303                 if (get_mode_size_bits(mode1) != get_mode_size_bits(mode2))
304                         return no_alias;
305
306                 /* try rule R5 */
307                 rel = different_types(adr1, adr2);
308                 if (rel != may_alias)
309                         return rel;
310 leave_type_based_alias:;
311         }
312
313         /* do we have a language specific memory disambiguator? */
314         if (language_disambuigator) {
315                 ir_alias_relation rel = (*language_disambuigator)(irg, adr1, mode1, adr2, mode2);
316                 if (rel != may_alias)
317                         return rel;
318         }
319
320         /* access points-to information here */
321         return may_alias;
322 }  /* _get_alias_relation */
323
324 /*
325  * Determine the alias relation between two addresses.
326  */
327 ir_alias_relation get_alias_relation(
328         ir_graph *irg,
329         ir_node *adr1, ir_mode *mode1,
330         ir_node *adr2, ir_mode *mode2)
331 {
332         ir_alias_relation rel = _get_alias_relation(irg, adr1, mode1, adr2, mode2);
333         return rel;
334 }  /* get_alias_relation */
335
336 /* Set a source language specific memory disambiguator function. */
337 void set_language_memory_disambiguator(DISAMBIGUATOR_FUNC func) {
338         language_disambuigator = func;
339 }  /* set_language_memory_disambiguator */
340
341 /** The result cache for the memory disambiguator. */
342 static set *result_cache = NULL;
343
344 /** An entry in the relation cache. */
345 typedef struct mem_disambig_entry {
346         ir_node           *adr1;    /**< The first address. */
347         ir_node           *adr2;    /**< The second address. */
348         ir_alias_relation result;   /**< The alias relation result. */
349 } mem_disambig_entry;
350
351 #define HASH_ENTRY(adr1, adr2)  (HASH_PTR(adr1) ^ HASH_PTR(adr2))
352
353 /**
354  * Compare two relation cache entries.
355  */
356 static int cmp_mem_disambig_entry(const void *elt, const void *key, size_t size) {
357         const mem_disambig_entry *p1 = elt;
358         const mem_disambig_entry *p2 = key;
359
360         return p1->adr1 == p2->adr1 && p1->adr2 == p2->adr2;
361 }  /* cmp_mem_disambig_entry */
362
363 /**
364  * Initialize the relation cache.
365  */
366 void mem_disambig_init(void) {
367         result_cache = new_set(cmp_mem_disambig_entry, 8);
368 }  /* mem_disambig_init */
369
370 /*
371  * Determine the alias relation between two addresses.
372  */
373 ir_alias_relation get_alias_relation_ex(
374         ir_graph *irg,
375         ir_node *adr1, ir_mode *mode1,
376         ir_node *adr2, ir_mode *mode2)
377 {
378         mem_disambig_entry key, *entry;
379
380         if (! get_opt_alias_analysis())
381                 return may_alias;
382
383         if (get_irn_opcode(adr1) > get_irn_opcode(adr2)) {
384                 ir_node *t = adr1;
385                 adr1 = adr2;
386                 adr2 = t;
387         }
388
389         key.adr1 = adr1;
390         key.adr2 = adr2;
391         entry = set_find(result_cache, &key, sizeof(key), HASH_ENTRY(adr1, adr2));
392         if (entry)
393                 return entry->result;
394
395         key.result = get_alias_relation(irg, adr1, mode1, adr2, mode2);
396
397         set_insert(result_cache, &key, sizeof(key), HASH_ENTRY(adr1, adr2));
398         return key.result;
399 }  /* get_alias_relation_ex */
400
401 /* Free the relation cache. */
402 void mem_disambig_term(void) {
403         if (result_cache) {
404                 del_set(result_cache);
405                 result_cache = NULL;
406         }
407 }  /* mem_disambig_term */
408
409 /**
410  * Check the mode of a Load/Store with the mode of the entity
411  * that is accessed.
412  * If the mode of the entity and the Load/Store mode do not match, we
413  * have the bad reinterpret case:
414  *
415  * int i;
416  * char b = *(char *)&i;
417  *
418  * We do NOT count this as one value and return address_taken
419  * in that case.
420  * However, we support an often used case. If the mode is two-complement
421  * we allow casts between signed/unsigned.
422  *
423  * @param mode     the mode of the Load/Store
424  * @param ent_mode the mode of the accessed entity
425  *
426  * @return non-zero if the Load/Store is a hidden cast, zero else
427  */
428 static int is_hidden_cast(ir_mode *mode, ir_mode *ent_mode) {
429         if (ent_mode != mode) {
430                 if (ent_mode == NULL ||
431                         get_mode_size_bits(ent_mode) != get_mode_size_bits(mode) ||
432                         get_mode_sort(ent_mode) != get_mode_sort(mode) ||
433                         get_mode_arithmetic(ent_mode) != irma_twos_complement ||
434                         get_mode_arithmetic(mode) != irma_twos_complement)
435                         return 1;
436         }
437         return 0;
438 }  /* is_hidden_cast */
439
440 /**
441  * Determine the address_taken state of a node (or it's successor Sels).
442  *
443  * @param irn  the node
444  */
445 static ir_address_taken_state find_address_taken_state(ir_node *irn) {
446         int     i;
447         ir_mode *emode, *mode;
448         ir_node *value;
449         ir_entity *ent;
450
451         for (i = get_irn_n_outs(irn) - 1; i >= 0; --i) {
452                 ir_node *succ = get_irn_out(irn, i);
453
454                 switch (get_irn_opcode(succ)) {
455                 case iro_Load:
456                         /* check if this load is not a hidden conversion */
457                         mode = get_Load_mode(succ);
458                         ent = is_SymConst(irn) ? get_SymConst_entity(irn) : get_Sel_entity(irn);
459                         emode = get_type_mode(get_entity_type(ent));
460                         if (is_hidden_cast(mode, emode))
461                                 return ir_address_taken;
462                         break;
463
464                 case iro_Store:
465                         /* check that the node is not the Store's value */
466                         value = get_Store_value(succ);
467                         if (value == irn)
468                                 return ir_address_taken;
469                         /* check if this Store is not a hidden conversion */
470                         mode = get_irn_mode(value);
471                         ent = is_SymConst(irn) ? get_SymConst_entity(irn) : get_Sel_entity(irn);
472                         emode = get_type_mode(get_entity_type(ent));
473                         if (is_hidden_cast(mode, emode))
474                                 return ir_address_taken;
475                         break;
476
477                 case iro_Sel: {
478                         /* Check the successor of irn. */
479                         ir_address_taken_state res = find_address_taken_state(succ);
480                         if (res != ir_address_not_taken)
481                                 return res;
482                         break;
483                 }
484
485                 case iro_Call:
486                         /* Only the call address is not an address taker but
487                            this is an uninteresting case, so we ignore it here. */
488                         return ir_address_taken;
489
490                 default:
491                         /* another op, the address may be taken */
492                         return ir_address_taken_unknown;
493                 }
494         }
495         /* All successors finished, the address is not taken. */
496         return ir_address_not_taken;
497 }  /* find_address_taken_state */
498
499 /**
500  * Update the "address taken" flag of all frame entities.
501  */
502 static void analyse_irg_address_taken(ir_graph *irg) {
503         ir_type *ft = get_irg_frame_type(irg);
504         ir_node *irg_frame;
505         int i;
506
507         /* set initial state to not_taken, as this is the "smallest" state */
508         for (i = get_class_n_members(ft) - 1; i >= 0; --i) {
509                 ir_entity *ent = get_class_member(ft, i);
510
511                 set_entity_address_taken(ent, ir_address_not_taken);
512         }
513
514         assure_irg_outs(irg);
515
516         irg_frame = get_irg_frame(irg);
517
518         for (i = get_irn_n_outs(irg_frame) - 1; i >= 0; --i) {
519                 ir_node *succ = get_irn_out(irg_frame, i);
520                 ir_address_taken_state state;
521
522             if (is_Sel(succ)) {
523                         ir_entity *ent = get_Sel_entity(succ);
524
525                         if (get_entity_address_taken(ent) == ir_address_taken)
526                                 continue;
527
528                         state = find_address_taken_state(succ);
529                         if (state > get_entity_address_taken(ent))
530                                 set_entity_address_taken(ent, state);
531                 }
532         }
533         /* now computed */
534         irg->adr_taken_state = ir_address_taken_computed;
535 }  /* analyse_address_taken */
536
537 /* Returns the current address taken state of the graph. */
538 ir_address_taken_computed_state get_irg_address_taken_state(const ir_graph *irg) {
539         return irg->adr_taken_state;
540 }  /* get_irg_address_taken_state */
541
542 /* Sets the current address taken state of the graph. */
543 void set_irg_address_taken_state(ir_graph *irg, ir_address_taken_computed_state state) {
544         irg->adr_taken_state = state;
545 }  /* set_irg_address_taken_state */
546
547 /* Assure that the address taken flag is computed for the given graph. */
548 void assure_irg_address_taken_computed(ir_graph *irg) {
549         if (irg->adr_taken_state == ir_address_taken_not_computed)
550                 analyse_irg_address_taken(irg);
551 }  /* assure_irg_address_taken_computed */
552
553 /**
554  * Initialize the address_taken flag for a global type like type.
555  */
556 static void init_taken_flag(ir_type * tp) {
557         int i;
558
559         /* All external visible entities are at least
560            ir_address_taken_unknown. This is very conservative. */
561         for (i = get_compound_n_members(tp) - 1; i >= 0; --i) {
562                 ir_entity *ent = get_compound_member(tp, i);
563                 ir_address_taken_state state;
564
565                 state = get_entity_visibility(ent) == visibility_external_visible ?
566                                 ir_address_taken_unknown : ir_address_not_taken ;
567                 set_entity_address_taken(ent, state);
568         }
569 }  /* init_taken_flag */
570
571 /**
572  * Print the address taken state of all entities of a given type for debugging.
573  */
574 static void print_address_taken_state(ir_type *tp) {
575         int i;
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 = get_entity_address_taken(ent);
579
580                 if (state != ir_address_not_taken) {
581                         assert(ir_address_not_taken <= state && state <= ir_address_taken);
582                         ir_printf("%+F: %s\n", ent, get_address_taken_state_name(state));
583                 }
584         }
585 }  /* print_address_taken_state */
586
587 /**
588  * Post-walker: check for global entity address
589  */
590 static void check_global_address(ir_node *irn, void *env) {
591         ir_node *tls = env;
592         ir_entity *ent;
593         ir_address_taken_state state;
594
595         if (is_SymConst(irn) && get_SymConst_kind(irn) == symconst_addr_ent) {
596                 /* A global. */
597                 ent = get_SymConst_entity(irn);
598         } else if (is_Sel(irn) && get_Sel_ptr(irn) == tls) {
599                 /* A TLS variable. */
600                 ent = get_Sel_entity(irn);
601         } else
602                 return;
603
604         if (get_entity_address_taken(ent) == ir_address_not_taken) {
605                 /* Already at the maximum. */
606                 return;
607         }
608         state = find_address_taken_state(irn);
609         if (state > get_entity_address_taken(ent))
610                 set_entity_address_taken(ent, state);
611 }  /* check_global_address */
612
613 /**
614  * Update the "address taken" flag of all global entities.
615  */
616 static void analyse_irp_globals_address_taken(void) {
617         int i;
618
619         init_taken_flag(get_glob_type());
620         init_taken_flag(get_tls_type());
621
622         for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
623                 ir_graph *irg = get_irp_irg(i);
624
625                 assure_irg_outs(irg);
626                 irg_walk_graph(irg, NULL, check_global_address, get_irg_tls(irg));
627         }
628         //print_address_taken_state(get_glob_type());
629         //print_address_taken_state(get_tls_type());
630
631         /* now computed */
632         irp->globals_adr_taken_state = ir_address_taken_computed;
633 }  /* analyse_irp_globals_address_taken */
634
635 /* Returns the current address taken state of the globals. */
636 ir_address_taken_computed_state get_irp_globals_address_taken_state(void) {
637         return irp->globals_adr_taken_state;
638 }  /* get_irp_globals_address_taken_state */
639
640 /* Sets the current address taken state of the graph. */
641 void set_irp_globals_address_taken_state(ir_address_taken_computed_state state) {
642         irp->globals_adr_taken_state = state;
643 }  /* set_irg_address_taken_state */
644
645 /* Assure that the address taken flag is computed for the globals. */
646 void assure_irp_globals_address_taken_computed(void) {
647         if (irp->globals_adr_taken_state == ir_address_taken_not_computed)
648                 analyse_irp_globals_address_taken();
649 }  /* assure_irp_globals_address_taken_computed */