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