Actually implement what r16324 promised.
[libfirm] / ir / be / ia32 / ia32_address_mode.c
1 /*
2  * Copyright (C) 1995-2007 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, "Optimisation warning tarval of %+F(%+F) is not "
71                                    "a long.\n", node, current_ir_graph);
72 #endif
73                         return 0;
74                 }
75                 return 1;
76         case iro_SymConst:
77                 /* the first SymConst of a DAG can be fold into an immediate */
78 #ifndef SUPPORT_NEGATIVE_SYMCONSTS
79                 /* unfortunately the assembler/linker doesn't support -symconst */
80                 if(negate)
81                         return 0;
82 #endif
83
84                 if(get_SymConst_kind(node) != symconst_addr_ent)
85                         return 0;
86                 (*symconsts)++;
87                 if(*symconsts > 1)
88                         return 0;
89
90                 return 1;
91         case iro_Add:
92         case iro_Sub:
93                 /* Add's and Sub's are typically supported as long as both operands are 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 "
168                               "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(!bitset_is_set(non_address_mode_nodes, get_irn_idx(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(!bitset_is_set(non_address_mode_nodes, get_irn_idx(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 force   if set, ignore the marking of node as a non-address-mode node
201  *
202  * @return the folded node
203  */
204 static ir_node *eat_immediates(ia32_address_t *addr, ir_node *node, int force)
205 {
206         if(!force && bitset_is_set(non_address_mode_nodes, get_irn_idx(node)))
207                 return node;
208
209         if(is_Add(node)) {
210                 ir_node *left  = get_Add_left(node);
211                 ir_node *right = get_Add_right(node);
212
213                 if(is_immediate(addr, left, 0)) {
214                         eat_immediate(addr, left, 0);
215                         return eat_immediates(addr, right, 0);
216                 }
217                 if(is_immediate(addr, right, 0)) {
218                         eat_immediate(addr, right, 0);
219                         return eat_immediates(addr, left, 0);
220                 }
221         } else if(is_Sub(node)) {
222                 ir_node *left  = get_Sub_left(node);
223                 ir_node *right = get_Sub_right(node);
224
225                 if(is_immediate(addr, right, 1)) {
226                         eat_immediate(addr, right, 1);
227                         return eat_immediates(addr, left, 0);
228                 }
229         }
230
231         return node;
232 }
233
234 /**
235  * Try to place a Shl into an address mode.
236  *
237  * @param addr    the address mode data so far
238  * @param node   the node to place
239  *
240  * @return non-zero on success
241  */
242 static int eat_shl(ia32_address_t *addr, ir_node *node)
243 {
244         ir_node *shifted_val;
245         long     val;
246
247         if(is_Shl(node)) {
248                 ir_node *right = get_Shl_right(node);
249                 tarval  *tv;
250
251                 /* we can use shl with 1, 2 or 3 shift */
252                 if(!is_Const(right))
253                         return 0;
254                 tv = get_Const_tarval(right);
255                 if(!tarval_is_long(tv))
256                         return 0;
257
258                 val = get_tarval_long(tv);
259                 if(val < 0 || val > 3)
260                         return 0;
261                 if(val == 0) {
262                         ir_fprintf(stderr, "Optimisation warning: unoptimized Shl(,0) "
263                                    "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 static int value_last_used_here(ir_node *here, ir_node *value)
422 {
423         ir_node *block = get_nodes_block(here);
424         const ir_edge_t *edge;
425
426         /* If the value is live end it is for sure it does not die here */
427         if (be_is_live_end(lv, block, value)) return 0;
428
429         /* if multiple nodes in this block use the value, then we cannot decide
430          * whether the value will die here (because there is no schedule yet).
431          * Assume it does not die in this case. */
432         foreach_out_edge(value, edge) {
433                 ir_node *user = get_edge_src_irn(edge);
434                 if (user != here && get_nodes_block(user) == block) {
435                         return 0;
436                 }
437         }
438
439         return 1;
440 }
441
442 /**
443  * Walker: mark those nodes that cannot be part of an address mode because
444  * there value must be access through an register
445  */
446 static void mark_non_address_nodes(ir_node *node, void *env)
447 {
448         int i, arity;
449         ir_node *val;
450         ir_node *left;
451         ir_node *right;
452         ir_mode *mode;
453         (void) env;
454
455         mode = get_irn_mode(node);
456         if(!mode_is_int(mode) && !mode_is_reference(mode) && mode != mode_b)
457                 return;
458
459         switch(get_irn_opcode(node)) {
460         case iro_Load:
461                 /* Nothing to do. especially do not mark the pointer, because we want to
462                  * turn it into AM. */
463                 break;
464
465         case iro_Store:
466                 /* Do not mark the pointer, because we want to turn it into AM. */
467                 val = get_Store_value(node);
468                 bitset_set(non_address_mode_nodes, get_irn_idx(val));
469                 break;
470
471         case iro_Shl:
472         case iro_Add:
473                 /* only 1 user: AM folding is always beneficial */
474                 if(get_irn_n_edges(node) <= 1)
475                         break;
476
477                 /* for adds and shls with multiple users we use this heuristic:
478                  * we do not fold them into address mode if their operands don't live
479                  * out of the block, because in this case we will reduce register
480                  * pressure. Otherwise we fold them in aggressively in the hope, that
481                  * the node itself doesn't exist anymore and we were able to save the
482                  * register for the result */
483                 left  = get_binop_left(node);
484                 right = get_binop_right(node);
485
486                 if (!value_last_used_here(node, left) &&
487                                 !value_last_used_here(node, right)) {
488                         return;
489                 }
490
491                 /* At least one of left and right are not used by anyone else, so it is
492                  * beneficial for the register pressure (if both are unused otherwise,
493                  * else neutral) and ALU use to not fold AM. */
494                 bitset_set(non_address_mode_nodes, get_irn_idx(node));
495                 break;
496
497         default:
498                 arity = get_irn_arity(node);
499
500                 for(i = 0; i < arity; ++i) {
501                         ir_node *in = get_irn_n(node, i);
502                         bitset_set(non_address_mode_nodes, get_irn_idx(in));
503                 }
504                 break;
505         }
506 }
507
508 void ia32_calculate_non_address_mode_nodes(be_irg_t *birg)
509 {
510         ir_graph *irg = be_get_birg_irg(birg);
511
512         lv                     = be_assure_liveness(birg);
513         non_address_mode_nodes = bitset_malloc(get_irg_last_idx(irg));
514
515         irg_walk_graph(irg, NULL, mark_non_address_nodes, NULL);
516 }
517
518 void ia32_free_non_address_mode_nodes(void)
519 {
520         bitset_free(non_address_mode_nodes);
521 }