blob: 9f1ff8d4358f6930c75bed9b87a6f71b9d1e49fe [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
Reid Spencer8ee5aac2005-04-26 03:26:15 +000075 ) = 0;
Reid Spencerf2534c72005-04-25 21:11:48 +000076
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.
Reid Spencer8ee5aac2005-04-26 03:26:15 +000088 ) = 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 Spencerfe91dfe2005-04-25 21:20:38 +0000107 CallOptimizer::~CallOptimizer() { }
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000108
109 /// Provide some functions for accessing standard library prototypes and
110 /// caching them so we don't have to keep recomputing them
111 FunctionType* get_strlen()
112 {
113 static FunctionType* strlen_type = 0;
114 if (!strlen_type)
115 {
116 std::vector<const Type*> args;
117 args.push_back(PointerType::get(Type::SByteTy));
118 strlen_type = FunctionType::get(Type::IntTy, args, false);
119 }
120 return strlen_type;
121 }
122
123 FunctionType* get_memcpy()
124 {
125 static FunctionType* memcpy_type = 0;
126 if (!memcpy_type)
127 {
128 // Note: this is for llvm.memcpy intrinsic
129 std::vector<const Type*> args;
130 args.push_back(PointerType::get(Type::SByteTy));
131 args.push_back(PointerType::get(Type::SByteTy));
132 args.push_back(Type::IntTy);
133 args.push_back(Type::IntTy);
134 memcpy_type = FunctionType::get(
135 PointerType::get(Type::SByteTy), args, false);
136 }
137 return memcpy_type;
138 }
Reid Spencer39a762d2005-04-25 02:53:12 +0000139}
140
141ModulePass *llvm::createSimplifyLibCallsPass()
142{
143 return new SimplifyLibCalls();
144}
145
146bool SimplifyLibCalls::runOnModule(Module &M)
147{
Reid Spencerf2534c72005-04-25 21:11:48 +0000148 bool result = false;
149
150 // The call optimizations can be recursive. That is, the optimization might
151 // generate a call to another function which can also be optimized. This way
152 // we make the CallOptimizer instances very specific to the case they handle.
153 // It also means we need to keep running over the function calls in the module
154 // until we don't get any more optimizations possible.
155 bool found_optimization = false;
156 do
Reid Spencer39a762d2005-04-25 02:53:12 +0000157 {
Reid Spencerf2534c72005-04-25 21:11:48 +0000158 found_optimization = false;
159 for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI)
Reid Spencer39a762d2005-04-25 02:53:12 +0000160 {
Reid Spencerf2534c72005-04-25 21:11:48 +0000161 // All the "well-known" functions are external and have external linkage
162 // because they live in a runtime library somewhere and were (probably)
163 // not compiled by LLVM. So, we only act on external functions that have
164 // external linkage and non-empty uses.
165 if (FI->isExternal() && FI->hasExternalLinkage() && !FI->use_empty())
Reid Spencer39a762d2005-04-25 02:53:12 +0000166 {
Reid Spencerf2534c72005-04-25 21:11:48 +0000167 // Get the optimization class that pertains to this function
168 if (CallOptimizer* CO = optlist[FI->getName().c_str()] )
Reid Spencer39a762d2005-04-25 02:53:12 +0000169 {
Reid Spencerf2534c72005-04-25 21:11:48 +0000170 // Make sure the called function is suitable for the optimization
171 if (CO->ValidateCalledFunction(FI))
Reid Spencer39a762d2005-04-25 02:53:12 +0000172 {
Reid Spencerf2534c72005-04-25 21:11:48 +0000173 // Loop over each of the uses of the function
174 for (Value::use_iterator UI = FI->use_begin(), UE = FI->use_end();
175 UI != UE ; )
Reid Spencer39a762d2005-04-25 02:53:12 +0000176 {
Reid Spencerf2534c72005-04-25 21:11:48 +0000177 // If the use of the function is a call instruction
178 if (CallInst* CI = dyn_cast<CallInst>(*UI++))
179 {
180 // Do the optimization on the CallOptimizer.
181 if (CO->OptimizeCall(CI))
182 {
183 ++SimplifiedLibCalls;
184 found_optimization = result = true;
185 }
186 }
Reid Spencer39a762d2005-04-25 02:53:12 +0000187 }
188 }
189 }
190 }
191 }
Reid Spencerf2534c72005-04-25 21:11:48 +0000192 } while (found_optimization);
193 return result;
Reid Spencer39a762d2005-04-25 02:53:12 +0000194}
195
196namespace {
197
198/// This CallOptimizer will find instances of a call to "exit" that occurs
199/// within the "main" function and change it to a simple "ret" instruction with
200/// the same value as passed to the exit function. It assumes that the
201/// instructions after the call to exit(3) can be deleted since they are
202/// unreachable anyway.
203/// @brief Replace calls to exit in main with a simple return
204struct ExitInMainOptimization : public CallOptimizer
205{
Reid Spencer9bbaa2a2005-04-25 03:59:26 +0000206 ExitInMainOptimization() : CallOptimizer("exit") {}
207 virtual ~ExitInMainOptimization() {}
Reid Spencerf2534c72005-04-25 21:11:48 +0000208
209 // Make sure the called function looks like exit (int argument, int return
210 // type, external linkage, not varargs).
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000211 virtual bool ValidateCalledFunction(const Function* f)
Reid Spencerf2534c72005-04-25 21:11:48 +0000212 {
213 if (f->getReturnType()->getTypeID() == Type::VoidTyID && !f->isVarArg())
214 if (f->arg_size() == 1)
215 if (f->arg_begin()->getType()->isInteger())
216 return true;
217 return false;
218 }
219
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000220 virtual bool OptimizeCall(CallInst* ci)
Reid Spencer9bbaa2a2005-04-25 03:59:26 +0000221 {
Reid Spencerf2534c72005-04-25 21:11:48 +0000222 // To be careful, we check that the call to exit is coming from "main", that
223 // main has external linkage, and the return type of main and the argument
224 // to exit have the same type.
225 Function *from = ci->getParent()->getParent();
226 if (from->hasExternalLinkage())
227 if (from->getReturnType() == ci->getOperand(1)->getType())
228 if (from->getName() == "main")
229 {
230 // Okay, time to actually do the optimization. First, get the basic
231 // block of the call instruction
232 BasicBlock* bb = ci->getParent();
Reid Spencer39a762d2005-04-25 02:53:12 +0000233
Reid Spencerf2534c72005-04-25 21:11:48 +0000234 // Create a return instruction that we'll replace the call with.
235 // Note that the argument of the return is the argument of the call
236 // instruction.
237 ReturnInst* ri = new ReturnInst(ci->getOperand(1), ci);
Reid Spencer39a762d2005-04-25 02:53:12 +0000238
Reid Spencerf2534c72005-04-25 21:11:48 +0000239 // Split the block at the call instruction which places it in a new
240 // basic block.
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000241 bb->splitBasicBlock(ci);
Reid Spencer39a762d2005-04-25 02:53:12 +0000242
Reid Spencerf2534c72005-04-25 21:11:48 +0000243 // The block split caused a branch instruction to be inserted into
244 // the end of the original block, right after the return instruction
245 // that we put there. That's not a valid block, so delete the branch
246 // instruction.
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000247 bb->getInstList().pop_back();
Reid Spencer39a762d2005-04-25 02:53:12 +0000248
Reid Spencerf2534c72005-04-25 21:11:48 +0000249 // Now we can finally get rid of the call instruction which now lives
250 // in the new basic block.
251 ci->eraseFromParent();
252
253 // Optimization succeeded, return true.
254 return true;
255 }
256 // We didn't pass the criteria for this optimization so return false
257 return false;
Reid Spencer9bbaa2a2005-04-25 03:59:26 +0000258 }
Reid Spencer39a762d2005-04-25 02:53:12 +0000259} ExitInMainOptimizer;
260
Reid Spencerf2534c72005-04-25 21:11:48 +0000261/// This CallOptimizer will simplify a call to the strcat library function. The
262/// simplification is possible only if the string being concatenated is a
263/// constant array or a constant expression that results in a constant array. In
264/// this case, if the array is small, we can generate a series of inline store
265/// instructions to effect the concatenation without calling strcat.
266/// @brief Simplify the strcat library function.
Reid Spencer9bbaa2a2005-04-25 03:59:26 +0000267struct StrCatOptimization : public CallOptimizer
268{
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000269private:
270 Function* strlen_func;
271 Function* memcpy_func;
272public:
273 StrCatOptimization()
274 : CallOptimizer("strcat")
275 , strlen_func(0)
276 , memcpy_func(0)
277 {}
Reid Spencer9bbaa2a2005-04-25 03:59:26 +0000278 virtual ~StrCatOptimization() {}
Reid Spencerf2534c72005-04-25 21:11:48 +0000279
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000280 inline Function* get_strlen_func(Module*M)
281 {
282 if (strlen_func)
283 return strlen_func;
284 return strlen_func = M->getOrInsertFunction("strlen",get_strlen());
285 }
286
287 inline Function* get_memcpy_func(Module* M)
288 {
289 if (memcpy_func)
290 return memcpy_func;
291 return memcpy_func = M->getOrInsertFunction("llvm.memcpy",get_memcpy());
292 }
293
Reid Spencerf2534c72005-04-25 21:11:48 +0000294 /// @brief Make sure that the "strcat" function has the right prototype
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000295 virtual bool ValidateCalledFunction(const Function* f)
Reid Spencerf2534c72005-04-25 21:11:48 +0000296 {
297 if (f->getReturnType() == PointerType::get(Type::SByteTy))
298 if (f->arg_size() == 2)
299 {
300 Function::const_arg_iterator AI = f->arg_begin();
301 if (AI++->getType() == PointerType::get(Type::SByteTy))
302 if (AI->getType() == PointerType::get(Type::SByteTy))
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000303 {
304 // Invalidate the pre-computed strlen_func and memcpy_func Functions
305 // because, by definition, this method is only called when a new
306 // Module is being traversed. Invalidation causes re-computation for
307 // the new Module (if necessary).
308 strlen_func = 0;
309 memcpy_func = 0;
310
311 // Indicate this is a suitable call type.
Reid Spencerf2534c72005-04-25 21:11:48 +0000312 return true;
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000313 }
Reid Spencerf2534c72005-04-25 21:11:48 +0000314 }
315 return false;
316 }
317
318 /// Perform the optimization if the length of the string concatenated
319 /// is reasonably short and it is a constant array.
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000320 virtual bool OptimizeCall(CallInst* ci)
Reid Spencer9bbaa2a2005-04-25 03:59:26 +0000321 {
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000322 User* GEP = 0;
323 // If the thing being appended is not a GEP instruction nor a constant
324 // expression with a GEP instruction, then return false because this is
325 // not a situation we can optimize.
326 if (GetElementPtrInst* GEPI =
327 dyn_cast<GetElementPtrInst>(ci->getOperand(2)))
328 GEP = GEPI;
329 else if (ConstantExpr* CE = dyn_cast<ConstantExpr>(ci->getOperand(2)))
330 if (CE->getOpcode() == Instruction::GetElementPtr)
331 GEP = CE;
332 else
Reid Spencerf2534c72005-04-25 21:11:48 +0000333 return false;
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000334 else
335 return false;
336
337 // Check to make sure that the first operand of the GEP is an integer and
338 // has value 0 so that we are sure we're indexing into the initializer.
339 if (ConstantInt* op1 = dyn_cast<ConstantInt>(GEP->getOperand(1)))
340 if (op1->isNullValue())
341 ;
342 else
343 return false;
344 else
345 return false;
346
347 // Ensure that the second operand is a constant int. If it isn't then this
348 // GEP is wonky and we're not really sure what were referencing into and
349 // better of not optimizing it.
350 if (!dyn_cast<ConstantInt>(GEP->getOperand(2)))
351 return false;
352
353 // The GEP instruction, constant or instruction, must reference a global
354 // variable that is a constant and is initialized. The referenced constant
355 // initializer is the array that we'll use for optimization.
356 GlobalVariable* GV = dyn_cast<GlobalVariable>(GEP->getOperand(0));
357 if (!GV || !GV->isConstant() || !GV->hasInitializer())
358 return false;
359
360 // Get the initializer
361 Constant* INTLZR = GV->getInitializer();
362
363 // Handle the ConstantArray case.
364 if (ConstantArray* A = dyn_cast<ConstantArray>(INTLZR))
365 {
366 // First off, we can't do this if the constant array isn't a string,
367 // meaning its base type is sbyte and its constant initializers for all
368 // the elements are constantInt or constantInt expressions.
369 if (!A->isString())
370 return false;
371
372 // Now we need to examine the source string to find its actual length. We
373 // can't rely on the size of the constant array becasue there could be a
374 // null terminator in the middle of the array. We also have to bail out if
375 // we find a non-integer constant initializer of one of the elements.
376 // Also, if we never find a terminator before the end of the array.
377 unsigned max_elems = A->getType()->getNumElements();
378 unsigned len = 0;
379 for (; len < max_elems; len++)
380 {
381 if (ConstantInt* CI = dyn_cast<ConstantInt>(A->getOperand(len)))
382 {
383 if (CI->isNullValue())
384 break; // we found end of string
385 }
386 else
387 return false; // This array isn't suitable, non-int initializer
Reid Spencerf2534c72005-04-25 21:11:48 +0000388 }
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000389 if (len >= max_elems)
390 return false; // This array isn't null terminated
391 else
392 len++; // increment for null terminator
393
394 // Extract some information from the instruction
395 Module* M = ci->getParent()->getParent()->getParent();
396
397 // We need to find the end of the destination string. That's where the
398 // memory is to be moved to. We just generate a call to strlen (further
399 // optimized in another pass). Note that the get_strlen_func() call
400 // caches the Function* for us.
401 CallInst* strlen_inst =
402 new CallInst(get_strlen_func(M),ci->getOperand(1),"",ci);
403
404 // Now that we have the destination's length, we must index into the
405 // destination's pointer to get the actual memcpy destination (end of
406 // the string .. we're concatenating).
407 std::vector<Value*> idx;
408 idx.push_back(strlen_inst);
409 GetElementPtrInst* gep =
410 new GetElementPtrInst(ci->getOperand(1),idx,"",ci);
411
412 // We have enough information to now generate the memcpy call to
413 // do the concatenation for us.
414 std::vector<Value*> vals;
415 vals.push_back(gep); // destination
416 vals.push_back(ci->getOperand(2)); // source
417 vals.push_back(ConstantSInt::get(Type::IntTy,len)); // length
418 vals.push_back(ConstantSInt::get(Type::IntTy,1)); // alignment
419 CallInst* memcpy_inst =
420 new CallInst(get_memcpy_func(M), vals, "", ci);
421
422 // Finally, substitute the first operand of the strcat call for the
423 // strcat call itself since strcat returns its first operand; and,
424 // kill the strcat CallInst.
425 ci->replaceAllUsesWith(ci->getOperand(1));
426 ci->eraseFromParent();
427 return true;
428 }
429
430 // Handle the ConstantAggregateZero case
431 else if (ConstantAggregateZero* CAZ =
432 dyn_cast<ConstantAggregateZero>(INTLZR))
433 {
434 // We know this is the zero length string case so we can just avoid
435 // the strcat altogether and replace the CallInst with its first operand
436 // (what strcat returns).
437 ci->replaceAllUsesWith(ci->getOperand(1));
438 ci->eraseFromParent();
439 return true;
Reid Spencerf2534c72005-04-25 21:11:48 +0000440 }
441
442 // We didn't pass the criteria for this optimization so return false.
Reid Spencer9bbaa2a2005-04-25 03:59:26 +0000443 return false;
444 }
445} StrCatOptimizer;
446
Reid Spencerf2534c72005-04-25 21:11:48 +0000447/// This CallOptimizer will simplify a call to the memcpy library function by
448/// expanding it out to a small set of stores if the copy source is a constant
449/// array.
450/// @brief Simplify the memcpy library function.
451struct MemCpyOptimization : public CallOptimizer
452{
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000453 MemCpyOptimization() : CallOptimizer("llvm.memcpy") {}
Reid Spencerf2534c72005-04-25 21:11:48 +0000454 virtual ~MemCpyOptimization() {}
455
456 /// @brief Make sure that the "memcpy" function has the right prototype
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000457 virtual bool ValidateCalledFunction(const Function* f)
Reid Spencerf2534c72005-04-25 21:11:48 +0000458 {
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000459 if (f->getReturnType() == PointerType::get(Type::VoidTy))
Reid Spencerf2534c72005-04-25 21:11:48 +0000460 if (f->arg_size() == 2)
461 {
462 Function::const_arg_iterator AI = f->arg_begin();
463 if (AI++->getType() == PointerType::get(Type::SByteTy))
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000464 if (AI++->getType() == PointerType::get(Type::SByteTy))
465 if (AI++->getType() == Type::IntTy)
466 if (AI->getType() == Type::IntTy)
Reid Spencerf2534c72005-04-25 21:11:48 +0000467 return true;
468 }
469 return false;
470 }
471
472 /// Perform the optimization if the length of the string concatenated
473 /// is reasonably short and it is a constant array.
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000474 virtual bool OptimizeCall(CallInst* ci)
Reid Spencerf2534c72005-04-25 21:11:48 +0000475 {
Reid Spencer8ee5aac2005-04-26 03:26:15 +0000476 //
Reid Spencerf2534c72005-04-25 21:11:48 +0000477 // We didn't pass the criteria for this optimization so return false.
478 return false;
479 }
480} MemCpyOptimizer;
Reid Spencer39a762d2005-04-25 02:53:12 +0000481}