Skip to content

Commit 7a5b040

Browse files
authored
[RISCV] Add initial support of memcmp expansion
There are two passes that have dependency on the implementation of `TargetTransformInfo::enableMemCmpExpansion` : `MergeICmps` and `ExpandMemCmp`. This PR adds the initial implementation of `enableMemCmpExpansion` so that we can have some basic benefits from these two passes. We don't enable expansion when there is no unaligned access support currently because there are some issues about unaligned loads and stores in `ExpandMemcmp` pass. We should fix these issues and enable the expansion later. Vector case hasn't been tested as we don't generate inlined vector instructions for memcmp currently. Reviewers: preames, arcbbb, topperc, asb, dtcxzyw Reviewed By: topperc, preames Pull Request: #107548
1 parent 41248b5 commit 7a5b040

File tree

4 files changed

+9961
-1105
lines changed

4 files changed

+9961
-1105
lines changed

llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2508,3 +2508,22 @@ bool RISCVTTIImpl::isProfitableToSinkOperands(
25082508
}
25092509
return true;
25102510
}
2511+
2512+
RISCVTTIImpl::TTI::MemCmpExpansionOptions
2513+
RISCVTTIImpl::enableMemCmpExpansion(bool OptSize, bool IsZeroCmp) const {
2514+
TTI::MemCmpExpansionOptions Options;
2515+
// TODO: Enable expansion when unaligned access is not supported after we fix
2516+
// issues in ExpandMemcmp.
2517+
if (!(ST->enableUnalignedScalarMem() &&
2518+
(ST->hasStdExtZbb() || ST->hasStdExtZbkb() || IsZeroCmp)))
2519+
return Options;
2520+
2521+
Options.AllowOverlappingLoads = true;
2522+
Options.MaxNumLoads = TLI->getMaxExpandSizeMemcmp(OptSize);
2523+
Options.NumLoadsPerBlock = Options.MaxNumLoads;
2524+
if (ST->is64Bit())
2525+
Options.LoadSizes = {8, 4, 2, 1};
2526+
else
2527+
Options.LoadSizes = {4, 2, 1};
2528+
return Options;
2529+
}

llvm/lib/Target/RISCV/RISCVTargetTransformInfo.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,9 @@ class RISCVTTIImpl : public BasicTTIImplBase<RISCVTTIImpl> {
434434

435435
bool isProfitableToSinkOperands(Instruction *I,
436436
SmallVectorImpl<Use *> &Ops) const;
437+
438+
TTI::MemCmpExpansionOptions enableMemCmpExpansion(bool OptSize,
439+
bool IsZeroCmp) const;
437440
};
438441

439442
} // end namespace llvm

0 commit comments

Comments
 (0)