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 | |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 20 | #include "induction_var_range.h" |
| 21 | #include "nodes.h" |
| 22 | #include "optimization.h" |
| 23 | |
| 24 | namespace art { |
| 25 | |
| 26 | /** |
| 27 | * Loop optimizations. Builds a loop hierarchy and applies optimizations to |
| 28 | * the detected nested loops, such as removal of dead induction and empty loops. |
| 29 | */ |
| 30 | class HLoopOptimization : public HOptimization { |
| 31 | public: |
| 32 | HLoopOptimization(HGraph* graph, HInductionVarAnalysis* induction_analysis); |
| 33 | |
| 34 | void Run() OVERRIDE; |
| 35 | |
| 36 | static constexpr const char* kLoopOptimizationPassName = "loop_optimization"; |
| 37 | |
| 38 | private: |
| 39 | /** |
| 40 | * A single loop inside the loop hierarchy representation. |
| 41 | */ |
Aart Bik | 9620230 | 2016-10-04 17:33:56 -0700 | [diff] [blame] | 42 | struct LoopNode : public ArenaObject<kArenaAllocLoopOptimization> { |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 43 | explicit LoopNode(HLoopInformation* lp_info) |
| 44 | : loop_info(lp_info), |
| 45 | outer(nullptr), |
| 46 | inner(nullptr), |
| 47 | previous(nullptr), |
| 48 | next(nullptr) {} |
Aart Bik | 482095d | 2016-10-10 15:39:10 -0700 | [diff] [blame] | 49 | HLoopInformation* const loop_info; |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 50 | LoopNode* outer; |
| 51 | LoopNode* inner; |
| 52 | LoopNode* previous; |
| 53 | LoopNode* next; |
| 54 | }; |
| 55 | |
Aart Bik | 9620230 | 2016-10-04 17:33:56 -0700 | [diff] [blame] | 56 | void LocalRun(); |
| 57 | |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 58 | void AddLoop(HLoopInformation* loop_info); |
| 59 | void RemoveLoop(LoopNode* node); |
| 60 | |
| 61 | void TraverseLoopsInnerToOuter(LoopNode* node); |
| 62 | |
| 63 | void SimplifyInduction(LoopNode* node); |
Aart Bik | 482095d | 2016-10-10 15:39:10 -0700 | [diff] [blame] | 64 | void SimplifyBlocks(LoopNode* node); |
Aart Bik | 9abf894 | 2016-10-14 09:49:42 -0700 | [diff] [blame] | 65 | void RemoveIfEmptyInnerLoop(LoopNode* node); |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 66 | |
Aart Bik | cc42be0 | 2016-10-20 16:14:16 -0700 | [diff] [blame] | 67 | bool IsPhiInduction(HPhi* phi); |
| 68 | bool IsEmptyHeader(HBasicBlock* block); |
| 69 | bool IsEmptyBody(HBasicBlock* block); |
| 70 | |
Aart Bik | 482095d | 2016-10-10 15:39:10 -0700 | [diff] [blame] | 71 | bool IsOnlyUsedAfterLoop(HLoopInformation* loop_info, |
Aart Bik | 8c4a854 | 2016-10-06 11:36:57 -0700 | [diff] [blame] | 72 | HInstruction* instruction, |
| 73 | /*out*/ int32_t* use_count); |
| 74 | void ReplaceAllUses(HInstruction* instruction, HInstruction* replacement); |
Aart Bik | 807868e | 2016-11-03 17:51:43 -0700 | [diff] [blame] | 75 | bool TryReplaceWithLastValue(HInstruction* instruction, HBasicBlock* block); |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 76 | |
Aart Bik | 9620230 | 2016-10-04 17:33:56 -0700 | [diff] [blame] | 77 | // Range information based on prior induction variable analysis. |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 78 | InductionVarRange induction_range_; |
| 79 | |
| 80 | // Phase-local heap memory allocator for the loop optimizer. Storage obtained |
Aart Bik | 9620230 | 2016-10-04 17:33:56 -0700 | [diff] [blame] | 81 | // through this allocator is immediately released when the loop optimizer is done. |
Nicolas Geoffray | ebe1674 | 2016-10-05 09:55:42 +0100 | [diff] [blame] | 82 | ArenaAllocator* loop_allocator_; |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 83 | |
Aart Bik | 9620230 | 2016-10-04 17:33:56 -0700 | [diff] [blame] | 84 | // Entries into the loop hierarchy representation. The hierarchy resides |
| 85 | // in phase-local heap memory. |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 86 | LoopNode* top_loop_; |
| 87 | LoopNode* last_loop_; |
| 88 | |
Aart Bik | 8c4a854 | 2016-10-06 11:36:57 -0700 | [diff] [blame] | 89 | // Temporary bookkeeping of a set of instructions. |
| 90 | // Contents reside in phase-local heap memory. |
| 91 | ArenaSet<HInstruction*>* iset_; |
| 92 | |
Aart Bik | 482095d | 2016-10-10 15:39:10 -0700 | [diff] [blame] | 93 | // Counter that tracks how many induction cycles have been simplified. Useful |
| 94 | // to trigger incremental updates of induction variable analysis of outer loops |
| 95 | // when the induction of inner loops has changed. |
| 96 | int32_t induction_simplication_count_; |
| 97 | |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 98 | // Flag that tracks if any simplifications have occurred. |
| 99 | bool simplified_; |
| 100 | |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 101 | friend class LoopOptimizationTest; |
| 102 | |
| 103 | DISALLOW_COPY_AND_ASSIGN(HLoopOptimization); |
| 104 | }; |
| 105 | |
| 106 | } // namespace art |
| 107 | |
| 108 | #endif // ART_COMPILER_OPTIMIZING_LOOP_OPTIMIZATION_H_ |