Skip to content

Missed pattern 0 modulo/div/mul N in phi #37827

@davidbolvansky

Description

@davidbolvansky
Bugzilla Link 38479
Version trunk
OS Linux
CC @efriedma-quic,@LebedevRI,@RKSimon,@rotateright

Extended Description

unsigned int hash_function(const char *str, unsigned hash_tab_size) {
unsigned int h = 0;
const unsigned char *p;
for (p = (const unsigned char *) str; *p != '\0'; p++)
h = 65599 * h + *p;
return h % hash_tab_size;
// return h / hash_tab_size;
// return h * hash_tab_size;
// similar code generation
}

Lets say p[0] is \0 -> .LBB0_1

Latest trunk, O3:
hash_function(char const*, unsigned int): # @​hash_function(char const*, unsigned int)
mov cl, byte ptr [rdi]
test cl, cl
je .LBB0_1
add rdi, 1
xor eax, eax
.LBB0_3: # =>This Inner Loop Header: Depth=1
movzx ecx, cl
imul eax, eax, 65599
add eax, ecx
movzx ecx, byte ptr [rdi]
add rdi, 1
test cl, cl
jne .LBB0_3
xor edx, edx
div esi
mov eax, edx
ret
.LBB0_1:
xor eax, eax
xor edx, edx
div esi // useless
mov

I believe these are important lines in the IR:
%16 = phi i32 [ 0, %2 ], [ %11, %5 ]
%17 = urem i32 %16, %1
ret i32 %17

In this case we should expand/simplify phi and remove "0 urem %1"

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions