2013-06-04 16:41:24 +00:00
|
|
|
// Copyright 2013 the V8 project authors. All rights reserved.
|
2014-04-29 06:42:26 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
2013-06-04 16:41:24 +00:00
|
|
|
|
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/hydrogen-environment-liveness.h"
|
2013-06-04 16:41:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
|
2013-06-27 13:15:10 +00:00
|
|
|
HEnvironmentLivenessAnalysisPhase::HEnvironmentLivenessAnalysisPhase(
|
2013-06-04 16:41:24 +00:00
|
|
|
HGraph* graph)
|
2013-06-27 13:15:10 +00:00
|
|
|
: HPhase("H_Environment liveness analysis", graph),
|
2013-06-04 16:41:24 +00:00
|
|
|
block_count_(graph->blocks()->length()),
|
|
|
|
maximum_environment_size_(graph->maximum_environment_size()),
|
2013-06-27 13:15:10 +00:00
|
|
|
live_at_block_start_(block_count_, zone()),
|
|
|
|
first_simulate_(block_count_, zone()),
|
|
|
|
first_simulate_invalid_for_index_(block_count_, zone()),
|
|
|
|
markers_(maximum_environment_size_, zone()),
|
2013-06-04 16:41:24 +00:00
|
|
|
collect_markers_(true),
|
2013-06-27 13:15:10 +00:00
|
|
|
last_simulate_(NULL),
|
|
|
|
went_live_since_last_simulate_(maximum_environment_size_, zone()) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(maximum_environment_size_ > 0);
|
2013-06-04 16:41:24 +00:00
|
|
|
for (int i = 0; i < block_count_; ++i) {
|
2013-06-27 13:15:10 +00:00
|
|
|
live_at_block_start_.Add(
|
2013-06-04 16:41:24 +00:00
|
|
|
new(zone()) BitVector(maximum_environment_size_, zone()), zone());
|
2013-06-27 13:15:10 +00:00
|
|
|
first_simulate_.Add(NULL, zone());
|
|
|
|
first_simulate_invalid_for_index_.Add(
|
2013-06-04 16:41:24 +00:00
|
|
|
new(zone()) BitVector(maximum_environment_size_, zone()), zone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-27 13:15:10 +00:00
|
|
|
void HEnvironmentLivenessAnalysisPhase::ZapEnvironmentSlot(
|
|
|
|
int index, HSimulate* simulate) {
|
2013-06-04 16:41:24 +00:00
|
|
|
int operand_index = simulate->ToOperandIndex(index);
|
|
|
|
if (operand_index == -1) {
|
2013-06-27 13:15:10 +00:00
|
|
|
simulate->AddAssignedValue(index, graph()->GetConstantUndefined());
|
2013-06-04 16:41:24 +00:00
|
|
|
} else {
|
2013-06-27 13:15:10 +00:00
|
|
|
simulate->SetOperandAt(operand_index, graph()->GetConstantUndefined());
|
2013-06-04 16:41:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-27 13:15:10 +00:00
|
|
|
void HEnvironmentLivenessAnalysisPhase::ZapEnvironmentSlotsInSuccessors(
|
|
|
|
HBasicBlock* block, BitVector* live) {
|
2013-06-04 16:41:24 +00:00
|
|
|
// When a value is live in successor A but dead in B, we must
|
|
|
|
// explicitly zap it in B.
|
|
|
|
for (HSuccessorIterator it(block->end()); !it.Done(); it.Advance()) {
|
|
|
|
HBasicBlock* successor = it.Current();
|
|
|
|
int successor_id = successor->block_id();
|
2013-06-27 13:15:10 +00:00
|
|
|
BitVector* live_in_successor = live_at_block_start_[successor_id];
|
2013-06-04 16:41:24 +00:00
|
|
|
if (live_in_successor->Equals(*live)) continue;
|
|
|
|
for (int i = 0; i < live->length(); ++i) {
|
|
|
|
if (!live->Contains(i)) continue;
|
|
|
|
if (live_in_successor->Contains(i)) continue;
|
2013-06-27 13:15:10 +00:00
|
|
|
if (first_simulate_invalid_for_index_.at(successor_id)->Contains(i)) {
|
2013-06-04 16:41:24 +00:00
|
|
|
continue;
|
|
|
|
}
|
2013-06-27 13:15:10 +00:00
|
|
|
HSimulate* simulate = first_simulate_.at(successor_id);
|
2013-06-04 16:41:24 +00:00
|
|
|
if (simulate == NULL) continue;
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(VerifyClosures(simulate->closure(),
|
2014-03-28 08:49:58 +00:00
|
|
|
block->last_environment()->closure()));
|
2013-06-04 16:41:24 +00:00
|
|
|
ZapEnvironmentSlot(i, simulate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-27 13:15:10 +00:00
|
|
|
void HEnvironmentLivenessAnalysisPhase::ZapEnvironmentSlotsForInstruction(
|
2013-06-04 16:41:24 +00:00
|
|
|
HEnvironmentMarker* marker) {
|
|
|
|
if (!marker->CheckFlag(HValue::kEndsLiveRange)) return;
|
|
|
|
HSimulate* simulate = marker->next_simulate();
|
|
|
|
if (simulate != NULL) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(VerifyClosures(simulate->closure(), marker->closure()));
|
2013-06-04 16:41:24 +00:00
|
|
|
ZapEnvironmentSlot(marker->index(), simulate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-27 13:15:10 +00:00
|
|
|
void HEnvironmentLivenessAnalysisPhase::UpdateLivenessAtBlockEnd(
|
2013-06-04 16:41:24 +00:00
|
|
|
HBasicBlock* block,
|
|
|
|
BitVector* live) {
|
|
|
|
// Liveness at the end of each block: union of liveness in successors.
|
|
|
|
live->Clear();
|
|
|
|
for (HSuccessorIterator it(block->end()); !it.Done(); it.Advance()) {
|
2013-06-27 13:15:10 +00:00
|
|
|
live->Union(*live_at_block_start_[it.Current()->block_id()]);
|
2013-06-04 16:41:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-27 13:15:10 +00:00
|
|
|
void HEnvironmentLivenessAnalysisPhase::UpdateLivenessAtInstruction(
|
2013-06-04 16:41:24 +00:00
|
|
|
HInstruction* instr,
|
|
|
|
BitVector* live) {
|
|
|
|
switch (instr->opcode()) {
|
|
|
|
case HValue::kEnvironmentMarker: {
|
|
|
|
HEnvironmentMarker* marker = HEnvironmentMarker::cast(instr);
|
|
|
|
int index = marker->index();
|
|
|
|
if (!live->Contains(index)) {
|
|
|
|
marker->SetFlag(HValue::kEndsLiveRange);
|
|
|
|
} else {
|
|
|
|
marker->ClearFlag(HValue::kEndsLiveRange);
|
|
|
|
}
|
2013-06-27 13:15:10 +00:00
|
|
|
if (!went_live_since_last_simulate_.Contains(index)) {
|
2013-06-04 16:41:24 +00:00
|
|
|
marker->set_next_simulate(last_simulate_);
|
|
|
|
}
|
|
|
|
if (marker->kind() == HEnvironmentMarker::LOOKUP) {
|
|
|
|
live->Add(index);
|
|
|
|
} else {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(marker->kind() == HEnvironmentMarker::BIND);
|
2013-06-04 16:41:24 +00:00
|
|
|
live->Remove(index);
|
2013-06-27 13:15:10 +00:00
|
|
|
went_live_since_last_simulate_.Add(index);
|
2013-06-04 16:41:24 +00:00
|
|
|
}
|
|
|
|
if (collect_markers_) {
|
|
|
|
// Populate |markers_| list during the first pass.
|
2013-06-27 13:15:10 +00:00
|
|
|
markers_.Add(marker, zone());
|
2013-06-04 16:41:24 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case HValue::kLeaveInlined:
|
|
|
|
// No environment values are live at the end of an inlined section.
|
|
|
|
live->Clear();
|
|
|
|
last_simulate_ = NULL;
|
|
|
|
|
2014-08-04 11:34:54 +00:00
|
|
|
// The following DCHECKs guard the assumption used in case
|
2013-06-04 16:41:24 +00:00
|
|
|
// kEnterInlined below:
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(instr->next()->IsSimulate());
|
|
|
|
DCHECK(instr->next()->next()->IsGoto());
|
2013-06-04 16:41:24 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
case HValue::kEnterInlined: {
|
|
|
|
// Those environment values are live that are live at any return
|
|
|
|
// target block. Here we make use of the fact that the end of an
|
|
|
|
// inline sequence always looks like this: HLeaveInlined, HSimulate,
|
|
|
|
// HGoto (to return_target block), with no environment lookups in
|
2014-08-04 11:34:54 +00:00
|
|
|
// between (see DCHECKs above).
|
2013-06-04 16:41:24 +00:00
|
|
|
HEnterInlined* enter = HEnterInlined::cast(instr);
|
|
|
|
live->Clear();
|
|
|
|
for (int i = 0; i < enter->return_targets()->length(); ++i) {
|
|
|
|
int return_id = enter->return_targets()->at(i)->block_id();
|
2013-08-28 15:00:30 +00:00
|
|
|
live->Union(*live_at_block_start_[return_id]);
|
2013-06-04 16:41:24 +00:00
|
|
|
}
|
|
|
|
last_simulate_ = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case HValue::kSimulate:
|
|
|
|
last_simulate_ = HSimulate::cast(instr);
|
2013-06-27 13:15:10 +00:00
|
|
|
went_live_since_last_simulate_.Clear();
|
2013-06-04 16:41:24 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-27 13:15:10 +00:00
|
|
|
void HEnvironmentLivenessAnalysisPhase::Run() {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(maximum_environment_size_ > 0);
|
2013-06-04 16:41:24 +00:00
|
|
|
|
|
|
|
// Main iteration. Compute liveness of environment slots, and store it
|
|
|
|
// for each block until it doesn't change any more. For efficiency, visit
|
|
|
|
// blocks in reverse order and walk backwards through each block. We
|
|
|
|
// need several iterations to propagate liveness through nested loops.
|
2013-06-27 13:15:10 +00:00
|
|
|
BitVector live(maximum_environment_size_, zone());
|
|
|
|
BitVector worklist(block_count_, zone());
|
2013-06-04 16:41:24 +00:00
|
|
|
for (int i = 0; i < block_count_; ++i) {
|
2013-06-27 13:15:10 +00:00
|
|
|
worklist.Add(i);
|
2013-06-04 16:41:24 +00:00
|
|
|
}
|
2013-06-27 13:15:10 +00:00
|
|
|
while (!worklist.IsEmpty()) {
|
2013-06-04 16:41:24 +00:00
|
|
|
for (int block_id = block_count_ - 1; block_id >= 0; --block_id) {
|
2013-06-27 13:15:10 +00:00
|
|
|
if (!worklist.Contains(block_id)) {
|
2013-06-04 16:41:24 +00:00
|
|
|
continue;
|
|
|
|
}
|
2013-06-27 13:15:10 +00:00
|
|
|
worklist.Remove(block_id);
|
2013-06-04 16:41:24 +00:00
|
|
|
last_simulate_ = NULL;
|
|
|
|
|
2013-06-27 13:15:10 +00:00
|
|
|
HBasicBlock* block = graph()->blocks()->at(block_id);
|
|
|
|
UpdateLivenessAtBlockEnd(block, &live);
|
2013-06-04 16:41:24 +00:00
|
|
|
|
2013-11-29 10:02:32 +00:00
|
|
|
for (HInstruction* instr = block->end(); instr != NULL;
|
2013-06-04 16:41:24 +00:00
|
|
|
instr = instr->previous()) {
|
2013-06-27 13:15:10 +00:00
|
|
|
UpdateLivenessAtInstruction(instr, &live);
|
2013-06-04 16:41:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reached the start of the block, do necessary bookkeeping:
|
|
|
|
// store computed information for this block and add predecessors
|
|
|
|
// to the work list as necessary.
|
2013-06-27 13:15:10 +00:00
|
|
|
first_simulate_.Set(block_id, last_simulate_);
|
|
|
|
first_simulate_invalid_for_index_[block_id]->CopyFrom(
|
|
|
|
went_live_since_last_simulate_);
|
|
|
|
if (live_at_block_start_[block_id]->UnionIsChanged(live)) {
|
2013-06-04 16:41:24 +00:00
|
|
|
for (int i = 0; i < block->predecessors()->length(); ++i) {
|
2013-06-27 13:15:10 +00:00
|
|
|
worklist.Add(block->predecessors()->at(i)->block_id());
|
2013-06-04 16:41:24 +00:00
|
|
|
}
|
|
|
|
if (block->IsInlineReturnTarget()) {
|
2013-06-27 13:15:10 +00:00
|
|
|
worklist.Add(block->inlined_entry_block()->block_id());
|
2013-06-04 16:41:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Only collect bind/lookup instructions during the first pass.
|
|
|
|
collect_markers_ = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Analysis finished. Zap dead environment slots.
|
2013-06-27 13:15:10 +00:00
|
|
|
for (int i = 0; i < markers_.length(); ++i) {
|
|
|
|
ZapEnvironmentSlotsForInstruction(markers_[i]);
|
2013-06-04 16:41:24 +00:00
|
|
|
}
|
|
|
|
for (int block_id = block_count_ - 1; block_id >= 0; --block_id) {
|
2013-06-27 13:15:10 +00:00
|
|
|
HBasicBlock* block = graph()->blocks()->at(block_id);
|
|
|
|
UpdateLivenessAtBlockEnd(block, &live);
|
|
|
|
ZapEnvironmentSlotsInSuccessors(block, &live);
|
2013-06-04 16:41:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, remove the HEnvironment{Bind,Lookup} markers.
|
2013-06-27 13:15:10 +00:00
|
|
|
for (int i = 0; i < markers_.length(); ++i) {
|
|
|
|
markers_[i]->DeleteAndReplaceWith(NULL);
|
2013-06-04 16:41:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-28 08:49:58 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
bool HEnvironmentLivenessAnalysisPhase::VerifyClosures(
|
|
|
|
Handle<JSFunction> a, Handle<JSFunction> b) {
|
|
|
|
Heap::RelocationLock for_heap_access(isolate()->heap());
|
|
|
|
AllowHandleDereference for_verification;
|
|
|
|
return a.is_identical_to(b);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-06-04 16:41:24 +00:00
|
|
|
} } // namespace v8::internal
|