Artem Serov | 121f203 | 2017-10-23 19:19:06 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | #include "loop_analysis.h" |
| 18 | |
Artem Serov | 72411e6 | 2017-10-19 16:18:07 +0100 | [diff] [blame] | 19 | #include "base/bit_vector-inl.h" |
Artem Serov | 0e32908 | 2018-06-12 10:23:27 +0100 | [diff] [blame^] | 20 | #include "induction_var_range.h" |
Artem Serov | 72411e6 | 2017-10-19 16:18:07 +0100 | [diff] [blame] | 21 | |
Artem Serov | 121f203 | 2017-10-23 19:19:06 +0100 | [diff] [blame] | 22 | namespace art { |
| 23 | |
| 24 | void LoopAnalysis::CalculateLoopBasicProperties(HLoopInformation* loop_info, |
Artem Serov | 0e32908 | 2018-06-12 10:23:27 +0100 | [diff] [blame^] | 25 | LoopAnalysisInfo* analysis_results, |
| 26 | int64_t trip_count) { |
| 27 | analysis_results->trip_count_ = trip_count; |
| 28 | |
Artem Serov | 121f203 | 2017-10-23 19:19:06 +0100 | [diff] [blame] | 29 | for (HBlocksInLoopIterator block_it(*loop_info); |
| 30 | !block_it.Done(); |
| 31 | block_it.Advance()) { |
| 32 | HBasicBlock* block = block_it.Current(); |
| 33 | |
Artem Serov | 0e32908 | 2018-06-12 10:23:27 +0100 | [diff] [blame^] | 34 | // Check whether one of the successor is loop exit. |
Artem Serov | 121f203 | 2017-10-23 19:19:06 +0100 | [diff] [blame] | 35 | for (HBasicBlock* successor : block->GetSuccessors()) { |
| 36 | if (!loop_info->Contains(*successor)) { |
| 37 | analysis_results->exits_num_++; |
Artem Serov | 0e32908 | 2018-06-12 10:23:27 +0100 | [diff] [blame^] | 38 | |
| 39 | // We track number of invariant loop exits which correspond to HIf instruction and |
| 40 | // can be eliminated by loop peeling; other control flow instruction are ignored and will |
| 41 | // not cause loop peeling to happen as they either cannot be inside a loop, or by |
| 42 | // definition cannot be loop exits (unconditional instructions), or are not beneficial for |
| 43 | // the optimization. |
| 44 | HIf* hif = block->GetLastInstruction()->AsIf(); |
| 45 | if (hif != nullptr && !loop_info->Contains(*hif->InputAt(0)->GetBlock())) { |
| 46 | analysis_results->invariant_exits_num_++; |
| 47 | } |
Artem Serov | 121f203 | 2017-10-23 19:19:06 +0100 | [diff] [blame] | 48 | } |
| 49 | } |
| 50 | |
| 51 | for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) { |
| 52 | HInstruction* instruction = it.Current(); |
Artem Serov | cf43fb6 | 2018-02-15 14:43:48 +0000 | [diff] [blame] | 53 | if (it.Current()->GetType() == DataType::Type::kInt64) { |
| 54 | analysis_results->has_long_type_instructions_ = true; |
| 55 | } |
Artem Serov | 72411e6 | 2017-10-19 16:18:07 +0100 | [diff] [blame] | 56 | if (MakesScalarPeelingUnrollingNonBeneficial(instruction)) { |
| 57 | analysis_results->has_instructions_preventing_scalar_peeling_ = true; |
Artem Serov | 121f203 | 2017-10-23 19:19:06 +0100 | [diff] [blame] | 58 | analysis_results->has_instructions_preventing_scalar_unrolling_ = true; |
| 59 | } |
| 60 | analysis_results->instr_num_++; |
| 61 | } |
| 62 | analysis_results->bb_num_++; |
| 63 | } |
| 64 | } |
| 65 | |
Artem Serov | 0e32908 | 2018-06-12 10:23:27 +0100 | [diff] [blame^] | 66 | int64_t LoopAnalysis::GetLoopTripCount(HLoopInformation* loop_info, |
| 67 | const InductionVarRange* induction_range) { |
| 68 | int64_t trip_count; |
| 69 | if (!induction_range->HasKnownTripCount(loop_info, &trip_count)) { |
| 70 | trip_count = LoopAnalysisInfo::kUnknownTripCount; |
Artem Serov | 72411e6 | 2017-10-19 16:18:07 +0100 | [diff] [blame] | 71 | } |
Artem Serov | 0e32908 | 2018-06-12 10:23:27 +0100 | [diff] [blame^] | 72 | return trip_count; |
Artem Serov | 72411e6 | 2017-10-19 16:18:07 +0100 | [diff] [blame] | 73 | } |
| 74 | |
Artem Serov | cf43fb6 | 2018-02-15 14:43:48 +0000 | [diff] [blame] | 75 | // Default implementation of loop helper; used for all targets unless a custom implementation |
| 76 | // is provided. Enables scalar loop peeling and unrolling with the most conservative heuristics. |
| 77 | class ArchDefaultLoopHelper : public ArchNoOptsLoopHelper { |
Artem Serov | 121f203 | 2017-10-23 19:19:06 +0100 | [diff] [blame] | 78 | public: |
| 79 | // Scalar loop unrolling parameters and heuristics. |
| 80 | // |
| 81 | // Maximum possible unrolling factor. |
Artem Serov | cf43fb6 | 2018-02-15 14:43:48 +0000 | [diff] [blame] | 82 | static constexpr uint32_t kScalarMaxUnrollFactor = 2; |
Artem Serov | 121f203 | 2017-10-23 19:19:06 +0100 | [diff] [blame] | 83 | // Loop's maximum instruction count. Loops with higher count will not be peeled/unrolled. |
Artem Serov | cf43fb6 | 2018-02-15 14:43:48 +0000 | [diff] [blame] | 84 | static constexpr uint32_t kScalarHeuristicMaxBodySizeInstr = 17; |
Artem Serov | 121f203 | 2017-10-23 19:19:06 +0100 | [diff] [blame] | 85 | // Loop's maximum basic block count. Loops with higher count will not be peeled/unrolled. |
Artem Serov | cf43fb6 | 2018-02-15 14:43:48 +0000 | [diff] [blame] | 86 | static constexpr uint32_t kScalarHeuristicMaxBodySizeBlocks = 6; |
Artem Serov | 121f203 | 2017-10-23 19:19:06 +0100 | [diff] [blame] | 87 | |
Artem Serov | 0e32908 | 2018-06-12 10:23:27 +0100 | [diff] [blame^] | 88 | bool IsLoopNonBeneficialForScalarOpts(LoopAnalysisInfo* analysis_info) const OVERRIDE { |
| 89 | return analysis_info->HasLongTypeInstructions() || |
| 90 | IsLoopTooBig(analysis_info, |
Artem Serov | cf43fb6 | 2018-02-15 14:43:48 +0000 | [diff] [blame] | 91 | kScalarHeuristicMaxBodySizeInstr, |
| 92 | kScalarHeuristicMaxBodySizeBlocks); |
Artem Serov | 121f203 | 2017-10-23 19:19:06 +0100 | [diff] [blame] | 93 | } |
| 94 | |
Artem Serov | 0e32908 | 2018-06-12 10:23:27 +0100 | [diff] [blame^] | 95 | uint32_t GetScalarUnrollingFactor(const LoopAnalysisInfo* analysis_info) const OVERRIDE { |
| 96 | int64_t trip_count = analysis_info->GetTripCount(); |
| 97 | // Unroll only loops with known trip count. |
| 98 | if (trip_count == LoopAnalysisInfo::kUnknownTripCount) { |
| 99 | return LoopAnalysisInfo::kNoUnrollingFactor; |
| 100 | } |
Artem Serov | cf43fb6 | 2018-02-15 14:43:48 +0000 | [diff] [blame] | 101 | uint32_t desired_unrolling_factor = kScalarMaxUnrollFactor; |
Artem Serov | 121f203 | 2017-10-23 19:19:06 +0100 | [diff] [blame] | 102 | if (trip_count < desired_unrolling_factor || trip_count % desired_unrolling_factor != 0) { |
Artem Serov | 0e32908 | 2018-06-12 10:23:27 +0100 | [diff] [blame^] | 103 | return LoopAnalysisInfo::kNoUnrollingFactor; |
Artem Serov | 121f203 | 2017-10-23 19:19:06 +0100 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | return desired_unrolling_factor; |
| 107 | } |
| 108 | |
Artem Serov | 72411e6 | 2017-10-19 16:18:07 +0100 | [diff] [blame] | 109 | bool IsLoopPeelingEnabled() const OVERRIDE { return true; } |
| 110 | |
Artem Serov | cf43fb6 | 2018-02-15 14:43:48 +0000 | [diff] [blame] | 111 | protected: |
| 112 | bool IsLoopTooBig(LoopAnalysisInfo* loop_analysis_info, |
| 113 | size_t instr_threshold, |
| 114 | size_t bb_threshold) const { |
| 115 | size_t instr_num = loop_analysis_info->GetNumberOfInstructions(); |
| 116 | size_t bb_num = loop_analysis_info->GetNumberOfBasicBlocks(); |
| 117 | return (instr_num >= instr_threshold || bb_num >= bb_threshold); |
| 118 | } |
| 119 | }; |
| 120 | |
| 121 | // Custom implementation of loop helper for arm64 target. Enables heuristics for scalar loop |
| 122 | // peeling and unrolling and supports SIMD loop unrolling. |
| 123 | class Arm64LoopHelper : public ArchDefaultLoopHelper { |
| 124 | public: |
| 125 | // SIMD loop unrolling parameters and heuristics. |
| 126 | // |
| 127 | // Maximum possible unrolling factor. |
| 128 | static constexpr uint32_t kArm64SimdMaxUnrollFactor = 8; |
| 129 | // Loop's maximum instruction count. Loops with higher count will not be unrolled. |
| 130 | static constexpr uint32_t kArm64SimdHeuristicMaxBodySizeInstr = 50; |
| 131 | |
| 132 | // Loop's maximum instruction count. Loops with higher count will not be peeled/unrolled. |
| 133 | static constexpr uint32_t kArm64ScalarHeuristicMaxBodySizeInstr = 40; |
| 134 | // Loop's maximum basic block count. Loops with higher count will not be peeled/unrolled. |
| 135 | static constexpr uint32_t kArm64ScalarHeuristicMaxBodySizeBlocks = 8; |
| 136 | |
| 137 | bool IsLoopNonBeneficialForScalarOpts(LoopAnalysisInfo* loop_analysis_info) const OVERRIDE { |
| 138 | return IsLoopTooBig(loop_analysis_info, |
| 139 | kArm64ScalarHeuristicMaxBodySizeInstr, |
| 140 | kArm64ScalarHeuristicMaxBodySizeBlocks); |
| 141 | } |
| 142 | |
Artem Serov | 121f203 | 2017-10-23 19:19:06 +0100 | [diff] [blame] | 143 | uint32_t GetSIMDUnrollingFactor(HBasicBlock* block, |
| 144 | int64_t trip_count, |
| 145 | uint32_t max_peel, |
| 146 | uint32_t vector_length) const OVERRIDE { |
| 147 | // Don't unroll with insufficient iterations. |
| 148 | // TODO: Unroll loops with unknown trip count. |
| 149 | DCHECK_NE(vector_length, 0u); |
| 150 | if (trip_count < (2 * vector_length + max_peel)) { |
Artem Serov | 0e32908 | 2018-06-12 10:23:27 +0100 | [diff] [blame^] | 151 | return LoopAnalysisInfo::kNoUnrollingFactor; |
Artem Serov | 121f203 | 2017-10-23 19:19:06 +0100 | [diff] [blame] | 152 | } |
| 153 | // Don't unroll for large loop body size. |
| 154 | uint32_t instruction_count = block->GetInstructions().CountSize(); |
| 155 | if (instruction_count >= kArm64SimdHeuristicMaxBodySizeInstr) { |
Artem Serov | 0e32908 | 2018-06-12 10:23:27 +0100 | [diff] [blame^] | 156 | return LoopAnalysisInfo::kNoUnrollingFactor; |
Artem Serov | 121f203 | 2017-10-23 19:19:06 +0100 | [diff] [blame] | 157 | } |
| 158 | // Find a beneficial unroll factor with the following restrictions: |
| 159 | // - At least one iteration of the transformed loop should be executed. |
| 160 | // - The loop body shouldn't be "too big" (heuristic). |
| 161 | |
| 162 | uint32_t uf1 = kArm64SimdHeuristicMaxBodySizeInstr / instruction_count; |
| 163 | uint32_t uf2 = (trip_count - max_peel) / vector_length; |
| 164 | uint32_t unroll_factor = |
| 165 | TruncToPowerOfTwo(std::min({uf1, uf2, kArm64SimdMaxUnrollFactor})); |
| 166 | DCHECK_GE(unroll_factor, 1u); |
| 167 | return unroll_factor; |
| 168 | } |
| 169 | }; |
| 170 | |
Artem Serov | cf43fb6 | 2018-02-15 14:43:48 +0000 | [diff] [blame] | 171 | ArchNoOptsLoopHelper* ArchNoOptsLoopHelper::Create(InstructionSet isa, |
| 172 | ArenaAllocator* allocator) { |
Artem Serov | 121f203 | 2017-10-23 19:19:06 +0100 | [diff] [blame] | 173 | switch (isa) { |
| 174 | case InstructionSet::kArm64: { |
| 175 | return new (allocator) Arm64LoopHelper; |
| 176 | } |
| 177 | default: { |
| 178 | return new (allocator) ArchDefaultLoopHelper; |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | } // namespace art |