From patchwork Fri Mar 24 23:47:35 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christopher Li X-Patchwork-Id: 9644249 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 64586602C9 for ; Fri, 24 Mar 2017 23:47:39 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4A9B826490 for ; Fri, 24 Mar 2017 23:47:39 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3B9F92787C; Fri, 24 Mar 2017 23:47:39 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.3 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,RCVD_IN_SORBS_SPAM,T_DKIM_INVALID,T_TVD_MIME_EPI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C131226490 for ; Fri, 24 Mar 2017 23:47:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751504AbdCXXri (ORCPT ); Fri, 24 Mar 2017 19:47:38 -0400 Received: from mail-it0-f54.google.com ([209.85.214.54]:37581 "EHLO mail-it0-f54.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751398AbdCXXri (ORCPT ); Fri, 24 Mar 2017 19:47:38 -0400 Received: by mail-it0-f54.google.com with SMTP id 190so5338093itm.0 for ; Fri, 24 Mar 2017 16:47:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc; bh=q+f7CMKEYXt+qBaVvCrMB6m2P74m/0uVf2uHYwoQzQg=; b=elDKHS+sxe9VlWvD27+ZzW2fjF38XsywewIorgGx1OrfC3SoMJDsLB6fTXWp7TlcR7 FLyBAz2ClZqWULlNOk7tmcvFfdKh1X+/85tYpiCebSB0ESaUi3z1WvCgzW7+iMnV+OcI A50AkAKRe5/rQJfCry2wwkcaHQuiVv2OaOcSpmniQp/q5b10MJ8fCTVs8c5XWNVRLrpF WHTwQA6oWNDIEC7quthWll2Puuc8l/gVRdxkwqRxQ4iRZqdVbnHGOLmrfl2JpPaVAi09 HPzKWTTRrdeXaRPpgut7UnVL2eg8HxaJk7jyMF6RNx8PBMI0FOcitpTHfuJmBrCugwub 5/ZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:sender:in-reply-to:references:from :date:message-id:subject:to:cc; bh=q+f7CMKEYXt+qBaVvCrMB6m2P74m/0uVf2uHYwoQzQg=; b=GgIbedxTrbA/PTGoqfqsooRdw+tjwjhBT3vGRUUWfYNaZKp36aP6hUL54nemF+GwBi iG213sE+OaidDcUNsqHfLgijxzgYTPOTb7bUU16leONvMUJ5M/6x0bmfJMBsOxKQMDWv G0bBtl+XCxKs0Aj8v+kRMP98wxVik+YGmSTZKQoCD04AXhdDPqyiEetb3X+lA3g9aPvX KRJfjQ99tqZk1unlbmknVome7gPiKg/WiX3/S4DQLnMM7ZWWp3bZ+HXXBWyJsFRVOIv0 HKbqJRwpOqBrT0CADr520RcXmxkbXlquVjEOKZKv88rO2aphfKQScFiPOLIbmLDDDRVL Cv5Q== X-Gm-Message-State: AFeK/H3J6iCCnLsOUjUMthFyoQTFiRoopUw31Ixh6iTKDUQQZsUTgAiPmRG44gBSXb/wAIl1Xn03nIs4G5u4GQ== X-Received: by 10.107.183.20 with SMTP id h20mr10913127iof.18.1490399256505; Fri, 24 Mar 2017 16:47:36 -0700 (PDT) MIME-Version: 1.0 Received: by 10.36.69.6 with HTTP; Fri, 24 Mar 2017 16:47:35 -0700 (PDT) In-Reply-To: <20170324081156.qhblvgndemthg7wf@macbook.local> References: <20170321001607.75169-1-luc.vanoostenryck@gmail.com> <20170321001607.75169-4-luc.vanoostenryck@gmail.com> <20170324081156.qhblvgndemthg7wf@macbook.local> From: Christopher Li Date: Fri, 24 Mar 2017 16:47:35 -0700 X-Google-Sender-Auth: 31V073JorDfoKdSUE8JwlT_JUrc Message-ID: Subject: Re: [PATCH v4 03/63] canonicalize compare instructions To: Luc Van Oostenryck Cc: Linux-Sparse , Dibyendu Majumdar , Jeff Garzik , Pekka Enberg Sender: linux-sparse-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP On Fri, Mar 24, 2017 at 1:11 AM, Luc Van Oostenryck wrote: > > Yes, indeed. > In fact, I think it need a name which explain even more > the purpose. I'll see. Sure, I am open to suggestion. BTW, the previous function is call compare_opcode() what it actually do is invert_compare(). I would just move the inverse test outside of this function and call it invert_compare() or invert_compare_opcode() > Yes, it's kinda ugly. > OTOH, given that jumps are cheaper and cheaper on modern CPU > and memory accesses slower and slower, is what you propose > really an optimization? And even if it would, does it matter? Well, jump through a indirect table is quit different than jump to a fix offset. I don't know what modern CPU does now days. It used to be indirect jump will flush the instruction pipe line. > > My preference, if performance mattered here, would be to play > with the bits of the opcode, something like: > #define OP_SET__EQUAL (1 << ...) > #define OP_SET__SIGNED (1 << ...) > #define OP_SET__LTHAN ... > #define OP_SET__GTHAN ... > > ... > > int swap_compare_opcode(opcode) { return opcode ^= (OP_SET__LTHAN|OP_SET__GTHAN); } That is ugly. I don't want that. I cook up an untested patch include here to show what I have in mind. It should be very close to your case statement way of writing it. You are welcome to change it. Chris diff --git a/simplify.c b/simplify.c index 3f39819..6e113d9 100644 --- a/simplify.c +++ b/simplify.c @@ -438,22 +438,24 @@ static int compare_opcode(int opcode, int inverse) static int compare_swap(int opcode) { - switch (opcode) { - case OP_SET_EQ: return OP_SET_EQ; - case OP_SET_NE: return OP_SET_NE; - - case OP_SET_LT: return OP_SET_GT; - case OP_SET_LE: return OP_SET_GE; - case OP_SET_GT: return OP_SET_LT; - case OP_SET_GE: return OP_SET_LE; - - case OP_SET_A: return OP_SET_B; - case OP_SET_AE: return OP_SET_BE; - case OP_SET_B: return OP_SET_A; - case OP_SET_BE: return OP_SET_AE; - default: - return opcode; - } + +#define CMP_OFFSET(opcode) ((opcode) - OP_BINCMP) + static const int swap_opcodes[CMP_OFFSET(OP_BINCMP_END) + 1] = { + [CMP_OFFSET(OP_SET_EQ)] = OP_SET_EQ, + [CMP_OFFSET(OP_SET_NE)] = OP_SET_NE, + + [CMP_OFFSET(OP_SET_LT)] = OP_SET_GT, + [CMP_OFFSET(OP_SET_GT)] = OP_SET_LT, + [CMP_OFFSET(OP_SET_LE)] = OP_SET_GE, + [CMP_OFFSET(OP_SET_GE)] = OP_SET_LE, + + [CMP_OFFSET(OP_SET_A)] = OP_SET_B, + [CMP_OFFSET(OP_SET_B)] = OP_SET_A, + [CMP_OFFSET(OP_SET_AE)] = OP_SET_BE, + [CMP_OFFSET(OP_SET_BE)] = OP_SET_AE, + }; + assert(opcode >= OP_BINCMP && opcode <= OP_BINCMP_END); + return swap_opcodes[CMP_OFFSET(opcode)]; } static int simplify_seteq_setne(struct instruction *insn, long long value)