blob: 57509ee410019e079d1863c1c7818ab8660ca15a [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#ifndef ART_COMPILER_OPTIMIZING_LOOP_ANALYSIS_H_
18#define ART_COMPILER_OPTIMIZING_LOOP_ANALYSIS_H_
19
20#include "nodes.h"
21
22namespace art {
23
Artem Serov0e329082018-06-12 10:23:27 +010024class InductionVarRange;
Artem Serov121f2032017-10-23 19:19:06 +010025class LoopAnalysis;
26
Artem Serov121f2032017-10-23 19:19:06 +010027// Class to hold cached information on properties of the loop.
28class LoopAnalysisInfo : public ValueObject {
29 public:
Artem Serov0e329082018-06-12 10:23:27 +010030 // No loop unrolling factor (just one copy of the loop-body).
31 static constexpr uint32_t kNoUnrollingFactor = 1;
32 // Used for unknown and non-constant trip counts (see InductionVarRange::HasKnownTripCount).
33 static constexpr int64_t kUnknownTripCount = -1;
34
Artem Serov121f2032017-10-23 19:19:06 +010035 explicit LoopAnalysisInfo(HLoopInformation* loop_info)
Artem Serov0e329082018-06-12 10:23:27 +010036 : trip_count_(kUnknownTripCount),
37 bb_num_(0),
Artem Serov121f2032017-10-23 19:19:06 +010038 instr_num_(0),
39 exits_num_(0),
Artem Serov0e329082018-06-12 10:23:27 +010040 invariant_exits_num_(0),
Artem Serov72411e62017-10-19 16:18:07 +010041 has_instructions_preventing_scalar_peeling_(false),
Artem Serov121f2032017-10-23 19:19:06 +010042 has_instructions_preventing_scalar_unrolling_(false),
Artem Serovcf43fb62018-02-15 14:43:48 +000043 has_long_type_instructions_(false),
Artem Serov121f2032017-10-23 19:19:06 +010044 loop_info_(loop_info) {}
45
Artem Serov0e329082018-06-12 10:23:27 +010046 int64_t GetTripCount() const { return trip_count_; }
Artem Serov121f2032017-10-23 19:19:06 +010047 size_t GetNumberOfBasicBlocks() const { return bb_num_; }
48 size_t GetNumberOfInstructions() const { return instr_num_; }
49 size_t GetNumberOfExits() const { return exits_num_; }
Artem Serov0e329082018-06-12 10:23:27 +010050 size_t GetNumberOfInvariantExits() const { return invariant_exits_num_; }
Artem Serov121f2032017-10-23 19:19:06 +010051
Artem Serov72411e62017-10-19 16:18:07 +010052 bool HasInstructionsPreventingScalarPeeling() const {
53 return has_instructions_preventing_scalar_peeling_;
54 }
55
Artem Serov121f2032017-10-23 19:19:06 +010056 bool HasInstructionsPreventingScalarUnrolling() const {
57 return has_instructions_preventing_scalar_unrolling_;
58 }
59
Artem Serov0e329082018-06-12 10:23:27 +010060 bool HasInstructionsPreventingScalarOpts() const {
61 return HasInstructionsPreventingScalarPeeling() || HasInstructionsPreventingScalarUnrolling();
62 }
63
Artem Serovcf43fb62018-02-15 14:43:48 +000064 bool HasLongTypeInstructions() const {
65 return has_long_type_instructions_;
66 }
67
Artem Serov0e329082018-06-12 10:23:27 +010068 HLoopInformation* GetLoopInfo() const { return loop_info_; }
Artem Serov121f2032017-10-23 19:19:06 +010069
70 private:
Artem Serov0e329082018-06-12 10:23:27 +010071 // Trip count of the loop if known, kUnknownTripCount otherwise.
72 int64_t trip_count_;
Artem Serov121f2032017-10-23 19:19:06 +010073 // Number of basic blocks in the loop body.
74 size_t bb_num_;
75 // Number of instructions in the loop body.
76 size_t instr_num_;
77 // Number of loop's exits.
78 size_t exits_num_;
Artem Serov0e329082018-06-12 10:23:27 +010079 // Number of "if" loop exits (with HIf instruction) whose condition is loop-invariant.
80 size_t invariant_exits_num_;
Artem Serov72411e62017-10-19 16:18:07 +010081 // Whether the loop has instructions which make scalar loop peeling non-beneficial.
82 bool has_instructions_preventing_scalar_peeling_;
Artem Serov121f2032017-10-23 19:19:06 +010083 // Whether the loop has instructions which make scalar loop unrolling non-beneficial.
84 bool has_instructions_preventing_scalar_unrolling_;
Artem Serovcf43fb62018-02-15 14:43:48 +000085 // Whether the loop has instructions of primitive long type; unrolling these loop will
86 // likely introduce spill/fills on 32-bit targets.
87 bool has_long_type_instructions_;
Artem Serov121f2032017-10-23 19:19:06 +010088
89 // Corresponding HLoopInformation.
Artem Serov0e329082018-06-12 10:23:27 +010090 HLoopInformation* loop_info_;
Artem Serov121f2032017-10-23 19:19:06 +010091
92 friend class LoopAnalysis;
93};
94
95// Placeholder class for methods and routines used to analyse loops, calculate loop properties
96// and characteristics.
97class LoopAnalysis : public ValueObject {
98 public:
99 // Calculates loops basic properties like body size, exits number, etc. and fills
100 // 'analysis_results' with this information.
101 static void CalculateLoopBasicProperties(HLoopInformation* loop_info,
Artem Serov0e329082018-06-12 10:23:27 +0100102 LoopAnalysisInfo* analysis_results,
103 int64_t trip_count);
Artem Serov121f2032017-10-23 19:19:06 +0100104
Artem Serov0e329082018-06-12 10:23:27 +0100105 // Returns the trip count of the loop if it is known and kUnknownTripCount otherwise.
106 static int64_t GetLoopTripCount(HLoopInformation* loop_info,
107 const InductionVarRange* induction_range);
Artem Serov72411e62017-10-19 16:18:07 +0100108
Artem Serov121f2032017-10-23 19:19:06 +0100109 private:
Artem Serov72411e62017-10-19 16:18:07 +0100110 // Returns whether an instruction makes scalar loop peeling/unrolling non-beneficial.
Artem Serov121f2032017-10-23 19:19:06 +0100111 //
112 // If in the loop body we have a dex/runtime call then its contribution to the whole
Artem Serov72411e62017-10-19 16:18:07 +0100113 // loop performance will probably prevail. So peeling/unrolling optimization will not bring
114 // any noticeable performance improvement. It will increase the code size.
115 static bool MakesScalarPeelingUnrollingNonBeneficial(HInstruction* instruction) {
Artem Serov121f2032017-10-23 19:19:06 +0100116 return (instruction->IsNewArray() ||
117 instruction->IsNewInstance() ||
118 instruction->IsUnresolvedInstanceFieldGet() ||
119 instruction->IsUnresolvedInstanceFieldSet() ||
120 instruction->IsUnresolvedStaticFieldGet() ||
121 instruction->IsUnresolvedStaticFieldSet() ||
Artem Serov72411e62017-10-19 16:18:07 +0100122 // TODO: Support loops with intrinsified invokes.
Artem Serova6e26142018-06-19 14:55:17 +0100123 instruction->IsInvoke());
Artem Serov121f2032017-10-23 19:19:06 +0100124 }
125};
126
127//
128// Helper class which holds target-dependent methods and constants needed for loop optimizations.
129//
130// To support peeling/unrolling for a new architecture one needs to create new helper class,
131// inherit it from this and add implementation for the following methods.
132//
Artem Serovcf43fb62018-02-15 14:43:48 +0000133class ArchNoOptsLoopHelper : public ArenaObject<kArenaAllocOptimization> {
Artem Serov121f2032017-10-23 19:19:06 +0100134 public:
Artem Serovcf43fb62018-02-15 14:43:48 +0000135 virtual ~ArchNoOptsLoopHelper() {}
Artem Serov121f2032017-10-23 19:19:06 +0100136
137 // Creates an instance of specialised helper for the target or default helper if the target
138 // doesn't support loop peeling and unrolling.
Artem Serovcf43fb62018-02-15 14:43:48 +0000139 static ArchNoOptsLoopHelper* Create(InstructionSet isa, ArenaAllocator* allocator);
Artem Serov121f2032017-10-23 19:19:06 +0100140
Artem Serovcf43fb62018-02-15 14:43:48 +0000141 // Returns whether the loop is not beneficial for loop peeling/unrolling.
Artem Serov121f2032017-10-23 19:19:06 +0100142 //
Artem Serovcf43fb62018-02-15 14:43:48 +0000143 // For example, if the loop body has too many instructions then peeling/unrolling optimization
144 // will not bring any noticeable performance improvement however will increase the code size.
Artem Serov121f2032017-10-23 19:19:06 +0100145 //
146 // Returns 'true' by default, should be overridden by particular target loop helper.
Artem Serovcf43fb62018-02-15 14:43:48 +0000147 virtual bool IsLoopNonBeneficialForScalarOpts(
Artem Serov121f2032017-10-23 19:19:06 +0100148 LoopAnalysisInfo* loop_analysis_info ATTRIBUTE_UNUSED) const { return true; }
149
150 // Returns optimal scalar unrolling factor for the loop.
151 //
152 // Returns kNoUnrollingFactor by default, should be overridden by particular target loop helper.
Artem Serov0e329082018-06-12 10:23:27 +0100153 virtual uint32_t GetScalarUnrollingFactor(
154 const LoopAnalysisInfo* analysis_info ATTRIBUTE_UNUSED) const {
155 return LoopAnalysisInfo::kNoUnrollingFactor;
Artem Serov121f2032017-10-23 19:19:06 +0100156 }
157
Artem Serov72411e62017-10-19 16:18:07 +0100158 // Returns whether scalar loop peeling is enabled,
159 //
160 // Returns 'false' by default, should be overridden by particular target loop helper.
161 virtual bool IsLoopPeelingEnabled() const { return false; }
162
Artem Serov18ba1da2018-05-16 19:06:32 +0100163 // Returns whether it is beneficial to fully unroll the loop.
164 //
165 // Returns 'false' by default, should be overridden by particular target loop helper.
166 virtual bool IsFullUnrollingBeneficial(LoopAnalysisInfo* analysis_info ATTRIBUTE_UNUSED) const {
167 return false;
168 }
169
Artem Serov121f2032017-10-23 19:19:06 +0100170 // Returns optimal SIMD unrolling factor for the loop.
171 //
172 // Returns kNoUnrollingFactor by default, should be overridden by particular target loop helper.
173 virtual uint32_t GetSIMDUnrollingFactor(HBasicBlock* block ATTRIBUTE_UNUSED,
174 int64_t trip_count ATTRIBUTE_UNUSED,
175 uint32_t max_peel ATTRIBUTE_UNUSED,
176 uint32_t vector_length ATTRIBUTE_UNUSED) const {
Artem Serov0e329082018-06-12 10:23:27 +0100177 return LoopAnalysisInfo::kNoUnrollingFactor;
Artem Serov121f2032017-10-23 19:19:06 +0100178 }
179};
180
181} // namespace art
182
183#endif // ART_COMPILER_OPTIMIZING_LOOP_ANALYSIS_H_