[PATCH] D153344: [BOLT][RISCV] Implement branch reversal

Job Noorman via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 21 01:21:34 PDT 2023


This revision was automatically updated to reflect the committed changes.
Closed by commit rG41b8aed499b1: [BOLT][RISCV] Implement branch reversal (authored by jobnoorman).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D153344/new/

https://reviews.llvm.org/D153344

Files:
  bolt/lib/Target/RISCV/RISCVMCPlusBuilder.cpp


Index: bolt/lib/Target/RISCV/RISCVMCPlusBuilder.cpp
===================================================================
--- bolt/lib/Target/RISCV/RISCVMCPlusBuilder.cpp
+++ bolt/lib/Target/RISCV/RISCVMCPlusBuilder.cpp
@@ -73,6 +73,37 @@
     }
   }
 
+  unsigned getInvertedBranchOpcode(unsigned Opcode) const {
+    switch (Opcode) {
+    default:
+      llvm_unreachable("Failed to invert branch opcode");
+      return Opcode;
+    case RISCV::BEQ:
+      return RISCV::BNE;
+    case RISCV::BNE:
+      return RISCV::BEQ;
+    case RISCV::BLT:
+      return RISCV::BGE;
+    case RISCV::BGE:
+      return RISCV::BLT;
+    case RISCV::BLTU:
+      return RISCV::BGEU;
+    case RISCV::BGEU:
+      return RISCV::BLTU;
+    case RISCV::C_BEQZ:
+      return RISCV::C_BNEZ;
+    case RISCV::C_BNEZ:
+      return RISCV::C_BEQZ;
+    }
+  }
+
+  bool reverseBranchCondition(MCInst &Inst, const MCSymbol *TBB,
+                              MCContext *Ctx) const override {
+    auto Opcode = getInvertedBranchOpcode(Inst.getOpcode());
+    Inst.setOpcode(Opcode);
+    return replaceBranchTarget(Inst, TBB, Ctx);
+  }
+
   bool replaceBranchTarget(MCInst &Inst, const MCSymbol *TBB,
                            MCContext *Ctx) const override {
     assert((isCall(Inst) || isBranch(Inst)) && !isIndirectBranch(Inst) &&


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D153344.533172.patch
Type: text/x-patch
Size: 1320 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230621/1ec6c245/attachment.bin>


More information about the llvm-commits mailing list