From 0df1955fdb388964e250c6f3af89fa0e8b3104a6 Mon Sep 17 00:00:00 2001 From: Yonghong Song Date: Thu, 16 Nov 2017 00:52:30 +0000 Subject: [PATCH] bpf: enable llvm-objdump to print out symbolized jmp target Add hook in BPF backend so that llvm-objdump can print out the jmp target with label names, e.g., ... if r1 != 2 goto 6 ... goto 7 ... LBB0_2: ... LBB0_4: ... Signed-off-by: Yonghong Song Acked-by: Alexei Starovoitov git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@318358 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Target/BPF/MCTargetDesc/BPFMCTargetDesc.cpp | 34 ++++++++++++ test/CodeGen/BPF/objdump_cond_op.ll | 69 +++++++++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 test/CodeGen/BPF/objdump_cond_op.ll diff --git a/lib/Target/BPF/MCTargetDesc/BPFMCTargetDesc.cpp b/lib/Target/BPF/MCTargetDesc/BPFMCTargetDesc.cpp index c8fbc0c2207..5c3bba6e669 100644 --- a/lib/Target/BPF/MCTargetDesc/BPFMCTargetDesc.cpp +++ b/lib/Target/BPF/MCTargetDesc/BPFMCTargetDesc.cpp @@ -15,6 +15,7 @@ #include "BPF.h" #include "InstPrinter/BPFInstPrinter.h" #include "MCTargetDesc/BPFMCAsmInfo.h" +#include "llvm/MC/MCInstrAnalysis.h" #include "llvm/MC/MCInstrInfo.h" #include "llvm/MC/MCRegisterInfo.h" #include "llvm/MC/MCSubtargetInfo.h" @@ -68,6 +69,35 @@ static MCInstPrinter *createBPFMCInstPrinter(const Triple &T, return nullptr; } +namespace { + +class BPFMCInstrAnalysis : public MCInstrAnalysis { +public: + explicit BPFMCInstrAnalysis(const MCInstrInfo *Info) + : MCInstrAnalysis(Info) {} + + 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; + if (isConditionalBranch(Inst)) { + Imm = Inst.getOperand(2).getImm(); + } else if (isUnconditionalBranch(Inst)) + Imm = Inst.getOperand(0).getImm(); + else + return false; + + Target = Addr + Size + Imm * Size; + return true; + } +}; + +} // end anonymous namespace + +static MCInstrAnalysis *createBPFInstrAnalysis(const MCInstrInfo *Info) { + return new BPFMCInstrAnalysis(Info); +} + extern "C" void LLVMInitializeBPFTargetMC() { for (Target *T : {&getTheBPFleTarget(), &getTheBPFbeTarget(), &getTheBPFTarget()}) { @@ -89,6 +119,9 @@ extern "C" void LLVMInitializeBPFTargetMC() { // Register the MCInstPrinter. TargetRegistry::RegisterMCInstPrinter(*T, createBPFMCInstPrinter); + + // Register the MC instruction analyzer. + TargetRegistry::RegisterMCInstrAnalysis(*T, createBPFInstrAnalysis); } // Register the MC code emitter @@ -114,4 +147,5 @@ extern "C" void LLVMInitializeBPFTargetMC() { TargetRegistry::RegisterMCAsmBackend(getTheBPFTarget(), createBPFbeAsmBackend); } + } diff --git a/test/CodeGen/BPF/objdump_cond_op.ll b/test/CodeGen/BPF/objdump_cond_op.ll new file mode 100644 index 00000000000..3abbb76ccc9 --- /dev/null +++ b/test/CodeGen/BPF/objdump_cond_op.ll @@ -0,0 +1,69 @@ +; RUN: llc -march=bpfel -filetype=obj -o - %s | llvm-objdump -d - | FileCheck %s + +; Source Code: +; int gbl; +; int test(int a, int b) { +; if (a == 2) { +; gbl = gbl * gbl * 2; +; goto out; +; } +; if (a != b) { +; gbl = gbl * 4; +; } +; out: +; return gbl; +; } + +@gbl = common local_unnamed_addr global i32 0, align 4 + +define i32 @test(i32, i32) local_unnamed_addr #0 { + %3 = icmp eq i32 %0, 2 + br i1 %3, label %4, label %8 + +;