From patchwork Thu Nov 29 14:00:22 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 1005388 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-491239-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="QSp9c8+F"; dkim-atps=neutral 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 435K1y1fJYz9ryk for ; Fri, 30 Nov 2018 01:01:25 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:reply-to:mime-version :content-type; q=dns; s=default; b=F3kFxBmlBEpl6hf0ByPqX9oPJlgX5 /8dM9god8LjibNqu0fgxxVVkkq9RsS4ao9hvLPNj+4cm3beYeZv9sWjulhGov59L I/dpN5kdSD5qisRz2VJoAu/LThYlGf5T+E2awshhXr7amexID20JIDqNc1ZJu6uc 5t9R00nPdILl1I= 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:date :from:to:cc:subject:message-id:reply-to:mime-version :content-type; s=default; bh=1h5wZTUsWY70LOR7OUO2NKNH9KI=; b=QSp 9c8+FyYVv1o4iMKl8vd5nmR6kea2RxFjEE77a+6l2xE5UmTKYaGc8I6S5BYHjKBZ 3AINpPKEo6eKcTKdgg6HaFeTL7nZAj1oHshDiQj5r52RPhii+IpGXnzgaRdEXPZb LRPx+S5onK8iTpbvXEJ9e742Yv3i1XBmrGLKi92A= Received: (qmail 57827 invoked by alias); 29 Nov 2018 14:01:00 -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 57624 invoked by uid 89); 29 Nov 2018 14:00:47 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-11.9 required=5.0 tests=BAYES_00, GIT_PATCH_2, GIT_PATCH_3, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=PLUS, altivec.h, altivech, UD:altivec.h X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 29 Nov 2018 14:00:41 +0000 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id A21053B717; Thu, 29 Nov 2018 14:00:28 +0000 (UTC) Received: from tucnak.zalov.cz (ovpn-117-214.ams2.redhat.com [10.36.117.214]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 42A0A26DFC; Thu, 29 Nov 2018 14:00:28 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.15.2/8.15.2) with ESMTP id wATE0P0q028302; Thu, 29 Nov 2018 15:00:25 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.15.2/8.15.2/Submit) id wATE0MTD025639; Thu, 29 Nov 2018 15:00:22 +0100 Date: Thu, 29 Nov 2018 15:00:22 +0100 From: Jakub Jelinek To: Segher Boessenkool , David Edelsohn Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] Fix vec_{add, sub} rs6000_gimple_fold_builtin folding (PR target/88234) Message-ID: <20181129140022.GI12380@tucnak> Reply-To: Jakub Jelinek MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.9.2 (2017-12-15) X-IsSubscribed: yes Hi! vec_add/sub of with vector unsigned args is lowered to a builtin which has vector signed args and therefore if not -fwrapv it is undefined if signed integer overflow occurs in those vectors. The following patch fixes it to make sure that those builtins are folded to PLUS/MINUS_EXPR done on unsigned vectors instead, so there is no UB. If it makes it through to RTL expansion, it makes no difference, but for UBSan it matters a lot and also I'd say if e.g. we'd extract just one scalar from the resulting vector, we'd optimize it just to a scalar +/- and could very well optimize based on lack of UB. I've looked at a couple of other builtins, but e.g. with vec_mul* couldn't trigger anything problematic. Bootstrapped/regtested on powerpc64{,le}-linux, ok for trunk? 2018-11-29 Jakub Jelinek PR target/88234 * config/rs6000/rs6000.c (rs6000_gimple_fold_builtin): For vec_add and vec_sub builtins, perform PLUS_EXPR or MINUS_EXPR in unsigned_type_for instead of vector integral type where overflow doesn't wrap. * gcc.dg/ubsan/pr88234.c: New test. Jakub --- gcc/config/rs6000/rs6000.c.jj 2018-11-29 08:41:29.753806139 +0100 +++ gcc/config/rs6000/rs6000.c 2018-11-29 11:39:04.783862074 +0100 @@ -15371,6 +15371,7 @@ rs6000_gimple_fold_builtin (gimple_stmt_ enum rs6000_builtins fn_code = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl); tree arg0, arg1, lhs, temp; + enum tree_code bcode; gimple *g; size_t uns_fncode = (size_t) fn_code; @@ -15409,10 +15410,32 @@ rs6000_gimple_fold_builtin (gimple_stmt_ case P8V_BUILTIN_VADDUDM: case ALTIVEC_BUILTIN_VADDFP: case VSX_BUILTIN_XVADDDP: + bcode = PLUS_EXPR; + do_binary: arg0 = gimple_call_arg (stmt, 0); arg1 = gimple_call_arg (stmt, 1); lhs = gimple_call_lhs (stmt); - g = gimple_build_assign (lhs, PLUS_EXPR, arg0, arg1); + if (INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (lhs))) + && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (TREE_TYPE (lhs)))) + { + /* Ensure the binary operation is performed in a type + that wraps if it is integral type. */ + gimple_seq stmts = NULL; + tree type = unsigned_type_for (TREE_TYPE (lhs)); + tree uarg0 = gimple_build (&stmts, VIEW_CONVERT_EXPR, + type, arg0); + tree uarg1 = gimple_build (&stmts, VIEW_CONVERT_EXPR, + type, arg1); + tree res = gimple_build (&stmts, gimple_location (stmt), bcode, + type, uarg0, uarg1); + gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT); + g = gimple_build_assign (lhs, VIEW_CONVERT_EXPR, + build1 (VIEW_CONVERT_EXPR, + TREE_TYPE (lhs), res)); + gsi_replace (gsi, g, true); + return true; + } + g = gimple_build_assign (lhs, bcode, arg0, arg1); gimple_set_location (g, gimple_location (stmt)); gsi_replace (gsi, g, true); return true; @@ -15424,13 +15447,8 @@ rs6000_gimple_fold_builtin (gimple_stmt_ case P8V_BUILTIN_VSUBUDM: case ALTIVEC_BUILTIN_VSUBFP: case VSX_BUILTIN_XVSUBDP: - arg0 = gimple_call_arg (stmt, 0); - arg1 = gimple_call_arg (stmt, 1); - lhs = gimple_call_lhs (stmt); - g = gimple_build_assign (lhs, MINUS_EXPR, arg0, arg1); - gimple_set_location (g, gimple_location (stmt)); - gsi_replace (gsi, g, true); - return true; + bcode = MINUS_EXPR; + goto do_binary; case VSX_BUILTIN_XVMULSP: case VSX_BUILTIN_XVMULDP: arg0 = gimple_call_arg (stmt, 0); --- gcc/testsuite/gcc.dg/ubsan/pr88234.c.jj 2018-11-29 12:13:06.879735598 +0100 +++ gcc/testsuite/gcc.dg/ubsan/pr88234.c 2018-11-29 12:13:54.594937165 +0100 @@ -0,0 +1,29 @@ +/* PR target/88234 */ +/* { dg-do run { target { powerpc*-*-* && vmx_hw } } } */ +/* { dg-require-effective-target powerpc_altivec_ok } */ +/* { dg-options "-fsanitize=signed-integer-overflow -fno-sanitize-recover=signed-integer-overflow -O2 -maltivec" } */ + +#include + +__attribute__((noipa)) vector unsigned int +f1 (vector unsigned int x, vector unsigned int y) +{ + return vec_add (x, y); +} + +__attribute__((noipa)) vector unsigned int +f2 (vector unsigned int x, vector unsigned int y) +{ + return vec_sub (x, y); +} + +int +main () +{ + vector unsigned int x = { __INT_MAX__, -__INT_MAX__, __INT_MAX__ - 3, -__INT_MAX__ + 4 }; + vector unsigned int y = { 1, -1, 4, -5 }; + vector unsigned int z = f1 (x, y); + f2 (z, x); + f2 (z, y); + return 0; +}