[llvm] r318442 - bpf: print backward branch target properly
Yonghong Song via llvm-commits
llvm-commits at lists.llvm.org
Thu Nov 16 11:15:37 PST 2017
Author: yhs
Date: Thu Nov 16 11:15:36 2017
New Revision: 318442
URL: http://llvm.org/viewvc/llvm-project?rev=318442&view=rev
Log:
bpf: print backward branch target properly
Currently, it prints the backward branch offset as unsigned value
like below:
7: 7d 34 0b 00 00 00 00 00 if r4 s>= r3 goto 11 <LBB0_3>
8: b7 00 00 00 00 00 00 00 r0 = 0
LBB0_2:
9: 07 00 00 00 01 00 00 00 r0 += 1
......
17: bf 31 00 00 00 00 00 00 r1 = r3
18: 6d 32 f6 ff 00 00 00 00 if r2 s> r3 goto 65526 <LBB0_3+0x7FFB0>
The correct print insn 18 should be:
18: 6d 32 f6 ff 00 00 00 00 if r2 s> r3 goto -10 <LBB0_2>
To provide better clarity and be consistent with kernel verifier output,
the insn 7 output is changed to the following with "+" added to
non-negative branch offset:
7: 7d 34 0b 00 00 00 00 00 if r4 s>= r3 goto +11 <LBB0_3>
Signed-off-by: Yonghong Song <yhs at fb.com>
Acked-by: Alexei Starovoitov <ast at kernel.org>
Added:
llvm/trunk/test/CodeGen/BPF/objdump_cond_op_2.ll
Modified:
llvm/trunk/lib/Target/BPF/BPFInstrInfo.td
llvm/trunk/lib/Target/BPF/InstPrinter/BPFInstPrinter.cpp
llvm/trunk/lib/Target/BPF/InstPrinter/BPFInstPrinter.h
llvm/trunk/lib/Target/BPF/MCTargetDesc/BPFMCTargetDesc.cpp
llvm/trunk/test/CodeGen/BPF/objdump_cond_op.ll
llvm/trunk/test/MC/BPF/insn-unit.s
Modified: llvm/trunk/lib/Target/BPF/BPFInstrInfo.td
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/BPF/BPFInstrInfo.td?rev=318442&r1=318441&r2=318442&view=diff
==============================================================================
--- llvm/trunk/lib/Target/BPF/BPFInstrInfo.td (original)
+++ llvm/trunk/lib/Target/BPF/BPFInstrInfo.td Thu Nov 16 11:15:36 2017
@@ -46,7 +46,9 @@ def BPFWrapper : SDNode<"BPFISD::Wr
def BPFIsLittleEndian : Predicate<"CurDAG->getDataLayout().isLittleEndian()">;
def BPFIsBigEndian : Predicate<"!CurDAG->getDataLayout().isLittleEndian()">;
-def brtarget : Operand<OtherVT>;
+def brtarget : Operand<OtherVT> {
+ let PrintMethod = "printBrTargetOperand";
+}
def calltarget : Operand<i64>;
def u64imm : Operand<i64> {
Modified: llvm/trunk/lib/Target/BPF/InstPrinter/BPFInstPrinter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/BPF/InstPrinter/BPFInstPrinter.cpp?rev=318442&r1=318441&r2=318442&view=diff
==============================================================================
--- llvm/trunk/lib/Target/BPF/InstPrinter/BPFInstPrinter.cpp (original)
+++ llvm/trunk/lib/Target/BPF/InstPrinter/BPFInstPrinter.cpp Thu Nov 16 11:15:36 2017
@@ -94,3 +94,16 @@ void BPFInstPrinter::printImm64Operand(c
else
O << Op;
}
+
+void BPFInstPrinter::printBrTargetOperand(const MCInst *MI, unsigned OpNo,
+ raw_ostream &O) {
+ const MCOperand &Op = MI->getOperand(OpNo);
+ if (Op.isImm()) {
+ int16_t Imm = Op.getImm();
+ O << ((Imm >= 0) ? "+" : "") << Imm;
+ } else if (Op.isExpr()) {
+ printExpr(Op.getExpr(), O);
+ } else {
+ O << Op;
+ }
+}
Modified: llvm/trunk/lib/Target/BPF/InstPrinter/BPFInstPrinter.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/BPF/InstPrinter/BPFInstPrinter.h?rev=318442&r1=318441&r2=318442&view=diff
==============================================================================
--- llvm/trunk/lib/Target/BPF/InstPrinter/BPFInstPrinter.h (original)
+++ llvm/trunk/lib/Target/BPF/InstPrinter/BPFInstPrinter.h Thu Nov 16 11:15:36 2017
@@ -30,6 +30,7 @@ public:
void printMemOperand(const MCInst *MI, int OpNo, raw_ostream &O,
const char *Modifier = nullptr);
void printImm64Operand(const MCInst *MI, unsigned OpNo, raw_ostream &O);
+ void printBrTargetOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O);
// Autogenerated by tblgen.
void printInstruction(const MCInst *MI, raw_ostream &O);
Modified: llvm/trunk/lib/Target/BPF/MCTargetDesc/BPFMCTargetDesc.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/BPF/MCTargetDesc/BPFMCTargetDesc.cpp?rev=318442&r1=318441&r2=318442&view=diff
==============================================================================
--- llvm/trunk/lib/Target/BPF/MCTargetDesc/BPFMCTargetDesc.cpp (original)
+++ llvm/trunk/lib/Target/BPF/MCTargetDesc/BPFMCTargetDesc.cpp Thu Nov 16 11:15:36 2017
@@ -79,7 +79,7 @@ public:
bool evaluateBranch(const MCInst &Inst, uint64_t Addr, uint64_t Size,
uint64_t &Target) const override {
// The target is the 3rd operand of cond inst and the 1st of uncond inst.
- int64_t Imm;
+ int16_t Imm;
if (isConditionalBranch(Inst)) {
Imm = Inst.getOperand(2).getImm();
} else if (isUnconditionalBranch(Inst))
Modified: llvm/trunk/test/CodeGen/BPF/objdump_cond_op.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/BPF/objdump_cond_op.ll?rev=318442&r1=318441&r2=318442&view=diff
==============================================================================
--- llvm/trunk/test/CodeGen/BPF/objdump_cond_op.ll (original)
+++ llvm/trunk/test/CodeGen/BPF/objdump_cond_op.ll Thu Nov 16 11:15:36 2017
@@ -27,7 +27,7 @@ define i32 @test(i32, i32) local_unnamed
br label %13
; CHECK: r1 <<= 32
; CHECK: r1 >>= 32
-; CHECK: if r1 != 2 goto 6 <LBB0_2>
+; CHECK: if r1 != 2 goto +6 <LBB0_2>
; <label>:8: ; preds = %2
%9 = icmp eq i32 %0, %1
@@ -38,7 +38,7 @@ define i32 @test(i32, i32) local_unnamed
; CHECK: r0 = *(u32 *)(r1 + 0)
; CHECK: r0 *= r0
; CHECK: r0 <<= 1
-; CHECK: goto 7 <LBB0_4>
+; CHECK: goto +7 <LBB0_4>
; <label>:11: ; preds = %8
%12 = shl nsw i32 %10, 2
@@ -49,7 +49,7 @@ define i32 @test(i32, i32) local_unnamed
; CHECK: r0 = *(u32 *)(r3 + 0)
; CHECK: r2 <<= 32
; CHECK: r2 >>= 32
-; CHECK: if r1 == r2 goto 4 <LBB0_5>
+; CHECK: if r1 == r2 goto +4 <LBB0_5>
; CHECK: r0 <<= 2
; <label>:13: ; preds = %4, %11
Added: llvm/trunk/test/CodeGen/BPF/objdump_cond_op_2.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/BPF/objdump_cond_op_2.ll?rev=318442&view=auto
==============================================================================
--- llvm/trunk/test/CodeGen/BPF/objdump_cond_op_2.ll (added)
+++ llvm/trunk/test/CodeGen/BPF/objdump_cond_op_2.ll Thu Nov 16 11:15:36 2017
@@ -0,0 +1,39 @@
+; RUN: llc -march=bpfel -filetype=obj -o - %s | llvm-objdump -d - | FileCheck %s
+
+; Source Code:
+; int test(int a, int b) {
+; int s = 0;
+; while (a < b) { s++; a += s; b -= s; }
+; return s;
+; }
+
+define i32 @test(i32, i32) local_unnamed_addr #0 {
+; CHECK-LABEL: test:
+ %3 = icmp slt i32 %0, %1
+ br i1 %3, label %4, label %13
+
+; <label>:4: ; preds = %2
+ br label %5
+; CHECK: if r4 s>= r3 goto +11 <LBB0_3>
+; CHECK: r0 = 0
+; CHECK-LABEL: LBB0_2:
+
+; <label>:5: ; preds = %4, %5
+ %6 = phi i32 [ %9, %5 ], [ 0, %4 ]
+ %7 = phi i32 [ %11, %5 ], [ %1, %4 ]
+ %8 = phi i32 [ %10, %5 ], [ %0, %4 ]
+ %9 = add nuw nsw i32 %6, 1
+ %10 = add nsw i32 %9, %8
+ %11 = sub nsw i32 %7, %9
+ %12 = icmp slt i32 %10, %11
+ br i1 %12, label %5, label %13
+; CHECK: r1 = r3
+; CHECK: if r2 s> r3 goto -10 <LBB0_2>
+
+; <label>:13: ; preds = %5, %2
+ %14 = phi i32 [ 0, %2 ], [ %9, %5 ]
+ ret i32 %14
+; CHECK-LABEL: LBB0_3:
+; CHECK: exit
+}
+attributes #0 = { norecurse nounwind readnone }
Modified: llvm/trunk/test/MC/BPF/insn-unit.s
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/BPF/insn-unit.s?rev=318442&r1=318441&r2=318442&view=diff
==============================================================================
--- llvm/trunk/test/MC/BPF/insn-unit.s (original)
+++ llvm/trunk/test/MC/BPF/insn-unit.s Thu Nov 16 11:15:36 2017
@@ -57,55 +57,55 @@
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 1a 00 00 00 00 00 goto +26
// 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 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
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 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
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 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
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 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
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 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
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 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
// ======== BPF_ALU64 Class ========
r0 += r1 // BPF_ADD | BPF_X
More information about the llvm-commits
mailing list