aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/Transforms/Scalar/SimplifyLibCalls.cpp32
-rw-r--r--test/Transforms/SimplifyLibCalls/2008-12-20-StrcmpMemcmp.ll10
2 files changed, 40 insertions, 2 deletions
diff --git a/lib/Transforms/Scalar/SimplifyLibCalls.cpp b/lib/Transforms/Scalar/SimplifyLibCalls.cpp
index 07c8d94..8723208 100644
--- a/lib/Transforms/Scalar/SimplifyLibCalls.cpp
+++ b/lib/Transforms/Scalar/SimplifyLibCalls.cpp
@@ -80,7 +80,10 @@ public:
/// EmitMemChr - 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.
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.
/// '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'
@@ -106,7 +109,7 @@ public:
/// EmitFWrite - 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.
void EmitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder<> &B);
-
+
};
} // End anonymous namespace.
@@ -151,6 +154,19 @@ Value *LibCallOptimization::EmitMemChr(Value *Ptr, Value *Val,
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.
/// '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
@@ -537,6 +553,18 @@ struct VISIBILITY_HIDDEN StrCmpOpt : public LibCallOptimization {
// strcmp(x, y) -> cnst (if both x and y are constant strings)
if (HasStr1 && HasStr2)
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;
}
};
diff --git a/test/Transforms/SimplifyLibCalls/2008-12-20-StrcmpMemcmp.ll b/test/Transforms/SimplifyLibCalls/2008-12-20-StrcmpMemcmp.ll
new file mode 100644
index 0000000..d35da8d
--- /dev/null
+++ b/test/Transforms/SimplifyLibCalls/2008-12-20-StrcmpMemcmp.ll
@@ -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
+}