besched: Add and use sched_foreach_safe().
[libfirm] / ir / be / ia32 / ia32_finish.c
1 /*
2  * This file is part of libFirm.
3  * Copyright (C) 2012 University of Karlsruhe.
4  */
5
6 /**
7  * @file
8  * @brief   This file implements functions to finalize the irg for emit.
9  * @author  Christian Wuerdig
10  */
11 #include "config.h"
12
13 #include "irnode.h"
14 #include "ircons.h"
15 #include "irgmod.h"
16 #include "irgwalk.h"
17 #include "iredges.h"
18 #include "irprintf.h"
19 #include "pdeq.h"
20 #include "error.h"
21 #include "firmstat_t.h"
22
23 #include "bearch.h"
24 #include "besched.h"
25 #include "benode.h"
26
27 #include "bearch_ia32_t.h"
28 #include "ia32_finish.h"
29 #include "ia32_new_nodes.h"
30 #include "ia32_common_transform.h"
31 #include "ia32_transform.h"
32 #include "ia32_dbg_stat.h"
33 #include "ia32_optimize.h"
34 #include "gen_ia32_regalloc_if.h"
35
36 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
37
38 /**
39  * Transforms a Sub or xSub into Neg--Add iff OUT_REG != SRC1_REG && OUT_REG == SRC2_REG.
40  * THIS FUNCTIONS MUST BE CALLED AFTER REGISTER ALLOCATION.
41  */
42 static void ia32_transform_sub_to_neg_add(ir_node *irn)
43 {
44         ir_graph *irg;
45         ir_node *in1, *in2, *noreg, *nomem, *res;
46         ir_node *noreg_fp, *block;
47         dbg_info *dbgi;
48         const arch_register_t *in1_reg, *in2_reg, *out_reg;
49
50         /* fix_am will solve this for AddressMode variants */
51         if (get_ia32_op_type(irn) != ia32_Normal)
52                 return;
53
54         irg      = get_irn_irg(irn);
55         noreg    = ia32_new_NoReg_gp(irg);
56         noreg_fp = ia32_new_NoReg_xmm(irg);
57         nomem    = get_irg_no_mem(irg);
58         in1      = get_irn_n(irn, n_ia32_binary_left);
59         in2      = get_irn_n(irn, n_ia32_binary_right);
60         in1_reg  = arch_get_irn_register(in1);
61         in2_reg  = arch_get_irn_register(in2);
62         out_reg  = arch_get_irn_register_out(irn, 0);
63
64         if (out_reg == in1_reg)
65                 return;
66
67         /* in case of sub and OUT == SRC2 we can transform the sequence into neg src2 -- add */
68         if (out_reg != in2_reg)
69                 return;
70
71         block = get_nodes_block(irn);
72         dbgi   = get_irn_dbg_info(irn);
73
74         /* generate the neg src2 */
75         if (is_ia32_xSub(irn)) {
76                 int size;
77                 ir_entity *entity;
78                 ir_mode *op_mode = get_ia32_ls_mode(irn);
79
80                 assert(get_irn_mode(irn) != mode_T);
81
82                 res = new_bd_ia32_xXor(dbgi, block, noreg, noreg, nomem, in2, noreg_fp);
83                 size = get_mode_size_bits(op_mode);
84                 entity = ia32_gen_fp_known_const(size == 32 ? ia32_SSIGN : ia32_DSIGN);
85                 set_ia32_am_sc(res, entity);
86                 set_ia32_op_type(res, ia32_AddrModeS);
87                 set_ia32_ls_mode(res, op_mode);
88
89                 arch_set_irn_register(res, in2_reg);
90
91                 /* add to schedule */
92                 sched_add_before(irn, res);
93
94                 /* generate the add */
95                 res = new_bd_ia32_xAdd(dbgi, block, noreg, noreg, nomem, res, in1);
96                 set_ia32_ls_mode(res, get_ia32_ls_mode(irn));
97         } else {
98                 ir_node *flags_proj = NULL;
99                 ir_node *carry;
100
101                 if (get_irn_mode(irn) == mode_T) {
102                         /* collect the Proj uses */
103                         foreach_out_edge(irn, edge) {
104                                 ir_node *proj = get_edge_src_irn(edge);
105                                 long     pn   = get_Proj_proj(proj);
106                                 if (pn == pn_ia32_flags) {
107                                         assert(flags_proj == NULL);
108                                         flags_proj = proj;
109                                         break;
110                                 }
111                         }
112                 }
113
114                 if (is_ia32_Sbb(irn)) {
115                         /* Feed borrow (in CF) as carry (via CMC) into NOT+ADC. */
116                         carry = get_irn_n(irn, n_ia32_Sbb_eflags);
117                         carry = new_bd_ia32_Cmc(dbgi, block, carry);
118                         goto carry;
119                 } else if (flags_proj != 0) {
120                         /*
121                          * ARG, the above technique does NOT set the flags right.
122                          * So, we must produce the following code:
123                          * t1 = ~b
124                          * t2 = a + ~b + Carry
125                          * Complement Carry
126                          *
127                          * a + -b = a + (~b + 1)  would set the carry flag wrong IFF both a and b are zero.
128                          */
129                         ir_node *cmc;
130                         ir_node *nnot;
131                         ir_node *adc;
132                         ir_node *adc_flags;
133
134                         carry = new_bd_ia32_Stc(dbgi, block);
135
136 carry:
137                         nnot = new_bd_ia32_Not(dbgi, block, in2);
138                         arch_set_irn_register(nnot, in2_reg);
139                         sched_add_before(irn, nnot);
140
141                         arch_set_irn_register(carry, &ia32_registers[REG_EFLAGS]);
142                         sched_add_before(irn, carry);
143
144                         adc = new_bd_ia32_Adc(dbgi, block, noreg, noreg, nomem, nnot, in1, carry);
145                         arch_set_irn_register(adc, out_reg);
146                         set_ia32_commutative(adc);
147
148                         if (flags_proj != NULL) {
149                                 set_irn_mode(adc, mode_T);
150                                 adc_flags = new_r_Proj(adc, mode_Iu, pn_ia32_Adc_flags);
151                                 arch_set_irn_register(adc_flags, &ia32_registers[REG_EFLAGS]);
152
153                                 cmc = new_bd_ia32_Cmc(dbgi, block, adc_flags);
154                                 arch_set_irn_register(cmc, &ia32_registers[REG_EFLAGS]);
155                                 sched_add_after(irn, cmc);
156                                 exchange(flags_proj, cmc);
157                         }
158
159                         res = adc;
160                 } else {
161                         res = new_bd_ia32_Neg(dbgi, block, in2);
162                         arch_set_irn_register(res, in2_reg);
163
164                         /* add to schedule */
165                         sched_add_before(irn, res);
166
167                         /* generate the add */
168                         res = new_bd_ia32_Add(dbgi, block, noreg, noreg, nomem, res, in1);
169                         arch_set_irn_register(res, out_reg);
170                         set_ia32_commutative(res);
171                 }
172         }
173
174         /* exchange the add and the sub */
175         edges_reroute(irn, res);
176         sched_replace(irn, res);
177
178         set_irn_mode(res, get_irn_mode(irn));
179
180         SET_IA32_ORIG_NODE(res, irn);
181
182         /* remove the old sub */
183         kill_node(irn);
184
185         DBG_OPT_SUB2NEGADD(irn, res);
186 }
187
188 static inline int need_constraint_copy(ir_node *irn)
189 {
190         /* TODO this should be determined from the node specification */
191         switch (get_ia32_irn_opcode(irn)) {
192                 case iro_ia32_IMul: {
193                         /* the 3 operand form of IMul needs no constraint copy */
194                         ir_node *right = get_irn_n(irn, n_ia32_IMul_right);
195                         return !is_ia32_Immediate(right);
196                 }
197
198                 case iro_ia32_Lea:
199                 case iro_ia32_Conv_I2I:
200                 case iro_ia32_CMovcc:
201                 case iro_ia32_Minus64Bit:
202                         return 0;
203
204                 default:
205                         return 1;
206         }
207 }
208
209 /**
210  * Returns the index of the "same" register.
211  * On the x86, we should have only one.
212  */
213 static int get_first_same(const arch_register_req_t* req)
214 {
215         const unsigned other = req->other_same;
216         int i;
217
218         for (i = 0; i < 32; ++i) {
219                 if (other & (1U << i)) return i;
220         }
221         panic("same position not found");
222 }
223
224 /**
225  * Insert copies for all ia32 nodes where the should_be_same requirement
226  * is not fulfilled.
227  * Transform Sub into Neg -- Add if IN2 == OUT
228  */
229 static void assure_should_be_same_requirements(ir_node *node)
230 {
231         const arch_register_t      *out_reg, *in_reg;
232         ir_node                    *in_node, *block;
233
234         block = get_nodes_block(node);
235
236         /* check all OUT requirements, if there is a should_be_same */
237         be_foreach_out(node, i) {
238                 int                          i2, arity;
239                 int                          same_pos;
240                 ir_node                     *uses_out_reg;
241                 const arch_register_req_t   *req = arch_get_irn_register_req_out(node, i);
242                 int                         uses_out_reg_pos;
243
244                 if (!arch_register_req_is(req, should_be_same))
245                         continue;
246
247                 same_pos = get_first_same(req);
248
249                 /* get in and out register */
250                 out_reg = arch_get_irn_register_out(node, i);
251                 in_node = get_irn_n(node, same_pos);
252                 in_reg  = arch_get_irn_register(in_node);
253
254                 /* requirement already fulfilled? */
255                 if (in_reg == out_reg)
256                         continue;
257                 assert(in_reg->reg_class == out_reg->reg_class);
258
259                 /* check if any other input operands uses the out register */
260                 arity = get_irn_arity(node);
261                 uses_out_reg     = NULL;
262                 uses_out_reg_pos = -1;
263                 for (i2 = 0; i2 < arity; ++i2) {
264                         ir_node               *in     = get_irn_n(node, i2);
265                         const arch_register_t *other_in_reg;
266
267                         if (!mode_is_data(get_irn_mode(in)))
268                                 continue;
269
270                         other_in_reg = arch_get_irn_register(in);
271
272                         if (other_in_reg != out_reg)
273                                 continue;
274
275                         if (uses_out_reg != NULL && in != uses_out_reg) {
276                                 panic("invalid register allocation");
277                         }
278                         uses_out_reg = in;
279                         if (uses_out_reg_pos >= 0)
280                                 uses_out_reg_pos = -1; /* multiple inputs... */
281                         else
282                                 uses_out_reg_pos = i2;
283                 }
284
285                 /* no-one else is using the out reg, we can simply copy it
286                  * (the register can't be live since the operation will override it
287                  *  anyway) */
288                 if (uses_out_reg == NULL) {
289                         ir_node *copy = be_new_Copy(block, in_node);
290                         DBG_OPT_2ADDRCPY(copy);
291
292                         /* destination is the out register */
293                         arch_set_irn_register(copy, out_reg);
294
295                         /* insert copy before the node into the schedule */
296                         sched_add_before(node, copy);
297
298                         /* set copy as in */
299                         set_irn_n(node, same_pos, copy);
300
301                         DBG((dbg, LEVEL_1,
302                                 "created copy %+F for should be same argument at input %d of %+F\n",
303                                 copy, same_pos, node));
304                         continue;
305                 }
306
307                 /* for commutative nodes we can simply swap the left/right */
308                 if (uses_out_reg_pos == n_ia32_binary_right && is_ia32_commutative(node)) {
309                         ia32_swap_left_right(node);
310                         DBG((dbg, LEVEL_1,
311                                 "swapped left/right input of %+F to resolve should be same constraint\n",
312                                 node));
313                         continue;
314                 }
315
316                 panic("Unresolved should_be_same constraint");
317         }
318 }
319
320 /**
321  * Following Problem:
322  * We have a source address mode node with base or index register equal to
323  * result register and unfulfilled should_be_same requirement. The constraint
324  * handler will insert a copy from the remaining input operand to the result
325  * register -> base or index is broken then.
326  * Solution: Turn back this address mode into explicit Load + Operation.
327  */
328 static void fix_am_source(ir_node *irn)
329 {
330         /* check only ia32 nodes with source address mode */
331         if (!is_ia32_irn(irn) || get_ia32_op_type(irn) != ia32_AddrModeS)
332                 return;
333         /* only need to fix binary operations */
334         if (get_ia32_am_support(irn) != ia32_am_binary)
335                 return;
336
337         be_foreach_out(irn, i) {
338                 const arch_register_req_t *req = arch_get_irn_register_req_out(irn, i);
339                 const arch_register_t     *out_reg;
340                 int                        same_pos;
341                 ir_node                   *same_node;
342                 const arch_register_t     *same_reg;
343                 ir_node                   *load_res;
344
345                 if (!arch_register_req_is(req, should_be_same))
346                         continue;
347
348                 /* get in and out register */
349                 out_reg   = arch_get_irn_register_out(irn, i);
350                 same_pos  = get_first_same(req);
351                 same_node = get_irn_n(irn, same_pos);
352                 same_reg  = arch_get_irn_register(same_node);
353
354                 /* should_be same constraint is fullfilled, nothing to do */
355                 if (out_reg == same_reg)
356                         continue;
357
358                 /* we only need to do something if the out reg is the same as base
359                          or index register */
360                 if (out_reg != arch_get_irn_register(get_irn_n(irn, n_ia32_base)) &&
361                                 out_reg != arch_get_irn_register(get_irn_n(irn, n_ia32_index)))
362                         continue;
363
364                 load_res = ia32_turn_back_am(irn);
365                 arch_set_irn_register(load_res, out_reg);
366
367                 DBG((dbg, LEVEL_3,
368                         "irg %+F: build back AM source for node %+F, inserted load %+F\n",
369                         get_irn_irg(irn), irn, get_Proj_pred(load_res)));
370                 break;
371         }
372 }
373
374 /**
375  * Block walker: finishes a block
376  */
377 static void ia32_finish_irg_walker(ir_node *block, void *env)
378 {
379         (void) env;
380
381         /* first: turn back AM source if necessary */
382         sched_foreach_safe(block, irn) {
383                 fix_am_source(irn);
384         }
385
386         sched_foreach_safe(block, irn) {
387                 /* check if there is a sub which need to be transformed */
388                 if (is_ia32_Sub(irn) || is_ia32_Sbb(irn) || is_ia32_xSub(irn)) {
389                         ia32_transform_sub_to_neg_add(irn);
390                 }
391         }
392
393         /* second: insert copies and finish irg */
394         sched_foreach_safe(block, irn) {
395                 if (is_ia32_irn(irn)) {
396                         /* some nodes are just a bit less efficient, but need no fixing if the
397                          * should be same requirement is not fulfilled */
398                         if (need_constraint_copy(irn))
399                                 assure_should_be_same_requirements(irn);
400                 }
401         }
402 }
403
404 /**
405  * Block walker: pushes all blocks on a wait queue
406  */
407 static void ia32_push_on_queue_walker(ir_node *block, void *env)
408 {
409         waitq *wq = (waitq*)env;
410         waitq_put(wq, block);
411 }
412
413
414 /**
415  * Add Copy nodes for not fulfilled should_be_equal constraints
416  */
417 void ia32_finish_irg(ir_graph *irg)
418 {
419         waitq *wq = new_waitq();
420
421         /* Push the blocks on the waitq because ia32_finish_irg_walker starts more walks ... */
422         irg_block_walk_graph(irg, NULL, ia32_push_on_queue_walker, wq);
423
424         while (! waitq_empty(wq)) {
425                 ir_node *block = (ir_node*)waitq_get(wq);
426                 ia32_finish_irg_walker(block, NULL);
427         }
428         del_waitq(wq);
429 }
430
431 void ia32_init_finish(void)
432 {
433         FIRM_DBG_REGISTER(dbg, "firm.be.ia32.finish");
434 }