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