From patchwork Mon Jun 9 19:10:00 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Evgeny Stupachenko X-Patchwork-Id: 357591 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 0ED801400AF for ; Tue, 10 Jun 2014 05:10:55 +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 :mime-version:in-reply-to:references:date:message-id:subject :from:to:cc:content-type; q=dns; s=default; b=pe/kZSpffEwxn43lIl uAOue1GpPJLj7i7IUkic2j9bltSVylAqA3FOodt1HB+by2js5NKDRwCNaTcGSJ7f 8JSwqJ7P1MjyP8ThSqdPyPY+0bMUHgQNXfJ02vFGPVYGNGaPtP1z611j07fZb79n QAy9cUjVu9P55dn5Ts66O1BGc= 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 :mime-version:in-reply-to:references:date:message-id:subject :from:to:cc:content-type; s=default; bh=yFrRwIIKu1GnlrkxmetXJ7NL i3o=; b=wU7YPBaeAOlaic8qygSRljgU8b9/77ekwKG0bP9Uni96n9y03xUF/teS H7LC71N3wAnke3azkCiRouIL9cituKlWdc1a0VZkmKeiNncI6nSApkWe7j9GPTaM TopIIv0XQc24yiHpw99RekrpoRqifHeRMPJ6VnHyaNY53yWM1D4= Received: (qmail 6709 invoked by alias); 9 Jun 2014 19:10:04 -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 6638 invoked by uid 89); 9 Jun 2014 19:10:03 -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, FREEMAIL_FROM, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.2 X-HELO: mail-ob0-f176.google.com Received: from mail-ob0-f176.google.com (HELO mail-ob0-f176.google.com) (209.85.214.176) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Mon, 09 Jun 2014 19:10:02 +0000 Received: by mail-ob0-f176.google.com with SMTP id wo20so6333452obc.21 for ; Mon, 09 Jun 2014 12:10:00 -0700 (PDT) MIME-Version: 1.0 X-Received: by 10.60.175.163 with SMTP id cb3mr27901533oec.29.1402341000411; Mon, 09 Jun 2014 12:10:00 -0700 (PDT) Received: by 10.76.18.209 with HTTP; Mon, 9 Jun 2014 12:10:00 -0700 (PDT) In-Reply-To: <5395E10A.6030103@redhat.com> References: <53909F88.7070009@redhat.com> <5395E10A.6030103@redhat.com> Date: Mon, 9 Jun 2014 23:10:00 +0400 Message-ID: Subject: Re: [PATCH, x86] Improves x86 permutation expand From: Evgeny Stupachenko To: Richard Henderson Cc: Uros Bizjak , GCC Patches X-IsSubscribed: yes Nice catch. Patch with corresponding changes: if (expand_vec_perm_2vperm2f128_vshuf (d)) On Mon, Jun 9, 2014 at 8:30 PM, Richard Henderson wrote: > On 06/09/2014 03:13 AM, Evgeny Stupachenko wrote: >> + /* First we apply one operand permutation to the part where >> + elements stay not in their respective lanes. */ >> + dcopy = *d; >> + if (which == 2) >> + dcopy.op0 = dcopy.op1 = d->op1; >> + else >> + dcopy.op0 = dcopy.op1 = d->op0; >> + dcopy.one_operand_p = true; >> + >> + for (i = 0; i < nelt; ++i) >> + { >> + unsigned e = d->perm[i]; >> + if (which == 2) >> + dcopy.perm[i] = ((e >= nelt) ? (e - nelt) : e); > > This is wrong for which == 1. For both cases this simplifies to > > dcopy.perm[i] = e & (nelt - 1); > >> + >> + for (i = 0; i < nelt; ++i) >> + { >> + unsigned e = d->perm[i]; >> + if (which == 2) >> + dcopy1.perm[i] = ((e >= nelt) ? (nelt + i) : e); >> + else >> + dcopy1.perm[i] = ((e < nelt) ? i : e); >> + } > > This is known to be a blend, so you know the value of E. > Simplifies to > > dcopy1.perm[i] = (e >= nelt ? nelt + i : i); > > > r~ diff --git a/gcc/config/i386/i386.c b/gcc/config/i386/i386.c index 8827256..0b80354 100644 --- a/gcc/config/i386/i386.c +++ b/gcc/config/i386/i386.c @@ -43185,6 +43185,80 @@ expand_vec_perm_palignr (struct expand_vec_perm_d *d) return ok; } +/* A subroutine of ix86_expand_vec_perm_const_1. Try to simplify + the permutation using the SSE4_1 pblendv instruction. Potentially + reduces permutaion from 2 pshufb and or to 1 pshufb and pblendv. */ + +static bool +expand_vec_perm_pblendv (struct expand_vec_perm_d *d) +{ + unsigned i, which, nelt = d->nelt; + struct expand_vec_perm_d dcopy, dcopy1; + enum machine_mode vmode = d->vmode; + bool ok; + + /* Use the same checks as in expand_vec_perm_blend, but skipping + AVX2 as it requires more than 2 instructions for general case. */ + if (d->one_operand_p) + return false; + if (TARGET_AVX && (vmode == V4DFmode || vmode == V8SFmode)) + ; + else if (TARGET_SSE4_1 && GET_MODE_SIZE (vmode) == 16) + ; + else + return false; + + /* Figure out where permutation elements stay not in their + respective lanes. */ + for (i = 0, which = 0; i < nelt; ++i) + { + unsigned e = d->perm[i]; + if (e != i) + which |= (e < nelt ? 1 : 2); + } + /* We can pblend the part where elements stay not in their + respective lanes only when these elements are all in one + half of a permutation. + {0 1 8 3 4 5 9 7} is ok as 8, 9 are at not at their respective + lanes, but both 8 and 9 >= 8 + {0 1 8 3 4 5 2 7} is not ok as 2 and 8 are not at their + respective lanes and 8 >= 8, but 2 not. */ + if (which != 1 && which != 2) + return false; + if (d->testing_p) + return true; + + /* First we apply one operand permutation to the part where + elements stay not in their respective lanes. */ + dcopy = *d; + if (which == 2) + dcopy.op0 = dcopy.op1 = d->op1; + else + dcopy.op0 = dcopy.op1 = d->op0; + dcopy.one_operand_p = true; + + for (i = 0; i < nelt; ++i) + dcopy.perm[i] = d->perm[i] & (nelt - 1); + + ok = expand_vec_perm_1 (&dcopy); + gcc_assert (ok); + + /* Next we put permuted elements into thier positions. */ + dcopy1 = *d; + if (which == 2) + dcopy1.op1 = dcopy.target; + else + dcopy1.op0 = dcopy.target; + + for (i = 0; i < nelt; ++i) + dcopy1.perm[i] = ((d->perm[i] >= nelt) ? (nelt + i) : i); + + ok = expand_vec_perm_blend (&dcopy1); + gcc_assert (ok); + + return true; +} + static bool expand_vec_perm_interleave3 (struct expand_vec_perm_d *d); /* A subroutine of ix86_expand_vec_perm_builtin_1. Try to simplify @@ -44557,6 +44631,9 @@ ix86_expand_vec_perm_const_1 (struct expand_vec_perm_d *d) if (expand_vec_perm_vperm2f128 (d)) return true; + if (expand_vec_perm_pblendv (d)) + return true; + /* Try sequences of three instructions. */