#include "gimple-pretty-print.h"
#include "params.h"
#include "expr.h"
+#include "plugin-api.h"
+#include "ipa-ref.h"
+#include "cgraph.h"
+#include "ipa-chkp.h"
/* A vector indexed by SSA_NAME_VERSION. 0 means unknown, positive value
is an index into strinfo vector, negative value stands for
if (si->stmt)
{
gimple stmt = si->stmt, lenstmt;
+ bool with_bounds = gimple_call_with_bounds_p (stmt);
tree callee, lhs, fn, tem;
location_t loc;
gimple_stmt_iterator gsi;
{
case BUILT_IN_STRCAT:
case BUILT_IN_STRCAT_CHK:
+ case BUILT_IN_STRCAT_CHKP:
+ case BUILT_IN_STRCAT_CHK_CHKP:
gsi = gsi_for_stmt (stmt);
fn = builtin_decl_implicit (BUILT_IN_STRLEN);
gcc_assert (lhs == NULL_TREE);
tem = unshare_expr (gimple_call_arg (stmt, 0));
- lenstmt = gimple_build_call (fn, 1, tem);
+ if (with_bounds)
+ {
+ lenstmt = gimple_build_call (chkp_maybe_create_clone (fn)->decl,
+ 2, tem, gimple_call_arg (stmt, 1));
+ gimple_call_set_with_bounds (lenstmt, true);
+ }
+ else
+ lenstmt = gimple_build_call (fn, 1, tem);
lhs = make_ssa_name (TREE_TYPE (TREE_TYPE (fn)), lenstmt);
gimple_call_set_lhs (lenstmt, lhs);
gimple_set_vuse (lenstmt, gimple_vuse (stmt));
/* FALLTHRU */
case BUILT_IN_STRCPY:
case BUILT_IN_STRCPY_CHK:
- if (gimple_call_num_args (stmt) == 2)
+ case BUILT_IN_STRCPY_CHKP:
+ case BUILT_IN_STRCPY_CHK_CHKP:
+ if (gimple_call_num_args (stmt) == (with_bounds ? 4 : 2))
fn = builtin_decl_implicit (BUILT_IN_STPCPY);
else
fn = builtin_decl_explicit (BUILT_IN_STPCPY_CHK);
+ if (with_bounds)
+ fn = chkp_maybe_create_clone (fn)->decl;
gcc_assert (lhs == NULL_TREE);
if (dump_file && (dump_flags & TDF_DETAILS) != 0)
{
/* FALLTHRU */
case BUILT_IN_STPCPY:
case BUILT_IN_STPCPY_CHK:
+ case BUILT_IN_STPCPY_CHKP:
+ case BUILT_IN_STPCPY_CHK_CHKP:
gcc_assert (lhs != NULL_TREE);
loc = gimple_location (stmt);
si->endptr = lhs;
tree vuse, callee, len;
struct laststmt_struct last = laststmt;
strinfo lastsi, firstsi;
+ unsigned len_arg_no = 2;
laststmt.stmt = NULL;
laststmt.len = NULL_TREE;
case BUILT_IN_MEMCPY:
case BUILT_IN_MEMCPY_CHK:
break;
+ case BUILT_IN_MEMCPY_CHKP:
+ case BUILT_IN_MEMCPY_CHK_CHKP:
+ len_arg_no = 4;
+ break;
default:
return;
}
- len = gimple_call_arg (last.stmt, 2);
+ len = gimple_call_arg (last.stmt, len_arg_no);
if (tree_fits_uhwi_p (len))
{
if (!tree_fits_uhwi_p (last.len)
else
return;
- gimple_call_set_arg (last.stmt, 2, last.len);
+ gimple_call_set_arg (last.stmt, len_arg_no, last.len);
update_stmt (last.stmt);
}
tree src;
gimple stmt = gsi_stmt (*gsi);
tree lhs = gimple_call_lhs (stmt);
+ bool with_bounds = gimple_call_with_bounds_p (stmt);
if (lhs == NULL_TREE)
return;
- if (!integer_zerop (gimple_call_arg (stmt, 1)))
+ if (!integer_zerop (gimple_call_arg (stmt, with_bounds ? 2 : 1)))
return;
src = gimple_call_arg (stmt, 0);
gimple stmt = gsi_stmt (*gsi);
strinfo si, dsi, olddsi, zsi;
location_t loc;
+ bool with_bounds = gimple_call_with_bounds_p (stmt);
- src = gimple_call_arg (stmt, 1);
+ src = gimple_call_arg (stmt, with_bounds ? 2 : 1);
dst = gimple_call_arg (stmt, 0);
lhs = gimple_call_lhs (stmt);
idx = get_stridx (src);
{
case BUILT_IN_STRCPY:
case BUILT_IN_STRCPY_CHK:
+ case BUILT_IN_STRCPY_CHKP:
+ case BUILT_IN_STRCPY_CHK_CHKP:
if (lhs != NULL_TREE || !builtin_decl_implicit_p (BUILT_IN_STPCPY))
return;
break;
case BUILT_IN_STPCPY:
case BUILT_IN_STPCPY_CHK:
+ case BUILT_IN_STPCPY_CHKP:
+ case BUILT_IN_STPCPY_CHK_CHKP:
if (lhs == NULL_TREE)
return;
else
switch (bcode)
{
case BUILT_IN_STRCPY:
+ case BUILT_IN_STRCPY_CHKP:
fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
if (lhs)
ssa_ver_to_stridx[SSA_NAME_VERSION (lhs)] = didx;
break;
case BUILT_IN_STRCPY_CHK:
+ case BUILT_IN_STRCPY_CHK_CHKP:
fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
if (lhs)
ssa_ver_to_stridx[SSA_NAME_VERSION (lhs)] = didx;
break;
case BUILT_IN_STPCPY:
+ case BUILT_IN_STPCPY_CHKP:
/* This would need adjustment of the lhs (subtract one),
or detection that the trailing '\0' doesn't need to be
written, if it will be immediately overwritten.
}
break;
case BUILT_IN_STPCPY_CHK:
+ case BUILT_IN_STPCPY_CHK_CHKP:
/* This would need adjustment of the lhs (subtract one),
or detection that the trailing '\0' doesn't need to be
written, if it will be immediately overwritten.
fprintf (dump_file, "Optimizing: ");
print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
}
- if (gimple_call_num_args (stmt) == 2)
- success = update_gimple_call (gsi, fn, 3, dst, src, len);
+ if (with_bounds)
+ {
+ fn = chkp_maybe_create_clone (fn)->decl;
+ if (gimple_call_num_args (stmt) == 4)
+ success = update_gimple_call (gsi, fn, 5, dst,
+ gimple_call_arg (stmt, 1),
+ src,
+ gimple_call_arg (stmt, 3),
+ len);
+ else
+ success = update_gimple_call (gsi, fn, 6, dst,
+ gimple_call_arg (stmt, 1),
+ src,
+ gimple_call_arg (stmt, 3),
+ len,
+ gimple_call_arg (stmt, 4));
+ }
else
- success = update_gimple_call (gsi, fn, 4, dst, src, len,
- gimple_call_arg (stmt, 2));
+ if (gimple_call_num_args (stmt) == 2)
+ success = update_gimple_call (gsi, fn, 3, dst, src, len);
+ else
+ success = update_gimple_call (gsi, fn, 4, dst, src, len,
+ gimple_call_arg (stmt, 2));
if (success)
{
stmt = gsi_stmt (*gsi);
+ gimple_call_set_with_bounds (stmt, with_bounds);
update_stmt (stmt);
if (dump_file && (dump_flags & TDF_DETAILS) != 0)
{
tree src, dst, len, lhs, oldlen, newlen;
gimple stmt = gsi_stmt (*gsi);
strinfo si, dsi, olddsi;
+ bool with_bounds = gimple_call_with_bounds_p (stmt);
- len = gimple_call_arg (stmt, 2);
- src = gimple_call_arg (stmt, 1);
+ len = gimple_call_arg (stmt, with_bounds ? 4 : 2);
+ src = gimple_call_arg (stmt, with_bounds ? 2 : 1);
dst = gimple_call_arg (stmt, 0);
idx = get_stridx (src);
if (idx == 0)
{
case BUILT_IN_MEMCPY:
case BUILT_IN_MEMCPY_CHK:
+ case BUILT_IN_MEMCPY_CHKP:
+ case BUILT_IN_MEMCPY_CHK_CHKP:
/* Allow adjust_last_stmt to decrease this memcpy's size. */
laststmt.stmt = stmt;
laststmt.len = dsi->length;
break;
case BUILT_IN_MEMPCPY:
case BUILT_IN_MEMPCPY_CHK:
+ case BUILT_IN_MEMPCPY_CHKP:
+ case BUILT_IN_MEMPCPY_CHK_CHKP:
break;
default:
gcc_unreachable ();
gimple stmt = gsi_stmt (*gsi);
strinfo si, dsi;
location_t loc;
+ bool with_bounds = gimple_call_with_bounds_p (stmt);
- src = gimple_call_arg (stmt, 1);
+ src = gimple_call_arg (stmt, with_bounds ? 2 : 1);
dst = gimple_call_arg (stmt, 0);
lhs = gimple_call_lhs (stmt);
switch (bcode)
{
case BUILT_IN_STRCAT:
+ case BUILT_IN_STRCAT_CHKP:
if (srclen != NULL_TREE)
fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
else
fn = builtin_decl_implicit (BUILT_IN_STRCPY);
break;
case BUILT_IN_STRCAT_CHK:
+ case BUILT_IN_STRCAT_CHK_CHKP:
if (srclen != NULL_TREE)
fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
else
fn = builtin_decl_explicit (BUILT_IN_STRCPY_CHK);
- objsz = gimple_call_arg (stmt, 2);
+ objsz = gimple_call_arg (stmt, with_bounds ? 4 : 2);
break;
default:
gcc_unreachable ();
fprintf (dump_file, "Optimizing: ");
print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
}
- if (srclen != NULL_TREE)
- success = update_gimple_call (gsi, fn, 3 + (objsz != NULL_TREE),
- dst, src, len, objsz);
+ if (with_bounds)
+ {
+ fn = chkp_maybe_create_clone (fn)->decl;
+ if (srclen != NULL_TREE)
+ success = update_gimple_call (gsi, fn, 5 + (objsz != NULL_TREE),
+ dst,
+ gimple_call_arg (stmt, 1),
+ src,
+ gimple_call_arg (stmt, 3),
+ len, objsz);
+ else
+ success = update_gimple_call (gsi, fn, 4 + (objsz != NULL_TREE),
+ dst,
+ gimple_call_arg (stmt, 1),
+ src,
+ gimple_call_arg (stmt, 3),
+ objsz);
+ }
else
- success = update_gimple_call (gsi, fn, 2 + (objsz != NULL_TREE),
- dst, src, objsz);
+ if (srclen != NULL_TREE)
+ success = update_gimple_call (gsi, fn, 3 + (objsz != NULL_TREE),
+ dst, src, len, objsz);
+ else
+ success = update_gimple_call (gsi, fn, 2 + (objsz != NULL_TREE),
+ dst, src, objsz);
if (success)
{
stmt = gsi_stmt (*gsi);
+ gimple_call_set_with_bounds (stmt, with_bounds);
update_stmt (stmt);
if (dump_file && (dump_flags & TDF_DETAILS) != 0)
{
switch (DECL_FUNCTION_CODE (callee))
{
case BUILT_IN_STRLEN:
+ case BUILT_IN_STRLEN_CHKP:
handle_builtin_strlen (gsi);
break;
case BUILT_IN_STRCHR:
+ case BUILT_IN_STRCHR_CHKP:
handle_builtin_strchr (gsi);
break;
case BUILT_IN_STRCPY:
case BUILT_IN_STRCPY_CHK:
case BUILT_IN_STPCPY:
case BUILT_IN_STPCPY_CHK:
+ case BUILT_IN_STRCPY_CHKP:
+ case BUILT_IN_STRCPY_CHK_CHKP:
+ case BUILT_IN_STPCPY_CHKP:
+ case BUILT_IN_STPCPY_CHK_CHKP:
handle_builtin_strcpy (DECL_FUNCTION_CODE (callee), gsi);
break;
case BUILT_IN_MEMCPY:
case BUILT_IN_MEMCPY_CHK:
case BUILT_IN_MEMPCPY:
case BUILT_IN_MEMPCPY_CHK:
+ case BUILT_IN_MEMCPY_CHKP:
+ case BUILT_IN_MEMCPY_CHK_CHKP:
+ case BUILT_IN_MEMPCPY_CHKP:
+ case BUILT_IN_MEMPCPY_CHK_CHKP:
handle_builtin_memcpy (DECL_FUNCTION_CODE (callee), gsi);
break;
case BUILT_IN_STRCAT:
case BUILT_IN_STRCAT_CHK:
+ case BUILT_IN_STRCAT_CHKP:
+ case BUILT_IN_STRCAT_CHK_CHKP:
handle_builtin_strcat (DECL_FUNCTION_CODE (callee), gsi);
break;
case BUILT_IN_MALLOC: