2013-09-26 16:25:57 +00:00
|
|
|
// Copyright 2013 the V8 project authors. All rights reserved.
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
#include "hydrogen-check-elimination.h"
|
|
|
|
#include "hydrogen-alias-analysis.h"
|
2013-11-28 15:50:54 +00:00
|
|
|
#include "hydrogen-flow-engine.h"
|
|
|
|
|
|
|
|
#define GLOBAL 1
|
|
|
|
|
|
|
|
// Only collect stats in debug mode.
|
|
|
|
#if DEBUG
|
|
|
|
#define INC_STAT(x) phase_->x++
|
|
|
|
#else
|
|
|
|
#define INC_STAT(x)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// For code de-uglification.
|
|
|
|
#define TRACE(x) if (FLAG_trace_check_elimination) PrintF x
|
2013-09-26 16:25:57 +00:00
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
typedef UniqueSet<Map>* MapSet;
|
|
|
|
|
2013-11-28 15:50:54 +00:00
|
|
|
struct HCheckTableEntry {
|
|
|
|
HValue* object_; // The object being approximated. NULL => invalid entry.
|
2014-02-10 15:32:54 +00:00
|
|
|
HInstruction* check_; // The last check instruction.
|
|
|
|
MapSet maps_; // The set of known maps for the object.
|
2013-11-28 15:50:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-02-10 15:32:54 +00:00
|
|
|
// The main data structure used during check elimination, which stores a
|
2013-09-26 16:25:57 +00:00
|
|
|
// set of known maps for each object.
|
2013-11-28 15:50:54 +00:00
|
|
|
class HCheckTable : public ZoneObject {
|
2013-09-26 16:25:57 +00:00
|
|
|
public:
|
2013-11-28 15:50:54 +00:00
|
|
|
static const int kMaxTrackedObjects = 10;
|
|
|
|
|
|
|
|
explicit HCheckTable(HCheckEliminationPhase* phase)
|
|
|
|
: phase_(phase),
|
|
|
|
cursor_(0),
|
|
|
|
size_(0) {
|
|
|
|
}
|
|
|
|
|
|
|
|
// The main processing of instructions.
|
|
|
|
HCheckTable* Process(HInstruction* instr, Zone* zone) {
|
|
|
|
switch (instr->opcode()) {
|
|
|
|
case HValue::kCheckMaps: {
|
|
|
|
ReduceCheckMaps(HCheckMaps::cast(instr));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case HValue::kLoadNamedField: {
|
|
|
|
ReduceLoadNamedField(HLoadNamedField::cast(instr));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case HValue::kStoreNamedField: {
|
|
|
|
ReduceStoreNamedField(HStoreNamedField::cast(instr));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case HValue::kCompareMap: {
|
|
|
|
ReduceCompareMap(HCompareMap::cast(instr));
|
|
|
|
break;
|
|
|
|
}
|
2014-03-17 09:11:38 +00:00
|
|
|
case HValue::kCompareObjectEqAndBranch: {
|
|
|
|
ReduceCompareObjectEqAndBranch(HCompareObjectEqAndBranch::cast(instr));
|
|
|
|
break;
|
|
|
|
}
|
2013-11-28 15:50:54 +00:00
|
|
|
case HValue::kTransitionElementsKind: {
|
|
|
|
ReduceTransitionElementsKind(
|
|
|
|
HTransitionElementsKind::cast(instr));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case HValue::kCheckMapValue: {
|
|
|
|
ReduceCheckMapValue(HCheckMapValue::cast(instr));
|
|
|
|
break;
|
|
|
|
}
|
2013-11-29 12:37:35 +00:00
|
|
|
case HValue::kCheckHeapObject: {
|
|
|
|
ReduceCheckHeapObject(HCheckHeapObject::cast(instr));
|
|
|
|
break;
|
|
|
|
}
|
2013-11-28 15:50:54 +00:00
|
|
|
default: {
|
|
|
|
// If the instruction changes maps uncontrollably, drop everything.
|
2014-02-25 16:11:58 +00:00
|
|
|
if (instr->CheckChangesFlag(kMaps) ||
|
|
|
|
instr->CheckChangesFlag(kOsrEntries)) {
|
|
|
|
Kill();
|
2013-11-28 15:50:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Improvements possible:
|
2013-11-29 12:37:35 +00:00
|
|
|
// - eliminate redundant HCheckSmi, HCheckInstanceType instructions
|
|
|
|
// - track which values have been HCheckHeapObject'd
|
2013-11-28 15:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2014-02-11 19:18:06 +00:00
|
|
|
// Support for global analysis with HFlowEngine: Merge given state with
|
|
|
|
// the other incoming state.
|
|
|
|
static HCheckTable* Merge(HCheckTable* succ_state, HBasicBlock* succ_block,
|
|
|
|
HCheckTable* pred_state, HBasicBlock* pred_block,
|
|
|
|
Zone* zone) {
|
|
|
|
if (pred_state == NULL || pred_block->IsUnreachable()) {
|
|
|
|
return succ_state;
|
|
|
|
}
|
|
|
|
if (succ_state == NULL) {
|
|
|
|
return pred_state->Copy(succ_block, pred_block, zone);
|
|
|
|
} else {
|
|
|
|
return succ_state->Merge(succ_block, pred_state, pred_block, zone);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Support for global analysis with HFlowEngine: Given state merged with all
|
|
|
|
// the other incoming states, prepare it for use.
|
|
|
|
static HCheckTable* Finish(HCheckTable* state, HBasicBlock* block,
|
|
|
|
Zone* zone) {
|
|
|
|
if (state == NULL) {
|
|
|
|
block->MarkUnreachable();
|
2014-02-28 14:16:38 +00:00
|
|
|
} else if (block->IsUnreachable()) {
|
|
|
|
state = NULL;
|
|
|
|
}
|
|
|
|
if (FLAG_trace_check_elimination) {
|
|
|
|
PrintF("Processing B%d, checkmaps-table:\n", block->block_id());
|
|
|
|
Print(state);
|
2014-02-11 19:18:06 +00:00
|
|
|
}
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Copy state to successor block.
|
2014-01-28 16:57:39 +00:00
|
|
|
HCheckTable* Copy(HBasicBlock* succ, HBasicBlock* from_block, Zone* zone) {
|
2013-11-28 15:50:54 +00:00
|
|
|
HCheckTable* copy = new(phase_->zone()) HCheckTable(phase_);
|
|
|
|
for (int i = 0; i < size_; i++) {
|
|
|
|
HCheckTableEntry* old_entry = &entries_[i];
|
2014-02-28 14:16:38 +00:00
|
|
|
ASSERT(old_entry->maps_->size() > 0);
|
2013-11-28 15:50:54 +00:00
|
|
|
HCheckTableEntry* new_entry = ©->entries_[i];
|
|
|
|
new_entry->object_ = old_entry->object_;
|
|
|
|
new_entry->maps_ = old_entry->maps_->Copy(phase_->zone());
|
2014-02-11 19:18:06 +00:00
|
|
|
// Keep the check if the existing check's block dominates the successor.
|
|
|
|
if (old_entry->check_ != NULL &&
|
|
|
|
old_entry->check_->block()->Dominates(succ)) {
|
|
|
|
new_entry->check_ = old_entry->check_;
|
|
|
|
} else {
|
|
|
|
// Leave it NULL till we meet a new check instruction for this object
|
|
|
|
// in the control flow.
|
|
|
|
new_entry->check_ = NULL;
|
|
|
|
}
|
2013-11-28 15:50:54 +00:00
|
|
|
}
|
2013-12-19 17:42:21 +00:00
|
|
|
copy->cursor_ = cursor_;
|
|
|
|
copy->size_ = size_;
|
|
|
|
|
2014-02-10 15:32:54 +00:00
|
|
|
// Create entries for succ block's phis.
|
2014-02-12 18:30:41 +00:00
|
|
|
if (!succ->IsLoopHeader() && succ->phis()->length() > 0) {
|
2014-02-10 15:32:54 +00:00
|
|
|
int pred_index = succ->PredecessorIndexOf(from_block);
|
|
|
|
for (int phi_index = 0;
|
|
|
|
phi_index < succ->phis()->length();
|
|
|
|
++phi_index) {
|
|
|
|
HPhi* phi = succ->phis()->at(phi_index);
|
|
|
|
HValue* phi_operand = phi->OperandAt(pred_index);
|
|
|
|
|
|
|
|
HCheckTableEntry* pred_entry = copy->Find(phi_operand);
|
|
|
|
if (pred_entry != NULL) {
|
|
|
|
// Create an entry for a phi in the table.
|
2014-02-25 16:11:58 +00:00
|
|
|
copy->Insert(phi, NULL, pred_entry->maps_->Copy(phase_->zone()));
|
2014-02-10 15:32:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-19 17:42:21 +00:00
|
|
|
// Branch-sensitive analysis for certain comparisons may add more facts
|
|
|
|
// to the state for the successor on the true branch.
|
2014-01-28 17:49:13 +00:00
|
|
|
bool learned = false;
|
2014-02-11 19:18:06 +00:00
|
|
|
if (succ->predecessors()->length() == 1) {
|
|
|
|
HControlInstruction* end = succ->predecessors()->at(0)->end();
|
|
|
|
bool is_true_branch = end->SuccessorAt(0) == succ;
|
2013-12-19 17:42:21 +00:00
|
|
|
if (end->IsCompareMap()) {
|
2013-12-02 18:34:33 +00:00
|
|
|
HCompareMap* cmp = HCompareMap::cast(end);
|
|
|
|
HValue* object = cmp->value()->ActualValue();
|
|
|
|
HCheckTableEntry* entry = copy->Find(object);
|
2014-02-11 19:18:06 +00:00
|
|
|
if (is_true_branch) {
|
|
|
|
// Learn on the true branch of if(CompareMap(x)).
|
|
|
|
if (entry == NULL) {
|
2014-02-25 16:11:58 +00:00
|
|
|
copy->Insert(object, cmp, cmp->map());
|
2014-02-11 19:18:06 +00:00
|
|
|
} else {
|
|
|
|
MapSet list = new(phase_->zone()) UniqueSet<Map>();
|
|
|
|
list->Add(cmp->map(), phase_->zone());
|
|
|
|
entry->maps_ = list;
|
|
|
|
entry->check_ = cmp;
|
|
|
|
}
|
2013-12-02 18:34:33 +00:00
|
|
|
} else {
|
2014-02-11 19:18:06 +00:00
|
|
|
// Learn on the false branch of if(CompareMap(x)).
|
|
|
|
if (entry != NULL) {
|
|
|
|
entry->maps_->Remove(cmp->map());
|
|
|
|
}
|
2013-12-02 18:34:33 +00:00
|
|
|
}
|
2014-01-28 17:49:13 +00:00
|
|
|
learned = true;
|
2014-02-11 19:18:06 +00:00
|
|
|
} else if (is_true_branch && end->IsCompareObjectEqAndBranch()) {
|
2013-12-19 17:42:21 +00:00
|
|
|
// Learn on the true branch of if(CmpObjectEq(x, y)).
|
|
|
|
HCompareObjectEqAndBranch* cmp =
|
|
|
|
HCompareObjectEqAndBranch::cast(end);
|
|
|
|
HValue* left = cmp->left()->ActualValue();
|
|
|
|
HValue* right = cmp->right()->ActualValue();
|
|
|
|
HCheckTableEntry* le = copy->Find(left);
|
|
|
|
HCheckTableEntry* re = copy->Find(right);
|
|
|
|
if (le == NULL) {
|
|
|
|
if (re != NULL) {
|
2014-02-25 16:11:58 +00:00
|
|
|
copy->Insert(left, NULL, re->maps_->Copy(zone));
|
2013-12-19 17:42:21 +00:00
|
|
|
}
|
|
|
|
} else if (re == NULL) {
|
2014-02-25 16:11:58 +00:00
|
|
|
copy->Insert(right, NULL, le->maps_->Copy(zone));
|
2013-12-19 17:42:21 +00:00
|
|
|
} else {
|
|
|
|
MapSet intersect = le->maps_->Intersect(re->maps_, zone);
|
|
|
|
le->maps_ = intersect;
|
|
|
|
re->maps_ = intersect->Copy(zone);
|
|
|
|
}
|
2014-01-28 17:49:13 +00:00
|
|
|
learned = true;
|
2013-12-02 18:34:33 +00:00
|
|
|
}
|
2013-12-19 17:42:21 +00:00
|
|
|
// Learning on false branches requires storing negative facts.
|
2013-12-02 18:34:33 +00:00
|
|
|
}
|
2013-12-19 17:42:21 +00:00
|
|
|
|
2014-01-28 17:49:13 +00:00
|
|
|
if (FLAG_trace_check_elimination) {
|
|
|
|
PrintF("B%d checkmaps-table %s from B%d:\n",
|
|
|
|
succ->block_id(),
|
|
|
|
learned ? "learned" : "copied",
|
|
|
|
from_block->block_id());
|
2014-02-28 14:16:38 +00:00
|
|
|
Print(copy);
|
2014-01-28 17:49:13 +00:00
|
|
|
}
|
|
|
|
|
2013-11-28 15:50:54 +00:00
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
2014-02-11 19:18:06 +00:00
|
|
|
// Merge this state with the other incoming state.
|
2014-01-28 16:57:39 +00:00
|
|
|
HCheckTable* Merge(HBasicBlock* succ, HCheckTable* that,
|
2014-02-10 15:32:54 +00:00
|
|
|
HBasicBlock* pred_block, Zone* zone) {
|
2014-02-11 19:18:06 +00:00
|
|
|
if (that->size_ == 0) {
|
|
|
|
// If the other state is empty, simply reset.
|
2014-02-25 16:11:58 +00:00
|
|
|
size_ = 0;
|
|
|
|
cursor_ = 0;
|
2014-02-11 19:18:06 +00:00
|
|
|
} else {
|
|
|
|
int pred_index = succ->PredecessorIndexOf(pred_block);
|
|
|
|
bool compact = false;
|
|
|
|
for (int i = 0; i < size_; i++) {
|
|
|
|
HCheckTableEntry* this_entry = &entries_[i];
|
|
|
|
HCheckTableEntry* that_entry;
|
|
|
|
if (this_entry->object_->IsPhi() &&
|
|
|
|
this_entry->object_->block() == succ) {
|
|
|
|
HPhi* phi = HPhi::cast(this_entry->object_);
|
|
|
|
HValue* phi_operand = phi->OperandAt(pred_index);
|
|
|
|
that_entry = that->Find(phi_operand);
|
2014-02-10 15:32:54 +00:00
|
|
|
|
2014-02-11 19:18:06 +00:00
|
|
|
} else {
|
|
|
|
that_entry = that->Find(this_entry->object_);
|
|
|
|
}
|
2014-02-10 15:32:54 +00:00
|
|
|
|
2014-02-11 19:18:06 +00:00
|
|
|
if (that_entry == NULL) {
|
|
|
|
this_entry->object_ = NULL;
|
|
|
|
compact = true;
|
|
|
|
} else {
|
|
|
|
this_entry->maps_ =
|
|
|
|
this_entry->maps_->Union(that_entry->maps_, phase_->zone());
|
|
|
|
if (this_entry->check_ != that_entry->check_) {
|
|
|
|
this_entry->check_ = NULL;
|
2014-01-28 17:49:13 +00:00
|
|
|
}
|
2014-02-11 19:18:06 +00:00
|
|
|
ASSERT(this_entry->maps_->size() > 0);
|
2014-01-28 17:49:13 +00:00
|
|
|
}
|
2013-11-28 15:50:54 +00:00
|
|
|
}
|
2014-02-11 19:18:06 +00:00
|
|
|
if (compact) Compact();
|
2013-11-28 15:50:54 +00:00
|
|
|
}
|
2014-02-11 19:18:06 +00:00
|
|
|
|
2014-01-28 17:49:13 +00:00
|
|
|
if (FLAG_trace_check_elimination) {
|
|
|
|
PrintF("B%d checkmaps-table merged with B%d table:\n",
|
2014-02-10 15:32:54 +00:00
|
|
|
succ->block_id(), pred_block->block_id());
|
2014-02-28 14:16:38 +00:00
|
|
|
Print(this);
|
2014-01-28 17:49:13 +00:00
|
|
|
}
|
2013-11-28 15:50:54 +00:00
|
|
|
return this;
|
2013-09-26 16:25:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ReduceCheckMaps(HCheckMaps* instr) {
|
|
|
|
HValue* object = instr->value()->ActualValue();
|
2013-11-28 15:50:54 +00:00
|
|
|
HCheckTableEntry* entry = Find(object);
|
|
|
|
if (entry != NULL) {
|
2013-09-26 16:25:57 +00:00
|
|
|
// entry found;
|
2013-11-28 15:50:54 +00:00
|
|
|
MapSet a = entry->maps_;
|
2014-04-29 04:59:37 +00:00
|
|
|
const UniqueSet<Map>* i = instr->map_set();
|
2013-09-26 16:25:57 +00:00
|
|
|
if (a->IsSubset(i)) {
|
|
|
|
// The first check is more strict; the second is redundant.
|
2013-11-28 15:50:54 +00:00
|
|
|
if (entry->check_ != NULL) {
|
2014-01-28 17:49:13 +00:00
|
|
|
TRACE(("Replacing redundant CheckMaps #%d at B%d with #%d\n",
|
|
|
|
instr->id(), instr->block()->block_id(), entry->check_->id()));
|
2013-11-28 15:50:54 +00:00
|
|
|
instr->DeleteAndReplaceWith(entry->check_);
|
|
|
|
INC_STAT(redundant_);
|
2013-09-26 16:25:57 +00:00
|
|
|
} else {
|
2014-01-28 17:49:13 +00:00
|
|
|
TRACE(("Marking redundant CheckMaps #%d at B%d as dead\n",
|
|
|
|
instr->id(), instr->block()->block_id()));
|
|
|
|
// Mark check as dead but leave it in the graph as a checkpoint for
|
|
|
|
// subsequent checks.
|
|
|
|
instr->SetFlag(HValue::kIsDead);
|
|
|
|
entry->check_ = instr;
|
2013-11-28 15:50:54 +00:00
|
|
|
INC_STAT(removed_);
|
2013-09-26 16:25:57 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2014-02-10 15:32:54 +00:00
|
|
|
MapSet intersection = i->Intersect(a, phase_->zone());
|
|
|
|
if (intersection->size() == 0) {
|
2013-09-26 16:25:57 +00:00
|
|
|
// Intersection is empty; probably megamorphic, which is likely to
|
|
|
|
// deopt anyway, so just leave things as they are.
|
2013-11-28 15:50:54 +00:00
|
|
|
INC_STAT(empty_);
|
2013-09-26 16:25:57 +00:00
|
|
|
} else {
|
2014-02-10 15:32:54 +00:00
|
|
|
// Update set of maps in the entry.
|
|
|
|
entry->maps_ = intersection;
|
|
|
|
if (intersection->size() != i->size()) {
|
|
|
|
// Narrow set of maps in the second check maps instruction.
|
|
|
|
HGraph* graph = instr->block()->graph();
|
|
|
|
if (entry->check_ != NULL &&
|
|
|
|
entry->check_->block() == instr->block() &&
|
|
|
|
entry->check_->IsCheckMaps()) {
|
|
|
|
// There is a check in the same block so replace it with a more
|
|
|
|
// strict check and eliminate the second check entirely.
|
|
|
|
HCheckMaps* check = HCheckMaps::cast(entry->check_);
|
|
|
|
TRACE(("CheckMaps #%d at B%d narrowed\n", check->id(),
|
|
|
|
check->block()->block_id()));
|
2014-02-19 12:34:50 +00:00
|
|
|
// Update map set and ensure that the check is alive.
|
2014-02-10 15:32:54 +00:00
|
|
|
check->set_map_set(intersection, graph->zone());
|
2014-02-19 12:34:50 +00:00
|
|
|
check->ClearFlag(HValue::kIsDead);
|
2014-02-10 15:32:54 +00:00
|
|
|
TRACE(("Replacing redundant CheckMaps #%d at B%d with #%d\n",
|
|
|
|
instr->id(), instr->block()->block_id(), entry->check_->id()));
|
|
|
|
instr->DeleteAndReplaceWith(entry->check_);
|
|
|
|
} else {
|
|
|
|
TRACE(("CheckMaps #%d at B%d narrowed\n", instr->id(),
|
|
|
|
instr->block()->block_id()));
|
|
|
|
instr->set_map_set(intersection, graph->zone());
|
|
|
|
entry->check_ = instr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FLAG_trace_check_elimination) {
|
2014-02-28 14:16:38 +00:00
|
|
|
Print(this);
|
2014-02-10 15:32:54 +00:00
|
|
|
}
|
|
|
|
INC_STAT(narrowed_);
|
|
|
|
}
|
2013-09-26 16:25:57 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// No entry; insert a new one.
|
2014-04-22 07:24:05 +00:00
|
|
|
Insert(object, instr, instr->map_set()->Copy(phase_->zone()));
|
2013-09-26 16:25:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReduceLoadNamedField(HLoadNamedField* instr) {
|
|
|
|
// Reduce a load of the map field when it is known to be a constant.
|
2014-04-15 07:36:47 +00:00
|
|
|
if (!IsMapAccess(instr->access())) {
|
2014-04-16 11:41:09 +00:00
|
|
|
// Check if we introduce field maps here.
|
2014-04-22 07:24:05 +00:00
|
|
|
if (instr->map_set()->size() != 0) {
|
|
|
|
Insert(instr, instr, instr->map_set()->Copy(phase_->zone()));
|
2014-04-15 07:36:47 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2013-09-26 16:25:57 +00:00
|
|
|
|
|
|
|
HValue* object = instr->object()->ActualValue();
|
|
|
|
MapSet maps = FindMaps(object);
|
|
|
|
if (maps == NULL || maps->size() != 1) return; // Not a constant.
|
|
|
|
|
|
|
|
Unique<Map> map = maps->at(0);
|
|
|
|
HConstant* constant = HConstant::CreateAndInsertBefore(
|
|
|
|
instr->block()->graph()->zone(), map, true, instr);
|
|
|
|
instr->DeleteAndReplaceWith(constant);
|
2013-11-28 15:50:54 +00:00
|
|
|
INC_STAT(loads_);
|
2013-09-26 16:25:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ReduceCheckMapValue(HCheckMapValue* instr) {
|
|
|
|
if (!instr->map()->IsConstant()) return; // Nothing to learn.
|
|
|
|
|
|
|
|
HValue* object = instr->value()->ActualValue();
|
|
|
|
// Match a HCheckMapValue(object, HConstant(map))
|
|
|
|
Unique<Map> map = MapConstant(instr->map());
|
2014-02-11 19:18:06 +00:00
|
|
|
|
|
|
|
HCheckTableEntry* entry = Find(object);
|
|
|
|
if (entry != NULL) {
|
|
|
|
MapSet maps = entry->maps_;
|
2013-09-26 16:25:57 +00:00
|
|
|
if (maps->Contains(map)) {
|
|
|
|
if (maps->size() == 1) {
|
|
|
|
// Object is known to have exactly this map.
|
2014-02-11 19:18:06 +00:00
|
|
|
if (entry->check_ != NULL) {
|
|
|
|
instr->DeleteAndReplaceWith(entry->check_);
|
|
|
|
} else {
|
|
|
|
// Mark check as dead but leave it in the graph as a checkpoint for
|
|
|
|
// subsequent checks.
|
|
|
|
instr->SetFlag(HValue::kIsDead);
|
|
|
|
entry->check_ = instr;
|
|
|
|
}
|
2013-11-28 15:50:54 +00:00
|
|
|
INC_STAT(removed_);
|
2013-09-26 16:25:57 +00:00
|
|
|
} else {
|
|
|
|
// Only one map survives the check.
|
|
|
|
maps->Clear();
|
2013-11-28 15:50:54 +00:00
|
|
|
maps->Add(map, phase_->zone());
|
2014-02-11 19:18:06 +00:00
|
|
|
entry->check_ = instr;
|
2013-09-26 16:25:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// No prior information.
|
2014-02-25 16:11:58 +00:00
|
|
|
Insert(object, instr, map);
|
2013-09-26 16:25:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-29 12:37:35 +00:00
|
|
|
void ReduceCheckHeapObject(HCheckHeapObject* instr) {
|
|
|
|
if (FindMaps(instr->value()->ActualValue()) != NULL) {
|
|
|
|
// If the object has known maps, it's definitely a heap object.
|
|
|
|
instr->DeleteAndReplaceWith(instr->value());
|
|
|
|
INC_STAT(removed_cho_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-26 16:25:57 +00:00
|
|
|
void ReduceStoreNamedField(HStoreNamedField* instr) {
|
|
|
|
HValue* object = instr->object()->ActualValue();
|
|
|
|
if (instr->has_transition()) {
|
|
|
|
// This store transitions the object to a new map.
|
|
|
|
Kill(object);
|
2014-02-25 16:11:58 +00:00
|
|
|
Insert(object, NULL, MapConstant(instr->transition()));
|
2013-09-26 16:25:57 +00:00
|
|
|
} else if (IsMapAccess(instr->access())) {
|
|
|
|
// This is a store directly to the map field of the object.
|
|
|
|
Kill(object);
|
|
|
|
if (!instr->value()->IsConstant()) return;
|
2014-02-25 16:11:58 +00:00
|
|
|
Insert(object, NULL, MapConstant(instr->value()));
|
2013-11-28 15:50:54 +00:00
|
|
|
} else {
|
|
|
|
// If the instruction changes maps, it should be handled above.
|
2014-02-11 06:53:14 +00:00
|
|
|
CHECK(!instr->CheckChangesFlag(kMaps));
|
2013-09-26 16:25:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReduceCompareMap(HCompareMap* instr) {
|
|
|
|
MapSet maps = FindMaps(instr->value()->ActualValue());
|
|
|
|
if (maps == NULL) return;
|
2014-02-11 19:18:06 +00:00
|
|
|
|
|
|
|
int succ;
|
2013-09-26 16:25:57 +00:00
|
|
|
if (maps->Contains(instr->map())) {
|
2014-02-11 19:18:06 +00:00
|
|
|
if (maps->size() != 1) {
|
|
|
|
TRACE(("CompareMap #%d for #%d at B%d can't be eliminated: "
|
|
|
|
"ambiguous set of maps\n", instr->id(), instr->value()->id(),
|
|
|
|
instr->block()->block_id()));
|
|
|
|
return;
|
2013-11-28 15:50:54 +00:00
|
|
|
}
|
2014-02-11 19:18:06 +00:00
|
|
|
succ = 0;
|
|
|
|
INC_STAT(compares_true_);
|
2013-09-26 16:25:57 +00:00
|
|
|
} else {
|
2014-02-11 19:18:06 +00:00
|
|
|
succ = 1;
|
2013-11-28 15:50:54 +00:00
|
|
|
INC_STAT(compares_false_);
|
2013-09-26 16:25:57 +00:00
|
|
|
}
|
2014-02-11 19:18:06 +00:00
|
|
|
|
|
|
|
TRACE(("Marking redundant CompareMap #%d for #%d at B%d as %s\n",
|
|
|
|
instr->id(), instr->value()->id(), instr->block()->block_id(),
|
|
|
|
succ == 0 ? "true" : "false"));
|
|
|
|
instr->set_known_successor_index(succ);
|
|
|
|
|
|
|
|
int unreachable_succ = 1 - succ;
|
|
|
|
instr->block()->MarkSuccEdgeUnreachable(unreachable_succ);
|
2013-09-26 16:25:57 +00:00
|
|
|
}
|
|
|
|
|
2014-03-17 09:11:38 +00:00
|
|
|
void ReduceCompareObjectEqAndBranch(HCompareObjectEqAndBranch* instr) {
|
|
|
|
MapSet maps_left = FindMaps(instr->left()->ActualValue());
|
|
|
|
if (maps_left == NULL) return;
|
|
|
|
MapSet maps_right = FindMaps(instr->right()->ActualValue());
|
|
|
|
if (maps_right == NULL) return;
|
|
|
|
MapSet intersection = maps_left->Intersect(maps_right, phase_->zone());
|
|
|
|
if (intersection->size() > 0) return;
|
|
|
|
|
|
|
|
TRACE(("Marking redundant CompareObjectEqAndBranch #%d at B%d as false\n",
|
|
|
|
instr->id(), instr->block()->block_id()));
|
|
|
|
int succ = 1;
|
|
|
|
instr->set_known_successor_index(succ);
|
|
|
|
|
|
|
|
int unreachable_succ = 1 - succ;
|
|
|
|
instr->block()->MarkSuccEdgeUnreachable(unreachable_succ);
|
|
|
|
}
|
|
|
|
|
2013-09-26 16:25:57 +00:00
|
|
|
void ReduceTransitionElementsKind(HTransitionElementsKind* instr) {
|
|
|
|
MapSet maps = FindMaps(instr->object()->ActualValue());
|
|
|
|
// Can only learn more about an object that already has a known set of maps.
|
|
|
|
if (maps == NULL) return;
|
|
|
|
if (maps->Contains(instr->original_map())) {
|
|
|
|
// If the object has the original map, it will be transitioned.
|
|
|
|
maps->Remove(instr->original_map());
|
2013-11-28 15:50:54 +00:00
|
|
|
maps->Add(instr->transitioned_map(), phase_->zone());
|
2013-09-26 16:25:57 +00:00
|
|
|
} else {
|
|
|
|
// Object does not have the given map, thus the transition is redundant.
|
|
|
|
instr->DeleteAndReplaceWith(instr->object());
|
2013-11-28 15:50:54 +00:00
|
|
|
INC_STAT(transitions_);
|
2013-09-26 16:25:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-25 16:11:58 +00:00
|
|
|
// Kill everything in the table.
|
|
|
|
void Kill() {
|
2013-11-28 15:50:54 +00:00
|
|
|
size_ = 0;
|
|
|
|
cursor_ = 0;
|
2013-09-26 16:25:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Kill everything in the table that may alias {object}.
|
|
|
|
void Kill(HValue* object) {
|
2013-11-28 15:50:54 +00:00
|
|
|
bool compact = false;
|
|
|
|
for (int i = 0; i < size_; i++) {
|
|
|
|
HCheckTableEntry* entry = &entries_[i];
|
|
|
|
ASSERT(entry->object_ != NULL);
|
|
|
|
if (phase_->aliasing_->MayAlias(entry->object_, object)) {
|
|
|
|
entry->object_ = NULL;
|
|
|
|
compact = true;
|
|
|
|
}
|
2013-09-26 16:25:57 +00:00
|
|
|
}
|
2013-11-28 15:50:54 +00:00
|
|
|
if (compact) Compact();
|
|
|
|
ASSERT(Find(object) == NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Compact() {
|
|
|
|
// First, compact the array in place.
|
|
|
|
int max = size_, dest = 0, old_cursor = cursor_;
|
|
|
|
for (int i = 0; i < max; i++) {
|
|
|
|
if (entries_[i].object_ != NULL) {
|
|
|
|
if (dest != i) entries_[dest] = entries_[i];
|
|
|
|
dest++;
|
|
|
|
} else {
|
|
|
|
if (i < old_cursor) cursor_--;
|
|
|
|
size_--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ASSERT(size_ == dest);
|
|
|
|
ASSERT(cursor_ <= size_);
|
|
|
|
|
|
|
|
// Preserve the age of the entries by moving the older entries to the end.
|
|
|
|
if (cursor_ == size_) return; // Cursor already points at end.
|
|
|
|
if (cursor_ != 0) {
|
|
|
|
// | L = oldest | R = newest | |
|
|
|
|
// ^ cursor ^ size ^ MAX
|
|
|
|
HCheckTableEntry tmp_entries[kMaxTrackedObjects];
|
|
|
|
int L = cursor_;
|
|
|
|
int R = size_ - cursor_;
|
|
|
|
|
|
|
|
OS::MemMove(&tmp_entries[0], &entries_[0], L * sizeof(HCheckTableEntry));
|
|
|
|
OS::MemMove(&entries_[0], &entries_[L], R * sizeof(HCheckTableEntry));
|
|
|
|
OS::MemMove(&entries_[R], &tmp_entries[0], L * sizeof(HCheckTableEntry));
|
|
|
|
}
|
|
|
|
|
|
|
|
cursor_ = size_; // Move cursor to end.
|
2013-09-26 16:25:57 +00:00
|
|
|
}
|
|
|
|
|
2014-02-28 14:16:38 +00:00
|
|
|
static void Print(HCheckTable* table) {
|
|
|
|
if (table == NULL) {
|
|
|
|
PrintF(" unreachable\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < table->size_; i++) {
|
|
|
|
HCheckTableEntry* entry = &table->entries_[i];
|
2013-11-28 15:50:54 +00:00
|
|
|
ASSERT(entry->object_ != NULL);
|
2014-02-10 15:32:54 +00:00
|
|
|
PrintF(" checkmaps-table @%d: %s #%d ", i,
|
2014-02-11 19:18:06 +00:00
|
|
|
entry->object_->IsPhi() ? "phi" : "object", entry->object_->id());
|
2013-11-28 15:50:54 +00:00
|
|
|
if (entry->check_ != NULL) {
|
|
|
|
PrintF("check #%d ", entry->check_->id());
|
2013-09-26 16:25:57 +00:00
|
|
|
}
|
2013-11-28 15:50:54 +00:00
|
|
|
MapSet list = entry->maps_;
|
2013-09-26 16:25:57 +00:00
|
|
|
PrintF("%d maps { ", list->size());
|
|
|
|
for (int j = 0; j < list->size(); j++) {
|
|
|
|
if (j > 0) PrintF(", ");
|
|
|
|
PrintF("%" V8PRIxPTR, list->at(j).Hashcode());
|
|
|
|
}
|
|
|
|
PrintF(" }\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-28 15:50:54 +00:00
|
|
|
HCheckTableEntry* Find(HValue* object) {
|
|
|
|
for (int i = size_ - 1; i >= 0; i--) {
|
|
|
|
// Search from most-recently-inserted to least-recently-inserted.
|
|
|
|
HCheckTableEntry* entry = &entries_[i];
|
|
|
|
ASSERT(entry->object_ != NULL);
|
|
|
|
if (phase_->aliasing_->MustAlias(entry->object_, object)) return entry;
|
2013-09-26 16:25:57 +00:00
|
|
|
}
|
2013-11-28 15:50:54 +00:00
|
|
|
return NULL;
|
2013-09-26 16:25:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MapSet FindMaps(HValue* object) {
|
2013-11-28 15:50:54 +00:00
|
|
|
HCheckTableEntry* entry = Find(object);
|
|
|
|
return entry == NULL ? NULL : entry->maps_;
|
2013-09-26 16:25:57 +00:00
|
|
|
}
|
|
|
|
|
2014-02-25 16:11:58 +00:00
|
|
|
void Insert(HValue* object, HInstruction* check, Unique<Map> map) {
|
2013-11-28 15:50:54 +00:00
|
|
|
MapSet list = new(phase_->zone()) UniqueSet<Map>();
|
|
|
|
list->Add(map, phase_->zone());
|
2014-02-25 16:11:58 +00:00
|
|
|
Insert(object, check, list);
|
2013-09-26 16:25:57 +00:00
|
|
|
}
|
|
|
|
|
2014-02-25 16:11:58 +00:00
|
|
|
void Insert(HValue* object, HInstruction* check, MapSet maps) {
|
2013-11-28 15:50:54 +00:00
|
|
|
HCheckTableEntry* entry = &entries_[cursor_++];
|
|
|
|
entry->object_ = object;
|
|
|
|
entry->check_ = check;
|
|
|
|
entry->maps_ = maps;
|
|
|
|
// If the table becomes full, wrap around and overwrite older entries.
|
|
|
|
if (cursor_ == kMaxTrackedObjects) cursor_ = 0;
|
|
|
|
if (size_ < kMaxTrackedObjects) size_++;
|
2013-09-26 16:25:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IsMapAccess(HObjectAccess access) {
|
|
|
|
return access.IsInobject() && access.offset() == JSObject::kMapOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
Unique<Map> MapConstant(HValue* value) {
|
|
|
|
return Unique<Map>::cast(HConstant::cast(value)->GetUnique());
|
|
|
|
}
|
|
|
|
|
2013-11-28 15:50:54 +00:00
|
|
|
friend class HCheckMapsEffects;
|
2014-02-11 19:18:06 +00:00
|
|
|
friend class HCheckEliminationPhase;
|
2013-09-26 16:25:57 +00:00
|
|
|
|
2013-11-28 15:50:54 +00:00
|
|
|
HCheckEliminationPhase* phase_;
|
|
|
|
HCheckTableEntry entries_[kMaxTrackedObjects];
|
|
|
|
int16_t cursor_; // Must be <= kMaxTrackedObjects
|
|
|
|
int16_t size_; // Must be <= kMaxTrackedObjects
|
2013-11-28 16:14:54 +00:00
|
|
|
// TODO(titzer): STATIC_ASSERT kMaxTrackedObjects < max(cursor_)
|
2013-11-28 15:50:54 +00:00
|
|
|
};
|
2013-09-26 16:25:57 +00:00
|
|
|
|
|
|
|
|
2013-11-28 15:50:54 +00:00
|
|
|
// Collects instructions that can cause effects that invalidate information
|
|
|
|
// needed for check elimination.
|
|
|
|
class HCheckMapsEffects : public ZoneObject {
|
|
|
|
public:
|
|
|
|
explicit HCheckMapsEffects(Zone* zone)
|
2014-02-25 16:11:58 +00:00
|
|
|
: maps_stored_(false),
|
|
|
|
stores_(5, zone) { }
|
2013-09-26 16:25:57 +00:00
|
|
|
|
2013-11-28 15:50:54 +00:00
|
|
|
inline bool Disabled() {
|
|
|
|
return false; // Effects are _not_ disabled.
|
|
|
|
}
|
2013-09-26 16:25:57 +00:00
|
|
|
|
2013-11-28 15:50:54 +00:00
|
|
|
// Process a possibly side-effecting instruction.
|
|
|
|
void Process(HInstruction* instr, Zone* zone) {
|
2014-02-25 16:11:58 +00:00
|
|
|
switch (instr->opcode()) {
|
|
|
|
case HValue::kStoreNamedField: {
|
|
|
|
stores_.Add(HStoreNamedField::cast(instr), zone);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case HValue::kOsrEntry: {
|
|
|
|
// Kill everything. Loads must not be hoisted past the OSR entry.
|
|
|
|
maps_stored_ = true;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
maps_stored_ |= (instr->CheckChangesFlag(kMaps) |
|
|
|
|
instr->CheckChangesFlag(kElementsKind));
|
|
|
|
}
|
2013-11-28 15:50:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply these effects to the given check elimination table.
|
|
|
|
void Apply(HCheckTable* table) {
|
2014-02-25 16:11:58 +00:00
|
|
|
if (maps_stored_) {
|
2013-11-28 15:50:54 +00:00
|
|
|
// Uncontrollable map modifications; kill everything.
|
2014-02-25 16:11:58 +00:00
|
|
|
table->Kill();
|
2013-11-28 15:50:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Kill maps for each store contained in these effects.
|
|
|
|
for (int i = 0; i < stores_.length(); i++) {
|
|
|
|
HStoreNamedField* s = stores_[i];
|
|
|
|
if (table->IsMapAccess(s->access()) || s->has_transition()) {
|
|
|
|
table->Kill(s->object()->ActualValue());
|
2013-09-26 16:25:57 +00:00
|
|
|
}
|
|
|
|
}
|
2013-11-28 15:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Union these effects with the other effects.
|
|
|
|
void Union(HCheckMapsEffects* that, Zone* zone) {
|
2014-02-25 16:11:58 +00:00
|
|
|
maps_stored_ |= that->maps_stored_;
|
2013-11-28 15:50:54 +00:00
|
|
|
for (int i = 0; i < that->stores_.length(); i++) {
|
|
|
|
stores_.Add(that->stores_[i], zone);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-02-25 16:11:58 +00:00
|
|
|
bool maps_stored_ : 1;
|
2013-11-28 15:50:54 +00:00
|
|
|
ZoneList<HStoreNamedField*> stores_;
|
|
|
|
};
|
2013-09-26 16:25:57 +00:00
|
|
|
|
2013-11-28 15:50:54 +00:00
|
|
|
|
|
|
|
// The main routine of the analysis phase. Use the HFlowEngine for either a
|
|
|
|
// local or a global analysis.
|
|
|
|
void HCheckEliminationPhase::Run() {
|
|
|
|
HFlowEngine<HCheckTable, HCheckMapsEffects> engine(graph(), zone());
|
|
|
|
HCheckTable* table = new(zone()) HCheckTable(this);
|
|
|
|
|
|
|
|
if (GLOBAL) {
|
|
|
|
// Perform a global analysis.
|
|
|
|
engine.AnalyzeDominatedBlocks(graph()->blocks()->at(0), table);
|
|
|
|
} else {
|
|
|
|
// Perform only local analysis.
|
|
|
|
for (int i = 0; i < graph()->blocks()->length(); i++) {
|
2014-02-25 16:11:58 +00:00
|
|
|
table->Kill();
|
2013-11-28 15:50:54 +00:00
|
|
|
engine.AnalyzeOneBlock(graph()->blocks()->at(i), table);
|
|
|
|
}
|
2013-09-26 16:25:57 +00:00
|
|
|
}
|
|
|
|
|
2013-11-28 15:50:54 +00:00
|
|
|
if (FLAG_trace_check_elimination) PrintStats();
|
2013-09-26 16:25:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-28 15:50:54 +00:00
|
|
|
// Are we eliminated yet?
|
|
|
|
void HCheckEliminationPhase::PrintStats() {
|
|
|
|
#if DEBUG
|
2013-11-29 12:37:35 +00:00
|
|
|
#define PRINT_STAT(x) if (x##_ > 0) PrintF(" %-16s = %2d\n", #x, x##_)
|
|
|
|
#else
|
|
|
|
#define PRINT_STAT(x)
|
2013-11-28 15:50:54 +00:00
|
|
|
#endif
|
2013-11-29 12:37:35 +00:00
|
|
|
PRINT_STAT(redundant);
|
|
|
|
PRINT_STAT(removed);
|
|
|
|
PRINT_STAT(removed_cho);
|
|
|
|
PRINT_STAT(narrowed);
|
|
|
|
PRINT_STAT(loads);
|
|
|
|
PRINT_STAT(empty);
|
|
|
|
PRINT_STAT(compares_true);
|
|
|
|
PRINT_STAT(compares_false);
|
|
|
|
PRINT_STAT(transitions);
|
2013-11-28 15:50:54 +00:00
|
|
|
}
|
|
|
|
|
2013-09-26 16:25:57 +00:00
|
|
|
} } // namespace v8::internal
|