hydrogen-redundant-phi.cc 1.98 KB
Newer Older
1
// Copyright 2013 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5
#include "src/crankshaft/hydrogen-redundant-phi.h"
6 7 8 9 10

namespace v8 {
namespace internal {

void HRedundantPhiEliminationPhase::Run() {
11
  // Gather all phis from all blocks first.
12
  const ZoneList<HBasicBlock*>* blocks(graph()->blocks());
13 14 15 16 17
  ZoneList<HPhi*> all_phis(blocks->length(), zone());
  for (int i = 0; i < blocks->length(); ++i) {
    HBasicBlock* block = blocks->at(i);
    for (int j = 0; j < block->phis()->length(); j++) {
      all_phis.Add(block->phis()->at(j), zone());
18
    }
19 20 21 22
  }

  // Iteratively reduce all phis in the list.
  ProcessPhis(&all_phis);
23 24 25 26 27

#if DEBUG
  // Make sure that we *really* removed all redundant phis.
  for (int i = 0; i < blocks->length(); ++i) {
    for (int j = 0; j < blocks->at(i)->phis()->length(); j++) {
28
      DCHECK(blocks->at(i)->phis()->at(j)->GetRedundantReplacement() == NULL);
29 30 31 32 33
    }
  }
#endif
}

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

void HRedundantPhiEliminationPhase::ProcessBlock(HBasicBlock* block) {
  ProcessPhis(block->phis());
}


void HRedundantPhiEliminationPhase::ProcessPhis(const ZoneList<HPhi*>* phis) {
  bool updated;
  do {
    // Iterately replace all redundant phis in the given list.
    updated = false;
    for (int i = 0; i < phis->length(); i++) {
      HPhi* phi = phis->at(i);
      if (phi->CheckFlag(HValue::kIsDead)) continue;  // Already replaced.

      HValue* replacement = phi->GetRedundantReplacement();
      if (replacement != NULL) {
        phi->SetFlag(HValue::kIsDead);
        for (HUseIterator it(phi->uses()); !it.Done(); it.Advance()) {
          HValue* value = it.value();
          value->SetOperandAt(it.index(), replacement);
          // Iterate again if used in another non-dead phi.
          updated |= value->IsPhi() && !value->CheckFlag(HValue::kIsDead);
        }
        phi->block()->RemovePhi(phi);
      }
    }
  } while (updated);
}


65 66
}  // namespace internal
}  // namespace v8