blob: 19468c0fac5e9ee7f2b787dcc30f2d2cfde2b0d0 [file] [log] [blame]
Rui Ueyama49560c72015-06-24 20:40:03 +00001//===- ICF.cpp ------------------------------------------------------------===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Rui Ueyama3a618e52016-12-02 08:03:58 +000010// ICF is short for Identical Code Folding. That is a size optimization to
11// identify and merge two or more read-only sections (typically functions)
12// that happened to have the same contents. It usually reduces output size
13// by a few percent.
Rui Ueyama5b93aa52015-09-11 04:29:03 +000014//
Rui Ueyama3a618e52016-12-02 08:03:58 +000015// On Windows, ICF is enabled by default.
Rui Ueyama5b93aa52015-09-11 04:29:03 +000016//
Rui Ueyama3a618e52016-12-02 08:03:58 +000017// See ELF/ICF.cpp for the details about the algortihm.
Rui Ueyama49560c72015-06-24 20:40:03 +000018//
19//===----------------------------------------------------------------------===//
20
21#include "Chunks.h"
Rui Ueyama3a618e52016-12-02 08:03:58 +000022#include "Error.h"
Rui Ueyama7b276e22015-07-30 22:57:21 +000023#include "Symbols.h"
Rui Ueyamaaa95e5a2015-09-18 21:06:34 +000024#include "lld/Core/Parallel.h"
Rui Ueyama7b276e22015-07-30 22:57:21 +000025#include "llvm/ADT/Hashing.h"
Rui Ueyama5b93aa52015-09-11 04:29:03 +000026#include "llvm/Support/Debug.h"
27#include "llvm/Support/raw_ostream.h"
28#include <algorithm>
Rui Ueyamae629a452015-09-18 22:31:15 +000029#include <atomic>
Rui Ueyama49560c72015-06-24 20:40:03 +000030#include <vector>
31
Rui Ueyama7b276e22015-07-30 22:57:21 +000032using namespace llvm;
33
Rui Ueyama49560c72015-06-24 20:40:03 +000034namespace lld {
35namespace coff {
Rui Ueyama5b93aa52015-09-11 04:29:03 +000036
Rui Ueyama92298d52015-09-16 14:19:10 +000037class ICF {
38public:
Rui Ueyamae8d1c592015-09-18 21:17:44 +000039 void run(const std::vector<Chunk *> &V);
Rui Ueyama92298d52015-09-16 14:19:10 +000040
41private:
Rui Ueyama3a618e52016-12-02 08:03:58 +000042 void segregate(size_t Begin, size_t End, bool Constant);
Rui Ueyama92298d52015-09-16 14:19:10 +000043
Rui Ueyama3a618e52016-12-02 08:03:58 +000044 bool equalsConstant(const SectionChunk *A, const SectionChunk *B);
45 bool equalsVariable(const SectionChunk *A, const SectionChunk *B);
46
47 uint32_t getHash(SectionChunk *C);
48 bool isEligible(SectionChunk *C);
49
50 size_t findBoundary(size_t Begin, size_t End);
51
52 void forEachColorRange(size_t Begin, size_t End,
53 std::function<void(size_t, size_t)> Fn);
54
55 void forEachColor(std::function<void(size_t, size_t)> Fn);
56
57 std::vector<SectionChunk *> Chunks;
58 int Cnt = 0;
59 std::atomic<uint32_t> NextId = {1};
60 std::atomic<bool> Repeat = {false};
Rui Ueyama92298d52015-09-16 14:19:10 +000061};
62
Rui Ueyama3a618e52016-12-02 08:03:58 +000063// Returns a hash value for S.
64uint32_t ICF::getHash(SectionChunk *C) {
Rui Ueyama92298d52015-09-16 14:19:10 +000065 return hash_combine(C->getPermissions(),
66 hash_value(C->SectionName),
67 C->NumRelocs,
Rui Ueyama548d22c2015-09-25 16:50:12 +000068 C->getAlign(),
Rui Ueyama92298d52015-09-16 14:19:10 +000069 uint32_t(C->Header->SizeOfRawData),
Rui Ueyama92298d52015-09-16 14:19:10 +000070 C->Checksum);
Rui Ueyama9cb28702015-09-16 03:26:31 +000071}
Rui Ueyama5b93aa52015-09-11 04:29:03 +000072
Rui Ueyama3a618e52016-12-02 08:03:58 +000073// Returns true if section S is subject of ICF.
74bool ICF::isEligible(SectionChunk *C) {
75 bool Global = C->Sym && C->Sym->isExternal();
76 bool Writable = C->getPermissions() & llvm::COFF::IMAGE_SCN_MEM_WRITE;
77 return C->isCOMDAT() && C->isLive() && Global && !Writable;
78}
79
80// Split a range into smaller ranges by recoloring sections
81void ICF::segregate(size_t Begin, size_t End, bool Constant) {
82 while (Begin < End) {
83 // Divide [Begin, End) into two. Let Mid be the start index of the
84 // second group.
85 auto Bound = std::stable_partition(
86 Chunks.begin() + Begin + 1, Chunks.begin() + End, [&](SectionChunk *S) {
87 if (Constant)
88 return equalsConstant(Chunks[Begin], S);
89 return equalsVariable(Chunks[Begin], S);
90 });
91 size_t Mid = Bound - Chunks.begin();
92
93 // Split [Begin, End) into [Begin, Mid) and [Mid, End).
94 uint32_t Id = NextId++;
95 for (size_t I = Begin; I < Mid; ++I)
96 Chunks[I]->Color[(Cnt + 1) % 2] = Id;
97
98 // If we created a group, we need to iterate the main loop again.
99 if (Mid != End)
100 Repeat = true;
101
102 Begin = Mid;
Rui Ueyama9cb28702015-09-16 03:26:31 +0000103 }
Rui Ueyama3a618e52016-12-02 08:03:58 +0000104}
105
106// Compare "non-moving" part of two sections, namely everything
107// except relocation targets.
108bool ICF::equalsConstant(const SectionChunk *A, const SectionChunk *B) {
109 if (A->NumRelocs != B->NumRelocs)
110 return false;
Rui Ueyama7b276e22015-07-30 22:57:21 +0000111
Rui Ueyama7d8263b2015-09-18 01:30:56 +0000112 // Compare relocations.
Rui Ueyama7b276e22015-07-30 22:57:21 +0000113 auto Eq = [&](const coff_relocation &R1, const coff_relocation &R2) {
Rui Ueyama9cb28702015-09-16 03:26:31 +0000114 if (R1.Type != R2.Type ||
115 R1.VirtualAddress != R2.VirtualAddress) {
Rui Ueyama7b276e22015-07-30 22:57:21 +0000116 return false;
Rui Ueyama9cb28702015-09-16 03:26:31 +0000117 }
Peter Collingbourne79a5e6b2016-12-09 21:55:24 +0000118 SymbolBody *B1 = A->File->getSymbolBody(R1.SymbolTableIndex);
119 SymbolBody *B2 = B->File->getSymbolBody(R2.SymbolTableIndex);
Rui Ueyama7b276e22015-07-30 22:57:21 +0000120 if (B1 == B2)
121 return true;
Rui Ueyama603d5112015-09-18 02:40:54 +0000122 if (auto *D1 = dyn_cast<DefinedRegular>(B1))
123 if (auto *D2 = dyn_cast<DefinedRegular>(B2))
124 return D1->getValue() == D2->getValue() &&
Rui Ueyama3a618e52016-12-02 08:03:58 +0000125 D1->getChunk()->Color[Cnt % 2] == D2->getChunk()->Color[Cnt % 2];
Rui Ueyama603d5112015-09-18 02:40:54 +0000126 return false;
Rui Ueyama7b276e22015-07-30 22:57:21 +0000127 };
Rui Ueyama7d8263b2015-09-18 01:30:56 +0000128 if (!std::equal(A->Relocs.begin(), A->Relocs.end(), B->Relocs.begin(), Eq))
129 return false;
130
Rui Ueyama603d5112015-09-18 02:40:54 +0000131 // Compare section attributes and contents.
132 return A->getPermissions() == B->getPermissions() &&
133 A->SectionName == B->SectionName &&
Rui Ueyama548d22c2015-09-25 16:50:12 +0000134 A->getAlign() == B->getAlign() &&
Rui Ueyama603d5112015-09-18 02:40:54 +0000135 A->Header->SizeOfRawData == B->Header->SizeOfRawData &&
136 A->Checksum == B->Checksum &&
137 A->getContents() == B->getContents();
Rui Ueyama9cb28702015-09-16 03:26:31 +0000138}
139
Rui Ueyama3a618e52016-12-02 08:03:58 +0000140// Compare "moving" part of two sections, namely relocation targets.
Rui Ueyama92298d52015-09-16 14:19:10 +0000141bool ICF::equalsVariable(const SectionChunk *A, const SectionChunk *B) {
Rui Ueyamac9a6e822015-09-18 01:51:37 +0000142 // Compare relocations.
143 auto Eq = [&](const coff_relocation &R1, const coff_relocation &R2) {
Peter Collingbourne79a5e6b2016-12-09 21:55:24 +0000144 SymbolBody *B1 = A->File->getSymbolBody(R1.SymbolTableIndex);
145 SymbolBody *B2 = B->File->getSymbolBody(R2.SymbolTableIndex);
Rui Ueyama5f389152015-09-20 20:19:12 +0000146 if (B1 == B2)
147 return true;
148 if (auto *D1 = dyn_cast<DefinedRegular>(B1))
Rui Ueyamae8d1c592015-09-18 21:17:44 +0000149 if (auto *D2 = dyn_cast<DefinedRegular>(B2))
Rui Ueyama3a618e52016-12-02 08:03:58 +0000150 return D1->getChunk()->Color[Cnt % 2] == D2->getChunk()->Color[Cnt % 2];
Rui Ueyamae8d1c592015-09-18 21:17:44 +0000151 return false;
Rui Ueyamac9a6e822015-09-18 01:51:37 +0000152 };
153 return std::equal(A->Relocs.begin(), A->Relocs.end(), B->Relocs.begin(), Eq);
Rui Ueyama9cb28702015-09-16 03:26:31 +0000154}
155
Rui Ueyama3a618e52016-12-02 08:03:58 +0000156size_t ICF::findBoundary(size_t Begin, size_t End) {
157 for (size_t I = Begin + 1; I < End; ++I)
158 if (Chunks[Begin]->Color[Cnt % 2] != Chunks[I]->Color[Cnt % 2])
159 return I;
160 return End;
161}
162
163void ICF::forEachColorRange(size_t Begin, size_t End,
164 std::function<void(size_t, size_t)> Fn) {
165 if (Begin > 0)
166 Begin = findBoundary(Begin - 1, End);
167
168 while (Begin < End) {
169 size_t Mid = findBoundary(Begin, Chunks.size());
170 Fn(Begin, Mid);
171 Begin = Mid;
Rui Ueyama9cb28702015-09-16 03:26:31 +0000172 }
173}
174
Rui Ueyama3a618e52016-12-02 08:03:58 +0000175// Call Fn on each color group.
176void ICF::forEachColor(std::function<void(size_t, size_t)> Fn) {
177 // If the number of sections are too small to use threading,
178 // call Fn sequentially.
179 if (Chunks.size() < 1024) {
180 forEachColorRange(0, Chunks.size(), Fn);
181 return;
Rui Ueyama9cb28702015-09-16 03:26:31 +0000182 }
Rui Ueyama3a618e52016-12-02 08:03:58 +0000183
184 // Split sections into 256 shards and call Fn in parallel.
185 size_t NumShards = 256;
186 size_t Step = Chunks.size() / NumShards;
187 parallel_for(size_t(0), NumShards, [&](size_t I) {
188 forEachColorRange(I * Step, (I + 1) * Step, Fn);
189 });
190 forEachColorRange(Step * NumShards, Chunks.size(), Fn);
Rui Ueyama7b276e22015-07-30 22:57:21 +0000191}
192
Rui Ueyama49560c72015-06-24 20:40:03 +0000193// Merge identical COMDAT sections.
Rui Ueyamaef907ec2015-09-04 21:35:54 +0000194// Two sections are considered the same if their section headers,
Rui Ueyama49560c72015-06-24 20:40:03 +0000195// contents and relocations are all the same.
Rui Ueyamae8d1c592015-09-18 21:17:44 +0000196void ICF::run(const std::vector<Chunk *> &Vec) {
Rui Ueyama92298d52015-09-16 14:19:10 +0000197 // Collect only mergeable sections and group by hash value.
Rui Ueyamae8d1c592015-09-18 21:17:44 +0000198 for (Chunk *C : Vec) {
Rui Ueyama3a618e52016-12-02 08:03:58 +0000199 auto *SC = dyn_cast<SectionChunk>(C);
200 if (!SC)
201 continue;
202
203 if (isEligible(SC)) {
204 // Set MSB to 1 to avoid collisions with non-hash colors.
205 SC->Color[0] = getHash(SC) | (1 << 31);
206 Chunks.push_back(SC);
207 } else {
208 SC->Color[0] = NextId++;
Rui Ueyama5b93aa52015-09-11 04:29:03 +0000209 }
Rui Ueyama9cb28702015-09-16 03:26:31 +0000210 }
211
Rui Ueyama3a618e52016-12-02 08:03:58 +0000212 if (Chunks.empty())
213 return;
214
Rui Ueyamaaa95e5a2015-09-18 21:06:34 +0000215 // From now on, sections in Chunks are ordered so that sections in
Rui Ueyama92298d52015-09-16 14:19:10 +0000216 // the same group are consecutive in the vector.
Rui Ueyama3a618e52016-12-02 08:03:58 +0000217 std::stable_sort(Chunks.begin(), Chunks.end(),
218 [](SectionChunk *A, SectionChunk *B) {
219 return A->Color[0] < B->Color[0];
220 });
Rui Ueyama9cb28702015-09-16 03:26:31 +0000221
Rui Ueyama3a618e52016-12-02 08:03:58 +0000222 // Compare static contents and assign unique IDs for each static content.
223 forEachColor([&](size_t Begin, size_t End) { segregate(Begin, End, true); });
224 ++Cnt;
Rui Ueyamaaa95e5a2015-09-18 21:06:34 +0000225
Rui Ueyama3a618e52016-12-02 08:03:58 +0000226 // Split groups by comparing relocations until convergence is obtained.
227 do {
228 Repeat = false;
229 forEachColor(
230 [&](size_t Begin, size_t End) { segregate(Begin, End, false); });
Rui Ueyama66c06ce2015-09-16 23:55:39 +0000231 ++Cnt;
Rui Ueyama3a618e52016-12-02 08:03:58 +0000232 } while (Repeat);
Rui Ueyama9cb28702015-09-16 03:26:31 +0000233
Rui Ueyamae6e206d2017-02-21 23:22:56 +0000234 log("ICF needed " + Twine(Cnt) + " iterations");
Rui Ueyama3a618e52016-12-02 08:03:58 +0000235
236 // Merge sections in the same colors.
237 forEachColor([&](size_t Begin, size_t End) {
238 if (End - Begin == 1)
239 return;
240
Rui Ueyamae6e206d2017-02-21 23:22:56 +0000241 log("Selected " + Chunks[Begin]->getDebugName());
Rui Ueyama3a618e52016-12-02 08:03:58 +0000242 for (size_t I = Begin + 1; I < End; ++I) {
Rui Ueyamae6e206d2017-02-21 23:22:56 +0000243 log(" Removed " + Chunks[I]->getDebugName());
Rui Ueyama3a618e52016-12-02 08:03:58 +0000244 Chunks[Begin]->replace(Chunks[I]);
Rui Ueyama9cb28702015-09-16 03:26:31 +0000245 }
Rui Ueyama3a618e52016-12-02 08:03:58 +0000246 });
Rui Ueyama49560c72015-06-24 20:40:03 +0000247}
248
Rui Ueyama3a618e52016-12-02 08:03:58 +0000249// Entry point to ICF.
250void doICF(const std::vector<Chunk *> &Chunks) { ICF().run(Chunks); }
251
Rui Ueyama49560c72015-06-24 20:40:03 +0000252} // namespace coff
253} // namespace lld