2016-11-22 18:05:03 +00:00
|
|
|
// Copyright 2016 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.
|
|
|
|
|
|
|
|
#ifndef V8_COMPILER_BYTECODE_ANALYSIS_H_
|
|
|
|
#define V8_COMPILER_BYTECODE_ANALYSIS_H_
|
|
|
|
|
2016-11-29 12:26:54 +00:00
|
|
|
#include "src/base/hashmap.h"
|
|
|
|
#include "src/compiler/bytecode-liveness-map.h"
|
2019-05-22 12:44:24 +00:00
|
|
|
#include "src/handles/handles.h"
|
2016-12-15 13:24:19 +00:00
|
|
|
#include "src/interpreter/bytecode-register.h"
|
2019-05-23 13:27:57 +00:00
|
|
|
#include "src/utils/bit-vector.h"
|
|
|
|
#include "src/utils/utils.h"
|
2016-11-22 18:05:03 +00:00
|
|
|
#include "src/zone/zone-containers.h"
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
class BytecodeArray;
|
|
|
|
|
|
|
|
namespace compiler {
|
|
|
|
|
2016-12-15 13:24:19 +00:00
|
|
|
class V8_EXPORT_PRIVATE BytecodeLoopAssignments {
|
|
|
|
public:
|
|
|
|
BytecodeLoopAssignments(int parameter_count, int register_count, Zone* zone);
|
|
|
|
|
|
|
|
void Add(interpreter::Register r);
|
2017-06-02 11:55:48 +00:00
|
|
|
void AddList(interpreter::Register r, uint32_t count);
|
2016-12-15 13:24:19 +00:00
|
|
|
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:
|
2019-04-29 11:33:56 +00:00
|
|
|
int const parameter_count_;
|
|
|
|
BitVector* const bit_vector_;
|
2016-12-15 13:24:19 +00:00
|
|
|
};
|
|
|
|
|
2018-01-23 13:51:38 +00:00
|
|
|
// Jump targets for resuming a suspended generator.
|
|
|
|
class V8_EXPORT_PRIVATE ResumeJumpTarget {
|
|
|
|
public:
|
|
|
|
// Create a resume jump target representing an actual resume.
|
|
|
|
static ResumeJumpTarget Leaf(int suspend_id, int target_offset);
|
|
|
|
|
|
|
|
// Create a resume jump target at a loop header, which will have another
|
|
|
|
// resume jump after the loop header is crossed.
|
|
|
|
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:
|
|
|
|
// The suspend id of the resume.
|
|
|
|
int suspend_id_;
|
|
|
|
// The target offset of this resume jump.
|
|
|
|
int target_offset_;
|
|
|
|
// The final offset of this resume, which may be across multiple jumps.
|
|
|
|
int final_target_offset_;
|
|
|
|
|
|
|
|
ResumeJumpTarget(int suspend_id, int target_offset, int final_target_offset);
|
|
|
|
};
|
|
|
|
|
2016-12-15 13:24:19 +00:00
|
|
|
struct V8_EXPORT_PRIVATE LoopInfo {
|
|
|
|
public:
|
|
|
|
LoopInfo(int parent_offset, int parameter_count, int register_count,
|
|
|
|
Zone* zone)
|
|
|
|
: parent_offset_(parent_offset),
|
2018-01-23 13:51:38 +00:00
|
|
|
assignments_(parameter_count, register_count, zone),
|
|
|
|
resume_jump_targets_(zone) {}
|
2016-12-15 13:24:19 +00:00
|
|
|
|
|
|
|
int parent_offset() const { return parent_offset_; }
|
|
|
|
|
2018-01-23 13:51:38 +00:00
|
|
|
const ZoneVector<ResumeJumpTarget>& resume_jump_targets() const {
|
|
|
|
return resume_jump_targets_;
|
|
|
|
}
|
|
|
|
void AddResumeTarget(const ResumeJumpTarget& target) {
|
|
|
|
resume_jump_targets_.push_back(target);
|
|
|
|
}
|
|
|
|
|
2016-12-15 13:24:19 +00:00
|
|
|
BytecodeLoopAssignments& assignments() { return assignments_; }
|
|
|
|
const BytecodeLoopAssignments& assignments() const { return assignments_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
// The offset to the parent loop, or -1 if there is no parent.
|
|
|
|
int parent_offset_;
|
|
|
|
BytecodeLoopAssignments assignments_;
|
2018-01-23 13:51:38 +00:00
|
|
|
ZoneVector<ResumeJumpTarget> resume_jump_targets_;
|
2016-12-15 13:24:19 +00:00
|
|
|
};
|
|
|
|
|
2019-07-15 12:07:48 +00:00
|
|
|
// Analyze the bytecodes to find the loop ranges, loop nesting, loop assignments
|
|
|
|
// and liveness. NOTE: The broker/serializer relies on the fact that an
|
|
|
|
// analysis for OSR (osr_bailout_id is not None) subsumes an analysis for
|
|
|
|
// non-OSR (osr_bailout_id is None).
|
|
|
|
class V8_EXPORT_PRIVATE BytecodeAnalysis : public ZoneObject {
|
2016-11-22 18:05:03 +00:00
|
|
|
public:
|
2016-11-29 12:26:54 +00:00
|
|
|
BytecodeAnalysis(Handle<BytecodeArray> bytecode_array, Zone* zone,
|
2019-07-15 12:07:48 +00:00
|
|
|
BailoutId osr_bailout_id, bool analyze_liveness);
|
2016-11-22 18:05:03 +00:00
|
|
|
|
|
|
|
// Return true if the given offset is a loop header
|
|
|
|
bool IsLoopHeader(int offset) const;
|
|
|
|
// Get the loop header offset of the containing loop for arbitrary
|
|
|
|
// {offset}, or -1 if the {offset} is not inside any loop.
|
|
|
|
int GetLoopOffsetFor(int offset) const;
|
2016-12-15 13:24:19 +00:00
|
|
|
// Get the loop info of the loop header at {header_offset}.
|
|
|
|
const LoopInfo& GetLoopInfoFor(int header_offset) const;
|
2016-11-22 18:05:03 +00:00
|
|
|
|
2018-01-23 13:51:38 +00:00
|
|
|
// Get the top-level resume jump targets.
|
|
|
|
const ZoneVector<ResumeJumpTarget>& resume_jump_targets() const {
|
|
|
|
return resume_jump_targets_;
|
|
|
|
}
|
|
|
|
|
2019-07-15 12:07:48 +00:00
|
|
|
// Gets the in-/out-liveness for the bytecode at {offset}.
|
2016-12-08 12:47:45 +00:00
|
|
|
const BytecodeLivenessState* GetInLivenessFor(int offset) const;
|
|
|
|
const BytecodeLivenessState* GetOutLivenessFor(int offset) const;
|
2016-11-29 12:26:54 +00:00
|
|
|
|
2019-07-15 12:07:48 +00:00
|
|
|
// In the case of OSR, the analysis also computes the (bytecode offset of the)
|
|
|
|
// OSR entry point from the {osr_bailout_id} that was given to the
|
|
|
|
// constructor.
|
|
|
|
int osr_entry_point() const {
|
|
|
|
CHECK_LE(0, osr_entry_point_);
|
|
|
|
return osr_entry_point_;
|
|
|
|
}
|
|
|
|
// Return the osr_bailout_id (for verification purposes).
|
|
|
|
BailoutId osr_bailout_id() const { return osr_bailout_id_; }
|
|
|
|
|
|
|
|
// Return whether liveness analysis was performed (for verification purposes).
|
|
|
|
bool liveness_analyzed() const { return analyze_liveness_; }
|
|
|
|
|
2016-11-22 18:05:03 +00:00
|
|
|
private:
|
2016-12-15 13:24:19 +00:00
|
|
|
struct LoopStackEntry {
|
|
|
|
int header_offset;
|
|
|
|
LoopInfo* loop_info;
|
|
|
|
};
|
|
|
|
|
2019-07-15 12:07:48 +00:00
|
|
|
void Analyze();
|
2016-11-22 18:05:03 +00:00
|
|
|
void PushLoop(int loop_header, int loop_end);
|
|
|
|
|
2016-11-29 12:26:54 +00:00
|
|
|
#if DEBUG
|
2018-01-23 13:51:38 +00:00
|
|
|
bool ResumeJumpTargetsAreValid();
|
|
|
|
bool ResumeJumpTargetLeavesResolveSuspendIds(
|
|
|
|
int parent_offset,
|
|
|
|
const ZoneVector<ResumeJumpTarget>& resume_jump_targets,
|
|
|
|
std::map<int, int>* unresolved_suspend_ids);
|
|
|
|
|
2016-11-29 12:26:54 +00:00
|
|
|
bool LivenessIsValid();
|
|
|
|
#endif
|
|
|
|
|
2016-11-22 18:05:03 +00:00
|
|
|
Zone* zone() const { return zone_; }
|
|
|
|
Handle<BytecodeArray> bytecode_array() const { return bytecode_array_; }
|
|
|
|
|
2019-04-29 11:33:56 +00:00
|
|
|
std::ostream& PrintLivenessTo(std::ostream& os) const;
|
|
|
|
|
|
|
|
Handle<BytecodeArray> const bytecode_array_;
|
|
|
|
Zone* const zone_;
|
2019-07-15 12:07:48 +00:00
|
|
|
BailoutId const osr_bailout_id_;
|
|
|
|
bool const analyze_liveness_;
|
2016-12-15 13:24:19 +00:00
|
|
|
ZoneStack<LoopStackEntry> loop_stack_;
|
2016-12-05 13:03:07 +00:00
|
|
|
ZoneVector<int> loop_end_index_queue_;
|
2018-01-23 13:51:38 +00:00
|
|
|
ZoneVector<ResumeJumpTarget> resume_jump_targets_;
|
2016-11-22 18:05:03 +00:00
|
|
|
ZoneMap<int, int> end_to_header_;
|
2016-12-15 13:24:19 +00:00
|
|
|
ZoneMap<int, LoopInfo> header_to_info_;
|
2017-04-24 15:17:46 +00:00
|
|
|
int osr_entry_point_;
|
2016-11-29 12:26:54 +00:00
|
|
|
BytecodeLivenessMap liveness_map_;
|
|
|
|
|
2016-11-22 18:05:03 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(BytecodeAnalysis);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace compiler
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|
|
|
|
|
|
|
|
#endif // V8_COMPILER_BYTECODE_ANALYSIS_H_
|