valueset: Remove the unused link field.
[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  */
25 #include "config.h"
26
27 #include "lowering.h"
28 #include "irmode_t.h"
29 #include "irnode_t.h"
30 #include "entity_t.h"
31 #include "typerep.h"
32 #include "irprog_t.h"
33 #include "ircons.h"
34 #include "irhooks.h"
35 #include "irgmod.h"
36 #include "irgwalk.h"
37 #include "irtools.h"
38 #include "irpass_t.h"
39
40 /**
41  * Lower a Sel node. Do not touch Sels accessing entities on the frame type.
42  */
43 static void lower_sel(ir_node *sel)
44 {
45         ir_graph  *irg   = get_irn_irg(sel);
46         ir_entity *ent   = get_Sel_entity(sel);
47         ir_type   *owner = get_entity_owner(ent);
48         dbg_info  *dbg   = get_irn_dbg_info(sel);
49         ir_mode   *mode  = get_irn_mode(sel);
50         ir_node   *bl    = get_nodes_block(sel);
51         ir_node   *newn;
52
53         /* we can only replace Sels when the layout of the owner type is decided. */
54         if (get_type_state(owner) != layout_fixed)
55                 return;
56
57         if (0 < get_Sel_n_indexs(sel)) {
58                 /* an Array access */
59                 ir_type *basetyp = get_entity_type(ent);
60                 ir_mode *basemode;
61                 ir_node *index;
62                 if (is_Primitive_type(basetyp))
63                         basemode = get_type_mode(basetyp);
64                 else
65                         basemode = mode_P_data;
66
67                 assert(basemode && "no mode for lowering Sel");
68                 assert((get_mode_size_bits(basemode) % 8 == 0) && "can not deal with unorthodox modes");
69                 index = get_Sel_index(sel, 0);
70
71                 if (is_Array_type(owner)) {
72                         ir_type *arr_ty = owner;
73                         size_t   dims   = get_array_n_dimensions(arr_ty);
74                         size_t  *map    = ALLOCAN(size_t, dims);
75                         ir_mode *mode_Int = get_reference_mode_signed_eq(mode);
76                         ir_tarval *tv;
77                         ir_node *last_size;
78                         size_t   i;
79
80                         assert(dims == (size_t)get_Sel_n_indexs(sel)
81                                 && "array dimension must match number of indices of Sel node");
82
83                         for (i = 0; i < dims; i++) {
84                                 size_t order = get_array_order(arr_ty, i);
85
86                                 assert(order < dims &&
87                                         "order of a dimension must be smaller than the arrays dim");
88                                 map[order] = i;
89                         }
90                         newn = get_Sel_ptr(sel);
91
92                         /* Size of the array element */
93                         tv = new_tarval_from_long(get_type_size_bytes(basetyp), mode_Int);
94                         last_size = new_rd_Const(dbg, irg, tv);
95
96                         /*
97                          * We compute the offset part of dimension d_i recursively
98                          * with the the offset part of dimension d_{i-1}
99                          *
100                          *     off_0 = sizeof(array_element_type);
101                          *     off_i = (u_i - l_i) * off_{i-1}  ; i >= 1
102                          *
103                          * whereas u_i is the upper bound of the current dimension
104                          * and l_i the lower bound of the current dimension.
105                          */
106                         for (i = dims; i > 0;) {
107                                 size_t dim = map[--i];
108                                 ir_node *lb, *ub, *elms, *n, *ind;
109
110                                 elms = NULL;
111                                 lb = get_array_lower_bound(arr_ty, dim);
112                                 ub = get_array_upper_bound(arr_ty, dim);
113
114                                 if (! is_Unknown(lb))
115                                         lb = new_rd_Conv(dbg, bl, copy_const_value(get_irn_dbg_info(sel), lb, bl), mode_Int);
116                                 else
117                                         lb = NULL;
118
119                                 if (! is_Unknown(ub))
120                                         ub = new_rd_Conv(dbg, bl, copy_const_value(get_irn_dbg_info(sel), ub, bl), mode_Int);
121                                 else
122                                         ub = NULL;
123
124                                 /*
125                                  * If the array has more than one dimension, lower and upper
126                                  * bounds have to be set in the non-last dimension.
127                                  */
128                                 if (i > 0) {
129                                         assert(lb != NULL && "lower bound has to be set in multi-dim array");
130                                         assert(ub != NULL && "upper bound has to be set in multi-dim array");
131
132                                         /* Elements in one Dimension */
133                                         elms = new_rd_Sub(dbg, bl, ub, lb, mode_Int);
134                                 }
135
136                                 ind = new_rd_Conv(dbg, bl, get_Sel_index(sel, dim), mode_Int);
137
138                                 /*
139                                  * Normalize index, id lower bound is set, also assume
140                                  * lower bound == 0
141                          */
142                                 if (lb != NULL)
143                                         ind = new_rd_Sub(dbg, bl, ind, lb, mode_Int);
144
145                                 n = new_rd_Mul(dbg, bl, ind, last_size, mode_Int);
146
147                                 /*
148                                  * see comment above.
149                                  */
150                                 if (i > 0)
151                                         last_size = new_rd_Mul(dbg, bl, last_size, elms, mode_Int);
152
153                                 newn = new_rd_Add(dbg, bl, newn, n, mode);
154                         }
155                 } else {
156                         /* no array type */
157                         ir_mode   *idx_mode = get_irn_mode(index);
158                         ir_tarval *tv       = new_tarval_from_long(get_mode_size_bytes(basemode), idx_mode);
159
160                         newn = new_rd_Add(dbg, bl, get_Sel_ptr(sel),
161                                 new_rd_Mul(dbg, bl, index,
162                                 new_r_Const(irg, tv),
163                                 idx_mode),
164                                 mode);
165                 }
166         } else if (is_Method_type(get_entity_type(ent)) && is_Class_type(owner)) {
167                 /* We need an additional load when accessing methods from a dispatch
168                  * table.
169                  * Matze TODO: Is this really still used? At least liboo does its own
170                  * lowering of Method-Sels...
171                  */
172                 ir_mode   *ent_mode = get_type_mode(get_entity_type(ent));
173                 int        offset   = get_entity_offset(ent);
174                 ir_mode   *mode_Int = get_reference_mode_signed_eq(mode);
175                 ir_tarval *tv       = new_tarval_from_long(offset, mode_Int);
176                 ir_node   *cnst     = new_rd_Const(dbg, irg, tv);
177                 ir_node   *add      = new_rd_Add(dbg, bl, get_Sel_ptr(sel), cnst, mode);
178                 ir_node   *mem      = get_Sel_mem(sel);
179                 newn = new_rd_Load(dbg, bl, mem, add, ent_mode, cons_none);
180                 newn = new_r_Proj(newn, ent_mode, pn_Load_res);
181         } else {
182                 int offset = get_entity_offset(ent);
183
184                 /* replace Sel by add(obj, const(ent.offset)) */
185                 newn = get_Sel_ptr(sel);
186                 if (offset != 0) {
187                         ir_mode   *mode_UInt = get_reference_mode_unsigned_eq(mode);
188                         ir_tarval *tv        = new_tarval_from_long(offset, mode_UInt);
189                         ir_node   *cnst      = new_r_Const(irg, tv);
190                         newn = new_rd_Add(dbg, bl, newn, cnst, mode);
191                 }
192         }
193
194         /* run the hooks */
195         hook_lower(sel);
196
197         exchange(sel, newn);
198 }
199
200 /**
201  * Lower a all possible SymConst nodes.
202  */
203 static void lower_symconst(ir_node *symc)
204 {
205         ir_node       *newn;
206         ir_type       *tp;
207         ir_entity     *ent;
208         ir_tarval     *tv;
209         ir_enum_const *ec;
210         ir_mode       *mode;
211         ir_graph      *irg;
212
213         switch (get_SymConst_kind(symc)) {
214         case symconst_type_size:
215                 /* rewrite the SymConst node by a Const node */
216                 irg  = get_irn_irg(symc);
217                 tp   = get_SymConst_type(symc);
218                 assert(get_type_state(tp) == layout_fixed);
219                 mode = get_irn_mode(symc);
220                 newn = new_r_Const_long(irg, mode, get_type_size_bytes(tp));
221                 assert(newn);
222                 /* run the hooks */
223                 hook_lower(symc);
224                 exchange(symc, newn);
225                 break;
226         case symconst_type_align:
227                 /* rewrite the SymConst node by a Const node */
228                 irg  = get_irn_irg(symc);
229                 tp   = get_SymConst_type(symc);
230                 assert(get_type_state(tp) == layout_fixed);
231                 mode = get_irn_mode(symc);
232                 newn = new_r_Const_long(irg, mode, get_type_alignment_bytes(tp));
233                 assert(newn);
234                 /* run the hooks */
235                 hook_lower(symc);
236                 exchange(symc, newn);
237                 break;
238         case symconst_addr_ent:
239                 /* leave */
240                 break;
241         case symconst_ofs_ent:
242                 /* rewrite the SymConst node by a Const node */
243                 irg  = get_irn_irg(symc);
244                 ent  = get_SymConst_entity(symc);
245                 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
246                 mode = get_irn_mode(symc);
247                 newn = new_r_Const_long(irg, mode, get_entity_offset(ent));
248                 assert(newn);
249                 /* run the hooks */
250                 hook_lower(symc);
251                 exchange(symc, newn);
252                 break;
253         case symconst_enum_const:
254                 /* rewrite the SymConst node by a Const node */
255                 irg  = get_irn_irg(symc);
256                 ec   = get_SymConst_enum(symc);
257                 assert(get_type_state(get_enumeration_owner(ec)) == layout_fixed);
258                 tv   = get_enumeration_value(ec);
259                 newn = new_r_Const(irg, tv);
260                 assert(newn);
261                 /* run the hooks */
262                 hook_lower(symc);
263                 exchange(symc, newn);
264                 break;
265
266         default:
267                 assert(!"unknown SymConst kind");
268                 break;
269         }
270 }
271
272 /**
273  * lowers IR-nodes, called from walker
274  */
275 static void lower_irnode(ir_node *irn, void *env)
276 {
277         (void) env;
278         switch (get_irn_opcode(irn)) {
279         case iro_Sel:
280                 lower_sel(irn);
281                 break;
282         case iro_SymConst:
283                 lower_symconst(irn);
284                 break;
285         default:
286                 break;
287         }
288 }
289
290 /*
291  * Replaces SymConsts by a real constant if possible.
292  * Replace Sel nodes by address computation.  Also resolves array access.
293  * Handle Bitfields by added And/Or calculations.
294  */
295 void lower_highlevel_graph(ir_graph *irg)
296 {
297         /* Finally: lower SymConst-Size and Sel nodes, unaligned Load/Stores. */
298         irg_walk_graph(irg, NULL, lower_irnode, NULL);
299 }
300
301 typedef struct pass_t {
302         ir_graph_pass_t pass;
303 } pass_t;
304
305 /**
306  * Wrapper for running lower_highlevel_graph() as an ir_graph pass.
307  */
308 static int lower_highlevel_graph_wrapper(ir_graph *irg, void *context)
309 {
310         (void)context;
311
312         lower_highlevel_graph(irg);
313         return 0;
314 }
315
316 ir_graph_pass_t *lower_highlevel_graph_pass(const char *name)
317 {
318         pass_t *pass = XMALLOCZ(pass_t);
319
320         return def_graph_pass_constructor(
321                 &pass->pass, name ? name : "lower_hl", lower_highlevel_graph_wrapper);
322 }
323
324 /*
325  * does the same as lower_highlevel() for all nodes on the const code irg
326  */
327 void lower_const_code(void)
328 {
329         walk_const_code(NULL, lower_irnode, NULL);
330 }
331
332 ir_prog_pass_t *lower_const_code_pass(const char *name)
333 {
334         return def_prog_pass(name ? name : "lower_const_code", lower_const_code);
335 }
336
337 /*
338  * Replaces SymConsts by a real constant if possible.
339  * Replace Sel nodes by address computation.  Also resolves array access.
340  * Handle Bitfields by added And/Or calculations.
341  */
342 void lower_highlevel()
343 {
344         size_t i, n;
345
346         n = get_irp_n_irgs();
347         for (i = 0; i < n; ++i) {
348                 ir_graph *irg = get_irp_irg(i);
349                 lower_highlevel_graph(irg);
350         }
351         lower_const_code();
352 }