#include "tree-ssa-sccvn.h"
#include "tree-cfg.h"
#include "domwalk.h"
+#include "ipa-ref.h"
+#include "plugin-api.h"
+#include "cgraph.h"
/* This algorithm is based on the SCC algorithm presented by Keith
Cooper and L. Taylor Simpson in "SCC-Based Value numbering"
temp.op0 = ref;
break;
}
- /* Fallthrough. */
+ break;
/* These are only interesting for their operands, their
existence, and their type. They will never be the last
ref in the chain of references (IE they require an
}
}
- /* Simplify reads from constant strings. */
- else if (op->opcode == ARRAY_REF
- && TREE_CODE (op->op0) == INTEGER_CST
- && integer_zerop (op->op1)
- && operands.length () == 2)
+ /* Simplify reads from constants or constant initializers. */
+ else if (BITS_PER_UNIT == 8
+ && is_gimple_reg_type (ref->type)
+ && (!INTEGRAL_TYPE_P (ref->type)
+ || TYPE_PRECISION (ref->type) % BITS_PER_UNIT == 0))
{
- vn_reference_op_t arg0;
- arg0 = &operands[1];
- if (arg0->opcode == STRING_CST
- && (TYPE_MODE (op->type)
- == TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0->op0))))
- && GET_MODE_CLASS (TYPE_MODE (op->type)) == MODE_INT
- && GET_MODE_SIZE (TYPE_MODE (op->type)) == 1
- && tree_int_cst_sgn (op->op0) >= 0
- && compare_tree_int (op->op0, TREE_STRING_LENGTH (arg0->op0)) < 0)
- return build_int_cst_type (op->type,
- (TREE_STRING_POINTER (arg0->op0)
- [TREE_INT_CST_LOW (op->op0)]));
+ HOST_WIDE_INT off = 0;
+ HOST_WIDE_INT size = tree_to_shwi (TYPE_SIZE (ref->type));
+ if (size % BITS_PER_UNIT != 0
+ || size > MAX_BITSIZE_MODE_ANY_MODE)
+ return NULL_TREE;
+ size /= BITS_PER_UNIT;
+ unsigned i;
+ for (i = 0; i < operands.length (); ++i)
+ {
+ if (operands[i].off == -1)
+ return NULL_TREE;
+ off += operands[i].off;
+ if (operands[i].opcode == MEM_REF)
+ {
+ ++i;
+ break;
+ }
+ }
+ vn_reference_op_t base = &operands[--i];
+ tree ctor = error_mark_node;
+ tree decl = NULL_TREE;
+ if (TREE_CODE_CLASS (base->opcode) == tcc_constant)
+ ctor = base->op0;
+ else if (base->opcode == MEM_REF
+ && base[1].opcode == ADDR_EXPR
+ && (TREE_CODE (TREE_OPERAND (base[1].op0, 0)) == VAR_DECL
+ || TREE_CODE (TREE_OPERAND (base[1].op0, 0)) == CONST_DECL))
+ {
+ decl = TREE_OPERAND (base[1].op0, 0);
+ ctor = ctor_for_folding (decl);
+ }
+ if (ctor == NULL_TREE)
+ return build_zero_cst (ref->type);
+ else if (ctor != error_mark_node)
+ {
+ if (decl)
+ {
+ tree res = fold_ctor_reference (ref->type, ctor,
+ off * BITS_PER_UNIT,
+ size * BITS_PER_UNIT, decl);
+ if (res)
+ {
+ STRIP_USELESS_TYPE_CONVERSION (res);
+ if (is_gimple_min_invariant (res))
+ return res;
+ }
+ }
+ else
+ {
+ unsigned char buf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
+ if (native_encode_expr (ctor, buf, size, off) > 0)
+ return native_interpret_expr (ref->type, buf, size);
+ }
+ }
}
return NULL_TREE;
may fail when comparing types for compatibility. But we really
don't care here - further lookups with the rewritten operands
will simply fail if we messed up types too badly. */
+ HOST_WIDE_INT extra_off = 0;
if (j == 0 && i >= 0
&& lhs_ops[0].opcode == MEM_REF
- && lhs_ops[0].off != -1
- && (lhs_ops[0].off == vr->operands[i].off))
- i--, j--;
+ && lhs_ops[0].off != -1)
+ {
+ if (lhs_ops[0].off == vr->operands[i].off)
+ i--, j--;
+ else if (vr->operands[i].opcode == MEM_REF
+ && vr->operands[i].off != -1)
+ {
+ extra_off = vr->operands[i].off - lhs_ops[0].off;
+ i--, j--;
+ }
+ }
/* i now points to the first additional op.
??? LHS may not be completely contained in VR, one or more
/* Now re-write REF to be based on the rhs of the assignment. */
copy_reference_ops_from_ref (gimple_assign_rhs1 (def_stmt), &rhs);
+
+ /* Apply an extra offset to the inner MEM_REF of the RHS. */
+ if (extra_off != 0)
+ {
+ if (rhs.length () < 2
+ || rhs[0].opcode != MEM_REF
+ || rhs[0].off == -1)
+ return (void *)-1;
+ rhs[0].off += extra_off;
+ rhs[0].op0 = int_const_binop (PLUS_EXPR, rhs[0].op0,
+ build_int_cst (TREE_TYPE (rhs[0].op0),
+ extra_off));
+ }
+
/* We need to pre-pend vr->operands[0..i] to rhs. */
vec<vn_reference_op_s> old = vr->operands;
if (i + 1 + rhs.length () > vr->operands.length ())
shared_lookup_references = vr->operands;
vr->hashcode = vn_reference_compute_hash (vr);
+ /* Try folding the new reference to a constant. */
+ tree val = fully_constant_vn_reference_p (vr);
+ if (val)
+ return vn_reference_lookup_or_insert_for_pieces
+ (vuse, vr->set, vr->type, vr->operands, val);
+
/* Adjust *ref from the new operands. */
if (!ao_ref_init_from_vn_reference (&r, vr->set, vr->type, vr->operands))
return (void *)-1;