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