On Mon, Sep 21, 2015 at 3:33 PM, Daniel Gutson <daniel.gut...@tallertechnologies.com> wrote: > On Mon, Sep 21, 2015 at 2:26 PM, Andrew Pinski <pins...@gmail.com> wrote: >> On Mon, Sep 21, 2015 at 10:20 AM, Daniel Gutson >> <daniel.gut...@tallertechnologies.com> wrote: >>> This is derived from https://gcc.gnu.org/ml/gcc-help/2015-03/msg00091.html >>> >>> Currently, gcc provides an optimization that transforms a call to >>> malloc and a call to memset into a call to calloc. >>> This is fine except when it takes place within the calloc() function >>> implementation itself, causing a recursive call. >>> Two alternatives have been proposed: -fno-malloc-builtin and disable >>> optimizations in calloc(). >>> I think the former is suboptimal since it affects all the code just >>> because of the implementation of one function (calloc()), >>> whereas the latter is suboptimal too since it disables the >>> optimizations in the whole function (calloc too). >>> I think of two alternatives: either make -fno-calloc-builtin to
BTW, this patch implements this alternative. >>> disable the optimization, or make the optimization aware of the >>> function context where it is operating and prevent it to do the >>> transformation if the function is calloc(). >>> >>> Please help me to find the best alternative so we can implent it. >> >> Did you try the optimize attribute? > > That is the 2nd alternative I mentioned as proposed. And I'm also aware that > the optimize attribute is somehow broken. Anyway, as I mentioned, I'd like to > avoid disabling (all the other) optimizations for calloc's implementation. > (FWIW, setting optimize as function attribute to O1 "fixes" the issue). > >> >> Also you can try the following: >> >> size_t ns = size*elements; >> If (ns / elements != size) >> return NULL; >> void *ptr = malloc (ns); >> asm ("":"+r"(ptr)); >> memset (ptr, 0, ns); >> >> Notice I put in a check for overflow in there. > > Thanks, it's a nice workaround. > >> >> Thanks, >> Andrew Pinski >> >> >>> >>> Thanks, >>> >>> Daniel. > > > > -- > > Daniel F. Gutson > Chief Engineering Officer, SPD > > San Lorenzo 47, 3rd Floor, Office 5 > Córdoba, Argentina > > Phone: +54 351 4217888 / +54 351 4218211 > Skype: dgutson > LinkedIn: http://ar.linkedin.com/in/danielgutson -- Daniel F. Gutson Chief Engineering Officer, SPD San Lorenzo 47, 3rd Floor, Office 5 Córdoba, Argentina Phone: +54 351 4217888 / +54 351 4218211 Skype: dgutson LinkedIn: http://ar.linkedin.com/in/danielgutson
diff --git a/gcc/tree-ssa-strlen.c b/gcc/tree-ssa-strlen.c index 34776a3..e17d8e2 100644 --- a/gcc/tree-ssa-strlen.c +++ b/gcc/tree-ssa-strlen.c @@ -1873,6 +1873,8 @@ handle_builtin_memset (gimple_stmt_iterator *gsi) tree size = gimple_call_arg (stmt2, 2); if (code1 == BUILT_IN_CALLOC) /* Not touching stmt1 */ ; + else if (!builtin_decl_declared_p(BUILT_IN_CALLOC)) + return true; else if (code1 == BUILT_IN_MALLOC && operand_equal_p (gimple_call_arg (stmt1, 0), size, 0)) {