blob: efb23e7d3ed52719bb44fdcb92204a23725b515b [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 Serov121f2032017-10-23 19:19:06 +010087
Artem Serov0e329082018-06-12 10:23:27 +010088 bool IsLoopNonBeneficialForScalarOpts(LoopAnalysisInfo* analysis_info) const OVERRIDE {
89 return analysis_info->HasLongTypeInstructions() ||
90 IsLoopTooBig(analysis_info,
Artem Serovcf43fb62018-02-15 14:43:48 +000091 kScalarHeuristicMaxBodySizeInstr,
92 kScalarHeuristicMaxBodySizeBlocks);
Artem Serov121f2032017-10-23 19:19:06 +010093 }
94
Artem Serov0e329082018-06-12 10:23:27 +010095 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 Serovcf43fb62018-02-15 14:43:48 +0000101 uint32_t desired_unrolling_factor = kScalarMaxUnrollFactor;
Artem Serov121f2032017-10-23 19:19:06 +0100102 if (trip_count < desired_unrolling_factor || trip_count % desired_unrolling_factor != 0) {
Artem Serov0e329082018-06-12 10:23:27 +0100103 return LoopAnalysisInfo::kNoUnrollingFactor;
Artem Serov121f2032017-10-23 19:19:06 +0100104 }
105
106 return desired_unrolling_factor;
107 }
108
Artem Serov72411e62017-10-19 16:18:07 +0100109 bool IsLoopPeelingEnabled() const OVERRIDE { return true; }
110
Artem Serovcf43fb62018-02-15 14:43:48 +0000111 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.
123class 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 Serov121f2032017-10-23 19:19:06 +0100143 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 Serov0e329082018-06-12 10:23:27 +0100151 return LoopAnalysisInfo::kNoUnrollingFactor;
Artem Serov121f2032017-10-23 19:19:06 +0100152 }
153 // Don't unroll for large loop body size.
154 uint32_t instruction_count = block->GetInstructions().CountSize();
155 if (instruction_count >= kArm64SimdHeuristicMaxBodySizeInstr) {
Artem Serov0e329082018-06-12 10:23:27 +0100156 return LoopAnalysisInfo::kNoUnrollingFactor;
Artem Serov121f2032017-10-23 19:19:06 +0100157 }
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 Serovcf43fb62018-02-15 14:43:48 +0000171ArchNoOptsLoopHelper* ArchNoOptsLoopHelper::Create(InstructionSet isa,
172 ArenaAllocator* allocator) {
Artem Serov121f2032017-10-23 19:19:06 +0100173 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