/* Memory address lowering and addressing mode selection.
- Copyright (C) 2004, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
-
+ Copyright (C) 2004, 2006, 2007, 2008, 2009, 2010
+ Free Software Foundation, Inc.
+
This file is part of GCC.
-
+
GCC is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 3, or (at your option) any
later version.
-
+
GCC is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
-
+
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#include "coretypes.h"
#include "tm.h"
#include "tree.h"
-#include "rtl.h"
#include "tm_p.h"
-#include "hard-reg-set.h"
#include "basic-block.h"
#include "output.h"
#include "diagnostic.h"
+#include "tree-pretty-print.h"
#include "tree-flow.h"
#include "tree-dump.h"
#include "tree-pass.h"
#include "timevar.h"
#include "flags.h"
#include "tree-inline.h"
+#include "tree-affine.h"
+
+/* FIXME: We compute address costs using RTL. */
#include "insn-config.h"
+#include "rtl.h"
#include "recog.h"
#include "expr.h"
#include "ggc.h"
-#include "tree-affine.h"
#include "target.h"
/* TODO -- handling of symbols (according to Richard Hendersons
comments, http://gcc.gnu.org/ml/gcc-patches/2005-04/msg00949.html):
-
+
There are at least 5 different kinds of symbols that we can run up against:
(1) binds_local_p, small data area.
/* Returns address for TARGET_MEM_REF with parameters given by ADDR
in address space AS.
- If REALLY_EXPAND is false, just make fake registers instead
+ If REALLY_EXPAND is false, just make fake registers instead
of really expanding the operands, and perform the expansion in-place
by using one of the "templates". */
struct mem_addr_template *templ;
if (addr->step && !integer_onep (addr->step))
- st = immed_double_const (TREE_INT_CST_LOW (addr->step),
- TREE_INT_CST_HIGH (addr->step), address_mode);
+ st = immed_double_int_const (tree_to_double_int (addr->step), address_mode);
else
st = NULL_RTX;
if (addr->offset && !integer_zerop (addr->offset))
- off = immed_double_const (TREE_INT_CST_LOW (addr->offset),
- TREE_INT_CST_HIGH (addr->offset), address_mode);
+ off = immed_double_int_const (tree_to_double_int (addr->offset), address_mode);
else
off = NULL_RTX;
aff_combination_remove_elt (addr, i);
}
+/* If ADDR contains an instance of BASE_HINT, move it to PARTS->base. */
+
+static void
+move_hint_to_base (tree type, struct mem_address *parts, tree base_hint,
+ aff_tree *addr)
+{
+ unsigned i;
+ tree val = NULL_TREE;
+ int qual;
+
+ for (i = 0; i < addr->n; i++)
+ {
+ if (!double_int_one_p (addr->elts[i].coef))
+ continue;
+
+ val = addr->elts[i].val;
+ if (operand_equal_p (val, base_hint, 0))
+ break;
+ }
+
+ if (i == addr->n)
+ return;
+
+ /* Cast value to appropriate pointer type. We cannot use a pointer
+ to TYPE directly, as the back-end will assume registers of pointer
+ type are aligned, and just the base itself may not actually be.
+ We use void pointer to the type's address space instead. */
+ qual = ENCODE_QUAL_ADDR_SPACE (TYPE_ADDR_SPACE (type));
+ type = build_qualified_type (void_type_node, qual);
+ parts->base = fold_convert (build_pointer_type (type), val);
+ aff_combination_remove_elt (addr, i);
+}
+
/* If ADDR contains an address of a dereferenced pointer, move it to
PARTS->base. */
element(s) to PARTS. */
static void
-most_expensive_mult_to_index (struct mem_address *parts, aff_tree *addr,
- bool speed)
+most_expensive_mult_to_index (tree type, struct mem_address *parts,
+ aff_tree *addr, bool speed)
{
+ addr_space_t as = TYPE_ADDR_SPACE (type);
+ enum machine_mode address_mode = targetm.addr_space.address_mode (as);
HOST_WIDE_INT coef;
double_int best_mult, amult, amult_neg;
unsigned best_mult_cost = 0, acost;
if (!double_int_fits_in_shwi_p (addr->elts[i].coef))
continue;
- /* FIXME: Should use the correct memory mode rather than Pmode. */
-
coef = double_int_to_shwi (addr->elts[i].coef);
if (coef == 1
- || !multiplier_allowed_in_address_p (coef, Pmode,
- ADDR_SPACE_GENERIC))
+ || !multiplier_allowed_in_address_p (coef, TYPE_MODE (type), as))
continue;
- acost = multiply_by_cost (coef, Pmode, speed);
+ acost = multiply_by_cost (coef, address_mode, speed);
if (acost > best_mult_cost)
{
{
amult = addr->elts[i].coef;
amult_neg = double_int_ext_for_comb (double_int_neg (amult), addr);
-
+
if (double_int_equal_p (amult, best_mult))
op_code = PLUS_EXPR;
else if (double_int_equal_p (amult_neg, best_mult))
mult_elt = fold_build1 (NEGATE_EXPR, sizetype, elt);
}
addr->n = j;
-
+
parts->index = mult_elt;
parts->step = double_int_to_tree (sizetype, best_mult);
}
-/* Splits address ADDR into PARTS.
-
+/* Splits address ADDR for a memory access of type TYPE into PARTS.
+ If BASE_HINT is non-NULL, it specifies an SSA name to be used
+ preferentially as base of the reference.
+
TODO -- be more clever about the distribution of the elements of ADDR
to PARTS. Some architectures do not support anything but single
register in address, possibly with a small integer offset; while
addressing modes is useless. */
static void
-addr_to_parts (aff_tree *addr, struct mem_address *parts, bool speed)
+addr_to_parts (tree type, aff_tree *addr, tree base_hint,
+ struct mem_address *parts, bool speed)
{
tree part;
unsigned i;
/* First move the most expensive feasible multiplication
to index. */
- most_expensive_mult_to_index (parts, addr, speed);
+ most_expensive_mult_to_index (type, parts, addr, speed);
/* Try to find a base of the reference. Since at the moment
there is no reliable way how to distinguish between pointer and its
offset, this is just a guess. */
- if (!parts->symbol)
+ if (!parts->symbol && base_hint)
+ move_hint_to_base (type, parts, base_hint, addr);
+ if (!parts->symbol && !parts->base)
move_pointer_to_base (parts, addr);
/* Then try to process the remaining elements. */
tree
create_mem_ref (gimple_stmt_iterator *gsi, tree type, aff_tree *addr,
- bool speed)
+ tree base_hint, bool speed)
{
tree mem_ref, tmp;
tree atype;
struct mem_address parts;
- addr_to_parts (addr, &parts, speed);
+ addr_to_parts (type, addr, base_hint, &parts, speed);
gimplify_mem_ref_parts (gsi, &parts);
mem_ref = create_mem_ref_raw (type, &parts);
if (mem_ref)
parts.index, parts.step),
true, NULL_TREE, true, GSI_SAME_STMT);
parts.step = NULL_TREE;
-
+
mem_ref = create_mem_ref_raw (type, &parts);
if (mem_ref)
return mem_ref;
{
tmp = build_addr (parts.symbol, current_function_decl);
gcc_assert (is_gimple_val (tmp));
-
+
/* Add the symbol to base, eventually forcing it to register. */
if (parts.base)
{
if (parts.base)
{
atype = TREE_TYPE (parts.base);
- parts.base = force_gimple_operand_gsi (gsi,
+ parts.base = force_gimple_operand_gsi (gsi,
fold_build2 (POINTER_PLUS_EXPR, atype,
parts.base,
fold_convert (sizetype, parts.offset)),
{
/* And the info about the original reference. */
TMR_ORIGINAL (to) = TMR_ORIGINAL (from);
+ TREE_SIDE_EFFECTS (to) = TREE_SIDE_EFFECTS (from);
+ TREE_THIS_VOLATILE (to) = TREE_THIS_VOLATILE (from);
}
/* Move constants in target_mem_ref REF to offset. Returns the new target
if (!changed)
return NULL_TREE;
-
+
ret = create_mem_ref_raw (TREE_TYPE (ref), &addr);
if (!ret)
return NULL_TREE;