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