1 /*
2 * Copyright (c) 2021-2023 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_IR_ANALYSIS_H
17 #define COMPILER_OPTIMIZER_IR_ANALYSIS_H
18
19 #include "graph.h"
20
21 #include <optional>
22
23 namespace panda::compiler {
24
25 /// The file contains small analysis functions which can be used in different passes
26 class Inst;
27 // returns Store value, for StoreArrayPair and StoreArrayPairI saved not last store value in second_value
28 Inst *InstStoredValue(Inst *inst, Inst **secondValue);
29 Inst *InstStoredValue(Inst *inst);
30 bool HasOsrEntryBetween(Inst *dominateInst, Inst *inst);
31 bool HasOsrEntryBetween(BasicBlock *dominateBb, BasicBlock *bb);
32 bool HasTryBlockBetween(Inst *dominateInst, Inst *inst);
33 bool IsSuitableForImplicitNullCheck(const Inst *inst);
34 bool IsInstNotNull(const Inst *inst);
35 SaveStateInst *CopySaveState(Graph *graph, SaveStateInst *inst);
36 bool CheckObjectRec(Inst *object, const Inst *user, const BasicBlock *block, Inst *startFrom, Marker visited,
37 Inst **failedSs = nullptr);
38 std::optional<bool> IsIfInverted(BasicBlock *phiBlock, IfImmInst *ifImm);
39
40 // If object input has known class, return pointer to the class, else returns nullptr
41 RuntimeInterface::ClassPtr GetClassPtrForObject(Inst *inst, size_t inputNum = 0);
42
IsInstInDifferentBlocks(Inst * i1,Inst * i2)43 inline bool IsInstInDifferentBlocks(Inst *i1, Inst *i2)
44 {
45 return i1->GetBasicBlock() != i2->GetBasicBlock();
46 }
47
48 // This function bypass all blocks and delete 'SaveStateOSR' if the block is no longer the header of the loop
49 void CleanupGraphSaveStateOSR(Graph *graph);
50
51 class IsSaveState;
52 class IsSaveStateCanTriggerGc;
53 // returns true is there is SaveState/SafePoint between instructions
54 template <typename T = IsSaveState>
55 bool HasSaveStateBetween(Inst *domInst, Inst *inst);
56
57 /**
58 * Functions below are using for create bridge in SaveStates between source instruction and target instruction.
59 * It use in GVN etc. It inserts `source` instruction into `SaveStates` on each path between `source` and
60 * `target` instructions to save the object in case GC is triggered on this path.
61 * Instructions on how to use it: compiler/docs/bridges.md
62 */
63 class SaveStateBridgesBuilder {
64 public:
65 ArenaVector<Inst *> *SearchMissingObjInSaveStates(Graph *graph, Inst *source, Inst *target,
66 Inst *stopSearch = nullptr, BasicBlock *targetBlock = nullptr);
67 void CreateBridgeInSS(Inst *source, ArenaVector<Inst *> *bridges);
68 void SearchAndCreateMissingObjInSaveState(Graph *graph, Inst *source, Inst *target, Inst *stopSearch = nullptr,
69 BasicBlock *targetBlock = nullptr);
70 void FixInstUsageInSS(Graph *graph, Inst *inst);
71 void FixSaveStatesInBB(BasicBlock *block);
72 void FixPhisWithCheckInputs(BasicBlock *block);
73 void DumpBridges(std::ostream &out, Inst *source, ArenaVector<Inst *> *bridges);
74
75 private:
76 void SearchSSOnWay(BasicBlock *block, Inst *startFrom, Inst *sourceInst, Marker visited,
77 ArenaVector<Inst *> *bridges, Inst *stopSearch);
78 bool IsSaveStateForGc(Inst *inst);
79 void ProcessSSUserPreds(Graph *graph, Inst *inst, Inst *targetInst);
80 void SearchInSaveStateAndFillBridgeVector(Inst *inst, Inst *searchedInst, ArenaVector<Inst *> *bridges);
81 void FixUsageInstInOtherBB(BasicBlock *block, Inst *inst);
82 void FixUsagePhiInBB(BasicBlock *block, Inst *inst);
83 void DeleteUnrealObjInSaveState(Inst *ss);
84 /**
85 * Pointer to moved out to class for reduce memory usage in each pair of equal instructions.
86 * When using functions, it looks like we work as if every time get a new vector,
87 * but one vector is always used and cleaned before use.
88 */
89 ArenaVector<Inst *> *bridges_ {nullptr};
90 };
91
92 class InstAppender {
93 public:
block_(block)94 explicit InstAppender(BasicBlock *block, Inst *insertAfter = nullptr) : block_(block), prev_(insertAfter) {}
95 ~InstAppender() = default;
96 DEFAULT_MOVE_SEMANTIC(InstAppender);
97 NO_COPY_SEMANTIC(InstAppender);
98
99 void Append(Inst *inst);
100 void Append(std::initializer_list<Inst *> instructions);
101
102 private:
103 BasicBlock *block_;
104 Inst *prev_ {nullptr};
105 };
106
107 bool StoreValueCanBeObject(Inst *inst);
108
109 bool IsConditionEqual(const Inst *inst0, const Inst *inst1, bool inverted);
110
111 } // namespace panda::compiler
112
113 #endif // COMPILER_OPTIMIZER_IR_ANALYSIS_H
114