#include "flags.h"
#include "function.h"
#include "dumpfile.h"
-#include "tree-flow.h"
+#include "bitmap.h"
+#include "gimple.h"
+#include "gimple-ssa.h"
+#include "tree-ssanames.h"
+#include "tree-into-ssa.h"
+#include "tree-dfa.h"
+#include "tree-ssa.h"
#include "tree-ssa-propagate.h"
#include "target.h"
-#include "gimple-fold.h"
+#include "ipa-utils.h"
+#include "gimple-pretty-print.h"
+#include "tree-ssa-address.h"
+#include "langhooks.h"
/* Return true when DECL can be referenced from current unit.
FROM_DECL (if non-null) specify constructor of variable DECL was taken from.
that has no corresponding callgraph/varpool node
declaring the body.
3) COMDAT functions referred by external vtables that
- we devirtualize only during final copmilation stage.
+ we devirtualize only during final compilation stage.
At this time we already decided that we will not output
the function body and thus we can't reference the symbol
directly. */
{
struct varpool_node *vnode;
struct cgraph_node *node;
- symtab_node snode;
+ symtab_node *snode;
+
+ if (DECL_ABSTRACT (decl))
+ return false;
+
+ /* We are concerned only about static/external vars and functions. */
+ if ((!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
+ || (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != FUNCTION_DECL))
+ return true;
+
+ /* Static objects can be referred only if they was not optimized out yet. */
+ if (!TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
+ {
+ snode = symtab_get_node (decl);
+ if (!snode)
+ return false;
+ node = dyn_cast <cgraph_node> (snode);
+ return !node || !node->global.inlined_to;
+ }
/* We will later output the initializer, so we can refer to it.
So we are concerned only when DECL comes from initializer of
|| TREE_CODE (from_decl) != VAR_DECL
|| !DECL_EXTERNAL (from_decl)
|| (flag_ltrans
- && symtab_get_node (from_decl)->symbol.in_other_partition))
- return true;
- /* We are concerned only about static/external vars and functions. */
- if ((!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
- || (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != FUNCTION_DECL))
+ && symtab_get_node (from_decl)->in_other_partition))
return true;
/* We are folding reference from external vtable. The vtable may reffer
to a symbol keyed to other compilation unit. The other compilation
unit may be in separate DSO and the symbol may be hidden. */
if (DECL_VISIBILITY_SPECIFIED (decl)
&& DECL_EXTERNAL (decl)
- && (!(snode = symtab_get_node (decl)) || !snode->symbol.in_other_partition))
+ && (!(snode = symtab_get_node (decl)) || !snode->in_other_partition))
return false;
/* When function is public, we always can introduce new reference.
Exception are the COMDAT functions where introducing a direct
The second is important when devirtualization happens during final
compilation stage when making a new reference no longer makes callee
to be compiled. */
- if (!node || !node->symbol.definition || node->global.inlined_to)
+ if (!node || !node->definition || node->global.inlined_to)
{
gcc_checking_assert (!TREE_ASM_WRITTEN (decl));
return false;
else if (TREE_CODE (decl) == VAR_DECL)
{
vnode = varpool_get_node (decl);
- if (!vnode || !vnode->symbol.definition)
+ if (!vnode || !vnode->definition)
{
gcc_checking_assert (!TREE_ASM_WRITTEN (decl));
return false;
cval = fold_convert (TREE_TYPE (orig_cval), cval);
return cval;
}
+ if (TREE_OVERFLOW_P (cval))
+ return drop_tree_overflow (cval);
return orig_cval;
}
represented by a declaration (i.e. a global or automatically allocated one)
or NULL if it cannot be found or is not safe. CST is expected to be an
ADDR_EXPR of such object or the function will return NULL. Currently it is
- safe to use such binfo only if it has no base binfo (i.e. no ancestors). */
+ safe to use such binfo only if it has no base binfo (i.e. no ancestors)
+ EXPECTED_TYPE is type of the class virtual belongs to. */
tree
-gimple_extract_devirt_binfo_from_cst (tree cst)
+gimple_extract_devirt_binfo_from_cst (tree cst, tree expected_type)
{
HOST_WIDE_INT offset, size, max_size;
- tree base, type, expected_type, binfo;
+ tree base, type, binfo;
bool last_artificial = false;
if (!flag_devirtualize
return NULL_TREE;
cst = TREE_OPERAND (cst, 0);
- expected_type = TREE_TYPE (cst);
base = get_ref_base_and_extent (cst, &offset, &size, &max_size);
type = TREE_TYPE (base);
if (!DECL_P (base)
{
if (gimple_call_addr_fndecl (OBJ_TYPE_REF_EXPR (callee)) != NULL_TREE)
{
+ if (dump_file && virtual_method_call_p (callee)
+ && !possible_polymorphic_call_target_p
+ (callee, cgraph_get_node (gimple_call_addr_fndecl
+ (OBJ_TYPE_REF_EXPR (callee)))))
+ {
+ fprintf (dump_file,
+ "Type inheritnace inconsistent devirtualization of ");
+ print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
+ fprintf (dump_file, " to ");
+ print_generic_expr (dump_file, callee, TDF_SLIM);
+ fprintf (dump_file, "\n");
+ }
+
gimple_call_set_fn (stmt, OBJ_TYPE_REF_EXPR (callee));
changed = true;
}
- else
+ else if (virtual_method_call_p (callee))
{
tree obj = OBJ_TYPE_REF_OBJECT (callee);
- tree binfo = gimple_extract_devirt_binfo_from_cst (obj);
+ tree binfo = gimple_extract_devirt_binfo_from_cst
+ (obj, obj_type_ref_class (callee));
if (binfo)
{
HOST_WIDE_INT token
tree fndecl = gimple_get_virt_method_for_binfo (token, binfo);
if (fndecl)
{
+#ifdef ENABLE_CHECKING
+ gcc_assert (possible_polymorphic_call_target_p
+ (callee, cgraph_get_node (fndecl)));
+
+#endif
gimple_call_set_fndecl (stmt, fndecl);
changed = true;
}
else
low_bound = double_int_zero;
/* Static constructors for variably sized objects makes no sense. */
- gcc_assert (TREE_CODE(TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor))))
+ gcc_assert (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor))))
== INTEGER_CST);
elt_size =
tree_to_double_int (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor))));
gimple_get_virt_method_for_binfo (HOST_WIDE_INT token, tree known_binfo)
{
unsigned HOST_WIDE_INT offset, size;
- tree v, fn, vtable;
+ tree v, fn, vtable, init;
vtable = v = BINFO_VTABLE (known_binfo);
/* If there is no virtual methods table, leave the OBJ_TYPE_REF alone. */
v = TREE_OPERAND (v, 0);
if (TREE_CODE (v) != VAR_DECL
- || !DECL_VIRTUAL_P (v)
- || !DECL_INITIAL (v)
- || DECL_INITIAL (v) == error_mark_node)
+ || !DECL_VIRTUAL_P (v))
return NULL_TREE;
+ init = ctor_for_folding (v);
+
+ /* The virtual tables should always be born with constructors.
+ and we always should assume that they are avaialble for
+ folding. At the moment we do not stream them in all cases,
+ but it should never happen that ctor seem unreachable. */
+ gcc_assert (init);
+ if (init == error_mark_node)
+ {
+ gcc_assert (in_lto_p);
+ return NULL_TREE;
+ }
gcc_checking_assert (TREE_CODE (TREE_TYPE (v)) == ARRAY_TYPE);
size = tree_low_cst (TYPE_SIZE (TREE_TYPE (TREE_TYPE (v))), 1);
offset += token * size;
- fn = fold_ctor_reference (TREE_TYPE (TREE_TYPE (v)), DECL_INITIAL (v),
- offset, size, vtable);
+ fn = fold_ctor_reference (TREE_TYPE (TREE_TYPE (v)), init,
+ offset, size, v);
if (!fn || integer_zerop (fn))
return NULL_TREE;
gcc_assert (TREE_CODE (fn) == ADDR_EXPR
return false;
}
+
+/* Given a pointer value OP0, return a simplified version of an
+ indirection through OP0, or NULL_TREE if no simplification is
+ possible. Note that the resulting type may be different from
+ the type pointed to in the sense that it is still compatible
+ from the langhooks point of view. */
+
+tree
+gimple_fold_indirect_ref (tree t)
+{
+ tree ptype = TREE_TYPE (t), type = TREE_TYPE (ptype);
+ tree sub = t;
+ tree subtype;
+
+ STRIP_NOPS (sub);
+ subtype = TREE_TYPE (sub);
+ if (!POINTER_TYPE_P (subtype))
+ return NULL_TREE;
+
+ if (TREE_CODE (sub) == ADDR_EXPR)
+ {
+ tree op = TREE_OPERAND (sub, 0);
+ tree optype = TREE_TYPE (op);
+ /* *&p => p */
+ if (useless_type_conversion_p (type, optype))
+ return op;
+
+ /* *(foo *)&fooarray => fooarray[0] */
+ if (TREE_CODE (optype) == ARRAY_TYPE
+ && TREE_CODE (TYPE_SIZE (TREE_TYPE (optype))) == INTEGER_CST
+ && useless_type_conversion_p (type, TREE_TYPE (optype)))
+ {
+ tree type_domain = TYPE_DOMAIN (optype);
+ tree min_val = size_zero_node;
+ if (type_domain && TYPE_MIN_VALUE (type_domain))
+ min_val = TYPE_MIN_VALUE (type_domain);
+ if (TREE_CODE (min_val) == INTEGER_CST)
+ return build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
+ }
+ /* *(foo *)&complexfoo => __real__ complexfoo */
+ else if (TREE_CODE (optype) == COMPLEX_TYPE
+ && useless_type_conversion_p (type, TREE_TYPE (optype)))
+ return fold_build1 (REALPART_EXPR, type, op);
+ /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
+ else if (TREE_CODE (optype) == VECTOR_TYPE
+ && useless_type_conversion_p (type, TREE_TYPE (optype)))
+ {
+ tree part_width = TYPE_SIZE (type);
+ tree index = bitsize_int (0);
+ return fold_build3 (BIT_FIELD_REF, type, op, part_width, index);
+ }
+ }
+
+ /* *(p + CST) -> ... */
+ if (TREE_CODE (sub) == POINTER_PLUS_EXPR
+ && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
+ {
+ tree addr = TREE_OPERAND (sub, 0);
+ tree off = TREE_OPERAND (sub, 1);
+ tree addrtype;
+
+ STRIP_NOPS (addr);
+ addrtype = TREE_TYPE (addr);
+
+ /* ((foo*)&vectorfoo)[1] -> BIT_FIELD_REF<vectorfoo,...> */
+ if (TREE_CODE (addr) == ADDR_EXPR
+ && TREE_CODE (TREE_TYPE (addrtype)) == VECTOR_TYPE
+ && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (addrtype)))
+ && host_integerp (off, 1))
+ {
+ unsigned HOST_WIDE_INT offset = tree_low_cst (off, 1);
+ tree part_width = TYPE_SIZE (type);
+ unsigned HOST_WIDE_INT part_widthi
+ = tree_low_cst (part_width, 0) / BITS_PER_UNIT;
+ unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
+ tree index = bitsize_int (indexi);
+ if (offset / part_widthi
+ <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (addrtype)))
+ return fold_build3 (BIT_FIELD_REF, type, TREE_OPERAND (addr, 0),
+ part_width, index);
+ }
+
+ /* ((foo*)&complexfoo)[1] -> __imag__ complexfoo */
+ if (TREE_CODE (addr) == ADDR_EXPR
+ && TREE_CODE (TREE_TYPE (addrtype)) == COMPLEX_TYPE
+ && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (addrtype))))
+ {
+ tree size = TYPE_SIZE_UNIT (type);
+ if (tree_int_cst_equal (size, off))
+ return fold_build1 (IMAGPART_EXPR, type, TREE_OPERAND (addr, 0));
+ }
+
+ /* *(p + CST) -> MEM_REF <p, CST>. */
+ if (TREE_CODE (addr) != ADDR_EXPR
+ || DECL_P (TREE_OPERAND (addr, 0)))
+ return fold_build2 (MEM_REF, type,
+ addr,
+ build_int_cst_wide (ptype,
+ TREE_INT_CST_LOW (off),
+ TREE_INT_CST_HIGH (off)));
+ }
+
+ /* *(foo *)fooarrptr => (*fooarrptr)[0] */
+ if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
+ && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (subtype)))) == INTEGER_CST
+ && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (subtype))))
+ {
+ tree type_domain;
+ tree min_val = size_zero_node;
+ tree osub = sub;
+ sub = gimple_fold_indirect_ref (sub);
+ if (! sub)
+ sub = build1 (INDIRECT_REF, TREE_TYPE (subtype), osub);
+ type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
+ if (type_domain && TYPE_MIN_VALUE (type_domain))
+ min_val = TYPE_MIN_VALUE (type_domain);
+ if (TREE_CODE (min_val) == INTEGER_CST)
+ return build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
+ }
+
+ return NULL_TREE;
+}