[llvm-bugs] [Bug 39509] New: `i = i % constant` for static local `i` is not optimized
via llvm-bugs
llvm-bugs at lists.llvm.org
Wed Oct 31 06:46:44 PDT 2018
https://bugs.llvm.org/show_bug.cgi?id=39509
Bug ID: 39509
Summary: `i = i % constant` for static local `i` is not
optimized
Product: clang
Version: trunk
Hardware: PC
OS: Linux
Status: NEW
Severity: enhancement
Priority: P
Component: C++
Assignee: unassignedclangbugs at nondot.org
Reporter: antoshkka at gmail.com
CC: dgregor at apple.com, llvm-bugs at lists.llvm.org,
richard-llvm at metafoo.co.uk
Consider the following example:
unsigned next_trivial() {
static int i = 0;
auto ret = i;
++i;
i = i % 10;
return ret;
}
For that example a very suboptimal assembly with multiplication, many registers
usage and multiple instructions is generated.
However, the above example could be rewritten in the following way:
unsigned next_trivial_optim() {
static int i = 0;
auto ret = i;
++i;
if (i == 10) { i = 0; }
return ret;
}
For the above code snippet a very short and clear assembly is produced, without
any multiplications and unnecessary instructions:
mov eax, dword ptr [rip + next_trivial_optim()::i]
lea ecx, [rax + 1]
xor edx, edx
cmp ecx, 10
cmovne edx, ecx
mov dword ptr [rip + next_trivial_optim()::i], edx
ret
Please, add an optimization to do that transformation.
--
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20181031/20f88870/attachment-0001.html>
More information about the llvm-bugs
mailing list