blob: 77f1c9ab96f34f9c212ab02113776f5d8d96fb70 [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 Ueyama0b289522016-02-25 18:43:51 +000074template <class ELFT> class ICF {
Rui Ueyama0b289522016-02-25 18:43:51 +000075public:
Rui Ueyama4f8d21f2016-05-02 19:30:42 +000076 void run();
Rui Ueyama0b289522016-02-25 18:43:51 +000077
78private:
79 uint64_t NextId = 1;
80
Rui Ueyamabd1f0632016-11-20 02:39:59 +000081 using Comparator = std::function<bool(const InputSection<ELFT> *,
82 const InputSection<ELFT> *)>;
Rui Ueyama0b289522016-02-25 18:43:51 +000083
Rui Ueyamae2dfbc12016-11-19 23:14:23 +000084 void segregate(MutableArrayRef<InputSection<ELFT> *> Arr, Comparator Eq);
Rui Ueyama0b289522016-02-25 18:43:51 +000085
Rui Ueyamae2dfbc12016-11-19 23:14:23 +000086 void
87 forEachGroup(std::vector<InputSection<ELFT> *> &V,
88 std::function<void(MutableArrayRef<InputSection<ELFT> *>)> Fn);
Rui Ueyama0b289522016-02-25 18:43:51 +000089};
90}
Rui Ueyama0b289522016-02-25 18:43:51 +000091
Rui Ueyama0b289522016-02-25 18:43:51 +000092// Returns a hash value for S. Note that the information about
93// relocation targets is not included in the hash value.
Rui Ueyamabd1f0632016-11-20 02:39:59 +000094template <class ELFT> static uint64_t getHash(InputSection<ELFT> *S) {
Rui Ueyamaa05134e2016-11-19 20:15:55 +000095 return hash_combine(S->Flags, S->getSize(), S->NumRelocations);
Rui Ueyama0b289522016-02-25 18:43:51 +000096}
97
Rui Ueyamabd1f0632016-11-20 02:39:59 +000098// Returns true if section S is subject of ICF.
99template <class ELFT> static bool isEligible(InputSection<ELFT> *S) {
Rui Ueyama0b289522016-02-25 18:43:51 +0000100 // .init and .fini contains instructions that must be executed to
101 // initialize and finalize the process. They cannot and should not
102 // be merged.
Rui Ueyamabd1f0632016-11-20 02:39:59 +0000103 return S->Live && (S->Flags & SHF_ALLOC) && !(S->Flags & SHF_WRITE) &&
104 S->Name != ".init" && S->Name != ".fini";
Rui Ueyama0b289522016-02-25 18:43:51 +0000105}
106
Rui Ueyamabd1f0632016-11-20 02:39:59 +0000107template <class ELFT> static std::vector<InputSection<ELFT> *> getSections() {
Rui Ueyama0b289522016-02-25 18:43:51 +0000108 std::vector<InputSection<ELFT> *> V;
Rui Ueyamabd1f0632016-11-20 02:39:59 +0000109 for (InputSectionBase<ELFT> *Sec : Symtab<ELFT>::X->Sections)
110 if (auto *S = dyn_cast<InputSection<ELFT>>(Sec))
111 if (isEligible(S))
112 V.push_back(S);
Rui Ueyama0b289522016-02-25 18:43:51 +0000113 return V;
114}
115
Rui Ueyama0b289522016-02-25 18:43:51 +0000116// All sections between Begin and End must have the same group ID before
117// you call this function. This function compare sections between Begin
118// and End using Eq and assign new group IDs for new groups.
119template <class ELFT>
Rui Ueyamae2dfbc12016-11-19 23:14:23 +0000120void ICF<ELFT>::segregate(MutableArrayRef<InputSection<ELFT> *> Arr,
Rui Ueyama0b289522016-02-25 18:43:51 +0000121 Comparator Eq) {
122 // This loop rearranges [Begin, End) so that all sections that are
123 // equal in terms of Eq are contiguous. The algorithm is quadratic in
124 // the worst case, but that is not an issue in practice because the
125 // number of distinct sections in [Begin, End) is usually very small.
Rui Ueyamae2dfbc12016-11-19 23:14:23 +0000126 InputSection<ELFT> **I = Arr.begin();
Rui Ueyama0b289522016-02-25 18:43:51 +0000127 for (;;) {
128 InputSection<ELFT> *Head = *I;
129 auto Bound = std::stable_partition(
Rui Ueyamae2dfbc12016-11-19 23:14:23 +0000130 I + 1, Arr.end(), [&](InputSection<ELFT> *S) { return Eq(Head, S); });
131 if (Bound == Arr.end())
Rui Ueyama0b289522016-02-25 18:43:51 +0000132 return;
133 uint64_t Id = NextId++;
134 for (; I != Bound; ++I)
135 (*I)->GroupId = Id;
136 }
137}
138
139template <class ELFT>
Rui Ueyamae2dfbc12016-11-19 23:14:23 +0000140void ICF<ELFT>::forEachGroup(
141 std::vector<InputSection<ELFT> *> &V,
142 std::function<void(MutableArrayRef<InputSection<ELFT> *>)> Fn) {
Rui Ueyamaeec23eb2016-02-26 15:13:24 +0000143 for (InputSection<ELFT> **I = V.data(), **E = I + V.size(); I != E;) {
Rui Ueyama0b289522016-02-25 18:43:51 +0000144 InputSection<ELFT> *Head = *I;
145 auto Bound = std::find_if(I + 1, E, [&](InputSection<ELFT> *S) {
146 return S->GroupId != Head->GroupId;
147 });
Rui Ueyamae2dfbc12016-11-19 23:14:23 +0000148 Fn({I, Bound});
Rui Ueyama0b289522016-02-25 18:43:51 +0000149 I = Bound;
150 }
151}
152
153// Compare two lists of relocations.
Rui Ueyamabd1f0632016-11-20 02:39:59 +0000154template <class ELFT, class RelTy>
155static bool relocationEq(ArrayRef<RelTy> RelsA, ArrayRef<RelTy> RelsB) {
Rui Ueyamaa05134e2016-11-19 20:15:55 +0000156 auto Eq = [](const RelTy &A, const RelTy &B) {
157 return A.r_offset == B.r_offset &&
158 A.getType(Config->Mips64EL) == B.getType(Config->Mips64EL) &&
159 getAddend<ELFT>(A) == getAddend<ELFT>(B);
160 };
161
162 return RelsA.size() == RelsB.size() &&
163 std::equal(RelsA.begin(), RelsA.end(), RelsB.begin(), Eq);
Rui Ueyama0b289522016-02-25 18:43:51 +0000164}
165
166// Compare "non-moving" part of two InputSections, namely everything
167// except relocation targets.
168template <class ELFT>
Rui Ueyamabd1f0632016-11-20 02:39:59 +0000169static bool equalsConstant(const InputSection<ELFT> *A,
170 const InputSection<ELFT> *B) {
171 if (A->NumRelocations != B->NumRelocations || A->Flags != B->Flags ||
172 A->getSize() != B->getSize() || A->Data != B->Data)
Rui Ueyama0b289522016-02-25 18:43:51 +0000173 return false;
174
Rui Ueyamabd1f0632016-11-20 02:39:59 +0000175 if (A->AreRelocsRela)
176 return relocationEq<ELFT>(A->relas(), B->relas());
177 return relocationEq<ELFT>(A->rels(), B->rels());
Rui Ueyama0b289522016-02-25 18:43:51 +0000178}
179
Rui Ueyamabd1f0632016-11-20 02:39:59 +0000180template <class ELFT, class RelTy>
181static bool variableEq(const InputSection<ELFT> *A, ArrayRef<RelTy> RelsA,
182 const InputSection<ELFT> *B, ArrayRef<RelTy> RelsB) {
Rui Ueyamaa05134e2016-11-19 20:15:55 +0000183 auto Eq = [&](const RelTy &RA, const RelTy &RB) {
Rui Ueyamabd1f0632016-11-20 02:39:59 +0000184 SymbolBody &SA = A->getFile()->getRelocTargetSym(RA);
185 SymbolBody &SB = B->getFile()->getRelocTargetSym(RB);
Rafael Espindola67d72c02016-03-11 12:06:30 +0000186 if (&SA == &SB)
Rui Ueyamaa05134e2016-11-19 20:15:55 +0000187 return true;
Rui Ueyama0b289522016-02-25 18:43:51 +0000188
189 // Or, the symbols should be pointing to the same section
190 // in terms of the group ID.
Rafael Espindola67d72c02016-03-11 12:06:30 +0000191 auto *DA = dyn_cast<DefinedRegular<ELFT>>(&SA);
192 auto *DB = dyn_cast<DefinedRegular<ELFT>>(&SB);
Rui Ueyama0b289522016-02-25 18:43:51 +0000193 if (!DA || !DB)
194 return false;
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000195 if (DA->Value != DB->Value)
Rui Ueyama0b289522016-02-25 18:43:51 +0000196 return false;
197 InputSection<ELFT> *X = dyn_cast<InputSection<ELFT>>(DA->Section);
198 InputSection<ELFT> *Y = dyn_cast<InputSection<ELFT>>(DB->Section);
Rui Ueyamaa05134e2016-11-19 20:15:55 +0000199 return X && Y && X->GroupId && X->GroupId == Y->GroupId;
200 };
201
202 return std::equal(RelsA.begin(), RelsA.end(), RelsB.begin(), Eq);
Rui Ueyama0b289522016-02-25 18:43:51 +0000203}
204
205// Compare "moving" part of two InputSections, namely relocation targets.
206template <class ELFT>
Rui Ueyamabd1f0632016-11-20 02:39:59 +0000207static bool equalsVariable(const InputSection<ELFT> *A,
208 const InputSection<ELFT> *B) {
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000209 if (A->AreRelocsRela)
Rui Ueyamaa05134e2016-11-19 20:15:55 +0000210 return variableEq(A, A->relas(), B, B->relas());
211 return variableEq(A, A->rels(), B, B->rels());
Rui Ueyama0b289522016-02-25 18:43:51 +0000212}
213
214// The main function of ICF.
Rui Ueyama4f8d21f2016-05-02 19:30:42 +0000215template <class ELFT> void ICF<ELFT>::run() {
Rui Ueyama0b289522016-02-25 18:43:51 +0000216 // Initially, we use hash values as section group IDs. Therefore,
217 // if two sections have the same ID, they are likely (but not
218 // guaranteed) to have the same static contents in terms of ICF.
Rui Ueyamabd1f0632016-11-20 02:39:59 +0000219 std::vector<InputSection<ELFT> *> Sections = getSections<ELFT>();
Rui Ueyamae2dfbc12016-11-19 23:14:23 +0000220 for (InputSection<ELFT> *S : Sections)
Rui Ueyama0b289522016-02-25 18:43:51 +0000221 // Set MSB on to avoid collisions with serial group IDs
222 S->GroupId = getHash(S) | (uint64_t(1) << 63);
223
224 // From now on, sections in V are ordered so that sections in
225 // the same group are consecutive in the vector.
Rui Ueyamae2dfbc12016-11-19 23:14:23 +0000226 std::stable_sort(Sections.begin(), Sections.end(),
Rui Ueyama0b289522016-02-25 18:43:51 +0000227 [](InputSection<ELFT> *A, InputSection<ELFT> *B) {
Petr Hosek901948a2016-08-22 18:53:09 +0000228 if (A->GroupId != B->GroupId)
229 return A->GroupId < B->GroupId;
230 // Within a group, put the highest alignment
231 // requirement first, so that's the one we'll keep.
232 return B->Alignment < A->Alignment;
Rui Ueyama0b289522016-02-25 18:43:51 +0000233 });
234
235 // Compare static contents and assign unique IDs for each static content.
Rui Ueyamae2dfbc12016-11-19 23:14:23 +0000236 forEachGroup(Sections, [&](MutableArrayRef<InputSection<ELFT> *> V) {
Rui Ueyamabd1f0632016-11-20 02:39:59 +0000237 segregate(V, equalsConstant<ELFT>);
Rui Ueyamae2dfbc12016-11-19 23:14:23 +0000238 });
Rui Ueyama0b289522016-02-25 18:43:51 +0000239
240 // Split groups by comparing relocations until we get a convergence.
241 int Cnt = 1;
242 for (;;) {
243 ++Cnt;
244 uint64_t Id = NextId;
Rui Ueyamae2dfbc12016-11-19 23:14:23 +0000245 forEachGroup(Sections, [&](MutableArrayRef<InputSection<ELFT> *> V) {
Rui Ueyamabd1f0632016-11-20 02:39:59 +0000246 segregate(V, equalsVariable<ELFT>);
Rui Ueyamae2dfbc12016-11-19 23:14:23 +0000247 });
Rui Ueyama0b289522016-02-25 18:43:51 +0000248 if (Id == NextId)
249 break;
250 }
Rui Ueyama10bd2832016-02-25 18:56:01 +0000251 log("ICF needed " + Twine(Cnt) + " iterations.");
Rui Ueyama0b289522016-02-25 18:43:51 +0000252
253 // Merge sections in the same group.
Rui Ueyamabd1f0632016-11-20 02:39:59 +0000254 forEachGroup(Sections, [](MutableArrayRef<InputSection<ELFT> *> V) {
255 log("selected " + V[0]->Name);
Rui Ueyamae2dfbc12016-11-19 23:14:23 +0000256 for (InputSection<ELFT> *S : V.slice(1)) {
Rafael Espindola042a3f22016-09-08 14:06:08 +0000257 log(" removed " + S->Name);
Rui Ueyamabd1f0632016-11-20 02:39:59 +0000258 V[0]->replace(S);
Rui Ueyama0b289522016-02-25 18:43:51 +0000259 }
Rui Ueyamae2dfbc12016-11-19 23:14:23 +0000260 });
Rui Ueyama0b289522016-02-25 18:43:51 +0000261}
262
263// ICF entry point function.
Rui Ueyama4f8d21f2016-05-02 19:30:42 +0000264template <class ELFT> void elf::doIcf() { ICF<ELFT>().run(); }
Rui Ueyama0b289522016-02-25 18:43:51 +0000265
Rui Ueyama4f8d21f2016-05-02 19:30:42 +0000266template void elf::doIcf<ELF32LE>();
267template void elf::doIcf<ELF32BE>();
268template void elf::doIcf<ELF64LE>();
269template void elf::doIcf<ELF64BE>();