2 * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief This file implements functions to finalize the irg for emit.
23 * @author Christian Wuerdig
38 #include "../bearch_t.h"
39 #include "../besched_t.h"
40 #include "../benode_t.h"
42 #include "bearch_ia32_t.h"
43 #include "ia32_finish.h"
44 #include "ia32_new_nodes.h"
45 #include "ia32_map_regs.h"
46 #include "ia32_transform.h"
47 #include "ia32_dbg_stat.h"
48 #include "ia32_optimize.h"
49 #include "gen_ia32_regalloc_if.h"
51 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
54 * Transforms a Sub or xSub into Neg--Add iff OUT_REG == SRC2_REG.
55 * THIS FUNCTIONS MUST BE CALLED AFTER REGISTER ALLOCATION.
57 static void ia32_transform_sub_to_neg_add(ir_node *irn, ia32_code_gen_t *cg) {
59 ir_node *in1, *in2, *noreg, *nomem, *res;
60 ir_node *noreg_fp, *block;
61 ir_mode *mode = get_irn_mode(irn);
62 dbg_info *dbg = get_irn_dbg_info(irn);
63 const arch_register_t *in1_reg, *in2_reg, *out_reg, **slots;
66 /* Return if AM node or not a Sub or xSub */
67 if (!(is_ia32_Sub(irn) || is_ia32_xSub(irn)) || get_ia32_op_type(irn) != ia32_Normal)
70 noreg = ia32_new_NoReg_gp(cg);
71 noreg_fp = ia32_new_NoReg_fp(cg);
72 nomem = new_rd_NoMem(cg->irg);
73 in1 = get_irn_n(irn, 2);
74 in2 = get_irn_n(irn, 3);
75 in1_reg = arch_get_irn_register(cg->arch_env, in1);
76 in2_reg = arch_get_irn_register(cg->arch_env, in2);
77 out_reg = get_ia32_out_reg(irn, 0);
80 block = get_nodes_block(irn);
82 /* in case of sub and OUT == SRC2 we can transform the sequence into neg src2 -- add */
83 if (!REGS_ARE_EQUAL(out_reg, in2_reg))
86 /* generate the neg src2 */
87 if(mode_is_float(mode)) {
91 res = new_rd_ia32_xXor(dbg, irg, block, noreg, noreg, in2, noreg_fp, nomem);
92 size = get_mode_size_bits(mode);
93 entity = ia32_gen_fp_known_const(size == 32 ? ia32_SSIGN : ia32_DSIGN);
94 set_ia32_am_sc(res, entity);
95 set_ia32_op_type(res, ia32_AddrModeS);
96 set_ia32_ls_mode(res, get_ia32_ls_mode(irn));
98 res = new_rd_ia32_Neg(dbg, irg, block, noreg, noreg, in2, nomem);
100 arch_set_irn_register(cg->arch_env, res, in2_reg);
102 /* add to schedule */
103 sched_add_before(irn, res);
105 /* generate the add */
106 if (mode_is_float(mode)) {
107 res = new_rd_ia32_xAdd(dbg, irg, block, noreg, noreg, res, in1, nomem);
108 set_ia32_am_support(res, ia32_am_Source);
109 set_ia32_ls_mode(res, get_ia32_ls_mode(irn));
112 res = new_rd_ia32_Add(dbg, irg, block, noreg, noreg, res, in1, nomem);
113 set_ia32_am_support(res, ia32_am_Full);
114 set_ia32_commutative(res);
117 SET_IA32_ORIG_NODE(res, ia32_get_old_node_name(cg, irn));
119 slots = get_ia32_slots(res);
122 /* exchange the add and the sub */
123 edges_reroute(irn, res, irg);
125 /* add to schedule */
126 sched_add_before(irn, res);
128 /* remove the old sub */
130 arity = get_irn_arity(irn);
131 for(i = 0; i < arity; ++i) {
132 set_irn_n(irn, i, new_Bad());
135 DBG_OPT_SUB2NEGADD(irn, res);
139 * Transforms a LEA into an Add if possible
140 * THIS FUNCTIONS MUST BE CALLED AFTER REGISTER ALLOCATION.
142 static void ia32_transform_lea_to_add(ir_node *irn, ia32_code_gen_t *cg) {
143 ia32_am_flavour_t am_flav;
145 dbg_info *dbg = get_irn_dbg_info(irn);
148 ir_node *nomem, *noreg, *base, *index, *op1, *op2;
151 const arch_register_t *out_reg, *base_reg, *index_reg;
154 if (! is_ia32_Lea(irn))
157 am_flav = get_ia32_am_flavour(irn);
159 /* mustn't have a symconst */
160 if (get_ia32_am_sc(irn) != NULL || get_ia32_frame_ent(irn) != NULL)
163 /* only some LEAs can be transformed to an Add */
164 if (am_flav != ia32_am_B && am_flav != ia32_am_OB && am_flav != ia32_am_OI && am_flav != ia32_am_BI)
167 noreg = ia32_new_NoReg_gp(cg);
168 nomem = new_rd_NoMem(cg->irg);
171 base = get_irn_n(irn, 0);
172 index = get_irn_n(irn,1);
174 if (am_flav & ia32_O) {
175 offs = get_ia32_am_offs_int(irn);
178 out_reg = arch_get_irn_register(cg->arch_env, irn);
179 base_reg = arch_get_irn_register(cg->arch_env, base);
180 index_reg = arch_get_irn_register(cg->arch_env, index);
183 block = get_nodes_block(irn);
185 switch(get_ia32_am_flavour(irn)) {
187 /* out register must be same as base register */
188 if (! REGS_ARE_EQUAL(out_reg, base_reg))
194 /* out register must be same as base register */
195 if (! REGS_ARE_EQUAL(out_reg, base_reg))
202 /* out register must be same as index register */
203 if (! REGS_ARE_EQUAL(out_reg, index_reg))
210 /* out register must be same as one in register */
211 if (REGS_ARE_EQUAL(out_reg, base_reg)) {
215 else if (REGS_ARE_EQUAL(out_reg, index_reg)) {
220 /* in registers a different from out -> no Add possible */
227 res = new_rd_ia32_Add(dbg, irg, block, noreg, noreg, op1, op2, nomem);
228 arch_set_irn_register(cg->arch_env, res, out_reg);
229 set_ia32_op_type(res, ia32_Normal);
230 set_ia32_commutative(res);
233 tarval *tv = new_tarval_from_long(offs, mode_Iu);
234 set_ia32_Immop_tarval(res, tv);
237 SET_IA32_ORIG_NODE(res, ia32_get_old_node_name(cg, irn));
239 /* add Add to schedule */
240 sched_add_before(irn, res);
242 DBG_OPT_LEA2ADD(irn, res);
244 /* remove the old LEA */
247 /* exchange the Add and the LEA */
251 static INLINE int need_constraint_copy(ir_node *irn) {
253 ! is_ia32_Lea(irn) && \
254 ! is_ia32_Conv_I2I(irn) && \
255 ! is_ia32_Conv_I2I8Bit(irn) && \
256 ! is_ia32_CmpCMov(irn) && \
257 ! is_ia32_PsiCondCMov(irn) && \
258 ! is_ia32_CmpSet(irn);
262 * Insert copies for all ia32 nodes where the should_be_same requirement
264 * Transform Sub into Neg -- Add if IN2 == OUT
266 static void ia32_finish_node(ir_node *irn, void *env) {
267 ia32_code_gen_t *cg = env;
268 const arch_register_req_t **reqs;
269 const arch_register_t *out_reg, *in_reg, *in2_reg;
271 ir_node *copy, *in_node, *block, *in2_node;
272 ia32_op_type_t op_tp;
274 if (is_ia32_irn(irn)) {
275 /* AM Dest nodes don't produce any values */
276 op_tp = get_ia32_op_type(irn);
277 if (op_tp == ia32_AddrModeD)
280 reqs = get_ia32_out_req_all(irn);
281 n_res = get_ia32_n_res(irn);
282 block = get_nodes_block(irn);
284 /* check all OUT requirements, if there is a should_be_same */
285 if ((op_tp == ia32_Normal || op_tp == ia32_AddrModeS) && need_constraint_copy(irn))
287 for (i = 0; i < n_res; i++) {
288 if (arch_register_req_is(reqs[i], should_be_same)) {
289 int same_pos = reqs[i]->other_same;
291 /* get in and out register */
292 out_reg = get_ia32_out_reg(irn, i);
293 in_node = get_irn_n(irn, same_pos);
294 in_reg = arch_get_irn_register(cg->arch_env, in_node);
296 /* don't copy ignore nodes */
297 if (arch_irn_is(cg->arch_env, in_node, ignore) && is_Proj(in_node))
300 /* check if in and out register are equal */
301 if (! REGS_ARE_EQUAL(out_reg, in_reg)) {
302 /* in case of a commutative op: just exchange the in's */
303 /* beware: the current op could be everything, so test for ia32 */
304 /* commutativity first before getting the second in */
305 if (is_ia32_commutative(irn)) {
306 in2_node = get_irn_n(irn, same_pos ^ 1);
307 in2_reg = arch_get_irn_register(cg->arch_env, in2_node);
309 if (REGS_ARE_EQUAL(out_reg, in2_reg)) {
310 set_irn_n(irn, same_pos, in2_node);
311 set_irn_n(irn, same_pos ^ 1, in_node);
318 DBG((dbg, LEVEL_1, "inserting copy for %+F in_pos %d\n", irn, same_pos));
319 /* create copy from in register */
320 copy = be_new_Copy(arch_register_get_class(in_reg), cg->irg, block, in_node);
322 DBG_OPT_2ADDRCPY(copy);
324 /* destination is the out register */
325 arch_set_irn_register(cg->arch_env, copy, out_reg);
327 /* insert copy before the node into the schedule */
328 sched_add_before(irn, copy);
331 set_irn_n(irn, same_pos, copy);
338 /* check xCmp: try to avoid unordered cmp */
339 if ((is_ia32_xCmp(irn) || is_ia32_xCmpCMov(irn) || is_ia32_xCmpSet(irn)) &&
340 op_tp == ia32_Normal &&
341 ! is_ia32_ImmConst(irn) && ! is_ia32_ImmSymConst(irn))
343 long pnc = get_ia32_pncode(irn);
345 if (pnc & pn_Cmp_Uo) {
347 int idx1 = 2, idx2 = 3;
349 if (is_ia32_xCmpCMov(irn)) {
354 tmp = get_irn_n(irn, idx1);
355 set_irn_n(irn, idx1, get_irn_n(irn, idx2));
356 set_irn_n(irn, idx2, tmp);
358 set_ia32_pncode(irn, get_negated_pnc(pnc, mode_E));
367 * We have a source address mode node with base or index register equal to
368 * result register. The constraint handler will insert a copy from the
369 * remaining input operand to the result register -> base or index is
371 * Solution: Turn back this address mode into explicit Load + Operation.
373 static void fix_am_source(ir_node *irn, void *env) {
374 ia32_code_gen_t *cg = env;
375 ir_node *base, *index, *noreg;
376 const arch_register_t *reg_base, *reg_index;
377 const arch_register_req_t **reqs;
380 /* check only ia32 nodes with source address mode */
381 if (! is_ia32_irn(irn) || get_ia32_op_type(irn) != ia32_AddrModeS)
383 /* no need to fix unary operations */
384 if (get_irn_arity(irn) == 4)
387 base = get_irn_n(irn, 0);
388 index = get_irn_n(irn, 1);
390 reg_base = arch_get_irn_register(cg->arch_env, base);
391 reg_index = arch_get_irn_register(cg->arch_env, index);
392 reqs = get_ia32_out_req_all(irn);
394 noreg = ia32_new_NoReg_gp(cg);
396 n_res = get_ia32_n_res(irn);
398 for (i = 0; i < n_res; i++) {
399 if (arch_register_req_is(reqs[i], should_be_same)) {
400 /* get in and out register */
401 const arch_register_t *out_reg = get_ia32_out_reg(irn, i);
402 int same_pos = reqs[i]->other_same;
405 there is a constraint for the remaining operand
406 and the result register is equal to base or index register
409 (REGS_ARE_EQUAL(out_reg, reg_base) || REGS_ARE_EQUAL(out_reg, reg_index)))
411 /* turn back address mode */
412 ir_node *in_node = get_irn_n(irn, 2);
413 const arch_register_t *in_reg = arch_get_irn_register(cg->arch_env, in_node);
414 ir_node *block = get_nodes_block(irn);
415 ir_mode *ls_mode = get_ia32_ls_mode(irn);
419 if (arch_register_get_class(in_reg) == &ia32_reg_classes[CLASS_ia32_gp]) {
420 load = new_rd_ia32_Load(NULL, cg->irg, block, base, index, get_irn_n(irn, 4));
421 pnres = pn_ia32_Load_res;
423 else if (arch_register_get_class(in_reg) == &ia32_reg_classes[CLASS_ia32_xmm]) {
424 load = new_rd_ia32_xLoad(NULL, cg->irg, block, base, index, get_irn_n(irn, 4));
425 pnres = pn_ia32_xLoad_res;
428 panic("cannot turn back address mode for this register class");
431 /* copy address mode information to load */
432 set_ia32_ls_mode(load, ls_mode);
433 set_ia32_am_flavour(load, get_ia32_am_flavour(irn));
434 set_ia32_op_type(load, ia32_AddrModeS);
435 set_ia32_am_support(load, ia32_am_Source);
436 set_ia32_am_scale(load, get_ia32_am_scale(irn));
437 set_ia32_am_sc(load, get_ia32_am_sc(irn));
438 add_ia32_am_offs_int(load, get_ia32_am_offs_int(irn));
439 set_ia32_frame_ent(load, get_ia32_frame_ent(irn));
441 if (is_ia32_use_frame(irn))
442 set_ia32_use_frame(load);
444 /* insert the load into schedule */
445 sched_add_before(irn, load);
447 DBG((dbg, LEVEL_3, "irg %+F: build back AM source for node %+F, inserted load %+F\n", cg->irg, irn, load));
449 load = new_r_Proj(cg->irg, block, load, ls_mode, pnres);
450 arch_set_irn_register(cg->arch_env, load, out_reg);
452 /* insert the load result proj into schedule */
453 sched_add_before(irn, load);
455 /* set the new input operand */
456 set_irn_n(irn, 3, load);
458 /* this is a normal node now */
459 set_irn_n(irn, 0, noreg);
460 set_irn_n(irn, 1, noreg);
461 set_ia32_op_type(irn, ia32_Normal);
469 static void ia32_finish_irg_walker(ir_node *block, void *env) {
472 /* first: turn back AM source if necessary */
473 for (irn = sched_first(block); ! sched_is_end(irn); irn = next) {
474 next = sched_next(irn);
475 fix_am_source(irn, env);
478 for (irn = sched_first(block); ! sched_is_end(irn); irn = next) {
479 ia32_code_gen_t *cg = env;
481 next = sched_next(irn);
483 /* check if there is a sub which need to be transformed */
484 ia32_transform_sub_to_neg_add(irn, cg);
486 /* transform a LEA into an Add if possible */
487 ia32_transform_lea_to_add(irn, cg);
490 /* second: insert copies and finish irg */
491 for (irn = sched_first(block); ! sched_is_end(irn); irn = next) {
492 next = sched_next(irn);
493 ia32_finish_node(irn, env);
497 static void ia32_push_on_queue_walker(ir_node *block, void *env) {
499 waitq_put(wq, block);
504 * Add Copy nodes for not fulfilled should_be_equal constraints
506 void ia32_finish_irg(ir_graph *irg, ia32_code_gen_t *cg) {
507 waitq *wq = new_waitq();
509 /* Push the blocks on the waitq because ia32_finish_irg_walker starts more walks ... */
510 irg_block_walk_graph(irg, NULL, ia32_push_on_queue_walker, wq);
512 while (! waitq_empty(wq)) {
513 ir_node *block = waitq_get(wq);
514 ia32_finish_irg_walker(block, cg);
519 void ia32_init_finish(void)
521 FIRM_DBG_REGISTER(dbg, "firm.be.ia32.finish");