Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +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 | // |
| 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 Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 60 | #include "SymbolTable.h" |
| 61 | |
| 62 | #include "llvm/ADT/Hashing.h" |
| 63 | #include "llvm/Object/ELF.h" |
| 64 | #include "llvm/Support/ELF.h" |
Rui Ueyama | a05134e | 2016-11-19 20:15:55 +0000 | [diff] [blame] | 65 | #include <algorithm> |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 66 | |
| 67 | using namespace lld; |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 68 | using namespace lld::elf; |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 69 | using namespace llvm; |
| 70 | using namespace llvm::ELF; |
| 71 | using namespace llvm::object; |
| 72 | |
Rui Ueyama | bd1f063 | 2016-11-20 02:39:59 +0000 | [diff] [blame] | 73 | namespace { |
Rui Ueyama | 9dedfb1 | 2016-11-30 01:50:03 +0000 | [diff] [blame^] | 74 | struct Range { |
| 75 | size_t Begin; |
| 76 | size_t End; |
| 77 | }; |
| 78 | |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 79 | template <class ELFT> class ICF { |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 80 | public: |
Rui Ueyama | 4f8d21f | 2016-05-02 19:30:42 +0000 | [diff] [blame] | 81 | void run(); |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 82 | |
| 83 | private: |
Rui Ueyama | 9dedfb1 | 2016-11-30 01:50:03 +0000 | [diff] [blame^] | 84 | void segregate(Range *R, bool Constant); |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 85 | |
Rui Ueyama | 9dedfb1 | 2016-11-30 01:50:03 +0000 | [diff] [blame^] | 86 | template <class RelTy> |
| 87 | bool constantEq(ArrayRef<RelTy> RelsA, ArrayRef<RelTy> RelsB); |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 88 | |
Rui Ueyama | 9dedfb1 | 2016-11-30 01:50:03 +0000 | [diff] [blame^] | 89 | template <class RelTy> |
| 90 | bool variableEq(const InputSection<ELFT> *A, ArrayRef<RelTy> RelsA, |
| 91 | const InputSection<ELFT> *B, ArrayRef<RelTy> RelsB); |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 92 | |
Rui Ueyama | 9dedfb1 | 2016-11-30 01:50:03 +0000 | [diff] [blame^] | 93 | bool equalsConstant(const InputSection<ELFT> *A, const InputSection<ELFT> *B); |
| 94 | bool equalsVariable(const InputSection<ELFT> *A, const InputSection<ELFT> *B); |
| 95 | |
| 96 | std::vector<InputSection<ELFT> *> Sections; |
| 97 | std::vector<Range> Ranges; |
| 98 | |
| 99 | // The main loop is repeated until we get a convergence. |
| 100 | bool Repeat = false; // If Repeat is true, we need to repeat. |
| 101 | int Cnt = 0; // Counter for the main loop. |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 102 | }; |
| 103 | } |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 104 | |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 105 | // Returns a hash value for S. Note that the information about |
| 106 | // relocation targets is not included in the hash value. |
Rui Ueyama | bd1f063 | 2016-11-20 02:39:59 +0000 | [diff] [blame] | 107 | template <class ELFT> static uint64_t getHash(InputSection<ELFT> *S) { |
Rui Ueyama | a05134e | 2016-11-19 20:15:55 +0000 | [diff] [blame] | 108 | return hash_combine(S->Flags, S->getSize(), S->NumRelocations); |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Rui Ueyama | bd1f063 | 2016-11-20 02:39:59 +0000 | [diff] [blame] | 111 | // Returns true if section S is subject of ICF. |
| 112 | template <class ELFT> static bool isEligible(InputSection<ELFT> *S) { |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 113 | // .init and .fini contains instructions that must be executed to |
| 114 | // initialize and finalize the process. They cannot and should not |
| 115 | // be merged. |
Rui Ueyama | bd1f063 | 2016-11-20 02:39:59 +0000 | [diff] [blame] | 116 | return S->Live && (S->Flags & SHF_ALLOC) && !(S->Flags & SHF_WRITE) && |
| 117 | S->Name != ".init" && S->Name != ".fini"; |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Rui Ueyama | 9dedfb1 | 2016-11-30 01:50:03 +0000 | [diff] [blame^] | 120 | // Before calling this function, all sections in range R must have the |
| 121 | // same group ID. |
| 122 | template <class ELFT> void ICF<ELFT>::segregate(Range *R, bool Constant) { |
| 123 | // This loop rearranges sections in range R so that all sections |
| 124 | // that are equal in terms of equals{Constant,Variable} are contiguous |
| 125 | // in Sections vector. |
| 126 | // |
| 127 | // The algorithm is quadratic in the worst case, but that is not an |
| 128 | // issue in practice because the number of the distinct sections in |
| 129 | // [R.Begin, R.End] is usually very small. |
| 130 | while (R->End - R->Begin > 1) { |
| 131 | // Divide range R into two. Let Mid be the start index of the |
| 132 | // second group. |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 133 | auto Bound = std::stable_partition( |
Rui Ueyama | 9dedfb1 | 2016-11-30 01:50:03 +0000 | [diff] [blame^] | 134 | Sections.begin() + R->Begin + 1, Sections.begin() + R->End, |
| 135 | [&](InputSection<ELFT> *S) { |
| 136 | if (Constant) |
| 137 | return equalsConstant(Sections[R->Begin], S); |
| 138 | return equalsVariable(Sections[R->Begin], S); |
| 139 | }); |
| 140 | size_t Mid = Bound - Sections.begin(); |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 141 | |
Rui Ueyama | 9dedfb1 | 2016-11-30 01:50:03 +0000 | [diff] [blame^] | 142 | if (Mid == R->End) |
| 143 | return; |
| 144 | |
| 145 | // Now we split [R.Begin, R.End) into [R.Begin, Mid) and [Mid, R.End). |
| 146 | if (Mid - R->Begin > 1) |
| 147 | Ranges.push_back({R->Begin, Mid}); |
| 148 | R->Begin = Mid; |
| 149 | |
| 150 | // Update GroupIds for the new group members. We use the index of |
| 151 | // the group first member as a group ID because that is unique. |
| 152 | for (size_t I = Mid; I < R->End; ++I) |
| 153 | Sections[I]->GroupId = Mid; |
| 154 | |
| 155 | // Since we have split a group, we need to repeat the main loop |
| 156 | // later to obtain a convergence. Remember that. |
| 157 | Repeat = true; |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | |
| 161 | // Compare two lists of relocations. |
Rui Ueyama | 9dedfb1 | 2016-11-30 01:50:03 +0000 | [diff] [blame^] | 162 | template <class ELFT> |
| 163 | template <class RelTy> |
| 164 | bool ICF<ELFT>::constantEq(ArrayRef<RelTy> RelsA, ArrayRef<RelTy> RelsB) { |
Rui Ueyama | a05134e | 2016-11-19 20:15:55 +0000 | [diff] [blame] | 165 | auto Eq = [](const RelTy &A, const RelTy &B) { |
| 166 | return A.r_offset == B.r_offset && |
| 167 | A.getType(Config->Mips64EL) == B.getType(Config->Mips64EL) && |
| 168 | getAddend<ELFT>(A) == getAddend<ELFT>(B); |
| 169 | }; |
| 170 | |
| 171 | return RelsA.size() == RelsB.size() && |
| 172 | std::equal(RelsA.begin(), RelsA.end(), RelsB.begin(), Eq); |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | // Compare "non-moving" part of two InputSections, namely everything |
| 176 | // except relocation targets. |
| 177 | template <class ELFT> |
Rui Ueyama | 9dedfb1 | 2016-11-30 01:50:03 +0000 | [diff] [blame^] | 178 | bool ICF<ELFT>::equalsConstant(const InputSection<ELFT> *A, |
| 179 | const InputSection<ELFT> *B) { |
Rui Ueyama | bd1f063 | 2016-11-20 02:39:59 +0000 | [diff] [blame] | 180 | if (A->NumRelocations != B->NumRelocations || A->Flags != B->Flags || |
| 181 | A->getSize() != B->getSize() || A->Data != B->Data) |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 182 | return false; |
| 183 | |
Rui Ueyama | bd1f063 | 2016-11-20 02:39:59 +0000 | [diff] [blame] | 184 | if (A->AreRelocsRela) |
Rui Ueyama | 9dedfb1 | 2016-11-30 01:50:03 +0000 | [diff] [blame^] | 185 | return constantEq(A->relas(), B->relas()); |
| 186 | return constantEq(A->rels(), B->rels()); |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Rui Ueyama | 7bed9ee | 2016-11-20 23:15:54 +0000 | [diff] [blame] | 189 | // Compare two lists of relocations. Returns true if all pairs of |
| 190 | // relocations point to the same section in terms of ICF. |
Rui Ueyama | 9dedfb1 | 2016-11-30 01:50:03 +0000 | [diff] [blame^] | 191 | template <class ELFT> |
| 192 | template <class RelTy> |
| 193 | bool ICF<ELFT>::variableEq(const InputSection<ELFT> *A, ArrayRef<RelTy> RelsA, |
| 194 | const InputSection<ELFT> *B, ArrayRef<RelTy> RelsB) { |
Rui Ueyama | a05134e | 2016-11-19 20:15:55 +0000 | [diff] [blame] | 195 | auto Eq = [&](const RelTy &RA, const RelTy &RB) { |
Rui Ueyama | bd1f063 | 2016-11-20 02:39:59 +0000 | [diff] [blame] | 196 | SymbolBody &SA = A->getFile()->getRelocTargetSym(RA); |
| 197 | SymbolBody &SB = B->getFile()->getRelocTargetSym(RB); |
Rafael Espindola | 67d72c0 | 2016-03-11 12:06:30 +0000 | [diff] [blame] | 198 | if (&SA == &SB) |
Rui Ueyama | a05134e | 2016-11-19 20:15:55 +0000 | [diff] [blame] | 199 | return true; |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 200 | |
| 201 | // Or, the symbols should be pointing to the same section |
| 202 | // in terms of the group ID. |
Rafael Espindola | 67d72c0 | 2016-03-11 12:06:30 +0000 | [diff] [blame] | 203 | auto *DA = dyn_cast<DefinedRegular<ELFT>>(&SA); |
| 204 | auto *DB = dyn_cast<DefinedRegular<ELFT>>(&SB); |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 205 | if (!DA || !DB) |
| 206 | return false; |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 207 | if (DA->Value != DB->Value) |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 208 | return false; |
Rui Ueyama | 9dedfb1 | 2016-11-30 01:50:03 +0000 | [diff] [blame^] | 209 | |
Rui Ueyama | 9f8cb73 | 2016-11-20 02:43:44 +0000 | [diff] [blame] | 210 | auto *X = dyn_cast<InputSection<ELFT>>(DA->Section); |
| 211 | auto *Y = dyn_cast<InputSection<ELFT>>(DB->Section); |
Rui Ueyama | 9dedfb1 | 2016-11-30 01:50:03 +0000 | [diff] [blame^] | 212 | if (!X || !Y) |
| 213 | return false; |
| 214 | return X->GroupId != 0 && X->GroupId == Y->GroupId; |
Rui Ueyama | a05134e | 2016-11-19 20:15:55 +0000 | [diff] [blame] | 215 | }; |
| 216 | |
| 217 | return std::equal(RelsA.begin(), RelsA.end(), RelsB.begin(), Eq); |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | // Compare "moving" part of two InputSections, namely relocation targets. |
| 221 | template <class ELFT> |
Rui Ueyama | 9dedfb1 | 2016-11-30 01:50:03 +0000 | [diff] [blame^] | 222 | bool ICF<ELFT>::equalsVariable(const InputSection<ELFT> *A, |
| 223 | const InputSection<ELFT> *B) { |
Rafael Espindola | 9f0c4bb | 2016-11-10 14:53:24 +0000 | [diff] [blame] | 224 | if (A->AreRelocsRela) |
Rui Ueyama | a05134e | 2016-11-19 20:15:55 +0000 | [diff] [blame] | 225 | return variableEq(A, A->relas(), B, B->relas()); |
| 226 | return variableEq(A, A->rels(), B, B->rels()); |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | // The main function of ICF. |
Rui Ueyama | 4f8d21f | 2016-05-02 19:30:42 +0000 | [diff] [blame] | 230 | template <class ELFT> void ICF<ELFT>::run() { |
Rui Ueyama | 9dedfb1 | 2016-11-30 01:50:03 +0000 | [diff] [blame^] | 231 | // Collect sections to merge. |
| 232 | for (InputSectionBase<ELFT> *Sec : Symtab<ELFT>::X->Sections) |
| 233 | if (auto *S = dyn_cast<InputSection<ELFT>>(Sec)) |
| 234 | if (isEligible(S)) |
| 235 | Sections.push_back(S); |
| 236 | |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 237 | // Initially, we use hash values as section group IDs. Therefore, |
| 238 | // if two sections have the same ID, they are likely (but not |
| 239 | // guaranteed) to have the same static contents in terms of ICF. |
Rui Ueyama | e2dfbc1 | 2016-11-19 23:14:23 +0000 | [diff] [blame] | 240 | for (InputSection<ELFT> *S : Sections) |
Rui Ueyama | 9dedfb1 | 2016-11-30 01:50:03 +0000 | [diff] [blame^] | 241 | // Set MSB to 1 to avoid collisions with non-hash IDs. |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 242 | S->GroupId = getHash(S) | (uint64_t(1) << 63); |
| 243 | |
Rui Ueyama | 7bed9ee | 2016-11-20 23:15:54 +0000 | [diff] [blame] | 244 | // From now on, sections in Sections are ordered so that sections in |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 245 | // the same group are consecutive in the vector. |
Rui Ueyama | e2dfbc1 | 2016-11-19 23:14:23 +0000 | [diff] [blame] | 246 | std::stable_sort(Sections.begin(), Sections.end(), |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 247 | [](InputSection<ELFT> *A, InputSection<ELFT> *B) { |
Petr Hosek | 901948a | 2016-08-22 18:53:09 +0000 | [diff] [blame] | 248 | if (A->GroupId != B->GroupId) |
| 249 | return A->GroupId < B->GroupId; |
| 250 | // Within a group, put the highest alignment |
| 251 | // requirement first, so that's the one we'll keep. |
| 252 | return B->Alignment < A->Alignment; |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 253 | }); |
| 254 | |
Rui Ueyama | 9dedfb1 | 2016-11-30 01:50:03 +0000 | [diff] [blame^] | 255 | // Split sections into groups by ID. And then we are going to |
| 256 | // split groups into more and more smaller groups. |
| 257 | // Note that we do not add single element groups because they |
| 258 | // are already the smallest. |
| 259 | Ranges.reserve(Sections.size()); |
| 260 | for (size_t I = 0, E = Sections.size(); I < E - 1;) { |
| 261 | // Let J be the first index whose element has a different ID. |
| 262 | size_t J = I + 1; |
| 263 | while (J < E && Sections[I]->GroupId == Sections[J]->GroupId) |
| 264 | ++J; |
| 265 | if (J - I > 1) |
| 266 | Ranges.push_back({I, J}); |
| 267 | I = J; |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 268 | } |
Rui Ueyama | 9dedfb1 | 2016-11-30 01:50:03 +0000 | [diff] [blame^] | 269 | |
| 270 | // Compare static contents and assign unique IDs for each static content. |
| 271 | std::for_each(Ranges.begin(), Ranges.end(), |
| 272 | [&](Range &R) { segregate(&R, true); }); |
| 273 | ++Cnt; |
| 274 | |
| 275 | // Split groups by comparing relocations until convergence is obtained. |
| 276 | do { |
| 277 | Repeat = false; |
| 278 | std::for_each(Ranges.begin(), Ranges.end(), |
| 279 | [&](Range &R) { segregate(&R, false); }); |
| 280 | ++Cnt; |
| 281 | } while (Repeat); |
| 282 | |
| 283 | log("ICF needed " + Twine(Cnt) + " iterations"); |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 284 | |
| 285 | // Merge sections in the same group. |
Rui Ueyama | 9dedfb1 | 2016-11-30 01:50:03 +0000 | [diff] [blame^] | 286 | for (Range R : Ranges) { |
| 287 | if (R.End - R.Begin == 1) |
| 288 | continue; |
| 289 | |
| 290 | log("selected " + Sections[R.Begin]->Name); |
| 291 | for (size_t I = R.Begin + 1; I < R.End; ++I) { |
| 292 | log(" removed " + Sections[I]->Name); |
| 293 | Sections[R.Begin]->replace(Sections[I]); |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 294 | } |
Rui Ueyama | 9dedfb1 | 2016-11-30 01:50:03 +0000 | [diff] [blame^] | 295 | } |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | // ICF entry point function. |
Rui Ueyama | 4f8d21f | 2016-05-02 19:30:42 +0000 | [diff] [blame] | 299 | template <class ELFT> void elf::doIcf() { ICF<ELFT>().run(); } |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 300 | |
Rui Ueyama | 4f8d21f | 2016-05-02 19:30:42 +0000 | [diff] [blame] | 301 | template void elf::doIcf<ELF32LE>(); |
| 302 | template void elf::doIcf<ELF32BE>(); |
| 303 | template void elf::doIcf<ELF64LE>(); |
| 304 | template void elf::doIcf<ELF64BE>(); |