/* In parser.cc */
extern tree cp_convert_range_for (tree, tree, tree, cp_decomp *, bool,
- unsigned short, bool);
+ tree, bool);
extern void cp_convert_omp_range_for (tree &, tree &, tree &,
tree &, tree &, tree &, tree &, tree &);
extern void cp_finish_omp_range_for (tree, tree);
extern void finish_else_clause (tree);
extern void finish_if_stmt (tree);
extern tree begin_while_stmt (void);
-extern void finish_while_stmt_cond (tree, tree, bool, unsigned short,
- bool);
+extern void finish_while_stmt_cond (tree, tree, bool, tree, bool);
extern void finish_while_stmt (tree);
extern tree begin_do_stmt (void);
extern void finish_do_body (tree);
-extern void finish_do_stmt (tree, tree, bool, unsigned short,
- bool);
+extern void finish_do_stmt (tree, tree, bool, tree, bool);
extern tree finish_return_stmt (tree);
extern tree begin_for_scope (tree *);
extern tree begin_for_stmt (tree, tree);
extern void finish_init_stmt (tree);
-extern void finish_for_cond (tree, tree, bool, unsigned short,
- bool);
+extern void finish_for_cond (tree, tree, bool, tree, bool);
extern void finish_for_expr (tree, tree);
extern void finish_for_stmt (tree);
extern tree begin_range_for_stmt (tree, tree);
static tree cp_parser_condition
(cp_parser *);
static tree cp_parser_iteration_statement
- (cp_parser *, bool *, bool, unsigned short, bool);
+ (cp_parser *, bool *, bool, tree, bool);
static bool cp_parser_init_statement
(cp_parser *, tree *decl);
static tree cp_parser_for
- (cp_parser *, bool, unsigned short, bool);
+ (cp_parser *, bool, tree, bool);
static tree cp_parser_c_for
- (cp_parser *, tree, tree, bool, unsigned short, bool);
+ (cp_parser *, tree, tree, bool, tree, bool);
static tree cp_parser_range_for
- (cp_parser *, tree, tree, tree, bool, unsigned short, bool, bool);
+ (cp_parser *, tree, tree, tree, bool, tree, bool, bool);
static void do_range_for_auto_deduction
(tree, tree, cp_decomp *);
static tree cp_parser_perform_range_for_lookup
case RID_DO:
case RID_FOR:
std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
- statement = cp_parser_iteration_statement (parser, if_p, false, 0,
- false);
+ statement = cp_parser_iteration_statement (parser, if_p, false,
+ NULL_TREE, false);
break;
case RID_BREAK:
not included. */
static tree
-cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll,
- bool novector)
+cp_parser_for (cp_parser *parser, bool ivdep, tree unroll, bool novector)
{
tree init, scope, decl;
bool is_range_for;
static tree
cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
- unsigned short unroll, bool novector)
+ tree unroll, bool novector)
{
/* Normal for loop */
tree condition = NULL_TREE;
static tree
cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
- bool ivdep, unsigned short unroll, bool novector,
- bool is_omp)
+ bool ivdep, tree unroll, bool novector, bool is_omp)
{
tree stmt, range_expr;
auto_vec <cxx_binding *, 16> bindings;
if (ivdep)
RANGE_FOR_IVDEP (stmt) = 1;
if (unroll)
- RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
+ RANGE_FOR_UNROLL (stmt) = unroll;
if (novector)
RANGE_FOR_NOVECTOR (stmt) = 1;
finish_range_for_decl (stmt, range_decl, range_expr);
tree
cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
- cp_decomp *decomp, bool ivdep, unsigned short unroll,
+ cp_decomp *decomp, bool ivdep, tree unroll,
bool novector)
{
tree begin, end;
static tree
cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
- unsigned short unroll, bool novector)
+ tree unroll, bool novector)
{
cp_token *token;
enum rid keyword;
cp_parser_require (parser, CPP_COLON, RT_COLON);
init = cp_parser_range_for (parser, NULL_TREE, NULL_TREE, decl,
- false, 0, false, true);
+ false, NULL_TREE, false, true);
cp_convert_omp_range_for (this_pre_body, sl, decl,
orig_decl, init, orig_init,
/* Parse a pragma GCC unroll. */
-static unsigned short
+static tree
cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
{
location_t location = cp_lexer_peek_token (parser->lexer)->location;
- tree expr = cp_parser_constant_expression (parser);
- unsigned short unroll;
- expr = maybe_constant_value (expr);
+ tree unroll = cp_parser_constant_expression (parser);
+ unroll = fold_non_dependent_expr (unroll);
HOST_WIDE_INT lunroll = 0;
- if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
- || TREE_CODE (expr) != INTEGER_CST
- || (lunroll = tree_to_shwi (expr)) < 0
- || lunroll >= USHRT_MAX)
+ if (type_dependent_expression_p (unroll))
+ ;
+ else if (!INTEGRAL_TYPE_P (TREE_TYPE (unroll))
+ || (!value_dependent_expression_p (unroll)
+ && (!tree_fits_shwi_p (unroll)
+ || (lunroll = tree_to_shwi (unroll)) < 0
+ || lunroll >= USHRT_MAX)))
{
error_at (location, "%<#pragma GCC unroll%> requires an"
" assignment-expression that evaluates to a non-negative"
" integral constant less than %u", USHRT_MAX);
- unroll = 0;
+ unroll = NULL_TREE;
}
- else
+ else if (TREE_CODE (unroll) == INTEGER_CST)
{
- unroll = (unsigned short)lunroll;
- if (unroll == 0)
- unroll = 1;
+ unroll = fold_convert (integer_type_node, unroll);
+ if (integer_zerop (unroll))
+ unroll = integer_one_node;
}
cp_parser_skip_to_pragma_eol (parser, pragma_tok);
return unroll;
case PRAGMA_NOVECTOR:
{
bool ivdep;
- unsigned short unroll = 0;
+ tree unroll = NULL_TREE;
bool novector = false;
const char *pragma_str;
}
else
{
- unsigned short unroll = (RANGE_FOR_UNROLL (t)
- ? tree_to_uhwi (RANGE_FOR_UNROLL (t)) : 0);
+ tree unroll = RECUR (RANGE_FOR_UNROLL (t));
stmt = cp_convert_range_for (stmt, decl, expr, decomp,
RANGE_FOR_IVDEP (t), unroll,
RANGE_FOR_NOVECTOR (t));
}
case ANNOTATE_EXPR:
- op1 = RECUR (TREE_OPERAND (t, 0));
- RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
- TREE_TYPE (op1), op1,
- RECUR (TREE_OPERAND (t, 1)),
- RECUR (TREE_OPERAND (t, 2))));
+ {
+ op1 = RECUR (TREE_OPERAND (t, 0));
+ tree op2 = RECUR (TREE_OPERAND (t, 1));
+ tree op3 = RECUR (TREE_OPERAND (t, 2));
+ if (TREE_CODE (op2) == INTEGER_CST
+ && wi::to_widest (op2) == (int) annot_expr_unroll_kind)
+ {
+ HOST_WIDE_INT lunroll;
+ if (type_dependent_expression_p (op3))
+ ;
+ else if (!INTEGRAL_TYPE_P (TREE_TYPE (op3))
+ || (!value_dependent_expression_p (op3)
+ && (!tree_fits_shwi_p (op3)
+ || (lunroll = tree_to_shwi (op3)) < 0
+ || lunroll >= USHRT_MAX)))
+ {
+ error_at (EXPR_LOCATION (TREE_OPERAND (t, 2)),
+ "%<#pragma GCC unroll%> requires an "
+ "assignment-expression that evaluates to a "
+ "non-negative integral constant less than %u",
+ USHRT_MAX);
+ op3 = integer_one_node;
+ }
+ else if (TREE_CODE (op3) == INTEGER_CST)
+ {
+ op3 = fold_convert (integer_type_node, op3);
+ if (integer_zerop (op3))
+ op3 = integer_one_node;
+ }
+ }
+ RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
+ TREE_TYPE (op1), op1, op2, op3));
+ }
default:
/* Handle Objective-C++ constructs, if appropriate. */
void
finish_while_stmt_cond (tree cond, tree while_stmt, bool ivdep,
- unsigned short unroll, bool novector)
+ tree unroll, bool novector)
{
cond = maybe_convert_cond (cond);
finish_cond (&WHILE_COND (while_stmt), cond);
WHILE_COND (while_stmt),
build_int_cst (integer_type_node,
annot_expr_unroll_kind),
- build_int_cst (integer_type_node,
- unroll));
+ unroll);
if (novector && cond != error_mark_node)
WHILE_COND (while_stmt) = build3 (ANNOTATE_EXPR,
TREE_TYPE (WHILE_COND (while_stmt)),
COND is as indicated. */
void
-finish_do_stmt (tree cond, tree do_stmt, bool ivdep, unsigned short unroll,
+finish_do_stmt (tree cond, tree do_stmt, bool ivdep, tree unroll,
bool novector)
{
cond = maybe_convert_cond (cond);
if (unroll && cond != error_mark_node)
cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
build_int_cst (integer_type_node, annot_expr_unroll_kind),
- build_int_cst (integer_type_node, unroll));
+ unroll);
if (novector && cond != error_mark_node)
cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
build_int_cst (integer_type_node, annot_expr_no_vector_kind),
FOR_STMT. */
void
-finish_for_cond (tree cond, tree for_stmt, bool ivdep, unsigned short unroll, bool novector)
+finish_for_cond (tree cond, tree for_stmt, bool ivdep, tree unroll,
+ bool novector)
{
cond = maybe_convert_cond (cond);
finish_cond (&FOR_COND (for_stmt), cond);
FOR_COND (for_stmt),
build_int_cst (integer_type_node,
annot_expr_unroll_kind),
- build_int_cst (integer_type_node,
- unroll));
+ unroll);
if (novector && cond != error_mark_node)
FOR_COND (for_stmt) = build3 (ANNOTATE_EXPR,
TREE_TYPE (FOR_COND (for_stmt)),
--- /dev/null
+// PR c++/112795
+// { dg-do compile { target c++11 } }
+// { dg-options "-O2 -fdump-tree-cunrolli-details" }
+
+void baz (int);
+constexpr int n = 3;
+
+template <int N>
+void
+foo ()
+{
+#pragma GCC unroll(n)
+ for (int i = 0; i != n; ++i)
+ baz (i);
+}
+
+template <int N>
+void
+bar ()
+{
+#pragma GCC unroll(N)
+ for (int i = 0; i != N; ++i)
+ baz (i);
+}
+
+void
+qux ()
+{
+ foo <2> ();
+ bar <6> ();
+ bar <10> ();
+}
+
+// { dg-final { scan-tree-dump "loop with 3 iterations completely unrolled" "cunrolli" } }
+// { dg-final { scan-tree-dump "loop with 6 iterations completely unrolled" "cunrolli" } }
+// { dg-final { scan-tree-dump "loop with 10 iterations completely unrolled" "cunrolli" } }
--- /dev/null
+// PR c++/112795
+// { dg-do compile { target c++11 } }
+
+void
+foo ()
+{
+ #pragma GCC unroll 1.0f // { dg-error "'#pragma GCC unroll' requires an assignment-expression that evaluates to a non-negative integral constant less than" }
+ for (int i = 0; i < 2; ++i)
+ ;
+ #pragma GCC unroll 0xffffffffffffffffULL // { dg-error "'#pragma GCC unroll' requires an assignment-expression that evaluates to a non-negative integral constant less than" }
+ for (int i = 0; i < 2; ++i)
+ ;
+ #pragma GCC unroll -42 // { dg-error "'#pragma GCC unroll' requires an assignment-expression that evaluates to a non-negative integral constant less than" }
+ for (int i = 0; i < 2; ++i)
+ ;
+}
+
+template <int N>
+void
+bar ()
+{
+ #pragma GCC unroll 1.0f // { dg-error "'#pragma GCC unroll' requires an assignment-expression that evaluates to a non-negative integral constant less than" }
+ for (int i = 0; i < 2; ++i)
+ ;
+ #pragma GCC unroll 0xffffffffffffffffULL // { dg-error "'#pragma GCC unroll' requires an assignment-expression that evaluates to a non-negative integral constant less than" }
+ for (int i = 0; i < 2; ++i)
+ ;
+ #pragma GCC unroll -42 // { dg-error "'#pragma GCC unroll' requires an assignment-expression that evaluates to a non-negative integral constant less than" }
+ for (int i = 0; i < 2; ++i)
+ ;
+}
+
+template <typename T, int N>
+void
+baz ()
+{
+ #pragma GCC unroll (N + 1.0f) // { dg-error "'#pragma GCC unroll' requires an assignment-expression that evaluates to a non-negative integral constant less than" }
+ for (int i = 0; i < 2; ++i)
+ ;
+ #pragma GCC unroll (N + 0xffffffffffffffffULL)
+ for (int i = 0; i < 2; ++i)
+ ;
+ #pragma GCC unroll (N - 42)
+ for (int i = 0; i < 2; ++i)
+ ;
+ #pragma GCC unroll ((T) 1.0f)
+ for (int i = 0; i < 2; ++i)
+ ;
+ #pragma GCC unroll ((T) 0xffffffffffffffffULL)
+ for (int i = 0; i < 2; ++i)
+ ;
+ #pragma GCC unroll ((T) -42)
+ for (int i = 0; i < 2; ++i)
+ ;
+}
+
+template <typename T, int N>
+void
+qux ()
+{
+ #pragma GCC unroll (N + 1.0f) // { dg-error "'#pragma GCC unroll' requires an assignment-expression that evaluates to a non-negative integral constant less than" }
+ for (int i = 0; i < 2; ++i)
+ ;
+ #pragma GCC unroll (N + 0xffffffffffffffffULL)// { dg-error "'#pragma GCC unroll' requires an assignment-expression that evaluates to a non-negative integral constant less than" }
+ for (int i = 0; i < 2; ++i)
+ ;
+ #pragma GCC unroll (N - 42) // { dg-error "'#pragma GCC unroll' requires an assignment-expression that evaluates to a non-negative integral constant less than" }
+ for (int i = 0; i < 2; ++i)
+ ;
+ #pragma GCC unroll ((T) 1.0f) // { dg-error "'#pragma GCC unroll' requires an assignment-expression that evaluates to a non-negative integral constant less than" }
+ for (int i = 0; i < 2; ++i)
+ ;
+ #pragma GCC unroll ((T) 0xffffffffffffffffULL)// { dg-error "'#pragma GCC unroll' requires an assignment-expression that evaluates to a non-negative integral constant less than" }
+ for (int i = 0; i < 2; ++i)
+ ;
+ #pragma GCC unroll ((T) -42) // { dg-error "'#pragma GCC unroll' requires an assignment-expression that evaluates to a non-negative integral constant less than" }
+ for (int i = 0; i < 2; ++i)
+ ;
+}
+
+void
+corge ()
+{
+ qux <float, 0> ();
+}