1 //===- ICF.cpp ------------------------------------------------------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // 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
15 // On Windows, ICF is enabled by default.
17 // See ELF/ICF.cpp for the details about the algortihm.
19 //===----------------------------------------------------------------------===//
24 #include "llvm/ADT/Hashing.h"
25 #include "llvm/Support/Debug.h"
26 #include "llvm/Support/Parallel.h"
27 #include "llvm/Support/raw_ostream.h"
39 void run(const std::vector<Chunk *> &V);
42 void segregate(size_t Begin, size_t End, bool Constant);
44 bool equalsConstant(const SectionChunk *A, const SectionChunk *B);
45 bool equalsVariable(const SectionChunk *A, const SectionChunk *B);
47 uint32_t getHash(SectionChunk *C);
48 bool isEligible(SectionChunk *C);
50 size_t findBoundary(size_t Begin, size_t End);
52 void forEachClassRange(size_t Begin, size_t End,
53 std::function<void(size_t, size_t)> Fn);
55 void forEachClass(std::function<void(size_t, size_t)> Fn);
57 std::vector<SectionChunk *> Chunks;
59 std::atomic<bool> Repeat = {false};
62 // Returns a hash value for S.
63 uint32_t ICF::getHash(SectionChunk *C) {
64 return hash_combine(C->getPermissions(),
65 hash_value(C->SectionName),
68 uint32_t(C->Header->SizeOfRawData),
72 // Returns true if section S is subject of ICF.
74 // Microsoft's documentation
75 // (https://msdn.microsoft.com/en-us/library/bxwfs976.aspx; visited April
76 // 2017) says that /opt:icf folds both functions and read-only data.
77 // Despite that, the MSVC linker folds only functions. We found
78 // a few instances of programs that are not safe for data merging.
79 // Therefore, we merge only functions just like the MSVC tool.
80 bool ICF::isEligible(SectionChunk *C) {
81 bool Global = C->Sym && C->Sym->isExternal();
82 bool Executable = C->getPermissions() & llvm::COFF::IMAGE_SCN_MEM_EXECUTE;
83 bool Writable = C->getPermissions() & llvm::COFF::IMAGE_SCN_MEM_WRITE;
84 return C->isCOMDAT() && C->isLive() && Global && Executable && !Writable;
87 // Split an equivalence class into smaller classes.
88 void ICF::segregate(size_t Begin, size_t End, bool Constant) {
90 // Divide [Begin, End) into two. Let Mid be the start index of the
92 auto Bound = std::stable_partition(
93 Chunks.begin() + Begin + 1, Chunks.begin() + End, [&](SectionChunk *S) {
95 return equalsConstant(Chunks[Begin], S);
96 return equalsVariable(Chunks[Begin], S);
98 size_t Mid = Bound - Chunks.begin();
100 // Split [Begin, End) into [Begin, Mid) and [Mid, End). We use Mid as an
101 // equivalence class ID because every group ends with a unique index.
102 for (size_t I = Begin; I < Mid; ++I)
103 Chunks[I]->Class[(Cnt + 1) % 2] = Mid;
105 // If we created a group, we need to iterate the main loop again.
113 // Compare "non-moving" part of two sections, namely everything
114 // except relocation targets.
115 bool ICF::equalsConstant(const SectionChunk *A, const SectionChunk *B) {
116 if (A->NumRelocs != B->NumRelocs)
119 // Compare relocations.
120 auto Eq = [&](const coff_relocation &R1, const coff_relocation &R2) {
121 if (R1.Type != R2.Type ||
122 R1.VirtualAddress != R2.VirtualAddress) {
125 SymbolBody *B1 = A->File->getSymbolBody(R1.SymbolTableIndex);
126 SymbolBody *B2 = B->File->getSymbolBody(R2.SymbolTableIndex);
129 if (auto *D1 = dyn_cast<DefinedRegular>(B1))
130 if (auto *D2 = dyn_cast<DefinedRegular>(B2))
131 return D1->getValue() == D2->getValue() &&
132 D1->getChunk()->Class[Cnt % 2] == D2->getChunk()->Class[Cnt % 2];
135 if (!std::equal(A->Relocs.begin(), A->Relocs.end(), B->Relocs.begin(), Eq))
138 // Compare section attributes and contents.
139 return A->getPermissions() == B->getPermissions() &&
140 A->SectionName == B->SectionName &&
141 A->getAlign() == B->getAlign() &&
142 A->Header->SizeOfRawData == B->Header->SizeOfRawData &&
143 A->Checksum == B->Checksum &&
144 A->getContents() == B->getContents();
147 // Compare "moving" part of two sections, namely relocation targets.
148 bool ICF::equalsVariable(const SectionChunk *A, const SectionChunk *B) {
149 // Compare relocations.
150 auto Eq = [&](const coff_relocation &R1, const coff_relocation &R2) {
151 SymbolBody *B1 = A->File->getSymbolBody(R1.SymbolTableIndex);
152 SymbolBody *B2 = B->File->getSymbolBody(R2.SymbolTableIndex);
155 if (auto *D1 = dyn_cast<DefinedRegular>(B1))
156 if (auto *D2 = dyn_cast<DefinedRegular>(B2))
157 return D1->getChunk()->Class[Cnt % 2] == D2->getChunk()->Class[Cnt % 2];
160 return std::equal(A->Relocs.begin(), A->Relocs.end(), B->Relocs.begin(), Eq);
163 size_t ICF::findBoundary(size_t Begin, size_t End) {
164 for (size_t I = Begin + 1; I < End; ++I)
165 if (Chunks[Begin]->Class[Cnt % 2] != Chunks[I]->Class[Cnt % 2])
170 void ICF::forEachClassRange(size_t Begin, size_t End,
171 std::function<void(size_t, size_t)> Fn) {
173 Begin = findBoundary(Begin - 1, End);
175 while (Begin < End) {
176 size_t Mid = findBoundary(Begin, Chunks.size());
182 // Call Fn on each class group.
183 void ICF::forEachClass(std::function<void(size_t, size_t)> Fn) {
184 // If the number of sections are too small to use threading,
185 // call Fn sequentially.
186 if (Chunks.size() < 1024) {
187 forEachClassRange(0, Chunks.size(), Fn);
192 // Split sections into 256 shards and call Fn in parallel.
193 size_t NumShards = 256;
194 size_t Step = Chunks.size() / NumShards;
195 for_each_n(parallel::par, size_t(0), NumShards, [&](size_t I) {
196 size_t End = (I == NumShards - 1) ? Chunks.size() : (I + 1) * Step;
197 forEachClassRange(I * Step, End, Fn);
202 // Merge identical COMDAT sections.
203 // Two sections are considered the same if their section headers,
204 // contents and relocations are all the same.
205 void ICF::run(const std::vector<Chunk *> &Vec) {
206 // Collect only mergeable sections and group by hash value.
208 for (Chunk *C : Vec) {
209 if (auto *SC = dyn_cast<SectionChunk>(C)) {
211 Chunks.push_back(SC);
213 SC->Class[0] = NextId++;
217 // Initially, we use hash values to partition sections.
218 for (SectionChunk *SC : Chunks)
219 // Set MSB to 1 to avoid collisions with non-hash classs.
220 SC->Class[0] = getHash(SC) | (1 << 31);
222 // From now on, sections in Chunks are ordered so that sections in
223 // the same group are consecutive in the vector.
224 std::stable_sort(Chunks.begin(), Chunks.end(),
225 [](SectionChunk *A, SectionChunk *B) {
226 return A->Class[0] < B->Class[0];
229 // Compare static contents and assign unique IDs for each static content.
230 forEachClass([&](size_t Begin, size_t End) { segregate(Begin, End, true); });
232 // Split groups by comparing relocations until convergence is obtained.
236 [&](size_t Begin, size_t End) { segregate(Begin, End, false); });
239 log("ICF needed " + Twine(Cnt) + " iterations");
241 // Merge sections in the same classs.
242 forEachClass([&](size_t Begin, size_t End) {
243 if (End - Begin == 1)
246 log("Selected " + Chunks[Begin]->getDebugName());
247 for (size_t I = Begin + 1; I < End; ++I) {
248 log(" Removed " + Chunks[I]->getDebugName());
249 Chunks[Begin]->replace(Chunks[I]);
254 // Entry point to ICF.
255 void doICF(const std::vector<Chunk *> &Chunks) { ICF().run(Chunks); }