Why does clang / llvm not optimize this?

When compiling this code with clang 3.9:

constexpr bool is_small(long long v) {
  return v < 0x4000000000000000;
}
int foo();
int f(int a) {
  if (is_small(a)) return a;
  else return foo();
}

it produces an assembly equivalent int f(int a) { return a; }, since it determined that is_small(a)it will always be true, as it ais int, which (on my platform) is always smaller 0x4000000000000000.

When I change is_smallto:

constexpr bool is_small(long long v) {
  return v >= -0x4000000000000000;
}

the same thing happens as expected.

However, when I change is_smallto check both conditions:

constexpr bool is_small(long long v) {
  return v < 0x4000000000000000 && v >= -0x4000000000000000;
}

clang does not optimize part ifand return foo().

(Here are the above snippets on Godbolt to play with: https://godbolt.org/g/fnoE9A )

Why is this happening? He clearly concluded that conditions are individually always true, why does this not apply to the logical connection of both?

+4
1

, LLVM. https://llvm.org/bugs/show_bug.cgi?id=30794, , .

LLVM is_small f(). , is_small v < 0x4000000000000000 && v >= -0x4000000000000000 v + 0x4000000000000000 > -1. , f().

+1

Source: https://habr.com/ru/post/1658610/


All Articles