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