fix lea to add transformation
[libfirm] / ir / be / ia32 / ia32_finish.c
1 /*
2  * Copyright (C) 1995-2007 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 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include "irnode.h"
31 #include "ircons.h"
32 #include "irgmod.h"
33 #include "irgwalk.h"
34 #include "iredges.h"
35 #include "pdeq.h"
36 #include "error.h"
37
38 #include "../bearch_t.h"
39 #include "../besched_t.h"
40 #include "../benode_t.h"
41
42 #include "bearch_ia32_t.h"
43 #include "ia32_finish.h"
44 #include "ia32_new_nodes.h"
45 #include "ia32_map_regs.h"
46 #include "ia32_transform.h"
47 #include "ia32_dbg_stat.h"
48 #include "ia32_optimize.h"
49 #include "gen_ia32_regalloc_if.h"
50
51 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
52
53 /**
54  * Transforms a Sub or xSub into Neg--Add iff OUT_REG == SRC2_REG.
55  * THIS FUNCTIONS MUST BE CALLED AFTER REGISTER ALLOCATION.
56  */
57 static void ia32_transform_sub_to_neg_add(ir_node *irn, ia32_code_gen_t *cg) {
58         ir_graph *irg;
59         ir_node *in1, *in2, *noreg, *nomem, *res;
60         ir_node *noreg_fp, *block;
61         ir_mode *mode = get_irn_mode(irn);
62         dbg_info *dbg = get_irn_dbg_info(irn);
63         const arch_register_t *in1_reg, *in2_reg, *out_reg, **slots;
64         int i, arity;
65
66         /* Return if AM node or not a Sub or xSub */
67         if (!(is_ia32_Sub(irn) || is_ia32_xSub(irn)) || get_ia32_op_type(irn) != ia32_Normal)
68                 return;
69
70         noreg   = ia32_new_NoReg_gp(cg);
71         noreg_fp = ia32_new_NoReg_fp(cg);
72         nomem   = new_rd_NoMem(cg->irg);
73         in1     = get_irn_n(irn, 2);
74         in2     = get_irn_n(irn, 3);
75         in1_reg = arch_get_irn_register(cg->arch_env, in1);
76         in2_reg = arch_get_irn_register(cg->arch_env, in2);
77         out_reg = get_ia32_out_reg(irn, 0);
78
79         irg     = cg->irg;
80         block   = get_nodes_block(irn);
81
82         /* in case of sub and OUT == SRC2 we can transform the sequence into neg src2 -- add */
83         if (!REGS_ARE_EQUAL(out_reg, in2_reg))
84                 return;
85
86         /* generate the neg src2 */
87         if(mode_is_float(mode)) {
88                 int size;
89                 ir_entity *entity;
90
91                 res = new_rd_ia32_xXor(dbg, irg, block, noreg, noreg, in2, noreg_fp, nomem);
92                 size = get_mode_size_bits(mode);
93                 entity = ia32_gen_fp_known_const(size == 32 ? ia32_SSIGN : ia32_DSIGN);
94                 set_ia32_am_sc(res, entity);
95                 set_ia32_op_type(res, ia32_AddrModeS);
96                 set_ia32_ls_mode(res, get_ia32_ls_mode(irn));
97         } else {
98                 res = new_rd_ia32_Neg(dbg, irg, block, noreg, noreg, in2, nomem);
99         }
100         arch_set_irn_register(cg->arch_env, res, in2_reg);
101
102         /* add to schedule */
103         sched_add_before(irn, res);
104
105         /* generate the add */
106         if (mode_is_float(mode)) {
107                 res = new_rd_ia32_xAdd(dbg, irg, block, noreg, noreg, res, in1, nomem);
108                 set_ia32_am_support(res, ia32_am_Source, ia32_am_binary);
109                 set_ia32_ls_mode(res, get_ia32_ls_mode(irn));
110         }
111         else {
112                 res = new_rd_ia32_Add(dbg, irg, block, noreg, noreg, res, in1, nomem);
113                 set_ia32_am_support(res, ia32_am_Full, ia32_am_binary);
114                 set_ia32_commutative(res);
115         }
116
117         SET_IA32_ORIG_NODE(res, ia32_get_old_node_name(cg, irn));
118         /* copy register */
119         slots    = get_ia32_slots(res);
120         slots[0] = in2_reg;
121
122         /* exchange the add and the sub */
123         edges_reroute(irn, res, irg);
124
125         /* add to schedule */
126         sched_add_before(irn, res);
127
128         /* remove the old sub */
129         sched_remove(irn);
130         arity = get_irn_arity(irn);
131         for(i = 0; i < arity; ++i) {
132                 set_irn_n(irn, i, new_Bad());
133         }
134
135         DBG_OPT_SUB2NEGADD(irn, res);
136 }
137
138 /**
139  * Transforms a LEA into an Add or SHL if possible.
140  * THIS FUNCTIONS MUST BE CALLED AFTER REGISTER ALLOCATION.
141  */
142 static void ia32_transform_lea_to_add_or_shl(ir_node *irn, ia32_code_gen_t *cg) {
143         ia32_am_flavour_t am_flav;
144         dbg_info         *dbg = get_irn_dbg_info(irn);
145         ir_graph         *irg;
146         ir_node          *res = NULL;
147         ir_node          *nomem, *noreg, *base, *index, *op1, *op2;
148         ir_node          *block;
149         long              offs = 0;
150         const arch_register_t *out_reg, *base_reg, *index_reg;
151
152         /* must be a LEA */
153         if (! is_ia32_Lea(irn))
154                 return;
155
156         am_flav = get_ia32_am_flavour(irn);
157
158         /* mustn't have a symconst */
159         if (get_ia32_am_sc(irn) != NULL || get_ia32_frame_ent(irn) != NULL)
160                 return;
161
162         if (am_flav == ia32_am_IS) {
163                 tarval *tv;
164
165                 /* Create a SHL */
166                 noreg     = ia32_new_NoReg_gp(cg);
167                 nomem     = new_rd_NoMem(cg->irg);
168                 index     = get_irn_n(irn, 1);
169                 index_reg = arch_get_irn_register(cg->arch_env, index);
170                 out_reg   = arch_get_irn_register(cg->arch_env, irn);
171
172                 if (! REGS_ARE_EQUAL(out_reg, index_reg))
173                         return;
174
175                 /* ok, we can transform it */
176                 irg = cg->irg;
177                 block = get_nodes_block(irn);
178
179                 res  = new_rd_ia32_Shl(dbg, irg, block, noreg, noreg, index, noreg, nomem);
180                 offs = get_ia32_am_scale(irn);
181                 tv = new_tarval_from_long(offs, mode_Iu);
182                 set_ia32_Immop_tarval(res, tv);
183                 arch_set_irn_register(cg->arch_env, res, out_reg);
184         } else {
185                 /* only some LEAs can be transformed to an Add */
186                 if (am_flav != ia32_am_B && am_flav != ia32_am_OB && am_flav != ia32_am_BI)
187                         return;
188
189                 noreg = ia32_new_NoReg_gp(cg);
190                 nomem = new_rd_NoMem(cg->irg);
191                 op1   = noreg;
192                 op2   = noreg;
193                 base  = get_irn_n(irn, 0);
194                 index = get_irn_n(irn, 1);
195                 offs  = get_ia32_am_offs_int(irn);
196
197                 out_reg   = arch_get_irn_register(cg->arch_env, irn);
198                 base_reg  = arch_get_irn_register(cg->arch_env, base);
199                 index_reg = arch_get_irn_register(cg->arch_env, index);
200
201                 irg = cg->irg;
202                 block = get_nodes_block(irn);
203
204                 switch(am_flav) {
205                         case ia32_am_B:
206                         case ia32_am_OB:
207                                 /* out register must be same as base register */
208                                 if (! REGS_ARE_EQUAL(out_reg, base_reg))
209                                         return;
210
211                                 op1 = base;
212                                 op2 = new_rd_ia32_Immediate(NULL, irg, block, NULL, 0, offs);
213                                 arch_set_irn_register(cg->arch_env, op2,
214                                                       &ia32_gp_regs[REG_GP_NOREG]);
215                                 break;
216                         case ia32_am_BI:
217                                 assert(offs == 0);
218                                 /* out register must be same as one in register */
219                                 if (REGS_ARE_EQUAL(out_reg, base_reg)) {
220                                         op1 = base;
221                                         op2 = index;
222                                 } else if (REGS_ARE_EQUAL(out_reg, index_reg)) {
223                                         op1 = index;
224                                         op2 = base;
225                                 } else {
226                                         /* in registers a different from out -> no Add possible */
227                                         return;
228                                 }
229                                 break;
230
231                         default:
232                                 assert(0);
233                                 break;
234                 }
235
236                 res = new_rd_ia32_Add(dbg, irg, block, noreg, noreg, op1, op2, nomem);
237                 arch_set_irn_register(cg->arch_env, res, out_reg);
238                 set_ia32_op_type(res, ia32_Normal);
239                 set_ia32_commutative(res);
240         }
241
242         SET_IA32_ORIG_NODE(res, ia32_get_old_node_name(cg, irn));
243
244         /* add new ADD/SHL to schedule */
245         sched_add_before(irn, res);
246
247         DBG_OPT_LEA2ADD(irn, res);
248
249         /* remove the old LEA */
250         sched_remove(irn);
251
252         /* exchange the Add and the LEA */
253         exchange(irn, res);
254 }
255
256 static INLINE int need_constraint_copy(ir_node *irn) {
257         return  ! is_ia32_Lea(irn)          &&
258                 ! is_ia32_Conv_I2I(irn)     &&
259                 ! is_ia32_Conv_I2I8Bit(irn) &&
260                 ! is_ia32_CmpCMov(irn)      &&
261                 ! is_ia32_CmpSet(irn);
262 }
263
264 /**
265  * Insert copies for all ia32 nodes where the should_be_same requirement
266  * is not fulfilled.
267  * Transform Sub into Neg -- Add if IN2 == OUT
268  */
269 static void ia32_finish_node(ir_node *irn, void *env) {
270         ia32_code_gen_t            *cg = env;
271         const arch_register_req_t **reqs;
272         const arch_register_t      *out_reg, *in_reg, *in2_reg;
273         int                         n_res, i;
274         ir_node                    *copy, *in_node, *block, *in2_node;
275         ia32_op_type_t              op_tp;
276
277         if (is_ia32_irn(irn)) {
278                 /* AM Dest nodes don't produce any values  */
279                 op_tp = get_ia32_op_type(irn);
280                 if (op_tp == ia32_AddrModeD)
281                         goto end;
282
283                 reqs  = get_ia32_out_req_all(irn);
284                 n_res = get_ia32_n_res(irn);
285                 block = get_nodes_block(irn);
286
287                 /* check all OUT requirements, if there is a should_be_same */
288                 if ((op_tp == ia32_Normal || op_tp == ia32_AddrModeS) && need_constraint_copy(irn))
289                 {
290                         for (i = 0; i < n_res; i++) {
291                                 if (arch_register_req_is(reqs[i], should_be_same)) {
292                                         int same_pos = reqs[i]->other_same;
293
294                                         /* get in and out register */
295                                         out_reg  = get_ia32_out_reg(irn, i);
296                                         in_node  = get_irn_n(irn, same_pos);
297                                         in_reg   = arch_get_irn_register(cg->arch_env, in_node);
298
299                                         /* don't copy ignore nodes */
300                                         if (arch_irn_is(cg->arch_env, in_node, ignore) && is_Proj(in_node))
301                                                 continue;
302
303                                         /* check if in and out register are equal */
304                                         if (! REGS_ARE_EQUAL(out_reg, in_reg)) {
305                                                 /* in case of a commutative op: just exchange the in's */
306                                                 /* beware: the current op could be everything, so test for ia32 */
307                                                 /*         commutativity first before getting the second in     */
308                                                 if (is_ia32_commutative(irn)) {
309                                                         in2_node = get_irn_n(irn, same_pos ^ 1);
310                                                         in2_reg  = arch_get_irn_register(cg->arch_env, in2_node);
311
312                                                         if (REGS_ARE_EQUAL(out_reg, in2_reg)) {
313                                                                 set_irn_n(irn, same_pos, in2_node);
314                                                                 set_irn_n(irn, same_pos ^ 1, in_node);
315                                                         }
316                                                         else
317                                                                 goto insert_copy;
318                                                 }
319                                                 else {
320 insert_copy:
321                                                         DBG((dbg, LEVEL_1, "inserting copy for %+F in_pos %d\n", irn, same_pos));
322                                                         /* create copy from in register */
323                                                         copy = be_new_Copy(arch_register_get_class(in_reg), cg->irg, block, in_node);
324
325                                                         DBG_OPT_2ADDRCPY(copy);
326
327                                                         /* destination is the out register */
328                                                         arch_set_irn_register(cg->arch_env, copy, out_reg);
329
330                                                         /* insert copy before the node into the schedule */
331                                                         sched_add_before(irn, copy);
332
333                                                         /* set copy as in */
334                                                         set_irn_n(irn, same_pos, copy);
335                                                 }
336                                         }
337                                 }
338                         }
339                 }
340
341                 /* check xCmp: try to avoid unordered cmp */
342                 if ((is_ia32_xCmp(irn) || is_ia32_xCmpCMov(irn) || is_ia32_xCmpSet(irn)) &&
343                         op_tp == ia32_Normal    &&
344                         ! is_ia32_ImmConst(irn) && ! is_ia32_ImmSymConst(irn))
345                 {
346                         long pnc = get_ia32_pncode(irn);
347
348                         if (pnc & pn_Cmp_Uo) {
349                                 ir_node *tmp;
350                                 int idx1 = 2, idx2 = 3;
351
352                                 if (is_ia32_xCmpCMov(irn)) {
353                                         idx1 = 0;
354                                         idx2 = 1;
355                                 }
356
357                                 tmp = get_irn_n(irn, idx1);
358                                 set_irn_n(irn, idx1, get_irn_n(irn, idx2));
359                                 set_irn_n(irn, idx2, tmp);
360
361                                 set_ia32_pncode(irn, get_negated_pnc(pnc, mode_E));
362                         }
363                 }
364         }
365 end: ;
366 }
367
368 /**
369  * Following Problem:
370  * We have a source address mode node with base or index register equal to
371  * result register. The constraint handler will insert a copy from the
372  * remaining input operand to the result register -> base or index is
373  * broken then.
374  * Solution: Turn back this address mode into explicit Load + Operation.
375  */
376 static void fix_am_source(ir_node *irn, void *env) {
377         ia32_code_gen_t *cg = env;
378         ir_node *base, *index, *noreg;
379         const arch_register_t *reg_base, *reg_index;
380         const arch_register_req_t **reqs;
381         int n_res, i;
382
383         /* check only ia32 nodes with source address mode */
384         if (! is_ia32_irn(irn) || get_ia32_op_type(irn) != ia32_AddrModeS)
385                 return;
386         /* no need to fix unary operations */
387         if (get_irn_arity(irn) == 4)
388                 return;
389
390         base  = get_irn_n(irn, 0);
391         index = get_irn_n(irn, 1);
392
393         reg_base  = arch_get_irn_register(cg->arch_env, base);
394         reg_index = arch_get_irn_register(cg->arch_env, index);
395         reqs      = get_ia32_out_req_all(irn);
396
397         noreg = ia32_new_NoReg_gp(cg);
398
399         n_res = get_ia32_n_res(irn);
400
401         for (i = 0; i < n_res; i++) {
402                 if (arch_register_req_is(reqs[i], should_be_same)) {
403                         /* get in and out register */
404                         const arch_register_t *out_reg  = get_ia32_out_reg(irn, i);
405                         int same_pos = reqs[i]->other_same;
406
407                         /*
408                                 there is a constraint for the remaining operand
409                                 and the result register is equal to base or index register
410                         */
411                         if (same_pos == 2 &&
412                                 (REGS_ARE_EQUAL(out_reg, reg_base) || REGS_ARE_EQUAL(out_reg, reg_index)))
413                         {
414                                 /* turn back address mode */
415                                 ir_node               *in_node = get_irn_n(irn, 2);
416                                 const arch_register_t *in_reg  = arch_get_irn_register(cg->arch_env, in_node);
417                                 ir_node               *block   = get_nodes_block(irn);
418                                 ir_mode               *ls_mode = get_ia32_ls_mode(irn);
419                                 ir_node *load;
420                                 int pnres;
421
422                                 if (arch_register_get_class(in_reg) == &ia32_reg_classes[CLASS_ia32_gp]) {
423                                         load  = new_rd_ia32_Load(NULL, cg->irg, block, base, index, get_irn_n(irn, 4));
424                                         pnres = pn_ia32_Load_res;
425                                 }
426                                 else if (arch_register_get_class(in_reg) == &ia32_reg_classes[CLASS_ia32_xmm]) {
427                                         load  = new_rd_ia32_xLoad(NULL, cg->irg, block, base, index, get_irn_n(irn, 4));
428                                         pnres = pn_ia32_xLoad_res;
429                                 }
430                                 else {
431                                         panic("cannot turn back address mode for this register class");
432                                 }
433
434                                 /* copy address mode information to load */
435                                 set_ia32_ls_mode(load, ls_mode);
436                                 set_ia32_am_flavour(load, get_ia32_am_flavour(irn));
437                                 set_ia32_op_type(load, ia32_AddrModeS);
438                                 set_ia32_am_scale(load, get_ia32_am_scale(irn));
439                                 set_ia32_am_sc(load, get_ia32_am_sc(irn));
440                                 add_ia32_am_offs_int(load, get_ia32_am_offs_int(irn));
441                                 set_ia32_frame_ent(load, get_ia32_frame_ent(irn));
442
443                                 if (is_ia32_use_frame(irn))
444                                         set_ia32_use_frame(load);
445
446                                 /* insert the load into schedule */
447                                 sched_add_before(irn, load);
448
449                                 DBG((dbg, LEVEL_3, "irg %+F: build back AM source for node %+F, inserted load %+F\n", cg->irg, irn, load));
450
451                                 load = new_r_Proj(cg->irg, block, load, ls_mode, pnres);
452                                 arch_set_irn_register(cg->arch_env, load, out_reg);
453
454                                 /* insert the load result proj into schedule */
455                                 sched_add_before(irn, load);
456
457                                 /* set the new input operand */
458                                 set_irn_n(irn, 3, load);
459
460                                 /* this is a normal node now */
461                                 set_irn_n(irn, 0, noreg);
462                                 set_irn_n(irn, 1, noreg);
463                                 set_ia32_op_type(irn, ia32_Normal);
464
465                                 break;
466                         }
467                 }
468         }
469 }
470
471 /**
472  * Block walker: finishes a block
473  */
474 static void ia32_finish_irg_walker(ir_node *block, void *env) {
475         ir_node *irn, *next;
476
477         /* first: turn back AM source if necessary */
478         for (irn = sched_first(block); ! sched_is_end(irn); irn = next) {
479                 next = sched_next(irn);
480                 fix_am_source(irn, env);
481         }
482
483         for (irn = sched_first(block); ! sched_is_end(irn); irn = next) {
484                 ia32_code_gen_t *cg = env;
485
486                 next = sched_next(irn);
487
488                 /* check if there is a sub which need to be transformed */
489                 ia32_transform_sub_to_neg_add(irn, cg);
490
491                 /* transform a LEA into an Add if possible */
492                 ia32_transform_lea_to_add_or_shl(irn, cg);
493         }
494
495         /* second: insert copies and finish irg */
496         for (irn = sched_first(block); ! sched_is_end(irn); irn = next) {
497                 next = sched_next(irn);
498                 ia32_finish_node(irn, env);
499         }
500 }
501
502 /**
503  * Block walker: pushes all blocks on a wait queue
504  */
505 static void ia32_push_on_queue_walker(ir_node *block, void *env) {
506         waitq *wq = env;
507         waitq_put(wq, block);
508 }
509
510
511 /**
512  * Add Copy nodes for not fulfilled should_be_equal constraints
513  */
514 void ia32_finish_irg(ir_graph *irg, ia32_code_gen_t *cg) {
515         waitq *wq = new_waitq();
516
517         /* Push the blocks on the waitq because ia32_finish_irg_walker starts more walks ... */
518         irg_block_walk_graph(irg, NULL, ia32_push_on_queue_walker, wq);
519
520         while (! waitq_empty(wq)) {
521                 ir_node *block = waitq_get(wq);
522                 ia32_finish_irg_walker(block, cg);
523         }
524         del_waitq(wq);
525 }
526
527 void ia32_init_finish(void)
528 {
529         FIRM_DBG_REGISTER(dbg, "firm.be.ia32.finish");
530 }