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