• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_CHECK_RESOLVER_H
17 #define PANDA_CHECK_RESOLVER_H
18 
19 #include "compiler/optimizer/ir/graph.h"
20 #include "compiler/optimizer/ir/inst.h"
21 #include "compiler/optimizer/pass.h"
22 #include "utils/arena_containers.h"
23 
24 /*
25  * Check Resolver.
26  *
27  * Check Resolver is a bytecodeopt-specific pass. In bytecode optimizer, we do
28  * not need all check-instructions, such as BoundsCheck, NullCheck, NegativeCheck
29  * and ZeroCheck, because the throws of these checks will be embedded in their
30  * underlying instructions during runtime.
31  * For the sake of saving ROM size, we can delete these check-instructions. Besides,
32  * when bytecode optimizer optimizes the code in which there exist operations on the
33  * element of an array, in the generated code the redundant asm lenarr will be generated
34  * with ldarr and starr. Here lenarr is generated from IR LenArray and LenArray is an
35  * input of BoundsCheck. CodeGen will encode LenArray but ignore BoundsCheck. That is
36  * why dead lenarr remains. So we can also benefit from the size of generated bytecode
37  * by deleting check-instructions.
38  * However, these LenArray that are generated from asm lenarr should be keeped, as they
39  * may throw in the original code logic. For LoadArray, Div, Mod, LoadStatic and LoadObject,
40  * since they can throw but they are not generated as inputs of check-instructions, We
41  * should keep all such insts.
42  *
43  * For every check-instruction, we replace the corresponding input of its users by the data
44  * flow input. Then we clear its NO_DCE flag such that it can be removed by DCE pass. We set
45  * the NO_DCE flag for the insts that should be keeped.
46  */
47 namespace ark::bytecodeopt {
48 
49 class PANDA_PUBLIC_API CheckResolver : public compiler::Optimization {
50 public:
CheckResolver(compiler::Graph * graph)51     explicit CheckResolver(compiler::Graph *graph) : compiler::Optimization(graph) {}
52     ~CheckResolver() override = default;
53     NO_COPY_SEMANTIC(CheckResolver);
54     NO_MOVE_SEMANTIC(CheckResolver);
55 
56     bool RunImpl() override;
57     static bool IsCheck(const compiler::Inst *inst);
58     static bool NeedSetNoDCE(const compiler::Inst *inst);
GetPassName()59     const char *GetPassName() const override
60     {
61         return "CheckResolver";
62     }
63 };
64 
65 }  // namespace ark::bytecodeopt
66 
67 #endif  // PANDA_CHECK_RESOLVER_H
68