some comments added and fixed
[libfirm] / ir / lower / lower_hl.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   Lower some High-level constructs, moved from the firmlower.
23  * @author  Boris Boesler, Goetz Lindenmaier, Michael Beck
24  * @version $Id$
25  */
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include "lowering.h"
31 #include "irmode_t.h"
32 #include "irnode_t.h"
33 #include "entity_t.h"
34 #include "typerep.h"
35 #include "irprog_t.h"
36 #include "ircons.h"
37 #include "irhooks.h"
38 #include "irgmod.h"
39 #include "irgwalk.h"
40
41 /**
42  * Lower a Sel node. Do not touch Sels accessing entities on the frame type.
43  */
44 static void lower_sel(ir_node *sel) {
45         ir_graph *irg = current_ir_graph;
46         ir_entity   *ent;
47         ir_node  *newn, *cnst, *index, *ptr, *bl;
48         tarval   *tv;
49         ir_mode  *basemode, *mode, *mode_Int;
50         ir_type  *basetyp, *owner;
51         dbg_info *dbg;
52
53         assert(is_Sel(sel));
54
55         ent   = get_Sel_entity(sel);
56         owner = get_entity_owner(ent);
57
58         /* Do not lower frame type access: must be lowered by the backend. */
59         if (is_frame_type(owner))
60                 return;
61
62         /*
63          * Cannot handle value param entities here.
64          * Must be lowered by the backend.
65          */
66         if (is_value_param_type(owner))
67                 return;
68
69         ptr  = get_Sel_ptr(sel);
70         dbg  = get_irn_dbg_info(sel);
71         mode = get_irn_mode(sel);
72
73         mode_Int = get_reference_mode_signed_eq(mode);
74
75         /* TLS access, must be handled by the linker */
76         if (get_tls_type() == owner) {
77                 symconst_symbol sym;
78
79                 sym.entity_p = ent;
80                 bl = get_nodes_block(sel);
81
82                 cnst = new_rd_SymConst(dbg, irg, bl, mode, sym, symconst_addr_ent);
83                 newn = new_rd_Add(dbg, irg, bl, ptr, cnst, mode);
84         } else {
85                 /* not TLS */
86
87                 assert(get_type_state(get_entity_owner(ent)) == layout_fixed);
88                 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
89
90                 bl = get_nodes_block(sel);
91                 if (0 < get_Sel_n_indexs(sel)) {
92                         /* an Array access */
93                         basetyp = get_entity_type(ent);
94                         if (is_Primitive_type(basetyp))
95                                 basemode = get_type_mode(basetyp);
96                         else
97                                 basemode = mode_P_data;
98
99                         assert(basemode && "no mode for lowering Sel");
100                         assert((get_mode_size_bits(basemode) % 8 == 0) && "can not deal with unorthodox modes");
101                         index = get_Sel_index(sel, 0);
102
103                         if (is_Array_type(owner)) {
104                                 ir_node *last_size;
105                                 ir_type *arr_ty = owner;
106                                 int dims = get_array_n_dimensions(arr_ty);
107                                 int *map = alloca(sizeof(int) * dims);
108                                 int i;
109
110                                 assert(dims == get_Sel_n_indexs(sel)
111                                         && "array dimension must match number of indices of Sel node");
112
113                                 for (i = 0; i < dims; i++) {
114                                         int order = get_array_order(arr_ty, i);
115
116                                         assert(order < dims &&
117                                                 "order of a dimension must be smaller than the arrays dim");
118                                         map[order] = i;
119                                 }
120                                 newn = get_Sel_ptr(sel);
121
122                                 /* Size of the array element */
123                                 tv = new_tarval_from_long(get_type_size_bytes(basetyp), mode_Int);
124                                 last_size = new_rd_Const(dbg, irg, get_irg_start_block(irg), mode_Int, tv);
125
126                                 /*
127                                  * We compute the offset part of dimension d_i recursively
128                                  * with the the offset part of dimension d_{i-1}
129                                  *
130                                  *     off_0 = sizeof(array_element_type);
131                                  *     off_i = (u_i - l_i) * off_{i-1}  ; i >= 1
132                                  *
133                                  * whereas u_i is the upper bound of the current dimension
134                                  * and l_i the lower bound of the current dimension.
135                                  */
136                                 for (i = dims - 1; i >= 0; i--) {
137                                         int dim = map[i];
138                                         ir_node *lb, *ub, *elms, *n, *ind;
139
140                                         elms = NULL;
141                                         lb = get_array_lower_bound(arr_ty, dim);
142                                         ub = get_array_upper_bound(arr_ty, dim);
143
144                                         assert(irg == current_ir_graph);
145                                         if (! is_Unknown(lb))
146                                                 lb = new_rd_Conv(dbg, irg, bl, copy_const_value(get_irn_dbg_info(sel), lb), mode_Int);
147                                         else
148                                                 lb = NULL;
149
150                                         if (! is_Unknown(ub))
151                                                 ub = new_rd_Conv(dbg, irg, bl, copy_const_value(get_irn_dbg_info(sel), ub), mode_Int);
152                                         else
153                                                 ub = NULL;
154
155                                         /*
156                                          * If the array has more than one dimension, lower and upper
157                                          * bounds have to be set in the non-last dimension.
158                                          */
159                                         if (i > 0) {
160                                                 assert(lb != NULL && "lower bound has to be set in multi-dim array");
161                                                 assert(ub != NULL && "upper bound has to be set in multi-dim array");
162
163                                                 /* Elements in one Dimension */
164                                                 elms = new_rd_Sub(dbg, irg, bl, ub, lb, mode_Int);
165                                         }
166
167                                         ind = new_rd_Conv(dbg, irg, bl, get_Sel_index(sel, dim), mode_Int);
168
169                                         /*
170                                          * Normalize index, id lower bound is set, also assume
171                                          * lower bound == 0
172                                          */
173                                         if (lb != NULL)
174                                                 ind = new_rd_Sub(dbg, irg, bl, ind, lb, mode_Int);
175
176                                         n = new_rd_Mul(dbg, irg, bl, ind, last_size, mode_Int);
177
178                                         /*
179                                          * see comment above.
180                                          */
181                                         if (i > 0)
182                                                 last_size = new_rd_Mul(dbg, irg, bl, last_size, elms, mode_Int);
183
184                                         newn = new_rd_Add(dbg, irg, bl, newn, n, mode);
185                                 }
186                         } else {
187                                 /* no array type */
188                                 ir_mode *idx_mode = get_irn_mode(index);
189                                 tarval *tv = new_tarval_from_long(get_mode_size_bytes(basemode), idx_mode);
190
191                                 newn = new_rd_Add(dbg, irg, bl, get_Sel_ptr(sel),
192                                         new_rd_Mul(dbg, irg, bl, index,
193                                         new_r_Const(irg, get_irg_start_block(irg), idx_mode, tv),
194                                         idx_mode),
195                                         mode);
196                         }
197                 } else if (is_Method_type(get_entity_type(ent)) &&
198                            is_Class_type(owner) &&
199                            (owner != get_glob_type()) &&
200                            (!is_frame_type(owner)))  {
201                         ir_node *add;
202                         ir_mode *ent_mode = get_type_mode(get_entity_type(ent));
203
204                         /* We need an additional load when accessing methods from a dispatch table. */
205                         tv   = new_tarval_from_long(get_entity_offset(ent), mode_Int);
206                         cnst = new_rd_Const(dbg, irg, get_irg_start_block(irg), mode_Int, tv);
207                         add  = new_rd_Add(dbg, irg, bl, get_Sel_ptr(sel), cnst, mode);
208 #ifdef DO_CACHEOPT  /* cacheopt version */
209                         newn = new_rd_Load(dbg, irg, bl, get_Sel_mem(sel), sel, ent_mode);
210                         cacheopt_map_addrs_register_node(newn);
211                         set_Load_ptr(newn, add);
212 #else /* normal code */
213                         newn = new_rd_Load(dbg, irg, bl, get_Sel_mem(sel), add, ent_mode);
214 #endif
215                         newn = new_r_Proj(irg, bl, newn, ent_mode, pn_Load_res);
216
217                 } else if (get_entity_owner(ent) != get_glob_type()) {
218                         int offset;
219
220                         /* replace Sel by add(obj, const(ent.offset)) */
221                         assert(!(get_entity_allocation(ent) == allocation_static &&
222                                 (get_entity_n_overwrites(ent) == 0 && get_entity_n_overwrittenby(ent) == 0)));
223                         newn   = get_Sel_ptr(sel);
224                         offset = get_entity_offset(ent);
225                         if (offset != 0) {
226                                 ir_mode *mode_UInt = get_reference_mode_unsigned_eq(mode);
227
228                                 tv = new_tarval_from_long(offset, mode_UInt);
229                                 cnst = new_r_Const(irg, get_irg_start_block(irg), mode_UInt, tv);
230                                 newn = new_rd_Add(dbg, irg, bl, newn, cnst, mode);
231                         }
232                 } else {
233                         /* global_type */
234                         newn = new_rd_SymConst_addr_ent(NULL, current_ir_graph, mode, ent, firm_unknown_type);
235                 }
236         }
237         /* run the hooks */
238         hook_lower(sel);
239
240         exchange(sel, newn);
241 }  /* lower_sel */
242
243 /**
244  * Lower a all possible SymConst nodes.
245  */
246 static void lower_symconst(ir_node *symc) {
247         ir_node       *newn;
248         ir_type       *tp;
249         ir_entity     *ent;
250         tarval        *tv;
251         ir_enum_const *ec;
252         ir_mode       *mode;
253
254         switch (get_SymConst_kind(symc)) {
255         case symconst_type_tag:
256                 assert(!"SymConst kind symconst_type_tag not implemented");
257                 break;
258         case symconst_type_size:
259                 /* rewrite the SymConst node by a Const node */
260                 tp   = get_SymConst_type(symc);
261                 assert(get_type_state(tp) == layout_fixed);
262                 mode = get_irn_mode(symc);
263                 tv   = new_tarval_from_long(get_type_size_bytes(tp), mode);
264                 newn = new_r_Const(current_ir_graph,
265                                    get_irg_start_block(current_ir_graph),
266                                    get_irn_mode(symc), tv);
267                 assert(newn);
268                 /* run the hooks */
269                 hook_lower(symc);
270                 exchange(symc, newn);
271                 break;
272         case symconst_type_align:
273                 /* rewrite the SymConst node by a Const node */
274                 tp   = get_SymConst_type(symc);
275                 assert(get_type_state(tp) == layout_fixed);
276                 mode = get_irn_mode(symc);
277                 tv   = new_tarval_from_long(get_type_alignment_bytes(tp), mode);
278                 newn = new_r_Const(current_ir_graph,
279                                    get_irg_start_block(current_ir_graph),
280                                    mode, tv);
281                 assert(newn);
282                 /* run the hooks */
283                 hook_lower(symc);
284                 exchange(symc, newn);
285                 break;
286         case symconst_addr_name:
287                 /* do not rewrite - pass info to back end */
288                 break;
289         case symconst_addr_ent:
290                 /* leave */
291                 break;
292         case symconst_ofs_ent:
293                 /* rewrite the SymConst node by a Const node */
294                 ent  = get_SymConst_entity(symc);
295                 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
296                 mode = get_irn_mode(symc);
297                 tv   = new_tarval_from_long(get_entity_offset(ent), mode);
298                 newn = new_r_Const(current_ir_graph,
299                                    get_irg_start_block(current_ir_graph),
300                                    mode, tv);
301                 assert(newn);
302                 /* run the hooks */
303                 hook_lower(symc);
304                 exchange(symc, newn);
305                 break;
306         case symconst_enum_const:
307                 /* rewrite the SymConst node by a Const node */
308                 ec   = get_SymConst_enum(symc);
309                 assert(get_type_state(get_enumeration_owner(ec)) == layout_fixed);
310                 tv   = get_enumeration_value(ec);
311                 newn = new_r_Const(current_ir_graph,
312                                   get_irg_start_block(current_ir_graph),
313                                   get_irn_mode(symc), tv);
314                 assert(newn);
315                 /* run the hooks */
316                 hook_lower(symc);
317                 exchange(symc, newn);
318                 break;
319         case symconst_label:
320                 /* leave */
321                 break;
322
323         default:
324                 assert(!"unknown SymConst kind");
325                 break;
326         }
327 }  /* lower_symconst */
328
329 /**
330  * Checks, whether a size is an integral size
331  *
332  * @param size  the size on bits
333  */
334 static int is_integral_size(int size) {
335         /* must be a 2^n */
336         if (size & (size-1))
337                 return 0;
338         /* must be at least byte size */
339         return size >= 8;
340 }  /* is_integral_size */
341
342 /**
343  * lower bitfield load access.
344  *
345  * @param proj  the Proj(result) node
346  * @param load  the Load node
347  */
348 static void lower_bitfields_loads(ir_node *proj, ir_node *load) {
349         ir_node *sel = get_Load_ptr(load);
350         ir_node *block, *n_proj, *res, *ptr;
351         ir_entity *ent;
352         ir_type *bf_type;
353         ir_mode *bf_mode, *mode;
354         int offset, bit_offset, bits, bf_bits, old_cse;
355         dbg_info *db;
356
357         if (get_irn_op(sel) != op_Sel)
358                 return;
359
360         ent     = get_Sel_entity(sel);
361         bf_type = get_entity_type(ent);
362
363         /* must be a bitfield type */
364         if (!is_Primitive_type(bf_type) || get_primitive_base_type(bf_type) == NULL)
365                 return;
366
367         /* We have a bitfield access, if either a bit offset is given, or
368            the size is not integral. */
369         bf_mode = get_type_mode(bf_type);
370         if (! bf_mode)
371                 return;
372
373         mode       = get_irn_mode(proj);
374         block      = get_nodes_block(proj);
375         bf_bits    = get_mode_size_bits(bf_mode);
376         bit_offset = get_entity_offset_bits_remainder(ent);
377
378         if (bit_offset == 0 && is_integral_size(bf_bits) && bf_mode == get_Load_mode(load))
379                 return;
380
381         bits   = get_mode_size_bits(mode);
382         offset = get_entity_offset(ent);
383
384         /*
385          * ok, here we are: now convert the Proj_mode_bf(Load) into And(Shr(Proj_mode(Load)) for unsigned
386          * and Shr(Shl(Proj_mode(load)) for signed
387          */
388
389         /* abandon bitfield sel */
390         ptr = get_Sel_ptr(sel);
391         db  = get_irn_dbg_info(sel);
392         ptr = new_rd_Add(db, current_ir_graph, block, ptr, new_Const_long(mode_Is, offset), get_irn_mode(ptr));
393
394         set_Load_ptr(load, ptr);
395         set_Load_mode(load, mode);
396
397
398         /* create new proj, switch off CSE or we may get the old one back */
399         old_cse = get_opt_cse();
400         set_opt_cse(0);
401         res = n_proj = new_r_Proj(current_ir_graph, block, load, mode, pn_Load_res);
402         set_opt_cse(old_cse);
403
404         if (mode_is_signed(mode)) { /* signed */
405                 int shift_count_up    = bits - (bf_bits + bit_offset);
406                 int shift_count_down  = bits - bf_bits;
407
408                 if (shift_count_up) {
409                         res = new_r_Shl(current_ir_graph, block, res,
410                                 new_r_Const(current_ir_graph, block, mode_Iu, new_tarval_from_long(shift_count_up, mode_Iu)), mode);
411                 }
412                 if (shift_count_down) {
413                         res = new_r_Shrs(current_ir_graph, block, res,
414                                 new_r_Const(current_ir_graph, block, mode_Iu, new_tarval_from_long(shift_count_down, mode_Iu)), mode);
415                 }
416         } else { /* unsigned */
417                 int shift_count_down  = bit_offset;
418                 unsigned mask = ((unsigned)-1) >> (bits - bf_bits);
419
420                 if (shift_count_down) {
421                         res = new_r_Shr(current_ir_graph, block, res,
422                                 new_r_Const(current_ir_graph, block, mode_Iu, new_tarval_from_long(shift_count_down, mode_Iu)), mode);
423                 }
424                 if (bits != bf_bits) {
425                         res = new_r_And(current_ir_graph, block, res,
426                                 new_r_Const(current_ir_graph, block, mode, new_tarval_from_long(mask, mode)), mode);
427                 }
428         }
429
430         exchange(proj, res);
431 }  /* lower_bitfields_loads */
432
433 /**
434  * lower bitfield store access.
435  *
436  * @todo: It adds a load which may produce an exception!
437  */
438 static void lower_bitfields_stores(ir_node *store) {
439         ir_node   *sel = get_Store_ptr(store);
440         ir_node   *ptr, *value;
441         ir_entity *ent;
442         ir_type *bf_type;
443         ir_mode *bf_mode, *mode;
444         ir_node *mem, *irn, *block;
445         unsigned mask, neg_mask;
446         int bf_bits, bits_mask, offset, bit_offset;
447         dbg_info *db;
448
449         /* check bitfield access */
450         if (get_irn_op(sel) != op_Sel)
451                 return;
452
453         ent     = get_Sel_entity(sel);
454         bf_type = get_entity_type(ent);
455
456         /* must be a bitfield type */
457         if (!is_Primitive_type(bf_type) || get_primitive_base_type(bf_type) == NULL)
458                 return;
459
460         /* We have a bitfield access, if either a bit offset is given, or
461            the size is not integral. */
462         bf_mode = get_type_mode(bf_type);
463         if (! bf_mode)
464                 return;
465
466         value      = get_Store_value(store);
467         mode       = get_irn_mode(value);
468         block      = get_nodes_block(store);
469
470         bf_bits    = get_mode_size_bits(bf_mode);
471         bit_offset = get_entity_offset_bits_remainder(ent);
472
473         if (bit_offset == 0 && is_integral_size(bf_bits) && bf_mode == get_irn_mode(value))
474                 return;
475
476         /*
477          * ok, here we are: now convert the Store(Sel(), value) into Or(And(Load(Sel),c), And(Value,c))
478          */
479         mem        = get_Store_mem(store);
480         offset     = get_entity_offset(ent);
481
482         bits_mask = get_mode_size_bits(mode) - bf_bits;
483         mask = ((unsigned)-1) >> bits_mask;
484         mask <<= bit_offset;
485         neg_mask = ~mask;
486
487         /* abandon bitfield sel */
488         ptr = get_Sel_ptr(sel);
489         db  = get_irn_dbg_info(sel);
490         ptr = new_rd_Add(db, current_ir_graph, block, ptr, new_Const_long(mode_Is, offset), get_irn_mode(ptr));
491
492         if (neg_mask) {
493                 /* there are some bits, normal case */
494                 irn  = new_r_Load(current_ir_graph, block, mem, ptr, mode);
495                 mem  = new_r_Proj(current_ir_graph, block, irn, mode_M, pn_Load_M);
496                 irn  = new_r_Proj(current_ir_graph, block, irn, mode, pn_Load_res);
497
498                 irn = new_r_And(current_ir_graph, block, irn,
499                         new_r_Const(current_ir_graph, block, mode, new_tarval_from_long(neg_mask, mode)), mode);
500
501                 if (bit_offset > 0) {
502                         value = new_r_Shl(current_ir_graph, block, value,
503                                 new_r_Const(current_ir_graph, block, mode_Iu, new_tarval_from_long(bit_offset, mode_Iu)), mode);
504                 }
505
506                 value = new_r_And(current_ir_graph, block, value,
507                         new_r_Const(current_ir_graph, block, mode, new_tarval_from_long(mask, mode)), mode);
508
509                 value = new_r_Or(current_ir_graph, block, value, irn, mode);
510         }
511
512         set_Store_mem(store, mem);
513         set_Store_value(store, value);
514         set_Store_ptr(store, ptr);
515 }  /* lower_bitfields_stores */
516
517 /**
518  * Lowers unaligned Loads.
519  */
520 static void lower_unaligned_Load(ir_node *load) {
521   (void) load;
522         /* NYI */
523 }
524
525 /**
526  * Lowers unaligned Stores
527  */
528 static void lower_unaligned_Store(ir_node *store) {
529         (void) store;
530         /* NYI */
531 }
532
533 /**
534  * lowers IR-nodes, called from walker
535  */
536 static void lower_irnode(ir_node *irn, void *env) {
537         (void) env;
538         switch (get_irn_opcode(irn)) {
539         case iro_Sel:
540                 lower_sel(irn);
541                 break;
542         case iro_SymConst:
543                 lower_symconst(irn);
544                 break;
545         case iro_Load:
546                 if (env != NULL && get_Load_align(irn) == align_non_aligned)
547                         lower_unaligned_Load(irn);
548                 break;
549         case iro_Store:
550                 if (env != NULL && get_Store_align(irn) == align_non_aligned)
551                         lower_unaligned_Store(irn);
552                 break;
553         case iro_Cast:
554                 exchange(irn, get_Cast_op(irn));
555                 break;
556         default:
557                 break;
558         }
559 }  /* lower_irnode */
560
561 /**
562  * Walker: lowers IR-nodes for bitfield access
563  */
564 static void lower_bf_access(ir_node *irn, void *env) {
565         (void) env;
566         switch (get_irn_opcode(irn)) {
567         case iro_Proj:
568         {
569                 long proj     = get_Proj_proj(irn);
570                 ir_node *pred = get_Proj_pred(irn);
571                 ir_op *op     = get_irn_op(pred);
572
573                 if ((proj == pn_Load_res) && (op == op_Load))
574                         lower_bitfields_loads(irn, pred);
575                 break;
576         }
577         case iro_Store:
578                 lower_bitfields_stores(irn);
579                 break;
580
581         default:
582                 break;
583         }
584 }  /* lower_bf_access */
585
586 /*
587  * Replaces SymConsts by a real constant if possible.
588  * Replace Sel nodes by address computation.  Also resolves array access.
589  * Handle Bitfields by added And/Or calculations.
590  */
591 void lower_highlevel_graph(ir_graph *irg, int lower_bitfields) {
592
593         if (lower_bitfields) {
594                 /* First step: lower bitfield access: must be run as long as Sels still
595                  * exists. */
596                 irg_walk_graph(irg, NULL, lower_bf_access, NULL);
597         }
598
599         /* Finally: lower SymConst-Size and Sel nodes, Casts, unaligned Load/Stores. */
600         irg_walk_graph(irg, NULL, lower_irnode, NULL);
601         set_irg_phase_low(irg);
602 }  /* lower_highlevel_graph */
603
604 /*
605  * does the same as lower_highlevel() for all nodes on the const code irg
606  */
607 void lower_const_code(void) {
608         walk_const_code(NULL, lower_irnode, NULL);
609 }  /* lower_const_code */
610
611 /*
612  * Replaces SymConsts by a real constant if possible.
613  * Replace Sel nodes by address computation.  Also resolves array access.
614  * Handle Bitfields by added And/Or calculations.
615  */
616 void lower_highlevel(int lower_bitfields) {
617         int i, n;
618
619         n = get_irp_n_irgs();
620         for (i = 0; i < n; ++i) {
621                 ir_graph *irg = get_irp_irg(i);
622                 lower_highlevel_graph(irg, lower_bitfields);
623         }
624         lower_const_code();
625 }  /* lower_highlevel */