Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef ART_COMPILER_OPTIMIZING_LOOP_OPTIMIZATION_H_ |
| 18 | #define ART_COMPILER_OPTIMIZING_LOOP_OPTIMIZATION_H_ |
| 19 | |
| 20 | #include <string> |
| 21 | |
| 22 | #include "induction_var_range.h" |
| 23 | #include "nodes.h" |
| 24 | #include "optimization.h" |
| 25 | |
| 26 | namespace art { |
| 27 | |
| 28 | /** |
| 29 | * Loop optimizations. Builds a loop hierarchy and applies optimizations to |
| 30 | * the detected nested loops, such as removal of dead induction and empty loops. |
| 31 | */ |
| 32 | class HLoopOptimization : public HOptimization { |
| 33 | public: |
| 34 | HLoopOptimization(HGraph* graph, HInductionVarAnalysis* induction_analysis); |
| 35 | |
| 36 | void Run() OVERRIDE; |
| 37 | |
| 38 | static constexpr const char* kLoopOptimizationPassName = "loop_optimization"; |
| 39 | |
| 40 | private: |
| 41 | /** |
| 42 | * A single loop inside the loop hierarchy representation. |
| 43 | */ |
| 44 | struct LoopNode : public ArenaObject<kArenaAllocInductionVarAnalysis> { |
| 45 | explicit LoopNode(HLoopInformation* lp_info) |
| 46 | : loop_info(lp_info), |
| 47 | outer(nullptr), |
| 48 | inner(nullptr), |
| 49 | previous(nullptr), |
| 50 | next(nullptr) {} |
| 51 | const HLoopInformation* const loop_info; |
| 52 | LoopNode* outer; |
| 53 | LoopNode* inner; |
| 54 | LoopNode* previous; |
| 55 | LoopNode* next; |
| 56 | }; |
| 57 | |
| 58 | void AddLoop(HLoopInformation* loop_info); |
| 59 | void RemoveLoop(LoopNode* node); |
| 60 | |
| 61 | void TraverseLoopsInnerToOuter(LoopNode* node); |
| 62 | |
| 63 | void SimplifyInduction(LoopNode* node); |
| 64 | void RemoveIfEmptyLoop(LoopNode* node); |
| 65 | |
| 66 | void ReplaceAllUses(HInstruction* instruction, |
| 67 | HInstruction* replacement, |
| 68 | HInstruction* exclusion); |
| 69 | |
| 70 | // Range analysis based on induction variables. |
| 71 | InductionVarRange induction_range_; |
| 72 | |
| 73 | // Phase-local heap memory allocator for the loop optimizer. Storage obtained |
| 74 | // through this allocator is released when the loop optimizer is done. |
| 75 | ArenaAllocator loop_allocator_; |
| 76 | |
| 77 | // Entries into the loop hierarchy representation. |
| 78 | LoopNode* top_loop_; |
| 79 | LoopNode* last_loop_; |
| 80 | |
| 81 | friend class LoopOptimizationTest; |
| 82 | |
| 83 | DISALLOW_COPY_AND_ASSIGN(HLoopOptimization); |
| 84 | }; |
| 85 | |
| 86 | } // namespace art |
| 87 | |
| 88 | #endif // ART_COMPILER_OPTIMIZING_LOOP_OPTIMIZATION_H_ |