1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef ECMASCRIPT_COMPILER_VALUE_NUMBERING_H 17 #define ECMASCRIPT_COMPILER_VALUE_NUMBERING_H 18 19 #include "ecmascript/compiler/circuit_builder.h" 20 #include "ecmascript/compiler/gate_accessor.h" 21 #include "ecmascript/compiler/graph_visitor.h" 22 #include "ecmascript/mem/chunk_containers.h" 23 24 namespace panda::ecmascript::kungfu { 25 class ValueNumbering : public GraphVisitor { 26 public: ValueNumbering(Circuit * circuit,bool enableLog,const std::string & name,Chunk * chunk)27 ValueNumbering(Circuit *circuit, bool enableLog, const std::string& name, Chunk* chunk) 28 : GraphVisitor(circuit, chunk), enableLog_(enableLog), 29 methodName_(name), entries_(chunk) {} 30 31 ~ValueNumbering() = default; 32 33 void Run(); 34 35 GateRef VisitGate(GateRef gate) override; 36 bool CheckReplacement(GateRef lhs, GateRef rhs); 37 private: IsLogEnabled()38 bool IsLogEnabled() const 39 { 40 return enableLog_; 41 } 42 GetMethodName()43 const std::string& GetMethodName() const 44 { 45 return methodName_; 46 } 47 48 size_t HashCode(GateRef gate); GetEntry(size_t hash)49 GateRef GetEntry(size_t hash) 50 { 51 ASSERT(hash < entries_.size()); 52 return entries_[hash]; 53 } SetEntry(size_t hash,GateRef gate)54 void SetEntry(size_t hash, GateRef gate) 55 { 56 ASSERT(hash < entries_.size()); 57 entries_[hash] = gate; 58 } 59 static const uint32_t CACHE_LENGTH_BIT = 8; 60 static const uint32_t CACHE_LENGTH = (1U << CACHE_LENGTH_BIT); 61 62 bool enableLog_ {false}; 63 std::string methodName_; 64 ChunkVector<GateRef> entries_; 65 }; 66 } // panda::ecmascript::kungfu 67 #endif // ECMASCRIPT_COMPILER_VALUE_NUMBERING_H