From patchwork Thu Jan 4 14:31:55 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Richard Earnshaw \(lists\)" X-Patchwork-Id: 123429 Delivered-To: patch@linaro.org Received: by 10.140.22.227 with SMTP id 90csp11500525qgn; Thu, 4 Jan 2018 06:33:06 -0800 (PST) X-Google-Smtp-Source: ACJfBotBdeDy0bXCv2dbiZmG6ceqM458HStkOc7KqHQsyvaAh/7+cTl82ZLhdulzNpc4uiJQoYXr X-Received: by 10.159.207.136 with SMTP id z8mr4898010plo.223.1515076386043; Thu, 04 Jan 2018 06:33:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1515076386; cv=none; d=google.com; s=arc-20160816; b=E+imYQmtWvvDV6CP3hSZEuZXyX5aDV/B5N2PruiSJ3KxqhjkQcT9rNOuduPHPsi7Q2 56AnMqz5Ehjyo39NuwRhbaw+JPdf3IUY66J8y4U/oN/If0P+4lsijqTLp0qEH2X0q2aP BBR/NunCWCMEEjwdGoko4XFv5G4DLKvRr0I5o7p62OujLH3y4aNkLx948lONbEz7STWU 2azvjsriMqD7ciSBVpICzfRKNS6ucJUHMmk9o1VrqXZvVqshATWg/39qA5vA4rFT3Vh1 b07EVMbs0C7cisc48CgJ7e34179K82QitJ+UsI8V+6VRkmA601kNYF/XCvibVIXht/hY BGig== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:references:in-reply-to:references:in-reply-to :message-id:date:subject:cc:to:from:delivered-to:sender:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence :mailing-list:dkim-signature:domainkey-signature :arc-authentication-results; bh=6LVK8g1lCn7XyjXOmHFppgdqpxOhUqo/b8664UqLCOw=; b=YQUhzruntlMmV2EfuOzO1zXEVBVlHNwFVQGyR1/Qu70DtaLDCXvi0wvzCxj/KGSnpB cA0BAcER1sdkj2li7uKW/KXB2onzNLpxKp6PXYCD0r6C6+5Z0Y8zer1GyZKbdppGHbzF i3IRAqB0Uv8ggx4yi1q8TS2xU3BiJIQZYoA4/a1/N2CkJs/gNT98BQ9kf42EyUd+sdRf nZxOQ2Da0ZCLxJL0cb/cAZHdFn8VKm/ZmWGPsC9e+/u9STnKKChM1d3H2CbCTzV+xYoq IZ0Ttl4Mh1+ZpsajHTyi7IGg4UVDsOZvlx8AAJ+Hpxh6ww6KGGJbtN8onEcT2eEKwhn9 60aA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=s0yrQtDj; spf=pass (google.com: domain of gcc-patches-return-470143-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=gcc-patches-return-470143-patch=linaro.org@gcc.gnu.org Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id 61si2359891plz.285.2018.01.04.06.33.05 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 04 Jan 2018 06:33:06 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-return-470143-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=s0yrQtDj; spf=pass (google.com: domain of gcc-patches-return-470143-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=gcc-patches-return-470143-patch=linaro.org@gcc.gnu.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references:mime-version:content-type; q=dns; s= default; b=U5zeel/4+B4TooZ8y2DWe+QyR5uE32Jrvzhbx5Y4oNj/TLNeX0DQW GKyuud1rFPTPRWz6NSPh28pMAhL9OrQ//aJH9uq4bxqbLZ3qNNlD8Vjd7tQEUsWc PQxXnlz0QMZd9yITD+WZD5qC6J5KaWAs5FbdxRsQwDIz3v8DkBetJg= 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:from :to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references:mime-version:content-type; s=default; bh=yrI4F8Wu4Jxl0XhL6CP8YSJE7jE=; b=s0yrQtDjhPDAVb1DYogLvZOGG+OO WR9pZ2MO68p4JiWMAaPGI4f/AjBUfs5rfjtuK7563P0TUCyRVpLLWUOOLLbd0oVX G++u21dSUP0SJic1EGY47Zj+GbZ9wHOWu7+HWZTM/Vlxn4mZTSGPgObjWGcw0Nwv TFpOxTS8uDEu5aM= Received: (qmail 100576 invoked by alias); 4 Jan 2018 14:32:12 -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 100444 invoked by uid 89); 4 Jan 2018 14:32:12 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, SPF_PASS, T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 spammy= X-HELO: foss.arm.com Received: from foss.arm.com (HELO foss.arm.com) (217.140.101.70) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 04 Jan 2018 14:32:09 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 9C3FD15BE; Thu, 4 Jan 2018 06:32:08 -0800 (PST) Received: from e105689-lin.cambridge.arm.com (e105689-lin.cambridge.arm.com [10.2.207.32]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id D3C123F41F; Thu, 4 Jan 2018 06:32:07 -0800 (PST) From: Richard Earnshaw To: gcc-patches@gcc.gnu.org Cc: Richard Earnshaw Subject: [PATCH 3/3] [gcc-7 backport] [arm] Implement support for the de-speculation intrinsic Date: Thu, 4 Jan 2018 14:31:55 +0000 Message-Id: In-Reply-To: References: In-Reply-To: References: MIME-Version: 1.0 This patch implements despeculation on ARM. We only support it when generating ARM or Thumb2 code (we need conditional execution); and we only support it for sizes up to DImode. For unsupported cases we fall back to the generic code generation sequence so that a suitable failure warning is emitted. * config/arm/arm.c (arm_inhibit_load_speculation): New function (TARGET_INHIBIT_LOAD_SPECULATION): Redefine. * config/arm/unspec.md (VUNSPEC_NOSPECULATE): New unspec_volatile code. * config/arm/arm.md (cmp_ior): Make this pattern callable. (nospeculate, nospeculatedi): New patterns. --- gcc/config/arm/arm.c | 108 ++++++++++++++++++++++++++++++++++++++++++++++ gcc/config/arm/arm.md | 40 ++++++++++++++++- gcc/config/arm/unspecs.md | 1 + 3 files changed, 148 insertions(+), 1 deletion(-) diff --git a/gcc/config/arm/arm.c b/gcc/config/arm/arm.c index 7b3f4c1..393bfd6 100644 --- a/gcc/config/arm/arm.c +++ b/gcc/config/arm/arm.c @@ -308,6 +308,8 @@ static unsigned int arm_elf_section_type_flags (tree decl, const char *name, int reloc); static void arm_expand_divmod_libfunc (rtx, machine_mode, rtx, rtx, rtx *, rtx *); static machine_mode arm_floatn_mode (int, bool); +static rtx arm_inhibit_load_speculation (machine_mode, rtx, rtx, rtx, rtx, + rtx, rtx); /* Table of machine attributes. */ static const struct attribute_spec arm_attribute_table[] = @@ -766,6 +768,9 @@ static const struct attribute_spec arm_attribute_table[] = #undef TARGET_CUSTOM_FUNCTION_DESCRIPTORS #define TARGET_CUSTOM_FUNCTION_DESCRIPTORS 2 +#undef TARGET_INHIBIT_LOAD_SPECULATION +#define TARGET_INHIBIT_LOAD_SPECULATION arm_inhibit_load_speculation + struct gcc_target targetm = TARGET_INITIALIZER; /* Obstack for minipool constant handling. */ @@ -31112,4 +31117,107 @@ arm_coproc_ldc_stc_legitimate_address (rtx op) } return false; } + +static rtx +arm_inhibit_load_speculation (machine_mode mode, rtx result, rtx mem, + rtx lower_bound, rtx upper_bound, + rtx fail_result, rtx cmpptr) +{ + rtx cond, comparison; + + /* We can't support this for Thumb1 as we have no suitable conditional + move operations. Nor do we support it for TImode. For both + these cases fall back to the generic code sequence which will emit + a suitable warning for us. */ + if (mode == TImode || TARGET_THUMB1) + return default_inhibit_load_speculation (mode, result, mem, lower_bound, + upper_bound, fail_result, cmpptr); + + + rtx target = gen_reg_rtx (mode); + rtx tgt2 = result; + + if (!register_operand (tgt2, mode)) + tgt2 = gen_reg_rtx (mode); + + if (!register_operand (cmpptr, ptr_mode)) + cmpptr = force_reg (ptr_mode, cmpptr); + + if (upper_bound == NULL) + { + if (!register_operand (lower_bound, ptr_mode)) + lower_bound = force_reg (ptr_mode, lower_bound); + + cond = arm_gen_compare_reg (LTU, cmpptr, lower_bound, NULL); + comparison = gen_rtx_LTU (VOIDmode, cond, const0_rtx); + } + else if (lower_bound == NULL) + { + if (!register_operand (upper_bound, ptr_mode)) + upper_bound = force_reg (ptr_mode, upper_bound); + + cond = arm_gen_compare_reg (GEU, cmpptr, upper_bound, NULL); + comparison = gen_rtx_GEU (VOIDmode, cond, const0_rtx); + } + else + { + /* We want to generate code for + result = (cmpptr < lower || cmpptr >= upper) ? 0 : *ptr; + Which can be recast to + result = (cmpptr < lower || upper <= cmpptr) ? 0 : *ptr; + which can be implemented as + cmp cmpptr, lower + cmpcs upper, cmpptr + bls 1f + ldr result, [ptr] + 1: + movls result, #0 + with suitable IT instructions as needed for thumb2. Later + optimization passes may make the load conditional. */ + + if (!register_operand (lower_bound, ptr_mode)) + lower_bound = force_reg (ptr_mode, lower_bound); + + if (!register_operand (upper_bound, ptr_mode)) + upper_bound = force_reg (ptr_mode, upper_bound); + + rtx comparison1 = gen_rtx_LTU (SImode, cmpptr, lower_bound); + rtx comparison2 = gen_rtx_LEU (SImode, upper_bound, cmpptr); + cond = gen_rtx_REG (arm_select_dominance_cc_mode (comparison1, + comparison2, + DOM_CC_X_OR_Y), + CC_REGNUM); + emit_insn (gen_cmp_ior (cmpptr, lower_bound, upper_bound, cmpptr, + comparison1, comparison2, cond)); + comparison = gen_rtx_NE (SImode, cond, const0_rtx); + } + + rtx_code_label *label = gen_label_rtx (); + emit_jump_insn (gen_arm_cond_branch (label, comparison, cond)); + emit_move_insn (target, mem); + emit_label (label); + + insn_code icode; + + switch (mode) + { + case QImode: icode = CODE_FOR_nospeculateqi; break; + case HImode: icode = CODE_FOR_nospeculatehi; break; + case SImode: icode = CODE_FOR_nospeculatesi; break; + case DImode: icode = CODE_FOR_nospeculatedi; break; + default: + gcc_unreachable (); + } + + if (! insn_operand_matches (icode, 4, fail_result)) + fail_result = force_reg (mode, fail_result); + + emit_insn (GEN_FCN (icode) (tgt2, comparison, cond, target, fail_result)); + + if (tgt2 != result) + emit_move_insn (result, tgt2); + + return result; +} + #include "gt-arm.h" diff --git a/gcc/config/arm/arm.md b/gcc/config/arm/arm.md index f9365cd..7a6c134 100644 --- a/gcc/config/arm/arm.md +++ b/gcc/config/arm/arm.md @@ -9511,7 +9511,7 @@ (set_attr "type" "multiple")] ) -(define_insn "*cmp_ior" +(define_insn "cmp_ior" [(set (match_operand 6 "dominant_cc_register" "") (compare (ior:SI @@ -12054,6 +12054,44 @@ [(set_attr "length" "4") (set_attr "type" "coproc")]) +(define_insn "nospeculate" + [(set (match_operand:QHSI 0 "s_register_operand" "=l,l,r") + (unspec_volatile:QHSI + [(match_operator 1 "arm_comparison_operator" + [(match_operand 2 "cc_register" "") (const_int 0)]) + (match_operand:QHSI 3 "s_register_operand" "0,0,0") + (match_operand:QHSI 4 "arm_not_operand" "I,K,r")] + VUNSPEC_NOSPECULATE))] + "TARGET_32BIT" + { + if (TARGET_THUMB) + return \"it\\t%d1\;mov%d1\\t%0, %4\;.inst 0xf3af8014\t%@ CSDB\"; + return \"mov%d1\\t%0, %4\;.inst 0xe320f014\t%@ CSDB\"; + } + [(set_attr "type" "mov_imm,mvn_imm,mov_reg") + (set_attr "conds" "use") + (set_attr "length" "8")] +) + +(define_insn "nospeculatedi" + [(set (match_operand:DI 0 "s_register_operand" "=r") + (unspec_volatile:DI + [(match_operator 1 "arm_comparison_operator" + [(match_operand 2 "cc_register" "") (const_int 0)]) + (match_operand:DI 3 "s_register_operand" "0") + (match_operand:DI 4 "arm_rhs_operand" "rI")] + VUNSPEC_NOSPECULATE))] + "TARGET_32BIT" + { + if (TARGET_THUMB) + return \"it\\t%d1\;mov%d1\\t%Q0, %Q4\;it\\t%d1\;mov%d1\\t%R0, %R4\;.inst 0xf3af8014\t%@ CSDB\"; + return \"mov%d1\\t%Q0, %Q4\;mov%d1\\t%R0, %R4\;.inst 0xe320f014\t%@ CSDB\"; + } + [(set_attr "type" "mov_reg") + (set_attr "conds" "use") + (set_attr "length" "12")] +) + ;; Vector bits common to IWMMXT and Neon (include "vec-common.md") ;; Load the Intel Wireless Multimedia Extension patterns diff --git a/gcc/config/arm/unspecs.md b/gcc/config/arm/unspecs.md index 99cfa41..7f296ae 100644 --- a/gcc/config/arm/unspecs.md +++ b/gcc/config/arm/unspecs.md @@ -168,6 +168,7 @@ VUNSPEC_MCRR2 ; Represent the coprocessor mcrr2 instruction. VUNSPEC_MRRC ; Represent the coprocessor mrrc instruction. VUNSPEC_MRRC2 ; Represent the coprocessor mrrc2 instruction. + VUNSPEC_NOSPECULATE ; Represent a despeculation sequence. ]) ;; Enumerators for NEON unspecs.