2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
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
29 #include "ia32_address_mode.h"
30 #include "ia32_transform.h"
36 #include "iredges_t.h"
39 #include "../benode.h"
40 #include "../belive.h"
44 /* gas/ld don't support negative symconsts :-( */
45 #undef SUPPORT_NEGATIVE_SYMCONSTS
47 static bitset_t *non_address_mode_nodes;
50 * Recursive worker for checking if a DAG with root node can be represented as a simple immediate,
52 * @param node the node
53 * @param symconsts number of symconsts found so far
54 * @param negate if set, the immediate must be negated
56 * @return non-zero if the DAG represents an immediate, 0 else
58 static int do_is_immediate(const ir_node *node, int *symconsts, int negate)
63 switch (get_irn_opcode(node)) {
65 /* Consts are typically immediates */
66 if (!tarval_is_long(get_Const_tarval(node))) {
69 "Optimisation warning tarval of %+F(%+F) is not a long.\n",
70 node, current_ir_graph);
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 */
83 if (get_SymConst_kind(node) != symconst_addr_ent)
91 /* we can use '0' for Unknowns */
95 /* Add's and Sub's are typically supported as long as both operands are
97 if (ia32_is_non_address_mode_node(node))
100 left = get_binop_left(node);
101 right = get_binop_right(node);
102 if (!do_is_immediate(left, symconsts, negate))
104 if (!do_is_immediate(right, symconsts, is_Sub(node) ? !negate : negate))
109 /* all other nodes are NO immediates */
115 * Checks if a DAG with a single root node can be represented as a simple immediate.
117 * @param node the node
119 * @return non-zero if the DAG represents an immediate, 0 else
122 static int is_immediate_simple(const ir_node *node)
125 return do_is_immediate(node, &symconsts, 0);
130 * Check if a DAG starting with root node can be folded into an address mode
133 * @param addr the address mode data so far
134 * @param node the node
135 * @param negate if set, the immediate must be negated
137 static int is_immediate(ia32_address_t *addr, const ir_node *node, int negate)
139 int symconsts = (addr->symconst_ent != NULL);
140 return do_is_immediate(node, &symconsts, negate);
144 * Place a DAG with root node into an address mode.
146 * @param addr the address mode data so far
147 * @param node the node
148 * @param negate if set, the immediate must be negated
150 static void eat_immediate(ia32_address_t *addr, ir_node *node, int negate)
157 switch (get_irn_opcode(node)) {
159 /* simply add the value to the offset */
160 tv = get_Const_tarval(node);
161 val = get_tarval_long(tv);
169 /* place the entity into the symconst */
170 if (addr->symconst_ent != NULL) {
171 panic("Internal error: more than 1 symconst in address calculation");
173 addr->symconst_ent = get_SymConst_entity(node);
174 #ifndef SUPPORT_NEGATIVE_SYMCONSTS
177 addr->symconst_sign = negate;
182 assert(!ia32_is_non_address_mode_node(node));
183 left = get_Add_left(node);
184 right = get_Add_right(node);
185 eat_immediate(addr, left, negate);
186 eat_immediate(addr, right, negate);
189 assert(!ia32_is_non_address_mode_node(node));
190 left = get_Sub_left(node);
191 right = get_Sub_right(node);
192 eat_immediate(addr, left, negate);
193 eat_immediate(addr, right, !negate);
196 panic("Internal error in immediate address calculation");
201 * Place operands of node into an address mode.
203 * @param addr the address mode data so far
204 * @param node the node
205 * @param flags the flags
207 * @return the folded node
209 static ir_node *eat_immediates(ia32_address_t *addr, ir_node *node,
210 ia32_create_am_flags_t flags)
212 if (!(flags & ia32_create_am_force) &&
213 ia32_is_non_address_mode_node(node) &&
214 (!(flags & ia32_create_am_double_use) || get_irn_n_edges(node) > 2))
218 ir_node *left = get_Add_left(node);
219 ir_node *right = get_Add_right(node);
221 if (is_immediate(addr, left, 0)) {
222 eat_immediate(addr, left, 0);
223 return eat_immediates(addr, right, 0);
225 if (is_immediate(addr, right, 0)) {
226 eat_immediate(addr, right, 0);
227 return eat_immediates(addr, left, 0);
229 } else if (is_Sub(node)) {
230 ir_node *left = get_Sub_left(node);
231 ir_node *right = get_Sub_right(node);
233 if (is_immediate(addr, right, 1)) {
234 eat_immediate(addr, right, 1);
235 return eat_immediates(addr, left, 0);
243 * Try to place a Shl into an address mode.
245 * @param addr the address mode data so far
246 * @param node the node to place
248 * @return non-zero on success
250 static int eat_shl(ia32_address_t *addr, ir_node *node)
252 ir_node *shifted_val;
256 ir_node *right = get_Shl_right(node);
259 /* we can use shl with 1, 2 or 3 shift */
260 if (!is_Const(right))
262 tv = get_Const_tarval(right);
263 if (!tarval_is_long(tv))
266 val = get_tarval_long(tv);
267 if (val < 0 || val > 3)
270 ir_fprintf(stderr, "Optimisation warning: unoptimized Shl(,0) found\n");
273 shifted_val = get_Shl_left(node);
274 } else if (is_Add(node)) {
275 /* might be an add x, x */
276 ir_node *left = get_Add_left(node);
277 ir_node *right = get_Add_right(node);
290 /* we can only eat a shl if we don't have a scale or index set yet */
291 if (addr->scale != 0 || addr->index != NULL)
293 if (ia32_is_non_address_mode_node(node))
296 #ifndef AGGRESSIVE_AM
297 if (get_irn_n_edges(node) > 1)
302 addr->index = shifted_val;
306 /* Create an address mode for a given node. */
307 void ia32_create_address_mode(ia32_address_t *addr, ir_node *node, ia32_create_am_flags_t flags)
312 if (is_immediate(addr, node, 0)) {
313 eat_immediate(addr, node, 0);
317 #ifndef AGGRESSIVE_AM
318 if (!(flags & ia32_create_am_force) && get_irn_n_edges(node) > 1) {
324 if (!(flags & ia32_create_am_force) &&
325 ia32_is_non_address_mode_node(node) &&
326 (!(flags & ia32_create_am_double_use) || get_irn_n_edges(node) > 2)) {
331 eat_imms = eat_immediates(addr, node, flags);
332 if (eat_imms != node) {
333 if (flags & ia32_create_am_force) {
334 eat_imms = ia32_skip_downconv(eat_imms);
339 #ifndef AGGRESSIVE_AM
340 if (get_irn_n_edges(node) > 1) {
345 if (ia32_is_non_address_mode_node(node)) {
351 /* starting point Add, Sub or Shl, FrameAddr */
353 /* We don't want to eat add x, x as shl here, so only test for real Shl
354 * instructions, because we want the former as Lea x, x, not Shl x, 1 */
355 if (eat_shl(addr, node))
357 } else if (is_immediate(addr, node, 0)) {
358 eat_immediate(addr, node, 0);
360 } else if (be_is_FrameAddr(node)) {
361 assert(addr->base == NULL);
362 assert(addr->frame_entity == NULL);
363 addr->base = be_get_FrameAddr_frame(node);
365 addr->frame_entity = be_get_FrameAddr_entity(node);
367 } else if (is_Add(node)) {
368 ir_node *left = get_Add_left(node);
369 ir_node *right = get_Add_right(node);
371 if (flags & ia32_create_am_force) {
372 left = ia32_skip_downconv(left);
373 right = ia32_skip_downconv(right);
376 assert(flags & ia32_create_am_force || !is_immediate(addr, left, 0));
377 assert(flags & ia32_create_am_force || !is_immediate(addr, right, 0));
379 if (eat_shl(addr, left)) {
381 } else if (eat_shl(addr, right)) {
385 be_is_FrameAddr(left) &&
386 !ia32_is_non_address_mode_node(left)) {
387 assert(addr->base == NULL);
388 assert(addr->frame_entity == NULL);
389 addr->base = be_get_FrameAddr_frame(left);
391 addr->frame_entity = be_get_FrameAddr_entity(left);
393 } else if (right != NULL &&
394 be_is_FrameAddr(right) &&
395 !ia32_is_non_address_mode_node(right)) {
396 assert(addr->base == NULL);
397 assert(addr->frame_entity == NULL);
398 addr->base = be_get_FrameAddr_frame(right);
400 addr->frame_entity = be_get_FrameAddr_entity(right);
405 if (addr->base != NULL) {
406 assert(addr->index == NULL && addr->scale == 0);
407 assert(right == NULL);
414 if (addr->base == NULL) {
417 assert(addr->index == NULL && addr->scale == 0);
427 void ia32_mark_non_am(ir_node *node)
429 bitset_set(non_address_mode_nodes, get_irn_idx(node));
432 int ia32_is_non_address_mode_node(ir_node const *node)
434 return bitset_is_set(non_address_mode_nodes, get_irn_idx(node));
437 static int value_last_used_here(be_lv_t *lv, ir_node *here, ir_node *value)
439 ir_node *block = get_nodes_block(here);
440 const ir_edge_t *edge;
442 /* If the value is live end it is for sure it does not die here */
443 if (be_is_live_end(lv, block, value)) return 0;
445 /* if multiple nodes in this block use the value, then we cannot decide
446 * whether the value will die here (because there is no schedule yet).
447 * Assume it does not die in this case. */
448 foreach_out_edge(value, edge) {
449 ir_node *user = get_edge_src_irn(edge);
450 if (user != here && get_nodes_block(user) == block) {
459 * Walker: mark those nodes that cannot be part of an address mode because
460 * their value must be accessed through a register
462 static void mark_non_address_nodes(ir_node *node, void *env)
472 mode = get_irn_mode(node);
473 if (!mode_is_int(mode) && !mode_is_reference(mode) && mode != mode_b)
476 switch (get_irn_opcode(node)) {
478 /* Nothing to do. especially do not mark the pointer, because we want to
479 * turn it into AM. */
483 /* Do not mark the pointer, because we want to turn it into AM. */
484 val = get_Store_value(node);
485 ia32_mark_non_am(val);
490 /* only 1 user: AM folding is always beneficial */
491 if (get_irn_n_edges(node) <= 1)
494 /* for adds and shls with multiple users we use this heuristic:
495 * we do not fold them into address mode if their operands don't live
496 * out of the block, because in this case we will reduce register
497 * pressure. Otherwise we fold them in aggressively in the hope, that
498 * the node itself doesn't exist anymore and we were able to save the
499 * register for the result */
500 left = get_binop_left(node);
501 right = get_binop_right(node);
503 /* Fold AM if any of the two operands does not die here. This duplicates
504 * an addition and has the same register pressure for the case that only
505 * one operand dies, but is faster (on Pentium 4).
506 * && instead of || only folds AM if both operands do not die here */
507 if (!value_last_used_here(lv, node, left) ||
508 !value_last_used_here(lv, node, right)) {
512 /* At least one of left and right are not used by anyone else, so it is
513 * beneficial for the register pressure (if both are unused otherwise,
514 * else neutral) and ALU use to not fold AM. */
515 ia32_mark_non_am(node);
519 arity = get_irn_arity(node);
521 for (i = 0; i < arity; ++i) {
522 ir_node *in = get_irn_n(node, i);
523 ia32_mark_non_am(in);
529 void ia32_calculate_non_address_mode_nodes(be_irg_t *birg)
531 ir_graph *irg = be_get_birg_irg(birg);
532 be_lv_t *lv = be_assure_liveness(birg);
534 non_address_mode_nodes = bitset_malloc(get_irg_last_idx(irg));
536 irg_walk_graph(irg, NULL, mark_non_address_nodes, lv);
539 void ia32_free_non_address_mode_nodes(void)
541 bitset_free(non_address_mode_nodes);