used new is_Global() and get_Global_entity() instead of SymConst calls
[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(no_alias);
60         X(may_alias);
61         X(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 no_alias if the Const is greater, 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) ? may_alias : 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) ? no_alias : 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 sure_alias iff idx1 == idx2
123  *         no_alias iff they ALWAYS differ more than size
124  *         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 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 ? sure_alias : 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 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 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 no_alias;
182                                         }
183                                         /* tv_size > tv2, so we can subtract without overflow */
184                                         tv2 = tarval_sub(tv_size, tv2);
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) ? no_alias : 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);
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) ? no_alias : 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 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 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 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 no_alias;
374                         } else {
375                                 /* different types */
376                                 return no_alias;
377                         }
378                 }
379         }
380         return may_alias;
381 }  /* different_types */
382
383 /**
384  * Returns non-zero if a node is a routine parameter.
385  *
386  * @param node  the Proj node to test
387  */
388 static int is_arg_Proj(ir_node *node) {
389         if (! is_Proj(node))
390                 return 0;
391         node = get_Proj_pred(node);
392         if (! is_Proj(node))
393                 return 0;
394         return pn_Start_T_args == get_Proj_proj(node) && is_Start(get_Proj_pred(node));
395 }  /* is_arg_Proj */
396
397 /**
398  * Returns non-zero if a node is a result on a malloc-like routine.
399  *
400  * @param node  the Proj node to test
401  */
402 static int is_malloc_Result(ir_node *node) {
403         node = get_Proj_pred(node);
404         if (! is_Proj(node))
405                 return 0;
406         node = get_Proj_pred(node);
407         if (! is_Call(node))
408                 return 0;
409         node = get_Call_ptr(node);
410         if (is_Global(node)) {
411                 ir_entity *ent = get_Global_entity(node);
412
413                 if (get_entity_additional_properties(ent) & mtp_property_malloc)
414                         return 1;
415                 return 0;
416         }
417         return 0;
418 }  /* is_malloc_Result */
419
420 /**
421  * classify storage locations.
422  * Except STORAGE_CLASS_POINTER they are all disjoint.
423  * STORAGE_CLASS_POINTER potentially aliases all classes which don't have a
424  * NOTTAKEN modifier.
425  */
426 typedef enum {
427         STORAGE_CLASS_POINTER           = 0x0000,
428         STORAGE_CLASS_GLOBALVAR         = 0x0001,
429         STORAGE_CLASS_LOCALVAR          = 0x0002,
430         STORAGE_CLASS_ARGUMENT          = 0x0003,
431         STORAGE_CLASS_TLS               = 0x0004,
432         STORAGE_CLASS_MALLOCED          = 0x0005,
433
434         STORAGE_CLASS_MODIFIER_NOTTAKEN = 0x1000,
435 } storage_class_class_t;
436
437 /**
438  * Classify a base pointer.
439  *
440  * @param irg  the graph of the pointer
441  * @param irn  the node representing the base address
442  * @param ent  the base entity of the base address iff any
443  */
444 static storage_class_class_t classify_pointer(ir_graph *irg, ir_node *irn, ir_entity *ent)
445 {
446         storage_class_class_t res = STORAGE_CLASS_POINTER;
447         if (is_Global(irn)) {
448                 ir_entity *entity = get_Global_entity(irn);
449                 res = STORAGE_CLASS_GLOBALVAR;
450                 if (get_entity_address_taken(entity) == ir_address_not_taken)
451                         res |= STORAGE_CLASS_MODIFIER_NOTTAKEN;
452         } else if (irn == get_irg_frame(irg)) {
453                 res = STORAGE_CLASS_LOCALVAR;
454                 if (ent != NULL && get_entity_address_taken(ent) == ir_address_not_taken)
455                         res |= STORAGE_CLASS_MODIFIER_NOTTAKEN;
456         } else if (is_arg_Proj(irn)) {
457                 return STORAGE_CLASS_ARGUMENT;
458         } else if (irn == get_irg_tls(irg)) {
459                 res = STORAGE_CLASS_TLS;
460                 if (ent != NULL && get_entity_address_taken(ent) == ir_address_not_taken)
461                         res |= STORAGE_CLASS_MODIFIER_NOTTAKEN;
462         } else if (is_Proj(irn) && is_malloc_Result(irn)) {
463                 return STORAGE_CLASS_MALLOCED;
464         }
465
466         return res;
467 }
468
469 /**
470  * If adr represents a Bitfield Sel, skip it
471  */
472 static ir_node *skip_Bitfield_Sels(ir_node *adr) {
473         if (is_Sel(adr)) {
474                 ir_entity *ent     = get_Sel_entity(adr);
475                 ir_type   *bf_type = get_entity_type(ent);
476
477                 /* is it a bitfield type? */
478                 if (is_Primitive_type(bf_type) && get_primitive_base_type(bf_type) != NULL)
479                         adr = get_Sel_ptr(adr);
480         }
481         return adr;
482 }
483
484 /**
485  * Determine the alias relation between two addresses.
486  */
487 static ir_alias_relation _get_alias_relation(
488         ir_graph *irg,
489         ir_node *adr1, ir_mode *mode1,
490         ir_node *adr2, ir_mode *mode2)
491 {
492         ir_entity             *ent1, *ent2;
493         unsigned              options;
494         long                  offset1 = 0;
495         long                  offset2 = 0;
496         ir_node               *base1;
497         ir_node               *base2;
498         ir_node               *orig_adr1 = adr1;
499         ir_node               *orig_adr2 = adr2;
500         unsigned              mode_size;
501         storage_class_class_t class1, class2;
502         int                   have_const_offsets;
503
504         if (! get_opt_alias_analysis())
505                 return may_alias;
506
507         if (adr1 == adr2)
508                 return sure_alias;
509
510         options = get_irg_memory_disambiguator_options(irg);
511
512         /* The Armageddon switch */
513         if (options & aa_opt_no_alias)
514                 return no_alias;
515
516         /* do the addresses have constants offsets?
517          *  Note: nodes are normalized to have constants at right inputs,
518          *        sub X, C is normalized to add X, -C
519          */
520         have_const_offsets = 1;
521         while (is_Add(adr1)) {
522                 ir_node *add_right = get_Add_right(adr1);
523                 if (is_Const(add_right)) {
524                         tarval *tv  = get_Const_tarval(add_right);
525                         offset1    += get_tarval_long(tv);
526                         adr1        = get_Add_left(adr1);
527                 } else if (mode_is_reference(get_irn_mode(add_right))) {
528                         adr1 = add_right;
529                         have_const_offsets = 0;
530                 } else {
531                         adr1 = get_Add_left(adr1);
532                         have_const_offsets = 0;
533                 }
534         }
535         while (is_Add(adr2)) {
536                 ir_node *add_right = get_Add_right(adr2);
537                 if (is_Const(add_right)) {
538                         tarval *tv  = get_Const_tarval(add_right);
539                         offset2    += get_tarval_long(tv);
540                         adr2        = get_Add_left(adr2);
541                 } else if (mode_is_reference(get_irn_mode(add_right))) {
542                         adr2 = add_right;
543                         have_const_offsets = 0;
544                 } else {
545                         adr2 = get_Add_left(adr2);
546                         have_const_offsets = 0;
547                 }
548         }
549
550         mode_size = get_mode_size_bytes(mode1);
551         if (get_mode_size_bytes(mode2) > mode_size) {
552                 mode_size = get_mode_size_bytes(mode2);
553         }
554
555         /* same base address -> compare offsets if possible.
556          * FIXME: type long is not sufficient for this task ...
557          */
558         if (adr1 == adr2 && have_const_offsets) {
559                 if ((unsigned long)labs(offset2 - offset1) >= mode_size)
560                         return no_alias;
561                 else
562                         return sure_alias;
563         }
564
565         /*
566          * Bitfields can be constructed as Sels from its base address.
567          * As they have different entities, the disambiguator would find that they are
568          * alias free. While this is true vor it's values, it is false for the addresses
569          * (strictly speaking, the Sel's are NOT the addresses of the bitfields).
570          * So, skip those bitfield selecting Sel's.
571          */
572         adr1 = skip_Bitfield_Sels(adr1);
573         adr2 = skip_Bitfield_Sels(adr2);
574
575         /* skip sels */
576         base1 = adr1;
577         base2 = adr2;
578         ent1  = NULL;
579         ent2  = NULL;
580         if (is_Sel(adr1)) {
581                 base1 = find_base_adr(adr1, &ent1);
582         }
583         if (is_Sel(adr2)) {
584                 base2 = find_base_adr(adr2, &ent2);
585         }
586
587         /* same base address -> compare sel entities */
588         if (base1 == base2 && ent1 != NULL && ent2 != NULL) {
589                 if (ent1 != ent2)
590                         return no_alias;
591                 else if (have_const_offsets)
592                         return different_sel_offsets(adr1, adr2);
593         }
594
595         class1 = classify_pointer(irg, base1, ent1);
596         class2 = classify_pointer(irg, base2, ent2);
597
598         if (class1 == STORAGE_CLASS_POINTER) {
599                 if (class2 & STORAGE_CLASS_MODIFIER_NOTTAKEN) {
600                         return no_alias;
601                 } else {
602                         return may_alias;
603                 }
604         } else if (class2 == STORAGE_CLASS_POINTER) {
605                 if (class1 & STORAGE_CLASS_MODIFIER_NOTTAKEN) {
606                         return no_alias;
607                 } else {
608                         return may_alias;
609                 }
610         }
611
612         if (class1 != class2) {
613                 return no_alias;
614         }
615
616         if (class1 == STORAGE_CLASS_GLOBALVAR) {
617                 ir_entity *entity1 = get_SymConst_entity(base1);
618                 ir_entity *entity2 = get_SymConst_entity(base2);
619                 if (entity1 != entity2)
620                         return no_alias;
621
622                 /* for some reason CSE didn't happen yet for the 2 SymConsts... */
623                 return may_alias;
624         }
625
626         /* Type based alias analysis */
627         if (options & aa_opt_type_based) {
628                 ir_alias_relation rel;
629
630                 if (options & aa_opt_byte_type_may_alias) {
631                         if (get_mode_size_bits(mode1) == 8 || get_mode_size_bits(mode2) == 8) {
632                                 /* One of the modes address a byte. Assume a may_alias and leave
633                                    the type based check. */
634                                 goto leave_type_based_alias;
635                         }
636                 }
637                 /* cheap check: If the mode sizes did not match, the types MUST be different */
638                 if (get_mode_size_bits(mode1) != get_mode_size_bits(mode2))
639                         return no_alias;
640
641                 /* cheap test: if only one is a reference mode, no alias */
642                 if (mode_is_reference(mode1) != mode_is_reference(mode2))
643                         return no_alias;
644
645                 /* try rule R5 */
646                 rel = different_types(orig_adr1, orig_adr2);
647                 if (rel != may_alias)
648                         return rel;
649 leave_type_based_alias:;
650         }
651
652         /* do we have a language specific memory disambiguator? */
653         if (language_disambuigator) {
654                 ir_alias_relation rel = (*language_disambuigator)(irg, orig_adr1, mode1, orig_adr2, mode2);
655                 if (rel != may_alias)
656                         return rel;
657         }
658
659         /* access points-to information here */
660         return may_alias;
661 }  /* _get_alias_relation */
662
663 /*
664  * Determine the alias relation between two addresses.
665  */
666 ir_alias_relation get_alias_relation(
667         ir_graph *irg,
668         ir_node *adr1, ir_mode *mode1,
669         ir_node *adr2, ir_mode *mode2)
670 {
671         ir_alias_relation rel = _get_alias_relation(irg, adr1, mode1, adr2, mode2);
672         DB((dbg, LEVEL_1, "alias(%+F, %+F) = %s\n", adr1, adr2, get_ir_alias_relation_name(rel)));
673         return rel;
674 }  /* get_alias_relation */
675
676 /* Set a source language specific memory disambiguator function. */
677 void set_language_memory_disambiguator(DISAMBIGUATOR_FUNC func) {
678         language_disambuigator = func;
679 }  /* set_language_memory_disambiguator */
680
681 /** The result cache for the memory disambiguator. */
682 static set *result_cache = NULL;
683
684 /** An entry in the relation cache. */
685 typedef struct mem_disambig_entry {
686         ir_node           *adr1;    /**< The first address. */
687         ir_node           *adr2;    /**< The second address. */
688         ir_alias_relation result;   /**< The alias relation result. */
689 } mem_disambig_entry;
690
691 #define HASH_ENTRY(adr1, adr2)  (HASH_PTR(adr1) ^ HASH_PTR(adr2))
692
693 /**
694  * Compare two relation cache entries.
695  */
696 static int cmp_mem_disambig_entry(const void *elt, const void *key, size_t size) {
697         const mem_disambig_entry *p1 = elt;
698         const mem_disambig_entry *p2 = key;
699         (void) size;
700
701         return p1->adr1 == p2->adr1 && p1->adr2 == p2->adr2;
702 }  /* cmp_mem_disambig_entry */
703
704 /**
705  * Initialize the relation cache.
706  */
707 void mem_disambig_init(void) {
708         result_cache = new_set(cmp_mem_disambig_entry, 8);
709 }  /* mem_disambig_init */
710
711 /*
712  * Determine the alias relation between two addresses.
713  */
714 ir_alias_relation get_alias_relation_ex(
715         ir_graph *irg,
716         ir_node *adr1, ir_mode *mode1,
717         ir_node *adr2, ir_mode *mode2)
718 {
719         mem_disambig_entry key, *entry;
720
721         ir_fprintf(stderr, "%+F <-> %+F\n", adr1, adr2);
722
723         if (! get_opt_alias_analysis())
724                 return may_alias;
725
726         if (get_irn_opcode(adr1) > get_irn_opcode(adr2)) {
727                 ir_node *t = adr1;
728                 adr1 = adr2;
729                 adr2 = t;
730         }
731
732         key.adr1 = adr1;
733         key.adr2 = adr2;
734         entry = set_find(result_cache, &key, sizeof(key), HASH_ENTRY(adr1, adr2));
735         if (entry)
736                 return entry->result;
737
738         key.result = get_alias_relation(irg, adr1, mode1, adr2, mode2);
739
740         set_insert(result_cache, &key, sizeof(key), HASH_ENTRY(adr1, adr2));
741         return key.result;
742 }  /* get_alias_relation_ex */
743
744 /* Free the relation cache. */
745 void mem_disambig_term(void) {
746         if (result_cache) {
747                 del_set(result_cache);
748                 result_cache = NULL;
749         }
750 }  /* mem_disambig_term */
751
752 /**
753  * Check the mode of a Load/Store with the mode of the entity
754  * that is accessed.
755  * If the mode of the entity and the Load/Store mode do not match, we
756  * have the bad reinterpret case:
757  *
758  * int i;
759  * char b = *(char *)&i;
760  *
761  * We do NOT count this as one value and return address_taken
762  * in that case.
763  * However, we support an often used case. If the mode is two-complement
764  * we allow casts between signed/unsigned.
765  *
766  * @param mode     the mode of the Load/Store
767  * @param ent_mode the mode of the accessed entity
768  *
769  * @return non-zero if the Load/Store is a hidden cast, zero else
770  */
771 static int is_hidden_cast(ir_mode *mode, ir_mode *ent_mode) {
772         if (ent_mode != mode) {
773                 if (ent_mode == NULL ||
774                         get_mode_size_bits(ent_mode) != get_mode_size_bits(mode) ||
775                         get_mode_sort(ent_mode) != get_mode_sort(mode) ||
776                         get_mode_arithmetic(ent_mode) != irma_twos_complement ||
777                         get_mode_arithmetic(mode) != irma_twos_complement)
778                         return 1;
779         }
780         return 0;
781 }  /* is_hidden_cast */
782
783 /**
784  * Determine the address_taken state of a node (or it's successor Sels).
785  *
786  * @param irn  the node
787  */
788 static ir_address_taken_state find_address_taken_state(ir_node *irn) {
789         int       i, j;
790         ir_mode   *emode, *mode;
791         ir_node   *value;
792         ir_entity *ent;
793         ir_type   *tp;
794
795         for (i = get_irn_n_outs(irn) - 1; i >= 0; --i) {
796                 ir_node *succ = get_irn_out(irn, i);
797
798                 switch (get_irn_opcode(succ)) {
799                 case iro_Load:
800                         /* check if this load is not a hidden conversion */
801                         mode = get_Load_mode(succ);
802                         ent = is_SymConst(irn) ? get_SymConst_entity(irn) : get_Sel_entity(irn);
803                         emode = get_type_mode(get_entity_type(ent));
804                         if (is_hidden_cast(mode, emode))
805                                 return ir_address_taken;
806                         break;
807
808                 case iro_Store:
809                         /* check that the node is not the Store's value */
810                         value = get_Store_value(succ);
811                         if (value == irn)
812                                 return ir_address_taken;
813                         /* check if this Store is not a hidden conversion */
814                         mode = get_irn_mode(value);
815                         ent = is_SymConst(irn) ? get_SymConst_entity(irn) : get_Sel_entity(irn);
816                         emode = get_type_mode(get_entity_type(ent));
817                         if (is_hidden_cast(mode, emode))
818                                 return ir_address_taken;
819                         break;
820
821                 case iro_CopyB:
822                         /* CopyB are like Loads/Stores */
823                         ent = is_SymConst(irn) ? get_SymConst_entity(irn) : get_Sel_entity(irn);
824                         tp  = get_entity_type(ent);
825                         if (tp != get_CopyB_type(succ)) {
826                                 /* bad, different types, might be a hidden conversion */
827                                 return ir_address_taken;
828                         }
829                         break;
830
831                 case iro_Sel: {
832                         /* Check the successor of irn. */
833                         ir_address_taken_state res = find_address_taken_state(succ);
834                         if (res != ir_address_not_taken)
835                                 return res;
836                         break;
837                 }
838
839                 case iro_Call:
840                         /* Only the call address is not an address taker but
841                            this is an uninteresting case, so we ignore it here. */
842                         for (j = get_Call_n_params(succ) - 1; j >= 0; --j) {
843                                 ir_node *param = get_Call_param(succ, j);
844                                 if (param == irn)
845                                         return ir_address_taken;
846                         }
847                         break;
848
849                 default:
850                         /* another op, the address may be taken */
851                         return ir_address_taken_unknown;
852                 }
853         }
854         /* All successors finished, the address is not taken. */
855         return ir_address_not_taken;
856 }  /* find_address_taken_state */
857
858 /**
859  * Update the "address taken" flag of all frame entities.
860  */
861 static void analyse_irg_address_taken(ir_graph *irg) {
862         ir_type *ft = get_irg_frame_type(irg);
863         ir_node *irg_frame;
864         int i;
865
866         /* set initial state to not_taken, as this is the "smallest" state */
867         for (i = get_class_n_members(ft) - 1; i >= 0; --i) {
868                 ir_entity *ent = get_class_member(ft, i);
869
870                 set_entity_address_taken(ent, ir_address_not_taken);
871         }
872
873         assure_irg_outs(irg);
874
875         irg_frame = get_irg_frame(irg);
876
877         for (i = get_irn_n_outs(irg_frame) - 1; i >= 0; --i) {
878                 ir_node *succ = get_irn_out(irg_frame, i);
879                 ir_address_taken_state state;
880
881             if (is_Sel(succ)) {
882                         ir_entity *ent = get_Sel_entity(succ);
883
884                         if (get_entity_address_taken(ent) == ir_address_taken)
885                                 continue;
886
887                         state = find_address_taken_state(succ);
888                         if (state > get_entity_address_taken(ent))
889                                 set_entity_address_taken(ent, state);
890                 }
891         }
892         /* now computed */
893         irg->adr_taken_state = ir_address_taken_computed;
894 }  /* analyse_address_taken */
895
896 /* Returns the current address taken state of the graph. */
897 ir_address_taken_computed_state get_irg_address_taken_state(const ir_graph *irg) {
898         return irg->adr_taken_state;
899 }  /* get_irg_address_taken_state */
900
901 /* Sets the current address taken state of the graph. */
902 void set_irg_address_taken_state(ir_graph *irg, ir_address_taken_computed_state state) {
903         irg->adr_taken_state = state;
904 }  /* set_irg_address_taken_state */
905
906 /* Assure that the address taken flag is computed for the given graph. */
907 void assure_irg_address_taken_computed(ir_graph *irg) {
908         if (irg->adr_taken_state == ir_address_taken_not_computed)
909                 analyse_irg_address_taken(irg);
910 }  /* assure_irg_address_taken_computed */
911
912
913 /**
914  * Initialize the address_taken flag for a global type like type.
915  */
916 static void init_taken_flag(ir_type * tp) {
917         int i;
918
919         /* All external visible entities are at least
920            ir_address_taken_unknown. This is very conservative. */
921         for (i = get_compound_n_members(tp) - 1; i >= 0; --i) {
922                 ir_entity *ent = get_compound_member(tp, i);
923                 ir_address_taken_state state;
924
925                 state = get_entity_visibility(ent) == visibility_external_visible ?
926                                 ir_address_taken_unknown : ir_address_not_taken ;
927                 set_entity_address_taken(ent, state);
928         }
929 }  /* init_taken_flag */
930
931 static void check_initializer_nodes(ir_initializer_t *initializer)
932 {
933         switch (initializer->kind) {
934         case IR_INITIALIZER_CONST: {
935                 ir_node *n = initializer->consti.value;
936
937                 /* let's check if it's an address */
938                 if (is_Global(n)) {
939                         ir_entity *ent = get_Global_entity(n);
940                         set_entity_address_taken(ent, ir_address_taken);
941                 }
942                 return;
943         }
944         case IR_INITIALIZER_TARVAL:
945         case IR_INITIALIZER_NULL:
946                 return;
947         case IR_INITIALIZER_COMPOUND: {
948                 size_t i;
949
950                 for (i = 0; i < initializer->compound.n_initializers; ++i) {
951                         ir_initializer_t *sub_initializer
952                                 = initializer->compound.initializers[i];
953                         check_initializer_nodes(sub_initializer);
954                 }
955                 return;
956         }
957         }
958         panic("invalid initialzier found");
959 }
960
961 /**
962  * Mark all entities used in the initializer for the given entity as address taken
963  */
964 static void check_initializer(ir_entity *ent) {
965         ir_node *n;
966         int i;
967
968         /* do not check uninitialized values */
969         if (get_entity_variability(ent) == variability_uninitialized)
970                 return;
971
972         /* Beware: Methods initialized with "themself". This does not count as a taken
973            address. */
974         if (is_Method_type(get_entity_type(ent)))
975                 return;
976
977         if (ent->has_initializer) {
978                 check_initializer_nodes(ent->attr.initializer);
979         } else if (is_atomic_entity(ent)) {
980                 /* let's check if it's an address */
981                 n = get_atomic_ent_value(ent);
982                 if (is_Global(n)) {
983                         ir_entity *ent = get_Global_entity(n);
984                         set_entity_address_taken(ent, ir_address_taken);
985                 }
986         } else {
987                 for (i = get_compound_ent_n_values(ent) - 1; i >= 0; --i) {
988                         n = get_compound_ent_value(ent, i);
989
990                         /* let's check if it's an address */
991                         if (is_Global(n)) {
992                                 ir_entity *ent = get_Global_entity(n);
993                                 set_entity_address_taken(ent, ir_address_taken);
994                         }
995                 }
996         }
997 }  /* check_initializer */
998
999
1000 /**
1001  * Mark all entities used in initializers as address taken
1002  */
1003 static void check_initializers(ir_type *tp) {
1004         int i;
1005
1006         for (i = get_compound_n_members(tp) - 1; i >= 0; --i) {
1007                 ir_entity *ent = get_compound_member(tp, i);
1008
1009                 check_initializer(ent);
1010         }
1011 }  /* check_initializers */
1012
1013 #ifdef DEBUG_libfirm
1014 /**
1015  * Print the address taken state of all entities of a given type for debugging.
1016  */
1017 static void print_address_taken_state(ir_type *tp) {
1018         int i;
1019         for (i = get_compound_n_members(tp) - 1; i >= 0; --i) {
1020                 ir_entity *ent = get_compound_member(tp, i);
1021                 ir_address_taken_state state = get_entity_address_taken(ent);
1022
1023                 if (state != ir_address_not_taken) {
1024                         assert(ir_address_not_taken <= (int) state && state <= ir_address_taken);
1025                         ir_printf("%+F: %s\n", ent, get_address_taken_state_name(state));
1026                 }
1027         }
1028 }  /* print_address_taken_state */
1029 #endif /* DEBUG_libfirm */
1030
1031 /**
1032  * Post-walker: check for global entity address
1033  */
1034 static void check_global_address(ir_node *irn, void *env) {
1035         ir_node *tls = env;
1036         ir_entity *ent;
1037         ir_address_taken_state state;
1038
1039         if (is_Global(irn)) {
1040                 /* A global. */
1041                 ent = get_Global_entity(irn);
1042         } else if (is_Sel(irn) && get_Sel_ptr(irn) == tls) {
1043                 /* A TLS variable. */
1044                 ent = get_Sel_entity(irn);
1045         } else
1046                 return;
1047
1048         if (get_entity_address_taken(ent) >= ir_address_taken) {
1049                 /* Already at the maximum. */
1050                 return;
1051         }
1052         state = find_address_taken_state(irn);
1053         if (state > get_entity_address_taken(ent))
1054                 set_entity_address_taken(ent, state);
1055 }  /* check_global_address */
1056
1057 /**
1058  * Update the "address taken" flag of all global entities.
1059  */
1060 static void analyse_irp_globals_address_taken(void) {
1061         int i;
1062
1063         FIRM_DBG_REGISTER(dbg, "firm.ana.irmemory");
1064
1065         init_taken_flag(get_glob_type());
1066         init_taken_flag(get_tls_type());
1067
1068         check_initializers(get_glob_type());
1069         check_initializers(get_tls_type());
1070
1071         for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
1072                 ir_graph *irg = get_irp_irg(i);
1073
1074                 assure_irg_outs(irg);
1075                 irg_walk_graph(irg, NULL, check_global_address, get_irg_tls(irg));
1076         }
1077
1078 #ifdef DEBUG_libfirm
1079         if (firm_dbg_get_mask(dbg) & LEVEL_1) {
1080                 print_address_taken_state(get_glob_type());
1081                 print_address_taken_state(get_tls_type());
1082         }
1083 #endif /* DEBUG_libfirm */
1084
1085         /* now computed */
1086         irp->globals_adr_taken_state = ir_address_taken_computed;
1087 }  /* analyse_irp_globals_address_taken */
1088
1089 /* Returns the current address taken state of the globals. */
1090 ir_address_taken_computed_state get_irp_globals_address_taken_state(void) {
1091         return irp->globals_adr_taken_state;
1092 }  /* get_irp_globals_address_taken_state */
1093
1094 /* Sets the current address taken state of the graph. */
1095 void set_irp_globals_address_taken_state(ir_address_taken_computed_state state) {
1096         irp->globals_adr_taken_state = state;
1097 }  /* set_irg_address_taken_state */
1098
1099 /* Assure that the address taken flag is computed for the globals. */
1100 void assure_irp_globals_address_taken_computed(void) {
1101         if (irp->globals_adr_taken_state == ir_address_taken_not_computed)
1102                 analyse_irp_globals_address_taken();
1103 }  /* assure_irp_globals_address_taken_computed */
1104
1105
1106 #include <adt/pmap.h>
1107 #include "typerep.h"
1108
1109 DEBUG_ONLY(static firm_dbg_module_t *dbgcall = NULL;)
1110
1111 /** Maps method types to cloned method types. */
1112 static pmap *mtp_map;
1113
1114 /**
1115  * Clone a method type if not already cloned.
1116  */
1117 static ir_type *clone_type_and_cache(ir_type *tp) {
1118         static ident *prefix = NULL;
1119         ir_type *res;
1120         pmap_entry *e = pmap_find(mtp_map, tp);
1121
1122         if (e)
1123                 return e->value;
1124
1125         if (prefix == NULL)
1126                 prefix = new_id_from_chars("C", 1);
1127
1128         res = clone_type_method(tp, prefix);
1129         pmap_insert(mtp_map, tp, res);
1130         DB((dbgcall, LEVEL_2, "cloned type %+F into %+F\n", tp, res));
1131
1132         return res;
1133 }  /* clone_type_and_cache */
1134
1135 /**
1136  * Copy the calling conventions from the entities to the call type.
1137  */
1138 static void update_calls_to_private(ir_node *call, void *env) {
1139         (void) env;
1140         if (is_Call(call)) {
1141                 ir_node *ptr = get_Call_ptr(call);
1142
1143                 if (is_SymConst(ptr)) {
1144                         ir_entity *ent = get_SymConst_entity(ptr);
1145                         ir_type *ctp = get_Call_type(call);
1146
1147                         if (get_entity_additional_properties(ent) & mtp_property_private) {
1148                                 if ((get_method_additional_properties(ctp) & mtp_property_private) == 0) {
1149                                         ctp = clone_type_and_cache(ctp);
1150                                         set_method_additional_property(ctp, mtp_property_private);
1151                                         set_Call_type(call, ctp);
1152                                         DB((dbgcall, LEVEL_1, "changed call to private method %+F\n", ent));
1153                                 }
1154                         }
1155                 }
1156         }
1157 }  /* update_calls_to_private */
1158
1159 /* Mark all private methods, i.e. those of which all call sites are known. */
1160 void mark_private_methods(void) {
1161         int i;
1162         int changed = 0;
1163
1164         FIRM_DBG_REGISTER(dbgcall, "firm.opt.cc");
1165
1166         assure_irp_globals_address_taken_computed();
1167
1168         mtp_map = pmap_create();
1169
1170         /* first step: change the calling conventions of the local non-escaped entities */
1171         for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
1172                 ir_graph               *irg = get_irp_irg(i);
1173                 ir_entity              *ent = get_irg_entity(irg);
1174                 ir_address_taken_state state = get_entity_address_taken(ent);
1175
1176                 if (get_entity_visibility(ent) == visibility_local &&
1177                     state == ir_address_not_taken) {
1178                         ir_type *mtp = get_entity_type(ent);
1179
1180                         set_entity_additional_property(ent, mtp_property_private);
1181                         DB((dbgcall, LEVEL_1, "found private method %+F\n", ent));
1182                         if ((get_method_additional_properties(mtp) & mtp_property_private) == 0) {
1183                                 /* need a new type */
1184                                 mtp = clone_type_and_cache(mtp);
1185                                 set_entity_type(ent, mtp);
1186                                 set_method_additional_property(mtp, mtp_property_private);
1187                                 changed = 1;
1188                         }
1189                 }
1190         }
1191
1192         if (changed)
1193                 all_irg_walk(NULL, update_calls_to_private, NULL);
1194
1195         pmap_destroy(mtp_map);
1196 }  /* mark_private_methods */