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