- removed strange add_irn_dep(get_irg_end(cg->irg), res)
[libfirm] / ir / be / ia32 / ia32_address_mode.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief       This file contains functions for matching firm graphs for
23  *              nodes that can be used as address mode for x86 instructions
24  * @author      Matthias Braun
25  * @version     $Id$
26  */
27 #include "config.h"
28
29 #include "ia32_address_mode.h"
30 #include "ia32_transform.h"
31
32 #include "irtypes.h"
33 #include "irnode_t.h"
34 #include "irprintf.h"
35 #include "error.h"
36 #include "iredges_t.h"
37 #include "irgwalk.h"
38
39 #include "../benode_t.h"
40
41 #define AGGRESSIVE_AM
42
43 /* gas/ld don't support negative symconsts :-( */
44 #undef SUPPORT_NEGATIVE_SYMCONSTS
45
46 static bitset_t *non_address_mode_nodes;
47
48 /**
49  * Recursive worker for checking if a DAG with root node can be represented as a simple immediate,
50  *
51  * @param node       the node
52  * @param symconsts  number of symconsts found so far
53  * @param negate     if set, the immediate must be negated
54  *
55  * @return non-zero if the DAG represents an immediate, 0 else
56  */
57 static int do_is_immediate(const ir_node *node, int *symconsts, int negate)
58 {
59         ir_node *left;
60         ir_node *right;
61
62         switch (get_irn_opcode(node)) {
63         case iro_Const:
64                 /* Consts are typically immediates */
65                 if (!tarval_is_long(get_Const_tarval(node))) {
66 #ifdef DEBUG_libfirm
67                         ir_fprintf(stderr,
68                                    "Optimisation warning tarval of %+F(%+F) is not a long.\n",
69                                    node, current_ir_graph);
70 #endif
71                         return 0;
72                 }
73                 return 1;
74         case iro_SymConst:
75                 /* the first SymConst of a DAG can be fold into an immediate */
76 #ifndef SUPPORT_NEGATIVE_SYMCONSTS
77                 /* unfortunately the assembler/linker doesn't support -symconst */
78                 if (negate)
79                         return 0;
80 #endif
81
82                 if (get_SymConst_kind(node) != symconst_addr_ent)
83                         return 0;
84                 (*symconsts)++;
85                 if (*symconsts > 1)
86                         return 0;
87
88                 return 1;
89         case iro_Add:
90         case iro_Sub:
91                 /* Add's and Sub's are typically supported as long as both operands are
92                  * immediates */
93                 if (ia32_is_non_address_mode_node(node))
94                         return 0;
95
96                 left  = get_binop_left(node);
97                 right = get_binop_right(node);
98                 if (!do_is_immediate(left, symconsts, negate))
99                         return 0;
100                 if (!do_is_immediate(right, symconsts, is_Sub(node) ? !negate : negate))
101                         return 0;
102
103                 return 1;
104         default:
105                 /* all other nodes are NO immediates */
106                 return 0;
107         }
108 }
109
110 /**
111  * Checks if a DAG with a single root node can be represented as a simple immediate.
112  *
113  * @param node  the node
114  *
115  * @return non-zero if the DAG represents an immediate, 0 else
116  */
117 #if 0
118 static int is_immediate_simple(const ir_node *node) {
119         int symconsts = 0;
120         return do_is_immediate(node, &symconsts, 0);
121 }
122 #endif
123
124 /**
125  * Check if a DAG starting with root node can be folded into an address mode
126  * as an immediate.
127  *
128  * @param addr    the address mode data so far
129  * @param node    the node
130  * @param negate  if set, the immediate must be negated
131  */
132 static int is_immediate(ia32_address_t *addr, const ir_node *node, int negate)
133 {
134         int symconsts = (addr->symconst_ent != NULL);
135         return do_is_immediate(node, &symconsts, negate);
136 }
137
138 /**
139  * Place a DAG with root node into an address mode.
140  *
141  * @param addr    the address mode data so far
142  * @param node    the node
143  * @param negate  if set, the immediate must be negated
144  */
145 static void eat_immediate(ia32_address_t *addr, ir_node *node, int negate)
146 {
147         tarval  *tv;
148         ir_node *left;
149         ir_node *right;
150         long    val;
151
152         switch (get_irn_opcode(node)) {
153         case iro_Const:
154                 /* simply add the value to the offset */
155                 tv = get_Const_tarval(node);
156                 val = get_tarval_long(tv);
157                 if (negate) {
158                         addr->offset -= val;
159                 } else {
160                         addr->offset += val;
161                 }
162                 break;
163         case iro_SymConst:
164                 /* place the entity into the symconst */
165                 if (addr->symconst_ent != NULL) {
166                         panic("Internal error: more than 1 symconst in address calculation");
167                 }
168                 addr->symconst_ent  = get_SymConst_entity(node);
169 #ifndef SUPPORT_NEGATIVE_SYMCONSTS
170                 assert(!negate);
171 #endif
172                 addr->symconst_sign = negate;
173                 break;
174         case iro_Add:
175                 assert(!ia32_is_non_address_mode_node(node));
176                 left  = get_Add_left(node);
177                 right = get_Add_right(node);
178                 eat_immediate(addr, left, negate);
179                 eat_immediate(addr, right, negate);
180                 break;
181         case iro_Sub:
182                 assert(!ia32_is_non_address_mode_node(node));
183                 left  = get_Sub_left(node);
184                 right = get_Sub_right(node);
185                 eat_immediate(addr, left, negate);
186                 eat_immediate(addr, right, !negate);
187                 break;
188         default:
189                 panic("Internal error in immediate address calculation");
190         }
191 }
192
193 /**
194  * Place operands of node into an address mode.
195  *
196  * @param addr    the address mode data so far
197  * @param node    the node
198  * @param flags   the flags
199  *
200  * @return the folded node
201  */
202 static ir_node *eat_immediates(ia32_address_t *addr, ir_node *node,
203                                ia32_create_am_flags_t flags)
204 {
205         if (!(flags & ia32_create_am_force)     &&
206                         ia32_is_non_address_mode_node(node) &&
207                         (!(flags & ia32_create_am_double_use) || get_irn_n_edges(node) > 2))
208                 return node;
209
210         if (is_Add(node)) {
211                 ir_node *left  = get_Add_left(node);
212                 ir_node *right = get_Add_right(node);
213
214                 if (is_immediate(addr, left, 0)) {
215                         eat_immediate(addr, left, 0);
216                         return eat_immediates(addr, right, 0);
217                 }
218                 if (is_immediate(addr, right, 0)) {
219                         eat_immediate(addr, right, 0);
220                         return eat_immediates(addr, left, 0);
221                 }
222         } else if (is_Sub(node)) {
223                 ir_node *left  = get_Sub_left(node);
224                 ir_node *right = get_Sub_right(node);
225
226                 if (is_immediate(addr, right, 1)) {
227                         eat_immediate(addr, right, 1);
228                         return eat_immediates(addr, left, 0);
229                 }
230         }
231
232         return node;
233 }
234
235 /**
236  * Try to place a Shl into an address mode.
237  *
238  * @param addr    the address mode data so far
239  * @param node   the node to place
240  *
241  * @return non-zero on success
242  */
243 static int eat_shl(ia32_address_t *addr, ir_node *node)
244 {
245         ir_node *shifted_val;
246         long     val;
247
248         if (is_Shl(node)) {
249                 ir_node *right = get_Shl_right(node);
250                 tarval  *tv;
251
252                 /* we can use shl with 1, 2 or 3 shift */
253                 if (!is_Const(right))
254                         return 0;
255                 tv = get_Const_tarval(right);
256                 if (!tarval_is_long(tv))
257                         return 0;
258
259                 val = get_tarval_long(tv);
260                 if (val < 0 || val > 3)
261                         return 0;
262                 if (val == 0) {
263                         ir_fprintf(stderr, "Optimisation warning: unoptimized Shl(,0) found\n");
264                 }
265
266                 shifted_val = get_Shl_left(node);
267         } else if (is_Add(node)) {
268                 /* might be an add x, x */
269                 ir_node *left  = get_Add_left(node);
270                 ir_node *right = get_Add_right(node);
271
272                 if (left != right)
273                         return 0;
274                 if (is_Const(left))
275                         return 0;
276
277                 val         = 1;
278                 shifted_val = left;
279         } else {
280                 return 0;
281         }
282
283         /* we can only eat a shl if we don't have a scale or index set yet */
284         if (addr->scale != 0 || addr->index != NULL)
285                 return 0;
286         if (ia32_is_non_address_mode_node(node))
287                 return 0;
288
289 #ifndef AGGRESSIVE_AM
290         if (get_irn_n_edges(node) > 1)
291                 return 0;
292 #endif
293
294         addr->scale = val;
295         addr->index = shifted_val;
296         return 1;
297 }
298
299 /* Create an address mode for a given node. */
300 void ia32_create_address_mode(ia32_address_t *addr, ir_node *node, ia32_create_am_flags_t flags)
301 {
302         int      res = 0;
303         ir_node *eat_imms;
304
305         if (is_immediate(addr, node, 0)) {
306                 eat_immediate(addr, node, 0);
307                 return;
308         }
309
310 #ifndef AGGRESSIVE_AM
311         if (!(flags & ia32_create_am_force) && get_irn_n_edges(node) > 1) {
312                 addr->base = node;
313                 return;
314         }
315 #endif
316
317         if (!(flags & ia32_create_am_force)     &&
318                         ia32_is_non_address_mode_node(node) &&
319                         (!(flags & ia32_create_am_double_use) || get_irn_n_edges(node) > 2)) {
320                 addr->base = node;
321                 return;
322         }
323
324         eat_imms = eat_immediates(addr, node, flags);
325         if (eat_imms != node) {
326                 if (flags & ia32_create_am_force) {
327                         eat_imms = ia32_skip_downconv(eat_imms);
328                 }
329
330                 res  = 1;
331                 node = eat_imms;
332 #ifndef AGGRESSIVE_AM
333                 if (get_irn_n_edges(node) > 1) {
334                         addr->base = node;
335                         return;
336                 }
337 #endif
338                 if (ia32_is_non_address_mode_node(node)) {
339                         addr->base = node;
340                         return;
341                 }
342         }
343
344         /* starting point Add, Sub or Shl, FrameAddr */
345         if (is_Shl(node)) {
346                 /* We don't want to eat add x, x as shl here, so only test for real Shl
347                  * instructions, because we want the former as Lea x, x, not Shl x, 1 */
348                 if (eat_shl(addr, node))
349                         return;
350         } else if (is_immediate(addr, node, 0)) {
351                 eat_immediate(addr, node, 0);
352                 return;
353         } else if (be_is_FrameAddr(node)) {
354                 assert(addr->base == NULL);
355                 assert(addr->frame_entity == NULL);
356                 addr->base         = be_get_FrameAddr_frame(node);
357                 addr->use_frame    = 1;
358                 addr->frame_entity = be_get_FrameAddr_entity(node);
359                 return;
360         } else if (is_Add(node)) {
361                 ir_node *left  = get_Add_left(node);
362                 ir_node *right = get_Add_right(node);
363
364                 if (flags & ia32_create_am_force) {
365                         left  = ia32_skip_downconv(left);
366                         right = ia32_skip_downconv(right);
367                 }
368
369                 assert(flags & ia32_create_am_force || !is_immediate(addr, left,  0));
370                 assert(flags & ia32_create_am_force || !is_immediate(addr, right, 0));
371
372                 if (eat_shl(addr, left)) {
373                         left = NULL;
374                 } else if (eat_shl(addr, right)) {
375                         right = NULL;
376                 }
377                 if (left != NULL          &&
378                                 be_is_FrameAddr(left) &&
379                                 !ia32_is_non_address_mode_node(left)) {
380                         assert(addr->base == NULL);
381                         assert(addr->frame_entity == NULL);
382                         addr->base         = be_get_FrameAddr_frame(left);
383                         addr->use_frame    = 1;
384                         addr->frame_entity = be_get_FrameAddr_entity(left);
385                         left               = NULL;
386                 } else if (right != NULL   &&
387                                 be_is_FrameAddr(right) &&
388                                 !ia32_is_non_address_mode_node(right)) {
389                         assert(addr->base == NULL);
390                         assert(addr->frame_entity == NULL);
391                         addr->base         = be_get_FrameAddr_frame(right);
392                         addr->use_frame    = 1;
393                         addr->frame_entity = be_get_FrameAddr_entity(right);
394                         right              = NULL;
395                 }
396
397                 if (left != NULL) {
398                         if (addr->base != NULL) {
399                                 assert(addr->index == NULL && addr->scale == 0);
400                                 assert(right == NULL);
401                                 addr->index = left;
402                         } else {
403                                 addr->base = left;
404                         }
405                 }
406                 if (right != NULL) {
407                         if (addr->base == NULL) {
408                                 addr->base = right;
409                         } else {
410                                 assert(addr->index == NULL && addr->scale == 0);
411                                 addr->index = right;
412                         }
413                 }
414                 return;
415         }
416
417         addr->base = node;
418 }
419
420 void ia32_mark_non_am(ir_node *node)
421 {
422         bitset_set(non_address_mode_nodes, get_irn_idx(node));
423 }
424
425 int ia32_is_non_address_mode_node(ir_node const *node)
426 {
427         return bitset_is_set(non_address_mode_nodes, get_irn_idx(node));
428 }
429
430 static int value_last_used_here(be_lv_t *lv, ir_node *here, ir_node *value)
431 {
432         ir_node         *block = get_nodes_block(here);
433         const ir_edge_t *edge;
434
435         /* If the value is live end it is for sure it does not die here */
436         if (be_is_live_end(lv, block, value)) return 0;
437
438         /* if multiple nodes in this block use the value, then we cannot decide
439          * whether the value will die here (because there is no schedule yet).
440          * Assume it does not die in this case. */
441         foreach_out_edge(value, edge) {
442                 ir_node *user = get_edge_src_irn(edge);
443                 if (user != here && get_nodes_block(user) == block) {
444                         return 0;
445                 }
446         }
447
448         return 1;
449 }
450
451 /**
452  * Walker: mark those nodes that cannot be part of an address mode because
453  * their value must be accessed through a register
454  */
455 static void mark_non_address_nodes(ir_node *node, void *env)
456 {
457         be_lv_t *lv = env;
458         int      arity;
459         int      i;
460         ir_node *val;
461         ir_node *left;
462         ir_node *right;
463         ir_mode *mode;
464
465         mode = get_irn_mode(node);
466         if (!mode_is_int(mode) && !mode_is_reference(mode) && mode != mode_b)
467                 return;
468
469         switch (get_irn_opcode(node)) {
470         case iro_Load:
471                 /* Nothing to do. especially do not mark the pointer, because we want to
472                  * turn it into AM. */
473                 break;
474
475         case iro_Store:
476                 /* Do not mark the pointer, because we want to turn it into AM. */
477                 val = get_Store_value(node);
478                 ia32_mark_non_am(val);
479                 break;
480
481         case iro_Shl:
482         case iro_Add:
483                 /* only 1 user: AM folding is always beneficial */
484                 if (get_irn_n_edges(node) <= 1)
485                         break;
486
487                 /* for adds and shls with multiple users we use this heuristic:
488                  * we do not fold them into address mode if their operands don't live
489                  * out of the block, because in this case we will reduce register
490                  * pressure. Otherwise we fold them in aggressively in the hope, that
491                  * the node itself doesn't exist anymore and we were able to save the
492                  * register for the result */
493                 left  = get_binop_left(node);
494                 right = get_binop_right(node);
495
496                 /* Fold AM if any of the two operands does not die here.  This duplicates
497                  * an addition and has the same register pressure for the case that only
498                  * one operand dies, but is faster (on Pentium 4).
499                  * && instead of || only folds AM if both operands do not die here */
500                 if (!value_last_used_here(lv, node, left) ||
501                                 !value_last_used_here(lv, node, right)) {
502                         return;
503                 }
504
505                 /* At least one of left and right are not used by anyone else, so it is
506                  * beneficial for the register pressure (if both are unused otherwise,
507                  * else neutral) and ALU use to not fold AM. */
508                 ia32_mark_non_am(node);
509                 break;
510
511         default:
512                 arity = get_irn_arity(node);
513
514                 for (i = 0; i < arity; ++i) {
515                         ir_node *in = get_irn_n(node, i);
516                         ia32_mark_non_am(in);
517                 }
518                 break;
519         }
520 }
521
522 void ia32_calculate_non_address_mode_nodes(be_irg_t *birg)
523 {
524         ir_graph *irg = be_get_birg_irg(birg);
525         be_lv_t  *lv  = be_assure_liveness(birg);
526
527         non_address_mode_nodes = bitset_malloc(get_irg_last_idx(irg));
528
529         irg_walk_graph(irg, NULL, mark_non_address_nodes, lv);
530 }
531
532 void ia32_free_non_address_mode_nodes(void)
533 {
534         bitset_free(non_address_mode_nodes);
535 }