[llvm] [BPF] Use mul for certain div/mod operations (PR #110712)

via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 1 12:06:15 PDT 2024


================
@@ -118,7 +118,9 @@ class BPFTargetLowering : public TargetLowering {
     return Op.size() >= 8 ? MVT::i64 : MVT::i32;
   }
 
-  bool isIntDivCheap(EVT VT, AttributeList Attr) const override { return true; }
+  bool isIntDivCheap(EVT VT, AttributeList Attr) const override {
+    return false;
----------------
eddyz87 wrote:

I think it's better to keep this "cheap" when cpuv4 is enabled.
E.g. as far as I understand for arm64 BPF backend division/modulo operations are translated as single instructions.
Thus, with the following patch on top [allow-diff-for-cpuv4.txt](https://github.com/user-attachments/files/17215083/allow-diff-for-cpuv4.txt):

```c
$ cat t1.c
struct S {
  int var[3];
};
int foo1 (struct S *a, struct S *b)
{
    return a - b;
}

$ clang --target=bpf -mcpu=v3 -O2 -S t1.c -o -
        ...
foo1:                                   # @foo1
# %bb.0:                                # %entry
	r0 = r1
	r0 -= r2
	r0 >>= 2
	w0 *= -1431655765
	exit
        ...

$ clang --target=bpf -mcpu=v4 -O2 -S t1.c -o -
        ...
foo1:                                   # @foo1
# %bb.0:                                # %entry
	r0 = r1
	r0 -= r2
	r0 s/= 12
	exit
        ...
```


https://github.com/llvm/llvm-project/pull/110712


More information about the llvm-commits mailing list