[llvm-dev] Optimised stack direction?

Samuel Williams via llvm-dev llvm-dev at lists.llvm.org
Tue Jun 25 04:38:59 PDT 2019


Why does LLVM struggle to optimize the following stack direction check?

#include <stdint.h>

inline int stack_direction() {
    int x = 0;
    int y = 0;

    return uintptr_t(&x) < uintptr_t(&y);
}

int main(int argc, const char * argv[]) {
return stack_direction();
}

It generates the following assembly:

main: # @main
  lea rcx, [rsp - 8]
  lea rdx, [rsp - 4]
  xor eax, eax
  cmp rdx, rcx
  setb al
  ret

It seems to me it should be possible, because clearly LLVM knows the layout
of x and y at compile time.

Shared code: https://godbolt.org/z/ZQKESy <https://t.co/8wdz6ftAm7?amp=1>

Thanks
Samuel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20190625/16e9dc24/attachment.html>


More information about the llvm-dev mailing list