1 //===- SimplifyLibCalls.cpp - Optimize specific well-known library calls --===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Reid Spencer and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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
13 // 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.
17 //===----------------------------------------------------------------------===//
19 #define DEBUG_TYPE "simplify-libcalls"
20 #include "llvm/Constants.h"
21 #include "llvm/DerivedTypes.h"
22 #include "llvm/Instructions.h"
23 #include "llvm/Module.h"
24 #include "llvm/Pass.h"
25 #include "llvm/ADT/hash_map"
26 #include "llvm/ADT/Statistic.h"
27 #include "llvm/Support/Debug.h"
28 #include "llvm/Target/TargetData.h"
29 #include "llvm/Transforms/IPO.h"
30 #include "llvm/Config/config.h"
36 /// This statistic keeps track of the total number of library calls that have
37 /// been simplified regardless of which call it is.
38 Statistic<> SimplifiedLibCalls("simplify-libcalls",
39 "Total number of library calls simplified");
41 // Forward declarations
42 class LibCallOptimization;
43 class SimplifyLibCalls;
45 /// @brief The list of optimizations deriving from LibCallOptimization
46 hash_map<std::string,LibCallOptimization*> optlist;
48 /// This class is the abstract base class for the set of optimizations that
49 /// corresponds to one library call. The SimplifyLibCalls pass will call the
50 /// ValidateCalledFunction method to ask the optimization if a given Function
51 /// is the kind that the optimization can handle. If the subclass returns true,
52 /// then SImplifyLibCalls will also call the OptimizeCall method to perform,
53 /// or attempt to perform, the optimization(s) for the library call. Otherwise,
54 /// OptimizeCall won't be called. Subclasses are responsible for providing the
55 /// name of the library call (strlen, strcpy, etc.) to the LibCallOptimization
56 /// constructor. This is used to efficiently select which call instructions to
57 /// optimize. The criteria for a "lib call" is "anything with well known
58 /// semantics", typically a library function that is defined by an international
59 /// standard. Because the semantics are well known, the optimizations can
60 /// generally short-circuit actually calling the function if there's a simpler
61 /// way (e.g. strlen(X) can be reduced to a constant if X is a constant global).
62 /// @brief Base class for library call optimizations
63 class LibCallOptimization
66 /// The \p fname argument must be the name of the library function being
67 /// optimized by the subclass.
68 /// @brief Constructor that registers the optimization.
69 LibCallOptimization(const char* fname, const char* description )
72 , occurrences("simplify-libcalls",description)
75 // Register this call optimizer in the optlist (a hash_map)
76 optlist[fname] = this;
79 /// @brief Deregister from the optlist
80 virtual ~LibCallOptimization() { optlist.erase(func_name); }
82 /// The implementation of this function in subclasses should determine if
83 /// \p F is suitable for the optimization. This method is called by
84 /// SimplifyLibCalls::runOnModule to short circuit visiting all the call
85 /// sites of such a function if that function is not suitable in the first
86 /// place. If the called function is suitabe, this method should return true;
87 /// false, otherwise. This function should also perform any lazy
88 /// initialization that the LibCallOptimization needs to do, if its to return
89 /// true. This avoids doing initialization until the optimizer is actually
90 /// going to be called upon to do some optimization.
91 /// @brief Determine if the function is suitable for optimization
92 virtual bool ValidateCalledFunction(
93 const Function* F, ///< The function that is the target of call sites
94 SimplifyLibCalls& SLC ///< The pass object invoking us
97 /// The implementations of this function in subclasses is the heart of the
98 /// SimplifyLibCalls algorithm. Sublcasses of this class implement
99 /// OptimizeCall to determine if (a) the conditions are right for optimizing
100 /// the call and (b) to perform the optimization. If an action is taken
101 /// against ci, the subclass is responsible for returning true and ensuring
102 /// that ci is erased from its parent.
103 /// @brief Optimize a call, if possible.
104 virtual bool OptimizeCall(
105 CallInst* ci, ///< The call instruction that should be optimized.
106 SimplifyLibCalls& SLC ///< The pass object invoking us
109 /// @brief Get the name of the library call being optimized
110 const char * getFunctionName() const { return func_name; }
113 /// @brief Called by SimplifyLibCalls to update the occurrences statistic.
114 void succeeded() { DEBUG(++occurrences); }
118 const char* func_name; ///< Name of the library call we optimize
120 Statistic<> occurrences; ///< debug statistic (-debug-only=simplify-libcalls)
124 /// This class is an LLVM Pass that applies each of the LibCallOptimization
125 /// instances to all the call sites in a module, relatively efficiently. The
126 /// purpose of this pass is to provide optimizations for calls to well-known
127 /// functions with well-known semantics, such as those in the c library. The
128 /// class provides the basic infrastructure for handling runOnModule. Whenever /// this pass finds a function call, it asks the appropriate optimizer to
129 /// validate the call (ValidateLibraryCall). If it is validated, then
130 /// the OptimizeCall method is also called.
131 /// @brief A ModulePass for optimizing well-known function calls.
132 class SimplifyLibCalls : public ModulePass
135 /// We need some target data for accurate signature details that are
136 /// target dependent. So we require target data in our AnalysisUsage.
137 /// @brief Require TargetData from AnalysisUsage.
138 virtual void getAnalysisUsage(AnalysisUsage& Info) const
140 // Ask that the TargetData analysis be performed before us so we can use
142 Info.addRequired<TargetData>();
145 /// For this pass, process all of the function calls in the module, calling
146 /// ValidateLibraryCall and OptimizeCall as appropriate.
147 /// @brief Run all the lib call optimizations on a Module.
148 virtual bool runOnModule(Module &M)
154 // The call optimizations can be recursive. That is, the optimization might
155 // generate a call to another function which can also be optimized. This way
156 // we make the LibCallOptimization instances very specific to the case they
157 // handle. It also means we need to keep running over the function calls in
158 // the module until we don't get any more optimizations possible.
159 bool found_optimization = false;
162 found_optimization = false;
163 for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI)
165 // All the "well-known" functions are external and have external linkage
166 // because they live in a runtime library somewhere and were (probably)
167 // not compiled by LLVM. So, we only act on external functions that
168 // have external linkage and non-empty uses.
169 if (!FI->isExternal() || !FI->hasExternalLinkage() || FI->use_empty())
172 // Get the optimization class that pertains to this function
173 LibCallOptimization* CO = optlist[FI->getName().c_str()];
177 // Make sure the called function is suitable for the optimization
178 if (!CO->ValidateCalledFunction(FI,*this))
181 // Loop over each of the uses of the function
182 for (Value::use_iterator UI = FI->use_begin(), UE = FI->use_end();
185 // If the use of the function is a call instruction
186 if (CallInst* CI = dyn_cast<CallInst>(*UI++))
188 // Do the optimization on the LibCallOptimization.
189 if (CO->OptimizeCall(CI,*this))
191 ++SimplifiedLibCalls;
192 found_optimization = result = true;
200 } while (found_optimization);
204 /// @brief Return the *current* module we're working on.
205 Module* getModule() const { return M; }
207 /// @brief Return the *current* target data for the module we're working on.
208 TargetData* getTargetData() const { return TD; }
210 /// @brief Return the size_t type -- syntactic shortcut
211 const Type* getIntPtrType() const { return TD->getIntPtrType(); }
213 /// @brief Return a Function* for the fputc libcall
214 Function* get_fputc(const Type* FILEptr_type)
218 std::vector<const Type*> args;
219 args.push_back(Type::IntTy);
220 args.push_back(FILEptr_type);
221 FunctionType* fputc_type =
222 FunctionType::get(Type::IntTy, args, false);
223 fputc_func = M->getOrInsertFunction("fputc",fputc_type);
228 /// @brief Return a Function* for the fwrite libcall
229 Function* get_fwrite(const Type* FILEptr_type)
233 std::vector<const Type*> args;
234 args.push_back(PointerType::get(Type::SByteTy));
235 args.push_back(TD->getIntPtrType());
236 args.push_back(TD->getIntPtrType());
237 args.push_back(FILEptr_type);
238 FunctionType* fwrite_type =
239 FunctionType::get(TD->getIntPtrType(), args, false);
240 fwrite_func = M->getOrInsertFunction("fwrite",fwrite_type);
245 /// @brief Return a Function* for the sqrt libcall
250 std::vector<const Type*> args;
251 args.push_back(Type::DoubleTy);
252 FunctionType* sqrt_type =
253 FunctionType::get(Type::DoubleTy, args, false);
254 sqrt_func = M->getOrInsertFunction("sqrt",sqrt_type);
259 /// @brief Return a Function* for the strlen libcall
260 Function* get_strcpy()
264 std::vector<const Type*> args;
265 args.push_back(PointerType::get(Type::SByteTy));
266 args.push_back(PointerType::get(Type::SByteTy));
267 FunctionType* strcpy_type =
268 FunctionType::get(PointerType::get(Type::SByteTy), args, false);
269 strcpy_func = M->getOrInsertFunction("strcpy",strcpy_type);
274 /// @brief Return a Function* for the strlen libcall
275 Function* get_strlen()
279 std::vector<const Type*> args;
280 args.push_back(PointerType::get(Type::SByteTy));
281 FunctionType* strlen_type =
282 FunctionType::get(TD->getIntPtrType(), args, false);
283 strlen_func = M->getOrInsertFunction("strlen",strlen_type);
288 /// @brief Return a Function* for the memchr libcall
289 Function* get_memchr()
293 std::vector<const Type*> args;
294 args.push_back(PointerType::get(Type::SByteTy));
295 args.push_back(Type::IntTy);
296 args.push_back(TD->getIntPtrType());
297 FunctionType* memchr_type = FunctionType::get(
298 PointerType::get(Type::SByteTy), args, false);
299 memchr_func = M->getOrInsertFunction("memchr",memchr_type);
304 /// @brief Return a Function* for the memcpy libcall
305 Function* get_memcpy()
309 // Note: this is for llvm.memcpy intrinsic
310 std::vector<const Type*> args;
311 args.push_back(PointerType::get(Type::SByteTy));
312 args.push_back(PointerType::get(Type::SByteTy));
313 args.push_back(Type::UIntTy);
314 args.push_back(Type::UIntTy);
315 FunctionType* memcpy_type = FunctionType::get(Type::VoidTy, args, false);
316 memcpy_func = M->getOrInsertFunction("llvm.memcpy",memcpy_type);
322 /// @brief Reset our cached data for a new Module
323 void reset(Module& mod)
326 TD = &getAnalysis<TargetData>();
337 Function* fputc_func; ///< Cached fputc function
338 Function* fwrite_func; ///< Cached fwrite function
339 Function* memcpy_func; ///< Cached llvm.memcpy function
340 Function* memchr_func; ///< Cached memchr function
341 Function* sqrt_func; ///< Cached sqrt function
342 Function* strcpy_func; ///< Cached strcpy function
343 Function* strlen_func; ///< Cached strlen function
344 Module* M; ///< Cached Module
345 TargetData* TD; ///< Cached TargetData
349 RegisterOpt<SimplifyLibCalls>
350 X("simplify-libcalls","Simplify well-known library calls");
352 } // anonymous namespace
354 // The only public symbol in this file which just instantiates the pass object
355 ModulePass *llvm::createSimplifyLibCallsPass()
357 return new SimplifyLibCalls();
360 // Classes below here, in the anonymous namespace, are all subclasses of the
361 // LibCallOptimization class, each implementing all optimizations possible for a
362 // single well-known library call. Each has a static singleton instance that
363 // auto registers it into the "optlist" global above.
366 // Forward declare a utility function.
367 bool getConstantStringLength(Value* V, uint64_t& len, ConstantArray** A = 0 );
369 /// This LibCallOptimization will find instances of a call to "exit" that occurs
370 /// within the "main" function and change it to a simple "ret" instruction with
371 /// the same value passed to the exit function. When this is done, it splits the
372 /// basic block at the exit(3) call and deletes the call instruction.
373 /// @brief Replace calls to exit in main with a simple return
374 struct ExitInMainOptimization : public LibCallOptimization
376 ExitInMainOptimization() : LibCallOptimization("exit",
377 "Number of 'exit' calls simplified") {}
378 virtual ~ExitInMainOptimization() {}
380 // Make sure the called function looks like exit (int argument, int return
381 // type, external linkage, not varargs).
382 virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC)
384 if (f->arg_size() >= 1)
385 if (f->arg_begin()->getType()->isInteger())
390 virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC)
392 // To be careful, we check that the call to exit is coming from "main", that
393 // main has external linkage, and the return type of main and the argument
394 // to exit have the same type.
395 Function *from = ci->getParent()->getParent();
396 if (from->hasExternalLinkage())
397 if (from->getReturnType() == ci->getOperand(1)->getType())
398 if (from->getName() == "main")
400 // Okay, time to actually do the optimization. First, get the basic
401 // block of the call instruction
402 BasicBlock* bb = ci->getParent();
404 // Create a return instruction that we'll replace the call with.
405 // Note that the argument of the return is the argument of the call
407 ReturnInst* ri = new ReturnInst(ci->getOperand(1), ci);
409 // Split the block at the call instruction which places it in a new
411 bb->splitBasicBlock(ci);
413 // The block split caused a branch instruction to be inserted into
414 // the end of the original block, right after the return instruction
415 // that we put there. That's not a valid block, so delete the branch
417 bb->getInstList().pop_back();
419 // Now we can finally get rid of the call instruction which now lives
420 // in the new basic block.
421 ci->eraseFromParent();
423 // Optimization succeeded, return true.
426 // We didn't pass the criteria for this optimization so return false
429 } ExitInMainOptimizer;
431 /// This LibCallOptimization will simplify a call to the strcat library
432 /// function. The simplification is possible only if the string being
433 /// concatenated is a constant array or a constant expression that results in
434 /// a constant string. In this case we can replace it with strlen + llvm.memcpy
435 /// of the constant string. Both of these calls are further reduced, if possible
436 /// on subsequent passes.
437 /// @brief Simplify the strcat library function.
438 struct StrCatOptimization : public LibCallOptimization
441 /// @brief Default constructor
442 StrCatOptimization() : LibCallOptimization("strcat",
443 "Number of 'strcat' calls simplified") {}
446 /// @breif Destructor
447 virtual ~StrCatOptimization() {}
449 /// @brief Make sure that the "strcat" function has the right prototype
450 virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC)
452 if (f->getReturnType() == PointerType::get(Type::SByteTy))
453 if (f->arg_size() == 2)
455 Function::const_arg_iterator AI = f->arg_begin();
456 if (AI++->getType() == PointerType::get(Type::SByteTy))
457 if (AI->getType() == PointerType::get(Type::SByteTy))
459 // Indicate this is a suitable call type.
466 /// @brief Optimize the strcat library function
467 virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC)
469 // Extract some information from the instruction
470 Module* M = ci->getParent()->getParent()->getParent();
471 Value* dest = ci->getOperand(1);
472 Value* src = ci->getOperand(2);
474 // Extract the initializer (while making numerous checks) from the
475 // source operand of the call to strcat. If we get null back, one of
476 // a variety of checks in get_GVInitializer failed
478 if (!getConstantStringLength(src,len))
481 // Handle the simple, do-nothing case
484 ci->replaceAllUsesWith(dest);
485 ci->eraseFromParent();
489 // Increment the length because we actually want to memcpy the null
490 // terminator as well.
493 // We need to find the end of the destination string. That's where the
494 // memory is to be moved to. We just generate a call to strlen (further
495 // optimized in another pass). Note that the SLC.get_strlen() call
496 // caches the Function* for us.
497 CallInst* strlen_inst =
498 new CallInst(SLC.get_strlen(), dest, dest->getName()+".len",ci);
500 // Now that we have the destination's length, we must index into the
501 // destination's pointer to get the actual memcpy destination (end of
502 // the string .. we're concatenating).
503 std::vector<Value*> idx;
504 idx.push_back(strlen_inst);
505 GetElementPtrInst* gep =
506 new GetElementPtrInst(dest,idx,dest->getName()+".indexed",ci);
508 // We have enough information to now generate the memcpy call to
509 // do the concatenation for us.
510 std::vector<Value*> vals;
511 vals.push_back(gep); // destination
512 vals.push_back(ci->getOperand(2)); // source
513 vals.push_back(ConstantUInt::get(Type::UIntTy,len)); // length
514 vals.push_back(ConstantUInt::get(Type::UIntTy,1)); // alignment
515 new CallInst(SLC.get_memcpy(), vals, "", ci);
517 // Finally, substitute the first operand of the strcat call for the
518 // strcat call itself since strcat returns its first operand; and,
519 // kill the strcat CallInst.
520 ci->replaceAllUsesWith(dest);
521 ci->eraseFromParent();
526 /// This LibCallOptimization will simplify a call to the strchr library
527 /// function. It optimizes out cases where the arguments are both constant
528 /// and the result can be determined statically.
529 /// @brief Simplify the strcmp library function.
530 struct StrChrOptimization : public LibCallOptimization
533 StrChrOptimization() : LibCallOptimization("strchr",
534 "Number of 'strchr' calls simplified") {}
535 virtual ~StrChrOptimization() {}
537 /// @brief Make sure that the "strchr" function has the right prototype
538 virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC)
540 if (f->getReturnType() == PointerType::get(Type::SByteTy) &&
546 /// @brief Perform the strcpy optimization
547 virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC)
549 // If there aren't three operands, bail
550 if (ci->getNumOperands() != 3)
553 // Check that the first argument to strchr is a constant array of sbyte.
554 // If it is, get the length and data, otherwise return false.
557 if (!getConstantStringLength(ci->getOperand(1),len,&CA))
560 // Check that the second argument to strchr is a constant int, return false
562 ConstantSInt* CSI = dyn_cast<ConstantSInt>(ci->getOperand(2));
565 // Just lower this to memchr since we know the length of the string as
567 Function* f = SLC.get_memchr();
568 std::vector<Value*> args;
569 args.push_back(ci->getOperand(1));
570 args.push_back(ci->getOperand(2));
571 args.push_back(ConstantUInt::get(SLC.getIntPtrType(),len));
572 ci->replaceAllUsesWith( new CallInst(f,args,ci->getName(),ci));
573 ci->eraseFromParent();
577 // Get the character we're looking for
578 int64_t chr = CSI->getValue();
580 // Compute the offset
582 bool char_found = false;
583 for (uint64_t i = 0; i < len; ++i)
585 if (ConstantSInt* CI = dyn_cast<ConstantSInt>(CA->getOperand(i)))
587 // Check for the null terminator
588 if (CI->isNullValue())
589 break; // we found end of string
590 else if (CI->getValue() == chr)
599 // strchr(s,c) -> offset_of_in(c,s)
600 // (if c is a constant integer and s is a constant string)
603 std::vector<Value*> indices;
604 indices.push_back(ConstantUInt::get(Type::ULongTy,offset));
605 GetElementPtrInst* GEP = new GetElementPtrInst(ci->getOperand(1),indices,
606 ci->getOperand(1)->getName()+".strchr",ci);
607 ci->replaceAllUsesWith(GEP);
610 ci->replaceAllUsesWith(
611 ConstantPointerNull::get(PointerType::get(Type::SByteTy)));
613 ci->eraseFromParent();
618 /// This LibCallOptimization will simplify a call to the strcmp library
619 /// function. It optimizes out cases where one or both arguments are constant
620 /// and the result can be determined statically.
621 /// @brief Simplify the strcmp library function.
622 struct StrCmpOptimization : public LibCallOptimization
625 StrCmpOptimization() : LibCallOptimization("strcmp",
626 "Number of 'strcmp' calls simplified") {}
627 virtual ~StrCmpOptimization() {}
629 /// @brief Make sure that the "strcpy" function has the right prototype
630 virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC)
632 if (f->getReturnType() == Type::IntTy && f->arg_size() == 2)
637 /// @brief Perform the strcpy optimization
638 virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC)
640 // First, check to see if src and destination are the same. If they are,
641 // then the optimization is to replace the CallInst with a constant 0
642 // because the call is a no-op.
643 Value* s1 = ci->getOperand(1);
644 Value* s2 = ci->getOperand(2);
648 ci->replaceAllUsesWith(ConstantInt::get(Type::IntTy,0));
649 ci->eraseFromParent();
653 bool isstr_1 = false;
656 if (getConstantStringLength(s1,len_1,&A1))
661 // strcmp("",x) -> *x
662 LoadInst* load = new LoadInst(s1,ci->getName()+".load",ci);
664 new CastInst(load,Type::IntTy,ci->getName()+".int",ci);
665 ci->replaceAllUsesWith(cast);
666 ci->eraseFromParent();
671 bool isstr_2 = false;
674 if (getConstantStringLength(s2,len_2,&A2))
679 // strcmp(x,"") -> *x
680 LoadInst* load = new LoadInst(s2,ci->getName()+".val",ci);
682 new CastInst(load,Type::IntTy,ci->getName()+".int",ci);
683 ci->replaceAllUsesWith(cast);
684 ci->eraseFromParent();
689 if (isstr_1 && isstr_2)
691 // strcmp(x,y) -> cnst (if both x and y are constant strings)
692 std::string str1 = A1->getAsString();
693 std::string str2 = A2->getAsString();
694 int result = strcmp(str1.c_str(), str2.c_str());
695 ci->replaceAllUsesWith(ConstantSInt::get(Type::IntTy,result));
696 ci->eraseFromParent();
703 /// This LibCallOptimization will simplify a call to the strncmp library
704 /// function. It optimizes out cases where one or both arguments are constant
705 /// and the result can be determined statically.
706 /// @brief Simplify the strncmp library function.
707 struct StrNCmpOptimization : public LibCallOptimization
710 StrNCmpOptimization() : LibCallOptimization("strncmp",
711 "Number of 'strncmp' calls simplified") {}
712 virtual ~StrNCmpOptimization() {}
714 /// @brief Make sure that the "strcpy" function has the right prototype
715 virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC)
717 if (f->getReturnType() == Type::IntTy && f->arg_size() == 3)
722 /// @brief Perform the strncpy optimization
723 virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC)
725 // First, check to see if src and destination are the same. If they are,
726 // then the optimization is to replace the CallInst with a constant 0
727 // because the call is a no-op.
728 Value* s1 = ci->getOperand(1);
729 Value* s2 = ci->getOperand(2);
732 // strncmp(x,x,l) -> 0
733 ci->replaceAllUsesWith(ConstantInt::get(Type::IntTy,0));
734 ci->eraseFromParent();
738 // Check the length argument, if it is Constant zero then the strings are
740 uint64_t len_arg = 0;
741 bool len_arg_is_const = false;
742 if (ConstantInt* len_CI = dyn_cast<ConstantInt>(ci->getOperand(3)))
744 len_arg_is_const = true;
745 len_arg = len_CI->getRawValue();
748 // strncmp(x,y,0) -> 0
749 ci->replaceAllUsesWith(ConstantInt::get(Type::IntTy,0));
750 ci->eraseFromParent();
755 bool isstr_1 = false;
758 if (getConstantStringLength(s1,len_1,&A1))
763 // strncmp("",x) -> *x
764 LoadInst* load = new LoadInst(s1,ci->getName()+".load",ci);
766 new CastInst(load,Type::IntTy,ci->getName()+".int",ci);
767 ci->replaceAllUsesWith(cast);
768 ci->eraseFromParent();
773 bool isstr_2 = false;
776 if (getConstantStringLength(s2,len_2,&A2))
781 // strncmp(x,"") -> *x
782 LoadInst* load = new LoadInst(s2,ci->getName()+".val",ci);
784 new CastInst(load,Type::IntTy,ci->getName()+".int",ci);
785 ci->replaceAllUsesWith(cast);
786 ci->eraseFromParent();
791 if (isstr_1 && isstr_2 && len_arg_is_const)
793 // strncmp(x,y,const) -> constant
794 std::string str1 = A1->getAsString();
795 std::string str2 = A2->getAsString();
796 int result = strncmp(str1.c_str(), str2.c_str(), len_arg);
797 ci->replaceAllUsesWith(ConstantSInt::get(Type::IntTy,result));
798 ci->eraseFromParent();
805 /// This LibCallOptimization will simplify a call to the strcpy library
806 /// function. Two optimizations are possible:
807 /// (1) If src and dest are the same and not volatile, just return dest
808 /// (2) If the src is a constant then we can convert to llvm.memmove
809 /// @brief Simplify the strcpy library function.
810 struct StrCpyOptimization : public LibCallOptimization
813 StrCpyOptimization() : LibCallOptimization("strcpy",
814 "Number of 'strcpy' calls simplified") {}
815 virtual ~StrCpyOptimization() {}
817 /// @brief Make sure that the "strcpy" function has the right prototype
818 virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC)
820 if (f->getReturnType() == PointerType::get(Type::SByteTy))
821 if (f->arg_size() == 2)
823 Function::const_arg_iterator AI = f->arg_begin();
824 if (AI++->getType() == PointerType::get(Type::SByteTy))
825 if (AI->getType() == PointerType::get(Type::SByteTy))
827 // Indicate this is a suitable call type.
834 /// @brief Perform the strcpy optimization
835 virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC)
837 // First, check to see if src and destination are the same. If they are,
838 // then the optimization is to replace the CallInst with the destination
839 // because the call is a no-op. Note that this corresponds to the
840 // degenerate strcpy(X,X) case which should have "undefined" results
841 // according to the C specification. However, it occurs sometimes and
842 // we optimize it as a no-op.
843 Value* dest = ci->getOperand(1);
844 Value* src = ci->getOperand(2);
847 ci->replaceAllUsesWith(dest);
848 ci->eraseFromParent();
852 // Get the length of the constant string referenced by the second operand,
853 // the "src" parameter. Fail the optimization if we can't get the length
854 // (note that getConstantStringLength does lots of checks to make sure this
857 if (!getConstantStringLength(ci->getOperand(2),len))
860 // If the constant string's length is zero we can optimize this by just
861 // doing a store of 0 at the first byte of the destination
864 new StoreInst(ConstantInt::get(Type::SByteTy,0),ci->getOperand(1),ci);
865 ci->replaceAllUsesWith(dest);
866 ci->eraseFromParent();
870 // Increment the length because we actually want to memcpy the null
871 // terminator as well.
874 // Extract some information from the instruction
875 Module* M = ci->getParent()->getParent()->getParent();
877 // We have enough information to now generate the memcpy call to
878 // do the concatenation for us.
879 std::vector<Value*> vals;
880 vals.push_back(dest); // destination
881 vals.push_back(src); // source
882 vals.push_back(ConstantUInt::get(Type::UIntTy,len)); // length
883 vals.push_back(ConstantUInt::get(Type::UIntTy,1)); // alignment
884 new CallInst(SLC.get_memcpy(), vals, "", ci);
886 // Finally, substitute the first operand of the strcat call for the
887 // strcat call itself since strcat returns its first operand; and,
888 // kill the strcat CallInst.
889 ci->replaceAllUsesWith(dest);
890 ci->eraseFromParent();
895 /// This LibCallOptimization will simplify a call to the strlen library
896 /// function by replacing it with a constant value if the string provided to
897 /// it is a constant array.
898 /// @brief Simplify the strlen library function.
899 struct StrLenOptimization : public LibCallOptimization
901 StrLenOptimization() : LibCallOptimization("strlen",
902 "Number of 'strlen' calls simplified") {}
903 virtual ~StrLenOptimization() {}
905 /// @brief Make sure that the "strlen" function has the right prototype
906 virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC)
908 if (f->getReturnType() == SLC.getTargetData()->getIntPtrType())
909 if (f->arg_size() == 1)
910 if (Function::const_arg_iterator AI = f->arg_begin())
911 if (AI->getType() == PointerType::get(Type::SByteTy))
916 /// @brief Perform the strlen optimization
917 virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC)
919 // Make sure we're dealing with an sbyte* here.
920 Value* str = ci->getOperand(1);
921 if (str->getType() != PointerType::get(Type::SByteTy))
924 // Does the call to strlen have exactly one use?
926 // Is that single use a binary operator?
927 if (BinaryOperator* bop = dyn_cast<BinaryOperator>(ci->use_back()))
928 // Is it compared against a constant integer?
929 if (ConstantInt* CI = dyn_cast<ConstantInt>(bop->getOperand(1)))
931 // Get the value the strlen result is compared to
932 uint64_t val = CI->getRawValue();
934 // If its compared against length 0 with == or !=
936 (bop->getOpcode() == Instruction::SetEQ ||
937 bop->getOpcode() == Instruction::SetNE))
939 // strlen(x) != 0 -> *x != 0
940 // strlen(x) == 0 -> *x == 0
941 LoadInst* load = new LoadInst(str,str->getName()+".first",ci);
942 BinaryOperator* rbop = BinaryOperator::create(bop->getOpcode(),
943 load, ConstantSInt::get(Type::SByteTy,0),
944 bop->getName()+".strlen", ci);
945 bop->replaceAllUsesWith(rbop);
946 bop->eraseFromParent();
947 ci->eraseFromParent();
952 // Get the length of the constant string operand
954 if (!getConstantStringLength(ci->getOperand(1),len))
957 // strlen("xyz") -> 3 (for example)
958 ci->replaceAllUsesWith(
959 ConstantInt::get(SLC.getTargetData()->getIntPtrType(),len));
960 ci->eraseFromParent();
965 /// This LibCallOptimization will simplify a call to the memcpy library
966 /// function by expanding it out to a single store of size 0, 1, 2, 4, or 8
967 /// bytes depending on the length of the string and the alignment. Additional
968 /// optimizations are possible in code generation (sequence of immediate store)
969 /// @brief Simplify the memcpy library function.
970 struct LLVMMemCpyOptimization : public LibCallOptimization
972 /// @brief Default Constructor
973 LLVMMemCpyOptimization() : LibCallOptimization("llvm.memcpy",
974 "Number of 'llvm.memcpy' calls simplified") {}
977 /// @brief Subclass Constructor
978 LLVMMemCpyOptimization(const char* fname, const char* desc)
979 : LibCallOptimization(fname, desc) {}
981 /// @brief Destructor
982 virtual ~LLVMMemCpyOptimization() {}
984 /// @brief Make sure that the "memcpy" function has the right prototype
985 virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& TD)
987 // Just make sure this has 4 arguments per LLVM spec.
988 return (f->arg_size() == 4);
991 /// Because of alignment and instruction information that we don't have, we
992 /// leave the bulk of this to the code generators. The optimization here just
993 /// deals with a few degenerate cases where the length of the string and the
994 /// alignment match the sizes of our intrinsic types so we can do a load and
995 /// store instead of the memcpy call.
996 /// @brief Perform the memcpy optimization.
997 virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& TD)
999 // Make sure we have constant int values to work with
1000 ConstantInt* LEN = dyn_cast<ConstantInt>(ci->getOperand(3));
1003 ConstantInt* ALIGN = dyn_cast<ConstantInt>(ci->getOperand(4));
1007 // If the length is larger than the alignment, we can't optimize
1008 uint64_t len = LEN->getRawValue();
1009 uint64_t alignment = ALIGN->getRawValue();
1011 alignment = 1; // Alignment 0 is identity for alignment 1
1012 if (len > alignment)
1015 // Get the type we will cast to, based on size of the string
1016 Value* dest = ci->getOperand(1);
1017 Value* src = ci->getOperand(2);
1022 // memcpy(d,s,0,a) -> noop
1023 ci->eraseFromParent();
1025 case 1: castType = Type::SByteTy; break;
1026 case 2: castType = Type::ShortTy; break;
1027 case 4: castType = Type::IntTy; break;
1028 case 8: castType = Type::LongTy; break;
1033 // Cast source and dest to the right sized primitive and then load/store
1035 new CastInst(src,PointerType::get(castType),src->getName()+".cast",ci);
1036 CastInst* DestCast =
1037 new CastInst(dest,PointerType::get(castType),dest->getName()+".cast",ci);
1038 LoadInst* LI = new LoadInst(SrcCast,SrcCast->getName()+".val",ci);
1039 StoreInst* SI = new StoreInst(LI, DestCast, ci);
1040 ci->eraseFromParent();
1043 } LLVMMemCpyOptimizer;
1045 /// This LibCallOptimization will simplify a call to the memmove library
1046 /// function. It is identical to MemCopyOptimization except for the name of
1048 /// @brief Simplify the memmove library function.
1049 struct LLVMMemMoveOptimization : public LLVMMemCpyOptimization
1051 /// @brief Default Constructor
1052 LLVMMemMoveOptimization() : LLVMMemCpyOptimization("llvm.memmove",
1053 "Number of 'llvm.memmove' calls simplified") {}
1055 } LLVMMemMoveOptimizer;
1057 /// This LibCallOptimization will simplify a call to the memset library
1058 /// function by expanding it out to a single store of size 0, 1, 2, 4, or 8
1059 /// bytes depending on the length argument.
1060 struct LLVMMemSetOptimization : public LibCallOptimization
1062 /// @brief Default Constructor
1063 LLVMMemSetOptimization() : LibCallOptimization("llvm.memset",
1064 "Number of 'llvm.memset' calls simplified") {}
1067 /// @brief Destructor
1068 virtual ~LLVMMemSetOptimization() {}
1070 /// @brief Make sure that the "memset" function has the right prototype
1071 virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& TD)
1073 // Just make sure this has 3 arguments per LLVM spec.
1074 return (f->arg_size() == 4);
1077 /// Because of alignment and instruction information that we don't have, we
1078 /// leave the bulk of this to the code generators. The optimization here just
1079 /// deals with a few degenerate cases where the length parameter is constant
1080 /// and the alignment matches the sizes of our intrinsic types so we can do
1081 /// store instead of the memcpy call. Other calls are transformed into the
1082 /// llvm.memset intrinsic.
1083 /// @brief Perform the memset optimization.
1084 virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& TD)
1086 // Make sure we have constant int values to work with
1087 ConstantInt* LEN = dyn_cast<ConstantInt>(ci->getOperand(3));
1090 ConstantInt* ALIGN = dyn_cast<ConstantInt>(ci->getOperand(4));
1094 // Extract the length and alignment
1095 uint64_t len = LEN->getRawValue();
1096 uint64_t alignment = ALIGN->getRawValue();
1098 // Alignment 0 is identity for alignment 1
1102 // If the length is zero, this is a no-op
1105 // memset(d,c,0,a) -> noop
1106 ci->eraseFromParent();
1110 // If the length is larger than the alignment, we can't optimize
1111 if (len > alignment)
1114 // Make sure we have a constant ubyte to work with so we can extract
1115 // the value to be filled.
1116 ConstantUInt* FILL = dyn_cast<ConstantUInt>(ci->getOperand(2));
1119 if (FILL->getType() != Type::UByteTy)
1122 // memset(s,c,n) -> store s, c (for n=1,2,4,8)
1124 // Extract the fill character
1125 uint64_t fill_char = FILL->getValue();
1126 uint64_t fill_value = fill_char;
1128 // Get the type we will cast to, based on size of memory area to fill, and
1129 // and the value we will store there.
1130 Value* dest = ci->getOperand(1);
1135 castType = Type::UByteTy;
1138 castType = Type::UShortTy;
1139 fill_value |= fill_char << 8;
1142 castType = Type::UIntTy;
1143 fill_value |= fill_char << 8 | fill_char << 16 | fill_char << 24;
1146 castType = Type::ULongTy;
1147 fill_value |= fill_char << 8 | fill_char << 16 | fill_char << 24;
1148 fill_value |= fill_char << 32 | fill_char << 40 | fill_char << 48;
1149 fill_value |= fill_char << 56;
1155 // Cast dest to the right sized primitive and then load/store
1156 CastInst* DestCast =
1157 new CastInst(dest,PointerType::get(castType),dest->getName()+".cast",ci);
1158 new StoreInst(ConstantUInt::get(castType,fill_value),DestCast, ci);
1159 ci->eraseFromParent();
1162 } LLVMMemSetOptimizer;
1164 /// This LibCallOptimization will simplify calls to the "pow" library
1165 /// function. It looks for cases where the result of pow is well known and
1166 /// substitutes the appropriate value.
1167 /// @brief Simplify the pow library function.
1168 struct PowOptimization : public LibCallOptimization
1171 /// @brief Default Constructor
1172 PowOptimization() : LibCallOptimization("pow",
1173 "Number of 'pow' calls simplified") {}
1175 /// @brief Destructor
1176 virtual ~PowOptimization() {}
1178 /// @brief Make sure that the "pow" function has the right prototype
1179 virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC)
1181 // Just make sure this has 2 arguments
1182 return (f->arg_size() == 2);
1185 /// @brief Perform the pow optimization.
1186 virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC)
1188 const Type *Ty = cast<Function>(ci->getOperand(0))->getReturnType();
1189 Value* base = ci->getOperand(1);
1190 Value* expn = ci->getOperand(2);
1191 if (ConstantFP *Op1 = dyn_cast<ConstantFP>(base)) {
1192 double Op1V = Op1->getValue();
1195 // pow(1.0,x) -> 1.0
1196 ci->replaceAllUsesWith(ConstantFP::get(Ty,1.0));
1197 ci->eraseFromParent();
1201 else if (ConstantFP* Op2 = dyn_cast<ConstantFP>(expn))
1203 double Op2V = Op2->getValue();
1206 // pow(x,0.0) -> 1.0
1207 ci->replaceAllUsesWith(ConstantFP::get(Ty,1.0));
1208 ci->eraseFromParent();
1211 else if (Op2V == 0.5)
1213 // pow(x,0.5) -> sqrt(x)
1214 CallInst* sqrt_inst = new CallInst(SLC.get_sqrt(), base,
1215 ci->getName()+".pow",ci);
1216 ci->replaceAllUsesWith(sqrt_inst);
1217 ci->eraseFromParent();
1220 else if (Op2V == 1.0)
1223 ci->replaceAllUsesWith(base);
1224 ci->eraseFromParent();
1227 else if (Op2V == -1.0)
1229 // pow(x,-1.0) -> 1.0/x
1230 BinaryOperator* div_inst= BinaryOperator::create(Instruction::Div,
1231 ConstantFP::get(Ty,1.0), base, ci->getName()+".pow", ci);
1232 ci->replaceAllUsesWith(div_inst);
1233 ci->eraseFromParent();
1237 return false; // opt failed
1241 /// This LibCallOptimization will simplify calls to the "fprintf" library
1242 /// function. It looks for cases where the result of fprintf is not used and the
1243 /// operation can be reduced to something simpler.
1244 /// @brief Simplify the pow library function.
1245 struct FPrintFOptimization : public LibCallOptimization
1248 /// @brief Default Constructor
1249 FPrintFOptimization() : LibCallOptimization("fprintf",
1250 "Number of 'fprintf' calls simplified") {}
1252 /// @brief Destructor
1253 virtual ~FPrintFOptimization() {}
1255 /// @brief Make sure that the "fprintf" function has the right prototype
1256 virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC)
1258 // Just make sure this has at least 2 arguments
1259 return (f->arg_size() >= 2);
1262 /// @brief Perform the fprintf optimization.
1263 virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC)
1265 // If the call has more than 3 operands, we can't optimize it
1266 if (ci->getNumOperands() > 4 || ci->getNumOperands() <= 2)
1269 // If the result of the fprintf call is used, none of these optimizations
1271 if (!ci->hasNUses(0))
1274 // All the optimizations depend on the length of the second argument and the
1275 // fact that it is a constant string array. Check that now
1277 ConstantArray* CA = 0;
1278 if (!getConstantStringLength(ci->getOperand(2), len, &CA))
1281 if (ci->getNumOperands() == 3)
1283 // Make sure there's no % in the constant array
1284 for (unsigned i = 0; i < len; ++i)
1286 if (ConstantInt* CI = dyn_cast<ConstantInt>(CA->getOperand(i)))
1288 // Check for the null terminator
1289 if (CI->getRawValue() == '%')
1290 return false; // we found end of string
1296 // fprintf(file,fmt) -> fwrite(fmt,strlen(fmt),1file)
1297 const Type* FILEptr_type = ci->getOperand(1)->getType();
1298 Function* fwrite_func = SLC.get_fwrite(FILEptr_type);
1301 std::vector<Value*> args;
1302 args.push_back(ci->getOperand(2));
1303 args.push_back(ConstantUInt::get(SLC.getIntPtrType(),len));
1304 args.push_back(ConstantUInt::get(SLC.getIntPtrType(),1));
1305 args.push_back(ci->getOperand(1));
1306 new CallInst(fwrite_func,args,ci->getName(),ci);
1307 ci->replaceAllUsesWith(ConstantSInt::get(Type::IntTy,len));
1308 ci->eraseFromParent();
1312 // The remaining optimizations require the format string to be length 2
1317 // The first character has to be a %
1318 if (ConstantInt* CI = dyn_cast<ConstantInt>(CA->getOperand(0)))
1319 if (CI->getRawValue() != '%')
1322 // Get the second character and switch on its value
1323 ConstantInt* CI = dyn_cast<ConstantInt>(CA->getOperand(1));
1324 switch (CI->getRawValue())
1329 ConstantArray* CA = 0;
1330 if (!getConstantStringLength(ci->getOperand(3), len, &CA))
1333 // fprintf(file,"%s",str) -> fwrite(fmt,strlen(fmt),1,file)
1334 const Type* FILEptr_type = ci->getOperand(1)->getType();
1335 Function* fwrite_func = SLC.get_fwrite(FILEptr_type);
1338 std::vector<Value*> args;
1339 args.push_back(ci->getOperand(3));
1340 args.push_back(ConstantUInt::get(SLC.getIntPtrType(),len));
1341 args.push_back(ConstantUInt::get(SLC.getIntPtrType(),1));
1342 args.push_back(ci->getOperand(1));
1343 new CallInst(fwrite_func,args,ci->getName(),ci);
1344 ci->replaceAllUsesWith(ConstantSInt::get(Type::IntTy,len));
1349 ConstantInt* CI = dyn_cast<ConstantInt>(ci->getOperand(3));
1353 const Type* FILEptr_type = ci->getOperand(1)->getType();
1354 Function* fputc_func = SLC.get_fputc(FILEptr_type);
1357 CastInst* cast = new CastInst(CI,Type::IntTy,CI->getName()+".int",ci);
1358 new CallInst(fputc_func,cast,ci->getOperand(1),"",ci);
1359 ci->replaceAllUsesWith(ConstantSInt::get(Type::IntTy,1));
1365 ci->eraseFromParent();
1371 /// This LibCallOptimization will simplify calls to the "sprintf" library
1372 /// function. It looks for cases where the result of sprintf is not used and the
1373 /// operation can be reduced to something simpler.
1374 /// @brief Simplify the pow library function.
1375 struct SPrintFOptimization : public LibCallOptimization
1378 /// @brief Default Constructor
1379 SPrintFOptimization() : LibCallOptimization("sprintf",
1380 "Number of 'sprintf' calls simplified") {}
1382 /// @brief Destructor
1383 virtual ~SPrintFOptimization() {}
1385 /// @brief Make sure that the "fprintf" function has the right prototype
1386 virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC)
1388 // Just make sure this has at least 2 arguments
1389 return (f->getReturnType() == Type::IntTy && f->arg_size() >= 2);
1392 /// @brief Perform the sprintf optimization.
1393 virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC)
1395 // If the call has more than 3 operands, we can't optimize it
1396 if (ci->getNumOperands() > 4 || ci->getNumOperands() < 3)
1399 // All the optimizations depend on the length of the second argument and the
1400 // fact that it is a constant string array. Check that now
1402 ConstantArray* CA = 0;
1403 if (!getConstantStringLength(ci->getOperand(2), len, &CA))
1406 if (ci->getNumOperands() == 3)
1410 // If the length is 0, we just need to store a null byte
1411 new StoreInst(ConstantInt::get(Type::SByteTy,0),ci->getOperand(1),ci);
1412 ci->replaceAllUsesWith(ConstantSInt::get(Type::IntTy,0));
1413 ci->eraseFromParent();
1417 // Make sure there's no % in the constant array
1418 for (unsigned i = 0; i < len; ++i)
1420 if (ConstantInt* CI = dyn_cast<ConstantInt>(CA->getOperand(i)))
1422 // Check for the null terminator
1423 if (CI->getRawValue() == '%')
1424 return false; // we found a %, can't optimize
1427 return false; // initializer is not constant int, can't optimize
1430 // Increment length because we want to copy the null byte too
1433 // sprintf(str,fmt) -> llvm.memcpy(str,fmt,strlen(fmt),1)
1434 Function* memcpy_func = SLC.get_memcpy();
1437 std::vector<Value*> args;
1438 args.push_back(ci->getOperand(1));
1439 args.push_back(ci->getOperand(2));
1440 args.push_back(ConstantUInt::get(Type::UIntTy,len));
1441 args.push_back(ConstantUInt::get(Type::UIntTy,1));
1442 new CallInst(memcpy_func,args,"",ci);
1443 ci->replaceAllUsesWith(ConstantSInt::get(Type::IntTy,len));
1444 ci->eraseFromParent();
1448 // The remaining optimizations require the format string to be length 2
1453 // The first character has to be a %
1454 if (ConstantInt* CI = dyn_cast<ConstantInt>(CA->getOperand(0)))
1455 if (CI->getRawValue() != '%')
1458 // Get the second character and switch on its value
1459 ConstantInt* CI = dyn_cast<ConstantInt>(CA->getOperand(1));
1460 switch (CI->getRawValue())
1465 if (ci->hasNUses(0))
1467 // sprintf(dest,"%s",str) -> strcpy(dest,str)
1468 Function* strcpy_func = SLC.get_strcpy();
1471 std::vector<Value*> args;
1472 args.push_back(ci->getOperand(1));
1473 args.push_back(ci->getOperand(3));
1474 new CallInst(strcpy_func,args,"",ci);
1476 else if (getConstantStringLength(ci->getOperand(3),len))
1478 // sprintf(dest,"%s",cstr) -> llvm.memcpy(dest,str,strlen(str),1)
1479 len++; // get the null-terminator
1480 Function* memcpy_func = SLC.get_memcpy();
1483 std::vector<Value*> args;
1484 args.push_back(ci->getOperand(1));
1485 args.push_back(ci->getOperand(3));
1486 args.push_back(ConstantUInt::get(Type::UIntTy,len));
1487 args.push_back(ConstantUInt::get(Type::UIntTy,1));
1488 new CallInst(memcpy_func,args,"",ci);
1489 ci->replaceAllUsesWith(ConstantSInt::get(Type::IntTy,len));
1495 // sprintf(dest,"%c",chr) -> store chr, dest
1497 new CastInst(ci->getOperand(3),Type::SByteTy,"char",ci);
1498 new StoreInst(cast, ci->getOperand(1), ci);
1499 GetElementPtrInst* gep = new GetElementPtrInst(ci->getOperand(1),
1500 ConstantUInt::get(Type::UIntTy,1),ci->getOperand(1)->getName()+".end",
1502 new StoreInst(ConstantInt::get(Type::SByteTy,0),gep,ci);
1503 ci->replaceAllUsesWith(ConstantSInt::get(Type::IntTy,1));
1509 ci->eraseFromParent();
1514 /// This LibCallOptimization will simplify calls to the "fputs" library
1515 /// function. It looks for cases where the result of fputs is not used and the
1516 /// operation can be reduced to something simpler.
1517 /// @brief Simplify the pow library function.
1518 struct PutsOptimization : public LibCallOptimization
1521 /// @brief Default Constructor
1522 PutsOptimization() : LibCallOptimization("fputs",
1523 "Number of 'fputs' calls simplified") {}
1525 /// @brief Destructor
1526 virtual ~PutsOptimization() {}
1528 /// @brief Make sure that the "fputs" function has the right prototype
1529 virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC)
1531 // Just make sure this has 2 arguments
1532 return (f->arg_size() == 2);
1535 /// @brief Perform the fputs optimization.
1536 virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC)
1538 // If the result is used, none of these optimizations work
1539 if (!ci->hasNUses(0))
1542 // All the optimizations depend on the length of the first argument and the
1543 // fact that it is a constant string array. Check that now
1545 if (!getConstantStringLength(ci->getOperand(1), len))
1551 // fputs("",F) -> noop
1555 // fputs(s,F) -> fputc(s[0],F) (if s is constant and strlen(s) == 1)
1556 const Type* FILEptr_type = ci->getOperand(2)->getType();
1557 Function* fputc_func = SLC.get_fputc(FILEptr_type);
1560 LoadInst* loadi = new LoadInst(ci->getOperand(1),
1561 ci->getOperand(1)->getName()+".byte",ci);
1562 CastInst* casti = new CastInst(loadi,Type::IntTy,
1563 loadi->getName()+".int",ci);
1564 new CallInst(fputc_func,casti,ci->getOperand(2),"",ci);
1569 // fputs(s,F) -> fwrite(s,1,len,F) (if s is constant and strlen(s) > 1)
1570 const Type* FILEptr_type = ci->getOperand(2)->getType();
1571 Function* fwrite_func = SLC.get_fwrite(FILEptr_type);
1574 std::vector<Value*> parms;
1575 parms.push_back(ci->getOperand(1));
1576 parms.push_back(ConstantUInt::get(SLC.getIntPtrType(),len));
1577 parms.push_back(ConstantUInt::get(SLC.getIntPtrType(),1));
1578 parms.push_back(ci->getOperand(2));
1579 new CallInst(fwrite_func,parms,"",ci);
1583 ci->eraseFromParent();
1584 return true; // success
1588 /// This LibCallOptimization will simplify calls to the "isdigit" library
1589 /// function. It simply does range checks the parameter explicitly.
1590 /// @brief Simplify the isdigit library function.
1591 struct IsDigitOptimization : public LibCallOptimization
1594 /// @brief Default Constructor
1595 IsDigitOptimization() : LibCallOptimization("isdigit",
1596 "Number of 'isdigit' calls simplified") {}
1598 /// @brief Destructor
1599 virtual ~IsDigitOptimization() {}
1601 /// @brief Make sure that the "fputs" function has the right prototype
1602 virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC)
1604 // Just make sure this has 1 argument
1605 return (f->arg_size() == 1);
1608 /// @brief Perform the toascii optimization.
1609 virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC)
1611 if (ConstantInt* CI = dyn_cast<ConstantInt>(ci->getOperand(1)))
1613 // isdigit(c) -> 0 or 1, if 'c' is constant
1614 uint64_t val = CI->getRawValue();
1615 if (val >= '0' && val <='9')
1616 ci->replaceAllUsesWith(ConstantSInt::get(Type::IntTy,1));
1618 ci->replaceAllUsesWith(ConstantSInt::get(Type::IntTy,0));
1619 ci->eraseFromParent();
1623 // isdigit(c) -> (unsigned)c - '0' <= 9
1625 new CastInst(ci->getOperand(1),Type::UIntTy,
1626 ci->getOperand(1)->getName()+".uint",ci);
1627 BinaryOperator* sub_inst = BinaryOperator::create(Instruction::Sub,cast,
1628 ConstantUInt::get(Type::UIntTy,0x30),
1629 ci->getOperand(1)->getName()+".sub",ci);
1630 SetCondInst* setcond_inst = new SetCondInst(Instruction::SetLE,sub_inst,
1631 ConstantUInt::get(Type::UIntTy,9),
1632 ci->getOperand(1)->getName()+".cmp",ci);
1634 new CastInst(setcond_inst,Type::IntTy,
1635 ci->getOperand(1)->getName()+".isdigit",ci);
1636 ci->replaceAllUsesWith(c2);
1637 ci->eraseFromParent();
1642 /// This LibCallOptimization will simplify calls to the "toascii" library
1643 /// function. It simply does the corresponding and operation to restrict the
1644 /// range of values to the ASCII character set (0-127).
1645 /// @brief Simplify the toascii library function.
1646 struct ToAsciiOptimization : public LibCallOptimization
1649 /// @brief Default Constructor
1650 ToAsciiOptimization() : LibCallOptimization("toascii",
1651 "Number of 'toascii' calls simplified") {}
1653 /// @brief Destructor
1654 virtual ~ToAsciiOptimization() {}
1656 /// @brief Make sure that the "fputs" function has the right prototype
1657 virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC)
1659 // Just make sure this has 2 arguments
1660 return (f->arg_size() == 1);
1663 /// @brief Perform the toascii optimization.
1664 virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC)
1666 // toascii(c) -> (c & 0x7f)
1667 Value* chr = ci->getOperand(1);
1668 BinaryOperator* and_inst = BinaryOperator::create(Instruction::And,chr,
1669 ConstantInt::get(chr->getType(),0x7F),ci->getName()+".toascii",ci);
1670 ci->replaceAllUsesWith(and_inst);
1671 ci->eraseFromParent();
1676 #if defined(HAVE_FFSLL)
1677 /// This LibCallOptimization will simplify calls to the "ffs" library
1678 /// calls which find the first set bit in an int, long, or long long. The
1679 /// optimization is to compute the result at compile time if the argument is
1681 /// @brief Simplify the ffs library function.
1682 struct FFSOptimization : public LibCallOptimization
1685 /// @brief Subclass Constructor
1686 FFSOptimization(const char* funcName, const char* description)
1687 : LibCallOptimization(funcName, description)
1691 /// @brief Default Constructor
1692 FFSOptimization() : LibCallOptimization("ffs",
1693 "Number of 'ffs' calls simplified") {}
1695 /// @brief Destructor
1696 virtual ~FFSOptimization() {}
1698 /// @brief Make sure that the "fputs" function has the right prototype
1699 virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC)
1701 // Just make sure this has 2 arguments
1702 return (f->arg_size() == 1 && f->getReturnType() == Type::IntTy);
1705 /// @brief Perform the ffs optimization.
1706 virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC)
1708 if (ConstantInt* CI = dyn_cast<ConstantInt>(ci->getOperand(1)))
1710 // ffs(cnst) -> bit#
1711 // ffsl(cnst) -> bit#
1712 uint64_t val = CI->getRawValue();
1713 int result = ffsll(static_cast<long long>(val));
1714 ci->replaceAllUsesWith(ConstantSInt::get(Type::IntTy, result));
1715 ci->eraseFromParent();
1722 /// This LibCallOptimization will simplify calls to the "ffsl" library
1723 /// calls. It simply uses FFSOptimization for which the transformation is
1725 /// @brief Simplify the ffsl library function.
1726 struct FFSLOptimization : public FFSOptimization
1729 /// @brief Default Constructor
1730 FFSLOptimization() : FFSOptimization("ffsl",
1731 "Number of 'ffsl' calls simplified") {}
1735 /// This LibCallOptimization will simplify calls to the "ffsll" library
1736 /// calls. It simply uses FFSOptimization for which the transformation is
1738 /// @brief Simplify the ffsl library function.
1739 struct FFSLLOptimization : public FFSOptimization
1742 /// @brief Default Constructor
1743 FFSLLOptimization() : FFSOptimization("ffsll",
1744 "Number of 'ffsll' calls simplified") {}
1750 /// A function to compute the length of a null-terminated constant array of
1751 /// integers. This function can't rely on the size of the constant array
1752 /// because there could be a null terminator in the middle of the array.
1753 /// We also have to bail out if we find a non-integer constant initializer
1754 /// of one of the elements or if there is no null-terminator. The logic
1755 /// below checks each of these conditions and will return true only if all
1756 /// conditions are met. In that case, the \p len parameter is set to the length
1757 /// of the null-terminated string. If false is returned, the conditions were
1758 /// not met and len is set to 0.
1759 /// @brief Get the length of a constant string (null-terminated array).
1760 bool getConstantStringLength(Value* V, uint64_t& len, ConstantArray** CA )
1762 assert(V != 0 && "Invalid args to getConstantStringLength");
1763 len = 0; // make sure we initialize this
1765 // If the value is not a GEP instruction nor a constant expression with a
1766 // GEP instruction, then return false because ConstantArray can't occur
1768 if (GetElementPtrInst* GEPI = dyn_cast<GetElementPtrInst>(V))
1770 else if (ConstantExpr* CE = dyn_cast<ConstantExpr>(V))
1771 if (CE->getOpcode() == Instruction::GetElementPtr)
1778 // Make sure the GEP has exactly three arguments.
1779 if (GEP->getNumOperands() != 3)
1782 // Check to make sure that the first operand of the GEP is an integer and
1783 // has value 0 so that we are sure we're indexing into the initializer.
1784 if (ConstantInt* op1 = dyn_cast<ConstantInt>(GEP->getOperand(1)))
1786 if (!op1->isNullValue())
1792 // Ensure that the second operand is a ConstantInt. If it isn't then this
1793 // GEP is wonky and we're not really sure what were referencing into and
1794 // better of not optimizing it. While we're at it, get the second index
1795 // value. We'll need this later for indexing the ConstantArray.
1796 uint64_t start_idx = 0;
1797 if (ConstantInt* CI = dyn_cast<ConstantInt>(GEP->getOperand(2)))
1798 start_idx = CI->getRawValue();
1802 // The GEP instruction, constant or instruction, must reference a global
1803 // variable that is a constant and is initialized. The referenced constant
1804 // initializer is the array that we'll use for optimization.
1805 GlobalVariable* GV = dyn_cast<GlobalVariable>(GEP->getOperand(0));
1806 if (!GV || !GV->isConstant() || !GV->hasInitializer())
1809 // Get the initializer.
1810 Constant* INTLZR = GV->getInitializer();
1812 // Handle the ConstantAggregateZero case
1813 if (ConstantAggregateZero* CAZ = dyn_cast<ConstantAggregateZero>(INTLZR))
1815 // This is a degenerate case. The initializer is constant zero so the
1816 // length of the string must be zero.
1821 // Must be a Constant Array
1822 ConstantArray* A = dyn_cast<ConstantArray>(INTLZR);
1826 // Get the number of elements in the array
1827 uint64_t max_elems = A->getType()->getNumElements();
1829 // Traverse the constant array from start_idx (derived above) which is
1830 // the place the GEP refers to in the array.
1831 for ( len = start_idx; len < max_elems; len++)
1833 if (ConstantInt* CI = dyn_cast<ConstantInt>(A->getOperand(len)))
1835 // Check for the null terminator
1836 if (CI->isNullValue())
1837 break; // we found end of string
1840 return false; // This array isn't suitable, non-int initializer
1842 if (len >= max_elems)
1843 return false; // This array isn't null terminated
1845 // Subtract out the initial value from the length
1849 return true; // success!
1853 // Additional cases that we need to add to this file:
1856 // * cbrt(expN(X)) -> expN(x/3)
1857 // * cbrt(sqrt(x)) -> pow(x,1/6)
1858 // * cbrt(sqrt(x)) -> pow(x,1/9)
1861 // * cos(-x) -> cos(x)
1864 // * exp(log(x)) -> x
1867 // * isascii(c) -> ((c & ~0x7f) == 0)
1870 // * isdigit(c) -> (unsigned)(c) - '0' <= 9
1873 // * log(exp(x)) -> x
1874 // * log(x**y) -> y*log(x)
1875 // * log(exp(y)) -> y*log(e)
1876 // * log(exp2(y)) -> y*log(2)
1877 // * log(exp10(y)) -> y*log(10)
1878 // * log(sqrt(x)) -> 0.5*log(x)
1879 // * log(pow(x,y)) -> y*log(x)
1881 // lround, lroundf, lroundl:
1882 // * lround(cnst) -> cnst'
1885 // * memcmp(s1,s2,0) -> 0
1886 // * memcmp(x,x,l) -> 0
1887 // * memcmp(x,y,l) -> cnst
1888 // (if all arguments are constant and strlen(x) <= l and strlen(y) <= l)
1889 // * memcmp(x,y,1) -> *x - *y
1892 // * memmove(d,s,l,a) -> memcpy(d,s,l,a)
1893 // (if s is a global constant array)
1896 // * pow(exp(x),y) -> exp(x*y)
1897 // * pow(sqrt(x),y) -> pow(x,y*0.5)
1898 // * pow(pow(x,y),z)-> pow(x,y*z)
1901 // * puts("") -> fputc("\n",stdout) (how do we get "stdout"?)
1903 // round, roundf, roundl:
1904 // * round(cnst) -> cnst'
1907 // * signbit(cnst) -> cnst'
1908 // * signbit(nncst) -> 0 (if pstv is a non-negative constant)
1910 // sqrt, sqrtf, sqrtl:
1911 // * sqrt(expN(x)) -> expN(x*0.5)
1912 // * sqrt(Nroot(x)) -> pow(x,1/(2*N))
1913 // * sqrt(pow(x,y)) -> pow(|x|,y*0.5)
1916 // * stpcpy(str, "literal") ->
1917 // llvm.memcpy(str,"literal",strlen("literal")+1,1)
1919 // * strrchr(s,c) -> reverse_offset_of_in(c,s)
1920 // (if c is a constant integer and s is a constant string)
1921 // * strrchr(s1,0) -> strchr(s1,0)
1924 // * strncat(x,y,0) -> x
1925 // * strncat(x,y,0) -> x (if strlen(y) = 0)
1926 // * strncat(x,y,l) -> strcat(x,y) (if y and l are constants an l > strlen(y))
1929 // * strncpy(d,s,0) -> d
1930 // * strncpy(d,s,l) -> memcpy(d,s,l,1)
1931 // (if s and l are constants)
1934 // * strpbrk(s,a) -> offset_in_for(s,a)
1935 // (if s and a are both constant strings)
1936 // * strpbrk(s,"") -> 0
1937 // * strpbrk(s,a) -> strchr(s,a[0]) (if a is constant string of length 1)
1940 // * strspn(s,a) -> const_int (if both args are constant)
1941 // * strspn("",a) -> 0
1942 // * strspn(s,"") -> 0
1943 // * strcspn(s,a) -> const_int (if both args are constant)
1944 // * strcspn("",a) -> 0
1945 // * strcspn(s,"") -> strlen(a)
1948 // * strstr(x,x) -> x
1949 // * strstr(s1,s2) -> offset_of_s2_in(s1)
1950 // (if s1 and s2 are constant strings)
1953 // * tan(atan(x)) -> x
1955 // trunc, truncf, truncl:
1956 // * trunc(cnst) -> cnst'