A new set of overloaded functions named getOrInsertLibFunc() are now supposed to be used instead of getOrInsertFunction() when building a libcall from within an LLVM optimizer(). The idea is that this new function also makes sure that any mandatory argument attributes are added to the function prototype (after calling getOrInsertFunction()). inferLibFuncAttributes() is renamed to inferNonMandatoryLibFuncAttrs() as it only adds attributes that are not necessary for correctness but merely helping with later optimizations. Generally, the front end is responsible for building a correct function prototype with the needed argument attributes. If the middle end however is the one creating the call, e.g. when replacing one libcall with another, it then must take this responsibility. This continues the work of properly handling argument extension if required by the target ABI when building a lib call. getOrInsertLibFunc() now does this for all libcalls currently built by any LLVM optimizer. It is expected that when in the future a new optimization builds a new libcall with an integer argument it is to be added to getOrInsertLibFunc() with the proper handling. Note that not all targets have it in their ABI to sign/zero extend integer arguments to the full register width, but this will be done selectively as determined by getExtAttrForI32Param(). Review: Eli Friedman, Nikita Popov, Dávid Bolvanský Differential Revision: https://reviews.llvm.org/D123198
90 lines
3.2 KiB
C++
90 lines
3.2 KiB
C++
//===- InferFunctionAttrs.cpp - Infer implicit function attributes --------===//
|
|
//
|
|
// 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "llvm/Transforms/IPO/InferFunctionAttrs.h"
|
|
#include "llvm/Analysis/TargetLibraryInfo.h"
|
|
#include "llvm/IR/Function.h"
|
|
#include "llvm/IR/Module.h"
|
|
#include "llvm/InitializePasses.h"
|
|
#include "llvm/Transforms/Utils/BuildLibCalls.h"
|
|
#include "llvm/Transforms/Utils/Local.h"
|
|
using namespace llvm;
|
|
|
|
#define DEBUG_TYPE "inferattrs"
|
|
|
|
static bool inferAllPrototypeAttributes(
|
|
Module &M, function_ref<TargetLibraryInfo &(Function &)> GetTLI) {
|
|
bool Changed = false;
|
|
|
|
for (Function &F : M.functions())
|
|
// We only infer things using the prototype and the name; we don't need
|
|
// definitions. This ensures libfuncs are annotated and also allows our
|
|
// CGSCC inference to avoid needing to duplicate the inference from other
|
|
// attribute logic on all calls to declarations (as declarations aren't
|
|
// explicitly visited by CGSCC passes in the new pass manager.)
|
|
if (F.isDeclaration() && !F.hasOptNone()) {
|
|
if (!F.hasFnAttribute(Attribute::NoBuiltin))
|
|
Changed |= inferNonMandatoryLibFuncAttrs(F, GetTLI(F));
|
|
Changed |= inferAttributesFromOthers(F);
|
|
}
|
|
|
|
return Changed;
|
|
}
|
|
|
|
PreservedAnalyses InferFunctionAttrsPass::run(Module &M,
|
|
ModuleAnalysisManager &AM) {
|
|
FunctionAnalysisManager &FAM =
|
|
AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
|
|
auto GetTLI = [&FAM](Function &F) -> TargetLibraryInfo & {
|
|
return FAM.getResult<TargetLibraryAnalysis>(F);
|
|
};
|
|
|
|
if (!inferAllPrototypeAttributes(M, GetTLI))
|
|
// If we didn't infer anything, preserve all analyses.
|
|
return PreservedAnalyses::all();
|
|
|
|
// Otherwise, we may have changed fundamental function attributes, so clear
|
|
// out all the passes.
|
|
return PreservedAnalyses::none();
|
|
}
|
|
|
|
namespace {
|
|
struct InferFunctionAttrsLegacyPass : public ModulePass {
|
|
static char ID; // Pass identification, replacement for typeid
|
|
InferFunctionAttrsLegacyPass() : ModulePass(ID) {
|
|
initializeInferFunctionAttrsLegacyPassPass(
|
|
*PassRegistry::getPassRegistry());
|
|
}
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
|
AU.addRequired<TargetLibraryInfoWrapperPass>();
|
|
}
|
|
|
|
bool runOnModule(Module &M) override {
|
|
if (skipModule(M))
|
|
return false;
|
|
|
|
auto GetTLI = [this](Function &F) -> TargetLibraryInfo & {
|
|
return this->getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
|
|
};
|
|
return inferAllPrototypeAttributes(M, GetTLI);
|
|
}
|
|
};
|
|
}
|
|
|
|
char InferFunctionAttrsLegacyPass::ID = 0;
|
|
INITIALIZE_PASS_BEGIN(InferFunctionAttrsLegacyPass, "inferattrs",
|
|
"Infer set function attributes", false, false)
|
|
INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
|
|
INITIALIZE_PASS_END(InferFunctionAttrsLegacyPass, "inferattrs",
|
|
"Infer set function attributes", false, false)
|
|
|
|
Pass *llvm::createInferFunctionAttrsLegacyPass() {
|
|
return new InferFunctionAttrsLegacyPass();
|
|
}
|