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"
35 /// This statistic keeps track of the total number of library calls that have
36 /// been simplified regardless of which call it is.
37 Statistic<> SimplifiedLibCalls("simplify-libcalls",
38 "Number of well-known library calls simplified");
40 // Forward declarations
41 class LibCallOptimization;
42 class SimplifyLibCalls;
44 /// @brief The list of optimizations deriving from LibCallOptimization
45 hash_map<std::string,LibCallOptimization*> optlist;
47 /// This class is the abstract base class for the set of optimizations that
48 /// corresponds to one library call. The SimplifyLibCalls pass will call the
49 /// ValidateCalledFunction method to ask the optimization if a given Function
50 /// is the kind that the optimization can handle. If the subclass returns true,
51 /// then SImplifyLibCalls will also call the OptimizeCall method to perform,
52 /// or attempt to perform, the optimization(s) for the library call. Otherwise,
53 /// OptimizeCall won't be called. Subclasses are responsible for providing the
54 /// name of the library call (strlen, strcpy, etc.) to the LibCallOptimization
55 /// constructor. This is used to efficiently select which call instructions to
56 /// optimize. The criteria for a "lib call" is "anything with well known
57 /// semantics", typically a library function that is defined by an international
58 /// standard. Because the semantics are well known, the optimizations can
59 /// generally short-circuit actually calling the function if there's a simpler
60 /// way (e.g. strlen(X) can be reduced to a constant if X is a constant global).
61 /// @brief Base class for library call optimizations
62 class LibCallOptimization
65 /// The \p fname argument must be the name of the library function being
66 /// optimized by the subclass.
67 /// @brief Constructor that registers the optimization.
68 LibCallOptimization(const char* fname,
69 const char* stat_name, const char* description )
72 , occurrences(stat_name,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 "simplify-libcalls:exit","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 "simplify-libcalls:strcat","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 "simplify-libcalls:strchr","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 "simplify-libcalls:strcmp","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 "simplify-libcalls:strncmp","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 "simplify-libcalls:strcpy","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 "simplify-libcalls:strlen","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 // Get the length of the string
921 if (!getConstantStringLength(ci->getOperand(1),len))
924 ci->replaceAllUsesWith(
925 ConstantInt::get(SLC.getTargetData()->getIntPtrType(),len));
926 ci->eraseFromParent();
931 /// This LibCallOptimization will simplify a call to the memcpy library
932 /// function by expanding it out to a single store of size 0, 1, 2, 4, or 8
933 /// bytes depending on the length of the string and the alignment. Additional
934 /// optimizations are possible in code generation (sequence of immediate store)
935 /// @brief Simplify the memcpy library function.
936 struct LLVMMemCpyOptimization : public LibCallOptimization
938 /// @brief Default Constructor
939 LLVMMemCpyOptimization() : LibCallOptimization("llvm.memcpy",
940 "simplify-libcalls:llvm.memcpy",
941 "Number of 'llvm.memcpy' calls simplified") {}
944 /// @brief Subclass Constructor
945 LLVMMemCpyOptimization(const char* fname, const char* sname, const char* desc)
946 : LibCallOptimization(fname, sname, desc) {}
948 /// @brief Destructor
949 virtual ~LLVMMemCpyOptimization() {}
951 /// @brief Make sure that the "memcpy" function has the right prototype
952 virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& TD)
954 // Just make sure this has 4 arguments per LLVM spec.
955 return (f->arg_size() == 4);
958 /// Because of alignment and instruction information that we don't have, we
959 /// leave the bulk of this to the code generators. The optimization here just
960 /// deals with a few degenerate cases where the length of the string and the
961 /// alignment match the sizes of our intrinsic types so we can do a load and
962 /// store instead of the memcpy call.
963 /// @brief Perform the memcpy optimization.
964 virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& TD)
966 // Make sure we have constant int values to work with
967 ConstantInt* LEN = dyn_cast<ConstantInt>(ci->getOperand(3));
970 ConstantInt* ALIGN = dyn_cast<ConstantInt>(ci->getOperand(4));
974 // If the length is larger than the alignment, we can't optimize
975 uint64_t len = LEN->getRawValue();
976 uint64_t alignment = ALIGN->getRawValue();
978 alignment = 1; // Alignment 0 is identity for alignment 1
982 // Get the type we will cast to, based on size of the string
983 Value* dest = ci->getOperand(1);
984 Value* src = ci->getOperand(2);
989 // memcpy(d,s,0,a) -> noop
990 ci->eraseFromParent();
992 case 1: castType = Type::SByteTy; break;
993 case 2: castType = Type::ShortTy; break;
994 case 4: castType = Type::IntTy; break;
995 case 8: castType = Type::LongTy; break;
1000 // Cast source and dest to the right sized primitive and then load/store
1002 new CastInst(src,PointerType::get(castType),src->getName()+".cast",ci);
1003 CastInst* DestCast =
1004 new CastInst(dest,PointerType::get(castType),dest->getName()+".cast",ci);
1005 LoadInst* LI = new LoadInst(SrcCast,SrcCast->getName()+".val",ci);
1006 StoreInst* SI = new StoreInst(LI, DestCast, ci);
1007 ci->eraseFromParent();
1010 } LLVMMemCpyOptimizer;
1012 /// This LibCallOptimization will simplify a call to the memmove library
1013 /// function. It is identical to MemCopyOptimization except for the name of
1015 /// @brief Simplify the memmove library function.
1016 struct LLVMMemMoveOptimization : public LLVMMemCpyOptimization
1018 /// @brief Default Constructor
1019 LLVMMemMoveOptimization() : LLVMMemCpyOptimization("llvm.memmove",
1020 "simplify-libcalls:llvm.memmove",
1021 "Number of 'llvm.memmove' calls simplified") {}
1023 } LLVMMemMoveOptimizer;
1025 /// This LibCallOptimization will simplify a call to the memset library
1026 /// function by expanding it out to a single store of size 0, 1, 2, 4, or 8
1027 /// bytes depending on the length argument.
1028 struct LLVMMemSetOptimization : public LibCallOptimization
1030 /// @brief Default Constructor
1031 LLVMMemSetOptimization() : LibCallOptimization("llvm.memset",
1032 "simplify-libcalls:llvm.memset",
1033 "Number of 'llvm.memset' calls simplified") {}
1036 /// @brief Destructor
1037 virtual ~LLVMMemSetOptimization() {}
1039 /// @brief Make sure that the "memset" function has the right prototype
1040 virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& TD)
1042 // Just make sure this has 3 arguments per LLVM spec.
1043 return (f->arg_size() == 4);
1046 /// Because of alignment and instruction information that we don't have, we
1047 /// leave the bulk of this to the code generators. The optimization here just
1048 /// deals with a few degenerate cases where the length parameter is constant
1049 /// and the alignment matches the sizes of our intrinsic types so we can do
1050 /// store instead of the memcpy call. Other calls are transformed into the
1051 /// llvm.memset intrinsic.
1052 /// @brief Perform the memset optimization.
1053 virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& TD)
1055 // Make sure we have constant int values to work with
1056 ConstantInt* LEN = dyn_cast<ConstantInt>(ci->getOperand(3));
1059 ConstantInt* ALIGN = dyn_cast<ConstantInt>(ci->getOperand(4));
1063 // Extract the length and alignment
1064 uint64_t len = LEN->getRawValue();
1065 uint64_t alignment = ALIGN->getRawValue();
1067 // Alignment 0 is identity for alignment 1
1071 // If the length is zero, this is a no-op
1074 // memset(d,c,0,a) -> noop
1075 ci->eraseFromParent();
1079 // If the length is larger than the alignment, we can't optimize
1080 if (len > alignment)
1083 // Make sure we have a constant ubyte to work with so we can extract
1084 // the value to be filled.
1085 ConstantUInt* FILL = dyn_cast<ConstantUInt>(ci->getOperand(2));
1088 if (FILL->getType() != Type::UByteTy)
1091 // memset(s,c,n) -> store s, c (for n=1,2,4,8)
1093 // Extract the fill character
1094 uint64_t fill_char = FILL->getValue();
1095 uint64_t fill_value = fill_char;
1097 // Get the type we will cast to, based on size of memory area to fill, and
1098 // and the value we will store there.
1099 Value* dest = ci->getOperand(1);
1104 castType = Type::UByteTy;
1107 castType = Type::UShortTy;
1108 fill_value |= fill_char << 8;
1111 castType = Type::UIntTy;
1112 fill_value |= fill_char << 8 | fill_char << 16 | fill_char << 24;
1115 castType = Type::ULongTy;
1116 fill_value |= fill_char << 8 | fill_char << 16 | fill_char << 24;
1117 fill_value |= fill_char << 32 | fill_char << 40 | fill_char << 48;
1118 fill_value |= fill_char << 56;
1124 // Cast dest to the right sized primitive and then load/store
1125 CastInst* DestCast =
1126 new CastInst(dest,PointerType::get(castType),dest->getName()+".cast",ci);
1127 new StoreInst(ConstantUInt::get(castType,fill_value),DestCast, ci);
1128 ci->eraseFromParent();
1131 } LLVMMemSetOptimizer;
1133 /// This LibCallOptimization will simplify calls to the "pow" library
1134 /// function. It looks for cases where the result of pow is well known and
1135 /// substitutes the appropriate value.
1136 /// @brief Simplify the pow library function.
1137 struct PowOptimization : public LibCallOptimization
1140 /// @brief Default Constructor
1141 PowOptimization() : LibCallOptimization("pow",
1142 "simplify-libcalls:pow", "Number of 'pow' calls simplified") {}
1144 /// @brief Destructor
1145 virtual ~PowOptimization() {}
1147 /// @brief Make sure that the "pow" function has the right prototype
1148 virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC)
1150 // Just make sure this has 2 arguments
1151 return (f->arg_size() == 2);
1154 /// @brief Perform the pow optimization.
1155 virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC)
1157 const Type *Ty = cast<Function>(ci->getOperand(0))->getReturnType();
1158 Value* base = ci->getOperand(1);
1159 Value* expn = ci->getOperand(2);
1160 if (ConstantFP *Op1 = dyn_cast<ConstantFP>(base)) {
1161 double Op1V = Op1->getValue();
1164 // pow(1.0,x) -> 1.0
1165 ci->replaceAllUsesWith(ConstantFP::get(Ty,1.0));
1166 ci->eraseFromParent();
1170 else if (ConstantFP* Op2 = dyn_cast<ConstantFP>(expn))
1172 double Op2V = Op2->getValue();
1175 // pow(x,0.0) -> 1.0
1176 ci->replaceAllUsesWith(ConstantFP::get(Ty,1.0));
1177 ci->eraseFromParent();
1180 else if (Op2V == 0.5)
1182 // pow(x,0.5) -> sqrt(x)
1183 CallInst* sqrt_inst = new CallInst(SLC.get_sqrt(), base,
1184 ci->getName()+".pow",ci);
1185 ci->replaceAllUsesWith(sqrt_inst);
1186 ci->eraseFromParent();
1189 else if (Op2V == 1.0)
1192 ci->replaceAllUsesWith(base);
1193 ci->eraseFromParent();
1196 else if (Op2V == -1.0)
1198 // pow(x,-1.0) -> 1.0/x
1199 BinaryOperator* div_inst= BinaryOperator::create(Instruction::Div,
1200 ConstantFP::get(Ty,1.0), base, ci->getName()+".pow", ci);
1201 ci->replaceAllUsesWith(div_inst);
1202 ci->eraseFromParent();
1206 return false; // opt failed
1210 /// This LibCallOptimization will simplify calls to the "fprintf" library
1211 /// function. It looks for cases where the result of fprintf is not used and the
1212 /// operation can be reduced to something simpler.
1213 /// @brief Simplify the pow library function.
1214 struct FPrintFOptimization : public LibCallOptimization
1217 /// @brief Default Constructor
1218 FPrintFOptimization() : LibCallOptimization("fprintf",
1219 "simplify-libcalls:fprintf", "Number of 'fprintf' calls simplified") {}
1221 /// @brief Destructor
1222 virtual ~FPrintFOptimization() {}
1224 /// @brief Make sure that the "fprintf" function has the right prototype
1225 virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC)
1227 // Just make sure this has at least 2 arguments
1228 return (f->arg_size() >= 2);
1231 /// @brief Perform the fprintf optimization.
1232 virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC)
1234 // If the call has more than 3 operands, we can't optimize it
1235 if (ci->getNumOperands() > 4 || ci->getNumOperands() <= 2)
1238 // If the result of the fprintf call is used, none of these optimizations
1240 if (!ci->hasNUses(0))
1243 // All the optimizations depend on the length of the second argument and the
1244 // fact that it is a constant string array. Check that now
1246 ConstantArray* CA = 0;
1247 if (!getConstantStringLength(ci->getOperand(2), len, &CA))
1250 if (ci->getNumOperands() == 3)
1252 // Make sure there's no % in the constant array
1253 for (unsigned i = 0; i < len; ++i)
1255 if (ConstantInt* CI = dyn_cast<ConstantInt>(CA->getOperand(i)))
1257 // Check for the null terminator
1258 if (CI->getRawValue() == '%')
1259 return false; // we found end of string
1265 // fprintf(file,fmt) -> fwrite(fmt,strlen(fmt),1file)
1266 const Type* FILEptr_type = ci->getOperand(1)->getType();
1267 Function* fwrite_func = SLC.get_fwrite(FILEptr_type);
1270 std::vector<Value*> args;
1271 args.push_back(ci->getOperand(2));
1272 args.push_back(ConstantUInt::get(SLC.getIntPtrType(),len));
1273 args.push_back(ConstantUInt::get(SLC.getIntPtrType(),1));
1274 args.push_back(ci->getOperand(1));
1275 new CallInst(fwrite_func,args,ci->getName(),ci);
1276 ci->replaceAllUsesWith(ConstantSInt::get(Type::IntTy,len));
1277 ci->eraseFromParent();
1281 // The remaining optimizations require the format string to be length 2
1286 // The first character has to be a %
1287 if (ConstantInt* CI = dyn_cast<ConstantInt>(CA->getOperand(0)))
1288 if (CI->getRawValue() != '%')
1291 // Get the second character and switch on its value
1292 ConstantInt* CI = dyn_cast<ConstantInt>(CA->getOperand(1));
1293 switch (CI->getRawValue())
1298 ConstantArray* CA = 0;
1299 if (!getConstantStringLength(ci->getOperand(3), len, &CA))
1302 // fprintf(file,"%s",str) -> fwrite(fmt,strlen(fmt),1,file)
1303 const Type* FILEptr_type = ci->getOperand(1)->getType();
1304 Function* fwrite_func = SLC.get_fwrite(FILEptr_type);
1307 std::vector<Value*> args;
1308 args.push_back(ci->getOperand(3));
1309 args.push_back(ConstantUInt::get(SLC.getIntPtrType(),len));
1310 args.push_back(ConstantUInt::get(SLC.getIntPtrType(),1));
1311 args.push_back(ci->getOperand(1));
1312 new CallInst(fwrite_func,args,ci->getName(),ci);
1313 ci->replaceAllUsesWith(ConstantSInt::get(Type::IntTy,len));
1318 ConstantInt* CI = dyn_cast<ConstantInt>(ci->getOperand(3));
1322 const Type* FILEptr_type = ci->getOperand(1)->getType();
1323 Function* fputc_func = SLC.get_fputc(FILEptr_type);
1326 CastInst* cast = new CastInst(CI,Type::IntTy,CI->getName()+".int",ci);
1327 new CallInst(fputc_func,cast,ci->getOperand(1),"",ci);
1328 ci->replaceAllUsesWith(ConstantSInt::get(Type::IntTy,1));
1334 ci->eraseFromParent();
1340 /// This LibCallOptimization will simplify calls to the "sprintf" library
1341 /// function. It looks for cases where the result of sprintf is not used and the
1342 /// operation can be reduced to something simpler.
1343 /// @brief Simplify the pow library function.
1344 struct SPrintFOptimization : public LibCallOptimization
1347 /// @brief Default Constructor
1348 SPrintFOptimization() : LibCallOptimization("sprintf",
1349 "simplify-libcalls:sprintf", "Number of 'sprintf' calls simplified") {}
1351 /// @brief Destructor
1352 virtual ~SPrintFOptimization() {}
1354 /// @brief Make sure that the "fprintf" function has the right prototype
1355 virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC)
1357 // Just make sure this has at least 2 arguments
1358 return (f->getReturnType() == Type::IntTy && f->arg_size() >= 2);
1361 /// @brief Perform the sprintf optimization.
1362 virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC)
1364 // If the call has more than 3 operands, we can't optimize it
1365 if (ci->getNumOperands() > 4 || ci->getNumOperands() < 3)
1368 // All the optimizations depend on the length of the second argument and the
1369 // fact that it is a constant string array. Check that now
1371 ConstantArray* CA = 0;
1372 if (!getConstantStringLength(ci->getOperand(2), len, &CA))
1375 if (ci->getNumOperands() == 3)
1379 // If the length is 0, we just need to store a null byte
1380 new StoreInst(ConstantInt::get(Type::SByteTy,0),ci->getOperand(1),ci);
1381 ci->replaceAllUsesWith(ConstantSInt::get(Type::IntTy,0));
1382 ci->eraseFromParent();
1386 // Make sure there's no % in the constant array
1387 for (unsigned i = 0; i < len; ++i)
1389 if (ConstantInt* CI = dyn_cast<ConstantInt>(CA->getOperand(i)))
1391 // Check for the null terminator
1392 if (CI->getRawValue() == '%')
1393 return false; // we found a %, can't optimize
1396 return false; // initializer is not constant int, can't optimize
1399 // Increment length because we want to copy the null byte too
1402 // sprintf(str,fmt) -> llvm.memcpy(str,fmt,strlen(fmt),1)
1403 Function* memcpy_func = SLC.get_memcpy();
1406 std::vector<Value*> args;
1407 args.push_back(ci->getOperand(1));
1408 args.push_back(ci->getOperand(2));
1409 args.push_back(ConstantUInt::get(Type::UIntTy,len));
1410 args.push_back(ConstantUInt::get(Type::UIntTy,1));
1411 new CallInst(memcpy_func,args,"",ci);
1412 ci->replaceAllUsesWith(ConstantSInt::get(Type::IntTy,len));
1413 ci->eraseFromParent();
1417 // The remaining optimizations require the format string to be length 2
1422 // The first character has to be a %
1423 if (ConstantInt* CI = dyn_cast<ConstantInt>(CA->getOperand(0)))
1424 if (CI->getRawValue() != '%')
1427 // Get the second character and switch on its value
1428 ConstantInt* CI = dyn_cast<ConstantInt>(CA->getOperand(1));
1429 switch (CI->getRawValue())
1434 if (ci->hasNUses(0))
1436 // sprintf(dest,"%s",str) -> strcpy(dest,str)
1437 Function* strcpy_func = SLC.get_strcpy();
1440 std::vector<Value*> args;
1441 args.push_back(ci->getOperand(1));
1442 args.push_back(ci->getOperand(3));
1443 new CallInst(strcpy_func,args,"",ci);
1445 else if (getConstantStringLength(ci->getOperand(3),len))
1447 // sprintf(dest,"%s",cstr) -> llvm.memcpy(dest,str,strlen(str),1)
1448 len++; // get the null-terminator
1449 Function* memcpy_func = SLC.get_memcpy();
1452 std::vector<Value*> args;
1453 args.push_back(ci->getOperand(1));
1454 args.push_back(ci->getOperand(3));
1455 args.push_back(ConstantUInt::get(Type::UIntTy,len));
1456 args.push_back(ConstantUInt::get(Type::UIntTy,1));
1457 new CallInst(memcpy_func,args,"",ci);
1458 ci->replaceAllUsesWith(ConstantSInt::get(Type::IntTy,len));
1464 // sprintf(dest,"%c",chr) -> store chr, dest
1466 new CastInst(ci->getOperand(3),Type::SByteTy,"char",ci);
1467 new StoreInst(cast, ci->getOperand(1), ci);
1468 GetElementPtrInst* gep = new GetElementPtrInst(ci->getOperand(1),
1469 ConstantUInt::get(Type::UIntTy,1),ci->getOperand(1)->getName()+".end",
1471 new StoreInst(ConstantInt::get(Type::SByteTy,0),gep,ci);
1472 ci->replaceAllUsesWith(ConstantSInt::get(Type::IntTy,1));
1478 ci->eraseFromParent();
1483 /// This LibCallOptimization will simplify calls to the "fputs" library
1484 /// function. It looks for cases where the result of fputs is not used and the
1485 /// operation can be reduced to something simpler.
1486 /// @brief Simplify the pow library function.
1487 struct PutsOptimization : public LibCallOptimization
1490 /// @brief Default Constructor
1491 PutsOptimization() : LibCallOptimization("fputs",
1492 "simplify-libcalls:fputs", "Number of 'fputs' calls simplified") {}
1494 /// @brief Destructor
1495 virtual ~PutsOptimization() {}
1497 /// @brief Make sure that the "fputs" function has the right prototype
1498 virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC)
1500 // Just make sure this has 2 arguments
1501 return (f->arg_size() == 2);
1504 /// @brief Perform the fputs optimization.
1505 virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC)
1507 // If the result is used, none of these optimizations work
1508 if (!ci->hasNUses(0))
1511 // All the optimizations depend on the length of the first argument and the
1512 // fact that it is a constant string array. Check that now
1514 if (!getConstantStringLength(ci->getOperand(1), len))
1520 // fputs("",F) -> noop
1524 // fputs(s,F) -> fputc(s[0],F) (if s is constant and strlen(s) == 1)
1525 const Type* FILEptr_type = ci->getOperand(2)->getType();
1526 Function* fputc_func = SLC.get_fputc(FILEptr_type);
1529 LoadInst* loadi = new LoadInst(ci->getOperand(1),
1530 ci->getOperand(1)->getName()+".byte",ci);
1531 CastInst* casti = new CastInst(loadi,Type::IntTy,
1532 loadi->getName()+".int",ci);
1533 new CallInst(fputc_func,casti,ci->getOperand(2),"",ci);
1538 // fputs(s,F) -> fwrite(s,1,len,F) (if s is constant and strlen(s) > 1)
1539 const Type* FILEptr_type = ci->getOperand(2)->getType();
1540 Function* fwrite_func = SLC.get_fwrite(FILEptr_type);
1543 std::vector<Value*> parms;
1544 parms.push_back(ci->getOperand(1));
1545 parms.push_back(ConstantUInt::get(SLC.getIntPtrType(),len));
1546 parms.push_back(ConstantUInt::get(SLC.getIntPtrType(),1));
1547 parms.push_back(ci->getOperand(2));
1548 new CallInst(fwrite_func,parms,"",ci);
1552 ci->eraseFromParent();
1553 return true; // success
1557 /// This LibCallOptimization will simplify calls to the "isdigit" library
1558 /// function. It simply does range checks the parameter explicitly.
1559 /// @brief Simplify the isdigit library function.
1560 struct IsDigitOptimization : public LibCallOptimization
1563 /// @brief Default Constructor
1564 IsDigitOptimization() : LibCallOptimization("isdigit",
1565 "simplify-libcalls:isdigit", "Number of 'isdigit' calls simplified") {}
1567 /// @brief Destructor
1568 virtual ~IsDigitOptimization() {}
1570 /// @brief Make sure that the "fputs" function has the right prototype
1571 virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC)
1573 // Just make sure this has 1 argument
1574 return (f->arg_size() == 1);
1577 /// @brief Perform the toascii optimization.
1578 virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC)
1580 if (ConstantInt* CI = dyn_cast<ConstantInt>(ci->getOperand(1)))
1582 // isdigit(c) -> 0 or 1, if 'c' is constant
1583 uint64_t val = CI->getRawValue();
1584 if (val >= '0' && val <='9')
1585 ci->replaceAllUsesWith(ConstantSInt::get(Type::IntTy,1));
1587 ci->replaceAllUsesWith(ConstantSInt::get(Type::IntTy,0));
1588 ci->eraseFromParent();
1592 // isdigit(c) -> (unsigned)c - '0' <= 9
1594 new CastInst(ci->getOperand(1),Type::UIntTy,
1595 ci->getOperand(1)->getName()+".uint",ci);
1596 BinaryOperator* sub_inst = BinaryOperator::create(Instruction::Sub,cast,
1597 ConstantUInt::get(Type::UIntTy,0x30),
1598 ci->getOperand(1)->getName()+".sub",ci);
1599 SetCondInst* setcond_inst = new SetCondInst(Instruction::SetLE,sub_inst,
1600 ConstantUInt::get(Type::UIntTy,9),
1601 ci->getOperand(1)->getName()+".cmp",ci);
1603 new CastInst(setcond_inst,Type::IntTy,
1604 ci->getOperand(1)->getName()+".isdigit",ci);
1605 ci->replaceAllUsesWith(c2);
1606 ci->eraseFromParent();
1611 /// This LibCallOptimization will simplify calls to the "toascii" library
1612 /// function. It simply does the corresponding and operation to restrict the
1613 /// range of values to the ASCII character set (0-127).
1614 /// @brief Simplify the toascii library function.
1615 struct ToAsciiOptimization : public LibCallOptimization
1618 /// @brief Default Constructor
1619 ToAsciiOptimization() : LibCallOptimization("toascii",
1620 "simplify-libcalls:toascii", "Number of 'toascii' calls simplified") {}
1622 /// @brief Destructor
1623 virtual ~ToAsciiOptimization() {}
1625 /// @brief Make sure that the "fputs" function has the right prototype
1626 virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC)
1628 // Just make sure this has 2 arguments
1629 return (f->arg_size() == 1);
1632 /// @brief Perform the toascii optimization.
1633 virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC)
1635 // toascii(c) -> (c & 0x7f)
1636 Value* chr = ci->getOperand(1);
1637 BinaryOperator* and_inst = BinaryOperator::create(Instruction::And,chr,
1638 ConstantInt::get(chr->getType(),0x7F),ci->getName()+".toascii",ci);
1639 ci->replaceAllUsesWith(and_inst);
1640 ci->eraseFromParent();
1645 /// A function to compute the length of a null-terminated constant array of
1646 /// integers. This function can't rely on the size of the constant array
1647 /// because there could be a null terminator in the middle of the array.
1648 /// We also have to bail out if we find a non-integer constant initializer
1649 /// of one of the elements or if there is no null-terminator. The logic
1650 /// below checks each of these conditions and will return true only if all
1651 /// conditions are met. In that case, the \p len parameter is set to the length
1652 /// of the null-terminated string. If false is returned, the conditions were
1653 /// not met and len is set to 0.
1654 /// @brief Get the length of a constant string (null-terminated array).
1655 bool getConstantStringLength(Value* V, uint64_t& len, ConstantArray** CA )
1657 assert(V != 0 && "Invalid args to getConstantStringLength");
1658 len = 0; // make sure we initialize this
1660 // If the value is not a GEP instruction nor a constant expression with a
1661 // GEP instruction, then return false because ConstantArray can't occur
1663 if (GetElementPtrInst* GEPI = dyn_cast<GetElementPtrInst>(V))
1665 else if (ConstantExpr* CE = dyn_cast<ConstantExpr>(V))
1666 if (CE->getOpcode() == Instruction::GetElementPtr)
1673 // Make sure the GEP has exactly three arguments.
1674 if (GEP->getNumOperands() != 3)
1677 // Check to make sure that the first operand of the GEP is an integer and
1678 // has value 0 so that we are sure we're indexing into the initializer.
1679 if (ConstantInt* op1 = dyn_cast<ConstantInt>(GEP->getOperand(1)))
1681 if (!op1->isNullValue())
1687 // Ensure that the second operand is a ConstantInt. If it isn't then this
1688 // GEP is wonky and we're not really sure what were referencing into and
1689 // better of not optimizing it. While we're at it, get the second index
1690 // value. We'll need this later for indexing the ConstantArray.
1691 uint64_t start_idx = 0;
1692 if (ConstantInt* CI = dyn_cast<ConstantInt>(GEP->getOperand(2)))
1693 start_idx = CI->getRawValue();
1697 // The GEP instruction, constant or instruction, must reference a global
1698 // variable that is a constant and is initialized. The referenced constant
1699 // initializer is the array that we'll use for optimization.
1700 GlobalVariable* GV = dyn_cast<GlobalVariable>(GEP->getOperand(0));
1701 if (!GV || !GV->isConstant() || !GV->hasInitializer())
1704 // Get the initializer.
1705 Constant* INTLZR = GV->getInitializer();
1707 // Handle the ConstantAggregateZero case
1708 if (ConstantAggregateZero* CAZ = dyn_cast<ConstantAggregateZero>(INTLZR))
1710 // This is a degenerate case. The initializer is constant zero so the
1711 // length of the string must be zero.
1716 // Must be a Constant Array
1717 ConstantArray* A = dyn_cast<ConstantArray>(INTLZR);
1721 // Get the number of elements in the array
1722 uint64_t max_elems = A->getType()->getNumElements();
1724 // Traverse the constant array from start_idx (derived above) which is
1725 // the place the GEP refers to in the array.
1726 for ( len = start_idx; len < max_elems; len++)
1728 if (ConstantInt* CI = dyn_cast<ConstantInt>(A->getOperand(len)))
1730 // Check for the null terminator
1731 if (CI->isNullValue())
1732 break; // we found end of string
1735 return false; // This array isn't suitable, non-int initializer
1737 if (len >= max_elems)
1738 return false; // This array isn't null terminated
1740 // Subtract out the initial value from the length
1744 return true; // success!
1748 // Additional cases that we need to add to this file:
1751 // * cbrt(expN(X)) -> expN(x/3)
1752 // * cbrt(sqrt(x)) -> pow(x,1/6)
1753 // * cbrt(sqrt(x)) -> pow(x,1/9)
1756 // * cos(-x) -> cos(x)
1759 // * exp(log(x)) -> x
1761 // ffs, ffsl, ffsll:
1762 // * ffs(cnst) -> cnst'
1765 // * isascii(c) -> ((c & ~0x7f) == 0)
1768 // * isdigit(c) -> (unsigned)(c) - '0' <= 9
1771 // * log(exp(x)) -> x
1772 // * log(x**y) -> y*log(x)
1773 // * log(exp(y)) -> y*log(e)
1774 // * log(exp2(y)) -> y*log(2)
1775 // * log(exp10(y)) -> y*log(10)
1776 // * log(sqrt(x)) -> 0.5*log(x)
1777 // * log(pow(x,y)) -> y*log(x)
1779 // lround, lroundf, lroundl:
1780 // * lround(cnst) -> cnst'
1783 // * memcmp(s1,s2,0) -> 0
1784 // * memcmp(x,x,l) -> 0
1785 // * memcmp(x,y,l) -> cnst
1786 // (if all arguments are constant and strlen(x) <= l and strlen(y) <= l)
1787 // * memcmp(x,y,1) -> *x - *y
1790 // * memmove(d,s,l,a) -> memcpy(d,s,l,a)
1791 // (if s is a global constant array)
1794 // * pow(exp(x),y) -> exp(x*y)
1795 // * pow(sqrt(x),y) -> pow(x,y*0.5)
1796 // * pow(pow(x,y),z)-> pow(x,y*z)
1799 // * puts("") -> fputc("\n",stdout) (how do we get "stdout"?)
1801 // round, roundf, roundl:
1802 // * round(cnst) -> cnst'
1805 // * signbit(cnst) -> cnst'
1806 // * signbit(nncst) -> 0 (if pstv is a non-negative constant)
1808 // sqrt, sqrtf, sqrtl:
1809 // * sqrt(expN(x)) -> expN(x*0.5)
1810 // * sqrt(Nroot(x)) -> pow(x,1/(2*N))
1811 // * sqrt(pow(x,y)) -> pow(|x|,y*0.5)
1814 // * strrchr(s,c) -> reverse_offset_of_in(c,s)
1815 // (if c is a constant integer and s is a constant string)
1816 // * strrchr(s1,0) -> strchr(s1,0)
1819 // * strncat(x,y,0) -> x
1820 // * strncat(x,y,0) -> x (if strlen(y) = 0)
1821 // * strncat(x,y,l) -> strcat(x,y) (if y and l are constants an l > strlen(y))
1824 // * strncpy(d,s,0) -> d
1825 // * strncpy(d,s,l) -> memcpy(d,s,l,1)
1826 // (if s and l are constants)
1829 // * strpbrk(s,a) -> offset_in_for(s,a)
1830 // (if s and a are both constant strings)
1831 // * strpbrk(s,"") -> 0
1832 // * strpbrk(s,a) -> strchr(s,a[0]) (if a is constant string of length 1)
1835 // * strspn(s,a) -> const_int (if both args are constant)
1836 // * strspn("",a) -> 0
1837 // * strspn(s,"") -> 0
1838 // * strcspn(s,a) -> const_int (if both args are constant)
1839 // * strcspn("",a) -> 0
1840 // * strcspn(s,"") -> strlen(a)
1843 // * strstr(x,x) -> x
1844 // * strstr(s1,s2) -> offset_of_s2_in(s1)
1845 // (if s1 and s2 are constant strings)
1848 // * tan(atan(x)) -> x
1850 // trunc, truncf, truncl:
1851 // * trunc(cnst) -> cnst'