lto-compress.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /* LTO IL compression streams.
  2. Copyright (C) 2009-2015 Free Software Foundation, Inc.
  3. Contributed by Simon Baldwin <simonb@google.com>
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it
  6. under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3, or (at your option)
  8. any later version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT
  10. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  12. License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. #include "config.h"
  17. #include "system.h"
  18. /* zlib.h includes other system headers. Those headers may test feature
  19. test macros. config.h may define feature test macros. For this reason,
  20. zlib.h needs to be included after, rather than before, config.h and
  21. system.h. */
  22. #include <zlib.h>
  23. #include "coretypes.h"
  24. #include "hash-set.h"
  25. #include "machmode.h"
  26. #include "vec.h"
  27. #include "double-int.h"
  28. #include "input.h"
  29. #include "alias.h"
  30. #include "symtab.h"
  31. #include "options.h"
  32. #include "wide-int.h"
  33. #include "inchash.h"
  34. #include "tree.h"
  35. #include "fold-const.h"
  36. #include "predict.h"
  37. #include "tm.h"
  38. #include "hard-reg-set.h"
  39. #include "input.h"
  40. #include "function.h"
  41. #include "basic-block.h"
  42. #include "tree-ssa-alias.h"
  43. #include "internal-fn.h"
  44. #include "gimple-expr.h"
  45. #include "is-a.h"
  46. #include "gimple.h"
  47. #include "diagnostic-core.h"
  48. #include "langhooks.h"
  49. #include "hash-map.h"
  50. #include "plugin-api.h"
  51. #include "ipa-ref.h"
  52. #include "cgraph.h"
  53. #include "lto-streamer.h"
  54. #include "lto-compress.h"
  55. /* Compression stream structure, holds the flush callback and opaque token,
  56. the buffered data, and a note of whether compressing or uncompressing. */
  57. struct lto_compression_stream
  58. {
  59. void (*callback) (const char *, unsigned, void *);
  60. void *opaque;
  61. char *buffer;
  62. size_t bytes;
  63. size_t allocation;
  64. bool is_compression;
  65. };
  66. /* Overall compression constants for zlib. */
  67. static const size_t Z_BUFFER_LENGTH = 4096;
  68. static const size_t MIN_STREAM_ALLOCATION = 1024;
  69. /* For zlib, allocate SIZE count of ITEMS and return the address, OPAQUE
  70. is unused. */
  71. static void *
  72. lto_zalloc (void *opaque, unsigned items, unsigned size)
  73. {
  74. gcc_assert (opaque == Z_NULL);
  75. return xmalloc (items * size);
  76. }
  77. /* For zlib, free memory at ADDRESS, OPAQUE is unused. */
  78. static void
  79. lto_zfree (void *opaque, void *address)
  80. {
  81. gcc_assert (opaque == Z_NULL);
  82. free (address);
  83. }
  84. /* Return a zlib compression level that zlib will not reject. Normalizes
  85. the compression level from the command line flag, clamping non-default
  86. values to the appropriate end of their valid range. */
  87. static int
  88. lto_normalized_zlib_level (void)
  89. {
  90. int level = flag_lto_compression_level;
  91. if (level != Z_DEFAULT_COMPRESSION)
  92. {
  93. if (level < Z_NO_COMPRESSION)
  94. level = Z_NO_COMPRESSION;
  95. else if (level > Z_BEST_COMPRESSION)
  96. level = Z_BEST_COMPRESSION;
  97. }
  98. return level;
  99. }
  100. /* Create a new compression stream, with CALLBACK flush function passed
  101. OPAQUE token, IS_COMPRESSION indicates if compressing or uncompressing. */
  102. static struct lto_compression_stream *
  103. lto_new_compression_stream (void (*callback) (const char *, unsigned, void *),
  104. void *opaque, bool is_compression)
  105. {
  106. struct lto_compression_stream *stream
  107. = (struct lto_compression_stream *) xmalloc (sizeof (*stream));
  108. memset (stream, 0, sizeof (*stream));
  109. stream->callback = callback;
  110. stream->opaque = opaque;
  111. stream->is_compression = is_compression;
  112. return stream;
  113. }
  114. /* Append NUM_CHARS from address BASE to STREAM. */
  115. static void
  116. lto_append_to_compression_stream (struct lto_compression_stream *stream,
  117. const char *base, size_t num_chars)
  118. {
  119. size_t required = stream->bytes + num_chars;
  120. if (stream->allocation < required)
  121. {
  122. if (stream->allocation == 0)
  123. stream->allocation = MIN_STREAM_ALLOCATION;
  124. while (stream->allocation < required)
  125. stream->allocation *= 2;
  126. stream->buffer = (char *) xrealloc (stream->buffer, stream->allocation);
  127. }
  128. memcpy (stream->buffer + stream->bytes, base, num_chars);
  129. stream->bytes += num_chars;
  130. }
  131. /* Free the buffer and memory associated with STREAM. */
  132. static void
  133. lto_destroy_compression_stream (struct lto_compression_stream *stream)
  134. {
  135. free (stream->buffer);
  136. free (stream);
  137. }
  138. /* Return a new compression stream, with CALLBACK flush function passed
  139. OPAQUE token. */
  140. struct lto_compression_stream *
  141. lto_start_compression (void (*callback) (const char *, unsigned, void *),
  142. void *opaque)
  143. {
  144. return lto_new_compression_stream (callback, opaque, true);
  145. }
  146. /* Append NUM_CHARS from address BASE to STREAM. */
  147. void
  148. lto_compress_block (struct lto_compression_stream *stream,
  149. const char *base, size_t num_chars)
  150. {
  151. gcc_assert (stream->is_compression);
  152. lto_append_to_compression_stream (stream, base, num_chars);
  153. lto_stats.num_output_il_bytes += num_chars;
  154. }
  155. /* Finalize STREAM compression, and free stream allocations. */
  156. void
  157. lto_end_compression (struct lto_compression_stream *stream)
  158. {
  159. unsigned char *cursor = (unsigned char *) stream->buffer;
  160. size_t remaining = stream->bytes;
  161. const size_t outbuf_length = Z_BUFFER_LENGTH;
  162. unsigned char *outbuf = (unsigned char *) xmalloc (outbuf_length);
  163. z_stream out_stream;
  164. size_t compressed_bytes = 0;
  165. int status;
  166. gcc_assert (stream->is_compression);
  167. out_stream.next_out = outbuf;
  168. out_stream.avail_out = outbuf_length;
  169. out_stream.next_in = cursor;
  170. out_stream.avail_in = remaining;
  171. out_stream.zalloc = lto_zalloc;
  172. out_stream.zfree = lto_zfree;
  173. out_stream.opaque = Z_NULL;
  174. status = deflateInit (&out_stream, lto_normalized_zlib_level ());
  175. if (status != Z_OK)
  176. internal_error ("compressed stream: %s", zError (status));
  177. do
  178. {
  179. size_t in_bytes, out_bytes;
  180. status = deflate (&out_stream, Z_FINISH);
  181. if (status != Z_OK && status != Z_STREAM_END)
  182. internal_error ("compressed stream: %s", zError (status));
  183. in_bytes = remaining - out_stream.avail_in;
  184. out_bytes = outbuf_length - out_stream.avail_out;
  185. stream->callback ((const char *) outbuf, out_bytes, stream->opaque);
  186. lto_stats.num_compressed_il_bytes += out_bytes;
  187. compressed_bytes += out_bytes;
  188. cursor += in_bytes;
  189. remaining -= in_bytes;
  190. out_stream.next_out = outbuf;
  191. out_stream.avail_out = outbuf_length;
  192. out_stream.next_in = cursor;
  193. out_stream.avail_in = remaining;
  194. }
  195. while (status != Z_STREAM_END);
  196. status = deflateEnd (&out_stream);
  197. if (status != Z_OK)
  198. internal_error ("compressed stream: %s", zError (status));
  199. lto_destroy_compression_stream (stream);
  200. free (outbuf);
  201. }
  202. /* Return a new uncompression stream, with CALLBACK flush function passed
  203. OPAQUE token. */
  204. struct lto_compression_stream *
  205. lto_start_uncompression (void (*callback) (const char *, unsigned, void *),
  206. void *opaque)
  207. {
  208. return lto_new_compression_stream (callback, opaque, false);
  209. }
  210. /* Append NUM_CHARS from address BASE to STREAM. */
  211. void
  212. lto_uncompress_block (struct lto_compression_stream *stream,
  213. const char *base, size_t num_chars)
  214. {
  215. gcc_assert (!stream->is_compression);
  216. lto_append_to_compression_stream (stream, base, num_chars);
  217. lto_stats.num_input_il_bytes += num_chars;
  218. }
  219. /* Finalize STREAM uncompression, and free stream allocations.
  220. Because of the way LTO IL streams are compressed, there may be several
  221. concatenated compressed segments in the accumulated data, so for this
  222. function we iterate decompressions until no data remains. */
  223. void
  224. lto_end_uncompression (struct lto_compression_stream *stream)
  225. {
  226. unsigned char *cursor = (unsigned char *) stream->buffer;
  227. size_t remaining = stream->bytes;
  228. const size_t outbuf_length = Z_BUFFER_LENGTH;
  229. unsigned char *outbuf = (unsigned char *) xmalloc (outbuf_length);
  230. size_t uncompressed_bytes = 0;
  231. gcc_assert (!stream->is_compression);
  232. while (remaining > 0)
  233. {
  234. z_stream in_stream;
  235. size_t out_bytes;
  236. int status;
  237. in_stream.next_out = outbuf;
  238. in_stream.avail_out = outbuf_length;
  239. in_stream.next_in = cursor;
  240. in_stream.avail_in = remaining;
  241. in_stream.zalloc = lto_zalloc;
  242. in_stream.zfree = lto_zfree;
  243. in_stream.opaque = Z_NULL;
  244. status = inflateInit (&in_stream);
  245. if (status != Z_OK)
  246. internal_error ("compressed stream: %s", zError (status));
  247. do
  248. {
  249. size_t in_bytes;
  250. status = inflate (&in_stream, Z_SYNC_FLUSH);
  251. if (status != Z_OK && status != Z_STREAM_END)
  252. internal_error ("compressed stream: %s", zError (status));
  253. in_bytes = remaining - in_stream.avail_in;
  254. out_bytes = outbuf_length - in_stream.avail_out;
  255. stream->callback ((const char *) outbuf, out_bytes, stream->opaque);
  256. lto_stats.num_uncompressed_il_bytes += out_bytes;
  257. uncompressed_bytes += out_bytes;
  258. cursor += in_bytes;
  259. remaining -= in_bytes;
  260. in_stream.next_out = outbuf;
  261. in_stream.avail_out = outbuf_length;
  262. in_stream.next_in = cursor;
  263. in_stream.avail_in = remaining;
  264. }
  265. while (!(status == Z_STREAM_END && out_bytes == 0));
  266. status = inflateEnd (&in_stream);
  267. if (status != Z_OK)
  268. internal_error ("compressed stream: %s", zError (status));
  269. }
  270. lto_destroy_compression_stream (stream);
  271. free (outbuf);
  272. }