aboutsummaryrefslogtreecommitdiff
path: root/gcc/ada/gcc-interface/utils2.c
diff options
context:
space:
mode:
authorEric Botcazou <ebotcazou@adacore.com>2015-11-30 11:46:32 +0000
committerEric Botcazou <ebotcazou@gcc.gnu.org>2015-11-30 11:46:32 +0000
commit933a73251d95fb930028cb76eee7a892f41f867b (patch)
tree04f64660b152400aaf2762b907ade2f74782d611 /gcc/ada/gcc-interface/utils2.c
parentf8125f0c9ce43aac6d7a96753e4617af68ee0009 (diff)
downloadgcc-933a73251d95fb930028cb76eee7a892f41f867b.zip
gcc-933a73251d95fb930028cb76eee7a892f41f867b.tar.gz
gcc-933a73251d95fb930028cb76eee7a892f41f867b.tar.bz2
gigi.h (is_simple_additive_expression): Declare.
* gcc-interface/gigi.h (is_simple_additive_expression): Declare. * gcc-interface/trans.c (struct range_check_info_d): Add DISP and NEG_P fields. (find_loop_for): Add DISP and NEG_P parameters with default value. Call is_simple_additive_expression to handle additive expressions. (Loop_Statement_to_gnu): Deal with displacement in range checks. (Raise_Error_to_gnu): Likewise. (gnat_to_gnu): Add call to find_loop_for. (is_simple_additive_expression): New function extracted from... (gnat_invariant_expr): ...here. Call it on the expression. From-SVN: r231064
Diffstat (limited to 'gcc/ada/gcc-interface/utils2.c')
-rw-r--r--gcc/ada/gcc-interface/utils2.c67
1 files changed, 53 insertions, 14 deletions
diff --git a/gcc/ada/gcc-interface/utils2.c b/gcc/ada/gcc-interface/utils2.c
index a74b8a8..ca307f3 100644
--- a/gcc/ada/gcc-interface/utils2.c
+++ b/gcc/ada/gcc-interface/utils2.c
@@ -2813,6 +2813,52 @@ done:
return exp;
}
+/* Return true if EXPR is the addition or the subtraction of a constant and,
+ if so, set *ADD to the addend, *CST to the constant and *MINUS_P to true
+ if this is a subtraction. */
+
+bool
+is_simple_additive_expression (tree expr, tree *add, tree *cst, bool *minus_p)
+{
+ /* Skip overflow checks. */
+ if (TREE_CODE (expr) == COND_EXPR
+ && TREE_CODE (COND_EXPR_THEN (expr)) == COMPOUND_EXPR
+ && TREE_CODE (TREE_OPERAND (COND_EXPR_THEN (expr), 0)) == CALL_EXPR
+ && get_callee_fndecl (TREE_OPERAND (COND_EXPR_THEN (expr), 0))
+ == gnat_raise_decls[CE_Overflow_Check_Failed])
+ expr = COND_EXPR_ELSE (expr);
+
+ if (TREE_CODE (expr) == PLUS_EXPR)
+ {
+ if (TREE_CONSTANT (TREE_OPERAND (expr, 0)))
+ {
+ *add = TREE_OPERAND (expr, 1);
+ *cst = TREE_OPERAND (expr, 0);
+ *minus_p = false;
+ return true;
+ }
+ else if (TREE_CONSTANT (TREE_OPERAND (expr, 1)))
+ {
+ *add = TREE_OPERAND (expr, 0);
+ *cst = TREE_OPERAND (expr, 1);
+ *minus_p = false;
+ return true;
+ }
+ }
+ else if (TREE_CODE (expr) == MINUS_EXPR)
+ {
+ if (TREE_CONSTANT (TREE_OPERAND (expr, 1)))
+ {
+ *add = TREE_OPERAND (expr, 0);
+ *cst = TREE_OPERAND (expr, 1);
+ *minus_p = true;
+ return true;
+ }
+ }
+
+ return false;
+}
+
/* If EXPR is an expression that is invariant in the current function, in the
sense that it can be evaluated anywhere in the function and any number of
times, return EXPR or an equivalent expression. Otherwise return NULL. */
@@ -2821,6 +2867,8 @@ tree
gnat_invariant_expr (tree expr)
{
const tree type = TREE_TYPE (expr);
+ tree add, cst;
+ bool minus_p;
expr = remove_conversions (expr, false);
@@ -2846,23 +2894,14 @@ gnat_invariant_expr (tree expr)
if (TREE_CONSTANT (expr))
return fold_convert (type, expr);
- /* Skip overflow checks since they don't change the invariantness. */
- if (TREE_CODE (expr) == COND_EXPR
- && TREE_CODE (COND_EXPR_THEN (expr)) == COMPOUND_EXPR
- && TREE_CODE (TREE_OPERAND (COND_EXPR_THEN (expr), 0)) == CALL_EXPR
- && get_callee_fndecl (TREE_OPERAND (COND_EXPR_THEN (expr), 0))
- == gnat_raise_decls[CE_Overflow_Check_Failed])
- expr = COND_EXPR_ELSE (expr);
-
/* Deal with addition or subtraction of constants. */
- if (TREE_CODE (expr) == PLUS_EXPR || TREE_CODE (expr) == MINUS_EXPR)
+ if (is_simple_additive_expression (expr, &add, &cst, &minus_p))
{
- tree op0 = gnat_invariant_expr (TREE_OPERAND (expr, 0));
- tree op1 = TREE_OPERAND (expr, 1);
- if (op0 && TREE_CONSTANT (op1))
+ add = gnat_invariant_expr (add);
+ if (add)
return
- fold_build2 (TREE_CODE (expr), type,
- fold_convert (type, op0), fold_convert (type, op1));
+ fold_build2 (minus_p ? MINUS_EXPR : PLUS_EXPR, type,
+ fold_convert (type, add), fold_convert (type, cst));
else
return NULL_TREE;
}