Index: include/llvm/Analysis/TargetTransformInfo.h =================================================================== --- include/llvm/Analysis/TargetTransformInfo.h +++ include/llvm/Analysis/TargetTransformInfo.h @@ -603,6 +603,22 @@ /// \return The size of a cache line in bytes. unsigned getCacheLineSize() const; + /// The possible cache levels + enum CacheLevel { + CL_L1D, // The L1 data cache + CL_L2D, // The L2 data cache + + // We currently do not model L3 caches, as their sizes differ widely between + // microarchitectures. Also, we currently do not have a use for L3 cache + // size modeling yet. + }; + + /// \return The size of the cache level in bytes. + unsigned getCacheSize(CacheLevel Level) const; + + /// \return The associativity of the cache level. + unsigned getCacheAssociativity(CacheLevel Level) const; + /// \return How much before a load we should place the prefetch instruction. /// This is currently measured in number of instructions. unsigned getPrefetchDistance() const; @@ -937,6 +953,8 @@ virtual bool shouldConsiderAddressTypePromotion( const Instruction &I, bool &AllowPromotionWithoutCommonHeader) = 0; virtual unsigned getCacheLineSize() = 0; + virtual unsigned getCacheSize(CacheLevel Level) = 0; + virtual unsigned getCacheAssociativity(CacheLevel Level) = 0; virtual unsigned getPrefetchDistance() = 0; virtual unsigned getMinPrefetchStride() = 0; virtual unsigned getMaxPrefetchIterationsAhead() = 0; @@ -1209,6 +1227,12 @@ unsigned getCacheLineSize() override { return Impl.getCacheLineSize(); } + unsigned getCacheSize(CacheLevel Level) override { + return Impl.getCacheSize(Level); + } + unsigned getCacheAssociativity(CacheLevel Level) override { + return Impl.getCacheAssociativity(Level); + } unsigned getPrefetchDistance() override { return Impl.getPrefetchDistance(); } unsigned getMinPrefetchStride() override { return Impl.getMinPrefetchStride(); Index: include/llvm/Analysis/TargetTransformInfoImpl.h =================================================================== --- include/llvm/Analysis/TargetTransformInfoImpl.h +++ include/llvm/Analysis/TargetTransformInfoImpl.h @@ -340,6 +340,19 @@ unsigned getCacheLineSize() { return 0; } + unsigned getCacheSize(TargetTransformInfo::CacheLevel Level) { + switch (Level) { + case TargetTransformInfo::CL_L1D: + return 32 * 1024; // 32 KByte + case TargetTransformInfo::CL_L2D: + return 256 * 1024; // 256 KByte + } + } + + unsigned getCacheAssociativity(TargetTransformInfo::CacheLevel Level) { + return 8; + } + unsigned getPrefetchDistance() { return 0; } unsigned getMinPrefetchStride() { return 1; } Index: lib/Analysis/TargetTransformInfo.cpp =================================================================== --- lib/Analysis/TargetTransformInfo.cpp +++ lib/Analysis/TargetTransformInfo.cpp @@ -321,6 +321,14 @@ return TTIImpl->getCacheLineSize(); } +unsigned TargetTransformInfo::getCacheSize(CacheLevel Level) const { + return TTIImpl->getCacheSize(Level); +} + +unsigned TargetTransformInfo::getCacheAssociativity(CacheLevel Level) const { + return TTIImpl->getCacheAssociativity(Level); +} + unsigned TargetTransformInfo::getPrefetchDistance() const { return TTIImpl->getPrefetchDistance(); } Index: lib/Target/X86/X86TargetTransformInfo.h =================================================================== --- lib/Target/X86/X86TargetTransformInfo.h +++ lib/Target/X86/X86TargetTransformInfo.h @@ -47,6 +47,12 @@ /// @} + /// \name Cache TTI Implementation + /// @{ + unsigned getCacheSize(TargetTransformInfo::CacheLevel Level) const; + unsigned getCacheAssociativity(TargetTransformInfo::CacheLevel Level) const; + /// @} + /// \name Vector TTI Implementations /// @{ Index: lib/Target/X86/X86TargetTransformInfo.cpp =================================================================== --- lib/Target/X86/X86TargetTransformInfo.cpp +++ lib/Target/X86/X86TargetTransformInfo.cpp @@ -66,6 +66,46 @@ return ST->hasPOPCNT() ? TTI::PSK_FastHardware : TTI::PSK_Software; } +unsigned X86TTIImpl::getCacheSize(TargetTransformInfo::CacheLevel Level) const { + switch (Level) { + case TargetTransformInfo::CL_L1D: + // - Penry + // - Nehalem + // - Westmere + // - Sandy Bridge + // - Ivy Bridge + // - Haswell + // - Broadwell + // - Skylake + // - Kabylake + return 32 * 1024; // 32 KByte + case TargetTransformInfo::CL_L2D: + // - Penry + // - Nehalem + // - Westmere + // - Sandy Bridge + // - Ivy Bridge + // - Haswell + // - Broadwell + // - Skylake + // - Kabylake + return 256 * 1024; // 256 KByte + } +} +unsigned X86TTIImpl::getCacheAssociativity( + TargetTransformInfo::CacheLevel Level) const { + // - Penry + // - Nehalem + // - Westmere + // - Sandy Bridge + // - Ivy Bridge + // - Haswell + // - Broadwell + // - Skylake + // - Kabylake + return 8; +} + unsigned X86TTIImpl::getNumberOfRegisters(bool Vector) { if (Vector && !ST->hasSSE1()) return 0;