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 | #include "loop_optimization.h" |
| 18 | |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 19 | #include "arch/arm/instruction_set_features_arm.h" |
| 20 | #include "arch/arm64/instruction_set_features_arm64.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 21 | #include "arch/instruction_set.h" |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 22 | #include "arch/mips/instruction_set_features_mips.h" |
| 23 | #include "arch/mips64/instruction_set_features_mips64.h" |
| 24 | #include "arch/x86/instruction_set_features_x86.h" |
| 25 | #include "arch/x86_64/instruction_set_features_x86_64.h" |
Aart Bik | 92685a8 | 2017-03-06 11:13:43 -0800 | [diff] [blame] | 26 | #include "driver/compiler_driver.h" |
Aart Bik | 9620230 | 2016-10-04 17:33:56 -0700 | [diff] [blame] | 27 | #include "linear_order.h" |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 28 | |
| 29 | namespace art { |
| 30 | |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 31 | // Enables vectorization (SIMDization) in the loop optimizer. |
| 32 | static constexpr bool kEnableVectorization = true; |
| 33 | |
Aart Bik | 14a68b4 | 2017-06-08 14:06:58 -0700 | [diff] [blame] | 34 | // All current SIMD targets want 16-byte alignment. |
| 35 | static constexpr size_t kAlignedBase = 16; |
| 36 | |
Aart Bik | 9abf894 | 2016-10-14 09:49:42 -0700 | [diff] [blame] | 37 | // Remove the instruction from the graph. A bit more elaborate than the usual |
| 38 | // instruction removal, since there may be a cycle in the use structure. |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 39 | static void RemoveFromCycle(HInstruction* instruction) { |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 40 | instruction->RemoveAsUserOfAllInputs(); |
| 41 | instruction->RemoveEnvironmentUsers(); |
| 42 | instruction->GetBlock()->RemoveInstructionOrPhi(instruction, /*ensure_safety=*/ false); |
Artem Serov | 21c7e6f | 2017-07-27 16:04:42 +0100 | [diff] [blame] | 43 | RemoveEnvironmentUses(instruction); |
| 44 | ResetEnvironmentInputRecords(instruction); |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Aart Bik | 807868e | 2016-11-03 17:51:43 -0700 | [diff] [blame] | 47 | // Detect a goto block and sets succ to the single successor. |
Aart Bik | e3dedc5 | 2016-11-02 17:50:27 -0700 | [diff] [blame] | 48 | static bool IsGotoBlock(HBasicBlock* block, /*out*/ HBasicBlock** succ) { |
| 49 | if (block->GetPredecessors().size() == 1 && |
| 50 | block->GetSuccessors().size() == 1 && |
| 51 | block->IsSingleGoto()) { |
| 52 | *succ = block->GetSingleSuccessor(); |
| 53 | return true; |
| 54 | } |
| 55 | return false; |
| 56 | } |
| 57 | |
Aart Bik | 807868e | 2016-11-03 17:51:43 -0700 | [diff] [blame] | 58 | // Detect an early exit loop. |
| 59 | static bool IsEarlyExit(HLoopInformation* loop_info) { |
| 60 | HBlocksInLoopReversePostOrderIterator it_loop(*loop_info); |
| 61 | for (it_loop.Advance(); !it_loop.Done(); it_loop.Advance()) { |
| 62 | for (HBasicBlock* successor : it_loop.Current()->GetSuccessors()) { |
| 63 | if (!loop_info->Contains(*successor)) { |
| 64 | return true; |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | return false; |
| 69 | } |
| 70 | |
Aart Bik | f3e61ee | 2017-04-12 17:09:20 -0700 | [diff] [blame] | 71 | // Detect a sign extension from the given type. Returns the promoted operand on success. |
| 72 | static bool IsSignExtensionAndGet(HInstruction* instruction, |
| 73 | Primitive::Type type, |
| 74 | /*out*/ HInstruction** operand) { |
| 75 | // Accept any already wider constant that would be handled properly by sign |
| 76 | // extension when represented in the *width* of the given narrower data type |
| 77 | // (the fact that char normally zero extends does not matter here). |
| 78 | int64_t value = 0; |
Aart Bik | 50e20d5 | 2017-05-05 14:07:29 -0700 | [diff] [blame] | 79 | if (IsInt64AndGet(instruction, /*out*/ &value)) { |
Aart Bik | f3e61ee | 2017-04-12 17:09:20 -0700 | [diff] [blame] | 80 | switch (type) { |
| 81 | case Primitive::kPrimByte: |
| 82 | if (std::numeric_limits<int8_t>::min() <= value && |
| 83 | std::numeric_limits<int8_t>::max() >= value) { |
| 84 | *operand = instruction; |
| 85 | return true; |
| 86 | } |
| 87 | return false; |
| 88 | case Primitive::kPrimChar: |
| 89 | case Primitive::kPrimShort: |
| 90 | if (std::numeric_limits<int16_t>::min() <= value && |
| 91 | std::numeric_limits<int16_t>::max() <= value) { |
| 92 | *operand = instruction; |
| 93 | return true; |
| 94 | } |
| 95 | return false; |
| 96 | default: |
| 97 | return false; |
| 98 | } |
| 99 | } |
| 100 | // An implicit widening conversion of a signed integer to an integral type sign-extends |
| 101 | // the two's-complement representation of the integer value to fill the wider format. |
| 102 | if (instruction->GetType() == type && (instruction->IsArrayGet() || |
| 103 | instruction->IsStaticFieldGet() || |
| 104 | instruction->IsInstanceFieldGet())) { |
| 105 | switch (type) { |
| 106 | case Primitive::kPrimByte: |
| 107 | case Primitive::kPrimShort: |
| 108 | *operand = instruction; |
| 109 | return true; |
| 110 | default: |
| 111 | return false; |
| 112 | } |
| 113 | } |
| 114 | // TODO: perhaps explicit conversions later too? |
| 115 | // (this may return something different from instruction) |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | // Detect a zero extension from the given type. Returns the promoted operand on success. |
| 120 | static bool IsZeroExtensionAndGet(HInstruction* instruction, |
| 121 | Primitive::Type type, |
| 122 | /*out*/ HInstruction** operand) { |
| 123 | // Accept any already wider constant that would be handled properly by zero |
| 124 | // extension when represented in the *width* of the given narrower data type |
| 125 | // (the fact that byte/short normally sign extend does not matter here). |
| 126 | int64_t value = 0; |
Aart Bik | 50e20d5 | 2017-05-05 14:07:29 -0700 | [diff] [blame] | 127 | if (IsInt64AndGet(instruction, /*out*/ &value)) { |
Aart Bik | f3e61ee | 2017-04-12 17:09:20 -0700 | [diff] [blame] | 128 | switch (type) { |
| 129 | case Primitive::kPrimByte: |
| 130 | if (std::numeric_limits<uint8_t>::min() <= value && |
| 131 | std::numeric_limits<uint8_t>::max() >= value) { |
| 132 | *operand = instruction; |
| 133 | return true; |
| 134 | } |
| 135 | return false; |
| 136 | case Primitive::kPrimChar: |
| 137 | case Primitive::kPrimShort: |
| 138 | if (std::numeric_limits<uint16_t>::min() <= value && |
| 139 | std::numeric_limits<uint16_t>::max() <= value) { |
| 140 | *operand = instruction; |
| 141 | return true; |
| 142 | } |
| 143 | return false; |
| 144 | default: |
| 145 | return false; |
| 146 | } |
| 147 | } |
| 148 | // An implicit widening conversion of a char to an integral type zero-extends |
| 149 | // the representation of the char value to fill the wider format. |
| 150 | if (instruction->GetType() == type && (instruction->IsArrayGet() || |
| 151 | instruction->IsStaticFieldGet() || |
| 152 | instruction->IsInstanceFieldGet())) { |
| 153 | if (type == Primitive::kPrimChar) { |
| 154 | *operand = instruction; |
| 155 | return true; |
| 156 | } |
| 157 | } |
| 158 | // A sign (or zero) extension followed by an explicit removal of just the |
| 159 | // higher sign bits is equivalent to a zero extension of the underlying operand. |
| 160 | if (instruction->IsAnd()) { |
| 161 | int64_t mask = 0; |
| 162 | HInstruction* a = instruction->InputAt(0); |
| 163 | HInstruction* b = instruction->InputAt(1); |
| 164 | // In (a & b) find (mask & b) or (a & mask) with sign or zero extension on the non-mask. |
| 165 | if ((IsInt64AndGet(a, /*out*/ &mask) && (IsSignExtensionAndGet(b, type, /*out*/ operand) || |
| 166 | IsZeroExtensionAndGet(b, type, /*out*/ operand))) || |
| 167 | (IsInt64AndGet(b, /*out*/ &mask) && (IsSignExtensionAndGet(a, type, /*out*/ operand) || |
| 168 | IsZeroExtensionAndGet(a, type, /*out*/ operand)))) { |
| 169 | switch ((*operand)->GetType()) { |
| 170 | case Primitive::kPrimByte: return mask == std::numeric_limits<uint8_t>::max(); |
| 171 | case Primitive::kPrimChar: |
| 172 | case Primitive::kPrimShort: return mask == std::numeric_limits<uint16_t>::max(); |
| 173 | default: return false; |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | // TODO: perhaps explicit conversions later too? |
| 178 | return false; |
| 179 | } |
| 180 | |
Aart Bik | 304c8a5 | 2017-05-23 11:01:13 -0700 | [diff] [blame] | 181 | // Detect situations with same-extension narrower operands. |
| 182 | // Returns true on success and sets is_unsigned accordingly. |
| 183 | static bool IsNarrowerOperands(HInstruction* a, |
| 184 | HInstruction* b, |
| 185 | Primitive::Type type, |
| 186 | /*out*/ HInstruction** r, |
| 187 | /*out*/ HInstruction** s, |
| 188 | /*out*/ bool* is_unsigned) { |
| 189 | if (IsSignExtensionAndGet(a, type, r) && IsSignExtensionAndGet(b, type, s)) { |
| 190 | *is_unsigned = false; |
| 191 | return true; |
| 192 | } else if (IsZeroExtensionAndGet(a, type, r) && IsZeroExtensionAndGet(b, type, s)) { |
| 193 | *is_unsigned = true; |
| 194 | return true; |
| 195 | } |
| 196 | return false; |
| 197 | } |
| 198 | |
| 199 | // As above, single operand. |
| 200 | static bool IsNarrowerOperand(HInstruction* a, |
| 201 | Primitive::Type type, |
| 202 | /*out*/ HInstruction** r, |
| 203 | /*out*/ bool* is_unsigned) { |
| 204 | if (IsSignExtensionAndGet(a, type, r)) { |
| 205 | *is_unsigned = false; |
| 206 | return true; |
| 207 | } else if (IsZeroExtensionAndGet(a, type, r)) { |
| 208 | *is_unsigned = true; |
| 209 | return true; |
| 210 | } |
| 211 | return false; |
| 212 | } |
| 213 | |
Aart Bik | 5f80500 | 2017-05-16 16:42:41 -0700 | [diff] [blame] | 214 | // Detect up to two instructions a and b, and an acccumulated constant c. |
| 215 | static bool IsAddConstHelper(HInstruction* instruction, |
| 216 | /*out*/ HInstruction** a, |
| 217 | /*out*/ HInstruction** b, |
| 218 | /*out*/ int64_t* c, |
| 219 | int32_t depth) { |
| 220 | static constexpr int32_t kMaxDepth = 8; // don't search too deep |
| 221 | int64_t value = 0; |
| 222 | if (IsInt64AndGet(instruction, &value)) { |
| 223 | *c += value; |
| 224 | return true; |
| 225 | } else if (instruction->IsAdd() && depth <= kMaxDepth) { |
| 226 | return IsAddConstHelper(instruction->InputAt(0), a, b, c, depth + 1) && |
| 227 | IsAddConstHelper(instruction->InputAt(1), a, b, c, depth + 1); |
| 228 | } else if (*a == nullptr) { |
| 229 | *a = instruction; |
| 230 | return true; |
| 231 | } else if (*b == nullptr) { |
| 232 | *b = instruction; |
| 233 | return true; |
| 234 | } |
| 235 | return false; // too many non-const operands |
| 236 | } |
| 237 | |
| 238 | // Detect a + b + c for an optional constant c. |
| 239 | static bool IsAddConst(HInstruction* instruction, |
| 240 | /*out*/ HInstruction** a, |
| 241 | /*out*/ HInstruction** b, |
| 242 | /*out*/ int64_t* c) { |
| 243 | if (instruction->IsAdd()) { |
| 244 | // Try to find a + b and accumulated c. |
| 245 | if (IsAddConstHelper(instruction->InputAt(0), a, b, c, /*depth*/ 0) && |
| 246 | IsAddConstHelper(instruction->InputAt(1), a, b, c, /*depth*/ 0) && |
| 247 | *b != nullptr) { |
| 248 | return true; |
| 249 | } |
| 250 | // Found a + b. |
| 251 | *a = instruction->InputAt(0); |
| 252 | *b = instruction->InputAt(1); |
| 253 | *c = 0; |
| 254 | return true; |
| 255 | } |
| 256 | return false; |
| 257 | } |
| 258 | |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 259 | // Test vector restrictions. |
| 260 | static bool HasVectorRestrictions(uint64_t restrictions, uint64_t tested) { |
| 261 | return (restrictions & tested) != 0; |
| 262 | } |
| 263 | |
Aart Bik | f3e61ee | 2017-04-12 17:09:20 -0700 | [diff] [blame] | 264 | // Insert an instruction. |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 265 | static HInstruction* Insert(HBasicBlock* block, HInstruction* instruction) { |
| 266 | DCHECK(block != nullptr); |
| 267 | DCHECK(instruction != nullptr); |
| 268 | block->InsertInstructionBefore(instruction, block->GetLastInstruction()); |
| 269 | return instruction; |
| 270 | } |
| 271 | |
Artem Serov | 21c7e6f | 2017-07-27 16:04:42 +0100 | [diff] [blame] | 272 | // Check that instructions from the induction sets are fully removed: have no uses |
| 273 | // and no other instructions use them. |
| 274 | static bool CheckInductionSetFullyRemoved(ArenaSet<HInstruction*>* iset) { |
| 275 | for (HInstruction* instr : *iset) { |
| 276 | if (instr->GetBlock() != nullptr || |
| 277 | !instr->GetUses().empty() || |
| 278 | !instr->GetEnvUses().empty() || |
| 279 | HasEnvironmentUsedByOthers(instr)) { |
| 280 | return false; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | return true; |
| 285 | } |
| 286 | |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 287 | // |
| 288 | // Class methods. |
| 289 | // |
| 290 | |
| 291 | HLoopOptimization::HLoopOptimization(HGraph* graph, |
Aart Bik | 92685a8 | 2017-03-06 11:13:43 -0800 | [diff] [blame] | 292 | CompilerDriver* compiler_driver, |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 293 | HInductionVarAnalysis* induction_analysis) |
| 294 | : HOptimization(graph, kLoopOptimizationPassName), |
Aart Bik | 92685a8 | 2017-03-06 11:13:43 -0800 | [diff] [blame] | 295 | compiler_driver_(compiler_driver), |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 296 | induction_range_(induction_analysis), |
Aart Bik | 9620230 | 2016-10-04 17:33:56 -0700 | [diff] [blame] | 297 | loop_allocator_(nullptr), |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 298 | global_allocator_(graph_->GetArena()), |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 299 | top_loop_(nullptr), |
Aart Bik | 8c4a854 | 2016-10-06 11:36:57 -0700 | [diff] [blame] | 300 | last_loop_(nullptr), |
Aart Bik | 482095d | 2016-10-10 15:39:10 -0700 | [diff] [blame] | 301 | iset_(nullptr), |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 302 | induction_simplication_count_(0), |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 303 | simplified_(false), |
| 304 | vector_length_(0), |
| 305 | vector_refs_(nullptr), |
Aart Bik | 14a68b4 | 2017-06-08 14:06:58 -0700 | [diff] [blame] | 306 | vector_peeling_candidate_(nullptr), |
| 307 | vector_runtime_test_a_(nullptr), |
| 308 | vector_runtime_test_b_(nullptr), |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 309 | vector_map_(nullptr) { |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | void HLoopOptimization::Run() { |
Mingyao Yang | 01b47b0 | 2017-02-03 12:09:57 -0800 | [diff] [blame] | 313 | // Skip if there is no loop or the graph has try-catch/irreducible loops. |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 314 | // TODO: make this less of a sledgehammer. |
Mingyao Yang | 69d75ff | 2017-02-07 13:06:06 -0800 | [diff] [blame] | 315 | if (!graph_->HasLoops() || graph_->HasTryCatch() || graph_->HasIrreducibleLoops()) { |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 316 | return; |
| 317 | } |
| 318 | |
Aart Bik | 9620230 | 2016-10-04 17:33:56 -0700 | [diff] [blame] | 319 | // Phase-local allocator that draws from the global pool. Since the allocator |
| 320 | // itself resides on the stack, it is destructed on exiting Run(), which |
| 321 | // implies its underlying memory is released immediately. |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 322 | ArenaAllocator allocator(global_allocator_->GetArenaPool()); |
Aart Bik | 9620230 | 2016-10-04 17:33:56 -0700 | [diff] [blame] | 323 | loop_allocator_ = &allocator; |
Nicolas Geoffray | ebe1674 | 2016-10-05 09:55:42 +0100 | [diff] [blame] | 324 | |
Aart Bik | 9620230 | 2016-10-04 17:33:56 -0700 | [diff] [blame] | 325 | // Perform loop optimizations. |
| 326 | LocalRun(); |
Mingyao Yang | 69d75ff | 2017-02-07 13:06:06 -0800 | [diff] [blame] | 327 | if (top_loop_ == nullptr) { |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 328 | graph_->SetHasLoops(false); // no more loops |
Mingyao Yang | 69d75ff | 2017-02-07 13:06:06 -0800 | [diff] [blame] | 329 | } |
| 330 | |
Aart Bik | 9620230 | 2016-10-04 17:33:56 -0700 | [diff] [blame] | 331 | // Detach. |
| 332 | loop_allocator_ = nullptr; |
| 333 | last_loop_ = top_loop_ = nullptr; |
| 334 | } |
| 335 | |
| 336 | void HLoopOptimization::LocalRun() { |
| 337 | // Build the linear order using the phase-local allocator. This step enables building |
| 338 | // a loop hierarchy that properly reflects the outer-inner and previous-next relation. |
| 339 | ArenaVector<HBasicBlock*> linear_order(loop_allocator_->Adapter(kArenaAllocLinearOrder)); |
| 340 | LinearizeGraph(graph_, loop_allocator_, &linear_order); |
| 341 | |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 342 | // Build the loop hierarchy. |
Aart Bik | 9620230 | 2016-10-04 17:33:56 -0700 | [diff] [blame] | 343 | for (HBasicBlock* block : linear_order) { |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 344 | if (block->IsLoopHeader()) { |
| 345 | AddLoop(block->GetLoopInformation()); |
| 346 | } |
| 347 | } |
Aart Bik | 9620230 | 2016-10-04 17:33:56 -0700 | [diff] [blame] | 348 | |
Aart Bik | 8c4a854 | 2016-10-06 11:36:57 -0700 | [diff] [blame] | 349 | // Traverse the loop hierarchy inner-to-outer and optimize. Traversal can use |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 350 | // temporary data structures using the phase-local allocator. All new HIR |
| 351 | // should use the global allocator. |
Aart Bik | 8c4a854 | 2016-10-06 11:36:57 -0700 | [diff] [blame] | 352 | if (top_loop_ != nullptr) { |
| 353 | ArenaSet<HInstruction*> iset(loop_allocator_->Adapter(kArenaAllocLoopOptimization)); |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 354 | ArenaSet<ArrayReference> refs(loop_allocator_->Adapter(kArenaAllocLoopOptimization)); |
| 355 | ArenaSafeMap<HInstruction*, HInstruction*> map( |
| 356 | std::less<HInstruction*>(), loop_allocator_->Adapter(kArenaAllocLoopOptimization)); |
| 357 | // Attach. |
Aart Bik | 8c4a854 | 2016-10-06 11:36:57 -0700 | [diff] [blame] | 358 | iset_ = &iset; |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 359 | vector_refs_ = &refs; |
| 360 | vector_map_ = ↦ |
| 361 | // Traverse. |
Aart Bik | 8c4a854 | 2016-10-06 11:36:57 -0700 | [diff] [blame] | 362 | TraverseLoopsInnerToOuter(top_loop_); |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 363 | // Detach. |
| 364 | iset_ = nullptr; |
| 365 | vector_refs_ = nullptr; |
| 366 | vector_map_ = nullptr; |
Aart Bik | 8c4a854 | 2016-10-06 11:36:57 -0700 | [diff] [blame] | 367 | } |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | void HLoopOptimization::AddLoop(HLoopInformation* loop_info) { |
| 371 | DCHECK(loop_info != nullptr); |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 372 | LoopNode* node = new (loop_allocator_) LoopNode(loop_info); |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 373 | if (last_loop_ == nullptr) { |
| 374 | // First loop. |
| 375 | DCHECK(top_loop_ == nullptr); |
| 376 | last_loop_ = top_loop_ = node; |
| 377 | } else if (loop_info->IsIn(*last_loop_->loop_info)) { |
| 378 | // Inner loop. |
| 379 | node->outer = last_loop_; |
| 380 | DCHECK(last_loop_->inner == nullptr); |
| 381 | last_loop_ = last_loop_->inner = node; |
| 382 | } else { |
| 383 | // Subsequent loop. |
| 384 | while (last_loop_->outer != nullptr && !loop_info->IsIn(*last_loop_->outer->loop_info)) { |
| 385 | last_loop_ = last_loop_->outer; |
| 386 | } |
| 387 | node->outer = last_loop_->outer; |
| 388 | node->previous = last_loop_; |
| 389 | DCHECK(last_loop_->next == nullptr); |
| 390 | last_loop_ = last_loop_->next = node; |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | void HLoopOptimization::RemoveLoop(LoopNode* node) { |
| 395 | DCHECK(node != nullptr); |
Aart Bik | 8c4a854 | 2016-10-06 11:36:57 -0700 | [diff] [blame] | 396 | DCHECK(node->inner == nullptr); |
| 397 | if (node->previous != nullptr) { |
| 398 | // Within sequence. |
| 399 | node->previous->next = node->next; |
| 400 | if (node->next != nullptr) { |
| 401 | node->next->previous = node->previous; |
| 402 | } |
| 403 | } else { |
| 404 | // First of sequence. |
| 405 | if (node->outer != nullptr) { |
| 406 | node->outer->inner = node->next; |
| 407 | } else { |
| 408 | top_loop_ = node->next; |
| 409 | } |
| 410 | if (node->next != nullptr) { |
| 411 | node->next->outer = node->outer; |
| 412 | node->next->previous = nullptr; |
| 413 | } |
| 414 | } |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | void HLoopOptimization::TraverseLoopsInnerToOuter(LoopNode* node) { |
| 418 | for ( ; node != nullptr; node = node->next) { |
Aart Bik | 6b69e0a | 2017-01-11 10:20:43 -0800 | [diff] [blame] | 419 | // Visit inner loops first. |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 420 | uint32_t current_induction_simplification_count = induction_simplication_count_; |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 421 | if (node->inner != nullptr) { |
| 422 | TraverseLoopsInnerToOuter(node->inner); |
| 423 | } |
Aart Bik | 6b69e0a | 2017-01-11 10:20:43 -0800 | [diff] [blame] | 424 | // Recompute induction information of this loop if the induction |
| 425 | // of any inner loop has been simplified. |
Aart Bik | 482095d | 2016-10-10 15:39:10 -0700 | [diff] [blame] | 426 | if (current_induction_simplification_count != induction_simplication_count_) { |
| 427 | induction_range_.ReVisit(node->loop_info); |
| 428 | } |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 429 | // Repeat simplifications in the loop-body until no more changes occur. |
Aart Bik | 6b69e0a | 2017-01-11 10:20:43 -0800 | [diff] [blame] | 430 | // Note that since each simplification consists of eliminating code (without |
| 431 | // introducing new code), this process is always finite. |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 432 | do { |
| 433 | simplified_ = false; |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 434 | SimplifyInduction(node); |
Aart Bik | 6b69e0a | 2017-01-11 10:20:43 -0800 | [diff] [blame] | 435 | SimplifyBlocks(node); |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 436 | } while (simplified_); |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 437 | // Optimize inner loop. |
Aart Bik | 9abf894 | 2016-10-14 09:49:42 -0700 | [diff] [blame] | 438 | if (node->inner == nullptr) { |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 439 | OptimizeInnerLoop(node); |
Aart Bik | 9abf894 | 2016-10-14 09:49:42 -0700 | [diff] [blame] | 440 | } |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 441 | } |
| 442 | } |
| 443 | |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 444 | // |
| 445 | // Optimization. |
| 446 | // |
| 447 | |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 448 | void HLoopOptimization::SimplifyInduction(LoopNode* node) { |
| 449 | HBasicBlock* header = node->loop_info->GetHeader(); |
| 450 | HBasicBlock* preheader = node->loop_info->GetPreHeader(); |
Aart Bik | 8c4a854 | 2016-10-06 11:36:57 -0700 | [diff] [blame] | 451 | // Scan the phis in the header to find opportunities to simplify an induction |
| 452 | // cycle that is only used outside the loop. Replace these uses, if any, with |
| 453 | // the last value and remove the induction cycle. |
| 454 | // Examples: for (int i = 0; x != null; i++) { .... no i .... } |
| 455 | // for (int i = 0; i < 10; i++, k++) { .... no k .... } return k; |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 456 | for (HInstructionIterator it(header->GetPhis()); !it.Done(); it.Advance()) { |
| 457 | HPhi* phi = it.Current()->AsPhi(); |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 458 | iset_->clear(); // prepare phi induction |
| 459 | if (TrySetPhiInduction(phi, /*restrict_uses*/ true) && |
| 460 | TryAssignLastValue(node->loop_info, phi, preheader, /*collect_loop_uses*/ false)) { |
Nicolas Geoffray | 1a0a519 | 2017-06-22 11:56:01 +0100 | [diff] [blame] | 461 | // Note that it's ok to have replaced uses after the loop with the last value, without |
| 462 | // being able to remove the cycle. Environment uses (which are the reason we may not be |
| 463 | // able to remove the cycle) within the loop will still hold the right value. |
| 464 | if (CanRemoveCycle()) { |
| 465 | for (HInstruction* i : *iset_) { |
| 466 | RemoveFromCycle(i); |
| 467 | } |
Artem Serov | 21c7e6f | 2017-07-27 16:04:42 +0100 | [diff] [blame] | 468 | |
| 469 | // Check that there are no records of the deleted instructions. |
| 470 | DCHECK(CheckInductionSetFullyRemoved(iset_)); |
Nicolas Geoffray | 1a0a519 | 2017-06-22 11:56:01 +0100 | [diff] [blame] | 471 | simplified_ = true; |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 472 | } |
Aart Bik | 482095d | 2016-10-10 15:39:10 -0700 | [diff] [blame] | 473 | } |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | void HLoopOptimization::SimplifyBlocks(LoopNode* node) { |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 478 | // Iterate over all basic blocks in the loop-body. |
| 479 | for (HBlocksInLoopIterator it(*node->loop_info); !it.Done(); it.Advance()) { |
| 480 | HBasicBlock* block = it.Current(); |
| 481 | // Remove dead instructions from the loop-body. |
Aart Bik | 6b69e0a | 2017-01-11 10:20:43 -0800 | [diff] [blame] | 482 | RemoveDeadInstructions(block->GetPhis()); |
| 483 | RemoveDeadInstructions(block->GetInstructions()); |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 484 | // Remove trivial control flow blocks from the loop-body. |
Aart Bik | 6b69e0a | 2017-01-11 10:20:43 -0800 | [diff] [blame] | 485 | if (block->GetPredecessors().size() == 1 && |
| 486 | block->GetSuccessors().size() == 1 && |
| 487 | block->GetSingleSuccessor()->GetPredecessors().size() == 1) { |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 488 | simplified_ = true; |
Aart Bik | 6b69e0a | 2017-01-11 10:20:43 -0800 | [diff] [blame] | 489 | block->MergeWith(block->GetSingleSuccessor()); |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 490 | } else if (block->GetSuccessors().size() == 2) { |
| 491 | // Trivial if block can be bypassed to either branch. |
| 492 | HBasicBlock* succ0 = block->GetSuccessors()[0]; |
| 493 | HBasicBlock* succ1 = block->GetSuccessors()[1]; |
| 494 | HBasicBlock* meet0 = nullptr; |
| 495 | HBasicBlock* meet1 = nullptr; |
| 496 | if (succ0 != succ1 && |
| 497 | IsGotoBlock(succ0, &meet0) && |
| 498 | IsGotoBlock(succ1, &meet1) && |
| 499 | meet0 == meet1 && // meets again |
| 500 | meet0 != block && // no self-loop |
| 501 | meet0->GetPhis().IsEmpty()) { // not used for merging |
| 502 | simplified_ = true; |
| 503 | succ0->DisconnectAndDelete(); |
| 504 | if (block->Dominates(meet0)) { |
| 505 | block->RemoveDominatedBlock(meet0); |
| 506 | succ1->AddDominatedBlock(meet0); |
| 507 | meet0->SetDominator(succ1); |
Aart Bik | e3dedc5 | 2016-11-02 17:50:27 -0700 | [diff] [blame] | 508 | } |
Aart Bik | 482095d | 2016-10-10 15:39:10 -0700 | [diff] [blame] | 509 | } |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 510 | } |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 511 | } |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 512 | } |
| 513 | |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 514 | void HLoopOptimization::OptimizeInnerLoop(LoopNode* node) { |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 515 | HBasicBlock* header = node->loop_info->GetHeader(); |
| 516 | HBasicBlock* preheader = node->loop_info->GetPreHeader(); |
Aart Bik | 9abf894 | 2016-10-14 09:49:42 -0700 | [diff] [blame] | 517 | // Ensure loop header logic is finite. |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 518 | int64_t trip_count = 0; |
| 519 | if (!induction_range_.IsFinite(node->loop_info, &trip_count)) { |
| 520 | return; |
Aart Bik | 9abf894 | 2016-10-14 09:49:42 -0700 | [diff] [blame] | 521 | } |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 522 | |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 523 | // Ensure there is only a single loop-body (besides the header). |
| 524 | HBasicBlock* body = nullptr; |
| 525 | for (HBlocksInLoopIterator it(*node->loop_info); !it.Done(); it.Advance()) { |
| 526 | if (it.Current() != header) { |
| 527 | if (body != nullptr) { |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 528 | return; |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 529 | } |
| 530 | body = it.Current(); |
| 531 | } |
| 532 | } |
Andreas Gampe | f45d61c | 2017-06-07 10:29:33 -0700 | [diff] [blame] | 533 | CHECK(body != nullptr); |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 534 | // Ensure there is only a single exit point. |
| 535 | if (header->GetSuccessors().size() != 2) { |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 536 | return; |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 537 | } |
| 538 | HBasicBlock* exit = (header->GetSuccessors()[0] == body) |
| 539 | ? header->GetSuccessors()[1] |
| 540 | : header->GetSuccessors()[0]; |
Aart Bik | 8c4a854 | 2016-10-06 11:36:57 -0700 | [diff] [blame] | 541 | // Ensure exit can only be reached by exiting loop. |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 542 | if (exit->GetPredecessors().size() != 1) { |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 543 | return; |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 544 | } |
Aart Bik | 6b69e0a | 2017-01-11 10:20:43 -0800 | [diff] [blame] | 545 | // Detect either an empty loop (no side effects other than plain iteration) or |
| 546 | // a trivial loop (just iterating once). Replace subsequent index uses, if any, |
| 547 | // with the last value and remove the loop, possibly after unrolling its body. |
| 548 | HInstruction* phi = header->GetFirstPhi(); |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 549 | iset_->clear(); // prepare phi induction |
| 550 | if (TrySetSimpleLoopHeader(header)) { |
Aart Bik | 6b69e0a | 2017-01-11 10:20:43 -0800 | [diff] [blame] | 551 | bool is_empty = IsEmptyBody(body); |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 552 | if ((is_empty || trip_count == 1) && |
| 553 | TryAssignLastValue(node->loop_info, phi, preheader, /*collect_loop_uses*/ true)) { |
Aart Bik | 6b69e0a | 2017-01-11 10:20:43 -0800 | [diff] [blame] | 554 | if (!is_empty) { |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 555 | // Unroll the loop-body, which sees initial value of the index. |
Aart Bik | 6b69e0a | 2017-01-11 10:20:43 -0800 | [diff] [blame] | 556 | phi->ReplaceWith(phi->InputAt(0)); |
| 557 | preheader->MergeInstructionsWith(body); |
| 558 | } |
| 559 | body->DisconnectAndDelete(); |
| 560 | exit->RemovePredecessor(header); |
| 561 | header->RemoveSuccessor(exit); |
| 562 | header->RemoveDominatedBlock(exit); |
| 563 | header->DisconnectAndDelete(); |
| 564 | preheader->AddSuccessor(exit); |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 565 | preheader->AddInstruction(new (global_allocator_) HGoto()); |
Aart Bik | 6b69e0a | 2017-01-11 10:20:43 -0800 | [diff] [blame] | 566 | preheader->AddDominatedBlock(exit); |
| 567 | exit->SetDominator(preheader); |
| 568 | RemoveLoop(node); // update hierarchy |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 569 | return; |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | // Vectorize loop, if possible and valid. |
| 574 | if (kEnableVectorization) { |
| 575 | iset_->clear(); // prepare phi induction |
| 576 | if (TrySetSimpleLoopHeader(header) && |
Aart Bik | 14a68b4 | 2017-06-08 14:06:58 -0700 | [diff] [blame] | 577 | ShouldVectorize(node, body, trip_count) && |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 578 | TryAssignLastValue(node->loop_info, phi, preheader, /*collect_loop_uses*/ true)) { |
| 579 | Vectorize(node, body, exit, trip_count); |
| 580 | graph_->SetHasSIMD(true); // flag SIMD usage |
| 581 | return; |
| 582 | } |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | // |
| 587 | // Loop vectorization. The implementation is based on the book by Aart J.C. Bik: |
| 588 | // "The Software Vectorization Handbook. Applying Multimedia Extensions for Maximum Performance." |
| 589 | // Intel Press, June, 2004 (http://www.aartbik.com/). |
| 590 | // |
| 591 | |
Aart Bik | 14a68b4 | 2017-06-08 14:06:58 -0700 | [diff] [blame] | 592 | bool HLoopOptimization::ShouldVectorize(LoopNode* node, HBasicBlock* block, int64_t trip_count) { |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 593 | // Reset vector bookkeeping. |
| 594 | vector_length_ = 0; |
| 595 | vector_refs_->clear(); |
Aart Bik | 14a68b4 | 2017-06-08 14:06:58 -0700 | [diff] [blame] | 596 | vector_peeling_candidate_ = nullptr; |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 597 | vector_runtime_test_a_ = |
| 598 | vector_runtime_test_b_= nullptr; |
| 599 | |
| 600 | // Phis in the loop-body prevent vectorization. |
| 601 | if (!block->GetPhis().IsEmpty()) { |
| 602 | return false; |
| 603 | } |
| 604 | |
| 605 | // Scan the loop-body, starting a right-hand-side tree traversal at each left-hand-side |
| 606 | // occurrence, which allows passing down attributes down the use tree. |
| 607 | for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) { |
| 608 | if (!VectorizeDef(node, it.Current(), /*generate_code*/ false)) { |
| 609 | return false; // failure to vectorize a left-hand-side |
| 610 | } |
| 611 | } |
| 612 | |
Aart Bik | 14a68b4 | 2017-06-08 14:06:58 -0700 | [diff] [blame] | 613 | // Does vectorization seem profitable? |
| 614 | if (!IsVectorizationProfitable(trip_count)) { |
| 615 | return false; |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | // Data dependence analysis. Find each pair of references with same type, where |
| 619 | // at least one is a write. Each such pair denotes a possible data dependence. |
| 620 | // This analysis exploits the property that differently typed arrays cannot be |
| 621 | // aliased, as well as the property that references either point to the same |
| 622 | // array or to two completely disjoint arrays, i.e., no partial aliasing. |
| 623 | // Other than a few simply heuristics, no detailed subscript analysis is done. |
| 624 | for (auto i = vector_refs_->begin(); i != vector_refs_->end(); ++i) { |
| 625 | for (auto j = i; ++j != vector_refs_->end(); ) { |
| 626 | if (i->type == j->type && (i->lhs || j->lhs)) { |
| 627 | // Found same-typed a[i+x] vs. b[i+y], where at least one is a write. |
| 628 | HInstruction* a = i->base; |
| 629 | HInstruction* b = j->base; |
| 630 | HInstruction* x = i->offset; |
| 631 | HInstruction* y = j->offset; |
| 632 | if (a == b) { |
| 633 | // Found a[i+x] vs. a[i+y]. Accept if x == y (loop-independent data dependence). |
| 634 | // Conservatively assume a loop-carried data dependence otherwise, and reject. |
| 635 | if (x != y) { |
| 636 | return false; |
| 637 | } |
| 638 | } else { |
| 639 | // Found a[i+x] vs. b[i+y]. Accept if x == y (at worst loop-independent data dependence). |
| 640 | // Conservatively assume a potential loop-carried data dependence otherwise, avoided by |
| 641 | // generating an explicit a != b disambiguation runtime test on the two references. |
| 642 | if (x != y) { |
Aart Bik | 37dc4df | 2017-06-28 14:08:00 -0700 | [diff] [blame] | 643 | // To avoid excessive overhead, we only accept one a != b test. |
| 644 | if (vector_runtime_test_a_ == nullptr) { |
| 645 | // First test found. |
| 646 | vector_runtime_test_a_ = a; |
| 647 | vector_runtime_test_b_ = b; |
| 648 | } else if ((vector_runtime_test_a_ != a || vector_runtime_test_b_ != b) && |
| 649 | (vector_runtime_test_a_ != b || vector_runtime_test_b_ != a)) { |
| 650 | return false; // second test would be needed |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 651 | } |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 652 | } |
| 653 | } |
| 654 | } |
| 655 | } |
| 656 | } |
| 657 | |
Aart Bik | 14a68b4 | 2017-06-08 14:06:58 -0700 | [diff] [blame] | 658 | // Consider dynamic loop peeling for alignment. |
| 659 | SetPeelingCandidate(trip_count); |
| 660 | |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 661 | // Success! |
| 662 | return true; |
| 663 | } |
| 664 | |
| 665 | void HLoopOptimization::Vectorize(LoopNode* node, |
| 666 | HBasicBlock* block, |
| 667 | HBasicBlock* exit, |
| 668 | int64_t trip_count) { |
| 669 | Primitive::Type induc_type = Primitive::kPrimInt; |
| 670 | HBasicBlock* header = node->loop_info->GetHeader(); |
| 671 | HBasicBlock* preheader = node->loop_info->GetPreHeader(); |
| 672 | |
Aart Bik | 14a68b4 | 2017-06-08 14:06:58 -0700 | [diff] [blame] | 673 | // Pick a loop unrolling factor for the vector loop. |
| 674 | uint32_t unroll = GetUnrollingFactor(block, trip_count); |
| 675 | uint32_t chunk = vector_length_ * unroll; |
| 676 | |
| 677 | // A cleanup loop is needed, at least, for any unknown trip count or |
| 678 | // for a known trip count with remainder iterations after vectorization. |
| 679 | bool needs_cleanup = trip_count == 0 || (trip_count % chunk) != 0; |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 680 | |
| 681 | // Adjust vector bookkeeping. |
| 682 | iset_->clear(); // prepare phi induction |
| 683 | bool is_simple_loop_header = TrySetSimpleLoopHeader(header); // fills iset_ |
| 684 | DCHECK(is_simple_loop_header); |
Aart Bik | 14a68b4 | 2017-06-08 14:06:58 -0700 | [diff] [blame] | 685 | vector_header_ = header; |
| 686 | vector_body_ = block; |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 687 | |
Aart Bik | 14a68b4 | 2017-06-08 14:06:58 -0700 | [diff] [blame] | 688 | // Generate dynamic loop peeling trip count, if needed: |
| 689 | // ptc = <peeling-needed-for-candidate> |
| 690 | HInstruction* ptc = nullptr; |
| 691 | if (vector_peeling_candidate_ != nullptr) { |
| 692 | DCHECK_LT(vector_length_, trip_count) << "dynamic peeling currently requires known trip count"; |
| 693 | // |
| 694 | // TODO: Implement this. Compute address of first access memory location and |
| 695 | // compute peeling factor to obtain kAlignedBase alignment. |
| 696 | // |
| 697 | needs_cleanup = true; |
| 698 | } |
| 699 | |
| 700 | // Generate loop control: |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 701 | // stc = <trip-count>; |
Aart Bik | 14a68b4 | 2017-06-08 14:06:58 -0700 | [diff] [blame] | 702 | // vtc = stc - (stc - ptc) % chunk; |
| 703 | // i = 0; |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 704 | HInstruction* stc = induction_range_.GenerateTripCount(node->loop_info, graph_, preheader); |
| 705 | HInstruction* vtc = stc; |
| 706 | if (needs_cleanup) { |
Aart Bik | 14a68b4 | 2017-06-08 14:06:58 -0700 | [diff] [blame] | 707 | DCHECK(IsPowerOfTwo(chunk)); |
| 708 | HInstruction* diff = stc; |
| 709 | if (ptc != nullptr) { |
| 710 | diff = Insert(preheader, new (global_allocator_) HSub(induc_type, stc, ptc)); |
| 711 | } |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 712 | HInstruction* rem = Insert( |
| 713 | preheader, new (global_allocator_) HAnd(induc_type, |
Aart Bik | 14a68b4 | 2017-06-08 14:06:58 -0700 | [diff] [blame] | 714 | diff, |
| 715 | graph_->GetIntConstant(chunk - 1))); |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 716 | vtc = Insert(preheader, new (global_allocator_) HSub(induc_type, stc, rem)); |
| 717 | } |
Aart Bik | 14a68b4 | 2017-06-08 14:06:58 -0700 | [diff] [blame] | 718 | vector_index_ = graph_->GetIntConstant(0); |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 719 | |
| 720 | // Generate runtime disambiguation test: |
| 721 | // vtc = a != b ? vtc : 0; |
| 722 | if (vector_runtime_test_a_ != nullptr) { |
| 723 | HInstruction* rt = Insert( |
| 724 | preheader, |
| 725 | new (global_allocator_) HNotEqual(vector_runtime_test_a_, vector_runtime_test_b_)); |
| 726 | vtc = Insert(preheader, |
| 727 | new (global_allocator_) HSelect(rt, vtc, graph_->GetIntConstant(0), kNoDexPc)); |
| 728 | needs_cleanup = true; |
| 729 | } |
| 730 | |
Aart Bik | 14a68b4 | 2017-06-08 14:06:58 -0700 | [diff] [blame] | 731 | // Generate dynamic peeling loop for alignment, if needed: |
| 732 | // for ( ; i < ptc; i += 1) |
| 733 | // <loop-body> |
| 734 | if (ptc != nullptr) { |
| 735 | vector_mode_ = kSequential; |
| 736 | GenerateNewLoop(node, |
| 737 | block, |
| 738 | graph_->TransformLoopForVectorization(vector_header_, vector_body_, exit), |
| 739 | vector_index_, |
| 740 | ptc, |
| 741 | graph_->GetIntConstant(1), |
| 742 | /*unroll*/ 1); |
| 743 | } |
| 744 | |
| 745 | // Generate vector loop, possibly further unrolled: |
| 746 | // for ( ; i < vtc; i += chunk) |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 747 | // <vectorized-loop-body> |
| 748 | vector_mode_ = kVector; |
| 749 | GenerateNewLoop(node, |
| 750 | block, |
Aart Bik | 14a68b4 | 2017-06-08 14:06:58 -0700 | [diff] [blame] | 751 | graph_->TransformLoopForVectorization(vector_header_, vector_body_, exit), |
| 752 | vector_index_, |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 753 | vtc, |
Aart Bik | 14a68b4 | 2017-06-08 14:06:58 -0700 | [diff] [blame] | 754 | graph_->GetIntConstant(vector_length_), // increment per unroll |
| 755 | unroll); |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 756 | HLoopInformation* vloop = vector_header_->GetLoopInformation(); |
| 757 | |
| 758 | // Generate cleanup loop, if needed: |
| 759 | // for ( ; i < stc; i += 1) |
| 760 | // <loop-body> |
| 761 | if (needs_cleanup) { |
| 762 | vector_mode_ = kSequential; |
| 763 | GenerateNewLoop(node, |
| 764 | block, |
| 765 | graph_->TransformLoopForVectorization(vector_header_, vector_body_, exit), |
Aart Bik | 14a68b4 | 2017-06-08 14:06:58 -0700 | [diff] [blame] | 766 | vector_index_, |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 767 | stc, |
Aart Bik | 14a68b4 | 2017-06-08 14:06:58 -0700 | [diff] [blame] | 768 | graph_->GetIntConstant(1), |
| 769 | /*unroll*/ 1); |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | // Remove the original loop by disconnecting the body block |
| 773 | // and removing all instructions from the header. |
| 774 | block->DisconnectAndDelete(); |
| 775 | while (!header->GetFirstInstruction()->IsGoto()) { |
| 776 | header->RemoveInstruction(header->GetFirstInstruction()); |
| 777 | } |
Aart Bik | 14a68b4 | 2017-06-08 14:06:58 -0700 | [diff] [blame] | 778 | // Update loop hierarchy: the old header now resides in the same outer loop |
| 779 | // as the old preheader. Note that we don't bother putting sequential |
| 780 | // loops back in the hierarchy at this point. |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 781 | header->SetLoopInformation(preheader->GetLoopInformation()); // outward |
| 782 | node->loop_info = vloop; |
| 783 | } |
| 784 | |
| 785 | void HLoopOptimization::GenerateNewLoop(LoopNode* node, |
| 786 | HBasicBlock* block, |
| 787 | HBasicBlock* new_preheader, |
| 788 | HInstruction* lo, |
| 789 | HInstruction* hi, |
Aart Bik | 14a68b4 | 2017-06-08 14:06:58 -0700 | [diff] [blame] | 790 | HInstruction* step, |
| 791 | uint32_t unroll) { |
| 792 | DCHECK(unroll == 1 || vector_mode_ == kVector); |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 793 | Primitive::Type induc_type = Primitive::kPrimInt; |
| 794 | // Prepare new loop. |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 795 | vector_preheader_ = new_preheader, |
| 796 | vector_header_ = vector_preheader_->GetSingleSuccessor(); |
| 797 | vector_body_ = vector_header_->GetSuccessors()[1]; |
Aart Bik | 14a68b4 | 2017-06-08 14:06:58 -0700 | [diff] [blame] | 798 | HPhi* phi = new (global_allocator_) HPhi(global_allocator_, |
| 799 | kNoRegNumber, |
| 800 | 0, |
| 801 | HPhi::ToPhiType(induc_type)); |
Aart Bik | b07d1bc | 2017-04-05 10:03:15 -0700 | [diff] [blame] | 802 | // Generate header and prepare body. |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 803 | // for (i = lo; i < hi; i += step) |
| 804 | // <loop-body> |
Aart Bik | 14a68b4 | 2017-06-08 14:06:58 -0700 | [diff] [blame] | 805 | HInstruction* cond = new (global_allocator_) HAboveOrEqual(phi, hi); |
| 806 | vector_header_->AddPhi(phi); |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 807 | vector_header_->AddInstruction(cond); |
| 808 | vector_header_->AddInstruction(new (global_allocator_) HIf(cond)); |
Aart Bik | 14a68b4 | 2017-06-08 14:06:58 -0700 | [diff] [blame] | 809 | vector_index_ = phi; |
| 810 | for (uint32_t u = 0; u < unroll; u++) { |
| 811 | // Clear map, leaving loop invariants setup during unrolling. |
| 812 | if (u == 0) { |
| 813 | vector_map_->clear(); |
| 814 | } else { |
| 815 | for (auto i = vector_map_->begin(); i != vector_map_->end(); ) { |
| 816 | if (i->second->IsVecReplicateScalar()) { |
| 817 | DCHECK(node->loop_info->IsDefinedOutOfTheLoop(i->first)); |
| 818 | ++i; |
| 819 | } else { |
| 820 | i = vector_map_->erase(i); |
| 821 | } |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 822 | } |
| 823 | } |
Aart Bik | 14a68b4 | 2017-06-08 14:06:58 -0700 | [diff] [blame] | 824 | // Generate instruction map. |
| 825 | for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) { |
| 826 | bool vectorized_def = VectorizeDef(node, it.Current(), /*generate_code*/ true); |
| 827 | DCHECK(vectorized_def); |
| 828 | } |
| 829 | // Generate body from the instruction map, but in original program order. |
| 830 | HEnvironment* env = vector_header_->GetFirstInstruction()->GetEnvironment(); |
| 831 | for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) { |
| 832 | auto i = vector_map_->find(it.Current()); |
| 833 | if (i != vector_map_->end() && !i->second->IsInBlock()) { |
| 834 | Insert(vector_body_, i->second); |
| 835 | // Deal with instructions that need an environment, such as the scalar intrinsics. |
| 836 | if (i->second->NeedsEnvironment()) { |
| 837 | i->second->CopyEnvironmentFromWithLoopPhiAdjustment(env, vector_header_); |
| 838 | } |
| 839 | } |
| 840 | } |
| 841 | vector_index_ = new (global_allocator_) HAdd(induc_type, vector_index_, step); |
| 842 | Insert(vector_body_, vector_index_); |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 843 | } |
Aart Bik | 14a68b4 | 2017-06-08 14:06:58 -0700 | [diff] [blame] | 844 | // Finalize phi for the loop index. |
| 845 | phi->AddInput(lo); |
| 846 | phi->AddInput(vector_index_); |
| 847 | vector_index_ = phi; |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | // TODO: accept reductions at left-hand-side, mixed-type store idioms, etc. |
| 851 | bool HLoopOptimization::VectorizeDef(LoopNode* node, |
| 852 | HInstruction* instruction, |
| 853 | bool generate_code) { |
| 854 | // Accept a left-hand-side array base[index] for |
| 855 | // (1) supported vector type, |
| 856 | // (2) loop-invariant base, |
| 857 | // (3) unit stride index, |
| 858 | // (4) vectorizable right-hand-side value. |
| 859 | uint64_t restrictions = kNone; |
| 860 | if (instruction->IsArraySet()) { |
| 861 | Primitive::Type type = instruction->AsArraySet()->GetComponentType(); |
| 862 | HInstruction* base = instruction->InputAt(0); |
| 863 | HInstruction* index = instruction->InputAt(1); |
| 864 | HInstruction* value = instruction->InputAt(2); |
| 865 | HInstruction* offset = nullptr; |
| 866 | if (TrySetVectorType(type, &restrictions) && |
| 867 | node->loop_info->IsDefinedOutOfTheLoop(base) && |
Aart Bik | 37dc4df | 2017-06-28 14:08:00 -0700 | [diff] [blame] | 868 | induction_range_.IsUnitStride(instruction, index, graph_, &offset) && |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 869 | VectorizeUse(node, value, generate_code, type, restrictions)) { |
| 870 | if (generate_code) { |
| 871 | GenerateVecSub(index, offset); |
Aart Bik | 14a68b4 | 2017-06-08 14:06:58 -0700 | [diff] [blame] | 872 | GenerateVecMem(instruction, vector_map_->Get(index), vector_map_->Get(value), offset, type); |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 873 | } else { |
| 874 | vector_refs_->insert(ArrayReference(base, offset, type, /*lhs*/ true)); |
| 875 | } |
Aart Bik | 6b69e0a | 2017-01-11 10:20:43 -0800 | [diff] [blame] | 876 | return true; |
| 877 | } |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 878 | return false; |
| 879 | } |
| 880 | // Branch back okay. |
| 881 | if (instruction->IsGoto()) { |
| 882 | return true; |
| 883 | } |
| 884 | // Otherwise accept only expressions with no effects outside the immediate loop-body. |
| 885 | // Note that actual uses are inspected during right-hand-side tree traversal. |
| 886 | return !IsUsedOutsideLoop(node->loop_info, instruction) && !instruction->DoesAnyWrite(); |
| 887 | } |
| 888 | |
Aart Bik | 304c8a5 | 2017-05-23 11:01:13 -0700 | [diff] [blame] | 889 | // TODO: saturation arithmetic. |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 890 | bool HLoopOptimization::VectorizeUse(LoopNode* node, |
| 891 | HInstruction* instruction, |
| 892 | bool generate_code, |
| 893 | Primitive::Type type, |
| 894 | uint64_t restrictions) { |
| 895 | // Accept anything for which code has already been generated. |
| 896 | if (generate_code) { |
| 897 | if (vector_map_->find(instruction) != vector_map_->end()) { |
| 898 | return true; |
| 899 | } |
| 900 | } |
| 901 | // Continue the right-hand-side tree traversal, passing in proper |
| 902 | // types and vector restrictions along the way. During code generation, |
| 903 | // all new nodes are drawn from the global allocator. |
| 904 | if (node->loop_info->IsDefinedOutOfTheLoop(instruction)) { |
| 905 | // Accept invariant use, using scalar expansion. |
| 906 | if (generate_code) { |
| 907 | GenerateVecInv(instruction, type); |
| 908 | } |
| 909 | return true; |
| 910 | } else if (instruction->IsArrayGet()) { |
Goran Jakovljevic | 19680d3 | 2017-05-11 10:38:36 +0200 | [diff] [blame] | 911 | // Deal with vector restrictions. |
| 912 | if (instruction->AsArrayGet()->IsStringCharAt() && |
| 913 | HasVectorRestrictions(restrictions, kNoStringCharAt)) { |
| 914 | return false; |
| 915 | } |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 916 | // Accept a right-hand-side array base[index] for |
| 917 | // (1) exact matching vector type, |
| 918 | // (2) loop-invariant base, |
| 919 | // (3) unit stride index, |
| 920 | // (4) vectorizable right-hand-side value. |
| 921 | HInstruction* base = instruction->InputAt(0); |
| 922 | HInstruction* index = instruction->InputAt(1); |
| 923 | HInstruction* offset = nullptr; |
| 924 | if (type == instruction->GetType() && |
| 925 | node->loop_info->IsDefinedOutOfTheLoop(base) && |
Aart Bik | 37dc4df | 2017-06-28 14:08:00 -0700 | [diff] [blame] | 926 | induction_range_.IsUnitStride(instruction, index, graph_, &offset)) { |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 927 | if (generate_code) { |
| 928 | GenerateVecSub(index, offset); |
Aart Bik | 14a68b4 | 2017-06-08 14:06:58 -0700 | [diff] [blame] | 929 | GenerateVecMem(instruction, vector_map_->Get(index), nullptr, offset, type); |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 930 | } else { |
| 931 | vector_refs_->insert(ArrayReference(base, offset, type, /*lhs*/ false)); |
| 932 | } |
| 933 | return true; |
| 934 | } |
| 935 | } else if (instruction->IsTypeConversion()) { |
| 936 | // Accept particular type conversions. |
| 937 | HTypeConversion* conversion = instruction->AsTypeConversion(); |
| 938 | HInstruction* opa = conversion->InputAt(0); |
| 939 | Primitive::Type from = conversion->GetInputType(); |
| 940 | Primitive::Type to = conversion->GetResultType(); |
| 941 | if ((to == Primitive::kPrimByte || |
| 942 | to == Primitive::kPrimChar || |
| 943 | to == Primitive::kPrimShort) && from == Primitive::kPrimInt) { |
| 944 | // Accept a "narrowing" type conversion from a "wider" computation for |
| 945 | // (1) conversion into final required type, |
| 946 | // (2) vectorizable operand, |
| 947 | // (3) "wider" operations cannot bring in higher order bits. |
| 948 | if (to == type && VectorizeUse(node, opa, generate_code, type, restrictions | kNoHiBits)) { |
| 949 | if (generate_code) { |
| 950 | if (vector_mode_ == kVector) { |
| 951 | vector_map_->Put(instruction, vector_map_->Get(opa)); // operand pass-through |
| 952 | } else { |
| 953 | GenerateVecOp(instruction, vector_map_->Get(opa), nullptr, type); |
| 954 | } |
| 955 | } |
| 956 | return true; |
| 957 | } |
| 958 | } else if (to == Primitive::kPrimFloat && from == Primitive::kPrimInt) { |
| 959 | DCHECK_EQ(to, type); |
| 960 | // Accept int to float conversion for |
| 961 | // (1) supported int, |
| 962 | // (2) vectorizable operand. |
| 963 | if (TrySetVectorType(from, &restrictions) && |
| 964 | VectorizeUse(node, opa, generate_code, from, restrictions)) { |
| 965 | if (generate_code) { |
| 966 | GenerateVecOp(instruction, vector_map_->Get(opa), nullptr, type); |
| 967 | } |
| 968 | return true; |
| 969 | } |
| 970 | } |
| 971 | return false; |
| 972 | } else if (instruction->IsNeg() || instruction->IsNot() || instruction->IsBooleanNot()) { |
| 973 | // Accept unary operator for vectorizable operand. |
| 974 | HInstruction* opa = instruction->InputAt(0); |
| 975 | if (VectorizeUse(node, opa, generate_code, type, restrictions)) { |
| 976 | if (generate_code) { |
| 977 | GenerateVecOp(instruction, vector_map_->Get(opa), nullptr, type); |
| 978 | } |
| 979 | return true; |
| 980 | } |
| 981 | } else if (instruction->IsAdd() || instruction->IsSub() || |
| 982 | instruction->IsMul() || instruction->IsDiv() || |
| 983 | instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) { |
| 984 | // Deal with vector restrictions. |
| 985 | if ((instruction->IsMul() && HasVectorRestrictions(restrictions, kNoMul)) || |
| 986 | (instruction->IsDiv() && HasVectorRestrictions(restrictions, kNoDiv))) { |
| 987 | return false; |
| 988 | } |
| 989 | // Accept binary operator for vectorizable operands. |
| 990 | HInstruction* opa = instruction->InputAt(0); |
| 991 | HInstruction* opb = instruction->InputAt(1); |
| 992 | if (VectorizeUse(node, opa, generate_code, type, restrictions) && |
| 993 | VectorizeUse(node, opb, generate_code, type, restrictions)) { |
| 994 | if (generate_code) { |
| 995 | GenerateVecOp(instruction, vector_map_->Get(opa), vector_map_->Get(opb), type); |
| 996 | } |
| 997 | return true; |
| 998 | } |
| 999 | } else if (instruction->IsShl() || instruction->IsShr() || instruction->IsUShr()) { |
Aart Bik | f3e61ee | 2017-04-12 17:09:20 -0700 | [diff] [blame] | 1000 | // Recognize vectorization idioms. |
| 1001 | if (VectorizeHalvingAddIdiom(node, instruction, generate_code, type, restrictions)) { |
| 1002 | return true; |
| 1003 | } |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 1004 | // Deal with vector restrictions. |
Aart Bik | 304c8a5 | 2017-05-23 11:01:13 -0700 | [diff] [blame] | 1005 | HInstruction* opa = instruction->InputAt(0); |
| 1006 | HInstruction* opb = instruction->InputAt(1); |
| 1007 | HInstruction* r = opa; |
| 1008 | bool is_unsigned = false; |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 1009 | if ((HasVectorRestrictions(restrictions, kNoShift)) || |
| 1010 | (instruction->IsShr() && HasVectorRestrictions(restrictions, kNoShr))) { |
| 1011 | return false; // unsupported instruction |
Aart Bik | 304c8a5 | 2017-05-23 11:01:13 -0700 | [diff] [blame] | 1012 | } else if (HasVectorRestrictions(restrictions, kNoHiBits)) { |
| 1013 | // Shifts right need extra care to account for higher order bits. |
| 1014 | // TODO: less likely shr/unsigned and ushr/signed can by flipping signess. |
| 1015 | if (instruction->IsShr() && |
| 1016 | (!IsNarrowerOperand(opa, type, &r, &is_unsigned) || is_unsigned)) { |
| 1017 | return false; // reject, unless all operands are sign-extension narrower |
| 1018 | } else if (instruction->IsUShr() && |
| 1019 | (!IsNarrowerOperand(opa, type, &r, &is_unsigned) || !is_unsigned)) { |
| 1020 | return false; // reject, unless all operands are zero-extension narrower |
| 1021 | } |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 1022 | } |
| 1023 | // Accept shift operator for vectorizable/invariant operands. |
| 1024 | // TODO: accept symbolic, albeit loop invariant shift factors. |
Aart Bik | 304c8a5 | 2017-05-23 11:01:13 -0700 | [diff] [blame] | 1025 | DCHECK(r != nullptr); |
| 1026 | if (generate_code && vector_mode_ != kVector) { // de-idiom |
| 1027 | r = opa; |
| 1028 | } |
Aart Bik | 50e20d5 | 2017-05-05 14:07:29 -0700 | [diff] [blame] | 1029 | int64_t distance = 0; |
Aart Bik | 304c8a5 | 2017-05-23 11:01:13 -0700 | [diff] [blame] | 1030 | if (VectorizeUse(node, r, generate_code, type, restrictions) && |
Aart Bik | 50e20d5 | 2017-05-05 14:07:29 -0700 | [diff] [blame] | 1031 | IsInt64AndGet(opb, /*out*/ &distance)) { |
Aart Bik | 65ffd8e | 2017-05-01 16:50:45 -0700 | [diff] [blame] | 1032 | // Restrict shift distance to packed data type width. |
| 1033 | int64_t max_distance = Primitive::ComponentSize(type) * 8; |
| 1034 | if (0 <= distance && distance < max_distance) { |
| 1035 | if (generate_code) { |
Aart Bik | 304c8a5 | 2017-05-23 11:01:13 -0700 | [diff] [blame] | 1036 | GenerateVecOp(instruction, vector_map_->Get(r), opb, type); |
Aart Bik | 65ffd8e | 2017-05-01 16:50:45 -0700 | [diff] [blame] | 1037 | } |
| 1038 | return true; |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 1039 | } |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 1040 | } |
| 1041 | } else if (instruction->IsInvokeStaticOrDirect()) { |
Aart Bik | 6daebeb | 2017-04-03 14:35:41 -0700 | [diff] [blame] | 1042 | // Accept particular intrinsics. |
| 1043 | HInvokeStaticOrDirect* invoke = instruction->AsInvokeStaticOrDirect(); |
| 1044 | switch (invoke->GetIntrinsic()) { |
| 1045 | case Intrinsics::kMathAbsInt: |
| 1046 | case Intrinsics::kMathAbsLong: |
| 1047 | case Intrinsics::kMathAbsFloat: |
| 1048 | case Intrinsics::kMathAbsDouble: { |
| 1049 | // Deal with vector restrictions. |
Aart Bik | 304c8a5 | 2017-05-23 11:01:13 -0700 | [diff] [blame] | 1050 | HInstruction* opa = instruction->InputAt(0); |
| 1051 | HInstruction* r = opa; |
| 1052 | bool is_unsigned = false; |
| 1053 | if (HasVectorRestrictions(restrictions, kNoAbs)) { |
Aart Bik | 6daebeb | 2017-04-03 14:35:41 -0700 | [diff] [blame] | 1054 | return false; |
Aart Bik | 304c8a5 | 2017-05-23 11:01:13 -0700 | [diff] [blame] | 1055 | } else if (HasVectorRestrictions(restrictions, kNoHiBits) && |
| 1056 | (!IsNarrowerOperand(opa, type, &r, &is_unsigned) || is_unsigned)) { |
| 1057 | return false; // reject, unless operand is sign-extension narrower |
Aart Bik | 6daebeb | 2017-04-03 14:35:41 -0700 | [diff] [blame] | 1058 | } |
| 1059 | // Accept ABS(x) for vectorizable operand. |
Aart Bik | 304c8a5 | 2017-05-23 11:01:13 -0700 | [diff] [blame] | 1060 | DCHECK(r != nullptr); |
| 1061 | if (generate_code && vector_mode_ != kVector) { // de-idiom |
| 1062 | r = opa; |
| 1063 | } |
| 1064 | if (VectorizeUse(node, r, generate_code, type, restrictions)) { |
Aart Bik | 6daebeb | 2017-04-03 14:35:41 -0700 | [diff] [blame] | 1065 | if (generate_code) { |
Aart Bik | 304c8a5 | 2017-05-23 11:01:13 -0700 | [diff] [blame] | 1066 | GenerateVecOp(instruction, vector_map_->Get(r), nullptr, type); |
Aart Bik | 6daebeb | 2017-04-03 14:35:41 -0700 | [diff] [blame] | 1067 | } |
| 1068 | return true; |
| 1069 | } |
| 1070 | return false; |
| 1071 | } |
Aart Bik | c8e93c7 | 2017-05-10 10:49:22 -0700 | [diff] [blame] | 1072 | case Intrinsics::kMathMinIntInt: |
| 1073 | case Intrinsics::kMathMinLongLong: |
| 1074 | case Intrinsics::kMathMinFloatFloat: |
| 1075 | case Intrinsics::kMathMinDoubleDouble: |
| 1076 | case Intrinsics::kMathMaxIntInt: |
| 1077 | case Intrinsics::kMathMaxLongLong: |
| 1078 | case Intrinsics::kMathMaxFloatFloat: |
| 1079 | case Intrinsics::kMathMaxDoubleDouble: { |
| 1080 | // Deal with vector restrictions. |
Nicolas Geoffray | 9231690 | 2017-05-23 08:06:07 +0000 | [diff] [blame] | 1081 | HInstruction* opa = instruction->InputAt(0); |
| 1082 | HInstruction* opb = instruction->InputAt(1); |
Aart Bik | 304c8a5 | 2017-05-23 11:01:13 -0700 | [diff] [blame] | 1083 | HInstruction* r = opa; |
| 1084 | HInstruction* s = opb; |
| 1085 | bool is_unsigned = false; |
| 1086 | if (HasVectorRestrictions(restrictions, kNoMinMax)) { |
| 1087 | return false; |
| 1088 | } else if (HasVectorRestrictions(restrictions, kNoHiBits) && |
| 1089 | !IsNarrowerOperands(opa, opb, type, &r, &s, &is_unsigned)) { |
| 1090 | return false; // reject, unless all operands are same-extension narrower |
| 1091 | } |
| 1092 | // Accept MIN/MAX(x, y) for vectorizable operands. |
| 1093 | DCHECK(r != nullptr && s != nullptr); |
| 1094 | if (generate_code && vector_mode_ != kVector) { // de-idiom |
| 1095 | r = opa; |
| 1096 | s = opb; |
| 1097 | } |
| 1098 | if (VectorizeUse(node, r, generate_code, type, restrictions) && |
| 1099 | VectorizeUse(node, s, generate_code, type, restrictions)) { |
Aart Bik | c8e93c7 | 2017-05-10 10:49:22 -0700 | [diff] [blame] | 1100 | if (generate_code) { |
Aart Bik | 304c8a5 | 2017-05-23 11:01:13 -0700 | [diff] [blame] | 1101 | GenerateVecOp( |
| 1102 | instruction, vector_map_->Get(r), vector_map_->Get(s), type, is_unsigned); |
Aart Bik | c8e93c7 | 2017-05-10 10:49:22 -0700 | [diff] [blame] | 1103 | } |
| 1104 | return true; |
| 1105 | } |
| 1106 | return false; |
| 1107 | } |
Aart Bik | 6daebeb | 2017-04-03 14:35:41 -0700 | [diff] [blame] | 1108 | default: |
| 1109 | return false; |
| 1110 | } // switch |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 1111 | } |
Aart Bik | 6b69e0a | 2017-01-11 10:20:43 -0800 | [diff] [blame] | 1112 | return false; |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 1113 | } |
| 1114 | |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 1115 | bool HLoopOptimization::TrySetVectorType(Primitive::Type type, uint64_t* restrictions) { |
| 1116 | const InstructionSetFeatures* features = compiler_driver_->GetInstructionSetFeatures(); |
| 1117 | switch (compiler_driver_->GetInstructionSet()) { |
| 1118 | case kArm: |
| 1119 | case kThumb2: |
Artem Serov | 8f7c410 | 2017-06-21 11:21:37 +0100 | [diff] [blame] | 1120 | // Allow vectorization for all ARM devices, because Android assumes that |
| 1121 | // ARM 32-bit always supports advanced SIMD. |
| 1122 | switch (type) { |
| 1123 | case Primitive::kPrimBoolean: |
| 1124 | case Primitive::kPrimByte: |
| 1125 | *restrictions |= kNoDiv; |
| 1126 | return TrySetVectorLength(8); |
| 1127 | case Primitive::kPrimChar: |
| 1128 | case Primitive::kPrimShort: |
| 1129 | *restrictions |= kNoDiv | kNoStringCharAt; |
| 1130 | return TrySetVectorLength(4); |
| 1131 | case Primitive::kPrimInt: |
| 1132 | *restrictions |= kNoDiv; |
| 1133 | return TrySetVectorLength(2); |
| 1134 | default: |
| 1135 | break; |
| 1136 | } |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 1137 | return false; |
| 1138 | case kArm64: |
| 1139 | // Allow vectorization for all ARM devices, because Android assumes that |
Artem Serov | d4bccf1 | 2017-04-03 18:47:32 +0100 | [diff] [blame] | 1140 | // ARMv8 AArch64 always supports advanced SIMD. |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 1141 | switch (type) { |
| 1142 | case Primitive::kPrimBoolean: |
| 1143 | case Primitive::kPrimByte: |
Aart Bik | 304c8a5 | 2017-05-23 11:01:13 -0700 | [diff] [blame] | 1144 | *restrictions |= kNoDiv; |
Artem Serov | d4bccf1 | 2017-04-03 18:47:32 +0100 | [diff] [blame] | 1145 | return TrySetVectorLength(16); |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 1146 | case Primitive::kPrimChar: |
| 1147 | case Primitive::kPrimShort: |
Aart Bik | 304c8a5 | 2017-05-23 11:01:13 -0700 | [diff] [blame] | 1148 | *restrictions |= kNoDiv; |
Artem Serov | d4bccf1 | 2017-04-03 18:47:32 +0100 | [diff] [blame] | 1149 | return TrySetVectorLength(8); |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 1150 | case Primitive::kPrimInt: |
| 1151 | *restrictions |= kNoDiv; |
Artem Serov | d4bccf1 | 2017-04-03 18:47:32 +0100 | [diff] [blame] | 1152 | return TrySetVectorLength(4); |
Artem Serov | b31f91f | 2017-04-05 11:31:19 +0100 | [diff] [blame] | 1153 | case Primitive::kPrimLong: |
Aart Bik | c8e93c7 | 2017-05-10 10:49:22 -0700 | [diff] [blame] | 1154 | *restrictions |= kNoDiv | kNoMul | kNoMinMax; |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 1155 | return TrySetVectorLength(2); |
| 1156 | case Primitive::kPrimFloat: |
Artem Serov | d4bccf1 | 2017-04-03 18:47:32 +0100 | [diff] [blame] | 1157 | return TrySetVectorLength(4); |
Artem Serov | b31f91f | 2017-04-05 11:31:19 +0100 | [diff] [blame] | 1158 | case Primitive::kPrimDouble: |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 1159 | return TrySetVectorLength(2); |
| 1160 | default: |
| 1161 | return false; |
| 1162 | } |
| 1163 | case kX86: |
| 1164 | case kX86_64: |
| 1165 | // Allow vectorization for SSE4-enabled X86 devices only (128-bit vectors). |
| 1166 | if (features->AsX86InstructionSetFeatures()->HasSSE4_1()) { |
| 1167 | switch (type) { |
| 1168 | case Primitive::kPrimBoolean: |
| 1169 | case Primitive::kPrimByte: |
Aart Bik | f3e61ee | 2017-04-12 17:09:20 -0700 | [diff] [blame] | 1170 | *restrictions |= kNoMul | kNoDiv | kNoShift | kNoAbs | kNoSignedHAdd | kNoUnroundedHAdd; |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 1171 | return TrySetVectorLength(16); |
| 1172 | case Primitive::kPrimChar: |
| 1173 | case Primitive::kPrimShort: |
Aart Bik | f3e61ee | 2017-04-12 17:09:20 -0700 | [diff] [blame] | 1174 | *restrictions |= kNoDiv | kNoAbs | kNoSignedHAdd | kNoUnroundedHAdd; |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 1175 | return TrySetVectorLength(8); |
| 1176 | case Primitive::kPrimInt: |
| 1177 | *restrictions |= kNoDiv; |
| 1178 | return TrySetVectorLength(4); |
| 1179 | case Primitive::kPrimLong: |
Aart Bik | c8e93c7 | 2017-05-10 10:49:22 -0700 | [diff] [blame] | 1180 | *restrictions |= kNoMul | kNoDiv | kNoShr | kNoAbs | kNoMinMax; |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 1181 | return TrySetVectorLength(2); |
| 1182 | case Primitive::kPrimFloat: |
Aart Bik | c8e93c7 | 2017-05-10 10:49:22 -0700 | [diff] [blame] | 1183 | *restrictions |= kNoMinMax; // -0.0 vs +0.0 |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 1184 | return TrySetVectorLength(4); |
| 1185 | case Primitive::kPrimDouble: |
Aart Bik | c8e93c7 | 2017-05-10 10:49:22 -0700 | [diff] [blame] | 1186 | *restrictions |= kNoMinMax; // -0.0 vs +0.0 |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 1187 | return TrySetVectorLength(2); |
| 1188 | default: |
| 1189 | break; |
| 1190 | } // switch type |
| 1191 | } |
| 1192 | return false; |
| 1193 | case kMips: |
Lena Djokic | 51765b0 | 2017-06-22 13:49:59 +0200 | [diff] [blame] | 1194 | if (features->AsMipsInstructionSetFeatures()->HasMsa()) { |
| 1195 | switch (type) { |
| 1196 | case Primitive::kPrimBoolean: |
| 1197 | case Primitive::kPrimByte: |
| 1198 | *restrictions |= kNoDiv; |
| 1199 | return TrySetVectorLength(16); |
| 1200 | case Primitive::kPrimChar: |
| 1201 | case Primitive::kPrimShort: |
| 1202 | *restrictions |= kNoDiv | kNoStringCharAt; |
| 1203 | return TrySetVectorLength(8); |
| 1204 | case Primitive::kPrimInt: |
| 1205 | *restrictions |= kNoDiv; |
| 1206 | return TrySetVectorLength(4); |
| 1207 | case Primitive::kPrimLong: |
| 1208 | *restrictions |= kNoDiv; |
| 1209 | return TrySetVectorLength(2); |
| 1210 | case Primitive::kPrimFloat: |
| 1211 | *restrictions |= kNoMinMax; // min/max(x, NaN) |
| 1212 | return TrySetVectorLength(4); |
| 1213 | case Primitive::kPrimDouble: |
| 1214 | *restrictions |= kNoMinMax; // min/max(x, NaN) |
| 1215 | return TrySetVectorLength(2); |
| 1216 | default: |
| 1217 | break; |
| 1218 | } // switch type |
| 1219 | } |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 1220 | return false; |
Goran Jakovljevic | 19680d3 | 2017-05-11 10:38:36 +0200 | [diff] [blame] | 1221 | case kMips64: |
| 1222 | if (features->AsMips64InstructionSetFeatures()->HasMsa()) { |
| 1223 | switch (type) { |
| 1224 | case Primitive::kPrimBoolean: |
| 1225 | case Primitive::kPrimByte: |
Goran Jakovljevic | 8fea1e1 | 2017-06-06 13:28:42 +0200 | [diff] [blame] | 1226 | *restrictions |= kNoDiv; |
Goran Jakovljevic | 19680d3 | 2017-05-11 10:38:36 +0200 | [diff] [blame] | 1227 | return TrySetVectorLength(16); |
| 1228 | case Primitive::kPrimChar: |
| 1229 | case Primitive::kPrimShort: |
Goran Jakovljevic | 8fea1e1 | 2017-06-06 13:28:42 +0200 | [diff] [blame] | 1230 | *restrictions |= kNoDiv | kNoStringCharAt; |
Goran Jakovljevic | 19680d3 | 2017-05-11 10:38:36 +0200 | [diff] [blame] | 1231 | return TrySetVectorLength(8); |
| 1232 | case Primitive::kPrimInt: |
Goran Jakovljevic | 8fea1e1 | 2017-06-06 13:28:42 +0200 | [diff] [blame] | 1233 | *restrictions |= kNoDiv; |
Goran Jakovljevic | 19680d3 | 2017-05-11 10:38:36 +0200 | [diff] [blame] | 1234 | return TrySetVectorLength(4); |
| 1235 | case Primitive::kPrimLong: |
Goran Jakovljevic | 8fea1e1 | 2017-06-06 13:28:42 +0200 | [diff] [blame] | 1236 | *restrictions |= kNoDiv; |
Goran Jakovljevic | 19680d3 | 2017-05-11 10:38:36 +0200 | [diff] [blame] | 1237 | return TrySetVectorLength(2); |
| 1238 | case Primitive::kPrimFloat: |
Goran Jakovljevic | 8fea1e1 | 2017-06-06 13:28:42 +0200 | [diff] [blame] | 1239 | *restrictions |= kNoMinMax; // min/max(x, NaN) |
Goran Jakovljevic | 19680d3 | 2017-05-11 10:38:36 +0200 | [diff] [blame] | 1240 | return TrySetVectorLength(4); |
| 1241 | case Primitive::kPrimDouble: |
Goran Jakovljevic | 8fea1e1 | 2017-06-06 13:28:42 +0200 | [diff] [blame] | 1242 | *restrictions |= kNoMinMax; // min/max(x, NaN) |
Goran Jakovljevic | 19680d3 | 2017-05-11 10:38:36 +0200 | [diff] [blame] | 1243 | return TrySetVectorLength(2); |
| 1244 | default: |
| 1245 | break; |
| 1246 | } // switch type |
| 1247 | } |
| 1248 | return false; |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 1249 | default: |
| 1250 | return false; |
| 1251 | } // switch instruction set |
| 1252 | } |
| 1253 | |
| 1254 | bool HLoopOptimization::TrySetVectorLength(uint32_t length) { |
| 1255 | DCHECK(IsPowerOfTwo(length) && length >= 2u); |
| 1256 | // First time set? |
| 1257 | if (vector_length_ == 0) { |
| 1258 | vector_length_ = length; |
| 1259 | } |
| 1260 | // Different types are acceptable within a loop-body, as long as all the corresponding vector |
| 1261 | // lengths match exactly to obtain a uniform traversal through the vector iteration space |
| 1262 | // (idiomatic exceptions to this rule can be handled by further unrolling sub-expressions). |
| 1263 | return vector_length_ == length; |
| 1264 | } |
| 1265 | |
| 1266 | void HLoopOptimization::GenerateVecInv(HInstruction* org, Primitive::Type type) { |
| 1267 | if (vector_map_->find(org) == vector_map_->end()) { |
| 1268 | // In scalar code, just use a self pass-through for scalar invariants |
| 1269 | // (viz. expression remains itself). |
| 1270 | if (vector_mode_ == kSequential) { |
| 1271 | vector_map_->Put(org, org); |
| 1272 | return; |
| 1273 | } |
| 1274 | // In vector code, explicit scalar expansion is needed. |
| 1275 | HInstruction* vector = new (global_allocator_) HVecReplicateScalar( |
| 1276 | global_allocator_, org, type, vector_length_); |
| 1277 | vector_map_->Put(org, Insert(vector_preheader_, vector)); |
| 1278 | } |
| 1279 | } |
| 1280 | |
| 1281 | void HLoopOptimization::GenerateVecSub(HInstruction* org, HInstruction* offset) { |
| 1282 | if (vector_map_->find(org) == vector_map_->end()) { |
Aart Bik | 14a68b4 | 2017-06-08 14:06:58 -0700 | [diff] [blame] | 1283 | HInstruction* subscript = vector_index_; |
Aart Bik | 37dc4df | 2017-06-28 14:08:00 -0700 | [diff] [blame] | 1284 | int64_t value = 0; |
| 1285 | if (!IsInt64AndGet(offset, &value) || value != 0) { |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 1286 | subscript = new (global_allocator_) HAdd(Primitive::kPrimInt, subscript, offset); |
| 1287 | if (org->IsPhi()) { |
| 1288 | Insert(vector_body_, subscript); // lacks layout placeholder |
| 1289 | } |
| 1290 | } |
| 1291 | vector_map_->Put(org, subscript); |
| 1292 | } |
| 1293 | } |
| 1294 | |
| 1295 | void HLoopOptimization::GenerateVecMem(HInstruction* org, |
| 1296 | HInstruction* opa, |
| 1297 | HInstruction* opb, |
Aart Bik | 14a68b4 | 2017-06-08 14:06:58 -0700 | [diff] [blame] | 1298 | HInstruction* offset, |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 1299 | Primitive::Type type) { |
| 1300 | HInstruction* vector = nullptr; |
| 1301 | if (vector_mode_ == kVector) { |
| 1302 | // Vector store or load. |
Aart Bik | 14a68b4 | 2017-06-08 14:06:58 -0700 | [diff] [blame] | 1303 | HInstruction* base = org->InputAt(0); |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 1304 | if (opb != nullptr) { |
| 1305 | vector = new (global_allocator_) HVecStore( |
Aart Bik | 14a68b4 | 2017-06-08 14:06:58 -0700 | [diff] [blame] | 1306 | global_allocator_, base, opa, opb, type, vector_length_); |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 1307 | } else { |
Aart Bik | db14fcf | 2017-04-25 15:53:58 -0700 | [diff] [blame] | 1308 | bool is_string_char_at = org->AsArrayGet()->IsStringCharAt(); |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 1309 | vector = new (global_allocator_) HVecLoad( |
Aart Bik | 14a68b4 | 2017-06-08 14:06:58 -0700 | [diff] [blame] | 1310 | global_allocator_, base, opa, type, vector_length_, is_string_char_at); |
| 1311 | } |
| 1312 | // Known dynamically enforced alignment? |
| 1313 | // TODO: detect offset + constant differences. |
| 1314 | // TODO: long run, static alignment analysis? |
| 1315 | if (vector_peeling_candidate_ != nullptr && |
| 1316 | vector_peeling_candidate_->base == base && |
| 1317 | vector_peeling_candidate_->offset == offset) { |
| 1318 | vector->AsVecMemoryOperation()->SetAlignment(Alignment(kAlignedBase, 0)); |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 1319 | } |
| 1320 | } else { |
| 1321 | // Scalar store or load. |
| 1322 | DCHECK(vector_mode_ == kSequential); |
| 1323 | if (opb != nullptr) { |
| 1324 | vector = new (global_allocator_) HArraySet(org->InputAt(0), opa, opb, type, kNoDexPc); |
| 1325 | } else { |
Aart Bik | db14fcf | 2017-04-25 15:53:58 -0700 | [diff] [blame] | 1326 | bool is_string_char_at = org->AsArrayGet()->IsStringCharAt(); |
| 1327 | vector = new (global_allocator_) HArrayGet( |
| 1328 | org->InputAt(0), opa, type, kNoDexPc, is_string_char_at); |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 1329 | } |
| 1330 | } |
| 1331 | vector_map_->Put(org, vector); |
| 1332 | } |
| 1333 | |
| 1334 | #define GENERATE_VEC(x, y) \ |
| 1335 | if (vector_mode_ == kVector) { \ |
| 1336 | vector = (x); \ |
| 1337 | } else { \ |
| 1338 | DCHECK(vector_mode_ == kSequential); \ |
| 1339 | vector = (y); \ |
| 1340 | } \ |
| 1341 | break; |
| 1342 | |
| 1343 | void HLoopOptimization::GenerateVecOp(HInstruction* org, |
| 1344 | HInstruction* opa, |
| 1345 | HInstruction* opb, |
Aart Bik | 304c8a5 | 2017-05-23 11:01:13 -0700 | [diff] [blame] | 1346 | Primitive::Type type, |
| 1347 | bool is_unsigned) { |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 1348 | if (vector_mode_ == kSequential) { |
Aart Bik | 304c8a5 | 2017-05-23 11:01:13 -0700 | [diff] [blame] | 1349 | // Non-converting scalar code follows implicit integral promotion. |
| 1350 | if (!org->IsTypeConversion() && (type == Primitive::kPrimBoolean || |
| 1351 | type == Primitive::kPrimByte || |
| 1352 | type == Primitive::kPrimChar || |
| 1353 | type == Primitive::kPrimShort)) { |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 1354 | type = Primitive::kPrimInt; |
| 1355 | } |
| 1356 | } |
| 1357 | HInstruction* vector = nullptr; |
| 1358 | switch (org->GetKind()) { |
| 1359 | case HInstruction::kNeg: |
| 1360 | DCHECK(opb == nullptr); |
| 1361 | GENERATE_VEC( |
| 1362 | new (global_allocator_) HVecNeg(global_allocator_, opa, type, vector_length_), |
| 1363 | new (global_allocator_) HNeg(type, opa)); |
| 1364 | case HInstruction::kNot: |
| 1365 | DCHECK(opb == nullptr); |
| 1366 | GENERATE_VEC( |
| 1367 | new (global_allocator_) HVecNot(global_allocator_, opa, type, vector_length_), |
| 1368 | new (global_allocator_) HNot(type, opa)); |
| 1369 | case HInstruction::kBooleanNot: |
| 1370 | DCHECK(opb == nullptr); |
| 1371 | GENERATE_VEC( |
| 1372 | new (global_allocator_) HVecNot(global_allocator_, opa, type, vector_length_), |
| 1373 | new (global_allocator_) HBooleanNot(opa)); |
| 1374 | case HInstruction::kTypeConversion: |
| 1375 | DCHECK(opb == nullptr); |
| 1376 | GENERATE_VEC( |
| 1377 | new (global_allocator_) HVecCnv(global_allocator_, opa, type, vector_length_), |
| 1378 | new (global_allocator_) HTypeConversion(type, opa, kNoDexPc)); |
| 1379 | case HInstruction::kAdd: |
| 1380 | GENERATE_VEC( |
| 1381 | new (global_allocator_) HVecAdd(global_allocator_, opa, opb, type, vector_length_), |
| 1382 | new (global_allocator_) HAdd(type, opa, opb)); |
| 1383 | case HInstruction::kSub: |
| 1384 | GENERATE_VEC( |
| 1385 | new (global_allocator_) HVecSub(global_allocator_, opa, opb, type, vector_length_), |
| 1386 | new (global_allocator_) HSub(type, opa, opb)); |
| 1387 | case HInstruction::kMul: |
| 1388 | GENERATE_VEC( |
| 1389 | new (global_allocator_) HVecMul(global_allocator_, opa, opb, type, vector_length_), |
| 1390 | new (global_allocator_) HMul(type, opa, opb)); |
| 1391 | case HInstruction::kDiv: |
| 1392 | GENERATE_VEC( |
| 1393 | new (global_allocator_) HVecDiv(global_allocator_, opa, opb, type, vector_length_), |
| 1394 | new (global_allocator_) HDiv(type, opa, opb, kNoDexPc)); |
| 1395 | case HInstruction::kAnd: |
| 1396 | GENERATE_VEC( |
| 1397 | new (global_allocator_) HVecAnd(global_allocator_, opa, opb, type, vector_length_), |
| 1398 | new (global_allocator_) HAnd(type, opa, opb)); |
| 1399 | case HInstruction::kOr: |
| 1400 | GENERATE_VEC( |
| 1401 | new (global_allocator_) HVecOr(global_allocator_, opa, opb, type, vector_length_), |
| 1402 | new (global_allocator_) HOr(type, opa, opb)); |
| 1403 | case HInstruction::kXor: |
| 1404 | GENERATE_VEC( |
| 1405 | new (global_allocator_) HVecXor(global_allocator_, opa, opb, type, vector_length_), |
| 1406 | new (global_allocator_) HXor(type, opa, opb)); |
| 1407 | case HInstruction::kShl: |
| 1408 | GENERATE_VEC( |
| 1409 | new (global_allocator_) HVecShl(global_allocator_, opa, opb, type, vector_length_), |
| 1410 | new (global_allocator_) HShl(type, opa, opb)); |
| 1411 | case HInstruction::kShr: |
| 1412 | GENERATE_VEC( |
| 1413 | new (global_allocator_) HVecShr(global_allocator_, opa, opb, type, vector_length_), |
| 1414 | new (global_allocator_) HShr(type, opa, opb)); |
| 1415 | case HInstruction::kUShr: |
| 1416 | GENERATE_VEC( |
| 1417 | new (global_allocator_) HVecUShr(global_allocator_, opa, opb, type, vector_length_), |
| 1418 | new (global_allocator_) HUShr(type, opa, opb)); |
| 1419 | case HInstruction::kInvokeStaticOrDirect: { |
Aart Bik | 6daebeb | 2017-04-03 14:35:41 -0700 | [diff] [blame] | 1420 | HInvokeStaticOrDirect* invoke = org->AsInvokeStaticOrDirect(); |
| 1421 | if (vector_mode_ == kVector) { |
| 1422 | switch (invoke->GetIntrinsic()) { |
| 1423 | case Intrinsics::kMathAbsInt: |
| 1424 | case Intrinsics::kMathAbsLong: |
| 1425 | case Intrinsics::kMathAbsFloat: |
| 1426 | case Intrinsics::kMathAbsDouble: |
| 1427 | DCHECK(opb == nullptr); |
| 1428 | vector = new (global_allocator_) HVecAbs(global_allocator_, opa, type, vector_length_); |
| 1429 | break; |
Aart Bik | c8e93c7 | 2017-05-10 10:49:22 -0700 | [diff] [blame] | 1430 | case Intrinsics::kMathMinIntInt: |
| 1431 | case Intrinsics::kMathMinLongLong: |
| 1432 | case Intrinsics::kMathMinFloatFloat: |
| 1433 | case Intrinsics::kMathMinDoubleDouble: { |
Aart Bik | c8e93c7 | 2017-05-10 10:49:22 -0700 | [diff] [blame] | 1434 | vector = new (global_allocator_) |
| 1435 | HVecMin(global_allocator_, opa, opb, type, vector_length_, is_unsigned); |
| 1436 | break; |
| 1437 | } |
| 1438 | case Intrinsics::kMathMaxIntInt: |
| 1439 | case Intrinsics::kMathMaxLongLong: |
| 1440 | case Intrinsics::kMathMaxFloatFloat: |
| 1441 | case Intrinsics::kMathMaxDoubleDouble: { |
Aart Bik | c8e93c7 | 2017-05-10 10:49:22 -0700 | [diff] [blame] | 1442 | vector = new (global_allocator_) |
| 1443 | HVecMax(global_allocator_, opa, opb, type, vector_length_, is_unsigned); |
| 1444 | break; |
| 1445 | } |
Aart Bik | 6daebeb | 2017-04-03 14:35:41 -0700 | [diff] [blame] | 1446 | default: |
| 1447 | LOG(FATAL) << "Unsupported SIMD intrinsic"; |
| 1448 | UNREACHABLE(); |
| 1449 | } // switch invoke |
| 1450 | } else { |
Aart Bik | 24b905f | 2017-04-06 09:59:06 -0700 | [diff] [blame] | 1451 | // In scalar code, simply clone the method invoke, and replace its operands with the |
| 1452 | // corresponding new scalar instructions in the loop. The instruction will get an |
| 1453 | // environment while being inserted from the instruction map in original program order. |
Aart Bik | 6daebeb | 2017-04-03 14:35:41 -0700 | [diff] [blame] | 1454 | DCHECK(vector_mode_ == kSequential); |
Aart Bik | 6e92fb3 | 2017-06-05 14:05:09 -0700 | [diff] [blame] | 1455 | size_t num_args = invoke->GetNumberOfArguments(); |
Aart Bik | 6daebeb | 2017-04-03 14:35:41 -0700 | [diff] [blame] | 1456 | HInvokeStaticOrDirect* new_invoke = new (global_allocator_) HInvokeStaticOrDirect( |
| 1457 | global_allocator_, |
Aart Bik | 6e92fb3 | 2017-06-05 14:05:09 -0700 | [diff] [blame] | 1458 | num_args, |
Aart Bik | 6daebeb | 2017-04-03 14:35:41 -0700 | [diff] [blame] | 1459 | invoke->GetType(), |
| 1460 | invoke->GetDexPc(), |
| 1461 | invoke->GetDexMethodIndex(), |
| 1462 | invoke->GetResolvedMethod(), |
| 1463 | invoke->GetDispatchInfo(), |
| 1464 | invoke->GetInvokeType(), |
| 1465 | invoke->GetTargetMethod(), |
| 1466 | invoke->GetClinitCheckRequirement()); |
| 1467 | HInputsRef inputs = invoke->GetInputs(); |
Aart Bik | 6e92fb3 | 2017-06-05 14:05:09 -0700 | [diff] [blame] | 1468 | size_t num_inputs = inputs.size(); |
| 1469 | DCHECK_LE(num_args, num_inputs); |
| 1470 | DCHECK_EQ(num_inputs, new_invoke->GetInputs().size()); // both invokes agree |
| 1471 | for (size_t index = 0; index < num_inputs; ++index) { |
| 1472 | HInstruction* new_input = index < num_args |
| 1473 | ? vector_map_->Get(inputs[index]) |
| 1474 | : inputs[index]; // beyond arguments: just pass through |
| 1475 | new_invoke->SetArgumentAt(index, new_input); |
Aart Bik | 6daebeb | 2017-04-03 14:35:41 -0700 | [diff] [blame] | 1476 | } |
Aart Bik | 9899026 | 2017-04-10 13:15:57 -0700 | [diff] [blame] | 1477 | new_invoke->SetIntrinsic(invoke->GetIntrinsic(), |
| 1478 | kNeedsEnvironmentOrCache, |
| 1479 | kNoSideEffects, |
| 1480 | kNoThrow); |
Aart Bik | 6daebeb | 2017-04-03 14:35:41 -0700 | [diff] [blame] | 1481 | vector = new_invoke; |
| 1482 | } |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 1483 | break; |
| 1484 | } |
| 1485 | default: |
| 1486 | break; |
| 1487 | } // switch |
| 1488 | CHECK(vector != nullptr) << "Unsupported SIMD operator"; |
| 1489 | vector_map_->Put(org, vector); |
| 1490 | } |
| 1491 | |
| 1492 | #undef GENERATE_VEC |
| 1493 | |
| 1494 | // |
Aart Bik | f3e61ee | 2017-04-12 17:09:20 -0700 | [diff] [blame] | 1495 | // Vectorization idioms. |
| 1496 | // |
| 1497 | |
| 1498 | // Method recognizes the following idioms: |
| 1499 | // rounding halving add (a + b + 1) >> 1 for unsigned/signed operands a, b |
| 1500 | // regular halving add (a + b) >> 1 for unsigned/signed operands a, b |
| 1501 | // Provided that the operands are promoted to a wider form to do the arithmetic and |
| 1502 | // then cast back to narrower form, the idioms can be mapped into efficient SIMD |
| 1503 | // implementation that operates directly in narrower form (plus one extra bit). |
| 1504 | // TODO: current version recognizes implicit byte/short/char widening only; |
| 1505 | // explicit widening from int to long could be added later. |
| 1506 | bool HLoopOptimization::VectorizeHalvingAddIdiom(LoopNode* node, |
| 1507 | HInstruction* instruction, |
| 1508 | bool generate_code, |
| 1509 | Primitive::Type type, |
| 1510 | uint64_t restrictions) { |
| 1511 | // Test for top level arithmetic shift right x >> 1 or logical shift right x >>> 1 |
Aart Bik | 304c8a5 | 2017-05-23 11:01:13 -0700 | [diff] [blame] | 1512 | // (note whether the sign bit in wider precision is shifted in has no effect |
Aart Bik | f3e61ee | 2017-04-12 17:09:20 -0700 | [diff] [blame] | 1513 | // on the narrow precision computed by the idiom). |
Aart Bik | 5f80500 | 2017-05-16 16:42:41 -0700 | [diff] [blame] | 1514 | int64_t distance = 0; |
Aart Bik | f3e61ee | 2017-04-12 17:09:20 -0700 | [diff] [blame] | 1515 | if ((instruction->IsShr() || |
| 1516 | instruction->IsUShr()) && |
Aart Bik | 5f80500 | 2017-05-16 16:42:41 -0700 | [diff] [blame] | 1517 | IsInt64AndGet(instruction->InputAt(1), /*out*/ &distance) && distance == 1) { |
| 1518 | // Test for (a + b + c) >> 1 for optional constant c. |
| 1519 | HInstruction* a = nullptr; |
| 1520 | HInstruction* b = nullptr; |
| 1521 | int64_t c = 0; |
| 1522 | if (IsAddConst(instruction->InputAt(0), /*out*/ &a, /*out*/ &b, /*out*/ &c)) { |
Aart Bik | 304c8a5 | 2017-05-23 11:01:13 -0700 | [diff] [blame] | 1523 | DCHECK(a != nullptr && b != nullptr); |
Aart Bik | 5f80500 | 2017-05-16 16:42:41 -0700 | [diff] [blame] | 1524 | // Accept c == 1 (rounded) or c == 0 (not rounded). |
| 1525 | bool is_rounded = false; |
| 1526 | if (c == 1) { |
| 1527 | is_rounded = true; |
| 1528 | } else if (c != 0) { |
| 1529 | return false; |
| 1530 | } |
| 1531 | // Accept consistent zero or sign extension on operands a and b. |
Aart Bik | f3e61ee | 2017-04-12 17:09:20 -0700 | [diff] [blame] | 1532 | HInstruction* r = nullptr; |
| 1533 | HInstruction* s = nullptr; |
| 1534 | bool is_unsigned = false; |
Aart Bik | 304c8a5 | 2017-05-23 11:01:13 -0700 | [diff] [blame] | 1535 | if (!IsNarrowerOperands(a, b, type, &r, &s, &is_unsigned)) { |
Aart Bik | f3e61ee | 2017-04-12 17:09:20 -0700 | [diff] [blame] | 1536 | return false; |
| 1537 | } |
| 1538 | // Deal with vector restrictions. |
| 1539 | if ((!is_unsigned && HasVectorRestrictions(restrictions, kNoSignedHAdd)) || |
| 1540 | (!is_rounded && HasVectorRestrictions(restrictions, kNoUnroundedHAdd))) { |
| 1541 | return false; |
| 1542 | } |
| 1543 | // Accept recognized halving add for vectorizable operands. Vectorized code uses the |
| 1544 | // shorthand idiomatic operation. Sequential code uses the original scalar expressions. |
| 1545 | DCHECK(r != nullptr && s != nullptr); |
Aart Bik | 304c8a5 | 2017-05-23 11:01:13 -0700 | [diff] [blame] | 1546 | if (generate_code && vector_mode_ != kVector) { // de-idiom |
| 1547 | r = instruction->InputAt(0); |
| 1548 | s = instruction->InputAt(1); |
| 1549 | } |
Aart Bik | f3e61ee | 2017-04-12 17:09:20 -0700 | [diff] [blame] | 1550 | if (VectorizeUse(node, r, generate_code, type, restrictions) && |
| 1551 | VectorizeUse(node, s, generate_code, type, restrictions)) { |
| 1552 | if (generate_code) { |
| 1553 | if (vector_mode_ == kVector) { |
| 1554 | vector_map_->Put(instruction, new (global_allocator_) HVecHalvingAdd( |
| 1555 | global_allocator_, |
| 1556 | vector_map_->Get(r), |
| 1557 | vector_map_->Get(s), |
| 1558 | type, |
| 1559 | vector_length_, |
| 1560 | is_unsigned, |
| 1561 | is_rounded)); |
| 1562 | } else { |
Aart Bik | 304c8a5 | 2017-05-23 11:01:13 -0700 | [diff] [blame] | 1563 | GenerateVecOp(instruction, vector_map_->Get(r), vector_map_->Get(s), type); |
Aart Bik | f3e61ee | 2017-04-12 17:09:20 -0700 | [diff] [blame] | 1564 | } |
| 1565 | } |
| 1566 | return true; |
| 1567 | } |
| 1568 | } |
| 1569 | } |
| 1570 | return false; |
| 1571 | } |
| 1572 | |
| 1573 | // |
Aart Bik | 14a68b4 | 2017-06-08 14:06:58 -0700 | [diff] [blame] | 1574 | // Vectorization heuristics. |
| 1575 | // |
| 1576 | |
| 1577 | bool HLoopOptimization::IsVectorizationProfitable(int64_t trip_count) { |
| 1578 | // Current heuristic: non-empty body with sufficient number |
| 1579 | // of iterations (if known). |
| 1580 | // TODO: refine by looking at e.g. operation count, alignment, etc. |
| 1581 | if (vector_length_ == 0) { |
| 1582 | return false; // nothing found |
| 1583 | } else if (0 < trip_count && trip_count < vector_length_) { |
| 1584 | return false; // insufficient iterations |
| 1585 | } |
| 1586 | return true; |
| 1587 | } |
| 1588 | |
| 1589 | void HLoopOptimization::SetPeelingCandidate(int64_t trip_count ATTRIBUTE_UNUSED) { |
| 1590 | // Current heuristic: none. |
| 1591 | // TODO: implement |
| 1592 | } |
| 1593 | |
| 1594 | uint32_t HLoopOptimization::GetUnrollingFactor(HBasicBlock* block, int64_t trip_count) { |
| 1595 | // Current heuristic: unroll by 2 on ARM64/X86 for large known trip |
| 1596 | // counts and small loop bodies. |
| 1597 | // TODO: refine with operation count, remaining iterations, etc. |
| 1598 | // Artem had some really cool ideas for this already. |
| 1599 | switch (compiler_driver_->GetInstructionSet()) { |
| 1600 | case kArm64: |
| 1601 | case kX86: |
| 1602 | case kX86_64: { |
| 1603 | size_t num_instructions = block->GetInstructions().CountSize(); |
| 1604 | if (num_instructions <= 10 && trip_count >= 4 * vector_length_) { |
| 1605 | return 2; |
| 1606 | } |
| 1607 | return 1; |
| 1608 | } |
| 1609 | default: |
| 1610 | return 1; |
| 1611 | } |
| 1612 | } |
| 1613 | |
| 1614 | // |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 1615 | // Helpers. |
| 1616 | // |
| 1617 | |
| 1618 | bool HLoopOptimization::TrySetPhiInduction(HPhi* phi, bool restrict_uses) { |
Nicolas Geoffray | f57c1ae | 2017-06-28 17:40:18 +0100 | [diff] [blame] | 1619 | // Special case Phis that have equivalent in a debuggable setup. Our graph checker isn't |
| 1620 | // smart enough to follow strongly connected components (and it's probably not worth |
| 1621 | // it to make it so). See b/33775412. |
| 1622 | if (graph_->IsDebuggable() && phi->HasEquivalentPhi()) { |
| 1623 | return false; |
| 1624 | } |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 1625 | DCHECK(iset_->empty()); |
Aart Bik | cc42be0 | 2016-10-20 16:14:16 -0700 | [diff] [blame] | 1626 | ArenaSet<HInstruction*>* set = induction_range_.LookupCycle(phi); |
| 1627 | if (set != nullptr) { |
| 1628 | for (HInstruction* i : *set) { |
Aart Bik | e3dedc5 | 2016-11-02 17:50:27 -0700 | [diff] [blame] | 1629 | // Check that, other than instructions that are no longer in the graph (removed earlier) |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 1630 | // each instruction is removable and, when restrict uses are requested, other than for phi, |
| 1631 | // all uses are contained within the cycle. |
Aart Bik | e3dedc5 | 2016-11-02 17:50:27 -0700 | [diff] [blame] | 1632 | if (!i->IsInBlock()) { |
| 1633 | continue; |
| 1634 | } else if (!i->IsRemovable()) { |
| 1635 | return false; |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 1636 | } else if (i != phi && restrict_uses) { |
Aart Bik | cc42be0 | 2016-10-20 16:14:16 -0700 | [diff] [blame] | 1637 | for (const HUseListNode<HInstruction*>& use : i->GetUses()) { |
| 1638 | if (set->find(use.GetUser()) == set->end()) { |
| 1639 | return false; |
| 1640 | } |
| 1641 | } |
| 1642 | } |
Aart Bik | e3dedc5 | 2016-11-02 17:50:27 -0700 | [diff] [blame] | 1643 | iset_->insert(i); // copy |
Aart Bik | cc42be0 | 2016-10-20 16:14:16 -0700 | [diff] [blame] | 1644 | } |
Aart Bik | cc42be0 | 2016-10-20 16:14:16 -0700 | [diff] [blame] | 1645 | return true; |
| 1646 | } |
| 1647 | return false; |
| 1648 | } |
| 1649 | |
| 1650 | // Find: phi: Phi(init, addsub) |
| 1651 | // s: SuspendCheck |
| 1652 | // c: Condition(phi, bound) |
| 1653 | // i: If(c) |
| 1654 | // TODO: Find a less pattern matching approach? |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 1655 | bool HLoopOptimization::TrySetSimpleLoopHeader(HBasicBlock* block) { |
Aart Bik | cc42be0 | 2016-10-20 16:14:16 -0700 | [diff] [blame] | 1656 | DCHECK(iset_->empty()); |
| 1657 | HInstruction* phi = block->GetFirstPhi(); |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 1658 | if (phi != nullptr && |
| 1659 | phi->GetNext() == nullptr && |
| 1660 | TrySetPhiInduction(phi->AsPhi(), /*restrict_uses*/ false)) { |
Aart Bik | cc42be0 | 2016-10-20 16:14:16 -0700 | [diff] [blame] | 1661 | HInstruction* s = block->GetFirstInstruction(); |
| 1662 | if (s != nullptr && s->IsSuspendCheck()) { |
| 1663 | HInstruction* c = s->GetNext(); |
Aart Bik | d86c085 | 2017-04-14 12:00:15 -0700 | [diff] [blame] | 1664 | if (c != nullptr && |
| 1665 | c->IsCondition() && |
| 1666 | c->GetUses().HasExactlyOneElement() && // only used for termination |
| 1667 | !c->HasEnvironmentUses()) { // unlikely, but not impossible |
Aart Bik | cc42be0 | 2016-10-20 16:14:16 -0700 | [diff] [blame] | 1668 | HInstruction* i = c->GetNext(); |
| 1669 | if (i != nullptr && i->IsIf() && i->InputAt(0) == c) { |
| 1670 | iset_->insert(c); |
| 1671 | iset_->insert(s); |
| 1672 | return true; |
| 1673 | } |
| 1674 | } |
| 1675 | } |
| 1676 | } |
| 1677 | return false; |
| 1678 | } |
| 1679 | |
| 1680 | bool HLoopOptimization::IsEmptyBody(HBasicBlock* block) { |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 1681 | if (!block->GetPhis().IsEmpty()) { |
| 1682 | return false; |
| 1683 | } |
| 1684 | for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) { |
| 1685 | HInstruction* instruction = it.Current(); |
| 1686 | if (!instruction->IsGoto() && iset_->find(instruction) == iset_->end()) { |
| 1687 | return false; |
Aart Bik | cc42be0 | 2016-10-20 16:14:16 -0700 | [diff] [blame] | 1688 | } |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 1689 | } |
| 1690 | return true; |
| 1691 | } |
| 1692 | |
| 1693 | bool HLoopOptimization::IsUsedOutsideLoop(HLoopInformation* loop_info, |
| 1694 | HInstruction* instruction) { |
| 1695 | for (const HUseListNode<HInstruction*>& use : instruction->GetUses()) { |
| 1696 | if (use.GetUser()->GetBlock()->GetLoopInformation() != loop_info) { |
| 1697 | return true; |
| 1698 | } |
Aart Bik | cc42be0 | 2016-10-20 16:14:16 -0700 | [diff] [blame] | 1699 | } |
| 1700 | return false; |
| 1701 | } |
| 1702 | |
Aart Bik | 482095d | 2016-10-10 15:39:10 -0700 | [diff] [blame] | 1703 | bool HLoopOptimization::IsOnlyUsedAfterLoop(HLoopInformation* loop_info, |
Aart Bik | 8c4a854 | 2016-10-06 11:36:57 -0700 | [diff] [blame] | 1704 | HInstruction* instruction, |
Aart Bik | 6b69e0a | 2017-01-11 10:20:43 -0800 | [diff] [blame] | 1705 | bool collect_loop_uses, |
Aart Bik | 8c4a854 | 2016-10-06 11:36:57 -0700 | [diff] [blame] | 1706 | /*out*/ int32_t* use_count) { |
| 1707 | for (const HUseListNode<HInstruction*>& use : instruction->GetUses()) { |
| 1708 | HInstruction* user = use.GetUser(); |
| 1709 | if (iset_->find(user) == iset_->end()) { // not excluded? |
| 1710 | HLoopInformation* other_loop_info = user->GetBlock()->GetLoopInformation(); |
Aart Bik | 482095d | 2016-10-10 15:39:10 -0700 | [diff] [blame] | 1711 | if (other_loop_info != nullptr && other_loop_info->IsIn(*loop_info)) { |
Aart Bik | 6b69e0a | 2017-01-11 10:20:43 -0800 | [diff] [blame] | 1712 | // If collect_loop_uses is set, simply keep adding those uses to the set. |
| 1713 | // Otherwise, reject uses inside the loop that were not already in the set. |
| 1714 | if (collect_loop_uses) { |
| 1715 | iset_->insert(user); |
| 1716 | continue; |
| 1717 | } |
Aart Bik | 8c4a854 | 2016-10-06 11:36:57 -0700 | [diff] [blame] | 1718 | return false; |
| 1719 | } |
| 1720 | ++*use_count; |
| 1721 | } |
| 1722 | } |
| 1723 | return true; |
| 1724 | } |
| 1725 | |
Nicolas Geoffray | 1a0a519 | 2017-06-22 11:56:01 +0100 | [diff] [blame] | 1726 | bool HLoopOptimization::TryReplaceWithLastValue(HLoopInformation* loop_info, |
| 1727 | HInstruction* instruction, |
| 1728 | HBasicBlock* block) { |
| 1729 | // Try to replace outside uses with the last value. |
Aart Bik | 807868e | 2016-11-03 17:51:43 -0700 | [diff] [blame] | 1730 | if (induction_range_.CanGenerateLastValue(instruction)) { |
Aart Bik | 6b69e0a | 2017-01-11 10:20:43 -0800 | [diff] [blame] | 1731 | HInstruction* replacement = induction_range_.GenerateLastValue(instruction, graph_, block); |
| 1732 | const HUseList<HInstruction*>& uses = instruction->GetUses(); |
| 1733 | for (auto it = uses.begin(), end = uses.end(); it != end;) { |
| 1734 | HInstruction* user = it->GetUser(); |
| 1735 | size_t index = it->GetIndex(); |
| 1736 | ++it; // increment before replacing |
| 1737 | if (iset_->find(user) == iset_->end()) { // not excluded? |
Nicolas Geoffray | 1a0a519 | 2017-06-22 11:56:01 +0100 | [diff] [blame] | 1738 | if (kIsDebugBuild) { |
| 1739 | // We have checked earlier in 'IsOnlyUsedAfterLoop' that the use is after the loop. |
| 1740 | HLoopInformation* other_loop_info = user->GetBlock()->GetLoopInformation(); |
| 1741 | CHECK(other_loop_info == nullptr || !other_loop_info->IsIn(*loop_info)); |
| 1742 | } |
Aart Bik | 6b69e0a | 2017-01-11 10:20:43 -0800 | [diff] [blame] | 1743 | user->ReplaceInput(replacement, index); |
| 1744 | induction_range_.Replace(user, instruction, replacement); // update induction |
| 1745 | } |
| 1746 | } |
| 1747 | const HUseList<HEnvironment*>& env_uses = instruction->GetEnvUses(); |
| 1748 | for (auto it = env_uses.begin(), end = env_uses.end(); it != end;) { |
| 1749 | HEnvironment* user = it->GetUser(); |
| 1750 | size_t index = it->GetIndex(); |
| 1751 | ++it; // increment before replacing |
| 1752 | if (iset_->find(user->GetHolder()) == iset_->end()) { // not excluded? |
Nicolas Geoffray | 1a0a519 | 2017-06-22 11:56:01 +0100 | [diff] [blame] | 1753 | // Only update environment uses after the loop. |
Aart Bik | 14a68b4 | 2017-06-08 14:06:58 -0700 | [diff] [blame] | 1754 | HLoopInformation* other_loop_info = user->GetHolder()->GetBlock()->GetLoopInformation(); |
Nicolas Geoffray | 1a0a519 | 2017-06-22 11:56:01 +0100 | [diff] [blame] | 1755 | if (other_loop_info == nullptr || !other_loop_info->IsIn(*loop_info)) { |
| 1756 | user->RemoveAsUserOfInput(index); |
| 1757 | user->SetRawEnvAt(index, replacement); |
| 1758 | replacement->AddEnvUseAt(user, index); |
| 1759 | } |
Aart Bik | 6b69e0a | 2017-01-11 10:20:43 -0800 | [diff] [blame] | 1760 | } |
| 1761 | } |
| 1762 | induction_simplication_count_++; |
Aart Bik | 807868e | 2016-11-03 17:51:43 -0700 | [diff] [blame] | 1763 | return true; |
Aart Bik | 8c4a854 | 2016-10-06 11:36:57 -0700 | [diff] [blame] | 1764 | } |
Aart Bik | 807868e | 2016-11-03 17:51:43 -0700 | [diff] [blame] | 1765 | return false; |
Aart Bik | 8c4a854 | 2016-10-06 11:36:57 -0700 | [diff] [blame] | 1766 | } |
| 1767 | |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 1768 | bool HLoopOptimization::TryAssignLastValue(HLoopInformation* loop_info, |
| 1769 | HInstruction* instruction, |
| 1770 | HBasicBlock* block, |
| 1771 | bool collect_loop_uses) { |
| 1772 | // Assigning the last value is always successful if there are no uses. |
| 1773 | // Otherwise, it succeeds in a no early-exit loop by generating the |
| 1774 | // proper last value assignment. |
| 1775 | int32_t use_count = 0; |
| 1776 | return IsOnlyUsedAfterLoop(loop_info, instruction, collect_loop_uses, &use_count) && |
| 1777 | (use_count == 0 || |
Nicolas Geoffray | 1a0a519 | 2017-06-22 11:56:01 +0100 | [diff] [blame] | 1778 | (!IsEarlyExit(loop_info) && TryReplaceWithLastValue(loop_info, instruction, block))); |
Aart Bik | f8f5a16 | 2017-02-06 15:35:29 -0800 | [diff] [blame] | 1779 | } |
| 1780 | |
Aart Bik | 6b69e0a | 2017-01-11 10:20:43 -0800 | [diff] [blame] | 1781 | void HLoopOptimization::RemoveDeadInstructions(const HInstructionList& list) { |
| 1782 | for (HBackwardInstructionIterator i(list); !i.Done(); i.Advance()) { |
| 1783 | HInstruction* instruction = i.Current(); |
| 1784 | if (instruction->IsDeadAndRemovable()) { |
| 1785 | simplified_ = true; |
| 1786 | instruction->GetBlock()->RemoveInstructionOrPhi(instruction); |
| 1787 | } |
| 1788 | } |
| 1789 | } |
| 1790 | |
Aart Bik | 14a68b4 | 2017-06-08 14:06:58 -0700 | [diff] [blame] | 1791 | bool HLoopOptimization::CanRemoveCycle() { |
| 1792 | for (HInstruction* i : *iset_) { |
| 1793 | // We can never remove instructions that have environment |
| 1794 | // uses when we compile 'debuggable'. |
| 1795 | if (i->HasEnvironmentUses() && graph_->IsDebuggable()) { |
| 1796 | return false; |
| 1797 | } |
| 1798 | // A deoptimization should never have an environment input removed. |
| 1799 | for (const HUseListNode<HEnvironment*>& use : i->GetEnvUses()) { |
| 1800 | if (use.GetUser()->GetHolder()->IsDeoptimize()) { |
| 1801 | return false; |
| 1802 | } |
| 1803 | } |
| 1804 | } |
| 1805 | return true; |
| 1806 | } |
| 1807 | |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 1808 | } // namespace art |