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