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 | // |
Rui Ueyama | 91ae861 | 2016-12-01 19:45:22 +0000 | [diff] [blame] | 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 |
| 13 | // by a few percent. |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 14 | // |
Rui Ueyama | 91ae861 | 2016-12-01 19:45:22 +0000 | [diff] [blame] | 15 | // In ICF, two sections are considered identical if they have the same |
| 16 | // section flags, section data, and relocations. Relocations are tricky, |
| 17 | // because two relocations are considered the same if they have the same |
| 18 | // relocation types, values, and if they point to the same sections *in |
| 19 | // terms of ICF*. |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 20 | // |
Rui Ueyama | 91ae861 | 2016-12-01 19:45:22 +0000 | [diff] [blame] | 21 | // Here is an example. If foo and bar defined below are compiled to the |
| 22 | // same machine instructions, ICF can and should merge the two, although |
| 23 | // their relocations point to each other. |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 24 | // |
| 25 | // void foo() { bar(); } |
| 26 | // void bar() { foo(); } |
| 27 | // |
Rui Ueyama | 91ae861 | 2016-12-01 19:45:22 +0000 | [diff] [blame] | 28 | // If you merge the two, their relocations point to the same section and |
| 29 | // thus you know they are mergeable, but how do we know they are mergeable |
| 30 | // in the first place? This is not an easy problem to solve. |
| 31 | // |
| 32 | // What we are doing in LLD is some sort of coloring algorithm. |
| 33 | // |
| 34 | // We color non-identical sections in different colors repeatedly. |
| 35 | // Sections in the same color when the algorithm terminates are considered |
| 36 | // identical. Here are the details: |
| 37 | // |
| 38 | // 1. First, we color all sections using their hash values of section |
| 39 | // types, section contents, and numbers of relocations. At this moment, |
| 40 | // relocation targets are not taken into account. We just color |
| 41 | // sections that apparently differ in different colors. |
| 42 | // |
| 43 | // 2. Next, for each color C, we visit sections in color C to compare |
| 44 | // relocation target colors. We recolor sections A and B in different |
| 45 | // colors if A's and B's relocations are different in terms of target |
| 46 | // colors. |
| 47 | // |
| 48 | // 3. If we recolor some section in step 2, relocations that were |
| 49 | // previously pointing to the same color targets may now be pointing to |
| 50 | // different colors. Therefore, repeat 2 until a convergence is |
| 51 | // obtained. |
| 52 | // |
| 53 | // 4. For each color C, pick an arbitrary section in color C, and merges |
| 54 | // other sections in color C with it. |
| 55 | // |
| 56 | // For small programs, this algorithm needs 3-5 iterations. For large |
| 57 | // programs such as Chromium, it takes more than 20 iterations. |
| 58 | // |
| 59 | // We parallelize each step so that multiple threads can work on different |
| 60 | // colors concurrently. That gave us a large performance boost when |
| 61 | // applying ICF on large programs. For example, MSVC link.exe or GNU gold |
| 62 | // takes 10-20 seconds to apply ICF on Chromium, whose output size is |
| 63 | // about 1.5 GB, but LLD can finish it in less than 2 seconds on a 2.8 GHz |
| 64 | // 40 core machine. Even without threading, LLD's ICF is still faster than |
| 65 | // MSVC or gold though. |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 66 | // |
| 67 | //===----------------------------------------------------------------------===// |
| 68 | |
| 69 | #include "ICF.h" |
| 70 | #include "Config.h" |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 71 | #include "SymbolTable.h" |
| 72 | |
Rui Ueyama | c183531 | 2016-12-01 17:09:04 +0000 | [diff] [blame] | 73 | #include "lld/Core/Parallel.h" |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 74 | #include "llvm/ADT/Hashing.h" |
| 75 | #include "llvm/Object/ELF.h" |
| 76 | #include "llvm/Support/ELF.h" |
Rui Ueyama | a05134e | 2016-11-19 20:15:55 +0000 | [diff] [blame] | 77 | #include <algorithm> |
Rui Ueyama | 1b6bab0 | 2016-12-02 05:35:46 +0000 | [diff] [blame] | 78 | #include <atomic> |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 79 | |
| 80 | using namespace lld; |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 81 | using namespace lld::elf; |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 82 | using namespace llvm; |
| 83 | using namespace llvm::ELF; |
| 84 | using namespace llvm::object; |
| 85 | |
Rui Ueyama | bd1f063 | 2016-11-20 02:39:59 +0000 | [diff] [blame] | 86 | namespace { |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 87 | template <class ELFT> class ICF { |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 88 | public: |
Rui Ueyama | 4f8d21f | 2016-05-02 19:30:42 +0000 | [diff] [blame] | 89 | void run(); |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 90 | |
| 91 | private: |
Rui Ueyama | 1b6bab0 | 2016-12-02 05:35:46 +0000 | [diff] [blame] | 92 | void segregate(size_t Begin, size_t End, bool Constant); |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 93 | |
Rui Ueyama | 9dedfb1 | 2016-11-30 01:50:03 +0000 | [diff] [blame] | 94 | template <class RelTy> |
| 95 | bool constantEq(ArrayRef<RelTy> RelsA, ArrayRef<RelTy> RelsB); |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 96 | |
Rui Ueyama | 9dedfb1 | 2016-11-30 01:50:03 +0000 | [diff] [blame] | 97 | template <class RelTy> |
| 98 | bool variableEq(const InputSection<ELFT> *A, ArrayRef<RelTy> RelsA, |
| 99 | const InputSection<ELFT> *B, ArrayRef<RelTy> RelsB); |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 100 | |
Rui Ueyama | 9dedfb1 | 2016-11-30 01:50:03 +0000 | [diff] [blame] | 101 | bool equalsConstant(const InputSection<ELFT> *A, const InputSection<ELFT> *B); |
| 102 | bool equalsVariable(const InputSection<ELFT> *A, const InputSection<ELFT> *B); |
| 103 | |
Rui Ueyama | 1b6bab0 | 2016-12-02 05:35:46 +0000 | [diff] [blame] | 104 | size_t findBoundary(size_t Begin, size_t End); |
Rui Ueyama | 9dedfb1 | 2016-11-30 01:50:03 +0000 | [diff] [blame] | 105 | |
Rui Ueyama | 1b6bab0 | 2016-12-02 05:35:46 +0000 | [diff] [blame] | 106 | void forEachColorRange(size_t Begin, size_t End, |
| 107 | std::function<void(size_t, size_t)> Fn); |
| 108 | |
| 109 | void forEachColor(std::function<void(size_t, size_t)> Fn); |
| 110 | |
| 111 | std::vector<InputSection<ELFT> *> Sections; |
Rui Ueyama | c183531 | 2016-12-01 17:09:04 +0000 | [diff] [blame] | 112 | int Cnt = 0; |
Rui Ueyama | 1b6bab0 | 2016-12-02 05:35:46 +0000 | [diff] [blame] | 113 | std::atomic<bool> Repeat = {false}; |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 114 | }; |
| 115 | } |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 116 | |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 117 | // Returns a hash value for S. Note that the information about |
| 118 | // relocation targets is not included in the hash value. |
Rui Ueyama | c183531 | 2016-12-01 17:09:04 +0000 | [diff] [blame] | 119 | template <class ELFT> static uint32_t getHash(InputSection<ELFT> *S) { |
Rui Ueyama | a05134e | 2016-11-19 20:15:55 +0000 | [diff] [blame] | 120 | return hash_combine(S->Flags, S->getSize(), S->NumRelocations); |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Rui Ueyama | bd1f063 | 2016-11-20 02:39:59 +0000 | [diff] [blame] | 123 | // Returns true if section S is subject of ICF. |
| 124 | template <class ELFT> static bool isEligible(InputSection<ELFT> *S) { |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 125 | // .init and .fini contains instructions that must be executed to |
| 126 | // initialize and finalize the process. They cannot and should not |
| 127 | // be merged. |
Rui Ueyama | bd1f063 | 2016-11-20 02:39:59 +0000 | [diff] [blame] | 128 | return S->Live && (S->Flags & SHF_ALLOC) && !(S->Flags & SHF_WRITE) && |
| 129 | S->Name != ".init" && S->Name != ".fini"; |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Rui Ueyama | 1b6bab0 | 2016-12-02 05:35:46 +0000 | [diff] [blame] | 132 | // Split a range into smaller ranges by recoloring sections |
| 133 | // in a given range. |
| 134 | template <class ELFT> |
| 135 | void ICF<ELFT>::segregate(size_t Begin, size_t End, bool Constant) { |
| 136 | // This loop rearranges sections in [Begin, End) so that all sections |
Rui Ueyama | 9dedfb1 | 2016-11-30 01:50:03 +0000 | [diff] [blame] | 137 | // that are equal in terms of equals{Constant,Variable} are contiguous |
Rui Ueyama | 1b6bab0 | 2016-12-02 05:35:46 +0000 | [diff] [blame] | 138 | // in [Begin, End). |
Rui Ueyama | 9dedfb1 | 2016-11-30 01:50:03 +0000 | [diff] [blame] | 139 | // |
| 140 | // The algorithm is quadratic in the worst case, but that is not an |
| 141 | // issue in practice because the number of the distinct sections in |
Rui Ueyama | 1b6bab0 | 2016-12-02 05:35:46 +0000 | [diff] [blame] | 142 | // each range is usually very small. |
Rui Ueyama | c183531 | 2016-12-01 17:09:04 +0000 | [diff] [blame] | 143 | |
Rui Ueyama | 1b6bab0 | 2016-12-02 05:35:46 +0000 | [diff] [blame] | 144 | while (Begin < End) { |
| 145 | // Divide [Begin, End) into two. Let Mid be the start index of the |
Rui Ueyama | 9dedfb1 | 2016-11-30 01:50:03 +0000 | [diff] [blame] | 146 | // second group. |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 147 | auto Bound = std::stable_partition( |
Rui Ueyama | c183531 | 2016-12-01 17:09:04 +0000 | [diff] [blame] | 148 | Sections.begin() + Begin + 1, Sections.begin() + End, |
Rui Ueyama | 9dedfb1 | 2016-11-30 01:50:03 +0000 | [diff] [blame] | 149 | [&](InputSection<ELFT> *S) { |
| 150 | if (Constant) |
Rui Ueyama | c183531 | 2016-12-01 17:09:04 +0000 | [diff] [blame] | 151 | return equalsConstant(Sections[Begin], S); |
| 152 | return equalsVariable(Sections[Begin], S); |
Rui Ueyama | 9dedfb1 | 2016-11-30 01:50:03 +0000 | [diff] [blame] | 153 | }); |
| 154 | size_t Mid = Bound - Sections.begin(); |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 155 | |
Rui Ueyama | 1b6bab0 | 2016-12-02 05:35:46 +0000 | [diff] [blame] | 156 | // Now we split [Begin, End) into [Begin, Mid) and [Mid, End) by |
| 157 | // updating the section colors in [Begin, End). We use Mid as a |
| 158 | // color ID because every group ends with a unique index. |
Rui Ueyama | c183531 | 2016-12-01 17:09:04 +0000 | [diff] [blame] | 159 | // |
Rui Ueyama | 91ae861 | 2016-12-01 19:45:22 +0000 | [diff] [blame] | 160 | // Note on Color[0] and Color[1]: we have two storages for colors. |
| 161 | // At the beginning of each iteration of the main loop, both have |
| 162 | // the same color. Color[0] contains the current color, and Color[1] |
Rui Ueyama | 1b6bab0 | 2016-12-02 05:35:46 +0000 | [diff] [blame] | 163 | // contains the next color which will be used on the next iteration. |
Rui Ueyama | c183531 | 2016-12-01 17:09:04 +0000 | [diff] [blame] | 164 | // |
| 165 | // Recall that other threads may be working on other ranges. They |
Rui Ueyama | 91ae861 | 2016-12-01 19:45:22 +0000 | [diff] [blame] | 166 | // may be reading colors that we are about to update. We cannot |
| 167 | // update colors in place because it breaks the invariance that |
| 168 | // all sections in the same group must have the same color. In |
| 169 | // other words, the following for loop is not an atomic operation, |
| 170 | // and that is observable from other threads. |
Rui Ueyama | c183531 | 2016-12-01 17:09:04 +0000 | [diff] [blame] | 171 | // |
Rui Ueyama | 91ae861 | 2016-12-01 19:45:22 +0000 | [diff] [blame] | 172 | // By writing new colors to write-only places, we can keep the invariance. |
Rui Ueyama | 1b6bab0 | 2016-12-02 05:35:46 +0000 | [diff] [blame] | 173 | for (size_t I = Begin; I < Mid; ++I) |
| 174 | Sections[I]->Color[(Cnt + 1) % 2] = Mid; |
Rui Ueyama | 9dedfb1 | 2016-11-30 01:50:03 +0000 | [diff] [blame] | 175 | |
Rui Ueyama | 1b6bab0 | 2016-12-02 05:35:46 +0000 | [diff] [blame] | 176 | // If we created a group, we need to iterate the main loop again. |
| 177 | if (Mid != End) |
| 178 | Repeat = true; |
| 179 | |
| 180 | Begin = Mid; |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
| 184 | // Compare two lists of relocations. |
Rui Ueyama | 9dedfb1 | 2016-11-30 01:50:03 +0000 | [diff] [blame] | 185 | template <class ELFT> |
| 186 | template <class RelTy> |
| 187 | bool ICF<ELFT>::constantEq(ArrayRef<RelTy> RelsA, ArrayRef<RelTy> RelsB) { |
Rui Ueyama | a05134e | 2016-11-19 20:15:55 +0000 | [diff] [blame] | 188 | auto Eq = [](const RelTy &A, const RelTy &B) { |
| 189 | return A.r_offset == B.r_offset && |
| 190 | A.getType(Config->Mips64EL) == B.getType(Config->Mips64EL) && |
| 191 | getAddend<ELFT>(A) == getAddend<ELFT>(B); |
| 192 | }; |
| 193 | |
| 194 | return RelsA.size() == RelsB.size() && |
| 195 | std::equal(RelsA.begin(), RelsA.end(), RelsB.begin(), Eq); |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | // Compare "non-moving" part of two InputSections, namely everything |
| 199 | // except relocation targets. |
| 200 | template <class ELFT> |
Rui Ueyama | 9dedfb1 | 2016-11-30 01:50:03 +0000 | [diff] [blame] | 201 | bool ICF<ELFT>::equalsConstant(const InputSection<ELFT> *A, |
| 202 | const InputSection<ELFT> *B) { |
Rui Ueyama | bd1f063 | 2016-11-20 02:39:59 +0000 | [diff] [blame] | 203 | if (A->NumRelocations != B->NumRelocations || A->Flags != B->Flags || |
| 204 | A->getSize() != B->getSize() || A->Data != B->Data) |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 205 | return false; |
| 206 | |
Rui Ueyama | bd1f063 | 2016-11-20 02:39:59 +0000 | [diff] [blame] | 207 | if (A->AreRelocsRela) |
Rui Ueyama | 9dedfb1 | 2016-11-30 01:50:03 +0000 | [diff] [blame] | 208 | return constantEq(A->relas(), B->relas()); |
| 209 | return constantEq(A->rels(), B->rels()); |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Rui Ueyama | 7bed9ee | 2016-11-20 23:15:54 +0000 | [diff] [blame] | 212 | // Compare two lists of relocations. Returns true if all pairs of |
| 213 | // relocations point to the same section in terms of ICF. |
Rui Ueyama | 9dedfb1 | 2016-11-30 01:50:03 +0000 | [diff] [blame] | 214 | template <class ELFT> |
| 215 | template <class RelTy> |
| 216 | bool ICF<ELFT>::variableEq(const InputSection<ELFT> *A, ArrayRef<RelTy> RelsA, |
| 217 | const InputSection<ELFT> *B, ArrayRef<RelTy> RelsB) { |
Rui Ueyama | a05134e | 2016-11-19 20:15:55 +0000 | [diff] [blame] | 218 | auto Eq = [&](const RelTy &RA, const RelTy &RB) { |
Rui Ueyama | 91ae861 | 2016-12-01 19:45:22 +0000 | [diff] [blame] | 219 | // The two sections must be identical. |
Rui Ueyama | bd1f063 | 2016-11-20 02:39:59 +0000 | [diff] [blame] | 220 | SymbolBody &SA = A->getFile()->getRelocTargetSym(RA); |
| 221 | SymbolBody &SB = B->getFile()->getRelocTargetSym(RB); |
Rafael Espindola | 67d72c0 | 2016-03-11 12:06:30 +0000 | [diff] [blame] | 222 | if (&SA == &SB) |
Rui Ueyama | a05134e | 2016-11-19 20:15:55 +0000 | [diff] [blame] | 223 | return true; |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 224 | |
Rui Ueyama | 91ae861 | 2016-12-01 19:45:22 +0000 | [diff] [blame] | 225 | // Or, the two sections must have the same color. |
Rafael Espindola | 67d72c0 | 2016-03-11 12:06:30 +0000 | [diff] [blame] | 226 | auto *DA = dyn_cast<DefinedRegular<ELFT>>(&SA); |
| 227 | auto *DB = dyn_cast<DefinedRegular<ELFT>>(&SB); |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 228 | if (!DA || !DB) |
| 229 | return false; |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 230 | if (DA->Value != DB->Value) |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 231 | return false; |
Rui Ueyama | 9dedfb1 | 2016-11-30 01:50:03 +0000 | [diff] [blame] | 232 | |
Rui Ueyama | 9f8cb73 | 2016-11-20 02:43:44 +0000 | [diff] [blame] | 233 | auto *X = dyn_cast<InputSection<ELFT>>(DA->Section); |
| 234 | auto *Y = dyn_cast<InputSection<ELFT>>(DB->Section); |
Rui Ueyama | 9dedfb1 | 2016-11-30 01:50:03 +0000 | [diff] [blame] | 235 | if (!X || !Y) |
| 236 | return false; |
Rui Ueyama | c183531 | 2016-12-01 17:09:04 +0000 | [diff] [blame] | 237 | |
Rui Ueyama | 83ec681 | 2016-12-02 17:23:58 +0000 | [diff] [blame] | 238 | // Ineligible sections have the special color 0. |
| 239 | // They can never be the same in terms of section colors. |
Rui Ueyama | 5419861 | 2016-12-02 18:40:43 +0000 | [diff] [blame] | 240 | if (X->Color[Cnt % 2] == 0) |
Rui Ueyama | 83ec681 | 2016-12-02 17:23:58 +0000 | [diff] [blame] | 241 | return false; |
Rui Ueyama | a6cd5fe | 2016-12-01 21:41:06 +0000 | [diff] [blame] | 242 | |
Rui Ueyama | 5419861 | 2016-12-02 18:40:43 +0000 | [diff] [blame] | 243 | return X->Color[Cnt % 2] == Y->Color[Cnt % 2]; |
Rui Ueyama | a05134e | 2016-11-19 20:15:55 +0000 | [diff] [blame] | 244 | }; |
| 245 | |
| 246 | return std::equal(RelsA.begin(), RelsA.end(), RelsB.begin(), Eq); |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | // Compare "moving" part of two InputSections, namely relocation targets. |
| 250 | template <class ELFT> |
Rui Ueyama | 9dedfb1 | 2016-11-30 01:50:03 +0000 | [diff] [blame] | 251 | bool ICF<ELFT>::equalsVariable(const InputSection<ELFT> *A, |
| 252 | const InputSection<ELFT> *B) { |
Rafael Espindola | 9f0c4bb | 2016-11-10 14:53:24 +0000 | [diff] [blame] | 253 | if (A->AreRelocsRela) |
Rui Ueyama | a05134e | 2016-11-19 20:15:55 +0000 | [diff] [blame] | 254 | return variableEq(A, A->relas(), B, B->relas()); |
| 255 | return variableEq(A, A->rels(), B, B->rels()); |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Rui Ueyama | 1b6bab0 | 2016-12-02 05:35:46 +0000 | [diff] [blame] | 258 | template <class ELFT> size_t ICF<ELFT>::findBoundary(size_t Begin, size_t End) { |
| 259 | for (size_t I = Begin + 1; I < End; ++I) |
| 260 | if (Sections[Begin]->Color[Cnt % 2] != Sections[I]->Color[Cnt % 2]) |
| 261 | return I; |
| 262 | return End; |
| 263 | } |
| 264 | |
| 265 | // Sections in the same color are contiguous in Sections vector. |
| 266 | // Therefore, Sections vector can be considered as contiguous groups |
| 267 | // of sections, grouped by colors. |
| 268 | // |
| 269 | // This function calls Fn on every group that starts within [Begin, End). |
| 270 | // Note that a group must starts in that range but doesn't necessarily |
| 271 | // have to end before End. |
| 272 | template <class ELFT> |
| 273 | void ICF<ELFT>::forEachColorRange(size_t Begin, size_t End, |
| 274 | std::function<void(size_t, size_t)> Fn) { |
| 275 | if (Begin > 0) |
| 276 | Begin = findBoundary(Begin - 1, End); |
| 277 | |
| 278 | while (Begin < End) { |
| 279 | size_t Mid = findBoundary(Begin, Sections.size()); |
| 280 | Fn(Begin, Mid); |
| 281 | Begin = Mid; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | // Call Fn on each color group. |
| 286 | template <class ELFT> |
| 287 | void ICF<ELFT>::forEachColor(std::function<void(size_t, size_t)> Fn) { |
| 288 | // If threading is disabled or the number of sections are |
| 289 | // too small to use threading, call Fn sequentially. |
| 290 | if (!Config->Threads || Sections.size() < 1024) { |
| 291 | forEachColorRange(0, Sections.size(), Fn); |
| 292 | return; |
| 293 | } |
| 294 | |
| 295 | // Split sections into 256 shards and call Fn in parallel. |
| 296 | size_t NumShards = 256; |
| 297 | size_t Step = Sections.size() / NumShards; |
| 298 | parallel_for(size_t(0), NumShards, [&](size_t I) { |
| 299 | forEachColorRange(I * Step, (I + 1) * Step, Fn); |
| 300 | }); |
| 301 | forEachColorRange(Step * NumShards, Sections.size(), Fn); |
Rui Ueyama | c183531 | 2016-12-01 17:09:04 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 304 | // The main function of ICF. |
Rui Ueyama | 4f8d21f | 2016-05-02 19:30:42 +0000 | [diff] [blame] | 305 | template <class ELFT> void ICF<ELFT>::run() { |
Rui Ueyama | 9dedfb1 | 2016-11-30 01:50:03 +0000 | [diff] [blame] | 306 | // Collect sections to merge. |
| 307 | for (InputSectionBase<ELFT> *Sec : Symtab<ELFT>::X->Sections) |
| 308 | if (auto *S = dyn_cast<InputSection<ELFT>>(Sec)) |
| 309 | if (isEligible(S)) |
| 310 | Sections.push_back(S); |
| 311 | |
Rui Ueyama | 91ae861 | 2016-12-01 19:45:22 +0000 | [diff] [blame] | 312 | // Initially, we use hash values to color sections. Therefore, if |
| 313 | // two sections have the same color, they are likely (but not |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 314 | // guaranteed) to have the same static contents in terms of ICF. |
Rui Ueyama | e2dfbc1 | 2016-11-19 23:14:23 +0000 | [diff] [blame] | 315 | for (InputSection<ELFT> *S : Sections) |
Rui Ueyama | 91ae861 | 2016-12-01 19:45:22 +0000 | [diff] [blame] | 316 | // Set MSB to 1 to avoid collisions with non-hash colors. |
Rui Ueyama | 1b6bab0 | 2016-12-02 05:35:46 +0000 | [diff] [blame] | 317 | S->Color[0] = getHash(S) | (1 << 31); |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 318 | |
Rui Ueyama | 7bed9ee | 2016-11-20 23:15:54 +0000 | [diff] [blame] | 319 | // From now on, sections in Sections are ordered so that sections in |
Rui Ueyama | 91ae861 | 2016-12-01 19:45:22 +0000 | [diff] [blame] | 320 | // the same color are consecutive in the vector. |
Rui Ueyama | e2dfbc1 | 2016-11-19 23:14:23 +0000 | [diff] [blame] | 321 | std::stable_sort(Sections.begin(), Sections.end(), |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 322 | [](InputSection<ELFT> *A, InputSection<ELFT> *B) { |
Rui Ueyama | 91ae861 | 2016-12-01 19:45:22 +0000 | [diff] [blame] | 323 | if (A->Color[0] != B->Color[0]) |
| 324 | return A->Color[0] < B->Color[0]; |
Petr Hosek | 901948a | 2016-08-22 18:53:09 +0000 | [diff] [blame] | 325 | // Within a group, put the highest alignment |
| 326 | // requirement first, so that's the one we'll keep. |
| 327 | return B->Alignment < A->Alignment; |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 328 | }); |
| 329 | |
Rui Ueyama | 9dedfb1 | 2016-11-30 01:50:03 +0000 | [diff] [blame] | 330 | // Compare static contents and assign unique IDs for each static content. |
Rui Ueyama | 1b6bab0 | 2016-12-02 05:35:46 +0000 | [diff] [blame] | 331 | forEachColor([&](size_t Begin, size_t End) { segregate(Begin, End, true); }); |
Rui Ueyama | 9dedfb1 | 2016-11-30 01:50:03 +0000 | [diff] [blame] | 332 | ++Cnt; |
| 333 | |
Rui Ueyama | 1b6bab0 | 2016-12-02 05:35:46 +0000 | [diff] [blame] | 334 | // Split groups by comparing relocations until convergence is obtained. |
| 335 | do { |
| 336 | Repeat = false; |
| 337 | forEachColor( |
| 338 | [&](size_t Begin, size_t End) { segregate(Begin, End, false); }); |
Rui Ueyama | 9dedfb1 | 2016-11-30 01:50:03 +0000 | [diff] [blame] | 339 | ++Cnt; |
Rui Ueyama | 1b6bab0 | 2016-12-02 05:35:46 +0000 | [diff] [blame] | 340 | } while (Repeat); |
Rui Ueyama | 9dedfb1 | 2016-11-30 01:50:03 +0000 | [diff] [blame] | 341 | |
| 342 | log("ICF needed " + Twine(Cnt) + " iterations"); |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 343 | |
Rui Ueyama | 91ae861 | 2016-12-01 19:45:22 +0000 | [diff] [blame] | 344 | // Merge sections in the same colors. |
Rui Ueyama | 1b6bab0 | 2016-12-02 05:35:46 +0000 | [diff] [blame] | 345 | forEachColor([&](size_t Begin, size_t End) { |
| 346 | if (End - Begin == 1) |
| 347 | return; |
Rui Ueyama | 9dedfb1 | 2016-11-30 01:50:03 +0000 | [diff] [blame] | 348 | |
Rui Ueyama | 1b6bab0 | 2016-12-02 05:35:46 +0000 | [diff] [blame] | 349 | log("selected " + Sections[Begin]->Name); |
| 350 | for (size_t I = Begin + 1; I < End; ++I) { |
Rui Ueyama | 9dedfb1 | 2016-11-30 01:50:03 +0000 | [diff] [blame] | 351 | log(" removed " + Sections[I]->Name); |
Rui Ueyama | 1b6bab0 | 2016-12-02 05:35:46 +0000 | [diff] [blame] | 352 | Sections[Begin]->replace(Sections[I]); |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 353 | } |
Rui Ueyama | 1b6bab0 | 2016-12-02 05:35:46 +0000 | [diff] [blame] | 354 | }); |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | // ICF entry point function. |
Rui Ueyama | 4f8d21f | 2016-05-02 19:30:42 +0000 | [diff] [blame] | 358 | template <class ELFT> void elf::doIcf() { ICF<ELFT>().run(); } |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 359 | |
Rui Ueyama | 4f8d21f | 2016-05-02 19:30:42 +0000 | [diff] [blame] | 360 | template void elf::doIcf<ELF32LE>(); |
| 361 | template void elf::doIcf<ELF32BE>(); |
| 362 | template void elf::doIcf<ELF64LE>(); |
| 363 | template void elf::doIcf<ELF64BE>(); |