Skip to content

Commit

Permalink
Only convert for frame-full callers
Browse files Browse the repository at this point in the history
  • Loading branch information
akiramenai committed Jul 30, 2024
1 parent 5b1087c commit b9ebaea
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions llvm/lib/Target/EraVM/EraVMReplaceNearCallPrepare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,20 @@

#include "EraVM.h"
#include "EraVMMachineFunctionInfo.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineFrameInfo.h"
#include "llvm/CodeGen/MachineModuleInfo.h"
#include "llvm/IR/InstIterator.h"
#include "llvm/IR/Instructions.h"
#include "llvm/Pass.h"

#define DEBUG_TYPE "eravm-replace-near-call-prepare"

using namespace llvm;

#define ERAVM_REPLACE_NEAR_CALL_PREPARE_NAME \
"EraVM replace near_call prepare pass"

namespace {

class EraVMReplaceNearCallPrepare final : public ModulePass {
Expand All @@ -41,20 +47,22 @@ class EraVMReplaceNearCallPrepare final : public ModulePass {
EraVMReplaceNearCallPrepare() : ModulePass(ID) {}

StringRef getPassName() const override {
return "EraVM replace near-call prepare";
return ERAVM_REPLACE_NEAR_CALL_PREPARE_NAME;
}

void getAnalysisUsage(AnalysisUsage &AU) const override {
ModulePass::getAnalysisUsage(AU);
AU.addRequired<MachineModuleInfoWrapperPass>();
AU.addPreserved<MachineModuleInfoWrapperPass>();
AU.setPreservesAll();
ModulePass::getAnalysisUsage(AU);
}

bool runOnModule(Module &M) override;
};

} // end anonymous namespace

static bool runImpl(Module &M) {
static bool runImpl(Module &M, MachineModuleInfo *MMI) {
bool Changed = false;

// Check whether current function is called via INVOKE, if none, then
Expand All @@ -64,10 +72,14 @@ static bool runImpl(Module &M) {
F.getName() == "__cxa_throw")
continue;

if (any_of(F.uses(), [](const Use &U) {
if (auto *II = dyn_cast<InvokeInst>(U.getUser()))
return true;
return false;
if (any_of(F.uses(),
[](const Use &U) { return isa<InvokeInst>(U.getUser()); }))
continue;

if (any_of(F.uses(), [MMI](const Use &U) {
auto &CallerFunc = *cast<Instruction>(U.getUser())->getFunction();
auto *CallerMF = MMI->getMachineFunction(CallerFunc);
return !CallerMF->getFrameInfo().getNumObjects();
}))
continue;

Expand Down Expand Up @@ -99,22 +111,18 @@ static bool runImpl(Module &M) {
bool EraVMReplaceNearCallPrepare::runOnModule(Module &M) {
if (skipModule(M))
return false;
MachineModuleInfo *MMI =
&getAnalysis<MachineModuleInfoWrapperPass>().getMMI();

return runImpl(M);
return runImpl(M, MMI);
}

char EraVMReplaceNearCallPrepare::ID = 0;

INITIALIZE_PASS(EraVMReplaceNearCallPrepare, "eravm-replace-near-call-prepare",
"Check whether a near-call can be replaced by jump", false,
ERAVM_REPLACE_NEAR_CALL_PREPARE_NAME, false,
false)

ModulePass *llvm::createEraVMReplaceNearCallPreparePass() {
return new EraVMReplaceNearCallPrepare;
}

PreservedAnalyses
EraVMReplaceNearCallPreparePass::run(Module &M, ModuleAnalysisManager &AM) {
runImpl(M);
return PreservedAnalyses::all();
}

0 comments on commit b9ebaea

Please sign in to comment.