From a8d08964475e5a364960d0d1d8a7ea422a31fa8b Mon Sep 17 00:00:00 2001 From: Nick Lewycky Date: Sun, 21 Dec 2008 00:19:21 +0000 Subject: [PATCH] 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 ++++++++++++++++++++-- .../SimplifyLibCalls/2008-12-20-StrcmpMemcmp.ll | 10 +++++++ 2 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 test/Transforms/SimplifyLibCalls/2008-12-20-StrcmpMemcmp.ll diff --git a/lib/Transforms/Scalar/SimplifyLibCalls.cpp b/lib/Transforms/Scalar/SimplifyLibCalls.cpp index 07c8d94860c..87232084051 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 00000000000..d35da8d1efd --- /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 +} -- 2.11.0