function names start with a lowercase letter; NFC

Note: There are no uses of these functions outside of
SimplifyLibCalls, so they could be static functions in
that file.

llvm-svn: 258172
This commit is contained in:
Sanjay Patel 2016-01-19 19:46:10 +00:00
parent 464307ffe7
commit d3112a5bcc
3 changed files with 90 additions and 90 deletions

View File

@ -23,88 +23,88 @@ namespace llvm {
class TargetLibraryInfo; class TargetLibraryInfo;
/// Return V if it is an i8*, otherwise cast it to i8*. /// Return V if it is an i8*, otherwise cast it to i8*.
Value *CastToCStr(Value *V, IRBuilder<> &B); Value *castToCStr(Value *V, IRBuilder<> &B);
/// Emit a call to the strlen function to the builder, for the specified /// Emit a call to the strlen function to the builder, for the specified
/// pointer. Ptr is required to be some pointer type, and the return value has /// pointer. Ptr is required to be some pointer type, and the return value has
/// 'intptr_t' type. /// 'intptr_t' type.
Value *EmitStrLen(Value *Ptr, IRBuilder<> &B, const DataLayout &DL, Value *emitStrLen(Value *Ptr, IRBuilder<> &B, const DataLayout &DL,
const TargetLibraryInfo *TLI); const TargetLibraryInfo *TLI);
/// Emit a call to the strnlen function to the builder, for the specified /// Emit a call to the strnlen function to the builder, for the specified
/// pointer. Ptr is required to be some pointer type, MaxLen must be of size_t /// pointer. Ptr is required to be some pointer type, MaxLen must be of size_t
/// type, and the return value has 'intptr_t' type. /// type, and the return value has 'intptr_t' type.
Value *EmitStrNLen(Value *Ptr, Value *MaxLen, IRBuilder<> &B, Value *emitStrNLen(Value *Ptr, Value *MaxLen, IRBuilder<> &B,
const DataLayout &DL, const TargetLibraryInfo *TLI); const DataLayout &DL, const TargetLibraryInfo *TLI);
/// Emit a call to the strchr function to the builder, for the specified /// Emit a call to the strchr function to the builder, for the specified
/// pointer and character. Ptr is required to be some pointer type, and the /// pointer and character. Ptr is required to be some pointer type, and the
/// return value has 'i8*' type. /// return value has 'i8*' type.
Value *EmitStrChr(Value *Ptr, char C, IRBuilder<> &B, Value *emitStrChr(Value *Ptr, char C, IRBuilder<> &B,
const TargetLibraryInfo *TLI); const TargetLibraryInfo *TLI);
/// Emit a call to the strncmp function to the builder. /// Emit a call to the strncmp function to the builder.
Value *EmitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B, Value *emitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
const DataLayout &DL, const TargetLibraryInfo *TLI); const DataLayout &DL, const TargetLibraryInfo *TLI);
/// Emit a call to the strcpy function to the builder, for the specified /// Emit a call to the strcpy function to the builder, for the specified
/// pointer arguments. /// pointer arguments.
Value *EmitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B, Value *emitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B,
const TargetLibraryInfo *TLI, StringRef Name = "strcpy"); const TargetLibraryInfo *TLI, StringRef Name = "strcpy");
/// Emit a call to the strncpy function to the builder, for the specified /// Emit a call to the strncpy function to the builder, for the specified
/// pointer arguments and length. /// pointer arguments and length.
Value *EmitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B, Value *emitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B,
const TargetLibraryInfo *TLI, StringRef Name = "strncpy"); const TargetLibraryInfo *TLI, StringRef Name = "strncpy");
/// Emit a call to the __memcpy_chk function to the builder. This expects that /// Emit a call to the __memcpy_chk function to the builder. This expects that
/// the Len and ObjSize have type 'intptr_t' and Dst/Src are pointers. /// the Len and ObjSize have type 'intptr_t' and Dst/Src are pointers.
Value *EmitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize, Value *emitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
IRBuilder<> &B, const DataLayout &DL, IRBuilder<> &B, const DataLayout &DL,
const TargetLibraryInfo *TLI); const TargetLibraryInfo *TLI);
/// Emit a call to the memchr function. This assumes that Ptr is a pointer, /// Emit a call to the memchr function. This assumes that Ptr is a pointer,
/// Val is an i32 value, and Len is an 'intptr_t' value. /// Val is an i32 value, and Len is an 'intptr_t' value.
Value *EmitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilder<> &B, Value *emitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilder<> &B,
const DataLayout &DL, const TargetLibraryInfo *TLI); const DataLayout &DL, const TargetLibraryInfo *TLI);
/// Emit a call to the memcmp function. /// Emit a call to the memcmp function.
Value *EmitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B, Value *emitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
const DataLayout &DL, const TargetLibraryInfo *TLI); const DataLayout &DL, const TargetLibraryInfo *TLI);
/// Emit a call to the unary function named 'Name' (e.g. 'floor'). This /// Emit a call to the unary function named 'Name' (e.g. 'floor'). This
/// function is known to take a single of type matching 'Op' and returns one /// function is known to take a single of type matching 'Op' and returns one
/// value with the same type. If 'Op' is a long double, 'l' is added as the /// value with the same type. If 'Op' is a long double, 'l' is added as the
/// suffix of name, if 'Op' is a float, we add a 'f' suffix. /// suffix of name, if 'Op' is a float, we add a 'f' suffix.
Value *EmitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilder<> &B, Value *emitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilder<> &B,
const AttributeSet &Attrs); const AttributeSet &Attrs);
/// Emit a call to the binary function named 'Name' (e.g. 'fmin'). This /// Emit a call to the binary function named 'Name' (e.g. 'fmin'). This
/// function is known to take type matching 'Op1' and 'Op2' and return one /// function is known to take type matching 'Op1' and 'Op2' and return one
/// value with the same type. If 'Op1/Op2' are long double, 'l' is added as /// value with the same type. If 'Op1/Op2' are long double, 'l' is added as
/// the suffix of name, if 'Op1/Op2' are float, we add a 'f' suffix. /// the suffix of name, if 'Op1/Op2' are float, we add a 'f' suffix.
Value *EmitBinaryFloatFnCall(Value *Op1, Value *Op2, StringRef Name, Value *emitBinaryFloatFnCall(Value *Op1, Value *Op2, StringRef Name,
IRBuilder<> &B, const AttributeSet &Attrs); IRBuilder<> &B, const AttributeSet &Attrs);
/// Emit a call to the putchar function. This assumes that Char is an integer. /// Emit a call to the putchar function. This assumes that Char is an integer.
Value *EmitPutChar(Value *Char, IRBuilder<> &B, const TargetLibraryInfo *TLI); Value *emitPutChar(Value *Char, IRBuilder<> &B, const TargetLibraryInfo *TLI);
/// Emit a call to the puts function. This assumes that Str is some pointer. /// Emit a call to the puts function. This assumes that Str is some pointer.
Value *EmitPutS(Value *Str, IRBuilder<> &B, const TargetLibraryInfo *TLI); Value *emitPutS(Value *Str, IRBuilder<> &B, const TargetLibraryInfo *TLI);
/// Emit a call to the fputc function. This assumes that Char is an i32, and /// Emit a call to the fputc function. This assumes that Char is an i32, and
/// File is a pointer to FILE. /// File is a pointer to FILE.
Value *EmitFPutC(Value *Char, Value *File, IRBuilder<> &B, Value *emitFPutC(Value *Char, Value *File, IRBuilder<> &B,
const TargetLibraryInfo *TLI); const TargetLibraryInfo *TLI);
/// Emit a call to the puts function. Str is required to be a pointer and /// Emit a call to the puts function. Str is required to be a pointer and
/// File is a pointer to FILE. /// File is a pointer to FILE.
Value *EmitFPutS(Value *Str, Value *File, IRBuilder<> &B, Value *emitFPutS(Value *Str, Value *File, IRBuilder<> &B,
const TargetLibraryInfo *TLI); const TargetLibraryInfo *TLI);
/// Emit a call to the fwrite function. This assumes that Ptr is a pointer, /// Emit a call to the fwrite function. This assumes that Ptr is a pointer,
/// Size is an 'intptr_t', and File is a pointer to FILE. /// Size is an 'intptr_t', and File is a pointer to FILE.
Value *EmitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder<> &B, Value *emitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder<> &B,
const DataLayout &DL, const TargetLibraryInfo *TLI); const DataLayout &DL, const TargetLibraryInfo *TLI);
} }

View File

@ -25,12 +25,12 @@
using namespace llvm; using namespace llvm;
Value *llvm::CastToCStr(Value *V, IRBuilder<> &B) { Value *llvm::castToCStr(Value *V, IRBuilder<> &B) {
unsigned AS = V->getType()->getPointerAddressSpace(); unsigned AS = V->getType()->getPointerAddressSpace();
return B.CreateBitCast(V, B.getInt8PtrTy(AS), "cstr"); return B.CreateBitCast(V, B.getInt8PtrTy(AS), "cstr");
} }
Value *llvm::EmitStrLen(Value *Ptr, IRBuilder<> &B, const DataLayout &DL, Value *llvm::emitStrLen(Value *Ptr, IRBuilder<> &B, const DataLayout &DL,
const TargetLibraryInfo *TLI) { const TargetLibraryInfo *TLI) {
if (!TLI->has(LibFunc::strlen)) if (!TLI->has(LibFunc::strlen))
return nullptr; return nullptr;
@ -45,14 +45,14 @@ Value *llvm::EmitStrLen(Value *Ptr, IRBuilder<> &B, const DataLayout &DL,
Constant *StrLen = M->getOrInsertFunction( Constant *StrLen = M->getOrInsertFunction(
"strlen", AttributeSet::get(M->getContext(), AS), "strlen", AttributeSet::get(M->getContext(), AS),
DL.getIntPtrType(Context), B.getInt8PtrTy(), nullptr); DL.getIntPtrType(Context), B.getInt8PtrTy(), nullptr);
CallInst *CI = B.CreateCall(StrLen, CastToCStr(Ptr, B), "strlen"); CallInst *CI = B.CreateCall(StrLen, castToCStr(Ptr, B), "strlen");
if (const Function *F = dyn_cast<Function>(StrLen->stripPointerCasts())) if (const Function *F = dyn_cast<Function>(StrLen->stripPointerCasts()))
CI->setCallingConv(F->getCallingConv()); CI->setCallingConv(F->getCallingConv());
return CI; return CI;
} }
Value *llvm::EmitStrChr(Value *Ptr, char C, IRBuilder<> &B, Value *llvm::emitStrChr(Value *Ptr, char C, IRBuilder<> &B,
const TargetLibraryInfo *TLI) { const TargetLibraryInfo *TLI) {
if (!TLI->has(LibFunc::strchr)) if (!TLI->has(LibFunc::strchr))
return nullptr; return nullptr;
@ -69,13 +69,13 @@ Value *llvm::EmitStrChr(Value *Ptr, char C, IRBuilder<> &B,
AS), AS),
I8Ptr, I8Ptr, I32Ty, nullptr); I8Ptr, I8Ptr, I32Ty, nullptr);
CallInst *CI = B.CreateCall( CallInst *CI = B.CreateCall(
StrChr, {CastToCStr(Ptr, B), ConstantInt::get(I32Ty, C)}, "strchr"); StrChr, {castToCStr(Ptr, B), ConstantInt::get(I32Ty, C)}, "strchr");
if (const Function *F = dyn_cast<Function>(StrChr->stripPointerCasts())) if (const Function *F = dyn_cast<Function>(StrChr->stripPointerCasts()))
CI->setCallingConv(F->getCallingConv()); CI->setCallingConv(F->getCallingConv());
return CI; return CI;
} }
Value *llvm::EmitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B, Value *llvm::emitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
const DataLayout &DL, const TargetLibraryInfo *TLI) { const DataLayout &DL, const TargetLibraryInfo *TLI) {
if (!TLI->has(LibFunc::strncmp)) if (!TLI->has(LibFunc::strncmp))
return nullptr; return nullptr;
@ -92,7 +92,7 @@ Value *llvm::EmitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
"strncmp", AttributeSet::get(M->getContext(), AS), B.getInt32Ty(), "strncmp", AttributeSet::get(M->getContext(), AS), B.getInt32Ty(),
B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context), nullptr); B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context), nullptr);
CallInst *CI = B.CreateCall( CallInst *CI = B.CreateCall(
StrNCmp, {CastToCStr(Ptr1, B), CastToCStr(Ptr2, B), Len}, "strncmp"); StrNCmp, {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, "strncmp");
if (const Function *F = dyn_cast<Function>(StrNCmp->stripPointerCasts())) if (const Function *F = dyn_cast<Function>(StrNCmp->stripPointerCasts()))
CI->setCallingConv(F->getCallingConv()); CI->setCallingConv(F->getCallingConv());
@ -100,7 +100,7 @@ Value *llvm::EmitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
return CI; return CI;
} }
Value *llvm::EmitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B, Value *llvm::emitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B,
const TargetLibraryInfo *TLI, StringRef Name) { const TargetLibraryInfo *TLI, StringRef Name) {
if (!TLI->has(LibFunc::strcpy)) if (!TLI->has(LibFunc::strcpy))
return nullptr; return nullptr;
@ -115,13 +115,13 @@ Value *llvm::EmitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B,
AttributeSet::get(M->getContext(), AS), AttributeSet::get(M->getContext(), AS),
I8Ptr, I8Ptr, I8Ptr, nullptr); I8Ptr, I8Ptr, I8Ptr, nullptr);
CallInst *CI = CallInst *CI =
B.CreateCall(StrCpy, {CastToCStr(Dst, B), CastToCStr(Src, B)}, Name); B.CreateCall(StrCpy, {castToCStr(Dst, B), castToCStr(Src, B)}, Name);
if (const Function *F = dyn_cast<Function>(StrCpy->stripPointerCasts())) if (const Function *F = dyn_cast<Function>(StrCpy->stripPointerCasts()))
CI->setCallingConv(F->getCallingConv()); CI->setCallingConv(F->getCallingConv());
return CI; return CI;
} }
Value *llvm::EmitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B, Value *llvm::emitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B,
const TargetLibraryInfo *TLI, StringRef Name) { const TargetLibraryInfo *TLI, StringRef Name) {
if (!TLI->has(LibFunc::strncpy)) if (!TLI->has(LibFunc::strncpy))
return nullptr; return nullptr;
@ -138,13 +138,13 @@ Value *llvm::EmitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B,
I8Ptr, I8Ptr, I8Ptr, I8Ptr, I8Ptr, I8Ptr,
Len->getType(), nullptr); Len->getType(), nullptr);
CallInst *CI = B.CreateCall( CallInst *CI = B.CreateCall(
StrNCpy, {CastToCStr(Dst, B), CastToCStr(Src, B), Len}, "strncpy"); StrNCpy, {castToCStr(Dst, B), castToCStr(Src, B), Len}, "strncpy");
if (const Function *F = dyn_cast<Function>(StrNCpy->stripPointerCasts())) if (const Function *F = dyn_cast<Function>(StrNCpy->stripPointerCasts()))
CI->setCallingConv(F->getCallingConv()); CI->setCallingConv(F->getCallingConv());
return CI; return CI;
} }
Value *llvm::EmitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize, Value *llvm::emitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
IRBuilder<> &B, const DataLayout &DL, IRBuilder<> &B, const DataLayout &DL,
const TargetLibraryInfo *TLI) { const TargetLibraryInfo *TLI) {
if (!TLI->has(LibFunc::memcpy_chk)) if (!TLI->has(LibFunc::memcpy_chk))
@ -159,15 +159,15 @@ Value *llvm::EmitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
"__memcpy_chk", AttributeSet::get(M->getContext(), AS), B.getInt8PtrTy(), "__memcpy_chk", AttributeSet::get(M->getContext(), AS), B.getInt8PtrTy(),
B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context), B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context),
DL.getIntPtrType(Context), nullptr); DL.getIntPtrType(Context), nullptr);
Dst = CastToCStr(Dst, B); Dst = castToCStr(Dst, B);
Src = CastToCStr(Src, B); Src = castToCStr(Src, B);
CallInst *CI = B.CreateCall(MemCpy, {Dst, Src, Len, ObjSize}); CallInst *CI = B.CreateCall(MemCpy, {Dst, Src, Len, ObjSize});
if (const Function *F = dyn_cast<Function>(MemCpy->stripPointerCasts())) if (const Function *F = dyn_cast<Function>(MemCpy->stripPointerCasts()))
CI->setCallingConv(F->getCallingConv()); CI->setCallingConv(F->getCallingConv());
return CI; return CI;
} }
Value *llvm::EmitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilder<> &B, Value *llvm::emitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilder<> &B,
const DataLayout &DL, const TargetLibraryInfo *TLI) { const DataLayout &DL, const TargetLibraryInfo *TLI) {
if (!TLI->has(LibFunc::memchr)) if (!TLI->has(LibFunc::memchr))
return nullptr; return nullptr;
@ -180,7 +180,7 @@ Value *llvm::EmitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilder<> &B,
Value *MemChr = M->getOrInsertFunction( Value *MemChr = M->getOrInsertFunction(
"memchr", AttributeSet::get(M->getContext(), AS), B.getInt8PtrTy(), "memchr", AttributeSet::get(M->getContext(), AS), B.getInt8PtrTy(),
B.getInt8PtrTy(), B.getInt32Ty(), DL.getIntPtrType(Context), nullptr); B.getInt8PtrTy(), B.getInt32Ty(), DL.getIntPtrType(Context), nullptr);
CallInst *CI = B.CreateCall(MemChr, {CastToCStr(Ptr, B), Val, Len}, "memchr"); CallInst *CI = B.CreateCall(MemChr, {castToCStr(Ptr, B), Val, Len}, "memchr");
if (const Function *F = dyn_cast<Function>(MemChr->stripPointerCasts())) if (const Function *F = dyn_cast<Function>(MemChr->stripPointerCasts()))
CI->setCallingConv(F->getCallingConv()); CI->setCallingConv(F->getCallingConv());
@ -188,7 +188,7 @@ Value *llvm::EmitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilder<> &B,
return CI; return CI;
} }
Value *llvm::EmitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B, Value *llvm::emitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
const DataLayout &DL, const TargetLibraryInfo *TLI) { const DataLayout &DL, const TargetLibraryInfo *TLI) {
if (!TLI->has(LibFunc::memcmp)) if (!TLI->has(LibFunc::memcmp))
return nullptr; return nullptr;
@ -205,7 +205,7 @@ Value *llvm::EmitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
"memcmp", AttributeSet::get(M->getContext(), AS), B.getInt32Ty(), "memcmp", AttributeSet::get(M->getContext(), AS), B.getInt32Ty(),
B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context), nullptr); B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context), nullptr);
CallInst *CI = B.CreateCall( CallInst *CI = B.CreateCall(
MemCmp, {CastToCStr(Ptr1, B), CastToCStr(Ptr2, B), Len}, "memcmp"); MemCmp, {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, "memcmp");
if (const Function *F = dyn_cast<Function>(MemCmp->stripPointerCasts())) if (const Function *F = dyn_cast<Function>(MemCmp->stripPointerCasts()))
CI->setCallingConv(F->getCallingConv()); CI->setCallingConv(F->getCallingConv());
@ -214,7 +214,7 @@ Value *llvm::EmitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
} }
/// Append a suffix to the function name according to the type of 'Op'. /// Append a suffix to the function name according to the type of 'Op'.
static void AppendTypeSuffix(Value *Op, StringRef &Name, static void appendTypeSuffix(Value *Op, StringRef &Name,
SmallString<20> &NameBuffer) { SmallString<20> &NameBuffer) {
if (!Op->getType()->isDoubleTy()) { if (!Op->getType()->isDoubleTy()) {
NameBuffer += Name; NameBuffer += Name;
@ -229,10 +229,10 @@ static void AppendTypeSuffix(Value *Op, StringRef &Name,
return; return;
} }
Value *llvm::EmitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilder<> &B, Value *llvm::emitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilder<> &B,
const AttributeSet &Attrs) { const AttributeSet &Attrs) {
SmallString<20> NameBuffer; SmallString<20> NameBuffer;
AppendTypeSuffix(Op, Name, NameBuffer); appendTypeSuffix(Op, Name, NameBuffer);
Module *M = B.GetInsertBlock()->getParent()->getParent(); Module *M = B.GetInsertBlock()->getParent()->getParent();
Value *Callee = M->getOrInsertFunction(Name, Op->getType(), Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
@ -245,10 +245,10 @@ Value *llvm::EmitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilder<> &B,
return CI; return CI;
} }
Value *llvm::EmitBinaryFloatFnCall(Value *Op1, Value *Op2, StringRef Name, Value *llvm::emitBinaryFloatFnCall(Value *Op1, Value *Op2, StringRef Name,
IRBuilder<> &B, const AttributeSet &Attrs) { IRBuilder<> &B, const AttributeSet &Attrs) {
SmallString<20> NameBuffer; SmallString<20> NameBuffer;
AppendTypeSuffix(Op1, Name, NameBuffer); appendTypeSuffix(Op1, Name, NameBuffer);
Module *M = B.GetInsertBlock()->getParent()->getParent(); Module *M = B.GetInsertBlock()->getParent()->getParent();
Value *Callee = M->getOrInsertFunction(Name, Op1->getType(), Op1->getType(), Value *Callee = M->getOrInsertFunction(Name, Op1->getType(), Op1->getType(),
@ -261,7 +261,7 @@ Value *llvm::EmitBinaryFloatFnCall(Value *Op1, Value *Op2, StringRef Name,
return CI; return CI;
} }
Value *llvm::EmitPutChar(Value *Char, IRBuilder<> &B, Value *llvm::emitPutChar(Value *Char, IRBuilder<> &B,
const TargetLibraryInfo *TLI) { const TargetLibraryInfo *TLI) {
if (!TLI->has(LibFunc::putchar)) if (!TLI->has(LibFunc::putchar))
return nullptr; return nullptr;
@ -281,7 +281,7 @@ Value *llvm::EmitPutChar(Value *Char, IRBuilder<> &B,
return CI; return CI;
} }
Value *llvm::EmitPutS(Value *Str, IRBuilder<> &B, Value *llvm::emitPutS(Value *Str, IRBuilder<> &B,
const TargetLibraryInfo *TLI) { const TargetLibraryInfo *TLI) {
if (!TLI->has(LibFunc::puts)) if (!TLI->has(LibFunc::puts))
return nullptr; return nullptr;
@ -297,13 +297,13 @@ Value *llvm::EmitPutS(Value *Str, IRBuilder<> &B,
B.getInt32Ty(), B.getInt32Ty(),
B.getInt8PtrTy(), B.getInt8PtrTy(),
nullptr); nullptr);
CallInst *CI = B.CreateCall(PutS, CastToCStr(Str, B), "puts"); CallInst *CI = B.CreateCall(PutS, castToCStr(Str, B), "puts");
if (const Function *F = dyn_cast<Function>(PutS->stripPointerCasts())) if (const Function *F = dyn_cast<Function>(PutS->stripPointerCasts()))
CI->setCallingConv(F->getCallingConv()); CI->setCallingConv(F->getCallingConv());
return CI; return CI;
} }
Value *llvm::EmitFPutC(Value *Char, Value *File, IRBuilder<> &B, Value *llvm::emitFPutC(Value *Char, Value *File, IRBuilder<> &B,
const TargetLibraryInfo *TLI) { const TargetLibraryInfo *TLI) {
if (!TLI->has(LibFunc::fputc)) if (!TLI->has(LibFunc::fputc))
return nullptr; return nullptr;
@ -334,7 +334,7 @@ Value *llvm::EmitFPutC(Value *Char, Value *File, IRBuilder<> &B,
return CI; return CI;
} }
Value *llvm::EmitFPutS(Value *Str, Value *File, IRBuilder<> &B, Value *llvm::emitFPutS(Value *Str, Value *File, IRBuilder<> &B,
const TargetLibraryInfo *TLI) { const TargetLibraryInfo *TLI) {
if (!TLI->has(LibFunc::fputs)) if (!TLI->has(LibFunc::fputs))
return nullptr; return nullptr;
@ -357,14 +357,14 @@ Value *llvm::EmitFPutS(Value *Str, Value *File, IRBuilder<> &B,
F = M->getOrInsertFunction(FPutsName, B.getInt32Ty(), F = M->getOrInsertFunction(FPutsName, B.getInt32Ty(),
B.getInt8PtrTy(), B.getInt8PtrTy(),
File->getType(), nullptr); File->getType(), nullptr);
CallInst *CI = B.CreateCall(F, {CastToCStr(Str, B), File}, "fputs"); CallInst *CI = B.CreateCall(F, {castToCStr(Str, B), File}, "fputs");
if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts())) if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
CI->setCallingConv(Fn->getCallingConv()); CI->setCallingConv(Fn->getCallingConv());
return CI; return CI;
} }
Value *llvm::EmitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder<> &B, Value *llvm::emitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder<> &B,
const DataLayout &DL, const TargetLibraryInfo *TLI) { const DataLayout &DL, const TargetLibraryInfo *TLI) {
if (!TLI->has(LibFunc::fwrite)) if (!TLI->has(LibFunc::fwrite))
return nullptr; return nullptr;
@ -389,7 +389,7 @@ Value *llvm::EmitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder<> &B,
DL.getIntPtrType(Context), File->getType(), DL.getIntPtrType(Context), File->getType(),
nullptr); nullptr);
CallInst *CI = CallInst *CI =
B.CreateCall(F, {CastToCStr(Ptr, B), Size, B.CreateCall(F, {castToCStr(Ptr, B), Size,
ConstantInt::get(DL.getIntPtrType(Context), 1), File}); ConstantInt::get(DL.getIntPtrType(Context), 1), File});
if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts())) if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))

View File

@ -203,7 +203,7 @@ Value *LibCallSimplifier::emitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len,
IRBuilder<> &B) { IRBuilder<> &B) {
// We need to find the end of the destination string. That's where the // We need to find the end of the destination string. That's where the
// memory is to be moved to. We just generate a call to strlen. // memory is to be moved to. We just generate a call to strlen.
Value *DstLen = EmitStrLen(Dst, B, DL, TLI); Value *DstLen = emitStrLen(Dst, B, DL, TLI);
if (!DstLen) if (!DstLen)
return nullptr; return nullptr;
@ -281,7 +281,7 @@ Value *LibCallSimplifier::optimizeStrChr(CallInst *CI, IRBuilder<> &B) {
if (Len == 0 || !FT->getParamType(1)->isIntegerTy(32)) // memchr needs i32. if (Len == 0 || !FT->getParamType(1)->isIntegerTy(32)) // memchr needs i32.
return nullptr; return nullptr;
return EmitMemChr(SrcStr, CI->getArgOperand(1), // include nul. return emitMemChr(SrcStr, CI->getArgOperand(1), // include nul.
ConstantInt::get(DL.getIntPtrType(CI->getContext()), Len), ConstantInt::get(DL.getIntPtrType(CI->getContext()), Len),
B, DL, TLI); B, DL, TLI);
} }
@ -291,7 +291,7 @@ Value *LibCallSimplifier::optimizeStrChr(CallInst *CI, IRBuilder<> &B) {
StringRef Str; StringRef Str;
if (!getConstantStringInfo(SrcStr, Str)) { if (!getConstantStringInfo(SrcStr, Str)) {
if (CharC->isZero()) // strchr(p, 0) -> p + strlen(p) if (CharC->isZero()) // strchr(p, 0) -> p + strlen(p)
return B.CreateGEP(B.getInt8Ty(), SrcStr, EmitStrLen(SrcStr, B, DL, TLI), return B.CreateGEP(B.getInt8Ty(), SrcStr, emitStrLen(SrcStr, B, DL, TLI),
"strchr"); "strchr");
return nullptr; return nullptr;
} }
@ -328,7 +328,7 @@ Value *LibCallSimplifier::optimizeStrRChr(CallInst *CI, IRBuilder<> &B) {
if (!getConstantStringInfo(SrcStr, Str)) { if (!getConstantStringInfo(SrcStr, Str)) {
// strrchr(s, 0) -> strchr(s, 0) // strrchr(s, 0) -> strchr(s, 0)
if (CharC->isZero()) if (CharC->isZero())
return EmitStrChr(SrcStr, '\0', B, TLI); return emitStrChr(SrcStr, '\0', B, TLI);
return nullptr; return nullptr;
} }
@ -375,7 +375,7 @@ Value *LibCallSimplifier::optimizeStrCmp(CallInst *CI, IRBuilder<> &B) {
uint64_t Len1 = GetStringLength(Str1P); uint64_t Len1 = GetStringLength(Str1P);
uint64_t Len2 = GetStringLength(Str2P); uint64_t Len2 = GetStringLength(Str2P);
if (Len1 && Len2) { if (Len1 && Len2) {
return EmitMemCmp(Str1P, Str2P, return emitMemCmp(Str1P, Str2P,
ConstantInt::get(DL.getIntPtrType(CI->getContext()), ConstantInt::get(DL.getIntPtrType(CI->getContext()),
std::min(Len1, Len2)), std::min(Len1, Len2)),
B, DL, TLI); B, DL, TLI);
@ -409,7 +409,7 @@ Value *LibCallSimplifier::optimizeStrNCmp(CallInst *CI, IRBuilder<> &B) {
return ConstantInt::get(CI->getType(), 0); return ConstantInt::get(CI->getType(), 0);
if (Length == 1) // strncmp(x,y,1) -> memcmp(x,y,1) if (Length == 1) // strncmp(x,y,1) -> memcmp(x,y,1)
return EmitMemCmp(Str1P, Str2P, CI->getArgOperand(2), B, DL, TLI); return emitMemCmp(Str1P, Str2P, CI->getArgOperand(2), B, DL, TLI);
StringRef Str1, Str2; StringRef Str1, Str2;
bool HasStr1 = getConstantStringInfo(Str1P, Str1); bool HasStr1 = getConstantStringInfo(Str1P, Str1);
@ -461,7 +461,7 @@ Value *LibCallSimplifier::optimizeStpCpy(CallInst *CI, IRBuilder<> &B) {
Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1); Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
if (Dst == Src) { // stpcpy(x,x) -> x+strlen(x) if (Dst == Src) { // stpcpy(x,x) -> x+strlen(x)
Value *StrLen = EmitStrLen(Src, B, DL, TLI); Value *StrLen = emitStrLen(Src, B, DL, TLI);
return StrLen ? B.CreateInBoundsGEP(B.getInt8Ty(), Dst, StrLen) : nullptr; return StrLen ? B.CreateInBoundsGEP(B.getInt8Ty(), Dst, StrLen) : nullptr;
} }
@ -587,7 +587,7 @@ Value *LibCallSimplifier::optimizeStrPBrk(CallInst *CI, IRBuilder<> &B) {
// strpbrk(s, "a") -> strchr(s, 'a') // strpbrk(s, "a") -> strchr(s, 'a')
if (HasS2 && S2.size() == 1) if (HasS2 && S2.size() == 1)
return EmitStrChr(CI->getArgOperand(0), S2[0], B, TLI); return emitStrChr(CI->getArgOperand(0), S2[0], B, TLI);
return nullptr; return nullptr;
} }
@ -664,7 +664,7 @@ Value *LibCallSimplifier::optimizeStrCSpn(CallInst *CI, IRBuilder<> &B) {
// strcspn(s, "") -> strlen(s) // strcspn(s, "") -> strlen(s)
if (HasS2 && S2.empty()) if (HasS2 && S2.empty())
return EmitStrLen(CI->getArgOperand(0), B, DL, TLI); return emitStrLen(CI->getArgOperand(0), B, DL, TLI);
return nullptr; return nullptr;
} }
@ -683,10 +683,10 @@ Value *LibCallSimplifier::optimizeStrStr(CallInst *CI, IRBuilder<> &B) {
// fold strstr(a, b) == a -> strncmp(a, b, strlen(b)) == 0 // fold strstr(a, b) == a -> strncmp(a, b, strlen(b)) == 0
if (isOnlyUsedInEqualityComparison(CI, CI->getArgOperand(0))) { if (isOnlyUsedInEqualityComparison(CI, CI->getArgOperand(0))) {
Value *StrLen = EmitStrLen(CI->getArgOperand(1), B, DL, TLI); Value *StrLen = emitStrLen(CI->getArgOperand(1), B, DL, TLI);
if (!StrLen) if (!StrLen)
return nullptr; return nullptr;
Value *StrNCmp = EmitStrNCmp(CI->getArgOperand(0), CI->getArgOperand(1), Value *StrNCmp = emitStrNCmp(CI->getArgOperand(0), CI->getArgOperand(1),
StrLen, B, DL, TLI); StrLen, B, DL, TLI);
if (!StrNCmp) if (!StrNCmp)
return nullptr; return nullptr;
@ -717,14 +717,14 @@ Value *LibCallSimplifier::optimizeStrStr(CallInst *CI, IRBuilder<> &B) {
return Constant::getNullValue(CI->getType()); return Constant::getNullValue(CI->getType());
// strstr("abcd", "bc") -> gep((char*)"abcd", 1) // strstr("abcd", "bc") -> gep((char*)"abcd", 1)
Value *Result = CastToCStr(CI->getArgOperand(0), B); Value *Result = castToCStr(CI->getArgOperand(0), B);
Result = B.CreateConstInBoundsGEP1_64(Result, Offset, "strstr"); Result = B.CreateConstInBoundsGEP1_64(Result, Offset, "strstr");
return B.CreateBitCast(Result, CI->getType()); return B.CreateBitCast(Result, CI->getType());
} }
// fold strstr(x, "y") -> strchr(x, 'y'). // fold strstr(x, "y") -> strchr(x, 'y').
if (HasStr2 && ToFindStr.size() == 1) { if (HasStr2 && ToFindStr.size() == 1) {
Value *StrChr = EmitStrChr(CI->getArgOperand(0), ToFindStr[0], B, TLI); Value *StrChr = emitStrChr(CI->getArgOperand(0), ToFindStr[0], B, TLI);
return StrChr ? B.CreateBitCast(StrChr, CI->getType()) : nullptr; return StrChr ? B.CreateBitCast(StrChr, CI->getType()) : nullptr;
} }
return nullptr; return nullptr;
@ -840,9 +840,9 @@ Value *LibCallSimplifier::optimizeMemCmp(CallInst *CI, IRBuilder<> &B) {
// memcmp(S1,S2,1) -> *(unsigned char*)LHS - *(unsigned char*)RHS // memcmp(S1,S2,1) -> *(unsigned char*)LHS - *(unsigned char*)RHS
if (Len == 1) { if (Len == 1) {
Value *LHSV = B.CreateZExt(B.CreateLoad(CastToCStr(LHS, B), "lhsc"), Value *LHSV = B.CreateZExt(B.CreateLoad(castToCStr(LHS, B), "lhsc"),
CI->getType(), "lhsv"); CI->getType(), "lhsv");
Value *RHSV = B.CreateZExt(B.CreateLoad(CastToCStr(RHS, B), "rhsc"), Value *RHSV = B.CreateZExt(B.CreateLoad(castToCStr(RHS, B), "rhsc"),
CI->getType(), "rhsv"); CI->getType(), "rhsv");
return B.CreateSub(LHSV, RHSV, "chardiff"); return B.CreateSub(LHSV, RHSV, "chardiff");
} }
@ -990,7 +990,7 @@ Value *LibCallSimplifier::optimizeUnaryDoubleFP(CallInst *CI, IRBuilder<> &B,
V = B.CreateCall(F, V); V = B.CreateCall(F, V);
} else { } else {
// The call is a library call rather than an intrinsic. // The call is a library call rather than an intrinsic.
V = EmitUnaryFloatFnCall(V, Callee->getName(), B, Callee->getAttributes()); V = emitUnaryFloatFnCall(V, Callee->getName(), B, Callee->getAttributes());
} }
return B.CreateFPExt(V, B.getDoubleTy()); return B.CreateFPExt(V, B.getDoubleTy());
@ -1023,7 +1023,7 @@ Value *LibCallSimplifier::optimizeBinaryDoubleFP(CallInst *CI, IRBuilder<> &B) {
// fmin((double)floatval1, (double)floatval2) // fmin((double)floatval1, (double)floatval2)
// -> (double)fminf(floatval1, floatval2) // -> (double)fminf(floatval1, floatval2)
// TODO: Handle intrinsics in the same way as in optimizeUnaryDoubleFP(). // TODO: Handle intrinsics in the same way as in optimizeUnaryDoubleFP().
Value *V = EmitBinaryFloatFnCall(V1, V2, Callee->getName(), B, Value *V = emitBinaryFloatFnCall(V1, V2, Callee->getName(), B,
Callee->getAttributes()); Callee->getAttributes());
return B.CreateFPExt(V, B.getDoubleTy()); return B.CreateFPExt(V, B.getDoubleTy());
} }
@ -1100,13 +1100,13 @@ Value *LibCallSimplifier::optimizePow(CallInst *CI, IRBuilder<> &B) {
if (Op1C->isExactlyValue(2.0) && if (Op1C->isExactlyValue(2.0) &&
hasUnaryFloatFn(TLI, Op1->getType(), LibFunc::exp2, LibFunc::exp2f, hasUnaryFloatFn(TLI, Op1->getType(), LibFunc::exp2, LibFunc::exp2f,
LibFunc::exp2l)) LibFunc::exp2l))
return EmitUnaryFloatFnCall(Op2, TLI->getName(LibFunc::exp2), B, return emitUnaryFloatFnCall(Op2, TLI->getName(LibFunc::exp2), B,
Callee->getAttributes()); Callee->getAttributes());
// pow(10.0, x) -> exp10(x) // pow(10.0, x) -> exp10(x)
if (Op1C->isExactlyValue(10.0) && if (Op1C->isExactlyValue(10.0) &&
hasUnaryFloatFn(TLI, Op1->getType(), LibFunc::exp10, LibFunc::exp10f, hasUnaryFloatFn(TLI, Op1->getType(), LibFunc::exp10, LibFunc::exp10f,
LibFunc::exp10l)) LibFunc::exp10l))
return EmitUnaryFloatFnCall(Op2, TLI->getName(LibFunc::exp10), B, return emitUnaryFloatFnCall(Op2, TLI->getName(LibFunc::exp10), B,
Callee->getAttributes()); Callee->getAttributes());
} }
@ -1125,7 +1125,7 @@ Value *LibCallSimplifier::optimizePow(CallInst *CI, IRBuilder<> &B) {
IRBuilder<>::FastMathFlagGuard Guard(B); IRBuilder<>::FastMathFlagGuard Guard(B);
B.setFastMathFlags(CI->getFastMathFlags()); B.setFastMathFlags(CI->getFastMathFlags());
Value *FMul = B.CreateFMul(OpC->getArgOperand(0), Op2, "mul"); Value *FMul = B.CreateFMul(OpC->getArgOperand(0), Op2, "mul");
return EmitUnaryFloatFnCall(FMul, OpCCallee->getName(), B, return emitUnaryFloatFnCall(FMul, OpCCallee->getName(), B,
OpCCallee->getAttributes()); OpCCallee->getAttributes());
} }
} }
@ -1147,7 +1147,7 @@ Value *LibCallSimplifier::optimizePow(CallInst *CI, IRBuilder<> &B) {
if (CI->hasUnsafeAlgebra()) { if (CI->hasUnsafeAlgebra()) {
IRBuilder<>::FastMathFlagGuard Guard(B); IRBuilder<>::FastMathFlagGuard Guard(B);
B.setFastMathFlags(CI->getFastMathFlags()); B.setFastMathFlags(CI->getFastMathFlags());
return EmitUnaryFloatFnCall(Op1, TLI->getName(LibFunc::sqrt), B, return emitUnaryFloatFnCall(Op1, TLI->getName(LibFunc::sqrt), B,
Callee->getAttributes()); Callee->getAttributes());
} }
@ -1157,9 +1157,9 @@ Value *LibCallSimplifier::optimizePow(CallInst *CI, IRBuilder<> &B) {
// TODO: In finite-only mode, this could be just fabs(sqrt(x)). // TODO: In finite-only mode, this could be just fabs(sqrt(x)).
Value *Inf = ConstantFP::getInfinity(CI->getType()); Value *Inf = ConstantFP::getInfinity(CI->getType());
Value *NegInf = ConstantFP::getInfinity(CI->getType(), true); Value *NegInf = ConstantFP::getInfinity(CI->getType(), true);
Value *Sqrt = EmitUnaryFloatFnCall(Op1, "sqrt", B, Callee->getAttributes()); Value *Sqrt = emitUnaryFloatFnCall(Op1, "sqrt", B, Callee->getAttributes());
Value *FAbs = Value *FAbs =
EmitUnaryFloatFnCall(Sqrt, "fabs", B, Callee->getAttributes()); emitUnaryFloatFnCall(Sqrt, "fabs", B, Callee->getAttributes());
Value *FCmp = B.CreateFCmpOEQ(Op1, NegInf); Value *FCmp = B.CreateFCmpOEQ(Op1, NegInf);
Value *Sel = B.CreateSelect(FCmp, Inf, FAbs); Value *Sel = B.CreateSelect(FCmp, Inf, FAbs);
return Sel; return Sel;
@ -1360,7 +1360,7 @@ Value *LibCallSimplifier::optimizeLog(CallInst *CI, IRBuilder<> &B) {
if (F && ((TLI->getLibFunc(F->getName(), Func) && TLI->has(Func) && if (F && ((TLI->getLibFunc(F->getName(), Func) && TLI->has(Func) &&
Func == LibFunc::pow) || F->getIntrinsicID() == Intrinsic::pow)) Func == LibFunc::pow) || F->getIntrinsicID() == Intrinsic::pow))
return B.CreateFMul(OpC->getArgOperand(1), return B.CreateFMul(OpC->getArgOperand(1),
EmitUnaryFloatFnCall(OpC->getOperand(0), Callee->getName(), B, emitUnaryFloatFnCall(OpC->getOperand(0), Callee->getName(), B,
Callee->getAttributes()), "mul"); Callee->getAttributes()), "mul");
// log(exp2(y)) -> y*log(2) // log(exp2(y)) -> y*log(2)
@ -1368,7 +1368,7 @@ Value *LibCallSimplifier::optimizeLog(CallInst *CI, IRBuilder<> &B) {
TLI->has(Func) && Func == LibFunc::exp2) TLI->has(Func) && Func == LibFunc::exp2)
return B.CreateFMul( return B.CreateFMul(
OpC->getArgOperand(0), OpC->getArgOperand(0),
EmitUnaryFloatFnCall(ConstantFP::get(CI->getType(), 2.0), emitUnaryFloatFnCall(ConstantFP::get(CI->getType(), 2.0),
Callee->getName(), B, Callee->getAttributes()), Callee->getName(), B, Callee->getAttributes()),
"logmul"); "logmul");
return Ret; return Ret;
@ -1769,7 +1769,7 @@ Value *LibCallSimplifier::optimizePrintFString(CallInst *CI, IRBuilder<> &B) {
// printf("x") -> putchar('x'), even for '%'. // printf("x") -> putchar('x'), even for '%'.
if (FormatStr.size() == 1) { if (FormatStr.size() == 1) {
Value *Res = EmitPutChar(B.getInt32(FormatStr[0]), B, TLI); Value *Res = emitPutChar(B.getInt32(FormatStr[0]), B, TLI);
if (CI->use_empty() || !Res) if (CI->use_empty() || !Res)
return Res; return Res;
return B.CreateIntCast(Res, CI->getType(), true); return B.CreateIntCast(Res, CI->getType(), true);
@ -1782,7 +1782,7 @@ Value *LibCallSimplifier::optimizePrintFString(CallInst *CI, IRBuilder<> &B) {
// pass to be run after this pass, to merge duplicate strings. // pass to be run after this pass, to merge duplicate strings.
FormatStr = FormatStr.drop_back(); FormatStr = FormatStr.drop_back();
Value *GV = B.CreateGlobalString(FormatStr, "str"); Value *GV = B.CreateGlobalString(FormatStr, "str");
Value *NewCI = EmitPutS(GV, B, TLI); Value *NewCI = emitPutS(GV, B, TLI);
return (CI->use_empty() || !NewCI) return (CI->use_empty() || !NewCI)
? NewCI ? NewCI
: ConstantInt::get(CI->getType(), FormatStr.size() + 1); : ConstantInt::get(CI->getType(), FormatStr.size() + 1);
@ -1792,7 +1792,7 @@ Value *LibCallSimplifier::optimizePrintFString(CallInst *CI, IRBuilder<> &B) {
// printf("%c", chr) --> putchar(chr) // printf("%c", chr) --> putchar(chr)
if (FormatStr == "%c" && CI->getNumArgOperands() > 1 && if (FormatStr == "%c" && CI->getNumArgOperands() > 1 &&
CI->getArgOperand(1)->getType()->isIntegerTy()) { CI->getArgOperand(1)->getType()->isIntegerTy()) {
Value *Res = EmitPutChar(CI->getArgOperand(1), B, TLI); Value *Res = emitPutChar(CI->getArgOperand(1), B, TLI);
if (CI->use_empty() || !Res) if (CI->use_empty() || !Res)
return Res; return Res;
@ -1802,7 +1802,7 @@ Value *LibCallSimplifier::optimizePrintFString(CallInst *CI, IRBuilder<> &B) {
// printf("%s\n", str) --> puts(str) // printf("%s\n", str) --> puts(str)
if (FormatStr == "%s\n" && CI->getNumArgOperands() > 1 && if (FormatStr == "%s\n" && CI->getNumArgOperands() > 1 &&
CI->getArgOperand(1)->getType()->isPointerTy()) { CI->getArgOperand(1)->getType()->isPointerTy()) {
return EmitPutS(CI->getArgOperand(1), B, TLI); return emitPutS(CI->getArgOperand(1), B, TLI);
} }
return nullptr; return nullptr;
} }
@ -1868,7 +1868,7 @@ Value *LibCallSimplifier::optimizeSPrintFString(CallInst *CI, IRBuilder<> &B) {
if (!CI->getArgOperand(2)->getType()->isIntegerTy()) if (!CI->getArgOperand(2)->getType()->isIntegerTy())
return nullptr; return nullptr;
Value *V = B.CreateTrunc(CI->getArgOperand(2), B.getInt8Ty(), "char"); Value *V = B.CreateTrunc(CI->getArgOperand(2), B.getInt8Ty(), "char");
Value *Ptr = CastToCStr(CI->getArgOperand(0), B); Value *Ptr = castToCStr(CI->getArgOperand(0), B);
B.CreateStore(V, Ptr); B.CreateStore(V, Ptr);
Ptr = B.CreateGEP(B.getInt8Ty(), Ptr, B.getInt32(1), "nul"); Ptr = B.CreateGEP(B.getInt8Ty(), Ptr, B.getInt32(1), "nul");
B.CreateStore(B.getInt8(0), Ptr); B.CreateStore(B.getInt8(0), Ptr);
@ -1881,7 +1881,7 @@ Value *LibCallSimplifier::optimizeSPrintFString(CallInst *CI, IRBuilder<> &B) {
if (!CI->getArgOperand(2)->getType()->isPointerTy()) if (!CI->getArgOperand(2)->getType()->isPointerTy())
return nullptr; return nullptr;
Value *Len = EmitStrLen(CI->getArgOperand(2), B, DL, TLI); Value *Len = emitStrLen(CI->getArgOperand(2), B, DL, TLI);
if (!Len) if (!Len)
return nullptr; return nullptr;
Value *IncLen = Value *IncLen =
@ -1941,7 +1941,7 @@ Value *LibCallSimplifier::optimizeFPrintFString(CallInst *CI, IRBuilder<> &B) {
if (FormatStr[i] == '%') // Could handle %% -> % if we cared. if (FormatStr[i] == '%') // Could handle %% -> % if we cared.
return nullptr; // We found a format specifier. return nullptr; // We found a format specifier.
return EmitFWrite( return emitFWrite(
CI->getArgOperand(1), CI->getArgOperand(1),
ConstantInt::get(DL.getIntPtrType(CI->getContext()), FormatStr.size()), ConstantInt::get(DL.getIntPtrType(CI->getContext()), FormatStr.size()),
CI->getArgOperand(0), B, DL, TLI); CI->getArgOperand(0), B, DL, TLI);
@ -1958,14 +1958,14 @@ Value *LibCallSimplifier::optimizeFPrintFString(CallInst *CI, IRBuilder<> &B) {
// fprintf(F, "%c", chr) --> fputc(chr, F) // fprintf(F, "%c", chr) --> fputc(chr, F)
if (!CI->getArgOperand(2)->getType()->isIntegerTy()) if (!CI->getArgOperand(2)->getType()->isIntegerTy())
return nullptr; return nullptr;
return EmitFPutC(CI->getArgOperand(2), CI->getArgOperand(0), B, TLI); return emitFPutC(CI->getArgOperand(2), CI->getArgOperand(0), B, TLI);
} }
if (FormatStr[1] == 's') { if (FormatStr[1] == 's') {
// fprintf(F, "%s", str) --> fputs(str, F) // fprintf(F, "%s", str) --> fputs(str, F)
if (!CI->getArgOperand(2)->getType()->isPointerTy()) if (!CI->getArgOperand(2)->getType()->isPointerTy())
return nullptr; return nullptr;
return EmitFPutS(CI->getArgOperand(2), CI->getArgOperand(0), B, TLI); return emitFPutS(CI->getArgOperand(2), CI->getArgOperand(0), B, TLI);
} }
return nullptr; return nullptr;
} }
@ -2024,8 +2024,8 @@ Value *LibCallSimplifier::optimizeFWrite(CallInst *CI, IRBuilder<> &B) {
// If this is writing one byte, turn it into fputc. // If this is writing one byte, turn it into fputc.
// This optimisation is only valid, if the return value is unused. // This optimisation is only valid, if the return value is unused.
if (Bytes == 1 && CI->use_empty()) { // fwrite(S,1,1,F) -> fputc(S[0],F) if (Bytes == 1 && CI->use_empty()) { // fwrite(S,1,1,F) -> fputc(S[0],F)
Value *Char = B.CreateLoad(CastToCStr(CI->getArgOperand(0), B), "char"); Value *Char = B.CreateLoad(castToCStr(CI->getArgOperand(0), B), "char");
Value *NewCI = EmitFPutC(Char, CI->getArgOperand(3), B, TLI); Value *NewCI = emitFPutC(Char, CI->getArgOperand(3), B, TLI);
return NewCI ? ConstantInt::get(CI->getType(), 1) : nullptr; return NewCI ? ConstantInt::get(CI->getType(), 1) : nullptr;
} }
@ -2049,7 +2049,7 @@ Value *LibCallSimplifier::optimizeFPuts(CallInst *CI, IRBuilder<> &B) {
return nullptr; return nullptr;
// Known to have no uses (see above). // Known to have no uses (see above).
return EmitFWrite( return emitFWrite(
CI->getArgOperand(0), CI->getArgOperand(0),
ConstantInt::get(DL.getIntPtrType(CI->getContext()), Len - 1), ConstantInt::get(DL.getIntPtrType(CI->getContext()), Len - 1),
CI->getArgOperand(1), B, DL, TLI); CI->getArgOperand(1), B, DL, TLI);
@ -2070,7 +2070,7 @@ Value *LibCallSimplifier::optimizePuts(CallInst *CI, IRBuilder<> &B) {
if (Str.empty() && CI->use_empty()) { if (Str.empty() && CI->use_empty()) {
// puts("") -> putchar('\n') // puts("") -> putchar('\n')
Value *Res = EmitPutChar(B.getInt32('\n'), B, TLI); Value *Res = emitPutChar(B.getInt32('\n'), B, TLI);
if (CI->use_empty() || !Res) if (CI->use_empty() || !Res)
return Res; return Res;
return B.CreateIntCast(Res, CI->getType(), true); return B.CreateIntCast(Res, CI->getType(), true);
@ -2474,7 +2474,7 @@ Value *FortifiedLibCallSimplifier::optimizeStrpCpyChk(CallInst *CI,
// __stpcpy_chk(x,x,...) -> x+strlen(x) // __stpcpy_chk(x,x,...) -> x+strlen(x)
if (Func == LibFunc::stpcpy_chk && !OnlyLowerUnknownSize && Dst == Src) { if (Func == LibFunc::stpcpy_chk && !OnlyLowerUnknownSize && Dst == Src) {
Value *StrLen = EmitStrLen(Src, B, DL, TLI); Value *StrLen = emitStrLen(Src, B, DL, TLI);
return StrLen ? B.CreateInBoundsGEP(B.getInt8Ty(), Dst, StrLen) : nullptr; return StrLen ? B.CreateInBoundsGEP(B.getInt8Ty(), Dst, StrLen) : nullptr;
} }
@ -2484,7 +2484,7 @@ Value *FortifiedLibCallSimplifier::optimizeStrpCpyChk(CallInst *CI,
// TODO: It might be nice to get a maximum length out of the possible // TODO: It might be nice to get a maximum length out of the possible
// string lengths for varying. // string lengths for varying.
if (isFortifiedCallFoldable(CI, 2, 1, true)) if (isFortifiedCallFoldable(CI, 2, 1, true))
return EmitStrCpy(Dst, Src, B, TLI, Name.substr(2, 6)); return emitStrCpy(Dst, Src, B, TLI, Name.substr(2, 6));
if (OnlyLowerUnknownSize) if (OnlyLowerUnknownSize)
return nullptr; return nullptr;
@ -2496,7 +2496,7 @@ Value *FortifiedLibCallSimplifier::optimizeStrpCpyChk(CallInst *CI,
Type *SizeTTy = DL.getIntPtrType(CI->getContext()); Type *SizeTTy = DL.getIntPtrType(CI->getContext());
Value *LenV = ConstantInt::get(SizeTTy, Len); Value *LenV = ConstantInt::get(SizeTTy, Len);
Value *Ret = EmitMemCpyChk(Dst, Src, LenV, ObjSize, B, DL, TLI); Value *Ret = emitMemCpyChk(Dst, Src, LenV, ObjSize, B, DL, TLI);
// If the function was an __stpcpy_chk, and we were able to fold it into // If the function was an __stpcpy_chk, and we were able to fold it into
// a __memcpy_chk, we still need to return the correct end pointer. // a __memcpy_chk, we still need to return the correct end pointer.
if (Ret && Func == LibFunc::stpcpy_chk) if (Ret && Func == LibFunc::stpcpy_chk)
@ -2513,7 +2513,7 @@ Value *FortifiedLibCallSimplifier::optimizeStrpNCpyChk(CallInst *CI,
if (!checkStringCopyLibFuncSignature(Callee, Func)) if (!checkStringCopyLibFuncSignature(Callee, Func))
return nullptr; return nullptr;
if (isFortifiedCallFoldable(CI, 3, 2, false)) { if (isFortifiedCallFoldable(CI, 3, 2, false)) {
Value *Ret = EmitStrNCpy(CI->getArgOperand(0), CI->getArgOperand(1), Value *Ret = emitStrNCpy(CI->getArgOperand(0), CI->getArgOperand(1),
CI->getArgOperand(2), B, TLI, Name.substr(2, 7)); CI->getArgOperand(2), B, TLI, Name.substr(2, 7));
return Ret; return Ret;
} }