2013-05-27 12:45:46 +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-05-27 12:45:46 +00:00
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/hydrogen.h"
|
|
|
|
#include "src/hydrogen-gvn.h"
|
|
|
|
#include "src/v8.h"
|
2013-05-27 12:45:46 +00:00
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
2014-09-02 07:07:52 +00:00
|
|
|
class HInstructionMap FINAL : public ZoneObject {
|
2013-05-27 12:45:46 +00:00
|
|
|
public:
|
2014-02-11 06:53:14 +00:00
|
|
|
HInstructionMap(Zone* zone, SideEffectsTracker* side_effects_tracker)
|
2013-05-27 12:45:46 +00:00
|
|
|
: array_size_(0),
|
|
|
|
lists_size_(0),
|
|
|
|
count_(0),
|
|
|
|
array_(NULL),
|
|
|
|
lists_(NULL),
|
2014-02-11 06:53:14 +00:00
|
|
|
free_list_head_(kNil),
|
|
|
|
side_effects_tracker_(side_effects_tracker) {
|
2013-05-27 12:45:46 +00:00
|
|
|
ResizeLists(kInitialSize, zone);
|
|
|
|
Resize(kInitialSize, zone);
|
|
|
|
}
|
|
|
|
|
2014-02-11 06:53:14 +00:00
|
|
|
void Kill(SideEffects side_effects);
|
2013-05-27 12:45:46 +00:00
|
|
|
|
2014-02-11 06:53:14 +00:00
|
|
|
void Add(HInstruction* instr, Zone* zone) {
|
|
|
|
present_depends_on_.Add(side_effects_tracker_->ComputeDependsOn(instr));
|
|
|
|
Insert(instr, zone);
|
2013-05-27 12:45:46 +00:00
|
|
|
}
|
|
|
|
|
2014-02-11 06:53:14 +00:00
|
|
|
HInstruction* Lookup(HInstruction* instr) const;
|
2013-05-27 12:45:46 +00:00
|
|
|
|
2014-02-11 06:53:14 +00:00
|
|
|
HInstructionMap* Copy(Zone* zone) const {
|
|
|
|
return new(zone) HInstructionMap(zone, this);
|
2013-05-27 12:45:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IsEmpty() const { return count_ == 0; }
|
|
|
|
|
|
|
|
private:
|
2014-02-11 06:53:14 +00:00
|
|
|
// A linked list of HInstruction* values. Stored in arrays.
|
|
|
|
struct HInstructionMapListElement {
|
|
|
|
HInstruction* instr;
|
2013-05-27 12:45:46 +00:00
|
|
|
int next; // Index in the array of the next list element.
|
|
|
|
};
|
|
|
|
static const int kNil = -1; // The end of a linked list
|
|
|
|
|
|
|
|
// Must be a power of 2.
|
|
|
|
static const int kInitialSize = 16;
|
|
|
|
|
2014-02-11 06:53:14 +00:00
|
|
|
HInstructionMap(Zone* zone, const HInstructionMap* other);
|
2013-05-27 12:45:46 +00:00
|
|
|
|
|
|
|
void Resize(int new_size, Zone* zone);
|
|
|
|
void ResizeLists(int new_size, Zone* zone);
|
2014-02-11 06:53:14 +00:00
|
|
|
void Insert(HInstruction* instr, Zone* zone);
|
2013-05-27 12:45:46 +00:00
|
|
|
uint32_t Bound(uint32_t value) const { return value & (array_size_ - 1); }
|
|
|
|
|
|
|
|
int array_size_;
|
|
|
|
int lists_size_;
|
2014-02-11 06:53:14 +00:00
|
|
|
int count_; // The number of values stored in the HInstructionMap.
|
|
|
|
SideEffects present_depends_on_;
|
|
|
|
HInstructionMapListElement* array_;
|
|
|
|
// Primary store - contains the first value
|
2013-05-27 12:45:46 +00:00
|
|
|
// with a given hash. Colliding elements are stored in linked lists.
|
2014-02-11 06:53:14 +00:00
|
|
|
HInstructionMapListElement* lists_;
|
|
|
|
// The linked lists containing hash collisions.
|
2013-05-27 12:45:46 +00:00
|
|
|
int free_list_head_; // Unused elements in lists_ are on the free list.
|
2014-02-11 06:53:14 +00:00
|
|
|
SideEffectsTracker* side_effects_tracker_;
|
2013-05-27 12:45:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-09-02 07:07:52 +00:00
|
|
|
class HSideEffectMap FINAL BASE_EMBEDDED {
|
2013-05-27 12:45:46 +00:00
|
|
|
public:
|
|
|
|
HSideEffectMap();
|
|
|
|
explicit HSideEffectMap(HSideEffectMap* other);
|
|
|
|
HSideEffectMap& operator= (const HSideEffectMap& other);
|
|
|
|
|
2014-02-11 06:53:14 +00:00
|
|
|
void Kill(SideEffects side_effects);
|
2013-05-27 12:45:46 +00:00
|
|
|
|
2014-02-11 06:53:14 +00:00
|
|
|
void Store(SideEffects side_effects, HInstruction* instr);
|
2013-05-27 12:45:46 +00:00
|
|
|
|
|
|
|
bool IsEmpty() const { return count_ == 0; }
|
|
|
|
|
|
|
|
inline HInstruction* operator[](int i) const {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(0 <= i);
|
|
|
|
DCHECK(i < kNumberOfTrackedSideEffects);
|
2013-05-27 12:45:46 +00:00
|
|
|
return data_[i];
|
|
|
|
}
|
|
|
|
inline HInstruction* at(int i) const { return operator[](i); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
int count_;
|
|
|
|
HInstruction* data_[kNumberOfTrackedSideEffects];
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void TraceGVN(const char* msg, ...) {
|
|
|
|
va_list arguments;
|
|
|
|
va_start(arguments, msg);
|
2014-06-30 13:25:46 +00:00
|
|
|
base::OS::VPrint(msg, arguments);
|
2013-05-27 12:45:46 +00:00
|
|
|
va_end(arguments);
|
|
|
|
}
|
|
|
|
|
2013-07-05 09:52:11 +00:00
|
|
|
|
2013-05-27 12:45:46 +00:00
|
|
|
// Wrap TraceGVN in macros to avoid the expense of evaluating its arguments when
|
|
|
|
// --trace-gvn is off.
|
|
|
|
#define TRACE_GVN_1(msg, a1) \
|
|
|
|
if (FLAG_trace_gvn) { \
|
|
|
|
TraceGVN(msg, a1); \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define TRACE_GVN_2(msg, a1, a2) \
|
|
|
|
if (FLAG_trace_gvn) { \
|
|
|
|
TraceGVN(msg, a1, a2); \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define TRACE_GVN_3(msg, a1, a2, a3) \
|
|
|
|
if (FLAG_trace_gvn) { \
|
|
|
|
TraceGVN(msg, a1, a2, a3); \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define TRACE_GVN_4(msg, a1, a2, a3, a4) \
|
|
|
|
if (FLAG_trace_gvn) { \
|
|
|
|
TraceGVN(msg, a1, a2, a3, a4); \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define TRACE_GVN_5(msg, a1, a2, a3, a4, a5) \
|
|
|
|
if (FLAG_trace_gvn) { \
|
|
|
|
TraceGVN(msg, a1, a2, a3, a4, a5); \
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-11 06:53:14 +00:00
|
|
|
HInstructionMap::HInstructionMap(Zone* zone, const HInstructionMap* other)
|
2013-05-27 12:45:46 +00:00
|
|
|
: array_size_(other->array_size_),
|
|
|
|
lists_size_(other->lists_size_),
|
|
|
|
count_(other->count_),
|
2014-02-11 06:53:14 +00:00
|
|
|
present_depends_on_(other->present_depends_on_),
|
|
|
|
array_(zone->NewArray<HInstructionMapListElement>(other->array_size_)),
|
|
|
|
lists_(zone->NewArray<HInstructionMapListElement>(other->lists_size_)),
|
|
|
|
free_list_head_(other->free_list_head_),
|
|
|
|
side_effects_tracker_(other->side_effects_tracker_) {
|
2014-05-27 07:57:22 +00:00
|
|
|
MemCopy(array_, other->array_,
|
|
|
|
array_size_ * sizeof(HInstructionMapListElement));
|
|
|
|
MemCopy(lists_, other->lists_,
|
|
|
|
lists_size_ * sizeof(HInstructionMapListElement));
|
2013-05-27 12:45:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-11 06:53:14 +00:00
|
|
|
void HInstructionMap::Kill(SideEffects changes) {
|
|
|
|
if (!present_depends_on_.ContainsAnyOf(changes)) return;
|
|
|
|
present_depends_on_.RemoveAll();
|
2013-05-27 12:45:46 +00:00
|
|
|
for (int i = 0; i < array_size_; ++i) {
|
2014-02-11 06:53:14 +00:00
|
|
|
HInstruction* instr = array_[i].instr;
|
|
|
|
if (instr != NULL) {
|
2013-05-27 12:45:46 +00:00
|
|
|
// Clear list of collisions first, so we know if it becomes empty.
|
|
|
|
int kept = kNil; // List of kept elements.
|
|
|
|
int next;
|
|
|
|
for (int current = array_[i].next; current != kNil; current = next) {
|
|
|
|
next = lists_[current].next;
|
2014-02-11 06:53:14 +00:00
|
|
|
HInstruction* instr = lists_[current].instr;
|
|
|
|
SideEffects depends_on = side_effects_tracker_->ComputeDependsOn(instr);
|
|
|
|
if (depends_on.ContainsAnyOf(changes)) {
|
2013-05-27 12:45:46 +00:00
|
|
|
// Drop it.
|
|
|
|
count_--;
|
|
|
|
lists_[current].next = free_list_head_;
|
|
|
|
free_list_head_ = current;
|
|
|
|
} else {
|
|
|
|
// Keep it.
|
|
|
|
lists_[current].next = kept;
|
|
|
|
kept = current;
|
2014-02-11 06:53:14 +00:00
|
|
|
present_depends_on_.Add(depends_on);
|
2013-05-27 12:45:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
array_[i].next = kept;
|
|
|
|
|
|
|
|
// Now possibly drop directly indexed element.
|
2014-02-11 06:53:14 +00:00
|
|
|
instr = array_[i].instr;
|
|
|
|
SideEffects depends_on = side_effects_tracker_->ComputeDependsOn(instr);
|
|
|
|
if (depends_on.ContainsAnyOf(changes)) { // Drop it.
|
2013-05-27 12:45:46 +00:00
|
|
|
count_--;
|
|
|
|
int head = array_[i].next;
|
|
|
|
if (head == kNil) {
|
2014-02-11 06:53:14 +00:00
|
|
|
array_[i].instr = NULL;
|
2013-05-27 12:45:46 +00:00
|
|
|
} else {
|
2014-02-11 06:53:14 +00:00
|
|
|
array_[i].instr = lists_[head].instr;
|
2013-05-27 12:45:46 +00:00
|
|
|
array_[i].next = lists_[head].next;
|
|
|
|
lists_[head].next = free_list_head_;
|
|
|
|
free_list_head_ = head;
|
|
|
|
}
|
|
|
|
} else {
|
2014-02-11 06:53:14 +00:00
|
|
|
present_depends_on_.Add(depends_on); // Keep it.
|
2013-05-27 12:45:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-11 06:53:14 +00:00
|
|
|
HInstruction* HInstructionMap::Lookup(HInstruction* instr) const {
|
|
|
|
uint32_t hash = static_cast<uint32_t>(instr->Hashcode());
|
2013-05-27 12:45:46 +00:00
|
|
|
uint32_t pos = Bound(hash);
|
2014-02-11 06:53:14 +00:00
|
|
|
if (array_[pos].instr != NULL) {
|
|
|
|
if (array_[pos].instr->Equals(instr)) return array_[pos].instr;
|
2013-05-27 12:45:46 +00:00
|
|
|
int next = array_[pos].next;
|
|
|
|
while (next != kNil) {
|
2014-02-11 06:53:14 +00:00
|
|
|
if (lists_[next].instr->Equals(instr)) return lists_[next].instr;
|
2013-05-27 12:45:46 +00:00
|
|
|
next = lists_[next].next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-11 06:53:14 +00:00
|
|
|
void HInstructionMap::Resize(int new_size, Zone* zone) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(new_size > count_);
|
2013-05-27 12:45:46 +00:00
|
|
|
// Hashing the values into the new array has no more collisions than in the
|
|
|
|
// old hash map, so we can use the existing lists_ array, if we are careful.
|
|
|
|
|
|
|
|
// Make sure we have at least one free element.
|
|
|
|
if (free_list_head_ == kNil) {
|
|
|
|
ResizeLists(lists_size_ << 1, zone);
|
|
|
|
}
|
|
|
|
|
2014-02-11 06:53:14 +00:00
|
|
|
HInstructionMapListElement* new_array =
|
|
|
|
zone->NewArray<HInstructionMapListElement>(new_size);
|
|
|
|
memset(new_array, 0, sizeof(HInstructionMapListElement) * new_size);
|
2013-05-27 12:45:46 +00:00
|
|
|
|
2014-02-11 06:53:14 +00:00
|
|
|
HInstructionMapListElement* old_array = array_;
|
2013-05-27 12:45:46 +00:00
|
|
|
int old_size = array_size_;
|
|
|
|
|
|
|
|
int old_count = count_;
|
|
|
|
count_ = 0;
|
2014-02-11 06:53:14 +00:00
|
|
|
// Do not modify present_depends_on_. It is currently correct.
|
2013-05-27 12:45:46 +00:00
|
|
|
array_size_ = new_size;
|
|
|
|
array_ = new_array;
|
|
|
|
|
|
|
|
if (old_array != NULL) {
|
|
|
|
// Iterate over all the elements in lists, rehashing them.
|
|
|
|
for (int i = 0; i < old_size; ++i) {
|
2014-02-11 06:53:14 +00:00
|
|
|
if (old_array[i].instr != NULL) {
|
2013-05-27 12:45:46 +00:00
|
|
|
int current = old_array[i].next;
|
|
|
|
while (current != kNil) {
|
2014-02-11 06:53:14 +00:00
|
|
|
Insert(lists_[current].instr, zone);
|
2013-05-27 12:45:46 +00:00
|
|
|
int next = lists_[current].next;
|
|
|
|
lists_[current].next = free_list_head_;
|
|
|
|
free_list_head_ = current;
|
|
|
|
current = next;
|
|
|
|
}
|
2014-02-11 06:53:14 +00:00
|
|
|
// Rehash the directly stored instruction.
|
|
|
|
Insert(old_array[i].instr, zone);
|
2013-05-27 12:45:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
USE(old_count);
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(count_ == old_count);
|
2013-05-27 12:45:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-11 06:53:14 +00:00
|
|
|
void HInstructionMap::ResizeLists(int new_size, Zone* zone) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(new_size > lists_size_);
|
2013-05-27 12:45:46 +00:00
|
|
|
|
2014-02-11 06:53:14 +00:00
|
|
|
HInstructionMapListElement* new_lists =
|
|
|
|
zone->NewArray<HInstructionMapListElement>(new_size);
|
|
|
|
memset(new_lists, 0, sizeof(HInstructionMapListElement) * new_size);
|
2013-05-27 12:45:46 +00:00
|
|
|
|
2014-02-11 06:53:14 +00:00
|
|
|
HInstructionMapListElement* old_lists = lists_;
|
2013-05-27 12:45:46 +00:00
|
|
|
int old_size = lists_size_;
|
|
|
|
|
|
|
|
lists_size_ = new_size;
|
|
|
|
lists_ = new_lists;
|
|
|
|
|
|
|
|
if (old_lists != NULL) {
|
2014-05-27 07:57:22 +00:00
|
|
|
MemCopy(lists_, old_lists, old_size * sizeof(HInstructionMapListElement));
|
2013-05-27 12:45:46 +00:00
|
|
|
}
|
|
|
|
for (int i = old_size; i < lists_size_; ++i) {
|
|
|
|
lists_[i].next = free_list_head_;
|
|
|
|
free_list_head_ = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-11 06:53:14 +00:00
|
|
|
void HInstructionMap::Insert(HInstruction* instr, Zone* zone) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(instr != NULL);
|
2013-05-27 12:45:46 +00:00
|
|
|
// Resizing when half of the hashtable is filled up.
|
|
|
|
if (count_ >= array_size_ >> 1) Resize(array_size_ << 1, zone);
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(count_ < array_size_);
|
2013-05-27 12:45:46 +00:00
|
|
|
count_++;
|
2014-02-11 06:53:14 +00:00
|
|
|
uint32_t pos = Bound(static_cast<uint32_t>(instr->Hashcode()));
|
|
|
|
if (array_[pos].instr == NULL) {
|
|
|
|
array_[pos].instr = instr;
|
2013-05-27 12:45:46 +00:00
|
|
|
array_[pos].next = kNil;
|
|
|
|
} else {
|
|
|
|
if (free_list_head_ == kNil) {
|
|
|
|
ResizeLists(lists_size_ << 1, zone);
|
|
|
|
}
|
|
|
|
int new_element_pos = free_list_head_;
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(new_element_pos != kNil);
|
2013-05-27 12:45:46 +00:00
|
|
|
free_list_head_ = lists_[free_list_head_].next;
|
2014-02-11 06:53:14 +00:00
|
|
|
lists_[new_element_pos].instr = instr;
|
2013-05-27 12:45:46 +00:00
|
|
|
lists_[new_element_pos].next = array_[pos].next;
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(array_[pos].next == kNil || lists_[array_[pos].next].instr != NULL);
|
2013-05-27 12:45:46 +00:00
|
|
|
array_[pos].next = new_element_pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
HSideEffectMap::HSideEffectMap() : count_(0) {
|
|
|
|
memset(data_, 0, kNumberOfTrackedSideEffects * kPointerSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
HSideEffectMap::HSideEffectMap(HSideEffectMap* other) : count_(other->count_) {
|
|
|
|
*this = *other; // Calls operator=.
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-27 07:57:22 +00:00
|
|
|
HSideEffectMap& HSideEffectMap::operator=(const HSideEffectMap& other) {
|
2013-05-27 12:45:46 +00:00
|
|
|
if (this != &other) {
|
2014-05-27 07:57:22 +00:00
|
|
|
MemCopy(data_, other.data_, kNumberOfTrackedSideEffects * kPointerSize);
|
2013-05-27 12:45:46 +00:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2013-07-05 09:52:11 +00:00
|
|
|
|
2014-02-11 06:53:14 +00:00
|
|
|
void HSideEffectMap::Kill(SideEffects side_effects) {
|
2013-05-27 12:45:46 +00:00
|
|
|
for (int i = 0; i < kNumberOfTrackedSideEffects; i++) {
|
2014-02-11 06:53:14 +00:00
|
|
|
if (side_effects.ContainsFlag(GVNFlagFromInt(i))) {
|
2013-05-27 12:45:46 +00:00
|
|
|
if (data_[i] != NULL) count_--;
|
|
|
|
data_[i] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-11 06:53:14 +00:00
|
|
|
void HSideEffectMap::Store(SideEffects side_effects, HInstruction* instr) {
|
2013-05-27 12:45:46 +00:00
|
|
|
for (int i = 0; i < kNumberOfTrackedSideEffects; i++) {
|
2014-02-11 06:53:14 +00:00
|
|
|
if (side_effects.ContainsFlag(GVNFlagFromInt(i))) {
|
2013-05-27 12:45:46 +00:00
|
|
|
if (data_[i] == NULL) count_++;
|
|
|
|
data_[i] = instr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-11 06:53:14 +00:00
|
|
|
SideEffects SideEffectsTracker::ComputeChanges(HInstruction* instr) {
|
2014-03-07 10:14:03 +00:00
|
|
|
int index;
|
2014-02-11 06:53:14 +00:00
|
|
|
SideEffects result(instr->ChangesFlags());
|
2014-03-07 10:14:03 +00:00
|
|
|
if (result.ContainsFlag(kGlobalVars)) {
|
2015-03-18 11:43:51 +00:00
|
|
|
if (instr->IsStoreNamedField()) {
|
|
|
|
HStoreNamedField* store = HStoreNamedField::cast(instr);
|
|
|
|
HConstant* target = HConstant::cast(store->object());
|
|
|
|
if (ComputeGlobalVar(Unique<PropertyCell>::cast(target->GetUnique()),
|
|
|
|
&index)) {
|
|
|
|
result.RemoveFlag(kGlobalVars);
|
2014-03-07 10:14:03 +00:00
|
|
|
result.AddSpecial(GlobalVar(index));
|
2015-03-18 11:43:51 +00:00
|
|
|
return result;
|
2014-03-07 10:14:03 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-18 11:43:51 +00:00
|
|
|
for (index = 0; index < kNumberOfGlobalVars; ++index) {
|
|
|
|
result.AddSpecial(GlobalVar(index));
|
|
|
|
}
|
|
|
|
} else if (result.ContainsFlag(kInobjectFields)) {
|
2014-02-11 06:53:14 +00:00
|
|
|
if (instr->IsStoreNamedField() &&
|
|
|
|
ComputeInobjectField(HStoreNamedField::cast(instr)->access(), &index)) {
|
|
|
|
result.RemoveFlag(kInobjectFields);
|
2014-03-07 10:14:03 +00:00
|
|
|
result.AddSpecial(InobjectField(index));
|
2014-02-11 06:53:14 +00:00
|
|
|
} else {
|
2014-03-07 10:14:03 +00:00
|
|
|
for (index = 0; index < kNumberOfInobjectFields; ++index) {
|
|
|
|
result.AddSpecial(InobjectField(index));
|
|
|
|
}
|
2014-02-11 06:53:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SideEffects SideEffectsTracker::ComputeDependsOn(HInstruction* instr) {
|
2014-03-07 10:14:03 +00:00
|
|
|
int index;
|
2014-02-11 06:53:14 +00:00
|
|
|
SideEffects result(instr->DependsOnFlags());
|
2014-03-07 10:14:03 +00:00
|
|
|
if (result.ContainsFlag(kGlobalVars)) {
|
2015-03-18 11:43:51 +00:00
|
|
|
if (instr->IsLoadNamedField()) {
|
|
|
|
HLoadNamedField* load = HLoadNamedField::cast(instr);
|
|
|
|
HConstant* target = HConstant::cast(load->object());
|
|
|
|
if (ComputeGlobalVar(Unique<PropertyCell>::cast(target->GetUnique()),
|
|
|
|
&index)) {
|
|
|
|
result.RemoveFlag(kGlobalVars);
|
2014-03-07 10:14:03 +00:00
|
|
|
result.AddSpecial(GlobalVar(index));
|
2015-03-18 11:43:51 +00:00
|
|
|
return result;
|
2014-03-07 10:14:03 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-18 11:43:51 +00:00
|
|
|
for (index = 0; index < kNumberOfGlobalVars; ++index) {
|
|
|
|
result.AddSpecial(GlobalVar(index));
|
|
|
|
}
|
|
|
|
} else if (result.ContainsFlag(kInobjectFields)) {
|
2014-02-11 06:53:14 +00:00
|
|
|
if (instr->IsLoadNamedField() &&
|
|
|
|
ComputeInobjectField(HLoadNamedField::cast(instr)->access(), &index)) {
|
|
|
|
result.RemoveFlag(kInobjectFields);
|
2014-03-07 10:14:03 +00:00
|
|
|
result.AddSpecial(InobjectField(index));
|
2014-02-11 06:53:14 +00:00
|
|
|
} else {
|
2014-03-07 10:14:03 +00:00
|
|
|
for (index = 0; index < kNumberOfInobjectFields; ++index) {
|
|
|
|
result.AddSpecial(InobjectField(index));
|
|
|
|
}
|
2014-02-11 06:53:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-09-30 10:29:32 +00:00
|
|
|
std::ostream& operator<<(std::ostream& os, const TrackedEffects& te) {
|
2014-07-07 09:57:29 +00:00
|
|
|
SideEffectsTracker* t = te.tracker;
|
2014-02-11 06:53:14 +00:00
|
|
|
const char* separator = "";
|
2014-07-07 09:57:29 +00:00
|
|
|
os << "[";
|
2014-02-11 06:53:14 +00:00
|
|
|
for (int bit = 0; bit < kNumberOfFlags; ++bit) {
|
|
|
|
GVNFlag flag = GVNFlagFromInt(bit);
|
2014-07-07 09:57:29 +00:00
|
|
|
if (te.effects.ContainsFlag(flag)) {
|
|
|
|
os << separator;
|
2014-02-11 06:53:14 +00:00
|
|
|
separator = ", ";
|
|
|
|
switch (flag) {
|
2014-07-07 09:57:29 +00:00
|
|
|
#define DECLARE_FLAG(Type) \
|
|
|
|
case k##Type: \
|
|
|
|
os << #Type; \
|
|
|
|
break;
|
2014-02-11 06:53:14 +00:00
|
|
|
GVN_TRACKED_FLAG_LIST(DECLARE_FLAG)
|
|
|
|
GVN_UNTRACKED_FLAG_LIST(DECLARE_FLAG)
|
|
|
|
#undef DECLARE_FLAG
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-07-07 09:57:29 +00:00
|
|
|
for (int index = 0; index < t->num_global_vars_; ++index) {
|
|
|
|
if (te.effects.ContainsSpecial(t->GlobalVar(index))) {
|
|
|
|
os << separator << "[" << *t->global_vars_[index].handle() << "]";
|
2014-03-07 10:14:03 +00:00
|
|
|
separator = ", ";
|
|
|
|
}
|
|
|
|
}
|
2014-07-07 09:57:29 +00:00
|
|
|
for (int index = 0; index < t->num_inobject_fields_; ++index) {
|
|
|
|
if (te.effects.ContainsSpecial(t->InobjectField(index))) {
|
|
|
|
os << separator << t->inobject_fields_[index];
|
2014-02-11 06:53:14 +00:00
|
|
|
separator = ", ";
|
|
|
|
}
|
|
|
|
}
|
2014-07-07 09:57:29 +00:00
|
|
|
os << "]";
|
|
|
|
return os;
|
2014-02-11 06:53:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-18 11:43:51 +00:00
|
|
|
bool SideEffectsTracker::ComputeGlobalVar(Unique<PropertyCell> cell,
|
|
|
|
int* index) {
|
2014-03-07 10:14:03 +00:00
|
|
|
for (int i = 0; i < num_global_vars_; ++i) {
|
|
|
|
if (cell == global_vars_[i]) {
|
|
|
|
*index = i;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (num_global_vars_ < kNumberOfGlobalVars) {
|
|
|
|
if (FLAG_trace_gvn) {
|
2014-07-03 07:18:30 +00:00
|
|
|
OFStream os(stdout);
|
|
|
|
os << "Tracking global var [" << *cell.handle() << "] "
|
2014-09-30 10:29:32 +00:00
|
|
|
<< "(mapped to index " << num_global_vars_ << ")" << std::endl;
|
2014-03-07 10:14:03 +00:00
|
|
|
}
|
|
|
|
*index = num_global_vars_;
|
|
|
|
global_vars_[num_global_vars_++] = cell;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-11 06:53:14 +00:00
|
|
|
bool SideEffectsTracker::ComputeInobjectField(HObjectAccess access,
|
|
|
|
int* index) {
|
|
|
|
for (int i = 0; i < num_inobject_fields_; ++i) {
|
2014-06-16 13:03:59 +00:00
|
|
|
if (access.Equals(inobject_fields_[i])) {
|
2014-02-11 06:53:14 +00:00
|
|
|
*index = i;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2014-03-07 10:14:03 +00:00
|
|
|
if (num_inobject_fields_ < kNumberOfInobjectFields) {
|
2014-02-11 06:53:14 +00:00
|
|
|
if (FLAG_trace_gvn) {
|
2014-07-07 09:57:29 +00:00
|
|
|
OFStream os(stdout);
|
|
|
|
os << "Tracking inobject field access " << access << " (mapped to index "
|
2014-09-30 10:29:32 +00:00
|
|
|
<< num_inobject_fields_ << ")" << std::endl;
|
2014-02-11 06:53:14 +00:00
|
|
|
}
|
|
|
|
*index = num_inobject_fields_;
|
|
|
|
inobject_fields_[num_inobject_fields_++] = access;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-27 13:09:08 +00:00
|
|
|
HGlobalValueNumberingPhase::HGlobalValueNumberingPhase(HGraph* graph)
|
2014-02-06 12:35:17 +00:00
|
|
|
: HPhase("H_Global value numbering", graph),
|
|
|
|
removed_side_effects_(false),
|
|
|
|
block_side_effects_(graph->blocks()->length(), zone()),
|
|
|
|
loop_side_effects_(graph->blocks()->length(), zone()),
|
|
|
|
visited_on_paths_(graph->blocks()->length(), zone()) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(!AllowHandleAllocation::IsAllowed());
|
2014-02-11 06:53:14 +00:00
|
|
|
block_side_effects_.AddBlock(
|
|
|
|
SideEffects(), graph->blocks()->length(), zone());
|
|
|
|
loop_side_effects_.AddBlock(
|
|
|
|
SideEffects(), graph->blocks()->length(), zone());
|
2014-02-05 17:21:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-06 12:35:17 +00:00
|
|
|
void HGlobalValueNumberingPhase::Run() {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(!removed_side_effects_);
|
2014-02-06 12:35:17 +00:00
|
|
|
for (int i = FLAG_gvn_iterations; i > 0; --i) {
|
|
|
|
// Compute the side effects.
|
|
|
|
ComputeBlockSideEffects();
|
|
|
|
|
|
|
|
// Perform loop invariant code motion if requested.
|
|
|
|
if (FLAG_loop_invariant_code_motion) LoopInvariantCodeMotion();
|
2014-02-05 17:21:34 +00:00
|
|
|
|
2014-02-06 12:35:17 +00:00
|
|
|
// Perform the actual value numbering.
|
|
|
|
AnalyzeGraph();
|
2013-05-27 12:45:46 +00:00
|
|
|
|
2014-02-06 12:35:17 +00:00
|
|
|
// Continue GVN if we removed any side effects.
|
|
|
|
if (!removed_side_effects_) break;
|
|
|
|
removed_side_effects_ = false;
|
|
|
|
|
|
|
|
// Clear all side effects.
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK_EQ(block_side_effects_.length(), graph()->blocks()->length());
|
|
|
|
DCHECK_EQ(loop_side_effects_.length(), graph()->blocks()->length());
|
2014-02-06 12:35:17 +00:00
|
|
|
for (int i = 0; i < graph()->blocks()->length(); ++i) {
|
|
|
|
block_side_effects_[i].RemoveAll();
|
|
|
|
loop_side_effects_[i].RemoveAll();
|
|
|
|
}
|
|
|
|
visited_on_paths_.Clear();
|
2013-05-27 12:45:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-27 13:09:08 +00:00
|
|
|
void HGlobalValueNumberingPhase::ComputeBlockSideEffects() {
|
|
|
|
for (int i = graph()->blocks()->length() - 1; i >= 0; --i) {
|
2013-05-27 12:45:46 +00:00
|
|
|
// Compute side effects for the block.
|
2013-06-27 13:09:08 +00:00
|
|
|
HBasicBlock* block = graph()->blocks()->at(i);
|
2014-02-11 06:53:14 +00:00
|
|
|
SideEffects side_effects;
|
2013-10-02 11:43:41 +00:00
|
|
|
if (block->IsReachable() && !block->IsDeoptimizing()) {
|
|
|
|
int id = block->block_id();
|
|
|
|
for (HInstructionIterator it(block); !it.Done(); it.Advance()) {
|
|
|
|
HInstruction* instr = it.Current();
|
2014-02-11 06:53:14 +00:00
|
|
|
side_effects.Add(side_effects_tracker_.ComputeChanges(instr));
|
2013-05-27 12:45:46 +00:00
|
|
|
}
|
2013-10-02 11:43:41 +00:00
|
|
|
block_side_effects_[id].Add(side_effects);
|
2013-05-27 12:45:46 +00:00
|
|
|
|
2013-10-02 11:43:41 +00:00
|
|
|
// Loop headers are part of their loop.
|
|
|
|
if (block->IsLoopHeader()) {
|
|
|
|
loop_side_effects_[id].Add(side_effects);
|
|
|
|
}
|
2013-05-27 12:45:46 +00:00
|
|
|
|
2013-10-02 11:43:41 +00:00
|
|
|
// Propagate loop side effects upwards.
|
|
|
|
if (block->HasParentLoopHeader()) {
|
2013-12-05 18:31:06 +00:00
|
|
|
HBasicBlock* with_parent = block;
|
|
|
|
if (block->IsLoopHeader()) side_effects = loop_side_effects_[id];
|
|
|
|
do {
|
|
|
|
HBasicBlock* parent_block = with_parent->parent_loop_header();
|
|
|
|
loop_side_effects_[parent_block->block_id()].Add(side_effects);
|
|
|
|
with_parent = parent_block;
|
|
|
|
} while (with_parent->HasParentLoopHeader());
|
2013-10-02 11:43:41 +00:00
|
|
|
}
|
2013-05-27 12:45:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-27 13:09:08 +00:00
|
|
|
void HGlobalValueNumberingPhase::LoopInvariantCodeMotion() {
|
2013-05-27 12:45:46 +00:00
|
|
|
TRACE_GVN_1("Using optimistic loop invariant code motion: %s\n",
|
2013-06-27 13:09:08 +00:00
|
|
|
graph()->use_optimistic_licm() ? "yes" : "no");
|
|
|
|
for (int i = graph()->blocks()->length() - 1; i >= 0; --i) {
|
|
|
|
HBasicBlock* block = graph()->blocks()->at(i);
|
2014-03-14 12:05:01 +00:00
|
|
|
if (block->IsLoopHeader()) {
|
2014-02-11 06:53:14 +00:00
|
|
|
SideEffects side_effects = loop_side_effects_[block->block_id()];
|
|
|
|
if (FLAG_trace_gvn) {
|
2014-07-07 09:57:29 +00:00
|
|
|
OFStream os(stdout);
|
|
|
|
os << "Try loop invariant motion for " << *block << " changes "
|
2014-09-30 10:29:32 +00:00
|
|
|
<< Print(side_effects) << std::endl;
|
2014-02-11 06:53:14 +00:00
|
|
|
}
|
2013-05-27 12:45:46 +00:00
|
|
|
HBasicBlock* last = block->loop_information()->GetLastBackEdge();
|
|
|
|
for (int j = block->block_id(); j <= last->block_id(); ++j) {
|
2014-02-10 08:59:51 +00:00
|
|
|
ProcessLoopBlock(graph()->blocks()->at(j), block, side_effects);
|
2013-05-27 12:45:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-27 13:09:08 +00:00
|
|
|
void HGlobalValueNumberingPhase::ProcessLoopBlock(
|
2013-05-27 12:45:46 +00:00
|
|
|
HBasicBlock* block,
|
|
|
|
HBasicBlock* loop_header,
|
2014-02-11 06:53:14 +00:00
|
|
|
SideEffects loop_kills) {
|
2013-05-27 12:45:46 +00:00
|
|
|
HBasicBlock* pre_header = loop_header->predecessors()->at(0);
|
2014-02-11 06:53:14 +00:00
|
|
|
if (FLAG_trace_gvn) {
|
2014-07-07 09:57:29 +00:00
|
|
|
OFStream os(stdout);
|
|
|
|
os << "Loop invariant code motion for " << *block << " depends on "
|
2014-09-30 10:29:32 +00:00
|
|
|
<< Print(loop_kills) << std::endl;
|
2014-02-11 06:53:14 +00:00
|
|
|
}
|
2013-05-27 12:45:46 +00:00
|
|
|
HInstruction* instr = block->first();
|
|
|
|
while (instr != NULL) {
|
|
|
|
HInstruction* next = instr->next();
|
|
|
|
if (instr->CheckFlag(HValue::kUseGVN)) {
|
2014-02-11 06:53:14 +00:00
|
|
|
SideEffects changes = side_effects_tracker_.ComputeChanges(instr);
|
|
|
|
SideEffects depends_on = side_effects_tracker_.ComputeDependsOn(instr);
|
|
|
|
if (FLAG_trace_gvn) {
|
2014-07-07 09:57:29 +00:00
|
|
|
OFStream os(stdout);
|
|
|
|
os << "Checking instruction i" << instr->id() << " ("
|
|
|
|
<< instr->Mnemonic() << ") changes " << Print(changes)
|
|
|
|
<< ", depends on " << Print(depends_on) << ". Loop changes "
|
2014-09-30 10:29:32 +00:00
|
|
|
<< Print(loop_kills) << std::endl;
|
2014-02-11 06:53:14 +00:00
|
|
|
}
|
|
|
|
bool can_hoist = !depends_on.ContainsAnyOf(loop_kills);
|
2013-05-27 12:45:46 +00:00
|
|
|
if (can_hoist && !graph()->use_optimistic_licm()) {
|
|
|
|
can_hoist = block->IsLoopSuccessorDominator();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (can_hoist) {
|
|
|
|
bool inputs_loop_invariant = true;
|
|
|
|
for (int i = 0; i < instr->OperandCount(); ++i) {
|
|
|
|
if (instr->OperandAt(i)->IsDefinedAfter(pre_header)) {
|
|
|
|
inputs_loop_invariant = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inputs_loop_invariant && ShouldMove(instr, loop_header)) {
|
2013-12-05 18:31:06 +00:00
|
|
|
TRACE_GVN_2("Hoisting loop invariant instruction i%d to block B%d\n",
|
|
|
|
instr->id(), pre_header->block_id());
|
2013-05-27 12:45:46 +00:00
|
|
|
// Move the instruction out of the loop.
|
|
|
|
instr->Unlink();
|
|
|
|
instr->InsertBefore(pre_header->end());
|
|
|
|
if (instr->HasSideEffects()) removed_side_effects_ = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
instr = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-27 13:09:08 +00:00
|
|
|
bool HGlobalValueNumberingPhase::AllowCodeMotion() {
|
2013-05-27 12:45:46 +00:00
|
|
|
return info()->IsStub() || info()->opt_count() + 1 < FLAG_max_opt_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-27 13:09:08 +00:00
|
|
|
bool HGlobalValueNumberingPhase::ShouldMove(HInstruction* instr,
|
|
|
|
HBasicBlock* loop_header) {
|
2014-03-14 12:05:01 +00:00
|
|
|
// If we've disabled code motion or we're in a block that unconditionally
|
|
|
|
// deoptimizes, don't move any instructions.
|
|
|
|
return AllowCodeMotion() && !instr->block()->IsDeoptimizing() &&
|
|
|
|
instr->block()->IsReachable();
|
2013-05-27 12:45:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-11 06:53:14 +00:00
|
|
|
SideEffects
|
2013-06-27 13:09:08 +00:00
|
|
|
HGlobalValueNumberingPhase::CollectSideEffectsOnPathsToDominatedBlock(
|
2013-05-27 12:45:46 +00:00
|
|
|
HBasicBlock* dominator, HBasicBlock* dominated) {
|
2014-02-11 06:53:14 +00:00
|
|
|
SideEffects side_effects;
|
2013-05-27 12:45:46 +00:00
|
|
|
for (int i = 0; i < dominated->predecessors()->length(); ++i) {
|
|
|
|
HBasicBlock* block = dominated->predecessors()->at(i);
|
2013-07-15 16:58:20 +00:00
|
|
|
if (dominator->block_id() < block->block_id() &&
|
2013-05-27 12:45:46 +00:00
|
|
|
block->block_id() < dominated->block_id() &&
|
2013-07-16 07:07:04 +00:00
|
|
|
!visited_on_paths_.Contains(block->block_id())) {
|
|
|
|
visited_on_paths_.Add(block->block_id());
|
2013-05-27 12:45:46 +00:00
|
|
|
side_effects.Add(block_side_effects_[block->block_id()]);
|
|
|
|
if (block->IsLoopHeader()) {
|
|
|
|
side_effects.Add(loop_side_effects_[block->block_id()]);
|
|
|
|
}
|
|
|
|
side_effects.Add(CollectSideEffectsOnPathsToDominatedBlock(
|
|
|
|
dominator, block));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return side_effects;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Each instance of this class is like a "stack frame" for the recursive
|
|
|
|
// traversal of the dominator tree done during GVN (the stack is handled
|
|
|
|
// as a double linked list).
|
|
|
|
// We reuse frames when possible so the list length is limited by the depth
|
|
|
|
// of the dominator tree but this forces us to initialize each frame calling
|
|
|
|
// an explicit "Initialize" method instead of a using constructor.
|
|
|
|
class GvnBasicBlockState: public ZoneObject {
|
|
|
|
public:
|
|
|
|
static GvnBasicBlockState* CreateEntry(Zone* zone,
|
|
|
|
HBasicBlock* entry_block,
|
2014-02-11 06:53:14 +00:00
|
|
|
HInstructionMap* entry_map) {
|
2013-05-27 12:45:46 +00:00
|
|
|
return new(zone)
|
|
|
|
GvnBasicBlockState(NULL, entry_block, entry_map, NULL, zone);
|
|
|
|
}
|
|
|
|
|
|
|
|
HBasicBlock* block() { return block_; }
|
2014-02-11 06:53:14 +00:00
|
|
|
HInstructionMap* map() { return map_; }
|
2013-05-27 12:45:46 +00:00
|
|
|
HSideEffectMap* dominators() { return &dominators_; }
|
|
|
|
|
|
|
|
GvnBasicBlockState* next_in_dominator_tree_traversal(
|
|
|
|
Zone* zone,
|
|
|
|
HBasicBlock** dominator) {
|
|
|
|
// This assignment needs to happen before calling next_dominated() because
|
|
|
|
// that call can reuse "this" if we are at the last dominated block.
|
|
|
|
*dominator = block();
|
|
|
|
GvnBasicBlockState* result = next_dominated(zone);
|
|
|
|
if (result == NULL) {
|
|
|
|
GvnBasicBlockState* dominator_state = pop();
|
|
|
|
if (dominator_state != NULL) {
|
|
|
|
// This branch is guaranteed not to return NULL because pop() never
|
|
|
|
// returns a state where "is_done() == true".
|
|
|
|
*dominator = dominator_state->block();
|
|
|
|
result = dominator_state->next_dominated(zone);
|
|
|
|
} else {
|
|
|
|
// Unnecessary (we are returning NULL) but done for cleanness.
|
|
|
|
*dominator = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void Initialize(HBasicBlock* block,
|
2014-02-11 06:53:14 +00:00
|
|
|
HInstructionMap* map,
|
2013-05-27 12:45:46 +00:00
|
|
|
HSideEffectMap* dominators,
|
|
|
|
bool copy_map,
|
|
|
|
Zone* zone) {
|
|
|
|
block_ = block;
|
|
|
|
map_ = copy_map ? map->Copy(zone) : map;
|
|
|
|
dominated_index_ = -1;
|
|
|
|
length_ = block->dominated_blocks()->length();
|
|
|
|
if (dominators != NULL) {
|
|
|
|
dominators_ = *dominators;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bool is_done() { return dominated_index_ >= length_; }
|
|
|
|
|
|
|
|
GvnBasicBlockState(GvnBasicBlockState* previous,
|
|
|
|
HBasicBlock* block,
|
2014-02-11 06:53:14 +00:00
|
|
|
HInstructionMap* map,
|
2013-05-27 12:45:46 +00:00
|
|
|
HSideEffectMap* dominators,
|
|
|
|
Zone* zone)
|
|
|
|
: previous_(previous), next_(NULL) {
|
|
|
|
Initialize(block, map, dominators, true, zone);
|
|
|
|
}
|
|
|
|
|
|
|
|
GvnBasicBlockState* next_dominated(Zone* zone) {
|
2014-03-14 12:05:01 +00:00
|
|
|
dominated_index_++;
|
|
|
|
if (dominated_index_ == length_ - 1) {
|
|
|
|
// No need to copy the map for the last child in the dominator tree.
|
|
|
|
Initialize(block_->dominated_blocks()->at(dominated_index_),
|
|
|
|
map(),
|
|
|
|
dominators(),
|
|
|
|
false,
|
|
|
|
zone);
|
|
|
|
return this;
|
|
|
|
} else if (dominated_index_ < length_) {
|
|
|
|
return push(zone, block_->dominated_blocks()->at(dominated_index_));
|
|
|
|
} else {
|
|
|
|
return NULL;
|
2013-05-27 12:45:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-16 08:37:49 +00:00
|
|
|
GvnBasicBlockState* push(Zone* zone, HBasicBlock* block) {
|
2013-05-27 12:45:46 +00:00
|
|
|
if (next_ == NULL) {
|
|
|
|
next_ =
|
2013-07-16 08:37:49 +00:00
|
|
|
new(zone) GvnBasicBlockState(this, block, map(), dominators(), zone);
|
2013-05-27 12:45:46 +00:00
|
|
|
} else {
|
2013-07-16 08:37:49 +00:00
|
|
|
next_->Initialize(block, map(), dominators(), true, zone);
|
2013-05-27 12:45:46 +00:00
|
|
|
}
|
|
|
|
return next_;
|
|
|
|
}
|
|
|
|
GvnBasicBlockState* pop() {
|
|
|
|
GvnBasicBlockState* result = previous_;
|
|
|
|
while (result != NULL && result->is_done()) {
|
|
|
|
TRACE_GVN_2("Backtracking from block B%d to block b%d\n",
|
|
|
|
block()->block_id(),
|
|
|
|
previous_->block()->block_id())
|
|
|
|
result = result->previous_;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
GvnBasicBlockState* previous_;
|
|
|
|
GvnBasicBlockState* next_;
|
|
|
|
HBasicBlock* block_;
|
2014-02-11 06:53:14 +00:00
|
|
|
HInstructionMap* map_;
|
2013-05-27 12:45:46 +00:00
|
|
|
HSideEffectMap dominators_;
|
|
|
|
int dominated_index_;
|
|
|
|
int length_;
|
|
|
|
};
|
|
|
|
|
2013-07-05 09:52:11 +00:00
|
|
|
|
2013-05-27 12:45:46 +00:00
|
|
|
// This is a recursive traversal of the dominator tree but it has been turned
|
|
|
|
// into a loop to avoid stack overflows.
|
|
|
|
// The logical "stack frames" of the recursion are kept in a list of
|
|
|
|
// GvnBasicBlockState instances.
|
2013-06-27 13:09:08 +00:00
|
|
|
void HGlobalValueNumberingPhase::AnalyzeGraph() {
|
|
|
|
HBasicBlock* entry_block = graph()->entry_block();
|
2014-02-11 06:53:14 +00:00
|
|
|
HInstructionMap* entry_map =
|
|
|
|
new(zone()) HInstructionMap(zone(), &side_effects_tracker_);
|
2013-05-27 12:45:46 +00:00
|
|
|
GvnBasicBlockState* current =
|
2013-06-26 13:36:16 +00:00
|
|
|
GvnBasicBlockState::CreateEntry(zone(), entry_block, entry_map);
|
2013-05-27 12:45:46 +00:00
|
|
|
|
|
|
|
while (current != NULL) {
|
|
|
|
HBasicBlock* block = current->block();
|
2014-02-11 06:53:14 +00:00
|
|
|
HInstructionMap* map = current->map();
|
2013-05-27 12:45:46 +00:00
|
|
|
HSideEffectMap* dominators = current->dominators();
|
|
|
|
|
|
|
|
TRACE_GVN_2("Analyzing block B%d%s\n",
|
|
|
|
block->block_id(),
|
|
|
|
block->IsLoopHeader() ? " (loop header)" : "");
|
|
|
|
|
|
|
|
// If this is a loop header kill everything killed by the loop.
|
|
|
|
if (block->IsLoopHeader()) {
|
|
|
|
map->Kill(loop_side_effects_[block->block_id()]);
|
2013-07-15 16:58:20 +00:00
|
|
|
dominators->Kill(loop_side_effects_[block->block_id()]);
|
2013-05-27 12:45:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Go through all instructions of the current block.
|
2013-07-11 11:41:33 +00:00
|
|
|
for (HInstructionIterator it(block); !it.Done(); it.Advance()) {
|
|
|
|
HInstruction* instr = it.Current();
|
|
|
|
if (instr->CheckFlag(HValue::kTrackSideEffectDominators)) {
|
|
|
|
for (int i = 0; i < kNumberOfTrackedSideEffects; i++) {
|
|
|
|
HValue* other = dominators->at(i);
|
2014-02-11 06:53:14 +00:00
|
|
|
GVNFlag flag = GVNFlagFromInt(i);
|
|
|
|
if (instr->DependsOnFlags().Contains(flag) && other != NULL) {
|
2013-07-11 11:41:33 +00:00
|
|
|
TRACE_GVN_5("Side-effect #%d in %d (%s) is dominated by %d (%s)\n",
|
|
|
|
i,
|
|
|
|
instr->id(),
|
|
|
|
instr->Mnemonic(),
|
|
|
|
other->id(),
|
|
|
|
other->Mnemonic());
|
2014-02-11 06:53:14 +00:00
|
|
|
if (instr->HandleSideEffectDominator(flag, other)) {
|
2014-02-05 17:21:34 +00:00
|
|
|
removed_side_effects_ = true;
|
|
|
|
}
|
2013-07-11 11:41:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Instruction was unlinked during graph traversal.
|
|
|
|
if (!instr->IsLinked()) continue;
|
|
|
|
|
2014-02-11 06:53:14 +00:00
|
|
|
SideEffects changes = side_effects_tracker_.ComputeChanges(instr);
|
|
|
|
if (!changes.IsEmpty()) {
|
2013-05-27 12:45:46 +00:00
|
|
|
// Clear all instructions in the map that are affected by side effects.
|
|
|
|
// Store instruction as the dominating one for tracked side effects.
|
2014-02-11 06:53:14 +00:00
|
|
|
map->Kill(changes);
|
|
|
|
dominators->Store(changes, instr);
|
|
|
|
if (FLAG_trace_gvn) {
|
2014-07-07 09:57:29 +00:00
|
|
|
OFStream os(stdout);
|
|
|
|
os << "Instruction i" << instr->id() << " changes " << Print(changes)
|
2014-09-30 10:29:32 +00:00
|
|
|
<< std::endl;
|
2014-02-11 06:53:14 +00:00
|
|
|
}
|
2013-05-27 12:45:46 +00:00
|
|
|
}
|
2014-05-12 07:49:11 +00:00
|
|
|
if (instr->CheckFlag(HValue::kUseGVN) &&
|
|
|
|
!instr->CheckFlag(HValue::kCantBeReplaced)) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(!instr->HasObservableSideEffects());
|
2014-02-11 06:53:14 +00:00
|
|
|
HInstruction* other = map->Lookup(instr);
|
2013-05-27 12:45:46 +00:00
|
|
|
if (other != NULL) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(instr->Equals(other) && other->Equals(instr));
|
2014-02-11 06:53:14 +00:00
|
|
|
TRACE_GVN_4("Replacing instruction i%d (%s) with i%d (%s)\n",
|
2013-05-27 12:45:46 +00:00
|
|
|
instr->id(),
|
|
|
|
instr->Mnemonic(),
|
|
|
|
other->id(),
|
|
|
|
other->Mnemonic());
|
|
|
|
if (instr->HasSideEffects()) removed_side_effects_ = true;
|
|
|
|
instr->DeleteAndReplaceWith(other);
|
|
|
|
} else {
|
2013-06-26 13:36:16 +00:00
|
|
|
map->Add(instr, zone());
|
2013-05-27 12:45:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
HBasicBlock* dominator_block;
|
|
|
|
GvnBasicBlockState* next =
|
2013-06-26 13:36:16 +00:00
|
|
|
current->next_in_dominator_tree_traversal(zone(),
|
2013-06-24 13:37:46 +00:00
|
|
|
&dominator_block);
|
2013-05-27 12:45:46 +00:00
|
|
|
|
|
|
|
if (next != NULL) {
|
|
|
|
HBasicBlock* dominated = next->block();
|
2014-02-11 06:53:14 +00:00
|
|
|
HInstructionMap* successor_map = next->map();
|
2013-05-27 12:45:46 +00:00
|
|
|
HSideEffectMap* successor_dominators = next->dominators();
|
|
|
|
|
|
|
|
// Kill everything killed on any path between this block and the
|
|
|
|
// dominated block. We don't have to traverse these paths if the
|
|
|
|
// value map and the dominators list is already empty. If the range
|
|
|
|
// of block ids (block_id, dominated_id) is empty there are no such
|
|
|
|
// paths.
|
|
|
|
if ((!successor_map->IsEmpty() || !successor_dominators->IsEmpty()) &&
|
|
|
|
dominator_block->block_id() + 1 < dominated->block_id()) {
|
|
|
|
visited_on_paths_.Clear();
|
2014-02-11 06:53:14 +00:00
|
|
|
SideEffects side_effects_on_all_paths =
|
2013-05-27 12:45:46 +00:00
|
|
|
CollectSideEffectsOnPathsToDominatedBlock(dominator_block,
|
|
|
|
dominated);
|
|
|
|
successor_map->Kill(side_effects_on_all_paths);
|
|
|
|
successor_dominators->Kill(side_effects_on_all_paths);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
current = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} } // namespace v8::internal
|