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#include "ecmascript/compiler/bytecodes.h"
16#include "ecmascript/compiler/circuit_builder.h"
17#include "ecmascript/compiler/constant_folding.h"
18#include "ecmascript/compiler/gate_accessor.h"
19#include "ecmascript/compiler/graph_editor.h"
20#include "ecmascript/compiler/pass.h"
21#include "ecmascript/compiler/share_opcodes.h"
22#include "ecmascript/compiler/stub_builder.h"
23#include "ecmascript/compiler/type.h"
24#include "ecmascript/compiler/variable_type.h"
25#include "ecmascript/compiler/verifier.h"
26#include "ecmascript/compiler/typed_bytecode_lowering.h"
27#include "ecmascript/compiler/typed_hcr_lowering.h"
28#include "ecmascript/mem/chunk.h"
29#include "ecmascript/mem/native_area_allocator.h"
30#include "ecmascript/tests/test_helper.h"
31#include "gtest/gtest-death-test.h"
32#include "gtest/gtest.h"
33
34namespace panda::test {
35class ConstantFoldingTest : public testing::Test {
36};
37using ecmascript::kungfu::Circuit;
38using ecmascript::kungfu::GateAccessor;
39using ecmascript::kungfu::GateType;
40using ecmascript::kungfu::MachineType;
41using ecmascript::kungfu::CircuitBuilder;
42using ecmascript::kungfu::Label;
43using ecmascript::kungfu::OpCode;
44using ecmascript::kungfu::GateRef;
45using ecmascript::kungfu::Variable;
46using ecmascript::kungfu::VariableType;
47using ecmascript::kungfu::Verifier;
48using ecmascript::kungfu::Environment;
49using ecmascript::kungfu::LoopPeeling;
50using ecmascript::kungfu::CombinedPassVisitor;
51using ecmascript::kungfu::TypedBinOp;
52using ecmascript::kungfu::PGOSampleType;
53using ecmascript::kungfu::GraphLinearizer;
54using ecmascript::kungfu::ConstantFolding;
55using ecmascript::kungfu::CompilationConfig;
56
57HWTEST_F_L0(ConstantFoldingTest, ConstantFoldingTypedBinOpTest)
58{
59    // construct a circuit
60    ecmascript::NativeAreaAllocator allocator;
61    Circuit circuit(&allocator);
62    ecmascript::Chunk chunk(&allocator);
63    GateAccessor acc(&circuit);
64    CircuitBuilder builder(&circuit);
65    Environment env(0, &builder);
66
67    builder.SetEnvironment(&env);
68
69    DEFVALUE(number1, (&builder), VariableType::INT32(), builder.Int32(14));
70    DEFVALUE(number2, (&builder), VariableType::INT32(), builder.Int32(7));
71
72    auto sum = builder.Int32Add(*number1, *number2);
73    auto convert = builder.ConvertInt32ToTaggedInt(sum);
74    builder.Return(convert);
75    CombinedPassVisitor constantFoldingVisitor(&circuit, false, "ConstantFoldingTypedBinOpTest", &chunk);
76    CompilationConfig cmpCfg = CompilationConfig("x86_64-unknown-linux-gnu");
77    ConstantFolding constantFolding(&circuit, &constantFoldingVisitor, &cmpCfg, false,
78                                    "ConstantFoldingTypedBinOpTest", &chunk);
79    constantFoldingVisitor.AddPass(&constantFolding);
80    constantFoldingVisitor.VisitGraph();
81
82    EXPECT_EQ(acc.GetOpCode(acc.GetValueIn(convert, 0)), OpCode::CONSTANT);
83    EXPECT_TRUE(acc.GetInt32FromConstant(acc.GetValueIn(convert, 0)) == 21);
84}
85} // namespace panda::test