blob: cc6343aeb5bb5aaed40c5b7748a28ff1b2bd18fc [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,
37 HInductionVarAnalysis* induction_analysis);
Aart Bik281c6812016-08-26 11:31:48 -070038
39 void Run() OVERRIDE;
40
41 static constexpr const char* kLoopOptimizationPassName = "loop_optimization";
42
43 private:
44 /**
45 * A single loop inside the loop hierarchy representation.
46 */
Aart Bik96202302016-10-04 17:33:56 -070047 struct LoopNode : public ArenaObject<kArenaAllocLoopOptimization> {
Aart Bik281c6812016-08-26 11:31:48 -070048 explicit LoopNode(HLoopInformation* lp_info)
49 : loop_info(lp_info),
50 outer(nullptr),
51 inner(nullptr),
52 previous(nullptr),
53 next(nullptr) {}
Aart Bikf8f5a162017-02-06 15:35:29 -080054 HLoopInformation* loop_info;
Aart Bik281c6812016-08-26 11:31:48 -070055 LoopNode* outer;
56 LoopNode* inner;
57 LoopNode* previous;
58 LoopNode* next;
59 };
60
Aart Bikf8f5a162017-02-06 15:35:29 -080061 /*
62 * Vectorization restrictions (bit mask).
63 */
64 enum VectorRestrictions {
Aart Bikf3e61ee2017-04-12 17:09:20 -070065 kNone = 0, // no restrictions
66 kNoMul = 1, // no multiplication
67 kNoDiv = 2, // no division
68 kNoShift = 4, // no shift
69 kNoShr = 8, // no arithmetic shift right
70 kNoHiBits = 16, // "wider" operations cannot bring in higher order bits
71 kNoSignedHAdd = 32, // no signed halving add
72 kNoUnroundedHAdd = 64, // no unrounded halving add
73 kNoAbs = 128, // no absolute value
Aart Bikc8e93c72017-05-10 10:49:22 -070074 kNoMinMax = 256, // no min/max
Goran Jakovljevic19680d32017-05-11 10:38:36 +020075 kNoStringCharAt = 512, // no StringCharAt
Aart Bikf8f5a162017-02-06 15:35:29 -080076 };
Aart Bik96202302016-10-04 17:33:56 -070077
Aart Bikf8f5a162017-02-06 15:35:29 -080078 /*
79 * Vectorization mode during synthesis
80 * (sequential peeling/cleanup loop or vector loop).
81 */
82 enum VectorMode {
83 kSequential,
84 kVector
85 };
86
87 /*
88 * Representation of a unit-stride array reference.
89 */
90 struct ArrayReference {
91 ArrayReference(HInstruction* b, HInstruction* o, Primitive::Type t, bool l)
92 : base(b), offset(o), type(t), lhs(l) { }
93 bool operator<(const ArrayReference& other) const {
94 return
95 (base < other.base) ||
96 (base == other.base &&
97 (offset < other.offset || (offset == other.offset &&
98 (type < other.type ||
99 (type == other.type && lhs < other.lhs)))));
100 }
101 HInstruction* base; // base address
102 HInstruction* offset; // offset + i
103 Primitive::Type type; // component type
104 bool lhs; // def/use
105 };
106
107 // Loop setup and traversal.
108 void LocalRun();
Aart Bik281c6812016-08-26 11:31:48 -0700109 void AddLoop(HLoopInformation* loop_info);
110 void RemoveLoop(LoopNode* node);
Aart Bik281c6812016-08-26 11:31:48 -0700111 void TraverseLoopsInnerToOuter(LoopNode* node);
112
Aart Bikf8f5a162017-02-06 15:35:29 -0800113 // Optimization.
Aart Bik281c6812016-08-26 11:31:48 -0700114 void SimplifyInduction(LoopNode* node);
Aart Bik482095d2016-10-10 15:39:10 -0700115 void SimplifyBlocks(LoopNode* node);
Aart Bikf8f5a162017-02-06 15:35:29 -0800116 void OptimizeInnerLoop(LoopNode* node);
117
118 // Vectorization analysis and synthesis.
119 bool CanVectorize(LoopNode* node, HBasicBlock* block, int64_t trip_count);
120 void Vectorize(LoopNode* node, HBasicBlock* block, HBasicBlock* exit, int64_t trip_count);
121 void GenerateNewLoop(LoopNode* node,
122 HBasicBlock* block,
123 HBasicBlock* new_preheader,
124 HInstruction* lo,
125 HInstruction* hi,
126 HInstruction* step);
127 bool VectorizeDef(LoopNode* node, HInstruction* instruction, bool generate_code);
128 bool VectorizeUse(LoopNode* node,
129 HInstruction* instruction,
130 bool generate_code,
131 Primitive::Type type,
132 uint64_t restrictions);
133 bool TrySetVectorType(Primitive::Type type, /*out*/ uint64_t* restrictions);
134 bool TrySetVectorLength(uint32_t length);
135 void GenerateVecInv(HInstruction* org, Primitive::Type type);
136 void GenerateVecSub(HInstruction* org, HInstruction* off);
137 void GenerateVecMem(HInstruction* org,
138 HInstruction* opa,
139 HInstruction* opb,
140 Primitive::Type type);
Aart Bik304c8a52017-05-23 11:01:13 -0700141 void GenerateVecOp(HInstruction* org,
142 HInstruction* opa,
143 HInstruction* opb,
144 Primitive::Type type,
145 bool is_unsigned = false);
Aart Bik281c6812016-08-26 11:31:48 -0700146
Aart Bikf3e61ee2017-04-12 17:09:20 -0700147 // Vectorization idioms.
148 bool VectorizeHalvingAddIdiom(LoopNode* node,
149 HInstruction* instruction,
150 bool generate_code,
151 Primitive::Type type,
152 uint64_t restrictions);
153
Aart Bik6b69e0a2017-01-11 10:20:43 -0800154 // Helpers.
Aart Bikf8f5a162017-02-06 15:35:29 -0800155 bool TrySetPhiInduction(HPhi* phi, bool restrict_uses);
156 bool TrySetSimpleLoopHeader(HBasicBlock* block);
Aart Bikcc42be02016-10-20 16:14:16 -0700157 bool IsEmptyBody(HBasicBlock* block);
Aart Bik482095d2016-10-10 15:39:10 -0700158 bool IsOnlyUsedAfterLoop(HLoopInformation* loop_info,
Aart Bik8c4a8542016-10-06 11:36:57 -0700159 HInstruction* instruction,
Aart Bik6b69e0a2017-01-11 10:20:43 -0800160 bool collect_loop_uses,
Aart Bik8c4a8542016-10-06 11:36:57 -0700161 /*out*/ int32_t* use_count);
Aart Bikf8f5a162017-02-06 15:35:29 -0800162 bool IsUsedOutsideLoop(HLoopInformation* loop_info,
163 HInstruction* instruction);
Nicolas Geoffray1a0a5192017-06-22 11:56:01 +0100164 bool TryReplaceWithLastValue(HLoopInformation* loop_info,
165 HInstruction* instruction,
166 HBasicBlock* block);
Aart Bikf8f5a162017-02-06 15:35:29 -0800167 bool TryAssignLastValue(HLoopInformation* loop_info,
168 HInstruction* instruction,
169 HBasicBlock* block,
170 bool collect_loop_uses);
Aart Bik6b69e0a2017-01-11 10:20:43 -0800171 void RemoveDeadInstructions(const HInstructionList& list);
Nicolas Geoffray1a0a5192017-06-22 11:56:01 +0100172 bool CanRemoveCycle(); // Whether the current 'iset_' is removable.
Aart Bik281c6812016-08-26 11:31:48 -0700173
Aart Bik92685a82017-03-06 11:13:43 -0800174 // Compiler driver (to query ISA features).
175 const CompilerDriver* compiler_driver_;
176
Aart Bik96202302016-10-04 17:33:56 -0700177 // Range information based on prior induction variable analysis.
Aart Bik281c6812016-08-26 11:31:48 -0700178 InductionVarRange induction_range_;
179
180 // Phase-local heap memory allocator for the loop optimizer. Storage obtained
Aart Bik96202302016-10-04 17:33:56 -0700181 // through this allocator is immediately released when the loop optimizer is done.
Nicolas Geoffrayebe16742016-10-05 09:55:42 +0100182 ArenaAllocator* loop_allocator_;
Aart Bik281c6812016-08-26 11:31:48 -0700183
Aart Bikf8f5a162017-02-06 15:35:29 -0800184 // Global heap memory allocator. Used to build HIR.
185 ArenaAllocator* global_allocator_;
186
Aart Bik96202302016-10-04 17:33:56 -0700187 // Entries into the loop hierarchy representation. The hierarchy resides
188 // in phase-local heap memory.
Aart Bik281c6812016-08-26 11:31:48 -0700189 LoopNode* top_loop_;
190 LoopNode* last_loop_;
191
Aart Bik8c4a8542016-10-06 11:36:57 -0700192 // Temporary bookkeeping of a set of instructions.
193 // Contents reside in phase-local heap memory.
194 ArenaSet<HInstruction*>* iset_;
195
Aart Bik482095d2016-10-10 15:39:10 -0700196 // Counter that tracks how many induction cycles have been simplified. Useful
197 // to trigger incremental updates of induction variable analysis of outer loops
198 // when the induction of inner loops has changed.
Aart Bikf8f5a162017-02-06 15:35:29 -0800199 uint32_t induction_simplication_count_;
Aart Bik482095d2016-10-10 15:39:10 -0700200
Aart Bikdf7822e2016-12-06 10:05:30 -0800201 // Flag that tracks if any simplifications have occurred.
202 bool simplified_;
203
Aart Bikf8f5a162017-02-06 15:35:29 -0800204 // Number of "lanes" for selected packed type.
205 uint32_t vector_length_;
206
207 // Set of array references in the vector loop.
208 // Contents reside in phase-local heap memory.
209 ArenaSet<ArrayReference>* vector_refs_;
210
211 // Mapping used during vectorization synthesis for both the scalar peeling/cleanup
212 // loop (simd_ is false) and the actual vector loop (simd_ is true). The data
213 // structure maps original instructions into the new instructions.
214 // Contents reside in phase-local heap memory.
215 ArenaSafeMap<HInstruction*, HInstruction*>* vector_map_;
216
217 // Temporary vectorization bookkeeping.
218 HBasicBlock* vector_preheader_; // preheader of the new loop
219 HBasicBlock* vector_header_; // header of the new loop
220 HBasicBlock* vector_body_; // body of the new loop
221 HInstruction* vector_runtime_test_a_;
222 HInstruction* vector_runtime_test_b_; // defines a != b runtime test
223 HPhi* vector_phi_; // the Phi representing the normalized loop index
224 VectorMode vector_mode_; // selects synthesis mode
225
Aart Bik281c6812016-08-26 11:31:48 -0700226 friend class LoopOptimizationTest;
227
228 DISALLOW_COPY_AND_ASSIGN(HLoopOptimization);
229};
230
231} // namespace art
232
233#endif // ART_COMPILER_OPTIMIZING_LOOP_OPTIMIZATION_H_