blob: 51e0a986b826c49c93e3d693585454663b920a5b [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"
23#include "nodes.h"
24#include "optimization.h"
25
26namespace art {
27
Aart Bik92685a82017-03-06 11:13:43 -080028class CompilerDriver;
29
Aart Bik281c6812016-08-26 11:31:48 -070030/**
31 * Loop optimizations. Builds a loop hierarchy and applies optimizations to
Aart Bikf8f5a162017-02-06 15:35:29 -080032 * the detected nested loops, such as removal of dead induction and empty loops
33 * and inner loop vectorization.
Aart Bik281c6812016-08-26 11:31:48 -070034 */
35class HLoopOptimization : public HOptimization {
36 public:
Aart Bik92685a82017-03-06 11:13:43 -080037 HLoopOptimization(HGraph* graph,
38 CompilerDriver* compiler_driver,
Aart Bikb92cc332017-09-06 15:53:17 -070039 HInductionVarAnalysis* induction_analysis,
40 OptimizingCompilerStats* stats);
Aart Bik281c6812016-08-26 11:31:48 -070041
42 void Run() OVERRIDE;
43
44 static constexpr const char* kLoopOptimizationPassName = "loop_optimization";
45
46 private:
47 /**
48 * A single loop inside the loop hierarchy representation.
49 */
Aart Bik96202302016-10-04 17:33:56 -070050 struct LoopNode : public ArenaObject<kArenaAllocLoopOptimization> {
Aart Bik281c6812016-08-26 11:31:48 -070051 explicit LoopNode(HLoopInformation* lp_info)
52 : loop_info(lp_info),
53 outer(nullptr),
54 inner(nullptr),
55 previous(nullptr),
56 next(nullptr) {}
Aart Bikf8f5a162017-02-06 15:35:29 -080057 HLoopInformation* loop_info;
Aart Bik281c6812016-08-26 11:31:48 -070058 LoopNode* outer;
59 LoopNode* inner;
60 LoopNode* previous;
61 LoopNode* next;
62 };
63
Aart Bikf8f5a162017-02-06 15:35:29 -080064 /*
65 * Vectorization restrictions (bit mask).
66 */
67 enum VectorRestrictions {
Aart Bik0148de42017-09-05 09:25:01 -070068 kNone = 0, // no restrictions
69 kNoMul = 1 << 0, // no multiplication
70 kNoDiv = 1 << 1, // no division
71 kNoShift = 1 << 2, // no shift
72 kNoShr = 1 << 3, // no arithmetic shift right
73 kNoHiBits = 1 << 4, // "wider" operations cannot bring in higher order bits
74 kNoSignedHAdd = 1 << 5, // no signed halving add
75 kNoUnroundedHAdd = 1 << 6, // no unrounded halving add
76 kNoAbs = 1 << 7, // no absolute value
77 kNoMinMax = 1 << 8, // no min/max
78 kNoStringCharAt = 1 << 9, // no StringCharAt
79 kNoReduction = 1 << 10, // no reduction
Aart Bikdbbac8f2017-09-01 13:06:08 -070080 kNoSAD = 1 << 11, // no sum of absolute differences (SAD)
Artem Serov6e9b1372017-10-05 16:48:30 +010081 kNoWideSAD = 1 << 12, // no sum of absolute differences (SAD) with operand widening
Aart Bikf8f5a162017-02-06 15:35:29 -080082 };
Aart Bik96202302016-10-04 17:33:56 -070083
Aart Bikf8f5a162017-02-06 15:35:29 -080084 /*
85 * Vectorization mode during synthesis
86 * (sequential peeling/cleanup loop or vector loop).
87 */
88 enum VectorMode {
89 kSequential,
90 kVector
91 };
92
93 /*
94 * Representation of a unit-stride array reference.
95 */
96 struct ArrayReference {
Aart Bik38a3f212017-10-20 17:02:21 -070097 ArrayReference(HInstruction* b, HInstruction* o, DataType::Type t, bool l, bool c = false)
98 : base(b), offset(o), type(t), lhs(l), is_string_char_at(c) { }
Aart Bikf8f5a162017-02-06 15:35:29 -080099 bool operator<(const ArrayReference& other) const {
100 return
101 (base < other.base) ||
102 (base == other.base &&
103 (offset < other.offset || (offset == other.offset &&
104 (type < other.type ||
Aart Bik38a3f212017-10-20 17:02:21 -0700105 (type == other.type &&
106 (lhs < other.lhs ||
107 (lhs == other.lhs &&
108 is_string_char_at < other.is_string_char_at)))))));
Aart Bikf8f5a162017-02-06 15:35:29 -0800109 }
Aart Bik38a3f212017-10-20 17:02:21 -0700110 HInstruction* base; // base address
111 HInstruction* offset; // offset + i
112 DataType::Type type; // component type
113 bool lhs; // def/use
114 bool is_string_char_at; // compressed string read
Aart Bikf8f5a162017-02-06 15:35:29 -0800115 };
116
Aart Bikb29f6842017-07-28 15:58:41 -0700117 //
Aart Bikf8f5a162017-02-06 15:35:29 -0800118 // Loop setup and traversal.
Aart Bikb29f6842017-07-28 15:58:41 -0700119 //
120
Aart Bikf8f5a162017-02-06 15:35:29 -0800121 void LocalRun();
Aart Bik281c6812016-08-26 11:31:48 -0700122 void AddLoop(HLoopInformation* loop_info);
123 void RemoveLoop(LoopNode* node);
Aart Bik281c6812016-08-26 11:31:48 -0700124
Aart Bikb29f6842017-07-28 15:58:41 -0700125 // Traverses all loops inner to outer to perform simplifications and optimizations.
126 // Returns true if loops nested inside current loop (node) have changed.
127 bool TraverseLoopsInnerToOuter(LoopNode* node);
128
129 //
Aart Bikf8f5a162017-02-06 15:35:29 -0800130 // Optimization.
Aart Bikb29f6842017-07-28 15:58:41 -0700131 //
132
Aart Bik281c6812016-08-26 11:31:48 -0700133 void SimplifyInduction(LoopNode* node);
Aart Bik482095d2016-10-10 15:39:10 -0700134 void SimplifyBlocks(LoopNode* node);
Aart Bikf8f5a162017-02-06 15:35:29 -0800135
Aart Bikb29f6842017-07-28 15:58:41 -0700136 // Performs optimizations specific to inner loop (empty loop removal,
137 // unrolling, vectorization). Returns true if anything changed.
138 bool OptimizeInnerLoop(LoopNode* node);
139
140 //
Aart Bikf8f5a162017-02-06 15:35:29 -0800141 // Vectorization analysis and synthesis.
Aart Bikb29f6842017-07-28 15:58:41 -0700142 //
143
Aart Bik14a68b42017-06-08 14:06:58 -0700144 bool ShouldVectorize(LoopNode* node, HBasicBlock* block, int64_t trip_count);
Aart Bikf8f5a162017-02-06 15:35:29 -0800145 void Vectorize(LoopNode* node, HBasicBlock* block, HBasicBlock* exit, int64_t trip_count);
146 void GenerateNewLoop(LoopNode* node,
147 HBasicBlock* block,
148 HBasicBlock* new_preheader,
149 HInstruction* lo,
150 HInstruction* hi,
Aart Bik14a68b42017-06-08 14:06:58 -0700151 HInstruction* step,
152 uint32_t unroll);
Aart Bikf8f5a162017-02-06 15:35:29 -0800153 bool VectorizeDef(LoopNode* node, HInstruction* instruction, bool generate_code);
154 bool VectorizeUse(LoopNode* node,
155 HInstruction* instruction,
156 bool generate_code,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100157 DataType::Type type,
Aart Bikf8f5a162017-02-06 15:35:29 -0800158 uint64_t restrictions);
Aart Bik38a3f212017-10-20 17:02:21 -0700159 uint32_t GetVectorSizeInBytes();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100160 bool TrySetVectorType(DataType::Type type, /*out*/ uint64_t* restrictions);
Aart Bikf8f5a162017-02-06 15:35:29 -0800161 bool TrySetVectorLength(uint32_t length);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100162 void GenerateVecInv(HInstruction* org, DataType::Type type);
Aart Bik14a68b42017-06-08 14:06:58 -0700163 void GenerateVecSub(HInstruction* org, HInstruction* offset);
Aart Bikf8f5a162017-02-06 15:35:29 -0800164 void GenerateVecMem(HInstruction* org,
165 HInstruction* opa,
166 HInstruction* opb,
Aart Bik14a68b42017-06-08 14:06:58 -0700167 HInstruction* offset,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100168 DataType::Type type);
Aart Bik0148de42017-09-05 09:25:01 -0700169 void GenerateVecReductionPhi(HPhi* phi);
170 void GenerateVecReductionPhiInputs(HPhi* phi, HInstruction* reduction);
171 HInstruction* ReduceAndExtractIfNeeded(HInstruction* instruction);
Aart Bik304c8a52017-05-23 11:01:13 -0700172 void GenerateVecOp(HInstruction* org,
173 HInstruction* opa,
174 HInstruction* opb,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100175 DataType::Type type,
Aart Bik304c8a52017-05-23 11:01:13 -0700176 bool is_unsigned = false);
Aart Bik281c6812016-08-26 11:31:48 -0700177
Aart Bikf3e61ee2017-04-12 17:09:20 -0700178 // Vectorization idioms.
179 bool VectorizeHalvingAddIdiom(LoopNode* node,
180 HInstruction* instruction,
181 bool generate_code,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100182 DataType::Type type,
Aart Bikf3e61ee2017-04-12 17:09:20 -0700183 uint64_t restrictions);
Aart Bikdbbac8f2017-09-01 13:06:08 -0700184 bool VectorizeSADIdiom(LoopNode* node,
185 HInstruction* instruction,
186 bool generate_code,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100187 DataType::Type type,
Aart Bikdbbac8f2017-09-01 13:06:08 -0700188 uint64_t restrictions);
Aart Bikf3e61ee2017-04-12 17:09:20 -0700189
Aart Bik14a68b42017-06-08 14:06:58 -0700190 // Vectorization heuristics.
Aart Bik38a3f212017-10-20 17:02:21 -0700191 Alignment ComputeAlignment(HInstruction* offset,
192 DataType::Type type,
193 bool is_string_char_at,
194 uint32_t peeling = 0);
195 void SetAlignmentStrategy(uint32_t peeling_votes[],
196 const ArrayReference* peeling_candidate);
197 uint32_t MaxNumberPeeled();
Aart Bik14a68b42017-06-08 14:06:58 -0700198 bool IsVectorizationProfitable(int64_t trip_count);
Aart Bik14a68b42017-06-08 14:06:58 -0700199 uint32_t GetUnrollingFactor(HBasicBlock* block, int64_t trip_count);
200
Aart Bikb29f6842017-07-28 15:58:41 -0700201 //
Aart Bik6b69e0a2017-01-11 10:20:43 -0800202 // Helpers.
Aart Bikb29f6842017-07-28 15:58:41 -0700203 //
204
Aart Bikf8f5a162017-02-06 15:35:29 -0800205 bool TrySetPhiInduction(HPhi* phi, bool restrict_uses);
Aart Bikb29f6842017-07-28 15:58:41 -0700206 bool TrySetPhiReduction(HPhi* phi);
207
208 // Detects loop header with a single induction (returned in main_phi), possibly
209 // other phis for reductions, but no other side effects. Returns true on success.
210 bool TrySetSimpleLoopHeader(HBasicBlock* block, /*out*/ HPhi** main_phi);
211
Aart Bikcc42be02016-10-20 16:14:16 -0700212 bool IsEmptyBody(HBasicBlock* block);
Aart Bik482095d2016-10-10 15:39:10 -0700213 bool IsOnlyUsedAfterLoop(HLoopInformation* loop_info,
Aart Bik8c4a8542016-10-06 11:36:57 -0700214 HInstruction* instruction,
Aart Bik6b69e0a2017-01-11 10:20:43 -0800215 bool collect_loop_uses,
Aart Bik38a3f212017-10-20 17:02:21 -0700216 /*out*/ uint32_t* use_count);
Aart Bikf8f5a162017-02-06 15:35:29 -0800217 bool IsUsedOutsideLoop(HLoopInformation* loop_info,
218 HInstruction* instruction);
Nicolas Geoffray1a0a5192017-06-22 11:56:01 +0100219 bool TryReplaceWithLastValue(HLoopInformation* loop_info,
220 HInstruction* instruction,
221 HBasicBlock* block);
Aart Bikf8f5a162017-02-06 15:35:29 -0800222 bool TryAssignLastValue(HLoopInformation* loop_info,
223 HInstruction* instruction,
224 HBasicBlock* block,
225 bool collect_loop_uses);
Aart Bik6b69e0a2017-01-11 10:20:43 -0800226 void RemoveDeadInstructions(const HInstructionList& list);
Nicolas Geoffray1a0a5192017-06-22 11:56:01 +0100227 bool CanRemoveCycle(); // Whether the current 'iset_' is removable.
Aart Bik281c6812016-08-26 11:31:48 -0700228
Aart Bik92685a82017-03-06 11:13:43 -0800229 // Compiler driver (to query ISA features).
230 const CompilerDriver* compiler_driver_;
231
Aart Bik96202302016-10-04 17:33:56 -0700232 // Range information based on prior induction variable analysis.
Aart Bik281c6812016-08-26 11:31:48 -0700233 InductionVarRange induction_range_;
234
235 // Phase-local heap memory allocator for the loop optimizer. Storage obtained
Aart Bik96202302016-10-04 17:33:56 -0700236 // through this allocator is immediately released when the loop optimizer is done.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100237 ScopedArenaAllocator* loop_allocator_;
Aart Bik281c6812016-08-26 11:31:48 -0700238
Aart Bikf8f5a162017-02-06 15:35:29 -0800239 // Global heap memory allocator. Used to build HIR.
240 ArenaAllocator* global_allocator_;
241
Aart Bik96202302016-10-04 17:33:56 -0700242 // Entries into the loop hierarchy representation. The hierarchy resides
243 // in phase-local heap memory.
Aart Bik281c6812016-08-26 11:31:48 -0700244 LoopNode* top_loop_;
245 LoopNode* last_loop_;
246
Aart Bik8c4a8542016-10-06 11:36:57 -0700247 // Temporary bookkeeping of a set of instructions.
248 // Contents reside in phase-local heap memory.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100249 ScopedArenaSet<HInstruction*>* iset_;
Aart Bik8c4a8542016-10-06 11:36:57 -0700250
Aart Bikb29f6842017-07-28 15:58:41 -0700251 // Temporary bookkeeping of reduction instructions. Mapping is two-fold:
252 // (1) reductions in the loop-body are mapped back to their phi definition,
253 // (2) phi definitions are mapped to their initial value (updated during
254 // code generation to feed the proper values into the new chain).
255 // Contents reside in phase-local heap memory.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100256 ScopedArenaSafeMap<HInstruction*, HInstruction*>* reductions_;
Aart Bik482095d2016-10-10 15:39:10 -0700257
Aart Bikdf7822e2016-12-06 10:05:30 -0800258 // Flag that tracks if any simplifications have occurred.
259 bool simplified_;
260
Aart Bikf8f5a162017-02-06 15:35:29 -0800261 // Number of "lanes" for selected packed type.
262 uint32_t vector_length_;
263
264 // Set of array references in the vector loop.
265 // Contents reside in phase-local heap memory.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100266 ScopedArenaSet<ArrayReference>* vector_refs_;
Aart Bikf8f5a162017-02-06 15:35:29 -0800267
Aart Bik38a3f212017-10-20 17:02:21 -0700268 // Static or dynamic loop peeling for alignment.
269 uint32_t vector_static_peeling_factor_;
270 const ArrayReference* vector_dynamic_peeling_candidate_;
Aart Bik14a68b42017-06-08 14:06:58 -0700271
272 // Dynamic data dependence test of the form a != b.
273 HInstruction* vector_runtime_test_a_;
274 HInstruction* vector_runtime_test_b_;
275
Aart Bikf8f5a162017-02-06 15:35:29 -0800276 // Mapping used during vectorization synthesis for both the scalar peeling/cleanup
Aart Bik14a68b42017-06-08 14:06:58 -0700277 // loop (mode is kSequential) and the actual vector loop (mode is kVector). The data
Aart Bikf8f5a162017-02-06 15:35:29 -0800278 // structure maps original instructions into the new instructions.
279 // Contents reside in phase-local heap memory.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100280 ScopedArenaSafeMap<HInstruction*, HInstruction*>* vector_map_;
Aart Bikf8f5a162017-02-06 15:35:29 -0800281
Aart Bik0148de42017-09-05 09:25:01 -0700282 // Permanent mapping used during vectorization synthesis.
283 // Contents reside in phase-local heap memory.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100284 ScopedArenaSafeMap<HInstruction*, HInstruction*>* vector_permanent_map_;
Aart Bik0148de42017-09-05 09:25:01 -0700285
Aart Bikf8f5a162017-02-06 15:35:29 -0800286 // Temporary vectorization bookkeeping.
Aart Bik14a68b42017-06-08 14:06:58 -0700287 VectorMode vector_mode_; // synthesis mode
Aart Bikf8f5a162017-02-06 15:35:29 -0800288 HBasicBlock* vector_preheader_; // preheader of the new loop
289 HBasicBlock* vector_header_; // header of the new loop
290 HBasicBlock* vector_body_; // body of the new loop
Aart Bik14a68b42017-06-08 14:06:58 -0700291 HInstruction* vector_index_; // normalized index of the new loop
Aart Bikf8f5a162017-02-06 15:35:29 -0800292
Aart Bik281c6812016-08-26 11:31:48 -0700293 friend class LoopOptimizationTest;
294
295 DISALLOW_COPY_AND_ASSIGN(HLoopOptimization);
296};
297
298} // namespace art
299
300#endif // ART_COMPILER_OPTIMIZING_LOOP_OPTIMIZATION_H_