Skip to content

[InstCombine] (a+a+a+a)/(a+a) is not optimized #68857

@ZY546

Description

@ZY546

Test case: https://godbolt.org/z/7caGb3avf

int n1;
void func1(int a){

    if(a>0&&a<10){
        n1=(a+a+a+a)/(a+a);
    }
}

It appears that InstCombine is missing this pattern (a+a+a+a)/(a+a) => 2
clang -O3:

if.then:
  %a.tr = trunc i32 %a to i8
  %div.lhs.trunc = shl nuw nsw i8 %a.tr, 2
  %div.rhs.trunc = shl nuw nsw i8 %a.tr, 1
  %div12 = udiv i8 %div.lhs.trunc, %div.rhs.trunc
  %div.zext = zext i8 %div12 to i32
  store i32 %div.zext, ptr @n1, align 4
  br label %if.end
func1(int):                              # @func1(int)
        lea     eax, [rdi - 1]
        cmp     eax, 8
        ja      .LBB0_2
        lea     eax, [4*rdi]
        add     dil, dil
        movzx   eax, al
        div     dil
        movzx   eax, al
        mov     dword ptr [rip + n1], eax
.LBB0_2:                                # %if.end
        ret

proof: https://alive2.llvm.org/ce/z/5UXwMN

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions