Commit 824a2881 authored by Jez Ng's avatar Jez Ng
Browse files

[lld-macho][nfc] Simplify MarkLive.cpp using `if constexpr`

No significant perf diff, as expected.

             base           diff           difference (95% CI)
  sys_time   1.722 ± 0.030  1.727 ± 0.027  [  -0.6% ..   +1.2%]
  user_time  5.081 ± 0.032  5.087 ± 0.030  [  -0.2% ..   +0.4%]
  wall_time  6.008 ± 0.056  6.029 ± 0.053  [  -0.1% ..   +0.8%]
  samples    25             37

Reviewed By: #lld-macho, oontvoo, thakis, BertalanD

Differential Revision: https://reviews.llvm.org/D133014
parent ada51eed
Loading
Loading
Loading
Loading
+37 −51
Original line number Diff line number Diff line
@@ -57,7 +57,6 @@ public:
private:
  void enqueue(InputSection *isec, uint64_t off, const WorklistEntry *prev);
  void addSym(Symbol *s, const WorklistEntry *prev);
  void printWhyLive(Symbol *s, const WorklistEntry *prev);
  const InputSection *getInputSection(const WorklistEntry *) const;
  WorklistEntry *makeEntry(InputSection *, const WorklistEntry *prev) const;

@@ -82,23 +81,7 @@ void MarkLiveImpl<RecordWhyLive>::enqueue(
  }
}

template <bool RecordWhyLive>
void MarkLiveImpl<RecordWhyLive>::addSym(
    Symbol *s,
    const typename MarkLiveImpl<RecordWhyLive>::WorklistEntry *prev) {
  if (s->used)
    return;
  s->used = true;
  printWhyLive(s, prev);
  if (auto *d = dyn_cast<Defined>(s)) {
    if (d->isec)
      enqueue(d->isec, d->value, prev);
    if (d->unwindEntry)
      enqueue(d->unwindEntry, 0, prev);
  }
}

static void printWhyLiveImpl(const Symbol *s, const WhyLiveEntry *prev) {
static void printWhyLive(const Symbol *s, const WhyLiveEntry *prev) {
  std::string out = toString(*s) + " from " + toString(s->getFile());
  int indent = 2;
  for (const WhyLiveEntry *entry = prev; entry;
@@ -113,45 +96,48 @@ static void printWhyLiveImpl(const Symbol *s, const WhyLiveEntry *prev) {
  message(out);
}

// NOTE: if/when `constexpr if` becomes available, we can simplify a lot of
// the partial template specializations below.

template <>
void MarkLiveImpl<true>::printWhyLive(Symbol *s, const WhyLiveEntry *prev) {
template <bool RecordWhyLive>
void MarkLiveImpl<RecordWhyLive>::addSym(
    Symbol *s,
    const typename MarkLiveImpl<RecordWhyLive>::WorklistEntry *prev) {
  if (s->used)
    return;
  s->used = true;
  if constexpr (RecordWhyLive)
    if (!config->whyLive.empty() && config->whyLive.match(s->getName()))
    printWhyLiveImpl(s, prev);
      printWhyLive(s, prev);
  if (auto *d = dyn_cast<Defined>(s)) {
    if (d->isec)
      enqueue(d->isec, d->value, prev);
    if (d->unwindEntry)
      enqueue(d->unwindEntry, 0, prev);
  }

template <>
void MarkLiveImpl<false>::printWhyLive(Symbol *s, const InputSection *prev) {}

template <>
const InputSection *
MarkLiveImpl<true>::getInputSection(const WhyLiveEntry *entry) const {
  return entry->isec;
}

template <>
const InputSection *
MarkLiveImpl<false>::getInputSection(const InputSection *isec) const {
  return isec;
template <bool RecordWhyLive>
const InputSection *MarkLiveImpl<RecordWhyLive>::getInputSection(
    const MarkLiveImpl<RecordWhyLive>::WorklistEntry *entry) const {
  if constexpr (RecordWhyLive)
    return entry->isec;
  else
    return entry;
}

template <>
typename MarkLiveImpl<true>::WorklistEntry *MarkLiveImpl<true>::makeEntry(
    InputSection *isec, const MarkLiveImpl<true>::WorklistEntry *prev) const {
template <bool RecordWhyLive>
typename MarkLiveImpl<RecordWhyLive>::WorklistEntry *
MarkLiveImpl<RecordWhyLive>::makeEntry(
    InputSection *isec,
    const MarkLiveImpl<RecordWhyLive>::WorklistEntry *prev) const {
  if constexpr (RecordWhyLive) {
    if (!isec) {
      assert(!prev);
      return nullptr;
    }
    return make<WhyLiveEntry>(isec, prev);
}

template <>
typename MarkLiveImpl<false>::WorklistEntry *MarkLiveImpl<false>::makeEntry(
    InputSection *isec, const MarkLiveImpl<false>::WorklistEntry *prev) const {
  } else {
    return isec;
  }
}

template <bool RecordWhyLive>
void MarkLiveImpl<RecordWhyLive>::markTransitively() {