kill the concept of an unknown register - it just heavily complicates code for nearly...
[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         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                 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         int      res = 0;
310         ir_node *eat_imms;
311
312         if (is_immediate(addr, node, 0)) {
313                 eat_immediate(addr, node, 0);
314                 return;
315         }
316
317 #ifndef AGGRESSIVE_AM
318         if (!(flags & ia32_create_am_force) && get_irn_n_edges(node) > 1) {
319                 addr->base = node;
320                 return;
321         }
322 #endif
323
324         if (!(flags & ia32_create_am_force)     &&
325                         ia32_is_non_address_mode_node(node) &&
326                         (!(flags & ia32_create_am_double_use) || get_irn_n_edges(node) > 2)) {
327                 addr->base = node;
328                 return;
329         }
330
331         eat_imms = eat_immediates(addr, node, flags);
332         if (eat_imms != node) {
333                 if (flags & ia32_create_am_force) {
334                         eat_imms = ia32_skip_downconv(eat_imms);
335                 }
336
337                 res  = 1;
338                 node = eat_imms;
339 #ifndef AGGRESSIVE_AM
340                 if (get_irn_n_edges(node) > 1) {
341                         addr->base = node;
342                         return;
343                 }
344 #endif
345                 if (ia32_is_non_address_mode_node(node)) {
346                         addr->base = node;
347                         return;
348                 }
349         }
350
351         /* starting point Add, Sub or Shl, FrameAddr */
352         if (is_Shl(node)) {
353                 /* We don't want to eat add x, x as shl here, so only test for real Shl
354                  * instructions, because we want the former as Lea x, x, not Shl x, 1 */
355                 if (eat_shl(addr, node))
356                         return;
357         } else if (is_immediate(addr, node, 0)) {
358                 eat_immediate(addr, node, 0);
359                 return;
360         } else if (be_is_FrameAddr(node)) {
361                 assert(addr->base == NULL);
362                 assert(addr->frame_entity == NULL);
363                 addr->base         = be_get_FrameAddr_frame(node);
364                 addr->use_frame    = 1;
365                 addr->frame_entity = be_get_FrameAddr_entity(node);
366                 return;
367         } else if (is_Add(node)) {
368                 ir_node *left  = get_Add_left(node);
369                 ir_node *right = get_Add_right(node);
370
371                 if (flags & ia32_create_am_force) {
372                         left  = ia32_skip_downconv(left);
373                         right = ia32_skip_downconv(right);
374                 }
375
376                 assert(flags & ia32_create_am_force || !is_immediate(addr, left,  0));
377                 assert(flags & ia32_create_am_force || !is_immediate(addr, right, 0));
378
379                 if (eat_shl(addr, left)) {
380                         left = NULL;
381                 } else if (eat_shl(addr, right)) {
382                         right = NULL;
383                 }
384                 if (left != NULL          &&
385                                 be_is_FrameAddr(left) &&
386                                 !ia32_is_non_address_mode_node(left)) {
387                         assert(addr->base == NULL);
388                         assert(addr->frame_entity == NULL);
389                         addr->base         = be_get_FrameAddr_frame(left);
390                         addr->use_frame    = 1;
391                         addr->frame_entity = be_get_FrameAddr_entity(left);
392                         left               = NULL;
393                 } else if (right != NULL   &&
394                                 be_is_FrameAddr(right) &&
395                                 !ia32_is_non_address_mode_node(right)) {
396                         assert(addr->base == NULL);
397                         assert(addr->frame_entity == NULL);
398                         addr->base         = be_get_FrameAddr_frame(right);
399                         addr->use_frame    = 1;
400                         addr->frame_entity = be_get_FrameAddr_entity(right);
401                         right              = NULL;
402                 }
403
404                 if (left != NULL) {
405                         if (addr->base != NULL) {
406                                 assert(addr->index == NULL && addr->scale == 0);
407                                 assert(right == NULL);
408                                 addr->index = left;
409                         } else {
410                                 addr->base = left;
411                         }
412                 }
413                 if (right != NULL) {
414                         if (addr->base == NULL) {
415                                 addr->base = right;
416                         } else {
417                                 assert(addr->index == NULL && addr->scale == 0);
418                                 addr->index = right;
419                         }
420                 }
421                 return;
422         }
423
424         addr->base = node;
425 }
426
427 void ia32_mark_non_am(ir_node *node)
428 {
429         bitset_set(non_address_mode_nodes, get_irn_idx(node));
430 }
431
432 int ia32_is_non_address_mode_node(ir_node const *node)
433 {
434         return bitset_is_set(non_address_mode_nodes, get_irn_idx(node));
435 }
436
437 static int value_last_used_here(be_lv_t *lv, ir_node *here, ir_node *value)
438 {
439         ir_node         *block = get_nodes_block(here);
440         const ir_edge_t *edge;
441
442         /* If the value is live end it is for sure it does not die here */
443         if (be_is_live_end(lv, block, value)) return 0;
444
445         /* if multiple nodes in this block use the value, then we cannot decide
446          * whether the value will die here (because there is no schedule yet).
447          * Assume it does not die in this case. */
448         foreach_out_edge(value, edge) {
449                 ir_node *user = get_edge_src_irn(edge);
450                 if (user != here && get_nodes_block(user) == block) {
451                         return 0;
452                 }
453         }
454
455         return 1;
456 }
457
458 /**
459  * Walker: mark those nodes that cannot be part of an address mode because
460  * their value must be accessed through a register
461  */
462 static void mark_non_address_nodes(ir_node *node, void *env)
463 {
464         be_lv_t *lv = env;
465         int      arity;
466         int      i;
467         ir_node *val;
468         ir_node *left;
469         ir_node *right;
470         ir_mode *mode;
471
472         mode = get_irn_mode(node);
473         if (!mode_is_int(mode) && !mode_is_reference(mode) && mode != mode_b)
474                 return;
475
476         switch (get_irn_opcode(node)) {
477         case iro_Load:
478                 /* Nothing to do. especially do not mark the pointer, because we want to
479                  * turn it into AM. */
480                 break;
481
482         case iro_Store:
483                 /* Do not mark the pointer, because we want to turn it into AM. */
484                 val = get_Store_value(node);
485                 ia32_mark_non_am(val);
486                 break;
487
488         case iro_Shl:
489         case iro_Add:
490                 /* only 1 user: AM folding is always beneficial */
491                 if (get_irn_n_edges(node) <= 1)
492                         break;
493
494                 /* for adds and shls with multiple users we use this heuristic:
495                  * we do not fold them into address mode if their operands don't live
496                  * out of the block, because in this case we will reduce register
497                  * pressure. Otherwise we fold them in aggressively in the hope, that
498                  * the node itself doesn't exist anymore and we were able to save the
499                  * register for the result */
500                 left  = get_binop_left(node);
501                 right = get_binop_right(node);
502
503                 /* Fold AM if any of the two operands does not die here.  This duplicates
504                  * an addition and has the same register pressure for the case that only
505                  * one operand dies, but is faster (on Pentium 4).
506                  * && instead of || only folds AM if both operands do not die here */
507                 if (!value_last_used_here(lv, node, left) ||
508                                 !value_last_used_here(lv, node, right)) {
509                         return;
510                 }
511
512                 /* At least one of left and right are not used by anyone else, so it is
513                  * beneficial for the register pressure (if both are unused otherwise,
514                  * else neutral) and ALU use to not fold AM. */
515                 ia32_mark_non_am(node);
516                 break;
517
518         default:
519                 arity = get_irn_arity(node);
520
521                 for (i = 0; i < arity; ++i) {
522                         ir_node *in = get_irn_n(node, i);
523                         ia32_mark_non_am(in);
524                 }
525                 break;
526         }
527 }
528
529 void ia32_calculate_non_address_mode_nodes(be_irg_t *birg)
530 {
531         ir_graph *irg = be_get_birg_irg(birg);
532         be_lv_t  *lv  = be_assure_liveness(birg);
533
534         non_address_mode_nodes = bitset_malloc(get_irg_last_idx(irg));
535
536         irg_walk_graph(irg, NULL, mark_non_address_nodes, lv);
537 }
538
539 void ia32_free_non_address_mode_nodes(void)
540 {
541         bitset_free(non_address_mode_nodes);
542 }