-/* Returns the expression associated to OLD_NAME in RENAME_MAP. */
-
-static tree
-get_rename (htab_t rename_map, tree old_name)
-{
- struct rename_map_elt_s tmp;
- PTR *slot;
-
- gcc_assert (TREE_CODE (old_name) == SSA_NAME);
- tmp.old_name = old_name;
- slot = htab_find_slot (rename_map, &tmp, NO_INSERT);
-
- if (slot && *slot)
- return ((rename_map_elt) *slot)->expr;
-
- return old_name;
-}
-
-/* Register in RENAME_MAP the rename tuple (OLD_NAME, EXPR). */
-
-void
-set_rename (htab_t rename_map, tree old_name, tree expr)
-{
- struct rename_map_elt_s tmp;
- PTR *slot;
-
- if (old_name == expr)
- return;
-
- tmp.old_name = old_name;
- slot = htab_find_slot (rename_map, &tmp, INSERT);
-
- if (!slot)
- return;
-
- if (*slot)
- free (*slot);
-
- *slot = new_rename_map_elt (old_name, expr);
-}
-
-/* Rename the SSA_NAMEs used in STMT and that appear in RENAME_MAP. */
-
-static void
-rename_variables_in_stmt (gimple stmt, htab_t rename_map, gimple_stmt_iterator *insert_gsi)
-{
- ssa_op_iter iter;
- use_operand_p use_p;
-
- FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
- {
- tree use = USE_FROM_PTR (use_p);
- tree expr, type_use, type_expr;
- gimple_seq stmts;
-
- if (TREE_CODE (use) != SSA_NAME)
- continue;
-
- expr = get_rename (rename_map, use);
- if (use == expr)
- continue;
-
- type_use = TREE_TYPE (use);
- type_expr = TREE_TYPE (expr);
-
- if (type_use != type_expr
- || (TREE_CODE (expr) != SSA_NAME
- && is_gimple_reg (use)))
- {
- tree var;
-
- if (is_gimple_debug (stmt))
- {
- if (gimple_debug_bind_p (stmt))
- gimple_debug_bind_reset_value (stmt);
- else
- gcc_unreachable ();
-
- break;
- }
-
- var = create_tmp_var (type_use, "var");
-
- if (type_use != type_expr)
- expr = fold_convert (type_use, expr);
-
- expr = build2 (MODIFY_EXPR, type_use, var, expr);
- expr = force_gimple_operand (expr, &stmts, true, NULL);
- gsi_insert_seq_before (insert_gsi, stmts, GSI_SAME_STMT);
- }
-
- replace_exp (use_p, expr);
- }
-
- update_stmt (stmt);
-}
-
-/* Returns true if NAME is a parameter of SESE. */
-
-static bool
-is_parameter (sese region, tree name)
-{
- int i;
- tree p;
-
- for (i = 0; VEC_iterate (tree, SESE_PARAMS (region), i, p); i++)
- if (p == name)
- return true;
-
- return false;
-}
-
-/* Returns true if NAME is an induction variable. */
-
-static bool
-is_iv (tree name)
-{
- return gimple_code (SSA_NAME_DEF_STMT (name)) == GIMPLE_PHI;
-}
-
-static void expand_scalar_variables_stmt (gimple, basic_block, sese,
- htab_t, gimple_stmt_iterator *);
-static tree
-expand_scalar_variables_expr (tree, tree, enum tree_code, tree, basic_block,
- sese, htab_t, gimple_stmt_iterator *);
-
-static tree
-expand_scalar_variables_call (gimple stmt, basic_block bb, sese region,
- htab_t rename_map, gimple_stmt_iterator *gsi)
-{
- int i, nargs = gimple_call_num_args (stmt);
- VEC (tree, gc) *args = VEC_alloc (tree, gc, nargs);
- tree fn_type = TREE_TYPE (gimple_call_fn (stmt));
- tree fn = gimple_call_fndecl (stmt);
- tree call_expr, var, lhs;
- gimple call;
-
- for (i = 0; i < nargs; i++)
- {
- tree arg = gimple_call_arg (stmt, i);
- tree t = TREE_TYPE (arg);
-
- var = create_tmp_var (t, "var");
- arg = expand_scalar_variables_expr (t, arg, TREE_CODE (arg), NULL,
- bb, region, rename_map, gsi);
- arg = build2 (MODIFY_EXPR, t, var, arg);
- arg = force_gimple_operand_gsi (gsi, arg, true, NULL,
- true, GSI_SAME_STMT);
- VEC_quick_push (tree, args, arg);
- }
-
- lhs = gimple_call_lhs (stmt);
- var = create_tmp_var (TREE_TYPE (lhs), "var");
- call_expr = build_call_vec (fn_type, fn, args);
- call = gimple_build_call_from_tree (call_expr);
- var = make_ssa_name (var, call);
- gimple_call_set_lhs (call, var);
- gsi_insert_before (gsi, call, GSI_SAME_STMT);
-
- return var;
-}
-
-/* Copies at GSI all the scalar computations on which the ssa_name OP0
- depends on in the SESE: these are all the scalar variables used in
- the definition of OP0, that are defined outside BB and still in the
- SESE, i.e. not a parameter of the SESE. The expression that is
- returned contains only induction variables from the generated code:
- RENAME_MAP contains the induction variables renaming mapping, and is used
- to translate the names of induction variables. */
-
-static tree
-expand_scalar_variables_ssa_name (tree type, tree op0, basic_block bb,
- sese region, htab_t rename_map,
- gimple_stmt_iterator *gsi)
-{
- gimple def_stmt;
- tree new_op;
-
- if (is_parameter (region, op0)
- || is_iv (op0))
- return fold_convert (type, get_rename (rename_map, op0));
-
- def_stmt = SSA_NAME_DEF_STMT (op0);
-
- /* Check whether we already have a rename for OP0. */
- new_op = get_rename (rename_map, op0);
-
- if (new_op != op0
- && gimple_bb (SSA_NAME_DEF_STMT (new_op)) == bb)
- return fold_convert (type, new_op);
-
- if (gimple_bb (def_stmt) == bb)
- {
- /* If the defining statement is in the basic block already
- we do not need to create a new expression for it, we
- only need to ensure its operands are expanded. */
- expand_scalar_variables_stmt (def_stmt, bb, region, rename_map, gsi);
- return fold_convert (type, new_op);
- }
- else
- {
- if (!gimple_bb (def_stmt)
- || !bb_in_sese_p (gimple_bb (def_stmt), region))
- return fold_convert (type, new_op);
-
- switch (gimple_code (def_stmt))
- {
- case GIMPLE_ASSIGN:
- {
- tree var0 = gimple_assign_rhs1 (def_stmt);
- enum tree_code subcode = gimple_assign_rhs_code (def_stmt);
- tree var1 = gimple_assign_rhs2 (def_stmt);
- tree type = gimple_expr_type (def_stmt);
-
- return expand_scalar_variables_expr (type, var0, subcode, var1, bb,
- region, rename_map, gsi);
- }
-
- case GIMPLE_CALL:
- return expand_scalar_variables_call (def_stmt, bb, region, rename_map, gsi);
-
- default:
- gcc_unreachable ();
- return new_op;
- }
- }
-}
-
-/* Copies at GSI all the scalar computations on which the expression
- OP0 CODE OP1 depends on in the SESE: these are all the scalar
- variables used in OP0 and OP1, defined outside BB and still defined
- in the SESE, i.e. not a parameter of the SESE. The expression that
- is returned contains only induction variables from the generated
- code: RENAME_MAP contains the induction variables renaming mapping, and is
- used to translate the names of induction variables. */
-
-static tree
-expand_scalar_variables_expr (tree type, tree op0, enum tree_code code,
- tree op1, basic_block bb, sese region,
- htab_t rename_map, gimple_stmt_iterator *gsi)
-{
- if (TREE_CODE_CLASS (code) == tcc_constant
- || TREE_CODE_CLASS (code) == tcc_declaration)
- return op0;
-
- /* For data references we have to duplicate also its memory
- indexing. */
- if (TREE_CODE_CLASS (code) == tcc_reference)
- {
- switch (code)
- {
- case REALPART_EXPR:
- case IMAGPART_EXPR:
- {
- tree op = TREE_OPERAND (op0, 0);
- tree res = expand_scalar_variables_expr
- (type, op, TREE_CODE (op), NULL, bb, region, rename_map, gsi);
- return build1 (code, type, res);
- }
-
- case INDIRECT_REF:
- {
- tree old_name = TREE_OPERAND (op0, 0);
- tree expr = expand_scalar_variables_ssa_name
- (type, old_name, bb, region, rename_map, gsi);
-
- if (TREE_CODE (expr) != SSA_NAME
- && is_gimple_reg (old_name))
- {
- tree type = TREE_TYPE (old_name);
- tree var = create_tmp_var (type, "var");
-
- expr = build2 (MODIFY_EXPR, type, var, expr);
- expr = force_gimple_operand_gsi (gsi, expr, true, NULL,
- true, GSI_SAME_STMT);
- }
-
- return fold_build1 (code, type, expr);
- }
-
- case ARRAY_REF:
- {
- tree op00 = TREE_OPERAND (op0, 0);
- tree op01 = TREE_OPERAND (op0, 1);
- tree op02 = TREE_OPERAND (op0, 2);
- tree op03 = TREE_OPERAND (op0, 3);
- tree base = expand_scalar_variables_expr
- (TREE_TYPE (op00), op00, TREE_CODE (op00), NULL, bb, region,
- rename_map, gsi);
- tree subscript = expand_scalar_variables_expr
- (TREE_TYPE (op01), op01, TREE_CODE (op01), NULL, bb, region,
- rename_map, gsi);
-
- return build4 (ARRAY_REF, type, base, subscript, op02, op03);
- }
-
- case COMPONENT_REF:
- return op0;
-
- default:
- /* The above cases should catch everything. */
- gcc_unreachable ();
- }
- }
-
- if (TREE_CODE_CLASS (code) == tcc_unary)
- {
- tree op0_type = TREE_TYPE (op0);
- enum tree_code op0_code = TREE_CODE (op0);
- tree op0_expr = expand_scalar_variables_expr (op0_type, op0, op0_code,
- NULL, bb, region, rename_map, gsi);
-
- return fold_build1 (code, type, op0_expr);
- }
-
- if (TREE_CODE_CLASS (code) == tcc_binary
- || TREE_CODE_CLASS (code) == tcc_comparison)
- {
- tree op0_type = TREE_TYPE (op0);
- enum tree_code op0_code = TREE_CODE (op0);
- tree op0_expr = expand_scalar_variables_expr (op0_type, op0, op0_code,
- NULL, bb, region, rename_map, gsi);
- tree op1_type = TREE_TYPE (op1);
- enum tree_code op1_code = TREE_CODE (op1);
- tree op1_expr = expand_scalar_variables_expr (op1_type, op1, op1_code,
- NULL, bb, region, rename_map, gsi);
-
- return fold_build2 (code, type, op0_expr, op1_expr);
- }
-
- if (code == SSA_NAME)
- return expand_scalar_variables_ssa_name (type, op0, bb, region, rename_map, gsi);
-
- if (code == ADDR_EXPR)
- {
- tree op00 = TREE_OPERAND (op0, 0);
-
- if (handled_component_p (op00)
- && TREE_CODE (op00) == ARRAY_REF)
- {
- tree e = expand_scalar_variables_expr (TREE_TYPE (op00), op00,
- TREE_CODE (op00),
- NULL, bb, region, rename_map, gsi);
- return fold_build1 (code, TREE_TYPE (op0), e);
- }
-
- return op0;
- }
-
- gcc_unreachable ();
- return NULL;
-}
-
-/* Copies at the beginning of BB all the scalar computations on which
- STMT depends on in the SESE: these are all the scalar variables used
- in STMT, defined outside BB and still defined in the SESE, i.e. not a
- parameter of the SESE. The expression that is returned contains
- only induction variables from the generated code: RENAME_MAP contains the
- induction variables renaming mapping, and is used to translate the
- names of induction variables. */
-
-static void
-expand_scalar_variables_stmt (gimple stmt, basic_block bb, sese region,
- htab_t rename_map, gimple_stmt_iterator *gsi)
-{
- ssa_op_iter iter;
- use_operand_p use_p;
-
- FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
- {
- tree use = USE_FROM_PTR (use_p);
- tree type = TREE_TYPE (use);
- enum tree_code code = TREE_CODE (use);
- tree use_expr;
-
- if (!is_gimple_reg (use))
- continue;
-
- /* Don't expand USE if we already have a rename for it. */
- use_expr = get_rename (rename_map, use);
- if (use_expr != use)
- continue;
-
- use_expr = expand_scalar_variables_expr (type, use, code, NULL, bb,
- region, rename_map, gsi);
- use_expr = fold_convert (type, use_expr);
-
- if (use_expr == use)
- continue;
-
- if (is_gimple_debug (stmt))
- {
- if (gimple_debug_bind_p (stmt))
- gimple_debug_bind_reset_value (stmt);
- else
- gcc_unreachable ();
-
- break;
- }
-
- if (TREE_CODE (use_expr) != SSA_NAME)
- {
- tree var = create_tmp_var (type, "var");
-
- use_expr = build2 (MODIFY_EXPR, type, var, use_expr);
- use_expr = force_gimple_operand_gsi (gsi, use_expr, true, NULL,
- true, GSI_SAME_STMT);
- }
-
- replace_exp (use_p, use_expr);
- }
-
- update_stmt (stmt);
-}
-
-/* Copies at the beginning of BB all the scalar computations on which
- BB depends on in the SESE: these are all the scalar variables used
- in BB, defined outside BB and still defined in the SESE, i.e. not a
- parameter of the SESE. The expression that is returned contains
- only induction variables from the generated code: RENAME_MAP contains the
- induction variables renaming mapping, and is used to translate the
- names of induction variables. */
-
-static void
-expand_scalar_variables (basic_block bb, sese region, htab_t rename_map)
-{
- gimple_stmt_iterator gsi;
-
- for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi);)
- {
- gimple stmt = gsi_stmt (gsi);
- expand_scalar_variables_stmt (stmt, bb, region, rename_map, &gsi);
- gsi_next (&gsi);
- }
-}
-
-/* Rename all the SSA_NAMEs from block BB according to the RENAME_MAP. */
-
-static void
-rename_variables (basic_block bb, htab_t rename_map)
-{
- gimple_stmt_iterator gsi;
- gimple_stmt_iterator insert_gsi = gsi_start_bb (bb);
-
- for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi); gsi_next (&gsi))
- rename_variables_in_stmt (gsi_stmt (gsi), rename_map, &insert_gsi);
-}
-
-/* Remove condition from BB. */
-
-static void
-remove_condition (basic_block bb)
-{
- gimple last = last_stmt (bb);
-
- if (last && gimple_code (last) == GIMPLE_COND)
- {
- gimple_stmt_iterator gsi = gsi_last_bb (bb);
- gsi_remove (&gsi, true);
- }
-}
-