mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2026-04-20 16:17:38 +00:00
Teach simplify libcall to transform __strcpy_chk to __memcpy_chk to enable optimizations down stream.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@99282 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@@ -49,10 +49,13 @@ protected:
|
||||
Function *Caller;
|
||||
const TargetData *TD;
|
||||
LLVMContext* Context;
|
||||
bool OptChkCall; // True if it's optimizing a *_chk libcall.
|
||||
public:
|
||||
LibCallOptimization() { }
|
||||
LibCallOptimization() : OptChkCall(false) { }
|
||||
virtual ~LibCallOptimization() {}
|
||||
|
||||
void setOptChkCall(bool c) { OptChkCall = c; }
|
||||
|
||||
/// CallOptimizer - This pure virtual method is implemented by base classes to
|
||||
/// do various optimizations. If this returns null then no transformation was
|
||||
/// performed. If it returns CI, then it transformed the call and CI is to be
|
||||
@@ -352,8 +355,10 @@ struct StrNCmpOpt : public LibCallOptimization {
|
||||
struct StrCpyOpt : public LibCallOptimization {
|
||||
virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
|
||||
// Verify the "strcpy" function prototype.
|
||||
unsigned NumParams = OptChkCall ? 3 : 2;
|
||||
const FunctionType *FT = Callee->getFunctionType();
|
||||
if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
|
||||
if (FT->getNumParams() != NumParams ||
|
||||
FT->getReturnType() != FT->getParamType(0) ||
|
||||
FT->getParamType(0) != FT->getParamType(1) ||
|
||||
FT->getParamType(0) != Type::getInt8PtrTy(*Context))
|
||||
return 0;
|
||||
@@ -371,8 +376,13 @@ struct StrCpyOpt : public LibCallOptimization {
|
||||
|
||||
// We have enough information to now generate the memcpy call to do the
|
||||
// concatenation for us. Make a memcpy to copy the nul byte with align = 1.
|
||||
EmitMemCpy(Dst, Src,
|
||||
ConstantInt::get(TD->getIntPtrType(*Context), Len), 1, B, TD);
|
||||
if (OptChkCall)
|
||||
EmitMemCpyChk(Dst, Src,
|
||||
ConstantInt::get(TD->getIntPtrType(*Context), Len),
|
||||
CI->getOperand(3), B, TD);
|
||||
else
|
||||
EmitMemCpy(Dst, Src,
|
||||
ConstantInt::get(TD->getIntPtrType(*Context), Len), 1, B, TD);
|
||||
return Dst;
|
||||
}
|
||||
};
|
||||
@@ -1162,7 +1172,8 @@ namespace {
|
||||
StringMap<LibCallOptimization*> Optimizations;
|
||||
// String and Memory LibCall Optimizations
|
||||
StrCatOpt StrCat; StrNCatOpt StrNCat; StrChrOpt StrChr; StrCmpOpt StrCmp;
|
||||
StrNCmpOpt StrNCmp; StrCpyOpt StrCpy; StrNCpyOpt StrNCpy; StrLenOpt StrLen;
|
||||
StrNCmpOpt StrNCmp; StrCpyOpt StrCpy; StrCpyOpt StrCpyChk;
|
||||
StrNCpyOpt StrNCpy; StrLenOpt StrLen;
|
||||
StrToOpt StrTo; StrStrOpt StrStr;
|
||||
MemCmpOpt MemCmp; MemCpyOpt MemCpy; MemMoveOpt MemMove; MemSetOpt MemSet;
|
||||
// Math Library Optimizations
|
||||
@@ -1228,6 +1239,10 @@ void SimplifyLibCalls::InitOptimizations() {
|
||||
Optimizations["memmove"] = &MemMove;
|
||||
Optimizations["memset"] = &MemSet;
|
||||
|
||||
// _chk variants of String and Memory LibCall Optimizations.
|
||||
StrCpyChk.setOptChkCall(true);
|
||||
Optimizations["__strcpy_chk"] = &StrCpyChk;
|
||||
|
||||
// Math Library Optimizations
|
||||
Optimizations["powf"] = &Pow;
|
||||
Optimizations["pow"] = &Pow;
|
||||
|
||||
@@ -108,7 +108,7 @@ Value *llvm::EmitStrNCpy(Value *Dst, Value *Src, Value *Len,
|
||||
|
||||
|
||||
/// EmitMemCpy - Emit a call to the memcpy function to the builder. This always
|
||||
/// expects that the size has type 'intptr_t' and Dst/Src are pointers.
|
||||
/// expects that Len has type 'intptr_t' and Dst/Src are pointers.
|
||||
Value *llvm::EmitMemCpy(Value *Dst, Value *Src, Value *Len,
|
||||
unsigned Align, IRBuilder<> &B, const TargetData *TD) {
|
||||
Module *M = B.GetInsertBlock()->getParent()->getParent();
|
||||
@@ -120,6 +120,30 @@ Value *llvm::EmitMemCpy(Value *Dst, Value *Src, Value *Len,
|
||||
ConstantInt::get(B.getInt32Ty(), Align));
|
||||
}
|
||||
|
||||
/// EmitMemCpyChk - 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.
|
||||
Value *llvm::EmitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
|
||||
IRBuilder<> &B, const TargetData *TD) {
|
||||
Module *M = B.GetInsertBlock()->getParent()->getParent();
|
||||
AttributeWithIndex AWI;
|
||||
AWI = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
|
||||
LLVMContext &Context = B.GetInsertBlock()->getContext();
|
||||
Value *MemCpy = M->getOrInsertFunction("__memcpy_chk",
|
||||
AttrListPtr::get(&AWI, 1),
|
||||
B.getInt8PtrTy(),
|
||||
B.getInt8PtrTy(),
|
||||
B.getInt8PtrTy(),
|
||||
TD->getIntPtrType(Context),
|
||||
TD->getIntPtrType(Context), NULL);
|
||||
Dst = CastToCStr(Dst, B);
|
||||
Src = CastToCStr(Src, B);
|
||||
CallInst *CI = B.CreateCall4(MemCpy, Dst, Src, Len, ObjSize);
|
||||
if (const Function *F = dyn_cast<Function>(MemCpy->stripPointerCasts()))
|
||||
CI->setCallingConv(F->getCallingConv());
|
||||
return CI;
|
||||
}
|
||||
|
||||
/// EmitMemMove - Emit a call to the memmove function to the builder. This
|
||||
/// always expects that the size has type 'intptr_t' and Dst/Src are pointers.
|
||||
Value *llvm::EmitMemMove(Value *Dst, Value *Src, Value *Len,
|
||||
|
||||
@@ -1,30 +1,37 @@
|
||||
; Test that the StrCpyOptimizer works correctly
|
||||
; RUN: opt < %s -simplify-libcalls -S | \
|
||||
; RUN: not grep {call.*strcpy}
|
||||
; RUN: opt < %s -simplify-libcalls -S | FileCheck %s
|
||||
|
||||
; This transformation requires the pointer size, as it assumes that size_t is
|
||||
; the size of a pointer.
|
||||
target datalayout = "-p:64:64:64"
|
||||
target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128-n8:16:32"
|
||||
|
||||
@hello = constant [6 x i8] c"hello\00" ; <[6 x i8]*> [#uses=1]
|
||||
@null = constant [1 x i8] zeroinitializer ; <[1 x i8]*> [#uses=1]
|
||||
@null_hello = constant [7 x i8] c"\00hello\00" ; <[7 x i8]*> [#uses=1]
|
||||
@hello = constant [6 x i8] c"hello\00"
|
||||
|
||||
declare i8* @strcpy(i8*, i8*)
|
||||
|
||||
declare i32 @puts(i8*)
|
||||
declare i8* @__strcpy_chk(i8*, i8*, i32) nounwind
|
||||
|
||||
define i32 @main() {
|
||||
%target = alloca [1024 x i8] ; <[1024 x i8]*> [#uses=1]
|
||||
%arg1 = getelementptr [1024 x i8]* %target, i32 0, i32 0 ; <i8*> [#uses=2]
|
||||
store i8 0, i8* %arg1
|
||||
%arg2 = getelementptr [6 x i8]* @hello, i32 0, i32 0 ; <i8*> [#uses=1]
|
||||
%rslt1 = call i8* @strcpy( i8* %arg1, i8* %arg2 ) ; <i8*> [#uses=1]
|
||||
%arg3 = getelementptr [1 x i8]* @null, i32 0, i32 0 ; <i8*> [#uses=1]
|
||||
%rslt2 = call i8* @strcpy( i8* %rslt1, i8* %arg3 ) ; <i8*> [#uses=1]
|
||||
%arg4 = getelementptr [7 x i8]* @null_hello, i32 0, i32 0 ; <i8*> [#uses=1]
|
||||
%rslt3 = call i8* @strcpy( i8* %rslt2, i8* %arg4 ) ; <i8*> [#uses=1]
|
||||
call i32 @puts( i8* %rslt3 ) ; <i32>:1 [#uses=0]
|
||||
ret i32 0
|
||||
declare i32 @llvm.objectsize.i32(i8*, i1) nounwind readonly
|
||||
|
||||
; rdar://6839935
|
||||
|
||||
define i32 @t1() {
|
||||
; CHECK: @t1
|
||||
%target = alloca [1024 x i8]
|
||||
%arg1 = getelementptr [1024 x i8]* %target, i32 0, i32 0
|
||||
%arg2 = getelementptr [6 x i8]* @hello, i32 0, i32 0
|
||||
%rslt1 = call i8* @strcpy( i8* %arg1, i8* %arg2 )
|
||||
; CHECK: @llvm.memcpy.i32
|
||||
ret i32 0
|
||||
}
|
||||
|
||||
define i32 @t2() {
|
||||
; CHECK: @t2
|
||||
%target = alloca [1024 x i8]
|
||||
%arg1 = getelementptr [1024 x i8]* %target, i32 0, i32 0
|
||||
%arg2 = getelementptr [6 x i8]* @hello, i32 0, i32 0
|
||||
%tmp1 = call i32 @llvm.objectsize.i32(i8* %arg1, i1 false)
|
||||
%rslt1 = call i8* @__strcpy_chk(i8* %arg1, i8* %arg2, i32 %tmp1)
|
||||
; CHECK: @__memcpy_chk
|
||||
ret i32 0
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user