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