instruction_simplifier.cc revision ba56d060116d6e145be348fa575314654c6b0572
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      if (input_const->AsIntConstant()->IsOne()) {
225        // Replace (bool_value == true) with bool_value
226        equal->ReplaceWith(input_value);
227        block->RemoveInstruction(equal);
228        RecordSimplification();
229      } else {
230        // Replace (bool_value == false) with !bool_value
231        DCHECK(input_const->AsIntConstant()->IsZero());
232        block->ReplaceAndRemoveInstructionWith(
233            equal, new (block->GetGraph()->GetArena()) HBooleanNot(input_value));
234        RecordSimplification();
235      }
236    }
237  }
238}
239
240void InstructionSimplifierVisitor::VisitNotEqual(HNotEqual* not_equal) {
241  HInstruction* input_const = not_equal->GetConstantRight();
242  if (input_const != nullptr) {
243    HInstruction* input_value = not_equal->GetLeastConstantLeft();
244    if (input_value->GetType() == Primitive::kPrimBoolean && input_const->IsIntConstant()) {
245      HBasicBlock* block = not_equal->GetBlock();
246      if (input_const->AsIntConstant()->IsOne()) {
247        // Replace (bool_value != true) with !bool_value
248        block->ReplaceAndRemoveInstructionWith(
249            not_equal, new (block->GetGraph()->GetArena()) HBooleanNot(input_value));
250        RecordSimplification();
251      } else {
252        // Replace (bool_value != false) with bool_value
253        DCHECK(input_const->AsIntConstant()->IsZero());
254        not_equal->ReplaceWith(input_value);
255        block->RemoveInstruction(not_equal);
256        RecordSimplification();
257      }
258    }
259  }
260}
261
262void InstructionSimplifierVisitor::VisitBooleanNot(HBooleanNot* bool_not) {
263  HInstruction* parent = bool_not->InputAt(0);
264  if (parent->IsBooleanNot()) {
265    HInstruction* value = parent->InputAt(0);
266    // Replace (!(!bool_value)) with bool_value
267    bool_not->ReplaceWith(value);
268    bool_not->GetBlock()->RemoveInstruction(bool_not);
269    // It is possible that `parent` is dead at this point but we leave
270    // its removal to DCE for simplicity.
271    RecordSimplification();
272  }
273}
274
275void InstructionSimplifierVisitor::VisitArrayLength(HArrayLength* instruction) {
276  HInstruction* input = instruction->InputAt(0);
277  // If the array is a NewArray with constant size, replace the array length
278  // with the constant instruction. This helps the bounds check elimination phase.
279  if (input->IsNewArray()) {
280    input = input->InputAt(0);
281    if (input->IsIntConstant()) {
282      instruction->ReplaceWith(input);
283    }
284  }
285}
286
287void InstructionSimplifierVisitor::VisitArraySet(HArraySet* instruction) {
288  HInstruction* value = instruction->GetValue();
289  if (value->GetType() != Primitive::kPrimNot) return;
290
291  if (value->IsArrayGet()) {
292    if (value->AsArrayGet()->GetArray() == instruction->GetArray()) {
293      // If the code is just swapping elements in the array, no need for a type check.
294      instruction->ClearNeedsTypeCheck();
295    }
296  }
297}
298
299void InstructionSimplifierVisitor::VisitTypeConversion(HTypeConversion* instruction) {
300  if (instruction->GetResultType() == instruction->GetInputType()) {
301    // Remove the instruction if it's converting to the same type.
302    instruction->ReplaceWith(instruction->GetInput());
303    instruction->GetBlock()->RemoveInstruction(instruction);
304  }
305}
306
307void InstructionSimplifierVisitor::VisitAdd(HAdd* instruction) {
308  HConstant* input_cst = instruction->GetConstantRight();
309  HInstruction* input_other = instruction->GetLeastConstantLeft();
310  if ((input_cst != nullptr) && input_cst->IsZero()) {
311    // Replace code looking like
312    //    ADD dst, src, 0
313    // with
314    //    src
315    instruction->ReplaceWith(input_other);
316    instruction->GetBlock()->RemoveInstruction(instruction);
317    return;
318  }
319
320  HInstruction* left = instruction->GetLeft();
321  HInstruction* right = instruction->GetRight();
322  bool left_is_neg = left->IsNeg();
323  bool right_is_neg = right->IsNeg();
324
325  if (left_is_neg && right_is_neg) {
326    if (TryMoveNegOnInputsAfterBinop(instruction)) {
327      return;
328    }
329  }
330
331  HNeg* neg = left_is_neg ? left->AsNeg() : right->AsNeg();
332  if ((left_is_neg ^ right_is_neg) && neg->HasOnlyOneNonEnvironmentUse()) {
333    // Replace code looking like
334    //    NEG tmp, b
335    //    ADD dst, a, tmp
336    // with
337    //    SUB dst, a, b
338    // We do not perform the optimization if the input negation has environment
339    // uses or multiple non-environment uses as it could lead to worse code. In
340    // particular, we do not want the live range of `b` to be extended if we are
341    // not sure the initial 'NEG' instruction can be removed.
342    HInstruction* other = left_is_neg ? right : left;
343    HSub* sub = new(GetGraph()->GetArena()) HSub(instruction->GetType(), other, neg->GetInput());
344    instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, sub);
345    RecordSimplification();
346    neg->GetBlock()->RemoveInstruction(neg);
347  }
348}
349
350void InstructionSimplifierVisitor::VisitAnd(HAnd* instruction) {
351  HConstant* input_cst = instruction->GetConstantRight();
352  HInstruction* input_other = instruction->GetLeastConstantLeft();
353
354  if ((input_cst != nullptr) && AreAllBitsSet(input_cst)) {
355    // Replace code looking like
356    //    AND dst, src, 0xFFF...FF
357    // with
358    //    src
359    instruction->ReplaceWith(input_other);
360    instruction->GetBlock()->RemoveInstruction(instruction);
361    return;
362  }
363
364  // We assume that GVN has run before, so we only perform a pointer comparison.
365  // If for some reason the values are equal but the pointers are different, we
366  // are still correct and only miss an optimization opportunity.
367  if (instruction->GetLeft() == instruction->GetRight()) {
368    // Replace code looking like
369    //    AND dst, src, src
370    // with
371    //    src
372    instruction->ReplaceWith(instruction->GetLeft());
373    instruction->GetBlock()->RemoveInstruction(instruction);
374  }
375}
376
377void InstructionSimplifierVisitor::VisitDiv(HDiv* instruction) {
378  HConstant* input_cst = instruction->GetConstantRight();
379  HInstruction* input_other = instruction->GetLeastConstantLeft();
380  Primitive::Type type = instruction->GetType();
381
382  if ((input_cst != nullptr) && input_cst->IsOne()) {
383    // Replace code looking like
384    //    DIV dst, src, 1
385    // with
386    //    src
387    instruction->ReplaceWith(input_other);
388    instruction->GetBlock()->RemoveInstruction(instruction);
389    return;
390  }
391
392  if ((input_cst != nullptr) && input_cst->IsMinusOne()) {
393    // Replace code looking like
394    //    DIV dst, src, -1
395    // with
396    //    NEG dst, src
397    instruction->GetBlock()->ReplaceAndRemoveInstructionWith(
398        instruction, new (GetGraph()->GetArena()) HNeg(type, input_other));
399    RecordSimplification();
400    return;
401  }
402
403  if ((input_cst != nullptr) && Primitive::IsFloatingPointType(type)) {
404    // Try replacing code looking like
405    //    DIV dst, src, constant
406    // with
407    //    MUL dst, src, 1 / constant
408    HConstant* reciprocal = nullptr;
409    if (type == Primitive::Primitive::kPrimDouble) {
410      double value = input_cst->AsDoubleConstant()->GetValue();
411      if (CanDivideByReciprocalMultiplyDouble(bit_cast<int64_t, double>(value))) {
412        reciprocal = GetGraph()->GetDoubleConstant(1.0 / value);
413      }
414    } else {
415      DCHECK_EQ(type, Primitive::kPrimFloat);
416      float value = input_cst->AsFloatConstant()->GetValue();
417      if (CanDivideByReciprocalMultiplyFloat(bit_cast<int32_t, float>(value))) {
418        reciprocal = GetGraph()->GetFloatConstant(1.0f / value);
419      }
420    }
421
422    if (reciprocal != nullptr) {
423      instruction->GetBlock()->ReplaceAndRemoveInstructionWith(
424          instruction, new (GetGraph()->GetArena()) HMul(type, input_other, reciprocal));
425      RecordSimplification();
426      return;
427    }
428  }
429}
430
431void InstructionSimplifierVisitor::VisitMul(HMul* instruction) {
432  HConstant* input_cst = instruction->GetConstantRight();
433  HInstruction* input_other = instruction->GetLeastConstantLeft();
434  Primitive::Type type = instruction->GetType();
435  HBasicBlock* block = instruction->GetBlock();
436  ArenaAllocator* allocator = GetGraph()->GetArena();
437
438  if (input_cst == nullptr) {
439    return;
440  }
441
442  if (input_cst->IsOne()) {
443    // Replace code looking like
444    //    MUL dst, src, 1
445    // with
446    //    src
447    instruction->ReplaceWith(input_other);
448    instruction->GetBlock()->RemoveInstruction(instruction);
449    return;
450  }
451
452  if (input_cst->IsMinusOne() &&
453      (Primitive::IsFloatingPointType(type) || Primitive::IsIntOrLongType(type))) {
454    // Replace code looking like
455    //    MUL dst, src, -1
456    // with
457    //    NEG dst, src
458    HNeg* neg = new (allocator) HNeg(type, input_other);
459    block->ReplaceAndRemoveInstructionWith(instruction, neg);
460    RecordSimplification();
461    return;
462  }
463
464  if (Primitive::IsFloatingPointType(type) &&
465      ((input_cst->IsFloatConstant() && input_cst->AsFloatConstant()->GetValue() == 2.0f) ||
466       (input_cst->IsDoubleConstant() && input_cst->AsDoubleConstant()->GetValue() == 2.0))) {
467    // Replace code looking like
468    //    FP_MUL dst, src, 2.0
469    // with
470    //    FP_ADD dst, src, src
471    // The 'int' and 'long' cases are handled below.
472    block->ReplaceAndRemoveInstructionWith(instruction,
473                                           new (allocator) HAdd(type, input_other, input_other));
474    RecordSimplification();
475    return;
476  }
477
478  if (Primitive::IsIntOrLongType(type)) {
479    int64_t factor = Int64FromConstant(input_cst);
480    // Even though constant propagation also takes care of the zero case, other
481    // optimizations can lead to having a zero multiplication.
482    if (factor == 0) {
483      // Replace code looking like
484      //    MUL dst, src, 0
485      // with
486      //    0
487      instruction->ReplaceWith(input_cst);
488      instruction->GetBlock()->RemoveInstruction(instruction);
489    } else if (IsPowerOfTwo(factor)) {
490      // Replace code looking like
491      //    MUL dst, src, pow_of_2
492      // with
493      //    SHL dst, src, log2(pow_of_2)
494      HIntConstant* shift = GetGraph()->GetIntConstant(WhichPowerOf2(factor));
495      HShl* shl = new(allocator) HShl(type, input_other, shift);
496      block->ReplaceAndRemoveInstructionWith(instruction, shl);
497      RecordSimplification();
498    }
499  }
500}
501
502void InstructionSimplifierVisitor::VisitNeg(HNeg* instruction) {
503  HInstruction* input = instruction->GetInput();
504  if (input->IsNeg()) {
505    // Replace code looking like
506    //    NEG tmp, src
507    //    NEG dst, tmp
508    // with
509    //    src
510    HNeg* previous_neg = input->AsNeg();
511    instruction->ReplaceWith(previous_neg->GetInput());
512    instruction->GetBlock()->RemoveInstruction(instruction);
513    // We perform the optimization even if the input negation has environment
514    // uses since it allows removing the current instruction. But we only delete
515    // the input negation only if it is does not have any uses left.
516    if (!previous_neg->HasUses()) {
517      previous_neg->GetBlock()->RemoveInstruction(previous_neg);
518    }
519    RecordSimplification();
520    return;
521  }
522
523  if (input->IsSub() && input->HasOnlyOneNonEnvironmentUse() &&
524      !Primitive::IsFloatingPointType(input->GetType())) {
525    // Replace code looking like
526    //    SUB tmp, a, b
527    //    NEG dst, tmp
528    // with
529    //    SUB dst, b, a
530    // We do not perform the optimization if the input subtraction has
531    // environment uses or multiple non-environment uses as it could lead to
532    // worse code. In particular, we do not want the live ranges of `a` and `b`
533    // to be extended if we are not sure the initial 'SUB' instruction can be
534    // removed.
535    // We do not perform optimization for fp because we could lose the sign of zero.
536    HSub* sub = input->AsSub();
537    HSub* new_sub =
538        new (GetGraph()->GetArena()) HSub(instruction->GetType(), sub->GetRight(), sub->GetLeft());
539    instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, new_sub);
540    if (!sub->HasUses()) {
541      sub->GetBlock()->RemoveInstruction(sub);
542    }
543    RecordSimplification();
544  }
545}
546
547void InstructionSimplifierVisitor::VisitNot(HNot* instruction) {
548  HInstruction* input = instruction->GetInput();
549  if (input->IsNot()) {
550    // Replace code looking like
551    //    NOT tmp, src
552    //    NOT dst, tmp
553    // with
554    //    src
555    // We perform the optimization even if the input negation has environment
556    // uses since it allows removing the current instruction. But we only delete
557    // the input negation only if it is does not have any uses left.
558    HNot* previous_not = input->AsNot();
559    instruction->ReplaceWith(previous_not->GetInput());
560    instruction->GetBlock()->RemoveInstruction(instruction);
561    if (!previous_not->HasUses()) {
562      previous_not->GetBlock()->RemoveInstruction(previous_not);
563    }
564    RecordSimplification();
565  }
566}
567
568void InstructionSimplifierVisitor::VisitOr(HOr* instruction) {
569  HConstant* input_cst = instruction->GetConstantRight();
570  HInstruction* input_other = instruction->GetLeastConstantLeft();
571
572  if ((input_cst != nullptr) && input_cst->IsZero()) {
573    // Replace code looking like
574    //    OR dst, src, 0
575    // with
576    //    src
577    instruction->ReplaceWith(input_other);
578    instruction->GetBlock()->RemoveInstruction(instruction);
579    return;
580  }
581
582  // We assume that GVN has run before, so we only perform a pointer comparison.
583  // If for some reason the values are equal but the pointers are different, we
584  // are still correct and only miss an optimization opportunity.
585  if (instruction->GetLeft() == instruction->GetRight()) {
586    // Replace code looking like
587    //    OR dst, src, src
588    // with
589    //    src
590    instruction->ReplaceWith(instruction->GetLeft());
591    instruction->GetBlock()->RemoveInstruction(instruction);
592  }
593}
594
595void InstructionSimplifierVisitor::VisitShl(HShl* instruction) {
596  VisitShift(instruction);
597}
598
599void InstructionSimplifierVisitor::VisitShr(HShr* instruction) {
600  VisitShift(instruction);
601}
602
603void InstructionSimplifierVisitor::VisitSub(HSub* instruction) {
604  HConstant* input_cst = instruction->GetConstantRight();
605  HInstruction* input_other = instruction->GetLeastConstantLeft();
606
607  if ((input_cst != nullptr) && input_cst->IsZero()) {
608    // Replace code looking like
609    //    SUB dst, src, 0
610    // with
611    //    src
612    instruction->ReplaceWith(input_other);
613    instruction->GetBlock()->RemoveInstruction(instruction);
614    return;
615  }
616
617  Primitive::Type type = instruction->GetType();
618  if (!Primitive::IsIntegralType(type)) {
619    return;
620  }
621
622  HBasicBlock* block = instruction->GetBlock();
623  ArenaAllocator* allocator = GetGraph()->GetArena();
624
625  HInstruction* left = instruction->GetLeft();
626  HInstruction* right = instruction->GetRight();
627  if (left->IsConstant()) {
628    if (Int64FromConstant(left->AsConstant()) == 0) {
629      // Replace code looking like
630      //    SUB dst, 0, src
631      // with
632      //    NEG dst, src
633      // Note that we cannot optimize `0.0 - x` to `-x` for floating-point. When
634      // `x` is `0.0`, the former expression yields `0.0`, while the later
635      // yields `-0.0`.
636      HNeg* neg = new (allocator) HNeg(type, right);
637      block->ReplaceAndRemoveInstructionWith(instruction, neg);
638      RecordSimplification();
639      return;
640    }
641  }
642
643  if (left->IsNeg() && right->IsNeg()) {
644    if (TryMoveNegOnInputsAfterBinop(instruction)) {
645      return;
646    }
647  }
648
649  if (right->IsNeg() && right->HasOnlyOneNonEnvironmentUse()) {
650    // Replace code looking like
651    //    NEG tmp, b
652    //    SUB dst, a, tmp
653    // with
654    //    ADD dst, a, b
655    HAdd* add = new(GetGraph()->GetArena()) HAdd(type, left, right->AsNeg()->GetInput());
656    instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, add);
657    RecordSimplification();
658    right->GetBlock()->RemoveInstruction(right);
659    return;
660  }
661
662  if (left->IsNeg() && left->HasOnlyOneNonEnvironmentUse()) {
663    // Replace code looking like
664    //    NEG tmp, a
665    //    SUB dst, tmp, b
666    // with
667    //    ADD tmp, a, b
668    //    NEG dst, tmp
669    // The second version is not intrinsically better, but enables more
670    // transformations.
671    HAdd* add = new(GetGraph()->GetArena()) HAdd(type, left->AsNeg()->GetInput(), right);
672    instruction->GetBlock()->InsertInstructionBefore(add, instruction);
673    HNeg* neg = new (GetGraph()->GetArena()) HNeg(instruction->GetType(), add);
674    instruction->GetBlock()->InsertInstructionBefore(neg, instruction);
675    instruction->ReplaceWith(neg);
676    instruction->GetBlock()->RemoveInstruction(instruction);
677    RecordSimplification();
678    left->GetBlock()->RemoveInstruction(left);
679  }
680}
681
682void InstructionSimplifierVisitor::VisitUShr(HUShr* instruction) {
683  VisitShift(instruction);
684}
685
686void InstructionSimplifierVisitor::VisitXor(HXor* instruction) {
687  HConstant* input_cst = instruction->GetConstantRight();
688  HInstruction* input_other = instruction->GetLeastConstantLeft();
689
690  if ((input_cst != nullptr) && input_cst->IsZero()) {
691    // Replace code looking like
692    //    XOR dst, src, 0
693    // with
694    //    src
695    instruction->ReplaceWith(input_other);
696    instruction->GetBlock()->RemoveInstruction(instruction);
697    return;
698  }
699
700  if ((input_cst != nullptr) && AreAllBitsSet(input_cst)) {
701    // Replace code looking like
702    //    XOR dst, src, 0xFFF...FF
703    // with
704    //    NOT dst, src
705    HNot* bitwise_not = new (GetGraph()->GetArena()) HNot(instruction->GetType(), input_other);
706    instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, bitwise_not);
707    RecordSimplification();
708    return;
709  }
710}
711
712}  // namespace art
713