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