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