2013-08-23 18:27:02 +08:00
|
|
|
//===--- PartiallyInlineLibCalls.cpp - Partially inline libcalls ----------===//
|
2013-06-12 06:21:44 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// 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
|
2013-06-12 06:21:44 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
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"
|
Sink all InitializePasses.h includes
This file lists every pass in LLVM, and is included by Pass.h, which is
very popular. Every time we add, remove, or rename a pass in LLVM, it
caused lots of recompilation.
I found this fact by looking at this table, which is sorted by the
number of times a file was changed over the last 100,000 git commits
multiplied by the number of object files that depend on it in the
current checkout:
recompiles touches affected_files header
342380 95 3604 llvm/include/llvm/ADT/STLExtras.h
314730 234 1345 llvm/include/llvm/InitializePasses.h
307036 118 2602 llvm/include/llvm/ADT/APInt.h
213049 59 3611 llvm/include/llvm/Support/MathExtras.h
170422 47 3626 llvm/include/llvm/Support/Compiler.h
162225 45 3605 llvm/include/llvm/ADT/Optional.h
158319 63 2513 llvm/include/llvm/ADT/Triple.h
140322 39 3598 llvm/include/llvm/ADT/StringRef.h
137647 59 2333 llvm/include/llvm/Support/Error.h
131619 73 1803 llvm/include/llvm/Support/FileSystem.h
Before this change, touching InitializePasses.h would cause 1345 files
to recompile. After this change, touching it only causes 550 compiles in
an incremental rebuild.
Reviewers: bkramer, asbirlea, bollu, jdoerfert
Differential Revision: https://reviews.llvm.org/D70211
2019-11-14 05:15:01 +08:00
|
|
|
#include "llvm/InitializePasses.h"
|
2018-09-14 04:33:04 +08:00
|
|
|
#include "llvm/Support/DebugCounter.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"
|
|
|
|
|
2018-09-14 04:33:04 +08:00
|
|
|
DEBUG_COUNTER(PILCounter, "partially-inline-libcalls-transform",
|
|
|
|
"Controls transformations in 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,
|
2017-11-28 05:15:43 +08:00
|
|
|
BasicBlock &CurrBB, Function::iterator &BB,
|
|
|
|
const TargetTransformInfo *TTI) {
|
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;
|
|
|
|
|
2018-09-14 04:33:04 +08:00
|
|
|
if (!DebugCounter::shouldExecute(PILCounter))
|
|
|
|
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.
|
2017-11-28 05:15:43 +08:00
|
|
|
// [if (v0 is a NaN) || if (src < 0)]
|
2013-06-12 06:21:44 +08:00
|
|
|
// 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());
|
2017-11-28 05:15:43 +08:00
|
|
|
Type *Ty = Call->getType();
|
|
|
|
PHINode *Phi = Builder.CreatePHI(Ty, 2);
|
2013-06-12 06:21:44 +08:00
|
|
|
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);
|
2017-11-28 05:15:43 +08:00
|
|
|
Value *FCmp = TTI->isFCmpOrdCheaperThanFCmpZero(Ty)
|
|
|
|
? Builder.CreateFCmpORD(Call, Call)
|
|
|
|
: Builder.CreateFCmpOGE(Call->getOperand(0),
|
|
|
|
ConstantFP::get(Ty, 0.0));
|
2013-06-12 06:21:44 +08:00
|
|
|
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;
|
|
|
|
|
2017-10-28 08:36:58 +08:00
|
|
|
if (Call->isNoBuiltin())
|
|
|
|
continue;
|
|
|
|
|
2016-05-20 23:43:39 +08:00
|
|
|
// 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;
|
2017-10-28 08:36:58 +08:00
|
|
|
if (CalledFunc->hasLocalLinkage() ||
|
|
|
|
!TLI->getLibFunc(*CalledFunc, LF) || !TLI->has(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()) &&
|
2017-11-28 05:15:43 +08:00
|
|
|
optimizeSQRT(Call, CalledFunc, *CurrBB, BB, TTI))
|
2016-05-20 23:43:39 +08:00
|
|
|
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 =
|
Change TargetLibraryInfo analysis passes to always require Function
Summary:
This is the first change to enable the TLI to be built per-function so
that -fno-builtin* handling can be migrated to use function attributes.
See discussion on D61634 for background. This is an enabler for fixing
handling of these options for LTO, for example.
This change should not affect behavior, as the provided function is not
yet used to build a specifically per-function TLI, but rather enables
that migration.
Most of the changes were very mechanical, e.g. passing a Function to the
legacy analysis pass's getTLI interface, or in Module level cases,
adding a callback. This is similar to the way the per-function TTI
analysis works.
There was one place where we were looking for builtins but not in the
context of a specific function. See FindCXAAtExit in
lib/Transforms/IPO/GlobalOpt.cpp. I'm somewhat concerned my workaround
could provide the wrong behavior in some corner cases. Suggestions
welcome.
Reviewers: chandlerc, hfinkel
Subscribers: arsenm, dschuff, jvesely, nhaehnle, mehdi_amini, javed.absar, sbc100, jgravelle-google, eraman, aheejin, steven_wu, george.burgess.iv, dexonsmith, jfb, asbirlea, gchatelet, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D66428
llvm-svn: 371284
2019-09-07 11:09:36 +08:00
|
|
|
&getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
|
2016-05-26 07:38:53 +08:00
|
|
|
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
|
|
|
}
|