Rui Ueyama | 6b5feed | 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 | 46ed29b | 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 | 9be9aa8 | 2015-09-16 03:26:31 +0000 | [diff] [blame] | 17 | // identical subgraphs as possible, if we consider sections as vertices and |
Rui Ueyama | 46ed29b | 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 | ca0e367 | 2015-09-15 00:35:41 +0000 | [diff] [blame] | 20 | // sections can make other sections, whose relocations now point to the same |
Rui Ueyama | 46ed29b | 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 | 9be9aa8 | 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 | 46ed29b | 2015-09-11 04:29:03 +0000 | [diff] [blame] | 26 | // |
Rui Ueyama | 9be9aa8 | 2015-09-16 03:26:31 +0000 | [diff] [blame] | 27 | // First, all sections are grouped by their "constant" values. Constant |
Rui Ueyama | 437e0d2 | 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 | 9be9aa8 | 2015-09-16 03:26:31 +0000 | [diff] [blame] | 29 | // section name, number of relocations, type and offset of each relocation, |
Rui Ueyama | 437e0d2 | 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 | 9be9aa8 | 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 | 6b5feed | 2015-06-24 20:40:03 +0000 | [diff] [blame] | 43 | // |
| 44 | //===----------------------------------------------------------------------===// |
| 45 | |
| 46 | #include "Chunks.h" |
Rui Ueyama | 791416f | 2015-07-30 22:57:21 +0000 | [diff] [blame] | 47 | #include "Symbols.h" |
Rui Ueyama | 708d7fa | 2015-09-18 21:06:34 +0000 | [diff] [blame] | 48 | #include "lld/Core/Parallel.h" |
Rui Ueyama | 791416f | 2015-07-30 22:57:21 +0000 | [diff] [blame] | 49 | #include "llvm/ADT/Hashing.h" |
Rui Ueyama | 46ed29b | 2015-09-11 04:29:03 +0000 | [diff] [blame] | 50 | #include "llvm/Support/Debug.h" |
| 51 | #include "llvm/Support/raw_ostream.h" |
| 52 | #include <algorithm> |
Rui Ueyama | 5197f54 | 2015-09-18 22:31:15 +0000 | [diff] [blame] | 53 | #include <atomic> |
Rui Ueyama | 6b5feed | 2015-06-24 20:40:03 +0000 | [diff] [blame] | 54 | #include <vector> |
| 55 | |
Rui Ueyama | 791416f | 2015-07-30 22:57:21 +0000 | [diff] [blame] | 56 | using namespace llvm; |
| 57 | |
Rui Ueyama | 6b5feed | 2015-06-24 20:40:03 +0000 | [diff] [blame] | 58 | namespace lld { |
| 59 | namespace coff { |
Rui Ueyama | 46ed29b | 2015-09-11 04:29:03 +0000 | [diff] [blame] | 60 | |
Rui Ueyama | 437e0d2 | 2015-09-16 14:19:10 +0000 | [diff] [blame] | 61 | typedef std::vector<SectionChunk *>::iterator ChunkIterator; |
| 62 | typedef bool (*Comparator)(const SectionChunk *, const SectionChunk *); |
| 63 | |
| 64 | class ICF { |
| 65 | public: |
Rui Ueyama | 0fff21f | 2015-09-18 21:17:44 +0000 | [diff] [blame] | 66 | void run(const std::vector<Chunk *> &V); |
Rui Ueyama | 437e0d2 | 2015-09-16 14:19:10 +0000 | [diff] [blame] | 67 | |
| 68 | private: |
| 69 | static uint64_t getHash(SectionChunk *C); |
| 70 | static bool equalsConstant(const SectionChunk *A, const SectionChunk *B); |
| 71 | static bool equalsVariable(const SectionChunk *A, const SectionChunk *B); |
Rui Ueyama | 0fff21f | 2015-09-18 21:17:44 +0000 | [diff] [blame] | 72 | bool forEachGroup(std::vector<SectionChunk *> &Chunks, Comparator Eq); |
Rui Ueyama | 437e0d2 | 2015-09-16 14:19:10 +0000 | [diff] [blame] | 73 | bool partition(ChunkIterator Begin, ChunkIterator End, Comparator Eq); |
| 74 | |
Rui Ueyama | 48788ff | 2015-09-20 01:44:44 +0000 | [diff] [blame] | 75 | std::atomic<uint64_t> NextID = { 1 }; |
Rui Ueyama | 437e0d2 | 2015-09-16 14:19:10 +0000 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | // Entry point to ICF. |
| 79 | void doICF(const std::vector<Chunk *> &Chunks) { |
Rui Ueyama | 0fff21f | 2015-09-18 21:17:44 +0000 | [diff] [blame] | 80 | ICF().run(Chunks); |
Rui Ueyama | 46ed29b | 2015-09-11 04:29:03 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Rui Ueyama | 437e0d2 | 2015-09-16 14:19:10 +0000 | [diff] [blame] | 83 | uint64_t ICF::getHash(SectionChunk *C) { |
| 84 | return hash_combine(C->getPermissions(), |
| 85 | hash_value(C->SectionName), |
| 86 | C->NumRelocs, |
Rui Ueyama | 473e5a7 | 2015-09-25 16:50:12 +0000 | [diff] [blame] | 87 | C->getAlign(), |
Rui Ueyama | 437e0d2 | 2015-09-16 14:19:10 +0000 | [diff] [blame] | 88 | uint32_t(C->Header->SizeOfRawData), |
Rui Ueyama | 437e0d2 | 2015-09-16 14:19:10 +0000 | [diff] [blame] | 89 | C->Checksum); |
Rui Ueyama | 9be9aa8 | 2015-09-16 03:26:31 +0000 | [diff] [blame] | 90 | } |
Rui Ueyama | 46ed29b | 2015-09-11 04:29:03 +0000 | [diff] [blame] | 91 | |
Rui Ueyama | 437e0d2 | 2015-09-16 14:19:10 +0000 | [diff] [blame] | 92 | bool ICF::equalsConstant(const SectionChunk *A, const SectionChunk *B) { |
Rui Ueyama | 89aa474 | 2015-09-18 02:40:54 +0000 | [diff] [blame] | 93 | if (A->AssocChildren.size() != B->AssocChildren.size() || |
| 94 | A->NumRelocs != B->NumRelocs) { |
Rui Ueyama | 791416f | 2015-07-30 22:57:21 +0000 | [diff] [blame] | 95 | return false; |
Rui Ueyama | 9be9aa8 | 2015-09-16 03:26:31 +0000 | [diff] [blame] | 96 | } |
Rui Ueyama | 791416f | 2015-07-30 22:57:21 +0000 | [diff] [blame] | 97 | |
Rui Ueyama | e2d2bfd | 2015-09-18 01:30:56 +0000 | [diff] [blame] | 98 | // Compare associative sections. |
Rui Ueyama | 9be9aa8 | 2015-09-16 03:26:31 +0000 | [diff] [blame] | 99 | for (size_t I = 0, E = A->AssocChildren.size(); I != E; ++I) |
| 100 | if (A->AssocChildren[I]->GroupID != B->AssocChildren[I]->GroupID) |
Rui Ueyama | 791416f | 2015-07-30 22:57:21 +0000 | [diff] [blame] | 101 | return false; |
| 102 | |
Rui Ueyama | e2d2bfd | 2015-09-18 01:30:56 +0000 | [diff] [blame] | 103 | // Compare relocations. |
Rui Ueyama | 791416f | 2015-07-30 22:57:21 +0000 | [diff] [blame] | 104 | auto Eq = [&](const coff_relocation &R1, const coff_relocation &R2) { |
Rui Ueyama | 9be9aa8 | 2015-09-16 03:26:31 +0000 | [diff] [blame] | 105 | if (R1.Type != R2.Type || |
| 106 | R1.VirtualAddress != R2.VirtualAddress) { |
Rui Ueyama | 791416f | 2015-07-30 22:57:21 +0000 | [diff] [blame] | 107 | return false; |
Rui Ueyama | 9be9aa8 | 2015-09-16 03:26:31 +0000 | [diff] [blame] | 108 | } |
| 109 | SymbolBody *B1 = A->File->getSymbolBody(R1.SymbolTableIndex)->repl(); |
| 110 | SymbolBody *B2 = B->File->getSymbolBody(R2.SymbolTableIndex)->repl(); |
Rui Ueyama | 791416f | 2015-07-30 22:57:21 +0000 | [diff] [blame] | 111 | if (B1 == B2) |
| 112 | return true; |
Rui Ueyama | 89aa474 | 2015-09-18 02:40:54 +0000 | [diff] [blame] | 113 | if (auto *D1 = dyn_cast<DefinedRegular>(B1)) |
| 114 | if (auto *D2 = dyn_cast<DefinedRegular>(B2)) |
| 115 | return D1->getValue() == D2->getValue() && |
| 116 | D1->getChunk()->GroupID == D2->getChunk()->GroupID; |
| 117 | return false; |
Rui Ueyama | 791416f | 2015-07-30 22:57:21 +0000 | [diff] [blame] | 118 | }; |
Rui Ueyama | e2d2bfd | 2015-09-18 01:30:56 +0000 | [diff] [blame] | 119 | if (!std::equal(A->Relocs.begin(), A->Relocs.end(), B->Relocs.begin(), Eq)) |
| 120 | return false; |
| 121 | |
Rui Ueyama | 89aa474 | 2015-09-18 02:40:54 +0000 | [diff] [blame] | 122 | // Compare section attributes and contents. |
| 123 | return A->getPermissions() == B->getPermissions() && |
| 124 | A->SectionName == B->SectionName && |
Rui Ueyama | 473e5a7 | 2015-09-25 16:50:12 +0000 | [diff] [blame] | 125 | A->getAlign() == B->getAlign() && |
Rui Ueyama | 89aa474 | 2015-09-18 02:40:54 +0000 | [diff] [blame] | 126 | A->Header->SizeOfRawData == B->Header->SizeOfRawData && |
| 127 | A->Checksum == B->Checksum && |
| 128 | A->getContents() == B->getContents(); |
Rui Ueyama | 9be9aa8 | 2015-09-16 03:26:31 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Rui Ueyama | 437e0d2 | 2015-09-16 14:19:10 +0000 | [diff] [blame] | 131 | bool ICF::equalsVariable(const SectionChunk *A, const SectionChunk *B) { |
Rui Ueyama | 75cea68 | 2015-09-18 01:51:37 +0000 | [diff] [blame] | 132 | // Compare associative sections. |
| 133 | for (size_t I = 0, E = A->AssocChildren.size(); I != E; ++I) |
| 134 | if (A->AssocChildren[I]->GroupID != B->AssocChildren[I]->GroupID) |
| 135 | return false; |
| 136 | |
| 137 | // Compare relocations. |
| 138 | auto Eq = [&](const coff_relocation &R1, const coff_relocation &R2) { |
| 139 | SymbolBody *B1 = A->File->getSymbolBody(R1.SymbolTableIndex)->repl(); |
Rui Ueyama | cc43c50 | 2015-09-20 20:19:12 +0000 | [diff] [blame] | 140 | SymbolBody *B2 = B->File->getSymbolBody(R2.SymbolTableIndex)->repl(); |
| 141 | if (B1 == B2) |
| 142 | return true; |
| 143 | if (auto *D1 = dyn_cast<DefinedRegular>(B1)) |
Rui Ueyama | 0fff21f | 2015-09-18 21:17:44 +0000 | [diff] [blame] | 144 | if (auto *D2 = dyn_cast<DefinedRegular>(B2)) |
| 145 | return D1->getChunk()->GroupID == D2->getChunk()->GroupID; |
Rui Ueyama | 0fff21f | 2015-09-18 21:17:44 +0000 | [diff] [blame] | 146 | return false; |
Rui Ueyama | 75cea68 | 2015-09-18 01:51:37 +0000 | [diff] [blame] | 147 | }; |
| 148 | return std::equal(A->Relocs.begin(), A->Relocs.end(), B->Relocs.begin(), Eq); |
Rui Ueyama | 9be9aa8 | 2015-09-16 03:26:31 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Rui Ueyama | 437e0d2 | 2015-09-16 14:19:10 +0000 | [diff] [blame] | 151 | bool ICF::partition(ChunkIterator Begin, ChunkIterator End, Comparator Eq) { |
Rui Ueyama | 9be9aa8 | 2015-09-16 03:26:31 +0000 | [diff] [blame] | 152 | bool R = false; |
| 153 | for (auto It = Begin;;) { |
| 154 | SectionChunk *Head = *It; |
| 155 | auto Bound = std::partition(It + 1, End, [&](SectionChunk *SC) { |
| 156 | return Eq(Head, SC); |
| 157 | }); |
| 158 | if (Bound == End) |
| 159 | return R; |
Rui Ueyama | 4b1d579 | 2015-09-25 16:38:13 +0000 | [diff] [blame] | 160 | uint64_t ID = NextID++; |
Rui Ueyama | 9be9aa8 | 2015-09-16 03:26:31 +0000 | [diff] [blame] | 161 | std::for_each(It, Bound, [&](SectionChunk *SC) { SC->GroupID = ID; }); |
| 162 | It = Bound; |
| 163 | R = true; |
| 164 | } |
| 165 | } |
| 166 | |
Rui Ueyama | 0fff21f | 2015-09-18 21:17:44 +0000 | [diff] [blame] | 167 | bool ICF::forEachGroup(std::vector<SectionChunk *> &Chunks, Comparator Eq) { |
Rui Ueyama | 9be9aa8 | 2015-09-16 03:26:31 +0000 | [diff] [blame] | 168 | bool R = false; |
Rui Ueyama | 0fff21f | 2015-09-18 21:17:44 +0000 | [diff] [blame] | 169 | for (auto It = Chunks.begin(), End = Chunks.end(); It != End;) { |
Rui Ueyama | 9be9aa8 | 2015-09-16 03:26:31 +0000 | [diff] [blame] | 170 | SectionChunk *Head = *It; |
| 171 | auto Bound = std::find_if(It + 1, End, [&](SectionChunk *SC) { |
| 172 | return SC->GroupID != Head->GroupID; |
| 173 | }); |
| 174 | if (partition(It, Bound, Eq)) |
| 175 | R = true; |
| 176 | It = Bound; |
| 177 | } |
| 178 | return R; |
Rui Ueyama | 791416f | 2015-07-30 22:57:21 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Rui Ueyama | 6b5feed | 2015-06-24 20:40:03 +0000 | [diff] [blame] | 181 | // Merge identical COMDAT sections. |
Rui Ueyama | a7f08f1 | 2015-09-04 21:35:54 +0000 | [diff] [blame] | 182 | // Two sections are considered the same if their section headers, |
Rui Ueyama | 6b5feed | 2015-06-24 20:40:03 +0000 | [diff] [blame] | 183 | // contents and relocations are all the same. |
Rui Ueyama | 0fff21f | 2015-09-18 21:17:44 +0000 | [diff] [blame] | 184 | void ICF::run(const std::vector<Chunk *> &Vec) { |
Rui Ueyama | 437e0d2 | 2015-09-16 14:19:10 +0000 | [diff] [blame] | 185 | // Collect only mergeable sections and group by hash value. |
Rui Ueyama | 0fff21f | 2015-09-18 21:17:44 +0000 | [diff] [blame] | 186 | parallel_for_each(Vec.begin(), Vec.end(), [&](Chunk *C) { |
Rui Ueyama | 9be9aa8 | 2015-09-16 03:26:31 +0000 | [diff] [blame] | 187 | if (auto *SC = dyn_cast<SectionChunk>(C)) { |
Rui Ueyama | 7233523 | 2015-12-03 02:23:33 +0000 | [diff] [blame] | 188 | bool Global = SC->Sym && SC->Sym->isExternal(); |
Rui Ueyama | 9be9aa8 | 2015-09-16 03:26:31 +0000 | [diff] [blame] | 189 | bool Writable = SC->getPermissions() & llvm::COFF::IMAGE_SCN_MEM_WRITE; |
Rui Ueyama | 7233523 | 2015-12-03 02:23:33 +0000 | [diff] [blame] | 190 | if (SC->isCOMDAT() && SC->isLive() && Global && !Writable) |
Rui Ueyama | 437e0d2 | 2015-09-16 14:19:10 +0000 | [diff] [blame] | 191 | SC->GroupID = getHash(SC) | (uint64_t(1) << 63); |
Rui Ueyama | 708d7fa | 2015-09-18 21:06:34 +0000 | [diff] [blame] | 192 | } |
| 193 | }); |
Rui Ueyama | 317c176 | 2015-10-26 16:20:00 +0000 | [diff] [blame] | 194 | std::vector<SectionChunk *> Chunks; |
Rui Ueyama | 0fff21f | 2015-09-18 21:17:44 +0000 | [diff] [blame] | 195 | for (Chunk *C : Vec) { |
Rui Ueyama | 708d7fa | 2015-09-18 21:06:34 +0000 | [diff] [blame] | 196 | if (auto *SC = dyn_cast<SectionChunk>(C)) { |
| 197 | if (SC->GroupID) { |
Rui Ueyama | 317c176 | 2015-10-26 16:20:00 +0000 | [diff] [blame] | 198 | Chunks.push_back(SC); |
Rui Ueyama | 9be9aa8 | 2015-09-16 03:26:31 +0000 | [diff] [blame] | 199 | } else { |
| 200 | SC->GroupID = NextID++; |
| 201 | } |
Rui Ueyama | 46ed29b | 2015-09-11 04:29:03 +0000 | [diff] [blame] | 202 | } |
Rui Ueyama | 9be9aa8 | 2015-09-16 03:26:31 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Rui Ueyama | 708d7fa | 2015-09-18 21:06:34 +0000 | [diff] [blame] | 205 | // From now on, sections in Chunks are ordered so that sections in |
Rui Ueyama | 437e0d2 | 2015-09-16 14:19:10 +0000 | [diff] [blame] | 206 | // the same group are consecutive in the vector. |
Rui Ueyama | 317c176 | 2015-10-26 16:20:00 +0000 | [diff] [blame] | 207 | std::sort(Chunks.begin(), Chunks.end(), |
| 208 | [](SectionChunk *A, SectionChunk *B) { |
| 209 | return A->GroupID < B->GroupID; |
| 210 | }); |
Rui Ueyama | 9be9aa8 | 2015-09-16 03:26:31 +0000 | [diff] [blame] | 211 | |
| 212 | // Split groups until we get a convergence. |
Rui Ueyama | 38ea2e5 | 2015-09-16 23:55:39 +0000 | [diff] [blame] | 213 | int Cnt = 1; |
Rui Ueyama | 317c176 | 2015-10-26 16:20:00 +0000 | [diff] [blame] | 214 | forEachGroup(Chunks, equalsConstant); |
Rui Ueyama | 708d7fa | 2015-09-18 21:06:34 +0000 | [diff] [blame] | 215 | |
| 216 | for (;;) { |
Rui Ueyama | 317c176 | 2015-10-26 16:20:00 +0000 | [diff] [blame] | 217 | if (!forEachGroup(Chunks, equalsVariable)) |
Rui Ueyama | 708d7fa | 2015-09-18 21:06:34 +0000 | [diff] [blame] | 218 | break; |
Rui Ueyama | 38ea2e5 | 2015-09-16 23:55:39 +0000 | [diff] [blame] | 219 | ++Cnt; |
Rui Ueyama | 708d7fa | 2015-09-18 21:06:34 +0000 | [diff] [blame] | 220 | } |
Rui Ueyama | 38ea2e5 | 2015-09-16 23:55:39 +0000 | [diff] [blame] | 221 | if (Config->Verbose) |
| 222 | llvm::outs() << "\nICF needed " << Cnt << " iterations.\n"; |
Rui Ueyama | 9be9aa8 | 2015-09-16 03:26:31 +0000 | [diff] [blame] | 223 | |
| 224 | // Merge sections in the same group. |
Rui Ueyama | 317c176 | 2015-10-26 16:20:00 +0000 | [diff] [blame] | 225 | for (auto It = Chunks.begin(), End = Chunks.end(); It != End;) { |
| 226 | SectionChunk *Head = *It++; |
| 227 | auto Bound = std::find_if(It, End, [&](SectionChunk *SC) { |
| 228 | return Head->GroupID != SC->GroupID; |
| 229 | }); |
| 230 | if (It == Bound) |
| 231 | continue; |
| 232 | if (Config->Verbose) |
| 233 | llvm::outs() << "Selected " << Head->getDebugName() << "\n"; |
| 234 | while (It != Bound) { |
| 235 | SectionChunk *SC = *It++; |
Rui Ueyama | 9be9aa8 | 2015-09-16 03:26:31 +0000 | [diff] [blame] | 236 | if (Config->Verbose) |
Rui Ueyama | 317c176 | 2015-10-26 16:20:00 +0000 | [diff] [blame] | 237 | llvm::outs() << " Removed " << SC->getDebugName() << "\n"; |
| 238 | Head->replace(SC); |
Rui Ueyama | 9be9aa8 | 2015-09-16 03:26:31 +0000 | [diff] [blame] | 239 | } |
Rui Ueyama | a7f08f1 | 2015-09-04 21:35:54 +0000 | [diff] [blame] | 240 | } |
Rui Ueyama | 6b5feed | 2015-06-24 20:40:03 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | } // namespace coff |
| 244 | } // namespace lld |