blob: 683cf23e84079df61548820bf31f3d3bd9535cb4 [file] [log] [blame]
Reid Spencer9bbaa2a2005-04-25 03:59:26 +00001//===- SimplifyLibCalls.cpp - Optimize specific well-known library calls --===//
Reid Spencer39a762d2005-04-25 02:53:12 +00002//
3// The LLVM Compiler Infrastructure
4//
Reid Spencer9bbaa2a2005-04-25 03:59:26 +00005// This file was developed by Reid Spencer and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
Reid Spencer39a762d2005-04-25 02:53:12 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements a variety of small optimizations for calls to specific
11// well-known (e.g. runtime library) function calls. For example, a call to the
12// function "exit(3)" that occurs within the main() function can be transformed
Reid Spencer9bbaa2a2005-04-25 03:59:26 +000013// into a simple "return 3" instruction. Any optimization that takes this form
14// (replace call to library function with simpler code that provides same
15// result) belongs in this file.
Reid Spencer39a762d2005-04-25 02:53:12 +000016//
17//===----------------------------------------------------------------------===//
18
19#include "llvm/Transforms/IPO.h"
20#include "llvm/Module.h"
21#include "llvm/Pass.h"
Reid Spencerf2534c72005-04-25 21:11:48 +000022#include "llvm/DerivedTypes.h"
23#include "llvm/Constants.h"
Reid Spencer39a762d2005-04-25 02:53:12 +000024#include "llvm/Instructions.h"
25#include "llvm/ADT/Statistic.h"
Reid Spencer9bbaa2a2005-04-25 03:59:26 +000026#include "llvm/ADT/hash_map"
Reid Spencerf2534c72005-04-25 21:11:48 +000027#include <iostream>
Reid Spencer39a762d2005-04-25 02:53:12 +000028using namespace llvm;
29
30namespace {
31 Statistic<> SimplifiedLibCalls("simplified-lib-calls",
32 "Number of well-known library calls simplified");
33
34 /// This class is the base class for a set of small but important
35 /// optimizations of calls to well-known functions, such as those in the c
36 /// library. This class provides the basic infrastructure for handling
37 /// runOnModule. Subclasses register themselves and provide two methods:
38 /// RecognizeCall and OptimizeCall. Whenever this class finds a function call,
39 /// it asks the subclasses to recognize the call. If it is recognized, then
40 /// the OptimizeCall method is called on that subclass instance. In this way
41 /// the subclasses implement the calling conditions on which they trigger and
42 /// the action to perform, making it easy to add new optimizations of this
43 /// form.
44 /// @brief A ModulePass for optimizing well-known function calls
45 struct SimplifyLibCalls : public ModulePass {
46
47
48 /// For this pass, process all of the function calls in the module, calling
49 /// RecognizeCall and OptimizeCall as appropriate.
50 virtual bool runOnModule(Module &M);
51
52 };
53
54 RegisterOpt<SimplifyLibCalls>
55 X("simplify-libcalls","Simplify well-known library calls");
56
57 struct CallOptimizer
58 {
59 /// @brief Constructor that registers the optimization
Reid Spencer9bbaa2a2005-04-25 03:59:26 +000060 CallOptimizer(const char * fname );
Reid Spencer39a762d2005-04-25 02:53:12 +000061
62 virtual ~CallOptimizer();
63
Reid Spencerf2534c72005-04-25 21:11:48 +000064 /// The implementation of this function in subclasses should determine if
65 /// \p F is suitable for the optimization. This method is called by
66 /// runOnModule to short circuit visiting all the call sites of such a
67 /// function if that function is not suitable in the first place.
68 /// If the called function is suitabe, this method should return true;
69 /// false, otherwise. This function should also perform any lazy
70 /// initialization that the CallOptimizer needs to do, if its to return
71 /// true. This avoids doing initialization until the optimizer is actually
72 /// going to be called upon to do some optimization.
73 virtual bool ValidateCalledFunction(
74 const Function* F ///< The function that is the target of call sites
75 ) const = 0;
76
Reid Spencer39a762d2005-04-25 02:53:12 +000077 /// The implementations of this function in subclasses is the heart of the
78 /// SimplifyLibCalls algorithm. Sublcasses of this class implement
79 /// OptimizeCall to determine if (a) the conditions are right for optimizing
80 /// the call and (b) to perform the optimization. If an action is taken
81 /// against ci, the subclass is responsible for returning true and ensuring
82 /// that ci is erased from its parent.
83 /// @param ci the call instruction under consideration
84 /// @param f the function that ci calls.
85 /// @brief Optimize a call, if possible.
Reid Spencerf2534c72005-04-25 21:11:48 +000086 virtual bool OptimizeCall(
87 CallInst* ci ///< The call instruction that should be optimized.
88 ) const = 0;
Reid Spencer9bbaa2a2005-04-25 03:59:26 +000089
Reid Spencerf2534c72005-04-25 21:11:48 +000090 const char * getFunctionName() const { return func_name; }
Reid Spencer9bbaa2a2005-04-25 03:59:26 +000091 private:
Reid Spencerf2534c72005-04-25 21:11:48 +000092 const char* func_name;
Reid Spencer39a762d2005-04-25 02:53:12 +000093 };
94
95 /// @brief The list of optimizations deriving from CallOptimizer
Reid Spencerf2534c72005-04-25 21:11:48 +000096
Reid Spencer9bbaa2a2005-04-25 03:59:26 +000097 hash_map<std::string,CallOptimizer*> optlist;
Reid Spencer39a762d2005-04-25 02:53:12 +000098
Reid Spencer9bbaa2a2005-04-25 03:59:26 +000099 CallOptimizer::CallOptimizer(const char* fname)
100 : func_name(fname)
Reid Spencer39a762d2005-04-25 02:53:12 +0000101 {
102 // Register this call optimizer
Reid Spencer9bbaa2a2005-04-25 03:59:26 +0000103 optlist[func_name] = this;
Reid Spencer39a762d2005-04-25 02:53:12 +0000104 }
105
106 /// Make sure we get our virtual table in this file.
Reid Spencerf2534c72005-04-25 21:11:48 +0000107 CallOptimizer::~CallOptimizer()
108 {
109 optlist.clear();
110 }
Reid Spencer39a762d2005-04-25 02:53:12 +0000111}
112
113ModulePass *llvm::createSimplifyLibCallsPass()
114{
115 return new SimplifyLibCalls();
116}
117
118bool SimplifyLibCalls::runOnModule(Module &M)
119{
Reid Spencerf2534c72005-04-25 21:11:48 +0000120 bool result = false;
121
122 // The call optimizations can be recursive. That is, the optimization might
123 // generate a call to another function which can also be optimized. This way
124 // we make the CallOptimizer instances very specific to the case they handle.
125 // It also means we need to keep running over the function calls in the module
126 // until we don't get any more optimizations possible.
127 bool found_optimization = false;
128 do
Reid Spencer39a762d2005-04-25 02:53:12 +0000129 {
Reid Spencerf2534c72005-04-25 21:11:48 +0000130 found_optimization = false;
131 for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI)
Reid Spencer39a762d2005-04-25 02:53:12 +0000132 {
Reid Spencerf2534c72005-04-25 21:11:48 +0000133 // All the "well-known" functions are external and have external linkage
134 // because they live in a runtime library somewhere and were (probably)
135 // not compiled by LLVM. So, we only act on external functions that have
136 // external linkage and non-empty uses.
137 if (FI->isExternal() && FI->hasExternalLinkage() && !FI->use_empty())
Reid Spencer39a762d2005-04-25 02:53:12 +0000138 {
Reid Spencerf2534c72005-04-25 21:11:48 +0000139 // Get the optimization class that pertains to this function
140 if (CallOptimizer* CO = optlist[FI->getName().c_str()] )
Reid Spencer39a762d2005-04-25 02:53:12 +0000141 {
Reid Spencerf2534c72005-04-25 21:11:48 +0000142 // Make sure the called function is suitable for the optimization
143 if (CO->ValidateCalledFunction(FI))
Reid Spencer39a762d2005-04-25 02:53:12 +0000144 {
Reid Spencerf2534c72005-04-25 21:11:48 +0000145 // Loop over each of the uses of the function
146 for (Value::use_iterator UI = FI->use_begin(), UE = FI->use_end();
147 UI != UE ; )
Reid Spencer39a762d2005-04-25 02:53:12 +0000148 {
Reid Spencerf2534c72005-04-25 21:11:48 +0000149 // If the use of the function is a call instruction
150 if (CallInst* CI = dyn_cast<CallInst>(*UI++))
151 {
152 // Do the optimization on the CallOptimizer.
153 if (CO->OptimizeCall(CI))
154 {
155 ++SimplifiedLibCalls;
156 found_optimization = result = true;
157 }
158 }
Reid Spencer39a762d2005-04-25 02:53:12 +0000159 }
160 }
161 }
162 }
163 }
Reid Spencerf2534c72005-04-25 21:11:48 +0000164 } while (found_optimization);
165 return result;
Reid Spencer39a762d2005-04-25 02:53:12 +0000166}
167
168namespace {
169
170/// This CallOptimizer will find instances of a call to "exit" that occurs
171/// within the "main" function and change it to a simple "ret" instruction with
172/// the same value as passed to the exit function. It assumes that the
173/// instructions after the call to exit(3) can be deleted since they are
174/// unreachable anyway.
175/// @brief Replace calls to exit in main with a simple return
176struct ExitInMainOptimization : public CallOptimizer
177{
Reid Spencer9bbaa2a2005-04-25 03:59:26 +0000178 ExitInMainOptimization() : CallOptimizer("exit") {}
179 virtual ~ExitInMainOptimization() {}
Reid Spencerf2534c72005-04-25 21:11:48 +0000180
181 // Make sure the called function looks like exit (int argument, int return
182 // type, external linkage, not varargs).
183 virtual bool ValidateCalledFunction(const Function* f) const
184 {
185 if (f->getReturnType()->getTypeID() == Type::VoidTyID && !f->isVarArg())
186 if (f->arg_size() == 1)
187 if (f->arg_begin()->getType()->isInteger())
188 return true;
189 return false;
190 }
191
Reid Spencer9bbaa2a2005-04-25 03:59:26 +0000192 virtual bool OptimizeCall(CallInst* ci) const
193 {
Reid Spencerf2534c72005-04-25 21:11:48 +0000194 // To be careful, we check that the call to exit is coming from "main", that
195 // main has external linkage, and the return type of main and the argument
196 // to exit have the same type.
197 Function *from = ci->getParent()->getParent();
198 if (from->hasExternalLinkage())
199 if (from->getReturnType() == ci->getOperand(1)->getType())
200 if (from->getName() == "main")
201 {
202 // Okay, time to actually do the optimization. First, get the basic
203 // block of the call instruction
204 BasicBlock* bb = ci->getParent();
Reid Spencer39a762d2005-04-25 02:53:12 +0000205
Reid Spencerf2534c72005-04-25 21:11:48 +0000206 // Create a return instruction that we'll replace the call with.
207 // Note that the argument of the return is the argument of the call
208 // instruction.
209 ReturnInst* ri = new ReturnInst(ci->getOperand(1), ci);
Reid Spencer39a762d2005-04-25 02:53:12 +0000210
Reid Spencerf2534c72005-04-25 21:11:48 +0000211 // Split the block at the call instruction which places it in a new
212 // basic block.
213 bb->splitBasicBlock(BasicBlock::iterator(ci));
Reid Spencer39a762d2005-04-25 02:53:12 +0000214
Reid Spencerf2534c72005-04-25 21:11:48 +0000215 // The block split caused a branch instruction to be inserted into
216 // the end of the original block, right after the return instruction
217 // that we put there. That's not a valid block, so delete the branch
218 // instruction.
219 bb->back().eraseFromParent();
Reid Spencer39a762d2005-04-25 02:53:12 +0000220
Reid Spencerf2534c72005-04-25 21:11:48 +0000221 // Now we can finally get rid of the call instruction which now lives
222 // in the new basic block.
223 ci->eraseFromParent();
224
225 // Optimization succeeded, return true.
226 return true;
227 }
228 // We didn't pass the criteria for this optimization so return false
229 return false;
Reid Spencer9bbaa2a2005-04-25 03:59:26 +0000230 }
Reid Spencer39a762d2005-04-25 02:53:12 +0000231} ExitInMainOptimizer;
232
Reid Spencerf2534c72005-04-25 21:11:48 +0000233/// This CallOptimizer will simplify a call to the strcat library function. The
234/// simplification is possible only if the string being concatenated is a
235/// constant array or a constant expression that results in a constant array. In
236/// this case, if the array is small, we can generate a series of inline store
237/// instructions to effect the concatenation without calling strcat.
238/// @brief Simplify the strcat library function.
Reid Spencer9bbaa2a2005-04-25 03:59:26 +0000239struct StrCatOptimization : public CallOptimizer
240{
241 StrCatOptimization() : CallOptimizer("strcat") {}
242 virtual ~StrCatOptimization() {}
Reid Spencerf2534c72005-04-25 21:11:48 +0000243
244 /// @brief Make sure that the "strcat" function has the right prototype
245 virtual bool ValidateCalledFunction(const Function* f) const
246 {
247 if (f->getReturnType() == PointerType::get(Type::SByteTy))
248 if (f->arg_size() == 2)
249 {
250 Function::const_arg_iterator AI = f->arg_begin();
251 if (AI++->getType() == PointerType::get(Type::SByteTy))
252 if (AI->getType() == PointerType::get(Type::SByteTy))
253 return true;
254 }
255 return false;
256 }
257
258 /// Perform the optimization if the length of the string concatenated
259 /// is reasonably short and it is a constant array.
Reid Spencer9bbaa2a2005-04-25 03:59:26 +0000260 virtual bool OptimizeCall(CallInst* ci) const
261 {
Reid Spencerf2534c72005-04-25 21:11:48 +0000262 // If the thing being appended is not a GEP instruction
263 GetElementPtrInst* GEP = dyn_cast<GetElementPtrInst>(ci->getOperand(2));
264 if (!GEP)
265 return false;
266
267 // Double check that we're dealing with a pointer to sbyte here
268 if (GEP->getType() != PointerType::get(Type::SByteTy))
269 return false;
270
271 // We can only optimize if the appended string is a constant
272 Constant* C = dyn_cast<Constant>(GEP->getPointerOperand());
273 if (!C)
274 return false;
275
276 // Check the various kinds of constants that are applicable
277 GlobalVariable* GV = dyn_cast<GlobalVariable>(C);
278 if (!GV)
279 return false;
280
281 // Only GVars that have initializers will do
282 if (GV->hasInitializer())
283 {
284 Constant* INTLZR = GV->getInitializer();
285 // And only if that initializer is ConstantArray
286 if (ConstantArray* A = dyn_cast<ConstantArray>(INTLZR))
287 {
288 assert(A->isString() && "This ought to be a string");
289
290 // Get the value of the string and determine its length. If the length
291 // is zero, we can just substitute the destination pointer for the
292 // call.
293 std::string str = A->getAsString().c_str();
294 if (str.length() == 0)
295 {
296 ci->replaceAllUsesWith(ci->getOperand(1));
297 ci->eraseFromParent();
298 return true;
299 }
300
301 // Otherwise, lets just turn this into a memcpy call which will be
302 // optimized out on the next pass.
303 else
304 {
305 // Extract some information
306 Module* M = ci->getParent()->getParent()->getParent();
307 // We need to find the end of the string of the first operand to the
308 // strcat call instruction. That's where the memory is to be moved
309 // to. So, generate code that does that
310 std::vector<const Type*> args;
311 args.push_back(PointerType::get(Type::SByteTy));
312 FunctionType* strlen_type =
313 FunctionType::get(Type::IntTy, args, false);
314 Function* strlen = M->getOrInsertFunction("strlen",strlen_type);
315 CallInst* strlen_inst =
316 new CallInst(strlen,ci->getOperand(1),"",ci);
317
318 // Now that we have the string length, we must add it to the pointer
319 // to get the memcpy destination.
320 std::vector<Value*> idx;
321 idx.push_back(strlen_inst);
322 GetElementPtrInst* gep =
323 new GetElementPtrInst(ci->getOperand(1),idx,"",ci);
324
325 // Generate the memcpy call
326 args.clear();
327 args.push_back(PointerType::get(Type::SByteTy));
328 args.push_back(PointerType::get(Type::SByteTy));
329 args.push_back(Type::IntTy);
330 FunctionType* memcpy_type = FunctionType::get(
331 PointerType::get(Type::SByteTy), args, false);
332 Function* memcpy = M->getOrInsertFunction("memcpy",memcpy_type);
333 std::vector<Value*> vals;
334 vals.push_back(gep);
335 vals.push_back(ci->getOperand(2));
336 vals.push_back(ConstantSInt::get(Type::IntTy,str.length()+1));
337 CallInst* memcpy_inst = new CallInst(memcpy, vals, "", ci);
338
339 // Finally, cast the result of the memcpy to the correct type which is
340 // the result of the strcat.
341 CastInst* cast_inst =
342 new CastInst(memcpy_inst, PointerType::get(Type::SByteTy),
343 ci->getName(),ci);
344
345 // And perform the stubstitution for the strcat call.
346 ci->replaceAllUsesWith(cast_inst);
347 ci->eraseFromParent();
348 return true;
349 }
350 }
351 else if (ConstantAggregateZero* CAZ =
352 dyn_cast<ConstantAggregateZero>(INTLZR))
353 {
354 // We know this is the zero length string case so we can just avoid
355 // the strcat altogether.
356 ci->replaceAllUsesWith(ci->getOperand(1));
357 ci->eraseFromParent();
358 return true;
359 }
360 else if (ConstantExpr* E = dyn_cast<ConstantExpr>(INTLZR))
361 {
362 return false;
363 }
364 }
365
366 // We didn't pass the criteria for this optimization so return false.
Reid Spencer9bbaa2a2005-04-25 03:59:26 +0000367 return false;
368 }
369} StrCatOptimizer;
370
Reid Spencerf2534c72005-04-25 21:11:48 +0000371/// This CallOptimizer will simplify a call to the memcpy library function by
372/// expanding it out to a small set of stores if the copy source is a constant
373/// array.
374/// @brief Simplify the memcpy library function.
375struct MemCpyOptimization : public CallOptimizer
376{
377 MemCpyOptimization() : CallOptimizer("memcpy") {}
378 virtual ~MemCpyOptimization() {}
379
380 /// @brief Make sure that the "memcpy" function has the right prototype
381 virtual bool ValidateCalledFunction(const Function* f) const
382 {
383 if (f->getReturnType() == PointerType::get(Type::SByteTy))
384 if (f->arg_size() == 2)
385 {
386 Function::const_arg_iterator AI = f->arg_begin();
387 if (AI++->getType() == PointerType::get(Type::SByteTy))
388 if (AI->getType() == PointerType::get(Type::SByteTy))
389 return true;
390 }
391 return false;
392 }
393
394 /// Perform the optimization if the length of the string concatenated
395 /// is reasonably short and it is a constant array.
396 virtual bool OptimizeCall(CallInst* ci) const
397 {
398 // We didn't pass the criteria for this optimization so return false.
399 return false;
400 }
401} MemCpyOptimizer;
Reid Spencer39a762d2005-04-25 02:53:12 +0000402}