Skip to content
SimplifyLibCalls.cpp 90.4 KiB
Newer Older

struct VISIBILITY_HIDDEN MemSetOpt : public LibCallOptimization {
  virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
    const FunctionType *FT = Callee->getFunctionType();
    if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
        !isa<PointerType>(FT->getParamType(0)) ||
        !isa<IntegerType>(FT->getParamType(1)) ||
        FT->getParamType(2) != TD->getIntPtrType())
      return 0;

    // memset(p, v, n) -> llvm.memset(p, v, n, 1)
    Value *Val = B.CreateIntCast(CI->getOperand(2), Type::Int8Ty, false);
    EmitMemSet(CI->getOperand(1), Val,  CI->getOperand(3), B);
//===----------------------------------------------------------------------===//
// Math Library Optimizations
//===----------------------------------------------------------------------===//

//===---------------------------------------===//
// 'pow*' Optimizations

struct VISIBILITY_HIDDEN PowOpt : public LibCallOptimization {
  virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
    const FunctionType *FT = Callee->getFunctionType();
    // Just make sure this has 2 arguments of the same FP type, which match the
    // result type.
    if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
        FT->getParamType(0) != FT->getParamType(1) ||
        !FT->getParamType(0)->isFloatingPoint())
      return 0;
    
    Value *Op1 = CI->getOperand(1), *Op2 = CI->getOperand(2);
    if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1)) {
      if (Op1C->isExactlyValue(1.0))  // pow(1.0, x) -> 1.0
        return Op1C;
      if (Op1C->isExactlyValue(2.0))  // pow(2.0, x) -> exp2(x)
        return EmitUnaryFloatFnCall(Op2, "exp2", B);
    }
    
    ConstantFP *Op2C = dyn_cast<ConstantFP>(Op2);
    if (Op2C == 0) return 0;
    
    if (Op2C->getValueAPF().isZero())  // pow(x, 0.0) -> 1.0
      return ConstantFP::get(CI->getType(), 1.0);
    
    if (Op2C->isExactlyValue(0.5)) {
      // FIXME: This is not safe for -0.0 and -inf.  This can only be done when
      // 'unsafe' math optimizations are allowed.
      // x    pow(x, 0.5)  sqrt(x)
      // ---------------------------------------------
      // -0.0    +0.0       -0.0
      // -inf    +inf       NaN
#if 0
      // pow(x, 0.5) -> sqrt(x)
      return B.CreateCall(get_sqrt(), Op1, "sqrt");
#endif
    }
    
    if (Op2C->isExactlyValue(1.0))  // pow(x, 1.0) -> x
      return Op1;
    if (Op2C->isExactlyValue(2.0))  // pow(x, 2.0) -> x*x
      return B.CreateFMul(Op1, Op1, "pow2");
    if (Op2C->isExactlyValue(-1.0)) // pow(x, -1.0) -> 1.0/x
      return B.CreateFDiv(ConstantFP::get(CI->getType(), 1.0),
                          Op1, "powrecip");
//===---------------------------------------===//
// 'exp2' Optimizations

struct VISIBILITY_HIDDEN Exp2Opt : public LibCallOptimization {
  virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
    const FunctionType *FT = Callee->getFunctionType();
    // Just make sure this has 1 argument of FP type, which matches the
    // result type.
    if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
        !FT->getParamType(0)->isFloatingPoint())
      return 0;
    
    Value *Op = CI->getOperand(1);
    // Turn exp2(sitofp(x)) -> ldexp(1.0, sext(x))  if sizeof(x) <= 32
    // Turn exp2(uitofp(x)) -> ldexp(1.0, zext(x))  if sizeof(x) < 32
    Value *LdExpArg = 0;
    if (SIToFPInst *OpC = dyn_cast<SIToFPInst>(Op)) {
      if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() <= 32)
        LdExpArg = B.CreateSExt(OpC->getOperand(0), Type::Int32Ty, "tmp");
    } else if (UIToFPInst *OpC = dyn_cast<UIToFPInst>(Op)) {
      if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() < 32)
        LdExpArg = B.CreateZExt(OpC->getOperand(0), Type::Int32Ty, "tmp");
    }
    if (LdExpArg) {
      const char *Name;
      if (Op->getType() == Type::FloatTy)
        Name = "ldexpf";
      else if (Op->getType() == Type::DoubleTy)
        Name = "ldexp";
      else
        Name = "ldexpl";

      Constant *One = ConstantFP::get(*Context, APFloat(1.0f));
      if (Op->getType() != Type::FloatTy)
        One = ConstantExpr::getFPExtend(One, Op->getType());

      Module *M = Caller->getParent();
      Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
                                             Op->getType(), Type::Int32Ty,NULL);
      CallInst *CI = B.CreateCall2(Callee, One, LdExpArg);
      if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
        CI->setCallingConv(F->getCallingConv());

      return CI;
//===---------------------------------------===//
// Double -> Float Shrinking Optimizations for Unary Functions like 'floor'

struct VISIBILITY_HIDDEN UnaryDoubleFPOpt : public LibCallOptimization {
  virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
    const FunctionType *FT = Callee->getFunctionType();
    if (FT->getNumParams() != 1 || FT->getReturnType() != Type::DoubleTy ||
        FT->getParamType(0) != Type::DoubleTy)
      return 0;
    // If this is something like 'floor((double)floatval)', convert to floorf.
    FPExtInst *Cast = dyn_cast<FPExtInst>(CI->getOperand(1));
    if (Cast == 0 || Cast->getOperand(0)->getType() != Type::FloatTy)
      return 0;

    // floor((double)floatval) -> (double)floorf(floatval)
    Value *V = Cast->getOperand(0);
    V = EmitUnaryFloatFnCall(V, Callee->getName().data(), B);
    return B.CreateFPExt(V, Type::DoubleTy);
  }
};

//===----------------------------------------------------------------------===//
// Integer Optimizations
//===----------------------------------------------------------------------===//

//===---------------------------------------===//
// 'ffs*' Optimizations

struct VISIBILITY_HIDDEN FFSOpt : public LibCallOptimization {
  virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
    const FunctionType *FT = Callee->getFunctionType();
    // Just make sure this has 2 arguments of the same FP type, which match the
    // result type.
    if (FT->getNumParams() != 1 || FT->getReturnType() != Type::Int32Ty ||
        !isa<IntegerType>(FT->getParamType(0)))
      return 0;
    
    Value *Op = CI->getOperand(1);
    
    // Constant fold.
    if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
      if (CI->getValue() == 0)  // ffs(0) -> 0.
        return Context->getNullValue(CI->getType());
      return ConstantInt::get(Type::Int32Ty, // ffs(c) -> cttz(c)+1
                              CI->getValue().countTrailingZeros()+1);
    }
    
    // ffs(x) -> x != 0 ? (i32)llvm.cttz(x)+1 : 0
    const Type *ArgType = Op->getType();
    Value *F = Intrinsic::getDeclaration(Callee->getParent(),
                                         Intrinsic::cttz, &ArgType, 1);
    Value *V = B.CreateCall(F, Op, "cttz");
    V = B.CreateAdd(V, ConstantInt::get(V->getType(), 1), "tmp");
    V = B.CreateIntCast(V, Type::Int32Ty, false, "tmp");
    
    Value *Cond = B.CreateICmpNE(Op, Context->getNullValue(ArgType), "tmp");
    return B.CreateSelect(Cond, V, ConstantInt::get(Type::Int32Ty, 0));
  }
};

//===---------------------------------------===//
// 'isdigit' Optimizations

struct VISIBILITY_HIDDEN IsDigitOpt : public LibCallOptimization {
  virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
    const FunctionType *FT = Callee->getFunctionType();
    // We require integer(i32)
    if (FT->getNumParams() != 1 || !isa<IntegerType>(FT->getReturnType()) ||
        FT->getParamType(0) != Type::Int32Ty)
      return 0;
    
    // isdigit(c) -> (c-'0') <u 10
    Value *Op = CI->getOperand(1);
    Op = B.CreateSub(Op, ConstantInt::get(Type::Int32Ty, '0'), 
                     "isdigittmp");
    Op = B.CreateICmpULT(Op, ConstantInt::get(Type::Int32Ty, 10), 
    return B.CreateZExt(Op, CI->getType());
  }
};

//===---------------------------------------===//
// 'isascii' Optimizations

struct VISIBILITY_HIDDEN IsAsciiOpt : public LibCallOptimization {
  virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
    const FunctionType *FT = Callee->getFunctionType();
    // We require integer(i32)
    if (FT->getNumParams() != 1 || !isa<IntegerType>(FT->getReturnType()) ||
        FT->getParamType(0) != Type::Int32Ty)
      return 0;
    
    // isascii(c) -> c <u 128
    Value *Op = CI->getOperand(1);
    Op = B.CreateICmpULT(Op, ConstantInt::get(Type::Int32Ty, 128),
    return B.CreateZExt(Op, CI->getType());
  }
};
  
//===---------------------------------------===//
// 'abs', 'labs', 'llabs' Optimizations

struct VISIBILITY_HIDDEN AbsOpt : public LibCallOptimization {
  virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
    const FunctionType *FT = Callee->getFunctionType();
    // We require integer(integer) where the types agree.
    if (FT->getNumParams() != 1 || !isa<IntegerType>(FT->getReturnType()) ||
        FT->getParamType(0) != FT->getReturnType())
      return 0;
    
    // abs(x) -> x >s -1 ? x : -x
    Value *Op = CI->getOperand(1);
    Value *Pos = B.CreateICmpSGT(Op, 
                             Context->getAllOnesValue(Op->getType()),
                                 "ispos");
    Value *Neg = B.CreateNeg(Op, "neg");
    return B.CreateSelect(Pos, Op, Neg);
  }
};
  

//===---------------------------------------===//
// 'toascii' Optimizations

struct VISIBILITY_HIDDEN ToAsciiOpt : public LibCallOptimization {
  virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
    const FunctionType *FT = Callee->getFunctionType();
    // We require i32(i32)
    if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
        FT->getParamType(0) != Type::Int32Ty)
      return 0;
    
    // isascii(c) -> c & 0x7f
    return B.CreateAnd(CI->getOperand(1),
  }
};

//===----------------------------------------------------------------------===//
// Formatting and IO Optimizations
//===----------------------------------------------------------------------===//

//===---------------------------------------===//
// 'printf' Optimizations

struct VISIBILITY_HIDDEN PrintFOpt : public LibCallOptimization {
  virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
    // Require one fixed pointer argument and an integer/void result.
    const FunctionType *FT = Callee->getFunctionType();
    if (FT->getNumParams() < 1 || !isa<PointerType>(FT->getParamType(0)) ||
        !(isa<IntegerType>(FT->getReturnType()) ||
          FT->getReturnType() == Type::VoidTy))
      return 0;
    
    // Check for a fixed format string.
    std::string FormatStr;
    if (!GetConstantStringInfo(CI->getOperand(1), FormatStr))
      return 0;

    // Empty format string -> noop.
    if (FormatStr.empty())  // Tolerate printf's declared void.
      return CI->use_empty() ? (Value*)CI : 
    
    // printf("x") -> putchar('x'), even for '%'.
    if (FormatStr.size() == 1) {
      EmitPutChar(ConstantInt::get(Type::Int32Ty, FormatStr[0]), B);
      return CI->use_empty() ? (Value*)CI : 
    }
    
    // printf("foo\n") --> puts("foo")
    if (FormatStr[FormatStr.size()-1] == '\n' &&
        FormatStr.find('%') == std::string::npos) {  // no format characters.
      // Create a string literal with no \n on it.  We expect the constant merge
      // pass to be run after this pass, to merge duplicate strings.
      FormatStr.erase(FormatStr.end()-1);
      Constant *C = ConstantArray::get(FormatStr, true);
      C = new GlobalVariable(*Callee->getParent(), C->getType(), true,
                             GlobalVariable::InternalLinkage, C, "str");
      EmitPutS(C, B);
      return CI->use_empty() ? (Value*)CI : 
                    ConstantInt::get(CI->getType(), FormatStr.size()+1);
    }
    
    // Optimize specific format strings.
    // printf("%c", chr) --> putchar(*(i8*)dst)
    if (FormatStr == "%c" && CI->getNumOperands() > 2 &&
        isa<IntegerType>(CI->getOperand(2)->getType())) {
      EmitPutChar(CI->getOperand(2), B);
      return CI->use_empty() ? (Value*)CI : 
    }
    
    // printf("%s\n", str) --> puts(str)
    if (FormatStr == "%s\n" && CI->getNumOperands() > 2 &&
        isa<PointerType>(CI->getOperand(2)->getType()) &&
        CI->use_empty()) {
      EmitPutS(CI->getOperand(2), B);
      return CI;
    }
    return 0;
  }
};

//===---------------------------------------===//
// 'sprintf' Optimizations

struct VISIBILITY_HIDDEN SPrintFOpt : public LibCallOptimization {
  virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
    // Require two fixed pointer arguments and an integer result.
    const FunctionType *FT = Callee->getFunctionType();
    if (FT->getNumParams() != 2 || !isa<PointerType>(FT->getParamType(0)) ||
        !isa<PointerType>(FT->getParamType(1)) ||
        !isa<IntegerType>(FT->getReturnType()))
      return 0;

    // Check for a fixed format string.
    std::string FormatStr;
    if (!GetConstantStringInfo(CI->getOperand(2), FormatStr))
      return 0;
    
    // If we just have a format string (nothing else crazy) transform it.
    if (CI->getNumOperands() == 3) {
      // Make sure there's no % in the constant array.  We could try to handle
      // %% -> % in the future if we cared.
      for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
        if (FormatStr[i] == '%')
          return 0; // we found a format specifier, bail out.
      
      // sprintf(str, fmt) -> llvm.memcpy(str, fmt, strlen(fmt)+1, 1)
      EmitMemCpy(CI->getOperand(1), CI->getOperand(2), // Copy the nul byte.
          ConstantInt::get(TD->getIntPtrType(), FormatStr.size()+1),1,B);
      return ConstantInt::get(CI->getType(), FormatStr.size());
    }
    
    // The remaining optimizations require the format string to be "%s" or "%c"
    // and have an extra operand.
    if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->getNumOperands() <4)
      return 0;
    
    // Decode the second character of the format string.
    if (FormatStr[1] == 'c') {
Chris Lattner's avatar
Chris Lattner committed
      // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
      if (!isa<IntegerType>(CI->getOperand(3)->getType())) return 0;
      Value *V = B.CreateTrunc(CI->getOperand(3), Type::Int8Ty, "char");
Chris Lattner's avatar
Chris Lattner committed
      Value *Ptr = CastToCStr(CI->getOperand(1), B);
      B.CreateStore(V, Ptr);
      Ptr = B.CreateGEP(Ptr, ConstantInt::get(Type::Int32Ty, 1), "nul");
      B.CreateStore(Context->getNullValue(Type::Int8Ty), Ptr);
      return ConstantInt::get(CI->getType(), 1);
    }
    
    if (FormatStr[1] == 's') {
      // sprintf(dest, "%s", str) -> llvm.memcpy(dest, str, strlen(str)+1, 1)
      if (!isa<PointerType>(CI->getOperand(3)->getType())) return 0;

      Value *Len = EmitStrLen(CI->getOperand(3), B);
      Value *IncLen = B.CreateAdd(Len,
                                  "leninc");
      EmitMemCpy(CI->getOperand(1), CI->getOperand(3), IncLen, 1, B);
      
      // The sprintf result is the unincremented number of bytes in the string.
      return B.CreateIntCast(Len, CI->getType(), false);
    }
    return 0;
  }
};

//===---------------------------------------===//
// 'fwrite' Optimizations

struct VISIBILITY_HIDDEN FWriteOpt : public LibCallOptimization {
  virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
    // Require a pointer, an integer, an integer, a pointer, returning integer.
    const FunctionType *FT = Callee->getFunctionType();
    if (FT->getNumParams() != 4 || !isa<PointerType>(FT->getParamType(0)) ||
        !isa<IntegerType>(FT->getParamType(1)) ||
        !isa<IntegerType>(FT->getParamType(2)) ||
        !isa<PointerType>(FT->getParamType(3)) ||
        !isa<IntegerType>(FT->getReturnType()))
      return 0;
    
    // Get the element size and count.
    ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getOperand(2));
    ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getOperand(3));
    if (!SizeC || !CountC) return 0;
    uint64_t Bytes = SizeC->getZExtValue()*CountC->getZExtValue();
    
    // If this is writing zero records, remove the call (it's a noop).
    if (Bytes == 0)
      return ConstantInt::get(CI->getType(), 0);
    
    // If this is writing one byte, turn it into fputc.
    if (Bytes == 1) {  // fwrite(S,1,1,F) -> fputc(S[0],F)
      Value *Char = B.CreateLoad(CastToCStr(CI->getOperand(1), B), "char");
      EmitFPutC(Char, CI->getOperand(4), B);
      return ConstantInt::get(CI->getType(), 1);
    }

    return 0;
  }
};

//===---------------------------------------===//
// 'fputs' Optimizations

struct VISIBILITY_HIDDEN FPutsOpt : public LibCallOptimization {
  virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
    // Require two pointers.  Also, we can't optimize if return value is used.
    const FunctionType *FT = Callee->getFunctionType();
    if (FT->getNumParams() != 2 || !isa<PointerType>(FT->getParamType(0)) ||
        !isa<PointerType>(FT->getParamType(1)) ||
        !CI->use_empty())
      return 0;
    
    // fputs(s,F) --> fwrite(s,1,strlen(s),F)
    uint64_t Len = GetStringLength(CI->getOperand(1));
Chris Lattner's avatar
Chris Lattner committed
    if (!Len) return 0;
    EmitFWrite(CI->getOperand(1),
               ConstantInt::get(TD->getIntPtrType(), Len-1),
               CI->getOperand(2), B);
    return CI;  // Known to have no uses (see above).
  }
};

//===---------------------------------------===//
// 'fprintf' Optimizations

struct VISIBILITY_HIDDEN FPrintFOpt : public LibCallOptimization {
  virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
    // Require two fixed paramters as pointers and integer result.
    const FunctionType *FT = Callee->getFunctionType();
    if (FT->getNumParams() != 2 || !isa<PointerType>(FT->getParamType(0)) ||
        !isa<PointerType>(FT->getParamType(1)) ||
        !isa<IntegerType>(FT->getReturnType()))
      return 0;
    
    // All the optimizations depend on the format string.
    std::string FormatStr;
    if (!GetConstantStringInfo(CI->getOperand(2), FormatStr))
      return 0;

    // fprintf(F, "foo") --> fwrite("foo", 3, 1, F)
    if (CI->getNumOperands() == 3) {
      for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
        if (FormatStr[i] == '%')  // Could handle %% -> % if we cared.
Chris Lattner's avatar
Chris Lattner committed
          return 0; // We found a format specifier.
      EmitFWrite(CI->getOperand(2), ConstantInt::get(TD->getIntPtrType(),
                                                     FormatStr.size()),
                 CI->getOperand(1), B);
      return ConstantInt::get(CI->getType(), FormatStr.size());
    }
    
    // The remaining optimizations require the format string to be "%s" or "%c"
    // and have an extra operand.
    if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->getNumOperands() <4)
      return 0;
    
    // Decode the second character of the format string.
    if (FormatStr[1] == 'c') {
      // fprintf(F, "%c", chr) --> *(i8*)dst = chr
      if (!isa<IntegerType>(CI->getOperand(3)->getType())) return 0;
      EmitFPutC(CI->getOperand(3), CI->getOperand(1), B);
      return ConstantInt::get(CI->getType(), 1);
    }
    
    if (FormatStr[1] == 's') {
      // fprintf(F, "%s", str) -> fputs(str, F)
      if (!isa<PointerType>(CI->getOperand(3)->getType()) || !CI->use_empty())
        return 0;
      EmitFPutS(CI->getOperand(3), CI->getOperand(1), B);
      return CI;
    }
    return 0;
  }
};


//===----------------------------------------------------------------------===//
// SimplifyLibCalls Pass Implementation
//===----------------------------------------------------------------------===//

namespace {
  /// This pass optimizes well known library functions from libc and libm.
  ///
  class VISIBILITY_HIDDEN SimplifyLibCalls : public FunctionPass {
    StringMap<LibCallOptimization*> Optimizations;
    // Miscellaneous LibCall Optimizations
    ExitOpt Exit; 
    // String and Memory LibCall Optimizations
    StrCatOpt StrCat; StrNCatOpt StrNCat; StrChrOpt StrChr; StrCmpOpt StrCmp;
    StrNCmpOpt StrNCmp; StrCpyOpt StrCpy; StrNCpyOpt StrNCpy; StrLenOpt StrLen;
    StrToOpt StrTo; MemCmpOpt MemCmp; MemCpyOpt MemCpy; MemMoveOpt MemMove;
    MemSetOpt MemSet;
    PowOpt Pow; Exp2Opt Exp2; UnaryDoubleFPOpt UnaryDoubleFP;
    FFSOpt FFS; AbsOpt Abs; IsDigitOpt IsDigit; IsAsciiOpt IsAscii;
    ToAsciiOpt ToAscii;
    // Formatting and IO Optimizations
    SPrintFOpt SPrintF; PrintFOpt PrintF;
    FWriteOpt FWrite; FPutsOpt FPuts; FPrintFOpt FPrintF;
    bool Modified;  // This is only used by doInitialization.
  public:
    static char ID; // Pass identification
    SimplifyLibCalls() : FunctionPass(&ID) {}

    void InitOptimizations();
    bool runOnFunction(Function &F);

    void setDoesNotAccessMemory(Function &F);
    void setOnlyReadsMemory(Function &F);
    void setDoesNotThrow(Function &F);
    void setDoesNotCapture(Function &F, unsigned n);
    void setDoesNotAlias(Function &F, unsigned n);
    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
      AU.addRequired<TargetData>();
    }
  };
  char SimplifyLibCalls::ID = 0;
} // end anonymous namespace.

static RegisterPass<SimplifyLibCalls>
X("simplify-libcalls", "Simplify well-known library calls");

// Public interface to the Simplify LibCalls pass.
FunctionPass *llvm::createSimplifyLibCallsPass() {
  return new SimplifyLibCalls(); 
}

/// Optimizations - Populate the Optimizations map with all the optimizations
/// we know.
void SimplifyLibCalls::InitOptimizations() {
  // Miscellaneous LibCall Optimizations
  Optimizations["exit"] = &Exit;
  
  // String and Memory LibCall Optimizations
  Optimizations["strcat"] = &StrCat;
  Optimizations["strncat"] = &StrNCat;
  Optimizations["strchr"] = &StrChr;
  Optimizations["strcmp"] = &StrCmp;
  Optimizations["strncmp"] = &StrNCmp;
  Optimizations["strcpy"] = &StrCpy;
  Optimizations["strncpy"] = &StrNCpy;
  Optimizations["strlen"] = &StrLen;
  Optimizations["strtol"] = &StrTo;
  Optimizations["strtod"] = &StrTo;
  Optimizations["strtof"] = &StrTo;
  Optimizations["strtoul"] = &StrTo;
  Optimizations["strtoll"] = &StrTo;
  Optimizations["strtold"] = &StrTo;
  Optimizations["strtoull"] = &StrTo;
  Optimizations["memcmp"] = &MemCmp;
  Optimizations["memcpy"] = &MemCpy;
  Optimizations["memmove"] = &MemMove;
  Optimizations["memset"] = &MemSet;
  
  // Math Library Optimizations
  Optimizations["powf"] = &Pow;
  Optimizations["pow"] = &Pow;
  Optimizations["powl"] = &Pow;
  Optimizations["llvm.pow.f32"] = &Pow;
  Optimizations["llvm.pow.f64"] = &Pow;
  Optimizations["llvm.pow.f80"] = &Pow;
  Optimizations["llvm.pow.f128"] = &Pow;
  Optimizations["llvm.pow.ppcf128"] = &Pow;
  Optimizations["exp2l"] = &Exp2;
  Optimizations["exp2"] = &Exp2;
  Optimizations["exp2f"] = &Exp2;
  Optimizations["llvm.exp2.ppcf128"] = &Exp2;
  Optimizations["llvm.exp2.f128"] = &Exp2;
  Optimizations["llvm.exp2.f80"] = &Exp2;
  Optimizations["llvm.exp2.f64"] = &Exp2;
  Optimizations["llvm.exp2.f32"] = &Exp2;
#ifdef HAVE_FLOORF
  Optimizations["floor"] = &UnaryDoubleFP;
#endif
#ifdef HAVE_CEILF
  Optimizations["ceil"] = &UnaryDoubleFP;
#endif
#ifdef HAVE_ROUNDF
  Optimizations["round"] = &UnaryDoubleFP;
#endif
#ifdef HAVE_RINTF
  Optimizations["rint"] = &UnaryDoubleFP;
#endif
#ifdef HAVE_NEARBYINTF
  Optimizations["nearbyint"] = &UnaryDoubleFP;
#endif
  
  // Integer Optimizations
  Optimizations["ffs"] = &FFS;
  Optimizations["ffsl"] = &FFS;
  Optimizations["ffsll"] = &FFS;
  Optimizations["abs"] = &Abs;
  Optimizations["labs"] = &Abs;
  Optimizations["llabs"] = &Abs;
  Optimizations["isdigit"] = &IsDigit;
  Optimizations["isascii"] = &IsAscii;
  Optimizations["toascii"] = &ToAscii;
  
  // Formatting and IO Optimizations
  Optimizations["sprintf"] = &SPrintF;
  Optimizations["printf"] = &PrintF;
  Optimizations["fwrite"] = &FWrite;
  Optimizations["fputs"] = &FPuts;
  Optimizations["fprintf"] = &FPrintF;
}


/// runOnFunction - Top level algorithm.
///
bool SimplifyLibCalls::runOnFunction(Function &F) {
  if (Optimizations.empty())
    InitOptimizations();
  
  const TargetData &TD = getAnalysis<TargetData>();
  
  IRBuilder<> Builder(F.getContext());

  bool Changed = false;
  for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
    for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
      // Ignore non-calls.
      CallInst *CI = dyn_cast<CallInst>(I++);
      if (!CI) continue;
      
      // Ignore indirect calls and calls to non-external functions.
      Function *Callee = CI->getCalledFunction();
      if (Callee == 0 || !Callee->isDeclaration() ||
          !(Callee->hasExternalLinkage() || Callee->hasDLLImportLinkage()))
        continue;
      
      // Ignore unknown calls.
Daniel Dunbar's avatar
Daniel Dunbar committed
      LibCallOptimization *LCO = Optimizations.lookup(Callee->getName());
      if (!LCO) continue;
      
      // Set the builder to the instruction after the call.
      Builder.SetInsertPoint(BB, I);
      
      // Try to optimize this call.
Daniel Dunbar's avatar
Daniel Dunbar committed
      Value *Result = LCO->OptimizeCall(CI, TD, Builder);
Daniel Dunbar's avatar
Daniel Dunbar committed
      DEBUG(errs() << "SimplifyLibCalls simplified: " << *CI;
            errs() << "  into: " << *Result << "\n");
      // Something changed!
      Changed = true;
      ++NumSimplified;
      
      // Inspect the instruction after the call (which was potentially just
      // added) next.
      I = CI; ++I;
      
      if (CI != Result && !CI->use_empty()) {
        CI->replaceAllUsesWith(Result);
        if (!Result->hasName())
          Result->takeName(CI);
      }
      CI->eraseFromParent();
    }
  }
  return Changed;
}

// Utility methods for doInitialization.

void SimplifyLibCalls::setDoesNotAccessMemory(Function &F) {
  if (!F.doesNotAccessMemory()) {
    F.setDoesNotAccessMemory();
    ++NumAnnotated;
    Modified = true;
  }
}
void SimplifyLibCalls::setOnlyReadsMemory(Function &F) {
  if (!F.onlyReadsMemory()) {
    F.setOnlyReadsMemory();
    ++NumAnnotated;
    Modified = true;
  }
}
void SimplifyLibCalls::setDoesNotThrow(Function &F) {
  if (!F.doesNotThrow()) {
    F.setDoesNotThrow();
    ++NumAnnotated;
    Modified = true;
  }
}
void SimplifyLibCalls::setDoesNotCapture(Function &F, unsigned n) {
  if (!F.doesNotCapture(n)) {
    F.setDoesNotCapture(n);
    ++NumAnnotated;
    Modified = true;
  }
}
void SimplifyLibCalls::setDoesNotAlias(Function &F, unsigned n) {
  if (!F.doesNotAlias(n)) {
    F.setDoesNotAlias(n);
    ++NumAnnotated;
    Modified = true;
  }
}

/// doInitialization - Add attributes to well-known functions.
bool SimplifyLibCalls::doInitialization(Module &M) {
  Modified = false;
  for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
    Function &F = *I;
    if (!F.isDeclaration())
      continue;

      continue;

    const FunctionType *FTy = F.getFunctionType();

    StringRef Name = F.getName();
    switch (Name[0]) {
        if (Name == "strlen") {
          if (FTy->getNumParams() != 1 ||
              !isa<PointerType>(FTy->getParamType(0)))
            continue;
          setOnlyReadsMemory(F);
          setDoesNotThrow(F);
          setDoesNotCapture(F, 1);
        } else if (Name == "strcpy" ||
                   Name == "stpcpy" ||
                   Name == "strcat" ||
                   Name == "strtol" ||
                   Name == "strtod" ||
                   Name == "strtof" ||
                   Name == "strtoul" ||
                   Name == "strtoll" ||
                   Name == "strtold" ||
                   Name == "strncat" ||
                   Name == "strncpy" ||
                   Name == "strtoull") {
          if (FTy->getNumParams() < 2 ||
              !isa<PointerType>(FTy->getParamType(1)))
            continue;
          setDoesNotThrow(F);
          setDoesNotCapture(F, 2);
        } else if (Name == "strxfrm") {
          if (FTy->getNumParams() != 3 ||
              !isa<PointerType>(FTy->getParamType(0)) ||
              !isa<PointerType>(FTy->getParamType(1)))
            continue;
          setDoesNotThrow(F);
          setDoesNotCapture(F, 1);
          setDoesNotCapture(F, 2);
        } else if (Name == "strcmp" ||
                   Name == "strspn" ||
                   Name == "strncmp" ||
                   Name ==" strcspn" ||
                   Name == "strcoll" ||
                   Name == "strcasecmp" ||
                   Name == "strncasecmp") {
          if (FTy->getNumParams() < 2 ||
              !isa<PointerType>(FTy->getParamType(0)) ||
              !isa<PointerType>(FTy->getParamType(1)))
            continue;
          setOnlyReadsMemory(F);
          setDoesNotThrow(F);
          setDoesNotCapture(F, 1);
          setDoesNotCapture(F, 2);
        } else if (Name == "strstr" ||
                   Name == "strpbrk") {
          if (FTy->getNumParams() != 2 ||
              !isa<PointerType>(FTy->getParamType(1)))
            continue;
          setOnlyReadsMemory(F);
          setDoesNotThrow(F);
          setDoesNotCapture(F, 2);
        } else if (Name == "strtok" ||
                   Name == "strtok_r") {
          if (FTy->getNumParams() < 2 ||
              !isa<PointerType>(FTy->getParamType(1)))
            continue;
          setDoesNotThrow(F);
          setDoesNotCapture(F, 2);
        } else if (Name == "scanf" ||
                   Name == "setbuf" ||
                   Name == "setvbuf") {
          if (FTy->getNumParams() < 1 ||
              !isa<PointerType>(FTy->getParamType(0)))
            continue;
          setDoesNotThrow(F);
          setDoesNotCapture(F, 1);
        } else if (Name == "strdup" ||
                   Name == "strndup") {
          if (FTy->getNumParams() < 1 ||
              !isa<PointerType>(FTy->getReturnType()) ||
              !isa<PointerType>(FTy->getParamType(0)))
            continue;
          setDoesNotThrow(F);
          setDoesNotAlias(F, 0);
          setDoesNotCapture(F, 1);
        } else if (Name == "stat" ||
                   Name == "sscanf" ||
                   Name == "sprintf" ||
                   Name == "statvfs") {
              !isa<PointerType>(FTy->getParamType(0)) ||
              !isa<PointerType>(FTy->getParamType(1)))
            continue;
          setDoesNotThrow(F);
          setDoesNotCapture(F, 1);
          setDoesNotCapture(F, 2);
        } else if (Name == "snprintf") {
          if (FTy->getNumParams() != 3 ||
              !isa<PointerType>(FTy->getParamType(0)) ||
              !isa<PointerType>(FTy->getParamType(2)))
            continue;
          setDoesNotThrow(F);
          setDoesNotCapture(F, 1);
          setDoesNotCapture(F, 3);
        } else if (Name == "setitimer") {
          if (FTy->getNumParams() != 3 ||
              !isa<PointerType>(FTy->getParamType(1)) ||
              !isa<PointerType>(FTy->getParamType(2)))
            continue;
          setDoesNotThrow(F);
          setDoesNotCapture(F, 2);
          setDoesNotCapture(F, 3);
        } else if (Name == "system") {
          if (FTy->getNumParams() != 1 ||
              !isa<PointerType>(FTy->getParamType(0)))
            continue;
          // May throw; "system" is a valid pthread cancellation point.
          setDoesNotCapture(F, 1);
        if (Name == "memcmp") {
          if (FTy->getNumParams() != 3 ||
              !isa<PointerType>(FTy->getParamType(0)) ||
              !isa<PointerType>(FTy->getParamType(1)))
            continue;
          setOnlyReadsMemory(F);
          setDoesNotThrow(F);
          setDoesNotCapture(F, 1);
          setDoesNotCapture(F, 2);
        } else if (Name == "memchr" ||
                   Name == "memrchr") {
          if (FTy->getNumParams() != 3)
            continue;
          setOnlyReadsMemory(F);
          setDoesNotThrow(F);
        } else if (Name == "modf" ||
                   Name == "modff" ||
                   Name == "modfl" ||
                   Name == "memcpy" ||
                   Name == "memccpy" ||
                   Name == "memmove") {
              !isa<PointerType>(FTy->getParamType(1)))
            continue;
          setDoesNotThrow(F);
          setDoesNotCapture(F, 2);
        } else if (Name == "memalign") {
          if (!isa<PointerType>(FTy->getReturnType()))
            continue;
          setDoesNotAlias(F, 0);
        } else if (Name == "mkdir" ||
                   Name == "mktime") {
          if (FTy->getNumParams() == 0 ||
              !isa<PointerType>(FTy->getParamType(0)))
            continue;
          setDoesNotThrow(F);
          setDoesNotCapture(F, 1);
        if (Name == "realloc") {
              !isa<PointerType>(FTy->getParamType(0)) ||
              !isa<PointerType>(FTy->getReturnType()))
            continue;
          setDoesNotThrow(F);
          setDoesNotAlias(F, 0);
          setDoesNotCapture(F, 1);
        } else if (Name == "read") {
          if (FTy->getNumParams() != 3 ||
              !isa<PointerType>(FTy->getParamType(1)))
            continue;
          // May throw; "read" is a valid pthread cancellation point.
        } else if (Name == "rmdir" ||
                   Name == "rewind" ||
                   Name == "remove" ||
                   Name == "realpath") {
              !isa<PointerType>(FTy->getParamType(0)))
            continue;
          setDoesNotThrow(F);
          setDoesNotCapture(F, 1);
        } else if (Name == "rename" ||
                   Name == "readlink") {
              !isa<PointerType>(FTy->getParamType(0)) ||
              !isa<PointerType>(FTy->getParamType(1)))
            continue;
          setDoesNotThrow(F);
          setDoesNotCapture(F, 1);
          setDoesNotCapture(F, 2);
        }
        break;
      case 'w':
          if (FTy->getNumParams() != 3 ||
              !isa<PointerType>(FTy->getParamType(1)))
            continue;
          // May throw; "write" is a valid pthread cancellation point.
          if (FTy->getNumParams() != 3 ||
              !isa<PointerType>(FTy->getParamType(0)) ||
              !isa<PointerType>(FTy->getParamType(1)))
            continue;
          setDoesNotThrow(F);
          setDoesNotCapture(F, 1);
          setDoesNotCapture(F, 2);
        } else if (Name == "bcmp") {
          if (FTy->getNumParams() != 3 ||
              !isa<PointerType>(FTy->getParamType(0)) ||
              !isa<PointerType>(FTy->getParamType(1)))
            continue;
          setDoesNotThrow(F);
          setOnlyReadsMemory(F);
          setDoesNotCapture(F, 1);
          setDoesNotCapture(F, 2);
        } else if (Name == "bzero") {
          if (FTy->getNumParams() != 2 ||
              !isa<PointerType>(FTy->getParamType(0)))
            continue;
          setDoesNotThrow(F);
          setDoesNotCapture(F, 1);
        }
        break;
      case 'c':
        if (Name == "calloc") {
          if (FTy->getNumParams() != 2 ||
              !isa<PointerType>(FTy->getReturnType()))
            continue;
          setDoesNotThrow(F);
          setDoesNotAlias(F, 0);
        } else if (Name == "chmod" ||
                   Name == "chown" ||
                   Name == "ctermid" ||
                   Name == "clearerr" ||
                   Name == "closedir") {
          if (FTy->getNumParams() == 0 ||
              !isa<PointerType>(FTy->getParamType(0)))
            continue;
          setDoesNotThrow(F);
          setDoesNotCapture(F, 1);
        }
        break;
      case 'a':
        if (Name == "atoi" ||
            Name == "atol" ||