[llvm] [RISCV] Insert simple landing pad before indirect jumps for Zicfilp. (PR #91860)

Yeting Kuo via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 6 07:44:22 PDT 2024


================
@@ -0,0 +1,85 @@
+//===------------ RISCVLandingPadSetup.cpp ---------------------------------==//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This is a RISC-V pass to setup landing pad labels for indirect jumps.
+// Currently it is only supported fixed labels.
+//
+//===----------------------------------------------------------------------===//
+
+#include "RISCV.h"
+#include "RISCVInstrInfo.h"
+#include "RISCVSubtarget.h"
+#include "llvm/CodeGen/MachineFunctionPass.h"
+#include "llvm/CodeGen/MachineInstrBuilder.h"
+#include "llvm/InitializePasses.h"
+
+using namespace llvm;
+
+#define DEBUG_TYPE "riscv-lpad-setup"
+#define PASS_NAME "RISC-V Landing Pad Setup"
+
+extern cl::opt<uint32_t> PreferredLandingPadLabel;
+
+namespace {
+
+class RISCVLandingPadSetup : public MachineFunctionPass {
+public:
+  static char ID;
+
+  RISCVLandingPadSetup() : MachineFunctionPass(ID) {}
+
+  bool runOnMachineFunction(MachineFunction &F) override;
+
+  StringRef getPassName() const override { return PASS_NAME; }
+
+  void getAnalysisUsage(AnalysisUsage &AU) const override {
+    AU.setPreservesCFG();
+    MachineFunctionPass::getAnalysisUsage(AU);
+  }
+};
+
+} // end anonymous namespace
+
+bool RISCVLandingPadSetup::runOnMachineFunction(MachineFunction &MF) {
+  const auto &STI = MF.getSubtarget<RISCVSubtarget>();
+  const RISCVInstrInfo &TII = *STI.getInstrInfo();
+
+  if (!STI.hasStdExtZicfilp())
+    return false;
+
+  bool Changed = false;
+  for (MachineBasicBlock &MBB : MF)
+    for (MachineInstr &MI : llvm::make_early_inc_range(MBB)) {
+      if (MI.getOpcode() != RISCV::PseudoBRINDNonX7 &&
+          MI.getOpcode() != RISCV::PseudoCALLIndirectNonX7 &&
+          MI.getOpcode() != RISCV::PseudoTAILIndirectNonX7)
+        continue;
+      uint32_t Label = 0;
+      if (PreferredLandingPadLabel.getNumOccurrences() > 0) {
+        if (!isUInt<20>(PreferredLandingPadLabel))
+          report_fatal_error(
+              "riscv-landing-pad-label=<val>, <val> needs to fit in "
+              "unsigned 20-bits");
+        Label = PreferredLandingPadLabel;
+      }
+      BuildMI(MBB, MI, MI.getDebugLoc(), TII.get(RISCV::LUI), RISCV::X7)
----------------
yetingk wrote:

I make this pass skip this insertion if label = 0 and also add test case for lpad = 1.

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


More information about the llvm-commits mailing list