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 "instruction_simplifier.h"
20*795d594fSAndroid Build Coastguard Worker
21*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
22*795d594fSAndroid Build Coastguard Worker
23*795d594fSAndroid Build Coastguard Worker namespace riscv64 {
24*795d594fSAndroid Build Coastguard Worker
25*795d594fSAndroid Build Coastguard Worker class InstructionSimplifierRiscv64Visitor final : public HGraphVisitor {
26*795d594fSAndroid Build Coastguard Worker public:
InstructionSimplifierRiscv64Visitor(HGraph * graph,OptimizingCompilerStats * stats)27*795d594fSAndroid Build Coastguard Worker InstructionSimplifierRiscv64Visitor(HGraph* graph, OptimizingCompilerStats* stats)
28*795d594fSAndroid Build Coastguard Worker : HGraphVisitor(graph), stats_(stats) {}
29*795d594fSAndroid Build Coastguard Worker
30*795d594fSAndroid Build Coastguard Worker private:
RecordSimplification()31*795d594fSAndroid Build Coastguard Worker void RecordSimplification() {
32*795d594fSAndroid Build Coastguard Worker MaybeRecordStat(stats_, MethodCompilationStat::kInstructionSimplificationsArch);
33*795d594fSAndroid Build Coastguard Worker }
34*795d594fSAndroid Build Coastguard Worker
VisitBasicBlock(HBasicBlock * block)35*795d594fSAndroid Build Coastguard Worker void VisitBasicBlock(HBasicBlock* block) override {
36*795d594fSAndroid Build Coastguard Worker for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
37*795d594fSAndroid Build Coastguard Worker HInstruction* instruction = it.Current();
38*795d594fSAndroid Build Coastguard Worker if (instruction->IsInBlock()) {
39*795d594fSAndroid Build Coastguard Worker instruction->Accept(this);
40*795d594fSAndroid Build Coastguard Worker }
41*795d594fSAndroid Build Coastguard Worker }
42*795d594fSAndroid Build Coastguard Worker }
43*795d594fSAndroid Build Coastguard Worker
44*795d594fSAndroid Build Coastguard Worker // Replace Add which has Shl with distance of 1 or 2 or 3 with Riscv64ShiftAdd
TryReplaceAddsWithShiftAdds(HShl * shl)45*795d594fSAndroid Build Coastguard Worker bool TryReplaceAddsWithShiftAdds(HShl* shl) {
46*795d594fSAndroid Build Coastguard Worker // There is no reason to replace Int32 Shl+Add with ShiftAdd because of
47*795d594fSAndroid Build Coastguard Worker // additional sign-extension required.
48*795d594fSAndroid Build Coastguard Worker if (shl->GetType() != DataType::Type::kInt64) {
49*795d594fSAndroid Build Coastguard Worker return false;
50*795d594fSAndroid Build Coastguard Worker }
51*795d594fSAndroid Build Coastguard Worker
52*795d594fSAndroid Build Coastguard Worker if (!shl->GetRight()->IsConstant()) {
53*795d594fSAndroid Build Coastguard Worker return false;
54*795d594fSAndroid Build Coastguard Worker }
55*795d594fSAndroid Build Coastguard Worker
56*795d594fSAndroid Build Coastguard Worker // The bytecode does not permit the shift distance to come from a wide variable
57*795d594fSAndroid Build Coastguard Worker DCHECK(shl->GetRight()->IsIntConstant());
58*795d594fSAndroid Build Coastguard Worker
59*795d594fSAndroid Build Coastguard Worker const int32_t distance = shl->GetRight()->AsIntConstant()->GetValue();
60*795d594fSAndroid Build Coastguard Worker if (distance < 1 || distance > 3) {
61*795d594fSAndroid Build Coastguard Worker return false;
62*795d594fSAndroid Build Coastguard Worker }
63*795d594fSAndroid Build Coastguard Worker
64*795d594fSAndroid Build Coastguard Worker bool replaced = false;
65*795d594fSAndroid Build Coastguard Worker
66*795d594fSAndroid Build Coastguard Worker for (const HUseListNode<HInstruction*>& use : shl->GetUses()) {
67*795d594fSAndroid Build Coastguard Worker HInstruction* user = use.GetUser();
68*795d594fSAndroid Build Coastguard Worker
69*795d594fSAndroid Build Coastguard Worker if (!user->IsAdd()) {
70*795d594fSAndroid Build Coastguard Worker continue;
71*795d594fSAndroid Build Coastguard Worker }
72*795d594fSAndroid Build Coastguard Worker HAdd* add = user->AsAdd();
73*795d594fSAndroid Build Coastguard Worker HInstruction* left = add->GetLeft();
74*795d594fSAndroid Build Coastguard Worker HInstruction* right = add->GetRight();
75*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(add->GetType(), DataType::Type::kInt64)
76*795d594fSAndroid Build Coastguard Worker << "Replaceable Add must be the same 64 bit type as the input";
77*795d594fSAndroid Build Coastguard Worker
78*795d594fSAndroid Build Coastguard Worker // If the HAdd to replace has both inputs the same HShl<1|2|3>, then
79*795d594fSAndroid Build Coastguard Worker // don't perform the optimization. The processor will not be able to execute
80*795d594fSAndroid Build Coastguard Worker // these shifts parallel which is the purpose of the replace below.
81*795d594fSAndroid Build Coastguard Worker if (left == right) {
82*795d594fSAndroid Build Coastguard Worker continue;
83*795d594fSAndroid Build Coastguard Worker }
84*795d594fSAndroid Build Coastguard Worker
85*795d594fSAndroid Build Coastguard Worker HInstruction* add_other_input = left == shl ? right : left;
86*795d594fSAndroid Build Coastguard Worker HRiscv64ShiftAdd* shift_add = new (GetGraph()->GetAllocator())
87*795d594fSAndroid Build Coastguard Worker HRiscv64ShiftAdd(shl->GetLeft(), add_other_input, distance);
88*795d594fSAndroid Build Coastguard Worker
89*795d594fSAndroid Build Coastguard Worker add->GetBlock()->ReplaceAndRemoveInstructionWith(add, shift_add);
90*795d594fSAndroid Build Coastguard Worker replaced = true;
91*795d594fSAndroid Build Coastguard Worker }
92*795d594fSAndroid Build Coastguard Worker
93*795d594fSAndroid Build Coastguard Worker if (!shl->HasUses()) {
94*795d594fSAndroid Build Coastguard Worker shl->GetBlock()->RemoveInstruction(shl);
95*795d594fSAndroid Build Coastguard Worker }
96*795d594fSAndroid Build Coastguard Worker
97*795d594fSAndroid Build Coastguard Worker return replaced;
98*795d594fSAndroid Build Coastguard Worker }
99*795d594fSAndroid Build Coastguard Worker
VisitAnd(HAnd * inst)100*795d594fSAndroid Build Coastguard Worker void VisitAnd(HAnd* inst) override {
101*795d594fSAndroid Build Coastguard Worker if (TryMergeNegatedInput(inst)) {
102*795d594fSAndroid Build Coastguard Worker RecordSimplification();
103*795d594fSAndroid Build Coastguard Worker }
104*795d594fSAndroid Build Coastguard Worker }
105*795d594fSAndroid Build Coastguard Worker
VisitOr(HOr * inst)106*795d594fSAndroid Build Coastguard Worker void VisitOr(HOr* inst) override {
107*795d594fSAndroid Build Coastguard Worker if (TryMergeNegatedInput(inst)) {
108*795d594fSAndroid Build Coastguard Worker RecordSimplification();
109*795d594fSAndroid Build Coastguard Worker }
110*795d594fSAndroid Build Coastguard Worker }
111*795d594fSAndroid Build Coastguard Worker
112*795d594fSAndroid Build Coastguard Worker // Replace code looking like
113*795d594fSAndroid Build Coastguard Worker // SHL tmp, a, 1 or 2 or 3
114*795d594fSAndroid Build Coastguard Worker // ADD dst, tmp, b
115*795d594fSAndroid Build Coastguard Worker // with
116*795d594fSAndroid Build Coastguard Worker // Riscv64ShiftAdd dst, a, b
VisitShl(HShl * inst)117*795d594fSAndroid Build Coastguard Worker void VisitShl(HShl* inst) override {
118*795d594fSAndroid Build Coastguard Worker if (TryReplaceAddsWithShiftAdds(inst)) {
119*795d594fSAndroid Build Coastguard Worker RecordSimplification();
120*795d594fSAndroid Build Coastguard Worker }
121*795d594fSAndroid Build Coastguard Worker }
122*795d594fSAndroid Build Coastguard Worker
VisitSub(HSub * inst)123*795d594fSAndroid Build Coastguard Worker void VisitSub(HSub* inst) override {
124*795d594fSAndroid Build Coastguard Worker if (TryMergeWithAnd(inst)) {
125*795d594fSAndroid Build Coastguard Worker RecordSimplification();
126*795d594fSAndroid Build Coastguard Worker }
127*795d594fSAndroid Build Coastguard Worker }
128*795d594fSAndroid Build Coastguard Worker
VisitXor(HXor * inst)129*795d594fSAndroid Build Coastguard Worker void VisitXor(HXor* inst) override {
130*795d594fSAndroid Build Coastguard Worker if (TryMergeNegatedInput(inst)) {
131*795d594fSAndroid Build Coastguard Worker RecordSimplification();
132*795d594fSAndroid Build Coastguard Worker }
133*795d594fSAndroid Build Coastguard Worker }
134*795d594fSAndroid Build Coastguard Worker
135*795d594fSAndroid Build Coastguard Worker OptimizingCompilerStats* stats_ = nullptr;
136*795d594fSAndroid Build Coastguard Worker };
137*795d594fSAndroid Build Coastguard Worker
Run()138*795d594fSAndroid Build Coastguard Worker bool InstructionSimplifierRiscv64::Run() {
139*795d594fSAndroid Build Coastguard Worker auto visitor = InstructionSimplifierRiscv64Visitor(graph_, stats_);
140*795d594fSAndroid Build Coastguard Worker visitor.VisitReversePostOrder();
141*795d594fSAndroid Build Coastguard Worker return true;
142*795d594fSAndroid Build Coastguard Worker }
143*795d594fSAndroid Build Coastguard Worker
144*795d594fSAndroid Build Coastguard Worker } // namespace riscv64
145*795d594fSAndroid Build Coastguard Worker } // namespace art
146