1 /** 2 * Copyright (c) 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 COMPILER_OPTIMIZER_OPTIMIZATIONS_OPTIMIZE_STRING_CONCAT_H 17 #define COMPILER_OPTIMIZER_OPTIMIZATIONS_OPTIMIZE_STRING_CONCAT_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 26 namespace ark::compiler { 27 28 /// Optimize String.concat calls by replacing it with StringBuilder.append 29 30 class OptimizeStringConcat : public Optimization { 31 public: 32 explicit OptimizeStringConcat(Graph *graph); 33 34 NO_MOVE_SEMANTIC(OptimizeStringConcat); 35 NO_COPY_SEMANTIC(OptimizeStringConcat); 36 ~OptimizeStringConcat() override = default; 37 38 bool RunImpl() override; 39 IsEnable()40 bool IsEnable() const override 41 { 42 return g_options.IsCompilerOptimizeStringConcat(); 43 } 44 GetPassName()45 const char *GetPassName() const override 46 { 47 return "OptimizeStringConcat"; 48 } 49 void InvalidateAnalyses() override; 50 51 private: 52 void CreateAppendArgsIntrinsic(Inst *instance, Inst *arg, SaveStateInst *saveState); 53 void CreateAppendArgsIntrinsics(Inst *instance, SaveStateInst *saveState); 54 void CreateAppendArgsIntrinsics(Inst *instance, Inst *args, uint64_t arrayLengthValue, SaveStateInst *saveState); 55 BasicBlock *CreateAppendArgsLoop(Inst *instance, Inst *str, Inst *args, LengthMethodInst *arrayLength, 56 Inst *concatCall); 57 void FixBrokenSaveStates(Inst *source, Inst *target); 58 bool HasStoreArrayUsersOnly(Inst *newArray, Inst *removable); 59 void ReplaceStringConcatWithStringBuilderAppend(Inst *concatCall); 60 61 private: 62 SaveStateBridgesBuilder ssb_ {}; 63 InstVector arrayElements_; 64 }; 65 66 } // namespace ark::compiler 67 68 #endif // COMPILER_OPTIMIZER_OPTIMIZATIONS_OPTIMIZE_STRING_CONCAT_H 69