1/* 2 * Copyright (C) 2014 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 "instruction_simplifier.h" 18 19#include "mirror/class-inl.h" 20#include "scoped_thread_state_change.h" 21 22namespace art { 23 24class InstructionSimplifierVisitor : public HGraphVisitor { 25 public: 26 InstructionSimplifierVisitor(HGraph* graph, OptimizingCompilerStats* stats) 27 : HGraphVisitor(graph), 28 stats_(stats) {} 29 30 void Run(); 31 32 private: 33 void RecordSimplification() { 34 simplification_occurred_ = true; 35 simplifications_at_current_position_++; 36 if (stats_) { 37 stats_->RecordStat(kInstructionSimplifications); 38 } 39 } 40 41 bool TryMoveNegOnInputsAfterBinop(HBinaryOperation* binop); 42 void VisitShift(HBinaryOperation* shift); 43 44 void VisitSuspendCheck(HSuspendCheck* check) OVERRIDE; 45 void VisitEqual(HEqual* equal) OVERRIDE; 46 void VisitNotEqual(HNotEqual* equal) OVERRIDE; 47 void VisitBooleanNot(HBooleanNot* bool_not) OVERRIDE; 48 void VisitArraySet(HArraySet* equal) OVERRIDE; 49 void VisitTypeConversion(HTypeConversion* instruction) OVERRIDE; 50 void VisitNullCheck(HNullCheck* instruction) OVERRIDE; 51 void VisitArrayLength(HArrayLength* instruction) OVERRIDE; 52 void VisitCheckCast(HCheckCast* instruction) OVERRIDE; 53 void VisitAdd(HAdd* instruction) OVERRIDE; 54 void VisitAnd(HAnd* instruction) OVERRIDE; 55 void VisitDiv(HDiv* instruction) OVERRIDE; 56 void VisitMul(HMul* instruction) OVERRIDE; 57 void VisitNeg(HNeg* instruction) OVERRIDE; 58 void VisitNot(HNot* instruction) OVERRIDE; 59 void VisitOr(HOr* instruction) OVERRIDE; 60 void VisitShl(HShl* instruction) OVERRIDE; 61 void VisitShr(HShr* instruction) OVERRIDE; 62 void VisitSub(HSub* instruction) OVERRIDE; 63 void VisitUShr(HUShr* instruction) OVERRIDE; 64 void VisitXor(HXor* instruction) OVERRIDE; 65 void VisitInstanceOf(HInstanceOf* instruction) OVERRIDE; 66 67 OptimizingCompilerStats* stats_; 68 bool simplification_occurred_ = false; 69 int simplifications_at_current_position_ = 0; 70 // We ensure we do not loop infinitely. The value is a finger in the air guess 71 // that should allow enough simplification. 72 static constexpr int kMaxSamePositionSimplifications = 10; 73}; 74 75void InstructionSimplifier::Run() { 76 InstructionSimplifierVisitor visitor(graph_, stats_); 77 visitor.Run(); 78} 79 80void InstructionSimplifierVisitor::Run() { 81 for (HReversePostOrderIterator it(*GetGraph()); !it.Done();) { 82 // The simplification of an instruction to another instruction may yield 83 // possibilities for other simplifications. So although we perform a reverse 84 // post order visit, we sometimes need to revisit an instruction index. 85 simplification_occurred_ = false; 86 VisitBasicBlock(it.Current()); 87 if (simplification_occurred_ && 88 (simplifications_at_current_position_ < kMaxSamePositionSimplifications)) { 89 // New simplifications may be applicable to the instruction at the 90 // current index, so don't advance the iterator. 91 continue; 92 } 93 simplifications_at_current_position_ = 0; 94 it.Advance(); 95 } 96} 97 98namespace { 99 100bool AreAllBitsSet(HConstant* constant) { 101 return Int64FromConstant(constant) == -1; 102} 103 104} // namespace 105 106// Returns true if the code was simplified to use only one negation operation 107// after the binary operation instead of one on each of the inputs. 108bool InstructionSimplifierVisitor::TryMoveNegOnInputsAfterBinop(HBinaryOperation* binop) { 109 DCHECK(binop->IsAdd() || binop->IsSub()); 110 DCHECK(binop->GetLeft()->IsNeg() && binop->GetRight()->IsNeg()); 111 HNeg* left_neg = binop->GetLeft()->AsNeg(); 112 HNeg* right_neg = binop->GetRight()->AsNeg(); 113 if (!left_neg->HasOnlyOneNonEnvironmentUse() || 114 !right_neg->HasOnlyOneNonEnvironmentUse()) { 115 return false; 116 } 117 // Replace code looking like 118 // NEG tmp1, a 119 // NEG tmp2, b 120 // ADD dst, tmp1, tmp2 121 // with 122 // ADD tmp, a, b 123 // NEG dst, tmp 124 binop->ReplaceInput(left_neg->GetInput(), 0); 125 binop->ReplaceInput(right_neg->GetInput(), 1); 126 left_neg->GetBlock()->RemoveInstruction(left_neg); 127 right_neg->GetBlock()->RemoveInstruction(right_neg); 128 HNeg* neg = new (GetGraph()->GetArena()) HNeg(binop->GetType(), binop); 129 binop->GetBlock()->InsertInstructionBefore(neg, binop->GetNext()); 130 binop->ReplaceWithExceptInReplacementAtIndex(neg, 0); 131 RecordSimplification(); 132 return true; 133} 134 135void InstructionSimplifierVisitor::VisitShift(HBinaryOperation* instruction) { 136 DCHECK(instruction->IsShl() || instruction->IsShr() || instruction->IsUShr()); 137 HConstant* input_cst = instruction->GetConstantRight(); 138 HInstruction* input_other = instruction->GetLeastConstantLeft(); 139 140 if (input_cst != nullptr) { 141 if (input_cst->IsZero()) { 142 // Replace code looking like 143 // SHL dst, src, 0 144 // with 145 // src 146 instruction->ReplaceWith(input_other); 147 instruction->GetBlock()->RemoveInstruction(instruction); 148 } else if (instruction->IsShl() && input_cst->IsOne()) { 149 // Replace Shl looking like 150 // SHL dst, src, 1 151 // with 152 // ADD dst, src, src 153 HAdd *add = new(GetGraph()->GetArena()) HAdd(instruction->GetType(), 154 input_other, 155 input_other); 156 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, add); 157 RecordSimplification(); 158 } 159 } 160} 161 162void InstructionSimplifierVisitor::VisitNullCheck(HNullCheck* null_check) { 163 HInstruction* obj = null_check->InputAt(0); 164 if (!obj->CanBeNull()) { 165 null_check->ReplaceWith(obj); 166 null_check->GetBlock()->RemoveInstruction(null_check); 167 if (stats_ != nullptr) { 168 stats_->RecordStat(MethodCompilationStat::kRemovedNullCheck); 169 } 170 } 171} 172 173void InstructionSimplifierVisitor::VisitCheckCast(HCheckCast* check_cast) { 174 HLoadClass* load_class = check_cast->InputAt(1)->AsLoadClass(); 175 if (!check_cast->InputAt(0)->CanBeNull()) { 176 check_cast->ClearMustDoNullCheck(); 177 } 178 179 if (!load_class->IsResolved()) { 180 // If the class couldn't be resolve it's not safe to compare against it. It's 181 // default type would be Top which might be wider that the actual class type 182 // and thus producing wrong results. 183 return; 184 } 185 ReferenceTypeInfo obj_rti = check_cast->InputAt(0)->GetReferenceTypeInfo(); 186 ReferenceTypeInfo class_rti = load_class->GetLoadedClassRTI(); 187 ScopedObjectAccess soa(Thread::Current()); 188 if (class_rti.IsSupertypeOf(obj_rti)) { 189 check_cast->GetBlock()->RemoveInstruction(check_cast); 190 if (stats_ != nullptr) { 191 stats_->RecordStat(MethodCompilationStat::kRemovedCheckedCast); 192 } 193 } 194} 195 196void InstructionSimplifierVisitor::VisitInstanceOf(HInstanceOf* instruction) { 197 if (!instruction->InputAt(0)->CanBeNull()) { 198 instruction->ClearMustDoNullCheck(); 199 } 200} 201 202void InstructionSimplifierVisitor::VisitSuspendCheck(HSuspendCheck* check) { 203 HBasicBlock* block = check->GetBlock(); 204 // Currently always keep the suspend check at entry. 205 if (block->IsEntryBlock()) return; 206 207 // Currently always keep suspend checks at loop entry. 208 if (block->IsLoopHeader() && block->GetFirstInstruction() == check) { 209 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == check); 210 return; 211 } 212 213 // Remove the suspend check that was added at build time for the baseline 214 // compiler. 215 block->RemoveInstruction(check); 216} 217 218void InstructionSimplifierVisitor::VisitEqual(HEqual* equal) { 219 HInstruction* input_const = equal->GetConstantRight(); 220 if (input_const != nullptr) { 221 HInstruction* input_value = equal->GetLeastConstantLeft(); 222 if (input_value->GetType() == Primitive::kPrimBoolean && input_const->IsIntConstant()) { 223 HBasicBlock* block = equal->GetBlock(); 224 // We are comparing the boolean to a constant which is of type int and can 225 // be any constant. 226 if (input_const->AsIntConstant()->IsOne()) { 227 // Replace (bool_value == true) with bool_value 228 equal->ReplaceWith(input_value); 229 block->RemoveInstruction(equal); 230 RecordSimplification(); 231 } else if (input_const->AsIntConstant()->IsZero()) { 232 // Replace (bool_value == false) with !bool_value 233 block->ReplaceAndRemoveInstructionWith( 234 equal, new (block->GetGraph()->GetArena()) HBooleanNot(input_value)); 235 RecordSimplification(); 236 } 237 } 238 } 239} 240 241void InstructionSimplifierVisitor::VisitNotEqual(HNotEqual* not_equal) { 242 HInstruction* input_const = not_equal->GetConstantRight(); 243 if (input_const != nullptr) { 244 HInstruction* input_value = not_equal->GetLeastConstantLeft(); 245 if (input_value->GetType() == Primitive::kPrimBoolean && input_const->IsIntConstant()) { 246 HBasicBlock* block = not_equal->GetBlock(); 247 // We are comparing the boolean to a constant which is of type int and can 248 // be any constant. 249 if (input_const->AsIntConstant()->IsOne()) { 250 // Replace (bool_value != true) with !bool_value 251 block->ReplaceAndRemoveInstructionWith( 252 not_equal, new (block->GetGraph()->GetArena()) HBooleanNot(input_value)); 253 RecordSimplification(); 254 } else if (input_const->AsIntConstant()->IsZero()) { 255 // Replace (bool_value != false) with bool_value 256 not_equal->ReplaceWith(input_value); 257 block->RemoveInstruction(not_equal); 258 RecordSimplification(); 259 } 260 } 261 } 262} 263 264void InstructionSimplifierVisitor::VisitBooleanNot(HBooleanNot* bool_not) { 265 HInstruction* parent = bool_not->InputAt(0); 266 if (parent->IsBooleanNot()) { 267 HInstruction* value = parent->InputAt(0); 268 // Replace (!(!bool_value)) with bool_value 269 bool_not->ReplaceWith(value); 270 bool_not->GetBlock()->RemoveInstruction(bool_not); 271 // It is possible that `parent` is dead at this point but we leave 272 // its removal to DCE for simplicity. 273 RecordSimplification(); 274 } 275} 276 277void InstructionSimplifierVisitor::VisitArrayLength(HArrayLength* instruction) { 278 HInstruction* input = instruction->InputAt(0); 279 // If the array is a NewArray with constant size, replace the array length 280 // with the constant instruction. This helps the bounds check elimination phase. 281 if (input->IsNewArray()) { 282 input = input->InputAt(0); 283 if (input->IsIntConstant()) { 284 instruction->ReplaceWith(input); 285 } 286 } 287} 288 289void InstructionSimplifierVisitor::VisitArraySet(HArraySet* instruction) { 290 HInstruction* value = instruction->GetValue(); 291 if (value->GetType() != Primitive::kPrimNot) return; 292 293 if (value->IsArrayGet()) { 294 if (value->AsArrayGet()->GetArray() == instruction->GetArray()) { 295 // If the code is just swapping elements in the array, no need for a type check. 296 instruction->ClearNeedsTypeCheck(); 297 } 298 } 299} 300 301void InstructionSimplifierVisitor::VisitTypeConversion(HTypeConversion* instruction) { 302 if (instruction->GetResultType() == instruction->GetInputType()) { 303 // Remove the instruction if it's converting to the same type. 304 instruction->ReplaceWith(instruction->GetInput()); 305 instruction->GetBlock()->RemoveInstruction(instruction); 306 } 307} 308 309void InstructionSimplifierVisitor::VisitAdd(HAdd* instruction) { 310 HConstant* input_cst = instruction->GetConstantRight(); 311 HInstruction* input_other = instruction->GetLeastConstantLeft(); 312 if ((input_cst != nullptr) && input_cst->IsZero()) { 313 // Replace code looking like 314 // ADD dst, src, 0 315 // with 316 // src 317 // Note that we cannot optimize `x + 0.0` to `x` for floating-point. When 318 // `x` is `-0.0`, the former expression yields `0.0`, while the later 319 // yields `-0.0`. 320 if (Primitive::IsIntegralType(instruction->GetType())) { 321 instruction->ReplaceWith(input_other); 322 instruction->GetBlock()->RemoveInstruction(instruction); 323 return; 324 } 325 } 326 327 HInstruction* left = instruction->GetLeft(); 328 HInstruction* right = instruction->GetRight(); 329 bool left_is_neg = left->IsNeg(); 330 bool right_is_neg = right->IsNeg(); 331 332 if (left_is_neg && right_is_neg) { 333 if (TryMoveNegOnInputsAfterBinop(instruction)) { 334 return; 335 } 336 } 337 338 HNeg* neg = left_is_neg ? left->AsNeg() : right->AsNeg(); 339 if ((left_is_neg ^ right_is_neg) && neg->HasOnlyOneNonEnvironmentUse()) { 340 // Replace code looking like 341 // NEG tmp, b 342 // ADD dst, a, tmp 343 // with 344 // SUB dst, a, b 345 // We do not perform the optimization if the input negation has environment 346 // uses or multiple non-environment uses as it could lead to worse code. In 347 // particular, we do not want the live range of `b` to be extended if we are 348 // not sure the initial 'NEG' instruction can be removed. 349 HInstruction* other = left_is_neg ? right : left; 350 HSub* sub = new(GetGraph()->GetArena()) HSub(instruction->GetType(), other, neg->GetInput()); 351 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, sub); 352 RecordSimplification(); 353 neg->GetBlock()->RemoveInstruction(neg); 354 } 355} 356 357void InstructionSimplifierVisitor::VisitAnd(HAnd* instruction) { 358 HConstant* input_cst = instruction->GetConstantRight(); 359 HInstruction* input_other = instruction->GetLeastConstantLeft(); 360 361 if ((input_cst != nullptr) && AreAllBitsSet(input_cst)) { 362 // Replace code looking like 363 // AND dst, src, 0xFFF...FF 364 // with 365 // src 366 instruction->ReplaceWith(input_other); 367 instruction->GetBlock()->RemoveInstruction(instruction); 368 return; 369 } 370 371 // We assume that GVN has run before, so we only perform a pointer comparison. 372 // If for some reason the values are equal but the pointers are different, we 373 // are still correct and only miss an optimization opportunity. 374 if (instruction->GetLeft() == instruction->GetRight()) { 375 // Replace code looking like 376 // AND dst, src, src 377 // with 378 // src 379 instruction->ReplaceWith(instruction->GetLeft()); 380 instruction->GetBlock()->RemoveInstruction(instruction); 381 } 382} 383 384void InstructionSimplifierVisitor::VisitDiv(HDiv* instruction) { 385 HConstant* input_cst = instruction->GetConstantRight(); 386 HInstruction* input_other = instruction->GetLeastConstantLeft(); 387 Primitive::Type type = instruction->GetType(); 388 389 if ((input_cst != nullptr) && input_cst->IsOne()) { 390 // Replace code looking like 391 // DIV dst, src, 1 392 // with 393 // src 394 instruction->ReplaceWith(input_other); 395 instruction->GetBlock()->RemoveInstruction(instruction); 396 return; 397 } 398 399 if ((input_cst != nullptr) && input_cst->IsMinusOne()) { 400 // Replace code looking like 401 // DIV dst, src, -1 402 // with 403 // NEG dst, src 404 instruction->GetBlock()->ReplaceAndRemoveInstructionWith( 405 instruction, new (GetGraph()->GetArena()) HNeg(type, input_other)); 406 RecordSimplification(); 407 return; 408 } 409 410 if ((input_cst != nullptr) && Primitive::IsFloatingPointType(type)) { 411 // Try replacing code looking like 412 // DIV dst, src, constant 413 // with 414 // MUL dst, src, 1 / constant 415 HConstant* reciprocal = nullptr; 416 if (type == Primitive::Primitive::kPrimDouble) { 417 double value = input_cst->AsDoubleConstant()->GetValue(); 418 if (CanDivideByReciprocalMultiplyDouble(bit_cast<int64_t, double>(value))) { 419 reciprocal = GetGraph()->GetDoubleConstant(1.0 / value); 420 } 421 } else { 422 DCHECK_EQ(type, Primitive::kPrimFloat); 423 float value = input_cst->AsFloatConstant()->GetValue(); 424 if (CanDivideByReciprocalMultiplyFloat(bit_cast<int32_t, float>(value))) { 425 reciprocal = GetGraph()->GetFloatConstant(1.0f / value); 426 } 427 } 428 429 if (reciprocal != nullptr) { 430 instruction->GetBlock()->ReplaceAndRemoveInstructionWith( 431 instruction, new (GetGraph()->GetArena()) HMul(type, input_other, reciprocal)); 432 RecordSimplification(); 433 return; 434 } 435 } 436} 437 438void InstructionSimplifierVisitor::VisitMul(HMul* instruction) { 439 HConstant* input_cst = instruction->GetConstantRight(); 440 HInstruction* input_other = instruction->GetLeastConstantLeft(); 441 Primitive::Type type = instruction->GetType(); 442 HBasicBlock* block = instruction->GetBlock(); 443 ArenaAllocator* allocator = GetGraph()->GetArena(); 444 445 if (input_cst == nullptr) { 446 return; 447 } 448 449 if (input_cst->IsOne()) { 450 // Replace code looking like 451 // MUL dst, src, 1 452 // with 453 // src 454 instruction->ReplaceWith(input_other); 455 instruction->GetBlock()->RemoveInstruction(instruction); 456 return; 457 } 458 459 if (input_cst->IsMinusOne() && 460 (Primitive::IsFloatingPointType(type) || Primitive::IsIntOrLongType(type))) { 461 // Replace code looking like 462 // MUL dst, src, -1 463 // with 464 // NEG dst, src 465 HNeg* neg = new (allocator) HNeg(type, input_other); 466 block->ReplaceAndRemoveInstructionWith(instruction, neg); 467 RecordSimplification(); 468 return; 469 } 470 471 if (Primitive::IsFloatingPointType(type) && 472 ((input_cst->IsFloatConstant() && input_cst->AsFloatConstant()->GetValue() == 2.0f) || 473 (input_cst->IsDoubleConstant() && input_cst->AsDoubleConstant()->GetValue() == 2.0))) { 474 // Replace code looking like 475 // FP_MUL dst, src, 2.0 476 // with 477 // FP_ADD dst, src, src 478 // The 'int' and 'long' cases are handled below. 479 block->ReplaceAndRemoveInstructionWith(instruction, 480 new (allocator) HAdd(type, input_other, input_other)); 481 RecordSimplification(); 482 return; 483 } 484 485 if (Primitive::IsIntOrLongType(type)) { 486 int64_t factor = Int64FromConstant(input_cst); 487 // Even though constant propagation also takes care of the zero case, other 488 // optimizations can lead to having a zero multiplication. 489 if (factor == 0) { 490 // Replace code looking like 491 // MUL dst, src, 0 492 // with 493 // 0 494 instruction->ReplaceWith(input_cst); 495 instruction->GetBlock()->RemoveInstruction(instruction); 496 } else if (IsPowerOfTwo(factor)) { 497 // Replace code looking like 498 // MUL dst, src, pow_of_2 499 // with 500 // SHL dst, src, log2(pow_of_2) 501 HIntConstant* shift = GetGraph()->GetIntConstant(WhichPowerOf2(factor)); 502 HShl* shl = new(allocator) HShl(type, input_other, shift); 503 block->ReplaceAndRemoveInstructionWith(instruction, shl); 504 RecordSimplification(); 505 } 506 } 507} 508 509void InstructionSimplifierVisitor::VisitNeg(HNeg* instruction) { 510 HInstruction* input = instruction->GetInput(); 511 if (input->IsNeg()) { 512 // Replace code looking like 513 // NEG tmp, src 514 // NEG dst, tmp 515 // with 516 // src 517 HNeg* previous_neg = input->AsNeg(); 518 instruction->ReplaceWith(previous_neg->GetInput()); 519 instruction->GetBlock()->RemoveInstruction(instruction); 520 // We perform the optimization even if the input negation has environment 521 // uses since it allows removing the current instruction. But we only delete 522 // the input negation only if it is does not have any uses left. 523 if (!previous_neg->HasUses()) { 524 previous_neg->GetBlock()->RemoveInstruction(previous_neg); 525 } 526 RecordSimplification(); 527 return; 528 } 529 530 if (input->IsSub() && input->HasOnlyOneNonEnvironmentUse() && 531 !Primitive::IsFloatingPointType(input->GetType())) { 532 // Replace code looking like 533 // SUB tmp, a, b 534 // NEG dst, tmp 535 // with 536 // SUB dst, b, a 537 // We do not perform the optimization if the input subtraction has 538 // environment uses or multiple non-environment uses as it could lead to 539 // worse code. In particular, we do not want the live ranges of `a` and `b` 540 // to be extended if we are not sure the initial 'SUB' instruction can be 541 // removed. 542 // We do not perform optimization for fp because we could lose the sign of zero. 543 HSub* sub = input->AsSub(); 544 HSub* new_sub = 545 new (GetGraph()->GetArena()) HSub(instruction->GetType(), sub->GetRight(), sub->GetLeft()); 546 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, new_sub); 547 if (!sub->HasUses()) { 548 sub->GetBlock()->RemoveInstruction(sub); 549 } 550 RecordSimplification(); 551 } 552} 553 554void InstructionSimplifierVisitor::VisitNot(HNot* instruction) { 555 HInstruction* input = instruction->GetInput(); 556 if (input->IsNot()) { 557 // Replace code looking like 558 // NOT tmp, src 559 // NOT dst, tmp 560 // with 561 // src 562 // We perform the optimization even if the input negation has environment 563 // uses since it allows removing the current instruction. But we only delete 564 // the input negation only if it is does not have any uses left. 565 HNot* previous_not = input->AsNot(); 566 instruction->ReplaceWith(previous_not->GetInput()); 567 instruction->GetBlock()->RemoveInstruction(instruction); 568 if (!previous_not->HasUses()) { 569 previous_not->GetBlock()->RemoveInstruction(previous_not); 570 } 571 RecordSimplification(); 572 } 573} 574 575void InstructionSimplifierVisitor::VisitOr(HOr* instruction) { 576 HConstant* input_cst = instruction->GetConstantRight(); 577 HInstruction* input_other = instruction->GetLeastConstantLeft(); 578 579 if ((input_cst != nullptr) && input_cst->IsZero()) { 580 // Replace code looking like 581 // OR dst, src, 0 582 // with 583 // src 584 instruction->ReplaceWith(input_other); 585 instruction->GetBlock()->RemoveInstruction(instruction); 586 return; 587 } 588 589 // We assume that GVN has run before, so we only perform a pointer comparison. 590 // If for some reason the values are equal but the pointers are different, we 591 // are still correct and only miss an optimization opportunity. 592 if (instruction->GetLeft() == instruction->GetRight()) { 593 // Replace code looking like 594 // OR dst, src, src 595 // with 596 // src 597 instruction->ReplaceWith(instruction->GetLeft()); 598 instruction->GetBlock()->RemoveInstruction(instruction); 599 } 600} 601 602void InstructionSimplifierVisitor::VisitShl(HShl* instruction) { 603 VisitShift(instruction); 604} 605 606void InstructionSimplifierVisitor::VisitShr(HShr* instruction) { 607 VisitShift(instruction); 608} 609 610void InstructionSimplifierVisitor::VisitSub(HSub* instruction) { 611 HConstant* input_cst = instruction->GetConstantRight(); 612 HInstruction* input_other = instruction->GetLeastConstantLeft(); 613 614 Primitive::Type type = instruction->GetType(); 615 if (Primitive::IsFloatingPointType(type)) { 616 return; 617 } 618 619 if ((input_cst != nullptr) && input_cst->IsZero()) { 620 // Replace code looking like 621 // SUB dst, src, 0 622 // with 623 // src 624 // Note that we cannot optimize `x - 0.0` to `x` for floating-point. When 625 // `x` is `-0.0`, the former expression yields `0.0`, while the later 626 // yields `-0.0`. 627 instruction->ReplaceWith(input_other); 628 instruction->GetBlock()->RemoveInstruction(instruction); 629 return; 630 } 631 632 HBasicBlock* block = instruction->GetBlock(); 633 ArenaAllocator* allocator = GetGraph()->GetArena(); 634 635 HInstruction* left = instruction->GetLeft(); 636 HInstruction* right = instruction->GetRight(); 637 if (left->IsConstant()) { 638 if (Int64FromConstant(left->AsConstant()) == 0) { 639 // Replace code looking like 640 // SUB dst, 0, src 641 // with 642 // NEG dst, src 643 // Note that we cannot optimize `0.0 - x` to `-x` for floating-point. When 644 // `x` is `0.0`, the former expression yields `0.0`, while the later 645 // yields `-0.0`. 646 HNeg* neg = new (allocator) HNeg(type, right); 647 block->ReplaceAndRemoveInstructionWith(instruction, neg); 648 RecordSimplification(); 649 return; 650 } 651 } 652 653 if (left->IsNeg() && right->IsNeg()) { 654 if (TryMoveNegOnInputsAfterBinop(instruction)) { 655 return; 656 } 657 } 658 659 if (right->IsNeg() && right->HasOnlyOneNonEnvironmentUse()) { 660 // Replace code looking like 661 // NEG tmp, b 662 // SUB dst, a, tmp 663 // with 664 // ADD dst, a, b 665 HAdd* add = new(GetGraph()->GetArena()) HAdd(type, left, right->AsNeg()->GetInput()); 666 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, add); 667 RecordSimplification(); 668 right->GetBlock()->RemoveInstruction(right); 669 return; 670 } 671 672 if (left->IsNeg() && left->HasOnlyOneNonEnvironmentUse()) { 673 // Replace code looking like 674 // NEG tmp, a 675 // SUB dst, tmp, b 676 // with 677 // ADD tmp, a, b 678 // NEG dst, tmp 679 // The second version is not intrinsically better, but enables more 680 // transformations. 681 HAdd* add = new(GetGraph()->GetArena()) HAdd(type, left->AsNeg()->GetInput(), right); 682 instruction->GetBlock()->InsertInstructionBefore(add, instruction); 683 HNeg* neg = new (GetGraph()->GetArena()) HNeg(instruction->GetType(), add); 684 instruction->GetBlock()->InsertInstructionBefore(neg, instruction); 685 instruction->ReplaceWith(neg); 686 instruction->GetBlock()->RemoveInstruction(instruction); 687 RecordSimplification(); 688 left->GetBlock()->RemoveInstruction(left); 689 } 690} 691 692void InstructionSimplifierVisitor::VisitUShr(HUShr* instruction) { 693 VisitShift(instruction); 694} 695 696void InstructionSimplifierVisitor::VisitXor(HXor* instruction) { 697 HConstant* input_cst = instruction->GetConstantRight(); 698 HInstruction* input_other = instruction->GetLeastConstantLeft(); 699 700 if ((input_cst != nullptr) && input_cst->IsZero()) { 701 // Replace code looking like 702 // XOR dst, src, 0 703 // with 704 // src 705 instruction->ReplaceWith(input_other); 706 instruction->GetBlock()->RemoveInstruction(instruction); 707 return; 708 } 709 710 if ((input_cst != nullptr) && AreAllBitsSet(input_cst)) { 711 // Replace code looking like 712 // XOR dst, src, 0xFFF...FF 713 // with 714 // NOT dst, src 715 HNot* bitwise_not = new (GetGraph()->GetArena()) HNot(instruction->GetType(), input_other); 716 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, bitwise_not); 717 RecordSimplification(); 718 return; 719 } 720} 721 722} // namespace art 723