| Rui Ueyama | 49560c7 | 2015-06-24 20:40:03 +0000 | [diff] [blame] | 1 | //===- 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 Ueyama | 5b93aa5 | 2015-09-11 04:29:03 +0000 | [diff] [blame] | 10 | // 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 Ueyama | 9cb2870 | 2015-09-16 03:26:31 +0000 | [diff] [blame] | 17 | // identical subgraphs as possible, if we consider sections as vertices and | 
| Rui Ueyama | 5b93aa5 | 2015-09-11 04:29:03 +0000 | [diff] [blame] | 18 | // 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 Ueyama | c48f78c | 2015-09-15 00:35:41 +0000 | [diff] [blame] | 20 | // sections can make other sections, whose relocations now point to the same | 
| Rui Ueyama | 5b93aa5 | 2015-09-11 04:29:03 +0000 | [diff] [blame] | 21 | // 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 Ueyama | 9cb2870 | 2015-09-16 03:26:31 +0000 | [diff] [blame] | 24 | // What we do in this file is this. We split sections into groups. Sections | 
|  | 25 | // in the same group are considered identical. | 
| Rui Ueyama | 5b93aa5 | 2015-09-11 04:29:03 +0000 | [diff] [blame] | 26 | // | 
| Rui Ueyama | 9cb2870 | 2015-09-16 03:26:31 +0000 | [diff] [blame] | 27 | // First, all sections are grouped by their "constant" values. Constant | 
| Rui Ueyama | 92298d5 | 2015-09-16 14:19:10 +0000 | [diff] [blame^] | 28 | // values are values that are never changed by ICF, such as section contents, | 
| Rui Ueyama | 9cb2870 | 2015-09-16 03:26:31 +0000 | [diff] [blame] | 29 | // section name, number of relocations, type and offset of each relocation, | 
| Rui Ueyama | 92298d5 | 2015-09-16 14:19:10 +0000 | [diff] [blame^] | 30 | // 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 Ueyama | 9cb2870 | 2015-09-16 03:26:31 +0000 | [diff] [blame] | 33 | // | 
|  | 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 Ueyama | 49560c7 | 2015-06-24 20:40:03 +0000 | [diff] [blame] | 43 | // | 
|  | 44 | //===----------------------------------------------------------------------===// | 
|  | 45 |  | 
|  | 46 | #include "Chunks.h" | 
| Rui Ueyama | 7b276e2 | 2015-07-30 22:57:21 +0000 | [diff] [blame] | 47 | #include "Symbols.h" | 
|  | 48 | #include "llvm/ADT/Hashing.h" | 
| Rui Ueyama | 5b93aa5 | 2015-09-11 04:29:03 +0000 | [diff] [blame] | 49 | #include "llvm/Support/Debug.h" | 
|  | 50 | #include "llvm/Support/raw_ostream.h" | 
|  | 51 | #include <algorithm> | 
| Rui Ueyama | 49560c7 | 2015-06-24 20:40:03 +0000 | [diff] [blame] | 52 | #include <vector> | 
|  | 53 |  | 
| Rui Ueyama | 7b276e2 | 2015-07-30 22:57:21 +0000 | [diff] [blame] | 54 | using namespace llvm; | 
|  | 55 |  | 
| Rui Ueyama | 49560c7 | 2015-06-24 20:40:03 +0000 | [diff] [blame] | 56 | namespace lld { | 
|  | 57 | namespace coff { | 
| Rui Ueyama | 5b93aa5 | 2015-09-11 04:29:03 +0000 | [diff] [blame] | 58 |  | 
| Rui Ueyama | 92298d5 | 2015-09-16 14:19:10 +0000 | [diff] [blame^] | 59 | typedef std::vector<SectionChunk *>::iterator ChunkIterator; | 
|  | 60 | typedef bool (*Comparator)(const SectionChunk *, const SectionChunk *); | 
|  | 61 |  | 
|  | 62 | class ICF { | 
|  | 63 | public: | 
|  | 64 | ICF(const std::vector<Chunk *> &V) : Chunks(V) {} | 
|  | 65 | void run(); | 
|  | 66 |  | 
|  | 67 | private: | 
|  | 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. | 
|  | 79 | void doICF(const std::vector<Chunk *> &Chunks) { | 
|  | 80 | if (Config->Verbose) | 
|  | 81 | llvm::outs() << "\nICF\n"; | 
|  | 82 | ICF(Chunks).run(); | 
| Rui Ueyama | 5b93aa5 | 2015-09-11 04:29:03 +0000 | [diff] [blame] | 83 | } | 
|  | 84 |  | 
| Rui Ueyama | 92298d5 | 2015-09-16 14:19:10 +0000 | [diff] [blame^] | 85 | uint64_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 Ueyama | 9cb2870 | 2015-09-16 03:26:31 +0000 | [diff] [blame] | 92 | } | 
| Rui Ueyama | 5b93aa5 | 2015-09-11 04:29:03 +0000 | [diff] [blame] | 93 |  | 
| Rui Ueyama | 92298d5 | 2015-09-16 14:19:10 +0000 | [diff] [blame^] | 94 | bool ICF::equalsConstant(const SectionChunk *A, const SectionChunk *B) { | 
| Rui Ueyama | 9cb2870 | 2015-09-16 03:26:31 +0000 | [diff] [blame] | 95 | 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 Ueyama | 7b276e2 | 2015-07-30 22:57:21 +0000 | [diff] [blame] | 102 | return false; | 
| Rui Ueyama | 9cb2870 | 2015-09-16 03:26:31 +0000 | [diff] [blame] | 103 | } | 
| Rui Ueyama | 7b276e2 | 2015-07-30 22:57:21 +0000 | [diff] [blame] | 104 |  | 
| Rui Ueyama | 9cb2870 | 2015-09-16 03:26:31 +0000 | [diff] [blame] | 105 | for (size_t I = 0, E = A->AssocChildren.size(); I != E; ++I) | 
|  | 106 | if (A->AssocChildren[I]->GroupID != B->AssocChildren[I]->GroupID) | 
| Rui Ueyama | 7b276e2 | 2015-07-30 22:57:21 +0000 | [diff] [blame] | 107 | return false; | 
|  | 108 |  | 
|  | 109 | // Compare relocations | 
|  | 110 | auto Eq = [&](const coff_relocation &R1, const coff_relocation &R2) { | 
| Rui Ueyama | 9cb2870 | 2015-09-16 03:26:31 +0000 | [diff] [blame] | 111 | if (R1.Type != R2.Type || | 
|  | 112 | R1.VirtualAddress != R2.VirtualAddress) { | 
| Rui Ueyama | 7b276e2 | 2015-07-30 22:57:21 +0000 | [diff] [blame] | 113 | return false; | 
| Rui Ueyama | 9cb2870 | 2015-09-16 03:26:31 +0000 | [diff] [blame] | 114 | } | 
|  | 115 | SymbolBody *B1 = A->File->getSymbolBody(R1.SymbolTableIndex)->repl(); | 
|  | 116 | SymbolBody *B2 = B->File->getSymbolBody(R2.SymbolTableIndex)->repl(); | 
| Rui Ueyama | 7b276e2 | 2015-07-30 22:57:21 +0000 | [diff] [blame] | 117 | if (B1 == B2) | 
|  | 118 | return true; | 
|  | 119 | auto *D1 = dyn_cast<DefinedRegular>(B1); | 
|  | 120 | auto *D2 = dyn_cast<DefinedRegular>(B2); | 
| Rui Ueyama | 9cb2870 | 2015-09-16 03:26:31 +0000 | [diff] [blame] | 121 | return D1 && D2 && | 
|  | 122 | D1->getValue() == D2->getValue() && | 
|  | 123 | D1->getChunk()->GroupID == D2->getChunk()->GroupID; | 
| Rui Ueyama | 7b276e2 | 2015-07-30 22:57:21 +0000 | [diff] [blame] | 124 | }; | 
| Rui Ueyama | 9cb2870 | 2015-09-16 03:26:31 +0000 | [diff] [blame] | 125 | return std::equal(A->Relocs.begin(), A->Relocs.end(), B->Relocs.begin(), Eq); | 
|  | 126 | } | 
|  | 127 |  | 
| Rui Ueyama | 92298d5 | 2015-09-16 14:19:10 +0000 | [diff] [blame^] | 128 | bool ICF::equalsVariable(const SectionChunk *A, const SectionChunk *B) { | 
| Rui Ueyama | 9cb2870 | 2015-09-16 03:26:31 +0000 | [diff] [blame] | 129 | 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 Ueyama | 92298d5 | 2015-09-16 14:19:10 +0000 | [diff] [blame^] | 137 | bool ICF::partition(ChunkIterator Begin, ChunkIterator End, Comparator Eq) { | 
| Rui Ueyama | 9cb2870 | 2015-09-16 03:26:31 +0000 | [diff] [blame] | 138 | 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 Ueyama | 92298d5 | 2015-09-16 14:19:10 +0000 | [diff] [blame^] | 153 | bool ICF::forEachGroup(std::vector<SectionChunk *> &SChunks, Comparator Eq) { | 
| Rui Ueyama | 9cb2870 | 2015-09-16 03:26:31 +0000 | [diff] [blame] | 154 | 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 Ueyama | 7b276e2 | 2015-07-30 22:57:21 +0000 | [diff] [blame] | 165 | } | 
|  | 166 |  | 
| Rui Ueyama | 49560c7 | 2015-06-24 20:40:03 +0000 | [diff] [blame] | 167 | // Merge identical COMDAT sections. | 
| Rui Ueyama | ef907ec | 2015-09-04 21:35:54 +0000 | [diff] [blame] | 168 | // Two sections are considered the same if their section headers, | 
| Rui Ueyama | 49560c7 | 2015-06-24 20:40:03 +0000 | [diff] [blame] | 169 | // contents and relocations are all the same. | 
| Rui Ueyama | 92298d5 | 2015-09-16 14:19:10 +0000 | [diff] [blame^] | 170 | void ICF::run() { | 
|  | 171 | // Collect only mergeable sections and group by hash value. | 
| Rui Ueyama | ef907ec | 2015-09-04 21:35:54 +0000 | [diff] [blame] | 172 | std::vector<SectionChunk *> SChunks; | 
| Rui Ueyama | 9cb2870 | 2015-09-16 03:26:31 +0000 | [diff] [blame] | 173 | 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 Ueyama | ef907ec | 2015-09-04 21:35:54 +0000 | [diff] [blame] | 177 | SChunks.push_back(SC); | 
| Rui Ueyama | 92298d5 | 2015-09-16 14:19:10 +0000 | [diff] [blame^] | 178 | SC->GroupID = getHash(SC) | (uint64_t(1) << 63); | 
| Rui Ueyama | 9cb2870 | 2015-09-16 03:26:31 +0000 | [diff] [blame] | 179 | } else { | 
|  | 180 | SC->GroupID = NextID++; | 
|  | 181 | } | 
| Rui Ueyama | 5b93aa5 | 2015-09-11 04:29:03 +0000 | [diff] [blame] | 182 | } | 
| Rui Ueyama | 9cb2870 | 2015-09-16 03:26:31 +0000 | [diff] [blame] | 183 | } | 
|  | 184 |  | 
| Rui Ueyama | 92298d5 | 2015-09-16 14:19:10 +0000 | [diff] [blame^] | 185 | // From now on, sections in SChunks are ordered so that sections in | 
|  | 186 | // the same group are consecutive in the vector. | 
| Rui Ueyama | 9cb2870 | 2015-09-16 03:26:31 +0000 | [diff] [blame] | 187 | std::sort(SChunks.begin(), SChunks.end(), | 
|  | 188 | [](SectionChunk *A, SectionChunk *B) { | 
|  | 189 | return A->GroupID < B->GroupID; | 
|  | 190 | }); | 
|  | 191 |  | 
| Rui Ueyama | 92298d5 | 2015-09-16 14:19:10 +0000 | [diff] [blame^] | 192 | // 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 Ueyama | 9cb2870 | 2015-09-16 03:26:31 +0000 | [diff] [blame] | 202 | // Split groups until we get a convergence. | 
| Rui Ueyama | 92298d5 | 2015-09-16 14:19:10 +0000 | [diff] [blame^] | 203 | forEachGroup(SChunks, equalsConstant); | 
|  | 204 | while (forEachGroup(SChunks, equalsVariable)); | 
| Rui Ueyama | 9cb2870 | 2015-09-16 03:26:31 +0000 | [diff] [blame] | 205 |  | 
|  | 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 Ueyama | ef907ec | 2015-09-04 21:35:54 +0000 | [diff] [blame] | 224 | } | 
| Rui Ueyama | 49560c7 | 2015-06-24 20:40:03 +0000 | [diff] [blame] | 225 | } | 
|  | 226 |  | 
|  | 227 | } // namespace coff | 
|  | 228 | } // namespace lld |