blob: f34751815bbc251a34e551ccbbc74c19186ce86b [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
Aart Bik281c6812016-08-26 11:31:48 -070020#include "induction_var_range.h"
21#include "nodes.h"
22#include "optimization.h"
23
24namespace art {
25
Aart Bik92685a82017-03-06 11:13:43 -080026class CompilerDriver;
27
Aart Bik281c6812016-08-26 11:31:48 -070028/**
29 * Loop optimizations. Builds a loop hierarchy and applies optimizations to
Aart Bikf8f5a162017-02-06 15:35:29 -080030 * the detected nested loops, such as removal of dead induction and empty loops
31 * and inner loop vectorization.
Aart Bik281c6812016-08-26 11:31:48 -070032 */
33class HLoopOptimization : public HOptimization {
34 public:
Aart Bik92685a82017-03-06 11:13:43 -080035 HLoopOptimization(HGraph* graph,
36 CompilerDriver* compiler_driver,
Aart Bikb92cc332017-09-06 15:53:17 -070037 HInductionVarAnalysis* induction_analysis,
38 OptimizingCompilerStats* stats);
Aart Bik281c6812016-08-26 11:31:48 -070039
40 void Run() OVERRIDE;
41
42 static constexpr const char* kLoopOptimizationPassName = "loop_optimization";
43
44 private:
45 /**
46 * A single loop inside the loop hierarchy representation.
47 */
Aart Bik96202302016-10-04 17:33:56 -070048 struct LoopNode : public ArenaObject<kArenaAllocLoopOptimization> {
Aart Bik281c6812016-08-26 11:31:48 -070049 explicit LoopNode(HLoopInformation* lp_info)
50 : loop_info(lp_info),
51 outer(nullptr),
52 inner(nullptr),
53 previous(nullptr),
54 next(nullptr) {}
Aart Bikf8f5a162017-02-06 15:35:29 -080055 HLoopInformation* loop_info;
Aart Bik281c6812016-08-26 11:31:48 -070056 LoopNode* outer;
57 LoopNode* inner;
58 LoopNode* previous;
59 LoopNode* next;
60 };
61
Aart Bikf8f5a162017-02-06 15:35:29 -080062 /*
63 * Vectorization restrictions (bit mask).
64 */
65 enum VectorRestrictions {
Aart Bik0148de42017-09-05 09:25:01 -070066 kNone = 0, // no restrictions
67 kNoMul = 1 << 0, // no multiplication
68 kNoDiv = 1 << 1, // no division
69 kNoShift = 1 << 2, // no shift
70 kNoShr = 1 << 3, // no arithmetic shift right
71 kNoHiBits = 1 << 4, // "wider" operations cannot bring in higher order bits
72 kNoSignedHAdd = 1 << 5, // no signed halving add
73 kNoUnroundedHAdd = 1 << 6, // no unrounded halving add
74 kNoAbs = 1 << 7, // no absolute value
75 kNoMinMax = 1 << 8, // no min/max
76 kNoStringCharAt = 1 << 9, // no StringCharAt
77 kNoReduction = 1 << 10, // no reduction
Aart Bikf8f5a162017-02-06 15:35:29 -080078 };
Aart Bik96202302016-10-04 17:33:56 -070079
Aart Bikf8f5a162017-02-06 15:35:29 -080080 /*
81 * Vectorization mode during synthesis
82 * (sequential peeling/cleanup loop or vector loop).
83 */
84 enum VectorMode {
85 kSequential,
86 kVector
87 };
88
89 /*
90 * Representation of a unit-stride array reference.
91 */
92 struct ArrayReference {
93 ArrayReference(HInstruction* b, HInstruction* o, Primitive::Type t, bool l)
94 : base(b), offset(o), type(t), lhs(l) { }
95 bool operator<(const ArrayReference& other) const {
96 return
97 (base < other.base) ||
98 (base == other.base &&
99 (offset < other.offset || (offset == other.offset &&
100 (type < other.type ||
101 (type == other.type && lhs < other.lhs)))));
102 }
103 HInstruction* base; // base address
104 HInstruction* offset; // offset + i
105 Primitive::Type type; // component type
106 bool lhs; // def/use
107 };
108
Aart Bikb29f6842017-07-28 15:58:41 -0700109 //
Aart Bikf8f5a162017-02-06 15:35:29 -0800110 // Loop setup and traversal.
Aart Bikb29f6842017-07-28 15:58:41 -0700111 //
112
Aart Bikf8f5a162017-02-06 15:35:29 -0800113 void LocalRun();
Aart Bik281c6812016-08-26 11:31:48 -0700114 void AddLoop(HLoopInformation* loop_info);
115 void RemoveLoop(LoopNode* node);
Aart Bik281c6812016-08-26 11:31:48 -0700116
Aart Bikb29f6842017-07-28 15:58:41 -0700117 // Traverses all loops inner to outer to perform simplifications and optimizations.
118 // Returns true if loops nested inside current loop (node) have changed.
119 bool TraverseLoopsInnerToOuter(LoopNode* node);
120
121 //
Aart Bikf8f5a162017-02-06 15:35:29 -0800122 // Optimization.
Aart Bikb29f6842017-07-28 15:58:41 -0700123 //
124
Aart Bik281c6812016-08-26 11:31:48 -0700125 void SimplifyInduction(LoopNode* node);
Aart Bik482095d2016-10-10 15:39:10 -0700126 void SimplifyBlocks(LoopNode* node);
Aart Bikf8f5a162017-02-06 15:35:29 -0800127
Aart Bikb29f6842017-07-28 15:58:41 -0700128 // Performs optimizations specific to inner loop (empty loop removal,
129 // unrolling, vectorization). Returns true if anything changed.
130 bool OptimizeInnerLoop(LoopNode* node);
131
132 //
Aart Bikf8f5a162017-02-06 15:35:29 -0800133 // Vectorization analysis and synthesis.
Aart Bikb29f6842017-07-28 15:58:41 -0700134 //
135
Aart Bik14a68b42017-06-08 14:06:58 -0700136 bool ShouldVectorize(LoopNode* node, HBasicBlock* block, int64_t trip_count);
Aart Bikf8f5a162017-02-06 15:35:29 -0800137 void Vectorize(LoopNode* node, HBasicBlock* block, HBasicBlock* exit, int64_t trip_count);
138 void GenerateNewLoop(LoopNode* node,
139 HBasicBlock* block,
140 HBasicBlock* new_preheader,
141 HInstruction* lo,
142 HInstruction* hi,
Aart Bik14a68b42017-06-08 14:06:58 -0700143 HInstruction* step,
144 uint32_t unroll);
Aart Bikf8f5a162017-02-06 15:35:29 -0800145 bool VectorizeDef(LoopNode* node, HInstruction* instruction, bool generate_code);
146 bool VectorizeUse(LoopNode* node,
147 HInstruction* instruction,
148 bool generate_code,
149 Primitive::Type type,
150 uint64_t restrictions);
151 bool TrySetVectorType(Primitive::Type type, /*out*/ uint64_t* restrictions);
152 bool TrySetVectorLength(uint32_t length);
153 void GenerateVecInv(HInstruction* org, Primitive::Type type);
Aart Bik14a68b42017-06-08 14:06:58 -0700154 void GenerateVecSub(HInstruction* org, HInstruction* offset);
Aart Bikf8f5a162017-02-06 15:35:29 -0800155 void GenerateVecMem(HInstruction* org,
156 HInstruction* opa,
157 HInstruction* opb,
Aart Bik14a68b42017-06-08 14:06:58 -0700158 HInstruction* offset,
Aart Bikf8f5a162017-02-06 15:35:29 -0800159 Primitive::Type type);
Aart Bik0148de42017-09-05 09:25:01 -0700160 void GenerateVecReductionPhi(HPhi* phi);
161 void GenerateVecReductionPhiInputs(HPhi* phi, HInstruction* reduction);
162 HInstruction* ReduceAndExtractIfNeeded(HInstruction* instruction);
Aart Bik304c8a52017-05-23 11:01:13 -0700163 void GenerateVecOp(HInstruction* org,
164 HInstruction* opa,
165 HInstruction* opb,
166 Primitive::Type type,
167 bool is_unsigned = false);
Aart Bik281c6812016-08-26 11:31:48 -0700168
Aart Bikf3e61ee2017-04-12 17:09:20 -0700169 // Vectorization idioms.
170 bool VectorizeHalvingAddIdiom(LoopNode* node,
171 HInstruction* instruction,
172 bool generate_code,
173 Primitive::Type type,
174 uint64_t restrictions);
175
Aart Bik14a68b42017-06-08 14:06:58 -0700176 // Vectorization heuristics.
177 bool IsVectorizationProfitable(int64_t trip_count);
Aart Bikb29f6842017-07-28 15:58:41 -0700178 void SetPeelingCandidate(const ArrayReference* candidate, int64_t trip_count);
Aart Bik14a68b42017-06-08 14:06:58 -0700179 uint32_t GetUnrollingFactor(HBasicBlock* block, int64_t trip_count);
180
Aart Bikb29f6842017-07-28 15:58:41 -0700181 //
Aart Bik6b69e0a2017-01-11 10:20:43 -0800182 // Helpers.
Aart Bikb29f6842017-07-28 15:58:41 -0700183 //
184
Aart Bikf8f5a162017-02-06 15:35:29 -0800185 bool TrySetPhiInduction(HPhi* phi, bool restrict_uses);
Aart Bikb29f6842017-07-28 15:58:41 -0700186 bool TrySetPhiReduction(HPhi* phi);
187
188 // Detects loop header with a single induction (returned in main_phi), possibly
189 // other phis for reductions, but no other side effects. Returns true on success.
190 bool TrySetSimpleLoopHeader(HBasicBlock* block, /*out*/ HPhi** main_phi);
191
Aart Bikcc42be02016-10-20 16:14:16 -0700192 bool IsEmptyBody(HBasicBlock* block);
Aart Bik482095d2016-10-10 15:39:10 -0700193 bool IsOnlyUsedAfterLoop(HLoopInformation* loop_info,
Aart Bik8c4a8542016-10-06 11:36:57 -0700194 HInstruction* instruction,
Aart Bik6b69e0a2017-01-11 10:20:43 -0800195 bool collect_loop_uses,
Aart Bik8c4a8542016-10-06 11:36:57 -0700196 /*out*/ int32_t* use_count);
Aart Bikf8f5a162017-02-06 15:35:29 -0800197 bool IsUsedOutsideLoop(HLoopInformation* loop_info,
198 HInstruction* instruction);
Nicolas Geoffray1a0a5192017-06-22 11:56:01 +0100199 bool TryReplaceWithLastValue(HLoopInformation* loop_info,
200 HInstruction* instruction,
201 HBasicBlock* block);
Aart Bikf8f5a162017-02-06 15:35:29 -0800202 bool TryAssignLastValue(HLoopInformation* loop_info,
203 HInstruction* instruction,
204 HBasicBlock* block,
205 bool collect_loop_uses);
Aart Bik6b69e0a2017-01-11 10:20:43 -0800206 void RemoveDeadInstructions(const HInstructionList& list);
Nicolas Geoffray1a0a5192017-06-22 11:56:01 +0100207 bool CanRemoveCycle(); // Whether the current 'iset_' is removable.
Aart Bik281c6812016-08-26 11:31:48 -0700208
Aart Bik92685a82017-03-06 11:13:43 -0800209 // Compiler driver (to query ISA features).
210 const CompilerDriver* compiler_driver_;
211
Aart Bik96202302016-10-04 17:33:56 -0700212 // Range information based on prior induction variable analysis.
Aart Bik281c6812016-08-26 11:31:48 -0700213 InductionVarRange induction_range_;
214
215 // Phase-local heap memory allocator for the loop optimizer. Storage obtained
Aart Bik96202302016-10-04 17:33:56 -0700216 // through this allocator is immediately released when the loop optimizer is done.
Nicolas Geoffrayebe16742016-10-05 09:55:42 +0100217 ArenaAllocator* loop_allocator_;
Aart Bik281c6812016-08-26 11:31:48 -0700218
Aart Bikf8f5a162017-02-06 15:35:29 -0800219 // Global heap memory allocator. Used to build HIR.
220 ArenaAllocator* global_allocator_;
221
Aart Bik96202302016-10-04 17:33:56 -0700222 // Entries into the loop hierarchy representation. The hierarchy resides
223 // in phase-local heap memory.
Aart Bik281c6812016-08-26 11:31:48 -0700224 LoopNode* top_loop_;
225 LoopNode* last_loop_;
226
Aart Bik8c4a8542016-10-06 11:36:57 -0700227 // Temporary bookkeeping of a set of instructions.
228 // Contents reside in phase-local heap memory.
229 ArenaSet<HInstruction*>* iset_;
230
Aart Bikb29f6842017-07-28 15:58:41 -0700231 // Temporary bookkeeping of reduction instructions. Mapping is two-fold:
232 // (1) reductions in the loop-body are mapped back to their phi definition,
233 // (2) phi definitions are mapped to their initial value (updated during
234 // code generation to feed the proper values into the new chain).
235 // Contents reside in phase-local heap memory.
236 ArenaSafeMap<HInstruction*, HInstruction*>* reductions_;
Aart Bik482095d2016-10-10 15:39:10 -0700237
Aart Bikdf7822e2016-12-06 10:05:30 -0800238 // Flag that tracks if any simplifications have occurred.
239 bool simplified_;
240
Aart Bikf8f5a162017-02-06 15:35:29 -0800241 // Number of "lanes" for selected packed type.
242 uint32_t vector_length_;
243
244 // Set of array references in the vector loop.
245 // Contents reside in phase-local heap memory.
246 ArenaSet<ArrayReference>* vector_refs_;
247
Aart Bik14a68b42017-06-08 14:06:58 -0700248 // Dynamic loop peeling candidate for alignment.
249 const ArrayReference* vector_peeling_candidate_;
250
251 // Dynamic data dependence test of the form a != b.
252 HInstruction* vector_runtime_test_a_;
253 HInstruction* vector_runtime_test_b_;
254
Aart Bikf8f5a162017-02-06 15:35:29 -0800255 // Mapping used during vectorization synthesis for both the scalar peeling/cleanup
Aart Bik14a68b42017-06-08 14:06:58 -0700256 // loop (mode is kSequential) and the actual vector loop (mode is kVector). The data
Aart Bikf8f5a162017-02-06 15:35:29 -0800257 // structure maps original instructions into the new instructions.
258 // Contents reside in phase-local heap memory.
259 ArenaSafeMap<HInstruction*, HInstruction*>* vector_map_;
260
Aart Bik0148de42017-09-05 09:25:01 -0700261 // Permanent mapping used during vectorization synthesis.
262 // Contents reside in phase-local heap memory.
263 ArenaSafeMap<HInstruction*, HInstruction*>* vector_permanent_map_;
264
Aart Bikf8f5a162017-02-06 15:35:29 -0800265 // Temporary vectorization bookkeeping.
Aart Bik14a68b42017-06-08 14:06:58 -0700266 VectorMode vector_mode_; // synthesis mode
Aart Bikf8f5a162017-02-06 15:35:29 -0800267 HBasicBlock* vector_preheader_; // preheader of the new loop
268 HBasicBlock* vector_header_; // header of the new loop
269 HBasicBlock* vector_body_; // body of the new loop
Aart Bik14a68b42017-06-08 14:06:58 -0700270 HInstruction* vector_index_; // normalized index of the new loop
Aart Bikf8f5a162017-02-06 15:35:29 -0800271
Aart Bik281c6812016-08-26 11:31:48 -0700272 friend class LoopOptimizationTest;
273
274 DISALLOW_COPY_AND_ASSIGN(HLoopOptimization);
275};
276
277} // namespace art
278
279#endif // ART_COMPILER_OPTIMIZING_LOOP_OPTIMIZATION_H_