Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions llvm/lib/Target/BPF/BPFMIPeephole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "BPFInstrInfo.h"
#include "BPFTargetMachine.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/CodeGen/LivePhysRegs.h"
#include "llvm/CodeGen/MachineFrameInfo.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
Expand Down Expand Up @@ -322,6 +323,7 @@ struct BPFMIPreEmitPeephole : public MachineFunctionPass {
bool eliminateRedundantMov();
bool adjustBranch();
bool insertMissingCallerSavedSpills();
bool removeMayGotoZero();

public:

Expand All @@ -337,6 +339,7 @@ struct BPFMIPreEmitPeephole : public MachineFunctionPass {
if (SupportGotol)
Changed = adjustBranch() || Changed;
Changed |= insertMissingCallerSavedSpills();
Changed |= removeMayGotoZero();
return Changed;
}
};
Expand Down Expand Up @@ -682,6 +685,56 @@ bool BPFMIPreEmitPeephole::insertMissingCallerSavedSpills() {
return Changed;
}

bool BPFMIPreEmitPeephole::removeMayGotoZero() {
bool Changed = false;
MachineBasicBlock *Prev_MBB, *Curr_MBB = nullptr;

for (MachineBasicBlock &MBB : make_early_inc_range(reverse(*MF))) {
Prev_MBB = Curr_MBB;
Curr_MBB = &MBB;
if (Prev_MBB == nullptr)
continue;

MachineInstr &MI = MBB.back();
if (MI.getOpcode() != TargetOpcode::INLINEASM_BR)
continue;

const char *AsmStr = MI.getOperand(0).getSymbolName();
SmallVector<StringRef, 4> AsmPieces;
SplitString(AsmStr, AsmPieces, ";\n");

// Do not support multiple insns in one inline asm.
if (AsmPieces.size() != 1)
continue;

// The asm insn must be a may_goto insn.
SmallVector<StringRef, 4> AsmOpPieces;
SplitString(AsmPieces[0], AsmOpPieces, " ");
if (AsmOpPieces[0] != "may_goto")
continue;

// Get the may_goto branch target.
MachineOperand &MO = MI.getOperand(InlineAsm::MIOp_FirstOperand + 1);
if (MO.getMBB() != Prev_MBB)
continue;

Changed = true;
if (MBB.begin() == MI) {
// Single 'may_goto' insn in the same basic block.
Curr_MBB->removeSuccessor(Prev_MBB);
for (MachineBasicBlock *Pred : Curr_MBB->predecessors())
Pred->replaceSuccessor(Curr_MBB, Prev_MBB);
Curr_MBB->eraseFromParent();
Curr_MBB = Prev_MBB;
} else {
// Remove 'may_goto' insn.
MI.eraseFromParent();
}
}

return Changed;
}

} // end default namespace

INITIALIZE_PASS(BPFMIPreEmitPeephole, "bpf-mi-pemit-peephole",
Expand Down
27 changes: 27 additions & 0 deletions llvm/test/CodeGen/BPF/may_goto.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
; RUN: llc -mtriple=bpfel -mcpu=v3 -filetype=obj -o - %s | llvm-objdump --no-show-raw-insn -d - | FileCheck %s

@j = dso_local local_unnamed_addr global i32 0, align 4

define dso_local noundef i32 @foo() local_unnamed_addr {
entry:
callbr void asm sideeffect "may_goto ${0:l}", "!i"()
to label %for.body [label %for.cond.cleanup]

for.cond.cleanup: ; preds = %for.body.2, %for.body.2, %for.body.1, %for.body, %entry
ret i32 0

for.body: ; preds = %entry
callbr void asm sideeffect "may_goto ${0:l}", "!i"()
to label %for.body.1 [label %for.cond.cleanup]

for.body.1: ; preds = %for.body
callbr void asm sideeffect "may_goto ${0:l}", "!i"()
to label %for.body.2 [label %for.cond.cleanup]

for.body.2: ; preds = %for.body.1
callbr void asm sideeffect "may_goto ${0:l}", "!i"()
to label %for.cond.cleanup [label %for.cond.cleanup]
}

; CHECK: 0: w0 = 0x0
; CHECK-NEXT: 1: exit
Loading