blob: c481cb2314946606cb538de2eaa9c6450d3adba6 [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 Ueyama5b93aa52015-09-11 04:29:03 +000010// Identical COMDAT Folding is a feature to merge COMDAT sections not by
11// name (which is regular COMDAT handling) but by contents. If two COMDAT
12// sections 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
Rui Ueyama9cb28702015-09-16 03:26:31 +000017// identical subgraphs as possible, if we consider sections as vertices and
Rui Ueyama5b93aa52015-09-11 04:29:03 +000018// relocations as edges. This may be a bit more complicated problem than you
19// might think. The order of processing sections matters since merging two
Rui Ueyamac48f78c2015-09-15 00:35:41 +000020// sections can make other sections, whose relocations now point to the same
Rui Ueyama5b93aa52015-09-11 04:29:03 +000021// section, mergeable. Graphs may contain cycles, which is common in COFF.
22// We need a sophisticated algorithm to do this properly and efficiently.
23//
Rui Ueyama9cb28702015-09-16 03:26:31 +000024// What we do in this file is this. We split sections into groups. Sections
25// in the same group are considered identical.
Rui Ueyama5b93aa52015-09-11 04:29:03 +000026//
Rui Ueyama9cb28702015-09-16 03:26:31 +000027// First, all sections are grouped by their "constant" values. Constant
Rui Ueyama92298d52015-09-16 14:19:10 +000028// values are values that are never changed by ICF, such as section contents,
Rui Ueyama9cb28702015-09-16 03:26:31 +000029// section name, number of relocations, type and offset of each relocation,
Rui Ueyama92298d52015-09-16 14:19:10 +000030// etc. Because we do not care about some relocation targets in this step,
31// two sections in the same group may not be identical, but at least two
32// sections in different groups can never be identical.
Rui Ueyama9cb28702015-09-16 03:26:31 +000033//
34// Then, we try to split each group by relocation targets. Relocations are
35// considered identical if and only if the relocation targets are in the
36// same group. Splitting a group may make more groups to be splittable,
37// because two relocations that were previously considered identical might
38// now point to different groups. We repeat this step until the convergence
39// is obtained.
40//
41// This algorithm is so-called "optimistic" algorithm described in
42// http://research.google.com/pubs/pub36912.html.
Rui Ueyama49560c72015-06-24 20:40:03 +000043//
44//===----------------------------------------------------------------------===//
45
46#include "Chunks.h"
Rui Ueyama7b276e22015-07-30 22:57:21 +000047#include "Symbols.h"
48#include "llvm/ADT/Hashing.h"
Rui Ueyama5b93aa52015-09-11 04:29:03 +000049#include "llvm/Support/Debug.h"
50#include "llvm/Support/raw_ostream.h"
51#include <algorithm>
Rui Ueyama49560c72015-06-24 20:40:03 +000052#include <vector>
53
Rui Ueyama7b276e22015-07-30 22:57:21 +000054using namespace llvm;
55
Rui Ueyama49560c72015-06-24 20:40:03 +000056namespace lld {
57namespace coff {
Rui Ueyama5b93aa52015-09-11 04:29:03 +000058
Rui Ueyama92298d52015-09-16 14:19:10 +000059typedef std::vector<SectionChunk *>::iterator ChunkIterator;
60typedef bool (*Comparator)(const SectionChunk *, const SectionChunk *);
61
62class ICF {
63public:
64 ICF(const std::vector<Chunk *> &V) : Chunks(V) {}
65 void run();
66
67private:
68 static uint64_t getHash(SectionChunk *C);
69 static bool equalsConstant(const SectionChunk *A, const SectionChunk *B);
70 static bool equalsVariable(const SectionChunk *A, const SectionChunk *B);
71 bool forEachGroup(std::vector<SectionChunk *> &SChunks, Comparator Eq);
72 bool partition(ChunkIterator Begin, ChunkIterator End, Comparator Eq);
73
74 const std::vector<Chunk *> &Chunks;
75 uint64_t NextID = 0;
76};
77
78// Entry point to ICF.
79void doICF(const std::vector<Chunk *> &Chunks) {
80 if (Config->Verbose)
81 llvm::outs() << "\nICF\n";
82 ICF(Chunks).run();
Rui Ueyama5b93aa52015-09-11 04:29:03 +000083}
84
Rui Ueyama92298d52015-09-16 14:19:10 +000085uint64_t ICF::getHash(SectionChunk *C) {
86 return hash_combine(C->getPermissions(),
87 hash_value(C->SectionName),
88 C->NumRelocs,
89 uint32_t(C->Header->SizeOfRawData),
90 std::distance(C->Relocs.end(), C->Relocs.begin()),
91 C->Checksum);
Rui Ueyama9cb28702015-09-16 03:26:31 +000092}
Rui Ueyama5b93aa52015-09-11 04:29:03 +000093
Rui Ueyama92298d52015-09-16 14:19:10 +000094bool ICF::equalsConstant(const SectionChunk *A, const SectionChunk *B) {
Rui Ueyama9cb28702015-09-16 03:26:31 +000095 if (A->getPermissions() != B->getPermissions() ||
96 A->SectionName != B->SectionName ||
97 A->Header->SizeOfRawData != B->Header->SizeOfRawData ||
98 A->NumRelocs != B->NumRelocs ||
99 A->Checksum != B->Checksum ||
100 A->AssocChildren.size() != B->AssocChildren.size() ||
101 A->getContents() != B->getContents()) {
Rui Ueyama7b276e22015-07-30 22:57:21 +0000102 return false;
Rui Ueyama9cb28702015-09-16 03:26:31 +0000103 }
Rui Ueyama7b276e22015-07-30 22:57:21 +0000104
Rui Ueyama9cb28702015-09-16 03:26:31 +0000105 for (size_t I = 0, E = A->AssocChildren.size(); I != E; ++I)
106 if (A->AssocChildren[I]->GroupID != B->AssocChildren[I]->GroupID)
Rui Ueyama7b276e22015-07-30 22:57:21 +0000107 return false;
108
109 // Compare relocations
110 auto Eq = [&](const coff_relocation &R1, const coff_relocation &R2) {
Rui Ueyama9cb28702015-09-16 03:26:31 +0000111 if (R1.Type != R2.Type ||
112 R1.VirtualAddress != R2.VirtualAddress) {
Rui Ueyama7b276e22015-07-30 22:57:21 +0000113 return false;
Rui Ueyama9cb28702015-09-16 03:26:31 +0000114 }
115 SymbolBody *B1 = A->File->getSymbolBody(R1.SymbolTableIndex)->repl();
116 SymbolBody *B2 = B->File->getSymbolBody(R2.SymbolTableIndex)->repl();
Rui Ueyama7b276e22015-07-30 22:57:21 +0000117 if (B1 == B2)
118 return true;
119 auto *D1 = dyn_cast<DefinedRegular>(B1);
120 auto *D2 = dyn_cast<DefinedRegular>(B2);
Rui Ueyama9cb28702015-09-16 03:26:31 +0000121 return D1 && D2 &&
122 D1->getValue() == D2->getValue() &&
123 D1->getChunk()->GroupID == D2->getChunk()->GroupID;
Rui Ueyama7b276e22015-07-30 22:57:21 +0000124 };
Rui Ueyama9cb28702015-09-16 03:26:31 +0000125 return std::equal(A->Relocs.begin(), A->Relocs.end(), B->Relocs.begin(), Eq);
126}
127
Rui Ueyama92298d52015-09-16 14:19:10 +0000128bool ICF::equalsVariable(const SectionChunk *A, const SectionChunk *B) {
Rui Ueyama9cb28702015-09-16 03:26:31 +0000129 assert(A->Successors.size() == B->Successors.size());
130 return std::equal(A->Successors.begin(), A->Successors.end(),
131 B->Successors.begin(),
132 [](const SectionChunk *X, const SectionChunk *Y) {
133 return X->GroupID == Y->GroupID;
134 });
135}
136
Rui Ueyama92298d52015-09-16 14:19:10 +0000137bool ICF::partition(ChunkIterator Begin, ChunkIterator End, Comparator Eq) {
Rui Ueyama9cb28702015-09-16 03:26:31 +0000138 bool R = false;
139 for (auto It = Begin;;) {
140 SectionChunk *Head = *It;
141 auto Bound = std::partition(It + 1, End, [&](SectionChunk *SC) {
142 return Eq(Head, SC);
143 });
144 if (Bound == End)
145 return R;
146 size_t ID = NextID++;
147 std::for_each(It, Bound, [&](SectionChunk *SC) { SC->GroupID = ID; });
148 It = Bound;
149 R = true;
150 }
151}
152
Rui Ueyama92298d52015-09-16 14:19:10 +0000153bool ICF::forEachGroup(std::vector<SectionChunk *> &SChunks, Comparator Eq) {
Rui Ueyama9cb28702015-09-16 03:26:31 +0000154 bool R = false;
155 for (auto It = SChunks.begin(), End = SChunks.end(); It != End;) {
156 SectionChunk *Head = *It;
157 auto Bound = std::find_if(It + 1, End, [&](SectionChunk *SC) {
158 return SC->GroupID != Head->GroupID;
159 });
160 if (partition(It, Bound, Eq))
161 R = true;
162 It = Bound;
163 }
164 return R;
Rui Ueyama7b276e22015-07-30 22:57:21 +0000165}
166
Rui Ueyama49560c72015-06-24 20:40:03 +0000167// Merge identical COMDAT sections.
Rui Ueyamaef907ec2015-09-04 21:35:54 +0000168// Two sections are considered the same if their section headers,
Rui Ueyama49560c72015-06-24 20:40:03 +0000169// contents and relocations are all the same.
Rui Ueyama92298d52015-09-16 14:19:10 +0000170void ICF::run() {
171 // Collect only mergeable sections and group by hash value.
Rui Ueyamaef907ec2015-09-04 21:35:54 +0000172 std::vector<SectionChunk *> SChunks;
Rui Ueyama9cb28702015-09-16 03:26:31 +0000173 for (Chunk *C : Chunks) {
174 if (auto *SC = dyn_cast<SectionChunk>(C)) {
175 bool Writable = SC->getPermissions() & llvm::COFF::IMAGE_SCN_MEM_WRITE;
176 if (SC->isCOMDAT() && SC->isLive() && !Writable) {
Rui Ueyamaef907ec2015-09-04 21:35:54 +0000177 SChunks.push_back(SC);
Rui Ueyama92298d52015-09-16 14:19:10 +0000178 SC->GroupID = getHash(SC) | (uint64_t(1) << 63);
Rui Ueyama9cb28702015-09-16 03:26:31 +0000179 } else {
180 SC->GroupID = NextID++;
181 }
Rui Ueyama5b93aa52015-09-11 04:29:03 +0000182 }
Rui Ueyama9cb28702015-09-16 03:26:31 +0000183 }
184
Rui Ueyama92298d52015-09-16 14:19:10 +0000185 // From now on, sections in SChunks are ordered so that sections in
186 // the same group are consecutive in the vector.
Rui Ueyama9cb28702015-09-16 03:26:31 +0000187 std::sort(SChunks.begin(), SChunks.end(),
188 [](SectionChunk *A, SectionChunk *B) {
189 return A->GroupID < B->GroupID;
190 });
191
Rui Ueyama92298d52015-09-16 14:19:10 +0000192 // Initialize chunk successors for equalsVariable.
193 for (SectionChunk *SC : SChunks) {
194 SC->Successors = SC->AssocChildren;
195 for (const coff_relocation &R : SC->Relocs) {
196 SymbolBody *B = SC->File->getSymbolBody(R.SymbolTableIndex)->repl();
197 if (auto D = dyn_cast<DefinedRegular>(B))
198 SC->Successors.push_back(D->getChunk());
199 }
200 }
201
Rui Ueyama9cb28702015-09-16 03:26:31 +0000202 // Split groups until we get a convergence.
Rui Ueyama92298d52015-09-16 14:19:10 +0000203 forEachGroup(SChunks, equalsConstant);
204 while (forEachGroup(SChunks, equalsVariable));
Rui Ueyama9cb28702015-09-16 03:26:31 +0000205
206 // Merge sections in the same group.
207 for (auto It = SChunks.begin(), End = SChunks.end(); It != End;) {
208 SectionChunk *Head = *It;
209 auto Bound = std::find_if(It + 1, End, [&](SectionChunk *SC) {
210 return Head->GroupID != SC->GroupID;
211 });
212 if (std::distance(It, Bound) == 1) {
213 It = Bound;
214 continue;
215 }
216 if (Config->Verbose)
217 llvm::outs() << "Selected " << Head->getDebugName() << "\n";
218 for (++It; It != Bound; ++It) {
219 SectionChunk *SC = *It;
220 if (Config->Verbose)
221 llvm::outs() << " Removed " << SC->getDebugName() << "\n";
222 SC->replaceWith(Head);
223 }
Rui Ueyamaef907ec2015-09-04 21:35:54 +0000224 }
Rui Ueyama49560c72015-06-24 20:40:03 +0000225}
226
227} // namespace coff
228} // namespace lld