wasm-escape-analysis.cc 2.25 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/compiler/wasm-escape-analysis.h"

#include "src/compiler/js-graph.h"
#include "src/compiler/node-properties.h"

namespace v8 {
namespace internal {
namespace compiler {

Reduction WasmEscapeAnalysis::Reduce(Node* node) {
  switch (node->opcode()) {
    case IrOpcode::kAllocateRaw:
      return ReduceAllocateRaw(node);
    default:
      return NoChange();
  }
}

Reduction WasmEscapeAnalysis::ReduceAllocateRaw(Node* node) {
  DCHECK_EQ(node->opcode(), IrOpcode::kAllocateRaw);
  // TODO(manoskouk): Account for phis.
26 27 28

  // Collect all value edges of {node} in this vector.
  std::vector<Edge> value_edges;
29 30 31
  for (Edge edge : node->use_edges()) {
    if (NodeProperties::IsValueEdge(edge)) {
      if (edge.index() != 0 ||
32 33
          (edge.from()->opcode() != IrOpcode::kStoreToObject &&
           edge.from()->opcode() != IrOpcode::kInitializeImmutableInObject)) {
34 35
        return NoChange();
      }
36
      value_edges.push_back(edge);
37 38 39
    }
  }

40 41 42 43 44 45
  // Remove all discovered stores from the effect chain.
  for (Edge edge : value_edges) {
    DCHECK(NodeProperties::IsValueEdge(edge));
    DCHECK_EQ(edge.index(), 0);
    Node* use = edge.from();
    DCHECK(!use->IsDead());
46 47
    DCHECK(use->opcode() == IrOpcode::kStoreToObject ||
           use->opcode() == IrOpcode::kInitializeImmutableInObject);
48 49 50 51 52 53
    // The value stored by this StoreToObject node might be another allocation
    // which has no more uses. Therefore we have to revisit it. Note that this
    // will not happen automatically: ReplaceWithValue does not trigger revisits
    // of former inputs of the replaced node.
    Node* stored_value = NodeProperties::GetValueInput(use, 2);
    Revisit(stored_value);
54 55 56
    ReplaceWithValue(use, mcgraph_->Dead(), NodeProperties::GetEffectInput(use),
                     mcgraph_->Dead());
    use->Kill();
57 58 59 60 61 62
  }

  // Remove the allocation from the effect and control chains.
  ReplaceWithValue(node, mcgraph_->Dead(), NodeProperties::GetEffectInput(node),
                   NodeProperties::GetControlInput(node));

63
  return Changed(node);
64 65 66 67 68
}

}  // namespace compiler
}  // namespace internal
}  // namespace v8