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