blob: d355cedb35ec54145c77bbb8f7d8fa6b6fd86b60 [file] [log] [blame]
Artem Serov121f2032017-10-23 19:19:06 +01001/*
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 Serov72411e62017-10-19 16:18:07 +010019#include "base/bit_vector-inl.h"
Artem Serov0e329082018-06-12 10:23:27 +010020#include "induction_var_range.h"
Artem Serov72411e62017-10-19 16:18:07 +010021
Artem Serov121f2032017-10-23 19:19:06 +010022namespace art {
23
24void LoopAnalysis::CalculateLoopBasicProperties(HLoopInformation* loop_info,
Artem Serov0e329082018-06-12 10:23:27 +010025 LoopAnalysisInfo* analysis_results,
26 int64_t trip_count) {
27 analysis_results->trip_count_ = trip_count;
28
Artem Serov121f2032017-10-23 19:19:06 +010029 for (HBlocksInLoopIterator block_it(*loop_info);
30 !block_it.Done();
31 block_it.Advance()) {
32 HBasicBlock* block = block_it.Current();
33
Artem Serov0e329082018-06-12 10:23:27 +010034 // Check whether one of the successor is loop exit.
Artem Serov121f2032017-10-23 19:19:06 +010035 for (HBasicBlock* successor : block->GetSuccessors()) {
36 if (!loop_info->Contains(*successor)) {
37 analysis_results->exits_num_++;
Artem Serov0e329082018-06-12 10:23:27 +010038
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 Serov121f2032017-10-23 19:19:06 +010048 }
49 }
50
51 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
52 HInstruction* instruction = it.Current();
Artem Serovcf43fb62018-02-15 14:43:48 +000053 if (it.Current()->GetType() == DataType::Type::kInt64) {
54 analysis_results->has_long_type_instructions_ = true;
55 }
Artem Serov72411e62017-10-19 16:18:07 +010056 if (MakesScalarPeelingUnrollingNonBeneficial(instruction)) {
57 analysis_results->has_instructions_preventing_scalar_peeling_ = true;
Artem Serov121f2032017-10-23 19:19:06 +010058 analysis_results->has_instructions_preventing_scalar_unrolling_ = true;
59 }
60 analysis_results->instr_num_++;
61 }
62 analysis_results->bb_num_++;
63 }
64}
65
Artem Serov0e329082018-06-12 10:23:27 +010066int64_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 Serov72411e62017-10-19 16:18:07 +010071 }
Artem Serov0e329082018-06-12 10:23:27 +010072 return trip_count;
Artem Serov72411e62017-10-19 16:18:07 +010073}
74
Artem Serovcf43fb62018-02-15 14:43:48 +000075// 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.
77class ArchDefaultLoopHelper : public ArchNoOptsLoopHelper {
Artem Serov121f2032017-10-23 19:19:06 +010078 public:
79 // Scalar loop unrolling parameters and heuristics.
80 //
81 // Maximum possible unrolling factor.
Artem Serovcf43fb62018-02-15 14:43:48 +000082 static constexpr uint32_t kScalarMaxUnrollFactor = 2;
Artem Serov121f2032017-10-23 19:19:06 +010083 // Loop's maximum instruction count. Loops with higher count will not be peeled/unrolled.
Artem Serovcf43fb62018-02-15 14:43:48 +000084 static constexpr uint32_t kScalarHeuristicMaxBodySizeInstr = 17;
Artem Serov121f2032017-10-23 19:19:06 +010085 // Loop's maximum basic block count. Loops with higher count will not be peeled/unrolled.
Artem Serovcf43fb62018-02-15 14:43:48 +000086 static constexpr uint32_t kScalarHeuristicMaxBodySizeBlocks = 6;
Artem Serov18ba1da2018-05-16 19:06:32 +010087 // Maximum number of instructions to be created as a result of full unrolling.
88 static constexpr uint32_t kScalarHeuristicFullyUnrolledMaxInstrThreshold = 35;
Artem Serov121f2032017-10-23 19:19:06 +010089
Artem Serov0e329082018-06-12 10:23:27 +010090 bool IsLoopNonBeneficialForScalarOpts(LoopAnalysisInfo* analysis_info) const OVERRIDE {
91 return analysis_info->HasLongTypeInstructions() ||
92 IsLoopTooBig(analysis_info,
Artem Serovcf43fb62018-02-15 14:43:48 +000093 kScalarHeuristicMaxBodySizeInstr,
94 kScalarHeuristicMaxBodySizeBlocks);
Artem Serov121f2032017-10-23 19:19:06 +010095 }
96
Artem Serov0e329082018-06-12 10:23:27 +010097 uint32_t GetScalarUnrollingFactor(const LoopAnalysisInfo* analysis_info) const OVERRIDE {
98 int64_t trip_count = analysis_info->GetTripCount();
99 // Unroll only loops with known trip count.
100 if (trip_count == LoopAnalysisInfo::kUnknownTripCount) {
101 return LoopAnalysisInfo::kNoUnrollingFactor;
102 }
Artem Serovcf43fb62018-02-15 14:43:48 +0000103 uint32_t desired_unrolling_factor = kScalarMaxUnrollFactor;
Artem Serov121f2032017-10-23 19:19:06 +0100104 if (trip_count < desired_unrolling_factor || trip_count % desired_unrolling_factor != 0) {
Artem Serov0e329082018-06-12 10:23:27 +0100105 return LoopAnalysisInfo::kNoUnrollingFactor;
Artem Serov121f2032017-10-23 19:19:06 +0100106 }
107
108 return desired_unrolling_factor;
109 }
110
Artem Serov72411e62017-10-19 16:18:07 +0100111 bool IsLoopPeelingEnabled() const OVERRIDE { return true; }
112
Artem Serov18ba1da2018-05-16 19:06:32 +0100113 bool IsFullUnrollingBeneficial(LoopAnalysisInfo* analysis_info) const OVERRIDE {
114 int64_t trip_count = analysis_info->GetTripCount();
115 // We assume that trip count is known.
116 DCHECK_NE(trip_count, LoopAnalysisInfo::kUnknownTripCount);
117 size_t instr_num = analysis_info->GetNumberOfInstructions();
118 return (trip_count * instr_num < kScalarHeuristicFullyUnrolledMaxInstrThreshold);
119 }
120
Artem Serovcf43fb62018-02-15 14:43:48 +0000121 protected:
122 bool IsLoopTooBig(LoopAnalysisInfo* loop_analysis_info,
123 size_t instr_threshold,
124 size_t bb_threshold) const {
125 size_t instr_num = loop_analysis_info->GetNumberOfInstructions();
126 size_t bb_num = loop_analysis_info->GetNumberOfBasicBlocks();
127 return (instr_num >= instr_threshold || bb_num >= bb_threshold);
128 }
129};
130
131// Custom implementation of loop helper for arm64 target. Enables heuristics for scalar loop
132// peeling and unrolling and supports SIMD loop unrolling.
133class Arm64LoopHelper : public ArchDefaultLoopHelper {
134 public:
135 // SIMD loop unrolling parameters and heuristics.
136 //
137 // Maximum possible unrolling factor.
138 static constexpr uint32_t kArm64SimdMaxUnrollFactor = 8;
139 // Loop's maximum instruction count. Loops with higher count will not be unrolled.
140 static constexpr uint32_t kArm64SimdHeuristicMaxBodySizeInstr = 50;
141
142 // Loop's maximum instruction count. Loops with higher count will not be peeled/unrolled.
143 static constexpr uint32_t kArm64ScalarHeuristicMaxBodySizeInstr = 40;
144 // Loop's maximum basic block count. Loops with higher count will not be peeled/unrolled.
145 static constexpr uint32_t kArm64ScalarHeuristicMaxBodySizeBlocks = 8;
146
147 bool IsLoopNonBeneficialForScalarOpts(LoopAnalysisInfo* loop_analysis_info) const OVERRIDE {
148 return IsLoopTooBig(loop_analysis_info,
149 kArm64ScalarHeuristicMaxBodySizeInstr,
150 kArm64ScalarHeuristicMaxBodySizeBlocks);
151 }
152
Artem Serov121f2032017-10-23 19:19:06 +0100153 uint32_t GetSIMDUnrollingFactor(HBasicBlock* block,
154 int64_t trip_count,
155 uint32_t max_peel,
156 uint32_t vector_length) const OVERRIDE {
157 // Don't unroll with insufficient iterations.
158 // TODO: Unroll loops with unknown trip count.
159 DCHECK_NE(vector_length, 0u);
160 if (trip_count < (2 * vector_length + max_peel)) {
Artem Serov0e329082018-06-12 10:23:27 +0100161 return LoopAnalysisInfo::kNoUnrollingFactor;
Artem Serov121f2032017-10-23 19:19:06 +0100162 }
163 // Don't unroll for large loop body size.
164 uint32_t instruction_count = block->GetInstructions().CountSize();
165 if (instruction_count >= kArm64SimdHeuristicMaxBodySizeInstr) {
Artem Serov0e329082018-06-12 10:23:27 +0100166 return LoopAnalysisInfo::kNoUnrollingFactor;
Artem Serov121f2032017-10-23 19:19:06 +0100167 }
168 // Find a beneficial unroll factor with the following restrictions:
169 // - At least one iteration of the transformed loop should be executed.
170 // - The loop body shouldn't be "too big" (heuristic).
171
172 uint32_t uf1 = kArm64SimdHeuristicMaxBodySizeInstr / instruction_count;
173 uint32_t uf2 = (trip_count - max_peel) / vector_length;
174 uint32_t unroll_factor =
175 TruncToPowerOfTwo(std::min({uf1, uf2, kArm64SimdMaxUnrollFactor}));
176 DCHECK_GE(unroll_factor, 1u);
177 return unroll_factor;
178 }
179};
180
Artem Serovcf43fb62018-02-15 14:43:48 +0000181ArchNoOptsLoopHelper* ArchNoOptsLoopHelper::Create(InstructionSet isa,
182 ArenaAllocator* allocator) {
Artem Serov121f2032017-10-23 19:19:06 +0100183 switch (isa) {
184 case InstructionSet::kArm64: {
185 return new (allocator) Arm64LoopHelper;
186 }
187 default: {
188 return new (allocator) ArchDefaultLoopHelper;
189 }
190 }
191}
192
193} // namespace art