bpf: add support for objdump -print-imm-hex
authorYonghong Song <yhs@fb.com>
Wed, 20 Dec 2017 19:39:58 +0000 (19:39 +0000)
committerYonghong Song <yhs@fb.com>
Wed, 20 Dec 2017 19:39:58 +0000 (19:39 +0000)
Add support for 'objdump -print-imm-hex' for imm64, operand imm
and branch target. If user programs encode immediate values
as hex numbers, such an option will make it easy to correlate
asm insns with source code. This option also makes it easy
to correlate imm values with insn encoding.

There is one changed behavior in this patch. In old way, we
print the 64bit imm as u64:
  O << (uint64_t)Op.getImm();
and the new way is:
  O << formatImm(Op.getImm());

The formatImm is defined in llvm/MC/MCInstPrinter.h as
  format_object<int64_t> formatImm(int64_t Value)

So the new way to print 64bit imm is i64 type.
If a 64bit value has the highest bit set, the old way
will print the value as a positive value and the
new way will print as a negative value. The new way
is consistent with x86_64.
For the code (see the test program):
 ...
 if (a == 0xABCDABCDabcdabcdULL)
 ...
x86_64 objdump, with and without -print-imm-hex, looks like:
 48 b8 cd ab cd ab cd ab cd ab   movabsq $-6067004223159161907, %rax
 48 b8 cd ab cd ab cd ab cd ab   movabsq $-0x5432543254325433, %rax

Signed-off-by: Yonghong Song <yhs@fb.com>
llvm-svn: 321215

llvm/lib/Target/BPF/InstPrinter/BPFInstPrinter.cpp
llvm/test/CodeGen/BPF/objdump_imm_hex.ll [new file with mode: 0644]

index 6f81e02..1f4ef09 100644 (file)
@@ -56,7 +56,7 @@ void BPFInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
   if (Op.isReg()) {
     O << getRegisterName(Op.getReg());
   } else if (Op.isImm()) {
-    O << (int32_t)Op.getImm();
+    O << formatImm((int32_t)Op.getImm());
   } else {
     assert(Op.isExpr() && "Expected an expression");
     printExpr(Op.getExpr(), O);
@@ -76,9 +76,9 @@ void BPFInstPrinter::printMemOperand(const MCInst *MI, int OpNo, raw_ostream &O,
   if (OffsetOp.isImm()) {
     auto Imm = OffsetOp.getImm();
     if (Imm >= 0)
-      O << " + " << formatDec(Imm);
+      O << " + " << formatImm(Imm);
     else
-      O << " - " << formatDec(-Imm);
+      O << " - " << formatImm(-Imm);
   } else {
     assert(0 && "Expected an immediate");
   }
@@ -88,7 +88,7 @@ void BPFInstPrinter::printImm64Operand(const MCInst *MI, unsigned OpNo,
                                        raw_ostream &O) {
   const MCOperand &Op = MI->getOperand(OpNo);
   if (Op.isImm())
-    O << (uint64_t)Op.getImm();
+    O << formatImm(Op.getImm());
   else if (Op.isExpr())
     printExpr(Op.getExpr(), O);
   else
@@ -100,7 +100,7 @@ void BPFInstPrinter::printBrTargetOperand(const MCInst *MI, unsigned OpNo,
   const MCOperand &Op = MI->getOperand(OpNo);
   if (Op.isImm()) {
     int16_t Imm = Op.getImm();
-    O << ((Imm >= 0) ? "+" : "") << Imm;
+    O << ((Imm >= 0) ? "+" : "") << formatImm(Imm);
   } else if (Op.isExpr()) {
     printExpr(Op.getExpr(), O);
   } else {
diff --git a/llvm/test/CodeGen/BPF/objdump_imm_hex.ll b/llvm/test/CodeGen/BPF/objdump_imm_hex.ll
new file mode 100644 (file)
index 0000000..a245a6c
--- /dev/null
@@ -0,0 +1,65 @@
+; RUN: llc -march=bpfel -filetype=obj -o - %s | llvm-objdump -d - | FileCheck --check-prefix=CHECK-DEC %s
+; RUN: llc -march=bpfel -filetype=obj -o - %s | llvm-objdump -d -print-imm-hex - | FileCheck --check-prefix=CHECK-HEX %s
+
+; Source Code:
+; int gbl;
+; int test(unsigned long long a, unsigned long long b) {
+;   int ret = 0;
+;   if (a == 0xABCDABCDabcdabcdULL) {
+;     gbl = gbl * gbl * 2;
+;     ret = 1;
+;     goto out;
+;   }
+;   if (b == 0xABCDabcdabcdULL) {
+;     gbl = gbl * 4;
+;     ret = 2;
+;   }
+;  out:
+;   return ret;
+; }
+
+@gbl = common local_unnamed_addr global i32 0, align 4
+
+; Function Attrs: norecurse nounwind
+define i32 @test(i64, i64) local_unnamed_addr #0 {
+; CHECK-LABEL: test
+  %3 = icmp eq i64 %0, -6067004223159161907
+  br i1 %3, label %4, label %8
+; CHECK-DEC: 18 03 00 00 cd ab cd ab 00 00 00 00 cd ab cd ab         r3 = -6067004223159161907 ll
+; CHECK-DEC: 5d 31 07 00 00 00 00 00         if r1 != r3 goto +7
+; CHECK-HEX: 18 03 00 00 cd ab cd ab 00 00 00 00 cd ab cd ab         r3 = -0x5432543254325433 ll
+; CHECK-HEX: 5d 31 07 00 00 00 00 00         if r1 != r3 goto +0x7
+
+; <label>:4:                                      ; preds = %2
+  %5 = load i32, i32* @gbl, align 4
+  %6 = shl i32 %5, 1
+; CHECK-DEC: 67 01 00 00 01 00 00 00         r1 <<= 1
+; CHECK-HEX: 67 01 00 00 01 00 00 00         r1 <<= 0x1
+  %7 = mul i32 %6, %5
+  br label %13
+
+; <label>:8:                                      ; preds = %2
+  %9 = icmp eq i64 %1, 188899839028173
+; CHECK-DEC: 18 01 00 00 cd ab cd ab 00 00 00 00 cd ab 00 00         r1 = 188899839028173 ll
+; CHECK-HEX: 18 01 00 00 cd ab cd ab 00 00 00 00 cd ab 00 00         r1 = 0xabcdabcdabcd ll
+  br i1 %9, label %10, label %16
+
+; <label>:10:                                     ; preds = %8
+  %11 = load i32, i32* @gbl, align 4
+  %12 = shl nsw i32 %11, 2
+  br label %13
+
+; <label>:13:                                     ; preds = %4, %10
+  %14 = phi i32 [ %12, %10 ], [ %7, %4 ]
+  %15 = phi i32 [ 2, %10 ], [ 1, %4 ]
+  store i32 %14, i32* @gbl, align 4
+; CHECK-DEC: 63 12 00 00 00 00 00 00         *(u32 *)(r2 + 0) = r1
+; CHECK-HEX: 63 12 00 00 00 00 00 00         *(u32 *)(r2 + 0x0) = r1
+  br label %16
+
+; <label>:16:                                     ; preds = %13, %8
+  %17 = phi i32 [ 0, %8 ], [ %15, %13 ]
+  ret i32 %17
+}
+
+attributes #0 = { norecurse nounwind }