summaryrefslogtreecommitdiff
path: root/compiler/optimizing/instruction_simplifier_riscv64.cc
blob: 0b9f8c58b13779ea9994dc692ee014667d02243a (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
92
93
94
95
96
97
98
99
/*
 * 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"

namespace art HIDDEN {

namespace riscv64 {

class InstructionSimplifierRiscv64Visitor final : public HGraphVisitor {
 public:
  InstructionSimplifierRiscv64Visitor(HGraph* graph, OptimizingCompilerStats* stats)
      : HGraphVisitor(graph), stats_(stats) {}

 private:
  void RecordSimplification() {
    MaybeRecordStat(stats_, MethodCompilationStat::kInstructionSimplificationsArch);
  }

  void VisitBasicBlock(HBasicBlock* block) override {
    for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
      HInstruction* instruction = it.Current();
      if (instruction->IsInBlock()) {
        instruction->Accept(this);
      }
    }
  }

  bool TryReplaceShiftAddWithOneInstruction(HShl* shl, HAdd* add) {
    // There is no reason to replace Int32 Shl+Add with ShiftAdd because of
    // additional sign-extension required.
    if (shl->GetType() != DataType::Type::kInt64) {
      return false;
    }

    if (!shl->GetRight()->IsIntConstant()) {
      return false;
    }

    const int32_t distance = shl->GetRight()->AsIntConstant()->GetValue();
    if (distance != 1 && distance != 2 && distance != 3) {
      return false;
    }

    if (!shl->HasOnlyOneNonEnvironmentUse()) {
      return false;
    }

    auto* const add_other_input = add->GetLeft() == shl ? add->GetRight() : add->GetLeft();
    auto* const shift_add = new (GetGraph()->GetAllocator())
        HRiscv64ShiftAdd(shl->GetLeft(), add_other_input, distance);

    DCHECK_EQ(add->GetType(), DataType::Type::kInt64)
        << "Riscv64ShiftAdd replacement should have the same 64 bit type";
    add->GetBlock()->ReplaceAndRemoveInstructionWith(add, shift_add);
    shl->GetBlock()->RemoveInstruction(shl);

    return true;
  }

  // Replace code looking like
  //    SHL tmp, a, 1 or 2 or 3
  //    ADD dst, tmp, b
  // with
  //    Riscv64ShiftAdd dst, a, b
  void VisitAdd(HAdd* add) override {
    auto* const left = add->GetLeft();
    auto* const right = add->GetRight();
    if (left->IsShl() && TryReplaceShiftAddWithOneInstruction(left->AsShl(), add)) {
      return;
    } else if (right->IsShl() && TryReplaceShiftAddWithOneInstruction(right->AsShl(), add)) {
      return;
    }
  }

  OptimizingCompilerStats* stats_ = nullptr;
};

bool InstructionSimplifierRiscv64::Run() {
  auto visitor = InstructionSimplifierRiscv64Visitor(graph_, stats_);
  visitor.VisitReversePostOrder();
  return true;
}

}  // namespace riscv64
}  // namespace art