From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ed1-x532.google.com (mail-ed1-x532.google.com [IPv6:2a00:1450:4864:20::532]) by sourceware.org (Postfix) with ESMTPS id 08B3C3858033 for ; Fri, 12 Nov 2021 17:17:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 08B3C3858033 Received: by mail-ed1-x532.google.com with SMTP id r12so40465120edt.6 for ; Fri, 12 Nov 2021 09:17:30 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=pxbzuPOwf3SzVBizJ6Cnk/Du2hTFUHa9SP5LOk2CoMM=; b=p5qcAHBKUCSIA7N+wgSgwTCu7bI+YCJB2W1ohHS+Zua/Ga2PwtSFmPukj3Hhib1UMQ mQMzFpt5jfLw5YsrB/27o1H/evV0xxcjqahbpue6fc2ebBd/tqbvHupFxuQqPXpJLFuG dhf2g18us+2f0q3sPNPVHLYy8kh2YYKxDAkhA19dm3sxTMrojHD9pdxF/57i6na8rgvc lg3PfEtNllwQB13dayKWjm8QU3fBiF4FZzy3eBetL/Cg7fw5q5/NiS7jAA/q8wygyeGz CG835yRePSR7bY02jPiJ1409xTKrFot2pMC3egmDGDzA2Tbs5NrMaFY8O79frmsmNOMB POPw== X-Gm-Message-State: AOAM5337DdcsOTDn6bxrO94YCtTK8aXP93eynvPagQvgimXrXe23MWZM oUhXMyIilaPJktUXlaMNeBKgmXdLYgz7FBzp5Nih1frnLjE= X-Google-Smtp-Source: ABdhPJxA4xaA/XRvwzWQqo5zn5WE+Idj+O14hAkEzEpZX+1s8KIF/LiBJO7dcrKYsHGHgEtP7kxvzssdIYiSGfhYoTs= X-Received: by 2002:a05:6402:11c8:: with SMTP id j8mr22876310edw.138.1636737449735; Fri, 12 Nov 2021 09:17:29 -0800 (PST) MIME-Version: 1.0 References: <20211111194116.1626980-1-siddhesh@gotplt.org> <20211111194116.1626980-2-siddhesh@gotplt.org> In-Reply-To: <20211111194116.1626980-2-siddhesh@gotplt.org> From: Prathamesh Kulkarni Date: Fri, 12 Nov 2021 22:46:53 +0530 Message-ID: Subject: Re: [PATCH 1/3] gimple-fold: Transform stp*cpy_chk to str*cpy directly To: Siddhesh Poyarekar Cc: gcc-patches@gcc.gnu.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-8.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 12 Nov 2021 17:17:33 -0000 On Fri, 12 Nov 2021 at 01:12, Siddhesh Poyarekar wrote: > > Avoid going through another folding cycle and use the ignore flag to > directly transform BUILT_IN_STPCPY_CHK to BUILT_IN_STRCPY when set, > likewise for BUILT_IN_STPNCPY_CHK to BUILT_IN_STPNCPY. > > Dump the transformation in dump_file so that we can verify in tests that > the direct transformation actually happened. > > gcc/ChangeLog: > > * gimple-fold.c (gimple_fold_builtin_stxcpy_chk, > gimple_fold_builtin_stxncpy_chk): Use BUILT_IN_STRNCPY if return > value is not used. > > gcc/testsuite/ChangeLog: > > * gcc.dg/fold-stringops.c: New test. > > Signed-off-by: Siddhesh Poyarekar > --- > gcc/gimple-fold.c | 50 +++++++++++++++++-------- > gcc/testsuite/gcc.dg/fold-stringops-1.c | 23 ++++++++++++ > 2 files changed, 57 insertions(+), 16 deletions(-) > create mode 100644 gcc/testsuite/gcc.dg/fold-stringops-1.c > > diff --git a/gcc/gimple-fold.c b/gcc/gimple-fold.c > index 6e25a7c05db..92e15784803 100644 > --- a/gcc/gimple-fold.c > +++ b/gcc/gimple-fold.c > @@ -3088,6 +3088,19 @@ gimple_fold_builtin_memory_chk (gimple_stmt_iterator *gsi, > return true; > } > > +static void > +dump_transformation (gimple *from, gimple *to) I assume that both from and to will always be builtin calls ? In that case, perhaps better to use gcall * here (and in rest of patch). Also, needs a top-level comment describing the function. > +{ > + if (dump_file && (dump_flags & TDF_DETAILS)) Perhaps better to use dump_enabled_p ? > + { > + fprintf (dump_file, "transformed "); Perhaps use dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, ...) ? I think you can use gimple_location to get the location. Thanks, Prathamesh > + print_generic_expr (dump_file, gimple_call_fn (from), dump_flags); > + fprintf (dump_file, " to "); > + print_generic_expr (dump_file, gimple_call_fn (to), dump_flags); > + fprintf (dump_file, "\n"); > + } > +} > + > /* Fold a call to the __st[rp]cpy_chk builtin. > DEST, SRC, and SIZE are the arguments to the call. > IGNORE is true if return value can be ignored. FCODE is the BUILT_IN_* > @@ -3184,12 +3197,13 @@ gimple_fold_builtin_stxcpy_chk (gimple_stmt_iterator *gsi, > } > > /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */ > - fn = builtin_decl_explicit (fcode == BUILT_IN_STPCPY_CHK > + fn = builtin_decl_explicit (fcode == BUILT_IN_STPCPY_CHK && !ignore > ? BUILT_IN_STPCPY : BUILT_IN_STRCPY); > if (!fn) > return false; > > gimple *repl = gimple_build_call (fn, 2, dest, src); > + dump_transformation (stmt, repl); > replace_call_with_call_and_fold (gsi, repl); > return true; > } > @@ -3209,19 +3223,6 @@ gimple_fold_builtin_stxncpy_chk (gimple_stmt_iterator *gsi, > bool ignore = gimple_call_lhs (stmt) == NULL_TREE; > tree fn; > > - if (fcode == BUILT_IN_STPNCPY_CHK && ignore) > - { > - /* If return value of __stpncpy_chk is ignored, > - optimize into __strncpy_chk. */ > - fn = builtin_decl_explicit (BUILT_IN_STRNCPY_CHK); > - if (fn) > - { > - gimple *repl = gimple_build_call (fn, 4, dest, src, len, size); > - replace_call_with_call_and_fold (gsi, repl); > - return true; > - } > - } > - > if (! tree_fits_uhwi_p (size)) > return false; > > @@ -3234,7 +3235,23 @@ gimple_fold_builtin_stxncpy_chk (gimple_stmt_iterator *gsi, > For MAXLEN only allow optimizing into non-_ocs function > if SIZE is >= MAXLEN, never convert to __ocs_fail (). */ > if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen)) > - return false; > + { > + if (fcode == BUILT_IN_STPNCPY_CHK && ignore) > + { > + /* If return value of __stpncpy_chk is ignored, > + optimize into __strncpy_chk. */ > + fn = builtin_decl_explicit (BUILT_IN_STRNCPY_CHK); > + if (fn) > + { > + gimple *repl = gimple_build_call (fn, 4, dest, src, len, > + size); > + replace_call_with_call_and_fold (gsi, repl); > + return true; > + } > + } > + > + return false; > + } > } > else > maxlen = len; > @@ -3244,12 +3261,13 @@ gimple_fold_builtin_stxncpy_chk (gimple_stmt_iterator *gsi, > } > > /* If __builtin_st{r,p}ncpy_chk is used, assume st{r,p}ncpy is available. */ > - fn = builtin_decl_explicit (fcode == BUILT_IN_STPNCPY_CHK > + fn = builtin_decl_explicit (fcode == BUILT_IN_STPNCPY_CHK && !ignore > ? BUILT_IN_STPNCPY : BUILT_IN_STRNCPY); > if (!fn) > return false; > > gimple *repl = gimple_build_call (fn, 3, dest, src, len); > + dump_transformation (stmt, repl); > replace_call_with_call_and_fold (gsi, repl); > return true; > } > diff --git a/gcc/testsuite/gcc.dg/fold-stringops-1.c b/gcc/testsuite/gcc.dg/fold-stringops-1.c > new file mode 100644 > index 00000000000..712dd369e7c > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/fold-stringops-1.c > @@ -0,0 +1,23 @@ > +/* { dg-do compile } */ > +/* { dg-options "-O2 -fdump-tree-lower-details" } */ > + > +char dst[2048]; > + > +char * > +copy1 (const char *src, int cond) > +{ > + __builtin___stpncpy_chk (dst, src, 42, __builtin_object_size (dst, 0)); > + > + return dst; > +} > + > +char * > +copy2 (void) > +{ > + __builtin___stpcpy_chk (dst, "Hello world", __builtin_object_size (dst, 0)); > + > + return dst; > +} > +/* { dg-final { scan-tree-dump "transformed __builtin___stpncpy_chk to __builtin_strncpy" "lower" } } */ > +/* { dg-final { scan-tree-dump "transformed __builtin___stpcpy_chk to __builtin_strcpy" "lower" } } */ > + > -- > 2.31.1 >