-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[RISCV] Insert simple landing pad before indirect jumps for Zicfilp.
This is cherry-picked from upstream pr #91860. This patch is based on #91855. This patch inserts simple landing pad ([pr])before indirct jumps. And also make option riscv-landing-pad-label influence this feature. [pr]: riscv-non-isa/riscv-elf-psabi-doc#417
- Loading branch information
Yeting Kuo
committed
Jul 18, 2024
1 parent
1974e9d
commit ab0b249
Showing
8 changed files
with
161 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
.addImm(Label); | ||
MachineInstrBuilder(MF, &MI).addUse(RISCV::X7, RegState::ImplicitKill); | ||
Changed = true; | ||
} | ||
|
||
return Changed; | ||
} | ||
|
||
INITIALIZE_PASS(RISCVLandingPadSetup, DEBUG_TYPE, PASS_NAME, false, false) | ||
|
||
char RISCVLandingPadSetup::ID = 0; | ||
|
||
FunctionPass *llvm::createRISCVLandingPadSetupPass() { | ||
return new RISCVLandingPadSetup(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters