Index: gcc-interface/decl.c =================================================================== --- gcc-interface/decl.c (revision 228315) +++ gcc-interface/decl.c (working copy) @@ -5585,6 +5585,7 @@ gnat_to_gnu_param (Entity_Id gnat_param, bool ro_param = in_param && !Address_Taken (gnat_param); bool by_return = false, by_component_ptr = false; bool by_ref = false; + bool restricted_aliasing_p = false; tree gnu_param; /* Copy-return is used only for the first parameter of a valued procedure. @@ -5675,15 +5676,12 @@ gnat_to_gnu_param (Entity_Id gnat_param, || (!foreign && default_pass_by_ref (gnu_param_type))))) { + gnu_param_type = build_reference_type (gnu_param_type); /* We take advantage of 6.2(12) by considering that references built for parameters whose type isn't by-ref and for which the mechanism hasn't - been forced to by-ref are restrict-qualified in the C sense. */ - bool restrict_p + been forced to by-ref allow only a restricted form of aliasing. */ + restricted_aliasing_p = !TYPE_IS_BY_REFERENCE_P (gnu_param_type) && mech != By_Reference; - gnu_param_type = build_reference_type (gnu_param_type); - if (restrict_p) - gnu_param_type - = change_qualified_type (gnu_param_type, TYPE_QUAL_RESTRICT); by_ref = true; } @@ -5731,6 +5729,7 @@ gnat_to_gnu_param (Entity_Id gnat_param, DECL_POINTS_TO_READONLY_P (gnu_param) = (ro_param && (by_ref || by_component_ptr)); DECL_CAN_NEVER_BE_NULL_P (gnu_param) = Can_Never_Be_Null (gnat_param); + DECL_RESTRICTED_ALIASING_P (gnu_param) = restricted_aliasing_p; /* If no Mechanism was specified, indicate what we're using, then back-annotate it. */ Index: gcc-interface/trans.c =================================================================== --- gcc-interface/trans.c (revision 228373) +++ gcc-interface/trans.c (working copy) @@ -2714,6 +2714,89 @@ can_be_lower_p (tree val1, tree val2) return tree_int_cst_lt (val1, val2); } +/* Helper function for walk_tree, used by independent_iterations_p below. */ + +static tree +scan_rhs_r (tree *tp, int *walk_subtrees, void *data) +{ + bitmap *params = (bitmap *)data; + tree t = *tp; + + /* No need to walk into types or decls. */ + if (IS_TYPE_OR_DECL_P (t)) + *walk_subtrees = 0; + + if (TREE_CODE (t) == PARM_DECL && bitmap_bit_p (*params, DECL_UID (t))) + return t; + + return NULL_TREE; +} + +/* Return true if STMT_LIST generates independent iterations in a loop. */ + +static bool +independent_iterations_p (tree stmt_list) +{ + tree_stmt_iterator tsi; + bitmap params = BITMAP_GGC_ALLOC(); + auto_vec rhs; + tree iter; + int i; + + if (TREE_CODE (stmt_list) == BIND_EXPR) + stmt_list = BIND_EXPR_BODY (stmt_list); + + /* Scan the list and return false on anything that is not either a check + or an assignment to a parameter with restricted aliasing. */ + for (tsi = tsi_start (stmt_list); !tsi_end_p (tsi); tsi_next (&tsi)) + { + tree stmt = tsi_stmt (tsi); + + switch (TREE_CODE (stmt)) + { + case COND_EXPR: + { + if (COND_EXPR_ELSE (stmt)) + return false; + if (TREE_CODE (COND_EXPR_THEN (stmt)) != CALL_EXPR) + return false; + tree func = get_callee_fndecl (COND_EXPR_THEN (stmt)); + if (!(func && TREE_THIS_VOLATILE (func))) + return false; + break; + } + + case MODIFY_EXPR: + { + tree lhs = TREE_OPERAND (stmt, 0); + while (handled_component_p (lhs)) + lhs = TREE_OPERAND (lhs, 0); + if (TREE_CODE (lhs) != INDIRECT_REF) + return false; + lhs = TREE_OPERAND (lhs, 0); + if (!(TREE_CODE (lhs) == PARM_DECL + && DECL_RESTRICTED_ALIASING_P (lhs))) + return false; + bitmap_set_bit (params, DECL_UID (lhs)); + rhs.safe_push (TREE_OPERAND (stmt, 1)); + break; + } + + default: + return false; + } + } + + /* At this point we know that the list contains only statements that will + modify parameters with restricted aliasing. Check that the statements + don't at the time read from these parameters. */ + FOR_EACH_VEC_ELT (rhs, i, iter) + if (walk_tree_without_duplicates (&iter, scan_rhs_r, ¶ms)) + return false; + + return true; +} + /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Loop_Statement, to a GCC tree, which is returned. */ @@ -3038,6 +3121,13 @@ Loop_Statement_to_gnu (Node_Id gnat_node add_stmt_with_node_force (rci->invariant_cond, gnat_node); } + /* Second, if loop vectorization is enabled and the iterations of the + loop can easily be proved as independent, mark the loop. */ + if (optimize + && flag_tree_loop_vectorize + && independent_iterations_p (LOOP_STMT_BODY (gnu_loop_stmt))) + LOOP_STMT_IVDEP (gnu_loop_stmt) = 1; + add_stmt (gnu_loop_stmt); gnat_poplevel (); gnu_loop_stmt = end_stmt_group (); Index: gcc-interface/ada-tree.h =================================================================== --- gcc-interface/ada-tree.h (revision 228315) +++ gcc-interface/ada-tree.h (working copy) @@ -369,6 +369,21 @@ do { \ in the main unit, i.e. the full declaration is available. */ #define DECL_TAFT_TYPE_P(NODE) DECL_LANG_FLAG_0 (TYPE_DECL_CHECK (NODE)) +/* Nonzero in a PARM_DECL passed by reference but for which only a restricted + form of aliasing is allowed. The first restriction comes explicitly from + the RM 6.2(12) clause: there is no read-after-write dependency between a + store based on such a PARM_DECL and a load not based on this PARM_DECL, + so stores based on such PARM_DECLs can be sunk past all loads based on + a distinct object. The second restriction can be inferred from the same + clause: there is no write-after-write dependency between a store based + on such a PARM_DECL and a store based on a distinct such PARM_DECL, as + the compiler would be allowed to pass the parameters by copy and the + order of assignment to actual parameters after a call is arbitrary as + per the RM 6.4.1(17) clause, so stores based on distinct such PARM_DECLs + can be swapped. */ +#define DECL_RESTRICTED_ALIASING_P(NODE) \ + DECL_LANG_FLAG_0 (PARM_DECL_CHECK (NODE)) + /* Nonzero in a DECL if it is always used by reference, i.e. an INDIRECT_REF is needed to access the object. */ #define DECL_BY_REF_P(NODE) DECL_LANG_FLAG_1 (NODE)