1 // Copyright 2022 the V8 project authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef V8_MAGLEV_MAGLEV_GRAPH_H_ 6 #define V8_MAGLEV_MAGLEV_GRAPH_H_ 7 8 #include <vector> 9 10 #include "src/maglev/maglev-basic-block.h" 11 #include "src/zone/zone-allocator.h" 12 13 namespace v8 { 14 namespace internal { 15 namespace maglev { 16 17 using BlockConstIterator = 18 std::vector<BasicBlock*, ZoneAllocator<BasicBlock*>>::const_iterator; 19 using BlockConstReverseIterator = 20 std::vector<BasicBlock*, 21 ZoneAllocator<BasicBlock*>>::const_reverse_iterator; 22 23 class Graph final : public ZoneObject { 24 public: New(Zone * zone)25 static Graph* New(Zone* zone) { return zone->New<Graph>(zone); } 26 27 // Shouldn't be used directly; public so that Zone::New can access it. Graph(Zone * zone)28 explicit Graph(Zone* zone) : blocks_(zone) {} 29 30 BasicBlock* operator[](int i) { return blocks_[i]; } 31 const BasicBlock* operator[](int i) const { return blocks_[i]; } 32 num_blocks()33 int num_blocks() const { return static_cast<int>(blocks_.size()); } 34 begin()35 BlockConstIterator begin() const { return blocks_.begin(); } end()36 BlockConstIterator end() const { return blocks_.end(); } rbegin()37 BlockConstReverseIterator rbegin() const { return blocks_.rbegin(); } rend()38 BlockConstReverseIterator rend() const { return blocks_.rend(); } 39 last_block()40 BasicBlock* last_block() const { return blocks_.back(); } 41 Add(BasicBlock * block)42 void Add(BasicBlock* block) { blocks_.push_back(block); } 43 stack_slots()44 uint32_t stack_slots() const { return stack_slots_; } set_stack_slots(uint32_t stack_slots)45 void set_stack_slots(uint32_t stack_slots) { 46 DCHECK_EQ(kMaxUInt32, stack_slots_); 47 DCHECK_NE(kMaxUInt32, stack_slots); 48 stack_slots_ = stack_slots; 49 } 50 51 private: 52 uint32_t stack_slots_ = kMaxUInt32; 53 ZoneVector<BasicBlock*> blocks_; 54 }; 55 56 } // namespace maglev 57 } // namespace internal 58 } // namespace v8 59 60 #endif // V8_MAGLEV_MAGLEV_GRAPH_H_ 61