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