mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-07-28 03:25:23 +00:00
simplify-libcalls: fold strstr(a, b) == a -> strncmp(a, b, strlen(b)) == 0
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@106047 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@@ -92,6 +92,20 @@ static bool IsOnlyUsedInZeroEqualityComparison(Value *V) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/// IsOnlyUsedInEqualityComparison - Return true if it is only used in equality
|
||||
/// comparisons with With.
|
||||
static bool IsOnlyUsedInEqualityComparison(Value *V, Value *With) {
|
||||
for (Value::use_iterator UI = V->use_begin(), E = V->use_end();
|
||||
UI != E; ++UI) {
|
||||
if (ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
|
||||
if (IC->isEquality() && IC->getOperand(1) == With)
|
||||
continue;
|
||||
// Unknown instruction.
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
//===----------------------------------------------------------------------===//
|
||||
// String and Memory LibCall Optimizations
|
||||
//===----------------------------------------------------------------------===//
|
||||
@@ -503,6 +517,23 @@ struct StrStrOpt : public LibCallOptimization {
|
||||
if (CI->getOperand(1) == CI->getOperand(2))
|
||||
return B.CreateBitCast(CI->getOperand(1), CI->getType());
|
||||
|
||||
// fold strstr(a, b) == a -> strncmp(a, b, strlen(b)) == 0
|
||||
if (TD && IsOnlyUsedInEqualityComparison(CI, CI->getOperand(1))) {
|
||||
Value *StrLen = EmitStrLen(CI->getOperand(2), B, TD);
|
||||
Value *StrNCmp = EmitStrNCmp(CI->getOperand(1), CI->getOperand(2),
|
||||
StrLen, B, TD);
|
||||
for (Value::use_iterator UI = CI->use_begin(), UE = CI->use_end();
|
||||
UI != UE; ) {
|
||||
ICmpInst *Old = cast<ICmpInst>(UI++);
|
||||
Value *Cmp = B.CreateICmp(Old->getPredicate(), StrNCmp,
|
||||
ConstantInt::getNullValue(StrNCmp->getType()),
|
||||
"cmp");
|
||||
Old->replaceAllUsesWith(Cmp);
|
||||
Old->eraseFromParent();
|
||||
}
|
||||
return CI;
|
||||
}
|
||||
|
||||
// See if either input string is a constant string.
|
||||
std::string SearchStr, ToFindStr;
|
||||
bool HasStr1 = GetConstantStringInfo(CI->getOperand(1), SearchStr);
|
||||
|
Reference in New Issue
Block a user