xref: /freebsd/contrib/llvm-project/lld/MachO/ICF.cpp (revision fe6060f10f634930ff71b7c50291ddc610da2475)
1*fe6060f1SDimitry Andric //===- ICF.cpp ------------------------------------------------------------===//
2*fe6060f1SDimitry Andric //
3*fe6060f1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*fe6060f1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*fe6060f1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*fe6060f1SDimitry Andric //
7*fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
8*fe6060f1SDimitry Andric 
9*fe6060f1SDimitry Andric #include "ICF.h"
10*fe6060f1SDimitry Andric #include "ConcatOutputSection.h"
11*fe6060f1SDimitry Andric #include "InputSection.h"
12*fe6060f1SDimitry Andric #include "Symbols.h"
13*fe6060f1SDimitry Andric #include "UnwindInfoSection.h"
14*fe6060f1SDimitry Andric 
15*fe6060f1SDimitry Andric #include "llvm/Support/Parallel.h"
16*fe6060f1SDimitry Andric #include "llvm/Support/TimeProfiler.h"
17*fe6060f1SDimitry Andric 
18*fe6060f1SDimitry Andric #include <atomic>
19*fe6060f1SDimitry Andric 
20*fe6060f1SDimitry Andric using namespace llvm;
21*fe6060f1SDimitry Andric using namespace lld;
22*fe6060f1SDimitry Andric using namespace lld::macho;
23*fe6060f1SDimitry Andric 
24*fe6060f1SDimitry Andric class ICF {
25*fe6060f1SDimitry Andric public:
26*fe6060f1SDimitry Andric   ICF(std::vector<ConcatInputSection *> &inputs);
27*fe6060f1SDimitry Andric 
28*fe6060f1SDimitry Andric   void run();
29*fe6060f1SDimitry Andric   void segregate(size_t begin, size_t end,
30*fe6060f1SDimitry Andric                  std::function<bool(const ConcatInputSection *,
31*fe6060f1SDimitry Andric                                     const ConcatInputSection *)>
32*fe6060f1SDimitry Andric                      equals);
33*fe6060f1SDimitry Andric   size_t findBoundary(size_t begin, size_t end);
34*fe6060f1SDimitry Andric   void forEachClassRange(size_t begin, size_t end,
35*fe6060f1SDimitry Andric                          std::function<void(size_t, size_t)> func);
36*fe6060f1SDimitry Andric   void forEachClass(std::function<void(size_t, size_t)> func);
37*fe6060f1SDimitry Andric 
38*fe6060f1SDimitry Andric   // ICF needs a copy of the inputs vector because its equivalence-class
39*fe6060f1SDimitry Andric   // segregation algorithm destroys the proper sequence.
40*fe6060f1SDimitry Andric   std::vector<ConcatInputSection *> icfInputs;
41*fe6060f1SDimitry Andric };
42*fe6060f1SDimitry Andric 
43*fe6060f1SDimitry Andric ICF::ICF(std::vector<ConcatInputSection *> &inputs) {
44*fe6060f1SDimitry Andric   icfInputs.assign(inputs.begin(), inputs.end());
45*fe6060f1SDimitry Andric }
46*fe6060f1SDimitry Andric 
47*fe6060f1SDimitry Andric // ICF = Identical Code Folding
48*fe6060f1SDimitry Andric //
49*fe6060f1SDimitry Andric // We only fold __TEXT,__text, so this is really "code" folding, and not
50*fe6060f1SDimitry Andric // "COMDAT" folding. String and scalar constant literals are deduplicated
51*fe6060f1SDimitry Andric // elsewhere.
52*fe6060f1SDimitry Andric //
53*fe6060f1SDimitry Andric // Summary of segments & sections:
54*fe6060f1SDimitry Andric //
55*fe6060f1SDimitry Andric // The __TEXT segment is readonly at the MMU. Some sections are already
56*fe6060f1SDimitry Andric // deduplicated elsewhere (__TEXT,__cstring & __TEXT,__literal*) and some are
57*fe6060f1SDimitry Andric // synthetic and inherently free of duplicates (__TEXT,__stubs &
58*fe6060f1SDimitry Andric // __TEXT,__unwind_info). Note that we don't yet run ICF on __TEXT,__const,
59*fe6060f1SDimitry Andric // because doing so induces many test failures.
60*fe6060f1SDimitry Andric //
61*fe6060f1SDimitry Andric // The __LINKEDIT segment is readonly at the MMU, yet entirely synthetic, and
62*fe6060f1SDimitry Andric // thus ineligible for ICF.
63*fe6060f1SDimitry Andric //
64*fe6060f1SDimitry Andric // The __DATA_CONST segment is read/write at the MMU, but is logically const to
65*fe6060f1SDimitry Andric // the application after dyld applies fixups to pointer data. We currently
66*fe6060f1SDimitry Andric // fold only the __DATA_CONST,__cfstring section.
67*fe6060f1SDimitry Andric //
68*fe6060f1SDimitry Andric // The __DATA segment is read/write at the MMU, and as application-writeable
69*fe6060f1SDimitry Andric // data, none of its sections are eligible for ICF.
70*fe6060f1SDimitry Andric //
71*fe6060f1SDimitry Andric // Please see the large block comment in lld/ELF/ICF.cpp for an explanation
72*fe6060f1SDimitry Andric // of the segregation algorithm.
73*fe6060f1SDimitry Andric //
74*fe6060f1SDimitry Andric // FIXME(gkm): implement keep-unique attributes
75*fe6060f1SDimitry Andric // FIXME(gkm): implement address-significance tables for MachO object files
76*fe6060f1SDimitry Andric 
77*fe6060f1SDimitry Andric static unsigned icfPass = 0;
78*fe6060f1SDimitry Andric static std::atomic<bool> icfRepeat{false};
79*fe6060f1SDimitry Andric 
80*fe6060f1SDimitry Andric // Compare "non-moving" parts of two ConcatInputSections, namely everything
81*fe6060f1SDimitry Andric // except references to other ConcatInputSections.
82*fe6060f1SDimitry Andric static bool equalsConstant(const ConcatInputSection *ia,
83*fe6060f1SDimitry Andric                            const ConcatInputSection *ib) {
84*fe6060f1SDimitry Andric   // We can only fold within the same OutputSection.
85*fe6060f1SDimitry Andric   if (ia->parent != ib->parent)
86*fe6060f1SDimitry Andric     return false;
87*fe6060f1SDimitry Andric   if (ia->data.size() != ib->data.size())
88*fe6060f1SDimitry Andric     return false;
89*fe6060f1SDimitry Andric   if (ia->data != ib->data)
90*fe6060f1SDimitry Andric     return false;
91*fe6060f1SDimitry Andric   if (ia->relocs.size() != ib->relocs.size())
92*fe6060f1SDimitry Andric     return false;
93*fe6060f1SDimitry Andric   auto f = [](const Reloc &ra, const Reloc &rb) {
94*fe6060f1SDimitry Andric     if (ra.type != rb.type)
95*fe6060f1SDimitry Andric       return false;
96*fe6060f1SDimitry Andric     if (ra.pcrel != rb.pcrel)
97*fe6060f1SDimitry Andric       return false;
98*fe6060f1SDimitry Andric     if (ra.length != rb.length)
99*fe6060f1SDimitry Andric       return false;
100*fe6060f1SDimitry Andric     if (ra.offset != rb.offset)
101*fe6060f1SDimitry Andric       return false;
102*fe6060f1SDimitry Andric     if (ra.addend != rb.addend)
103*fe6060f1SDimitry Andric       return false;
104*fe6060f1SDimitry Andric     if (ra.referent.is<Symbol *>() != rb.referent.is<Symbol *>())
105*fe6060f1SDimitry Andric       return false;
106*fe6060f1SDimitry Andric 
107*fe6060f1SDimitry Andric     InputSection *isecA, *isecB;
108*fe6060f1SDimitry Andric     if (ra.referent.is<Symbol *>()) {
109*fe6060f1SDimitry Andric       const auto *sa = ra.referent.get<Symbol *>();
110*fe6060f1SDimitry Andric       const auto *sb = rb.referent.get<Symbol *>();
111*fe6060f1SDimitry Andric       if (sa->kind() != sb->kind())
112*fe6060f1SDimitry Andric         return false;
113*fe6060f1SDimitry Andric       if (isa<Defined>(sa)) {
114*fe6060f1SDimitry Andric         const auto *da = cast<Defined>(sa);
115*fe6060f1SDimitry Andric         const auto *db = cast<Defined>(sb);
116*fe6060f1SDimitry Andric         if (da->isec && db->isec) {
117*fe6060f1SDimitry Andric           isecA = da->isec;
118*fe6060f1SDimitry Andric           isecB = db->isec;
119*fe6060f1SDimitry Andric         } else {
120*fe6060f1SDimitry Andric           assert(da->isAbsolute() && db->isAbsolute());
121*fe6060f1SDimitry Andric           return da->value == db->value;
122*fe6060f1SDimitry Andric         }
123*fe6060f1SDimitry Andric       } else {
124*fe6060f1SDimitry Andric         assert(isa<DylibSymbol>(sa));
125*fe6060f1SDimitry Andric         return sa == sb;
126*fe6060f1SDimitry Andric       }
127*fe6060f1SDimitry Andric     } else {
128*fe6060f1SDimitry Andric       isecA = ra.referent.get<InputSection *>();
129*fe6060f1SDimitry Andric       isecB = rb.referent.get<InputSection *>();
130*fe6060f1SDimitry Andric     }
131*fe6060f1SDimitry Andric 
132*fe6060f1SDimitry Andric     if (isecA->parent != isecB->parent)
133*fe6060f1SDimitry Andric       return false;
134*fe6060f1SDimitry Andric     // Sections with identical parents should be of the same kind.
135*fe6060f1SDimitry Andric     assert(isecA->kind() == isecB->kind());
136*fe6060f1SDimitry Andric     // We will compare ConcatInputSection contents in equalsVariable.
137*fe6060f1SDimitry Andric     if (isa<ConcatInputSection>(isecA))
138*fe6060f1SDimitry Andric       return true;
139*fe6060f1SDimitry Andric     // Else we have two literal sections. References to them are equal iff their
140*fe6060f1SDimitry Andric     // offsets in the output section are equal.
141*fe6060f1SDimitry Andric     return isecA->getOffset(ra.addend) == isecB->getOffset(rb.addend);
142*fe6060f1SDimitry Andric   };
143*fe6060f1SDimitry Andric   return std::equal(ia->relocs.begin(), ia->relocs.end(), ib->relocs.begin(),
144*fe6060f1SDimitry Andric                     f);
145*fe6060f1SDimitry Andric }
146*fe6060f1SDimitry Andric 
147*fe6060f1SDimitry Andric // Compare the "moving" parts of two ConcatInputSections -- i.e. everything not
148*fe6060f1SDimitry Andric // handled by equalsConstant().
149*fe6060f1SDimitry Andric static bool equalsVariable(const ConcatInputSection *ia,
150*fe6060f1SDimitry Andric                            const ConcatInputSection *ib) {
151*fe6060f1SDimitry Andric   assert(ia->relocs.size() == ib->relocs.size());
152*fe6060f1SDimitry Andric   auto f = [](const Reloc &ra, const Reloc &rb) {
153*fe6060f1SDimitry Andric     // We already filtered out mismatching values/addends in equalsConstant.
154*fe6060f1SDimitry Andric     if (ra.referent == rb.referent)
155*fe6060f1SDimitry Andric       return true;
156*fe6060f1SDimitry Andric     const ConcatInputSection *isecA, *isecB;
157*fe6060f1SDimitry Andric     if (ra.referent.is<Symbol *>()) {
158*fe6060f1SDimitry Andric       // Matching DylibSymbols are already filtered out by the
159*fe6060f1SDimitry Andric       // identical-referent check above. Non-matching DylibSymbols were filtered
160*fe6060f1SDimitry Andric       // out in equalsConstant(). So we can safely cast to Defined here.
161*fe6060f1SDimitry Andric       const auto *da = cast<Defined>(ra.referent.get<Symbol *>());
162*fe6060f1SDimitry Andric       const auto *db = cast<Defined>(rb.referent.get<Symbol *>());
163*fe6060f1SDimitry Andric       if (da->isAbsolute())
164*fe6060f1SDimitry Andric         return true;
165*fe6060f1SDimitry Andric       isecA = dyn_cast<ConcatInputSection>(da->isec);
166*fe6060f1SDimitry Andric       if (!isecA)
167*fe6060f1SDimitry Andric         return true; // literal sections were checked in equalsConstant.
168*fe6060f1SDimitry Andric       isecB = cast<ConcatInputSection>(db->isec);
169*fe6060f1SDimitry Andric     } else {
170*fe6060f1SDimitry Andric       const auto *sa = ra.referent.get<InputSection *>();
171*fe6060f1SDimitry Andric       const auto *sb = rb.referent.get<InputSection *>();
172*fe6060f1SDimitry Andric       isecA = dyn_cast<ConcatInputSection>(sa);
173*fe6060f1SDimitry Andric       if (!isecA)
174*fe6060f1SDimitry Andric         return true;
175*fe6060f1SDimitry Andric       isecB = cast<ConcatInputSection>(sb);
176*fe6060f1SDimitry Andric     }
177*fe6060f1SDimitry Andric     return isecA->icfEqClass[icfPass % 2] == isecB->icfEqClass[icfPass % 2];
178*fe6060f1SDimitry Andric   };
179*fe6060f1SDimitry Andric   return std::equal(ia->relocs.begin(), ia->relocs.end(), ib->relocs.begin(),
180*fe6060f1SDimitry Andric                     f);
181*fe6060f1SDimitry Andric }
182*fe6060f1SDimitry Andric 
183*fe6060f1SDimitry Andric // Find the first InputSection after BEGIN whose equivalence class differs
184*fe6060f1SDimitry Andric size_t ICF::findBoundary(size_t begin, size_t end) {
185*fe6060f1SDimitry Andric   uint64_t beginHash = icfInputs[begin]->icfEqClass[icfPass % 2];
186*fe6060f1SDimitry Andric   for (size_t i = begin + 1; i < end; ++i)
187*fe6060f1SDimitry Andric     if (beginHash != icfInputs[i]->icfEqClass[icfPass % 2])
188*fe6060f1SDimitry Andric       return i;
189*fe6060f1SDimitry Andric   return end;
190*fe6060f1SDimitry Andric }
191*fe6060f1SDimitry Andric 
192*fe6060f1SDimitry Andric // Invoke FUNC on subranges with matching equivalence class
193*fe6060f1SDimitry Andric void ICF::forEachClassRange(size_t begin, size_t end,
194*fe6060f1SDimitry Andric                             std::function<void(size_t, size_t)> func) {
195*fe6060f1SDimitry Andric   while (begin < end) {
196*fe6060f1SDimitry Andric     size_t mid = findBoundary(begin, end);
197*fe6060f1SDimitry Andric     func(begin, mid);
198*fe6060f1SDimitry Andric     begin = mid;
199*fe6060f1SDimitry Andric   }
200*fe6060f1SDimitry Andric }
201*fe6060f1SDimitry Andric 
202*fe6060f1SDimitry Andric // Split icfInputs into shards, then parallelize invocation of FUNC on subranges
203*fe6060f1SDimitry Andric // with matching equivalence class
204*fe6060f1SDimitry Andric void ICF::forEachClass(std::function<void(size_t, size_t)> func) {
205*fe6060f1SDimitry Andric   // Only use threads when the benefits outweigh the overhead.
206*fe6060f1SDimitry Andric   const size_t threadingThreshold = 1024;
207*fe6060f1SDimitry Andric   if (icfInputs.size() < threadingThreshold) {
208*fe6060f1SDimitry Andric     forEachClassRange(0, icfInputs.size(), func);
209*fe6060f1SDimitry Andric     ++icfPass;
210*fe6060f1SDimitry Andric     return;
211*fe6060f1SDimitry Andric   }
212*fe6060f1SDimitry Andric 
213*fe6060f1SDimitry Andric   // Shard into non-overlapping intervals, and call FUNC in parallel.  The
214*fe6060f1SDimitry Andric   // sharding must be completed before any calls to FUNC are made so that FUNC
215*fe6060f1SDimitry Andric   // can modify the InputSection in its shard without causing data races.
216*fe6060f1SDimitry Andric   const size_t shards = 256;
217*fe6060f1SDimitry Andric   size_t step = icfInputs.size() / shards;
218*fe6060f1SDimitry Andric   size_t boundaries[shards + 1];
219*fe6060f1SDimitry Andric   boundaries[0] = 0;
220*fe6060f1SDimitry Andric   boundaries[shards] = icfInputs.size();
221*fe6060f1SDimitry Andric   parallelForEachN(1, shards, [&](size_t i) {
222*fe6060f1SDimitry Andric     boundaries[i] = findBoundary((i - 1) * step, icfInputs.size());
223*fe6060f1SDimitry Andric   });
224*fe6060f1SDimitry Andric   parallelForEachN(1, shards + 1, [&](size_t i) {
225*fe6060f1SDimitry Andric     if (boundaries[i - 1] < boundaries[i]) {
226*fe6060f1SDimitry Andric       forEachClassRange(boundaries[i - 1], boundaries[i], func);
227*fe6060f1SDimitry Andric     }
228*fe6060f1SDimitry Andric   });
229*fe6060f1SDimitry Andric   ++icfPass;
230*fe6060f1SDimitry Andric }
231*fe6060f1SDimitry Andric 
232*fe6060f1SDimitry Andric void ICF::run() {
233*fe6060f1SDimitry Andric   // Into each origin-section hash, combine all reloc referent section hashes.
234*fe6060f1SDimitry Andric   for (icfPass = 0; icfPass < 2; ++icfPass) {
235*fe6060f1SDimitry Andric     parallelForEach(icfInputs, [&](ConcatInputSection *isec) {
236*fe6060f1SDimitry Andric       uint64_t hash = isec->icfEqClass[icfPass % 2];
237*fe6060f1SDimitry Andric       for (const Reloc &r : isec->relocs) {
238*fe6060f1SDimitry Andric         if (auto *sym = r.referent.dyn_cast<Symbol *>()) {
239*fe6060f1SDimitry Andric           if (auto *dylibSym = dyn_cast<DylibSymbol>(sym))
240*fe6060f1SDimitry Andric             hash += dylibSym->stubsHelperIndex;
241*fe6060f1SDimitry Andric           else if (auto *defined = dyn_cast<Defined>(sym)) {
242*fe6060f1SDimitry Andric             if (defined->isec) {
243*fe6060f1SDimitry Andric               if (auto isec = dyn_cast<ConcatInputSection>(defined->isec))
244*fe6060f1SDimitry Andric                 hash += defined->value + isec->icfEqClass[icfPass % 2];
245*fe6060f1SDimitry Andric               else
246*fe6060f1SDimitry Andric                 hash += defined->isec->kind() +
247*fe6060f1SDimitry Andric                         defined->isec->getOffset(defined->value);
248*fe6060f1SDimitry Andric             } else {
249*fe6060f1SDimitry Andric               hash += defined->value;
250*fe6060f1SDimitry Andric             }
251*fe6060f1SDimitry Andric           } else
252*fe6060f1SDimitry Andric             llvm_unreachable("foldIdenticalSections symbol kind");
253*fe6060f1SDimitry Andric         }
254*fe6060f1SDimitry Andric       }
255*fe6060f1SDimitry Andric       // Set MSB to 1 to avoid collisions with non-hashed classes.
256*fe6060f1SDimitry Andric       isec->icfEqClass[(icfPass + 1) % 2] = hash | (1ull << 63);
257*fe6060f1SDimitry Andric     });
258*fe6060f1SDimitry Andric   }
259*fe6060f1SDimitry Andric 
260*fe6060f1SDimitry Andric   llvm::stable_sort(
261*fe6060f1SDimitry Andric       icfInputs, [](const ConcatInputSection *a, const ConcatInputSection *b) {
262*fe6060f1SDimitry Andric         return a->icfEqClass[0] < b->icfEqClass[0];
263*fe6060f1SDimitry Andric       });
264*fe6060f1SDimitry Andric   forEachClass(
265*fe6060f1SDimitry Andric       [&](size_t begin, size_t end) { segregate(begin, end, equalsConstant); });
266*fe6060f1SDimitry Andric 
267*fe6060f1SDimitry Andric   // Split equivalence groups by comparing relocations until convergence
268*fe6060f1SDimitry Andric   do {
269*fe6060f1SDimitry Andric     icfRepeat = false;
270*fe6060f1SDimitry Andric     forEachClass([&](size_t begin, size_t end) {
271*fe6060f1SDimitry Andric       segregate(begin, end, equalsVariable);
272*fe6060f1SDimitry Andric     });
273*fe6060f1SDimitry Andric   } while (icfRepeat);
274*fe6060f1SDimitry Andric   log("ICF needed " + Twine(icfPass) + " iterations");
275*fe6060f1SDimitry Andric 
276*fe6060f1SDimitry Andric   // Fold sections within equivalence classes
277*fe6060f1SDimitry Andric   forEachClass([&](size_t begin, size_t end) {
278*fe6060f1SDimitry Andric     if (end - begin < 2)
279*fe6060f1SDimitry Andric       return;
280*fe6060f1SDimitry Andric     ConcatInputSection *beginIsec = icfInputs[begin];
281*fe6060f1SDimitry Andric     for (size_t i = begin + 1; i < end; ++i)
282*fe6060f1SDimitry Andric       beginIsec->foldIdentical(icfInputs[i]);
283*fe6060f1SDimitry Andric   });
284*fe6060f1SDimitry Andric }
285*fe6060f1SDimitry Andric 
286*fe6060f1SDimitry Andric // Split an equivalence class into smaller classes.
287*fe6060f1SDimitry Andric void ICF::segregate(
288*fe6060f1SDimitry Andric     size_t begin, size_t end,
289*fe6060f1SDimitry Andric     std::function<bool(const ConcatInputSection *, const ConcatInputSection *)>
290*fe6060f1SDimitry Andric         equals) {
291*fe6060f1SDimitry Andric   while (begin < end) {
292*fe6060f1SDimitry Andric     // Divide [begin, end) into two. Let mid be the start index of the
293*fe6060f1SDimitry Andric     // second group.
294*fe6060f1SDimitry Andric     auto bound = std::stable_partition(icfInputs.begin() + begin + 1,
295*fe6060f1SDimitry Andric                                        icfInputs.begin() + end,
296*fe6060f1SDimitry Andric                                        [&](ConcatInputSection *isec) {
297*fe6060f1SDimitry Andric                                          return equals(icfInputs[begin], isec);
298*fe6060f1SDimitry Andric                                        });
299*fe6060f1SDimitry Andric     size_t mid = bound - icfInputs.begin();
300*fe6060f1SDimitry Andric 
301*fe6060f1SDimitry Andric     // Split [begin, end) into [begin, mid) and [mid, end). We use mid as an
302*fe6060f1SDimitry Andric     // equivalence class ID because every group ends with a unique index.
303*fe6060f1SDimitry Andric     for (size_t i = begin; i < mid; ++i)
304*fe6060f1SDimitry Andric       icfInputs[i]->icfEqClass[(icfPass + 1) % 2] = mid;
305*fe6060f1SDimitry Andric 
306*fe6060f1SDimitry Andric     // If we created a group, we need to iterate the main loop again.
307*fe6060f1SDimitry Andric     if (mid != end)
308*fe6060f1SDimitry Andric       icfRepeat = true;
309*fe6060f1SDimitry Andric 
310*fe6060f1SDimitry Andric     begin = mid;
311*fe6060f1SDimitry Andric   }
312*fe6060f1SDimitry Andric }
313*fe6060f1SDimitry Andric 
314*fe6060f1SDimitry Andric template <class Ptr>
315*fe6060f1SDimitry Andric DenseSet<const InputSection *> findFunctionsWithUnwindInfo() {
316*fe6060f1SDimitry Andric   DenseSet<const InputSection *> result;
317*fe6060f1SDimitry Andric   for (ConcatInputSection *isec : in.unwindInfo->getInputs()) {
318*fe6060f1SDimitry Andric     for (size_t i = 0; i < isec->relocs.size(); ++i) {
319*fe6060f1SDimitry Andric       Reloc &r = isec->relocs[i];
320*fe6060f1SDimitry Andric       assert(target->hasAttr(r.type, RelocAttrBits::UNSIGNED));
321*fe6060f1SDimitry Andric       if (r.offset % sizeof(CompactUnwindEntry<Ptr>) !=
322*fe6060f1SDimitry Andric           offsetof(CompactUnwindEntry<Ptr>, functionAddress))
323*fe6060f1SDimitry Andric         continue;
324*fe6060f1SDimitry Andric       result.insert(r.referent.get<InputSection *>());
325*fe6060f1SDimitry Andric     }
326*fe6060f1SDimitry Andric   }
327*fe6060f1SDimitry Andric   return result;
328*fe6060f1SDimitry Andric }
329*fe6060f1SDimitry Andric 
330*fe6060f1SDimitry Andric void macho::foldIdenticalSections() {
331*fe6060f1SDimitry Andric   TimeTraceScope timeScope("Fold Identical Code Sections");
332*fe6060f1SDimitry Andric   // The ICF equivalence-class segregation algorithm relies on pre-computed
333*fe6060f1SDimitry Andric   // hashes of InputSection::data for the ConcatOutputSection::inputs and all
334*fe6060f1SDimitry Andric   // sections referenced by their relocs. We could recursively traverse the
335*fe6060f1SDimitry Andric   // relocs to find every referenced InputSection, but that precludes easy
336*fe6060f1SDimitry Andric   // parallelization. Therefore, we hash every InputSection here where we have
337*fe6060f1SDimitry Andric   // them all accessible as simple vectors.
338*fe6060f1SDimitry Andric 
339*fe6060f1SDimitry Andric   // ICF can't fold functions with unwind info
340*fe6060f1SDimitry Andric   DenseSet<const InputSection *> functionsWithUnwindInfo =
341*fe6060f1SDimitry Andric       target->wordSize == 8 ? findFunctionsWithUnwindInfo<uint64_t>()
342*fe6060f1SDimitry Andric                             : findFunctionsWithUnwindInfo<uint32_t>();
343*fe6060f1SDimitry Andric 
344*fe6060f1SDimitry Andric   // If an InputSection is ineligible for ICF, we give it a unique ID to force
345*fe6060f1SDimitry Andric   // it into an unfoldable singleton equivalence class.  Begin the unique-ID
346*fe6060f1SDimitry Andric   // space at inputSections.size(), so that it will never intersect with
347*fe6060f1SDimitry Andric   // equivalence-class IDs which begin at 0. Since hashes & unique IDs never
348*fe6060f1SDimitry Andric   // coexist with equivalence-class IDs, this is not necessary, but might help
349*fe6060f1SDimitry Andric   // someone keep the numbers straight in case we ever need to debug the
350*fe6060f1SDimitry Andric   // ICF::segregate()
351*fe6060f1SDimitry Andric   std::vector<ConcatInputSection *> hashable;
352*fe6060f1SDimitry Andric   uint64_t icfUniqueID = inputSections.size();
353*fe6060f1SDimitry Andric   for (ConcatInputSection *isec : inputSections) {
354*fe6060f1SDimitry Andric     // FIXME: consider non-code __text sections as hashable?
355*fe6060f1SDimitry Andric     bool isHashable = (isCodeSection(isec) || isCfStringSection(isec)) &&
356*fe6060f1SDimitry Andric                       !isec->shouldOmitFromOutput() &&
357*fe6060f1SDimitry Andric                       !functionsWithUnwindInfo.contains(isec) &&
358*fe6060f1SDimitry Andric                       isec->isHashableForICF();
359*fe6060f1SDimitry Andric     if (isHashable)
360*fe6060f1SDimitry Andric       hashable.push_back(isec);
361*fe6060f1SDimitry Andric     else
362*fe6060f1SDimitry Andric       isec->icfEqClass[0] = ++icfUniqueID;
363*fe6060f1SDimitry Andric   }
364*fe6060f1SDimitry Andric   parallelForEach(hashable,
365*fe6060f1SDimitry Andric                   [](ConcatInputSection *isec) { isec->hashForICF(); });
366*fe6060f1SDimitry Andric   // Now that every input section is either hashed or marked as unique, run the
367*fe6060f1SDimitry Andric   // segregation algorithm to detect foldable subsections.
368*fe6060f1SDimitry Andric   ICF(hashable).run();
369*fe6060f1SDimitry Andric }
370