"llvm/lib/git@repo.hca.bsc.es:rferrer/llvm-epi-0.8.git" did not exist on "0bddac16a8c5798df1528ccf43da92a392a488be"
Newer
Older
// memset(p, v, n) -> llvm.memset(p, v, n, 1)
Value *Val = B.CreateTrunc(CI->getOperand(2), Type::Int8Ty);
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 Context->getConstantFP(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(Context->getConstantFP(CI->getType(), 1.0),
Op1, "powrecip");
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 = Context->getConstantFP(APFloat(1.0f));
if (Op->getType() != Type::FloatTy)
One = Context->getConstantExprFPExtend(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->getNameStart(), 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 Context->getConstantInt(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, Context->getConstantInt(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, Context->getConstantInt(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, Context->getConstantInt(Type::Int32Ty, '0'),
"isdigittmp");
Op = B.CreateICmpULT(Op, Context->getConstantInt(Type::Int32Ty, 10),
"isdigit");
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, Context->getConstantInt(Type::Int32Ty, 128),
"isascii");
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),
Context->getConstantInt(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 :
Context->getConstantInt(CI->getType(), 0);
// printf("x") -> putchar('x'), even for '%'.
if (FormatStr.size() == 1) {
EmitPutChar(Context->getConstantInt(Type::Int32Ty, FormatStr[0]), B);
return CI->use_empty() ? (Value*)CI :
Context->getConstantInt(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 = Context->getConstantArray(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 :
Context->getConstantInt(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 :
Context->getConstantInt(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.
Context->getConstantInt(TD->getIntPtrType(), FormatStr.size()+1),1,B);
return Context->getConstantInt(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);
Ptr = B.CreateGEP(Ptr, Context->getConstantInt(Type::Int32Ty, 1), "nul");
B.CreateStore(Context->getNullValue(Type::Int8Ty), Ptr);
return Context->getConstantInt(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,
Context->getConstantInt(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)
return Context->getConstantInt(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 Context->getConstantInt(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));
EmitFWrite(CI->getOperand(1),
Context->getConstantInt(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.
EmitFWrite(CI->getOperand(2), Context->getConstantInt(TD->getIntPtrType(),
FormatStr.size()),
CI->getOperand(1), B);
return Context->getConstantInt(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 Context->getConstantInt(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);
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
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;
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
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(*Context);
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
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.
const char *CalleeName = Callee->getNameStart();
StringMap<LibCallOptimization*>::iterator OMI =
Optimizations.find(CalleeName, CalleeName+Callee->getNameLen());
if (OMI == Optimizations.end()) continue;
// Set the builder to the instruction after the call.
Builder.SetInsertPoint(BB, I);
// Try to optimize this call.
Value *Result = OMI->second->OptimizeCall(CI, TD, Builder);
if (Result == 0) continue;
DEBUG(DOUT << "SimplifyLibCalls simplified: " << *CI;
DOUT << " 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.
1693
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
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) {
Owen Anderson
committed
Context = &M.getContext();
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
Modified = false;
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
Function &F = *I;
if (!F.isDeclaration())
continue;
unsigned NameLen = F.getNameLen();
if (!NameLen)
continue;
const FunctionType *FTy = F.getFunctionType();
const char *NameStr = F.getNameStart();
switch (NameStr[0]) {
case 's':
if (NameLen == 6 && !strcmp(NameStr, "strlen")) {
if (FTy->getNumParams() != 1 ||
!isa<PointerType>(FTy->getParamType(0)))
continue;
setOnlyReadsMemory(F);
setDoesNotThrow(F);
setDoesNotCapture(F, 1);
} else if ((NameLen == 6 && !strcmp(NameStr, "strcpy")) ||
(NameLen == 6 && !strcmp(NameStr, "stpcpy")) ||
(NameLen == 6 && !strcmp(NameStr, "strcat")) ||
Nick Lewycky
committed
(NameLen == 6 && !strcmp(NameStr, "strtol")) ||
(NameLen == 6 && !strcmp(NameStr, "strtod")) ||
(NameLen == 6 && !strcmp(NameStr, "strtof")) ||
(NameLen == 7 && !strcmp(NameStr, "strtoul")) ||
(NameLen == 7 && !strcmp(NameStr, "strtoll")) ||
(NameLen == 7 && !strcmp(NameStr, "strtold")) ||
(NameLen == 7 && !strcmp(NameStr, "strncat")) ||
Nick Lewycky
committed
(NameLen == 7 && !strcmp(NameStr, "strncpy")) ||
(NameLen == 8 && !strcmp(NameStr, "strtoull"))) {
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
if (FTy->getNumParams() < 2 ||
!isa<PointerType>(FTy->getParamType(1)))
continue;
setDoesNotThrow(F);
setDoesNotCapture(F, 2);
} else if (NameLen == 7 && !strcmp(NameStr, "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 ((NameLen == 6 && !strcmp(NameStr, "strcmp")) ||
(NameLen == 6 && !strcmp(NameStr, "strspn")) ||
(NameLen == 7 && !strcmp(NameStr, "strncmp")) ||
(NameLen == 7 && !strcmp(NameStr, "strcspn")) ||
(NameLen == 7 && !strcmp(NameStr, "strcoll")) ||
(NameLen == 10 && !strcmp(NameStr, "strcasecmp")) ||
(NameLen == 11 && !strcmp(NameStr, "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 ((NameLen == 6 && !strcmp(NameStr, "strstr")) ||
(NameLen == 7 && !strcmp(NameStr, "strpbrk"))) {
if (FTy->getNumParams() != 2 ||
!isa<PointerType>(FTy->getParamType(1)))
continue;
setOnlyReadsMemory(F);
setDoesNotThrow(F);
setDoesNotCapture(F, 2);
} else if ((NameLen == 6 && !strcmp(NameStr, "strtok")) ||
(NameLen == 8 && !strcmp(NameStr, "strtok_r"))) {
if (FTy->getNumParams() < 2 ||
!isa<PointerType>(FTy->getParamType(1)))
continue;
setDoesNotThrow(F);
setDoesNotCapture(F, 2);
} else if ((NameLen == 5 && !strcmp(NameStr, "scanf")) ||
(NameLen == 6 && !strcmp(NameStr, "setbuf")) ||
(NameLen == 7 && !strcmp(NameStr, "setvbuf"))) {
if (FTy->getNumParams() < 1 ||
!isa<PointerType>(FTy->getParamType(0)))
continue;
setDoesNotThrow(F);
setDoesNotCapture(F, 1);
} else if ((NameLen == 6 && !strcmp(NameStr, "strdup")) ||
(NameLen == 7 && !strcmp(NameStr, "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 ((NameLen == 4 && !strcmp(NameStr, "stat")) ||
(NameLen == 6 && !strcmp(NameStr, "sscanf")) ||
(NameLen == 7 && !strcmp(NameStr, "sprintf")) ||
(NameLen == 7 && !strcmp(NameStr, "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 (NameLen == 8 && !strcmp(NameStr, "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 (NameLen == 9 && !strcmp(NameStr, "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 (NameLen == 6 && !strcmp(NameStr, "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 (NameLen == 6 && !strcmp(NameStr, "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 ((NameLen == 6 && !strcmp(NameStr, "memchr")) ||
(NameLen == 7 && !strcmp(NameStr, "memrchr"))) {
if (FTy->getNumParams() != 3)
continue;
setOnlyReadsMemory(F);
setDoesNotThrow(F);
} else if ((NameLen == 4 && !strcmp(NameStr, "modf")) ||
(NameLen == 5 && !strcmp(NameStr, "modff")) ||
(NameLen == 5 && !strcmp(NameStr, "modfl")) ||
(NameLen == 6 && !strcmp(NameStr, "memcpy")) ||
(NameLen == 7 && !strcmp(NameStr, "memccpy")) ||
(NameLen == 7 && !strcmp(NameStr, "memmove"))) {
if (FTy->getNumParams() < 2 ||
!isa<PointerType>(FTy->getParamType(1)))
continue;
setDoesNotThrow(F);
setDoesNotCapture(F, 2);
} else if (NameLen == 8 && !strcmp(NameStr, "memalign")) {
if (!isa<PointerType>(FTy->getReturnType()))
continue;
setDoesNotAlias(F, 0);
} else if ((NameLen == 5 && !strcmp(NameStr, "mkdir")) ||
(NameLen == 6 && !strcmp(NameStr, "mktime"))) {
if (FTy->getNumParams() == 0 ||
!isa<PointerType>(FTy->getParamType(0)))
continue;
setDoesNotThrow(F);
setDoesNotCapture(F, 1);
}
break;
case 'r':
if (NameLen == 7 && !strcmp(NameStr, "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 (NameLen == 4 && !strcmp(NameStr, "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 ((NameLen == 5 && !strcmp(NameStr, "rmdir")) ||
(NameLen == 6 && !strcmp(NameStr, "rewind")) ||
(NameLen == 6 && !strcmp(NameStr, "remove")) ||
(NameLen == 8 && !strcmp(NameStr, "realpath"))) {
if (FTy->getNumParams() < 1 ||
!isa<PointerType>(FTy->getParamType(0)))
continue;
setDoesNotThrow(F);
setDoesNotCapture(F, 1);
} else if ((NameLen == 6 && !strcmp(NameStr, "rename")) ||
(NameLen == 8 && !strcmp(NameStr, "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 (NameLen == 5 && !strcmp(NameStr, "write")) {
if (FTy->getNumParams() != 3 ||
!isa<PointerType>(FTy->getParamType(1)))
continue;
// May throw; "write" is a valid pthread cancellation point.
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
setDoesNotCapture(F, 2);
}
break;
case 'b':
if (NameLen == 5 && !strcmp(NameStr, "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 (NameLen == 4 && !strcmp(NameStr, "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 (NameLen == 5 && !strcmp(NameStr, "bzero")) {
if (FTy->getNumParams() != 2 ||
!isa<PointerType>(FTy->getParamType(0)))
continue;
setDoesNotThrow(F);
setDoesNotCapture(F, 1);
}
break;
case 'c':
if (NameLen == 6 && !strcmp(NameStr, "calloc")) {
if (FTy->getNumParams() != 2 ||
!isa<PointerType>(FTy->getReturnType()))
continue;
setDoesNotThrow(F);
setDoesNotAlias(F, 0);
} else if ((NameLen == 5 && !strcmp(NameStr, "chmod")) ||
(NameLen == 5 && !strcmp(NameStr, "chown")) ||
(NameLen == 7 && !strcmp(NameStr, "ctermid")) ||
(NameLen == 8 && !strcmp(NameStr, "clearerr")) ||
(NameLen == 8 && !strcmp(NameStr, "closedir"))) {
if (FTy->getNumParams() == 0 ||
!isa<PointerType>(FTy->getParamType(0)))
continue;
setDoesNotThrow(F);
setDoesNotCapture(F, 1);
}
break;
case 'a':
if ((NameLen == 4 && !strcmp(NameStr, "atoi")) ||
(NameLen == 4 && !strcmp(NameStr, "atol")) ||
(NameLen == 4 && !strcmp(NameStr, "atof")) ||
(NameLen == 5 && !strcmp(NameStr, "atoll"))) {
if (FTy->getNumParams() != 1 ||
!isa<PointerType>(FTy->getParamType(0)))
continue;