From patchwork Tue Sep 16 14:59:57 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marat Zakirov X-Patchwork-Id: 390084 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4BFD3140111 for ; Wed, 17 Sep 2014 01:00:18 +1000 (EST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :message-id:date:from:mime-version:to:subject:references :in-reply-to:content-type; q=dns; s=default; b=Hrd69w57nbGmjk76t GzzNK6jmbJAfhJ9KQNNkW02aI4L+CWMZe0I98QLCFfY14AZifa2OYHyYxzBoc1ft n80bq0CyxLqW5hltBoCElVzOIwPXxVhBCuSsZLe1s6Jwdkcjb+Q2vKMme/1XOhuo 0/YX5AqRR9xjStd+skkgLcFlOw= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :message-id:date:from:mime-version:to:subject:references :in-reply-to:content-type; s=default; bh=RWw/rDw2Zr/b9QCnUFWJ+tm dG7k=; b=lRSmzrRvQiLcWWs6Rlr/FK8PnbF8RfXZIL14uaZsBhkNTq7X0FN6RXV q/uXylTlDmd/3ur6hp3QrhRtj0VU3tivxhTUuZY+730AddTSn1WAcNG/fAlJaNZg zz6IrUBc3YToBd8yqs616ERupQ68vgIj64ANHBCJ/+fca9bQ1RY4= Received: (qmail 30851 invoked by alias); 16 Sep 2014 15:00:07 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 30753 invoked by uid 89); 16 Sep 2014 15:00:07 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.4 required=5.0 tests=AWL, BAYES_00, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=ham version=3.3.2 X-HELO: mailout3.w1.samsung.com Received: from mailout3.w1.samsung.com (HELO mailout3.w1.samsung.com) (210.118.77.13) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (DES-CBC3-SHA encrypted) ESMTPS; Tue, 16 Sep 2014 15:00:04 +0000 Received: from eucpsbgm1.samsung.com (unknown [203.254.199.244]) by mailout3.w1.samsung.com (Oracle Communications Messaging Server 7u4-24.01(7.0.4.24.0) 64bit (built Nov 17 2011)) with ESMTP id <0NC0008T21SP0D30@mailout3.w1.samsung.com> for gcc-patches@gcc.gnu.org; Tue, 16 Sep 2014 16:02:49 +0100 (BST) Received: from eusync1.samsung.com ( [203.254.199.211]) by eucpsbgm1.samsung.com (EUCPMTA) with SMTP id 00.1C.25543.E6058145; Tue, 16 Sep 2014 15:59:58 +0100 (BST) Received: from [106.109.129.103] by eusync1.samsung.com (Oracle Communications Messaging Server 7u4-23.01(7.0.4.23.0) 64bit (built Aug 10 2011)) with ESMTPA id <0NC000KCO1NXH300@eusync1.samsung.com>; Tue, 16 Sep 2014 15:59:58 +0100 (BST) Message-id: <5418506D.6050806@samsung.com> Date: Tue, 16 Sep 2014 18:59:57 +0400 From: Marat Zakirov User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.1.1 MIME-version: 1.0 To: "gcc-patches@gcc.gnu.org" , kcc@google.com, jakub@redhat.com, Yury Gribov Subject: [PINGv2][PATCH] Asan optimization for aligned accesses. References: <54047383.8000709@samsung.com> <54048C70.6000802@samsung.com> <540495BD.20303@samsung.com> <54059420.9070907@samsung.com> <5405A74E.2070001@samsung.com> <5405B5E5.9030904@samsung.com> <5405BC13.5070504@samsung.com> <5405CBBF.5010202@samsung.com> <5405DC2A.7050503@samsung.com> <5405DDBE.10703@samsung.com> <54104471.8010005@samsung.com> In-reply-to: <54104471.8010005@samsung.com> Content-type: multipart/mixed; boundary=------------080502000206000103020403 On 09/10/2014 04:30 PM, Marat Zakirov wrote: > On 09/02/2014 07:09 PM, Marat Zakirov wrote: >>> Hi all! >>> >>> Here's a simple optimization patch for Asan. It stores alignment >>> information into ASAN_CHECK which is then extracted by sanopt to >>> reduce number of "and 0x7" instructions for sufficiently aligned >>> accesses. I checked it on linux kernel by comparing results of >>> objdump -d -j .text vmlinux | grep "and.*0x7," for optimized and >>> regular cases. It eliminates 12% of and 0x7's. >>> >>> No regressions. Sanitized GCC was successfully Asan-bootstrapped. No >>> false positives were found in kernel. >>> >>> --Marat >>> > gcc/ChangeLog: 2014-09-02 Marat Zakirov * asan.c (build_check_stmt): Alignment arg was added. (asan_expand_check_ifn): Optimization for alignment >= 8. gcc/testsuite/ChangeLog: 2014-09-02 Marat Zakirov * c-c++-common/asan/red-align-1.c: New test. * c-c++-common/asan/red-align-2.c: New test. diff --git a/gcc/asan.c b/gcc/asan.c index 58e7719..aed5ede 100644 --- a/gcc/asan.c +++ b/gcc/asan.c @@ -1639,9 +1639,11 @@ build_check_stmt (location_t loc, tree base, tree len, if (end_instrumented) flags |= ASAN_CHECK_END_INSTRUMENTED; - g = gimple_build_call_internal (IFN_ASAN_CHECK, 3, + g = gimple_build_call_internal (IFN_ASAN_CHECK, 4, build_int_cst (integer_type_node, flags), - base, len); + base, len, + build_int_cst (integer_type_node, + align/BITS_PER_UNIT)); gimple_set_location (g, loc); if (before_p) gsi_insert_before (&gsi, g, GSI_SAME_STMT); @@ -2434,6 +2436,7 @@ asan_expand_check_ifn (gimple_stmt_iterator *iter, bool use_calls) tree base = gimple_call_arg (g, 1); tree len = gimple_call_arg (g, 2); + HOST_WIDE_INT align = tree_to_shwi (gimple_call_arg (g, 3)); HOST_WIDE_INT size_in_bytes = is_scalar_access && tree_fits_shwi_p (len) ? tree_to_shwi (len) : -1; @@ -2547,7 +2550,10 @@ asan_expand_check_ifn (gimple_stmt_iterator *iter, bool use_calls) gimple shadow_test = build_assign (NE_EXPR, shadow, 0); gimple_seq seq = NULL; gimple_seq_add_stmt (&seq, shadow_test); - gimple_seq_add_stmt (&seq, build_assign (BIT_AND_EXPR, base_addr, 7)); + /* Aligned (>= 8 bytes) access do not need & 7. */ + if (align < 8) + gimple_seq_add_stmt (&seq, build_assign (BIT_AND_EXPR, + base_addr, 7)); gimple_seq_add_stmt (&seq, build_type_cast (shadow_type, gimple_seq_last (seq))); if (real_size_in_bytes > 1) diff --git a/gcc/internal-fn.def b/gcc/internal-fn.def index 7ae60f3..54ade9f 100644 --- a/gcc/internal-fn.def +++ b/gcc/internal-fn.def @@ -55,4 +55,4 @@ DEF_INTERNAL_FN (UBSAN_CHECK_SUB, ECF_CONST | ECF_LEAF | ECF_NOTHROW, NULL) DEF_INTERNAL_FN (UBSAN_CHECK_MUL, ECF_CONST | ECF_LEAF | ECF_NOTHROW, NULL) DEF_INTERNAL_FN (ABNORMAL_DISPATCHER, ECF_NORETURN, NULL) DEF_INTERNAL_FN (BUILTIN_EXPECT, ECF_CONST | ECF_LEAF | ECF_NOTHROW, NULL) -DEF_INTERNAL_FN (ASAN_CHECK, ECF_TM_PURE | ECF_LEAF | ECF_NOTHROW, ".W..") +DEF_INTERNAL_FN (ASAN_CHECK, ECF_TM_PURE | ECF_LEAF | ECF_NOTHROW, ".W...") diff --git a/gcc/testsuite/c-c++-common/asan/red-align-1.c b/gcc/testsuite/c-c++-common/asan/red-align-1.c new file mode 100644 index 0000000..1edb3a2 --- /dev/null +++ b/gcc/testsuite/c-c++-common/asan/red-align-1.c @@ -0,0 +1,20 @@ +/* This tests aligment propagation to structure elem and + abcense of redudant & 7. */ + +/* { dg-options "-fdump-tree-sanopt" } */ +/* { dg-do compile } */ +/* { dg-skip-if "" { *-*-* } { "-flto" } { "" } } */ + +struct st { + int a; + int b; + int c; +} __attribute__((aligned(16))); + +int foo (struct st * s_p) +{ + return s_p->a; +} + +/* { dg-final { scan-tree-dump-times "& 7" 0 "sanopt" } } */ +/* { dg-final { cleanup-tree-dump "sanopt" } } */ diff --git a/gcc/testsuite/c-c++-common/asan/red-align-2.c b/gcc/testsuite/c-c++-common/asan/red-align-2.c new file mode 100644 index 0000000..161fe3c --- /dev/null +++ b/gcc/testsuite/c-c++-common/asan/red-align-2.c @@ -0,0 +1,20 @@ +/* This tests aligment propagation to structure elem and + abcense of redudant & 7. */ + +/* { dg-options "-fdump-tree-sanopt" } */ +/* { dg-do compile } */ +/* { dg-skip-if "" { *-*-* } { "-flto" } { "" } } */ + +struct st { + int a; + int b; + int c; +} __attribute__((aligned(16))); + +int foo (struct st * s_p) +{ + return s_p->b; +} + +/* { dg-final { scan-tree-dump-times "& 7" 1 "sanopt" } } */ +/* { dg-final { cleanup-tree-dump "sanopt" } } */