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