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