1 /*
2  * Copyright (c) 2023-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef PANDA_BYTECODE_OPT_CANONICALIZATION_H
17 #define PANDA_BYTECODE_OPT_CANONICALIZATION_H
18 
19 #include "bytecodeopt_options.h"
20 #include "compiler/optimizer/ir/graph.h"
21 #include "compiler/optimizer/pass.h"
22 #include "compiler/optimizer/ir/graph_visitor.h"
23 
24 namespace ark::bytecodeopt {
25 
26 using ark::compiler::BasicBlock;
27 using ark::compiler::Inst;
28 using ark::compiler::Opcode;
29 
30 // NOLINTNEXTLINE(fuchsia-multiple-inheritance)
31 class Canonicalization : public compiler::Optimization, public compiler::GraphVisitor {
32 public:
Canonicalization(compiler::Graph *graph)33     explicit Canonicalization(compiler::Graph *graph) : compiler::Optimization(graph) {}
34     ~Canonicalization() override = default;
35     NO_COPY_SEMANTIC(Canonicalization);
36     NO_MOVE_SEMANTIC(Canonicalization);
37 
38     bool RunImpl() override;
39     const char *GetPassName() const override
40     {
41         return "Canonicalization";
42     }
43     bool IsEnable() const override
44     {
45         return g_options.IsCanonicalization();
46     }
47 
GetStatus() const48     bool GetStatus() const
49     {
50         return result_;
51     }
52 
53     const ArenaVector<BasicBlock *> &GetBlocksToVisit() const override
54     {
55         return GetGraph()->GetBlocksRPO();
56     }
57 
58     void VisitCommutative(Inst *inst);
59     static void VisitCompare([[maybe_unused]] GraphVisitor *v, Inst *inst);
60 
61     static bool TrySwapReverseInput(Inst *inst);
62     static bool TrySwapConstantInput(Inst *inst);
63 #include "optimizer/ir/visitor.inc"
64 private:
65     bool result_ {false};
66 };
67 }  // namespace ark::bytecodeopt
68 
69 #endif  // PANDA_BYTECODE_OPT_CANONICALIZATION_H
70