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