14514f5e3Sopenharmony_ci/* 24514f5e3Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License. 54514f5e3Sopenharmony_ci * You may obtain a copy of the License at 64514f5e3Sopenharmony_ci * 74514f5e3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 84514f5e3Sopenharmony_ci * 94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and 134514f5e3Sopenharmony_ci * limitations under the License. 144514f5e3Sopenharmony_ci */ 154514f5e3Sopenharmony_ci 164514f5e3Sopenharmony_ci#include "ecmascript/compiler/combined_pass_visitor.h" 174514f5e3Sopenharmony_ci#include "ecmascript/compiler/dead_code_elimination.h" 184514f5e3Sopenharmony_ci#include "ecmascript/compiler/verifier.h" 194514f5e3Sopenharmony_ci#include "ecmascript/tests/test_helper.h" 204514f5e3Sopenharmony_ci 214514f5e3Sopenharmony_cinamespace panda::test { 224514f5e3Sopenharmony_ciclass DeadCodeEliminationTests : public testing::Test { 234514f5e3Sopenharmony_ci}; 244514f5e3Sopenharmony_ciusing ecmascript::kungfu::Chunk; 254514f5e3Sopenharmony_ciusing ecmascript::kungfu::CombinedPassVisitor; 264514f5e3Sopenharmony_ciusing ecmascript::kungfu::Circuit; 274514f5e3Sopenharmony_ciusing ecmascript::kungfu::CircuitBuilder; 284514f5e3Sopenharmony_ciusing ecmascript::kungfu::DeadCodeElimination; 294514f5e3Sopenharmony_ciusing ecmascript::kungfu::Environment; 304514f5e3Sopenharmony_ciusing ecmascript::kungfu::OpCode; 314514f5e3Sopenharmony_ciusing ecmascript::kungfu::GateType; 324514f5e3Sopenharmony_ciusing ecmascript::kungfu::MachineType; 334514f5e3Sopenharmony_ciusing ecmascript::kungfu::GateAccessor; 344514f5e3Sopenharmony_ciusing ecmascript::kungfu::GateRef; 354514f5e3Sopenharmony_ciusing ecmascript::kungfu::Verifier; 364514f5e3Sopenharmony_ciusing ecmascript::NativeAreaAllocator; 374514f5e3Sopenharmony_ci 384514f5e3Sopenharmony_ciHWTEST_F_L0(DeadCodeEliminationTests, CommonEliminationFramework) 394514f5e3Sopenharmony_ci{ 404514f5e3Sopenharmony_ci // construct a circuit 414514f5e3Sopenharmony_ci ecmascript::NativeAreaAllocator allocator; 424514f5e3Sopenharmony_ci Circuit circuit(&allocator); 434514f5e3Sopenharmony_ci CircuitBuilder builder(&circuit); 444514f5e3Sopenharmony_ci GateAccessor acc(&circuit); 454514f5e3Sopenharmony_ci auto depend = acc.GetDependRoot(); 464514f5e3Sopenharmony_ci auto arg0 = builder.Arguments(0); 474514f5e3Sopenharmony_ci auto dead = circuit.DeadGate(); 484514f5e3Sopenharmony_ci auto ifBranch = circuit.NewGate(circuit.IfBranch(0), {dead, arg0}); 494514f5e3Sopenharmony_ci auto ifTrue = circuit.NewGate(circuit.IfTrue(), {ifBranch}); 504514f5e3Sopenharmony_ci auto ifFalse = circuit.NewGate(circuit.IfFalse(), {ifBranch}); 514514f5e3Sopenharmony_ci auto trueReturn = circuit.NewGate(circuit.Return(), {ifTrue, depend, arg0, circuit.GetReturnRoot()}); 524514f5e3Sopenharmony_ci auto falseReturn = circuit.NewGate(circuit.Return(), {ifFalse, depend, arg0, circuit.GetReturnRoot()}); 534514f5e3Sopenharmony_ci Chunk chunk(&allocator); 544514f5e3Sopenharmony_ci CombinedPassVisitor visitor(&circuit, false, "dead code elimination", &chunk); 554514f5e3Sopenharmony_ci DeadCodeElimination elimination(&circuit, &visitor, &chunk); 564514f5e3Sopenharmony_ci visitor.AddPass(&elimination); 574514f5e3Sopenharmony_ci visitor.VisitGraph(); 584514f5e3Sopenharmony_ci EXPECT_TRUE(acc.IsNop(ifBranch)); 594514f5e3Sopenharmony_ci EXPECT_TRUE(acc.IsNop(ifTrue)); 604514f5e3Sopenharmony_ci EXPECT_TRUE(acc.IsNop(ifFalse)); 614514f5e3Sopenharmony_ci EXPECT_TRUE(acc.IsNop(trueReturn)); 624514f5e3Sopenharmony_ci EXPECT_TRUE(acc.IsNop(falseReturn)); 634514f5e3Sopenharmony_ci} 644514f5e3Sopenharmony_ci 654514f5e3Sopenharmony_ciHWTEST_F_L0(DeadCodeEliminationTests, MergeEliminationFramework) 664514f5e3Sopenharmony_ci{ 674514f5e3Sopenharmony_ci // construct a circuit 684514f5e3Sopenharmony_ci ecmascript::NativeAreaAllocator allocator; 694514f5e3Sopenharmony_ci Circuit circuit(&allocator); 704514f5e3Sopenharmony_ci CircuitBuilder builder(&circuit); 714514f5e3Sopenharmony_ci Environment env(0, &builder); 724514f5e3Sopenharmony_ci builder.SetEnvironment(&env); 734514f5e3Sopenharmony_ci GateAccessor acc(&circuit); 744514f5e3Sopenharmony_ci auto entry = acc.GetStateRoot(); 754514f5e3Sopenharmony_ci auto depend = acc.GetDependRoot(); 764514f5e3Sopenharmony_ci auto arg0 = builder.Arguments(0); 774514f5e3Sopenharmony_ci auto arg1 = builder.Arguments(1); 784514f5e3Sopenharmony_ci auto arg2 = builder.Arguments(2); 794514f5e3Sopenharmony_ci auto dead = circuit.DeadGate(); 804514f5e3Sopenharmony_ci auto icmp = circuit.NewGate(circuit.Icmp(1), MachineType::I1, {arg0, arg1}, GateType::NJSValue()); 814514f5e3Sopenharmony_ci auto ifBranch = circuit.NewGate(circuit.IfBranch(0), {entry, icmp}); 824514f5e3Sopenharmony_ci circuit.NewGate(circuit.IfTrue(), {ifBranch}); 834514f5e3Sopenharmony_ci auto ifFalse = circuit.NewGate(circuit.IfFalse(), {ifBranch}); 844514f5e3Sopenharmony_ci auto merge = circuit.NewGate(circuit.Merge(2), {dead, ifFalse}); 854514f5e3Sopenharmony_ci auto valueSelector = circuit.NewGate(circuit.ValueSelector(2), {merge, arg1, arg2}); 864514f5e3Sopenharmony_ci auto circuitReturn = circuit.NewGate(circuit.Return(), {merge, depend, valueSelector, circuit.GetReturnRoot()}); 874514f5e3Sopenharmony_ci Chunk chunk(&allocator); 884514f5e3Sopenharmony_ci CombinedPassVisitor visitor(&circuit, false, "dead code elimination", &chunk); 894514f5e3Sopenharmony_ci DeadCodeElimination elimination(&circuit, &visitor, &chunk); 904514f5e3Sopenharmony_ci visitor.AddPass(&elimination); 914514f5e3Sopenharmony_ci visitor.VisitGraph(); 924514f5e3Sopenharmony_ci EXPECT_TRUE(acc.IsNop(merge)); 934514f5e3Sopenharmony_ci EXPECT_TRUE(acc.IsNop(valueSelector)); 944514f5e3Sopenharmony_ci EXPECT_TRUE(acc.GetValueIn(circuitReturn) == arg2); 954514f5e3Sopenharmony_ci} 964514f5e3Sopenharmony_ci 974514f5e3Sopenharmony_ciHWTEST_F_L0(DeadCodeEliminationTests, MergeInputEliminationFramework) 984514f5e3Sopenharmony_ci{ 994514f5e3Sopenharmony_ci // construct a circuit 1004514f5e3Sopenharmony_ci ecmascript::NativeAreaAllocator allocator; 1014514f5e3Sopenharmony_ci Circuit circuit(&allocator); 1024514f5e3Sopenharmony_ci CircuitBuilder builder(&circuit); 1034514f5e3Sopenharmony_ci Environment env(0, &builder); 1044514f5e3Sopenharmony_ci builder.SetEnvironment(&env); 1054514f5e3Sopenharmony_ci GateAccessor acc(&circuit); 1064514f5e3Sopenharmony_ci auto entry = acc.GetStateRoot(); 1074514f5e3Sopenharmony_ci auto depend = acc.GetDependRoot(); 1084514f5e3Sopenharmony_ci auto arg0 = builder.Arguments(0); 1094514f5e3Sopenharmony_ci auto arg1 = builder.Arguments(1); 1104514f5e3Sopenharmony_ci auto arg2 = builder.Arguments(2); 1114514f5e3Sopenharmony_ci auto dead = circuit.DeadGate(); 1124514f5e3Sopenharmony_ci auto icmp = circuit.NewGate(circuit.Icmp(1), MachineType::I1, {arg0, arg1}, GateType::NJSValue()); 1134514f5e3Sopenharmony_ci auto ifBranch = circuit.NewGate(circuit.IfBranch(0), {entry, icmp}); 1144514f5e3Sopenharmony_ci auto ifTrue = circuit.NewGate(circuit.IfTrue(), {ifBranch}); 1154514f5e3Sopenharmony_ci auto ifFalse = circuit.NewGate(circuit.IfFalse(), {ifBranch}); 1164514f5e3Sopenharmony_ci auto ifBranch2 = circuit.NewGate(circuit.IfBranch(0), {ifTrue, icmp}); 1174514f5e3Sopenharmony_ci auto ifTrue2 = circuit.NewGate(circuit.IfTrue(), {ifBranch2}); 1184514f5e3Sopenharmony_ci circuit.NewGate(circuit.IfFalse(), {ifBranch2}); 1194514f5e3Sopenharmony_ci auto merge = circuit.NewGate(circuit.Merge(3), {ifTrue2, dead, ifFalse}); 1204514f5e3Sopenharmony_ci auto valueSelector = circuit.NewGate(circuit.ValueSelector(3), {merge, arg0, arg1, arg2}); 1214514f5e3Sopenharmony_ci auto circuitReturn = circuit.NewGate(circuit.Return(), {merge, depend, valueSelector, circuit.GetReturnRoot()}); 1224514f5e3Sopenharmony_ci Chunk chunk(&allocator); 1234514f5e3Sopenharmony_ci CombinedPassVisitor visitor(&circuit, false, "dead code elimination", &chunk); 1244514f5e3Sopenharmony_ci DeadCodeElimination elimination(&circuit, &visitor, &chunk); 1254514f5e3Sopenharmony_ci visitor.AddPass(&elimination); 1264514f5e3Sopenharmony_ci visitor.VisitGraph(); 1274514f5e3Sopenharmony_ci EXPECT_TRUE(acc.GetOpCode(merge) == OpCode::MERGE); 1284514f5e3Sopenharmony_ci EXPECT_TRUE(acc.GetOpCode(valueSelector) == OpCode::VALUE_SELECTOR); 1294514f5e3Sopenharmony_ci EXPECT_TRUE(acc.GetValueIn(circuitReturn) == valueSelector); 1304514f5e3Sopenharmony_ci EXPECT_TRUE(acc.GetNumIns(merge) == 2); 1314514f5e3Sopenharmony_ci EXPECT_TRUE(acc.GetNumIns(valueSelector) == 3); 1324514f5e3Sopenharmony_ci EXPECT_TRUE(acc.GetIn(merge, 0) == ifTrue2); 1334514f5e3Sopenharmony_ci EXPECT_TRUE(acc.GetIn(merge, 1) == ifFalse); 1344514f5e3Sopenharmony_ci EXPECT_TRUE(acc.GetIn(valueSelector, 1) == arg0); 1354514f5e3Sopenharmony_ci EXPECT_TRUE(acc.GetIn(valueSelector, 2) == arg2); 1364514f5e3Sopenharmony_ci} 1374514f5e3Sopenharmony_ci 1384514f5e3Sopenharmony_ci}