mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-06-14 14:24:05 +00:00
[SimplifyLibCalls] Factor out fortified libcall handling.
This lets us remove CGP duplicate. Differential Revision: http://reviews.llvm.org/D6541 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@225640 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@ -486,135 +486,3 @@ Value *llvm::EmitFWrite(Value *Ptr, Value *Size, Value *File,
|
||||
CI->setCallingConv(Fn->getCallingConv());
|
||||
return CI;
|
||||
}
|
||||
|
||||
SimplifyFortifiedLibCalls::~SimplifyFortifiedLibCalls() { }
|
||||
|
||||
bool SimplifyFortifiedLibCalls::fold(CallInst *CI, const DataLayout *TD,
|
||||
const TargetLibraryInfo *TLI) {
|
||||
// We really need DataLayout for later.
|
||||
if (!TD) return false;
|
||||
|
||||
this->CI = CI;
|
||||
Function *Callee = CI->getCalledFunction();
|
||||
StringRef Name = Callee->getName();
|
||||
FunctionType *FT = Callee->getFunctionType();
|
||||
LLVMContext &Context = CI->getParent()->getContext();
|
||||
IRBuilder<> B(CI);
|
||||
|
||||
if (Name == "__memcpy_chk") {
|
||||
// Check if this has the right signature.
|
||||
if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
|
||||
!FT->getParamType(0)->isPointerTy() ||
|
||||
!FT->getParamType(1)->isPointerTy() ||
|
||||
FT->getParamType(2) != TD->getIntPtrType(Context) ||
|
||||
FT->getParamType(3) != TD->getIntPtrType(Context))
|
||||
return false;
|
||||
|
||||
if (isFoldable(3, 2, false)) {
|
||||
B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
|
||||
CI->getArgOperand(2), 1);
|
||||
replaceCall(CI->getArgOperand(0));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Should be similar to memcpy.
|
||||
if (Name == "__mempcpy_chk") {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (Name == "__memmove_chk") {
|
||||
// Check if this has the right signature.
|
||||
if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
|
||||
!FT->getParamType(0)->isPointerTy() ||
|
||||
!FT->getParamType(1)->isPointerTy() ||
|
||||
FT->getParamType(2) != TD->getIntPtrType(Context) ||
|
||||
FT->getParamType(3) != TD->getIntPtrType(Context))
|
||||
return false;
|
||||
|
||||
if (isFoldable(3, 2, false)) {
|
||||
B.CreateMemMove(CI->getArgOperand(0), CI->getArgOperand(1),
|
||||
CI->getArgOperand(2), 1);
|
||||
replaceCall(CI->getArgOperand(0));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
if (Name == "__memset_chk") {
|
||||
// Check if this has the right signature.
|
||||
if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
|
||||
!FT->getParamType(0)->isPointerTy() ||
|
||||
!FT->getParamType(1)->isIntegerTy() ||
|
||||
FT->getParamType(2) != TD->getIntPtrType(Context) ||
|
||||
FT->getParamType(3) != TD->getIntPtrType(Context))
|
||||
return false;
|
||||
|
||||
if (isFoldable(3, 2, false)) {
|
||||
Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(),
|
||||
false);
|
||||
B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), 1);
|
||||
replaceCall(CI->getArgOperand(0));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
if (Name == "__strcpy_chk" || Name == "__stpcpy_chk") {
|
||||
// Check if this has the right signature.
|
||||
if (FT->getNumParams() != 3 ||
|
||||
FT->getReturnType() != FT->getParamType(0) ||
|
||||
FT->getParamType(0) != FT->getParamType(1) ||
|
||||
FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
|
||||
FT->getParamType(2) != TD->getIntPtrType(Context))
|
||||
return 0;
|
||||
|
||||
|
||||
// If a) we don't have any length information, or b) we know this will
|
||||
// fit then just lower to a plain st[rp]cpy. Otherwise we'll keep our
|
||||
// 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 (isFoldable(2, 1, true)) {
|
||||
Value *Ret = EmitStrCpy(CI->getArgOperand(0), CI->getArgOperand(1), B, TD,
|
||||
TLI, Name.substr(2, 6));
|
||||
if (!Ret)
|
||||
return false;
|
||||
replaceCall(Ret);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
if (Name == "__strncpy_chk" || Name == "__stpncpy_chk") {
|
||||
// Check if this has the right signature.
|
||||
if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
|
||||
FT->getParamType(0) != FT->getParamType(1) ||
|
||||
FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
|
||||
!FT->getParamType(2)->isIntegerTy() ||
|
||||
FT->getParamType(3) != TD->getIntPtrType(Context))
|
||||
return false;
|
||||
|
||||
if (isFoldable(3, 2, false)) {
|
||||
Value *Ret = EmitStrNCpy(CI->getArgOperand(0), CI->getArgOperand(1),
|
||||
CI->getArgOperand(2), B, TD, TLI,
|
||||
Name.substr(2, 7));
|
||||
if (!Ret)
|
||||
return false;
|
||||
replaceCall(Ret);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
if (Name == "__strcat_chk") {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (Name == "__strncat_chk") {
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
Reference in New Issue
Block a user