- STRIP_NOPS (top);
- STRIP_NOPS (bot);
-
- if (operand_equal_p (top, bot, 0))
- return build_int_cst (type, 1);
-
- code = TREE_CODE (top);
- switch (code)
- {
- case MULT_EXPR:
- mby = TREE_OPERAND (top, 1);
- if (TREE_CODE (mby) != INTEGER_CST)
- return NULL_TREE;
-
- res = constant_multiple_of (type, TREE_OPERAND (top, 0), bot);
- if (!res)
- return NULL_TREE;
-
- return fold_binary_to_constant (MULT_EXPR, type, res,
- fold_convert (type, mby));
-
- case PLUS_EXPR:
- case MINUS_EXPR:
- p0 = constant_multiple_of (type, TREE_OPERAND (top, 0), bot);
- if (!p0)
- return NULL_TREE;
- p1 = constant_multiple_of (type, TREE_OPERAND (top, 1), bot);
- if (!p1)
- return NULL_TREE;
-
- return fold_binary_to_constant (code, type, p0, p1);
-
- case INTEGER_CST:
- if (TREE_CODE (bot) != INTEGER_CST)
- return NULL_TREE;
-
- bot = fold_convert (type, bot);
- top = fold_convert (type, top);
-
- /* If BOT seems to be negative, try dividing by -BOT instead, and negate
- the result afterwards. */
- if (tree_int_cst_sign_bit (bot))
- {
- negate = true;
- bot = fold_unary_to_constant (NEGATE_EXPR, type, bot);
- }
- else
- negate = false;
-
- /* Ditto for TOP. */
- if (tree_int_cst_sign_bit (top))
- {
- negate = !negate;
- top = fold_unary_to_constant (NEGATE_EXPR, type, top);
- }
-
- if (!zero_p (fold_binary_to_constant (TRUNC_MOD_EXPR, type, top, bot)))
- return NULL_TREE;
-
- res = fold_binary_to_constant (EXACT_DIV_EXPR, type, top, bot);
- if (negate)
- res = fold_unary_to_constant (NEGATE_EXPR, type, res);
- return res;
-
- default:
- return NULL_TREE;
- }
-}
-
-/* Sets COMB to CST. */
-
-static void
-aff_combination_const (struct affine_tree_combination *comb, tree type,
- unsigned HOST_WIDE_INT cst)
-{
- unsigned prec = TYPE_PRECISION (type);
-
- comb->type = type;
- comb->mask = (((unsigned HOST_WIDE_INT) 2 << (prec - 1)) - 1);
-
- comb->n = 0;
- comb->rest = NULL_TREE;
- comb->offset = cst & comb->mask;
-}
-
-/* Sets COMB to single element ELT. */
-
-static void
-aff_combination_elt (struct affine_tree_combination *comb, tree type, tree elt)
-{
- unsigned prec = TYPE_PRECISION (type);
-
- comb->type = type;
- comb->mask = (((unsigned HOST_WIDE_INT) 2 << (prec - 1)) - 1);
-
- comb->n = 1;
- comb->elts[0] = elt;
- comb->coefs[0] = 1;
- comb->rest = NULL_TREE;
- comb->offset = 0;
-}
-
-/* Scales COMB by SCALE. */
-
-static void
-aff_combination_scale (struct affine_tree_combination *comb,
- unsigned HOST_WIDE_INT scale)
-{
- unsigned i, j;
-
- if (scale == 1)
- return;
-
- if (scale == 0)
- {
- aff_combination_const (comb, comb->type, 0);
- return;
- }
-
- comb->offset = (scale * comb->offset) & comb->mask;
- for (i = 0, j = 0; i < comb->n; i++)
- {
- comb->coefs[j] = (scale * comb->coefs[i]) & comb->mask;
- comb->elts[j] = comb->elts[i];
- if (comb->coefs[j] != 0)
- j++;
- }
- comb->n = j;
-
- if (comb->rest)
- {
- if (comb->n < MAX_AFF_ELTS)
- {
- comb->coefs[comb->n] = scale;
- comb->elts[comb->n] = comb->rest;
- comb->rest = NULL_TREE;
- comb->n++;
- }
- else
- comb->rest = fold_build2 (MULT_EXPR, comb->type, comb->rest,
- build_int_cst_type (comb->type, scale));
- }
-}
-
-/* Adds ELT * SCALE to COMB. */
-
-static void
-aff_combination_add_elt (struct affine_tree_combination *comb, tree elt,
- unsigned HOST_WIDE_INT scale)
-{
- unsigned i;
-
- if (scale == 0)
- return;
-
- for (i = 0; i < comb->n; i++)
- if (operand_equal_p (comb->elts[i], elt, 0))
- {
- comb->coefs[i] = (comb->coefs[i] + scale) & comb->mask;
- if (comb->coefs[i])
- return;
-
- comb->n--;
- comb->coefs[i] = comb->coefs[comb->n];
- comb->elts[i] = comb->elts[comb->n];
-
- if (comb->rest)
- {
- gcc_assert (comb->n == MAX_AFF_ELTS - 1);
- comb->coefs[comb->n] = 1;
- comb->elts[comb->n] = comb->rest;
- comb->rest = NULL_TREE;
- comb->n++;
- }
- return;
- }
- if (comb->n < MAX_AFF_ELTS)