Issue 131056
Summary [Clang] Missed optimization within bitshift on bools
Labels clang
Assignees
Reporter BreadTom
    C code:
func1 only differs by using another bool from comparison of (ptr[n] == '_').
Otherwise, they should behave exactly the same.
```
#include <stdbool.h>
#include <stddef.h>

unsigned long func0(unsigned char * restrict ptr){
        unsigned long ret_val = 0;
        bool tmp_bool = false;
        for(size_t n = 0; n < 64; n++){
                tmp_bool = (ptr[n] == '_') || (ptr[n] >= 'a' && ptr[n] <= 'z') || (ptr[n] >= 'A' && ptr[n] <= 'Z') || (ptr[n] >= '0' && ptr[n] <= '9');
                ret_val |= ((unsigned long)tmp_bool << n);
                tmp_bool = false;
 }
        return ret_val;
}

unsigned long func1(unsigned char * restrict ptr){
        unsigned long ret_val = 0;
        bool tmp_bool = false;
        bool tmp_bool1 = false;
        for(size_t n = 0; n < 64; n++){
                tmp_bool1 = ptr[n] == '_';
                tmp_bool = tmp_bool || (ptr[n] >= 'a' && ptr[n] <= 'z') || (ptr[n] >= 'A' && ptr[n] <= 'Z') || (ptr[n] >= '0' && ptr[n] <= '9');
                ret_val |= ((unsigned long)(tmp_bool || tmp_bool1) << n);
                tmp_bool = false;
		tmp_bool1 = false;
        }
        return ret_val;
}
```
[Godbolt](https://godbolt.org/z/xx6q4abjs) show that func1 reduces instructions by 101 compared to func0.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to