From f6bb69c6a03c1e98b28df3453fae2edc7ca20704 Mon Sep 17 00:00:00 2001 From: John Porto Date: Mon, 24 Oct 2022 22:16:30 -0700 Subject: [PATCH] Minor fixes when accesing CallInst operands Summary: Fix minor issues found while adding support for emitting textual function names when calling non-callables. Reviewed By: tmikov, avp Differential Revision: D40617534 fbshipit-source-id: 02b8782159d4f30f10fc875f47a947fd822e9f57 --- include/hermes/IR/Instrs.h | 6 +++++- lib/BCGen/HBC/Passes.cpp | 2 +- lib/Optimizer/Scalar/FuncSigOpts.cpp | 4 ++-- lib/Utils/Dumper.cpp | 2 +- 4 files changed, 9 insertions(+), 5 deletions(-) diff --git a/include/hermes/IR/Instrs.h b/include/hermes/IR/Instrs.h index cf9b8183e..8fba0106c 100644 --- a/include/hermes/IR/Instrs.h +++ b/include/hermes/IR/Instrs.h @@ -699,6 +699,10 @@ class CallInst : public Instruction { CallInst(const CallInst &) = delete; void operator=(const CallInst &) = delete; + // Forces the code to use the appropriate getters instead of relying on + // hard-coded offsets when accessing the arguments. + using Instruction::getOperand; + public: enum { CalleeIdx, ThisIdx }; @@ -721,7 +725,7 @@ class CallInst : public Instruction { } unsigned getNumArguments() const { - return getNumOperands() - 1; + return getNumOperands() - ThisIdx; } explicit CallInst( diff --git a/lib/BCGen/HBC/Passes.cpp b/lib/BCGen/HBC/Passes.cpp index f36512028..8c60f49ce 100644 --- a/lib/BCGen/HBC/Passes.cpp +++ b/lib/BCGen/HBC/Passes.cpp @@ -811,7 +811,7 @@ bool SpillRegisters::requiresShortOperand(Instruction *I, int op) { case ValueKind::CallBuiltinInstKind: case ValueKind::HBCConstructInstKind: case ValueKind::HBCCallDirectInstKind: - return op == 0; + return op == CallInst::CalleeIdx; default: return true; } diff --git a/lib/Optimizer/Scalar/FuncSigOpts.cpp b/lib/Optimizer/Scalar/FuncSigOpts.cpp index 3259b5125..4a174c4a9 100644 --- a/lib/Optimizer/Scalar/FuncSigOpts.cpp +++ b/lib/Optimizer/Scalar/FuncSigOpts.cpp @@ -149,11 +149,11 @@ static bool performFSO(Function *F, std::vector &worklist) { // Replace all unused arguments with undef. for (auto &arg : unusedParams) { - auto *prevArg = arg.first->getOperand(arg.second + 1); + auto *prevArg = arg.first->getArgument(arg.second); if (!llvh::isa(prevArg)) toRedo.insert(arg.first->getParent()->getParent()); - arg.first->setOperand(undef, arg.second + 1); + arg.first->setArgument(undef, arg.second); NumArgsOpt++; } diff --git a/lib/Utils/Dumper.cpp b/lib/Utils/Dumper.cpp index d7431829f..e16327d86 100644 --- a/lib/Utils/Dumper.cpp +++ b/lib/Utils/Dumper.cpp @@ -99,7 +99,7 @@ void IRPrinter::printTypeLabel(Type T) { void IRPrinter::printValueLabel(Instruction *I, Value *V, unsigned opIndex) { auto &ctx = I->getContext(); - if (isa(I) && opIndex == 0) { + if (isa(I) && opIndex == CallInst::CalleeIdx) { os << "[" << getBuiltinMethodName(cast(I)->getBuiltinIndex()) << "]";