[llvm] [BPF] Remove 'may_goto 0' instructions (PR #123482)

via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 24 22:47:11 PST 2025


================
@@ -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")
----------------
yonghong-song wrote:

Make sense. I did further check for asm format. That is, it must be 'may_goto $0' or 'may_goto ${0:l}'. Note that '$0'  is the *first* argument of the asm code. '${0:l}' is the first *label* argument of the asm code. Since we already enforced asm opcode INLINEASM_BR so '$0' must be refer to a label.

https://github.com/llvm/llvm-project/pull/123482


More information about the llvm-commits mailing list