From 13a09e298c37df0711aeb935e0653ae51313d19a Mon Sep 17 00:00:00 2001 From: Nick Lewycky Date: Sun, 21 Dec 2008 00:19:21 +0000 Subject: 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 --- lib/Transforms/Scalar/SimplifyLibCalls.cpp | 32 ++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) (limited to 'lib/Transforms/Scalar/SimplifyLibCalls.cpp') 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; } }; -- cgit v1.1