From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1816) id 8352E38582B7; Mon, 12 Jun 2023 12:25:56 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8352E38582B7 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1686572756; bh=rgnOPnIl9dGrZMLNCKVcj1p3aHHWBvuJ0kF3D4xCp80=; h=From:To:Subject:Date:From; b=tWT5WDYE06zxInxUYp9P5F40ZuIuQmPsnNByAmTOdh0oKB/oc3uZ9Ne95iJaORCd4 PulAwKSFxNaS5bc5qaEA5TNYf+xEoPDa2KplBhQwGgfOGQNCvvO4dD7hp13Hxwu4gx 08KjYDy8Hb14lnv1MSpzZ6cpGlJeqI0JU4tHCCCQ= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Kyrylo Tkachov To: gcc-cvs@gcc.gnu.org Subject: [gcc r14-1712] simplify-rtx: Implement constant folding of SS_TRUNCATE, US_TRUNCATE X-Act-Checkin: gcc X-Git-Author: Kyrylo Tkachov X-Git-Refname: refs/heads/master X-Git-Oldrev: 84cbf560ff62a57432374d8a878251234ffcb99a X-Git-Newrev: 921b841350c4fc298d09f6c5674663e0f4208610 Message-Id: <20230612122556.8352E38582B7@sourceware.org> Date: Mon, 12 Jun 2023 12:25:56 +0000 (GMT) List-Id: https://gcc.gnu.org/g:921b841350c4fc298d09f6c5674663e0f4208610 commit r14-1712-g921b841350c4fc298d09f6c5674663e0f4208610 Author: Kyrylo Tkachov Date: Mon Jun 12 11:42:29 2023 +0100 simplify-rtx: Implement constant folding of SS_TRUNCATE, US_TRUNCATE This patch implements RTL constant-folding for the SS_TRUNCATE and US_TRUNCATE codes. The semantics are a clamping operation on the argument with the min and max of the narrow mode, followed by a truncation. The signedness of the clamp and the min/max extrema is derived from the signedness of the saturating operation. We have a number of instructions in aarch64 that use SS_TRUNCATE and US_TRUNCATE to represent their operations and we have pretty thorough runtime tests in gcc.target/aarch64/advsimd-intrinsics/vqmovn*.c. With this patch the instructions are folded away at optimisation levels and the correctness checks still pass. Bootstrapped and tested on aarch64-none-linux-gnu and aarch64_be-none-elf. gcc/ChangeLog: * simplify-rtx.cc (simplify_const_unary_operation): Handle US_TRUNCATE, SS_TRUNCATE. Diff: --- gcc/simplify-rtx.cc | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/gcc/simplify-rtx.cc b/gcc/simplify-rtx.cc index 276be67aa67..21b7eb484d0 100644 --- a/gcc/simplify-rtx.cc +++ b/gcc/simplify-rtx.cc @@ -2131,6 +2131,20 @@ simplify_const_unary_operation (enum rtx_code code, machine_mode mode, result = wide_int::from (op0, width, UNSIGNED); break; + case US_TRUNCATE: + case SS_TRUNCATE: + { + signop sgn = code == US_TRUNCATE ? UNSIGNED : SIGNED; + wide_int nmax + = wide_int::from (wi::max_value (width, sgn), + GET_MODE_PRECISION (imode), sgn); + wide_int nmin + = wide_int::from (wi::min_value (width, sgn), + GET_MODE_PRECISION (imode), sgn); + result = wi::min (wi::max (op0, nmin, sgn), nmax, sgn); + result = wide_int::from (result, width, sgn); + break; + } case SIGN_EXTEND: result = wide_int::from (op0, width, SIGNED); break;