fa1ad2b5d8895976471e65e2785712da0dfe1a7d
[libfirm] / ir / lower / lower_mode_b.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       lowers operations with mode_b. The result is a graph which
23  *              might still contains some convs from/to mode_b, but no
24  *              operations are performed on them anymore, they are just there
25  *              so modes match. A backend can safely skip all mode_b convs.
26  * @author      Matthias Braun, Christoph Mallon
27  * @version     $Id$
28  *
29  * After this pass the following should hold:
30  *   - The only inputs with mode_b are for the Cond node or the
31  *     Sel input of a Mux node.
32  *   - The only nodes producing mode_b are: Proj(Cmp) and ConvB(X) (where X
33  *     is some mode that can be converted to the lowered mode).
34  *     ConvB will usually be implemented by a comparison with 0 producing some
35  *     flags in the backends. It's debatable wether ConvB(X) is a goode idea.
36  *     Maybe we should rather introduce a Test node.
37  * All other former uses should be converted to manipulations with an integer
38  * mode that was specified in the pass configuration.
39  */
40 #include "config.h"
41
42 #include <stdlib.h>
43 #include <stdbool.h>
44
45 #include "irnode_t.h"
46 #include "ircons_t.h"
47 #include "irflag.h"
48 #include "irgwalk.h"
49 #include "irtools.h"
50 #include "iredges.h"
51 #include "iropt_t.h"
52 #include "tv.h"
53 #include "error.h"
54 #include "lowering.h"
55 #include "pdeq.h"
56 #include "irpass_t.h"
57
58 static lower_mode_b_config_t  config;
59 static ir_type               *lowered_type  = NULL;
60 static pdeq                  *lowered_nodes = NULL;
61
62 /**
63  * Removes a node if its out-edge count has reached 0.
64  * temporary hack until we have proper automatic dead code elimination.
65  */
66 static void maybe_kill_node(ir_node *node)
67 {
68         ir_graph *irg;
69         int       i, arity;
70
71         if (get_irn_n_edges(node) != 0)
72                 return;
73
74         irg = get_irn_irg(node);
75
76         assert(!is_Bad(node));
77
78         arity = get_irn_arity(node);
79         for (i = 0; i < arity; ++i) {
80                 set_irn_n(node, i, new_Bad());
81         }
82         set_nodes_block(node, new_Bad());
83
84         edges_node_deleted(node, irg);
85 }
86
87 static ir_node *create_not(dbg_info *dbgi, ir_node *node)
88 {
89         ir_node  *block  = get_nodes_block(node);
90         ir_mode  *mode   = config.lowered_mode;
91         tarval   *tv_one = get_tarval_one(mode);
92         ir_node  *one    = new_d_Const(dbgi, tv_one);
93
94         return new_rd_Eor(dbgi, block, node, one, mode);
95 }
96
97 static ir_node *create_convb(ir_node *node)
98 {
99         ir_node  *block = get_nodes_block(node);
100         ir_node  *conv  = new_rd_Conv(NULL, block, node, mode_b);
101
102         return conv;
103 }
104
105 static ir_type *create_lowered_type(void)
106 {
107         if (lowered_type == NULL) {
108                 lowered_type = new_type_primitive(config.lowered_mode);
109         }
110         return lowered_type;
111 }
112
113 /**
114  * creates a "set" node that produces a 0 or 1 based on a Cmp result
115  */
116 static ir_node *create_set(ir_node *node)
117 {
118         dbg_info *dbgi    = get_irn_dbg_info(node);
119         ir_mode  *mode    = config.lowered_set_mode;
120         tarval   *tv_one  = get_tarval_one(mode);
121         ir_node  *one     = new_d_Const(dbgi, tv_one);
122         ir_node  *block   = get_nodes_block(node);
123         tarval   *tv_zero = get_tarval_null(mode);
124         ir_node  *zero    = new_d_Const(dbgi, tv_zero);
125
126         ir_node *set      = new_rd_Mux(dbgi, block, node, zero, one, mode);
127
128         if (mode != config.lowered_mode) {
129                 set = new_r_Conv(block, set, config.lowered_mode);
130         }
131
132         return set;
133 }
134
135 static void adjust_method_type(ir_type *method_type)
136 {
137         int i;
138         int n_params;
139         int n_res;
140
141         n_params = get_method_n_params(method_type);
142         for (i = 0; i < n_params; ++i) {
143                 ir_type *param = get_method_param_type(method_type, i);
144                 if (get_type_mode(param) == mode_b) {
145                         set_method_param_type(method_type, i, create_lowered_type());
146                 }
147         }
148
149         n_res = get_method_n_ress(method_type);
150         for (i = 0; i < n_res; ++i) {
151                 ir_type *res_type = get_method_res_type(method_type, i);
152                 if (get_type_mode(res_type) == mode_b) {
153                         set_method_res_type(method_type, i, create_lowered_type());
154                 }
155         }
156 }
157
158 static ir_node *lower_node(ir_node *node)
159 {
160         dbg_info *dbgi  = get_irn_dbg_info(node);
161         ir_node  *block = get_nodes_block(node);
162         ir_mode *mode   = config.lowered_mode;
163         ir_node  *res;
164
165         assert(get_irn_mode(node) == mode_b);
166
167         res = get_irn_link(node);
168         if (res != NULL)
169                 return res;
170
171         switch (get_irn_opcode(node)) {
172         case iro_Phi: {
173                 int       i, arity;
174                 ir_node **in;
175                 ir_node  *unknown, *new_phi;
176
177                 arity   = get_irn_arity(node);
178                 in      = ALLOCAN(ir_node*, arity);
179                 unknown = new_Unknown(config.lowered_mode);
180                 for (i = 0; i < arity; ++i) {
181                         in[i] = unknown;
182                 }
183                 new_phi = new_r_Phi(block, arity, in, config.lowered_mode);
184                 set_irn_link(node, new_phi);
185                 pdeq_putr(lowered_nodes, node);
186
187                 for (i = 0; i < arity; ++i) {
188                         ir_node *in     = get_irn_n(node, i);
189                         ir_node *low_in = lower_node(in);
190
191                         set_irn_n(new_phi, i, low_in);
192                 }
193
194                 return new_phi;
195         }
196
197         case iro_And:
198         case iro_Or:
199         case iro_Eor: {
200                 int i, arity;
201                 ir_node *copy = exact_copy(node);
202
203                 arity = get_irn_arity(node);
204                 for (i = 0; i < arity; ++i) {
205                         ir_node *in     = get_irn_n(node, i);
206                         ir_node *low_in = lower_node(in);
207
208                         set_irn_n(copy, i, low_in);
209                 }
210                 set_irn_mode(copy, config.lowered_mode);
211
212                 set_irn_link(node, copy);
213                 pdeq_putr(lowered_nodes, node);
214                 return copy;
215         }
216         case iro_Not: {
217                 ir_node *op     = get_Not_op(node);
218                 ir_node *low_op = lower_node(op);
219
220                 res = create_not(dbgi, low_op);
221                 set_irn_link(node, res);
222                 pdeq_putr(lowered_nodes, node);
223                 return res;
224         }
225         case iro_Mux: {
226                 ir_node *cond        = get_Mux_sel(node);
227                 ir_node *low_cond    = lower_node(cond);
228                 ir_node *v_true      = get_Mux_true(node);
229                 ir_node *low_v_true  = lower_node(v_true);
230                 ir_node *v_false     = get_Mux_false(node);
231                 ir_node *low_v_false = lower_node(v_false);
232
233                 ir_node *and0     = new_rd_And(dbgi, block, low_cond, low_v_true, mode);
234                 ir_node *not_cond = create_not(dbgi, low_cond);
235                 ir_node *and1     = new_rd_And(dbgi, block, not_cond, low_v_false, mode);
236                 ir_node *or       = new_rd_Or(dbgi, block, and0, and1, mode);
237
238                 set_irn_link(node, or);
239                 pdeq_putr(lowered_nodes, node);
240                 return or;
241         }
242         case iro_Conv: {
243                 ir_node *pred     = get_Conv_op(node);
244                 ir_mode *mode     = get_irn_mode(pred);
245                 tarval  *tv_zeroc = get_tarval_null(mode);
246                 ir_node *zero_cmp = new_d_Const(dbgi, tv_zeroc);
247                 ir_node *set;
248
249                 ir_node *cmp      = new_rd_Cmp(dbgi, block, pred, zero_cmp);
250                 ir_node *proj     = new_rd_Proj(dbgi, block, cmp, mode_b, pn_Cmp_Lg);
251                 set = create_set(proj);
252
253                 set_irn_link(node, set);
254                 pdeq_putr(lowered_nodes, node);
255                 return set;
256         }
257         case iro_Proj: {
258                 ir_node *pred = get_Proj_pred(node);
259
260                 if (is_Cmp(pred)) {
261                         ir_node *left  = get_Cmp_left(pred);
262                         ir_node *right = get_Cmp_right(pred);
263                         ir_mode *cmp_mode  = get_irn_mode(left);
264                         ir_node *set;
265
266                         if ((mode_is_int(cmp_mode) || mode_is_reference(cmp_mode)) && (
267                                                 get_mode_size_bits(cmp_mode) < get_mode_size_bits(mode) ||
268                                                 (mode_is_signed(cmp_mode) && is_Const(right) && is_Const_null(right))
269                                         )) {
270                                 int      pnc      = get_Proj_proj(node);
271                                 int      need_not = 0;
272                                 ir_node *a        = NULL;
273                                 ir_node *b        = NULL;
274
275                                 if (pnc == pn_Cmp_Lt) {
276                                         /* a < b  ->  (a - b) >> 31 */
277                                         a = left;
278                                         b = right;
279                                 } else if (pnc == pn_Cmp_Le) {
280                                         /* a <= b  -> ~(a - b) >> 31 */
281                                         a        = right;
282                                         b        = left;
283                                         need_not = 1;
284                                 } else if (pnc == pn_Cmp_Gt) {
285                                         /* a > b   -> (b - a) >> 31 */
286                                         a = right;
287                                         b = left;
288                                 } else if (pnc == pn_Cmp_Ge) {
289                                         /* a >= b   -> ~(a - b) >> 31 */
290                                         a        = left;
291                                         b        = right;
292                                         need_not = 1;
293                                 }
294
295                                 if (a != NULL) {
296                                         int      bits      = get_mode_size_bits(mode);
297                                         tarval  *tv        = new_tarval_from_long(bits-1, mode_Iu);
298                                         ir_node *shift_cnt = new_d_Const(dbgi, tv);
299
300                                         if (cmp_mode != mode) {
301                                                 a = new_rd_Conv(dbgi, block, a, mode);
302                                                 b = new_rd_Conv(dbgi, block, b, mode);
303                                         }
304
305                                         res = new_rd_Sub(dbgi, block, a, b, mode);
306                                         if (need_not) {
307                                                 res = new_rd_Not(dbgi, block, res, mode);
308                                         }
309                                         res = new_rd_Shr(dbgi, block, res, shift_cnt, mode);
310
311                                         set_irn_link(node, res);
312                                         pdeq_putr(lowered_nodes, node);
313                                         return res;
314                                 }
315                         }
316
317                         /* synthesize the 0/1 value */
318                         set = create_set(node);
319                         set_irn_link(node, set);
320                         pdeq_putr(lowered_nodes, node);
321                         return set;
322                 } else if (is_Proj(pred) && is_Call(get_Proj_pred(pred))) {
323                         ir_type   *type   = get_Call_type(get_Proj_pred(pred));
324                         adjust_method_type(type);
325                         set_irn_mode(node, mode);
326                         return node;
327                 } else if (is_Proj(pred) && is_Start(get_Proj_pred(pred))) {
328                         ir_entity *entity = get_irg_entity(current_ir_graph);
329                         ir_type   *type   = get_entity_type(entity);
330                         adjust_method_type(type);
331                         set_irn_mode(node, mode);
332                         return node;
333                 }
334
335                 panic("unexpected projb: %+F (pred: %+F)", node, pred);
336         }
337         case iro_Const: {
338                 tarval *tv = get_Const_tarval(node);
339                 if (tv == get_tarval_b_true()) {
340                         tarval  *tv_one  = get_tarval_one(mode);
341                         res              = new_d_Const(dbgi, tv_one);
342                 } else if (tv == get_tarval_b_false()) {
343                         tarval  *tv_zero = get_tarval_null(mode);
344                         res              = new_d_Const(dbgi, tv_zero);
345                 } else {
346                         panic("invalid boolean const %+F", node);
347                 }
348                 set_irn_link(node, res);
349                 pdeq_putr(lowered_nodes, node);
350                 return res;
351         }
352         case iro_Unknown:
353                 return new_Unknown(config.lowered_mode);
354         default:
355                 panic("didn't expect %+F to have mode_b", node);
356         }
357 }
358
359 static void lower_mode_b_walker(ir_node *node, void *env)
360 {
361         int i, arity;
362         bool changed = 0;
363         (void) env;
364
365         arity = get_irn_arity(node);
366         for (i = 0; i < arity; ++i) {
367                 ir_node *lowered_in;
368                 ir_node *in = get_irn_n(node, i);
369                 if (get_irn_mode(in) != mode_b)
370                         continue;
371
372                 if (! config.lower_direct_cmp) {
373                         /* Proj(Cmp) as input for Cond and Mux nodes needs no changes.
374                            (Mux with mode_b is an exception as it gets replaced by and/or
375                             anyway so we still lower the inputs then) */
376                         if (is_Cond(node) ||
377                             (is_Mux(node) && get_irn_mode(node) != mode_b)) {
378                                 if (is_Proj(in)) {
379                                         ir_node *pred = get_Proj_pred(in);
380                                         if (is_Cmp(pred))
381                                                 continue;
382                                 }
383                         }
384                 }
385
386                 lowered_in = lower_node(in);
387
388                 if (is_Call(node)) {
389                         ir_type *type = get_Call_type(node);
390                         adjust_method_type(type);
391                 } else if (is_Cond(node) || (is_Mux(node) && i == 0)) {
392                         lowered_in = create_convb(lowered_in);
393                 }
394                 set_irn_n(node, i, lowered_in);
395                 changed = true;
396         }
397         if (changed) {
398                 add_identities(current_ir_graph->value_table, node);
399         }
400 }
401
402 static void clear_links(ir_node *node, void *env)
403 {
404         (void) env;
405         set_irn_link(node, NULL);
406 }
407
408 void ir_lower_mode_b(ir_graph *irg, const lower_mode_b_config_t *nconfig)
409 {
410         ir_entity *entity = get_irg_entity(irg);
411         ir_type   *type   = get_entity_type(entity);
412
413         config        = *nconfig;
414         lowered_nodes = new_pdeq();
415         lowered_type  = NULL;
416
417         /* ensure no optimisation touches muxes anymore */
418         set_irg_state(irg, IR_GRAPH_STATE_KEEP_MUX);
419
420         ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
421
422         adjust_method_type(type);
423
424         set_opt_allow_conv_b(0);
425         irg_walk_graph(irg, clear_links, NULL, NULL);
426         irg_walk_graph(irg, lower_mode_b_walker, NULL, NULL);
427
428         while(!pdeq_empty(lowered_nodes)) {
429                 ir_node *node = (ir_node*) pdeq_getr(lowered_nodes);
430                 maybe_kill_node(node);
431         }
432         del_pdeq(lowered_nodes);
433
434         ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
435 }
436
437 struct pass_t {
438         ir_graph_pass_t             pass;
439         const lower_mode_b_config_t *config;
440 };
441
442 /**
443  * Wrapper to run ir_lower_mode_b() as an ir_graph pass
444  */
445 static int pass_wrapper(ir_graph *irg, void *context)
446 {
447         struct pass_t *pass = context;
448
449         ir_lower_mode_b(irg, pass->config);
450         return 0;
451 }
452
453 ir_graph_pass_t *ir_lower_mode_b_pass(
454         const char *name, const lower_mode_b_config_t *config)
455 {
456         struct pass_t *pass = XMALLOCZ(struct pass_t);
457
458         pass->config = config;
459         return def_graph_pass_constructor(
460                 &pass->pass, name ? name : "lower_mode_b", pass_wrapper);
461 }