+/* Given that an actual argument is an SSA_NAME that is a result of a phi
+ statement PHI, try to find out whether NAME is in fact a
+ multiple-inheritance typecast from a descendant into an ancestor of a formal
+ parameter and thus can be described by an ancestor jump function and if so,
+ write the appropriate function into JFUNC.
+
+ Essentially we want to match the following pattern:
+
+ if (obj_2(D) != 0B)
+ goto <bb 3>;
+ else
+ goto <bb 4>;
+
+ <bb 3>:
+ iftmp.1_3 = &obj_2(D)->D.1762;
+
+ <bb 4>:
+ # iftmp.1_1 = PHI <iftmp.1_3(3), 0B(2)>
+ D.1879_6 = middleman_1 (iftmp.1_1, i_5(D));
+ return D.1879_6; */
+
+static void
+compute_complex_ancestor_jump_func (struct ipa_node_params *info,
+ struct ipa_jump_func *jfunc,
+ gimple phi)
+{
+ HOST_WIDE_INT offset, size, max_size;
+ gimple assign, cond;
+ basic_block phi_bb, assign_bb, cond_bb;
+ tree tmp, parm, expr;
+ int index, i;
+
+ if (gimple_phi_num_args (phi) != 2
+ || !integer_zerop (PHI_ARG_DEF (phi, 1)))
+ return;
+
+ tmp = PHI_ARG_DEF (phi, 0);
+ if (TREE_CODE (tmp) != SSA_NAME
+ || SSA_NAME_IS_DEFAULT_DEF (tmp)
+ || !POINTER_TYPE_P (TREE_TYPE (tmp))
+ || TREE_CODE (TREE_TYPE (TREE_TYPE (tmp))) != RECORD_TYPE)
+ return;
+
+ assign = SSA_NAME_DEF_STMT (tmp);
+ assign_bb = gimple_bb (assign);
+ if (!single_pred_p (assign_bb)
+ || !gimple_assign_single_p (assign))
+ return;
+ expr = gimple_assign_rhs1 (assign);
+
+ if (TREE_CODE (expr) != ADDR_EXPR)
+ return;
+ expr = TREE_OPERAND (expr, 0);
+ expr = get_ref_base_and_extent (expr, &offset, &size, &max_size);
+
+ if (TREE_CODE (expr) != INDIRECT_REF
+ /* If this is a varying address, punt. */
+ || max_size == -1
+ || max_size != size)
+ return;
+ parm = TREE_OPERAND (expr, 0);
+ if (TREE_CODE (parm) != SSA_NAME
+ || !SSA_NAME_IS_DEFAULT_DEF (parm))
+ return;
+
+ index = ipa_get_param_decl_index (info, SSA_NAME_VAR (parm));
+ if (index < 0)
+ return;
+
+ cond_bb = single_pred (assign_bb);
+ cond = last_stmt (cond_bb);
+ if (!cond
+ || gimple_code (cond) != GIMPLE_COND
+ || gimple_cond_code (cond) != NE_EXPR
+ || gimple_cond_lhs (cond) != parm
+ || !integer_zerop (gimple_cond_rhs (cond)))
+ return;
+
+
+ phi_bb = gimple_bb (phi);
+ for (i = 0; i < 2; i++)
+ {
+ basic_block pred = EDGE_PRED (phi_bb, i)->src;
+ if (pred != assign_bb && pred != cond_bb)
+ return;
+ }
+
+ jfunc->type = IPA_JF_ANCESTOR;
+ jfunc->value.ancestor.formal_id = index;
+ jfunc->value.ancestor.offset = offset;
+ jfunc->value.ancestor.type = TREE_TYPE (TREE_TYPE (tmp));
+}
+
+/* Given OP whch is passed as an actual argument to a called function,
+ determine if it is possible to construct a KNOWN_TYPE jump function for it
+ and if so, create one and store it to JFUNC. */
+
+static void
+compute_known_type_jump_func (tree op, struct ipa_jump_func *jfunc)
+{
+ tree binfo;
+
+ if (TREE_CODE (op) != ADDR_EXPR)
+ return;
+
+ op = TREE_OPERAND (op, 0);
+ binfo = gimple_get_relevant_ref_binfo (op, NULL_TREE);
+ if (binfo)
+ {
+ jfunc->type = IPA_JF_KNOWN_TYPE;
+ jfunc->value.base_binfo = binfo;
+ }
+}
+
+