[llvm] [ARM] Fold Or of CSINC into CSINC (PR #190765)
John Brawn via llvm-commits
llvm-commits at lists.llvm.org
Fri Apr 10 04:04:27 PDT 2026
john-brawn-arm wrote:
This transformation doesn't look correct to me. If I'm understanding this correctly, this is equivalent to transforming ``x | (n ? 0 : 1)`` into ``n ? x : 1`` which gives the wrong result when n is zero. Running this test program confirms this:
```
#include <stdio.h>
int fn1_c(int n, int x) {
return x | (n ? 0 : 1);
}
int fn2_c(int n, int x) {
return n ? x : 1;
}
int fn1_asm(int n, int x) {
int ret;
asm("cmp %1, #0\n"
"csinc %0, zr, zr, ne\n"
"orr %0, %0, %2"
: "=r"(ret) : "r"(n), "r"(x));
return ret;
}
int fn2_asm(int n, int x) {
int ret;
asm("cmp %1, #0\n"
"csinc %0, %2, zr, ne"
: "=r"(ret) : "r"(n), "r"(x));
return ret;
}
int main() {
printf("C:\n");
for (int i = 0; i < 8; i++) {
printf("%d %d: %d %d\n", 0, i, fn1_c(0, i), fn2_c(0, i));
printf("%d %d: %d %d\n", 1, i, fn1_c(1, i), fn2_c(1, i));
}
printf("Asm:\n");
for (int i = 0; i < 8; i++) {
printf("%d %d: %d %d\n", 0, i, fn1_asm(0, i), fn2_asm(0, i));
printf("%d %d: %d %d\n", 1, i, fn1_asm(1, i), fn2_asm(1, i));
}
return 0;
}
```
on cortex-m85 is
```
C:
0 0: 1 1
1 0: 0 0
0 1: 1 1
1 1: 1 1
0 2: 3 1
1 2: 2 2
0 3: 3 1
1 3: 3 3
0 4: 5 1
1 4: 4 4
0 5: 5 1
1 5: 5 5
0 6: 7 1
1 6: 6 6
0 7: 7 1
1 7: 7 7
Asm:
0 0: 1 1
1 0: 0 0
0 1: 1 1
1 1: 1 1
0 2: 3 1
1 2: 2 2
0 3: 3 1
1 3: 3 3
0 4: 5 1
1 4: 4 4
0 5: 5 1
1 5: 5 5
0 6: 7 1
1 6: 6 6
0 7: 7 1
1 7: 7 7
```
https://github.com/llvm/llvm-project/pull/190765
More information about the llvm-commits
mailing list