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: unassignedclangb...@nondot.org
          Reporter: antosh...@gmail.com
                CC: dgre...@apple.com, llvm-bugs@lists.llvm.org,
                    richard-l...@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.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to