[llvm] 8fb3f84 - BPF: Workaround InstCombine trunc+icmp => mask+icmp Optimization
Yonghong Song via llvm-commits
llvm-commits at lists.llvm.org
Thu Nov 18 20:27:37 PST 2021
Author: Yonghong Song
Date: 2021-11-18T20:25:28-08:00
New Revision: 8fb3f84484094230d563c1e4c1088755b7cde15b
URL: https://github.com/llvm/llvm-project/commit/8fb3f84484094230d563c1e4c1088755b7cde15b
DIFF: https://github.com/llvm/llvm-project/commit/8fb3f84484094230d563c1e4c1088755b7cde15b.diff
LOG: BPF: Workaround InstCombine trunc+icmp => mask+icmp Optimization
Patch [1] added further InstCombine trunc+icmp => mask+icmp
optimization and this caused a couple of bpf selftest failure.
Previous llvm BPF backend patch [2] introduced llvm.bpf.compare
builtin to handle such situations.
This patch further added support ">" and ">=" icmp opcodes.
Tested with bpf selftests and all tests are passed including two
previously failed ones.
Note Patch [1] also added optimization if the to-be-compared
constant is negative-power-of-2 (-C) or not-of-power-of-2 (~C).
This patch didn't implement these two cases as typical bpf
program compares a scalar to a positive length or boundary value,
and this scalar later is used as a index into an array buffer
or packet buffer.
[1] https://reviews.llvm.org/D112634
[2] https://reviews.llvm.org/D112938
Differential Revision: https://reviews.llvm.org/D114215
Added:
llvm/test/CodeGen/BPF/adjust-opt-icmp4.ll
Modified:
llvm/lib/Target/BPF/BPFAdjustOpt.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Target/BPF/BPFAdjustOpt.cpp b/llvm/lib/Target/BPF/BPFAdjustOpt.cpp
index 64bedf4da1ddc..69d0bca0bd772 100644
--- a/llvm/lib/Target/BPF/BPFAdjustOpt.cpp
+++ b/llvm/lib/Target/BPF/BPFAdjustOpt.cpp
@@ -131,10 +131,10 @@ bool BPFAdjustOptImpl::adjustICmpToBuiltin() {
auto ConstOp1Val = ConstOp1->getValue().getZExtValue();
auto Op = Icmp->getPredicate();
- if (Op == ICmpInst::ICMP_ULT) {
+ if (Op == ICmpInst::ICMP_ULT || Op == ICmpInst::ICMP_UGE) {
if ((ConstOp1Val - 1) & ConstOp1Val)
continue;
- } else if (Op == ICmpInst::ICMP_ULE) {
+ } else if (Op == ICmpInst::ICMP_ULE || Op == ICmpInst::ICMP_UGT) {
if (ConstOp1Val & (ConstOp1Val + 1))
continue;
} else {
diff --git a/llvm/test/CodeGen/BPF/adjust-opt-icmp4.ll b/llvm/test/CodeGen/BPF/adjust-opt-icmp4.ll
new file mode 100644
index 0000000000000..efdab8c4c70fe
--- /dev/null
+++ b/llvm/test/CodeGen/BPF/adjust-opt-icmp4.ll
@@ -0,0 +1,85 @@
+; RUN: opt -O2 -S -mtriple=bpf-pc-linux %s -o %t1
+; RUN: llc %t1 -o - | FileCheck -check-prefixes=CHECK,CHECK-V1 %s
+; RUN: opt -O2 -S -mtriple=bpf-pc-linux %s -o %t1
+; RUN: llc %t1 -mcpu=v3 -o - | FileCheck -check-prefixes=CHECK,CHECK-V3 %s
+;
+; Source:
+; int test1(unsigned long a) {
+; if ((unsigned)a > 3) return 2;
+; return 3;
+; }
+; int test2(unsigned long a) {
+; if ((unsigned)a >= 4) return 2;
+; return 3;
+; }
+; Compilation flag:
+; clang -target bpf -O2 -S -emit-llvm -Xclang -disable-llvm-passes test.c
+
+; Function Attrs: nounwind
+define dso_local i32 @test1(i64 %a) #0 {
+entry:
+ %retval = alloca i32, align 4
+ %a.addr = alloca i64, align 8
+ store i64 %a, i64* %a.addr, align 8, !tbaa !3
+ %0 = load i64, i64* %a.addr, align 8, !tbaa !3
+ %conv = trunc i64 %0 to i32
+ %cmp = icmp ugt i32 %conv, 3
+ br i1 %cmp, label %if.then, label %if.end
+
+if.then: ; preds = %entry
+ store i32 2, i32* %retval, align 4
+ br label %return
+
+if.end: ; preds = %entry
+ store i32 3, i32* %retval, align 4
+ br label %return
+
+return: ; preds = %if.end, %if.then
+ %1 = load i32, i32* %retval, align 4
+ ret i32 %1
+}
+
+; CHECK-LABEL: test1
+; CHECK-V1: if r[[#]] > 3 goto
+; CHECK-V3: if w[[#]] > 3 goto
+
+; Function Attrs: nounwind
+define dso_local i32 @test2(i64 %a) #0 {
+entry:
+ %retval = alloca i32, align 4
+ %a.addr = alloca i64, align 8
+ store i64 %a, i64* %a.addr, align 8, !tbaa !3
+ %0 = load i64, i64* %a.addr, align 8, !tbaa !3
+ %conv = trunc i64 %0 to i32
+ %cmp = icmp uge i32 %conv, 4
+ br i1 %cmp, label %if.then, label %if.end
+
+if.then: ; preds = %entry
+ store i32 2, i32* %retval, align 4
+ br label %return
+
+if.end: ; preds = %entry
+ store i32 3, i32* %retval, align 4
+ br label %return
+
+return: ; preds = %if.end, %if.then
+ %1 = load i32, i32* %retval, align 4
+ ret i32 %1
+}
+
+; CHECK-LABEL: test2
+; CHECK-V1: if r[[#]] > 3 goto
+; CHECK-V3: if w[[#]] > 3 goto
+
+attributes #0 = { nounwind "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
+
+!llvm.module.flags = !{!0, !1}
+!llvm.ident = !{!2}
+
+!0 = !{i32 1, !"wchar_size", i32 4}
+!1 = !{i32 7, !"frame-pointer", i32 2}
+!2 = !{!"clang version 14.0.0 (https://github.com/llvm/llvm-project.git 930ccf0191b4a33332d924522e5676fff583f083)"}
+!3 = !{!4, !4, i64 0}
+!4 = !{!"long", !5, i64 0}
+!5 = !{!"omnipotent char", !6, i64 0}
+!6 = !{!"Simple C/C++ TBAA"}
More information about the llvm-commits
mailing list