ia32_transform_sub_to_neg_add() now can handle mode_T.
[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_t.h"
40
41 #define AGGRESSIVE_AM
42
43 /* gas/ld don't support negative symconsts :-( */
44 #undef SUPPORT_NEGATIVE_SYMCONSTS
45
46 static be_lv_t  *lv;
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(bitset_is_set(non_address_mode_nodes, get_irn_idx(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         int symconsts = 0;
121         return do_is_immediate(node, &symconsts, 0);
122 }
123 #endif
124
125 /**
126  * Check if a DAG starting with root node can be folded into an address mode
127  * as an immediate.
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 int is_immediate(ia32_address_t *addr, const ir_node *node, int negate)
134 {
135         int symconsts = (addr->symconst_ent != NULL);
136         return do_is_immediate(node, &symconsts, negate);
137 }
138
139 /**
140  * Place a DAG with root node into an address mode.
141  *
142  * @param addr    the address mode data so far
143  * @param node    the node
144  * @param negate  if set, the immediate must be negated
145  */
146 static void eat_immediate(ia32_address_t *addr, ir_node *node, int negate)
147 {
148         tarval  *tv;
149         ir_node *left;
150         ir_node *right;
151         long    val;
152
153         switch (get_irn_opcode(node)) {
154         case iro_Const:
155                 /* simply add the value to the offset */
156                 tv = get_Const_tarval(node);
157                 val = get_tarval_long(tv);
158                 if (negate) {
159                         addr->offset -= val;
160                 } else {
161                         addr->offset += val;
162                 }
163                 break;
164         case iro_SymConst:
165                 /* place the entity into the symconst */
166                 if (addr->symconst_ent != NULL) {
167                         panic("Internal error: more than 1 symconst in address calculation");
168                 }
169                 addr->symconst_ent  = get_SymConst_entity(node);
170 #ifndef SUPPORT_NEGATIVE_SYMCONSTS
171                 assert(!negate);
172 #endif
173                 addr->symconst_sign = negate;
174                 break;
175         case iro_Add:
176                 assert(!bitset_is_set(non_address_mode_nodes, get_irn_idx(node)));
177                 left  = get_Add_left(node);
178                 right = get_Add_right(node);
179                 eat_immediate(addr, left, negate);
180                 eat_immediate(addr, right, negate);
181                 break;
182         case iro_Sub:
183                 assert(!bitset_is_set(non_address_mode_nodes, get_irn_idx(node)));
184                 left  = get_Sub_left(node);
185                 right = get_Sub_right(node);
186                 eat_immediate(addr, left, negate);
187                 eat_immediate(addr, right, !negate);
188                 break;
189         default:
190                 panic("Internal error in immediate address calculation");
191         }
192 }
193
194 /**
195  * Place operands of node into an address mode.
196  *
197  * @param addr    the address mode data so far
198  * @param node    the node
199  * @param force   if set, ignore the marking of node as a non-address-mode node
200  *
201  * @return the folded node
202  */
203 static ir_node *eat_immediates(ia32_address_t *addr, ir_node *node, int force)
204 {
205         if(!force && bitset_is_set(non_address_mode_nodes, get_irn_idx(node)))
206                 return node;
207
208         if(is_Add(node)) {
209                 ir_node *left  = get_Add_left(node);
210                 ir_node *right = get_Add_right(node);
211
212                 if(is_immediate(addr, left, 0)) {
213                         eat_immediate(addr, left, 0);
214                         return eat_immediates(addr, right, 0);
215                 }
216                 if(is_immediate(addr, right, 0)) {
217                         eat_immediate(addr, right, 0);
218                         return eat_immediates(addr, left, 0);
219                 }
220         } else if(is_Sub(node)) {
221                 ir_node *left  = get_Sub_left(node);
222                 ir_node *right = get_Sub_right(node);
223
224                 if(is_immediate(addr, right, 1)) {
225                         eat_immediate(addr, right, 1);
226                         return eat_immediates(addr, left, 0);
227                 }
228         }
229
230         return node;
231 }
232
233 /**
234  * Try to place a Shl into an address mode.
235  *
236  * @param addr    the address mode data so far
237  * @param node   the node to place
238  *
239  * @return non-zero on success
240  */
241 static int eat_shl(ia32_address_t *addr, ir_node *node)
242 {
243         ir_node *shifted_val;
244         long     val;
245
246         if(is_Shl(node)) {
247                 ir_node *right = get_Shl_right(node);
248                 tarval  *tv;
249
250                 /* we can use shl with 1, 2 or 3 shift */
251                 if(!is_Const(right))
252                         return 0;
253                 tv = get_Const_tarval(right);
254                 if(!tarval_is_long(tv))
255                         return 0;
256
257                 val = get_tarval_long(tv);
258                 if(val < 0 || val > 3)
259                         return 0;
260                 if(val == 0) {
261                         ir_fprintf(stderr, "Optimisation warning: unoptimized Shl(,0) found\n");
262                 }
263
264                 shifted_val = get_Shl_left(node);
265         } else if(is_Add(node)) {
266                 /* might be an add x, x */
267                 ir_node *left  = get_Add_left(node);
268                 ir_node *right = get_Add_right(node);
269
270                 if(left != right)
271                         return 0;
272                 if(is_Const(left))
273                         return 0;
274
275                 val         = 1;
276                 shifted_val = left;
277         } else {
278                 return 0;
279         }
280
281         /* we can only eat a shl if we don't have a scale or index set yet */
282         if(addr->scale != 0 || addr->index != NULL)
283                 return 0;
284         if(bitset_is_set(non_address_mode_nodes, get_irn_idx(node)))
285                 return 0;
286
287 #ifndef AGGRESSIVE_AM
288         if(get_irn_n_edges(node) > 1)
289                 return 0;
290 #endif
291
292         addr->scale = val;
293         addr->index = shifted_val;
294         return 1;
295 }
296
297 /* Create an address mode for a given node. */
298 void ia32_create_address_mode(ia32_address_t *addr, ir_node *node, int force)
299 {
300         int      res = 0;
301         ir_node *eat_imms;
302
303         if(is_immediate(addr, node, 0)) {
304                 eat_immediate(addr, node, 0);
305                 return;
306         }
307
308 #ifndef AGGRESSIVE_AM
309         if(!force && get_irn_n_edges(node) > 1) {
310                 addr->base = node;
311                 return;
312         }
313 #endif
314
315         if(!force && bitset_is_set(non_address_mode_nodes, get_irn_idx(node))) {
316                 addr->base = node;
317                 return;
318         }
319
320         eat_imms = eat_immediates(addr, node, force);
321         if(eat_imms != node) {
322                 if(force) {
323                         eat_imms = ia32_skip_downconv(eat_imms);
324                 }
325
326                 res  = 1;
327                 node = eat_imms;
328 #ifndef AGGRESSIVE_AM
329                 if(get_irn_n_edges(node) > 1) {
330                         addr->base = node;
331                         return;
332                 }
333 #endif
334                 if(bitset_is_set(non_address_mode_nodes, get_irn_idx(node))) {
335                         addr->base = node;
336                         return;
337                 }
338         }
339
340         /* starting point Add, Sub or Shl, FrameAddr */
341         if(is_Shl(node)) {
342                 /* We don't want to eat add x, x as shl here, so only test for real Shl
343                  * instructions, because we want the former as Lea x, x, not Shl x, 1 */
344                 if(eat_shl(addr, node))
345                         return;
346         } else if(is_immediate(addr, node, 0)) {
347                 eat_immediate(addr, node, 0);
348                 return;
349         } else if(be_is_FrameAddr(node)) {
350                 assert(addr->base == NULL);
351                 assert(addr->frame_entity == NULL);
352                 addr->base         = be_get_FrameAddr_frame(node);
353                 addr->use_frame    = 1;
354                 addr->frame_entity = be_get_FrameAddr_entity(node);
355                 return;
356         } else if(is_Add(node)) {
357                 ir_node *left  = get_Add_left(node);
358                 ir_node *right = get_Add_right(node);
359
360                 if(force) {
361                         left  = ia32_skip_downconv(left);
362                         right = ia32_skip_downconv(right);
363                 }
364
365                 assert(force || !is_immediate(addr, left, 0));
366                 assert(force || !is_immediate(addr, right, 0));
367
368                 if(eat_shl(addr, left)) {
369                         left = NULL;
370                 } else if(eat_shl(addr, right)) {
371                         right = NULL;
372                 }
373                 if(left != NULL && be_is_FrameAddr(left)
374                                 && !bitset_is_set(non_address_mode_nodes, get_irn_idx(left))) {
375                         assert(addr->base == NULL);
376                         assert(addr->frame_entity == NULL);
377                         addr->base         = be_get_FrameAddr_frame(left);
378                         addr->use_frame    = 1;
379                         addr->frame_entity = be_get_FrameAddr_entity(left);
380                         left               = NULL;
381                 } else if(right != NULL && be_is_FrameAddr(right)
382                                 && !bitset_is_set(non_address_mode_nodes, get_irn_idx(right))) {
383                         assert(addr->base == NULL);
384                         assert(addr->frame_entity == NULL);
385                         addr->base         = be_get_FrameAddr_frame(right);
386                         addr->use_frame    = 1;
387                         addr->frame_entity = be_get_FrameAddr_entity(right);
388                         right              = NULL;
389                 }
390
391                 if(left != NULL) {
392                         if(addr->base != NULL) {
393                                 assert(addr->index == NULL && addr->scale == 0);
394                                 assert(right == NULL);
395                                 addr->index = left;
396                         } else {
397                                 addr->base = left;
398                         }
399                 }
400                 if(right != NULL) {
401                         if(addr->base == NULL) {
402                                 addr->base = right;
403                         } else {
404                                 assert(addr->index == NULL && addr->scale == 0);
405                                 addr->index = right;
406                         }
407                 }
408                 return;
409         }
410
411         addr->base = node;
412 }
413
414 void ia32_mark_non_am(ir_node *node)
415 {
416         bitset_set(non_address_mode_nodes, get_irn_idx(node));
417 }
418
419 int ia32_is_non_address_mode_node(ir_node *node)
420 {
421         return bitset_is_set(non_address_mode_nodes, get_irn_idx(node));
422 }
423
424 static int value_last_used_here(ir_node *here, ir_node *value)
425 {
426         ir_node *block = get_nodes_block(here);
427         const ir_edge_t *edge;
428
429         /* If the value is live end it is for sure it does not die here */
430         if (be_is_live_end(lv, block, value)) return 0;
431
432         /* if multiple nodes in this block use the value, then we cannot decide
433          * whether the value will die here (because there is no schedule yet).
434          * Assume it does not die in this case. */
435         foreach_out_edge(value, edge) {
436                 ir_node *user = get_edge_src_irn(edge);
437                 if (user != here && get_nodes_block(user) == block) {
438                         return 0;
439                 }
440         }
441
442         return 1;
443 }
444
445 /**
446  * Walker: mark those nodes that cannot be part of an address mode because
447  * their value must be accessed through a register
448  */
449 static void mark_non_address_nodes(ir_node *node, void *env)
450 {
451         int i, arity;
452         ir_node *val;
453         ir_node *left;
454         ir_node *right;
455         ir_mode *mode;
456         (void) env;
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                 bitset_set(non_address_mode_nodes, get_irn_idx(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(node, left) ||
494                                 !value_last_used_here(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                 bitset_set(non_address_mode_nodes, get_irn_idx(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                         bitset_set(non_address_mode_nodes, get_irn_idx(in));
510                 }
511                 break;
512         }
513 }
514
515 void ia32_calculate_non_address_mode_nodes(be_irg_t *birg)
516 {
517         ir_graph *irg = be_get_birg_irg(birg);
518
519         lv                     = be_assure_liveness(birg);
520         non_address_mode_nodes = bitset_malloc(get_irg_last_idx(irg));
521
522         irg_walk_graph(irg, NULL, mark_non_address_nodes, NULL);
523 }
524
525 void ia32_free_non_address_mode_nodes(void)
526 {
527         bitset_free(non_address_mode_nodes);
528 }