some belady3 fixes
[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 different.
280  *
281  * @param adr1  The first address.
282  * @param adr2  The second address.
283  */
284 static ir_alias_relation different_sel_offsets(ir_node *sel1, ir_node *sel2) {
285         /* seems to be broken */
286         (void) sel1;
287         (void) sel2;
288 #if 0
289         ir_entity *ent1 = get_Sel_entity(sel1);
290         ir_entity *ent2 = get_Sel_entity(sel2);
291         int i, check_arr = 0;
292
293         if (ent1 == ent2)
294                 check_arr = 1;
295         else {
296                 ir_type *tp1 = get_entity_type(ent1);
297                 ir_type *tp2 = get_entity_type(ent2);
298
299                 if (tp1 == tp2)
300                         check_arr = 1;
301                 else if (get_type_state(tp1) == layout_fixed && get_type_state(tp2) == layout_fixed &&
302                          get_type_size_bits(tp1) == get_type_size_bits(tp2))
303                         check_arr = 1;
304         }
305         if (check_arr) {
306                 /* we select an entity of same size, check for indexes */
307                 int n = get_Sel_n_indexs(sel1);
308                 int have_no = 0;
309
310                 if (n > 0 && n == get_Sel_n_indexs(sel2)) {
311                         /* same non-zero number of indexes, an array access, check */
312                         for (i = 0; i < n; ++i) {
313                                 ir_node *idx1 = get_Sel_index(sel1, i);
314                                 ir_node *idx2 = get_Sel_index(sel2, i);
315                                 ir_alias_relation res = different_index(idx1, idx2, 0); /* we can safely IGNORE the size here if it's at least >0 */
316
317                                 if (res == may_alias)
318                                         return may_alias;
319                                 else if (res == no_alias)
320                                         have_no = 1;
321                         }
322                         /* if we have at least one no_alias, there is no alias relation, else we have sure */
323                         return have_no > 0 ? no_alias : sure_alias;
324                 }
325         }
326 #endif
327         return may_alias;
328 }  /* different_sel_offsets */
329
330 /**
331  * Determine the alias relation by checking if adr1 and adr2 are pointer
332  * to different type.
333  *
334  * @param adr1    The first address.
335  * @param adr2    The second address.
336  */
337 static ir_alias_relation different_types(ir_node *adr1, ir_node *adr2)
338 {
339         ir_entity *ent1 = NULL, *ent2 = NULL;
340
341         if (is_SymConst_addr_ent(adr1))
342                 ent1 = get_SymConst_entity(adr1);
343         else if (is_Sel(adr1))
344                 ent1 = get_Sel_entity(adr1);
345
346         if (is_SymConst_addr_ent(adr2))
347                 ent2 = get_SymConst_entity(adr2);
348         else if (is_Sel(adr2))
349                 ent2 = get_Sel_entity(adr2);
350
351         if (ent1 != NULL && ent2 != NULL) {
352                 ir_type *tp1 = get_entity_type(ent1);
353                 ir_type *tp2 = get_entity_type(ent2);
354
355                 if (tp1 != tp2) {
356                         if (is_Pointer_type(tp1) && is_Pointer_type(tp2)) {
357                                 /* do deref until no pointer types are found */
358                                 do {
359                                         tp1 = get_pointer_points_to_type(tp1);
360                                         tp2 = get_pointer_points_to_type(tp2);
361                                 } while (is_Pointer_type(tp1) && is_Pointer_type(tp2));
362                         }
363
364                         if (get_type_tpop(tp1) != get_type_tpop(tp2)) {
365                                 /* different type structure */
366                                 return no_alias;
367                         }
368                         if (is_Class_type(tp1)) {
369                                 /* check class hierarchy */
370                                 if (! is_SubClass_of(tp1, tp2) &&
371                                         ! is_SubClass_of(tp2, tp1))
372                                         return no_alias;
373                         } else {
374                                 /* different types */
375                                 return no_alias;
376                         }
377                 }
378         }
379         return may_alias;
380 }  /* different_types */
381
382 /**
383  * Check if an offset is a constant and these constant is bigger or equal
384  * than a given size.
385  */
386 static int check_const_offset(ir_node *offset, int size) {
387         ir_mode *mode = get_irn_mode(offset);
388
389         /* ok, we found an offset, check for constant */
390         if (is_Const(offset) && mode_is_int(mode)) {
391                 tarval *tv = new_tarval_from_long(size, mode);
392
393                 /* size <= offset ? */
394                 if (tarval_cmp(tv, get_Const_tarval(offset)) & (pn_Cmp_Eq|pn_Cmp_Lt))
395                         return 1;
396         }
397         return 0;
398 }  /* check_const_offset */
399
400 /**
401  * Check if we can determine that the two pointers always have an offset bigger then size
402  */
403 static ir_alias_relation _different_pointer(ir_node *adr1, ir_node *adr2, int size) {
404         int found = 0;
405
406         if (is_Add(adr1)) {
407                 /* first address is the result of a pointer addition */
408                 ir_node *l1 = get_Add_left(adr1);
409                 ir_node *r1 = get_Add_right(adr1);
410
411                 if (l1 == adr2) {
412                         found = check_const_offset(r1, size);
413                 } else if (r1 == adr2) {
414                         found = check_const_offset(l1, size);
415                 } else if (is_Add(adr2)) {
416                         /* second address is the result of a pointer addition */
417                         ir_node *l2 = get_Add_left(adr2);
418                         ir_node *r2 = get_Add_right(adr2);
419
420                         if (l1 == l2) {
421                                 return _different_pointer(r1, r2, size);
422                         } else if (l1 == r2) {
423                                 return _different_pointer(r1, l2, size);
424                         } else if (r1 == l2) {
425                                 return _different_pointer(l1, r2, size);
426                         } else if (r1 == r2) {
427                                 return _different_pointer(l1, l2, size);
428                         }
429                 }
430         } else if (is_Add(adr2)) {
431                 /* second address is the result of a pointer addition */
432                 ir_node *l2 = get_Add_left(adr2);
433                 ir_node *r2  = get_Add_right(adr2);
434
435                 if (l2 == adr1) {
436                         found = check_const_offset(r2, size);
437                 } else if (r2 == adr1) {
438                         found = check_const_offset(l2, size);
439                 }
440         } else {
441                 return different_index(adr1, adr2, size);
442         }
443         return found ? no_alias : may_alias;
444 }  /* _different_pointer */
445
446 /**
447  * Check if we can determine that the two pointers always have an offset bigger then the maximum size of mode1, mode2
448  */
449 static ir_alias_relation different_pointer(ir_node *adr1, ir_mode *mode1, ir_node *adr2, ir_mode *mode2) {
450         int size = get_mode_size_bytes(mode1);
451         int n    = get_mode_size_bytes(mode2);
452
453         if (n > size)
454                 size = n;
455         return _different_pointer(adr1, adr2, size);
456 }  /* different_pointer */
457
458 /**
459  * Returns non-zero if a node is a routine parameter.
460  *
461  * @param node  the Proj node to test
462  */
463 static int is_arg_Proj(ir_node *node) {
464         node = get_Proj_pred(node);
465         if (! is_Proj(node))
466                 return 0;
467         return pn_Start_T_args == get_Proj_proj(node) && is_Start(get_Proj_pred(node));
468 }  /* is_arg_Proj */
469
470 /**
471  * Returns non-zero if a node is a result on a malloc-like routine.
472  *
473  * @param node  the Proj node to test
474  */
475 static int is_malloc_Result(ir_node *node) {
476         node = get_Proj_pred(node);
477         if (! is_Proj(node))
478                 return 0;
479         node = get_Proj_pred(node);
480         if (! is_Call(node))
481                 return 0;
482         node = get_Call_ptr(node);
483         if (is_SymConst_addr_ent(node)) {
484                 ir_entity *ent = get_SymConst_entity(node);
485
486                 if (get_entity_additional_properties(ent) & mtp_property_malloc)
487                         return 1;
488                 return 0;
489         }
490         return 0;
491 }  /* is_malloc_Result */
492
493 /**
494  * Returns true if an address represents a global variable.
495  *
496  * @param irn  the node representing the address
497  */
498 static INLINE int is_global_var(ir_node *irn) {
499         return is_SymConst_addr_ent(irn);
500 }  /* is_global_var */
501
502 /**
503  * Determine the alias relation between two addresses.
504  */
505 static ir_alias_relation _get_alias_relation(
506         ir_graph *irg,
507         ir_node *adr1, ir_mode *mode1,
508         ir_node *adr2, ir_mode *mode2)
509 {
510         ir_opcode op1, op2;
511         ir_entity *ent1, *ent2;
512         unsigned options;
513
514         if (! get_opt_alias_analysis())
515                 return may_alias;
516
517         if (adr1 == adr2)
518                 return sure_alias;
519
520         options = get_irg_memory_disambiguator_options(irg);
521
522         /* The Armageddon switch */
523         if (options & aa_opt_no_alias)
524                 return no_alias;
525
526         /* Two save some code, sort the addresses by its id's. Beware, this
527            might break some things, so better check here. */
528         assert(iro_SymConst < iro_Sel && iro_Sel < iro_Proj && "Code dependence broken");
529         op1 = get_irn_opcode(adr1);
530         op2 = get_irn_opcode(adr2);
531
532         if (op1 > op2) {
533                 ir_node *t = adr1;
534                 ir_mode *m = mode1;
535                 adr1  = adr2;
536                 mode1 = mode2;
537                 adr2  = t;
538                 mode2 = m;
539         }
540
541         if (is_global_var(adr1)) {
542                 /* first address is a global variable */
543
544                 if (is_global_var(adr2)) {
545                         /* both addresses are global variables and we know
546                            they are different (R1 a) */
547                         if (get_SymConst_entity(adr1) != get_SymConst_entity(adr2))
548                                 return no_alias;
549                         else {
550                                 /* equal entity addresses */
551                                 return sure_alias;
552                         }
553                 } else if (is_Sel(adr2)) {
554                         ir_node *base2 = find_base_adr(adr2, &ent2);
555
556                         if (is_global_var(base2)) {
557                                 /* base2 address is a global var (R1 a) */
558                                 if (adr1 != base2)
559                                         return no_alias;
560                         } else if (base2 == get_irg_frame(irg)) {
561                                 /* the second one is a local variable so they are always
562                                    different (R1 b) */
563                                 return no_alias;
564                         } else if (base2 == get_irg_tls(irg)) {
565                                 /* the second one is a TLS variable so they are always
566                                    different (R1 c) */
567                                 return no_alias;
568                         } else if (is_Proj(base2)) {
569                                 if (is_malloc_Result(base2)) {
570                                         /* the second one is an offset from a result of a malloc like call, ie.
571                                            freshly allocated non-aliases heap memory, (R1 f) */
572                                         return no_alias;
573                                 }
574                         }
575                 } else if (is_Proj(adr2)) {
576                         if (is_malloc_Result(adr2)) {
577                                 /* the second one is a result of a malloc like call, ie.
578                                    freshly allocated non-aliases heap memory, (R1 f) */
579                                 return no_alias;
580                         }
581                 }
582
583                 /* Here we are: the first is a global var, the second some pointer. */
584                 ent1 = get_SymConst_entity(adr1);
585                 if (get_entity_address_taken(ent1) == ir_address_not_taken) {
586                         /* The address of the global variable was never taken, so
587                            the pointer cannot match (R2). */
588                         return no_alias;
589                 }
590         } else if (is_Sel(adr1)) {
591                 /* the first address is a Sel */
592                 ir_node *base1 = find_base_adr(adr1, &ent1);
593
594                 if (base1 == get_irg_frame(irg)) {
595                         /* first is a local variable ent1 */
596                         if (is_Sel(adr2)) {
597                                 /* the second address is a Sel */
598                                 ir_node *base2 = find_base_adr(adr2, &ent2);
599
600                                 if (base1 == base2) {
601                                         /* identical bases: both are local variables */
602                                         if (ent1 != ent2) {
603                                                 /* both addresses are local variables and we know
604                                                they are different (R1 a) */
605                                                 return no_alias;
606                                         } else {
607                                                 /* same local var */
608                                                 return different_sel_offsets(adr1, adr2);
609                                         }
610                                 } else if (base2 == get_irg_tls(irg)) {
611                                         /* the second one is a TLS variable so they are always
612                                        different (R1 d) */
613                                         return no_alias;
614                                 } else if (is_Proj(base2)) {
615                                         if (is_arg_Proj(base2)) {
616                                                 /* the second one is an offset from a parameter so they are
617                                                    always different (R1 e) */
618                                                 return no_alias;
619                                         } else if (is_malloc_Result(base2)) {
620                                                 /* the second one is an offset from a result of a malloc like call, ie.
621                                                    freshly allocated non-aliases heap memory (R1 g) */
622                                                 return no_alias;
623                                         }
624                                 }
625                         } else if (is_Proj(adr2)) {
626                                 if (is_arg_Proj(adr2)) {
627                                         /* a local variable and a parameter are always different (R1 e) */
628                                         return no_alias;
629                                 } else if (is_malloc_Result(adr2)) {
630                                         /* the second one is a result of a malloc like call, ie.
631                                            freshly allocated non-aliases heap memory (R1 g) */
632                                         return no_alias;
633                                 }
634                         }
635                 } else if (base1 == get_irg_tls(irg)) {
636                         /* the first is a TLS variable */
637                         if (is_Sel(adr2)) {
638                                 /* the second address is a Sel */
639                                 ir_node *base2 = find_base_adr(adr2, &ent2);
640
641                                 if (base1 == base2) {
642                                         if (ent1 != ent2) {
643                                                 /* both addresses are tls variables and we know
644                                                they are different (R1 a) */
645                                                 return no_alias;
646                                         } else {
647                                                 /* same tls var */
648                                                 return different_sel_offsets(adr1, adr2);
649                                         }
650                                 } else if (base2 == get_irg_frame(irg)) {
651                                         /* the first one is a tls variable, the second a local one,
652                                            they are different (R1 d) */
653                                         return no_alias;
654                                 } else if (is_Proj(base2)) {
655                                         if (is_malloc_Result(base2)) {
656                                                 /* the second one is an offset from a result of a malloc like call, ie.
657                                                    freshly allocated non-aliases heap memory (R1 h) */
658                                                 return no_alias;
659                                         }
660                                 }
661                         } else if (is_Proj(adr2)) {
662                                 if (is_malloc_Result(adr2)) {
663                                         /* the second one is an offset from a result of a malloc like call, ie.
664                                            freshly allocated non-aliases heap memory (R1 h) */
665                                         return no_alias;
666                                 }
667                         }
668                 } else if (is_Proj(base1)) {
669                         if (is_arg_Proj(base1)) {
670                                 /* the first one is an offset from a parameter */
671                                 if (is_Sel(adr2)) {
672                                         /* the second address is a Sel */
673                                         ir_node *base2 = find_base_adr(adr2, &ent2);
674
675                                         if (base2 == get_irg_frame(irg)) {
676                                                 /* the second one is a local variable so they are always
677                                                    different (R1 e) */
678                                                 return no_alias;
679                                         } else if (is_Proj(base2)) {
680                                                 if (is_malloc_Result(base2)) {
681                                                         /* the second one is an offset from a result of a malloc like call, ie.
682                                                            freshly allocated non-aliases heap memory (R1 Ã­) */
683                                                         return no_alias;
684                                                 }
685                                         }
686                                 } else if (is_Proj(adr2)) {
687                                         if (is_malloc_Result(adr2)) {
688                                                 /* the second one is a malloc like call, ie.
689                                                    freshly allocated non-aliases heap memory (R1 Ã­) */
690                                                 return no_alias;
691                                         }
692                                 }
693                         }
694                 } else if (is_global_var(base1)) {
695                         /* the first one is an offset from a global variable */
696                         ent1 = get_SymConst_entity(base1);
697                         if (is_Sel(adr2)) {
698                                 /* the second address is a Sel */
699                                 ir_node *base2 = find_base_adr(adr2, &ent2);
700
701                                 if (base1 == base2) {
702                                         /* same global var */
703                                         return different_sel_offsets(adr1, adr2);
704                                 } else if (base2 == get_irg_frame(irg)) {
705                                         /* the second one is a local variable so they are always
706                                        different (R1 a) */
707                                         return no_alias;
708                                 } else if (base2 == get_irg_tls(irg)) {
709                                         /* the second one is a TLS variable so they are always
710                                        different (R1 a) */
711                                         return no_alias;
712                                 } else if (is_Proj(base2)) {
713                                         if (is_arg_Proj(base2)) {
714                                                 if (get_entity_address_taken(ent1) == ir_address_not_taken) {
715                                                         /* The address of the global variable was never taken, so
716                                                            the pointer cannot match (R2). */
717                                                         return no_alias;
718                                                 }
719                                         } else if (is_malloc_Result(base2)) {
720                                                 /* the second one is an offset from a result of a malloc like call, ie.
721                                                    freshly allocated non-aliases heap memory (R1 g) */
722                                                 return no_alias;
723                                         }
724                                 } else if (is_global_var(base2)) {
725                                         ent2 = get_SymConst_entity(base2);
726                                         /* both addresses are global variables and we know
727                                            they are different (R1 a) */
728                                         if (ent1 != ent2)
729                                                 return no_alias;
730                                 }
731                         }
732                 }
733         } else {
734                 /* Note: we cannot check for malloc result here, as we cannot be sure the result is not stored anywhere
735                    after getting if.
736                  */
737
738                 /* some pointers, check if they have the same base buf constant offset */
739                 ir_alias_relation rel = different_pointer(adr1, mode1, adr2, mode2);
740                 if (rel != may_alias)
741                         return rel;
742         }
743
744
745         if (options & aa_opt_type_based) { /* Type based alias analysis */
746                 ir_alias_relation rel;
747
748                 if (options & aa_opt_byte_type_may_alias) {
749                         if (get_mode_size_bits(mode1) == 8 || get_mode_size_bits(mode2) == 8) {
750                                 /* One of the modes address a byte. Assume a may_alias and leave
751                                    the type based check. */
752                                 goto leave_type_based_alias;
753                         }
754                 }
755                 /* cheap check: If the mode sizes did not match, the types MUST be different */
756                 if (get_mode_size_bits(mode1) != get_mode_size_bits(mode2))
757                         return no_alias;
758
759                 /* cheap test: if only one is a reference mode, no alias */
760                 if (mode_is_reference(mode1) != mode_is_reference(mode2))
761                         return no_alias;
762
763                 /* try rule R5 */
764                 rel = different_types(adr1, adr2);
765                 if (rel != may_alias)
766                         return rel;
767 leave_type_based_alias:;
768         }
769
770         /* do we have a language specific memory disambiguator? */
771         if (language_disambuigator) {
772                 ir_alias_relation rel = (*language_disambuigator)(irg, adr1, mode1, adr2, mode2);
773                 if (rel != may_alias)
774                         return rel;
775         }
776
777         /* access points-to information here */
778         return may_alias;
779 }  /* _get_alias_relation */
780
781 /*
782  * Determine the alias relation between two addresses.
783  */
784 ir_alias_relation get_alias_relation(
785         ir_graph *irg,
786         ir_node *adr1, ir_mode *mode1,
787         ir_node *adr2, ir_mode *mode2)
788 {
789         ir_alias_relation rel = _get_alias_relation(irg, adr1, mode1, adr2, mode2);
790         DB((dbg, LEVEL_1, "alias(%+F, %+f) = %s\n", adr1, adr2, get_ir_alias_relation_name(rel)));
791         return rel;
792 }  /* get_alias_relation */
793
794 /* Set a source language specific memory disambiguator function. */
795 void set_language_memory_disambiguator(DISAMBIGUATOR_FUNC func) {
796         language_disambuigator = func;
797 }  /* set_language_memory_disambiguator */
798
799 /** The result cache for the memory disambiguator. */
800 static set *result_cache = NULL;
801
802 /** An entry in the relation cache. */
803 typedef struct mem_disambig_entry {
804         ir_node           *adr1;    /**< The first address. */
805         ir_node           *adr2;    /**< The second address. */
806         ir_alias_relation result;   /**< The alias relation result. */
807 } mem_disambig_entry;
808
809 #define HASH_ENTRY(adr1, adr2)  (HASH_PTR(adr1) ^ HASH_PTR(adr2))
810
811 /**
812  * Compare two relation cache entries.
813  */
814 static int cmp_mem_disambig_entry(const void *elt, const void *key, size_t size) {
815         const mem_disambig_entry *p1 = elt;
816         const mem_disambig_entry *p2 = key;
817         (void) size;
818
819         return p1->adr1 == p2->adr1 && p1->adr2 == p2->adr2;
820 }  /* cmp_mem_disambig_entry */
821
822 /**
823  * Initialize the relation cache.
824  */
825 void mem_disambig_init(void) {
826         result_cache = new_set(cmp_mem_disambig_entry, 8);
827 }  /* mem_disambig_init */
828
829 /*
830  * Determine the alias relation between two addresses.
831  */
832 ir_alias_relation get_alias_relation_ex(
833         ir_graph *irg,
834         ir_node *adr1, ir_mode *mode1,
835         ir_node *adr2, ir_mode *mode2)
836 {
837         mem_disambig_entry key, *entry;
838
839         if (! get_opt_alias_analysis())
840                 return may_alias;
841
842         if (get_irn_opcode(adr1) > get_irn_opcode(adr2)) {
843                 ir_node *t = adr1;
844                 adr1 = adr2;
845                 adr2 = t;
846         }
847
848         key.adr1 = adr1;
849         key.adr2 = adr2;
850         entry = set_find(result_cache, &key, sizeof(key), HASH_ENTRY(adr1, adr2));
851         if (entry)
852                 return entry->result;
853
854         key.result = get_alias_relation(irg, adr1, mode1, adr2, mode2);
855
856         set_insert(result_cache, &key, sizeof(key), HASH_ENTRY(adr1, adr2));
857         return key.result;
858 }  /* get_alias_relation_ex */
859
860 /* Free the relation cache. */
861 void mem_disambig_term(void) {
862         if (result_cache) {
863                 del_set(result_cache);
864                 result_cache = NULL;
865         }
866 }  /* mem_disambig_term */
867
868 /**
869  * Check the mode of a Load/Store with the mode of the entity
870  * that is accessed.
871  * If the mode of the entity and the Load/Store mode do not match, we
872  * have the bad reinterpret case:
873  *
874  * int i;
875  * char b = *(char *)&i;
876  *
877  * We do NOT count this as one value and return address_taken
878  * in that case.
879  * However, we support an often used case. If the mode is two-complement
880  * we allow casts between signed/unsigned.
881  *
882  * @param mode     the mode of the Load/Store
883  * @param ent_mode the mode of the accessed entity
884  *
885  * @return non-zero if the Load/Store is a hidden cast, zero else
886  */
887 static int is_hidden_cast(ir_mode *mode, ir_mode *ent_mode) {
888         if (ent_mode != mode) {
889                 if (ent_mode == NULL ||
890                         get_mode_size_bits(ent_mode) != get_mode_size_bits(mode) ||
891                         get_mode_sort(ent_mode) != get_mode_sort(mode) ||
892                         get_mode_arithmetic(ent_mode) != irma_twos_complement ||
893                         get_mode_arithmetic(mode) != irma_twos_complement)
894                         return 1;
895         }
896         return 0;
897 }  /* is_hidden_cast */
898
899 /**
900  * Determine the address_taken state of a node (or it's successor Sels).
901  *
902  * @param irn  the node
903  */
904 static ir_address_taken_state find_address_taken_state(ir_node *irn) {
905         int     i, j;
906         ir_mode *emode, *mode;
907         ir_node *value;
908         ir_entity *ent;
909
910         for (i = get_irn_n_outs(irn) - 1; i >= 0; --i) {
911                 ir_node *succ = get_irn_out(irn, i);
912
913                 switch (get_irn_opcode(succ)) {
914                 case iro_Load:
915                         /* check if this load is not a hidden conversion */
916                         mode = get_Load_mode(succ);
917                         ent = is_SymConst(irn) ? get_SymConst_entity(irn) : get_Sel_entity(irn);
918                         emode = get_type_mode(get_entity_type(ent));
919                         if (is_hidden_cast(mode, emode))
920                                 return ir_address_taken;
921                         break;
922
923                 case iro_Store:
924                         /* check that the node is not the Store's value */
925                         value = get_Store_value(succ);
926                         if (value == irn)
927                                 return ir_address_taken;
928                         /* check if this Store is not a hidden conversion */
929                         mode = get_irn_mode(value);
930                         ent = is_SymConst(irn) ? get_SymConst_entity(irn) : get_Sel_entity(irn);
931                         emode = get_type_mode(get_entity_type(ent));
932                         if (is_hidden_cast(mode, emode))
933                                 return ir_address_taken;
934                         break;
935
936                 case iro_Sel: {
937                         /* Check the successor of irn. */
938                         ir_address_taken_state res = find_address_taken_state(succ);
939                         if (res != ir_address_not_taken)
940                                 return res;
941                         break;
942                 }
943
944                 case iro_Call:
945                         /* Only the call address is not an address taker but
946                            this is an uninteresting case, so we ignore it here. */
947                         for (j = get_Call_n_params(succ) - 1; j >= 0; --j) {
948                                 ir_node *param = get_Call_param(succ, j);
949                                 if (param == irn)
950                                         return ir_address_taken;
951                         }
952                         break;
953
954                 default:
955                         /* another op, the address may be taken */
956                         return ir_address_taken_unknown;
957                 }
958         }
959         /* All successors finished, the address is not taken. */
960         return ir_address_not_taken;
961 }  /* find_address_taken_state */
962
963 /**
964  * Update the "address taken" flag of all frame entities.
965  */
966 static void analyse_irg_address_taken(ir_graph *irg) {
967         ir_type *ft = get_irg_frame_type(irg);
968         ir_node *irg_frame;
969         int i;
970
971         /* set initial state to not_taken, as this is the "smallest" state */
972         for (i = get_class_n_members(ft) - 1; i >= 0; --i) {
973                 ir_entity *ent = get_class_member(ft, i);
974
975                 set_entity_address_taken(ent, ir_address_not_taken);
976         }
977
978         assure_irg_outs(irg);
979
980         irg_frame = get_irg_frame(irg);
981
982         for (i = get_irn_n_outs(irg_frame) - 1; i >= 0; --i) {
983                 ir_node *succ = get_irn_out(irg_frame, i);
984                 ir_address_taken_state state;
985
986             if (is_Sel(succ)) {
987                         ir_entity *ent = get_Sel_entity(succ);
988
989                         if (get_entity_address_taken(ent) == ir_address_taken)
990                                 continue;
991
992                         state = find_address_taken_state(succ);
993                         if (state > get_entity_address_taken(ent))
994                                 set_entity_address_taken(ent, state);
995                 }
996         }
997         /* now computed */
998         irg->adr_taken_state = ir_address_taken_computed;
999 }  /* analyse_address_taken */
1000
1001 /* Returns the current address taken state of the graph. */
1002 ir_address_taken_computed_state get_irg_address_taken_state(const ir_graph *irg) {
1003         return irg->adr_taken_state;
1004 }  /* get_irg_address_taken_state */
1005
1006 /* Sets the current address taken state of the graph. */
1007 void set_irg_address_taken_state(ir_graph *irg, ir_address_taken_computed_state state) {
1008         irg->adr_taken_state = state;
1009 }  /* set_irg_address_taken_state */
1010
1011 /* Assure that the address taken flag is computed for the given graph. */
1012 void assure_irg_address_taken_computed(ir_graph *irg) {
1013         if (irg->adr_taken_state == ir_address_taken_not_computed)
1014                 analyse_irg_address_taken(irg);
1015 }  /* assure_irg_address_taken_computed */
1016
1017
1018 /**
1019  * Initialize the address_taken flag for a global type like type.
1020  */
1021 static void init_taken_flag(ir_type * tp) {
1022         int i;
1023
1024         /* All external visible entities are at least
1025            ir_address_taken_unknown. This is very conservative. */
1026         for (i = get_compound_n_members(tp) - 1; i >= 0; --i) {
1027                 ir_entity *ent = get_compound_member(tp, i);
1028                 ir_address_taken_state state;
1029
1030                 state = get_entity_visibility(ent) == visibility_external_visible ?
1031                                 ir_address_taken_unknown : ir_address_not_taken ;
1032                 set_entity_address_taken(ent, state);
1033         }
1034 }  /* init_taken_flag */
1035
1036 static void check_initializer_nodes(ir_initializer_t *initializer)
1037 {
1038         switch(initializer->kind) {
1039         case IR_INITIALIZER_CONST: {
1040                 ir_node *n = initializer->consti.value;
1041
1042                 /* let's check if it's an address */
1043                 if (is_SymConst_addr_ent(n)) {
1044                         ir_entity *ent = get_SymConst_entity(n);
1045                         set_entity_address_taken(ent, ir_address_taken);
1046                 }
1047                 return;
1048         }
1049         case IR_INITIALIZER_TARVAL:
1050         case IR_INITIALIZER_NULL:
1051                 return;
1052         case IR_INITIALIZER_COMPOUND: {
1053                 size_t i;
1054
1055                 for(i = 0; i < initializer->compound.n_initializers; ++i) {
1056                         ir_initializer_t *sub_initializer
1057                                 = initializer->compound.initializers[i];
1058                         check_initializer_nodes(sub_initializer);
1059                 }
1060                 return;
1061         }
1062         }
1063         panic("invalid initialzier found");
1064 }
1065
1066 /**
1067  * Mark all entities used in the initializer for the given entity as address taken
1068  */
1069 static void check_initializer(ir_entity *ent) {
1070         ir_node *n;
1071         int i;
1072
1073         /* do not check uninitialized values */
1074         if (get_entity_variability(ent) == variability_uninitialized)
1075                 return;
1076
1077         /* Beware: Methods initialized with "themself". This does not count as a taken
1078            address. */
1079         if (is_Method_type(get_entity_type(ent)))
1080                 return;
1081
1082         if (ent->has_initializer) {
1083                 check_initializer_nodes(ent->attr.initializer);
1084         } else if (is_atomic_entity(ent)) {
1085                 /* let's check if it's an address */
1086                 n = get_atomic_ent_value(ent);
1087                 if (is_SymConst_addr_ent(n)) {
1088                         ir_entity *ent = get_SymConst_entity(n);
1089                         set_entity_address_taken(ent, ir_address_taken);
1090                 }
1091         } else {
1092                 for (i = get_compound_ent_n_values(ent) - 1; i >= 0; --i) {
1093                         n = get_compound_ent_value(ent, i);
1094
1095                         /* let's check if it's an address */
1096                         if (is_SymConst_addr_ent(n)) {
1097                                 ir_entity *ent = get_SymConst_entity(n);
1098                                 set_entity_address_taken(ent, ir_address_taken);
1099                         }
1100                 }
1101         }
1102 }  /* check_initializer */
1103
1104
1105 /**
1106  * Mark all entities used in initializers as address taken
1107  */
1108 static void check_initializers(ir_type *tp) {
1109         int i;
1110
1111         for (i = get_compound_n_members(tp) - 1; i >= 0; --i) {
1112                 ir_entity *ent = get_compound_member(tp, i);
1113
1114                 check_initializer(ent);
1115         }
1116 }  /* check_initializers */
1117
1118 #ifdef DEBUG_libfirm
1119 /**
1120  * Print the address taken state of all entities of a given type for debugging.
1121  */
1122 static void print_address_taken_state(ir_type *tp) {
1123         int i;
1124         for (i = get_compound_n_members(tp) - 1; i >= 0; --i) {
1125                 ir_entity *ent = get_compound_member(tp, i);
1126                 ir_address_taken_state state = get_entity_address_taken(ent);
1127
1128                 if (state != ir_address_not_taken) {
1129                         assert(ir_address_not_taken <= (int) state && state <= ir_address_taken);
1130                         ir_printf("%+F: %s\n", ent, get_address_taken_state_name(state));
1131                 }
1132         }
1133 }  /* print_address_taken_state */
1134 #endif /* DEBUG_libfirm */
1135
1136 /**
1137  * Post-walker: check for global entity address
1138  */
1139 static void check_global_address(ir_node *irn, void *env) {
1140         ir_node *tls = env;
1141         ir_entity *ent;
1142         ir_address_taken_state state;
1143
1144         if (is_SymConst_addr_ent(irn)) {
1145                 /* A global. */
1146                 ent = get_SymConst_entity(irn);
1147         } else if (is_Sel(irn) && get_Sel_ptr(irn) == tls) {
1148                 /* A TLS variable. */
1149                 ent = get_Sel_entity(irn);
1150         } else
1151                 return;
1152
1153         if (get_entity_address_taken(ent) >= ir_address_taken) {
1154                 /* Already at the maximum. */
1155                 return;
1156         }
1157         state = find_address_taken_state(irn);
1158         if (state > get_entity_address_taken(ent))
1159                 set_entity_address_taken(ent, state);
1160 }  /* check_global_address */
1161
1162 /**
1163  * Update the "address taken" flag of all global entities.
1164  */
1165 static void analyse_irp_globals_address_taken(void) {
1166         int i;
1167
1168         FIRM_DBG_REGISTER(dbg, "firm.ana.irmemory");
1169
1170         init_taken_flag(get_glob_type());
1171         init_taken_flag(get_tls_type());
1172
1173         check_initializers(get_glob_type());
1174         check_initializers(get_tls_type());
1175
1176         for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
1177                 ir_graph *irg = get_irp_irg(i);
1178
1179                 assure_irg_outs(irg);
1180                 irg_walk_graph(irg, NULL, check_global_address, get_irg_tls(irg));
1181         }
1182
1183 #ifdef DEBUG_libfirm
1184         if (firm_dbg_get_mask(dbg) & LEVEL_1) {
1185                 print_address_taken_state(get_glob_type());
1186                 print_address_taken_state(get_tls_type());
1187         }
1188 #endif /* DEBUG_libfirm */
1189
1190         /* now computed */
1191         irp->globals_adr_taken_state = ir_address_taken_computed;
1192 }  /* analyse_irp_globals_address_taken */
1193
1194 /* Returns the current address taken state of the globals. */
1195 ir_address_taken_computed_state get_irp_globals_address_taken_state(void) {
1196         return irp->globals_adr_taken_state;
1197 }  /* get_irp_globals_address_taken_state */
1198
1199 /* Sets the current address taken state of the graph. */
1200 void set_irp_globals_address_taken_state(ir_address_taken_computed_state state) {
1201         irp->globals_adr_taken_state = state;
1202 }  /* set_irg_address_taken_state */
1203
1204 /* Assure that the address taken flag is computed for the globals. */
1205 void assure_irp_globals_address_taken_computed(void) {
1206         if (irp->globals_adr_taken_state == ir_address_taken_not_computed)
1207                 analyse_irp_globals_address_taken();
1208 }  /* assure_irp_globals_address_taken_computed */
1209
1210
1211 #include <adt/pmap.h>
1212 #include "typerep.h"
1213
1214 DEBUG_ONLY(static firm_dbg_module_t *dbgcall = NULL;)
1215
1216 /** Maps method types to cloned method types. */
1217 static pmap *mtp_map;
1218
1219 /**
1220  * Clone a method type if not already cloned.
1221  */
1222 static ir_type *clone_type_and_cache(ir_type *tp) {
1223         static ident *prefix = NULL;
1224         ir_type *res;
1225         pmap_entry *e = pmap_find(mtp_map, tp);
1226
1227         if (e)
1228                 return e->value;
1229
1230         if (prefix == NULL)
1231                 prefix = new_id_from_chars("C", 1);
1232
1233         res = clone_type_method(tp, prefix);
1234         pmap_insert(mtp_map, tp, res);
1235         DB((dbgcall, LEVEL_2, "cloned type %+F into %+F\n", tp, res));
1236
1237         return res;
1238 }  /* clone_type_and_cache */
1239
1240 /**
1241  * Copy the calling conventions from the entities to the call type.
1242  */
1243 static void update_calls_to_private(ir_node *call, void *env) {
1244         (void) env;
1245         if (is_Call(call)) {
1246                 ir_node *ptr = get_Call_ptr(call);
1247
1248                 if (is_SymConst(ptr)) {
1249                         ir_entity *ent = get_SymConst_entity(ptr);
1250                         ir_type *ctp = get_Call_type(call);
1251
1252                         if (get_entity_additional_properties(ent) & mtp_property_private) {
1253                                 if ((get_method_additional_properties(ctp) & mtp_property_private) == 0) {
1254                                         ctp = clone_type_and_cache(ctp);
1255                                         set_method_additional_property(ctp, mtp_property_private);
1256                                         set_Call_type(call, ctp);
1257                                         DB((dbgcall, LEVEL_1, "changed call to private method %+F\n", ent));
1258                                 }
1259                         }
1260                 }
1261         }
1262 }  /* update_calls_to_private */
1263
1264 /* Mark all private methods, i.e. those of which all call sites are known. */
1265 void mark_private_methods(void) {
1266         int i;
1267         int changed = 0;
1268
1269         FIRM_DBG_REGISTER(dbgcall, "firm.opt.cc");
1270
1271         assure_irp_globals_address_taken_computed();
1272
1273         mtp_map = pmap_create();
1274
1275         /* first step: change the calling conventions of the local non-escaped entities */
1276         for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
1277                 ir_graph               *irg = get_irp_irg(i);
1278                 ir_entity              *ent = get_irg_entity(irg);
1279                 ir_address_taken_state state = get_entity_address_taken(ent);
1280
1281                 if (get_entity_visibility(ent) == visibility_local &&
1282                     state == ir_address_not_taken) {
1283                         ir_type *mtp = get_entity_type(ent);
1284
1285                         set_entity_additional_property(ent, mtp_property_private);
1286                         DB((dbgcall, LEVEL_1, "found private method %+F\n", ent));
1287                         if ((get_method_additional_properties(mtp) & mtp_property_private) == 0) {
1288                                 /* need a new type */
1289                                 mtp = clone_type_and_cache(mtp);
1290                                 set_entity_type(ent, mtp);
1291                                 set_method_additional_property(mtp, mtp_property_private);
1292                                 changed = 1;
1293                         }
1294                 }
1295         }
1296
1297         if (changed)
1298                 all_irg_walk(NULL, update_calls_to_private, NULL);
1299
1300         pmap_destroy(mtp_map);
1301 }  /* mark_private_methods */