aboutsummaryrefslogtreecommitdiff
path: root/deps/v8/src/compiler/frame-elider.cc
blob: 7c3f9b2741ff3b0566bfc13065d26082425fe19b (plain)
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
26
27
28
29
30
31
32
33
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright 2015 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/base/adapters.h"
#include "src/compiler/frame-elider.h"

namespace v8 {
namespace internal {
namespace compiler {

FrameElider::FrameElider(InstructionSequence* code) : code_(code) {}

void FrameElider::Run() {
  MarkBlocks();
  PropagateMarks();
  MarkDeConstruction();
}


void FrameElider::MarkBlocks() {
  for (auto block : instruction_blocks()) {
    if (block->needs_frame()) continue;
    for (auto i = block->code_start(); i < block->code_end(); ++i) {
      if (InstructionAt(i)->IsCall() ||
          InstructionAt(i)->opcode() == ArchOpcode::kArchDeoptimize) {
        block->mark_needs_frame();
        break;
      }
    }
  }
}


void FrameElider::PropagateMarks() {
  while (PropagateInOrder() && PropagateReversed()) {
  }
}


void FrameElider::MarkDeConstruction() {
  for (auto block : instruction_blocks()) {
    if (block->needs_frame()) {
      // Special case: The start block needs a frame.
      if (block->predecessors().empty()) {
        block->mark_must_construct_frame();
      }
      // Find "frame -> no frame" transitions, inserting frame
      // deconstructions.
      for (auto succ : block->successors()) {
        if (!InstructionBlockAt(succ)->needs_frame()) {
          DCHECK_EQ(1U, block->SuccessorCount());
          block->mark_must_deconstruct_frame();
        }
      }
    } else {
      // Find "no frame -> frame" transitions, inserting frame constructions.
      for (auto succ : block->successors()) {
        if (InstructionBlockAt(succ)->needs_frame()) {
          DCHECK_NE(1U, block->SuccessorCount());
          InstructionBlockAt(succ)->mark_must_construct_frame();
        }
      }
    }
  }
}


bool FrameElider::PropagateInOrder() {
  bool changed = false;
  for (auto block : instruction_blocks()) {
    changed |= PropagateIntoBlock(block);
  }
  return changed;
}


bool FrameElider::PropagateReversed() {
  bool changed = false;
  for (auto block : base::Reversed(instruction_blocks())) {
    changed |= PropagateIntoBlock(block);
  }
  return changed;
}


bool FrameElider::PropagateIntoBlock(InstructionBlock* block) {
  // Already marked, nothing to do...
  if (block->needs_frame()) return false;

  // Never mark the dummy end node, otherwise we might incorrectly decide to
  // put frame deconstruction code there later,
  if (block->successors().empty()) return false;

  // Propagate towards the end ("downwards") if there is a predecessor needing
  // a frame, but don't "bleed" from deferred code to non-deferred code.
  for (auto pred : block->predecessors()) {
    if (InstructionBlockAt(pred)->needs_frame() &&
        (!InstructionBlockAt(pred)->IsDeferred() || block->IsDeferred())) {
      block->mark_needs_frame();
      return true;
    }
  }

  // Propagate towards start ("upwards") if there are successors and all of
  // them need a frame.
  for (auto succ : block->successors()) {
    if (!InstructionBlockAt(succ)->needs_frame()) return false;
  }
  block->mark_needs_frame();
  return true;
}


const InstructionBlocks& FrameElider::instruction_blocks() const {
  return code_->instruction_blocks();
}


InstructionBlock* FrameElider::InstructionBlockAt(RpoNumber rpo_number) const {
  return code_->InstructionBlockAt(rpo_number);
}


Instruction* FrameElider::InstructionAt(int index) const {
  return code_->InstructionAt(index);
}

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