LLVM 20.0.0git
Compression.h
Go to the documentation of this file.
1//===-- llvm/Support/Compression.h ---Compression----------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains basic functions for compression/decompression.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_SUPPORT_COMPRESSION_H
14#define LLVM_SUPPORT_COMPRESSION_H
15
16#include "llvm/ADT/ArrayRef.h"
18
19namespace llvm {
20template <typename T> class SmallVectorImpl;
21class Error;
22
23// None indicates no compression. The other members are a subset of
24// compression::Format, which is used for compressed debug sections in some
25// object file formats (e.g. ELF). This is a separate class as we may add new
26// compression::Format members for non-debugging purposes.
28 None, ///< No compression
29 Zlib, ///< zlib
30 Zstd, ///< Zstandard
31};
32
33namespace compression {
34namespace zlib {
35
36constexpr int NoCompression = 0;
37constexpr int BestSpeedCompression = 1;
38constexpr int DefaultCompression = 6;
39constexpr int BestSizeCompression = 9;
40
41bool isAvailable();
42
44 SmallVectorImpl<uint8_t> &CompressedBuffer,
45 int Level = DefaultCompression);
46
48 size_t &UncompressedSize);
49
51 size_t UncompressedSize);
52
53} // End of namespace zlib
54
55namespace zstd {
56
57constexpr int NoCompression = -5;
58constexpr int BestSpeedCompression = 1;
59constexpr int DefaultCompression = 5;
60constexpr int BestSizeCompression = 12;
61
62bool isAvailable();
63
65 SmallVectorImpl<uint8_t> &CompressedBuffer,
66 int Level = DefaultCompression, bool EnableLdm = false);
67
69 size_t &UncompressedSize);
70
72 size_t UncompressedSize);
73
74} // End of namespace zstd
75
76enum class Format {
77 Zlib,
78 Zstd,
79};
80
82 switch (Type) {
84 llvm_unreachable("not a compression type");
86 return Format::Zlib;
88 return Format::Zstd;
89 }
91}
92
93struct Params {
94 constexpr Params(Format F)
95 : format(F), level(F == Format::Zlib ? zlib::DefaultCompression
96 : zstd::DefaultCompression) {}
97 constexpr Params(Format F, int L, bool Ldm = false)
98 : format(F), level(L), zstdEnableLdm(Ldm) {}
100
102 int level;
103 bool zstdEnableLdm = false; // Enable zstd long distance matching
104 // This may support multi-threading for zstd in the future. Note that
105 // different threads may produce different output, so be careful if certain
106 // output determinism is desired.
107};
108
109// Return nullptr if LLVM was built with support (LLVM_ENABLE_ZLIB,
110// LLVM_ENABLE_ZSTD) for the specified compression format; otherwise
111// return a string literal describing the reason.
112const char *getReasonIfUnsupported(Format F);
113
114// Compress Input with the specified format P.Format. If Level is -1, use
115// *::DefaultCompression for the format.
118
119// Decompress Input. The uncompressed size must be available.
121 uint8_t *Output, size_t UncompressedSize);
123 SmallVectorImpl<uint8_t> &Output, size_t UncompressedSize);
125 SmallVectorImpl<uint8_t> &Output, size_t UncompressedSize);
126
127} // End of namespace compression
128
129} // End of namespace llvm
130
131#endif
#define F(x, y, z)
Definition: MD5.cpp:55
#define P(N)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:573
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void compress(ArrayRef< uint8_t > Input, SmallVectorImpl< uint8_t > &CompressedBuffer, int Level=DefaultCompression)
Error decompress(ArrayRef< uint8_t > Input, uint8_t *Output, size_t &UncompressedSize)
constexpr int NoCompression
Definition: Compression.h:36
constexpr int BestSizeCompression
Definition: Compression.h:39
constexpr int DefaultCompression
Definition: Compression.h:38
constexpr int BestSpeedCompression
Definition: Compression.h:37
Error decompress(ArrayRef< uint8_t > Input, uint8_t *Output, size_t &UncompressedSize)
constexpr int NoCompression
Definition: Compression.h:57
void compress(ArrayRef< uint8_t > Input, SmallVectorImpl< uint8_t > &CompressedBuffer, int Level=DefaultCompression, bool EnableLdm=false)
constexpr int BestSpeedCompression
Definition: Compression.h:58
constexpr int DefaultCompression
Definition: Compression.h:59
constexpr int BestSizeCompression
Definition: Compression.h:60
const char * getReasonIfUnsupported(Format F)
Definition: Compression.cpp:30
Error decompress(DebugCompressionType T, ArrayRef< uint8_t > Input, uint8_t *Output, size_t UncompressedSize)
Definition: Compression.cpp:58
Format formatFor(DebugCompressionType Type)
Definition: Compression.h:81
void compress(Params P, ArrayRef< uint8_t > Input, SmallVectorImpl< uint8_t > &Output)
Definition: Compression.cpp:46
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ None
Definition: CodeGenData.h:106
DebugCompressionType
Definition: Compression.h:27
Params(DebugCompressionType Type)
Definition: Compression.h:99
constexpr Params(Format F)
Definition: Compression.h:94
constexpr Params(Format F, int L, bool Ldm=false)
Definition: Compression.h:97