blob: 0120cffa56306e17b7e42de4c9f4935140246459 [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 Markoca6fff82017-10-03 14:49:14 +010020#include "base/scoped_arena_allocator.h"
21#include "base/scoped_arena_containers.h"
Aart Bik281c6812016-08-26 11:31:48 -070022#include "induction_var_range.h"
Artem Serov121f2032017-10-23 19:19:06 +010023#include "loop_analysis.h"
Aart Bik281c6812016-08-26 11:31:48 -070024#include "nodes.h"
25#include "optimization.h"
Artem Serov121f2032017-10-23 19:19:06 +010026#include "superblock_cloner.h"
Aart Bik281c6812016-08-26 11:31:48 -070027
28namespace art {
29
Aart Bik92685a82017-03-06 11:13:43 -080030class CompilerDriver;
Artem Serov121f2032017-10-23 19:19:06 +010031class ArchDefaultLoopHelper;
Aart Bik92685a82017-03-06 11:13:43 -080032
Aart Bik281c6812016-08-26 11:31:48 -070033/**
34 * Loop optimizations. Builds a loop hierarchy and applies optimizations to
Aart Bikf8f5a162017-02-06 15:35:29 -080035 * the detected nested loops, such as removal of dead induction and empty loops
36 * and inner loop vectorization.
Aart Bik281c6812016-08-26 11:31:48 -070037 */
38class HLoopOptimization : public HOptimization {
39 public:
Aart Bik92685a82017-03-06 11:13:43 -080040 HLoopOptimization(HGraph* graph,
41 CompilerDriver* compiler_driver,
Aart Bikb92cc332017-09-06 15:53:17 -070042 HInductionVarAnalysis* induction_analysis,
Aart Bik2ca10eb2017-11-15 15:17:53 -080043 OptimizingCompilerStats* stats,
44 const char* name = kLoopOptimizationPassName);
Aart Bik281c6812016-08-26 11:31:48 -070045
46 void Run() OVERRIDE;
47
48 static constexpr const char* kLoopOptimizationPassName = "loop_optimization";
49
50 private:
51 /**
52 * A single loop inside the loop hierarchy representation.
53 */
Aart Bik96202302016-10-04 17:33:56 -070054 struct LoopNode : public ArenaObject<kArenaAllocLoopOptimization> {
Aart Bik281c6812016-08-26 11:31:48 -070055 explicit LoopNode(HLoopInformation* lp_info)
56 : loop_info(lp_info),
57 outer(nullptr),
58 inner(nullptr),
59 previous(nullptr),
60 next(nullptr) {}
Aart Bikf8f5a162017-02-06 15:35:29 -080061 HLoopInformation* loop_info;
Aart Bik281c6812016-08-26 11:31:48 -070062 LoopNode* outer;
63 LoopNode* inner;
64 LoopNode* previous;
65 LoopNode* next;
66 };
67
Aart Bikf8f5a162017-02-06 15:35:29 -080068 /*
69 * Vectorization restrictions (bit mask).
70 */
71 enum VectorRestrictions {
Aart Bik0148de42017-09-05 09:25:01 -070072 kNone = 0, // no restrictions
73 kNoMul = 1 << 0, // no multiplication
74 kNoDiv = 1 << 1, // no division
75 kNoShift = 1 << 2, // no shift
76 kNoShr = 1 << 3, // no arithmetic shift right
77 kNoHiBits = 1 << 4, // "wider" operations cannot bring in higher order bits
78 kNoSignedHAdd = 1 << 5, // no signed halving add
79 kNoUnroundedHAdd = 1 << 6, // no unrounded halving add
80 kNoAbs = 1 << 7, // no absolute value
81 kNoMinMax = 1 << 8, // no min/max
82 kNoStringCharAt = 1 << 9, // no StringCharAt
83 kNoReduction = 1 << 10, // no reduction
Aart Bikdbbac8f2017-09-01 13:06:08 -070084 kNoSAD = 1 << 11, // no sum of absolute differences (SAD)
Artem Serov6e9b1372017-10-05 16:48:30 +010085 kNoWideSAD = 1 << 12, // no sum of absolute differences (SAD) with operand widening
Aart Bik29aa0822018-03-08 11:28:00 -080086 kNoSaturation = 1 << 13, // no saturation arithmetic
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 Bikf8f5a162017-02-06 15:35:29 -0800126 void 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 // Performs loop peeling/unrolling once (depends on the 'do_unrolling'); the transformation
149 // preserves the header and the loop info.
150 //
151 // Note: the function records copying information about blocks and instructions.
152 void PeelOrUnrollOnce(LoopNode* loop_node,
153 bool do_unrolling,
154 SuperblockCloner::HBasicBlockMap* bb_map,
155 SuperblockCloner::HInstructionMap* hir_map);
156
157 // Tries to apply loop unrolling for branch penalty reduction and better instruction scheduling
158 // opportunities. Returns whether transformation happened.
159 bool TryUnrollingForBranchPenaltyReduction(LoopNode* loop_node);
160
Aart Bikb29f6842017-07-28 15:58:41 -0700161 //
Aart Bikf8f5a162017-02-06 15:35:29 -0800162 // Vectorization analysis and synthesis.
Aart Bikb29f6842017-07-28 15:58:41 -0700163 //
164
Aart Bik14a68b42017-06-08 14:06:58 -0700165 bool ShouldVectorize(LoopNode* node, HBasicBlock* block, int64_t trip_count);
Aart Bikf8f5a162017-02-06 15:35:29 -0800166 void Vectorize(LoopNode* node, HBasicBlock* block, HBasicBlock* exit, int64_t trip_count);
167 void GenerateNewLoop(LoopNode* node,
168 HBasicBlock* block,
169 HBasicBlock* new_preheader,
170 HInstruction* lo,
171 HInstruction* hi,
Aart Bik14a68b42017-06-08 14:06:58 -0700172 HInstruction* step,
173 uint32_t unroll);
Aart Bikf8f5a162017-02-06 15:35:29 -0800174 bool VectorizeDef(LoopNode* node, HInstruction* instruction, bool generate_code);
175 bool VectorizeUse(LoopNode* node,
176 HInstruction* instruction,
177 bool generate_code,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100178 DataType::Type type,
Aart Bikf8f5a162017-02-06 15:35:29 -0800179 uint64_t restrictions);
Aart Bik38a3f212017-10-20 17:02:21 -0700180 uint32_t GetVectorSizeInBytes();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100181 bool TrySetVectorType(DataType::Type type, /*out*/ uint64_t* restrictions);
Aart Bikf8f5a162017-02-06 15:35:29 -0800182 bool TrySetVectorLength(uint32_t length);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100183 void GenerateVecInv(HInstruction* org, DataType::Type type);
Aart Bik14a68b42017-06-08 14:06:58 -0700184 void GenerateVecSub(HInstruction* org, HInstruction* offset);
Aart Bikf8f5a162017-02-06 15:35:29 -0800185 void GenerateVecMem(HInstruction* org,
186 HInstruction* opa,
187 HInstruction* opb,
Aart Bik14a68b42017-06-08 14:06:58 -0700188 HInstruction* offset,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100189 DataType::Type type);
Aart Bik0148de42017-09-05 09:25:01 -0700190 void GenerateVecReductionPhi(HPhi* phi);
191 void GenerateVecReductionPhiInputs(HPhi* phi, HInstruction* reduction);
192 HInstruction* ReduceAndExtractIfNeeded(HInstruction* instruction);
Aart Bik304c8a52017-05-23 11:01:13 -0700193 void GenerateVecOp(HInstruction* org,
194 HInstruction* opa,
195 HInstruction* opb,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100196 DataType::Type type,
Aart Bik304c8a52017-05-23 11:01:13 -0700197 bool is_unsigned = false);
Aart Bik281c6812016-08-26 11:31:48 -0700198
Aart Bikf3e61ee2017-04-12 17:09:20 -0700199 // Vectorization idioms.
Aart Bik29aa0822018-03-08 11:28:00 -0800200 bool VectorizeSaturationIdiom(LoopNode* node,
201 HInstruction* instruction,
202 bool generate_code,
203 DataType::Type type,
204 uint64_t restrictions);
Aart Bikf3e61ee2017-04-12 17:09:20 -0700205 bool VectorizeHalvingAddIdiom(LoopNode* node,
206 HInstruction* instruction,
207 bool generate_code,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100208 DataType::Type type,
Aart Bikf3e61ee2017-04-12 17:09:20 -0700209 uint64_t restrictions);
Aart Bikdbbac8f2017-09-01 13:06:08 -0700210 bool VectorizeSADIdiom(LoopNode* node,
211 HInstruction* instruction,
212 bool generate_code,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100213 DataType::Type type,
Aart Bikdbbac8f2017-09-01 13:06:08 -0700214 uint64_t restrictions);
Aart Bikf3e61ee2017-04-12 17:09:20 -0700215
Aart Bik14a68b42017-06-08 14:06:58 -0700216 // Vectorization heuristics.
Aart Bik38a3f212017-10-20 17:02:21 -0700217 Alignment ComputeAlignment(HInstruction* offset,
218 DataType::Type type,
219 bool is_string_char_at,
220 uint32_t peeling = 0);
221 void SetAlignmentStrategy(uint32_t peeling_votes[],
222 const ArrayReference* peeling_candidate);
223 uint32_t MaxNumberPeeled();
Aart Bik14a68b42017-06-08 14:06:58 -0700224 bool IsVectorizationProfitable(int64_t trip_count);
Aart Bik14a68b42017-06-08 14:06:58 -0700225
Aart Bikb29f6842017-07-28 15:58:41 -0700226 //
Aart Bik6b69e0a2017-01-11 10:20:43 -0800227 // Helpers.
Aart Bikb29f6842017-07-28 15:58:41 -0700228 //
229
Aart Bikf8f5a162017-02-06 15:35:29 -0800230 bool TrySetPhiInduction(HPhi* phi, bool restrict_uses);
Aart Bikb29f6842017-07-28 15:58:41 -0700231 bool TrySetPhiReduction(HPhi* phi);
232
233 // Detects loop header with a single induction (returned in main_phi), possibly
234 // other phis for reductions, but no other side effects. Returns true on success.
235 bool TrySetSimpleLoopHeader(HBasicBlock* block, /*out*/ HPhi** main_phi);
236
Aart Bikcc42be02016-10-20 16:14:16 -0700237 bool IsEmptyBody(HBasicBlock* block);
Aart Bik482095d2016-10-10 15:39:10 -0700238 bool IsOnlyUsedAfterLoop(HLoopInformation* loop_info,
Aart Bik8c4a8542016-10-06 11:36:57 -0700239 HInstruction* instruction,
Aart Bik6b69e0a2017-01-11 10:20:43 -0800240 bool collect_loop_uses,
Aart Bik38a3f212017-10-20 17:02:21 -0700241 /*out*/ uint32_t* use_count);
Aart Bikf8f5a162017-02-06 15:35:29 -0800242 bool IsUsedOutsideLoop(HLoopInformation* loop_info,
243 HInstruction* instruction);
Nicolas Geoffray1a0a5192017-06-22 11:56:01 +0100244 bool TryReplaceWithLastValue(HLoopInformation* loop_info,
245 HInstruction* instruction,
246 HBasicBlock* block);
Aart Bikf8f5a162017-02-06 15:35:29 -0800247 bool TryAssignLastValue(HLoopInformation* loop_info,
248 HInstruction* instruction,
249 HBasicBlock* block,
250 bool collect_loop_uses);
Aart Bik6b69e0a2017-01-11 10:20:43 -0800251 void RemoveDeadInstructions(const HInstructionList& list);
Nicolas Geoffray1a0a5192017-06-22 11:56:01 +0100252 bool CanRemoveCycle(); // Whether the current 'iset_' is removable.
Aart Bik281c6812016-08-26 11:31:48 -0700253
Aart Bik92685a82017-03-06 11:13:43 -0800254 // Compiler driver (to query ISA features).
255 const CompilerDriver* compiler_driver_;
256
Aart Bik96202302016-10-04 17:33:56 -0700257 // Range information based on prior induction variable analysis.
Aart Bik281c6812016-08-26 11:31:48 -0700258 InductionVarRange induction_range_;
259
260 // Phase-local heap memory allocator for the loop optimizer. Storage obtained
Aart Bik96202302016-10-04 17:33:56 -0700261 // through this allocator is immediately released when the loop optimizer is done.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100262 ScopedArenaAllocator* loop_allocator_;
Aart Bik281c6812016-08-26 11:31:48 -0700263
Aart Bikf8f5a162017-02-06 15:35:29 -0800264 // Global heap memory allocator. Used to build HIR.
265 ArenaAllocator* global_allocator_;
266
Aart Bik96202302016-10-04 17:33:56 -0700267 // Entries into the loop hierarchy representation. The hierarchy resides
268 // in phase-local heap memory.
Aart Bik281c6812016-08-26 11:31:48 -0700269 LoopNode* top_loop_;
270 LoopNode* last_loop_;
271
Aart Bik8c4a8542016-10-06 11:36:57 -0700272 // Temporary bookkeeping of a set of instructions.
273 // Contents reside in phase-local heap memory.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100274 ScopedArenaSet<HInstruction*>* iset_;
Aart Bik8c4a8542016-10-06 11:36:57 -0700275
Aart Bikb29f6842017-07-28 15:58:41 -0700276 // Temporary bookkeeping of reduction instructions. Mapping is two-fold:
277 // (1) reductions in the loop-body are mapped back to their phi definition,
278 // (2) phi definitions are mapped to their initial value (updated during
279 // code generation to feed the proper values into the new chain).
280 // Contents reside in phase-local heap memory.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100281 ScopedArenaSafeMap<HInstruction*, HInstruction*>* reductions_;
Aart Bik482095d2016-10-10 15:39:10 -0700282
Aart Bikdf7822e2016-12-06 10:05:30 -0800283 // Flag that tracks if any simplifications have occurred.
284 bool simplified_;
285
Aart Bikf8f5a162017-02-06 15:35:29 -0800286 // Number of "lanes" for selected packed type.
287 uint32_t vector_length_;
288
289 // Set of array references in the vector loop.
290 // Contents reside in phase-local heap memory.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100291 ScopedArenaSet<ArrayReference>* vector_refs_;
Aart Bikf8f5a162017-02-06 15:35:29 -0800292
Aart Bik38a3f212017-10-20 17:02:21 -0700293 // Static or dynamic loop peeling for alignment.
294 uint32_t vector_static_peeling_factor_;
295 const ArrayReference* vector_dynamic_peeling_candidate_;
Aart Bik14a68b42017-06-08 14:06:58 -0700296
297 // Dynamic data dependence test of the form a != b.
298 HInstruction* vector_runtime_test_a_;
299 HInstruction* vector_runtime_test_b_;
300
Aart Bikf8f5a162017-02-06 15:35:29 -0800301 // Mapping used during vectorization synthesis for both the scalar peeling/cleanup
Aart Bik14a68b42017-06-08 14:06:58 -0700302 // loop (mode is kSequential) and the actual vector loop (mode is kVector). The data
Aart Bikf8f5a162017-02-06 15:35:29 -0800303 // structure maps original instructions into the new instructions.
304 // Contents reside in phase-local heap memory.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100305 ScopedArenaSafeMap<HInstruction*, HInstruction*>* vector_map_;
Aart Bikf8f5a162017-02-06 15:35:29 -0800306
Aart Bik0148de42017-09-05 09:25:01 -0700307 // Permanent mapping used during vectorization synthesis.
308 // Contents reside in phase-local heap memory.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100309 ScopedArenaSafeMap<HInstruction*, HInstruction*>* vector_permanent_map_;
Aart Bik0148de42017-09-05 09:25:01 -0700310
Aart Bikf8f5a162017-02-06 15:35:29 -0800311 // Temporary vectorization bookkeeping.
Aart Bik14a68b42017-06-08 14:06:58 -0700312 VectorMode vector_mode_; // synthesis mode
Aart Bikf8f5a162017-02-06 15:35:29 -0800313 HBasicBlock* vector_preheader_; // preheader of the new loop
314 HBasicBlock* vector_header_; // header of the new loop
315 HBasicBlock* vector_body_; // body of the new loop
Aart Bik14a68b42017-06-08 14:06:58 -0700316 HInstruction* vector_index_; // normalized index of the new loop
Aart Bikf8f5a162017-02-06 15:35:29 -0800317
Artem Serov121f2032017-10-23 19:19:06 +0100318 // Helper for target-specific behaviour for loop optimizations.
319 ArchDefaultLoopHelper* arch_loop_helper_;
320
Aart Bik281c6812016-08-26 11:31:48 -0700321 friend class LoopOptimizationTest;
322
323 DISALLOW_COPY_AND_ASSIGN(HLoopOptimization);
324};
325
326} // namespace art
327
328#endif // ART_COMPILER_OPTIMIZING_LOOP_OPTIMIZATION_H_