[llvm] [ARM] Swap sides of cmp/cmn based on folding ability (PR #191915)
John Brawn via llvm-commits
llvm-commits at lists.llvm.org
Wed Apr 15 05:18:56 PDT 2026
john-brawn-arm wrote:
Looking at these simple test cases:
```
void cmp_shiftx(int x, int y) {
if ((x<<1) == y)
otherfn();
}
void cmp_shifty(int x, int y) {
if (x == (y<<1))
otherfn();
}
void cmn_shiftx(int x, int y) {
if ((x<<1) == -y)
otherfn();
}
void cmn_shifty(int x, int y) {
if (x == -(y<<1))
otherfn();
}
```
and compiling with ``--target=arm-none-eabi -mcpu=cortex-m33 -O2`` it looks like currently we fail to use cmn_shiftx, and it looks like just adding an extra pattern fixes that:
```
diff --git a/llvm/lib/Target/ARM/ARMInstrThumb2.td b/llvm/lib/Target/ARM/ARMInstrThumb2.td
index 4f0cea8b00aa..ad4d2b8c2187 100644
--- a/llvm/lib/Target/ARM/ARMInstrThumb2.td
+++ b/llvm/lib/Target/ARM/ARMInstrThumb2.td
@@ -3556,6 +3556,8 @@ def : T2Pat<(ARMcmpZ GPRnopc:$Rn, (ineg rGPR:$Rm)),
(t2CMNrr GPRnopc:$Rn, rGPR:$Rm)>;
def : T2Pat<(ARMcmpZ GPRnopc:$Rn, (ineg t2_so_reg:$ShiftedRm)),
(t2CMNrs GPRnopc:$Rn, t2_so_reg:$ShiftedRm)>;
+def : T2Pat<(ARMcmpZ (ineg GPRnopc:$Rn), t2_so_reg:$ShiftedRm),
+ (t2CMNrs GPRnopc:$Rn, t2_so_reg:$ShiftedRm)>;
defm t2TST : T2I_cmp_irs<0b0000, "tst", rGPR,
IIC_iTSTi, IIC_iTSTr, IIC_iTSTsi,
```
Do you have any examples which your approach would optimize where the above would not? If so please add tests for them.
https://github.com/llvm/llvm-project/pull/191915
More information about the llvm-commits
mailing list