diff options
author | 2024-08-09 08:48:07 +0000 | |
---|---|---|
committer | 2024-08-12 14:02:13 +0000 | |
commit | 936ada167f4e2a51b583aaa245dd3f88b4880eea (patch) | |
tree | 3e74a3dec855f5d5506176940df5473533f62979 | |
parent | 4018a7d772fc09c7955eb4c88eea788be5cc2143 (diff) |
riscv64: Fix Shl+Add simplification.
Test: m test-art-host-gtest
Change-Id: I62803340fb308d24120436bca2005eb75d2ccc85
-rw-r--r-- | compiler/Android.bp | 1 | ||||
-rw-r--r-- | compiler/optimizing/instruction_simplifier_riscv64.cc | 2 | ||||
-rw-r--r-- | compiler/optimizing/instruction_simplifier_riscv64_test.cc | 91 |
3 files changed, 93 insertions, 1 deletions
diff --git a/compiler/Android.bp b/compiler/Android.bp index 0bfc8fa7b2..05a599cf4a 100644 --- a/compiler/Android.bp +++ b/compiler/Android.bp @@ -511,6 +511,7 @@ art_cc_defaults { }, riscv64: { srcs: [ + "optimizing/instruction_simplifier_riscv64_test.cc", "utils/riscv64/managed_register_riscv64_test.cc", ], }, diff --git a/compiler/optimizing/instruction_simplifier_riscv64.cc b/compiler/optimizing/instruction_simplifier_riscv64.cc index b12941b37d..6dc269f8c5 100644 --- a/compiler/optimizing/instruction_simplifier_riscv64.cc +++ b/compiler/optimizing/instruction_simplifier_riscv64.cc @@ -57,7 +57,7 @@ class InstructionSimplifierRiscv64Visitor final : public HGraphVisitor { DCHECK(shl->GetRight()->IsIntConstant()); const int32_t distance = shl->GetRight()->AsIntConstant()->GetValue(); - if ((distance & ~0x3) != 0) { + if (distance < 1 || distance > 3) { return false; } diff --git a/compiler/optimizing/instruction_simplifier_riscv64_test.cc b/compiler/optimizing/instruction_simplifier_riscv64_test.cc new file mode 100644 index 0000000000..15c81c8957 --- /dev/null +++ b/compiler/optimizing/instruction_simplifier_riscv64_test.cc @@ -0,0 +1,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 |