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