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