[llvm] 50f5cef - [BPF] Implement mod operation
Jim Lin via llvm-commits
llvm-commits at lists.llvm.org
Wed May 11 19:56:31 PDT 2022
Author: Jim Lin
Date: 2022-05-12T10:59:18+08:00
New Revision: 50f5cef39140e038486d6a8640cc7306fb924b06
URL: https://github.com/llvm/llvm-project/commit/50f5cef39140e038486d6a8640cc7306fb924b06
DIFF: https://github.com/llvm/llvm-project/commit/50f5cef39140e038486d6a8640cc7306fb924b06.diff
LOG: [BPF] Implement mod operation
Implement BPF_MOD instruction to fix lack of assembly parser support mentioned in https://github.com/llvm/llvm-project/issues/55192.
Reviewed By: ast
Differential Revision: https://reviews.llvm.org/D125207
Added:
Modified:
llvm/lib/Target/BPF/BPFISelLowering.cpp
llvm/lib/Target/BPF/BPFInstrFormats.td
llvm/lib/Target/BPF/BPFInstrInfo.td
llvm/test/CodeGen/BPF/32-bit-subreg-alu.ll
llvm/test/MC/BPF/insn-unit.s
Removed:
################################################################################
diff --git a/llvm/lib/Target/BPF/BPFISelLowering.cpp b/llvm/lib/Target/BPF/BPFISelLowering.cpp
index bf3e6dc3cc78b..16876e74c4a18 100644
--- a/llvm/lib/Target/BPF/BPFISelLowering.cpp
+++ b/llvm/lib/Target/BPF/BPFISelLowering.cpp
@@ -103,7 +103,6 @@ BPFTargetLowering::BPFTargetLowering(const TargetMachine &TM,
setOperationAction(ISD::SDIVREM, VT, Expand);
setOperationAction(ISD::UDIVREM, VT, Expand);
setOperationAction(ISD::SREM, VT, Expand);
- setOperationAction(ISD::UREM, VT, Expand);
setOperationAction(ISD::MULHU, VT, Expand);
setOperationAction(ISD::MULHS, VT, Expand);
setOperationAction(ISD::UMUL_LOHI, VT, Expand);
diff --git a/llvm/lib/Target/BPF/BPFInstrFormats.td b/llvm/lib/Target/BPF/BPFInstrFormats.td
index a809065014e59..27db0be080ae5 100644
--- a/llvm/lib/Target/BPF/BPFInstrFormats.td
+++ b/llvm/lib/Target/BPF/BPFInstrFormats.td
@@ -39,6 +39,7 @@ def BPF_AND : BPFArithOp<0x5>;
def BPF_LSH : BPFArithOp<0x6>;
def BPF_RSH : BPFArithOp<0x7>;
def BPF_NEG : BPFArithOp<0x8>;
+def BPF_MOD : BPFArithOp<0x9>;
def BPF_XOR : BPFArithOp<0xa>;
def BPF_MOV : BPFArithOp<0xb>;
def BPF_ARSH : BPFArithOp<0xc>;
diff --git a/llvm/lib/Target/BPF/BPFInstrInfo.td b/llvm/lib/Target/BPF/BPFInstrInfo.td
index ce5bb00434922..6cac478561b2c 100644
--- a/llvm/lib/Target/BPF/BPFInstrInfo.td
+++ b/llvm/lib/Target/BPF/BPFInstrInfo.td
@@ -298,6 +298,7 @@ let isAsCheapAsAMove = 1 in {
}
defm MUL : ALU<BPF_MUL, "*=", mul>;
defm DIV : ALU<BPF_DIV, "/=", udiv>;
+ defm MOD : ALU<BPF_MOD, "%=", urem>;
}
class NEG_RR<BPFOpClass Class, BPFArithOp Opc,
diff --git a/llvm/test/CodeGen/BPF/32-bit-subreg-alu.ll b/llvm/test/CodeGen/BPF/32-bit-subreg-alu.ll
index 2e4e92e0126eb..42888ff58f5e2 100644
--- a/llvm/test/CodeGen/BPF/32-bit-subreg-alu.ll
+++ b/llvm/test/CodeGen/BPF/32-bit-subreg-alu.ll
@@ -51,6 +51,16 @@
; return a / 0xf;
; }
;
+; unsigned rem(unsigned a, unsigned b)
+; {
+; return a % b;
+; }
+;
+; unsigned rem_i(unsigned a)
+; {
+; return a % 0xf;
+; }
+;
; int or(int a, int b)
; {
; return a | b;
@@ -194,6 +204,22 @@ entry:
ret i32 %div
}
+; Function Attrs: norecurse nounwind readnone
+define dso_local i32 @rem(i32 %a, i32 %b) local_unnamed_addr #0 {
+entry:
+ %rem = urem i32 %a, %b
+; CHECK: w{{[0-9]+}} %= w{{[0-9]+}}
+ ret i32 %rem
+}
+
+; Function Attrs: norecurse nounwind readnone
+define dso_local i32 @rem_i(i32 %a) local_unnamed_addr #0 {
+entry:
+ %rem = urem i32 %a, 15
+; CHECK: w{{[0-9]+}} %= 15
+ ret i32 %rem
+}
+
; Function Attrs: norecurse nounwind readnone
define dso_local i32 @or(i32 %a, i32 %b) local_unnamed_addr #0 {
entry:
diff --git a/llvm/test/MC/BPF/insn-unit.s b/llvm/test/MC/BPF/insn-unit.s
index bb05fb982210b..aed82287896ce 100644
--- a/llvm/test/MC/BPF/insn-unit.s
+++ b/llvm/test/MC/BPF/insn-unit.s
@@ -65,65 +65,67 @@
goto Llabel0 // BPF_JA
call 1 // BPF_CALL
exit // BPF_EXIT
-// CHECK: 05 00 1a 00 00 00 00 00 goto +26
+// CHECK: 05 00 1b 00 00 00 00 00 goto +27
// CHECK: 85 00 00 00 01 00 00 00 call 1
// CHECK: 95 00 00 00 00 00 00 00 exit
if r0 == r1 goto Llabel0 // BPF_JEQ | BPF_X
if r3 != r4 goto Llabel0 // BPF_JNE | BPF_X
-// CHECK: 1d 10 17 00 00 00 00 00 if r0 == r1 goto +23
-// CHECK: 5d 43 16 00 00 00 00 00 if r3 != r4 goto +22
+// CHECK: 1d 10 18 00 00 00 00 00 if r0 == r1 goto +24
+// CHECK: 5d 43 17 00 00 00 00 00 if r3 != r4 goto +23
if r1 > r2 goto Llabel0 // BPF_JGT | BPF_X
if r2 >= r3 goto Llabel0 // BPF_JGE | BPF_X
if r4 s> r5 goto Llabel0 // BPF_JSGT | BPF_X
if r5 s>= r6 goto Llabel0 // BPF_JSGE | BPF_X
-// CHECK: 2d 21 15 00 00 00 00 00 if r1 > r2 goto +21
-// CHECK: 3d 32 14 00 00 00 00 00 if r2 >= r3 goto +20
-// CHECK: 6d 54 13 00 00 00 00 00 if r4 s> r5 goto +19
-// CHECK: 7d 65 12 00 00 00 00 00 if r5 s>= r6 goto +18
+// CHECK: 2d 21 16 00 00 00 00 00 if r1 > r2 goto +22
+// CHECK: 3d 32 15 00 00 00 00 00 if r2 >= r3 goto +21
+// CHECK: 6d 54 14 00 00 00 00 00 if r4 s> r5 goto +20
+// CHECK: 7d 65 13 00 00 00 00 00 if r5 s>= r6 goto +19
if r6 < r7 goto Llabel0 // BPF_JLT | BPF_X
if r7 <= r8 goto Llabel0 // BPF_JLE | BPF_X
if r8 s< r9 goto Llabel0 // BPF_JSLT | BPF_X
if r9 s<= r10 goto Llabel0 // BPF_JSLE | BPF_X
-// CHECK: ad 76 11 00 00 00 00 00 if r6 < r7 goto +17
-// CHECK: bd 87 10 00 00 00 00 00 if r7 <= r8 goto +16
-// CHECK: cd 98 0f 00 00 00 00 00 if r8 s< r9 goto +15
-// CHECK: dd a9 0e 00 00 00 00 00 if r9 s<= r10 goto +14
+// CHECK: ad 76 12 00 00 00 00 00 if r6 < r7 goto +18
+// CHECK: bd 87 11 00 00 00 00 00 if r7 <= r8 goto +17
+// CHECK: cd 98 10 00 00 00 00 00 if r8 s< r9 goto +16
+// CHECK: dd a9 0f 00 00 00 00 00 if r9 s<= r10 goto +15
if r0 == 0 goto Llabel0 // BPF_JEQ | BPF_K
if r3 != -1 goto Llabel0 // BPF_JNE | BPF_K
-// CHECK: 15 00 0d 00 00 00 00 00 if r0 == 0 goto +13
-// CHECK: 55 03 0c 00 ff ff ff ff if r3 != -1 goto +12
+// CHECK: 15 00 0e 00 00 00 00 00 if r0 == 0 goto +14
+// CHECK: 55 03 0d 00 ff ff ff ff if r3 != -1 goto +13
if r1 > 64 goto Llabel0 // BPF_JGT | BPF_K
if r2 >= 0xffffffff goto Llabel0 // BPF_JGE | BPF_K
if r4 s> 0xffffffff goto Llabel0 // BPF_JSGT | BPF_K
if r5 s>= 0x7fffffff goto Llabel0 // BPF_JSGE | BPF_K
-// CHECK: 25 01 0b 00 40 00 00 00 if r1 > 64 goto +11
-// CHECK: 35 02 0a 00 ff ff ff ff if r2 >= -1 goto +10
-// CHECK: 65 04 09 00 ff ff ff ff if r4 s> -1 goto +9
-// CHECK: 75 05 08 00 ff ff ff 7f if r5 s>= 2147483647 goto +8
+// CHECK: 25 01 0c 00 40 00 00 00 if r1 > 64 goto +12
+// CHECK: 35 02 0b 00 ff ff ff ff if r2 >= -1 goto +11
+// CHECK: 65 04 0a 00 ff ff ff ff if r4 s> -1 goto +10
+// CHECK: 75 05 09 00 ff ff ff 7f if r5 s>= 2147483647 goto +9
if r6 < 0xff goto Llabel0 // BPF_JLT | BPF_K
if r7 <= 0xffff goto Llabel0 // BPF_JLE | BPF_K
if r8 s< 0 goto Llabel0 // BPF_JSLT | BPF_K
if r9 s<= -1 goto Llabel0 // BPF_JSLE | BPF_K
-// CHECK: a5 06 07 00 ff 00 00 00 if r6 < 255 goto +7
-// CHECK: b5 07 06 00 ff ff 00 00 if r7 <= 65535 goto +6
-// CHECK: c5 08 05 00 00 00 00 00 if r8 s< 0 goto +5
-// CHECK: d5 09 04 00 ff ff ff ff if r9 s<= -1 goto +4
+// CHECK: a5 06 08 00 ff 00 00 00 if r6 < 255 goto +8
+// CHECK: b5 07 07 00 ff ff 00 00 if r7 <= 65535 goto +7
+// CHECK: c5 08 06 00 00 00 00 00 if r8 s< 0 goto +6
+// CHECK: d5 09 05 00 ff ff ff ff if r9 s<= -1 goto +5
// ======== BPF_ALU64 Class ========
r0 += r1 // BPF_ADD | BPF_X
r1 -= r2 // BPF_SUB | BPF_X
r2 *= r3 // BPF_MUL | BPF_X
r3 /= r4 // BPF_DIV | BPF_X
+ r4 %= r5 // BPF_MOD | BPF_X
// CHECK: 0f 10 00 00 00 00 00 00 r0 += r1
// CHECK: 1f 21 00 00 00 00 00 00 r1 -= r2
// CHECK: 2f 32 00 00 00 00 00 00 r2 *= r3
// CHECK: 3f 43 00 00 00 00 00 00 r3 /= r4
+// CHECK: 9f 54 00 00 00 00 00 00 r4 %= r5
Llabel0 :
r2 = -r2 // BPF_NEG
@@ -155,10 +157,12 @@ Llabel0 :
r1 -= 0x1 // BPF_SUB | BPF_K
r2 *= -4 // BPF_MUL | BPF_K
r3 /= 5 // BPF_DIV | BPF_K
+ r4 %= 5 // BPF_MOD | BPF_K
// CHECK: 07 00 00 00 01 00 00 00 r0 += 1
// CHECK: 17 01 00 00 01 00 00 00 r1 -= 1
// CHECK: 27 02 00 00 fc ff ff ff r2 *= -4
// CHECK: 37 03 00 00 05 00 00 00 r3 /= 5
+// CHECK: 97 04 00 00 05 00 00 00 r4 %= 5
r4 |= 0xff // BPF_OR | BPF_K
r5 &= 0xFF // BPF_AND | BPF_K
More information about the llvm-commits
mailing list