Motivated case:
void foo(int a, int b, int c, int d, int* res1, int* res2, int* res3) { *res1 = a + b + c + d; *res2 = b + c; *res3 = a + d; }
There are two common expressions a+d & c+d, currently Reassociate can recognize one of them.
It gets:
%add = add i32 %b, %c %add.1 = add i32 %add, %a %add.2 = add i32 %add.1, %d store i32 %add, i32*res2, align 4, !tbaa !2 store i32 %add.2, i32* %res1, align 4, !tbaa !2 %add.3 = add nsw i32 %a, %d store i32 %add.3, i32* %res3, align 4, !tbaa !2 ret void
But we expect:
%add1 = add i32 %d, %a %add = add i32 %c, %b %add2 = add i32 %add, %add1 store i32 %add2, i32* %res1, align 4, !tbaa !2 store i32 %add, i32* %res2, align 4, !tbaa !2 store i32 %add1, i32* %res3, align 4, !tbaa !2 ret void
This is first part to recognize more than one pattern. This is NFC.
Changes about rewriting these common expressions will be posted in following patches.
clang-format: please reformat the code