diff options
Diffstat (limited to 'gcc/gimple-fold.c')
-rw-r--r-- | gcc/gimple-fold.c | 67 |
1 files changed, 58 insertions, 9 deletions
diff --git a/gcc/gimple-fold.c b/gcc/gimple-fold.c index 403fbb8..87ce3d8 100644 --- a/gcc/gimple-fold.c +++ b/gcc/gimple-fold.c @@ -30,6 +30,7 @@ along with GCC; see the file COPYING3. If not see #include "ssa.h" #include "cgraph.h" #include "gimple-pretty-print.h" +#include "gimple-ssa-warn-restrict.h" #include "fold-const.h" #include "stmt.h" #include "expr.h" @@ -663,13 +664,12 @@ size_must_be_zero_p (tree size) return wi::eq_p (min, wone) && wi::geu_p (max, ssize_max); } -/* Fold function call to builtin mem{{,p}cpy,move}. Return - false if no simplification can be made. - If ENDP is 0, return DEST (like memcpy). - If ENDP is 1, return DEST+LEN (like mempcpy). - If ENDP is 2, return DEST+LEN-1 (like stpcpy). - If ENDP is 3, return DEST, additionally *SRC and *DEST may overlap - (memmove). */ +/* Fold function call to builtin mem{{,p}cpy,move}. Try to detect and + diagnose (otherwise undefined) overlapping copies without preventing + folding. When folded, GCC guarantees that overlapping memcpy has + the same semantics as memmove. Call to the library memcpy need not + provide the same guarantee. Return false if no simplification can + be made. */ static bool gimple_fold_builtin_memory_op (gimple_stmt_iterator *gsi, @@ -681,6 +681,12 @@ gimple_fold_builtin_memory_op (gimple_stmt_iterator *gsi, tree destvar, srcvar; location_t loc = gimple_location (stmt); + tree func = gimple_call_fndecl (stmt); + bool nowarn = gimple_no_warning_p (stmt); + bool check_overlap = (DECL_FUNCTION_CODE (func) != BUILT_IN_MEMMOVE + && DECL_FUNCTION_CODE (func) != BUILT_IN_MEMMOVE_CHK + && !nowarn); + /* If the LEN parameter is a constant zero or in range where the only valid value is zero, return DEST. */ if (size_must_be_zero_p (len)) @@ -704,6 +710,15 @@ gimple_fold_builtin_memory_op (gimple_stmt_iterator *gsi, DEST{,+LEN,+LEN-1}. */ if (operand_equal_p (src, dest, 0)) { + /* Avoid diagnosing exact overlap in calls to __builtin_memcpy. + It's safe and may even be emitted by GCC itself (see bug + 32667). However, diagnose it in explicit calls to the memcpy + function. */ + if (check_overlap && *IDENTIFIER_POINTER (DECL_NAME (func)) != '_') + warning_at (loc, OPT_Wrestrict, + "%qD source argument is the same as destination", + func); + unlink_stmt_vdef (stmt); if (gimple_vdef (stmt) && TREE_CODE (gimple_vdef (stmt)) == SSA_NAME) release_ssa_name (gimple_vdef (stmt)); @@ -753,6 +768,13 @@ gimple_fold_builtin_memory_op (gimple_stmt_iterator *gsi, unsigned ilen = tree_to_uhwi (len); if (pow2p_hwi (ilen)) { + /* Detect invalid bounds and overlapping copies and issue + either -Warray-bounds or -Wrestrict. */ + if (!nowarn + && check_bounds_or_overlap (as_a <gcall *>(stmt), + dest, src, len, len)) + gimple_set_no_warning (stmt, true); + scalar_int_mode mode; tree type = lang_hooks.types.type_for_size (ilen * 8, 1); if (type @@ -1025,6 +1047,11 @@ gimple_fold_builtin_memory_op (gimple_stmt_iterator *gsi, } } + /* Detect invalid bounds and overlapping copies and issue either + -Warray-bounds or -Wrestrict. */ + if (!nowarn) + check_bounds_or_overlap (as_a <gcall *>(stmt), dest, src, len, len); + gimple *new_stmt; if (is_gimple_reg_type (TREE_TYPE (srcvar))) { @@ -1418,7 +1445,7 @@ get_range_strlen (tree arg, tree length[2], bitmap *visited, int type, tree op3 = gimple_assign_rhs3 (def_stmt); return get_range_strlen (op2, length, visited, type, fuzzy, flexp) && get_range_strlen (op3, length, visited, type, fuzzy, flexp); - } + } return false; case GIMPLE_PHI: @@ -1510,12 +1537,19 @@ static bool gimple_fold_builtin_strcpy (gimple_stmt_iterator *gsi, tree dest, tree src) { - location_t loc = gimple_location (gsi_stmt (*gsi)); + gimple *stmt = gsi_stmt (*gsi); + location_t loc = gimple_location (stmt); tree fn; /* If SRC and DEST are the same (and not volatile), return DEST. */ if (operand_equal_p (src, dest, 0)) { + tree func = gimple_call_fndecl (stmt); + + warning_at (loc, OPT_Wrestrict, + "%qD source argument is the same as destination", + func); + replace_call_with_value (gsi, dest); return true; } @@ -2416,6 +2450,15 @@ gimple_fold_builtin_memory_chk (gimple_stmt_iterator *gsi, (resp. DEST+LEN for __mempcpy_chk). */ if (fcode != BUILT_IN_MEMSET_CHK && operand_equal_p (src, dest, 0)) { + if (fcode != BUILT_IN_MEMMOVE && fcode != BUILT_IN_MEMMOVE_CHK) + { + tree func = gimple_call_fndecl (stmt); + + warning_at (loc, OPT_Wrestrict, + "%qD source argument is the same as destination", + func); + } + if (fcode != BUILT_IN_MEMPCPY_CHK) { replace_call_with_value (gsi, dest); @@ -2517,6 +2560,12 @@ gimple_fold_builtin_stxcpy_chk (gimple_stmt_iterator *gsi, /* If SRC and DEST are the same (and not volatile), return DEST. */ if (fcode == BUILT_IN_STRCPY_CHK && operand_equal_p (src, dest, 0)) { + tree func = gimple_call_fndecl (stmt); + + warning_at (loc, OPT_Wrestrict, + "%qD source argument is the same as destination", + func); + replace_call_with_value (gsi, dest); return true; } |