llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

clang produces wrong code with -O1 to -Os

Open haoxintu opened this issue 6 years ago • 3 comments

Bugzilla Link 45631
Resolution INVALID
Resolved on Apr 21, 2020 09:47
Version trunk
OS All
CC @DougGregor,@LebedevRI,@zygoloid

Extended Description

Hi, test.c

#include<stdio.h> long a; int b = 94, tf_0_var_795; void c(long long *d, int e) { *d = e; } int main() { tf_0_var_795 = !(~0 + 7 << b); c(&a, tf_0_var_795); printf("%d\n", a); }

Clang produces wrong code with -O1 to -Os, the correct output is 0, but it gets wrong with 1.

$ clang-trunk -w test.c && ./a.out 0 $ clang-trunk -w -O1 test.c && ./a.out 1 $ clang-trunk -w -O2 test.c && ./a.out 1 $ clang-trunk -w -O3 test.c && ./a.out 1 $ clang-trunk -w -Os test.c && ./a.out 1

Also, I have tested almost all released clang versions I used, the results are the same as I reported above.

haoxintu avatar Apr 21 '20 15:04 haoxintu

hi, Lebedev, thanks for your quick response.

I have noticed that exponent is too large, but might clang has a default solution to deal with it in case of the occasional mistake?

I'm not aware of anything for this case, and i'm not really sure how that would look.

LebedevRI avatar Apr 21 '20 16:04 LebedevRI

hi, Lebedev, thanks for your quick response.

I have noticed that exponent is too large, but might clang has a default solution to deal with it in case of the occasional mistake?

haoxintu avatar Apr 21 '20 16:04 haoxintu

https://godbolt.org/z/JyVFcW example.c:6:27: runtime error: shift exponent 94 is too large for 32-bit type 'int'

SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior example.c:6:27 in

LebedevRI avatar Apr 21 '20 15:04 LebedevRI