forked from OSchip/llvm-project
NFC: Pull out a function to reduce some duplication
Part of https://reviews.llvm.org/D62358 llvm-svn: 362271
This commit is contained in:
parent
fa6bcd0b96
commit
5234921119
|
@ -70,12 +70,22 @@ namespace llvm {
|
|||
/// Emit a call to the strcpy function to the builder, for the specified
|
||||
/// pointer arguments.
|
||||
Value *emitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B,
|
||||
const TargetLibraryInfo *TLI, StringRef Name = "strcpy");
|
||||
const TargetLibraryInfo *TLI);
|
||||
|
||||
/// Emit a call to the stpcpy function to the builder, for the specified
|
||||
/// pointer arguments.
|
||||
Value *emitStpCpy(Value *Dst, Value *Src, IRBuilder<> &B,
|
||||
const TargetLibraryInfo *TLI);
|
||||
|
||||
/// Emit a call to the strncpy function to the builder, for the specified
|
||||
/// pointer arguments and length.
|
||||
Value *emitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B,
|
||||
const TargetLibraryInfo *TLI, StringRef Name = "strncpy");
|
||||
const TargetLibraryInfo *TLI);
|
||||
|
||||
/// Emit a call to the stpncpy function to the builder, for the specified
|
||||
/// pointer arguments and length.
|
||||
Value *emitStpNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B,
|
||||
const TargetLibraryInfo *TLI);
|
||||
|
||||
/// 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.
|
||||
|
|
|
@ -789,100 +789,76 @@ Value *llvm::castToCStr(Value *V, IRBuilder<> &B) {
|
|||
return B.CreateBitCast(V, B.getInt8PtrTy(AS), "cstr");
|
||||
}
|
||||
|
||||
Value *llvm::emitStrLen(Value *Ptr, IRBuilder<> &B, const DataLayout &DL,
|
||||
const TargetLibraryInfo *TLI) {
|
||||
if (!TLI->has(LibFunc_strlen))
|
||||
static Value *emitLibCall(LibFunc TheLibFunc, Type *ReturnType,
|
||||
ArrayRef<Type *> ParamTypes,
|
||||
ArrayRef<Value *> Operands, IRBuilder<> &B,
|
||||
const TargetLibraryInfo *TLI,
|
||||
bool IsVaArgs = false) {
|
||||
if (!TLI->has(TheLibFunc))
|
||||
return nullptr;
|
||||
|
||||
Module *M = B.GetInsertBlock()->getModule();
|
||||
StringRef StrlenName = TLI->getName(LibFunc_strlen);
|
||||
LLVMContext &Context = B.GetInsertBlock()->getContext();
|
||||
FunctionCallee StrLen = M->getOrInsertFunction(
|
||||
StrlenName, DL.getIntPtrType(Context), B.getInt8PtrTy());
|
||||
inferLibFuncAttributes(M, StrlenName, *TLI);
|
||||
CallInst *CI = B.CreateCall(StrLen, castToCStr(Ptr, B), StrlenName);
|
||||
StringRef FuncName = TLI->getName(TheLibFunc);
|
||||
FunctionType *FuncType = FunctionType::get(ReturnType, ParamTypes, IsVaArgs);
|
||||
FunctionCallee Callee = M->getOrInsertFunction(FuncName, FuncType);
|
||||
inferLibFuncAttributes(M, FuncName, *TLI);
|
||||
CallInst *CI = B.CreateCall(Callee, Operands, FuncName);
|
||||
if (const Function *F =
|
||||
dyn_cast<Function>(StrLen.getCallee()->stripPointerCasts()))
|
||||
dyn_cast<Function>(Callee.getCallee()->stripPointerCasts()))
|
||||
CI->setCallingConv(F->getCallingConv());
|
||||
|
||||
return CI;
|
||||
}
|
||||
|
||||
Value *llvm::emitStrLen(Value *Ptr, IRBuilder<> &B, const DataLayout &DL,
|
||||
const TargetLibraryInfo *TLI) {
|
||||
LLVMContext &Context = B.GetInsertBlock()->getContext();
|
||||
return emitLibCall(LibFunc_strlen, DL.getIntPtrType(Context),
|
||||
B.getInt8PtrTy(), castToCStr(Ptr, B), B, TLI);
|
||||
}
|
||||
|
||||
Value *llvm::emitStrChr(Value *Ptr, char C, IRBuilder<> &B,
|
||||
const TargetLibraryInfo *TLI) {
|
||||
if (!TLI->has(LibFunc_strchr))
|
||||
return nullptr;
|
||||
|
||||
Module *M = B.GetInsertBlock()->getModule();
|
||||
StringRef StrChrName = TLI->getName(LibFunc_strchr);
|
||||
Type *I8Ptr = B.getInt8PtrTy();
|
||||
Type *I32Ty = B.getInt32Ty();
|
||||
FunctionCallee StrChr =
|
||||
M->getOrInsertFunction(StrChrName, I8Ptr, I8Ptr, I32Ty);
|
||||
inferLibFuncAttributes(M, StrChrName, *TLI);
|
||||
CallInst *CI = B.CreateCall(
|
||||
StrChr, {castToCStr(Ptr, B), ConstantInt::get(I32Ty, C)}, StrChrName);
|
||||
if (const Function *F =
|
||||
dyn_cast<Function>(StrChr.getCallee()->stripPointerCasts()))
|
||||
CI->setCallingConv(F->getCallingConv());
|
||||
return CI;
|
||||
return emitLibCall(LibFunc_strchr, I8Ptr, {I8Ptr, I32Ty},
|
||||
{castToCStr(Ptr, B), ConstantInt::get(I32Ty, C)}, B, TLI);
|
||||
}
|
||||
|
||||
Value *llvm::emitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
|
||||
const DataLayout &DL, const TargetLibraryInfo *TLI) {
|
||||
if (!TLI->has(LibFunc_strncmp))
|
||||
return nullptr;
|
||||
|
||||
Module *M = B.GetInsertBlock()->getModule();
|
||||
StringRef StrNCmpName = TLI->getName(LibFunc_strncmp);
|
||||
LLVMContext &Context = B.GetInsertBlock()->getContext();
|
||||
FunctionCallee StrNCmp =
|
||||
M->getOrInsertFunction(StrNCmpName, B.getInt32Ty(), B.getInt8PtrTy(),
|
||||
B.getInt8PtrTy(), DL.getIntPtrType(Context));
|
||||
inferLibFuncAttributes(M, StrNCmpName, *TLI);
|
||||
CallInst *CI = B.CreateCall(
|
||||
StrNCmp, {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, StrNCmpName);
|
||||
|
||||
if (const Function *F =
|
||||
dyn_cast<Function>(StrNCmp.getCallee()->stripPointerCasts()))
|
||||
CI->setCallingConv(F->getCallingConv());
|
||||
|
||||
return CI;
|
||||
return emitLibCall(
|
||||
LibFunc_strncmp, B.getInt32Ty(),
|
||||
{B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context)},
|
||||
{castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, B, TLI);
|
||||
}
|
||||
|
||||
Value *llvm::emitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B,
|
||||
const TargetLibraryInfo *TLI, StringRef Name) {
|
||||
if (!TLI->has(LibFunc_strcpy))
|
||||
return nullptr;
|
||||
|
||||
Module *M = B.GetInsertBlock()->getModule();
|
||||
const TargetLibraryInfo *TLI) {
|
||||
Type *I8Ptr = B.getInt8PtrTy();
|
||||
FunctionCallee StrCpy = M->getOrInsertFunction(Name, I8Ptr, I8Ptr, I8Ptr);
|
||||
inferLibFuncAttributes(M, Name, *TLI);
|
||||
CallInst *CI =
|
||||
B.CreateCall(StrCpy, {castToCStr(Dst, B), castToCStr(Src, B)}, Name);
|
||||
if (const Function *F =
|
||||
dyn_cast<Function>(StrCpy.getCallee()->stripPointerCasts()))
|
||||
CI->setCallingConv(F->getCallingConv());
|
||||
return CI;
|
||||
return emitLibCall(LibFunc_strcpy, I8Ptr, {I8Ptr, I8Ptr},
|
||||
{castToCStr(Dst, B), castToCStr(Src, B)}, B, TLI);
|
||||
}
|
||||
|
||||
Value *llvm::emitStpCpy(Value *Dst, Value *Src, IRBuilder<> &B,
|
||||
const TargetLibraryInfo *TLI) {
|
||||
Type *I8Ptr = B.getInt8PtrTy();
|
||||
return emitLibCall(LibFunc_stpcpy, I8Ptr, {I8Ptr, I8Ptr},
|
||||
{castToCStr(Dst, B), castToCStr(Src, B)}, B, TLI);
|
||||
}
|
||||
|
||||
Value *llvm::emitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B,
|
||||
const TargetLibraryInfo *TLI, StringRef Name) {
|
||||
if (!TLI->has(LibFunc_strncpy))
|
||||
return nullptr;
|
||||
|
||||
Module *M = B.GetInsertBlock()->getModule();
|
||||
const TargetLibraryInfo *TLI) {
|
||||
Type *I8Ptr = B.getInt8PtrTy();
|
||||
FunctionCallee StrNCpy =
|
||||
M->getOrInsertFunction(Name, I8Ptr, I8Ptr, I8Ptr, Len->getType());
|
||||
inferLibFuncAttributes(M, Name, *TLI);
|
||||
CallInst *CI = B.CreateCall(
|
||||
StrNCpy, {castToCStr(Dst, B), castToCStr(Src, B), Len}, Name);
|
||||
if (const Function *F =
|
||||
dyn_cast<Function>(StrNCpy.getCallee()->stripPointerCasts()))
|
||||
CI->setCallingConv(F->getCallingConv());
|
||||
return CI;
|
||||
return emitLibCall(LibFunc_strncpy, I8Ptr, {I8Ptr, I8Ptr, Len->getType()},
|
||||
{castToCStr(Dst, B), castToCStr(Src, B), Len}, B, TLI);
|
||||
}
|
||||
|
||||
Value *llvm::emitStpNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B,
|
||||
const TargetLibraryInfo *TLI) {
|
||||
Type *I8Ptr = B.getInt8PtrTy();
|
||||
return emitLibCall(LibFunc_stpncpy, I8Ptr, {I8Ptr, I8Ptr, Len->getType()},
|
||||
{castToCStr(Dst, B), castToCStr(Src, B), Len}, B, TLI);
|
||||
}
|
||||
|
||||
Value *llvm::emitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
|
||||
|
@ -911,58 +887,29 @@ Value *llvm::emitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
|
|||
|
||||
Value *llvm::emitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilder<> &B,
|
||||
const DataLayout &DL, const TargetLibraryInfo *TLI) {
|
||||
if (!TLI->has(LibFunc_memchr))
|
||||
return nullptr;
|
||||
|
||||
Module *M = B.GetInsertBlock()->getModule();
|
||||
StringRef MemChrName = TLI->getName(LibFunc_memchr);
|
||||
LLVMContext &Context = B.GetInsertBlock()->getContext();
|
||||
FunctionCallee MemChr =
|
||||
M->getOrInsertFunction(MemChrName, B.getInt8PtrTy(), B.getInt8PtrTy(),
|
||||
B.getInt32Ty(), DL.getIntPtrType(Context));
|
||||
inferLibFuncAttributes(M, MemChrName, *TLI);
|
||||
CallInst *CI = B.CreateCall(MemChr, {castToCStr(Ptr, B), Val, Len}, MemChrName);
|
||||
|
||||
if (const Function *F =
|
||||
dyn_cast<Function>(MemChr.getCallee()->stripPointerCasts()))
|
||||
CI->setCallingConv(F->getCallingConv());
|
||||
|
||||
return CI;
|
||||
}
|
||||
|
||||
// Common code for memcmp() and bcmp(), which have the exact same properties,
|
||||
// just a slight difference in semantics.
|
||||
static Value *emitMemCmpOrBcmp(llvm::LibFunc libfunc, Value *Ptr1, Value *Ptr2,
|
||||
Value *Len, IRBuilder<> &B, const DataLayout &DL,
|
||||
const TargetLibraryInfo *TLI) {
|
||||
if (!TLI->has(libfunc))
|
||||
return nullptr;
|
||||
|
||||
Module *M = B.GetInsertBlock()->getModule();
|
||||
StringRef CmpFnName = TLI->getName(libfunc);
|
||||
LLVMContext &Context = B.GetInsertBlock()->getContext();
|
||||
FunctionCallee CmpFn =
|
||||
M->getOrInsertFunction(CmpFnName, B.getInt32Ty(), B.getInt8PtrTy(),
|
||||
B.getInt8PtrTy(), DL.getIntPtrType(Context));
|
||||
inferLibFuncAttributes(M, CmpFnName, *TLI);
|
||||
CallInst *CI = B.CreateCall(
|
||||
CmpFn, {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, CmpFnName);
|
||||
|
||||
if (const Function *F =
|
||||
dyn_cast<Function>(CmpFn.getCallee()->stripPointerCasts()))
|
||||
CI->setCallingConv(F->getCallingConv());
|
||||
|
||||
return CI;
|
||||
return emitLibCall(
|
||||
LibFunc_memchr, B.getInt8PtrTy(),
|
||||
{B.getInt8PtrTy(), B.getInt32Ty(), DL.getIntPtrType(Context)},
|
||||
{castToCStr(Ptr, B), Val, Len}, B, TLI);
|
||||
}
|
||||
|
||||
Value *llvm::emitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
|
||||
const DataLayout &DL, const TargetLibraryInfo *TLI) {
|
||||
return emitMemCmpOrBcmp(LibFunc_memcmp, Ptr1, Ptr2, Len, B, DL, TLI);
|
||||
LLVMContext &Context = B.GetInsertBlock()->getContext();
|
||||
return emitLibCall(
|
||||
LibFunc_memcmp, B.getInt32Ty(),
|
||||
{B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context)},
|
||||
{castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, B, TLI);
|
||||
}
|
||||
|
||||
Value *llvm::emitBCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
|
||||
const DataLayout &DL, const TargetLibraryInfo *TLI) {
|
||||
return emitMemCmpOrBcmp(LibFunc_bcmp, Ptr1, Ptr2, Len, B, DL, TLI);
|
||||
LLVMContext &Context = B.GetInsertBlock()->getContext();
|
||||
return emitLibCall(
|
||||
LibFunc_bcmp, B.getInt32Ty(),
|
||||
{B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context)},
|
||||
{castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, B, TLI);
|
||||
}
|
||||
|
||||
/// Append a suffix to the function name according to the type of 'Op'.
|
||||
|
|
|
@ -2887,8 +2887,6 @@ Value *FortifiedLibCallSimplifier::optimizeMemSetChk(CallInst *CI,
|
|||
Value *FortifiedLibCallSimplifier::optimizeStrpCpyChk(CallInst *CI,
|
||||
IRBuilder<> &B,
|
||||
LibFunc Func) {
|
||||
Function *Callee = CI->getCalledFunction();
|
||||
StringRef Name = Callee->getName();
|
||||
const DataLayout &DL = CI->getModule()->getDataLayout();
|
||||
Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1),
|
||||
*ObjSize = CI->getArgOperand(2);
|
||||
|
@ -2904,8 +2902,12 @@ Value *FortifiedLibCallSimplifier::optimizeStrpCpyChk(CallInst *CI,
|
|||
// st[rp]cpy_chk call which may fail at runtime if the size is too long.
|
||||
// TODO: It might be nice to get a maximum length out of the possible
|
||||
// string lengths for varying.
|
||||
if (isFortifiedCallFoldable(CI, 2, 1, true))
|
||||
return emitStrCpy(Dst, Src, B, TLI, Name.substr(2, 6));
|
||||
if (isFortifiedCallFoldable(CI, 2, 1, true)) {
|
||||
if (Func == LibFunc_strcpy_chk)
|
||||
return emitStrCpy(Dst, Src, B, TLI);
|
||||
else
|
||||
return emitStpCpy(Dst, Src, B, TLI);
|
||||
}
|
||||
|
||||
if (OnlyLowerUnknownSize)
|
||||
return nullptr;
|
||||
|
@ -2928,13 +2930,15 @@ Value *FortifiedLibCallSimplifier::optimizeStrpCpyChk(CallInst *CI,
|
|||
Value *FortifiedLibCallSimplifier::optimizeStrpNCpyChk(CallInst *CI,
|
||||
IRBuilder<> &B,
|
||||
LibFunc Func) {
|
||||
Function *Callee = CI->getCalledFunction();
|
||||
StringRef Name = Callee->getName();
|
||||
if (isFortifiedCallFoldable(CI, 3, 2, false)) {
|
||||
Value *Ret = emitStrNCpy(CI->getArgOperand(0), CI->getArgOperand(1),
|
||||
CI->getArgOperand(2), B, TLI, Name.substr(2, 7));
|
||||
return Ret;
|
||||
if (Func == LibFunc_strncpy_chk)
|
||||
return emitStrNCpy(CI->getArgOperand(0), CI->getArgOperand(1),
|
||||
CI->getArgOperand(2), B, TLI);
|
||||
else
|
||||
return emitStpNCpy(CI->getArgOperand(0), CI->getArgOperand(1),
|
||||
CI->getArgOperand(2), B, TLI);
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue