cleanup: Remove duplicate MIN/MAX macros.
[libfirm] / ir / ana / irmemory.c
1 /*
2  * This file is part of libFirm.
3  * Copyright (C) 2012 University of Karlsruhe.
4  */
5
6 /**
7  * @file
8  * @brief    Memory disambiguator
9  * @author   Michael Beck
10  * @date     27.12.2006
11  */
12 #include "config.h"
13
14 #include <stdlib.h>
15 #include <stdbool.h>
16
17 #include "adt/pmap.h"
18 #include "irnode_t.h"
19 #include "irgraph_t.h"
20 #include "irprog_t.h"
21 #include "irmemory_t.h"
22 #include "irmemory.h"
23 #include "irflag.h"
24 #include "hashptr.h"
25 #include "irflag.h"
26 #include "irouts.h"
27 #include "irgwalk.h"
28 #include "irprintf.h"
29 #include "debug.h"
30 #include "error.h"
31 #include "typerep.h"
32 #include "irpass.h"
33
34 /** The debug handle. */
35 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
36 DEBUG_ONLY(static firm_dbg_module_t *dbgcall = NULL;)
37
38 /** The source language specific language disambiguator function. */
39 static DISAMBIGUATOR_FUNC language_disambuigator = NULL;
40
41 /** The global memory disambiguator options. */
42 static unsigned global_mem_disamgig_opt = aa_opt_no_opt;
43
44 const char *get_ir_alias_relation_name(ir_alias_relation rel)
45 {
46 #define X(a) case a: return #a
47         switch (rel) {
48         X(ir_no_alias);
49         X(ir_may_alias);
50         X(ir_sure_alias);
51         default:
52                 panic("UNKNOWN alias relation");
53         }
54 #undef X
55 }
56
57 unsigned get_irg_memory_disambiguator_options(const ir_graph *irg)
58 {
59         unsigned opt = irg->mem_disambig_opt;
60         if (opt & aa_opt_inherited)
61                 return global_mem_disamgig_opt;
62         return opt;
63 }
64
65 void set_irg_memory_disambiguator_options(ir_graph *irg, unsigned options)
66 {
67         irg->mem_disambig_opt = options & ~aa_opt_inherited;
68 }
69
70 void set_irp_memory_disambiguator_options(unsigned options)
71 {
72         global_mem_disamgig_opt = options;
73 }
74
75 ir_storage_class_class_t get_base_sc(ir_storage_class_class_t x)
76 {
77         return x & ~ir_sc_modifiers;
78 }
79
80 /**
81  * Find the base address and entity of an Sel node.
82  *
83  * @param sel  the node
84  * @param pEnt after return points to the base entity.
85  *
86  * @return the base address.
87  */
88 static ir_node *find_base_adr(const ir_node *sel, ir_entity **pEnt)
89 {
90         ir_node *ptr = get_Sel_ptr(sel);
91
92         while (is_Sel(ptr)) {
93                 sel = ptr;
94                 ptr = get_Sel_ptr(sel);
95         }
96         *pEnt = get_Sel_entity(sel);
97         return ptr;
98 }
99
100 /**
101  * Check if a given Const node is greater or equal a given size.
102  *
103  * @param cns   a Const node
104  * @param size  a integer size
105  *
106  * @return ir_no_alias if the Const is greater, ir_may_alias else
107  */
108 static ir_alias_relation check_const(const ir_node *cns, int size)
109 {
110         ir_tarval *tv = get_Const_tarval(cns);
111         ir_tarval *tv_size;
112
113         if (size == 0)
114                 return tarval_is_null(tv) ? ir_may_alias : ir_no_alias;
115         tv_size = new_tarval_from_long(size, get_tarval_mode(tv));
116         return tarval_cmp(tv_size, tv) & (ir_relation_less_equal) ? ir_no_alias : ir_may_alias;
117 }
118
119 /**
120  * Treat idx1 and idx2 as integer indexes and check if they differ always more than size.
121  *
122  * @param idx1  a node representing the first index
123  * @param idx2  a node representing the second index
124  * @param size  an integer size
125  *
126  * @return ir_sure_alias iff idx1 == idx2
127  *         ir_no_alias iff they ALWAYS differ more than size
128  *         ir_may_alias else
129  */
130 static ir_alias_relation different_index(const ir_node *idx1, const ir_node *idx2, int size)
131 {
132         if (idx1 == idx2)
133                 return ir_sure_alias;
134         if (is_Const(idx1) && is_Const(idx2)) {
135                 /* both are const, we can compare them */
136                 ir_tarval *tv1 = get_Const_tarval(idx1);
137                 ir_tarval *tv2 = get_Const_tarval(idx2);
138                 ir_tarval *tv, *tv_size;
139                 ir_mode *m1, *m2;
140
141                 if (size == 0)
142                         return tv1 == tv2 ? ir_sure_alias : ir_no_alias;
143
144                 /* arg, modes may be different */
145                 m1 = get_tarval_mode(tv1);
146                 m2 = get_tarval_mode(tv2);
147                 if (m1 != m2) {
148                         int size = get_mode_size_bits(m1) - get_mode_size_bits(m2);
149
150                         if (size < 0) {
151                                 /* m1 is a small mode, cast up */
152                                 m1 = mode_is_signed(m1) ? find_signed_mode(m2) : find_unsigned_mode(m2);
153                                 if (m1 == NULL) {
154                                         /* should NOT happen, but if it does we give up here */
155                                         return ir_may_alias;
156                                 }
157                                 tv1 = tarval_convert_to(tv1, m1);
158                         } else if (size > 0) {
159                                 /* m2 is a small mode, cast up */
160                                 m2 = mode_is_signed(m2) ? find_signed_mode(m1) : find_unsigned_mode(m1);
161                                 if (m2 == NULL) {
162                                         /* should NOT happen, but if it does we give up here */
163                                         return ir_may_alias;
164                                 }
165                                 tv2 = tarval_convert_to(tv2, m2);
166                         }
167                         /* here the size should be identical, check for signed */
168                         if (get_mode_sign(m1) != get_mode_sign(m2)) {
169                                 /* find the signed */
170                                 if (mode_is_signed(m2)) {
171                                         ir_tarval *t = tv1;
172                                         ir_mode *tm = m1;
173                                         tv1 = tv2; m1 = m2;
174                                         tv2 = t;   m2 = tm;
175                                 }
176
177                                 /* m1 is now the signed one */
178                                 if (!tarval_is_negative(tv1)) {
179                                         /* tv1 is signed, but >= 0, simply cast into unsigned */
180                                         tv1 = tarval_convert_to(tv1, m2);
181                                 } else {
182                                         tv_size = new_tarval_from_long(size, m2);
183
184                                         if (tarval_cmp(tv2, tv_size) & (ir_relation_greater_equal)) {
185                                                 /* tv1 is negative and tv2 >= tv_size, so the difference is bigger than size */
186                                                 return ir_no_alias;
187                                         }
188                                         /* tv_size > tv2, so we can subtract without overflow */
189                                         tv2 = tarval_sub(tv_size, tv2, NULL);
190
191                                         /* tv1 is < 0, so we can negate it */
192                                         tv1 = tarval_neg(tv1);
193
194                                         /* cast it into unsigned. for two-complement it does the right thing for MIN_INT */
195                                         tv1 = tarval_convert_to(tv1, m2);
196
197                                         /* now we can compare without overflow */
198                                         return tarval_cmp(tv1, tv2) & (ir_relation_greater_equal) ? ir_no_alias : ir_may_alias;
199                                 }
200                         }
201                 }
202                 if (tarval_cmp(tv1, tv2) == ir_relation_greater) {
203                         ir_tarval *t = tv1;
204                         tv1 = tv2;
205                         tv2 = t;
206                 }
207                 /* tv1 is now the "smaller" one */
208                 tv      = tarval_sub(tv2, tv1, NULL);
209                 tv_size = new_tarval_from_long(size, get_tarval_mode(tv));
210                 return tarval_cmp(tv_size, tv) & (ir_relation_less_equal) ? ir_no_alias : ir_may_alias;
211         }
212
213         /* Note: we rely here on the fact that normalization puts constants on the RIGHT side */
214         if (is_Add(idx1)) {
215                 ir_node *l1 = get_Add_left(idx1);
216                 ir_node *r1 = get_Add_right(idx1);
217
218                 if (l1 == idx2) {
219                         /* x + c == y */
220                         if (is_Const(r1))
221                                 return check_const(r1, size);
222                 }
223                 if (is_Add(idx2)) {
224                         /* both are Adds, check if they are of x + a == x + b kind */
225                         ir_node *l2 = get_Add_left(idx2);
226                         ir_node *r2 = get_Add_right(idx2);
227
228                         if (l1 == l2)
229                                 return different_index(r1, r2, size);
230                         else if (l1 == r2)
231                                 return different_index(r1, l2, size);
232                         else if (r1 == r2)
233                                 return different_index(l1, l2, size);
234                         else if (r1 == l2)
235                                 return different_index(l1, r2, size);
236                 }
237         }
238         if (is_Add(idx2)) {
239                 ir_node *l2 = get_Add_left(idx2);
240                 ir_node *r2 = get_Add_right(idx2);
241
242                 if (l2 == idx1) {
243                         /* x + c == y */
244                         if (is_Const(r2))
245                                 return check_const(r2, size);
246                 }
247         }
248
249         if (is_Sub(idx1)) {
250                 ir_node *l1 = get_Sub_left(idx1);
251                 ir_node *r1 = get_Sub_right(idx1);
252
253                 if (l1 == idx2) {
254                         /* x - c == y */
255                         if (is_Const(r1))
256                                 return check_const(r1, size);
257                 }
258
259                 if (is_Sub(idx2)) {
260                         /* both are Subs, check if they are of x - a == x - b kind */
261                         ir_node *l2 = get_Sub_left(idx2);
262
263                         if (l1 == l2) {
264                                 ir_node *r2 = get_Sub_right(idx2);
265                                 return different_index(r1, r2, size);
266                         }
267                 }
268         }
269         if (is_Sub(idx2)) {
270                 ir_node *l2 = get_Sub_left(idx2);
271                 ir_node *r2 = get_Sub_right(idx2);
272
273                 if (l2 == idx1) {
274                         /* x - c == y */
275                         if (is_Const(r2))
276                                 return check_const(r2, size);
277                 }
278
279         }
280         return ir_may_alias;
281 }
282
283 /**
284  * Two Sel addresses have the same base address, check if their offsets are
285  * different.
286  *
287  * @param adr1  The first address.
288  * @param adr2  The second address.
289  */
290 static ir_alias_relation different_sel_offsets(const ir_node *sel1, const ir_node *sel2)
291 {
292         /* TODO: fix */
293         (void) sel1;
294         (void) sel2;
295 #if 0
296         ir_entity *ent1 = get_Sel_entity(sel1);
297         ir_entity *ent2 = get_Sel_entity(sel2);
298         int i, check_arr = 0;
299
300         if (ent1 == ent2)
301                 check_arr = 1;
302         else {
303                 ir_type *tp1 = get_entity_type(ent1);
304                 ir_type *tp2 = get_entity_type(ent2);
305
306                 if (tp1 == tp2)
307                         check_arr = 1;
308                 else if (get_type_state(tp1) == layout_fixed && get_type_state(tp2) == layout_fixed &&
309                          get_type_size_bits(tp1) == get_type_size_bits(tp2))
310                         check_arr = 1;
311         }
312         if (check_arr) {
313                 /* we select an entity of same size, check for indexes */
314                 int n = get_Sel_n_indexs(sel1);
315                 int have_no = 0;
316
317                 if (n > 0 && n == get_Sel_n_indexs(sel2)) {
318                         /* same non-zero number of indexes, an array access, check */
319                         for (i = 0; i < n; ++i) {
320                                 ir_node *idx1 = get_Sel_index(sel1, i);
321                                 ir_node *idx2 = get_Sel_index(sel2, i);
322                                 ir_alias_relation res = different_index(idx1, idx2, 0); /* we can safely IGNORE the size here if it's at least >0 */
323
324                                 if (res == may_alias)
325                                         return may_alias;
326                                 else if (res == no_alias)
327                                         have_no = 1;
328                         }
329                         /* if we have at least one no_alias, there is no alias relation, else we have sure */
330                         return have_no > 0 ? no_alias : sure_alias;
331                 }
332         }
333 #else
334         (void) different_index;
335 #endif
336         return ir_may_alias;
337 }
338
339 /**
340  * Determine the alias relation by checking if adr1 and adr2 are pointer
341  * to different type.
342  *
343  * @param adr1    The first address.
344  * @param adr2    The second address.
345  */
346 static ir_alias_relation different_types(const ir_node *adr1, const ir_node *adr2)
347 {
348         ir_entity *ent1 = NULL, *ent2 = NULL;
349
350         if (is_SymConst_addr_ent(adr1))
351                 ent1 = get_SymConst_entity(adr1);
352         else if (is_Sel(adr1))
353                 ent1 = get_Sel_entity(adr1);
354
355         if (is_SymConst_addr_ent(adr2))
356                 ent2 = get_SymConst_entity(adr2);
357         else if (is_Sel(adr2))
358                 ent2 = get_Sel_entity(adr2);
359
360         if (ent1 != NULL && ent2 != NULL) {
361                 ir_type *tp1 = get_entity_type(ent1);
362                 ir_type *tp2 = get_entity_type(ent2);
363
364                 if (tp1 != tp2) {
365                         /* do deref until no pointer types are found */
366                         while (is_Pointer_type(tp1) && is_Pointer_type(tp2)) {
367                                 tp1 = get_pointer_points_to_type(tp1);
368                                 tp2 = get_pointer_points_to_type(tp2);
369                         }
370
371                         if (get_type_tpop(tp1) != get_type_tpop(tp2)) {
372                                 /* different type structure */
373                                 return ir_no_alias;
374                         }
375                         if (is_Class_type(tp1)) {
376                                 /* check class hierarchy */
377                                 if (! is_SubClass_of(tp1, tp2) &&
378                                         ! is_SubClass_of(tp2, tp1))
379                                         return ir_no_alias;
380                         } else {
381                                 /* different types */
382                                 return ir_no_alias;
383                         }
384                 }
385         }
386         return ir_may_alias;
387 }
388
389 /**
390  * Returns non-zero if a node is a result on a malloc-like routine.
391  *
392  * @param node  the Proj node to test
393  */
394 static int is_malloc_Result(const ir_node *node)
395 {
396         node = get_Proj_pred(node);
397         if (! is_Proj(node))
398                 return 0;
399         node = get_Proj_pred(node);
400         if (! is_Call(node))
401                 return 0;
402         node = get_Call_ptr(node);
403         if (is_SymConst_addr_ent(node)) {
404                 ir_entity *ent = get_SymConst_entity(node);
405
406                 if (get_entity_additional_properties(ent) & mtp_property_malloc)
407                         return 1;
408                 return 0;
409         }
410         return 0;
411 }
412
413 ir_storage_class_class_t classify_pointer(const ir_node *irn,
414                                           const ir_entity *ent)
415 {
416         ir_graph *irg = get_irn_irg(irn);
417         ir_storage_class_class_t res = ir_sc_pointer;
418         if (is_SymConst_addr_ent(irn)) {
419                 ir_entity *entity = get_SymConst_entity(irn);
420                 ir_type   *owner  = get_entity_owner(entity);
421                 res = owner == get_tls_type() ? ir_sc_tls : ir_sc_globalvar;
422                 if (! (get_entity_usage(entity) & ir_usage_address_taken))
423                         res |= ir_sc_modifier_nottaken;
424         } else if (irn == get_irg_frame(irg)) {
425                 res = ir_sc_localvar;
426                 if (ent != NULL && !(get_entity_usage(ent) & ir_usage_address_taken))
427                         res |= ir_sc_modifier_nottaken;
428         } else if (is_Proj(irn) && is_malloc_Result(irn)) {
429                 return ir_sc_malloced;
430         } else if (is_Const(irn)) {
431                 return ir_sc_globaladdr;
432         } else if (is_arg_Proj(irn)) {
433                 res |= ir_sc_modifier_argument;
434         }
435
436         return res;
437 }
438
439 /**
440  * If adr represents a Bitfield Sel, skip it
441  */
442 static const ir_node *skip_Bitfield_Sels(const ir_node *adr)
443 {
444         if (is_Sel(adr)) {
445                 ir_entity *ent     = get_Sel_entity(adr);
446                 ir_type   *bf_type = get_entity_type(ent);
447
448                 /* is it a bitfield type? */
449                 if (is_Primitive_type(bf_type) && get_primitive_base_type(bf_type) != NULL)
450                         adr = get_Sel_ptr(adr);
451         }
452         return adr;
453 }
454
455 /**
456  * Determine the alias relation between two addresses.
457  *
458  * @param addr1  pointer address of the first memory operation
459  * @param mode1  the mode of the accessed data through addr1
460  * @param addr2  pointer address of the second memory operation
461  * @param mode2  the mode of the accessed data through addr2
462  *
463  * @return found memory relation
464  */
465 static ir_alias_relation _get_alias_relation(
466         const ir_node *adr1, const ir_mode *mode1,
467         const ir_node *adr2, const ir_mode *mode2)
468 {
469         ir_entity             *ent1, *ent2;
470         unsigned              options;
471         long                  offset1 = 0;
472         long                  offset2 = 0;
473         const ir_node         *base1;
474         const ir_node         *base2;
475         const ir_node         *orig_adr1 = adr1;
476         const ir_node         *orig_adr2 = adr2;
477         ir_graph              *irg;
478         unsigned              mode_size;
479         ir_storage_class_class_t class1, class2, mod1, mod2;
480         int                   have_const_offsets;
481
482         if (! get_opt_alias_analysis())
483                 return ir_may_alias;
484
485         if (adr1 == adr2)
486                 return ir_sure_alias;
487
488         irg = get_irn_irg(adr1);
489         options = get_irg_memory_disambiguator_options(irg);
490
491         /* The Armageddon switch */
492         if (options & aa_opt_no_alias)
493                 return ir_no_alias;
494
495         /* do the addresses have constants offsets?
496          *  Note: nodes are normalized to have constants at right inputs,
497          *        sub X, C is normalized to add X, -C
498          */
499         have_const_offsets = 1;
500         while (is_Add(adr1)) {
501                 ir_node *add_right = get_Add_right(adr1);
502                 if (is_Const(add_right) && !mode_is_reference(get_irn_mode(add_right))) {
503                         ir_tarval *tv  = get_Const_tarval(add_right);
504                         offset1    += get_tarval_long(tv);
505                         adr1        = get_Add_left(adr1);
506                 } else if (mode_is_reference(get_irn_mode(add_right))) {
507                         adr1 = add_right;
508                         have_const_offsets = 0;
509                 } else {
510                         adr1 = get_Add_left(adr1);
511                         have_const_offsets = 0;
512                 }
513         }
514         while (is_Add(adr2)) {
515                 ir_node *add_right = get_Add_right(adr2);
516                 if (is_Const(add_right) && !mode_is_reference(get_irn_mode(add_right))) {
517                         ir_tarval *tv  = get_Const_tarval(add_right);
518                         offset2    += get_tarval_long(tv);
519                         adr2        = get_Add_left(adr2);
520                 } else if (mode_is_reference(get_irn_mode(add_right))) {
521                         adr2 = add_right;
522                         have_const_offsets = 0;
523                 } else {
524                         adr2 = get_Add_left(adr2);
525                         have_const_offsets = 0;
526                 }
527         }
528
529         mode_size = get_mode_size_bytes(mode1);
530         if (get_mode_size_bytes(mode2) > mode_size) {
531                 mode_size = get_mode_size_bytes(mode2);
532         }
533
534         /* same base address -> compare offsets if possible.
535          * FIXME: type long is not sufficient for this task ...
536          */
537         if (adr1 == adr2 && have_const_offsets) {
538                 if ((unsigned long)labs(offset2 - offset1) >= mode_size)
539                         return ir_no_alias;
540                 else
541                         return ir_sure_alias;
542         }
543
544         /*
545          * Bitfields can be constructed as Sels from its base address.
546          * As they have different entities, the disambiguator would find that they are
547          * alias free. While this is true for its values, it is false for the addresses
548          * (strictly speaking, the Sel's are NOT the addresses of the bitfields).
549          * So, skip those bitfield selecting Sel's.
550          */
551         adr1 = skip_Bitfield_Sels(adr1);
552         adr2 = skip_Bitfield_Sels(adr2);
553
554         /* skip Sels */
555         base1 = adr1;
556         base2 = adr2;
557         ent1  = NULL;
558         ent2  = NULL;
559         if (is_Sel(adr1)) {
560                 base1 = find_base_adr(adr1, &ent1);
561         }
562         if (is_Sel(adr2)) {
563                 base2 = find_base_adr(adr2, &ent2);
564         }
565
566         /* same base address -> compare Sel entities */
567         if (base1 == base2 && ent1 != NULL && ent2 != NULL) {
568                 if (ent1 != ent2)
569                         return ir_no_alias;
570                 else if (have_const_offsets)
571                         return different_sel_offsets(adr1, adr2);
572         }
573
574         mod1 = classify_pointer(base1, ent1);
575         mod2 = classify_pointer(base2, ent2);
576
577         class1 = get_base_sc(mod1);
578         class2 = get_base_sc(mod2);
579
580         /* struct-access cannot alias with variables */
581         if (ent1 == NULL && ent2 != NULL && is_compound_type(get_entity_owner(ent2))
582                 && (class1 == ir_sc_globalvar || class1 == ir_sc_localvar || class1 == ir_sc_tls || class1 == ir_sc_globaladdr)) {
583                 return ir_no_alias;
584         }
585         if (ent2 == NULL && ent1 != NULL && is_compound_type(get_entity_owner(ent1))
586                 && (class2 == ir_sc_globalvar || class2 == ir_sc_localvar || class2 == ir_sc_tls || class2 == ir_sc_globaladdr)) {
587                 return ir_no_alias;
588         }
589
590         if (class1 == ir_sc_pointer || class2 == ir_sc_pointer) {
591                 /* swap pointer class to class1 */
592                 if (class2 == ir_sc_pointer) {
593                         ir_storage_class_class_t temp = mod1;
594                         mod1 = mod2;
595                         mod2 = temp;
596                         class1 = get_base_sc(mod1);
597                         class2 = get_base_sc(mod2);
598                 }
599                 /* a pointer and an object whose address was never taken */
600                 if (mod2 & ir_sc_modifier_nottaken) {
601                         return ir_no_alias;
602                 }
603                 if (mod1 & ir_sc_modifier_argument) {
604                         if ( (options & aa_opt_no_alias_args)
605                                         && (mod2 & ir_sc_modifier_argument))
606                                 return ir_no_alias;
607                         if ( (options & aa_opt_no_alias_args_global)
608                                         && (class2 == ir_sc_globalvar
609                                                 || class2 == ir_sc_tls
610                                                 || class2 == ir_sc_globaladdr))
611                                 return ir_no_alias;
612                 }
613         } else if (class1 != class2) {
614                 /* two objects from different memory spaces */
615                 return ir_no_alias;
616         } else {
617                 /* both classes are equal */
618                 if (class1 == ir_sc_globalvar) {
619                         ir_entity *entity1 = get_SymConst_entity(base1);
620                         ir_entity *entity2 = get_SymConst_entity(base2);
621                         if (entity1 != entity2)
622                                 return ir_no_alias;
623
624                         /* for some reason CSE didn't happen yet for the 2 SymConsts... */
625                         return ir_may_alias;
626                 } else if (class1 == ir_sc_globaladdr) {
627                         ir_tarval *tv = get_Const_tarval(base1);
628                         offset1      += get_tarval_long(tv);
629                         tv            = get_Const_tarval(base2);
630                         offset2      += get_tarval_long(tv);
631
632                         if ((unsigned long)labs(offset2 - offset1) >= mode_size)
633                                 return ir_no_alias;
634                         else
635                                 return ir_sure_alias;
636                 }
637         }
638
639         /* Type based alias analysis */
640         if (options & aa_opt_type_based) {
641                 ir_alias_relation rel;
642
643                 if (options & aa_opt_byte_type_may_alias) {
644                         if (get_mode_size_bits(mode1) == 8 || get_mode_size_bits(mode2) == 8) {
645                                 /* One of the modes address a byte. Assume a ir_may_alias and leave
646                                    the type based check. */
647                                 goto leave_type_based_alias;
648                         }
649                 }
650                 /* cheap check: If the mode sizes did not match, the types MUST be different */
651                 if (get_mode_size_bits(mode1) != get_mode_size_bits(mode2))
652                         return ir_no_alias;
653
654                 /* cheap test: if only one is a reference mode, no alias */
655                 if (mode_is_reference(mode1) != mode_is_reference(mode2))
656                         return ir_no_alias;
657
658                 /* cheap test: if arithmetic is different, no alias */
659                 if (get_mode_arithmetic(mode1) != get_mode_arithmetic(mode2))
660                         return ir_no_alias;
661
662                 /* try rule R5 */
663                 rel = different_types(orig_adr1, orig_adr2);
664                 if (rel != ir_may_alias)
665                         return rel;
666 leave_type_based_alias:;
667         }
668
669         /* do we have a language specific memory disambiguator? */
670         if (language_disambuigator != NULL) {
671                 ir_alias_relation rel = language_disambuigator(orig_adr1, mode1, orig_adr2, mode2);
672                 if (rel != ir_may_alias)
673                         return rel;
674         }
675
676         /* access points-to information here */
677         return ir_may_alias;
678 }
679
680 ir_alias_relation get_alias_relation(
681         const ir_node *adr1, const ir_mode *mode1,
682         const ir_node *adr2, const ir_mode *mode2)
683 {
684         ir_alias_relation rel = _get_alias_relation(adr1, mode1, adr2, mode2);
685         DB((dbg, LEVEL_1, "alias(%+F, %+F) = %s\n", adr1, adr2, get_ir_alias_relation_name(rel)));
686         return rel;
687 }
688
689 void set_language_memory_disambiguator(DISAMBIGUATOR_FUNC func)
690 {
691         language_disambuigator = func;
692 }
693
694 /** The result cache for the memory disambiguator. */
695 static set *result_cache = NULL;
696
697 /** An entry in the relation cache. */
698 typedef struct mem_disambig_entry {
699         const ir_node     *adr1;    /**< The first address. */
700         const ir_mode     *mode1;   /**< The first address mode. */
701         const ir_node     *adr2;    /**< The second address. */
702         const ir_mode     *mode2;   /**< The second address mode. */
703         ir_alias_relation result;   /**< The alias relation result. */
704 } mem_disambig_entry;
705
706 #define HASH_ENTRY(adr1, adr2)  (hash_ptr(adr1) ^ hash_ptr(adr2))
707
708 /**
709  * Compare two relation cache entries.
710  */
711 static int cmp_mem_disambig_entry(const void *elt, const void *key, size_t size)
712 {
713         const mem_disambig_entry *p1 = (const mem_disambig_entry*) elt;
714         const mem_disambig_entry *p2 = (const mem_disambig_entry*) key;
715         (void) size;
716
717         return p1->adr1 == p2->adr1 && p1->adr2 == p2->adr2 &&
718                p1->mode1 == p2->mode1 && p1->mode2 == p2->mode2;
719 }
720
721 void mem_disambig_init(void)
722 {
723         result_cache = new_set(cmp_mem_disambig_entry, 8);
724 }
725
726 ir_alias_relation get_alias_relation_ex(
727         const ir_node *adr1, const ir_mode *mode1,
728         const ir_node *adr2, const ir_mode *mode2)
729 {
730         mem_disambig_entry key, *entry;
731
732         ir_fprintf(stderr, "%+F <-> %+F\n", adr1, adr2);
733
734         if (! get_opt_alias_analysis())
735                 return ir_may_alias;
736
737         if (get_irn_opcode(adr1) > get_irn_opcode(adr2)) {
738                 const ir_node *t = adr1;
739                 adr1 = adr2;
740                 adr2 = t;
741         }
742
743         key.adr1  = adr1;
744         key.adr2  = adr2;
745         key.mode1 = mode1;
746         key.mode2 = mode2;
747         entry = set_find(mem_disambig_entry, result_cache, &key, sizeof(key), HASH_ENTRY(adr1, adr2));
748         if (entry != NULL)
749                 return entry->result;
750
751         key.result = get_alias_relation(adr1, mode1, adr2, mode2);
752
753         (void)set_insert(mem_disambig_entry, result_cache, &key, sizeof(key), HASH_ENTRY(adr1, adr2));
754         return key.result;
755 }
756
757 void mem_disambig_term(void)
758 {
759         if (result_cache != NULL) {
760                 del_set(result_cache);
761                 result_cache = NULL;
762         }
763 }
764
765 /**
766  * Check the mode of a Load/Store with the mode of the entity
767  * that is accessed.
768  * If the mode of the entity and the Load/Store mode do not match, we
769  * have the bad reinterpret case:
770  *
771  * int i;
772  * char b = *(char *)&i;
773  *
774  * We do NOT count this as one value and return address_taken
775  * in that case.
776  * However, we support an often used case. If the mode is two-complement
777  * we allow casts between signed/unsigned.
778  *
779  * @param mode     the mode of the Load/Store
780  * @param ent_mode the mode of the accessed entity
781  *
782  * @return non-zero if the Load/Store is a hidden cast, zero else
783  */
784 static int is_hidden_cast(const ir_mode *mode, const ir_mode *ent_mode)
785 {
786         if (ent_mode == NULL)
787                 return false;
788
789         if (ent_mode != mode) {
790                 if (ent_mode == NULL ||
791                         get_mode_size_bits(ent_mode) != get_mode_size_bits(mode) ||
792                         get_mode_arithmetic(ent_mode) != irma_twos_complement ||
793                         get_mode_arithmetic(mode) != irma_twos_complement)
794                         return true;
795         }
796         return false;
797 }
798
799 /**
800  * Determine the usage state of a node (or its successor Sels).
801  *
802  * @param irn  the node
803  */
804 static ir_entity_usage determine_entity_usage(const ir_node *irn, ir_entity *entity)
805 {
806         int       i;
807         ir_mode   *emode, *mode;
808         ir_node   *value;
809         ir_type   *tp;
810         unsigned   res = 0;
811
812         for (i = get_irn_n_outs(irn) - 1; i >= 0; --i) {
813                 ir_node *succ = get_irn_out(irn, i);
814
815                 switch (get_irn_opcode(succ)) {
816                 case iro_Load:
817                         /* beware: irn might be a Id node here, so irn might be not
818                            equal to get_Load_ptr(succ) */
819                         res |= ir_usage_read;
820
821                         /* check if this load is not a hidden conversion */
822                         mode  = get_Load_mode(succ);
823                         emode = get_type_mode(get_entity_type(entity));
824                         if (is_hidden_cast(mode, emode))
825                                 res |= ir_usage_reinterpret_cast;
826                         break;
827
828                 case iro_Store:
829                         /* check that the node is not the Store's value */
830                         if (irn == get_Store_value(succ)) {
831                                 res |= ir_usage_unknown;
832                         }
833                         if (irn == get_Store_ptr(succ)) {
834                                 res |= ir_usage_write;
835
836                                 /* check if this Store is not a hidden conversion */
837                                 value = get_Store_value(succ);
838                                 mode  = get_irn_mode(value);
839                                 emode = get_type_mode(get_entity_type(entity));
840                                 if (is_hidden_cast(mode, emode))
841                                         res |= ir_usage_reinterpret_cast;
842                         }
843                         assert(irn != get_Store_mem(succ));
844                         break;
845
846                 case iro_CopyB:
847                         /* CopyB are like Loads/Stores */
848                         tp  = get_entity_type(entity);
849                         if (tp != get_CopyB_type(succ)) {
850                                 /* bad, different types, might be a hidden conversion */
851                                 res |= ir_usage_reinterpret_cast;
852                         }
853                         if (irn == get_CopyB_dst(succ)) {
854                                 res |= ir_usage_write;
855                         } else {
856                                 assert(irn == get_CopyB_src(succ));
857                                 res |= ir_usage_read;
858                         }
859                         break;
860
861                 case iro_Add:
862                 case iro_Sub:
863                         /* Check the successor of irn. */
864                         res |= determine_entity_usage(succ, entity);
865                         break;
866                 case iro_Sel: {
867                         ir_entity *sel_entity = get_Sel_entity(succ);
868                         /* this analysis can't handle unions correctly */
869                         if (is_Union_type(get_entity_owner(sel_entity))) {
870                                 res |= ir_usage_unknown;
871                                 break;
872                         }
873                         /* Check the successor of irn. */
874                         res |= determine_entity_usage(succ, sel_entity);
875                         break;
876                 }
877
878                 case iro_Call:
879                         if (irn == get_Call_ptr(succ)) {
880                                 /* TODO: we could check for reinterpret casts here...
881                                  * But I doubt anyone is interested in that bit for
882                                  * function entities and I'm too lazy to write the code now.
883                                  */
884                                 res |= ir_usage_read;
885                         } else {
886                                 assert(irn != get_Call_mem(succ));
887                                 res |= ir_usage_unknown;
888                         }
889                         break;
890
891                 /* skip identities */
892                 case iro_Id:
893                         res |= determine_entity_usage(succ, entity);
894                         break;
895
896                 /* skip tuples */
897                 case iro_Tuple: {
898                         int input_nr;
899                         for (input_nr = get_Tuple_n_preds(succ) - 1; input_nr >= 0;
900                                         --input_nr) {
901                                 ir_node *pred = get_Tuple_pred(succ, input_nr);
902                                 if (pred == irn) {
903                                         int k;
904                                         /* we found one input */
905                                         for (k = get_irn_n_outs(succ) - 1; k >= 0; --k) {
906                                                 ir_node *proj = get_irn_out(succ, k);
907
908                                                 if (is_Proj(proj) && get_Proj_proj(proj) == input_nr) {
909                                                         res |= determine_entity_usage(proj, entity);
910                                                         break;
911                                                 }
912                                         }
913                                 }
914                         }
915                         break;
916                 }
917
918                 default:
919                         /* another op, we don't know anything (we could do more advanced
920                          * things like a dataflow analysis here) */
921                         res |= ir_usage_unknown;
922                         break;
923                 }
924         }
925
926         return (ir_entity_usage) res;
927 }
928
929 /**
930  * Update the usage flags of all frame entities.
931  */
932 static void analyse_irg_entity_usage(ir_graph *irg)
933 {
934         ir_type *ft = get_irg_frame_type(irg);
935         ir_node *irg_frame;
936         size_t i, n;
937         int j, k, static_link_arg;
938
939         assure_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_OUTS);
940
941         /* set initial state to not_taken, as this is the "smallest" state */
942         for (i = 0, n = get_class_n_members(ft); i < n; ++i) {
943                 ir_entity *ent = get_class_member(ft, i);
944
945                 /* methods can only be analyzed globally */
946                 if (! is_method_entity(ent)) {
947                         ir_entity_usage flags = ir_usage_none;
948                         if (get_entity_linkage(ent) & IR_LINKAGE_HIDDEN_USER)
949                                 flags = ir_usage_unknown;
950                         set_entity_usage(ent, flags);
951                 }
952         }
953
954         irg_frame = get_irg_frame(irg);
955
956         for (j = get_irn_n_outs(irg_frame) - 1; j >= 0; --j) {
957                 ir_node        *succ = get_irn_out(irg_frame, j);
958                 ir_entity      *entity;
959                 unsigned        flags;
960
961                 if (!is_Sel(succ))
962                         continue;
963
964                 entity = get_Sel_entity(succ);
965                 flags  = get_entity_usage(entity);
966                 flags |= determine_entity_usage(succ, entity);
967                 set_entity_usage(entity, (ir_entity_usage) flags);
968         }
969
970         /* check inner functions accessing outer frame */
971         static_link_arg = 0;
972         for (i = 0, n = get_class_n_members(ft); i < n; ++i) {
973                 ir_entity *ent = get_class_member(ft, i);
974                 ir_graph  *inner_irg;
975                 ir_node   *args;
976
977                 if (! is_method_entity(ent))
978                         continue;
979
980                 inner_irg = get_entity_irg(ent);
981                 if (inner_irg == NULL)
982                         continue;
983
984                 assure_irg_outs(inner_irg);
985                 args = get_irg_args(inner_irg);
986                 for (j = get_irn_n_outs(args) - 1; j >= 0; --j) {
987                         ir_node *arg = get_irn_out(args, j);
988
989                         if (get_Proj_proj(arg) == static_link_arg) {
990                                 for (k = get_irn_n_outs(arg) - 1; k >= 0; --k) {
991                                         ir_node *succ = get_irn_out(arg, k);
992
993                                         if (is_Sel(succ)) {
994                                                 ir_entity *entity = get_Sel_entity(succ);
995
996                                                 if (get_entity_owner(entity) == ft) {
997                                                         /* found an access to the outer frame */
998                                                         unsigned flags;
999
1000                                                         flags  = get_entity_usage(entity);
1001                                                         flags |= determine_entity_usage(succ, entity);
1002                                                         set_entity_usage(entity, (ir_entity_usage) flags);
1003                                                 }
1004                                         }
1005                                 }
1006                         }
1007                 }
1008         }
1009
1010         /* now computed */
1011         add_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_ENTITY_USAGE);
1012 }
1013
1014 void assure_irg_entity_usage_computed(ir_graph *irg)
1015 {
1016         if (irg_has_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_ENTITY_USAGE))
1017                 return;
1018
1019         analyse_irg_entity_usage(irg);
1020 }
1021
1022
1023 /**
1024  * Initialize the entity_usage flag for a global type like type.
1025  */
1026 static void init_entity_usage(ir_type *tp)
1027 {
1028         size_t i, n;
1029
1030         /* We have to be conservative: All external visible entities are unknown */
1031         for (i = 0, n = get_compound_n_members(tp); i < n; ++i) {
1032                 ir_entity       *ent   = get_compound_member(tp, i);
1033                 unsigned         flags = ir_usage_none;
1034
1035                 if (entity_is_externally_visible(ent)) {
1036                         flags |= ir_usage_unknown;
1037                 }
1038                 set_entity_usage(ent, (ir_entity_usage) flags);
1039         }
1040 }
1041
1042 /**
1043  * Mark all entities used in the initializer as unknown usage.
1044  *
1045  * @param initializer  the initializer to check
1046  */
1047 static void check_initializer_nodes(ir_initializer_t *initializer)
1048 {
1049         size_t  i;
1050         ir_node *n;
1051
1052         switch (initializer->kind) {
1053         case IR_INITIALIZER_CONST:
1054                 /* let's check if it's an address */
1055                 n = initializer->consti.value;
1056                 if (is_SymConst_addr_ent(n)) {
1057                         ir_entity *ent = get_SymConst_entity(n);
1058                         set_entity_usage(ent, ir_usage_unknown);
1059                 }
1060                 return;
1061         case IR_INITIALIZER_TARVAL:
1062         case IR_INITIALIZER_NULL:
1063                 return;
1064         case IR_INITIALIZER_COMPOUND:
1065                 for (i = 0; i < initializer->compound.n_initializers; ++i) {
1066                         ir_initializer_t *sub_initializer
1067                                 = initializer->compound.initializers[i];
1068                         check_initializer_nodes(sub_initializer);
1069                 }
1070                 return;
1071         }
1072         panic("invalid initializer found");
1073 }
1074
1075 /**
1076  * Mark all entities used in the initializer for the given entity as unknown
1077  * usage.
1078  *
1079  * @param ent  the entity
1080  */
1081 static void check_initializer(ir_entity *ent)
1082 {
1083         /* Beware: Methods are always initialized with "themself". This does not
1084          * count as a taken address.
1085          * TODO: this initialisation with "themself" is wrong and should be removed
1086          */
1087         if (is_Method_type(get_entity_type(ent)))
1088                 return;
1089
1090         if (ent->initializer != NULL) {
1091                 check_initializer_nodes(ent->initializer);
1092         }
1093 }
1094
1095
1096 /**
1097  * Mark all entities used in initializers as unknown usage.
1098  *
1099  * @param tp  a compound type
1100  */
1101 static void check_initializers(ir_type *tp)
1102 {
1103         size_t i, n;
1104
1105         for (i = 0, n = get_compound_n_members(tp); i < n; ++i) {
1106                 ir_entity *ent = get_compound_member(tp, i);
1107
1108                 check_initializer(ent);
1109         }
1110 }
1111
1112 #ifdef DEBUG_libfirm
1113 /**
1114  * Print the entity usage flags of all entities of a given type for debugging.
1115  *
1116  * @param tp  a compound type
1117  */
1118 static void print_entity_usage_flags(const ir_type *tp)
1119 {
1120         size_t i, n;
1121         for (i = 0, n = get_compound_n_members(tp); i < n; ++i) {
1122                 ir_entity *ent = get_compound_member(tp, i);
1123                 ir_entity_usage flags = get_entity_usage(ent);
1124
1125                 if (flags == 0)
1126                         continue;
1127                 ir_printf("%+F:", ent);
1128                 if (flags & ir_usage_address_taken)
1129                         printf(" address_taken");
1130                 if (flags & ir_usage_read)
1131                         printf(" read");
1132                 if (flags & ir_usage_write)
1133                         printf(" write");
1134                 if (flags & ir_usage_reinterpret_cast)
1135                         printf(" reinterp_cast");
1136                 printf("\n");
1137         }
1138 }
1139 #endif /* DEBUG_libfirm */
1140
1141 /**
1142  * Post-walker: check for global entity address
1143  */
1144 static void check_global_address(ir_node *irn, void *data)
1145 {
1146         ir_entity *ent;
1147         unsigned flags;
1148         (void) data;
1149
1150         if (is_SymConst_addr_ent(irn)) {
1151                 /* A global. */
1152                 ent = get_SymConst_entity(irn);
1153         } else
1154                 return;
1155
1156         flags = get_entity_usage(ent);
1157         flags |= determine_entity_usage(irn, ent);
1158         set_entity_usage(ent, (ir_entity_usage) flags);
1159 }
1160
1161 /**
1162  * Update the entity usage flags of all global entities.
1163  */
1164 static void analyse_irp_globals_entity_usage(void)
1165 {
1166         size_t i, n;
1167         ir_segment_t s;
1168
1169         for (s = IR_SEGMENT_FIRST; s <= IR_SEGMENT_LAST; ++s) {
1170                 ir_type *type = get_segment_type(s);
1171                 init_entity_usage(type);
1172         }
1173
1174         for (s = IR_SEGMENT_FIRST; s <= IR_SEGMENT_LAST; ++s) {
1175                 ir_type *type = get_segment_type(s);
1176                 check_initializers(type);
1177         }
1178
1179         for (i = 0, n = get_irp_n_irgs(); i < n; ++i) {
1180                 ir_graph *irg = get_irp_irg(i);
1181
1182                 assure_irg_outs(irg);
1183                 irg_walk_graph(irg, NULL, check_global_address, NULL);
1184         }
1185
1186 #ifdef DEBUG_libfirm
1187         if (firm_dbg_get_mask(dbg) & LEVEL_1) {
1188                 for (s = IR_SEGMENT_FIRST; s <= IR_SEGMENT_LAST; ++s) {
1189                         print_entity_usage_flags(get_segment_type(s));
1190                 }
1191         }
1192 #endif /* DEBUG_libfirm */
1193
1194         /* now computed */
1195         irp->globals_entity_usage_state = ir_entity_usage_computed;
1196 }
1197
1198 ir_entity_usage_computed_state get_irp_globals_entity_usage_state(void)
1199 {
1200         return irp->globals_entity_usage_state;
1201 }
1202
1203 void set_irp_globals_entity_usage_state(ir_entity_usage_computed_state state)
1204 {
1205         irp->globals_entity_usage_state = state;
1206 }
1207
1208 void assure_irp_globals_entity_usage_computed(void)
1209 {
1210         if (irp->globals_entity_usage_state != ir_entity_usage_not_computed)
1211                 return;
1212
1213         analyse_irp_globals_entity_usage();
1214 }
1215
1216 void firm_init_memory_disambiguator(void)
1217 {
1218         FIRM_DBG_REGISTER(dbg, "firm.ana.irmemory");
1219         FIRM_DBG_REGISTER(dbgcall, "firm.opt.cc");
1220 }
1221
1222
1223 /** Maps method types to cloned method types. */
1224 static pmap *mtp_map;
1225
1226 /**
1227  * Clone a method type if not already cloned.
1228  *
1229  * @param tp  the type to clone
1230  */
1231 static ir_type *clone_type_and_cache(ir_type *tp)
1232 {
1233         ir_type *res = pmap_get(ir_type, mtp_map, tp);
1234
1235         if (res == NULL) {
1236                 res = clone_type_method(tp);
1237                 pmap_insert(mtp_map, tp, res);
1238         }
1239
1240         return res;
1241 }
1242
1243 /**
1244  * Walker: clone all call types of Calls to methods having the
1245  * mtp_property_private property set.
1246  */
1247 static void update_calls_to_private(ir_node *call, void *env)
1248 {
1249         (void) env;
1250         if (is_Call(call)) {
1251                 ir_node *ptr = get_Call_ptr(call);
1252
1253                 if (is_SymConst(ptr)) {
1254                         ir_entity *ent = get_SymConst_entity(ptr);
1255                         ir_type *ctp = get_Call_type(call);
1256
1257                         if (get_entity_additional_properties(ent) & mtp_property_private) {
1258                                 if ((get_method_additional_properties(ctp) & mtp_property_private) == 0) {
1259                                         ctp = clone_type_and_cache(ctp);
1260                                         add_method_additional_properties(ctp, mtp_property_private);
1261                                         set_Call_type(call, ctp);
1262                                         DB((dbgcall, LEVEL_1, "changed call to private method %+F using cloned type %+F\n", ent, ctp));
1263                                 }
1264                         }
1265                 }
1266         }
1267 }
1268
1269 void mark_private_methods(void)
1270 {
1271         size_t i, n;
1272         int changed = 0;
1273
1274         assure_irp_globals_entity_usage_computed();
1275
1276         mtp_map = pmap_create();
1277
1278         /* first step: change the calling conventions of the local non-escaped entities */
1279         for (i = 0, n = get_irp_n_irgs(); i < n; ++i) {
1280                 ir_graph        *irg   = get_irp_irg(i);
1281                 ir_entity       *ent   = get_irg_entity(irg);
1282                 ir_entity_usage  flags = get_entity_usage(ent);
1283
1284                 if (!(flags & ir_usage_address_taken) && !entity_is_externally_visible(ent)) {
1285                         ir_type *mtp = get_entity_type(ent);
1286
1287                         add_entity_additional_properties(ent, mtp_property_private);
1288                         DB((dbgcall, LEVEL_1, "found private method %+F\n", ent));
1289                         if ((get_method_additional_properties(mtp) & mtp_property_private) == 0) {
1290                                 /* need a new type */
1291                                 mtp = clone_type_and_cache(mtp);
1292                                 add_method_additional_properties(mtp, mtp_property_private);
1293                                 set_entity_type(ent, mtp);
1294                                 DB((dbgcall, LEVEL_2, "changed entity type of %+F to %+F\n", ent, mtp));
1295                                 changed = 1;
1296                         }
1297                 }
1298         }
1299
1300         if (changed)
1301                 all_irg_walk(NULL, update_calls_to_private, NULL);
1302
1303         pmap_destroy(mtp_map);
1304 }
1305
1306 ir_prog_pass_t *mark_private_methods_pass(const char *name)
1307 {
1308         return def_prog_pass(name ? name : "mark_private_methods", mark_private_methods);
1309 }