From 0cb3eedd37fd338b91e4d3cda5c17bf9bc796fdc Mon Sep 17 00:00:00 2001 From: Alessandro Di Federico Date: Wed, 7 Aug 2024 15:17:13 +0200 Subject: [PATCH] Do not use CallBase::getCalledFunction Use our wrapper, which does not return nullptr if the `FunctionType`s do not match. --- include/revng-c/Support/DecompilationHelpers.h | 4 ++-- lib/Backend/DecompileFunction.cpp | 14 +++++++------- lib/Canonicalize/HoistStructPhis.cpp | 3 ++- lib/Canonicalize/ImplicitModelCastPass.cpp | 2 +- lib/Canonicalize/MakeModelCastPass.cpp | 2 +- lib/Canonicalize/MakeModelGEPPass.cpp | 4 ++-- .../OperatorPrecedenceResolutionPass.cpp | 4 ++-- lib/Canonicalize/PrepareLLVMIRForMLIR.cpp | 2 +- .../Backend/DLAUpdateModelTypes.cpp | 2 +- .../Frontend/SCEVBaseAddressExplorer.cpp | 2 +- lib/InitModelTypes/InitModelTypes.cpp | 2 +- .../SegregateStackAccessesPass.cpp | 6 +++--- lib/RemoveLiftingArtifacts/CleanupIRPass.cpp | 2 +- .../PromoteInitCSVToUndef.cpp | 2 +- lib/RestructureCFG/FallThroughScopeAnalysis.cpp | 4 ++-- lib/Support/ModelHelpers.cpp | 4 ++-- 16 files changed, 30 insertions(+), 29 deletions(-) diff --git a/include/revng-c/Support/DecompilationHelpers.h b/include/revng-c/Support/DecompilationHelpers.h index 9ee5fa8bb..05b01ef66 100644 --- a/include/revng-c/Support/DecompilationHelpers.h +++ b/include/revng-c/Support/DecompilationHelpers.h @@ -36,7 +36,7 @@ inline bool mayReadMemory(const llvm::Instruction &I) { // We have to hardcode revng_call_stack_arguments and revng_stack_frame // because SegregateStackAccesses has to mark them as functions that read // inaccessible memory, in order to prevent some LLVM optimizations. - if (llvm::Function *Callee = Call->getCalledFunction()) { + if (llvm::Function *Callee = getCalledFunction(Call)) { llvm::StringRef Name = Callee->getName(); if (Name.startswith("revng_call_stack_arguments") or Name.startswith("revng_stack_frame")) @@ -95,7 +95,7 @@ inline bool isCallStackArgumentDecl(const llvm::Value *I) { if (not Call) return false; - auto *Callee = Call->getCalledFunction(); + auto *Callee = getCalledFunction(Call); if (not Callee) return false; diff --git a/lib/Backend/DecompileFunction.cpp b/lib/Backend/DecompileFunction.cpp index 54e9cad88..2cbeca9cb 100644 --- a/lib/Backend/DecompileFunction.cpp +++ b/lib/Backend/DecompileFunction.cpp @@ -110,7 +110,7 @@ static bool isStackFrameDecl(const llvm::Value *I) { if (not Call) return false; - auto *Callee = Call->getCalledFunction(); + auto *Callee = getCalledFunction(Call); if (not Callee) return false; @@ -824,7 +824,7 @@ CCodeGenerator::getCustomOpcodeToken(const llvm::CallInst *Call) const { const auto *I = llvm::cast(Call->getArgOperand(1)); const auto *CallReturnsStruct = llvm::cast(AggregateOp); - const llvm::Function *Callee = CallReturnsStruct->getCalledFunction(); + const llvm::Function *Callee = getCalledFunction(CallReturnsStruct); const auto &CalleePrototype = getCallSitePrototype(Model, CallReturnsStruct); @@ -846,7 +846,7 @@ CCodeGenerator::getCustomOpcodeToken(const llvm::CallInst *Call) const { } if (isCallToTagged(Call, FunctionTags::SegmentRef)) { - auto *Callee = Call->getCalledFunction(); + auto *Callee = getCalledFunction(Call); const auto &[StartAddress, VirtualSize] = extractSegmentKeyFromMetadata(*Callee); model::Segment Segment = Model.Segments().at({ StartAddress, VirtualSize }); @@ -859,7 +859,7 @@ CCodeGenerator::getCustomOpcodeToken(const llvm::CallInst *Call) const { rc_return rc_recur getToken(Call->getArgOperand(0)); if (isCallToTagged(Call, FunctionTags::OpaqueCSVValue)) { - auto *Callee = Call->getCalledFunction(); + auto *Callee = getCalledFunction(Call); std::string HelperRef = getHelperFunctionLocationReference(Callee, B); rc_return rc_recur getCallToken(Call, HelperRef, /*prototype=*/nullptr); } @@ -925,7 +925,7 @@ CCodeGenerator::getIsolatedCallToken(const llvm::CallInst *Call) const { .serialize(); } else { // Isolated function - llvm::Function *CalledFunc = Call->getCalledFunction(); + llvm::Function *CalledFunc = getCalledFunction(Call); revng_assert(CalledFunc); const model::Function *ModelFunc = llvmToModelFunction(Model, *CalledFunc); @@ -949,7 +949,7 @@ CCodeGenerator::getIsolatedCallToken(const llvm::CallInst *Call) const { RecursiveCoroutine CCodeGenerator::getNonIsolatedCallToken(const llvm::CallInst *Call) const { - auto *CalledFunc = Call->getCalledFunction(); + auto *CalledFunc = getCalledFunction(Call); revng_assert(CalledFunc and CalledFunc->hasName(), "Special functions should all have a name"); @@ -2021,7 +2021,7 @@ static ASTVarDeclMap computeVariableDeclarationScope(const llvm::Function &F, } revng_assert(not isCallToNonIsolated(Call) - or not Call->getCalledFunction()->isTargetIntrinsic()); + or not getCalledFunction(Call)->isTargetIntrinsic()); } } diff --git a/lib/Canonicalize/HoistStructPhis.cpp b/lib/Canonicalize/HoistStructPhis.cpp index 9f8fa126f..1598e99bf 100644 --- a/lib/Canonicalize/HoistStructPhis.cpp +++ b/lib/Canonicalize/HoistStructPhis.cpp @@ -6,6 +6,7 @@ #include "revng/Support/Debug.h" #include "revng/Support/FunctionTags.h" +#include "revng/Support/IRHelpers.h" using namespace llvm; @@ -57,7 +58,7 @@ public: CalledValue = Call->getCalledOperand(); FirstCall = Call; - Function *Callee = Call->getCalledFunction(); + Function *Callee = getCalledFunction(Call); revng_assert(Callee != nullptr); revng_assert(isLastBeforeTerminator(Call) diff --git a/lib/Canonicalize/ImplicitModelCastPass.cpp b/lib/Canonicalize/ImplicitModelCastPass.cpp index 3cc955d97..54821ed5f 100644 --- a/lib/Canonicalize/ImplicitModelCastPass.cpp +++ b/lib/Canonicalize/ImplicitModelCastPass.cpp @@ -424,7 +424,7 @@ bool IMCP::collectTypeInfoForTypePromotion(llvm::Instruction *I, }; if (auto *Call = dyn_cast(I)) { - auto *Callee = Call->getCalledFunction(); + auto *Callee = getCalledFunction(Call); if (isCallToIsolatedFunction(Call)) { if (not Callee) CheckTypeFor(Call->getCalledOperandUse()); diff --git a/lib/Canonicalize/MakeModelCastPass.cpp b/lib/Canonicalize/MakeModelCastPass.cpp index adee3a555..5611340ed 100644 --- a/lib/Canonicalize/MakeModelCastPass.cpp +++ b/lib/Canonicalize/MakeModelCastPass.cpp @@ -97,7 +97,7 @@ MMCP::serializeTypesForModelCast(Instruction *I, const model::Binary &Model) { if (auto *Call = dyn_cast(I)) { // Lifted functions have their prototype on the model - auto *Callee = Call->getCalledFunction(); + auto *Callee = getCalledFunction(Call); if (isCallToIsolatedFunction(Call)) { // For indirect calls, cast the callee to the right function type diff --git a/lib/Canonicalize/MakeModelGEPPass.cpp b/lib/Canonicalize/MakeModelGEPPass.cpp index 82cd7c68d..d3a44b53a 100644 --- a/lib/Canonicalize/MakeModelGEPPass.cpp +++ b/lib/Canonicalize/MakeModelGEPPass.cpp @@ -1698,7 +1698,7 @@ getAccessedTypeOnIR(const llvm::Use &U, // Assert that we're returning a proper struct, initialized with // struct initializers, but don't do anything here. - const auto *Returned = cast(RetVal)->getCalledFunction(); + const auto *Returned = getCalledFunction(cast(RetVal)); revng_assert(FunctionTags::StructInitializer.isTagOf(Returned)); } break; @@ -1736,7 +1736,7 @@ getAccessedTypeOnIR(const llvm::Use &U, if (isCallToTagged(Call, FunctionTags::StructInitializer)) { - const llvm::Function &CalledF = *Call->getCalledFunction(); + const llvm::Function &CalledF = *getCalledFunction(Call); // Special case for struct initializers unsigned ArgNum = Call->getArgOperandNo(&U); diff --git a/lib/Canonicalize/OperatorPrecedenceResolutionPass.cpp b/lib/Canonicalize/OperatorPrecedenceResolutionPass.cpp index 3dd2132f2..6a7c3670f 100644 --- a/lib/Canonicalize/OperatorPrecedenceResolutionPass.cpp +++ b/lib/Canonicalize/OperatorPrecedenceResolutionPass.cpp @@ -146,7 +146,7 @@ static bool isCustomOpcode(const Value *I) { if (nullptr == Call) return false; - const auto *CalledFunc = Call->getCalledFunction(); + const auto *CalledFunc = getCalledFunction(Call); if (nullptr == CalledFunc) return false; @@ -170,7 +170,7 @@ static bool isCustomOpcode(const Value *I) { static unsigned getCustomOpcode(const Instruction *I) { revng_assert(isCustomOpcode(I)); - auto *CalledFunc = cast(I)->getCalledFunction(); + auto *CalledFunc = getCalledFunction(cast(I)); revng_assert(CalledFunc); if (FunctionTags::AddressOf.isTagOf(CalledFunc)) diff --git a/lib/Canonicalize/PrepareLLVMIRForMLIR.cpp b/lib/Canonicalize/PrepareLLVMIRForMLIR.cpp index bd66bff19..876bb67b0 100644 --- a/lib/Canonicalize/PrepareLLVMIRForMLIR.cpp +++ b/lib/Canonicalize/PrepareLLVMIRForMLIR.cpp @@ -119,7 +119,7 @@ static void adjustRevngMetadata(Module &M) { // artificial one - DILocation(line: 0)), since calls to inlinable // functions must have a !dbg attachment. if (Call->getFunction()->getSubprogram() - and Call->getCalledFunction()) { + and getCalledFunction(Call)) { auto Location = DILocation::get(M.getContext(), 0, 0, diff --git a/lib/DataLayoutAnalysis/Backend/DLAUpdateModelTypes.cpp b/lib/DataLayoutAnalysis/Backend/DLAUpdateModelTypes.cpp index e3e07967f..f905d8cbb 100644 --- a/lib/DataLayoutAnalysis/Backend/DLAUpdateModelTypes.cpp +++ b/lib/DataLayoutAnalysis/Backend/DLAUpdateModelTypes.cpp @@ -658,7 +658,7 @@ static bool updateStackFrameType(model::Function &ModelFunc, if (not Call) continue; - auto *Callee = Call->getCalledFunction(); + auto *Callee = getCalledFunction(Call); if (not Callee or Callee->getName() != "revng_stack_frame") continue; diff --git a/lib/DataLayoutAnalysis/Frontend/SCEVBaseAddressExplorer.cpp b/lib/DataLayoutAnalysis/Frontend/SCEVBaseAddressExplorer.cpp index 745b46db1..39f128341 100644 --- a/lib/DataLayoutAnalysis/Frontend/SCEVBaseAddressExplorer.cpp +++ b/lib/DataLayoutAnalysis/Frontend/SCEVBaseAddressExplorer.cpp @@ -27,7 +27,7 @@ static bool isConstantAddress(const llvm::ConstantInt *C) { // address. static bool isAlwaysAddress(const llvm::Value *V) { if (auto *Call = dyn_cast_or_null(V)) - if (auto *Callee = Call->getCalledFunction()) + if (auto *Callee = getCalledFunction(Call)) if (FunctionTags::ReturnsPolymorphic.isTagOf(Callee) or FunctionTags::AddressOf.isTagOf(Callee)) return true; diff --git a/lib/InitModelTypes/InitModelTypes.cpp b/lib/InitModelTypes/InitModelTypes.cpp index a547f55a1..7413d5ddc 100644 --- a/lib/InitModelTypes/InitModelTypes.cpp +++ b/lib/InitModelTypes/InitModelTypes.cpp @@ -162,7 +162,7 @@ static TypeVector getReturnTypes(const llvm::CallInst *Call, if (not ReturnTypes.empty()) return ReturnTypes; - auto *CalledFunc = Call->getCalledFunction(); + auto *CalledFunc = getCalledFunction(Call); revng_assert(CalledFunc); if (FunctionTags::Parentheses.isTagOf(CalledFunc) diff --git a/lib/PromoteStackPointer/SegregateStackAccessesPass.cpp b/lib/PromoteStackPointer/SegregateStackAccessesPass.cpp index 984df5a91..918925fab 100644 --- a/lib/PromoteStackPointer/SegregateStackAccessesPass.cpp +++ b/lib/PromoteStackPointer/SegregateStackAccessesPass.cpp @@ -526,7 +526,7 @@ private: for (BasicBlock &BB : *NewFunction) { if (auto *Ret = dyn_cast(BB.getTerminator())) { auto *Call = cast(Ret->getReturnValue()); - auto *Callee = Call->getCalledFunction(); + auto *Callee = getCalledFunction(Call); revng_assert(Call != nullptr); revng_assert(FunctionTags::StructInitializer.isTagOf(Callee)); } @@ -713,7 +713,7 @@ private: ReturnValues.push_back(RetValue); } else { auto *Call = cast(Ret->getReturnValue()); - auto *Callee = Call->getCalledFunction(); + auto *Callee = getCalledFunction(Call); revng_assert(Call != nullptr); revng_assert(FunctionTags::StructInitializer.isTagOf(Callee)); llvm::copy(Call->args(), std::back_inserter(ReturnValues)); @@ -751,7 +751,7 @@ private: // Turn struct_initializer into a an integer for (ReturnInst *Ret : Returns) { auto *Call = cast(Ret->getReturnValue()); - auto *Callee = Call->getCalledFunction(); + auto *Callee = getCalledFunction(Call); revng_assert(Call != nullptr); revng_assert(FunctionTags::StructInitializer.isTagOf(Callee)); diff --git a/lib/RemoveLiftingArtifacts/CleanupIRPass.cpp b/lib/RemoveLiftingArtifacts/CleanupIRPass.cpp index c6c04a5bb..1571c0821 100644 --- a/lib/RemoveLiftingArtifacts/CleanupIRPass.cpp +++ b/lib/RemoveLiftingArtifacts/CleanupIRPass.cpp @@ -84,7 +84,7 @@ bool CleanupIRPass::Impl::replaceInstructions(Function &F) { IRBuilder<> Builder(Context); Builder.SetInsertPointPastAllocas(Call->getFunction()); Value *AllocatedSize = nullptr; - if (auto *Callee = Call->getCalledFunction(); + if (auto *Callee = getCalledFunction(Call); Callee and Callee->getName().startswith("revng_stack_frame")) { AllocatedSize = Call->getArgOperand(0); } else { diff --git a/lib/RemoveLiftingArtifacts/PromoteInitCSVToUndef.cpp b/lib/RemoveLiftingArtifacts/PromoteInitCSVToUndef.cpp index 49e88c214..dcd5492f8 100644 --- a/lib/RemoveLiftingArtifacts/PromoteInitCSVToUndef.cpp +++ b/lib/RemoveLiftingArtifacts/PromoteInitCSVToUndef.cpp @@ -31,7 +31,7 @@ undefPreservedRegistersInitialization(Function &F, auto Next = std::next(It); if (auto *Call = dyn_cast(&*It)) { - auto *Callee = Call->getCalledFunction(); + auto *Callee = getCalledFunction(Call); const char *MDName = "revng.abi_register"; diff --git a/lib/RestructureCFG/FallThroughScopeAnalysis.cpp b/lib/RestructureCFG/FallThroughScopeAnalysis.cpp index 0533d820b..d0cbc7711 100644 --- a/lib/RestructureCFG/FallThroughScopeAnalysis.cpp +++ b/lib/RestructureCFG/FallThroughScopeAnalysis.cpp @@ -194,7 +194,7 @@ fallThroughScopeImpl(const model::Binary &Model, // use the `llvmToModelFunction` helper in order to retrieve the // corresponding `model::Function` to check for the `NoReturn` // attribute. - const Function *CalleeFunction = Call->getCalledFunction(); + const Function *CalleeFunction = getCalledFunction(Call); const model::Function *CalleeFunctionModel = llvmToModelFunction(Model, *CalleeFunction); if (isNoReturn(*CalleeFunctionModel)) { @@ -208,7 +208,7 @@ fallThroughScopeImpl(const model::Binary &Model, // The called function may be a dynamic function. In this case, we use // the name of the dynamic symbol in order to retrieve the // `model::DynamicFunction` and check for the `NoReturn` attribute. - const Function *CalleeFunction = Call->getCalledFunction(); + const Function *CalleeFunction = getCalledFunction(Call); llvm::StringRef SymbolName = CalleeFunction->getName() .drop_front(strlen("dynamic_")); const model::DynamicFunction diff --git a/lib/Support/ModelHelpers.cpp b/lib/Support/ModelHelpers.cpp index 52682d913..a9c13291d 100644 --- a/lib/Support/ModelHelpers.cpp +++ b/lib/Support/ModelHelpers.cpp @@ -276,7 +276,7 @@ getStrongModelInfo(const llvm::Instruction *Inst, const model::Binary &Model) { } else { // Non-isolated functions do not have a Prototype in the model, but we can // infer their returned type(s) in other ways - auto *CalledFunc = Call->getCalledFunction(); + auto *CalledFunc = getCalledFunction(Call); const auto &FuncName = CalledFunc->getName(); auto FTags = FunctionTags::TagsSet::from(CalledFunc); @@ -401,7 +401,7 @@ getExpectedModelType(const llvm::Use *U, const model::Binary &Model) { revng_assert(not Call->isIndirectCall()); unsigned int ArgOperandIdx = Call->getArgOperandNo(U); - auto *CalledFunc = Call->getCalledFunction(); + auto *CalledFunc = getCalledFunction(Call); auto FTags = FunctionTags::TagsSet::from(CalledFunc); if (FTags.contains(FunctionTags::AddressOf)