2 * This file implements functions to finalize the irg for emit.
3 * @author Christian Wuerdig
18 #include "../bearch.h"
19 #include "../besched_t.h"
20 #include "../benode_t.h"
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"
32 * Transforms a Sub or xSub into Neg--Add iff OUT_REG == SRC2_REG.
33 * THIS FUNCTIONS MUST BE CALLED AFTER REGISTER ALLOCATION.
35 static void ia32_transform_sub_to_neg_add(ir_node *irn, ia32_code_gen_t *cg) {
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;
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)
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);
58 block = get_nodes_block(irn);
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))
64 /* generate the neg src2 */
65 if(mode_is_float(mode)) {
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, mode);
76 res = new_rd_ia32_Neg(dbg, irg, block, noreg, noreg, in2, nomem);
78 arch_set_irn_register(cg->arch_env, res, in2_reg);
81 sched_add_before(irn, res);
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);
89 res = new_rd_ia32_Add(dbg, irg, block, noreg, noreg, res, in1, nomem);
90 set_ia32_am_support(res, ia32_am_Full);
91 set_ia32_commutative(res);
94 SET_IA32_ORIG_NODE(res, ia32_get_old_node_name(cg, irn));
96 slots = get_ia32_slots(res);
99 /* exchange the add and the sub */
100 edges_reroute(irn, res, irg);
102 /* add to schedule */
103 sched_add_before(irn, res);
105 /* remove the old sub */
107 arity = get_irn_arity(irn);
108 for(i = 0; i < arity; ++i) {
109 set_irn_n(irn, i, new_Bad());
112 DBG_OPT_SUB2NEGADD(irn, res);
116 * Transforms a LEA into an Add if possible
117 * THIS FUNCTIONS MUST BE CALLED AFTER REGISTER ALLOCATION.
119 static void ia32_transform_lea_to_add(ir_node *irn, ia32_code_gen_t *cg) {
120 ia32_am_flavour_t am_flav;
122 dbg_info *dbg = get_irn_dbg_info(irn);
125 ir_node *nomem, *noreg, *base, *index, *op1, *op2;
128 const arch_register_t *out_reg, *base_reg, *index_reg;
131 if (! is_ia32_Lea(irn))
134 am_flav = get_ia32_am_flavour(irn);
136 /* mustn't have a symconst */
137 if (get_ia32_am_sc(irn))
140 /* only some LEAs can be transformed to an Add */
141 if (am_flav != ia32_am_B && am_flav != ia32_am_OB && am_flav != ia32_am_OI && am_flav != ia32_am_BI)
144 noreg = ia32_new_NoReg_gp(cg);
145 nomem = new_rd_NoMem(cg->irg);
148 base = get_irn_n(irn, 0);
149 index = get_irn_n(irn,1);
151 if (am_flav & ia32_O) {
152 offs = get_ia32_am_offs_int(irn);
155 out_reg = arch_get_irn_register(cg->arch_env, irn);
156 base_reg = arch_get_irn_register(cg->arch_env, base);
157 index_reg = arch_get_irn_register(cg->arch_env, index);
160 block = get_nodes_block(irn);
162 switch(get_ia32_am_flavour(irn)) {
164 /* out register must be same as base register */
165 if (! REGS_ARE_EQUAL(out_reg, base_reg))
171 /* out register must be same as base register */
172 if (! REGS_ARE_EQUAL(out_reg, base_reg))
179 /* out register must be same as index register */
180 if (! REGS_ARE_EQUAL(out_reg, index_reg))
187 /* out register must be same as one in register */
188 if (REGS_ARE_EQUAL(out_reg, base_reg)) {
192 else if (REGS_ARE_EQUAL(out_reg, index_reg)) {
197 /* in registers a different from out -> no Add possible */
204 res = new_rd_ia32_Add(dbg, irg, block, noreg, noreg, op1, op2, nomem);
205 arch_set_irn_register(cg->arch_env, res, out_reg);
206 set_ia32_op_type(res, ia32_Normal);
207 set_ia32_commutative(res);
210 tarval *tv = new_tarval_from_long(offs, mode_Iu);
211 set_ia32_Immop_tarval(res, tv);
214 SET_IA32_ORIG_NODE(res, ia32_get_old_node_name(cg, irn));
216 /* add Add to schedule */
217 sched_add_before(irn, res);
219 DBG_OPT_LEA2ADD(irn, res);
221 /* remove the old LEA */
224 /* exchange the Add and the LEA */
228 static INLINE int need_constraint_copy(ir_node *irn) {
230 ! is_ia32_Lea(irn) && \
231 ! is_ia32_Conv_I2I(irn) && \
232 ! is_ia32_Conv_I2I8Bit(irn) && \
233 ! is_ia32_CmpCMov(irn) && \
234 ! is_ia32_PsiCondCMov(irn) && \
235 ! is_ia32_CmpSet(irn);
239 * Insert copies for all ia32 nodes where the should_be_same requirement
241 * Transform Sub into Neg -- Add if IN2 == OUT
243 static void ia32_finish_node(ir_node *irn, void *env) {
244 ia32_code_gen_t *cg = env;
245 const ia32_register_req_t **reqs;
246 const arch_register_t *out_reg, *in_reg, *in2_reg;
248 ir_node *copy, *in_node, *block, *in2_node;
249 ia32_op_type_t op_tp;
251 if (is_ia32_irn(irn)) {
252 /* AM Dest nodes don't produce any values */
253 op_tp = get_ia32_op_type(irn);
254 if (op_tp == ia32_AddrModeD)
257 reqs = get_ia32_out_req_all(irn);
258 n_res = get_ia32_n_res(irn);
259 block = get_nodes_block(irn);
261 /* check all OUT requirements, if there is a should_be_same */
262 if ((op_tp == ia32_Normal || op_tp == ia32_AddrModeS) && need_constraint_copy(irn))
264 for (i = 0; i < n_res; i++) {
265 if (arch_register_req_is(&(reqs[i]->req), should_be_same)) {
266 /* get in and out register */
267 out_reg = get_ia32_out_reg(irn, i);
268 in_node = get_irn_n(irn, reqs[i]->same_pos);
269 in_reg = arch_get_irn_register(cg->arch_env, in_node);
271 /* don't copy ignore nodes */
272 if (arch_irn_is(cg->arch_env, in_node, ignore) && is_Proj(in_node))
275 /* check if in and out register are equal */
276 if (! REGS_ARE_EQUAL(out_reg, in_reg)) {
277 /* in case of a commutative op: just exchange the in's */
278 /* beware: the current op could be everything, so test for ia32 */
279 /* commutativity first before getting the second in */
280 if (is_ia32_commutative(irn)) {
281 in2_node = get_irn_n(irn, reqs[i]->same_pos ^ 1);
282 in2_reg = arch_get_irn_register(cg->arch_env, in2_node);
284 if (REGS_ARE_EQUAL(out_reg, in2_reg)) {
285 set_irn_n(irn, reqs[i]->same_pos, in2_node);
286 set_irn_n(irn, reqs[i]->same_pos ^ 1, in_node);
293 DBG((cg->mod, LEVEL_1, "inserting copy for %+F in_pos %d\n", irn, reqs[i]->same_pos));
294 /* create copy from in register */
295 copy = be_new_Copy(arch_register_get_class(in_reg), cg->irg, block, in_node);
297 DBG_OPT_2ADDRCPY(copy);
299 /* destination is the out register */
300 arch_set_irn_register(cg->arch_env, copy, out_reg);
302 /* insert copy before the node into the schedule */
303 sched_add_before(irn, copy);
306 set_irn_n(irn, reqs[i]->same_pos, copy);
313 /* check xCmp: try to avoid unordered cmp */
314 if ((is_ia32_xCmp(irn) || is_ia32_xCmpCMov(irn) || is_ia32_xCmpSet(irn)) &&
315 op_tp == ia32_Normal &&
316 ! is_ia32_ImmConst(irn) && ! is_ia32_ImmSymConst(irn))
318 long pnc = get_ia32_pncode(irn);
320 if (pnc & pn_Cmp_Uo) {
322 int idx1 = 2, idx2 = 3;
324 if (is_ia32_xCmpCMov(irn)) {
329 tmp = get_irn_n(irn, idx1);
330 set_irn_n(irn, idx1, get_irn_n(irn, idx2));
331 set_irn_n(irn, idx2, tmp);
333 set_ia32_pncode(irn, get_negated_pnc(pnc, mode_E));
338 If we have a CondJmp/CmpSet/xCmpSet with immediate,
339 we need to check if it's the right operand, otherwise
340 we have to change it, as CMP doesn't support immediate
344 if ((is_ia32_CondJmp(irn) || is_ia32_CmpSet(irn) || is_ia32_xCmpSet(irn)) &&
345 (is_ia32_ImmConst(irn) || is_ia32_ImmSymConst(irn)) &&
346 op_tp == ia32_AddrModeS)
348 set_ia32_op_type(irn, ia32_AddrModeD);
349 set_ia32_pncode(irn, get_inversed_pnc(get_ia32_pncode(irn)));
358 * We have a source address mode node with base or index register equal to
359 * result register. The constraint handler will insert a copy from the
360 * remaining input operand to the result register -> base or index is
362 * Solution: Turn back this address mode into explicit Load + Operation.
364 static void fix_am_source(ir_node *irn, void *env) {
365 ia32_code_gen_t *cg = env;
366 ir_node *base, *index, *noreg;
367 const arch_register_t *reg_base, *reg_index;
368 const ia32_register_req_t **reqs;
371 /* check only ia32 nodes with source address mode */
372 if (! is_ia32_irn(irn) || get_ia32_op_type(irn) != ia32_AddrModeS)
374 /* no need to fix unary operations */
375 if (get_irn_arity(irn) == 4)
378 base = get_irn_n(irn, 0);
379 index = get_irn_n(irn, 1);
381 reg_base = arch_get_irn_register(cg->arch_env, base);
382 reg_index = arch_get_irn_register(cg->arch_env, index);
383 reqs = get_ia32_out_req_all(irn);
385 noreg = ia32_new_NoReg_gp(cg);
387 n_res = get_ia32_n_res(irn);
389 for (i = 0; i < n_res; i++) {
390 if (arch_register_req_is(&(reqs[i]->req), should_be_same)) {
391 /* get in and out register */
392 const arch_register_t *out_reg = get_ia32_out_reg(irn, i);
395 there is a constraint for the remaining operand
396 and the result register is equal to base or index register
398 if (reqs[i]->same_pos == 2 &&
399 (REGS_ARE_EQUAL(out_reg, reg_base) || REGS_ARE_EQUAL(out_reg, reg_index)))
401 /* turn back address mode */
402 ir_node *in_node = get_irn_n(irn, 2);
403 const arch_register_t *in_reg = arch_get_irn_register(cg->arch_env, in_node);
404 ir_node *block = get_nodes_block(irn);
405 ir_mode *ls_mode = get_ia32_ls_mode(irn);
409 if (arch_register_get_class(in_reg) == &ia32_reg_classes[CLASS_ia32_gp]) {
410 load = new_rd_ia32_Load(NULL, cg->irg, block, base, index, get_irn_n(irn, 4));
411 pnres = pn_ia32_Load_res;
413 else if (arch_register_get_class(in_reg) == &ia32_reg_classes[CLASS_ia32_xmm]) {
414 load = new_rd_ia32_xLoad(NULL, cg->irg, block, base, index, get_irn_n(irn, 4));
415 pnres = pn_ia32_xLoad_res;
418 assert(0 && "cannot turn back address mode for this register class");
421 /* copy address mode information to load */
422 set_ia32_ls_mode(load, ls_mode);
423 set_ia32_am_flavour(load, get_ia32_am_flavour(irn));
424 set_ia32_op_type(load, ia32_AddrModeS);
425 set_ia32_am_support(load, ia32_am_Source);
426 set_ia32_am_scale(load, get_ia32_am_scale(irn));
427 set_ia32_am_sc(load, get_ia32_am_sc(irn));
428 add_ia32_am_offs_int(load, get_ia32_am_offs_int(irn));
429 set_ia32_frame_ent(load, get_ia32_frame_ent(irn));
431 if (is_ia32_use_frame(irn))
432 set_ia32_use_frame(load);
434 /* insert the load into schedule */
435 sched_add_before(irn, load);
437 DBG((cg->mod, LEVEL_3, "irg %+F: build back AM source for node %+F, inserted load %+F\n", cg->irg, irn, load));
439 load = new_r_Proj(cg->irg, block, load, ls_mode, pnres);
440 arch_set_irn_register(cg->arch_env, load, out_reg);
442 /* insert the load result proj into schedule */
443 sched_add_before(irn, load);
445 /* set the new input operand */
446 set_irn_n(irn, 3, load);
448 /* this is a normal node now */
449 set_irn_n(irn, 0, noreg);
450 set_irn_n(irn, 1, noreg);
451 set_ia32_op_type(irn, ia32_Normal);
459 static void ia32_finish_irg_walker(ir_node *block, void *env) {
462 /* first: turn back AM source if necessary */
463 for (irn = sched_first(block); ! sched_is_end(irn); irn = next) {
464 next = sched_next(irn);
465 fix_am_source(irn, env);
468 for (irn = sched_first(block); ! sched_is_end(irn); irn = next) {
469 ia32_code_gen_t *cg = env;
471 next = sched_next(irn);
473 /* check if there is a sub which need to be transformed */
474 ia32_transform_sub_to_neg_add(irn, cg);
476 /* transform a LEA into an Add if possible */
477 ia32_transform_lea_to_add(irn, cg);
480 /* second: insert copies and finish irg */
481 for (irn = sched_first(block); ! sched_is_end(irn); irn = next) {
482 next = sched_next(irn);
483 ia32_finish_node(irn, env);
487 static void ia32_push_on_queue_walker(ir_node *block, void *env) {
489 waitq_put(wq, block);
494 * Add Copy nodes for not fulfilled should_be_equal constraints
496 void ia32_finish_irg(ir_graph *irg, ia32_code_gen_t *cg) {
497 waitq *wq = new_waitq();
499 /* Push the blocks on the waitq because ia32_finish_irg_walker starts more walks ... */
500 irg_block_walk_graph(irg, NULL, ia32_push_on_queue_walker, wq);
502 while (! waitq_empty(wq)) {
503 ir_node *block = waitq_get(wq);
504 ia32_finish_irg_walker(block, cg);