Skip to content

Commit

Permalink
Conditionally eliminate library calls where the result value is not used
Browse files Browse the repository at this point in the history
Summary:
This pass shrink-wraps a condition to some library calls where the call
result is not used. For example:
   sqrt(val);
 is transformed to
   if (val < 0)
     sqrt(val);
Even if the result of library call is not being used, the compiler cannot
safely delete the call because the function can set errno on error
conditions.
Note in many functions, the error condition solely depends on the incoming
parameter. In this optimization, we can generate the condition can lead to
the errno to shrink-wrap the call. Since the chances of hitting the error
condition is low, the runtime call is effectively eliminated.

These partially dead calls are usually results of C++ abstraction penalty
exposed by inlining. This optimization hits 108 times in 19 C/C++ programs
in SPEC2006.

Reviewers: hfinkel, mehdi_amini, davidxl

Subscribers: modocache, mgorny, mehdi_amini, xur, llvm-commits, beanz

Differential Revision: https://reviews.llvm.org/D24414

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@284542 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
xur-llvm committed Oct 18, 2016
1 parent 6e98f1c commit fe89a6b
Show file tree
Hide file tree
Showing 13 changed files with 1,234 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/llvm/InitializePasses.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ void initializeLazyBranchProbabilityInfoPassPass(PassRegistry&);
void initializeLazyBlockFrequencyInfoPassPass(PassRegistry&);
void initializeLazyValueInfoWrapperPassPass(PassRegistry&);
void initializeLegalizerPass(PassRegistry&);
void initializeLibCallsShrinkWrapLegacyPassPass(PassRegistry&);
void initializeLintPass(PassRegistry&);
void initializeLiveDebugValuesPass(PassRegistry&);
void initializeLiveDebugVariablesPass(PassRegistry&);
Expand Down
1 change: 1 addition & 0 deletions include/llvm/LinkAllPasses.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ namespace {
(void) llvm::createCFLAndersAAWrapperPass();
(void) llvm::createCFLSteensAAWrapperPass();
(void) llvm::createStructurizeCFGPass();
(void) llvm::createLibCallsShrinkWrapPass();
(void) llvm::createConstantMergePass();
(void) llvm::createConstantPropagationPass();
(void) llvm::createCostModelAnalysisPass();
Expand Down
6 changes: 6 additions & 0 deletions include/llvm/Transforms/Scalar.h
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,12 @@ FunctionPass *createLoopDataPrefetchPass();
///===---------------------------------------------------------------------===//
ModulePass *createNameAnonGlobalPass();

//===----------------------------------------------------------------------===//
//
// LibCallsShrinkWrap - Shrink-wraps a call to function if the result is not
// used.
//
FunctionPass *createLibCallsShrinkWrapPass();
} // End llvm namespace

#endif
27 changes: 27 additions & 0 deletions include/llvm/Transforms/Utils/LibCallsShrinkWrap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//===- LibCallsShrinkWrap.h - Shrink Wrap Library Calls -------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_TRANSFORMS_UTILS_LIBCALLSSHRINKWRAP_H
#define LLVM_TRANSFORMS_UTILS_LIBCALLSSHRINKWRAP_H

#include "llvm/IR/PassManager.h"

namespace llvm {

class LibCallsShrinkWrapPass : public PassInfoMixin<LibCallsShrinkWrapPass> {
public:
static StringRef name() { return "LibCallsShrinkWrapPass"; }

PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM);
};
} // end namespace llvm

#endif // LLVM_TRANSFORMS_UTILS_LIBCALLSSHRINKWRAP_H
1 change: 1 addition & 0 deletions lib/Passes/PassBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
#include "llvm/Transforms/Utils/AddDiscriminators.h"
#include "llvm/Transforms/Utils/BreakCriticalEdges.h"
#include "llvm/Transforms/Utils/LCSSA.h"
#include "llvm/Transforms/Utils/LibCallsShrinkWrap.h"
#include "llvm/Transforms/Utils/LoopSimplify.h"
#include "llvm/Transforms/Utils/LowerInvoke.h"
#include "llvm/Transforms/Utils/Mem2Reg.h"
Expand Down
1 change: 1 addition & 0 deletions lib/Passes/PassRegistry.def
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ FUNCTION_PASS("instsimplify", InstSimplifierPass())
FUNCTION_PASS("invalidate<all>", InvalidateAllAnalysesPass())
FUNCTION_PASS("float2int", Float2IntPass())
FUNCTION_PASS("no-op-function", NoOpFunctionPass())
FUNCTION_PASS("libcalls-shrinkwrap", LibCallsShrinkWrapPass())
FUNCTION_PASS("loweratomic", LowerAtomicPass())
FUNCTION_PASS("lower-expect", LowerExpectIntrinsicPass())
FUNCTION_PASS("lower-guard-intrinsic", LowerGuardIntrinsicPass())
Expand Down
7 changes: 7 additions & 0 deletions lib/Transforms/IPO/PassManagerBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ static cl::opt<bool> EnableGVNHoist(
"enable-gvn-hoist", cl::init(true), cl::Hidden,
cl::desc("Enable the GVN hoisting pass (default = on)"));

static cl::opt<bool>
DisableLibCallsShrinkWrap("disable-libcalls-shrinkwrap", cl::init(false),
cl::Hidden,
cl::desc("Disable shrink-wrap library calls"));

PassManagerBuilder::PassManagerBuilder() {
OptLevel = 2;
SizeLevel = 0;
Expand Down Expand Up @@ -297,6 +302,8 @@ void PassManagerBuilder::addFunctionSimplificationPasses(
MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
// Combine silly seq's
addInstructionCombiningPass(MPM);
if (SizeLevel == 0 && !DisableLibCallsShrinkWrap)
MPM.add(createLibCallsShrinkWrapPass());
addExtensionsToPM(EP_Peephole, MPM);

MPM.add(createTailCallEliminationPass()); // Eliminate tail calls
Expand Down
1 change: 1 addition & 0 deletions lib/Transforms/Utils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ add_llvm_library(LLVMTransformUtils
InstructionNamer.cpp
IntegerDivision.cpp
LCSSA.cpp
LibCallsShrinkWrap.cpp
Local.cpp
LoopSimplify.cpp
LoopUnroll.cpp
Expand Down
Loading

0 comments on commit fe89a6b

Please sign in to comment.