From patchwork Thu May 17 02:14:28 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kugan Vivekanandarajah X-Patchwork-Id: 136078 Delivered-To: patch@linaro.org Received: by 2002:a2e:9706:0:0:0:0:0 with SMTP id r6-v6csp1696401lji; Wed, 16 May 2018 19:16:21 -0700 (PDT) X-Google-Smtp-Source: AB8JxZrtDn0SFZIn+Z+mq+QflUkUrE44qPau7wyojvhgM3WHxn2+8KWbWgbCSQh6cl+ruw6M9BtV X-Received: by 2002:a17:902:a716:: with SMTP id w22-v6mr3215660plq.215.1526523381872; Wed, 16 May 2018 19:16:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526523381; cv=none; d=google.com; s=arc-20160816; b=Bqsa0IT1rTsW9AzI+oh0vcDUNv0pjRyvToUrdatEECJ2F3G5jYE0Uu3CxutqM4MSOT LrW/mHbmKBVK+JcBNjJwCQLg4IYCf2pojTnwK5h+Uef/GwHKhqBMaRXePKhFogtRwONB 6rHP9VWuoMy56WWEVqKb+dYvdL6cqqo1TJrusCqmlhhA8yvri8ZFZk8bmZ6EglGf50Iy HgAnhGX7B4RPLCOKhBLTAy7+DGwidCvLsVtNjobEE1FtHx3EpR7bVxJbgZ22hDZuy+zi 4tV4iTCzy5mEmIaShWQiDe74rDjtAMsLxn1UY9V6tQLL7PXGCnpD4G8TwyyxAlR+/UPY jEVQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=to:subject:message-id:date:from:mime-version:delivered-to:sender :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:mailing-list:dkim-signature:domainkey-signature :arc-authentication-results; bh=A4PEpfq/gnCT9FEQYjvcnfXzgbmn5xTT+C6j/3Qonsc=; b=Hvp7+mGVPnTrk50NHq/RYSWBAly0/jksF1WbJHPCJAH1SEfV+5WUIGVKZsISqJaKDE Z5p2v/POELfJ1EzN35z8geOc8GBVt0B9ttTGZxZl9N6KcXK68wUle+dzmRE3HBf6x0ul y6XNstiIAasSV5HwdI3SZMW59tYMFhwIU0ALUA1mXNMQWDL7wfjG+wLrWzLtvMSiq/Uu 4Xk/eZsSBM7/fQa84Iiz9GrnPaA3NBBGSpnnCjLtf1eHIXCa/XN1mW/fmogIsyd30Rp8 RXz/5LHZYNRbhSjfwOlesKTnWJLqA7odkxo7RhdP1ZeINex60H3nkI4v/NCkEscF9BEY Su4g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=PkKKs7ey; spf=pass (google.com: domain of gcc-patches-return-477791-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=gcc-patches-return-477791-patch=linaro.org@gcc.gnu.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id m11-v6si3880768pls.497.2018.05.16.19.16.21 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 16 May 2018 19:16:21 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-return-477791-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=PkKKs7ey; spf=pass (google.com: domain of gcc-patches-return-477791-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=gcc-patches-return-477791-patch=linaro.org@gcc.gnu.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org 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:from:date:message-id:subject:to:content-type; q= dns; s=default; b=visEV4h7zLIXs3Gs0cZsHmmZovhs+gEUZhCYcS9m2xVcOx 1BtrZyZG5OArWlExUN/JY9rwKTrVQ5l1em6GQO9AHLZ/HFnQ63nBjbDVJWAkc6XF orRWKJ5QWlOs9ucwk8+K0i5azzI47ro4VserydxeQCZKv6/VvHsGr3oLgjOKU= 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:from:date:message-id:subject:to:content-type; s= default; bh=CgxlMEhVbvXq22iSo1athfj0g0U=; b=PkKKs7eyYHF3eZEpmwEQ SkNaYMlXqDaX9J9F2Gau73PUdgQUjsm2Pyye/KHMAKbGYzW4L0eiQY0i5o6D34UD auJ13fNqUczRg04Q0ewS1cUcH/2cNqMareXD3+vY6ABcDKC53x5+KGo+Nq/XQf3N V8JFYImOEhxd5X9ki0RCBXY= Received: (qmail 78861 invoked by alias); 17 May 2018 02:16:08 -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 75541 invoked by uid 89); 17 May 2018 02:15:34 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No 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, RCVD_IN_DNSWL_NONE, SPF_PASS, TIME_LIMIT_EXCEEDED autolearn=unavailable version=3.3.2 spammy=4668, location_t X-HELO: mail-lf0-f42.google.com Received: from mail-lf0-f42.google.com (HELO mail-lf0-f42.google.com) (209.85.215.42) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 17 May 2018 02:15:19 +0000 Received: by mail-lf0-f42.google.com with SMTP id z142-v6so6350208lff.5 for ; Wed, 16 May 2018 19:15:11 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=6z1hKAcAWc5VRXCWPtUQySkHMEm9gLn7N8IIgRVTmFE=; b=B1732mR5BNMtNmcIH5nxOov7uM1OgcQ1DSf3tjz3jJsYrJ6yiJgQgNJKQ/qj6I4twM rRxxY/wm2KiCZ5kb89b9X4F1tHZ6QLyQbKH2LRBCwttL+glWLtR7IDqD7F3Jeo3vf+b6 t+E/SstC3uQiHFnZMlhV1LfEtOtoufUgZR9AuT+AY1UTL+uqZdL1DKm0WLGVXknv3IQt +9oahYU7pCbJyRGFHFBG/EACa5vXoGle13K96mJtFAPmbM+BwSXfaeHb0Af4TMoXDWKR LWU/hA1fbhsXjKJghuUpTcWHUeP6bDi4GKRujihLHlUqA+3fObO1uGHm5IUpwmEncw3J tcmQ== X-Gm-Message-State: ALKqPwc/LroNluovfw6lghzy2IiWM97i1Sm6gPbYGyXj580PPEQg+uk4 4uTdCioXavdYhWC1Xj8pD8HWj/ucdCMuAUe8G/cSHsgkrhU= X-Received: by 2002:a19:d763:: with SMTP id o96-v6mr15822936lfg.89.1526523308940; Wed, 16 May 2018 19:15:08 -0700 (PDT) MIME-Version: 1.0 Received: by 10.46.41.129 with HTTP; Wed, 16 May 2018 19:14:28 -0700 (PDT) From: Kugan Vivekanandarajah Date: Thu, 17 May 2018 12:14:28 +1000 Message-ID: Subject: [RFC][PR64946] "abs" vectorization fails for char/short types To: GCC Patches X-IsSubscribed: yes As mentioned in the PR, I am trying to add ABSU_EXPR to fix this issue. In the attached patch, in fold_cond_expr_with_comparison I am generating ABSU_EXPR for these cases. As I understand, absu_expr is well defined in RTL. So, the issue is generating absu_expr and transferring to RTL in the correct way. I am not sure I am not doing all that is needed. I will clean up and add more test-cases based on the feedback. Thanks, Kugan gcc/ChangeLog: 2018-05-13 Kugan Vivekanandarajah * expr.c (expand_expr_real_2): Handle ABSU_EXPR. * fold-const.c (fold_cond_expr_with_comparison): Generate ABSU_EXPR (fold_unary_loc): Handle ABSU_EXPR. * optabs-tree.c (optab_for_tree_code): Likewise. * tree-cfg.c (verify_expr): Likewise. (verify_gimple_assign_unary): Likewise. * tree-if-conv.c (fold_build_cond_expr): Likewise. * tree-inline.c (estimate_operator_cost): Likewise. * tree-pretty-print.c (dump_generic_node): Likewise. * tree.def (ABSU_EXPR): New. gcc/testsuite/ChangeLog: 2018-05-13 Kugan Vivekanandarajah * gcc.dg/absu.c: New test. diff --git a/gcc/expr.c b/gcc/expr.c index 5e3d9a5..67f8dd1 100644 --- a/gcc/expr.c +++ b/gcc/expr.c @@ -9063,6 +9063,7 @@ expand_expr_real_2 (sepops ops, rtx target, machine_mode tmode, return REDUCE_BIT_FIELD (temp); case ABS_EXPR: + case ABSU_EXPR: op0 = expand_expr (treeop0, subtarget, VOIDmode, EXPAND_NORMAL); if (modifier == EXPAND_STACK_PARM) @@ -9074,7 +9075,8 @@ expand_expr_real_2 (sepops ops, rtx target, machine_mode tmode, /* Unsigned abs is simply the operand. Testing here means we don't risk generating incorrect code below. */ - if (TYPE_UNSIGNED (type)) + if (TYPE_UNSIGNED (type) + && (code != ABSU_EXPR)) return op0; return expand_abs (mode, op0, target, unsignedp, diff --git a/gcc/fold-const.c b/gcc/fold-const.c index 3a99b66..6e80178 100644 --- a/gcc/fold-const.c +++ b/gcc/fold-const.c @@ -5324,8 +5324,17 @@ fold_cond_expr_with_comparison (location_t loc, tree type, case GT_EXPR: if (TYPE_UNSIGNED (TREE_TYPE (arg1))) break; - tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1); - return fold_convert_loc (loc, type, tem); + if (TREE_CODE (arg1) == NOP_EXPR) + { + arg1 = TREE_OPERAND (arg1, 0); + tem = fold_build1_loc (loc, ABSU_EXPR, unsigned_type_for (arg1_type), arg1); + return fold_convert_loc (loc, type, tem); + } + else + { + tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1); + return fold_convert_loc (loc, type, tem); + } case UNLE_EXPR: case UNLT_EXPR: if (flag_trapping_math) @@ -7698,7 +7707,8 @@ fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0) if (arg0) { if (CONVERT_EXPR_CODE_P (code) - || code == FLOAT_EXPR || code == ABS_EXPR || code == NEGATE_EXPR) + || code == FLOAT_EXPR || code == ABS_EXPR + || code == ABSU_EXPR || code == NEGATE_EXPR) { /* Don't use STRIP_NOPS, because signedness of argument type matters. */ diff --git a/gcc/optabs-tree.c b/gcc/optabs-tree.c index 71e172c..2b812e5 100644 --- a/gcc/optabs-tree.c +++ b/gcc/optabs-tree.c @@ -235,6 +235,7 @@ optab_for_tree_code (enum tree_code code, const_tree type, return trapv ? negv_optab : neg_optab; case ABS_EXPR: + case ABSU_EXPR: return trapv ? absv_optab : abs_optab; default: diff --git a/gcc/testsuite/gcc.dg/absu.c b/gcc/testsuite/gcc.dg/absu.c index e69de29..43e651b 100644 --- a/gcc/testsuite/gcc.dg/absu.c +++ b/gcc/testsuite/gcc.dg/absu.c @@ -0,0 +1,39 @@ + +/* { dg-do run } */ +/* { dg-options "-O0" } */ + +#include +#define ABS(x) (((x) >= 0) ? (x) : -(x)) + +#define DEF_TEST(TYPE) \ +void foo_##TYPE (signed TYPE x, unsigned TYPE y){ \ + TYPE t = ABS (x); \ + if (t != y) \ + __builtin_abort (); \ +} \ + +DEF_TEST (char); +DEF_TEST (short); +DEF_TEST (int); +DEF_TEST (long); +void main () +{ + foo_char (SCHAR_MIN + 1, SCHAR_MAX); + foo_char (0, 0); + foo_char (SCHAR_MAX, SCHAR_MAX); + + foo_int (-1, 1); + foo_int (0, 0); + foo_int (INT_MAX, INT_MAX); + foo_int (INT_MIN + 1, INT_MAX); + + foo_short (-1, 1); + foo_short (0, 0); + foo_short (SHRT_MAX, SHRT_MAX); + foo_short (SHRT_MIN + 1, SHRT_MAX); + + foo_long (-1, 1); + foo_long (0, 0); + foo_long (LONG_MAX, LONG_MAX); + foo_long (LONG_MIN + 1, LONG_MAX); +} diff --git a/gcc/tree-cfg.c b/gcc/tree-cfg.c index 9485f73..59a115c 100644 --- a/gcc/tree-cfg.c +++ b/gcc/tree-cfg.c @@ -3167,6 +3167,9 @@ verify_expr (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED) CHECK_OP (0, "invalid operand to unary operator"); break; + case ABSU_EXPR: + break; + case REALPART_EXPR: case IMAGPART_EXPR: case BIT_FIELD_REF: @@ -3937,6 +3940,9 @@ verify_gimple_assign_unary (gassign *stmt) case PAREN_EXPR: case CONJ_EXPR: break; + case ABSU_EXPR: + /* FIXME. */ + return false; case VEC_DUPLICATE_EXPR: if (TREE_CODE (lhs_type) != VECTOR_TYPE diff --git a/gcc/tree-if-conv.c b/gcc/tree-if-conv.c index 71dac4f..13d4c25 100644 --- a/gcc/tree-if-conv.c +++ b/gcc/tree-if-conv.c @@ -466,7 +466,8 @@ fold_build_cond_expr (tree type, tree cond, tree rhs, tree lhs) if (is_gimple_val (cond_expr)) return cond_expr; - if (TREE_CODE (cond_expr) == ABS_EXPR) + if (TREE_CODE (cond_expr) == ABS_EXPR + || TREE_CODE (cond_expr) == ABSU_EXPR) { rhs1 = TREE_OPERAND (cond_expr, 1); STRIP_USELESS_TYPE_CONVERSION (rhs1); diff --git a/gcc/tree-inline.c b/gcc/tree-inline.c index 5a0a252..d272974 100644 --- a/gcc/tree-inline.c +++ b/gcc/tree-inline.c @@ -3866,6 +3866,7 @@ estimate_operator_cost (enum tree_code code, eni_weights *weights, case MIN_EXPR: case MAX_EXPR: case ABS_EXPR: + case ABSU_EXPR: case LSHIFT_EXPR: case RSHIFT_EXPR: diff --git a/gcc/tree-pretty-print.c b/gcc/tree-pretty-print.c index 276ad00..b74d8c9 100644 --- a/gcc/tree-pretty-print.c +++ b/gcc/tree-pretty-print.c @@ -2460,6 +2460,12 @@ dump_generic_node (pretty_printer *pp, tree node, int spc, dump_flags_t flags, pp_greater (pp); break; + case ABSU_EXPR: + pp_string (pp, "ABSU_EXPR <"); + dump_generic_node (pp, TREE_OPERAND (node, 0), spc, flags, false); + pp_greater (pp); + break; + case RANGE_EXPR: NIY; break; diff --git a/gcc/tree.def b/gcc/tree.def index 31de6c0..768167b 100644 --- a/gcc/tree.def +++ b/gcc/tree.def @@ -760,6 +760,7 @@ DEFTREECODE (MAX_EXPR, "max_expr", tcc_binary, 2) An ABS_EXPR must have either an INTEGER_TYPE or a REAL_TYPE. The operand of the ABS_EXPR must have the same type. */ DEFTREECODE (ABS_EXPR, "abs_expr", tcc_unary, 1) +DEFTREECODE (ABSU_EXPR, "absu_expr", tcc_unary, 1) /* Shift operations for shift and rotate. Shift means logical shift if done on an