2013-08-23 18:27:02 +08:00
|
|
|
//===--- PartiallyInlineLibCalls.cpp - Partially inline libcalls ----------===//
|
2013-06-12 06:21:44 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2013-08-23 18:27:02 +08:00
|
|
|
// This pass tries to partially inline the fast path of well-known library
|
|
|
|
// functions, such as using square-root instructions for cases where sqrt()
|
|
|
|
// does not need to set errno.
|
2013-06-12 06:21:44 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-05-26 07:38:53 +08:00
|
|
|
#include "llvm/Transforms/Scalar/PartiallyInlineLibCalls.h"
|
2016-04-18 17:17:29 +08:00
|
|
|
#include "llvm/Analysis/TargetLibraryInfo.h"
|
2013-08-23 18:27:02 +08:00
|
|
|
#include "llvm/Analysis/TargetTransformInfo.h"
|
2013-06-12 06:21:44 +08:00
|
|
|
#include "llvm/IR/IRBuilder.h"
|
2013-08-23 18:27:02 +08:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2013-06-12 06:21:44 +08:00
|
|
|
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 10:55:47 +08:00
|
|
|
#define DEBUG_TYPE "partially-inline-libcalls"
|
|
|
|
|
2013-06-12 06:21:44 +08:00
|
|
|
|
2016-05-20 23:43:39 +08:00
|
|
|
static bool optimizeSQRT(CallInst *Call, Function *CalledFunc,
|
|
|
|
BasicBlock &CurrBB, Function::iterator &BB) {
|
2013-06-12 06:21:44 +08:00
|
|
|
// There is no need to change the IR, since backend will emit sqrt
|
|
|
|
// instruction if the call has already been marked read-only.
|
|
|
|
if (Call->onlyReadsMemory())
|
|
|
|
return false;
|
|
|
|
|
2014-08-02 07:21:21 +08:00
|
|
|
// The call must have the expected result type.
|
|
|
|
if (!Call->getType()->isFloatingPointTy())
|
|
|
|
return false;
|
|
|
|
|
2013-06-12 06:21:44 +08:00
|
|
|
// Do the following transformation:
|
|
|
|
//
|
|
|
|
// (before)
|
|
|
|
// dst = sqrt(src)
|
|
|
|
//
|
|
|
|
// (after)
|
|
|
|
// v0 = sqrt_noreadmem(src) # native sqrt instruction.
|
|
|
|
// if (v0 is a NaN)
|
|
|
|
// v1 = sqrt(src) # library call.
|
|
|
|
// dst = phi(v0, v1)
|
|
|
|
//
|
|
|
|
|
|
|
|
// Move all instructions following Call to newly created block JoinBB.
|
|
|
|
// Create phi and replace all uses.
|
2015-01-18 10:39:37 +08:00
|
|
|
BasicBlock *JoinBB = llvm::SplitBlock(&CurrBB, Call->getNextNode());
|
2013-06-12 06:21:44 +08:00
|
|
|
IRBuilder<> Builder(JoinBB, JoinBB->begin());
|
|
|
|
PHINode *Phi = Builder.CreatePHI(Call->getType(), 2);
|
|
|
|
Call->replaceAllUsesWith(Phi);
|
|
|
|
|
|
|
|
// Create basic block LibCallBB and insert a call to library function sqrt.
|
|
|
|
BasicBlock *LibCallBB = BasicBlock::Create(CurrBB.getContext(), "call.sqrt",
|
|
|
|
CurrBB.getParent(), JoinBB);
|
|
|
|
Builder.SetInsertPoint(LibCallBB);
|
|
|
|
Instruction *LibCall = Call->clone();
|
|
|
|
Builder.Insert(LibCall);
|
|
|
|
Builder.CreateBr(JoinBB);
|
|
|
|
|
|
|
|
// Add attribute "readnone" so that backend can use a native sqrt instruction
|
|
|
|
// for this call. Insert a FP compare instruction and a conditional branch
|
|
|
|
// at the end of CurrBB.
|
Rename AttributeSet to AttributeList
Summary:
This class is a list of AttributeSetNodes corresponding the function
prototype of a call or function declaration. This class used to be
called ParamAttrListPtr, then AttrListPtr, then AttributeSet. It is
typically accessed by parameter and return value index, so
"AttributeList" seems like a more intuitive name.
Rename AttributeSetImpl to AttributeListImpl to follow suit.
It's useful to rename this class so that we can rename AttributeSetNode
to AttributeSet later. AttributeSet is the set of attributes that apply
to a single function, argument, or return value.
Reviewers: sanjoy, javed.absar, chandlerc, pete
Reviewed By: pete
Subscribers: pete, jholewinski, arsenm, dschuff, mehdi_amini, jfb, nhaehnle, sbc100, void, llvm-commits
Differential Revision: https://reviews.llvm.org/D31102
llvm-svn: 298393
2017-03-22 00:57:19 +08:00
|
|
|
Call->addAttribute(AttributeList::FunctionIndex, Attribute::ReadNone);
|
2013-06-12 06:21:44 +08:00
|
|
|
CurrBB.getTerminator()->eraseFromParent();
|
|
|
|
Builder.SetInsertPoint(&CurrBB);
|
|
|
|
Value *FCmp = Builder.CreateFCmpOEQ(Call, Call);
|
|
|
|
Builder.CreateCondBr(FCmp, JoinBB, LibCallBB);
|
|
|
|
|
|
|
|
// Add phi operands.
|
|
|
|
Phi->addIncoming(Call, &CurrBB);
|
|
|
|
Phi->addIncoming(LibCall, LibCallBB);
|
|
|
|
|
2015-10-14 03:26:58 +08:00
|
|
|
BB = JoinBB->getIterator();
|
2013-06-12 06:21:44 +08:00
|
|
|
return true;
|
|
|
|
}
|
2013-08-23 18:27:02 +08:00
|
|
|
|
2016-05-26 07:38:53 +08:00
|
|
|
static bool runPartiallyInlineLibCalls(Function &F, TargetLibraryInfo *TLI,
|
|
|
|
const TargetTransformInfo *TTI) {
|
2016-05-20 23:43:39 +08:00
|
|
|
bool Changed = false;
|
2016-05-26 07:38:53 +08:00
|
|
|
|
2016-05-20 23:43:39 +08:00
|
|
|
Function::iterator CurrBB;
|
|
|
|
for (Function::iterator BB = F.begin(), BE = F.end(); BB != BE;) {
|
|
|
|
CurrBB = BB++;
|
|
|
|
|
|
|
|
for (BasicBlock::iterator II = CurrBB->begin(), IE = CurrBB->end();
|
|
|
|
II != IE; ++II) {
|
|
|
|
CallInst *Call = dyn_cast<CallInst>(&*II);
|
|
|
|
Function *CalledFunc;
|
|
|
|
|
|
|
|
if (!Call || !(CalledFunc = Call->getCalledFunction()))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Skip if function either has local linkage or is not a known library
|
|
|
|
// function.
|
[Analysis] Add LibFunc_ prefix to enums in TargetLibraryInfo. (NFC)
Summary:
The LibFunc::Func enum holds enumerators named for libc functions.
Unfortunately, there are real situations, including libc implementations, where
function names are actually macros (musl uses "#define fopen64 fopen", for
example; any other transitively visible macro would have similar effects).
Strictly speaking, a conforming C++ Standard Library should provide any such
macros as functions instead (via <cstdio>). However, there are some "library"
functions which are not part of the standard, and thus not subject to this
rule (fopen64, for example). So, in order to be both portable and consistent,
the enum should not use the bare function names.
The old enum naming used a namespace LibFunc and an enum Func, with bare
enumerators. This patch changes LibFunc to be an enum with enumerators prefixed
with "LibFFunc_". (Unfortunately, a scoped enum is not sufficient to override
macros.)
There are additional changes required in clang.
Reviewers: rsmith
Subscribers: mehdi_amini, mzolotukhin, nemanjai, llvm-commits
Differential Revision: https://reviews.llvm.org/D28476
llvm-svn: 292848
2017-01-24 07:16:46 +08:00
|
|
|
LibFunc LF;
|
2016-05-20 23:43:39 +08:00
|
|
|
if (CalledFunc->hasLocalLinkage() || !CalledFunc->hasName() ||
|
[Analysis] Add LibFunc_ prefix to enums in TargetLibraryInfo. (NFC)
Summary:
The LibFunc::Func enum holds enumerators named for libc functions.
Unfortunately, there are real situations, including libc implementations, where
function names are actually macros (musl uses "#define fopen64 fopen", for
example; any other transitively visible macro would have similar effects).
Strictly speaking, a conforming C++ Standard Library should provide any such
macros as functions instead (via <cstdio>). However, there are some "library"
functions which are not part of the standard, and thus not subject to this
rule (fopen64, for example). So, in order to be both portable and consistent,
the enum should not use the bare function names.
The old enum naming used a namespace LibFunc and an enum Func, with bare
enumerators. This patch changes LibFunc to be an enum with enumerators prefixed
with "LibFFunc_". (Unfortunately, a scoped enum is not sufficient to override
macros.)
There are additional changes required in clang.
Reviewers: rsmith
Subscribers: mehdi_amini, mzolotukhin, nemanjai, llvm-commits
Differential Revision: https://reviews.llvm.org/D28476
llvm-svn: 292848
2017-01-24 07:16:46 +08:00
|
|
|
!TLI->getLibFunc(CalledFunc->getName(), LF))
|
2016-05-20 23:43:39 +08:00
|
|
|
continue;
|
|
|
|
|
[Analysis] Add LibFunc_ prefix to enums in TargetLibraryInfo. (NFC)
Summary:
The LibFunc::Func enum holds enumerators named for libc functions.
Unfortunately, there are real situations, including libc implementations, where
function names are actually macros (musl uses "#define fopen64 fopen", for
example; any other transitively visible macro would have similar effects).
Strictly speaking, a conforming C++ Standard Library should provide any such
macros as functions instead (via <cstdio>). However, there are some "library"
functions which are not part of the standard, and thus not subject to this
rule (fopen64, for example). So, in order to be both portable and consistent,
the enum should not use the bare function names.
The old enum naming used a namespace LibFunc and an enum Func, with bare
enumerators. This patch changes LibFunc to be an enum with enumerators prefixed
with "LibFFunc_". (Unfortunately, a scoped enum is not sufficient to override
macros.)
There are additional changes required in clang.
Reviewers: rsmith
Subscribers: mehdi_amini, mzolotukhin, nemanjai, llvm-commits
Differential Revision: https://reviews.llvm.org/D28476
llvm-svn: 292848
2017-01-24 07:16:46 +08:00
|
|
|
switch (LF) {
|
|
|
|
case LibFunc_sqrtf:
|
|
|
|
case LibFunc_sqrt:
|
2016-05-20 23:43:39 +08:00
|
|
|
if (TTI->haveFastSqrt(Call->getType()) &&
|
|
|
|
optimizeSQRT(Call, CalledFunc, *CurrBB, BB))
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Changed = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2016-05-26 07:38:53 +08:00
|
|
|
PreservedAnalyses
|
2016-08-09 08:28:15 +08:00
|
|
|
PartiallyInlineLibCallsPass::run(Function &F, FunctionAnalysisManager &AM) {
|
2016-05-26 07:38:53 +08:00
|
|
|
auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
|
|
|
|
auto &TTI = AM.getResult<TargetIRAnalysis>(F);
|
|
|
|
if (!runPartiallyInlineLibCalls(F, &TLI, &TTI))
|
|
|
|
return PreservedAnalyses::all();
|
|
|
|
return PreservedAnalyses::none();
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class PartiallyInlineLibCallsLegacyPass : public FunctionPass {
|
|
|
|
public:
|
|
|
|
static char ID;
|
|
|
|
|
|
|
|
PartiallyInlineLibCallsLegacyPass() : FunctionPass(ID) {
|
|
|
|
initializePartiallyInlineLibCallsLegacyPassPass(
|
|
|
|
*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
|
|
|
AU.addRequired<TargetLibraryInfoWrapperPass>();
|
|
|
|
AU.addRequired<TargetTransformInfoWrapperPass>();
|
|
|
|
FunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool runOnFunction(Function &F) override {
|
|
|
|
if (skipFunction(F))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
TargetLibraryInfo *TLI =
|
|
|
|
&getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
|
|
|
|
const TargetTransformInfo *TTI =
|
|
|
|
&getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
|
|
|
|
return runPartiallyInlineLibCalls(F, TLI, TTI);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
char PartiallyInlineLibCallsLegacyPass::ID = 0;
|
|
|
|
INITIALIZE_PASS_BEGIN(PartiallyInlineLibCallsLegacyPass,
|
|
|
|
"partially-inline-libcalls",
|
|
|
|
"Partially inline calls to library functions", false,
|
|
|
|
false)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
|
|
|
|
INITIALIZE_PASS_END(PartiallyInlineLibCallsLegacyPass,
|
|
|
|
"partially-inline-libcalls",
|
|
|
|
"Partially inline calls to library functions", false, false)
|
|
|
|
|
2013-08-23 18:27:02 +08:00
|
|
|
FunctionPass *llvm::createPartiallyInlineLibCallsPass() {
|
2016-05-26 07:38:53 +08:00
|
|
|
return new PartiallyInlineLibCallsLegacyPass();
|
2013-08-23 18:27:02 +08:00
|
|
|
}
|