1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2024 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker *
4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker *
8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker *
10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker */
16*795d594fSAndroid Build Coastguard Worker
17*795d594fSAndroid Build Coastguard Worker #include "instruction_simplifier_riscv64.h"
18*795d594fSAndroid Build Coastguard Worker
19*795d594fSAndroid Build Coastguard Worker #include <gtest/gtest.h>
20*795d594fSAndroid Build Coastguard Worker
21*795d594fSAndroid Build Coastguard Worker #include "base/globals.h"
22*795d594fSAndroid Build Coastguard Worker #include "optimizing_unit_test.h"
23*795d594fSAndroid Build Coastguard Worker
24*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
25*795d594fSAndroid Build Coastguard Worker namespace riscv64 {
26*795d594fSAndroid Build Coastguard Worker
27*795d594fSAndroid Build Coastguard Worker class InstructionSimplifierRiscv64Test : public OptimizingUnitTest {};
28*795d594fSAndroid Build Coastguard Worker
TEST_F(InstructionSimplifierRiscv64Test,SimplifyShiftAdd)29*795d594fSAndroid Build Coastguard Worker TEST_F(InstructionSimplifierRiscv64Test, SimplifyShiftAdd) {
30*795d594fSAndroid Build Coastguard Worker HGraph* graph = CreateGraph();
31*795d594fSAndroid Build Coastguard Worker HBasicBlock* entry = AddNewBlock();
32*795d594fSAndroid Build Coastguard Worker graph->SetEntryBlock(entry);
33*795d594fSAndroid Build Coastguard Worker graph->BuildDominatorTree();
34*795d594fSAndroid Build Coastguard Worker
35*795d594fSAndroid Build Coastguard Worker HInstruction* param0 = MakeParam(DataType::Type::kInt64);
36*795d594fSAndroid Build Coastguard Worker HInstruction* param1 = MakeParam(DataType::Type::kInt64);
37*795d594fSAndroid Build Coastguard Worker HInstruction* c0 = graph->GetIntConstant(0);
38*795d594fSAndroid Build Coastguard Worker HInstruction* c1 = graph->GetIntConstant(1);
39*795d594fSAndroid Build Coastguard Worker HInstruction* c2 = graph->GetIntConstant(2);
40*795d594fSAndroid Build Coastguard Worker HInstruction* c3 = graph->GetIntConstant(3);
41*795d594fSAndroid Build Coastguard Worker HInstruction* c4 = graph->GetIntConstant(4);
42*795d594fSAndroid Build Coastguard Worker
43*795d594fSAndroid Build Coastguard Worker HInstruction* shl0 = MakeBinOp<HShl>(entry, DataType::Type::kInt64, param0, c0);
44*795d594fSAndroid Build Coastguard Worker HInstruction* add_shl0 = MakeBinOp<HAdd>(entry, DataType::Type::kInt64, param1, shl0);
45*795d594fSAndroid Build Coastguard Worker HInstruction* shl1 = MakeBinOp<HShl>(entry, DataType::Type::kInt64, param0, c1);
46*795d594fSAndroid Build Coastguard Worker HInstruction* add_shl1 = MakeBinOp<HAdd>(entry, DataType::Type::kInt64, param1, shl1);
47*795d594fSAndroid Build Coastguard Worker HInstruction* shl2 = MakeBinOp<HShl>(entry, DataType::Type::kInt64, param0, c2);
48*795d594fSAndroid Build Coastguard Worker HInstruction* add_shl2 = MakeBinOp<HAdd>(entry, DataType::Type::kInt64, param1, shl2);
49*795d594fSAndroid Build Coastguard Worker HInstruction* shl3 = MakeBinOp<HShl>(entry, DataType::Type::kInt64, param0, c3);
50*795d594fSAndroid Build Coastguard Worker HInstruction* add_shl3 = MakeBinOp<HAdd>(entry, DataType::Type::kInt64, param1, shl3);
51*795d594fSAndroid Build Coastguard Worker HInstruction* shl4 = MakeBinOp<HShl>(entry, DataType::Type::kInt64, param0, c4);
52*795d594fSAndroid Build Coastguard Worker HInstruction* add_shl4 = MakeBinOp<HAdd>(entry, DataType::Type::kInt64, param1, shl4);
53*795d594fSAndroid Build Coastguard Worker
54*795d594fSAndroid Build Coastguard Worker InstructionSimplifierRiscv64 simplifier(graph, /*stats=*/ nullptr);
55*795d594fSAndroid Build Coastguard Worker simplifier.Run();
56*795d594fSAndroid Build Coastguard Worker
57*795d594fSAndroid Build Coastguard Worker EXPECT_FALSE(add_shl0->GetBlock() == nullptr);
58*795d594fSAndroid Build Coastguard Worker EXPECT_TRUE(add_shl1->GetBlock() == nullptr);
59*795d594fSAndroid Build Coastguard Worker EXPECT_TRUE(add_shl2->GetBlock() == nullptr);
60*795d594fSAndroid Build Coastguard Worker EXPECT_TRUE(add_shl3->GetBlock() == nullptr);
61*795d594fSAndroid Build Coastguard Worker EXPECT_FALSE(add_shl4->GetBlock() == nullptr);
62*795d594fSAndroid Build Coastguard Worker }
63*795d594fSAndroid Build Coastguard Worker
TEST_F(InstructionSimplifierRiscv64Test,SimplifyShiftAddReusedShift)64*795d594fSAndroid Build Coastguard Worker TEST_F(InstructionSimplifierRiscv64Test, SimplifyShiftAddReusedShift) {
65*795d594fSAndroid Build Coastguard Worker HGraph* graph = CreateGraph();
66*795d594fSAndroid Build Coastguard Worker HBasicBlock* entry = AddNewBlock();
67*795d594fSAndroid Build Coastguard Worker graph->SetEntryBlock(entry);
68*795d594fSAndroid Build Coastguard Worker graph->BuildDominatorTree();
69*795d594fSAndroid Build Coastguard Worker
70*795d594fSAndroid Build Coastguard Worker HInstruction* param0 = MakeParam(DataType::Type::kInt64);
71*795d594fSAndroid Build Coastguard Worker HInstruction* param1 = MakeParam(DataType::Type::kInt64);
72*795d594fSAndroid Build Coastguard Worker HInstruction* param2 = MakeParam(DataType::Type::kInt64);
73*795d594fSAndroid Build Coastguard Worker HInstruction* param3 = MakeParam(DataType::Type::kInt64);
74*795d594fSAndroid Build Coastguard Worker HInstruction* c1 = graph->GetIntConstant(1);
75*795d594fSAndroid Build Coastguard Worker
76*795d594fSAndroid Build Coastguard Worker HInstruction* shl1 = MakeBinOp<HShl>(entry, DataType::Type::kInt64, param0, c1);
77*795d594fSAndroid Build Coastguard Worker HInstruction* add1 = MakeBinOp<HAdd>(entry, DataType::Type::kInt64, param1, shl1);
78*795d594fSAndroid Build Coastguard Worker HInstruction* add2 = MakeBinOp<HAdd>(entry, DataType::Type::kInt64, param2, shl1);
79*795d594fSAndroid Build Coastguard Worker HInstruction* add3 = MakeBinOp<HAdd>(entry, DataType::Type::kInt64, param3, shl1);
80*795d594fSAndroid Build Coastguard Worker
81*795d594fSAndroid Build Coastguard Worker InstructionSimplifierRiscv64 simplifier(graph, /*stats=*/ nullptr);
82*795d594fSAndroid Build Coastguard Worker simplifier.Run();
83*795d594fSAndroid Build Coastguard Worker
84*795d594fSAndroid Build Coastguard Worker EXPECT_TRUE(shl1->GetBlock() == nullptr);
85*795d594fSAndroid Build Coastguard Worker EXPECT_TRUE(add1->GetBlock() == nullptr);
86*795d594fSAndroid Build Coastguard Worker EXPECT_TRUE(add2->GetBlock() == nullptr);
87*795d594fSAndroid Build Coastguard Worker EXPECT_TRUE(add3->GetBlock() == nullptr);
88*795d594fSAndroid Build Coastguard Worker }
89*795d594fSAndroid Build Coastguard Worker
90*795d594fSAndroid Build Coastguard Worker } // namespace riscv64
91*795d594fSAndroid Build Coastguard Worker } // namespace art
92