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