Newer
Older
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);
return CI->getOperand(1);
}
};
//===----------------------------------------------------------------------===//
// 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),
return 0;
}
};
//===---------------------------------------===//
// '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;
}
return 0;
}
};
//===---------------------------------------===//
// 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());
Owen Anderson
committed
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");
Owen Anderson
committed
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");
Owen Anderson
committed
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);
Owen Anderson
committed
Op = B.CreateSub(Op, ConstantInt::get(Type::Int32Ty, '0'),
Owen Anderson
committed
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);
Owen Anderson
committed
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);
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),
Owen Anderson
committed
ConstantInt::get(CI->getType(),0x7F));
}
};
//===----------------------------------------------------------------------===//
// 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 :
Owen Anderson
committed
ConstantInt::get(CI->getType(), 0);
// printf("x") -> putchar('x'), even for '%'.
if (FormatStr.size() == 1) {
Owen Anderson
committed
EmitPutChar(ConstantInt::get(Type::Int32Ty, FormatStr[0]), B);
return CI->use_empty() ? (Value*)CI :
Owen Anderson
committed
ConstantInt::get(CI->getType(), 1);
}
// 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);
Owen Anderson
committed
C = new GlobalVariable(*Callee->getParent(), C->getType(), true,
GlobalVariable::InternalLinkage, C, "str");
EmitPutS(C, B);
return CI->use_empty() ? (Value*)CI :
Owen Anderson
committed
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 :
Owen Anderson
committed
ConstantInt::get(CI->getType(), 1);
}
// 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.
Owen Anderson
committed
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') {
// 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");
Value *Ptr = CastToCStr(CI->getOperand(1), B);
B.CreateStore(V, Ptr);
Owen Anderson
committed
Ptr = B.CreateGEP(Ptr, ConstantInt::get(Type::Int32Ty, 1), "nul");
B.CreateStore(Context->getNullValue(Type::Int8Ty), Ptr);
Owen Anderson
committed
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);
Owen Anderson
committed
ConstantInt::get(Len->getType(), 1),
"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)
Owen Anderson
committed
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);
Owen Anderson
committed
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));
Owen Anderson
committed
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.
Owen Anderson
committed
EmitFWrite(CI->getOperand(2), ConstantInt::get(TD->getIntPtrType(),
FormatStr.size()),
CI->getOperand(1), B);
Owen Anderson
committed
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);
Owen Anderson
committed
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;
}
};
Bill Wendling
committed
} // end anonymous namespace.
//===----------------------------------------------------------------------===//
// 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;
// Math Library Optimizations
PowOpt Pow; Exp2Opt Exp2; UnaryDoubleFPOpt UnaryDoubleFP;
// Integer Optimizations
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);
bool doInitialization(Module &M);
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
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;
Nick Lewycky
committed
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;
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
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.
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.
Value *Result = LCO->OptimizeCall(CI, TD, Builder);
if (Result == 0) continue;
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.
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
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;
if (!F.hasName())
continue;
const FunctionType *FTy = F.getFunctionType();
StringRef Name = F.getName();
switch (Name[0]) {
case 's':
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") {
if (FTy->getNumParams() < 2 ||
!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);
}
break;
case 'm':
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") {
if (FTy->getNumParams() < 2 ||
!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);
}
break;
case 'r':
if (Name == "realloc") {
if (FTy->getNumParams() != 2 ||
!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.
setDoesNotCapture(F, 2);
} else if (Name == "rmdir" ||
Name == "rewind" ||
Name == "remove" ||
Name == "realpath") {
if (FTy->getNumParams() < 1 ||
!isa<PointerType>(FTy->getParamType(0)))
continue;
setDoesNotThrow(F);
setDoesNotCapture(F, 1);
} else if (Name == "rename" ||
Name == "readlink") {
if (FTy->getNumParams() < 2 ||
!isa<PointerType>(FTy->getParamType(0)) ||
!isa<PointerType>(FTy->getParamType(1)))
continue;
setDoesNotThrow(F);
setDoesNotCapture(F, 1);
setDoesNotCapture(F, 2);
}
break;
case 'w':
if (Name == "write") {
if (FTy->getNumParams() != 3 ||
!isa<PointerType>(FTy->getParamType(1)))
continue;
// May throw; "write" is a valid pthread cancellation point.
setDoesNotCapture(F, 2);
}
break;
case 'b':
if (Name == "bcopy") {
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" ||
Name == "atof" ||
Name == "atoll") {
if (FTy->getNumParams() != 1 ||
!isa<PointerType>(FTy->getParamType(0)))
continue;
setDoesNotThrow(F);
setOnlyReadsMemory(F);