Coverage Report

Created: 2019-01-21 03:01

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/COFF/ICF.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- ICF.cpp ------------------------------------------------------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// ICF is short for Identical Code Folding. That is a size optimization to
10
// identify and merge two or more read-only sections (typically functions)
11
// that happened to have the same contents. It usually reduces output size
12
// by a few percent.
13
//
14
// On Windows, ICF is enabled by default.
15
//
16
// See ELF/ICF.cpp for the details about the algortihm.
17
//
18
//===----------------------------------------------------------------------===//
19
20
#include "ICF.h"
21
#include "Chunks.h"
22
#include "Symbols.h"
23
#include "lld/Common/ErrorHandler.h"
24
#include "lld/Common/Threads.h"
25
#include "lld/Common/Timer.h"
26
#include "llvm/ADT/Hashing.h"
27
#include "llvm/Support/Debug.h"
28
#include "llvm/Support/Parallel.h"
29
#include "llvm/Support/raw_ostream.h"
30
#include "llvm/Support/xxhash.h"
31
#include <algorithm>
32
#include <atomic>
33
#include <vector>
34
35
using namespace llvm;
36
37
namespace lld {
38
namespace coff {
39
40
static Timer ICFTimer("ICF", Timer::root());
41
42
class ICF {
43
public:
44
  void run(ArrayRef<Chunk *> V);
45
46
private:
47
  void segregate(size_t Begin, size_t End, bool Constant);
48
49
  bool assocEquals(const SectionChunk *A, const SectionChunk *B);
50
51
  bool equalsConstant(const SectionChunk *A, const SectionChunk *B);
52
  bool equalsVariable(const SectionChunk *A, const SectionChunk *B);
53
54
  uint32_t getHash(SectionChunk *C);
55
  bool isEligible(SectionChunk *C);
56
57
  size_t findBoundary(size_t Begin, size_t End);
58
59
  void forEachClassRange(size_t Begin, size_t End,
60
                         std::function<void(size_t, size_t)> Fn);
61
62
  void forEachClass(std::function<void(size_t, size_t)> Fn);
63
64
  std::vector<SectionChunk *> Chunks;
65
  int Cnt = 0;
66
  std::atomic<bool> Repeat = {false};
67
};
68
69
// Returns true if section S is subject of ICF.
70
//
71
// Microsoft's documentation
72
// (https://msdn.microsoft.com/en-us/library/bxwfs976.aspx; visited April
73
// 2017) says that /opt:icf folds both functions and read-only data.
74
// Despite that, the MSVC linker folds only functions. We found
75
// a few instances of programs that are not safe for data merging.
76
// Therefore, we merge only functions just like the MSVC tool. However, we also
77
// merge read-only sections in a couple of cases where the address of the
78
// section is insignificant to the user program and the behaviour matches that
79
// of the Visual C++ linker.
80
1.36k
bool ICF::isEligible(SectionChunk *C) {
81
1.36k
  // Non-comdat chunks, dead chunks, and writable chunks are not elegible.
82
1.36k
  bool Writable = C->getOutputCharacteristics() & llvm::COFF::IMAGE_SCN_MEM_WRITE;
83
1.36k
  if (!C->isCOMDAT() || 
!C->Live213
||
Writable191
)
84
1.17k
    return false;
85
183
86
183
  // Code sections are eligible.
87
183
  if (C->getOutputCharacteristics() & llvm::COFF::IMAGE_SCN_MEM_EXECUTE)
88
104
    return true;
89
79
90
79
  // .pdata and .xdata unwind info sections are eligible.
91
79
  StringRef OutSecName = C->getSectionName().split('$').first;
92
79
  if (OutSecName == ".pdata" || 
OutSecName == ".xdata"62
)
93
35
    return true;
94
44
95
44
  // So are vtables.
96
44
  if (C->Sym && 
C->Sym->getName().startswith("??_7")33
)
97
2
    return true;
98
42
99
42
  // Anything else not in an address-significance table is eligible.
100
42
  return !C->KeepUnique;
101
42
}
102
103
// Split an equivalence class into smaller classes.
104
233
void ICF::segregate(size_t Begin, size_t End, bool Constant) {
105
467
  while (Begin < End) {
106
234
    // Divide [Begin, End) into two. Let Mid be the start index of the
107
234
    // second group.
108
234
    auto Bound = std::stable_partition(
109
234
        Chunks.begin() + Begin + 1, Chunks.begin() + End, [&](SectionChunk *S) {
110
53
          if (Constant)
111
27
            return equalsConstant(Chunks[Begin], S);
112
26
          return equalsVariable(Chunks[Begin], S);
113
26
        });
114
234
    size_t Mid = Bound - Chunks.begin();
115
234
116
234
    // Split [Begin, End) into [Begin, Mid) and [Mid, End). We use Mid as an
117
234
    // equivalence class ID because every group ends with a unique index.
118
520
    for (size_t I = Begin; I < Mid; 
++I286
)
119
286
      Chunks[I]->Class[(Cnt + 1) % 2] = Mid;
120
234
121
234
    // If we created a group, we need to iterate the main loop again.
122
234
    if (Mid != End)
123
1
      Repeat = true;
124
234
125
234
    Begin = Mid;
126
234
  }
127
233
}
128
129
// Returns true if two sections' associative children are equal.
130
53
bool ICF::assocEquals(const SectionChunk *A, const SectionChunk *B) {
131
106
  auto ChildClasses = [&](const SectionChunk *SC) {
132
106
    std::vector<uint32_t> Classes;
133
106
    for (const SectionChunk *C : SC->children())
134
22
      if (!C->SectionName.startswith(".debug") &&
135
22
          
C->SectionName != ".gfids$y"16
&&
C->SectionName != ".gljmp$y"14
)
136
12
        Classes.push_back(C->Class[Cnt % 2]);
137
106
    return Classes;
138
106
  };
139
53
  return ChildClasses(A) == ChildClasses(B);
140
53
}
141
142
// Compare "non-moving" part of two sections, namely everything
143
// except relocation targets.
144
27
bool ICF::equalsConstant(const SectionChunk *A, const SectionChunk *B) {
145
27
  if (A->Relocs.size() != B->Relocs.size())
146
0
    return false;
147
27
148
27
  // Compare relocations.
149
27
  auto Eq = [&](const coff_relocation &R1, const coff_relocation &R2) {
150
11
    if (R1.Type != R2.Type ||
151
11
        R1.VirtualAddress != R2.VirtualAddress) {
152
0
      return false;
153
0
    }
154
11
    Symbol *B1 = A->File->getSymbol(R1.SymbolTableIndex);
155
11
    Symbol *B2 = B->File->getSymbol(R2.SymbolTableIndex);
156
11
    if (B1 == B2)
157
6
      return true;
158
5
    if (auto *D1 = dyn_cast<DefinedRegular>(B1))
159
5
      if (auto *D2 = dyn_cast<DefinedRegular>(B2))
160
5
        return D1->getValue() == D2->getValue() &&
161
5
               D1->getChunk()->Class[Cnt % 2] == D2->getChunk()->Class[Cnt % 2];
162
0
    return false;
163
0
  };
164
27
  if (!std::equal(A->Relocs.begin(), A->Relocs.end(), B->Relocs.begin(), Eq))
165
0
    return false;
166
27
167
27
  // Compare section attributes and contents.
168
27
  return A->getOutputCharacteristics() == B->getOutputCharacteristics() &&
169
27
         A->SectionName == B->SectionName &&
170
27
         A->Header->SizeOfRawData == B->Header->SizeOfRawData &&
171
27
         A->Checksum == B->Checksum && A->getContents() == B->getContents() &&
172
27
         assocEquals(A, B);
173
27
}
174
175
// Compare "moving" part of two sections, namely relocation targets.
176
26
bool ICF::equalsVariable(const SectionChunk *A, const SectionChunk *B) {
177
26
  // Compare relocations.
178
26
  auto Eq = [&](const coff_relocation &R1, const coff_relocation &R2) {
179
9
    Symbol *B1 = A->File->getSymbol(R1.SymbolTableIndex);
180
9
    Symbol *B2 = B->File->getSymbol(R2.SymbolTableIndex);
181
9
    if (B1 == B2)
182
4
      return true;
183
5
    if (auto *D1 = dyn_cast<DefinedRegular>(B1))
184
5
      if (auto *D2 = dyn_cast<DefinedRegular>(B2))
185
5
        return D1->getChunk()->Class[Cnt % 2] == D2->getChunk()->Class[Cnt % 2];
186
0
    return false;
187
0
  };
188
26
  return std::equal(A->Relocs.begin(), A->Relocs.end(), B->Relocs.begin(),
189
26
                    Eq) &&
190
26
         assocEquals(A, B);
191
26
}
192
193
// Find the first Chunk after Begin that has a different class from Begin.
194
350
size_t ICF::findBoundary(size_t Begin, size_t End) {
195
429
  for (size_t I = Begin + 1; I < End; 
++I79
)
196
228
    if (Chunks[Begin]->Class[Cnt % 2] != Chunks[I]->Class[Cnt % 2])
197
149
      return I;
198
350
  
return End201
;
199
350
}
200
201
void ICF::forEachClassRange(size_t Begin, size_t End,
202
1.10k
                            std::function<void(size_t, size_t)> Fn) {
203
1.45k
  while (Begin < End) {
204
350
    size_t Mid = findBoundary(Begin, End);
205
350
    Fn(Begin, Mid);
206
350
    Begin = Mid;
207
350
  }
208
1.10k
}
209
210
// Call Fn on each class group.
211
1.10k
void ICF::forEachClass(std::function<void(size_t, size_t)> Fn) {
212
1.10k
  // If the number of sections are too small to use threading,
213
1.10k
  // call Fn sequentially.
214
1.10k
  if (Chunks.size() < 1024) {
215
1.10k
    forEachClassRange(0, Chunks.size(), Fn);
216
1.10k
    ++Cnt;
217
1.10k
    return;
218
1.10k
  }
219
0
220
0
  // Shard into non-overlapping intervals, and call Fn in parallel.
221
0
  // The sharding must be completed before any calls to Fn are made
222
0
  // so that Fn can modify the Chunks in its shard without causing data
223
0
  // races.
224
0
  const size_t NumShards = 256;
225
0
  size_t Step = Chunks.size() / NumShards;
226
0
  size_t Boundaries[NumShards + 1];
227
0
  Boundaries[0] = 0;
228
0
  Boundaries[NumShards] = Chunks.size();
229
0
  parallelForEachN(1, NumShards, [&](size_t I) {
230
0
    Boundaries[I] = findBoundary((I - 1) * Step, Chunks.size());
231
0
  });
232
0
  parallelForEachN(1, NumShards + 1, [&](size_t I) {
233
0
    if (Boundaries[I - 1] < Boundaries[I]) {
234
0
      forEachClassRange(Boundaries[I - 1], Boundaries[I], Fn);
235
0
    }
236
0
  });
237
0
  ++Cnt;
238
0
}
239
240
// Merge identical COMDAT sections.
241
// Two sections are considered the same if their section headers,
242
// contents and relocations are all the same.
243
367
void ICF::run(ArrayRef<Chunk *> Vec) {
244
367
  ScopedTimer T(ICFTimer);
245
367
246
367
  // Collect only mergeable sections and group by hash value.
247
367
  uint32_t NextId = 1;
248
1.37k
  for (Chunk *C : Vec) {
249
1.37k
    if (auto *SC = dyn_cast<SectionChunk>(C)) {
250
1.36k
      if (isEligible(SC))
251
143
        Chunks.push_back(SC);
252
1.21k
      else
253
1.21k
        SC->Class[0] = NextId++;
254
1.36k
    }
255
1.37k
  }
256
367
257
367
  // Make sure that ICF doesn't merge sections that are being handled by string
258
367
  // tail merging.
259
367
  for (auto &P : MergeChunk::Instances)
260
3
    for (SectionChunk *SC : P.second->Sections)
261
7
      SC->Class[0] = NextId++;
262
367
263
367
  // Initially, we use hash values to partition sections.
264
367
  parallelForEach(Chunks, [&](SectionChunk *SC) {
265
143
    SC->Class[1] = xxHash64(SC->getContents());
266
143
  });
267
367
268
367
  // Combine the hashes of the sections referenced by each section into its
269
367
  // hash.
270
367
  parallelForEach(Chunks, [&](SectionChunk *SC) {
271
143
    uint32_t Hash = SC->Class[1];
272
143
    for (Symbol *B : SC->symbols())
273
108
      if (auto *Sym = dyn_cast_or_null<DefinedRegular>(B))
274
105
        Hash ^= Sym->getChunk()->Class[1];
275
143
    // Set MSB to 1 to avoid collisions with non-hash classs.
276
143
    SC->Class[0] = Hash | (1U << 31);
277
143
  });
278
367
279
367
  // From now on, sections in Chunks are ordered so that sections in
280
367
  // the same group are consecutive in the vector.
281
367
  std::stable_sort(Chunks.begin(), Chunks.end(),
282
367
                   [](SectionChunk *A, SectionChunk *B) {
283
126
                     return A->Class[0] < B->Class[0];
284
126
                   });
285
367
286
367
  // Compare static contents and assign unique IDs for each static content.
287
367
  forEachClass([&](size_t Begin, size_t End) 
{ segregate(Begin, End, true); }116
);
288
367
289
367
  // Split groups by comparing relocations until convergence is obtained.
290
367
  do {
291
367
    Repeat = false;
292
367
    forEachClass(
293
367
        [&](size_t Begin, size_t End) 
{ segregate(Begin, End, false); }117
);
294
367
  } while (Repeat);
295
367
296
367
  log("ICF needed " + Twine(Cnt) + " iterations");
297
367
298
367
  // Merge sections in the same classs.
299
367
  forEachClass([&](size_t Begin, size_t End) {
300
117
    if (End - Begin == 1)
301
96
      return;
302
21
303
21
    log("Selected " + Chunks[Begin]->getDebugName());
304
47
    for (size_t I = Begin + 1; I < End; 
++I26
) {
305
26
      log("  Removed " + Chunks[I]->getDebugName());
306
26
      Chunks[Begin]->replace(Chunks[I]);
307
26
    }
308
21
  });
309
367
}
310
311
// Entry point to ICF.
312
367
void doICF(ArrayRef<Chunk *> Chunks) { ICF().run(Chunks); }
313
314
} // namespace coff
315
} // namespace lld