blob: f895bff479a89dcae158543b6603e44b021f32a4 [file] [log] [blame]
Aart Bik281c6812016-08-26 11:31:48 -07001/*
2 * Copyright (C) 2016 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_OPTIMIZATION_H_
18#define ART_COMPILER_OPTIMIZING_LOOP_OPTIMIZATION_H_
19
Vladimir Markoe2727152019-10-10 10:46:42 +010020#include "base/macros.h"
Vladimir Markoca6fff82017-10-03 14:49:14 +010021#include "base/scoped_arena_allocator.h"
22#include "base/scoped_arena_containers.h"
Aart Bik281c6812016-08-26 11:31:48 -070023#include "induction_var_range.h"
Artem Serov121f2032017-10-23 19:19:06 +010024#include "loop_analysis.h"
Aart Bik281c6812016-08-26 11:31:48 -070025#include "nodes.h"
26#include "optimization.h"
Artem Serov121f2032017-10-23 19:19:06 +010027#include "superblock_cloner.h"
Aart Bik281c6812016-08-26 11:31:48 -070028
Vladimir Markoe2727152019-10-10 10:46:42 +010029namespace art HIDDEN {
Aart Bik281c6812016-08-26 11:31:48 -070030
Vladimir Markoa0431112018-06-25 09:32:54 +010031class CompilerOptions;
Artem Serovcf43fb62018-02-15 14:43:48 +000032class ArchNoOptsLoopHelper;
Aart Bik92685a82017-03-06 11:13:43 -080033
Aart Bik281c6812016-08-26 11:31:48 -070034/**
35 * Loop optimizations. Builds a loop hierarchy and applies optimizations to
Aart Bikf8f5a162017-02-06 15:35:29 -080036 * the detected nested loops, such as removal of dead induction and empty loops
37 * and inner loop vectorization.
Aart Bik281c6812016-08-26 11:31:48 -070038 */
39class HLoopOptimization : public HOptimization {
40 public:
Aart Bik92685a82017-03-06 11:13:43 -080041 HLoopOptimization(HGraph* graph,
Vladimir Markoa0431112018-06-25 09:32:54 +010042 const CompilerOptions* compiler_options,
Aart Bikb92cc332017-09-06 15:53:17 -070043 HInductionVarAnalysis* induction_analysis,
Aart Bik2ca10eb2017-11-15 15:17:53 -080044 OptimizingCompilerStats* stats,
45 const char* name = kLoopOptimizationPassName);
Aart Bik281c6812016-08-26 11:31:48 -070046
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010047 bool Run() override;
Aart Bik281c6812016-08-26 11:31:48 -070048
49 static constexpr const char* kLoopOptimizationPassName = "loop_optimization";
50
51 private:
52 /**
53 * A single loop inside the loop hierarchy representation.
54 */
Aart Bik96202302016-10-04 17:33:56 -070055 struct LoopNode : public ArenaObject<kArenaAllocLoopOptimization> {
Aart Bik281c6812016-08-26 11:31:48 -070056 explicit LoopNode(HLoopInformation* lp_info)
57 : loop_info(lp_info),
58 outer(nullptr),
59 inner(nullptr),
60 previous(nullptr),
61 next(nullptr) {}
Aart Bikf8f5a162017-02-06 15:35:29 -080062 HLoopInformation* loop_info;
Aart Bik281c6812016-08-26 11:31:48 -070063 LoopNode* outer;
64 LoopNode* inner;
65 LoopNode* previous;
66 LoopNode* next;
67 };
68
Aart Bikf8f5a162017-02-06 15:35:29 -080069 /*
70 * Vectorization restrictions (bit mask).
71 */
72 enum VectorRestrictions {
Aart Bik0148de42017-09-05 09:25:01 -070073 kNone = 0, // no restrictions
74 kNoMul = 1 << 0, // no multiplication
75 kNoDiv = 1 << 1, // no division
76 kNoShift = 1 << 2, // no shift
77 kNoShr = 1 << 3, // no arithmetic shift right
78 kNoHiBits = 1 << 4, // "wider" operations cannot bring in higher order bits
79 kNoSignedHAdd = 1 << 5, // no signed halving add
80 kNoUnroundedHAdd = 1 << 6, // no unrounded halving add
81 kNoAbs = 1 << 7, // no absolute value
Aart Bik3f08e9b2018-05-01 13:42:03 -070082 kNoStringCharAt = 1 << 8, // no StringCharAt
83 kNoReduction = 1 << 9, // no reduction
84 kNoSAD = 1 << 10, // no sum of absolute differences (SAD)
85 kNoWideSAD = 1 << 11, // no sum of absolute differences (SAD) with operand widening
Artem Serovaaac0e32018-08-07 00:52:22 +010086 kNoDotProd = 1 << 12, // no dot product
Aart Bikf8f5a162017-02-06 15:35:29 -080087 };
Aart Bik96202302016-10-04 17:33:56 -070088
Aart Bikf8f5a162017-02-06 15:35:29 -080089 /*
90 * Vectorization mode during synthesis
91 * (sequential peeling/cleanup loop or vector loop).
92 */
93 enum VectorMode {
94 kSequential,
95 kVector
96 };
97
98 /*
99 * Representation of a unit-stride array reference.
100 */
101 struct ArrayReference {
Aart Bik38a3f212017-10-20 17:02:21 -0700102 ArrayReference(HInstruction* b, HInstruction* o, DataType::Type t, bool l, bool c = false)
103 : base(b), offset(o), type(t), lhs(l), is_string_char_at(c) { }
Aart Bikf8f5a162017-02-06 15:35:29 -0800104 bool operator<(const ArrayReference& other) const {
105 return
106 (base < other.base) ||
107 (base == other.base &&
108 (offset < other.offset || (offset == other.offset &&
109 (type < other.type ||
Aart Bik38a3f212017-10-20 17:02:21 -0700110 (type == other.type &&
111 (lhs < other.lhs ||
112 (lhs == other.lhs &&
113 is_string_char_at < other.is_string_char_at)))))));
Aart Bikf8f5a162017-02-06 15:35:29 -0800114 }
Aart Bik38a3f212017-10-20 17:02:21 -0700115 HInstruction* base; // base address
116 HInstruction* offset; // offset + i
117 DataType::Type type; // component type
118 bool lhs; // def/use
119 bool is_string_char_at; // compressed string read
Aart Bikf8f5a162017-02-06 15:35:29 -0800120 };
121
Aart Bikb29f6842017-07-28 15:58:41 -0700122 //
Aart Bikf8f5a162017-02-06 15:35:29 -0800123 // Loop setup and traversal.
Aart Bikb29f6842017-07-28 15:58:41 -0700124 //
125
Aart Bik24773202018-04-26 10:28:51 -0700126 bool LocalRun();
Aart Bik281c6812016-08-26 11:31:48 -0700127 void AddLoop(HLoopInformation* loop_info);
128 void RemoveLoop(LoopNode* node);
Aart Bik281c6812016-08-26 11:31:48 -0700129
Aart Bikb29f6842017-07-28 15:58:41 -0700130 // Traverses all loops inner to outer to perform simplifications and optimizations.
131 // Returns true if loops nested inside current loop (node) have changed.
132 bool TraverseLoopsInnerToOuter(LoopNode* node);
133
134 //
Aart Bikf8f5a162017-02-06 15:35:29 -0800135 // Optimization.
Aart Bikb29f6842017-07-28 15:58:41 -0700136 //
137
Aart Bik281c6812016-08-26 11:31:48 -0700138 void SimplifyInduction(LoopNode* node);
Aart Bik482095d2016-10-10 15:39:10 -0700139 void SimplifyBlocks(LoopNode* node);
Aart Bikf8f5a162017-02-06 15:35:29 -0800140
Artem Serov121f2032017-10-23 19:19:06 +0100141 // Performs optimizations specific to inner loop with finite header logic (empty loop removal,
Aart Bikb29f6842017-07-28 15:58:41 -0700142 // unrolling, vectorization). Returns true if anything changed.
Artem Serov121f2032017-10-23 19:19:06 +0100143 bool TryOptimizeInnerLoopFinite(LoopNode* node);
144
145 // Performs optimizations specific to inner loop. Returns true if anything changed.
Aart Bikb29f6842017-07-28 15:58:41 -0700146 bool OptimizeInnerLoop(LoopNode* node);
147
Artem Serov121f2032017-10-23 19:19:06 +0100148 // Tries to apply loop unrolling for branch penalty reduction and better instruction scheduling
Artem Serov0e329082018-06-12 10:23:27 +0100149 // opportunities. Returns whether transformation happened. 'generate_code' determines whether the
150 // optimization should be actually applied.
151 bool TryUnrollingForBranchPenaltyReduction(LoopAnalysisInfo* analysis_info,
152 bool generate_code = true);
Artem Serov121f2032017-10-23 19:19:06 +0100153
Artem Serov72411e62017-10-19 16:18:07 +0100154 // Tries to apply loop peeling for loop invariant exits elimination. Returns whether
Artem Serov0e329082018-06-12 10:23:27 +0100155 // transformation happened. 'generate_code' determines whether the optimization should be
156 // actually applied.
157 bool TryPeelingForLoopInvariantExitsElimination(LoopAnalysisInfo* analysis_info,
158 bool generate_code = true);
159
Artem Serov18ba1da2018-05-16 19:06:32 +0100160 // Tries to perform whole loop unrolling for a small loop with a small trip count to eliminate
161 // the loop check overhead and to have more opportunities for inter-iteration optimizations.
162 // Returns whether transformation happened. 'generate_code' determines whether the optimization
163 // should be actually applied.
164 bool TryFullUnrolling(LoopAnalysisInfo* analysis_info, bool generate_code = true);
165
Artem Serov0e329082018-06-12 10:23:27 +0100166 // Tries to apply scalar loop peeling and unrolling.
167 bool TryPeelingAndUnrolling(LoopNode* node);
Artem Serov72411e62017-10-19 16:18:07 +0100168
Aart Bikb29f6842017-07-28 15:58:41 -0700169 //
Aart Bikf8f5a162017-02-06 15:35:29 -0800170 // Vectorization analysis and synthesis.
Aart Bikb29f6842017-07-28 15:58:41 -0700171 //
172
Aart Bik14a68b42017-06-08 14:06:58 -0700173 bool ShouldVectorize(LoopNode* node, HBasicBlock* block, int64_t trip_count);
Aart Bikf8f5a162017-02-06 15:35:29 -0800174 void Vectorize(LoopNode* node, HBasicBlock* block, HBasicBlock* exit, int64_t trip_count);
175 void GenerateNewLoop(LoopNode* node,
176 HBasicBlock* block,
177 HBasicBlock* new_preheader,
178 HInstruction* lo,
179 HInstruction* hi,
Aart Bik14a68b42017-06-08 14:06:58 -0700180 HInstruction* step,
181 uint32_t unroll);
Aart Bikf8f5a162017-02-06 15:35:29 -0800182 bool VectorizeDef(LoopNode* node, HInstruction* instruction, bool generate_code);
183 bool VectorizeUse(LoopNode* node,
184 HInstruction* instruction,
185 bool generate_code,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100186 DataType::Type type,
Aart Bikf8f5a162017-02-06 15:35:29 -0800187 uint64_t restrictions);
Aart Bik38a3f212017-10-20 17:02:21 -0700188 uint32_t GetVectorSizeInBytes();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100189 bool TrySetVectorType(DataType::Type type, /*out*/ uint64_t* restrictions);
Aart Bikf8f5a162017-02-06 15:35:29 -0800190 bool TrySetVectorLength(uint32_t length);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100191 void GenerateVecInv(HInstruction* org, DataType::Type type);
Aart Bik14a68b42017-06-08 14:06:58 -0700192 void GenerateVecSub(HInstruction* org, HInstruction* offset);
Aart Bikf8f5a162017-02-06 15:35:29 -0800193 void GenerateVecMem(HInstruction* org,
194 HInstruction* opa,
195 HInstruction* opb,
Aart Bik14a68b42017-06-08 14:06:58 -0700196 HInstruction* offset,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100197 DataType::Type type);
Aart Bik0148de42017-09-05 09:25:01 -0700198 void GenerateVecReductionPhi(HPhi* phi);
199 void GenerateVecReductionPhiInputs(HPhi* phi, HInstruction* reduction);
200 HInstruction* ReduceAndExtractIfNeeded(HInstruction* instruction);
Aart Bik304c8a52017-05-23 11:01:13 -0700201 void GenerateVecOp(HInstruction* org,
202 HInstruction* opa,
203 HInstruction* opb,
Aart Bik3f08e9b2018-05-01 13:42:03 -0700204 DataType::Type type);
Aart Bik281c6812016-08-26 11:31:48 -0700205
Aart Bikf3e61ee2017-04-12 17:09:20 -0700206 // Vectorization idioms.
Aart Bik29aa0822018-03-08 11:28:00 -0800207 bool VectorizeSaturationIdiom(LoopNode* node,
208 HInstruction* instruction,
209 bool generate_code,
210 DataType::Type type,
211 uint64_t restrictions);
Aart Bikf3e61ee2017-04-12 17:09:20 -0700212 bool VectorizeHalvingAddIdiom(LoopNode* node,
213 HInstruction* instruction,
214 bool generate_code,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100215 DataType::Type type,
Aart Bikf3e61ee2017-04-12 17:09:20 -0700216 uint64_t restrictions);
Aart Bikdbbac8f2017-09-01 13:06:08 -0700217 bool VectorizeSADIdiom(LoopNode* node,
218 HInstruction* instruction,
219 bool generate_code,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100220 DataType::Type type,
Aart Bikdbbac8f2017-09-01 13:06:08 -0700221 uint64_t restrictions);
Artem Serovaaac0e32018-08-07 00:52:22 +0100222 bool VectorizeDotProdIdiom(LoopNode* node,
223 HInstruction* instruction,
224 bool generate_code,
225 DataType::Type type,
226 uint64_t restrictions);
Aart Bikf3e61ee2017-04-12 17:09:20 -0700227
Aart Bik14a68b42017-06-08 14:06:58 -0700228 // Vectorization heuristics.
Aart Bik38a3f212017-10-20 17:02:21 -0700229 Alignment ComputeAlignment(HInstruction* offset,
230 DataType::Type type,
231 bool is_string_char_at,
232 uint32_t peeling = 0);
233 void SetAlignmentStrategy(uint32_t peeling_votes[],
234 const ArrayReference* peeling_candidate);
235 uint32_t MaxNumberPeeled();
Aart Bik14a68b42017-06-08 14:06:58 -0700236 bool IsVectorizationProfitable(int64_t trip_count);
Aart Bik14a68b42017-06-08 14:06:58 -0700237
Aart Bikb29f6842017-07-28 15:58:41 -0700238 //
Aart Bik6b69e0a2017-01-11 10:20:43 -0800239 // Helpers.
Aart Bikb29f6842017-07-28 15:58:41 -0700240 //
241
Aart Bikf8f5a162017-02-06 15:35:29 -0800242 bool TrySetPhiInduction(HPhi* phi, bool restrict_uses);
Aart Bikb29f6842017-07-28 15:58:41 -0700243 bool TrySetPhiReduction(HPhi* phi);
244
245 // Detects loop header with a single induction (returned in main_phi), possibly
246 // other phis for reductions, but no other side effects. Returns true on success.
247 bool TrySetSimpleLoopHeader(HBasicBlock* block, /*out*/ HPhi** main_phi);
248
Aart Bikcc42be02016-10-20 16:14:16 -0700249 bool IsEmptyBody(HBasicBlock* block);
Aart Bik482095d2016-10-10 15:39:10 -0700250 bool IsOnlyUsedAfterLoop(HLoopInformation* loop_info,
Aart Bik8c4a8542016-10-06 11:36:57 -0700251 HInstruction* instruction,
Aart Bik6b69e0a2017-01-11 10:20:43 -0800252 bool collect_loop_uses,
Aart Bik38a3f212017-10-20 17:02:21 -0700253 /*out*/ uint32_t* use_count);
Aart Bikf8f5a162017-02-06 15:35:29 -0800254 bool IsUsedOutsideLoop(HLoopInformation* loop_info,
255 HInstruction* instruction);
Nicolas Geoffray1a0a5192017-06-22 11:56:01 +0100256 bool TryReplaceWithLastValue(HLoopInformation* loop_info,
257 HInstruction* instruction,
258 HBasicBlock* block);
Aart Bikf8f5a162017-02-06 15:35:29 -0800259 bool TryAssignLastValue(HLoopInformation* loop_info,
260 HInstruction* instruction,
261 HBasicBlock* block,
262 bool collect_loop_uses);
Aart Bik6b69e0a2017-01-11 10:20:43 -0800263 void RemoveDeadInstructions(const HInstructionList& list);
Nicolas Geoffray1a0a5192017-06-22 11:56:01 +0100264 bool CanRemoveCycle(); // Whether the current 'iset_' is removable.
Aart Bik281c6812016-08-26 11:31:48 -0700265
Vladimir Markoa0431112018-06-25 09:32:54 +0100266 // Compiler options (to query ISA features).
267 const CompilerOptions* compiler_options_;
Aart Bik92685a82017-03-06 11:13:43 -0800268
Aart Bik96202302016-10-04 17:33:56 -0700269 // Range information based on prior induction variable analysis.
Aart Bik281c6812016-08-26 11:31:48 -0700270 InductionVarRange induction_range_;
271
272 // Phase-local heap memory allocator for the loop optimizer. Storage obtained
Aart Bik96202302016-10-04 17:33:56 -0700273 // through this allocator is immediately released when the loop optimizer is done.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100274 ScopedArenaAllocator* loop_allocator_;
Aart Bik281c6812016-08-26 11:31:48 -0700275
Aart Bikf8f5a162017-02-06 15:35:29 -0800276 // Global heap memory allocator. Used to build HIR.
277 ArenaAllocator* global_allocator_;
278
Aart Bik96202302016-10-04 17:33:56 -0700279 // Entries into the loop hierarchy representation. The hierarchy resides
280 // in phase-local heap memory.
Aart Bik281c6812016-08-26 11:31:48 -0700281 LoopNode* top_loop_;
282 LoopNode* last_loop_;
283
Aart Bik8c4a8542016-10-06 11:36:57 -0700284 // Temporary bookkeeping of a set of instructions.
285 // Contents reside in phase-local heap memory.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100286 ScopedArenaSet<HInstruction*>* iset_;
Aart Bik8c4a8542016-10-06 11:36:57 -0700287
Aart Bikb29f6842017-07-28 15:58:41 -0700288 // Temporary bookkeeping of reduction instructions. Mapping is two-fold:
289 // (1) reductions in the loop-body are mapped back to their phi definition,
290 // (2) phi definitions are mapped to their initial value (updated during
291 // code generation to feed the proper values into the new chain).
292 // Contents reside in phase-local heap memory.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100293 ScopedArenaSafeMap<HInstruction*, HInstruction*>* reductions_;
Aart Bik482095d2016-10-10 15:39:10 -0700294
Aart Bikdf7822e2016-12-06 10:05:30 -0800295 // Flag that tracks if any simplifications have occurred.
296 bool simplified_;
297
Aart Bikf8f5a162017-02-06 15:35:29 -0800298 // Number of "lanes" for selected packed type.
299 uint32_t vector_length_;
300
301 // Set of array references in the vector loop.
302 // Contents reside in phase-local heap memory.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100303 ScopedArenaSet<ArrayReference>* vector_refs_;
Aart Bikf8f5a162017-02-06 15:35:29 -0800304
Aart Bik38a3f212017-10-20 17:02:21 -0700305 // Static or dynamic loop peeling for alignment.
306 uint32_t vector_static_peeling_factor_;
307 const ArrayReference* vector_dynamic_peeling_candidate_;
Aart Bik14a68b42017-06-08 14:06:58 -0700308
309 // Dynamic data dependence test of the form a != b.
310 HInstruction* vector_runtime_test_a_;
311 HInstruction* vector_runtime_test_b_;
312
Aart Bikf8f5a162017-02-06 15:35:29 -0800313 // Mapping used during vectorization synthesis for both the scalar peeling/cleanup
Aart Bik14a68b42017-06-08 14:06:58 -0700314 // loop (mode is kSequential) and the actual vector loop (mode is kVector). The data
Aart Bikf8f5a162017-02-06 15:35:29 -0800315 // structure maps original instructions into the new instructions.
316 // Contents reside in phase-local heap memory.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100317 ScopedArenaSafeMap<HInstruction*, HInstruction*>* vector_map_;
Aart Bikf8f5a162017-02-06 15:35:29 -0800318
Aart Bik0148de42017-09-05 09:25:01 -0700319 // Permanent mapping used during vectorization synthesis.
320 // Contents reside in phase-local heap memory.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100321 ScopedArenaSafeMap<HInstruction*, HInstruction*>* vector_permanent_map_;
Aart Bik0148de42017-09-05 09:25:01 -0700322
Aart Bikf8f5a162017-02-06 15:35:29 -0800323 // Temporary vectorization bookkeeping.
Aart Bik14a68b42017-06-08 14:06:58 -0700324 VectorMode vector_mode_; // synthesis mode
Aart Bikf8f5a162017-02-06 15:35:29 -0800325 HBasicBlock* vector_preheader_; // preheader of the new loop
326 HBasicBlock* vector_header_; // header of the new loop
327 HBasicBlock* vector_body_; // body of the new loop
Aart Bik14a68b42017-06-08 14:06:58 -0700328 HInstruction* vector_index_; // normalized index of the new loop
Aart Bikf8f5a162017-02-06 15:35:29 -0800329
Artem Serov121f2032017-10-23 19:19:06 +0100330 // Helper for target-specific behaviour for loop optimizations.
Artem Serovcf43fb62018-02-15 14:43:48 +0000331 ArchNoOptsLoopHelper* arch_loop_helper_;
Artem Serov121f2032017-10-23 19:19:06 +0100332
Aart Bik281c6812016-08-26 11:31:48 -0700333 friend class LoopOptimizationTest;
334
335 DISALLOW_COPY_AND_ASSIGN(HLoopOptimization);
336};
337
338} // namespace art
339
340#endif // ART_COMPILER_OPTIMIZING_LOOP_OPTIMIZATION_H_