X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=ir%2Fopt%2Fopt_confirms.c;h=0bab0a0e526f279076c105539f13481a894725a5;hb=ce6161a7e42a48f7422b7babcc64d8ace18e2687;hp=95df834e57d9a75d9a3ae886081855027e2407bf;hpb=0d470055af1b3bc6c917e6f004123e5aab47de8f;p=libfirm diff --git a/ir/opt/opt_confirms.c b/ir/opt/opt_confirms.c index 95df834e5..0bab0a0e5 100644 --- a/ir/opt/opt_confirms.c +++ b/ir/opt/opt_confirms.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved. + * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved. * * This file is part of libFirm. * @@ -23,9 +23,7 @@ * @author Michael Beck * @version $Id$ */ -#ifdef HAVE_CONFIG_H -# include "config.h" -#endif +#include "config.h" #undef DEBUG_CONFIRM @@ -33,7 +31,7 @@ #include "irnode_t.h" #include "iropt_t.h" #include "iropt_dbg.h" -#include "opt_confirms.h" +#include "iroptimize.h" #include "irflag_t.h" #include "irprintf.h" @@ -52,15 +50,15 @@ enum range_tags { * such kind of operation, we use border flags allowing * all intervals. */ -typedef struct _interval_t { - tarval *min; /**< lowest border */ - tarval *max; /**< highest border */ +typedef struct interval_t { + ir_tarval *min; /**< lowest border */ + ir_tarval *max; /**< highest border */ unsigned char flags; /**< border flags */ } interval_t; #ifdef DEBUG_CONFIRM -#define compare_iv(l_iv, r_iv, pnc) compare_iv_dbg(l_iv, r_iv, pnc) +#define compare_iv(l_iv, r_iv, pnc) compare_iv_dbg(l_iv, r_iv, pnc) /* forward */ static tarval *compare_iv_dbg(const interval_t *l_iv, const interval_t *r_iv, pn_Cmp pnc); @@ -97,15 +95,26 @@ static tarval *compare_iv_dbg(const interval_t *l_iv, const interval_t *r_iv, pn * This is a often needed case, so we handle here Confirm * nodes too. */ -int value_not_zero(ir_node *n, ir_node **confirm) { +FIRM_API int value_not_zero(const ir_node *n, ir_node_cnst_ptr *confirm) +{ #define RET_ON(x) if (x) { *confirm = n; return 1; }; break - tarval *tv; + ir_tarval *tv; ir_mode *mode = get_irn_mode(n); pn_Cmp pnc; *confirm = NULL; - while (get_irn_op(n) == op_Confirm) { + + /* there might be several Confirms one after other that form an interval */ + for (;;) { + if (is_Minus(n)) { + /* we can safely skip Minus when checking for != 0 */ + n = get_unop_op(n); + continue; + } + if (! is_Confirm(n)) + break; + /* * Note: A Confirm is never after a Const. So, * we simply can check the bound for being a Const @@ -141,8 +150,6 @@ int value_not_zero(ir_node *n, ir_node **confirm) { default: break; } - - /* there might be several Confirms one after other that form an interval */ n = get_Confirm_value(n); } tv = value_of(n); @@ -167,48 +174,92 @@ int value_not_zero(ir_node *n, ir_node **confirm) { * - A SymConst(entity) is NEVER a NULL pointer * - Confirms are evaluated */ -int value_not_null(ir_node *n, ir_node **confirm) { - ir_op *op; +FIRM_API int value_not_null(const ir_node *n, ir_node_cnst_ptr *confirm) +{ + ir_tarval *tv; *confirm = NULL; - n = skip_Cast(n); - op = get_irn_op(n); + n = skip_Cast_const(n); + + tv = value_of(n); + if (tarval_is_constant(tv) && ! tarval_is_null(tv)) + return 1; + assert(mode_is_reference(get_irn_mode(n))); if (get_opt_sel_based_null_check_elim()) { /* skip all Sel nodes and Cast's */ - while (op == op_Sel) { + while (is_Sel(n)) { n = skip_Cast(get_Sel_ptr(n)); - op = get_irn_op(n); } } - if (op == op_SymConst && get_SymConst_kind(n) == symconst_addr_ent) + while (1) { + if (is_Cast(n)) { n = get_Cast_op(n); continue; } + if (is_Proj(n)) { n = get_Proj_pred(n); continue; } + break; + } + + if (is_Global(n)) { + /* global references are never NULL */ + return 1; + } else if (n == get_irg_frame(get_irn_irg(n))) { + /* local references are never NULL */ return 1; - if (op == op_Const) { - tarval *tv = get_Const_tarval(n); - - if (tv != tarval_bad && classify_tarval(tv) != TV_CLASSIFY_NULL) - return 1; - } else if (op == op_Confirm) { - if (get_Confirm_cmp(n) == pn_Cmp_Lg && - classify_Const(get_Confirm_bound(n)) == CNST_NULL) { - *confirm = n; - return 1; + } else if (is_Alloc(n)) { + /* alloc never returns NULL (it throws an exception instead) */ + return 1; + } else { + /* check for more Confirms */ + for (; is_Confirm(n); n = skip_Cast(get_Confirm_value(n))) { + if (get_Confirm_cmp(n) == pn_Cmp_Lg) { + ir_node *bound = get_Confirm_bound(n); + ir_tarval *tv = value_of(bound); + + if (tarval_is_null(tv)) { + *confirm = n; + return 1; + } + } } } return 0; } /* value_not_null */ +#ifdef __cplusplus +extern "C++" { + static inline ir_value_classify_sign operator *(ir_value_classify_sign sign, int mul) { + return (ir_value_classify_sign) (sign*mul); + } +} +#endif + /* * Check, if the value of a node can be confirmed >= 0 or <= 0, * If the mode of the value did not honor signed zeros, else * check for >= 0 or < 0. */ -value_classify_sign classify_value_sign(ir_node *n) { - tarval *tv, *c; +FIRM_API ir_value_classify_sign classify_value_sign(ir_node *n) +{ + ir_tarval *tv, *c; ir_mode *mode; pn_Cmp cmp, ncmp; + int negate = 1; + + for (;;) { + unsigned code = get_irn_opcode(n); - if (get_irn_op(n) != op_Confirm) + switch (code) { + case iro_Minus: + negate *= -1; + n = get_Minus_op(n); + continue; + case iro_Confirm: + break; + default: + return value_classified_unknown; + } + break; + } + if (!is_Confirm(n)) return value_classified_unknown; tv = value_of(get_Confirm_bound(n)); @@ -250,7 +301,7 @@ value_classify_sign classify_value_sign(ir_node *n) { return value_classified_unknown; /* yep, negative */ - return value_classified_negative; + return value_classified_negative * negate; case pn_Cmp_Ge: /* @@ -281,7 +332,7 @@ value_classify_sign classify_value_sign(ir_node *n) { } /* yep, positive */ - return value_classified_positive; + return value_classified_positive * negate; default: return value_classified_unknown; @@ -294,7 +345,8 @@ value_classify_sign classify_value_sign(ir_node *n) { * @return the filled interval or NULL if no interval * can be created (happens only on floating point */ -static interval_t *get_interval_from_tv(interval_t *iv, tarval *tv) { +static interval_t *get_interval_from_tv(interval_t *iv, ir_tarval *tv) +{ ir_mode *mode = get_tarval_mode(tv); if (tv == tarval_bad) { @@ -341,9 +393,10 @@ static interval_t *get_interval_from_tv(interval_t *iv, tarval *tv) { * @return the filled interval or NULL if no interval * can be created (happens only on floating point */ -static interval_t *get_interval(interval_t *iv, ir_node *bound, pn_Cmp pnc) { - ir_mode *mode = get_irn_mode(bound); - tarval *tv = value_of(bound); +static interval_t *get_interval(interval_t *iv, ir_node *bound, pn_Cmp pnc) +{ + ir_mode *mode = get_irn_mode(bound); + ir_tarval *tv = value_of(bound); if (tv == tarval_bad) { /* There is nothing we could do here. For integer @@ -443,10 +496,11 @@ static interval_t *get_interval(interval_t *iv, ir_node *bound, pn_Cmp pnc) { * tarval_b_true or tarval_b_false it it can be evaluated, * tarval_bad else */ -static tarval *(compare_iv)(const interval_t *l_iv, const interval_t *r_iv, pn_Cmp pnc) { - pn_Cmp res; - unsigned flags; - tarval *tv_true = tarval_b_true, *tv_false = tarval_b_false; +static ir_tarval *(compare_iv)(const interval_t *l_iv, const interval_t *r_iv, pn_Cmp pnc) +{ + pn_Cmp res; + unsigned flags; + ir_tarval *tv_true = tarval_b_true, *tv_false = tarval_b_false; /* if one interval contains NaNs, we cannot evaluate anything */ if (! l_iv || ! r_iv) @@ -454,7 +508,7 @@ static tarval *(compare_iv)(const interval_t *l_iv, const interval_t *r_iv, pn_C /* we can only check ordered relations */ if (pnc & pn_Cmp_Uo) { - tarval *t; + ir_tarval *t; pnc = get_negated_pnc(pnc, get_tarval_mode(l_iv->min)); t = tv_true; @@ -567,11 +621,11 @@ static tarval *(compare_iv)(const interval_t *l_iv, const interval_t *r_iv, pn_C /** * Returns non-zero, if a given relation is transitive. */ -static int is_transitive(pn_Cmp pnc) { +static int is_transitive(pn_Cmp pnc) +{ return (pn_Cmp_False < pnc && pnc < pn_Cmp_Lg); } /* is_transitive */ - /** * Return the value of a Cmp if one or both predecessors * are Confirm nodes. @@ -581,41 +635,41 @@ static int is_transitive(pn_Cmp pnc) { * @param right the right operand of the Cmp * @param pnc the compare relation */ -tarval *computed_value_Cmp_Confirm(ir_node *cmp, ir_node *left, ir_node *right, pn_Cmp pnc) { +FIRM_API ir_tarval *computed_value_Cmp_Confirm(ir_node *cmp, ir_node *left, ir_node *right, pn_Cmp pnc) +{ ir_node *l_bound; - pn_Cmp l_pnc, res_pnc, neg_pnc; - interval_t l_iv, r_iv; - tarval *tv; + pn_Cmp l_pnc, res_pnc, neg_pnc; + interval_t l_iv, r_iv; + ir_tarval *tv; ir_mode *mode; - if (get_irn_op(right) == op_Confirm) { - ir_node *t; - + if (is_Confirm(right)) { /* we want the Confirm on the left side */ - t = left; - left = right; - right = t; + ir_node *t = right; + right = left; + left = t; pnc = get_inversed_pnc(pnc); - } else if (get_irn_op(left) != op_Confirm) { - /* no Confirm on either one side, finish */ - return tarval_bad; + } else if (! is_Confirm(left)) { + /* nothing more found */ + tv = tarval_bad; + goto check_null_case; } /* ok, here at least left is a Confirm, right might be */ l_bound = get_Confirm_bound(left); l_pnc = get_Confirm_cmp(left); - if (get_irn_op(right) == op_Confirm) { + if (is_Confirm(right)) { /* - * both sides are Confirm's. Check some rare cases first. - */ + * both sides are Confirm's. Check some rare cases first. + */ ir_node *r_bound = get_Confirm_bound(right); - pn_Cmp r_pnc = get_Confirm_cmp(right); + pn_Cmp r_pnc = get_Confirm_cmp(right); /* - * some check can be made WITHOUT constant bounds - */ + * some check can be made WITHOUT constant bounds + */ if (r_bound == l_bound) { if (is_transitive(l_pnc)) { pn_Cmp r_inc_pnc = get_inversed_pnc(r_pnc); @@ -732,6 +786,24 @@ tarval *computed_value_Cmp_Confirm(ir_node *cmp, ir_node *left, ir_node *right, get_interval(&l_iv, l_bound, l_pnc), get_interval_from_tv(&r_iv, tv), pnc); + } else { +check_null_case: + /* check some other cases */ + if ((pnc == pn_Cmp_Eq || pnc == pn_Cmp_Lg) && + is_Const(right) && is_Const_null(right)) { + /* for == 0 or != 0 we have some special tools */ + ir_mode *mode = get_irn_mode(left); + const ir_node *dummy; + if (mode_is_reference(mode)) { + if (value_not_null(left, &dummy)) { + tv = pnc == pn_Cmp_Eq ? tarval_b_false : tarval_b_true; + } + } else { + if (value_not_zero(left, &dummy)) { + tv = pnc == pn_Cmp_Eq ? tarval_b_false : tarval_b_true; + } + } + } } if (tv != tarval_bad) @@ -748,7 +820,8 @@ tarval *computed_value_Cmp_Confirm(ir_node *cmp, ir_node *left, ir_node *right, * @param len length of the string buffer * @param iv the interval */ -static int iv_snprintf(char *buf, size_t len, const interval_t *iv) { +static int iv_snprintf(char *buf, size_t len, const interval_t *iv) +{ char smin[64], smax[64]; if (iv) { @@ -775,7 +848,8 @@ static int iv_snprintf(char *buf, size_t len, const interval_t *iv) { * @param r_iv the right interval * @param pnc the compare relation */ -static void print_iv_cmp(const interval_t *l_iv, const interval_t *r_iv, pn_Cmp pnc) { +static void print_iv_cmp(const interval_t *l_iv, const interval_t *r_iv, pn_Cmp pnc) +{ char sl[128], sr[128]; iv_snprintf(sl, sizeof(sl), l_iv); @@ -791,7 +865,8 @@ static void print_iv_cmp(const interval_t *l_iv, const interval_t *r_iv, pn_Cmp * @param r_iv the right interval * @param pnc the compare relation */ -static tarval *compare_iv_dbg(const interval_t *l_iv, const interval_t *r_iv, pn_Cmp pnc) { +static tarval *compare_iv_dbg(const interval_t *l_iv, const interval_t *r_iv, pn_Cmp pnc) +{ tarval *tv = (compare_iv)(l_iv, r_iv, pnc); if (tv == tarval_bad)