Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:29

0001 //===-- llvm/Support/Compression.h ---Compression----------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file contains basic functions for compression/decompression.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_SUPPORT_COMPRESSION_H
0014 #define LLVM_SUPPORT_COMPRESSION_H
0015 
0016 #include "llvm/ADT/ArrayRef.h"
0017 #include "llvm/Support/DataTypes.h"
0018 
0019 namespace llvm {
0020 template <typename T> class SmallVectorImpl;
0021 class Error;
0022 
0023 // None indicates no compression. The other members are a subset of
0024 // compression::Format, which is used for compressed debug sections in some
0025 // object file formats (e.g. ELF). This is a separate class as we may add new
0026 // compression::Format members for non-debugging purposes.
0027 enum class DebugCompressionType {
0028   None, ///< No compression
0029   Zlib, ///< zlib
0030   Zstd, ///< Zstandard
0031 };
0032 
0033 namespace compression {
0034 namespace zlib {
0035 
0036 constexpr int NoCompression = 0;
0037 constexpr int BestSpeedCompression = 1;
0038 constexpr int DefaultCompression = 6;
0039 constexpr int BestSizeCompression = 9;
0040 
0041 bool isAvailable();
0042 
0043 void compress(ArrayRef<uint8_t> Input,
0044               SmallVectorImpl<uint8_t> &CompressedBuffer,
0045               int Level = DefaultCompression);
0046 
0047 Error decompress(ArrayRef<uint8_t> Input, uint8_t *Output,
0048                  size_t &UncompressedSize);
0049 
0050 Error decompress(ArrayRef<uint8_t> Input, SmallVectorImpl<uint8_t> &Output,
0051                  size_t UncompressedSize);
0052 
0053 } // End of namespace zlib
0054 
0055 namespace zstd {
0056 
0057 constexpr int NoCompression = -5;
0058 constexpr int BestSpeedCompression = 1;
0059 constexpr int DefaultCompression = 5;
0060 constexpr int BestSizeCompression = 12;
0061 
0062 bool isAvailable();
0063 
0064 void compress(ArrayRef<uint8_t> Input,
0065               SmallVectorImpl<uint8_t> &CompressedBuffer,
0066               int Level = DefaultCompression, bool EnableLdm = false);
0067 
0068 Error decompress(ArrayRef<uint8_t> Input, uint8_t *Output,
0069                  size_t &UncompressedSize);
0070 
0071 Error decompress(ArrayRef<uint8_t> Input, SmallVectorImpl<uint8_t> &Output,
0072                  size_t UncompressedSize);
0073 
0074 } // End of namespace zstd
0075 
0076 enum class Format {
0077   Zlib,
0078   Zstd,
0079 };
0080 
0081 inline Format formatFor(DebugCompressionType Type) {
0082   switch (Type) {
0083   case DebugCompressionType::None:
0084     llvm_unreachable("not a compression type");
0085   case DebugCompressionType::Zlib:
0086     return Format::Zlib;
0087   case DebugCompressionType::Zstd:
0088     return Format::Zstd;
0089   }
0090   llvm_unreachable("");
0091 }
0092 
0093 struct Params {
0094   constexpr Params(Format F)
0095       : format(F), level(F == Format::Zlib ? zlib::DefaultCompression
0096                                            : zstd::DefaultCompression) {}
0097   constexpr Params(Format F, int L, bool Ldm = false)
0098       : format(F), level(L), zstdEnableLdm(Ldm) {}
0099   Params(DebugCompressionType Type) : Params(formatFor(Type)) {}
0100 
0101   Format format;
0102   int level;
0103   bool zstdEnableLdm = false; // Enable zstd long distance matching
0104   // This may support multi-threading for zstd in the future. Note that
0105   // different threads may produce different output, so be careful if certain
0106   // output determinism is desired.
0107 };
0108 
0109 // Return nullptr if LLVM was built with support (LLVM_ENABLE_ZLIB,
0110 // LLVM_ENABLE_ZSTD) for the specified compression format; otherwise
0111 // return a string literal describing the reason.
0112 const char *getReasonIfUnsupported(Format F);
0113 
0114 // Compress Input with the specified format P.Format. If Level is -1, use
0115 // *::DefaultCompression for the format.
0116 void compress(Params P, ArrayRef<uint8_t> Input,
0117               SmallVectorImpl<uint8_t> &Output);
0118 
0119 // Decompress Input. The uncompressed size must be available.
0120 Error decompress(DebugCompressionType T, ArrayRef<uint8_t> Input,
0121                  uint8_t *Output, size_t UncompressedSize);
0122 Error decompress(Format F, ArrayRef<uint8_t> Input,
0123                  SmallVectorImpl<uint8_t> &Output, size_t UncompressedSize);
0124 Error decompress(DebugCompressionType T, ArrayRef<uint8_t> Input,
0125                  SmallVectorImpl<uint8_t> &Output, size_t UncompressedSize);
0126 
0127 } // End of namespace compression
0128 
0129 } // End of namespace llvm
0130 
0131 #endif