blob: 4968fd1a9078821681383220fa2eacac61dff0d4 [file] [log] [blame]
Rui Ueyama0b289522016-02-25 18:43:51 +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//
10// Identical Code Folding is a feature to merge sections not by name (which
11// is regular comdat handling) but by contents. If two non-writable sections
12// have the same data, relocations, attributes, etc., then the two
13// are considered identical and merged by the linker. This optimization
14// makes outputs smaller.
15//
16// ICF is theoretically a problem of reducing graphs by merging as many
17// identical subgraphs as possible if we consider sections as vertices and
18// relocations as edges. It may sound simple, but it is a bit more
19// complicated than you might think. The order of processing sections
20// matters because merging two sections can make other sections, whose
21// relocations now point to the same section, mergeable. Graphs may contain
22// cycles. We need a sophisticated algorithm to do this properly and
23// efficiently.
24//
25// What we do in this file is this. We split sections into groups. Sections
26// in the same group are considered identical.
27//
28// We begin by optimistically putting all sections into a single equivalence
29// class. Then we apply a series of checks that split this initial
30// equivalence class into more and more refined equivalence classes based on
31// the properties by which a section can be distinguished.
32//
33// We begin by checking that the section contents and flags are the
34// same. This only needs to be done once since these properties don't depend
35// on the current equivalence class assignment.
36//
37// Then we split the equivalence classes based on checking that their
38// relocations are the same, where relocation targets are compared by their
39// equivalence class, not the concrete section. This may need to be done
40// multiple times because as the equivalence classes are refined, two
41// sections that had a relocation target in the same equivalence class may
42// now target different equivalence classes, and hence these two sections
43// must be put in different equivalence classes (whereas in the previous
44// iteration they were not since the relocation target was the same.)
45//
46// Our algorithm is smart enough to merge the following mutually-recursive
47// functions.
48//
49// void foo() { bar(); }
50// void bar() { foo(); }
51//
52// This algorithm is so-called "optimistic" algorithm described in
53// http://research.google.com/pubs/pub36912.html. (Note that what GNU
54// gold implemented is different from the optimistic algorithm.)
55//
56//===----------------------------------------------------------------------===//
57
58#include "ICF.h"
59#include "Config.h"
Rui Ueyama0b289522016-02-25 18:43:51 +000060#include "SymbolTable.h"
61
62#include "llvm/ADT/Hashing.h"
63#include "llvm/Object/ELF.h"
64#include "llvm/Support/ELF.h"
Rui Ueyamaa05134e2016-11-19 20:15:55 +000065#include <algorithm>
Rui Ueyama0b289522016-02-25 18:43:51 +000066
67using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:54 +000068using namespace lld::elf;
Rui Ueyama0b289522016-02-25 18:43:51 +000069using namespace llvm;
70using namespace llvm::ELF;
71using namespace llvm::object;
72
Rui Ueyamabd1f0632016-11-20 02:39:59 +000073namespace {
Rui Ueyama9dedfb12016-11-30 01:50:03 +000074struct Range {
75 size_t Begin;
76 size_t End;
77};
78
Rui Ueyama0b289522016-02-25 18:43:51 +000079template <class ELFT> class ICF {
Rui Ueyama0b289522016-02-25 18:43:51 +000080public:
Rui Ueyama4f8d21f2016-05-02 19:30:42 +000081 void run();
Rui Ueyama0b289522016-02-25 18:43:51 +000082
83private:
Rui Ueyama9dedfb12016-11-30 01:50:03 +000084 void segregate(Range *R, bool Constant);
Rui Ueyama0b289522016-02-25 18:43:51 +000085
Rui Ueyama9dedfb12016-11-30 01:50:03 +000086 template <class RelTy>
87 bool constantEq(ArrayRef<RelTy> RelsA, ArrayRef<RelTy> RelsB);
Rui Ueyama0b289522016-02-25 18:43:51 +000088
Rui Ueyama9dedfb12016-11-30 01:50:03 +000089 template <class RelTy>
90 bool variableEq(const InputSection<ELFT> *A, ArrayRef<RelTy> RelsA,
91 const InputSection<ELFT> *B, ArrayRef<RelTy> RelsB);
Rui Ueyama0b289522016-02-25 18:43:51 +000092
Rui Ueyama9dedfb12016-11-30 01:50:03 +000093 bool equalsConstant(const InputSection<ELFT> *A, const InputSection<ELFT> *B);
94 bool equalsVariable(const InputSection<ELFT> *A, const InputSection<ELFT> *B);
95
96 std::vector<InputSection<ELFT> *> Sections;
97 std::vector<Range> Ranges;
98
99 // The main loop is repeated until we get a convergence.
100 bool Repeat = false; // If Repeat is true, we need to repeat.
101 int Cnt = 0; // Counter for the main loop.
Rui Ueyama0b289522016-02-25 18:43:51 +0000102};
103}
Rui Ueyama0b289522016-02-25 18:43:51 +0000104
Rui Ueyama0b289522016-02-25 18:43:51 +0000105// Returns a hash value for S. Note that the information about
106// relocation targets is not included in the hash value.
Rui Ueyamabd1f0632016-11-20 02:39:59 +0000107template <class ELFT> static uint64_t getHash(InputSection<ELFT> *S) {
Rui Ueyamaa05134e2016-11-19 20:15:55 +0000108 return hash_combine(S->Flags, S->getSize(), S->NumRelocations);
Rui Ueyama0b289522016-02-25 18:43:51 +0000109}
110
Rui Ueyamabd1f0632016-11-20 02:39:59 +0000111// Returns true if section S is subject of ICF.
112template <class ELFT> static bool isEligible(InputSection<ELFT> *S) {
Rui Ueyama0b289522016-02-25 18:43:51 +0000113 // .init and .fini contains instructions that must be executed to
114 // initialize and finalize the process. They cannot and should not
115 // be merged.
Rui Ueyamabd1f0632016-11-20 02:39:59 +0000116 return S->Live && (S->Flags & SHF_ALLOC) && !(S->Flags & SHF_WRITE) &&
117 S->Name != ".init" && S->Name != ".fini";
Rui Ueyama0b289522016-02-25 18:43:51 +0000118}
119
Rui Ueyama9dedfb12016-11-30 01:50:03 +0000120// Before calling this function, all sections in range R must have the
121// same group ID.
122template <class ELFT> void ICF<ELFT>::segregate(Range *R, bool Constant) {
123 // This loop rearranges sections in range R so that all sections
124 // that are equal in terms of equals{Constant,Variable} are contiguous
125 // in Sections vector.
126 //
127 // The algorithm is quadratic in the worst case, but that is not an
128 // issue in practice because the number of the distinct sections in
129 // [R.Begin, R.End] is usually very small.
130 while (R->End - R->Begin > 1) {
131 // Divide range R into two. Let Mid be the start index of the
132 // second group.
Rui Ueyama0b289522016-02-25 18:43:51 +0000133 auto Bound = std::stable_partition(
Rui Ueyama9dedfb12016-11-30 01:50:03 +0000134 Sections.begin() + R->Begin + 1, Sections.begin() + R->End,
135 [&](InputSection<ELFT> *S) {
136 if (Constant)
137 return equalsConstant(Sections[R->Begin], S);
138 return equalsVariable(Sections[R->Begin], S);
139 });
140 size_t Mid = Bound - Sections.begin();
Rui Ueyama0b289522016-02-25 18:43:51 +0000141
Rui Ueyama9dedfb12016-11-30 01:50:03 +0000142 if (Mid == R->End)
143 return;
144
145 // Now we split [R.Begin, R.End) into [R.Begin, Mid) and [Mid, R.End).
146 if (Mid - R->Begin > 1)
147 Ranges.push_back({R->Begin, Mid});
148 R->Begin = Mid;
149
150 // Update GroupIds for the new group members. We use the index of
151 // the group first member as a group ID because that is unique.
152 for (size_t I = Mid; I < R->End; ++I)
153 Sections[I]->GroupId = Mid;
154
155 // Since we have split a group, we need to repeat the main loop
156 // later to obtain a convergence. Remember that.
157 Repeat = true;
Rui Ueyama0b289522016-02-25 18:43:51 +0000158 }
159}
160
161// Compare two lists of relocations.
Rui Ueyama9dedfb12016-11-30 01:50:03 +0000162template <class ELFT>
163template <class RelTy>
164bool ICF<ELFT>::constantEq(ArrayRef<RelTy> RelsA, ArrayRef<RelTy> RelsB) {
Rui Ueyamaa05134e2016-11-19 20:15:55 +0000165 auto Eq = [](const RelTy &A, const RelTy &B) {
166 return A.r_offset == B.r_offset &&
167 A.getType(Config->Mips64EL) == B.getType(Config->Mips64EL) &&
168 getAddend<ELFT>(A) == getAddend<ELFT>(B);
169 };
170
171 return RelsA.size() == RelsB.size() &&
172 std::equal(RelsA.begin(), RelsA.end(), RelsB.begin(), Eq);
Rui Ueyama0b289522016-02-25 18:43:51 +0000173}
174
175// Compare "non-moving" part of two InputSections, namely everything
176// except relocation targets.
177template <class ELFT>
Rui Ueyama9dedfb12016-11-30 01:50:03 +0000178bool ICF<ELFT>::equalsConstant(const InputSection<ELFT> *A,
179 const InputSection<ELFT> *B) {
Rui Ueyamabd1f0632016-11-20 02:39:59 +0000180 if (A->NumRelocations != B->NumRelocations || A->Flags != B->Flags ||
181 A->getSize() != B->getSize() || A->Data != B->Data)
Rui Ueyama0b289522016-02-25 18:43:51 +0000182 return false;
183
Rui Ueyamabd1f0632016-11-20 02:39:59 +0000184 if (A->AreRelocsRela)
Rui Ueyama9dedfb12016-11-30 01:50:03 +0000185 return constantEq(A->relas(), B->relas());
186 return constantEq(A->rels(), B->rels());
Rui Ueyama0b289522016-02-25 18:43:51 +0000187}
188
Rui Ueyama7bed9ee2016-11-20 23:15:54 +0000189// Compare two lists of relocations. Returns true if all pairs of
190// relocations point to the same section in terms of ICF.
Rui Ueyama9dedfb12016-11-30 01:50:03 +0000191template <class ELFT>
192template <class RelTy>
193bool ICF<ELFT>::variableEq(const InputSection<ELFT> *A, ArrayRef<RelTy> RelsA,
194 const InputSection<ELFT> *B, ArrayRef<RelTy> RelsB) {
Rui Ueyamaa05134e2016-11-19 20:15:55 +0000195 auto Eq = [&](const RelTy &RA, const RelTy &RB) {
Rui Ueyamabd1f0632016-11-20 02:39:59 +0000196 SymbolBody &SA = A->getFile()->getRelocTargetSym(RA);
197 SymbolBody &SB = B->getFile()->getRelocTargetSym(RB);
Rafael Espindola67d72c02016-03-11 12:06:30 +0000198 if (&SA == &SB)
Rui Ueyamaa05134e2016-11-19 20:15:55 +0000199 return true;
Rui Ueyama0b289522016-02-25 18:43:51 +0000200
201 // Or, the symbols should be pointing to the same section
202 // in terms of the group ID.
Rafael Espindola67d72c02016-03-11 12:06:30 +0000203 auto *DA = dyn_cast<DefinedRegular<ELFT>>(&SA);
204 auto *DB = dyn_cast<DefinedRegular<ELFT>>(&SB);
Rui Ueyama0b289522016-02-25 18:43:51 +0000205 if (!DA || !DB)
206 return false;
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000207 if (DA->Value != DB->Value)
Rui Ueyama0b289522016-02-25 18:43:51 +0000208 return false;
Rui Ueyama9dedfb12016-11-30 01:50:03 +0000209
Rui Ueyama9f8cb732016-11-20 02:43:44 +0000210 auto *X = dyn_cast<InputSection<ELFT>>(DA->Section);
211 auto *Y = dyn_cast<InputSection<ELFT>>(DB->Section);
Rui Ueyama9dedfb12016-11-30 01:50:03 +0000212 if (!X || !Y)
213 return false;
214 return X->GroupId != 0 && X->GroupId == Y->GroupId;
Rui Ueyamaa05134e2016-11-19 20:15:55 +0000215 };
216
217 return std::equal(RelsA.begin(), RelsA.end(), RelsB.begin(), Eq);
Rui Ueyama0b289522016-02-25 18:43:51 +0000218}
219
220// Compare "moving" part of two InputSections, namely relocation targets.
221template <class ELFT>
Rui Ueyama9dedfb12016-11-30 01:50:03 +0000222bool ICF<ELFT>::equalsVariable(const InputSection<ELFT> *A,
223 const InputSection<ELFT> *B) {
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000224 if (A->AreRelocsRela)
Rui Ueyamaa05134e2016-11-19 20:15:55 +0000225 return variableEq(A, A->relas(), B, B->relas());
226 return variableEq(A, A->rels(), B, B->rels());
Rui Ueyama0b289522016-02-25 18:43:51 +0000227}
228
229// The main function of ICF.
Rui Ueyama4f8d21f2016-05-02 19:30:42 +0000230template <class ELFT> void ICF<ELFT>::run() {
Rui Ueyama9dedfb12016-11-30 01:50:03 +0000231 // Collect sections to merge.
232 for (InputSectionBase<ELFT> *Sec : Symtab<ELFT>::X->Sections)
233 if (auto *S = dyn_cast<InputSection<ELFT>>(Sec))
234 if (isEligible(S))
235 Sections.push_back(S);
236
Rui Ueyama0b289522016-02-25 18:43:51 +0000237 // Initially, we use hash values as section group IDs. Therefore,
238 // if two sections have the same ID, they are likely (but not
239 // guaranteed) to have the same static contents in terms of ICF.
Rui Ueyamae2dfbc12016-11-19 23:14:23 +0000240 for (InputSection<ELFT> *S : Sections)
Rui Ueyama9dedfb12016-11-30 01:50:03 +0000241 // Set MSB to 1 to avoid collisions with non-hash IDs.
Rui Ueyama0b289522016-02-25 18:43:51 +0000242 S->GroupId = getHash(S) | (uint64_t(1) << 63);
243
Rui Ueyama7bed9ee2016-11-20 23:15:54 +0000244 // From now on, sections in Sections are ordered so that sections in
Rui Ueyama0b289522016-02-25 18:43:51 +0000245 // the same group are consecutive in the vector.
Rui Ueyamae2dfbc12016-11-19 23:14:23 +0000246 std::stable_sort(Sections.begin(), Sections.end(),
Rui Ueyama0b289522016-02-25 18:43:51 +0000247 [](InputSection<ELFT> *A, InputSection<ELFT> *B) {
Petr Hosek901948a2016-08-22 18:53:09 +0000248 if (A->GroupId != B->GroupId)
249 return A->GroupId < B->GroupId;
250 // Within a group, put the highest alignment
251 // requirement first, so that's the one we'll keep.
252 return B->Alignment < A->Alignment;
Rui Ueyama0b289522016-02-25 18:43:51 +0000253 });
254
Rui Ueyama9dedfb12016-11-30 01:50:03 +0000255 // Split sections into groups by ID. And then we are going to
256 // split groups into more and more smaller groups.
257 // Note that we do not add single element groups because they
258 // are already the smallest.
259 Ranges.reserve(Sections.size());
260 for (size_t I = 0, E = Sections.size(); I < E - 1;) {
261 // Let J be the first index whose element has a different ID.
262 size_t J = I + 1;
263 while (J < E && Sections[I]->GroupId == Sections[J]->GroupId)
264 ++J;
265 if (J - I > 1)
266 Ranges.push_back({I, J});
267 I = J;
Rui Ueyama0b289522016-02-25 18:43:51 +0000268 }
Rui Ueyama9dedfb12016-11-30 01:50:03 +0000269
270 // Compare static contents and assign unique IDs for each static content.
271 std::for_each(Ranges.begin(), Ranges.end(),
272 [&](Range &R) { segregate(&R, true); });
273 ++Cnt;
274
275 // Split groups by comparing relocations until convergence is obtained.
276 do {
277 Repeat = false;
278 std::for_each(Ranges.begin(), Ranges.end(),
279 [&](Range &R) { segregate(&R, false); });
280 ++Cnt;
281 } while (Repeat);
282
283 log("ICF needed " + Twine(Cnt) + " iterations");
Rui Ueyama0b289522016-02-25 18:43:51 +0000284
285 // Merge sections in the same group.
Rui Ueyama9dedfb12016-11-30 01:50:03 +0000286 for (Range R : Ranges) {
287 if (R.End - R.Begin == 1)
288 continue;
289
290 log("selected " + Sections[R.Begin]->Name);
291 for (size_t I = R.Begin + 1; I < R.End; ++I) {
292 log(" removed " + Sections[I]->Name);
293 Sections[R.Begin]->replace(Sections[I]);
Rui Ueyama0b289522016-02-25 18:43:51 +0000294 }
Rui Ueyama9dedfb12016-11-30 01:50:03 +0000295 }
Rui Ueyama0b289522016-02-25 18:43:51 +0000296}
297
298// ICF entry point function.
Rui Ueyama4f8d21f2016-05-02 19:30:42 +0000299template <class ELFT> void elf::doIcf() { ICF<ELFT>().run(); }
Rui Ueyama0b289522016-02-25 18:43:51 +0000300
Rui Ueyama4f8d21f2016-05-02 19:30:42 +0000301template void elf::doIcf<ELF32LE>();
302template void elf::doIcf<ELF32BE>();
303template void elf::doIcf<ELF64LE>();
304template void elf::doIcf<ELF64BE>();