#ifndef V8_COMPILER_BYTECODE_ANALYSIS_H_
#define V8_COMPILER_BYTECODE_ANALYSIS_H_
#include <optional>
#include "src/compiler/bytecode-liveness-map.h"
#include "src/handles/handles.h"
#include "src/interpreter/bytecode-register.h"
#include "src/utils/bit-vector.h"
#include "src/utils/utils.h"
#include "src/zone/zone-containers.h"
namespace v8 {
namespace internal {
class BytecodeArray;
namespace compiler {
class V8_EXPORT_PRIVATE BytecodeLoopAssignments {
public:
BytecodeLoopAssignments(int parameter_count, int register_count, Zone* zone);
void Add(interpreter::Register r);
void AddList(interpreter::Register r, uint32_t count);
void Union(const BytecodeLoopAssignments& other);
bool ContainsParameter(int index) const;
bool ContainsLocal(int index) const;
int parameter_count() const { return parameter_count_; }
int local_count() const { return bit_vector_->length() - parameter_count_; }
private:
int parameter_count_;
BitVector* bit_vector_;
};
class V8_EXPORT_PRIVATE ResumeJumpTarget {
public:
static ResumeJumpTarget Leaf(int suspend_id, int target_offset);
static ResumeJumpTarget AtLoopHeader(int loop_header_offset,
const ResumeJumpTarget& next);
int suspend_id() const { return suspend_id_; }
int target_offset() const { return target_offset_; }
bool is_leaf() const { return target_offset_ == final_target_offset_; }
private:
int suspend_id_;
int target_offset_;
int final_target_offset_;
ResumeJumpTarget(int suspend_id, int target_offset, int final_target_offset);
};
struct V8_EXPORT_PRIVATE LoopInfo {
public:
LoopInfo(int parent_offset, int loop_start, int loop_end, int parameter_count,
int register_count, Zone* zone)
: parent_offset_(parent_offset),
loop_start_(loop_start),
loop_end_(loop_end),
assignments_(parameter_count, register_count, zone),
resume_jump_targets_(zone) {}
LoopInfo(const LoopInfo&) V8_NOEXCEPT = default;
LoopInfo(LoopInfo&&) V8_NOEXCEPT = default;
LoopInfo& operator=(const LoopInfo&) V8_NOEXCEPT = default;
LoopInfo& operator=(LoopInfo&&) V8_NOEXCEPT = default;
~LoopInfo() = default;
int parent_offset() const { return parent_offset_; }
int loop_start() const { return loop_start_; }
int loop_end() const { return loop_end_; }
bool resumable() const { return resumable_; }
void mark_resumable() { resumable_ = true; }
bool innermost() const { return innermost_; }
void mark_not_innermost() { innermost_ = false; }
bool Contains(int offset) const {
return offset >= loop_start_ && offset < loop_end_;
}
const ZoneVector<ResumeJumpTarget>& resume_jump_targets() const {
return resume_jump_targets_;
}
void AddResumeTarget(const ResumeJumpTarget& target) {
resume_jump_targets_.push_back(target);
}
BytecodeLoopAssignments& assignments() { return assignments_; }
const BytecodeLoopAssignments& assignments() const { return assignments_; }
private:
int parent_offset_;
int loop_start_;
int loop_end_;
bool resumable_ = false;
bool innermost_ = true;
BytecodeLoopAssignments assignments_;
ZoneVector<ResumeJumpTarget> resume_jump_targets_;
};
class V8_EXPORT_PRIVATE BytecodeAnalysis : public ZoneObject {
public:
BytecodeAnalysis(Handle<BytecodeArray> bytecode_array, Zone* zone,
BytecodeOffset osr_bailout_id, bool analyze_liveness);
BytecodeAnalysis(const BytecodeAnalysis&) = delete;
BytecodeAnalysis& operator=(const BytecodeAnalysis&) = delete;
bool IsLoopHeader(int offset) const;
int GetLoopOffsetFor(int offset) const;
int GetLoopEndOffsetForInnermost(int header_offset) const;
const LoopInfo& GetLoopInfoFor(int header_offset) const;
const LoopInfo* TryGetLoopInfoFor(int header_offset) const;
const base::Vector<const LoopInfo>& GetLoopInfos() const {
return loop_infos_;
}
const ZoneVector<ResumeJumpTarget>& resume_jump_targets() const {
return resume_jump_targets_;
}
const BytecodeLivenessState* GetInLivenessFor(int offset) const;
const BytecodeLivenessState* GetOutLivenessFor(int offset) const;
int osr_entry_point() const {
CHECK_LE(0, osr_entry_point_);
return osr_entry_point_;
}
BytecodeOffset osr_bailout_id() const { return osr_bailout_id_; }
bool liveness_analyzed() const { return analyze_liveness_; }
int bytecode_count() const { return bytecode_count_; }
private:
BytecodeLivenessMap& liveness_map() {
DCHECK(analyze_liveness_);
return *liveness_map_;
}
const BytecodeLivenessMap& liveness_map() const {
DCHECK(analyze_liveness_);
return *liveness_map_;
}
BytecodeOffset const osr_bailout_id_;
bool const analyze_liveness_;
ZoneVector<ResumeJumpTarget> resume_jump_targets_;
ZoneMap<int, int> end_to_header_;
base::Vector<const LoopInfo> loop_infos_;
int osr_entry_point_;
std::optional<BytecodeLivenessMap> liveness_map_;
int bytecode_count_ = -1;
class BytecodeAnalysisImpl;
friend class BytecodeAnalysisImpl;
};
}
}
}
#endif