Do not expose ICF class from the file.

Also this patch uses file-scope functions instead of class member function.

Now that ICF class is not visible from outside, InputSection class
can no longer be "friend" of it. So I removed the friend relation
and just make it expose the features to public.

llvm-svn: 287480
This commit is contained in:
Rui Ueyama 2016-11-20 02:39:59 +00:00
parent b14fc390dc
commit bd1f0630a8
2 changed files with 40 additions and 88 deletions

View File

@ -57,13 +57,11 @@
#include "ICF.h" #include "ICF.h"
#include "Config.h" #include "Config.h"
#include "OutputSections.h"
#include "SymbolTable.h" #include "SymbolTable.h"
#include "llvm/ADT/Hashing.h" #include "llvm/ADT/Hashing.h"
#include "llvm/Object/ELF.h" #include "llvm/Object/ELF.h"
#include "llvm/Support/ELF.h" #include "llvm/Support/ELF.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm> #include <algorithm>
using namespace lld; using namespace lld;
@ -72,81 +70,46 @@ using namespace llvm;
using namespace llvm::ELF; using namespace llvm::ELF;
using namespace llvm::object; using namespace llvm::object;
namespace lld { namespace {
namespace elf {
template <class ELFT> class ICF { template <class ELFT> class ICF {
typedef typename ELFT::Shdr Elf_Shdr;
typedef typename ELFT::Sym Elf_Sym;
typedef typename ELFT::uint uintX_t;
typedef Elf_Rel_Impl<ELFT, false> Elf_Rel;
using Comparator = std::function<bool(const InputSection<ELFT> *,
const InputSection<ELFT> *)>;
public: public:
void run(); void run();
private: private:
uint64_t NextId = 1; uint64_t NextId = 1;
static void setLive(SymbolTable<ELFT> *S); using Comparator = std::function<bool(const InputSection<ELFT> *,
static uint64_t relSize(InputSection<ELFT> *S); const InputSection<ELFT> *)>;
static uint64_t getHash(InputSection<ELFT> *S);
static bool isEligible(InputSectionBase<ELFT> *Sec);
static std::vector<InputSection<ELFT> *> getSections();
void segregate(MutableArrayRef<InputSection<ELFT> *> Arr, Comparator Eq); void segregate(MutableArrayRef<InputSection<ELFT> *> Arr, Comparator Eq);
void void
forEachGroup(std::vector<InputSection<ELFT> *> &V, forEachGroup(std::vector<InputSection<ELFT> *> &V,
std::function<void(MutableArrayRef<InputSection<ELFT> *>)> Fn); std::function<void(MutableArrayRef<InputSection<ELFT> *>)> Fn);
template <class RelTy>
static bool relocationEq(ArrayRef<RelTy> RA, ArrayRef<RelTy> RB);
template <class RelTy>
static bool variableEq(const InputSection<ELFT> *A, ArrayRef<RelTy> RA,
const InputSection<ELFT> *B, ArrayRef<RelTy> RB);
static bool equalsConstant(const InputSection<ELFT> *A,
const InputSection<ELFT> *B);
static bool equalsVariable(const InputSection<ELFT> *A,
const InputSection<ELFT> *B);
}; };
} }
}
// Returns a hash value for S. Note that the information about // Returns a hash value for S. Note that the information about
// relocation targets is not included in the hash value. // relocation targets is not included in the hash value.
template <class ELFT> uint64_t ICF<ELFT>::getHash(InputSection<ELFT> *S) { template <class ELFT> static uint64_t getHash(InputSection<ELFT> *S) {
return hash_combine(S->Flags, S->getSize(), S->NumRelocations); return hash_combine(S->Flags, S->getSize(), S->NumRelocations);
} }
// Returns true if Sec is subject of ICF. // Returns true if section S is subject of ICF.
template <class ELFT> bool ICF<ELFT>::isEligible(InputSectionBase<ELFT> *Sec) { template <class ELFT> static bool isEligible(InputSection<ELFT> *S) {
if (!Sec->Live)
return false;
auto *S = dyn_cast<InputSection<ELFT>>(Sec);
if (!S)
return false;
// .init and .fini contains instructions that must be executed to // .init and .fini contains instructions that must be executed to
// initialize and finalize the process. They cannot and should not // initialize and finalize the process. They cannot and should not
// be merged. // be merged.
StringRef Name = S->Name; return S->Live && (S->Flags & SHF_ALLOC) && !(S->Flags & SHF_WRITE) &&
if (Name == ".init" || Name == ".fini") S->Name != ".init" && S->Name != ".fini";
return false;
return (S->Flags & SHF_ALLOC) && !(S->Flags & SHF_WRITE);
} }
template <class ELFT> template <class ELFT> static std::vector<InputSection<ELFT> *> getSections() {
std::vector<InputSection<ELFT> *> ICF<ELFT>::getSections() {
std::vector<InputSection<ELFT> *> V; std::vector<InputSection<ELFT> *> V;
for (InputSectionBase<ELFT> *S : Symtab<ELFT>::X->Sections) for (InputSectionBase<ELFT> *Sec : Symtab<ELFT>::X->Sections)
if (auto *S = dyn_cast<InputSection<ELFT>>(Sec))
if (isEligible(S)) if (isEligible(S))
V.push_back(cast<InputSection<ELFT>>(S)); V.push_back(S);
return V; return V;
} }
@ -188,9 +151,8 @@ void ICF<ELFT>::forEachGroup(
} }
// Compare two lists of relocations. // Compare two lists of relocations.
template <class ELFT> template <class ELFT, class RelTy>
template <class RelTy> static bool relocationEq(ArrayRef<RelTy> RelsA, ArrayRef<RelTy> RelsB) {
bool ICF<ELFT>::relocationEq(ArrayRef<RelTy> RelsA, ArrayRef<RelTy> RelsB) {
auto Eq = [](const RelTy &A, const RelTy &B) { auto Eq = [](const RelTy &A, const RelTy &B) {
return A.r_offset == B.r_offset && return A.r_offset == B.r_offset &&
A.getType(Config->Mips64EL) == B.getType(Config->Mips64EL) && A.getType(Config->Mips64EL) == B.getType(Config->Mips64EL) &&
@ -204,30 +166,23 @@ bool ICF<ELFT>::relocationEq(ArrayRef<RelTy> RelsA, ArrayRef<RelTy> RelsB) {
// Compare "non-moving" part of two InputSections, namely everything // Compare "non-moving" part of two InputSections, namely everything
// except relocation targets. // except relocation targets.
template <class ELFT> template <class ELFT>
bool ICF<ELFT>::equalsConstant(const InputSection<ELFT> *A, static bool equalsConstant(const InputSection<ELFT> *A,
const InputSection<ELFT> *B) { const InputSection<ELFT> *B) {
if (A->NumRelocations != B->NumRelocations) if (A->NumRelocations != B->NumRelocations || A->Flags != B->Flags ||
A->getSize() != B->getSize() || A->Data != B->Data)
return false; return false;
if (A->AreRelocsRela) { if (A->AreRelocsRela)
if (!relocationEq(A->relas(), B->relas())) return relocationEq<ELFT>(A->relas(), B->relas());
return false; return relocationEq<ELFT>(A->rels(), B->rels());
} else {
if (!relocationEq(A->rels(), B->rels()))
return false;
} }
return A->Flags == B->Flags && A->getSize() == B->getSize() && template <class ELFT, class RelTy>
A->Data == B->Data; static bool variableEq(const InputSection<ELFT> *A, ArrayRef<RelTy> RelsA,
}
template <class ELFT>
template <class RelTy>
bool ICF<ELFT>::variableEq(const InputSection<ELFT> *A, ArrayRef<RelTy> RelsA,
const InputSection<ELFT> *B, ArrayRef<RelTy> RelsB) { const InputSection<ELFT> *B, ArrayRef<RelTy> RelsB) {
auto Eq = [&](const RelTy &RA, const RelTy &RB) { auto Eq = [&](const RelTy &RA, const RelTy &RB) {
SymbolBody &SA = A->File->getRelocTargetSym(RA); SymbolBody &SA = A->getFile()->getRelocTargetSym(RA);
SymbolBody &SB = B->File->getRelocTargetSym(RB); SymbolBody &SB = B->getFile()->getRelocTargetSym(RB);
if (&SA == &SB) if (&SA == &SB)
return true; return true;
@ -249,7 +204,7 @@ bool ICF<ELFT>::variableEq(const InputSection<ELFT> *A, ArrayRef<RelTy> RelsA,
// Compare "moving" part of two InputSections, namely relocation targets. // Compare "moving" part of two InputSections, namely relocation targets.
template <class ELFT> template <class ELFT>
bool ICF<ELFT>::equalsVariable(const InputSection<ELFT> *A, static bool equalsVariable(const InputSection<ELFT> *A,
const InputSection<ELFT> *B) { const InputSection<ELFT> *B) {
if (A->AreRelocsRela) if (A->AreRelocsRela)
return variableEq(A, A->relas(), B, B->relas()); return variableEq(A, A->relas(), B, B->relas());
@ -261,7 +216,7 @@ template <class ELFT> void ICF<ELFT>::run() {
// Initially, we use hash values as section group IDs. Therefore, // Initially, we use hash values as section group IDs. Therefore,
// if two sections have the same ID, they are likely (but not // if two sections have the same ID, they are likely (but not
// guaranteed) to have the same static contents in terms of ICF. // guaranteed) to have the same static contents in terms of ICF.
std::vector<InputSection<ELFT> *> Sections = getSections(); std::vector<InputSection<ELFT> *> Sections = getSections<ELFT>();
for (InputSection<ELFT> *S : Sections) for (InputSection<ELFT> *S : Sections)
// Set MSB on to avoid collisions with serial group IDs // Set MSB on to avoid collisions with serial group IDs
S->GroupId = getHash(S) | (uint64_t(1) << 63); S->GroupId = getHash(S) | (uint64_t(1) << 63);
@ -279,7 +234,7 @@ template <class ELFT> void ICF<ELFT>::run() {
// Compare static contents and assign unique IDs for each static content. // Compare static contents and assign unique IDs for each static content.
forEachGroup(Sections, [&](MutableArrayRef<InputSection<ELFT> *> V) { forEachGroup(Sections, [&](MutableArrayRef<InputSection<ELFT> *> V) {
segregate(V, equalsConstant); segregate(V, equalsConstant<ELFT>);
}); });
// Split groups by comparing relocations until we get a convergence. // Split groups by comparing relocations until we get a convergence.
@ -288,7 +243,7 @@ template <class ELFT> void ICF<ELFT>::run() {
++Cnt; ++Cnt;
uint64_t Id = NextId; uint64_t Id = NextId;
forEachGroup(Sections, [&](MutableArrayRef<InputSection<ELFT> *> V) { forEachGroup(Sections, [&](MutableArrayRef<InputSection<ELFT> *> V) {
segregate(V, equalsVariable); segregate(V, equalsVariable<ELFT>);
}); });
if (Id == NextId) if (Id == NextId)
break; break;
@ -296,12 +251,11 @@ template <class ELFT> void ICF<ELFT>::run() {
log("ICF needed " + Twine(Cnt) + " iterations."); log("ICF needed " + Twine(Cnt) + " iterations.");
// Merge sections in the same group. // Merge sections in the same group.
forEachGroup(Sections, [&](MutableArrayRef<InputSection<ELFT> *> V) { forEachGroup(Sections, [](MutableArrayRef<InputSection<ELFT> *> V) {
InputSection<ELFT> *Head = V[0]; log("selected " + V[0]->Name);
log("selected " + Head->Name);
for (InputSection<ELFT> *S : V.slice(1)) { for (InputSection<ELFT> *S : V.slice(1)) {
log(" removed " + S->Name); log(" removed " + S->Name);
Head->replace(S); V[0]->replace(S);
} }
}); });
} }

View File

@ -26,7 +26,6 @@ class DefinedCommon;
class SymbolBody; class SymbolBody;
struct SectionPiece; struct SectionPiece;
template <class ELFT> class ICF;
template <class ELFT> class DefinedRegular; template <class ELFT> class DefinedRegular;
template <class ELFT> class ObjectFile; template <class ELFT> class ObjectFile;
template <class ELFT> class OutputSection; template <class ELFT> class OutputSection;
@ -238,7 +237,6 @@ public:
// This corresponds to a non SHF_MERGE section of an input file. // This corresponds to a non SHF_MERGE section of an input file.
template <class ELFT> class InputSection : public InputSectionBase<ELFT> { template <class ELFT> class InputSection : public InputSectionBase<ELFT> {
friend ICF<ELFT>;
typedef InputSectionBase<ELFT> Base; typedef InputSectionBase<ELFT> Base;
typedef typename ELFT::Shdr Elf_Shdr; typedef typename ELFT::Shdr Elf_Shdr;
typedef typename ELFT::Rela Elf_Rela; typedef typename ELFT::Rela Elf_Rela;
@ -285,15 +283,15 @@ public:
template <class RelTy> template <class RelTy>
void relocateNonAlloc(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels); void relocateNonAlloc(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels);
private: // Used by ICF.
template <class RelTy> uint64_t GroupId = 0;
void copyRelocations(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels);
// Called by ICF to merge two input sections. // Called by ICF to merge two input sections.
void replace(InputSection<ELFT> *Other); void replace(InputSection<ELFT> *Other);
// Used by ICF. private:
uint64_t GroupId = 0; template <class RelTy>
void copyRelocations(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels);
llvm::TinyPtrVector<const Thunk<ELFT> *> Thunks; llvm::TinyPtrVector<const Thunk<ELFT> *> Thunks;
}; };