Implement the IsDigitOptimization for simplifying calls to the isdigit
[oota-llvm.git] / lib / Transforms / IPO / SimplifyLibCalls.cpp
1 //===- SimplifyLibCalls.cpp - Optimize specific well-known library calls --===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements a variety of small optimizations for calls to specific
11 // well-known (e.g. runtime library) function calls. For example, a call to the
12 // function "exit(3)" that occurs within the main() function can be transformed
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. 
16 //
17 //===----------------------------------------------------------------------===//
18
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 <iostream>
31 using namespace llvm;
32
33 namespace {
34
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");
39
40 // Forward declarations
41 class LibCallOptimization;
42 class SimplifyLibCalls;
43
44 /// @brief The list of optimizations deriving from LibCallOptimization
45 hash_map<std::string,LibCallOptimization*> optlist;
46
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
63 {
64 public:
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 )
70     : func_name(fname)
71 #ifndef NDEBUG
72     , occurrences(stat_name,description)
73 #endif
74   {
75     // Register this call optimizer in the optlist (a hash_map)
76     optlist[fname] = this;
77   }
78
79   /// @brief Deregister from the optlist
80   virtual ~LibCallOptimization() { optlist.erase(func_name); }
81
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
95   ) = 0;
96
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
107   ) = 0;
108
109   /// @brief Get the name of the library call being optimized
110   const char * getFunctionName() const { return func_name; }
111
112 #ifndef NDEBUG
113   /// @brief Called by SimplifyLibCalls to update the occurrences statistic.
114   void succeeded() { ++occurrences; }
115 #endif
116
117 private:
118   const char* func_name; ///< Name of the library call we optimize
119 #ifndef NDEBUG
120   Statistic<> occurrences; ///< debug statistic (-debug-only=simplify-libcalls)
121 #endif
122 };
123
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 
133 {
134 public:
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
139   {
140     // Ask that the TargetData analysis be performed before us so we can use
141     // the target data.
142     Info.addRequired<TargetData>();
143   }
144
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)
149   {
150     reset(M);
151
152     bool result = false;
153
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;
160     do
161     {
162       found_optimization = false;
163       for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI)
164       {
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())
170           continue;
171
172         // Get the optimization class that pertains to this function
173         LibCallOptimization* CO = optlist[FI->getName().c_str()];
174         if (!CO)
175           continue;
176
177         // Make sure the called function is suitable for the optimization
178         if (!CO->ValidateCalledFunction(FI,*this))
179           continue;
180
181         // Loop over each of the uses of the function
182         for (Value::use_iterator UI = FI->use_begin(), UE = FI->use_end(); 
183              UI != UE ; )
184         {
185           // If the use of the function is a call instruction
186           if (CallInst* CI = dyn_cast<CallInst>(*UI++))
187           {
188             // Do the optimization on the LibCallOptimization.
189             if (CO->OptimizeCall(CI,*this))
190             {
191               ++SimplifiedLibCalls;
192               found_optimization = result = true;
193 #ifndef NDEBUG
194               CO->succeeded();
195 #endif
196             }
197           }
198         }
199       }
200     } while (found_optimization);
201     return result;
202   }
203
204   /// @brief Return the *current* module we're working on.
205   Module* getModule() const { return M; }
206
207   /// @brief Return the *current* target data for the module we're working on.
208   TargetData* getTargetData() const { return TD; }
209
210   /// @brief Return the size_t type -- syntactic shortcut
211   const Type* getIntPtrType() const { return TD->getIntPtrType(); }
212
213   /// @brief Return a Function* for the fputc libcall
214   Function* get_fputc(const Type* FILEptr_type)
215   {
216     if (!fputc_func)
217     {
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);
224     }
225     return fputc_func;
226   }
227
228   /// @brief Return a Function* for the fwrite libcall
229   Function* get_fwrite(const Type* FILEptr_type)
230   {
231     if (!fwrite_func)
232     {
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);
241     }
242     return fwrite_func;
243   }
244
245   /// @brief Return a Function* for the sqrt libcall
246   Function* get_sqrt()
247   {
248     if (!sqrt_func)
249     {
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);
255     }
256     return sqrt_func;
257   }
258
259   /// @brief Return a Function* for the strlen libcall
260   Function* get_strcpy()
261   {
262     if (!strcpy_func)
263     {
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);
270     }
271     return strcpy_func;
272   }
273
274   /// @brief Return a Function* for the strlen libcall
275   Function* get_strlen()
276   {
277     if (!strlen_func)
278     {
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);
284     }
285     return strlen_func;
286   }
287
288   /// @brief Return a Function* for the memchr libcall
289   Function* get_memchr()
290   {
291     if (!memchr_func)
292     {
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);
300     }
301     return memchr_func;
302   }
303
304   /// @brief Return a Function* for the memcpy libcall
305   Function* get_memcpy()
306   {
307     if (!memcpy_func)
308     {
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);
317     }
318     return memcpy_func;
319   }
320
321 private:
322   /// @brief Reset our cached data for a new Module
323   void reset(Module& mod)
324   {
325     M = &mod;
326     TD = &getAnalysis<TargetData>();
327     fputc_func = 0;
328     fwrite_func = 0;
329     memcpy_func = 0;
330     memchr_func = 0;
331     sqrt_func   = 0;
332     strcpy_func = 0;
333     strlen_func = 0;
334   }
335
336 private:
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
346 };
347
348 // Register the pass
349 RegisterOpt<SimplifyLibCalls> 
350 X("simplify-libcalls","Simplify well-known library calls");
351
352 } // anonymous namespace
353
354 // The only public symbol in this file which just instantiates the pass object
355 ModulePass *llvm::createSimplifyLibCallsPass() 
356
357   return new SimplifyLibCalls(); 
358 }
359
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. 
364 namespace {
365
366 // Forward declare a utility function.
367 bool getConstantStringLength(Value* V, uint64_t& len, ConstantArray** A = 0 );
368
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
375 {
376   ExitInMainOptimization() : LibCallOptimization("exit",
377       "simplify-libcalls:exit","Number of 'exit' calls simplified") {}
378   virtual ~ExitInMainOptimization() {}
379
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)
383   {
384     if (f->arg_size() >= 1)
385       if (f->arg_begin()->getType()->isInteger())
386         return true;
387     return false;
388   }
389
390   virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC)
391   {
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")
399         {
400           // Okay, time to actually do the optimization. First, get the basic 
401           // block of the call instruction
402           BasicBlock* bb = ci->getParent();
403
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 
406           // instruction.
407           ReturnInst* ri = new ReturnInst(ci->getOperand(1), ci);
408
409           // Split the block at the call instruction which places it in a new
410           // basic block.
411           bb->splitBasicBlock(ci);
412
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
416           // instruction.
417           bb->getInstList().pop_back();
418
419           // Now we can finally get rid of the call instruction which now lives
420           // in the new basic block.
421           ci->eraseFromParent();
422
423           // Optimization succeeded, return true.
424           return true;
425         }
426     // We didn't pass the criteria for this optimization so return false
427     return false;
428   }
429 } ExitInMainOptimizer;
430
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
439 {
440 public:
441   /// @brief Default constructor
442   StrCatOptimization() : LibCallOptimization("strcat",
443       "simplify-libcalls:strcat","Number of 'strcat' calls simplified") {}
444
445 public:
446   /// @breif  Destructor
447   virtual ~StrCatOptimization() {}
448
449   /// @brief Make sure that the "strcat" function has the right prototype
450   virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC) 
451   {
452     if (f->getReturnType() == PointerType::get(Type::SByteTy))
453       if (f->arg_size() == 2) 
454       {
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))
458           {
459             // Indicate this is a suitable call type.
460             return true;
461           }
462       }
463     return false;
464   }
465
466   /// @brief Optimize the strcat library function
467   virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC)
468   {
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);
473
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
477     uint64_t len = 0;
478     if (!getConstantStringLength(src,len))
479       return false;
480
481     // Handle the simple, do-nothing case
482     if (len == 0)
483     {
484       ci->replaceAllUsesWith(dest);
485       ci->eraseFromParent();
486       return true;
487     }
488
489     // Increment the length because we actually want to memcpy the null
490     // terminator as well.
491     len++;
492
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);
499
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);
507
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);
516
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();
522     return true;
523   }
524 } StrCatOptimizer;
525
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
531 {
532 public:
533   StrChrOptimization() : LibCallOptimization("strchr",
534       "simplify-libcalls:strchr","Number of 'strchr' calls simplified") {}
535   virtual ~StrChrOptimization() {}
536
537   /// @brief Make sure that the "strchr" function has the right prototype
538   virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC) 
539   {
540     if (f->getReturnType() == PointerType::get(Type::SByteTy) && 
541         f->arg_size() == 2)
542       return true;
543     return false;
544   }
545
546   /// @brief Perform the strcpy optimization
547   virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC)
548   {
549     // If there aren't three operands, bail
550     if (ci->getNumOperands() != 3)
551       return false;
552
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.
555     uint64_t len = 0;
556     ConstantArray* CA;
557     if (!getConstantStringLength(ci->getOperand(1),len,&CA))
558       return false;
559
560     // Check that the second argument to strchr is a constant int, return false
561     // if it isn't
562     ConstantSInt* CSI = dyn_cast<ConstantSInt>(ci->getOperand(2));
563     if (!CSI)
564     {
565       // Just lower this to memchr since we know the length of the string as
566       // it is constant.
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();
574       return true;
575     }
576
577     // Get the character we're looking for
578     int64_t chr = CSI->getValue();
579
580     // Compute the offset
581     uint64_t offset = 0;
582     bool char_found = false;
583     for (uint64_t i = 0; i < len; ++i)
584     {
585       if (ConstantSInt* CI = dyn_cast<ConstantSInt>(CA->getOperand(i)))
586       {
587         // Check for the null terminator
588         if (CI->isNullValue())
589           break; // we found end of string
590         else if (CI->getValue() == chr)
591         {
592           char_found = true;
593           offset = i;
594           break;
595         }
596       }
597     }
598
599     // strchr(s,c)  -> offset_of_in(c,s)
600     //    (if c is a constant integer and s is a constant string)
601     if (char_found)
602     {
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);
608     }
609     else
610       ci->replaceAllUsesWith(
611           ConstantPointerNull::get(PointerType::get(Type::SByteTy)));
612
613     ci->eraseFromParent();
614     return true;
615   }
616 } StrChrOptimizer;
617
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
623 {
624 public:
625   StrCmpOptimization() : LibCallOptimization("strcmp",
626       "simplify-libcalls:strcmp","Number of 'strcmp' calls simplified") {}
627   virtual ~StrCmpOptimization() {}
628
629   /// @brief Make sure that the "strcpy" function has the right prototype
630   virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC) 
631   {
632     if (f->getReturnType() == Type::IntTy && f->arg_size() == 2)
633       return true;
634     return false;
635   }
636
637   /// @brief Perform the strcpy optimization
638   virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC)
639   {
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);
645     if (s1 == s2)
646     {
647       // strcmp(x,x)  -> 0
648       ci->replaceAllUsesWith(ConstantInt::get(Type::IntTy,0));
649       ci->eraseFromParent();
650       return true;
651     }
652
653     bool isstr_1 = false;
654     uint64_t len_1 = 0;
655     ConstantArray* A1;
656     if (getConstantStringLength(s1,len_1,&A1))
657     {
658       isstr_1 = true;
659       if (len_1 == 0)
660       {
661         // strcmp("",x) -> *x
662         LoadInst* load = new LoadInst(s1,ci->getName()+".load",ci);
663         CastInst* cast = 
664           new CastInst(load,Type::IntTy,ci->getName()+".int",ci);
665         ci->replaceAllUsesWith(cast);
666         ci->eraseFromParent();
667         return true;
668       }
669     }
670
671     bool isstr_2 = false;
672     uint64_t len_2 = 0;
673     ConstantArray* A2;
674     if (getConstantStringLength(s2,len_2,&A2))
675     {
676       isstr_2 = true;
677       if (len_2 == 0)
678       {
679         // strcmp(x,"") -> *x
680         LoadInst* load = new LoadInst(s2,ci->getName()+".val",ci);
681         CastInst* cast = 
682           new CastInst(load,Type::IntTy,ci->getName()+".int",ci);
683         ci->replaceAllUsesWith(cast);
684         ci->eraseFromParent();
685         return true;
686       }
687     }
688
689     if (isstr_1 && isstr_2)
690     {
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();
697       return true;
698     }
699     return false;
700   }
701 } StrCmpOptimizer;
702
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
708 {
709 public:
710   StrNCmpOptimization() : LibCallOptimization("strncmp",
711       "simplify-libcalls:strncmp","Number of 'strncmp' calls simplified") {}
712   virtual ~StrNCmpOptimization() {}
713
714   /// @brief Make sure that the "strcpy" function has the right prototype
715   virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC) 
716   {
717     if (f->getReturnType() == Type::IntTy && f->arg_size() == 3)
718       return true;
719     return false;
720   }
721
722   /// @brief Perform the strncpy optimization
723   virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC)
724   {
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);
730     if (s1 == s2)
731     {
732       // strncmp(x,x,l)  -> 0
733       ci->replaceAllUsesWith(ConstantInt::get(Type::IntTy,0));
734       ci->eraseFromParent();
735       return true;
736     }
737
738     // Check the length argument, if it is Constant zero then the strings are
739     // considered equal.
740     uint64_t len_arg = 0;
741     bool len_arg_is_const = false;
742     if (ConstantInt* len_CI = dyn_cast<ConstantInt>(ci->getOperand(3)))
743     {
744       len_arg_is_const = true;
745       len_arg = len_CI->getRawValue();
746       if (len_arg == 0)
747       {
748         // strncmp(x,y,0)   -> 0
749         ci->replaceAllUsesWith(ConstantInt::get(Type::IntTy,0));
750         ci->eraseFromParent();
751         return true;
752       } 
753     }
754
755     bool isstr_1 = false;
756     uint64_t len_1 = 0;
757     ConstantArray* A1;
758     if (getConstantStringLength(s1,len_1,&A1))
759     {
760       isstr_1 = true;
761       if (len_1 == 0)
762       {
763         // strncmp("",x) -> *x
764         LoadInst* load = new LoadInst(s1,ci->getName()+".load",ci);
765         CastInst* cast = 
766           new CastInst(load,Type::IntTy,ci->getName()+".int",ci);
767         ci->replaceAllUsesWith(cast);
768         ci->eraseFromParent();
769         return true;
770       }
771     }
772
773     bool isstr_2 = false;
774     uint64_t len_2 = 0;
775     ConstantArray* A2;
776     if (getConstantStringLength(s2,len_2,&A2))
777     {
778       isstr_2 = true;
779       if (len_2 == 0)
780       {
781         // strncmp(x,"") -> *x
782         LoadInst* load = new LoadInst(s2,ci->getName()+".val",ci);
783         CastInst* cast = 
784           new CastInst(load,Type::IntTy,ci->getName()+".int",ci);
785         ci->replaceAllUsesWith(cast);
786         ci->eraseFromParent();
787         return true;
788       }
789     }
790
791     if (isstr_1 && isstr_2 && len_arg_is_const)
792     {
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();
799       return true;
800     }
801     return false;
802   }
803 } StrNCmpOptimizer;
804
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
811 {
812 public:
813   StrCpyOptimization() : LibCallOptimization("strcpy",
814       "simplify-libcalls:strcpy","Number of 'strcpy' calls simplified") {}
815   virtual ~StrCpyOptimization() {}
816
817   /// @brief Make sure that the "strcpy" function has the right prototype
818   virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC) 
819   {
820     if (f->getReturnType() == PointerType::get(Type::SByteTy))
821       if (f->arg_size() == 2) 
822       {
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))
826           {
827             // Indicate this is a suitable call type.
828             return true;
829           }
830       }
831     return false;
832   }
833
834   /// @brief Perform the strcpy optimization
835   virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC)
836   {
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);
845     if (dest == src)
846     {
847       ci->replaceAllUsesWith(dest);
848       ci->eraseFromParent();
849       return true;
850     }
851     
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
855     // is valid).
856     uint64_t len = 0;
857     if (!getConstantStringLength(ci->getOperand(2),len))
858       return false;
859
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
862     if (len == 0)
863     {
864       new StoreInst(ConstantInt::get(Type::SByteTy,0),ci->getOperand(1),ci);
865       ci->replaceAllUsesWith(dest);
866       ci->eraseFromParent();
867       return true;
868     }
869
870     // Increment the length because we actually want to memcpy the null
871     // terminator as well.
872     len++;
873
874     // Extract some information from the instruction
875     Module* M = ci->getParent()->getParent()->getParent();
876
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);
885
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();
891     return true;
892   }
893 } StrCpyOptimizer;
894
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
900 {
901   StrLenOptimization() : LibCallOptimization("strlen",
902       "simplify-libcalls:strlen","Number of 'strlen' calls simplified") {}
903   virtual ~StrLenOptimization() {}
904
905   /// @brief Make sure that the "strlen" function has the right prototype
906   virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC)
907   {
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))
912             return true;
913     return false;
914   }
915
916   /// @brief Perform the strlen optimization
917   virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC)
918   {
919     // Get the length of the string
920     uint64_t len = 0;
921     if (!getConstantStringLength(ci->getOperand(1),len))
922       return false;
923
924     ci->replaceAllUsesWith(
925         ConstantInt::get(SLC.getTargetData()->getIntPtrType(),len));
926     ci->eraseFromParent();
927     return true;
928   }
929 } StrLenOptimizer;
930
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
937 {
938   /// @brief Default Constructor
939   LLVMMemCpyOptimization() : LibCallOptimization("llvm.memcpy",
940       "simplify-libcalls:llvm.memcpy",
941       "Number of 'llvm.memcpy' calls simplified") {}
942
943 protected:
944   /// @brief Subclass Constructor 
945   LLVMMemCpyOptimization(const char* fname, const char* sname, const char* desc)
946     : LibCallOptimization(fname, sname, desc) {}
947 public:
948   /// @brief Destructor
949   virtual ~LLVMMemCpyOptimization() {}
950
951   /// @brief Make sure that the "memcpy" function has the right prototype
952   virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& TD)
953   {
954     // Just make sure this has 4 arguments per LLVM spec.
955     return (f->arg_size() == 4);
956   }
957
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)
965   {
966     // Make sure we have constant int values to work with
967     ConstantInt* LEN = dyn_cast<ConstantInt>(ci->getOperand(3));
968     if (!LEN)
969       return false;
970     ConstantInt* ALIGN = dyn_cast<ConstantInt>(ci->getOperand(4));
971     if (!ALIGN)
972       return false;
973
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();
977     if (alignment == 0)
978       alignment = 1; // Alignment 0 is identity for alignment 1
979     if (len > alignment)
980       return false;
981
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);
985     Type* castType = 0;
986     switch (len)
987     {
988       case 0:
989         // memcpy(d,s,0,a) -> noop
990         ci->eraseFromParent();
991         return true;
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;
996       default:
997         return false;
998     }
999
1000     // Cast source and dest to the right sized primitive and then load/store
1001     CastInst* SrcCast = 
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();
1008     return true;
1009   }
1010 } LLVMMemCpyOptimizer;
1011
1012 /// This LibCallOptimization will simplify a call to the memmove library 
1013 /// function. It is identical to MemCopyOptimization except for the name of 
1014 /// the intrinsic.
1015 /// @brief Simplify the memmove library function.
1016 struct LLVMMemMoveOptimization : public LLVMMemCpyOptimization
1017 {
1018   /// @brief Default Constructor
1019   LLVMMemMoveOptimization() : LLVMMemCpyOptimization("llvm.memmove",
1020       "simplify-libcalls:llvm.memmove",
1021       "Number of 'llvm.memmove' calls simplified") {}
1022
1023 } LLVMMemMoveOptimizer;
1024
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
1029 {
1030   /// @brief Default Constructor
1031   LLVMMemSetOptimization() : LibCallOptimization("llvm.memset",
1032       "simplify-libcalls:llvm.memset",
1033       "Number of 'llvm.memset' calls simplified") {}
1034
1035 public:
1036   /// @brief Destructor
1037   virtual ~LLVMMemSetOptimization() {}
1038
1039   /// @brief Make sure that the "memset" function has the right prototype
1040   virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& TD)
1041   {
1042     // Just make sure this has 3 arguments per LLVM spec.
1043     return (f->arg_size() == 4);
1044   }
1045
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)
1054   {
1055     // Make sure we have constant int values to work with
1056     ConstantInt* LEN = dyn_cast<ConstantInt>(ci->getOperand(3));
1057     if (!LEN)
1058       return false;
1059     ConstantInt* ALIGN = dyn_cast<ConstantInt>(ci->getOperand(4));
1060     if (!ALIGN)
1061       return false;
1062
1063     // Extract the length and alignment
1064     uint64_t len = LEN->getRawValue();
1065     uint64_t alignment = ALIGN->getRawValue();
1066
1067     // Alignment 0 is identity for alignment 1
1068     if (alignment == 0)
1069       alignment = 1;
1070
1071     // If the length is zero, this is a no-op
1072     if (len == 0)
1073     {
1074       // memset(d,c,0,a) -> noop
1075       ci->eraseFromParent();
1076       return true;
1077     }
1078
1079     // If the length is larger than the alignment, we can't optimize
1080     if (len > alignment)
1081       return false;
1082
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));
1086     if (!FILL)
1087       return false;
1088     if (FILL->getType() != Type::UByteTy)
1089       return false;
1090
1091     // memset(s,c,n) -> store s, c (for n=1,2,4,8)
1092     
1093     // Extract the fill character
1094     uint64_t fill_char = FILL->getValue();
1095     uint64_t fill_value = fill_char;
1096
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);
1100     Type* castType = 0;
1101     switch (len)
1102     {
1103       case 1: 
1104         castType = Type::UByteTy; 
1105         break;
1106       case 2: 
1107         castType = Type::UShortTy; 
1108         fill_value |= fill_char << 8;
1109         break;
1110       case 4: 
1111         castType = Type::UIntTy;
1112         fill_value |= fill_char << 8 | fill_char << 16 | fill_char << 24;
1113         break;
1114       case 8: 
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;
1119         break;
1120       default:
1121         return false;
1122     }
1123
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();
1129     return true;
1130   }
1131 } LLVMMemSetOptimizer;
1132
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
1138 {
1139 public:
1140   /// @brief Default Constructor
1141   PowOptimization() : LibCallOptimization("pow",
1142       "simplify-libcalls:pow", "Number of 'pow' calls simplified") {}
1143
1144   /// @brief Destructor
1145   virtual ~PowOptimization() {}
1146
1147   /// @brief Make sure that the "pow" function has the right prototype
1148   virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC)
1149   {
1150     // Just make sure this has 2 arguments
1151     return (f->arg_size() == 2);
1152   }
1153
1154   /// @brief Perform the pow optimization.
1155   virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC)
1156   {
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();
1162       if (Op1V == 1.0)
1163       {
1164         // pow(1.0,x) -> 1.0
1165         ci->replaceAllUsesWith(ConstantFP::get(Ty,1.0));
1166         ci->eraseFromParent();
1167         return true;
1168       }
1169     } 
1170     else if (ConstantFP* Op2 = dyn_cast<ConstantFP>(expn)) 
1171     {
1172       double Op2V = Op2->getValue();
1173       if (Op2V == 0.0)
1174       {
1175         // pow(x,0.0) -> 1.0
1176         ci->replaceAllUsesWith(ConstantFP::get(Ty,1.0));
1177         ci->eraseFromParent();
1178         return true;
1179       }
1180       else if (Op2V == 0.5)
1181       {
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();
1187         return true;
1188       }
1189       else if (Op2V == 1.0)
1190       {
1191         // pow(x,1.0) -> x
1192         ci->replaceAllUsesWith(base);
1193         ci->eraseFromParent();
1194         return true;
1195       }
1196       else if (Op2V == -1.0)
1197       {
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();
1203         return true;
1204       }
1205     }
1206     return false; // opt failed
1207   }
1208 } PowOptimizer;
1209
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
1215 {
1216 public:
1217   /// @brief Default Constructor
1218   FPrintFOptimization() : LibCallOptimization("fprintf",
1219       "simplify-libcalls:fprintf", "Number of 'fprintf' calls simplified") {}
1220
1221   /// @brief Destructor
1222   virtual ~FPrintFOptimization() {}
1223
1224   /// @brief Make sure that the "fprintf" function has the right prototype
1225   virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC)
1226   {
1227     // Just make sure this has at least 2 arguments
1228     return (f->arg_size() >= 2);
1229   }
1230
1231   /// @brief Perform the fprintf optimization.
1232   virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC)
1233   {
1234     // If the call has more than 3 operands, we can't optimize it
1235     if (ci->getNumOperands() > 4 || ci->getNumOperands() <= 2)
1236       return false;
1237
1238     // If the result of the fprintf call is used, none of these optimizations 
1239     // can be made.
1240     if (!ci->hasNUses(0)) 
1241       return false;
1242
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
1245     uint64_t len = 0; 
1246     ConstantArray* CA = 0;
1247     if (!getConstantStringLength(ci->getOperand(2), len, &CA))
1248       return false;
1249
1250     if (ci->getNumOperands() == 3)
1251     {
1252       // Make sure there's no % in the constant array
1253       for (unsigned i = 0; i < len; ++i)
1254       {
1255         if (ConstantInt* CI = dyn_cast<ConstantInt>(CA->getOperand(i)))
1256         {
1257           // Check for the null terminator
1258           if (CI->getRawValue() == '%')
1259             return false; // we found end of string
1260         }
1261         else 
1262           return false;
1263       }
1264
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);
1268       if (!fwrite_func)
1269         return false;
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();
1278       return true;
1279     }
1280
1281     // The remaining optimizations require the format string to be length 2
1282     // "%s" or "%c".
1283     if (len != 2)
1284       return false;
1285
1286     // The first character has to be a %
1287     if (ConstantInt* CI = dyn_cast<ConstantInt>(CA->getOperand(0)))
1288       if (CI->getRawValue() != '%')
1289         return false;
1290
1291     // Get the second character and switch on its value
1292     ConstantInt* CI = dyn_cast<ConstantInt>(CA->getOperand(1));
1293     switch (CI->getRawValue())
1294     {
1295       case 's':
1296       {
1297         uint64_t len = 0; 
1298         ConstantArray* CA = 0;
1299         if (!getConstantStringLength(ci->getOperand(3), len, &CA))
1300           return false;
1301
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);
1305         if (!fwrite_func)
1306           return false;
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));
1314         break;
1315       }
1316       case 'c':
1317       {
1318         ConstantInt* CI = dyn_cast<ConstantInt>(ci->getOperand(3));
1319         if (!CI)
1320           return false;
1321
1322         const Type* FILEptr_type = ci->getOperand(1)->getType();
1323         Function* fputc_func = SLC.get_fputc(FILEptr_type);
1324         if (!fputc_func)
1325           return false;
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));
1329         break;
1330       }
1331       default:
1332         return false;
1333     }
1334     ci->eraseFromParent();
1335     return true;
1336   }
1337 } FPrintFOptimizer;
1338
1339
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
1345 {
1346 public:
1347   /// @brief Default Constructor
1348   SPrintFOptimization() : LibCallOptimization("sprintf",
1349       "simplify-libcalls:sprintf", "Number of 'sprintf' calls simplified") {}
1350
1351   /// @brief Destructor
1352   virtual ~SPrintFOptimization() {}
1353
1354   /// @brief Make sure that the "fprintf" function has the right prototype
1355   virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC)
1356   {
1357     // Just make sure this has at least 2 arguments
1358     return (f->getReturnType() == Type::IntTy && f->arg_size() >= 2);
1359   }
1360
1361   /// @brief Perform the sprintf optimization.
1362   virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC)
1363   {
1364     // If the call has more than 3 operands, we can't optimize it
1365     if (ci->getNumOperands() > 4 || ci->getNumOperands() < 3)
1366       return false;
1367
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
1370     uint64_t len = 0; 
1371     ConstantArray* CA = 0;
1372     if (!getConstantStringLength(ci->getOperand(2), len, &CA))
1373       return false;
1374
1375     if (ci->getNumOperands() == 3)
1376     {
1377       if (len == 0)
1378       {
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();
1383         return true;
1384       }
1385
1386       // Make sure there's no % in the constant array
1387       for (unsigned i = 0; i < len; ++i)
1388       {
1389         if (ConstantInt* CI = dyn_cast<ConstantInt>(CA->getOperand(i)))
1390         {
1391           // Check for the null terminator
1392           if (CI->getRawValue() == '%')
1393             return false; // we found a %, can't optimize
1394         }
1395         else 
1396           return false; // initializer is not constant int, can't optimize
1397       }
1398
1399       // Increment length because we want to copy the null byte too
1400       len++;
1401
1402       // sprintf(str,fmt) -> llvm.memcpy(str,fmt,strlen(fmt),1) 
1403       Function* memcpy_func = SLC.get_memcpy();
1404       if (!memcpy_func)
1405         return false;
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();
1414       return true;
1415     }
1416
1417     // The remaining optimizations require the format string to be length 2
1418     // "%s" or "%c".
1419     if (len != 2)
1420       return false;
1421
1422     // The first character has to be a %
1423     if (ConstantInt* CI = dyn_cast<ConstantInt>(CA->getOperand(0)))
1424       if (CI->getRawValue() != '%')
1425         return false;
1426
1427     // Get the second character and switch on its value
1428     ConstantInt* CI = dyn_cast<ConstantInt>(CA->getOperand(1));
1429     switch (CI->getRawValue())
1430     {
1431       case 's':
1432       {
1433         uint64_t len = 0;
1434         if (ci->hasNUses(0))
1435         {
1436           // sprintf(dest,"%s",str) -> strcpy(dest,str) 
1437           Function* strcpy_func = SLC.get_strcpy();
1438           if (!strcpy_func)
1439             return false;
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);
1444         }
1445         else if (getConstantStringLength(ci->getOperand(3),len))
1446         {
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();
1450           if (!memcpy_func)
1451             return false;
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));
1459         }
1460         break;
1461       }
1462       case 'c':
1463       {
1464         // sprintf(dest,"%c",chr) -> store chr, dest
1465         CastInst* cast = 
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",
1470           ci);
1471         new StoreInst(ConstantInt::get(Type::SByteTy,0),gep,ci);
1472         ci->replaceAllUsesWith(ConstantSInt::get(Type::IntTy,1));
1473         break;
1474       }
1475       default:
1476         return false;
1477     }
1478     ci->eraseFromParent();
1479     return true;
1480   }
1481 } SPrintFOptimizer;
1482
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
1488 {
1489 public:
1490   /// @brief Default Constructor
1491   PutsOptimization() : LibCallOptimization("fputs",
1492       "simplify-libcalls:fputs", "Number of 'fputs' calls simplified") {}
1493
1494   /// @brief Destructor
1495   virtual ~PutsOptimization() {}
1496
1497   /// @brief Make sure that the "fputs" function has the right prototype
1498   virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC)
1499   {
1500     // Just make sure this has 2 arguments
1501     return (f->arg_size() == 2);
1502   }
1503
1504   /// @brief Perform the fputs optimization.
1505   virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC)
1506   {
1507     // If the result is used, none of these optimizations work
1508     if (!ci->hasNUses(0)) 
1509       return false;
1510
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
1513     uint64_t len = 0; 
1514     if (!getConstantStringLength(ci->getOperand(1), len))
1515       return false;
1516
1517     switch (len)
1518     {
1519       case 0:
1520         // fputs("",F) -> noop
1521         break;
1522       case 1:
1523       {
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);
1527         if (!fputc_func)
1528           return false;
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);
1534         break;
1535       }
1536       default:
1537       {  
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);
1541         if (!fwrite_func)
1542           return false;
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);
1549         break;
1550       }
1551     }
1552     ci->eraseFromParent();
1553     return true; // success
1554   }
1555 } PutsOptimizer;
1556
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
1561 {
1562 public:
1563   /// @brief Default Constructor
1564   IsDigitOptimization() : LibCallOptimization("isdigit",
1565       "simplify-libcalls:isdigit", "Number of 'isdigit' calls simplified") {}
1566
1567   /// @brief Destructor
1568   virtual ~IsDigitOptimization() {}
1569
1570   /// @brief Make sure that the "fputs" function has the right prototype
1571   virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC)
1572   {
1573     // Just make sure this has 1 argument
1574     return (f->arg_size() == 1);
1575   }
1576
1577   /// @brief Perform the toascii optimization.
1578   virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC)
1579   {
1580     if (ConstantInt* CI = dyn_cast<ConstantInt>(ci->getOperand(1)))
1581     {
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));
1586       else
1587         ci->replaceAllUsesWith(ConstantSInt::get(Type::IntTy,0));
1588       ci->eraseFromParent();
1589       return true;
1590     }
1591
1592     // isdigit(c)   -> (unsigned)c - '0' <= 9
1593     CastInst* cast = 
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);
1602     CastInst* c2 = 
1603       new CastInst(setcond_inst,Type::IntTy,
1604         ci->getOperand(1)->getName()+".isdigit",ci);
1605     ci->replaceAllUsesWith(c2);
1606     ci->eraseFromParent();
1607     return true;
1608   }
1609 } IsDigitOptimizer;
1610
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
1616 {
1617 public:
1618   /// @brief Default Constructor
1619   ToAsciiOptimization() : LibCallOptimization("toascii",
1620       "simplify-libcalls:toascii", "Number of 'toascii' calls simplified") {}
1621
1622   /// @brief Destructor
1623   virtual ~ToAsciiOptimization() {}
1624
1625   /// @brief Make sure that the "fputs" function has the right prototype
1626   virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC)
1627   {
1628     // Just make sure this has 2 arguments
1629     return (f->arg_size() == 1);
1630   }
1631
1632   /// @brief Perform the toascii optimization.
1633   virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC)
1634   {
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();
1641     return true;
1642   }
1643 } ToAsciiOptimizer;
1644
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 )
1656 {
1657   assert(V != 0 && "Invalid args to getConstantStringLength");
1658   len = 0; // make sure we initialize this 
1659   User* GEP = 0;
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 
1662   // any other way
1663   if (GetElementPtrInst* GEPI = dyn_cast<GetElementPtrInst>(V))
1664     GEP = GEPI;
1665   else if (ConstantExpr* CE = dyn_cast<ConstantExpr>(V))
1666     if (CE->getOpcode() == Instruction::GetElementPtr)
1667       GEP = CE;
1668     else
1669       return false;
1670   else
1671     return false;
1672
1673   // Make sure the GEP has exactly three arguments.
1674   if (GEP->getNumOperands() != 3)
1675     return false;
1676
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)))
1680   {
1681     if (!op1->isNullValue())
1682       return false;
1683   }
1684   else
1685     return false;
1686
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();
1694   else
1695     return false;
1696
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())
1702     return false;
1703
1704   // Get the initializer.
1705   Constant* INTLZR = GV->getInitializer();
1706
1707   // Handle the ConstantAggregateZero case
1708   if (ConstantAggregateZero* CAZ = dyn_cast<ConstantAggregateZero>(INTLZR))
1709   {
1710     // This is a degenerate case. The initializer is constant zero so the
1711     // length of the string must be zero.
1712     len = 0;
1713     return true;
1714   }
1715
1716   // Must be a Constant Array
1717   ConstantArray* A = dyn_cast<ConstantArray>(INTLZR);
1718   if (!A)
1719     return false;
1720
1721   // Get the number of elements in the array
1722   uint64_t max_elems = A->getType()->getNumElements();
1723
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++)
1727   {
1728     if (ConstantInt* CI = dyn_cast<ConstantInt>(A->getOperand(len)))
1729     {
1730       // Check for the null terminator
1731       if (CI->isNullValue())
1732         break; // we found end of string
1733     }
1734     else
1735       return false; // This array isn't suitable, non-int initializer
1736   }
1737   if (len >= max_elems)
1738     return false; // This array isn't null terminated
1739
1740   // Subtract out the initial value from the length
1741   len -= start_idx;
1742   if (CA)
1743     *CA = A;
1744   return true; // success!
1745 }
1746
1747 // TODO: 
1748 //   Additional cases that we need to add to this file:
1749 //
1750 // cbrt:
1751 //   * cbrt(expN(X))  -> expN(x/3)
1752 //   * cbrt(sqrt(x))  -> pow(x,1/6)
1753 //   * cbrt(sqrt(x))  -> pow(x,1/9)
1754 //
1755 // cos, cosf, cosl:
1756 //   * cos(-x)  -> cos(x)
1757 //
1758 // exp, expf, expl:
1759 //   * exp(log(x))  -> x
1760 //
1761 // ffs, ffsl, ffsll:
1762 //   * ffs(cnst)     -> cnst'
1763 //
1764 // isascii:
1765 //   * isascii(c)    -> ((c & ~0x7f) == 0)
1766 //   
1767 // isdigit:
1768 //   * isdigit(c)    -> (unsigned)(c) - '0' <= 9
1769 //
1770 // log, logf, logl:
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)
1778 //
1779 // lround, lroundf, lroundl:
1780 //   * lround(cnst) -> cnst'
1781 //
1782 // memcmp:
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
1788 //
1789 // memmove:
1790 //   * memmove(d,s,l,a) -> memcpy(d,s,l,a) 
1791 //       (if s is a global constant array)
1792 //
1793 // pow, powf, powl:
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)
1797 //
1798 // puts:
1799 //   * puts("") -> fputc("\n",stdout) (how do we get "stdout"?)
1800 //
1801 // round, roundf, roundl:
1802 //   * round(cnst) -> cnst'
1803 //
1804 // signbit:
1805 //   * signbit(cnst) -> cnst'
1806 //   * signbit(nncst) -> 0 (if pstv is a non-negative constant)
1807 //
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)
1812 //
1813 // strrchr:
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)
1817 //
1818 // strncat:
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))
1822 //
1823 // strncpy:
1824 //   * strncpy(d,s,0) -> d
1825 //   * strncpy(d,s,l) -> memcpy(d,s,l,1)
1826 //      (if s and l are constants)
1827 //
1828 // strpbrk:
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)
1833 //
1834 // strspn, strcspn:
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)
1841 //
1842 // strstr:
1843 //   * strstr(x,x)  -> x
1844 //   * strstr(s1,s2) -> offset_of_s2_in(s1)  
1845 //       (if s1 and s2 are constant strings)
1846 //    
1847 // tan, tanf, tanl:
1848 //   * tan(atan(x)) -> x
1849 // 
1850 // trunc, truncf, truncl:
1851 //   * trunc(cnst) -> cnst'
1852 //
1853 // 
1854 }