aboutsummaryrefslogtreecommitdiff
path: root/gcc/tree-cfg.cc
diff options
context:
space:
mode:
authorZhao Wei Liew <zhaoweiliew@gmail.com>2022-01-28 13:36:39 -0500
committerJeff Law <jeffreyalaw@gmail.com>2022-01-28 13:36:39 -0500
commitc2b610e7c6c89fd422c5c31f01023bcddf3cf4a5 (patch)
tree5f0d362b89de28da00f3b2adeeb8d10dfa1e98d3 /gcc/tree-cfg.cc
parenta591c71b41e18e4ff86852a974592af4962aef57 (diff)
downloadgcc-c2b610e7c6c89fd422c5c31f01023bcddf3cf4a5.zip
gcc-c2b610e7c6c89fd422c5c31f01023bcddf3cf4a5.tar.gz
gcc-c2b610e7c6c89fd422c5c31f01023bcddf3cf4a5.tar.bz2
match.pd: Simplify 1 / X for integer X [PR95424]
This patch implements an optimization for the following C++ code: int f(int x) { return 1 / x; } int f(unsigned int x) { return 1 / x; } Before this patch, x86-64 gcc -std=c++20 -O3 produces the following assembly: f(int): xor edx, edx mov eax, 1 idiv edi ret f(unsigned int): xor edx, edx mov eax, 1 div edi ret In comparison, clang++ -std=c++20 -O3 produces the following assembly: f(int): lea ecx, [rdi + 1] xor eax, eax cmp ecx, 3 cmovb eax, edi ret f(unsigned int): xor eax, eax cmp edi, 1 sete al ret Clang's output is more efficient as it avoids expensive div operations. With this patch, GCC now produces the following assembly: f(int): lea eax, [rdi + 1] cmp eax, 2 mov eax, 0 cmovbe eax, edi ret f(unsigned int): xor eax, eax cmp edi, 1 sete al ret which is virtually identical to Clang's assembly output. Any slight differences in the output for f(int) is possibly related to a different missed optimization. v2: https://gcc.gnu.org/pipermail/gcc-patches/2022-January/587751.html Changes from v2: 1. Refactor from using a switch statement to using the built-in if-else statement. v1: https://gcc.gnu.org/pipermail/gcc-patches/2022-January/587634.html Changes from v1: 1. Refactor common if conditions. 2. Use build_[minus_]one_cst (type) to get -1/1 of the correct type. 3. Match only for TRUNC_DIV_EXPR and TYPE_PRECISION (type) > 1. gcc/ChangeLog: PR tree-optimization/95424 * match.pd: Simplify 1 / X where X is an integer.
Diffstat (limited to 'gcc/tree-cfg.cc')
0 files changed, 0 insertions, 0 deletions