mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-07-18 10:24:45 +00:00
Turn strcmp into memcmp, such as strcmp(P, "x") --> memcmp(P, "x", 2).
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@61297 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@@ -81,6 +81,9 @@ public:
|
|||||||
/// a pointer, Val is an i32 value, and Len is an 'intptr_t' value.
|
/// a pointer, 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);
|
||||||
|
|
||||||
|
/// EmitMemCmp - Emit a call to the memcmp function.
|
||||||
|
Value *EmitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B);
|
||||||
|
|
||||||
/// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name' (e.g.
|
/// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name' (e.g.
|
||||||
/// 'floor'). This function is known to take a single of type matching 'Op'
|
/// 'floor'). This 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'
|
/// and returns one value with the same type. If 'Op' is a long double, 'l'
|
||||||
@@ -151,6 +154,19 @@ Value *LibCallOptimization::EmitMemChr(Value *Ptr, Value *Val,
|
|||||||
return B.CreateCall3(MemChr, CastToCStr(Ptr, B), Val, Len, "memchr");
|
return B.CreateCall3(MemChr, CastToCStr(Ptr, B), Val, Len, "memchr");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// EmitMemCmp - Emit a call to the memcmp function.
|
||||||
|
Value *LibCallOptimization::EmitMemCmp(Value *Ptr1, Value *Ptr2,
|
||||||
|
Value *Len, IRBuilder<> &B) {
|
||||||
|
Module *M = Caller->getParent();
|
||||||
|
Value *MemCmp = M->getOrInsertFunction("memcmp",
|
||||||
|
Type::Int32Ty,
|
||||||
|
PointerType::getUnqual(Type::Int8Ty),
|
||||||
|
PointerType::getUnqual(Type::Int8Ty),
|
||||||
|
TD->getIntPtrType(), NULL);
|
||||||
|
return B.CreateCall3(MemCmp, CastToCStr(Ptr1, B), CastToCStr(Ptr2, B),
|
||||||
|
Len, "memcmp");
|
||||||
|
}
|
||||||
|
|
||||||
/// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name' (e.g.
|
/// EmitUnaryFloatFnCall - 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
|
/// 'floor'). This 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
|
/// returns one value with the same type. If 'Op' is a long double, 'l' is
|
||||||
@@ -537,6 +553,18 @@ struct VISIBILITY_HIDDEN StrCmpOpt : public LibCallOptimization {
|
|||||||
// strcmp(x, y) -> cnst (if both x and y are constant strings)
|
// strcmp(x, y) -> cnst (if both x and y are constant strings)
|
||||||
if (HasStr1 && HasStr2)
|
if (HasStr1 && HasStr2)
|
||||||
return ConstantInt::get(CI->getType(), strcmp(Str1.c_str(),Str2.c_str()));
|
return ConstantInt::get(CI->getType(), strcmp(Str1.c_str(),Str2.c_str()));
|
||||||
|
|
||||||
|
// strcmp(P, "x") -> memcmp(P, "x", 2)
|
||||||
|
uint64_t Len1 = GetStringLength(Str1P);
|
||||||
|
uint64_t Len2 = GetStringLength(Str2P);
|
||||||
|
if (Len1 || Len2) {
|
||||||
|
// Choose the smallest Len excluding 0 which means 'unknown'.
|
||||||
|
if (!Len1 || (Len2 && Len2 < Len1))
|
||||||
|
Len1 = Len2;
|
||||||
|
return EmitMemCmp(Str1P, Str2P,
|
||||||
|
ConstantInt::get(TD->getIntPtrType(), Len1), B);
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
10
test/Transforms/SimplifyLibCalls/2008-12-20-StrcmpMemcmp.ll
Normal file
10
test/Transforms/SimplifyLibCalls/2008-12-20-StrcmpMemcmp.ll
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
; RUN: llvm-as < %s | opt -simplify-libcalls | llvm-dis | grep call.*memcmp
|
||||||
|
|
||||||
|
@.str = internal constant [2 x i8] c"x\00"
|
||||||
|
|
||||||
|
declare i32 @strcmp(i8* %dest, i8* %src)
|
||||||
|
|
||||||
|
define i32 @foo(i8* %x, i8* %y) {
|
||||||
|
%A = call i32 @strcmp(i8* %x, i8* getelementptr ([2 x i8]* @.str, i32 0, i32 0))
|
||||||
|
ret i32 %A
|
||||||
|
}
|
Reference in New Issue
Block a user