Coverage Report

Created: 2018-08-19 21:11

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/ELF/ICF.cpp
Line
Count
Source (jump to first uncovered line)
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
// ICF is short for Identical Code Folding. This 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.
14
//
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*.
20
//
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.
24
//
25
//   void foo() { bar(); }
26
//   void bar() { foo(); }
27
//
28
// If you merge the two, their relocations point to the same section and
29
// thus you know they are mergeable, but how do you know they are
30
// mergeable in the first place? This is not an easy problem to solve.
31
//
32
// What we are doing in LLD is to partition sections into equivalence
33
// classes. Sections in the same equivalence class when the algorithm
34
// terminates are considered identical. Here are details:
35
//
36
// 1. First, we partition sections using their hash values as keys. Hash
37
//    values contain section types, section contents and numbers of
38
//    relocations. During this step, relocation targets are not taken into
39
//    account. We just put sections that apparently differ into different
40
//    equivalence classes.
41
//
42
// 2. Next, for each equivalence class, we visit sections to compare
43
//    relocation targets. Relocation targets are considered equivalent if
44
//    their targets are in the same equivalence class. Sections with
45
//    different relocation targets are put into different equivalence
46
//    clases.
47
//
48
// 3. If we split an equivalence class in step 2, two relocations
49
//    previously target the same equivalence class may now target
50
//    different equivalence classes. Therefore, we repeat step 2 until a
51
//    convergence is obtained.
52
//
53
// 4. For each equivalence class C, pick an arbitrary section in C, and
54
//    merge all the other sections in 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
// This algorithm was mentioned as an "optimistic algorithm" in [1],
60
// though gold implements a different algorithm than this.
61
//
62
// We parallelize each step so that multiple threads can work on different
63
// equivalence classes concurrently. That gave us a large performance
64
// boost when applying ICF on large programs. For example, MSVC link.exe
65
// or GNU gold takes 10-20 seconds to apply ICF on Chromium, whose output
66
// size is about 1.5 GB, but LLD can finish it in less than 2 seconds on a
67
// 2.8 GHz 40 core machine. Even without threading, LLD's ICF is still
68
// faster than MSVC or gold though.
69
//
70
// [1] Safe ICF: Pointer Safe and Unwinding aware Identical Code Folding
71
// in the Gold Linker
72
// http://static.googleusercontent.com/media/research.google.com/en//pubs/archive/36912.pdf
73
//
74
//===----------------------------------------------------------------------===//
75
76
#include "ICF.h"
77
#include "Config.h"
78
#include "SymbolTable.h"
79
#include "Symbols.h"
80
#include "SyntheticSections.h"
81
#include "Writer.h"
82
#include "lld/Common/Threads.h"
83
#include "llvm/ADT/StringExtras.h"
84
#include "llvm/BinaryFormat/ELF.h"
85
#include "llvm/Object/ELF.h"
86
#include "llvm/Support/xxhash.h"
87
#include <algorithm>
88
#include <atomic>
89
90
using namespace lld;
91
using namespace lld::elf;
92
using namespace llvm;
93
using namespace llvm::ELF;
94
using namespace llvm::object;
95
96
namespace {
97
template <class ELFT> class ICF {
98
public:
99
  void run();
100
101
private:
102
  void segregate(size_t Begin, size_t End, bool Constant);
103
104
  template <class RelTy>
105
  bool constantEq(const InputSection *A, ArrayRef<RelTy> RelsA,
106
                  const InputSection *B, ArrayRef<RelTy> RelsB);
107
108
  template <class RelTy>
109
  bool variableEq(const InputSection *A, ArrayRef<RelTy> RelsA,
110
                  const InputSection *B, ArrayRef<RelTy> RelsB);
111
112
  bool equalsConstant(const InputSection *A, const InputSection *B);
113
  bool equalsVariable(const InputSection *A, const InputSection *B);
114
115
  size_t findBoundary(size_t Begin, size_t End);
116
117
  void forEachClassRange(size_t Begin, size_t End,
118
                         llvm::function_ref<void(size_t, size_t)> Fn);
119
120
  void forEachClass(llvm::function_ref<void(size_t, size_t)> Fn);
121
122
  std::vector<InputSection *> Sections;
123
124
  // We repeat the main loop while `Repeat` is true.
125
  std::atomic<bool> Repeat;
126
127
  // The main loop counter.
128
  int Cnt = 0;
129
130
  // We have two locations for equivalence classes. On the first iteration
131
  // of the main loop, Class[0] has a valid value, and Class[1] contains
132
  // garbage. We read equivalence classes from slot 0 and write to slot 1.
133
  // So, Class[0] represents the current class, and Class[1] represents
134
  // the next class. On each iteration, we switch their roles and use them
135
  // alternately.
136
  //
137
  // Why are we doing this? Recall that other threads may be working on
138
  // other equivalence classes in parallel. They may read sections that we
139
  // are updating. We cannot update equivalence classes in place because
140
  // it breaks the invariance that all possibly-identical sections must be
141
  // in the same equivalence class at any moment. In other words, the for
142
  // loop to update equivalence classes is not atomic, and that is
143
  // observable from other threads. By writing new classes to other
144
  // places, we can keep the invariance.
145
  //
146
  // Below, `Current` has the index of the current class, and `Next` has
147
  // the index of the next class. If threading is enabled, they are either
148
  // (0, 1) or (1, 0).
149
  //
150
  // Note on single-thread: if that's the case, they are always (0, 0)
151
  // because we can safely read the next class without worrying about race
152
  // conditions. Using the same location makes this algorithm converge
153
  // faster because it uses results of the same iteration earlier.
154
  int Current = 0;
155
  int Next = 0;
156
};
157
}
158
159
// Returns true if section S is subject of ICF.
160
2.43k
static bool isEligible(InputSection *S) {
161
2.43k
  if (!S->Live || S->KeepUnique || 
!(S->Flags & SHF_ALLOC)2.36k
)
162
135
    return false;
163
2.30k
164
2.30k
  // Don't merge writable sections. .data.rel.ro sections are marked as writable
165
2.30k
  // but are semantically read-only.
166
2.30k
  if ((S->Flags & SHF_WRITE) && 
S->Name != ".data.rel.ro"3
&&
167
2.30k
      
!S->Name.startswith(".data.rel.ro.")2
)
168
1
    return false;
169
2.30k
170
2.30k
  // SHF_LINK_ORDER sections are ICF'd as a unit with their dependent sections,
171
2.30k
  // so we don't consider them for ICF individually.
172
2.30k
  if (S->Flags & SHF_LINK_ORDER)
173
4
    return false;
174
2.29k
175
2.29k
  // Don't merge synthetic sections as their Data member is not valid and empty.
176
2.29k
  // The Data member needs to be valid for ICF as it is used by ICF to determine
177
2.29k
  // the equality of section contents.
178
2.29k
  if (isa<SyntheticSection>(S))
179
9
    return false;
180
2.29k
181
2.29k
  // .init and .fini contains instructions that must be executed to initialize
182
2.29k
  // and finalize the process. They cannot and should not be merged.
183
2.29k
  if (S->Name == ".init" || 
S->Name == ".fini"2.28k
)
184
2
    return false;
185
2.28k
186
2.28k
  // A user program may enumerate sections named with a C identifier using
187
2.28k
  // __start_* and __stop_* symbols. We cannot ICF any such sections because
188
2.28k
  // that could change program semantics.
189
2.28k
  if (isValidCIdentifier(S->Name))
190
2
    return false;
191
2.28k
192
2.28k
  return true;
193
2.28k
}
194
195
// Split an equivalence class into smaller classes.
196
template <class ELFT>
197
2.35k
void ICF<ELFT>::segregate(size_t Begin, size_t End, bool Constant) {
198
2.35k
  // This loop rearranges sections in [Begin, End) so that all sections
199
2.35k
  // that are equal in terms of equals{Constant,Variable} are contiguous
200
2.35k
  // in [Begin, End).
201
2.35k
  //
202
2.35k
  // The algorithm is quadratic in the worst case, but that is not an
203
2.35k
  // issue in practice because the number of the distinct sections in
204
2.35k
  // each range is usually very small.
205
2.35k
206
6.77k
  while (Begin < End) {
207
4.41k
    // Divide [Begin, End) into two. Let Mid be the start index of the
208
4.41k
    // second group.
209
4.41k
    auto Bound =
210
4.41k
        std::stable_partition(Sections.begin() + Begin + 1,
211
2.09M
                              Sections.begin() + End, [&](InputSection *S) {
212
2.09M
                                if (Constant)
213
2.09M
                                  return equalsConstant(Sections[Begin], S);
214
80
                                return equalsVariable(Sections[Begin], S);
215
80
                              });
ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)1, false> >::segregate(unsigned long, unsigned long, bool)::'lambda'(lld::elf::InputSection*)::operator()(lld::elf::InputSection*) const
Line
Count
Source
211
6
                              Sections.begin() + End, [&](InputSection *S) {
212
6
                                if (Constant)
213
3
                                  return equalsConstant(Sections[Begin], S);
214
3
                                return equalsVariable(Sections[Begin], S);
215
3
                              });
Unexecuted instantiation: ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)0, false> >::segregate(unsigned long, unsigned long, bool)::'lambda'(lld::elf::InputSection*)::operator()(lld::elf::InputSection*) const
ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)1, true> >::segregate(unsigned long, unsigned long, bool)::'lambda'(lld::elf::InputSection*)::operator()(lld::elf::InputSection*) const
Line
Count
Source
211
2.09M
                              Sections.begin() + End, [&](InputSection *S) {
212
2.09M
                                if (Constant)
213
2.09M
                                  return equalsConstant(Sections[Begin], S);
214
77
                                return equalsVariable(Sections[Begin], S);
215
77
                              });
Unexecuted instantiation: ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)0, true> >::segregate(unsigned long, unsigned long, bool)::'lambda'(lld::elf::InputSection*)::operator()(lld::elf::InputSection*) const
216
4.41k
    size_t Mid = Bound - Sections.begin();
217
4.41k
218
4.41k
    // Now we split [Begin, End) into [Begin, Mid) and [Mid, End) by
219
4.41k
    // updating the sections in [Begin, Mid). We use Mid as an equivalence
220
4.41k
    // class ID because every group ends with a unique index.
221
8.99k
    for (size_t I = Begin; I < Mid; 
++I4.57k
)
222
4.57k
      Sections[I]->Class[Next] = Mid;
223
4.41k
224
4.41k
    // If we created a group, we need to iterate the main loop again.
225
4.41k
    if (Mid != End)
226
2.06k
      Repeat = true;
227
4.41k
228
4.41k
    Begin = Mid;
229
4.41k
  }
230
2.35k
}
ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)1, false> >::segregate(unsigned long, unsigned long, bool)
Line
Count
Source
197
20
void ICF<ELFT>::segregate(size_t Begin, size_t End, bool Constant) {
198
20
  // This loop rearranges sections in [Begin, End) so that all sections
199
20
  // that are equal in terms of equals{Constant,Variable} are contiguous
200
20
  // in [Begin, End).
201
20
  //
202
20
  // The algorithm is quadratic in the worst case, but that is not an
203
20
  // issue in practice because the number of the distinct sections in
204
20
  // each range is usually very small.
205
20
206
40
  while (Begin < End) {
207
20
    // Divide [Begin, End) into two. Let Mid be the start index of the
208
20
    // second group.
209
20
    auto Bound =
210
20
        std::stable_partition(Sections.begin() + Begin + 1,
211
20
                              Sections.begin() + End, [&](InputSection *S) {
212
20
                                if (Constant)
213
20
                                  return equalsConstant(Sections[Begin], S);
214
20
                                return equalsVariable(Sections[Begin], S);
215
20
                              });
216
20
    size_t Mid = Bound - Sections.begin();
217
20
218
20
    // Now we split [Begin, End) into [Begin, Mid) and [Mid, End) by
219
20
    // updating the sections in [Begin, Mid). We use Mid as an equivalence
220
20
    // class ID because every group ends with a unique index.
221
46
    for (size_t I = Begin; I < Mid; 
++I26
)
222
26
      Sections[I]->Class[Next] = Mid;
223
20
224
20
    // If we created a group, we need to iterate the main loop again.
225
20
    if (Mid != End)
226
0
      Repeat = true;
227
20
228
20
    Begin = Mid;
229
20
  }
230
20
}
Unexecuted instantiation: ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)0, false> >::segregate(unsigned long, unsigned long, bool)
ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)1, true> >::segregate(unsigned long, unsigned long, bool)
Line
Count
Source
197
2.33k
void ICF<ELFT>::segregate(size_t Begin, size_t End, bool Constant) {
198
2.33k
  // This loop rearranges sections in [Begin, End) so that all sections
199
2.33k
  // that are equal in terms of equals{Constant,Variable} are contiguous
200
2.33k
  // in [Begin, End).
201
2.33k
  //
202
2.33k
  // The algorithm is quadratic in the worst case, but that is not an
203
2.33k
  // issue in practice because the number of the distinct sections in
204
2.33k
  // each range is usually very small.
205
2.33k
206
6.73k
  while (Begin < End) {
207
4.39k
    // Divide [Begin, End) into two. Let Mid be the start index of the
208
4.39k
    // second group.
209
4.39k
    auto Bound =
210
4.39k
        std::stable_partition(Sections.begin() + Begin + 1,
211
4.39k
                              Sections.begin() + End, [&](InputSection *S) {
212
4.39k
                                if (Constant)
213
4.39k
                                  return equalsConstant(Sections[Begin], S);
214
4.39k
                                return equalsVariable(Sections[Begin], S);
215
4.39k
                              });
216
4.39k
    size_t Mid = Bound - Sections.begin();
217
4.39k
218
4.39k
    // Now we split [Begin, End) into [Begin, Mid) and [Mid, End) by
219
4.39k
    // updating the sections in [Begin, Mid). We use Mid as an equivalence
220
4.39k
    // class ID because every group ends with a unique index.
221
8.94k
    for (size_t I = Begin; I < Mid; 
++I4.55k
)
222
4.55k
      Sections[I]->Class[Next] = Mid;
223
4.39k
224
4.39k
    // If we created a group, we need to iterate the main loop again.
225
4.39k
    if (Mid != End)
226
2.06k
      Repeat = true;
227
4.39k
228
4.39k
    Begin = Mid;
229
4.39k
  }
230
2.33k
}
Unexecuted instantiation: ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)0, true> >::segregate(unsigned long, unsigned long, bool)
231
232
// Compare two lists of relocations.
233
template <class ELFT>
234
template <class RelTy>
235
bool ICF<ELFT>::constantEq(const InputSection *SecA, ArrayRef<RelTy> RA,
236
91
                           const InputSection *SecB, ArrayRef<RelTy> RB) {
237
102
  for (size_t I = 0; I < RA.size(); 
++I11
) {
238
22
    if (RA[I].r_offset != RB[I].r_offset ||
239
22
        
RA[I].getType(Config->IsMips64EL) != RB[I].getType(Config->IsMips64EL)21
)
240
2
      return false;
241
20
242
20
    uint64_t AddA = getAddend<ELFT>(RA[I]);
243
20
    uint64_t AddB = getAddend<ELFT>(RB[I]);
244
20
245
20
    Symbol &SA = SecA->template getFile<ELFT>()->getRelocTargetSym(RA[I]);
246
20
    Symbol &SB = SecB->template getFile<ELFT>()->getRelocTargetSym(RB[I]);
247
20
    if (&SA == &SB) {
248
5
      if (AddA == AddB)
249
4
        continue;
250
1
      return false;
251
1
    }
252
15
253
15
    auto *DA = dyn_cast<Defined>(&SA);
254
15
    auto *DB = dyn_cast<Defined>(&SB);
255
15
    if (!DA || !DB)
256
1
      return false;
257
14
258
14
    // Relocations referring to absolute symbols are constant-equal if their
259
14
    // values are equal.
260
14
    if (!DA->Section && 
!DB->Section2
&&
DA->Value + AddA == DB->Value + AddB2
)
261
1
      continue;
262
13
    if (!DA->Section || 
!DB->Section12
)
263
1
      return false;
264
12
265
12
    if (DA->Section->kind() != DB->Section->kind())
266
1
      return false;
267
11
268
11
    // Relocations referring to InputSections are constant-equal if their
269
11
    // section offsets are equal.
270
11
    if (isa<InputSection>(DA->Section)) {
271
5
      if (DA->Value + AddA == DB->Value + AddB)
272
4
        continue;
273
1
      return false;
274
1
    }
275
6
276
6
    // Relocations referring to MergeInputSections are constant-equal if their
277
6
    // offsets in the output section are equal.
278
6
    auto *X = dyn_cast<MergeInputSection>(DA->Section);
279
6
    if (!X)
280
1
      return false;
281
5
    auto *Y = cast<MergeInputSection>(DB->Section);
282
5
    if (X->getParent() != Y->getParent())
283
1
      return false;
284
4
285
4
    uint64_t OffsetA =
286
4
        SA.isSection() ? 
X->getOffset(AddA)1
:
X->getOffset(DA->Value) + AddA3
;
287
4
    uint64_t OffsetB =
288
4
        SB.isSection() ? 
Y->getOffset(AddB)1
:
Y->getOffset(DB->Value) + AddB3
;
289
4
    if (OffsetA != OffsetB)
290
2
      return false;
291
4
  }
292
91
293
91
  
return true80
;
294
91
}
Unexecuted instantiation: ICF.cpp:bool (anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)1, false> >::constantEq<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> >(lld::elf::InputSection const*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> >, lld::elf::InputSection const*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> >)
ICF.cpp:bool (anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)1, false> >::constantEq<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> >(lld::elf::InputSection const*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> >, lld::elf::InputSection const*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> >)
Line
Count
Source
236
3
                           const InputSection *SecB, ArrayRef<RelTy> RB) {
237
4
  for (size_t I = 0; I < RA.size(); 
++I1
) {
238
1
    if (RA[I].r_offset != RB[I].r_offset ||
239
1
        RA[I].getType(Config->IsMips64EL) != RB[I].getType(Config->IsMips64EL))
240
0
      return false;
241
1
242
1
    uint64_t AddA = getAddend<ELFT>(RA[I]);
243
1
    uint64_t AddB = getAddend<ELFT>(RB[I]);
244
1
245
1
    Symbol &SA = SecA->template getFile<ELFT>()->getRelocTargetSym(RA[I]);
246
1
    Symbol &SB = SecB->template getFile<ELFT>()->getRelocTargetSym(RB[I]);
247
1
    if (&SA == &SB) {
248
1
      if (AddA == AddB)
249
1
        continue;
250
0
      return false;
251
0
    }
252
0
253
0
    auto *DA = dyn_cast<Defined>(&SA);
254
0
    auto *DB = dyn_cast<Defined>(&SB);
255
0
    if (!DA || !DB)
256
0
      return false;
257
0
258
0
    // Relocations referring to absolute symbols are constant-equal if their
259
0
    // values are equal.
260
0
    if (!DA->Section && !DB->Section && DA->Value + AddA == DB->Value + AddB)
261
0
      continue;
262
0
    if (!DA->Section || !DB->Section)
263
0
      return false;
264
0
265
0
    if (DA->Section->kind() != DB->Section->kind())
266
0
      return false;
267
0
268
0
    // Relocations referring to InputSections are constant-equal if their
269
0
    // section offsets are equal.
270
0
    if (isa<InputSection>(DA->Section)) {
271
0
      if (DA->Value + AddA == DB->Value + AddB)
272
0
        continue;
273
0
      return false;
274
0
    }
275
0
276
0
    // Relocations referring to MergeInputSections are constant-equal if their
277
0
    // offsets in the output section are equal.
278
0
    auto *X = dyn_cast<MergeInputSection>(DA->Section);
279
0
    if (!X)
280
0
      return false;
281
0
    auto *Y = cast<MergeInputSection>(DB->Section);
282
0
    if (X->getParent() != Y->getParent())
283
0
      return false;
284
0
285
0
    uint64_t OffsetA =
286
0
        SA.isSection() ? X->getOffset(AddA) : X->getOffset(DA->Value) + AddA;
287
0
    uint64_t OffsetB =
288
0
        SB.isSection() ? Y->getOffset(AddB) : Y->getOffset(DB->Value) + AddB;
289
0
    if (OffsetA != OffsetB)
290
0
      return false;
291
0
  }
292
3
293
3
  return true;
294
3
}
Unexecuted instantiation: ICF.cpp:bool (anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)0, false> >::constantEq<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> >(lld::elf::InputSection const*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> >, lld::elf::InputSection const*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> >)
Unexecuted instantiation: ICF.cpp:bool (anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)0, false> >::constantEq<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> >(lld::elf::InputSection const*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> >, lld::elf::InputSection const*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> >)
ICF.cpp:bool (anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)1, true> >::constantEq<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> >(lld::elf::InputSection const*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> >, lld::elf::InputSection const*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> >)
Line
Count
Source
236
20
                           const InputSection *SecB, ArrayRef<RelTy> RB) {
237
30
  for (size_t I = 0; I < RA.size(); 
++I10
) {
238
21
    if (RA[I].r_offset != RB[I].r_offset ||
239
21
        
RA[I].getType(Config->IsMips64EL) != RB[I].getType(Config->IsMips64EL)20
)
240
2
      return false;
241
19
242
19
    uint64_t AddA = getAddend<ELFT>(RA[I]);
243
19
    uint64_t AddB = getAddend<ELFT>(RB[I]);
244
19
245
19
    Symbol &SA = SecA->template getFile<ELFT>()->getRelocTargetSym(RA[I]);
246
19
    Symbol &SB = SecB->template getFile<ELFT>()->getRelocTargetSym(RB[I]);
247
19
    if (&SA == &SB) {
248
4
      if (AddA == AddB)
249
3
        continue;
250
1
      return false;
251
1
    }
252
15
253
15
    auto *DA = dyn_cast<Defined>(&SA);
254
15
    auto *DB = dyn_cast<Defined>(&SB);
255
15
    if (!DA || !DB)
256
1
      return false;
257
14
258
14
    // Relocations referring to absolute symbols are constant-equal if their
259
14
    // values are equal.
260
14
    if (!DA->Section && 
!DB->Section2
&&
DA->Value + AddA == DB->Value + AddB2
)
261
1
      continue;
262
13
    if (!DA->Section || 
!DB->Section12
)
263
1
      return false;
264
12
265
12
    if (DA->Section->kind() != DB->Section->kind())
266
1
      return false;
267
11
268
11
    // Relocations referring to InputSections are constant-equal if their
269
11
    // section offsets are equal.
270
11
    if (isa<InputSection>(DA->Section)) {
271
5
      if (DA->Value + AddA == DB->Value + AddB)
272
4
        continue;
273
1
      return false;
274
1
    }
275
6
276
6
    // Relocations referring to MergeInputSections are constant-equal if their
277
6
    // offsets in the output section are equal.
278
6
    auto *X = dyn_cast<MergeInputSection>(DA->Section);
279
6
    if (!X)
280
1
      return false;
281
5
    auto *Y = cast<MergeInputSection>(DB->Section);
282
5
    if (X->getParent() != Y->getParent())
283
1
      return false;
284
4
285
4
    uint64_t OffsetA =
286
4
        SA.isSection() ? 
X->getOffset(AddA)1
:
X->getOffset(DA->Value) + AddA3
;
287
4
    uint64_t OffsetB =
288
4
        SB.isSection() ? 
Y->getOffset(AddB)1
:
Y->getOffset(DB->Value) + AddB3
;
289
4
    if (OffsetA != OffsetB)
290
2
      return false;
291
4
  }
292
20
293
20
  
return true9
;
294
20
}
ICF.cpp:bool (anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)1, true> >::constantEq<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> >(lld::elf::InputSection const*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> >, lld::elf::InputSection const*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> >)
Line
Count
Source
236
68
                           const InputSection *SecB, ArrayRef<RelTy> RB) {
237
68
  for (size_t I = 0; I < RA.size(); 
++I0
) {
238
0
    if (RA[I].r_offset != RB[I].r_offset ||
239
0
        RA[I].getType(Config->IsMips64EL) != RB[I].getType(Config->IsMips64EL))
240
0
      return false;
241
0
242
0
    uint64_t AddA = getAddend<ELFT>(RA[I]);
243
0
    uint64_t AddB = getAddend<ELFT>(RB[I]);
244
0
245
0
    Symbol &SA = SecA->template getFile<ELFT>()->getRelocTargetSym(RA[I]);
246
0
    Symbol &SB = SecB->template getFile<ELFT>()->getRelocTargetSym(RB[I]);
247
0
    if (&SA == &SB) {
248
0
      if (AddA == AddB)
249
0
        continue;
250
0
      return false;
251
0
    }
252
0
253
0
    auto *DA = dyn_cast<Defined>(&SA);
254
0
    auto *DB = dyn_cast<Defined>(&SB);
255
0
    if (!DA || !DB)
256
0
      return false;
257
0
258
0
    // Relocations referring to absolute symbols are constant-equal if their
259
0
    // values are equal.
260
0
    if (!DA->Section && !DB->Section && DA->Value + AddA == DB->Value + AddB)
261
0
      continue;
262
0
    if (!DA->Section || !DB->Section)
263
0
      return false;
264
0
265
0
    if (DA->Section->kind() != DB->Section->kind())
266
0
      return false;
267
0
268
0
    // Relocations referring to InputSections are constant-equal if their
269
0
    // section offsets are equal.
270
0
    if (isa<InputSection>(DA->Section)) {
271
0
      if (DA->Value + AddA == DB->Value + AddB)
272
0
        continue;
273
0
      return false;
274
0
    }
275
0
276
0
    // Relocations referring to MergeInputSections are constant-equal if their
277
0
    // offsets in the output section are equal.
278
0
    auto *X = dyn_cast<MergeInputSection>(DA->Section);
279
0
    if (!X)
280
0
      return false;
281
0
    auto *Y = cast<MergeInputSection>(DB->Section);
282
0
    if (X->getParent() != Y->getParent())
283
0
      return false;
284
0
285
0
    uint64_t OffsetA =
286
0
        SA.isSection() ? X->getOffset(AddA) : X->getOffset(DA->Value) + AddA;
287
0
    uint64_t OffsetB =
288
0
        SB.isSection() ? Y->getOffset(AddB) : Y->getOffset(DB->Value) + AddB;
289
0
    if (OffsetA != OffsetB)
290
0
      return false;
291
0
  }
292
68
293
68
  return true;
294
68
}
Unexecuted instantiation: ICF.cpp:bool (anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)0, true> >::constantEq<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> >(lld::elf::InputSection const*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> >, lld::elf::InputSection const*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> >)
Unexecuted instantiation: ICF.cpp:bool (anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)0, true> >::constantEq<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> >(lld::elf::InputSection const*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> >, lld::elf::InputSection const*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> >)
295
296
// Compare "non-moving" part of two InputSections, namely everything
297
// except relocation targets.
298
template <class ELFT>
299
2.09M
bool ICF<ELFT>::equalsConstant(const InputSection *A, const InputSection *B) {
300
2.09M
  if (A->NumRelocations != B->NumRelocations || A->Flags != B->Flags ||
301
2.09M
      
A->getSize() != B->getSize()2.09M
||
A->Data != B->Data2.09M
)
302
1
    return false;
303
2.09M
304
2.09M
  // If two sections have different output sections, we cannot merge them.
305
2.09M
  // FIXME: This doesn't do the right thing in the case where there is a linker
306
2.09M
  // script. We probably need to move output section assignment before ICF to
307
2.09M
  // get the correct behaviour here.
308
2.09M
  if (getOutputSectionName(A) != getOutputSectionName(B))
309
2.09M
    return false;
310
91
311
91
  if (A->AreRelocsRela)
312
20
    return constantEq(A, A->template relas<ELFT>(), B,
313
20
                      B->template relas<ELFT>());
314
71
  return constantEq(A, A->template rels<ELFT>(), B, B->template rels<ELFT>());
315
71
}
ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)1, false> >::equalsConstant(lld::elf::InputSection const*, lld::elf::InputSection const*)
Line
Count
Source
299
3
bool ICF<ELFT>::equalsConstant(const InputSection *A, const InputSection *B) {
300
3
  if (A->NumRelocations != B->NumRelocations || A->Flags != B->Flags ||
301
3
      A->getSize() != B->getSize() || A->Data != B->Data)
302
0
    return false;
303
3
304
3
  // If two sections have different output sections, we cannot merge them.
305
3
  // FIXME: This doesn't do the right thing in the case where there is a linker
306
3
  // script. We probably need to move output section assignment before ICF to
307
3
  // get the correct behaviour here.
308
3
  if (getOutputSectionName(A) != getOutputSectionName(B))
309
0
    return false;
310
3
311
3
  if (A->AreRelocsRela)
312
0
    return constantEq(A, A->template relas<ELFT>(), B,
313
0
                      B->template relas<ELFT>());
314
3
  return constantEq(A, A->template rels<ELFT>(), B, B->template rels<ELFT>());
315
3
}
Unexecuted instantiation: ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)0, false> >::equalsConstant(lld::elf::InputSection const*, lld::elf::InputSection const*)
ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)1, true> >::equalsConstant(lld::elf::InputSection const*, lld::elf::InputSection const*)
Line
Count
Source
299
2.09M
bool ICF<ELFT>::equalsConstant(const InputSection *A, const InputSection *B) {
300
2.09M
  if (A->NumRelocations != B->NumRelocations || A->Flags != B->Flags ||
301
2.09M
      
A->getSize() != B->getSize()2.09M
||
A->Data != B->Data2.09M
)
302
1
    return false;
303
2.09M
304
2.09M
  // If two sections have different output sections, we cannot merge them.
305
2.09M
  // FIXME: This doesn't do the right thing in the case where there is a linker
306
2.09M
  // script. We probably need to move output section assignment before ICF to
307
2.09M
  // get the correct behaviour here.
308
2.09M
  if (getOutputSectionName(A) != getOutputSectionName(B))
309
2.09M
    return false;
310
88
311
88
  if (A->AreRelocsRela)
312
20
    return constantEq(A, A->template relas<ELFT>(), B,
313
20
                      B->template relas<ELFT>());
314
68
  return constantEq(A, A->template rels<ELFT>(), B, B->template rels<ELFT>());
315
68
}
Unexecuted instantiation: ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)0, true> >::equalsConstant(lld::elf::InputSection const*, lld::elf::InputSection const*)
316
317
// Compare two lists of relocations. Returns true if all pairs of
318
// relocations point to the same section in terms of ICF.
319
template <class ELFT>
320
template <class RelTy>
321
bool ICF<ELFT>::variableEq(const InputSection *SecA, ArrayRef<RelTy> RA,
322
80
                           const InputSection *SecB, ArrayRef<RelTy> RB) {
323
80
  assert(RA.size() == RB.size());
324
80
325
90
  for (size_t I = 0; I < RA.size(); 
++I10
) {
326
11
    // The two sections must be identical.
327
11
    Symbol &SA = SecA->template getFile<ELFT>()->getRelocTargetSym(RA[I]);
328
11
    Symbol &SB = SecB->template getFile<ELFT>()->getRelocTargetSym(RB[I]);
329
11
    if (&SA == &SB)
330
4
      continue;
331
7
332
7
    auto *DA = cast<Defined>(&SA);
333
7
    auto *DB = cast<Defined>(&SB);
334
7
335
7
    // We already dealt with absolute and non-InputSection symbols in
336
7
    // constantEq, and for InputSections we have already checked everything
337
7
    // except the equivalence class.
338
7
    if (!DA->Section)
339
1
      continue;
340
6
    auto *X = dyn_cast<InputSection>(DA->Section);
341
6
    if (!X)
342
2
      continue;
343
4
    auto *Y = cast<InputSection>(DB->Section);
344
4
345
4
    // Ineligible sections are in the special equivalence class 0.
346
4
    // They can never be the same in terms of the equivalence class.
347
4
    if (X->Class[Current] == 0)
348
1
      return false;
349
3
    if (X->Class[Current] != Y->Class[Current])
350
0
      return false;
351
79
  };
352
79
353
79
  return true;
354
80
}
Unexecuted instantiation: ICF.cpp:bool (anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)1, false> >::variableEq<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> >(lld::elf::InputSection const*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> >, lld::elf::InputSection const*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> >)
ICF.cpp:bool (anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)1, false> >::variableEq<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> >(lld::elf::InputSection const*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> >, lld::elf::InputSection const*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> >)
Line
Count
Source
322
3
                           const InputSection *SecB, ArrayRef<RelTy> RB) {
323
3
  assert(RA.size() == RB.size());
324
3
325
4
  for (size_t I = 0; I < RA.size(); 
++I1
) {
326
1
    // The two sections must be identical.
327
1
    Symbol &SA = SecA->template getFile<ELFT>()->getRelocTargetSym(RA[I]);
328
1
    Symbol &SB = SecB->template getFile<ELFT>()->getRelocTargetSym(RB[I]);
329
1
    if (&SA == &SB)
330
1
      continue;
331
0
332
0
    auto *DA = cast<Defined>(&SA);
333
0
    auto *DB = cast<Defined>(&SB);
334
0
335
0
    // We already dealt with absolute and non-InputSection symbols in
336
0
    // constantEq, and for InputSections we have already checked everything
337
0
    // except the equivalence class.
338
0
    if (!DA->Section)
339
0
      continue;
340
0
    auto *X = dyn_cast<InputSection>(DA->Section);
341
0
    if (!X)
342
0
      continue;
343
0
    auto *Y = cast<InputSection>(DB->Section);
344
0
345
0
    // Ineligible sections are in the special equivalence class 0.
346
0
    // They can never be the same in terms of the equivalence class.
347
0
    if (X->Class[Current] == 0)
348
0
      return false;
349
0
    if (X->Class[Current] != Y->Class[Current])
350
0
      return false;
351
3
  };
352
3
353
3
  return true;
354
3
}
Unexecuted instantiation: ICF.cpp:bool (anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)0, false> >::variableEq<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> >(lld::elf::InputSection const*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> >, lld::elf::InputSection const*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> >)
Unexecuted instantiation: ICF.cpp:bool (anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)0, false> >::variableEq<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> >(lld::elf::InputSection const*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> >, lld::elf::InputSection const*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> >)
ICF.cpp:bool (anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)1, true> >::variableEq<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> >(lld::elf::InputSection const*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> >, lld::elf::InputSection const*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> >)
Line
Count
Source
322
9
                           const InputSection *SecB, ArrayRef<RelTy> RB) {
323
9
  assert(RA.size() == RB.size());
324
9
325
18
  for (size_t I = 0; I < RA.size(); 
++I9
) {
326
10
    // The two sections must be identical.
327
10
    Symbol &SA = SecA->template getFile<ELFT>()->getRelocTargetSym(RA[I]);
328
10
    Symbol &SB = SecB->template getFile<ELFT>()->getRelocTargetSym(RB[I]);
329
10
    if (&SA == &SB)
330
3
      continue;
331
7
332
7
    auto *DA = cast<Defined>(&SA);
333
7
    auto *DB = cast<Defined>(&SB);
334
7
335
7
    // We already dealt with absolute and non-InputSection symbols in
336
7
    // constantEq, and for InputSections we have already checked everything
337
7
    // except the equivalence class.
338
7
    if (!DA->Section)
339
1
      continue;
340
6
    auto *X = dyn_cast<InputSection>(DA->Section);
341
6
    if (!X)
342
2
      continue;
343
4
    auto *Y = cast<InputSection>(DB->Section);
344
4
345
4
    // Ineligible sections are in the special equivalence class 0.
346
4
    // They can never be the same in terms of the equivalence class.
347
4
    if (X->Class[Current] == 0)
348
1
      return false;
349
3
    if (X->Class[Current] != Y->Class[Current])
350
0
      return false;
351
8
  };
352
8
353
8
  return true;
354
9
}
ICF.cpp:bool (anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)1, true> >::variableEq<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> >(lld::elf::InputSection const*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> >, lld::elf::InputSection const*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> >)
Line
Count
Source
322
68
                           const InputSection *SecB, ArrayRef<RelTy> RB) {
323
68
  assert(RA.size() == RB.size());
324
68
325
68
  for (size_t I = 0; I < RA.size(); 
++I0
) {
326
0
    // The two sections must be identical.
327
0
    Symbol &SA = SecA->template getFile<ELFT>()->getRelocTargetSym(RA[I]);
328
0
    Symbol &SB = SecB->template getFile<ELFT>()->getRelocTargetSym(RB[I]);
329
0
    if (&SA == &SB)
330
0
      continue;
331
0
332
0
    auto *DA = cast<Defined>(&SA);
333
0
    auto *DB = cast<Defined>(&SB);
334
0
335
0
    // We already dealt with absolute and non-InputSection symbols in
336
0
    // constantEq, and for InputSections we have already checked everything
337
0
    // except the equivalence class.
338
0
    if (!DA->Section)
339
0
      continue;
340
0
    auto *X = dyn_cast<InputSection>(DA->Section);
341
0
    if (!X)
342
0
      continue;
343
0
    auto *Y = cast<InputSection>(DB->Section);
344
0
345
0
    // Ineligible sections are in the special equivalence class 0.
346
0
    // They can never be the same in terms of the equivalence class.
347
0
    if (X->Class[Current] == 0)
348
0
      return false;
349
0
    if (X->Class[Current] != Y->Class[Current])
350
0
      return false;
351
68
  };
352
68
353
68
  return true;
354
68
}
Unexecuted instantiation: ICF.cpp:bool (anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)0, true> >::variableEq<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> >(lld::elf::InputSection const*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> >, lld::elf::InputSection const*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> >)
Unexecuted instantiation: ICF.cpp:bool (anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)0, true> >::variableEq<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> >(lld::elf::InputSection const*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> >, lld::elf::InputSection const*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> >)
355
356
// Compare "moving" part of two InputSections, namely relocation targets.
357
template <class ELFT>
358
80
bool ICF<ELFT>::equalsVariable(const InputSection *A, const InputSection *B) {
359
80
  if (A->AreRelocsRela)
360
9
    return variableEq(A, A->template relas<ELFT>(), B,
361
9
                      B->template relas<ELFT>());
362
71
  return variableEq(A, A->template rels<ELFT>(), B, B->template rels<ELFT>());
363
71
}
ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)1, false> >::equalsVariable(lld::elf::InputSection const*, lld::elf::InputSection const*)
Line
Count
Source
358
3
bool ICF<ELFT>::equalsVariable(const InputSection *A, const InputSection *B) {
359
3
  if (A->AreRelocsRela)
360
0
    return variableEq(A, A->template relas<ELFT>(), B,
361
0
                      B->template relas<ELFT>());
362
3
  return variableEq(A, A->template rels<ELFT>(), B, B->template rels<ELFT>());
363
3
}
Unexecuted instantiation: ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)0, false> >::equalsVariable(lld::elf::InputSection const*, lld::elf::InputSection const*)
ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)1, true> >::equalsVariable(lld::elf::InputSection const*, lld::elf::InputSection const*)
Line
Count
Source
358
77
bool ICF<ELFT>::equalsVariable(const InputSection *A, const InputSection *B) {
359
77
  if (A->AreRelocsRela)
360
9
    return variableEq(A, A->template relas<ELFT>(), B,
361
9
                      B->template relas<ELFT>());
362
68
  return variableEq(A, A->template rels<ELFT>(), B, B->template rels<ELFT>());
363
68
}
Unexecuted instantiation: ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)0, true> >::equalsVariable(lld::elf::InputSection const*, lld::elf::InputSection const*)
364
365
5.07k
template <class ELFT> size_t ICF<ELFT>::findBoundary(size_t Begin, size_t End) {
366
5.07k
  uint32_t Class = Sections[Begin]->Class[Current];
367
221k
  for (size_t I = Begin + 1; I < End; 
++I216k
)
368
221k
    if (Class != Sections[I]->Class[Current])
369
4.38k
      return I;
370
5.07k
  
return End681
;
371
5.07k
}
ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)1, false> >::findBoundary(unsigned long, unsigned long)
Line
Count
Source
365
30
template <class ELFT> size_t ICF<ELFT>::findBoundary(size_t Begin, size_t End) {
366
30
  uint32_t Class = Sections[Begin]->Class[Current];
367
39
  for (size_t I = Begin + 1; I < End; 
++I9
)
368
30
    if (Class != Sections[I]->Class[Current])
369
21
      return I;
370
30
  
return End9
;
371
30
}
Unexecuted instantiation: ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)0, false> >::findBoundary(unsigned long, unsigned long)
ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)1, true> >::findBoundary(unsigned long, unsigned long)
Line
Count
Source
365
5.04k
template <class ELFT> size_t ICF<ELFT>::findBoundary(size_t Begin, size_t End) {
366
5.04k
  uint32_t Class = Sections[Begin]->Class[Current];
367
221k
  for (size_t I = Begin + 1; I < End; 
++I216k
)
368
221k
    if (Class != Sections[I]->Class[Current])
369
4.36k
      return I;
370
5.04k
  
return End672
;
371
5.04k
}
Unexecuted instantiation: ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)0, true> >::findBoundary(unsigned long, unsigned long)
372
373
// Sections in the same equivalence class are contiguous in Sections
374
// vector. Therefore, Sections vector can be considered as contiguous
375
// groups of sections, grouped by the class.
376
//
377
// This function calls Fn on every group within [Begin, End).
378
template <class ELFT>
379
void ICF<ELFT>::forEachClassRange(size_t Begin, size_t End,
380
427
                                  llvm::function_ref<void(size_t, size_t)> Fn) {
381
4.98k
  while (Begin < End) {
382
4.56k
    size_t Mid = findBoundary(Begin, End);
383
4.56k
    Fn(Begin, Mid);
384
4.56k
    Begin = Mid;
385
4.56k
  }
386
427
}
ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)1, false> >::forEachClassRange(unsigned long, unsigned long, llvm::function_ref<void (unsigned long, unsigned long)>)
Line
Count
Source
380
9
                                  llvm::function_ref<void(size_t, size_t)> Fn) {
381
39
  while (Begin < End) {
382
30
    size_t Mid = findBoundary(Begin, End);
383
30
    Fn(Begin, Mid);
384
30
    Begin = Mid;
385
30
  }
386
9
}
Unexecuted instantiation: ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)0, false> >::forEachClassRange(unsigned long, unsigned long, llvm::function_ref<void (unsigned long, unsigned long)>)
ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)1, true> >::forEachClassRange(unsigned long, unsigned long, llvm::function_ref<void (unsigned long, unsigned long)>)
Line
Count
Source
380
418
                                  llvm::function_ref<void(size_t, size_t)> Fn) {
381
4.94k
  while (Begin < End) {
382
4.53k
    size_t Mid = findBoundary(Begin, End);
383
4.53k
    Fn(Begin, Mid);
384
4.53k
    Begin = Mid;
385
4.53k
  }
386
418
}
Unexecuted instantiation: ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)0, true> >::forEachClassRange(unsigned long, unsigned long, llvm::function_ref<void (unsigned long, unsigned long)>)
387
388
// Call Fn on each equivalence class.
389
template <class ELFT>
390
115
void ICF<ELFT>::forEachClass(llvm::function_ref<void(size_t, size_t)> Fn) {
391
115
  // If threading is disabled or the number of sections are
392
115
  // too small to use threading, call Fn sequentially.
393
115
  if (!ThreadsEnabled || Sections.size() < 1024) {
394
113
    forEachClassRange(0, Sections.size(), Fn);
395
113
    ++Cnt;
396
113
    return;
397
113
  }
398
2
399
2
  Current = Cnt % 2;
400
2
  Next = (Cnt + 1) % 2;
401
2
402
2
  // Shard into non-overlapping intervals, and call Fn in parallel.
403
2
  // The sharding must be completed before any calls to Fn are made
404
2
  // so that Fn can modify the Chunks in its shard without causing data
405
2
  // races.
406
2
  const size_t NumShards = 256;
407
2
  size_t Step = Sections.size() / NumShards;
408
2
  size_t Boundaries[NumShards + 1];
409
2
  Boundaries[0] = 0;
410
2
  Boundaries[NumShards] = Sections.size();
411
2
412
510
  parallelForEachN(1, NumShards, [&](size_t I) {
413
510
    Boundaries[I] = findBoundary((I - 1) * Step, Sections.size());
414
510
  });
Unexecuted instantiation: ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)1, false> >::forEachClass(llvm::function_ref<void (unsigned long, unsigned long)>)::'lambda'(unsigned long)::operator()(unsigned long) const
Unexecuted instantiation: ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)0, false> >::forEachClass(llvm::function_ref<void (unsigned long, unsigned long)>)::'lambda'(unsigned long)::operator()(unsigned long) const
ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)1, true> >::forEachClass(llvm::function_ref<void (unsigned long, unsigned long)>)::'lambda'(unsigned long)::operator()(unsigned long) const
Line
Count
Source
412
510
  parallelForEachN(1, NumShards, [&](size_t I) {
413
510
    Boundaries[I] = findBoundary((I - 1) * Step, Sections.size());
414
510
  });
Unexecuted instantiation: ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)0, true> >::forEachClass(llvm::function_ref<void (unsigned long, unsigned long)>)::'lambda'(unsigned long)::operator()(unsigned long) const
415
2
416
512
  parallelForEachN(1, NumShards + 1, [&](size_t I) {
417
512
    if (Boundaries[I - 1] < Boundaries[I])
418
257
      forEachClassRange(Boundaries[I - 1], Boundaries[I], Fn);
419
512
  });
Unexecuted instantiation: ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)1, false> >::forEachClass(llvm::function_ref<void (unsigned long, unsigned long)>)::'lambda0'(unsigned long)::operator()(unsigned long) const
Unexecuted instantiation: ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)0, false> >::forEachClass(llvm::function_ref<void (unsigned long, unsigned long)>)::'lambda0'(unsigned long)::operator()(unsigned long) const
ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)1, true> >::forEachClass(llvm::function_ref<void (unsigned long, unsigned long)>)::'lambda0'(unsigned long)::operator()(unsigned long) const
Line
Count
Source
416
512
  parallelForEachN(1, NumShards + 1, [&](size_t I) {
417
512
    if (Boundaries[I - 1] < Boundaries[I])
418
257
      forEachClassRange(Boundaries[I - 1], Boundaries[I], Fn);
419
512
  });
Unexecuted instantiation: ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)0, true> >::forEachClass(llvm::function_ref<void (unsigned long, unsigned long)>)::'lambda0'(unsigned long)::operator()(unsigned long) const
420
2
  ++Cnt;
421
2
}
ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)1, false> >::forEachClass(llvm::function_ref<void (unsigned long, unsigned long)>)
Line
Count
Source
390
6
void ICF<ELFT>::forEachClass(llvm::function_ref<void(size_t, size_t)> Fn) {
391
6
  // If threading is disabled or the number of sections are
392
6
  // too small to use threading, call Fn sequentially.
393
6
  if (!ThreadsEnabled || Sections.size() < 1024) {
394
6
    forEachClassRange(0, Sections.size(), Fn);
395
6
    ++Cnt;
396
6
    return;
397
6
  }
398
0
399
0
  Current = Cnt % 2;
400
0
  Next = (Cnt + 1) % 2;
401
0
402
0
  // Shard into non-overlapping intervals, and call Fn in parallel.
403
0
  // The sharding must be completed before any calls to Fn are made
404
0
  // so that Fn can modify the Chunks in its shard without causing data
405
0
  // races.
406
0
  const size_t NumShards = 256;
407
0
  size_t Step = Sections.size() / NumShards;
408
0
  size_t Boundaries[NumShards + 1];
409
0
  Boundaries[0] = 0;
410
0
  Boundaries[NumShards] = Sections.size();
411
0
412
0
  parallelForEachN(1, NumShards, [&](size_t I) {
413
0
    Boundaries[I] = findBoundary((I - 1) * Step, Sections.size());
414
0
  });
415
0
416
0
  parallelForEachN(1, NumShards + 1, [&](size_t I) {
417
0
    if (Boundaries[I - 1] < Boundaries[I])
418
0
      forEachClassRange(Boundaries[I - 1], Boundaries[I], Fn);
419
0
  });
420
0
  ++Cnt;
421
0
}
Unexecuted instantiation: ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)0, false> >::forEachClass(llvm::function_ref<void (unsigned long, unsigned long)>)
ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)1, true> >::forEachClass(llvm::function_ref<void (unsigned long, unsigned long)>)
Line
Count
Source
390
109
void ICF<ELFT>::forEachClass(llvm::function_ref<void(size_t, size_t)> Fn) {
391
109
  // If threading is disabled or the number of sections are
392
109
  // too small to use threading, call Fn sequentially.
393
109
  if (!ThreadsEnabled || Sections.size() < 1024) {
394
107
    forEachClassRange(0, Sections.size(), Fn);
395
107
    ++Cnt;
396
107
    return;
397
107
  }
398
2
399
2
  Current = Cnt % 2;
400
2
  Next = (Cnt + 1) % 2;
401
2
402
2
  // Shard into non-overlapping intervals, and call Fn in parallel.
403
2
  // The sharding must be completed before any calls to Fn are made
404
2
  // so that Fn can modify the Chunks in its shard without causing data
405
2
  // races.
406
2
  const size_t NumShards = 256;
407
2
  size_t Step = Sections.size() / NumShards;
408
2
  size_t Boundaries[NumShards + 1];
409
2
  Boundaries[0] = 0;
410
2
  Boundaries[NumShards] = Sections.size();
411
2
412
2
  parallelForEachN(1, NumShards, [&](size_t I) {
413
2
    Boundaries[I] = findBoundary((I - 1) * Step, Sections.size());
414
2
  });
415
2
416
2
  parallelForEachN(1, NumShards + 1, [&](size_t I) {
417
2
    if (Boundaries[I - 1] < Boundaries[I])
418
2
      forEachClassRange(Boundaries[I - 1], Boundaries[I], Fn);
419
2
  });
420
2
  ++Cnt;
421
2
}
Unexecuted instantiation: ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)0, true> >::forEachClass(llvm::function_ref<void (unsigned long, unsigned long)>)
422
423
137
static void print(const Twine &S) {
424
137
  if (Config->PrintIcfSections)
425
98
    message(S);
426
137
}
427
428
// The main function of ICF.
429
57
template <class ELFT> void ICF<ELFT>::run() {
430
57
  // Collect sections to merge.
431
57
  for (InputSectionBase *Sec : InputSections)
432
2.44k
    if (auto *S = dyn_cast<InputSection>(Sec))
433
2.43k
      if (isEligible(S))
434
2.28k
        Sections.push_back(S);
435
57
436
57
  // Initially, we use hash values to partition sections.
437
2.28k
  parallelForEach(Sections, [&](InputSection *S) {
438
2.28k
    // Set MSB to 1 to avoid collisions with non-hash IDs.
439
2.28k
    S->Class[0] = xxHash64(S->Data) | (1U << 31);
440
2.28k
  });
ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)1, false> >::run()::'lambda'(lld::elf::InputSection*)::operator()(lld::elf::InputSection*) const
Line
Count
Source
437
13
  parallelForEach(Sections, [&](InputSection *S) {
438
13
    // Set MSB to 1 to avoid collisions with non-hash IDs.
439
13
    S->Class[0] = xxHash64(S->Data) | (1U << 31);
440
13
  });
Unexecuted instantiation: ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)0, false> >::run()::'lambda'(lld::elf::InputSection*)::operator()(lld::elf::InputSection*) const
ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)1, true> >::run()::'lambda'(lld::elf::InputSection*)::operator()(lld::elf::InputSection*) const
Line
Count
Source
437
2.27k
  parallelForEach(Sections, [&](InputSection *S) {
438
2.27k
    // Set MSB to 1 to avoid collisions with non-hash IDs.
439
2.27k
    S->Class[0] = xxHash64(S->Data) | (1U << 31);
440
2.27k
  });
Unexecuted instantiation: ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)0, true> >::run()::'lambda'(lld::elf::InputSection*)::operator()(lld::elf::InputSection*) const
441
57
442
57
  // From now on, sections in Sections vector are ordered so that sections
443
57
  // in the same equivalence class are consecutive in the vector.
444
57
  std::stable_sort(Sections.begin(), Sections.end(),
445
6.65k
                   [](InputSection *A, InputSection *B) {
446
6.65k
                     return A->Class[0] < B->Class[0];
447
6.65k
                   });
ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)1, false> >::run()::'lambda'(lld::elf::InputSection*, lld::elf::InputSection*)::operator()(lld::elf::InputSection*, lld::elf::InputSection*) const
Line
Count
Source
445
16
                   [](InputSection *A, InputSection *B) {
446
16
                     return A->Class[0] < B->Class[0];
447
16
                   });
Unexecuted instantiation: ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)0, false> >::run()::'lambda'(lld::elf::InputSection*, lld::elf::InputSection*)::operator()(lld::elf::InputSection*, lld::elf::InputSection*) const
ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)1, true> >::run()::'lambda'(lld::elf::InputSection*, lld::elf::InputSection*)::operator()(lld::elf::InputSection*, lld::elf::InputSection*) const
Line
Count
Source
445
6.63k
                   [](InputSection *A, InputSection *B) {
446
6.63k
                     return A->Class[0] < B->Class[0];
447
6.63k
                   });
Unexecuted instantiation: ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)0, true> >::run()::'lambda'(lld::elf::InputSection*, lld::elf::InputSection*)::operator()(lld::elf::InputSection*, lld::elf::InputSection*) const
448
57
449
57
  // Compare static contents and assign unique IDs for each static content.
450
144
  forEachClass([&](size_t Begin, size_t End) { segregate(Begin, End, true); });
ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)1, false> >::run()::'lambda'(unsigned long, unsigned long)::operator()(unsigned long, unsigned long) const
Line
Count
Source
450
10
  forEachClass([&](size_t Begin, size_t End) { segregate(Begin, End, true); });
Unexecuted instantiation: ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)0, false> >::run()::'lambda'(unsigned long, unsigned long)::operator()(unsigned long, unsigned long) const
ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)1, true> >::run()::'lambda'(unsigned long, unsigned long)::operator()(unsigned long, unsigned long) const
Line
Count
Source
450
134
  forEachClass([&](size_t Begin, size_t End) { segregate(Begin, End, true); });
Unexecuted instantiation: ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)0, true> >::run()::'lambda'(unsigned long, unsigned long)::operator()(unsigned long, unsigned long) const
451
57
452
57
  // Split groups by comparing relocations until convergence is obtained.
453
58
  do {
454
58
    Repeat = false;
455
58
    forEachClass(
456
2.21k
        [&](size_t Begin, size_t End) { segregate(Begin, End, false); });
ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)1, false> >::run()::'lambda0'(unsigned long, unsigned long)::operator()(unsigned long, unsigned long) const
Line
Count
Source
456
10
        [&](size_t Begin, size_t End) { segregate(Begin, End, false); });
Unexecuted instantiation: ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)0, false> >::run()::'lambda0'(unsigned long, unsigned long)::operator()(unsigned long, unsigned long) const
ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)1, true> >::run()::'lambda0'(unsigned long, unsigned long)::operator()(unsigned long, unsigned long) const
Line
Count
Source
456
2.20k
        [&](size_t Begin, size_t End) { segregate(Begin, End, false); });
Unexecuted instantiation: ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)0, true> >::run()::'lambda0'(unsigned long, unsigned long)::operator()(unsigned long, unsigned long) const
457
58
  } while (Repeat);
458
57
459
57
  log("ICF needed " + Twine(Cnt) + " iterations");
460
57
461
57
  // Merge sections by the equivalence class.
462
2.20k
  forEachClassRange(0, Sections.size(), [&](size_t Begin, size_t End) {
463
2.20k
    if (End - Begin == 1)
464
2.14k
      return;
465
58
    print("selected section " + toString(Sections[Begin]));
466
137
    for (size_t I = Begin + 1; I < End; 
++I79
) {
467
79
      print("  removing identical section " + toString(Sections[I]));
468
79
      Sections[Begin]->replace(Sections[I]);
469
79
470
79
      // At this point we know sections merged are fully identical and hence
471
79
      // we want to remove duplicate implicit dependencies such as link order
472
79
      // and relocation sections.
473
79
      for (InputSection *IS : Sections[I]->DependentSections)
474
2
        IS->Live = false;
475
79
    }
476
58
  });
ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)1, false> >::run()::'lambda1'(unsigned long, unsigned long)::operator()(unsigned long, unsigned long) const
Line
Count
Source
462
10
  forEachClassRange(0, Sections.size(), [&](size_t Begin, size_t End) {
463
10
    if (End - Begin == 1)
464
7
      return;
465
3
    print("selected section " + toString(Sections[Begin]));
466
6
    for (size_t I = Begin + 1; I < End; 
++I3
) {
467
3
      print("  removing identical section " + toString(Sections[I]));
468
3
      Sections[Begin]->replace(Sections[I]);
469
3
470
3
      // At this point we know sections merged are fully identical and hence
471
3
      // we want to remove duplicate implicit dependencies such as link order
472
3
      // and relocation sections.
473
3
      for (InputSection *IS : Sections[I]->DependentSections)
474
1
        IS->Live = false;
475
3
    }
476
3
  });
Unexecuted instantiation: ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)0, false> >::run()::'lambda1'(unsigned long, unsigned long)::operator()(unsigned long, unsigned long) const
ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)1, true> >::run()::'lambda1'(unsigned long, unsigned long)::operator()(unsigned long, unsigned long) const
Line
Count
Source
462
2.19k
  forEachClassRange(0, Sections.size(), [&](size_t Begin, size_t End) {
463
2.19k
    if (End - Begin == 1)
464
2.14k
      return;
465
55
    print("selected section " + toString(Sections[Begin]));
466
131
    for (size_t I = Begin + 1; I < End; 
++I76
) {
467
76
      print("  removing identical section " + toString(Sections[I]));
468
76
      Sections[Begin]->replace(Sections[I]);
469
76
470
76
      // At this point we know sections merged are fully identical and hence
471
76
      // we want to remove duplicate implicit dependencies such as link order
472
76
      // and relocation sections.
473
76
      for (InputSection *IS : Sections[I]->DependentSections)
474
1
        IS->Live = false;
475
76
    }
476
55
  });
Unexecuted instantiation: ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)0, true> >::run()::'lambda1'(unsigned long, unsigned long)::operator()(unsigned long, unsigned long) const
477
57
}
ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)1, false> >::run()
Line
Count
Source
429
3
template <class ELFT> void ICF<ELFT>::run() {
430
3
  // Collect sections to merge.
431
3
  for (InputSectionBase *Sec : InputSections)
432
18
    if (auto *S = dyn_cast<InputSection>(Sec))
433
18
      if (isEligible(S))
434
13
        Sections.push_back(S);
435
3
436
3
  // Initially, we use hash values to partition sections.
437
3
  parallelForEach(Sections, [&](InputSection *S) {
438
3
    // Set MSB to 1 to avoid collisions with non-hash IDs.
439
3
    S->Class[0] = xxHash64(S->Data) | (1U << 31);
440
3
  });
441
3
442
3
  // From now on, sections in Sections vector are ordered so that sections
443
3
  // in the same equivalence class are consecutive in the vector.
444
3
  std::stable_sort(Sections.begin(), Sections.end(),
445
3
                   [](InputSection *A, InputSection *B) {
446
3
                     return A->Class[0] < B->Class[0];
447
3
                   });
448
3
449
3
  // Compare static contents and assign unique IDs for each static content.
450
3
  forEachClass([&](size_t Begin, size_t End) { segregate(Begin, End, true); });
451
3
452
3
  // Split groups by comparing relocations until convergence is obtained.
453
3
  do {
454
3
    Repeat = false;
455
3
    forEachClass(
456
3
        [&](size_t Begin, size_t End) { segregate(Begin, End, false); });
457
3
  } while (Repeat);
458
3
459
3
  log("ICF needed " + Twine(Cnt) + " iterations");
460
3
461
3
  // Merge sections by the equivalence class.
462
3
  forEachClassRange(0, Sections.size(), [&](size_t Begin, size_t End) {
463
3
    if (End - Begin == 1)
464
3
      return;
465
3
    print("selected section " + toString(Sections[Begin]));
466
3
    for (size_t I = Begin + 1; I < End; ++I) {
467
3
      print("  removing identical section " + toString(Sections[I]));
468
3
      Sections[Begin]->replace(Sections[I]);
469
3
470
3
      // At this point we know sections merged are fully identical and hence
471
3
      // we want to remove duplicate implicit dependencies such as link order
472
3
      // and relocation sections.
473
3
      for (InputSection *IS : Sections[I]->DependentSections)
474
3
        IS->Live = false;
475
3
    }
476
3
  });
477
3
}
Unexecuted instantiation: ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)0, false> >::run()
ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)1, true> >::run()
Line
Count
Source
429
54
template <class ELFT> void ICF<ELFT>::run() {
430
54
  // Collect sections to merge.
431
54
  for (InputSectionBase *Sec : InputSections)
432
2.42k
    if (auto *S = dyn_cast<InputSection>(Sec))
433
2.42k
      if (isEligible(S))
434
2.27k
        Sections.push_back(S);
435
54
436
54
  // Initially, we use hash values to partition sections.
437
54
  parallelForEach(Sections, [&](InputSection *S) {
438
54
    // Set MSB to 1 to avoid collisions with non-hash IDs.
439
54
    S->Class[0] = xxHash64(S->Data) | (1U << 31);
440
54
  });
441
54
442
54
  // From now on, sections in Sections vector are ordered so that sections
443
54
  // in the same equivalence class are consecutive in the vector.
444
54
  std::stable_sort(Sections.begin(), Sections.end(),
445
54
                   [](InputSection *A, InputSection *B) {
446
54
                     return A->Class[0] < B->Class[0];
447
54
                   });
448
54
449
54
  // Compare static contents and assign unique IDs for each static content.
450
54
  forEachClass([&](size_t Begin, size_t End) { segregate(Begin, End, true); });
451
54
452
54
  // Split groups by comparing relocations until convergence is obtained.
453
55
  do {
454
55
    Repeat = false;
455
55
    forEachClass(
456
55
        [&](size_t Begin, size_t End) { segregate(Begin, End, false); });
457
55
  } while (Repeat);
458
54
459
54
  log("ICF needed " + Twine(Cnt) + " iterations");
460
54
461
54
  // Merge sections by the equivalence class.
462
54
  forEachClassRange(0, Sections.size(), [&](size_t Begin, size_t End) {
463
54
    if (End - Begin == 1)
464
54
      return;
465
54
    print("selected section " + toString(Sections[Begin]));
466
54
    for (size_t I = Begin + 1; I < End; ++I) {
467
54
      print("  removing identical section " + toString(Sections[I]));
468
54
      Sections[Begin]->replace(Sections[I]);
469
54
470
54
      // At this point we know sections merged are fully identical and hence
471
54
      // we want to remove duplicate implicit dependencies such as link order
472
54
      // and relocation sections.
473
54
      for (InputSection *IS : Sections[I]->DependentSections)
474
54
        IS->Live = false;
475
54
    }
476
54
  });
477
54
}
Unexecuted instantiation: ICF.cpp:(anonymous namespace)::ICF<llvm::object::ELFType<(llvm::support::endianness)0, true> >::run()
478
479
// ICF entry point function.
480
57
template <class ELFT> void elf::doIcf() { ICF<ELFT>().run(); }
void lld::elf::doIcf<llvm::object::ELFType<(llvm::support::endianness)1, false> >()
Line
Count
Source
480
3
template <class ELFT> void elf::doIcf() { ICF<ELFT>().run(); }
Unexecuted instantiation: void lld::elf::doIcf<llvm::object::ELFType<(llvm::support::endianness)0, false> >()
void lld::elf::doIcf<llvm::object::ELFType<(llvm::support::endianness)1, true> >()
Line
Count
Source
480
54
template <class ELFT> void elf::doIcf() { ICF<ELFT>().run(); }
Unexecuted instantiation: void lld::elf::doIcf<llvm::object::ELFType<(llvm::support::endianness)0, true> >()
481
482
template void elf::doIcf<ELF32LE>();
483
template void elf::doIcf<ELF32BE>();
484
template void elf::doIcf<ELF64LE>();
485
template void elf::doIcf<ELF64BE>();