PR tree-optimization/66718
[official-gcc.git] / gcc / lto-compress.c
blobd979b78b7581b414bd76423cb6ee8ac0125513c7
1 /* LTO IL compression streams.
3 Copyright (C) 2009-2015 Free Software Foundation, Inc.
4 Contributed by Simon Baldwin <simonb@google.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
16 License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 /* zlib.h includes other system headers. Those headers may test feature
25 test macros. config.h may define feature test macros. For this reason,
26 zlib.h needs to be included after, rather than before, config.h and
27 system.h. */
28 #include <zlib.h>
29 #include "coretypes.h"
30 #include "alias.h"
31 #include "symtab.h"
32 #include "options.h"
33 #include "tree.h"
34 #include "fold-const.h"
35 #include "predict.h"
36 #include "tm.h"
37 #include "hard-reg-set.h"
38 #include "function.h"
39 #include "basic-block.h"
40 #include "tree-ssa-alias.h"
41 #include "internal-fn.h"
42 #include "gimple-expr.h"
43 #include "gimple.h"
44 #include "diagnostic-core.h"
45 #include "langhooks.h"
46 #include "cgraph.h"
47 #include "lto-streamer.h"
48 #include "lto-compress.h"
50 /* Compression stream structure, holds the flush callback and opaque token,
51 the buffered data, and a note of whether compressing or uncompressing. */
53 struct lto_compression_stream
55 void (*callback) (const char *, unsigned, void *);
56 void *opaque;
57 char *buffer;
58 size_t bytes;
59 size_t allocation;
60 bool is_compression;
63 /* Overall compression constants for zlib. */
65 static const size_t Z_BUFFER_LENGTH = 4096;
66 static const size_t MIN_STREAM_ALLOCATION = 1024;
68 /* For zlib, allocate SIZE count of ITEMS and return the address, OPAQUE
69 is unused. */
71 static void *
72 lto_zalloc (void *opaque, unsigned items, unsigned size)
74 gcc_assert (opaque == Z_NULL);
75 return xmalloc (items * size);
78 /* For zlib, free memory at ADDRESS, OPAQUE is unused. */
80 static void
81 lto_zfree (void *opaque, void *address)
83 gcc_assert (opaque == Z_NULL);
84 free (address);
87 /* Return a zlib compression level that zlib will not reject. Normalizes
88 the compression level from the command line flag, clamping non-default
89 values to the appropriate end of their valid range. */
91 static int
92 lto_normalized_zlib_level (void)
94 int level = flag_lto_compression_level;
96 if (level != Z_DEFAULT_COMPRESSION)
98 if (level < Z_NO_COMPRESSION)
99 level = Z_NO_COMPRESSION;
100 else if (level > Z_BEST_COMPRESSION)
101 level = Z_BEST_COMPRESSION;
104 return level;
107 /* Create a new compression stream, with CALLBACK flush function passed
108 OPAQUE token, IS_COMPRESSION indicates if compressing or uncompressing. */
110 static struct lto_compression_stream *
111 lto_new_compression_stream (void (*callback) (const char *, unsigned, void *),
112 void *opaque, bool is_compression)
114 struct lto_compression_stream *stream
115 = (struct lto_compression_stream *) xmalloc (sizeof (*stream));
117 memset (stream, 0, sizeof (*stream));
118 stream->callback = callback;
119 stream->opaque = opaque;
120 stream->is_compression = is_compression;
122 return stream;
125 /* Append NUM_CHARS from address BASE to STREAM. */
127 static void
128 lto_append_to_compression_stream (struct lto_compression_stream *stream,
129 const char *base, size_t num_chars)
131 size_t required = stream->bytes + num_chars;
133 if (stream->allocation < required)
135 if (stream->allocation == 0)
136 stream->allocation = MIN_STREAM_ALLOCATION;
137 while (stream->allocation < required)
138 stream->allocation *= 2;
140 stream->buffer = (char *) xrealloc (stream->buffer, stream->allocation);
143 memcpy (stream->buffer + stream->bytes, base, num_chars);
144 stream->bytes += num_chars;
147 /* Free the buffer and memory associated with STREAM. */
149 static void
150 lto_destroy_compression_stream (struct lto_compression_stream *stream)
152 free (stream->buffer);
153 free (stream);
156 /* Return a new compression stream, with CALLBACK flush function passed
157 OPAQUE token. */
159 struct lto_compression_stream *
160 lto_start_compression (void (*callback) (const char *, unsigned, void *),
161 void *opaque)
163 return lto_new_compression_stream (callback, opaque, true);
166 /* Append NUM_CHARS from address BASE to STREAM. */
168 void
169 lto_compress_block (struct lto_compression_stream *stream,
170 const char *base, size_t num_chars)
172 gcc_assert (stream->is_compression);
174 lto_append_to_compression_stream (stream, base, num_chars);
175 lto_stats.num_output_il_bytes += num_chars;
178 /* Finalize STREAM compression, and free stream allocations. */
180 void
181 lto_end_compression (struct lto_compression_stream *stream)
183 unsigned char *cursor = (unsigned char *) stream->buffer;
184 size_t remaining = stream->bytes;
185 const size_t outbuf_length = Z_BUFFER_LENGTH;
186 unsigned char *outbuf = (unsigned char *) xmalloc (outbuf_length);
187 z_stream out_stream;
188 size_t compressed_bytes = 0;
189 int status;
191 gcc_assert (stream->is_compression);
193 out_stream.next_out = outbuf;
194 out_stream.avail_out = outbuf_length;
195 out_stream.next_in = cursor;
196 out_stream.avail_in = remaining;
197 out_stream.zalloc = lto_zalloc;
198 out_stream.zfree = lto_zfree;
199 out_stream.opaque = Z_NULL;
201 status = deflateInit (&out_stream, lto_normalized_zlib_level ());
202 if (status != Z_OK)
203 internal_error ("compressed stream: %s", zError (status));
207 size_t in_bytes, out_bytes;
209 status = deflate (&out_stream, Z_FINISH);
210 if (status != Z_OK && status != Z_STREAM_END)
211 internal_error ("compressed stream: %s", zError (status));
213 in_bytes = remaining - out_stream.avail_in;
214 out_bytes = outbuf_length - out_stream.avail_out;
216 stream->callback ((const char *) outbuf, out_bytes, stream->opaque);
217 lto_stats.num_compressed_il_bytes += out_bytes;
218 compressed_bytes += out_bytes;
220 cursor += in_bytes;
221 remaining -= in_bytes;
223 out_stream.next_out = outbuf;
224 out_stream.avail_out = outbuf_length;
225 out_stream.next_in = cursor;
226 out_stream.avail_in = remaining;
228 while (status != Z_STREAM_END);
230 status = deflateEnd (&out_stream);
231 if (status != Z_OK)
232 internal_error ("compressed stream: %s", zError (status));
234 lto_destroy_compression_stream (stream);
235 free (outbuf);
238 /* Return a new uncompression stream, with CALLBACK flush function passed
239 OPAQUE token. */
241 struct lto_compression_stream *
242 lto_start_uncompression (void (*callback) (const char *, unsigned, void *),
243 void *opaque)
245 return lto_new_compression_stream (callback, opaque, false);
248 /* Append NUM_CHARS from address BASE to STREAM. */
250 void
251 lto_uncompress_block (struct lto_compression_stream *stream,
252 const char *base, size_t num_chars)
254 gcc_assert (!stream->is_compression);
256 lto_append_to_compression_stream (stream, base, num_chars);
257 lto_stats.num_input_il_bytes += num_chars;
260 /* Finalize STREAM uncompression, and free stream allocations.
262 Because of the way LTO IL streams are compressed, there may be several
263 concatenated compressed segments in the accumulated data, so for this
264 function we iterate decompressions until no data remains. */
266 void
267 lto_end_uncompression (struct lto_compression_stream *stream)
269 unsigned char *cursor = (unsigned char *) stream->buffer;
270 size_t remaining = stream->bytes;
271 const size_t outbuf_length = Z_BUFFER_LENGTH;
272 unsigned char *outbuf = (unsigned char *) xmalloc (outbuf_length);
273 size_t uncompressed_bytes = 0;
275 gcc_assert (!stream->is_compression);
277 while (remaining > 0)
279 z_stream in_stream;
280 size_t out_bytes;
281 int status;
283 in_stream.next_out = outbuf;
284 in_stream.avail_out = outbuf_length;
285 in_stream.next_in = cursor;
286 in_stream.avail_in = remaining;
287 in_stream.zalloc = lto_zalloc;
288 in_stream.zfree = lto_zfree;
289 in_stream.opaque = Z_NULL;
291 status = inflateInit (&in_stream);
292 if (status != Z_OK)
293 internal_error ("compressed stream: %s", zError (status));
297 size_t in_bytes;
299 status = inflate (&in_stream, Z_SYNC_FLUSH);
300 if (status != Z_OK && status != Z_STREAM_END)
301 internal_error ("compressed stream: %s", zError (status));
303 in_bytes = remaining - in_stream.avail_in;
304 out_bytes = outbuf_length - in_stream.avail_out;
306 stream->callback ((const char *) outbuf, out_bytes, stream->opaque);
307 lto_stats.num_uncompressed_il_bytes += out_bytes;
308 uncompressed_bytes += out_bytes;
310 cursor += in_bytes;
311 remaining -= in_bytes;
313 in_stream.next_out = outbuf;
314 in_stream.avail_out = outbuf_length;
315 in_stream.next_in = cursor;
316 in_stream.avail_in = remaining;
318 while (!(status == Z_STREAM_END && out_bytes == 0));
320 status = inflateEnd (&in_stream);
321 if (status != Z_OK)
322 internal_error ("compressed stream: %s", zError (status));
325 lto_destroy_compression_stream (stream);
326 free (outbuf);