introduce more generic resource reservation debug helpers instead of old set_using_xx...
[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, "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
94                  * immediates */
95                 if(bitset_is_set(non_address_mode_nodes, get_irn_idx(node)))
96                         return 0;
97
98                 left  = get_binop_left(node);
99                 right = get_binop_right(node);
100                 if(!do_is_immediate(left, symconsts, negate))
101                         return 0;
102                 if(!do_is_immediate(right, symconsts, is_Sub(node) ? !negate : negate))
103                         return 0;
104
105                 return 1;
106         default:
107                 /* all other nodes are NO immediates */
108                 return 0;
109         }
110 }
111
112 /**
113  * Checks if a DAG with a single root node can be represented as a simple immediate.
114  *
115  * @param node  the node
116  *
117  * @return non-zero if the DAG represents an immediate, 0 else
118  */
119 #if 0
120 static int is_immediate_simple(const ir_node *node) {
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 "
169                               "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) "
264                                    "found\n");
265                 }
266
267                 shifted_val = get_Shl_left(node);
268         } else if(is_Add(node)) {
269                 /* might be an add x, x */
270                 ir_node *left  = get_Add_left(node);
271                 ir_node *right = get_Add_right(node);
272
273                 if(left != right)
274                         return 0;
275                 if(is_Const(left))
276                         return 0;
277
278                 val         = 1;
279                 shifted_val = left;
280         } else {
281                 return 0;
282         }
283
284         /* we can only eat a shl if we don't have a scale or index set yet */
285         if(addr->scale != 0 || addr->index != NULL)
286                 return 0;
287         if(bitset_is_set(non_address_mode_nodes, get_irn_idx(node)))
288                 return 0;
289
290 #ifndef AGGRESSIVE_AM
291         if(get_irn_n_edges(node) > 1)
292                 return 0;
293 #endif
294
295         addr->scale = val;
296         addr->index = shifted_val;
297         return 1;
298 }
299
300 /* Create an address mode for a given node. */
301 void ia32_create_address_mode(ia32_address_t *addr, ir_node *node, int force)
302 {
303         int      res = 0;
304         ir_node *eat_imms;
305
306         if(is_immediate(addr, node, 0)) {
307                 eat_immediate(addr, node, 0);
308                 return;
309         }
310
311 #ifndef AGGRESSIVE_AM
312         if(!force && get_irn_n_edges(node) > 1) {
313                 addr->base = node;
314                 return;
315         }
316 #endif
317
318         if(!force && bitset_is_set(non_address_mode_nodes, get_irn_idx(node))) {
319                 addr->base = node;
320                 return;
321         }
322
323         eat_imms = eat_immediates(addr, node, force);
324         if(eat_imms != node) {
325                 if(force) {
326                         eat_imms = ia32_skip_downconv(eat_imms);
327                 }
328
329                 res  = 1;
330                 node = eat_imms;
331 #ifndef AGGRESSIVE_AM
332                 if(get_irn_n_edges(node) > 1) {
333                         addr->base = node;
334                         return;
335                 }
336 #endif
337                 if(bitset_is_set(non_address_mode_nodes, get_irn_idx(node))) {
338                         addr->base = node;
339                         return;
340                 }
341         }
342
343         /* starting point Add, Sub or Shl, FrameAddr */
344         if(is_Shl(node)) {
345                 /* We don't want to eat add x, x as shl here, so only test for real Shl
346                  * instructions, because we want the former as Lea x, x, not Shl x, 1 */
347                 if(eat_shl(addr, node))
348                         return;
349         } else if(is_immediate(addr, node, 0)) {
350                 eat_immediate(addr, node, 0);
351                 return;
352         } else if(be_is_FrameAddr(node)) {
353                 assert(addr->base == NULL);
354                 assert(addr->frame_entity == NULL);
355                 addr->base         = be_get_FrameAddr_frame(node);
356                 addr->use_frame    = 1;
357                 addr->frame_entity = be_get_FrameAddr_entity(node);
358                 return;
359         } else if(is_Add(node)) {
360                 ir_node *left  = get_Add_left(node);
361                 ir_node *right = get_Add_right(node);
362
363                 if(force) {
364                         left  = ia32_skip_downconv(left);
365                         right = ia32_skip_downconv(right);
366                 }
367
368                 assert(force || !is_immediate(addr, left, 0));
369                 assert(force || !is_immediate(addr, right, 0));
370
371                 if(eat_shl(addr, left)) {
372                         left = NULL;
373                 } else if(eat_shl(addr, right)) {
374                         right = NULL;
375                 }
376                 if(left != NULL && be_is_FrameAddr(left)
377                                 && !bitset_is_set(non_address_mode_nodes, get_irn_idx(left))) {
378                         assert(addr->base == NULL);
379                         assert(addr->frame_entity == NULL);
380                         addr->base         = be_get_FrameAddr_frame(left);
381                         addr->use_frame    = 1;
382                         addr->frame_entity = be_get_FrameAddr_entity(left);
383                         left               = NULL;
384                 } else if(right != NULL && be_is_FrameAddr(right)
385                                 && !bitset_is_set(non_address_mode_nodes, get_irn_idx(right))) {
386                         assert(addr->base == NULL);
387                         assert(addr->frame_entity == NULL);
388                         addr->base         = be_get_FrameAddr_frame(right);
389                         addr->use_frame    = 1;
390                         addr->frame_entity = be_get_FrameAddr_entity(right);
391                         right              = NULL;
392                 }
393
394                 if(left != NULL) {
395                         if(addr->base != NULL) {
396                                 assert(addr->index == NULL && addr->scale == 0);
397                                 assert(right == NULL);
398                                 addr->index = left;
399                         } else {
400                                 addr->base = left;
401                         }
402                 }
403                 if(right != NULL) {
404                         if(addr->base == NULL) {
405                                 addr->base = right;
406                         } else {
407                                 assert(addr->index == NULL && addr->scale == 0);
408                                 addr->index = right;
409                         }
410                 }
411                 return;
412         }
413
414         addr->base = node;
415 }
416
417 void ia32_mark_non_am(ir_node *node)
418 {
419         bitset_set(non_address_mode_nodes, get_irn_idx(node));
420 }
421
422 static int value_last_used_here(ir_node *here, ir_node *value)
423 {
424         ir_node *block = get_nodes_block(here);
425         const ir_edge_t *edge;
426
427         /* If the value is live end it is for sure it does not die here */
428         if (be_is_live_end(lv, block, value)) return 0;
429
430         /* if multiple nodes in this block use the value, then we cannot decide
431          * whether the value will die here (because there is no schedule yet).
432          * Assume it does not die in this case. */
433         foreach_out_edge(value, edge) {
434                 ir_node *user = get_edge_src_irn(edge);
435                 if (user != here && get_nodes_block(user) == block) {
436                         return 0;
437                 }
438         }
439
440         return 1;
441 }
442
443 /**
444  * Walker: mark those nodes that cannot be part of an address mode because
445  * there value must be access through an register
446  */
447 static void mark_non_address_nodes(ir_node *node, void *env)
448 {
449         int i, arity;
450         ir_node *val;
451         ir_node *left;
452         ir_node *right;
453         ir_mode *mode;
454         (void) env;
455
456         mode = get_irn_mode(node);
457         if(!mode_is_int(mode) && !mode_is_reference(mode) && mode != mode_b)
458                 return;
459
460         switch(get_irn_opcode(node)) {
461         case iro_Load:
462                 /* Nothing to do. especially do not mark the pointer, because we want to
463                  * turn it into AM. */
464                 break;
465
466         case iro_Store:
467                 /* Do not mark the pointer, because we want to turn it into AM. */
468                 val = get_Store_value(node);
469                 bitset_set(non_address_mode_nodes, get_irn_idx(val));
470                 break;
471
472         case iro_Shl:
473         case iro_Add:
474                 /* only 1 user: AM folding is always beneficial */
475                 if(get_irn_n_edges(node) <= 1)
476                         break;
477
478                 /* for adds and shls with multiple users we use this heuristic:
479                  * we do not fold them into address mode if their operands don't live
480                  * out of the block, because in this case we will reduce register
481                  * pressure. Otherwise we fold them in aggressively in the hope, that
482                  * the node itself doesn't exist anymore and we were able to save the
483                  * register for the result */
484                 left  = get_binop_left(node);
485                 right = get_binop_right(node);
486
487                 /* Fold AM if any of the two operands does not die here.  This duplicates
488                  * an addition and has the same register pressure for the case that only
489                  * one operand dies, but is faster (on Pentium 4).
490                  * && instead of || only folds AM if both operands do not die here */
491                 if (!value_last_used_here(node, left) ||
492                                 !value_last_used_here(node, right)) {
493                         return;
494                 }
495
496                 /* At least one of left and right are not used by anyone else, so it is
497                  * beneficial for the register pressure (if both are unused otherwise,
498                  * else neutral) and ALU use to not fold AM. */
499                 bitset_set(non_address_mode_nodes, get_irn_idx(node));
500                 break;
501
502         default:
503                 arity = get_irn_arity(node);
504
505                 for(i = 0; i < arity; ++i) {
506                         ir_node *in = get_irn_n(node, i);
507                         bitset_set(non_address_mode_nodes, get_irn_idx(in));
508                 }
509                 break;
510         }
511 }
512
513 void ia32_calculate_non_address_mode_nodes(be_irg_t *birg)
514 {
515         ir_graph *irg = be_get_birg_irg(birg);
516
517         lv                     = be_assure_liveness(birg);
518         non_address_mode_nodes = bitset_malloc(get_irg_last_idx(irg));
519
520         irg_walk_graph(irg, NULL, mark_non_address_nodes, NULL);
521 }
522
523 void ia32_free_non_address_mode_nodes(void)
524 {
525         bitset_free(non_address_mode_nodes);
526 }