llvm.org GIT mirror llvm / be87bce
Remove the simplify-libcalls pass (finally) This commit completely removes what is left of the simplify-libcalls pass. All of the functionality has now been migrated to the instcombine and functionattrs passes. The following C API functions are now NOPs: 1. LLVMAddSimplifyLibCallsPass 2. LLVMPassManagerBuilderSetDisableSimplifyLibCalls git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@184459 91177308-0d34-0410-b5e6-96231b3b80d8 Meador Inge 6 years ago
10 changed file(s) with 52 addition(s) and 266 deletion(s). Raw diff Collapse all Expand all
239239 void initializeScalarEvolutionPass(PassRegistry&);
240240 void initializeSimpleInlinerPass(PassRegistry&);
241241 void initializeRegisterCoalescerPass(PassRegistry&);
242 void initializeSimplifyLibCallsPass(PassRegistry&);
243242 void initializeSingleLoopExtractorPass(PassRegistry&);
244243 void initializeSinkingPass(PassRegistry&);
245244 void initializeSlotIndexesPass(PassRegistry&);
129129 (void) llvm::createRegionViewerPass();
130130 (void) llvm::createSCCPPass();
131131 (void) llvm::createScalarReplAggregatesPass();
132 (void) llvm::createSimplifyLibCallsPass();
133132 (void) llvm::createSingleLoopExtractorPass();
134133 (void) llvm::createStripSymbolsPass();
135134 (void) llvm::createStripNonDebugSymbolsPass();
9999 /// added to the per-module passes.
100100 Pass *Inliner;
101101
102 bool DisableSimplifyLibCalls;
103102 bool DisableUnitAtATime;
104103 bool DisableUnrollLoops;
105104 bool BBVectorize;
302302
303303 //===----------------------------------------------------------------------===//
304304 //
305 /// createSimplifyLibCallsPass - This pass optimizes specific calls to
306 /// specific well-known (library) functions.
307 FunctionPass *createSimplifyLibCallsPass();
308
309 //===----------------------------------------------------------------------===//
310 //
311305 // CodeGenPrepare - This pass prepares a function for instruction selection.
312306 //
313307 FunctionPass *createCodeGenPreparePass(const TargetMachine *TM = 0);
5353 SizeLevel = 0;
5454 LibraryInfo = 0;
5555 Inliner = 0;
56 DisableSimplifyLibCalls = false;
5756 DisableUnitAtATime = false;
5857 DisableUnrollLoops = false;
5958 BBVectorize = RunBBVectorization;
173172 else
174173 MPM.add(createScalarReplAggregatesPass(-1, false));
175174 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
176 if (!DisableSimplifyLibCalls)
177 MPM.add(createSimplifyLibCallsPass()); // Library Call Optimizations
178175 MPM.add(createJumpThreadingPass()); // Thread jumps.
179176 MPM.add(createCorrelatedValuePropagationPass()); // Propagate conditionals
180177 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
378375 void
379376 LLVMPassManagerBuilderSetDisableSimplifyLibCalls(LLVMPassManagerBuilderRef PMB,
380377 LLVMBool Value) {
381 PassManagerBuilder *Builder = unwrap(PMB);
382 Builder->DisableSimplifyLibCalls = Value;
378 // NOTE: The simplify-libcalls pass has been removed.
383379 }
384380
385381 void
2727 Scalar.cpp
2828 ScalarReplAggregates.cpp
2929 SimplifyCFGPass.cpp
30 SimplifyLibCalls.cpp
3130 Sink.cpp
3231 StructurizeCFG.cpp
3332 TailRecursionElimination.cpp
5858 initializeSROA_SSAUpPass(Registry);
5959 initializeCFGSimplifyPassPass(Registry);
6060 initializeStructurizeCFGPass(Registry);
61 initializeSimplifyLibCallsPass(Registry);
6261 initializeSinkingPass(Registry);
6362 initializeTailCallElimPass(Registry);
6463 }
149148 }
150149
151150 void LLVMAddSimplifyLibCallsPass(LLVMPassManagerRef PM) {
152 unwrap(PM)->add(createSimplifyLibCallsPass());
151 // NOTE: The simplify-libcalls pass has been removed.
153152 }
154153
155154 void LLVMAddTailCallEliminationPass(LLVMPassManagerRef PM) {
+0
-247
lib/Transforms/Scalar/SimplifyLibCalls.cpp less more
None //===- SimplifyLibCalls.cpp - Optimize specific well-known library calls --===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements a simple pass that applies a variety of small
10 // optimizations for calls to specific well-known function calls (e.g. runtime
11 // library functions). Any optimization that takes the very simple form
12 // "replace call to library function with simpler code that provides the same
13 // result" belongs in this file.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #define DEBUG_TYPE "simplify-libcalls"
18 #include "llvm/Transforms/Scalar.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/ADT/SmallPtrSet.h"
21 #include "llvm/ADT/StringMap.h"
22 #include "llvm/Analysis/ValueTracking.h"
23 #include "llvm/Config/config.h" // FIXME: Shouldn't depend on host!
24 #include "llvm/IR/DataLayout.h"
25 #include "llvm/IR/IRBuilder.h"
26 #include "llvm/IR/LLVMContext.h"
27 #include "llvm/IR/Module.h"
28 #include "llvm/Pass.h"
29 #include "llvm/Support/CommandLine.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/Support/raw_ostream.h"
32 #include "llvm/Target/TargetLibraryInfo.h"
33 #include "llvm/Transforms/Utils/BuildLibCalls.h"
34 using namespace llvm;
35
36
37 //===----------------------------------------------------------------------===//
38 // Optimizer Base Class
39 //===----------------------------------------------------------------------===//
40
41 /// This class is the abstract base class for the set of optimizations that
42 /// corresponds to one library call.
43 namespace {
44 class LibCallOptimization {
45 protected:
46 Function *Caller;
47 const DataLayout *TD;
48 const TargetLibraryInfo *TLI;
49 LLVMContext* Context;
50 public:
51 LibCallOptimization() { }
52 virtual ~LibCallOptimization() {}
53
54 /// CallOptimizer - This pure virtual method is implemented by base classes to
55 /// do various optimizations. If this returns null then no transformation was
56 /// performed. If it returns CI, then it transformed the call and CI is to be
57 /// deleted. If it returns something else, replace CI with the new value and
58 /// delete CI.
59 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B)
60 =0;
61
62 Value *OptimizeCall(CallInst *CI, const DataLayout *TD,
63 const TargetLibraryInfo *TLI, IRBuilder<> &B) {
64 Caller = CI->getParent()->getParent();
65 this->TD = TD;
66 this->TLI = TLI;
67 if (CI->getCalledFunction())
68 Context = &CI->getCalledFunction()->getContext();
69
70 // We never change the calling convention.
71 if (CI->getCallingConv() != llvm::CallingConv::C)
72 return NULL;
73
74 return CallOptimizer(CI->getCalledFunction(), CI, B);
75 }
76 };
77 } // End anonymous namespace.
78
79
80 //===----------------------------------------------------------------------===//
81 // SimplifyLibCalls Pass Implementation
82 //===----------------------------------------------------------------------===//
83
84 namespace {
85 /// This pass optimizes well known library functions from libc and libm.
86 ///
87 class SimplifyLibCalls : public FunctionPass {
88 TargetLibraryInfo *TLI;
89
90 StringMap Optimizations;
91 public:
92 static char ID; // Pass identification
93 SimplifyLibCalls() : FunctionPass(ID) {
94 initializeSimplifyLibCallsPass(*PassRegistry::getPassRegistry());
95 }
96 void AddOpt(LibFunc::Func F, LibCallOptimization* Opt);
97 void AddOpt(LibFunc::Func F1, LibFunc::Func F2, LibCallOptimization* Opt);
98
99 void InitOptimizations();
100 bool runOnFunction(Function &F);
101
102 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
103 AU.addRequired();
104 }
105 };
106 } // end anonymous namespace.
107
108 char SimplifyLibCalls::ID = 0;
109
110 INITIALIZE_PASS_BEGIN(SimplifyLibCalls, "simplify-libcalls",
111 "Simplify well-known library calls", false, false)
112 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo)
113 INITIALIZE_PASS_END(SimplifyLibCalls, "simplify-libcalls",
114 "Simplify well-known library calls", false, false)
115
116 // Public interface to the Simplify LibCalls pass.
117 FunctionPass *llvm::createSimplifyLibCallsPass() {
118 return new SimplifyLibCalls();
119 }
120
121 void SimplifyLibCalls::AddOpt(LibFunc::Func F, LibCallOptimization* Opt) {
122 if (TLI->has(F))
123 Optimizations[TLI->getName(F)] = Opt;
124 }
125
126 void SimplifyLibCalls::AddOpt(LibFunc::Func F1, LibFunc::Func F2,
127 LibCallOptimization* Opt) {
128 if (TLI->has(F1) && TLI->has(F2))
129 Optimizations[TLI->getName(F1)] = Opt;
130 }
131
132 /// Optimizations - Populate the Optimizations map with all the optimizations
133 /// we know.
134 void SimplifyLibCalls::InitOptimizations() {
135 }
136
137
138 /// runOnFunction - Top level algorithm.
139 ///
140 bool SimplifyLibCalls::runOnFunction(Function &F) {
141 TLI = &getAnalysis();
142
143 if (Optimizations.empty())
144 InitOptimizations();
145
146 const DataLayout *TD = getAnalysisIfAvailable();
147
148 IRBuilder<> Builder(F.getContext());
149
150 bool Changed = false;
151 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
152 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
153 // Ignore non-calls.
154 CallInst *CI = dyn_cast(I++);
155 if (!CI || CI->hasFnAttr(Attribute::NoBuiltin)) continue;
156
157 // Ignore indirect calls and calls to non-external functions.
158 Function *Callee = CI->getCalledFunction();
159 if (Callee == 0 || !Callee->isDeclaration() ||
160 !(Callee->hasExternalLinkage() || Callee->hasDLLImportLinkage()))
161 continue;
162
163 // Ignore unknown calls.
164 LibCallOptimization *LCO = Optimizations.lookup(Callee->getName());
165 if (!LCO) continue;
166
167 // Set the builder to the instruction after the call.
168 Builder.SetInsertPoint(BB, I);
169
170 // Use debug location of CI for all new instructions.
171 Builder.SetCurrentDebugLocation(CI->getDebugLoc());
172
173 // Try to optimize this call.
174 Value *Result = LCO->OptimizeCall(CI, TD, TLI, Builder);
175 if (Result == 0) continue;
176
177 DEBUG(dbgs() << "SimplifyLibCalls simplified: " << *CI;
178 dbgs() << " into: " << *Result << "\n");
179
180 // Something changed!
181 Changed = true;
182
183 // Inspect the instruction after the call (which was potentially just
184 // added) next.
185 I = CI; ++I;
186
187 if (CI != Result && !CI->use_empty()) {
188 CI->replaceAllUsesWith(Result);
189 if (!Result->hasName())
190 Result->takeName(CI);
191 }
192 CI->eraseFromParent();
193 }
194 }
195 return Changed;
196 }
197
198 // TODO:
199 // Additional cases that we need to add to this file:
200 //
201 // cbrt:
202 // * cbrt(expN(X)) -> expN(x/3)
203 // * cbrt(sqrt(x)) -> pow(x,1/6)
204 // * cbrt(sqrt(x)) -> pow(x,1/9)
205 //
206 // exp, expf, expl:
207 // * exp(log(x)) -> x
208 //
209 // log, logf, logl:
210 // * log(exp(x)) -> x
211 // * log(x**y) -> y*log(x)
212 // * log(exp(y)) -> y*log(e)
213 // * log(exp2(y)) -> y*log(2)
214 // * log(exp10(y)) -> y*log(10)
215 // * log(sqrt(x)) -> 0.5*log(x)
216 // * log(pow(x,y)) -> y*log(x)
217 //
218 // lround, lroundf, lroundl:
219 // * lround(cnst) -> cnst'
220 //
221 // pow, powf, powl:
222 // * pow(exp(x),y) -> exp(x*y)
223 // * pow(sqrt(x),y) -> pow(x,y*0.5)
224 // * pow(pow(x,y),z)-> pow(x,y*z)
225 //
226 // round, roundf, roundl:
227 // * round(cnst) -> cnst'
228 //
229 // signbit:
230 // * signbit(cnst) -> cnst'
231 // * signbit(nncst) -> 0 (if pstv is a non-negative constant)
232 //
233 // sqrt, sqrtf, sqrtl:
234 // * sqrt(expN(x)) -> expN(x*0.5)
235 // * sqrt(Nroot(x)) -> pow(x,1/(2*N))
236 // * sqrt(pow(x,y)) -> pow(|x|,y*0.5)
237 //
238 // strchr:
239 // * strchr(p, 0) -> strlen(p)
240 // tan, tanf, tanl:
241 // * tan(atan(x)) -> x
242 //
243 // trunc, truncf, truncl:
244 // * trunc(cnst) -> cnst'
245 //
246 //
19491949 }
19501950
19511951 }
1952
1953 // TODO:
1954 // Additional cases that we need to add to this file:
1955 //
1956 // cbrt:
1957 // * cbrt(expN(X)) -> expN(x/3)
1958 // * cbrt(sqrt(x)) -> pow(x,1/6)
1959 // * cbrt(sqrt(x)) -> pow(x,1/9)
1960 //
1961 // exp, expf, expl:
1962 // * exp(log(x)) -> x
1963 //
1964 // log, logf, logl:
1965 // * log(exp(x)) -> x
1966 // * log(x**y) -> y*log(x)
1967 // * log(exp(y)) -> y*log(e)
1968 // * log(exp2(y)) -> y*log(2)
1969 // * log(exp10(y)) -> y*log(10)
1970 // * log(sqrt(x)) -> 0.5*log(x)
1971 // * log(pow(x,y)) -> y*log(x)
1972 //
1973 // lround, lroundf, lroundl:
1974 // * lround(cnst) -> cnst'
1975 //
1976 // pow, powf, powl:
1977 // * pow(exp(x),y) -> exp(x*y)
1978 // * pow(sqrt(x),y) -> pow(x,y*0.5)
1979 // * pow(pow(x,y),z)-> pow(x,y*z)
1980 //
1981 // round, roundf, roundl:
1982 // * round(cnst) -> cnst'
1983 //
1984 // signbit:
1985 // * signbit(cnst) -> cnst'
1986 // * signbit(nncst) -> 0 (if pstv is a non-negative constant)
1987 //
1988 // sqrt, sqrtf, sqrtl:
1989 // * sqrt(expN(x)) -> expN(x*0.5)
1990 // * sqrt(Nroot(x)) -> pow(x,1/(2*N))
1991 // * sqrt(pow(x,y)) -> pow(|x|,y*0.5)
1992 //
1993 // strchr:
1994 // * strchr(p, 0) -> strlen(p)
1995 // tan, tanf, tanl:
1996 // * tan(atan(x)) -> x
1997 //
1998 // trunc, truncf, truncl:
1999 // * trunc(cnst) -> cnst'
2000 //
2001 //
444444 }
445445 Builder.DisableUnitAtATime = !UnitAtATime;
446446 Builder.DisableUnrollLoops = OptLevel == 0;
447 Builder.DisableSimplifyLibCalls = DisableSimplifyLibCalls;
448447
449448 Builder.populateFunctionPassManager(FPM);
450449 Builder.populateModulePassManager(MPM);
464463 if (!DisableInline)
465464 Builder.Inliner = createFunctionInliningPass();
466465 Builder.OptLevel = 3;
467 Builder.DisableSimplifyLibCalls = DisableSimplifyLibCalls;
468466 Builder.populateModulePassManager(PM);
469467 }
470468