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