summaryrefslogtreecommitdiff
path: root/compiler/optimizing/instruction_simplifier_riscv64_test.cc
blob: 15c81c8957836afd410623f036e23656a495b613 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "instruction_simplifier_riscv64.h"

#include <gtest/gtest.h>

#include "base/globals.h"
#include "optimizing_unit_test.h"

namespace art HIDDEN {
namespace riscv64 {

class InstructionSimplifierRiscv64Test : public OptimizingUnitTest {};

TEST_F(InstructionSimplifierRiscv64Test, SimplifyShiftAdd) {
  HGraph* graph = CreateGraph();
  HBasicBlock* entry = AddNewBlock();
  graph->SetEntryBlock(entry);
  graph->BuildDominatorTree();

  HInstruction* param0 = MakeParam(DataType::Type::kInt64);
  HInstruction* param1 = MakeParam(DataType::Type::kInt64);
  HInstruction* c0 = graph->GetIntConstant(0);
  HInstruction* c1 = graph->GetIntConstant(1);
  HInstruction* c2 = graph->GetIntConstant(2);
  HInstruction* c3 = graph->GetIntConstant(3);
  HInstruction* c4 = graph->GetIntConstant(4);

  HInstruction* shl0 = MakeBinOp<HShl>(entry, DataType::Type::kInt64, param0, c0);
  HInstruction* add_shl0 = MakeBinOp<HAdd>(entry, DataType::Type::kInt64, param1, shl0);
  HInstruction* shl1 = MakeBinOp<HShl>(entry, DataType::Type::kInt64, param0, c1);
  HInstruction* add_shl1 = MakeBinOp<HAdd>(entry, DataType::Type::kInt64, param1, shl1);
  HInstruction* shl2 = MakeBinOp<HShl>(entry, DataType::Type::kInt64, param0, c2);
  HInstruction* add_shl2 = MakeBinOp<HAdd>(entry, DataType::Type::kInt64, param1, shl2);
  HInstruction* shl3 = MakeBinOp<HShl>(entry, DataType::Type::kInt64, param0, c3);
  HInstruction* add_shl3 = MakeBinOp<HAdd>(entry, DataType::Type::kInt64, param1, shl3);
  HInstruction* shl4 = MakeBinOp<HShl>(entry, DataType::Type::kInt64, param0, c4);
  HInstruction* add_shl4 = MakeBinOp<HAdd>(entry, DataType::Type::kInt64, param1, shl4);

  InstructionSimplifierRiscv64 simplifier(graph, /*stats=*/ nullptr);
  simplifier.Run();

  EXPECT_FALSE(add_shl0->GetBlock() == nullptr);
  EXPECT_TRUE(add_shl1->GetBlock() == nullptr);
  EXPECT_TRUE(add_shl2->GetBlock() == nullptr);
  EXPECT_TRUE(add_shl3->GetBlock() == nullptr);
  EXPECT_FALSE(add_shl4->GetBlock() == nullptr);
}

TEST_F(InstructionSimplifierRiscv64Test, SimplifyShiftAddReusedShift) {
  HGraph* graph = CreateGraph();
  HBasicBlock* entry = AddNewBlock();
  graph->SetEntryBlock(entry);
  graph->BuildDominatorTree();

  HInstruction* param0 = MakeParam(DataType::Type::kInt64);
  HInstruction* param1 = MakeParam(DataType::Type::kInt64);
  HInstruction* param2 = MakeParam(DataType::Type::kInt64);
  HInstruction* param3 = MakeParam(DataType::Type::kInt64);
  HInstruction* c1 = graph->GetIntConstant(1);

  HInstruction* shl1 = MakeBinOp<HShl>(entry, DataType::Type::kInt64, param0, c1);
  HInstruction* add1 = MakeBinOp<HAdd>(entry, DataType::Type::kInt64, param1, shl1);
  HInstruction* add2 = MakeBinOp<HAdd>(entry, DataType::Type::kInt64, param2, shl1);
  HInstruction* add3 = MakeBinOp<HAdd>(entry, DataType::Type::kInt64, param3, shl1);

  InstructionSimplifierRiscv64 simplifier(graph, /*stats=*/ nullptr);
  simplifier.Run();

  EXPECT_TRUE(shl1->GetBlock() == nullptr);
  EXPECT_TRUE(add1->GetBlock() == nullptr);
  EXPECT_TRUE(add2->GetBlock() == nullptr);
  EXPECT_TRUE(add3->GetBlock() == nullptr);
}

}  // namespace riscv64
}  // namespace art