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