Correct the NOT+ADC transformation.
[libfirm] / ir / be / ia32 / ia32_finish.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   This file implements functions to finalize the irg for emit.
23  * @author  Christian Wuerdig
24  * @version $Id$
25  */
26 #include "config.h"
27
28 #include "irnode.h"
29 #include "ircons.h"
30 #include "irgmod.h"
31 #include "irgwalk.h"
32 #include "iredges.h"
33 #include "irprintf.h"
34 #include "pdeq.h"
35 #include "error.h"
36
37 #include "../bearch.h"
38 #include "../besched.h"
39 #include "../benode.h"
40
41 #include "bearch_ia32_t.h"
42 #include "ia32_finish.h"
43 #include "ia32_new_nodes.h"
44 #include "ia32_common_transform.h"
45 #include "ia32_transform.h"
46 #include "ia32_dbg_stat.h"
47 #include "ia32_optimize.h"
48 #include "gen_ia32_regalloc_if.h"
49
50 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
51
52 /**
53  * Transforms a Sub or xSub into Neg--Add iff OUT_REG != SRC1_REG && OUT_REG == SRC2_REG.
54  * THIS FUNCTIONS MUST BE CALLED AFTER REGISTER ALLOCATION.
55  */
56 static void ia32_transform_sub_to_neg_add(ir_node *irn)
57 {
58         ir_graph *irg;
59         ir_node *in1, *in2, *noreg, *nomem, *res;
60         ir_node *noreg_fp, *block;
61         dbg_info *dbgi;
62         const arch_register_t *in1_reg, *in2_reg, *out_reg;
63
64         /* fix_am will solve this for AddressMode variants */
65         if (get_ia32_op_type(irn) != ia32_Normal)
66                 return;
67
68         irg      = get_irn_irg(irn);
69         noreg    = ia32_new_NoReg_gp(irg);
70         noreg_fp = ia32_new_NoReg_xmm(irg);
71         nomem    = get_irg_no_mem(irg);
72         in1      = get_irn_n(irn, n_ia32_binary_left);
73         in2      = get_irn_n(irn, n_ia32_binary_right);
74         in1_reg  = arch_get_irn_register(in1);
75         in2_reg  = arch_get_irn_register(in2);
76         out_reg  = arch_irn_get_register(irn, 0);
77
78         if (out_reg == in1_reg)
79                 return;
80
81         /* in case of sub and OUT == SRC2 we can transform the sequence into neg src2 -- add */
82         if (out_reg != in2_reg)
83                 return;
84
85         block = get_nodes_block(irn);
86         dbgi   = get_irn_dbg_info(irn);
87
88         /* generate the neg src2 */
89         if (is_ia32_xSub(irn)) {
90                 int size;
91                 ir_entity *entity;
92                 ir_mode *op_mode = get_ia32_ls_mode(irn);
93
94                 assert(get_irn_mode(irn) != mode_T);
95
96                 res = new_bd_ia32_xXor(dbgi, block, noreg, noreg, nomem, in2, noreg_fp);
97                 size = get_mode_size_bits(op_mode);
98                 entity = ia32_gen_fp_known_const(size == 32 ? ia32_SSIGN : ia32_DSIGN);
99                 set_ia32_am_sc(res, entity);
100                 set_ia32_op_type(res, ia32_AddrModeS);
101                 set_ia32_ls_mode(res, op_mode);
102
103                 arch_set_irn_register(res, in2_reg);
104
105                 /* add to schedule */
106                 sched_add_before(irn, res);
107
108                 /* generate the add */
109                 res = new_bd_ia32_xAdd(dbgi, block, noreg, noreg, nomem, res, in1);
110                 set_ia32_ls_mode(res, get_ia32_ls_mode(irn));
111         } else {
112                 ir_node         *flags_proj = NULL;
113                 ir_node         *carry;
114                 const ir_edge_t *edge;
115
116                 if (get_irn_mode(irn) == mode_T) {
117                         /* collect the Proj uses */
118                         foreach_out_edge(irn, edge) {
119                                 ir_node *proj = get_edge_src_irn(edge);
120                                 long     pn   = get_Proj_proj(proj);
121                                 if (pn == pn_ia32_flags) {
122                                         assert(flags_proj == NULL);
123                                         flags_proj = proj;
124                                         break;
125                                 }
126                         }
127                 }
128
129                 if (is_ia32_Sbb(irn)) {
130                         /* Feed borrow (in CF) as carry (via CMC) into NOT+ADC. */
131                         carry = get_irn_n(irn, n_ia32_Sbb_eflags);
132                         carry = new_bd_ia32_Cmc(dbgi, block, carry);
133                         goto carry;
134                 } else if (flags_proj != 0) {
135                         /*
136                          * ARG, the above technique does NOT set the flags right.
137                          * So, we must produce the following code:
138                          * t1 = ~b
139                          * t2 = a + ~b + Carry
140                          * Complement Carry
141                          *
142                          * a + -b = a + (~b + 1)  would set the carry flag wrong IFF both a and b are zero.
143                          */
144                         ir_node *cmc;
145                         ir_node *nnot;
146                         ir_node *adc;
147                         ir_node *adc_flags;
148
149                         carry = new_bd_ia32_Stc(dbgi, block);
150
151 carry:
152                         nnot = new_bd_ia32_Not(dbgi, block, in2);
153                         arch_set_irn_register(nnot, in2_reg);
154                         sched_add_before(irn, nnot);
155
156                         arch_set_irn_register(carry, &ia32_registers[REG_EFLAGS]);
157                         sched_add_before(irn, carry);
158
159                         adc = new_bd_ia32_Adc(dbgi, block, noreg, noreg, nomem, nnot, in1, carry);
160                         arch_set_irn_register(adc, out_reg);
161                         set_ia32_commutative(adc);
162
163                         if (flags_proj != NULL) {
164                                 adc_flags = new_r_Proj(adc, mode_Iu, pn_ia32_Adc_flags);
165                                 arch_set_irn_register(adc_flags, &ia32_registers[REG_EFLAGS]);
166
167                                 cmc = new_bd_ia32_Cmc(dbgi, block, adc_flags);
168                                 arch_set_irn_register(cmc, &ia32_registers[REG_EFLAGS]);
169                                 sched_add_after(irn, cmc);
170                                 exchange(flags_proj, cmc);
171                         }
172
173                         res = adc;
174                 } else {
175                         res = new_bd_ia32_Neg(dbgi, block, in2);
176                         arch_set_irn_register(res, in2_reg);
177
178                         /* add to schedule */
179                         sched_add_before(irn, res);
180
181                         /* generate the add */
182                         res = new_bd_ia32_Add(dbgi, block, noreg, noreg, nomem, res, in1);
183                         arch_set_irn_register(res, out_reg);
184                         set_ia32_commutative(res);
185                 }
186         }
187
188         /* exchange the add and the sub */
189         edges_reroute(irn, res);
190         sched_add_before(irn, res);
191
192         set_irn_mode(res, get_irn_mode(irn));
193
194         SET_IA32_ORIG_NODE(res, irn);
195
196         /* remove the old sub */
197         sched_remove(irn);
198         kill_node(irn);
199
200         DBG_OPT_SUB2NEGADD(irn, res);
201 }
202
203 static inline int need_constraint_copy(ir_node *irn)
204 {
205         /* TODO this should be determined from the node specification */
206         switch (get_ia32_irn_opcode(irn)) {
207                 case iro_ia32_IMul: {
208                         /* the 3 operand form of IMul needs no constraint copy */
209                         ir_node *right = get_irn_n(irn, n_ia32_IMul_right);
210                         return !is_ia32_Immediate(right);
211                 }
212
213                 case iro_ia32_Lea:
214                 case iro_ia32_Conv_I2I:
215                 case iro_ia32_Conv_I2I8Bit:
216                 case iro_ia32_CMovcc:
217                         return 0;
218
219                 default:
220                         return 1;
221         }
222 }
223
224 /**
225  * Returns the index of the "same" register.
226  * On the x86, we should have only one.
227  */
228 static int get_first_same(const arch_register_req_t* req)
229 {
230         const unsigned other = req->other_same;
231         int i;
232
233         for (i = 0; i < 32; ++i) {
234                 if (other & (1U << i)) return i;
235         }
236         panic("same position not found");
237 }
238
239 /**
240  * Insert copies for all ia32 nodes where the should_be_same requirement
241  * is not fulfilled.
242  * Transform Sub into Neg -- Add if IN2 == OUT
243  */
244 static void assure_should_be_same_requirements(ir_node *node)
245 {
246         const arch_register_t      *out_reg, *in_reg;
247         int                         n_res, i;
248         ir_node                    *in_node, *block;
249
250         n_res = arch_irn_get_n_outs(node);
251         block = get_nodes_block(node);
252
253         /* check all OUT requirements, if there is a should_be_same */
254         for (i = 0; i < n_res; i++) {
255                 int                          i2, arity;
256                 int                          same_pos;
257                 ir_node                     *uses_out_reg;
258                 const arch_register_req_t   *req = arch_get_out_register_req(node, i);
259                 const arch_register_class_t *cls;
260                 int                         uses_out_reg_pos;
261
262                 if (!arch_register_req_is(req, should_be_same))
263                         continue;
264
265                 same_pos = get_first_same(req);
266
267                 /* get in and out register */
268                 out_reg = arch_irn_get_register(node, i);
269                 in_node = get_irn_n(node, same_pos);
270                 in_reg  = arch_get_irn_register(in_node);
271
272                 /* requirement already fulfilled? */
273                 if (in_reg == out_reg)
274                         continue;
275                 cls = arch_register_get_class(in_reg);
276                 assert(cls == arch_register_get_class(out_reg));
277
278                 /* check if any other input operands uses the out register */
279                 arity = get_irn_arity(node);
280                 uses_out_reg     = NULL;
281                 uses_out_reg_pos = -1;
282                 for (i2 = 0; i2 < arity; ++i2) {
283                         ir_node               *in     = get_irn_n(node, i2);
284                         const arch_register_t *other_in_reg;
285
286                         if (!mode_is_data(get_irn_mode(in)))
287                                 continue;
288
289                         other_in_reg = arch_get_irn_register(in);
290
291                         if (other_in_reg != out_reg)
292                                 continue;
293
294                         if (uses_out_reg != NULL && in != uses_out_reg) {
295                                 panic("invalid register allocation");
296                         }
297                         uses_out_reg = in;
298                         if (uses_out_reg_pos >= 0)
299                                 uses_out_reg_pos = -1; /* multiple inputs... */
300                         else
301                                 uses_out_reg_pos = i2;
302                 }
303
304                 /* no-one else is using the out reg, we can simply copy it
305                  * (the register can't be live since the operation will override it
306                  *  anyway) */
307                 if (uses_out_reg == NULL) {
308                         ir_node *copy = be_new_Copy(cls, block, in_node);
309                         DBG_OPT_2ADDRCPY(copy);
310
311                         /* destination is the out register */
312                         arch_set_irn_register(copy, out_reg);
313
314                         /* insert copy before the node into the schedule */
315                         sched_add_before(node, copy);
316
317                         /* set copy as in */
318                         set_irn_n(node, same_pos, copy);
319
320                         DBG((dbg, LEVEL_1,
321                                 "created copy %+F for should be same argument at input %d of %+F\n",
322                                 copy, same_pos, node));
323                         continue;
324                 }
325
326                 /* for commutative nodes we can simply swap the left/right */
327                 if (uses_out_reg_pos == n_ia32_binary_right && is_ia32_commutative(node)) {
328                         ia32_swap_left_right(node);
329                         DBG((dbg, LEVEL_1,
330                                 "swapped left/right input of %+F to resolve should be same constraint\n",
331                                 node));
332                         continue;
333                 }
334
335                 panic("Unresolved should_be_same constraint");
336         }
337 }
338
339 /**
340  * Following Problem:
341  * We have a source address mode node with base or index register equal to
342  * result register and unfulfilled should_be_same requirement. The constraint
343  * handler will insert a copy from the remaining input operand to the result
344  * register -> base or index is broken then.
345  * Solution: Turn back this address mode into explicit Load + Operation.
346  */
347 static void fix_am_source(ir_node *irn)
348 {
349         int                         n_res, i;
350
351         /* check only ia32 nodes with source address mode */
352         if (!is_ia32_irn(irn) || get_ia32_op_type(irn) != ia32_AddrModeS)
353                 return;
354         /* only need to fix binary operations */
355         if (get_ia32_am_support(irn) != ia32_am_binary)
356                 return;
357
358         n_res = arch_irn_get_n_outs(irn);
359
360         for (i = 0; i < n_res; i++) {
361                 const arch_register_req_t *req = arch_get_out_register_req(irn, i);
362                 const arch_register_t     *out_reg;
363                 int                        same_pos;
364                 ir_node                   *same_node;
365                 const arch_register_t     *same_reg;
366                 ir_node                   *load_res;
367
368                 if (!arch_register_req_is(req, should_be_same))
369                         continue;
370
371                 /* get in and out register */
372                 out_reg   = arch_irn_get_register(irn, i);
373                 same_pos  = get_first_same(req);
374                 same_node = get_irn_n(irn, same_pos);
375                 same_reg  = arch_get_irn_register(same_node);
376
377                 /* should_be same constraint is fullfilled, nothing to do */
378                 if (out_reg == same_reg)
379                         continue;
380
381                 /* we only need to do something if the out reg is the same as base
382                          or index register */
383                 if (out_reg != arch_get_irn_register(get_irn_n(irn, n_ia32_base)) &&
384                                 out_reg != arch_get_irn_register(get_irn_n(irn, n_ia32_index)))
385                         continue;
386
387                 load_res = ia32_turn_back_am(irn);
388                 arch_set_irn_register(load_res, out_reg);
389
390                 DBG((dbg, LEVEL_3,
391                         "irg %+F: build back AM source for node %+F, inserted load %+F\n",
392                         get_irn_irg(irn), irn, get_Proj_pred(load_res)));
393                 break;
394         }
395 }
396
397 /**
398  * Block walker: finishes a block
399  */
400 static void ia32_finish_irg_walker(ir_node *block, void *env)
401 {
402         ir_node *irn, *next;
403         (void) env;
404
405         /* first: turn back AM source if necessary */
406         for (irn = sched_first(block); ! sched_is_end(irn); irn = next) {
407                 next = sched_next(irn);
408                 fix_am_source(irn);
409         }
410
411         for (irn = sched_first(block); ! sched_is_end(irn); irn = next) {
412                 next = sched_next(irn);
413
414                 /* check if there is a sub which need to be transformed */
415                 if (is_ia32_Sub(irn) || is_ia32_Sbb(irn) || is_ia32_xSub(irn)) {
416                         ia32_transform_sub_to_neg_add(irn);
417                 }
418         }
419
420         /* second: insert copies and finish irg */
421         for (irn = sched_first(block); ! sched_is_end(irn); irn = next) {
422                 next = sched_next(irn);
423                 if (is_ia32_irn(irn)) {
424                         /* some nodes are just a bit less efficient, but need no fixing if the
425                          * should be same requirement is not fulfilled */
426                         if (need_constraint_copy(irn))
427                                 assure_should_be_same_requirements(irn);
428                 }
429         }
430 }
431
432 /**
433  * Block walker: pushes all blocks on a wait queue
434  */
435 static void ia32_push_on_queue_walker(ir_node *block, void *env)
436 {
437         waitq *wq = (waitq*)env;
438         waitq_put(wq, block);
439 }
440
441
442 /**
443  * Add Copy nodes for not fulfilled should_be_equal constraints
444  */
445 void ia32_finish_irg(ir_graph *irg)
446 {
447         waitq *wq = new_waitq();
448
449         /* Push the blocks on the waitq because ia32_finish_irg_walker starts more walks ... */
450         irg_block_walk_graph(irg, NULL, ia32_push_on_queue_walker, wq);
451
452         while (! waitq_empty(wq)) {
453                 ir_node *block = (ir_node*)waitq_get(wq);
454                 ia32_finish_irg_walker(block, NULL);
455         }
456         del_waitq(wq);
457 }
458
459 void ia32_init_finish(void)
460 {
461         FIRM_DBG_REGISTER(dbg, "firm.be.ia32.finish");
462 }