Index: Transforms/Utils/SimplifyLibCalls.cpp =================================================================== --- Transforms/Utils/SimplifyLibCalls.cpp +++ Transforms/Utils/SimplifyLibCalls.cpp @@ -32,6 +32,7 @@ #include "llvm/Support/CommandLine.h" #include "llvm/Target/TargetLibraryInfo.h" #include "llvm/Transforms/Utils/BuildLibCalls.h" +#include "llvm/Transforms/Utils/Local.h" using namespace llvm; using namespace PatternMatch; @@ -949,6 +950,26 @@ return B.CreateSub(LHSV, RHSV, "chardiff"); } + // memcmp(S1,S2,8)==0 -> (*(int64_t*)S1 != *(int64_t*)S2)==0 + // (And same for 16/32 bit.) + if (Len <= 8 && isPowerOf2_64(Len) && + isOnlyUsedInZeroEqualityComparison(CI) && + getKnownAlignment(LHS, DL, nullptr, CI) >= Len && + getKnownAlignment(RHS, DL, nullptr, CI) >= Len) { + + IntegerType *IntType = IntegerType::get(CI->getContext(), Len * 8); + + Type *LHSPtrTy = + IntType->getPointerTo(LHS->getType()->getPointerAddressSpace()); + Type *RHSPtrTy = + IntType->getPointerTo(RHS->getType()->getPointerAddressSpace()); + + Value *LHSV = B.CreateLoad(B.CreateBitCast(LHS, LHSPtrTy, "lhsc"), "lhsv"); + Value *RHSV = B.CreateLoad(B.CreateBitCast(RHS, RHSPtrTy, "rhsc"), "rhsv"); + + return B.CreateZExt(B.CreateICmpNE(LHSV, RHSV), CI->getType(), "memcmp"); + } + // Constant folding: memcmp(x, y, l) -> cnst (all arguments are constant) StringRef LHSStr, RHSStr; if (getConstantStringInfo(LHS, LHSStr) &&