Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 1 | /* |
| 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 Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 20 | #include "induction_var_range.h" |
| 21 | #include "nodes.h" |
| 22 | #include "optimization.h" |
| 23 | |
| 24 | namespace art { |
| 25 | |
Aart Bik | 92685a8 | 2017-03-06 11:13:43 -0800 | [diff] [blame] | 26 | class CompilerDriver; |
| 27 | |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 28 | /** |
| 29 | * Loop optimizations. Builds a loop hierarchy and applies optimizations to |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 30 | * the detected nested loops, such as removal of dead induction and empty loops |
| 31 | * and inner loop vectorization. |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 32 | */ |
| 33 | class HLoopOptimization : public HOptimization { |
| 34 | public: |
Aart Bik | 92685a8 | 2017-03-06 11:13:43 -0800 | [diff] [blame] | 35 | HLoopOptimization(HGraph* graph, |
| 36 | CompilerDriver* compiler_driver, |
| 37 | HInductionVarAnalysis* induction_analysis); |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 38 | |
| 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 Bik | 9620230 | 2016-10-04 17:33:56 -0700 | [diff] [blame] | 47 | struct LoopNode : public ArenaObject<kArenaAllocLoopOptimization> { |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 48 | explicit LoopNode(HLoopInformation* lp_info) |
| 49 | : loop_info(lp_info), |
| 50 | outer(nullptr), |
| 51 | inner(nullptr), |
| 52 | previous(nullptr), |
| 53 | next(nullptr) {} |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 54 | HLoopInformation* loop_info; |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 55 | LoopNode* outer; |
| 56 | LoopNode* inner; |
| 57 | LoopNode* previous; |
| 58 | LoopNode* next; |
| 59 | }; |
| 60 | |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 61 | /* |
| 62 | * Vectorization restrictions (bit mask). |
| 63 | */ |
| 64 | enum VectorRestrictions { |
Aart Bik | 0148de4 | 2017-09-05 09:25:01 -0700 | [diff] [blame] | 65 | kNone = 0, // no restrictions |
| 66 | kNoMul = 1 << 0, // no multiplication |
| 67 | kNoDiv = 1 << 1, // no division |
| 68 | kNoShift = 1 << 2, // no shift |
| 69 | kNoShr = 1 << 3, // no arithmetic shift right |
| 70 | kNoHiBits = 1 << 4, // "wider" operations cannot bring in higher order bits |
| 71 | kNoSignedHAdd = 1 << 5, // no signed halving add |
| 72 | kNoUnroundedHAdd = 1 << 6, // no unrounded halving add |
| 73 | kNoAbs = 1 << 7, // no absolute value |
| 74 | kNoMinMax = 1 << 8, // no min/max |
| 75 | kNoStringCharAt = 1 << 9, // no StringCharAt |
| 76 | kNoReduction = 1 << 10, // no reduction |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 77 | }; |
Aart Bik | 9620230 | 2016-10-04 17:33:56 -0700 | [diff] [blame] | 78 | |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 79 | /* |
| 80 | * Vectorization mode during synthesis |
| 81 | * (sequential peeling/cleanup loop or vector loop). |
| 82 | */ |
| 83 | enum VectorMode { |
| 84 | kSequential, |
| 85 | kVector |
| 86 | }; |
| 87 | |
| 88 | /* |
| 89 | * Representation of a unit-stride array reference. |
| 90 | */ |
| 91 | struct ArrayReference { |
| 92 | ArrayReference(HInstruction* b, HInstruction* o, Primitive::Type t, bool l) |
| 93 | : base(b), offset(o), type(t), lhs(l) { } |
| 94 | bool operator<(const ArrayReference& other) const { |
| 95 | return |
| 96 | (base < other.base) || |
| 97 | (base == other.base && |
| 98 | (offset < other.offset || (offset == other.offset && |
| 99 | (type < other.type || |
| 100 | (type == other.type && lhs < other.lhs))))); |
| 101 | } |
| 102 | HInstruction* base; // base address |
| 103 | HInstruction* offset; // offset + i |
| 104 | Primitive::Type type; // component type |
| 105 | bool lhs; // def/use |
| 106 | }; |
| 107 | |
Aart Bik | b29f684 | 2017-07-28 15:58:41 -0700 | [diff] [blame] | 108 | // |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 109 | // Loop setup and traversal. |
Aart Bik | b29f684 | 2017-07-28 15:58:41 -0700 | [diff] [blame] | 110 | // |
| 111 | |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 112 | void LocalRun(); |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 113 | void AddLoop(HLoopInformation* loop_info); |
| 114 | void RemoveLoop(LoopNode* node); |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 115 | |
Aart Bik | b29f684 | 2017-07-28 15:58:41 -0700 | [diff] [blame] | 116 | // Traverses all loops inner to outer to perform simplifications and optimizations. |
| 117 | // Returns true if loops nested inside current loop (node) have changed. |
| 118 | bool TraverseLoopsInnerToOuter(LoopNode* node); |
| 119 | |
| 120 | // |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 121 | // Optimization. |
Aart Bik | b29f684 | 2017-07-28 15:58:41 -0700 | [diff] [blame] | 122 | // |
| 123 | |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 124 | void SimplifyInduction(LoopNode* node); |
Aart Bik | 482095d | 2016-10-10 15:39:10 -0700 | [diff] [blame] | 125 | void SimplifyBlocks(LoopNode* node); |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 126 | |
Aart Bik | b29f684 | 2017-07-28 15:58:41 -0700 | [diff] [blame] | 127 | // Performs optimizations specific to inner loop (empty loop removal, |
| 128 | // unrolling, vectorization). Returns true if anything changed. |
| 129 | bool OptimizeInnerLoop(LoopNode* node); |
| 130 | |
| 131 | // |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 132 | // Vectorization analysis and synthesis. |
Aart Bik | b29f684 | 2017-07-28 15:58:41 -0700 | [diff] [blame] | 133 | // |
| 134 | |
Aart Bik | 14a68b4 | 2017-06-08 14:06:58 -0700 | [diff] [blame] | 135 | bool ShouldVectorize(LoopNode* node, HBasicBlock* block, int64_t trip_count); |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 136 | void Vectorize(LoopNode* node, HBasicBlock* block, HBasicBlock* exit, int64_t trip_count); |
| 137 | void GenerateNewLoop(LoopNode* node, |
| 138 | HBasicBlock* block, |
| 139 | HBasicBlock* new_preheader, |
| 140 | HInstruction* lo, |
| 141 | HInstruction* hi, |
Aart Bik | 14a68b4 | 2017-06-08 14:06:58 -0700 | [diff] [blame] | 142 | HInstruction* step, |
| 143 | uint32_t unroll); |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 144 | bool VectorizeDef(LoopNode* node, HInstruction* instruction, bool generate_code); |
| 145 | bool VectorizeUse(LoopNode* node, |
| 146 | HInstruction* instruction, |
| 147 | bool generate_code, |
| 148 | Primitive::Type type, |
| 149 | uint64_t restrictions); |
| 150 | bool TrySetVectorType(Primitive::Type type, /*out*/ uint64_t* restrictions); |
| 151 | bool TrySetVectorLength(uint32_t length); |
| 152 | void GenerateVecInv(HInstruction* org, Primitive::Type type); |
Aart Bik | 14a68b4 | 2017-06-08 14:06:58 -0700 | [diff] [blame] | 153 | void GenerateVecSub(HInstruction* org, HInstruction* offset); |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 154 | void GenerateVecMem(HInstruction* org, |
| 155 | HInstruction* opa, |
| 156 | HInstruction* opb, |
Aart Bik | 14a68b4 | 2017-06-08 14:06:58 -0700 | [diff] [blame] | 157 | HInstruction* offset, |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 158 | Primitive::Type type); |
Aart Bik | 0148de4 | 2017-09-05 09:25:01 -0700 | [diff] [blame] | 159 | void GenerateVecReductionPhi(HPhi* phi); |
| 160 | void GenerateVecReductionPhiInputs(HPhi* phi, HInstruction* reduction); |
| 161 | HInstruction* ReduceAndExtractIfNeeded(HInstruction* instruction); |
Aart Bik | 304c8a5 | 2017-05-23 11:01:13 -0700 | [diff] [blame] | 162 | void GenerateVecOp(HInstruction* org, |
| 163 | HInstruction* opa, |
| 164 | HInstruction* opb, |
| 165 | Primitive::Type type, |
| 166 | bool is_unsigned = false); |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 167 | |
Aart Bik | f3e61ee | 2017-04-12 17:09:20 -0700 | [diff] [blame] | 168 | // Vectorization idioms. |
| 169 | bool VectorizeHalvingAddIdiom(LoopNode* node, |
| 170 | HInstruction* instruction, |
| 171 | bool generate_code, |
| 172 | Primitive::Type type, |
| 173 | uint64_t restrictions); |
| 174 | |
Aart Bik | 14a68b4 | 2017-06-08 14:06:58 -0700 | [diff] [blame] | 175 | // Vectorization heuristics. |
| 176 | bool IsVectorizationProfitable(int64_t trip_count); |
Aart Bik | b29f684 | 2017-07-28 15:58:41 -0700 | [diff] [blame] | 177 | void SetPeelingCandidate(const ArrayReference* candidate, int64_t trip_count); |
Aart Bik | 14a68b4 | 2017-06-08 14:06:58 -0700 | [diff] [blame] | 178 | uint32_t GetUnrollingFactor(HBasicBlock* block, int64_t trip_count); |
| 179 | |
Aart Bik | b29f684 | 2017-07-28 15:58:41 -0700 | [diff] [blame] | 180 | // |
Aart Bik | 6b69e0a | 2017-01-11 10:20:43 -0800 | [diff] [blame] | 181 | // Helpers. |
Aart Bik | b29f684 | 2017-07-28 15:58:41 -0700 | [diff] [blame] | 182 | // |
| 183 | |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 184 | bool TrySetPhiInduction(HPhi* phi, bool restrict_uses); |
Aart Bik | b29f684 | 2017-07-28 15:58:41 -0700 | [diff] [blame] | 185 | bool TrySetPhiReduction(HPhi* phi); |
| 186 | |
| 187 | // Detects loop header with a single induction (returned in main_phi), possibly |
| 188 | // other phis for reductions, but no other side effects. Returns true on success. |
| 189 | bool TrySetSimpleLoopHeader(HBasicBlock* block, /*out*/ HPhi** main_phi); |
| 190 | |
Aart Bik | cc42be0 | 2016-10-20 16:14:16 -0700 | [diff] [blame] | 191 | bool IsEmptyBody(HBasicBlock* block); |
Aart Bik | 482095d | 2016-10-10 15:39:10 -0700 | [diff] [blame] | 192 | bool IsOnlyUsedAfterLoop(HLoopInformation* loop_info, |
Aart Bik | 8c4a854 | 2016-10-06 11:36:57 -0700 | [diff] [blame] | 193 | HInstruction* instruction, |
Aart Bik | 6b69e0a | 2017-01-11 10:20:43 -0800 | [diff] [blame] | 194 | bool collect_loop_uses, |
Aart Bik | 8c4a854 | 2016-10-06 11:36:57 -0700 | [diff] [blame] | 195 | /*out*/ int32_t* use_count); |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 196 | bool IsUsedOutsideLoop(HLoopInformation* loop_info, |
| 197 | HInstruction* instruction); |
Nicolas Geoffray | 1a0a519 | 2017-06-22 11:56:01 +0100 | [diff] [blame] | 198 | bool TryReplaceWithLastValue(HLoopInformation* loop_info, |
| 199 | HInstruction* instruction, |
| 200 | HBasicBlock* block); |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 201 | bool TryAssignLastValue(HLoopInformation* loop_info, |
| 202 | HInstruction* instruction, |
| 203 | HBasicBlock* block, |
| 204 | bool collect_loop_uses); |
Aart Bik | 6b69e0a | 2017-01-11 10:20:43 -0800 | [diff] [blame] | 205 | void RemoveDeadInstructions(const HInstructionList& list); |
Nicolas Geoffray | 1a0a519 | 2017-06-22 11:56:01 +0100 | [diff] [blame] | 206 | bool CanRemoveCycle(); // Whether the current 'iset_' is removable. |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 207 | |
Aart Bik | 92685a8 | 2017-03-06 11:13:43 -0800 | [diff] [blame] | 208 | // Compiler driver (to query ISA features). |
| 209 | const CompilerDriver* compiler_driver_; |
| 210 | |
Aart Bik | 9620230 | 2016-10-04 17:33:56 -0700 | [diff] [blame] | 211 | // Range information based on prior induction variable analysis. |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 212 | InductionVarRange induction_range_; |
| 213 | |
| 214 | // Phase-local heap memory allocator for the loop optimizer. Storage obtained |
Aart Bik | 9620230 | 2016-10-04 17:33:56 -0700 | [diff] [blame] | 215 | // through this allocator is immediately released when the loop optimizer is done. |
Nicolas Geoffray | ebe1674 | 2016-10-05 09:55:42 +0100 | [diff] [blame] | 216 | ArenaAllocator* loop_allocator_; |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 217 | |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 218 | // Global heap memory allocator. Used to build HIR. |
| 219 | ArenaAllocator* global_allocator_; |
| 220 | |
Aart Bik | 9620230 | 2016-10-04 17:33:56 -0700 | [diff] [blame] | 221 | // Entries into the loop hierarchy representation. The hierarchy resides |
| 222 | // in phase-local heap memory. |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 223 | LoopNode* top_loop_; |
| 224 | LoopNode* last_loop_; |
| 225 | |
Aart Bik | 8c4a854 | 2016-10-06 11:36:57 -0700 | [diff] [blame] | 226 | // Temporary bookkeeping of a set of instructions. |
| 227 | // Contents reside in phase-local heap memory. |
| 228 | ArenaSet<HInstruction*>* iset_; |
| 229 | |
Aart Bik | b29f684 | 2017-07-28 15:58:41 -0700 | [diff] [blame] | 230 | // Temporary bookkeeping of reduction instructions. Mapping is two-fold: |
| 231 | // (1) reductions in the loop-body are mapped back to their phi definition, |
| 232 | // (2) phi definitions are mapped to their initial value (updated during |
| 233 | // code generation to feed the proper values into the new chain). |
| 234 | // Contents reside in phase-local heap memory. |
| 235 | ArenaSafeMap<HInstruction*, HInstruction*>* reductions_; |
Aart Bik | 482095d | 2016-10-10 15:39:10 -0700 | [diff] [blame] | 236 | |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 237 | // Flag that tracks if any simplifications have occurred. |
| 238 | bool simplified_; |
| 239 | |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 240 | // Number of "lanes" for selected packed type. |
| 241 | uint32_t vector_length_; |
| 242 | |
| 243 | // Set of array references in the vector loop. |
| 244 | // Contents reside in phase-local heap memory. |
| 245 | ArenaSet<ArrayReference>* vector_refs_; |
| 246 | |
Aart Bik | 14a68b4 | 2017-06-08 14:06:58 -0700 | [diff] [blame] | 247 | // Dynamic loop peeling candidate for alignment. |
| 248 | const ArrayReference* vector_peeling_candidate_; |
| 249 | |
| 250 | // Dynamic data dependence test of the form a != b. |
| 251 | HInstruction* vector_runtime_test_a_; |
| 252 | HInstruction* vector_runtime_test_b_; |
| 253 | |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 254 | // Mapping used during vectorization synthesis for both the scalar peeling/cleanup |
Aart Bik | 14a68b4 | 2017-06-08 14:06:58 -0700 | [diff] [blame] | 255 | // loop (mode is kSequential) and the actual vector loop (mode is kVector). The data |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 256 | // structure maps original instructions into the new instructions. |
| 257 | // Contents reside in phase-local heap memory. |
| 258 | ArenaSafeMap<HInstruction*, HInstruction*>* vector_map_; |
| 259 | |
Aart Bik | 0148de4 | 2017-09-05 09:25:01 -0700 | [diff] [blame] | 260 | // Permanent mapping used during vectorization synthesis. |
| 261 | // Contents reside in phase-local heap memory. |
| 262 | ArenaSafeMap<HInstruction*, HInstruction*>* vector_permanent_map_; |
| 263 | |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 264 | // Temporary vectorization bookkeeping. |
Aart Bik | 14a68b4 | 2017-06-08 14:06:58 -0700 | [diff] [blame] | 265 | VectorMode vector_mode_; // synthesis mode |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 266 | HBasicBlock* vector_preheader_; // preheader of the new loop |
| 267 | HBasicBlock* vector_header_; // header of the new loop |
| 268 | HBasicBlock* vector_body_; // body of the new loop |
Aart Bik | 14a68b4 | 2017-06-08 14:06:58 -0700 | [diff] [blame] | 269 | HInstruction* vector_index_; // normalized index of the new loop |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 270 | |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 271 | friend class LoopOptimizationTest; |
| 272 | |
| 273 | DISALLOW_COPY_AND_ASSIGN(HLoopOptimization); |
| 274 | }; |
| 275 | |
| 276 | } // namespace art |
| 277 | |
| 278 | #endif // ART_COMPILER_OPTIMIZING_LOOP_OPTIMIZATION_H_ |