blob: a2124455e2807bed3e3dc38ac522fd57d82637c0 [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"
20
Artem Serov121f2032017-10-23 19:19:06 +010021namespace art {
22
23void LoopAnalysis::CalculateLoopBasicProperties(HLoopInformation* loop_info,
24 LoopAnalysisInfo* analysis_results) {
25 for (HBlocksInLoopIterator block_it(*loop_info);
26 !block_it.Done();
27 block_it.Advance()) {
28 HBasicBlock* block = block_it.Current();
29
30 for (HBasicBlock* successor : block->GetSuccessors()) {
31 if (!loop_info->Contains(*successor)) {
32 analysis_results->exits_num_++;
33 }
34 }
35
36 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
37 HInstruction* instruction = it.Current();
Artem Serovcf43fb62018-02-15 14:43:48 +000038 if (it.Current()->GetType() == DataType::Type::kInt64) {
39 analysis_results->has_long_type_instructions_ = true;
40 }
Artem Serov72411e62017-10-19 16:18:07 +010041 if (MakesScalarPeelingUnrollingNonBeneficial(instruction)) {
42 analysis_results->has_instructions_preventing_scalar_peeling_ = true;
Artem Serov121f2032017-10-23 19:19:06 +010043 analysis_results->has_instructions_preventing_scalar_unrolling_ = true;
44 }
45 analysis_results->instr_num_++;
46 }
47 analysis_results->bb_num_++;
48 }
49}
50
Artem Serov72411e62017-10-19 16:18:07 +010051bool LoopAnalysis::HasLoopAtLeastOneInvariantExit(HLoopInformation* loop_info) {
52 HGraph* graph = loop_info->GetHeader()->GetGraph();
53 for (uint32_t block_id : loop_info->GetBlocks().Indexes()) {
54 HBasicBlock* block = graph->GetBlocks()[block_id];
55 DCHECK(block != nullptr);
56 if (block->EndsWithIf()) {
57 HIf* hif = block->GetLastInstruction()->AsIf();
58 HInstruction* input = hif->InputAt(0);
59 if (IsLoopExit(loop_info, hif) && !loop_info->Contains(*input->GetBlock())) {
60 return true;
61 }
62 }
63 }
64 return false;
65}
66
Artem Serovcf43fb62018-02-15 14:43:48 +000067// Default implementation of loop helper; used for all targets unless a custom implementation
68// is provided. Enables scalar loop peeling and unrolling with the most conservative heuristics.
69class ArchDefaultLoopHelper : public ArchNoOptsLoopHelper {
Artem Serov121f2032017-10-23 19:19:06 +010070 public:
71 // Scalar loop unrolling parameters and heuristics.
72 //
73 // Maximum possible unrolling factor.
Artem Serovcf43fb62018-02-15 14:43:48 +000074 static constexpr uint32_t kScalarMaxUnrollFactor = 2;
Artem Serov121f2032017-10-23 19:19:06 +010075 // Loop's maximum instruction count. Loops with higher count will not be peeled/unrolled.
Artem Serovcf43fb62018-02-15 14:43:48 +000076 static constexpr uint32_t kScalarHeuristicMaxBodySizeInstr = 17;
Artem Serov121f2032017-10-23 19:19:06 +010077 // Loop's maximum basic block count. Loops with higher count will not be peeled/unrolled.
Artem Serovcf43fb62018-02-15 14:43:48 +000078 static constexpr uint32_t kScalarHeuristicMaxBodySizeBlocks = 6;
Artem Serov121f2032017-10-23 19:19:06 +010079
Artem Serovcf43fb62018-02-15 14:43:48 +000080 bool IsLoopNonBeneficialForScalarOpts(LoopAnalysisInfo* loop_analysis_info) const OVERRIDE {
81 return loop_analysis_info->HasLongTypeInstructions() ||
82 IsLoopTooBig(loop_analysis_info,
83 kScalarHeuristicMaxBodySizeInstr,
84 kScalarHeuristicMaxBodySizeBlocks);
Artem Serov121f2032017-10-23 19:19:06 +010085 }
86
87 uint32_t GetScalarUnrollingFactor(HLoopInformation* loop_info ATTRIBUTE_UNUSED,
88 uint64_t trip_count) const OVERRIDE {
Artem Serovcf43fb62018-02-15 14:43:48 +000089 uint32_t desired_unrolling_factor = kScalarMaxUnrollFactor;
Artem Serov121f2032017-10-23 19:19:06 +010090 if (trip_count < desired_unrolling_factor || trip_count % desired_unrolling_factor != 0) {
91 return kNoUnrollingFactor;
92 }
93
94 return desired_unrolling_factor;
95 }
96
Artem Serov72411e62017-10-19 16:18:07 +010097 bool IsLoopPeelingEnabled() const OVERRIDE { return true; }
98
Artem Serovcf43fb62018-02-15 14:43:48 +000099 protected:
100 bool IsLoopTooBig(LoopAnalysisInfo* loop_analysis_info,
101 size_t instr_threshold,
102 size_t bb_threshold) const {
103 size_t instr_num = loop_analysis_info->GetNumberOfInstructions();
104 size_t bb_num = loop_analysis_info->GetNumberOfBasicBlocks();
105 return (instr_num >= instr_threshold || bb_num >= bb_threshold);
106 }
107};
108
109// Custom implementation of loop helper for arm64 target. Enables heuristics for scalar loop
110// peeling and unrolling and supports SIMD loop unrolling.
111class Arm64LoopHelper : public ArchDefaultLoopHelper {
112 public:
113 // SIMD loop unrolling parameters and heuristics.
114 //
115 // Maximum possible unrolling factor.
116 static constexpr uint32_t kArm64SimdMaxUnrollFactor = 8;
117 // Loop's maximum instruction count. Loops with higher count will not be unrolled.
118 static constexpr uint32_t kArm64SimdHeuristicMaxBodySizeInstr = 50;
119
120 // Loop's maximum instruction count. Loops with higher count will not be peeled/unrolled.
121 static constexpr uint32_t kArm64ScalarHeuristicMaxBodySizeInstr = 40;
122 // Loop's maximum basic block count. Loops with higher count will not be peeled/unrolled.
123 static constexpr uint32_t kArm64ScalarHeuristicMaxBodySizeBlocks = 8;
124
125 bool IsLoopNonBeneficialForScalarOpts(LoopAnalysisInfo* loop_analysis_info) const OVERRIDE {
126 return IsLoopTooBig(loop_analysis_info,
127 kArm64ScalarHeuristicMaxBodySizeInstr,
128 kArm64ScalarHeuristicMaxBodySizeBlocks);
129 }
130
Artem Serov121f2032017-10-23 19:19:06 +0100131 uint32_t GetSIMDUnrollingFactor(HBasicBlock* block,
132 int64_t trip_count,
133 uint32_t max_peel,
134 uint32_t vector_length) const OVERRIDE {
135 // Don't unroll with insufficient iterations.
136 // TODO: Unroll loops with unknown trip count.
137 DCHECK_NE(vector_length, 0u);
138 if (trip_count < (2 * vector_length + max_peel)) {
139 return kNoUnrollingFactor;
140 }
141 // Don't unroll for large loop body size.
142 uint32_t instruction_count = block->GetInstructions().CountSize();
143 if (instruction_count >= kArm64SimdHeuristicMaxBodySizeInstr) {
144 return kNoUnrollingFactor;
145 }
146 // Find a beneficial unroll factor with the following restrictions:
147 // - At least one iteration of the transformed loop should be executed.
148 // - The loop body shouldn't be "too big" (heuristic).
149
150 uint32_t uf1 = kArm64SimdHeuristicMaxBodySizeInstr / instruction_count;
151 uint32_t uf2 = (trip_count - max_peel) / vector_length;
152 uint32_t unroll_factor =
153 TruncToPowerOfTwo(std::min({uf1, uf2, kArm64SimdMaxUnrollFactor}));
154 DCHECK_GE(unroll_factor, 1u);
155 return unroll_factor;
156 }
157};
158
Artem Serovcf43fb62018-02-15 14:43:48 +0000159ArchNoOptsLoopHelper* ArchNoOptsLoopHelper::Create(InstructionSet isa,
160 ArenaAllocator* allocator) {
Artem Serov121f2032017-10-23 19:19:06 +0100161 switch (isa) {
162 case InstructionSet::kArm64: {
163 return new (allocator) Arm64LoopHelper;
164 }
165 default: {
166 return new (allocator) ArchDefaultLoopHelper;
167 }
168 }
169}
170
171} // namespace art