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