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