Skip to content
SimplifyLibCalls.cpp 86.9 KiB
Newer Older
Reid Spencer's avatar
Reid Spencer committed
//===- SimplifyLibCalls.cpp - Optimize specific well-known library calls --===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements a module pass that applies a variety of small
// optimizations for calls to specific well-known function calls (e.g. runtime
// library functions). For example, a call to the function "exit(3)" that
// occurs within the main() function can be transformed into a simple "return 3"
// instruction. Any optimization that takes this form (replace call to library
// function with simpler code that provides the same result) belongs in this
// file.
//
//===----------------------------------------------------------------------===//

#define DEBUG_TYPE "simplify-libcalls"
#include "llvm/Constants.h"
#include "llvm/Intrinsics.h"
#include "llvm/Module.h"
#include "llvm/Pass.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/StringMap.h"
Reid Spencer's avatar
Reid Spencer committed
#include "llvm/Config/config.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Target/TargetData.h"
Reid Spencer's avatar
Reid Spencer committed
/// This statistic keeps track of the total number of library calls that have
/// been simplified regardless of which call it is.
STATISTIC(SimplifiedLibCalls, "Number of library calls simplified");
Reid Spencer's avatar
Reid Spencer committed

namespace {
  // Forward declarations
  class LibCallOptimization;
  class SimplifyLibCalls;
  
/// This list is populated by the constructor for LibCallOptimization class.
/// Therefore all subclasses are registered here at static initialization time
/// and this list is what the SimplifyLibCalls pass uses to apply the individual
/// optimizations to the call sites.
Reid Spencer's avatar
Reid Spencer committed
/// @brief The list of optimizations deriving from LibCallOptimization
static LibCallOptimization *OptList = 0;
Reid Spencer's avatar
Reid Spencer committed

/// This class is the abstract base class for the set of optimizations that
Reid Spencer's avatar
Reid Spencer committed
/// corresponds to one library call. The SimplifyLibCalls pass will call the
Reid Spencer's avatar
Reid Spencer committed
/// ValidateCalledFunction method to ask the optimization if a given Function
Reid Spencer's avatar
Reid Spencer committed
/// is the kind that the optimization can handle. If the subclass returns true,
/// then SImplifyLibCalls will also call the OptimizeCall method to perform,
Reid Spencer's avatar
Reid Spencer committed
/// or attempt to perform, the optimization(s) for the library call. Otherwise,
/// OptimizeCall won't be called. Subclasses are responsible for providing the
/// name of the library call (strlen, strcpy, etc.) to the LibCallOptimization
/// constructor. This is used to efficiently select which call instructions to
/// optimize. The criteria for a "lib call" is "anything with well known
Reid Spencer's avatar
Reid Spencer committed
/// semantics", typically a library function that is defined by an international
/// standard. Because the semantics are well known, the optimizations can
Reid Spencer's avatar
Reid Spencer committed
/// generally short-circuit actually calling the function if there's a simpler
/// way (e.g. strlen(X) can be reduced to a constant if X is a constant global).
Reid Spencer's avatar
Reid Spencer committed
/// @brief Base class for library call optimizations
class VISIBILITY_HIDDEN LibCallOptimization {
  LibCallOptimization **Prev, *Next;
  const char *FunctionName; ///< Name of the library call we optimize
#ifndef NDEBUG
  Statistic occurrences; ///< debug statistic (-debug-only=simplify-libcalls)
  /// The \p fname argument must be the name of the library function being
Reid Spencer's avatar
Reid Spencer committed
  /// optimized by the subclass.
  /// @brief Constructor that registers the optimization.
  LibCallOptimization(const char *FName, const char *Description)
    : FunctionName(FName) {
      
    occurrences.construct("simplify-libcalls", Description);
    // Register this optimizer in the list of optimizations.
    Next = OptList;
    OptList = this;
    Prev = &OptList;
    if (Next) Next->Prev = &Next;
  
  /// getNext - All libcall optimizations are chained together into a list,
  /// return the next one in the list.
  LibCallOptimization *getNext() { return Next; }
Reid Spencer's avatar
Reid Spencer committed
  /// @brief Deregister from the optlist
  virtual ~LibCallOptimization() {
    *Prev = Next;
    if (Next) Next->Prev = Prev;
  }
Reid Spencer's avatar
Reid Spencer committed

  /// The implementation of this function in subclasses should determine if
  /// \p F is suitable for the optimization. This method is called by
  /// SimplifyLibCalls::runOnModule to short circuit visiting all the call
  /// sites of such a function if that function is not suitable in the first
Reid Spencer's avatar
Reid Spencer committed
  /// place.  If the called function is suitabe, this method should return true;
  /// false, otherwise. This function should also perform any lazy
  /// initialization that the LibCallOptimization needs to do, if its to return
Reid Spencer's avatar
Reid Spencer committed
  /// true. This avoids doing initialization until the optimizer is actually
  /// going to be called upon to do some optimization.
Reid Spencer's avatar
Reid Spencer committed
  /// @brief Determine if the function is suitable for optimization
Reid Spencer's avatar
Reid Spencer committed
  virtual bool ValidateCalledFunction(
    const Function* F,    ///< The function that is the target of call sites
    SimplifyLibCalls& SLC ///< The pass object invoking us
  ) = 0;

  /// The implementations of this function in subclasses is the heart of the
  /// SimplifyLibCalls algorithm. Sublcasses of this class implement
Reid Spencer's avatar
Reid Spencer committed
  /// OptimizeCall to determine if (a) the conditions are right for optimizing
  /// the call and (b) to perform the optimization. If an action is taken
Reid Spencer's avatar
Reid Spencer committed
  /// against ci, the subclass is responsible for returning true and ensuring
  /// that ci is erased from its parent.
  /// @brief Optimize a call, if possible.
  virtual bool OptimizeCall(
    CallInst* ci,          ///< The call instruction that should be optimized.
    SimplifyLibCalls& SLC  ///< The pass object invoking us
  ) = 0;

  /// @brief Get the name of the library call being optimized
  const char *getFunctionName() const { return FunctionName; }
  bool ReplaceCallWith(CallInst *CI, Value *V) {
    if (!CI->use_empty())
      CI->replaceAllUsesWith(V);
    CI->eraseFromParent();
    return true;
  }
  
Reid Spencer's avatar
Reid Spencer committed
  /// @brief Called by SimplifyLibCalls to update the occurrences statistic.
  void succeeded() {
Reid Spencer's avatar
Reid Spencer committed
#ifndef NDEBUG
    DEBUG(++occurrences);
Reid Spencer's avatar
Reid Spencer committed
#endif
/// This class is an LLVM Pass that applies each of the LibCallOptimization
Reid Spencer's avatar
Reid Spencer committed
/// instances to all the call sites in a module, relatively efficiently. The
/// purpose of this pass is to provide optimizations for calls to well-known
Reid Spencer's avatar
Reid Spencer committed
/// functions with well-known semantics, such as those in the c library. The
/// class provides the basic infrastructure for handling runOnModule.  Whenever
/// this pass finds a function call, it asks the appropriate optimizer to
Reid Spencer's avatar
Reid Spencer committed
/// validate the call (ValidateLibraryCall). If it is validated, then
/// the OptimizeCall method is also called.
Reid Spencer's avatar
Reid Spencer committed
/// @brief A ModulePass for optimizing well-known function calls.
class VISIBILITY_HIDDEN SimplifyLibCalls : public ModulePass {
Nick Lewycky's avatar
Nick Lewycky committed
  static char ID; // Pass identification, replacement for typeid
  SimplifyLibCalls() : ModulePass((intptr_t)&ID) {}

Reid Spencer's avatar
Reid Spencer committed
  /// We need some target data for accurate signature details that are
  /// target dependent. So we require target data in our AnalysisUsage.
Reid Spencer's avatar
Reid Spencer committed
  /// @brief Require TargetData from AnalysisUsage.
  virtual void getAnalysisUsage(AnalysisUsage& Info) const {
Reid Spencer's avatar
Reid Spencer committed
    // Ask that the TargetData analysis be performed before us so we can use
    // the target data.
    Info.addRequired<TargetData>();
  }
Reid Spencer's avatar
Reid Spencer committed
  /// For this pass, process all of the function calls in the module, calling
  /// ValidateLibraryCall and OptimizeCall as appropriate.
Reid Spencer's avatar
Reid Spencer committed
  /// @brief Run all the lib call optimizations on a Module.
  virtual bool runOnModule(Module &M) {
Reid Spencer's avatar
Reid Spencer committed
    reset(M);
Reid Spencer's avatar
Reid Spencer committed
    bool result = false;
    StringMap<LibCallOptimization*> OptznMap;
    for (LibCallOptimization *Optzn = OptList; Optzn; Optzn = Optzn->getNext())
      OptznMap[Optzn->getFunctionName()] = Optzn;
Reid Spencer's avatar
Reid Spencer committed
    // The call optimizations can be recursive. That is, the optimization might
    // generate a call to another function which can also be optimized. This way
    // we make the LibCallOptimization instances very specific to the case they
    // handle. It also means we need to keep running over the function calls in
Reid Spencer's avatar
Reid Spencer committed
    // the module until we don't get any more optimizations possible.
    bool found_optimization = false;
Reid Spencer's avatar
Reid Spencer committed
      found_optimization = false;
      for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI) {
Reid Spencer's avatar
Reid Spencer committed
        // All the "well-known" functions are external and have external linkage
        // because they live in a runtime library somewhere and were (probably)
        // not compiled by LLVM.  So, we only act on external functions that
        // have external or dllimport linkage and non-empty uses.
            !(FI->hasExternalLinkage() || FI->hasDLLImportLinkage()) ||
            FI->use_empty())
Reid Spencer's avatar
Reid Spencer committed
          continue;

        // Get the optimization class that pertains to this function
        StringMap<LibCallOptimization*>::iterator OMI =
          OptznMap.find(FI->getName());
        if (OMI == OptznMap.end()) continue;
        
        LibCallOptimization *CO = OMI->second;
Reid Spencer's avatar
Reid Spencer committed

        // Make sure the called function is suitable for the optimization
        if (!CO->ValidateCalledFunction(FI, *this))
Reid Spencer's avatar
Reid Spencer committed
          continue;

        // Loop over each of the uses of the function
        for (Value::use_iterator UI = FI->use_begin(), UE = FI->use_end();
Reid Spencer's avatar
Reid Spencer committed
          // If the use of the function is a call instruction
          if (CallInst* CI = dyn_cast<CallInst>(*UI++)) {
Reid Spencer's avatar
Reid Spencer committed
            // Do the optimization on the LibCallOptimization.
            if (CO->OptimizeCall(CI, *this)) {
Reid Spencer's avatar
Reid Spencer committed
              ++SimplifiedLibCalls;
              found_optimization = result = true;
Reid Spencer's avatar
Reid Spencer committed
              CO->succeeded();
Reid Spencer's avatar
Reid Spencer committed
    } while (found_optimization);
Reid Spencer's avatar
Reid Spencer committed
    return result;
  }
Reid Spencer's avatar
Reid Spencer committed
  /// @brief Return the *current* module we're working on.
  Module* getModule() const { return M; }
Reid Spencer's avatar
Reid Spencer committed

  /// @brief Return the *current* target data for the module we're working on.
  TargetData* getTargetData() const { return TD; }

  /// @brief Return the size_t type -- syntactic shortcut
  const Type* getIntPtrType() const { return TD->getIntPtrType(); }

  /// @brief Return a Function* for the putchar libcall
    if (!putchar_func)
Reid Spencer's avatar
Reid Spencer committed
      putchar_func = 
        M->getOrInsertFunction("putchar", Type::Int32Ty, Type::Int32Ty, NULL);
    return putchar_func;
  }

  /// @brief Return a Function* for the puts libcall
    if (!puts_func)
Reid Spencer's avatar
Reid Spencer committed
      puts_func = M->getOrInsertFunction("puts", Type::Int32Ty,
                                         NULL);
    return puts_func;
  }

  /// @brief Return a Function* for the fputc libcall
  Constant *get_fputc(const Type* FILEptr_type) {
Reid Spencer's avatar
Reid Spencer committed
      fputc_func = M->getOrInsertFunction("fputc", Type::Int32Ty, Type::Int32Ty,
                                          FILEptr_type, NULL);
  /// @brief Return a Function* for the fputs libcall
  Constant *get_fputs(const Type* FILEptr_type) {
Reid Spencer's avatar
Reid Spencer committed
      fputs_func = M->getOrInsertFunction("fputs", Type::Int32Ty,
                                          FILEptr_type, NULL);
    return fputs_func;
  }

  /// @brief Return a Function* for the fwrite libcall
  Constant *get_fwrite(const Type* FILEptr_type) {
      fwrite_func = M->getOrInsertFunction("fwrite", TD->getIntPtrType(),
                                           TD->getIntPtrType(),
                                           TD->getIntPtrType(),
                                           FILEptr_type, NULL);
    return fwrite_func;
  }

  /// @brief Return a Function* for the sqrt libcall
      sqrt_func = M->getOrInsertFunction("sqrt", Type::DoubleTy, 
                                         Type::DoubleTy, NULL);
Owen Anderson's avatar
Owen Anderson committed
  /// @brief Return a Function* for the strcpy libcall
      strcpy_func = M->getOrInsertFunction("strcpy",
                                           PointerType::getUnqual(Type::Int8Ty),
                                           PointerType::getUnqual(Type::Int8Ty),
                                           PointerType::getUnqual(Type::Int8Ty),
Reid Spencer's avatar
Reid Spencer committed
  /// @brief Return a Function* for the strlen libcall
Reid Spencer's avatar
Reid Spencer committed
    if (!strlen_func)
      strlen_func = M->getOrInsertFunction("strlen", TD->getIntPtrType(),
Reid Spencer's avatar
Reid Spencer committed
    return strlen_func;
  /// @brief Return a Function* for the memchr libcall
      memchr_func = M->getOrInsertFunction("memchr",
                                           PointerType::getUnqual(Type::Int8Ty),
                                           PointerType::getUnqual(Type::Int8Ty),
Reid Spencer's avatar
Reid Spencer committed
                                           Type::Int32Ty, TD->getIntPtrType(),
Reid Spencer's avatar
Reid Spencer committed
  /// @brief Return a Function* for the memcpy libcall
      Intrinsic::ID IID = (TD->getIntPtrType() == Type::Int32Ty) ?
        Intrinsic::memcpy_i32 : Intrinsic::memcpy_i64;
      memcpy_func = Intrinsic::getDeclaration(M, IID);
Reid Spencer's avatar
Reid Spencer committed
    return memcpy_func;
  Constant *getUnaryFloatFunction(const char *BaseName, const Type *T = 0) {
    if (T == 0) T = Type::FloatTy;
    
    char NameBuffer[20];
    const char *Name;
    if (T == Type::DoubleTy)
      Name = BaseName;              // floor
    else {
      Name = NameBuffer;
      unsigned NameLen = strlen(BaseName);
      assert(NameLen < sizeof(NameBuffer)-2 && "Buffer too small");
      memcpy(NameBuffer, BaseName, NameLen);
      if (T == Type::FloatTy)
        NameBuffer[NameLen] = 'f';  // floorf
      else
        NameBuffer[NameLen] = 'l';  // floorl
      NameBuffer[NameLen+1] = 0;
    }
    
    return M->getOrInsertFunction(Name, T, T, NULL);
  Constant *get_floorf() { return getUnaryFloatFunction("floor"); }
  Constant *get_ceilf()  { return getUnaryFloatFunction( "ceil"); }
  Constant *get_roundf() { return getUnaryFloatFunction("round"); }
  Constant *get_rintf()  { return getUnaryFloatFunction( "rint"); }
  Constant *get_nearbyintf() { return getUnaryFloatFunction("nearbyint"); }

  
  
Reid Spencer's avatar
Reid Spencer committed
private:
Reid Spencer's avatar
Reid Spencer committed
  /// @brief Reset our cached data for a new Module
  void reset(Module& mod) {
Reid Spencer's avatar
Reid Spencer committed
    M = &mod;
    TD = &getAnalysis<TargetData>();
    putchar_func = 0;
    puts_func = 0;
Reid Spencer's avatar
Reid Spencer committed
    memcpy_func = 0;
Reid Spencer's avatar
Reid Spencer committed
    strlen_func = 0;
  }
Reid Spencer's avatar
Reid Spencer committed
private:
  /// Caches for function pointers.
  Constant *putchar_func, *puts_func;
  Constant *fputc_func, *fputs_func, *fwrite_func;
  Constant *memcpy_func, *memchr_func;
  Constant *sqrt_func;
  Constant *strcpy_func, *strlen_func;
  Module *M;             ///< Cached Module
  TargetData *TD;        ///< Cached TargetData
Devang Patel's avatar
Devang Patel committed
char SimplifyLibCalls::ID = 0;
Reid Spencer's avatar
Reid Spencer committed
// Register the pass
RegisterPass<SimplifyLibCalls>
X("simplify-libcalls", "Simplify well-known library calls");
Reid Spencer's avatar
Reid Spencer committed
} // anonymous namespace
Reid Spencer's avatar
Reid Spencer committed
// The only public symbol in this file which just instantiates the pass object
ModulePass *llvm::createSimplifyLibCallsPass() {
  return new SimplifyLibCalls();
// Forward declare utility functions.
static bool GetConstantStringInfo(Value *V, std::string &Str);
static Value *CastToCStr(Value *V, Instruction *IP);
static uint64_t GetStringLength(Value *V);


Reid Spencer's avatar
Reid Spencer committed
// Classes below here, in the anonymous namespace, are all subclasses of the
// LibCallOptimization class, each implementing all optimizations possible for a
// single well-known library call. Each has a static singleton instance that
// auto registers it into the "optlist" global above.
Reid Spencer's avatar
Reid Spencer committed
namespace {
Reid Spencer's avatar
Reid Spencer committed
/// This LibCallOptimization will find instances of a call to "exit" that occurs
/// within the "main" function and change it to a simple "ret" instruction with
Reid Spencer's avatar
Reid Spencer committed
/// the same value passed to the exit function. When this is done, it splits the
/// basic block at the exit(3) call and deletes the call instruction.
/// @brief Replace calls to exit in main with a simple return
struct VISIBILITY_HIDDEN ExitInMainOptimization : public LibCallOptimization {
  ExitInMainOptimization() : LibCallOptimization("exit",
      "Number of 'exit' calls simplified") {}

  // Make sure the called function looks like exit (int argument, int return
  // type, external linkage, not varargs).
  virtual bool ValidateCalledFunction(const Function *F, SimplifyLibCalls &SLC){
    return F->arg_size() >= 1 && F->arg_begin()->getType()->isInteger();
  virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC) {
    // To be careful, we check that the call to exit is coming from "main", that
    // main has external linkage, and the return type of main and the argument
    // to exit have the same type.
    Function *from = ci->getParent()->getParent();
    if (from->hasExternalLinkage())
      if (from->getReturnType() == ci->getOperand(1)->getType()
          && !isa<StructType>(from->getReturnType()))
        if (from->getName() == "main") {
          // Okay, time to actually do the optimization. First, get the basic
          // block of the call instruction
          BasicBlock* bb = ci->getParent();
Reid Spencer's avatar
Reid Spencer committed

          // Create a return instruction that we'll replace the call with.
          // Note that the argument of the return is the argument of the call
          ReturnInst::Create(ci->getOperand(1), ci);
Reid Spencer's avatar
Reid Spencer committed

          // Split the block at the call instruction which places it in a new
          // basic block.
          bb->splitBasicBlock(ci);
Reid Spencer's avatar
Reid Spencer committed

          // The block split caused a branch instruction to be inserted into
          // the end of the original block, right after the return instruction
          // that we put there. That's not a valid block, so delete the branch
          // instruction.
          bb->getInstList().pop_back();
Reid Spencer's avatar
Reid Spencer committed

          // Now we can finally get rid of the call instruction which now lives
          // in the new basic block.
          ci->eraseFromParent();

          // Optimization succeeded, return true.
          return true;
        }
    // We didn't pass the criteria for this optimization so return false
    return false;
Reid Spencer's avatar
Reid Spencer committed
  }
} ExitInMainOptimizer;

/// This LibCallOptimization will simplify a call to the strcat library
/// function. The simplification is possible only if the string being
/// concatenated is a constant array or a constant expression that results in
/// a constant string. In this case we can replace it with strlen + llvm.memcpy
Reid Spencer's avatar
Reid Spencer committed
/// of the constant string. Both of these calls are further reduced, if possible
/// on subsequent passes.
/// @brief Simplify the strcat library function.
struct VISIBILITY_HIDDEN StrCatOptimization : public LibCallOptimization {
public:
Reid Spencer's avatar
Reid Spencer committed
  /// @brief Default constructor
  StrCatOptimization() : LibCallOptimization("strcat",
      "Number of 'strcat' calls simplified") {}
Reid Spencer's avatar
Reid Spencer committed
public:
  /// @brief Make sure that the "strcat" function has the right prototype
  virtual bool ValidateCalledFunction(const Function *F, SimplifyLibCalls &SLC){
    const FunctionType *FT = F->getFunctionType();
    return FT->getNumParams() == 2 &&
           FT->getReturnType() == PointerType::getUnqual(Type::Int8Ty) &&
           FT->getParamType(0) == FT->getReturnType() &&
           FT->getParamType(1) == FT->getReturnType();
Reid Spencer's avatar
Reid Spencer committed
  /// @brief Optimize the strcat library function
  virtual bool OptimizeCall(CallInst *CI, SimplifyLibCalls &SLC) {
Reid Spencer's avatar
Reid Spencer committed
    // Extract some information from the instruction
    Value *Dst = CI->getOperand(1);
    Value *Src = CI->getOperand(2);
Reid Spencer's avatar
Reid Spencer committed

    // See if we can get the length of the input string.
    uint64_t Len = GetStringLength(Src);
    if (Len == 0) return false;
    --Len;  // Unbias length.
    
    // Handle the simple, do-nothing case
    // We need to find the end of the destination string.  That's where the
    // memory is to be moved to. We just generate a call to strlen.
    CallInst *DstLen = CallInst::Create(SLC.get_strlen(), Dst,
                                        Dst->getName()+".len", CI);
    // Now that we have the destination's length, we must index into the
    // destination's pointer to get the actual memcpy destination (end of
    // the string .. we're concatenating).
    Dst = GetElementPtrInst::Create(Dst, DstLen, Dst->getName()+".indexed", CI);

    // We have enough information to now generate the memcpy call to
    // do the concatenation for us.
    Value *Vals[] = {
      Dst, Src,
      ConstantInt::get(SLC.getIntPtrType(), Len+1), // copy nul byte.
      ConstantInt::get(Type::Int32Ty, 1)  // alignment
    };
    CallInst::Create(SLC.get_memcpy(), Vals, Vals + 4, "", CI);
Reid Spencer's avatar
Reid Spencer committed
  }
} StrCatOptimizer;
/// This LibCallOptimization will simplify a call to the strchr library
/// function.  It optimizes out cases where the arguments are both constant
/// and the result can be determined statically.
/// @brief Simplify the strcmp library function.
struct VISIBILITY_HIDDEN StrChrOptimization : public LibCallOptimization {
public:
  StrChrOptimization() : LibCallOptimization("strchr",
      "Number of 'strchr' calls simplified") {}

  /// @brief Make sure that the "strchr" function has the right prototype
  virtual bool ValidateCalledFunction(const Function *F, SimplifyLibCalls &SLC){
    const FunctionType *FT = F->getFunctionType();
    return FT->getNumParams() == 2 &&
           FT->getReturnType() == PointerType::getUnqual(Type::Int8Ty) &&
           FT->getParamType(0) == FT->getReturnType() &&
           isa<IntegerType>(FT->getParamType(1));
  /// @brief Perform the strchr optimizations
  virtual bool OptimizeCall(CallInst *CI, SimplifyLibCalls &SLC) {
    Value *SrcStr = CI->getOperand(1);
    // If the second operand is not constant, see if we can compute the length
    // and turn this into memchr.
    ConstantInt *CSI = dyn_cast<ConstantInt>(CI->getOperand(2));
    if (CSI == 0) {
      uint64_t Len = GetStringLength(SrcStr);
      if (Len == 0) return false;
      
      Value *Args[3] = {
        CI->getOperand(1),
        CI->getOperand(2),
        ConstantInt::get(SLC.getIntPtrType(), Len) // include nul.
      return ReplaceCallWith(CI, CallInst::Create(SLC.get_memchr(),
                                                  Args, Args + 3,
                                                  CI->getName(), CI));
    
    // Otherwise, the character is a constant, see if the first argument is
    // a string literal.  If so, we can constant fold.
    std::string Str;
    if (!GetConstantStringInfo(SrcStr, Str))
      return false;
    // strchr can find the nul character.
    Str += '\0';
    // Get the character we're looking for
    char CharValue = CSI->getSExtValue();

      if (i == Str.size())    // Didn't find the char.  strchr returns null.
        return ReplaceCallWith(CI, Constant::getNullValue(CI->getType()));
      // Did we find our match?
      if (Str[i] == CharValue)
        break;
    // strchr(s+n,c)  -> gep(s+n+i,c)
    //    (if c is a constant integer and s is a constant string)
    Value *Idx = ConstantInt::get(Type::Int64Ty, i);
    Value *GEP = GetElementPtrInst::Create(CI->getOperand(1), Idx, 
                                           CI->getOperand(1)->getName() +
                                           ".strchr", CI);
/// This LibCallOptimization will simplify a call to the strcmp library
/// function.  It optimizes out cases where one or both arguments are constant
/// and the result can be determined statically.
/// @brief Simplify the strcmp library function.
struct VISIBILITY_HIDDEN StrCmpOptimization : public LibCallOptimization {
  StrCmpOptimization() : LibCallOptimization("strcmp",
      "Number of 'strcmp' calls simplified") {}
  /// @brief Make sure that the "strcmp" function has the right prototype
  virtual bool ValidateCalledFunction(const Function *F, SimplifyLibCalls &SLC){
    const FunctionType *FT = F->getFunctionType();
    return FT->getReturnType() == Type::Int32Ty && FT->getNumParams() == 2 &&
           FT->getParamType(0) == FT->getParamType(1) &&
           FT->getParamType(0) == PointerType::getUnqual(Type::Int8Ty);
  /// @brief Perform the strcmp optimization
  virtual bool OptimizeCall(CallInst *CI, SimplifyLibCalls &SLC) {
    // First, check to see if src and destination are the same. If they are,
    // then the optimization is to replace the CallInst with a constant 0
    // because the call is a no-op.
    Value *Str1P = CI->getOperand(1);
    Value *Str2P = CI->getOperand(2);
    if (Str1P == Str2P)      // strcmp(x,x)  -> 0
      return ReplaceCallWith(CI, ConstantInt::get(CI->getType(), 0));
    std::string Str1;
    if (!GetConstantStringInfo(Str1P, Str1))
      return false;
    if (Str1.empty()) {
      // strcmp("", x) -> *x
      Value *V = new LoadInst(Str2P, CI->getName()+".load", CI);
      V = new ZExtInst(V, CI->getType(), CI->getName()+".int", CI);
    std::string Str2;
    if (!GetConstantStringInfo(Str2P, Str2))
      return false;
    if (Str2.empty()) {
      // strcmp(x,"") -> *x
      Value *V = new LoadInst(Str1P, CI->getName()+".load", CI);
      V = new ZExtInst(V, CI->getType(), CI->getName()+".int", CI);
    // strcmp(x, y)  -> cnst  (if both x and y are constant strings)
    int R = strcmp(Str1.c_str(), Str2.c_str());
    return ReplaceCallWith(CI, ConstantInt::get(CI->getType(), R));
/// This LibCallOptimization will simplify a call to the strncmp library
/// function.  It optimizes out cases where one or both arguments are constant
/// and the result can be determined statically.
/// @brief Simplify the strncmp library function.
struct VISIBILITY_HIDDEN StrNCmpOptimization : public LibCallOptimization {
  StrNCmpOptimization() : LibCallOptimization("strncmp",
      "Number of 'strncmp' calls simplified") {}
  /// @brief Make sure that the "strncmp" function has the right prototype
Chris Lattner's avatar
Chris Lattner committed
  virtual bool ValidateCalledFunction(const Function *F, SimplifyLibCalls &SLC){
    const FunctionType *FT = F->getFunctionType();
    return FT->getReturnType() == Type::Int32Ty && FT->getNumParams() == 3 &&
           FT->getParamType(0) == FT->getParamType(1) &&
           FT->getParamType(0) == PointerType::getUnqual(Type::Int8Ty) &&
Chris Lattner's avatar
Chris Lattner committed
           isa<IntegerType>(FT->getParamType(2));
Chris Lattner's avatar
Chris Lattner committed
  /// @brief Perform the strncmp optimization
  virtual bool OptimizeCall(CallInst *CI, SimplifyLibCalls &SLC) {
    // First, check to see if src and destination are the same. If they are,
    // then the optimization is to replace the CallInst with a constant 0
    // because the call is a no-op.
Chris Lattner's avatar
Chris Lattner committed
    Value *Str1P = CI->getOperand(1);
    Value *Str2P = CI->getOperand(2);
    if (Str1P == Str2P)  // strncmp(x,x, n)  -> 0
      return ReplaceCallWith(CI, ConstantInt::get(CI->getType(), 0));
    // Check the length argument, if it is Constant zero then the strings are
    // considered equal.
    uint64_t Length;
    if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getOperand(3)))
      Length = LengthArg->getZExtValue();
    else
      return false;
    
    if (Length == 0) // strncmp(x,y,0)   -> 0
      return ReplaceCallWith(CI, ConstantInt::get(CI->getType(), 0));
    std::string Str1;
    if (!GetConstantStringInfo(Str1P, Str1))
      return false;
    if (Str1.empty()) {
      // strncmp("", x, n) -> *x
Chris Lattner's avatar
Chris Lattner committed
      Value *V = new LoadInst(Str2P, CI->getName()+".load", CI);
      V = new ZExtInst(V, CI->getType(), CI->getName()+".int", CI);
    std::string Str2;
    if (!GetConstantStringInfo(Str2P, Str2))
      return false;
    if (Str2.empty()) {
      // strncmp(x, "", n) -> *x
Chris Lattner's avatar
Chris Lattner committed
      Value *V = new LoadInst(Str1P, CI->getName()+".load", CI);
      V = new ZExtInst(V, CI->getType(), CI->getName()+".int", CI);
    // strncmp(x, y, n)  -> cnst  (if both x and y are constant strings)
    int R = strncmp(Str1.c_str(), Str2.c_str(), Length);
    return ReplaceCallWith(CI, ConstantInt::get(CI->getType(), R));
/// This LibCallOptimization will simplify a call to the strcpy library
/// function.  Two optimizations are possible:
Reid Spencer's avatar
Reid Spencer committed
/// (1) If src and dest are the same and not volatile, just return dest
/// (2) If the src is a constant then we can convert to llvm.memmove
/// @brief Simplify the strcpy library function.
struct VISIBILITY_HIDDEN StrCpyOptimization : public LibCallOptimization {
Reid Spencer's avatar
Reid Spencer committed
public:
  StrCpyOptimization() : LibCallOptimization("strcpy",
      "Number of 'strcpy' calls simplified") {}
Reid Spencer's avatar
Reid Spencer committed

  /// @brief Make sure that the "strcpy" function has the right prototype
  virtual bool ValidateCalledFunction(const Function *F, SimplifyLibCalls &SLC){
    const FunctionType *FT = F->getFunctionType();
    return FT->getNumParams() == 2 &&
           FT->getParamType(0) == FT->getParamType(1) &&
           FT->getReturnType() == FT->getParamType(0) &&
           FT->getParamType(0) == PointerType::getUnqual(Type::Int8Ty);
Reid Spencer's avatar
Reid Spencer committed
  }

  /// @brief Perform the strcpy optimization
  virtual bool OptimizeCall(CallInst *CI, SimplifyLibCalls &SLC) {
Reid Spencer's avatar
Reid Spencer committed
    // First, check to see if src and destination are the same. If they are,
    // then the optimization is to replace the CallInst with the destination
    // because the call is a no-op. Note that this corresponds to the
Reid Spencer's avatar
Reid Spencer committed
    // degenerate strcpy(X,X) case which should have "undefined" results
    // according to the C specification. However, it occurs sometimes and
    // we optimize it as a no-op.
    Value *Dst = CI->getOperand(1);
    Value *Src = CI->getOperand(2);
    if (Dst == Src) {
      // strcpy(x, x) -> x
    // See if we can get the length of the input string.
    uint64_t Len = GetStringLength(Src);
    if (Len == 0) return false;
    --Len;  // Unbias length.
Reid Spencer's avatar
Reid Spencer committed
    // If the constant string's length is zero we can optimize this by just
    // doing a store of 0 at the first byte of the destination.
    if (Len == 0) {
      new StoreInst(ConstantInt::get(Type::Int8Ty, 0), Dst, CI);
Reid Spencer's avatar
Reid Spencer committed
    }

    // We have enough information to now generate the memcpy call to
    // do the concatenation for us.
    Value *MemcpyOps[] = {
      Dst, Src,
      ConstantInt::get(SLC.getIntPtrType(), Len+1),// Length including nul byte.
      ConstantInt::get(Type::Int32Ty, 1) // alignment
    };
    CallInst::Create(SLC.get_memcpy(), MemcpyOps, MemcpyOps + 4, "", CI);
Reid Spencer's avatar
Reid Spencer committed
  }
} StrCpyOptimizer;

/// This LibCallOptimization will simplify a call to the strlen library
/// function by replacing it with a constant value if the string provided to
Reid Spencer's avatar
Reid Spencer committed
/// it is a constant array.
/// @brief Simplify the strlen library function.
struct VISIBILITY_HIDDEN StrLenOptimization : public LibCallOptimization {
  StrLenOptimization() : LibCallOptimization("strlen",
      "Number of 'strlen' calls simplified") {}

  /// @brief Make sure that the "strlen" function has the right prototype
  virtual bool ValidateCalledFunction(const Function *F, SimplifyLibCalls &SLC){
    const FunctionType *FT = F->getFunctionType();
    return FT->getNumParams() == 1 &&
           FT->getParamType(0) == PointerType::getUnqual(Type::Int8Ty) &&
           isa<IntegerType>(FT->getReturnType());
  }

  /// @brief Perform the strlen optimization
  virtual bool OptimizeCall(CallInst *CI, SimplifyLibCalls &SLC) {
    // Make sure we're dealing with an sbyte* here.

    // Does the call to strlen have exactly one use?
    if (CI->hasOneUse()) {
Reid Spencer's avatar
Reid Spencer committed
      // Is that single use a icmp operator?
      if (ICmpInst *Cmp = dyn_cast<ICmpInst>(CI->use_back()))
        // Is it compared against a constant integer?
        if (ConstantInt *Cst = dyn_cast<ConstantInt>(Cmp->getOperand(1))) {
          // If its compared against length 0 with == or !=
          if (Cst->getZExtValue() == 0 && Cmp->isEquality()) {
            // strlen(x) != 0 -> *x != 0
            // strlen(x) == 0 -> *x == 0
            Value *V = new LoadInst(Src, Src->getName()+".first", CI);
            V = new ICmpInst(Cmp->getPredicate(), V, 
                             ConstantInt::get(Type::Int8Ty, 0),
                             Cmp->getName()+".strlen", CI);
            Cmp->replaceAllUsesWith(V);
            Cmp->eraseFromParent();
            return ReplaceCallWith(CI, 0);  // no uses.

    // Get the length of the constant string operand
    // strlen("xyz") -> 3 (for example)
    if (uint64_t Len = GetStringLength(Src))
      return ReplaceCallWith(CI, ConstantInt::get(CI->getType(), Len-1));
    return false;
  }
} StrLenOptimizer;

/// IsOnlyUsedInEqualsComparison - Return true if it only matters that the value
/// is equal or not-equal to zero. 
static bool IsOnlyUsedInEqualsZeroComparison(Instruction *I) {
  for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
       UI != E; ++UI) {
    if (ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
      if (IC->isEquality())
        if (Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
          if (C->isNullValue())
            continue;
    // Unknown instruction.
    return false;
  }
  return true;
}

/// This memcmpOptimization will simplify a call to the memcmp library
/// function.
struct VISIBILITY_HIDDEN memcmpOptimization : public LibCallOptimization {
  /// @brief Default Constructor
  memcmpOptimization()
    : LibCallOptimization("memcmp", "Number of 'memcmp' calls simplified") {}
  
  /// @brief Make sure that the "memcmp" function has the right prototype
  virtual bool ValidateCalledFunction(const Function *F, SimplifyLibCalls &TD) {
    Function::const_arg_iterator AI = F->arg_begin();
    if (F->arg_size() != 3 || !isa<PointerType>(AI->getType())) return false;
    if (!isa<PointerType>((++AI)->getType())) return false;
    if (!(++AI)->getType()->isInteger()) return false;
    if (!F->getReturnType()->isInteger()) return false;
    return true;
  }
  
  /// Because of alignment and instruction information that we don't have, we
  /// leave the bulk of this to the code generators.
  ///
  /// Note that we could do much more if we could force alignment on otherwise
  /// small aligned allocas, or if we could indicate that loads have a small
  /// alignment.
  virtual bool OptimizeCall(CallInst *CI, SimplifyLibCalls &TD) {
    Value *LHS = CI->getOperand(1), *RHS = CI->getOperand(2);

    // If the two operands are the same, return zero.
    if (LHS == RHS) {
      // memcmp(s,s,x) -> 0
      return ReplaceCallWith(CI, Constant::getNullValue(CI->getType()));
    }
    
    // Make sure we have a constant length.
    ConstantInt *LenC = dyn_cast<ConstantInt>(CI->getOperand(3));
    if (!LenC) return false;
Reid Spencer's avatar
Reid Spencer committed
    uint64_t Len = LenC->getZExtValue();
      
    // If the length is zero, this returns 0.
    switch (Len) {
    case 0:
      // memcmp(s1,s2,0) -> 0
      return ReplaceCallWith(CI, Constant::getNullValue(CI->getType()));
    case 1: {
      // memcmp(S1,S2,1) -> *(ubyte*)S1 - *(ubyte*)S2
      const Type *UCharPtr = PointerType::getUnqual(Type::Int8Ty);
Reid Spencer's avatar
Reid Spencer committed
      CastInst *Op1Cast = CastInst::create(
          Instruction::BitCast, LHS, UCharPtr, LHS->getName(), CI);
      CastInst *Op2Cast = CastInst::create(
          Instruction::BitCast, RHS, UCharPtr, RHS->getName(), CI);
      Value *S1V = new LoadInst(Op1Cast, LHS->getName()+".val", CI);
      Value *S2V = new LoadInst(Op2Cast, RHS->getName()+".val", CI);
      Value *RV = BinaryOperator::createSub(S1V, S2V, CI->getName()+".diff",CI);
      if (RV->getType() != CI->getType())
        RV = CastInst::createIntegerCast(RV, CI->getType(), false, 
                                         RV->getName(), CI);
    }
    case 2:
      if (IsOnlyUsedInEqualsZeroComparison(CI)) {
        // TODO: IF both are aligned, use a short load/compare.
      
        // memcmp(S1,S2,2) -> S1[0]-S2[0] | S1[1]-S2[1] iff only ==/!= 0 matters
        const Type *UCharPtr = PointerType::getUnqual(Type::Int8Ty);
Reid Spencer's avatar
Reid Spencer committed
        CastInst *Op1Cast = CastInst::create(
            Instruction::BitCast, LHS, UCharPtr, LHS->getName(), CI);
        CastInst *Op2Cast = CastInst::create(
            Instruction::BitCast, RHS, UCharPtr, RHS->getName(), CI);
        Value *S1V1 = new LoadInst(Op1Cast, LHS->getName()+".val1", CI);
        Value *S2V1 = new LoadInst(Op2Cast, RHS->getName()+".val1", CI);
        Value *D1 = BinaryOperator::createSub(S1V1, S2V1,
                                              CI->getName()+".d1", CI);
Reid Spencer's avatar
Reid Spencer committed
        Constant *One = ConstantInt::get(Type::Int32Ty, 1);
        Value *G1 = GetElementPtrInst::Create(Op1Cast, One, "next1v", CI);
        Value *G2 = GetElementPtrInst::Create(Op2Cast, One, "next2v", CI);
        Value *S1V2 = new LoadInst(G1, LHS->getName()+".val2", CI);
        Value *S2V2 = new LoadInst(G2, RHS->getName()+".val2", CI);
        Value *D2 = BinaryOperator::createSub(S1V2, S2V2,
                                              CI->getName()+".d1", CI);
        Value *Or = BinaryOperator::createOr(D1, D2, CI->getName()+".res", CI);
        if (Or->getType() != CI->getType())
          Or = CastInst::createIntegerCast(Or, CI->getType(), false /*ZExt*/, 
                                           Or->getName(), CI);
      }
      break;
    default:
      break;
    }
    
    return false;
  }
} memcmpOptimizer;

/// This LibCallOptimization will simplify a call to the memcpy library
/// function.  It simply converts them into calls to llvm.memcpy.*;
/// the resulting call should be optimized later.
/// @brief Simplify the memcpy library function.
struct VISIBILITY_HIDDEN MemCpyOptimization : public LibCallOptimization {
public:
  MemCpyOptimization() : LibCallOptimization("memcpy",
      "Number of 'memcpy' calls simplified") {}

  /// @brief Make sure that the "memcpy" function has the right prototype
  virtual bool ValidateCalledFunction(const Function *F, SimplifyLibCalls &SLC){
    const FunctionType *FT = F->getFunctionType();
    const Type* voidPtr = PointerType::getUnqual(Type::Int8Ty);
    return FT->getReturnType() == voidPtr && FT->getNumParams() == 3 &&
           FT->getParamType(0) == voidPtr &&
           FT->getParamType(1) == voidPtr &&
           FT->getParamType(2) == SLC.getIntPtrType();
  }

  /// @brief Perform the memcpy optimization
  virtual bool OptimizeCall(CallInst *CI, SimplifyLibCalls &SLC) {
    Value *MemcpyOps[] = {
      CI->getOperand(1), CI->getOperand(2), CI->getOperand(3),
      ConstantInt::get(Type::Int32Ty, 1)   // align = 1 always.
    };
    CallInst::Create(SLC.get_memcpy(), MemcpyOps, MemcpyOps + 4, "", CI);
    // memcpy always returns the destination
    return ReplaceCallWith(CI, CI->getOperand(1));
  }
} MemCpyOptimizer;
/// This LibCallOptimization will simplify a call to the memcpy library
/// function by expanding it out to a single store of size 0, 1, 2, 4, or 8
Reid Spencer's avatar
Reid Spencer committed
/// bytes depending on the length of the string and the alignment. Additional
/// optimizations are possible in code generation (sequence of immediate store)
/// @brief Simplify the memcpy library function.
struct VISIBILITY_HIDDEN LLVMMemCpyMoveOptzn : public LibCallOptimization {
  LLVMMemCpyMoveOptzn(const char* fname, const char* desc)
  : LibCallOptimization(fname, desc) {}

  /// @brief Make sure that the "memcpy" function has the right prototype
  virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& TD) {
    // Just make sure this has 4 arguments per LLVM spec.
  /// Because of alignment and instruction information that we don't have, we
  /// leave the bulk of this to the code generators. The optimization here just
  /// deals with a few degenerate cases where the length of the string and the
  /// alignment match the sizes of our intrinsic types so we can do a load and
  /// store instead of the memcpy call.
  /// @brief Perform the memcpy optimization.
  virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& TD) {
    // Make sure we have constant int values to work with
    ConstantInt* LEN = dyn_cast<ConstantInt>(ci->getOperand(3));
    if (!LEN)
      return false;
    ConstantInt* ALIGN = dyn_cast<ConstantInt>(ci->getOperand(4));
    if (!ALIGN)
      return false;

    // If the length is larger than the alignment, we can't optimize