• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2025 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 COMPILER_OPTIMIZER_OPTIMIZATIONS_LOWER_BOXED_BOOLEAN_H
17 #define COMPILER_OPTIMIZER_OPTIMIZATIONS_LOWER_BOXED_BOOLEAN_H
18 
19 #include "optimizer/analysis/loop_analyzer.h"
20 #include "optimizer/ir/analysis.h"
21 #include "optimizer/ir/basicblock.h"
22 #include "optimizer/ir/graph.h"
23 #include "optimizer/ir/inst.h"
24 #include "optimizer/pass.h"
25 #include "optimizer/ir/graph_visitor.h"
26 
27 namespace ark::compiler {
28 
29 /*
30  * This pass lowers boxed Boolean values into primitive boolean operations.
31  * It targets LoadObject instructions that access the 'value' field of std.core.Boolean and replaces them
32  * with primitive values (0 or 1) when the input is known at compile time.
33  */
34 
35 // NOLINTNEXTLINE(fuchsia-multiple-inheritance)
36 class LowerBoxedBoolean : public Optimization, public GraphVisitor {
37 public:
LowerBoxedBoolean(Graph * graph)38     explicit LowerBoxedBoolean(Graph *graph)
39         : Optimization(graph), instReplacements_(graph->GetLocalAllocator()->Adapter())
40     {
41     }
42 
43     NO_MOVE_SEMANTIC(LowerBoxedBoolean);
44     NO_COPY_SEMANTIC(LowerBoxedBoolean);
45     ~LowerBoxedBoolean() override = default;
46 
47     bool RunImpl() override;
48 
GetPassName()49     const char *GetPassName() const override
50     {
51         return "LowerBoxedBoolean";
52     }
53 
GetBlocksToVisit()54     const ArenaVector<BasicBlock *> &GetBlocksToVisit() const override
55     {
56         return GetGraph()->GetBlocksRPO();
57     }
58 
IsEnable()59     bool IsEnable() const override
60     {
61         return g_options.IsCompilerLowerBoxedBoolean();
62     }
63 
64     static void VisitCompare(GraphVisitor *v, Inst *inst);
65     static void VisitLoadObject(GraphVisitor *v, Inst *inst);
66 
67 #include "optimizer/ir/visitor.inc"
68 
69 private:
70     static void ProcessInput(GraphVisitor *v, Inst *inst);
71     static void ProcessLoadStatic(GraphVisitor *v, Inst *inst);
72     static void ProcessPhi(GraphVisitor *v, Inst *inst);
73     static bool IsValidLoadObjectInput(Inst *input);
74     static bool IsCompareWithNullPtr(Inst *inst);
75     static bool HasOnlyKnownUsers(Inst *inst);
76     static bool ProcessSaveState(Inst *saveState, Inst *inst);
77     static bool CheckSaveStateUsers(Inst *saveStateInst, Inst *inst);
78     static bool IsNullCheckUsingInput(Inst *inst, Inst *input);
79     static std::optional<uint32_t> GetBooleanFieldValue(Inst *inst);
80 
81     void SetInstReplacement(Inst *oldInst, Inst *newInst);
82     Inst *GetReplacement(Inst *inst);
83     bool HasReplacement(Inst *inst) const;
84     void SetVisited(Inst *inst);
85     bool IsVisited(Inst *inst) const;
86     void SetApplied();
87 
88     bool isApplied_ {false};
89     Marker visitedMarker_ {UNDEF_MARKER};
90     ArenaUnorderedMap<Inst *, Inst *> instReplacements_;
91 };
92 
93 }  // namespace ark::compiler
94 
95 #endif  // COMPILER_OPTIMIZER_OPTIMIZATIONS_LOWER_BOXED_BOOLEAN_H
96