instruction_simplifier.cc revision a83a54d7f2322060f08480f8aabac5eb07268912
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 "intrinsics.h"
20#include "mirror/class-inl.h"
21#include "scoped_thread_state_change.h"
22
23namespace art {
24
25class InstructionSimplifierVisitor : public HGraphDelegateVisitor {
26 public:
27  InstructionSimplifierVisitor(HGraph* graph, OptimizingCompilerStats* stats)
28      : HGraphDelegateVisitor(graph),
29        stats_(stats) {}
30
31  void Run();
32
33 private:
34  void RecordSimplification() {
35    simplification_occurred_ = true;
36    simplifications_at_current_position_++;
37    if (stats_) {
38      stats_->RecordStat(kInstructionSimplifications);
39    }
40  }
41
42  bool TryMoveNegOnInputsAfterBinop(HBinaryOperation* binop);
43  void VisitShift(HBinaryOperation* shift);
44
45  void VisitSuspendCheck(HSuspendCheck* check) OVERRIDE;
46  void VisitEqual(HEqual* equal) OVERRIDE;
47  void VisitNotEqual(HNotEqual* equal) OVERRIDE;
48  void VisitBooleanNot(HBooleanNot* bool_not) OVERRIDE;
49  void VisitInstanceFieldSet(HInstanceFieldSet* equal) OVERRIDE;
50  void VisitStaticFieldSet(HStaticFieldSet* equal) OVERRIDE;
51  void VisitArraySet(HArraySet* equal) OVERRIDE;
52  void VisitTypeConversion(HTypeConversion* instruction) OVERRIDE;
53  void VisitNullCheck(HNullCheck* instruction) OVERRIDE;
54  void VisitArrayLength(HArrayLength* instruction) OVERRIDE;
55  void VisitCheckCast(HCheckCast* instruction) OVERRIDE;
56  void VisitAdd(HAdd* instruction) OVERRIDE;
57  void VisitAnd(HAnd* instruction) OVERRIDE;
58  void VisitCondition(HCondition* instruction) OVERRIDE;
59  void VisitGreaterThan(HGreaterThan* condition) OVERRIDE;
60  void VisitGreaterThanOrEqual(HGreaterThanOrEqual* condition) OVERRIDE;
61  void VisitLessThan(HLessThan* condition) OVERRIDE;
62  void VisitLessThanOrEqual(HLessThanOrEqual* condition) OVERRIDE;
63  void VisitDiv(HDiv* instruction) OVERRIDE;
64  void VisitMul(HMul* instruction) OVERRIDE;
65  void VisitNeg(HNeg* instruction) OVERRIDE;
66  void VisitNot(HNot* instruction) OVERRIDE;
67  void VisitOr(HOr* instruction) OVERRIDE;
68  void VisitShl(HShl* instruction) OVERRIDE;
69  void VisitShr(HShr* instruction) OVERRIDE;
70  void VisitSub(HSub* instruction) OVERRIDE;
71  void VisitUShr(HUShr* instruction) OVERRIDE;
72  void VisitXor(HXor* instruction) OVERRIDE;
73  void VisitInstanceOf(HInstanceOf* instruction) OVERRIDE;
74  void VisitFakeString(HFakeString* fake_string) OVERRIDE;
75  void VisitInvoke(HInvoke* invoke) OVERRIDE;
76
77  bool CanEnsureNotNullAt(HInstruction* instr, HInstruction* at) const;
78
79  OptimizingCompilerStats* stats_;
80  bool simplification_occurred_ = false;
81  int simplifications_at_current_position_ = 0;
82  // We ensure we do not loop infinitely. The value is a finger in the air guess
83  // that should allow enough simplification.
84  static constexpr int kMaxSamePositionSimplifications = 10;
85};
86
87void InstructionSimplifier::Run() {
88  InstructionSimplifierVisitor visitor(graph_, stats_);
89  visitor.Run();
90}
91
92void InstructionSimplifierVisitor::Run() {
93  // Iterate in reverse post order to open up more simplifications to users
94  // of instructions that got simplified.
95  for (HReversePostOrderIterator it(*GetGraph()); !it.Done();) {
96    // The simplification of an instruction to another instruction may yield
97    // possibilities for other simplifications. So although we perform a reverse
98    // post order visit, we sometimes need to revisit an instruction index.
99    simplification_occurred_ = false;
100    VisitBasicBlock(it.Current());
101    if (simplification_occurred_ &&
102        (simplifications_at_current_position_ < kMaxSamePositionSimplifications)) {
103      // New simplifications may be applicable to the instruction at the
104      // current index, so don't advance the iterator.
105      continue;
106    }
107    simplifications_at_current_position_ = 0;
108    it.Advance();
109  }
110}
111
112namespace {
113
114bool AreAllBitsSet(HConstant* constant) {
115  return Int64FromConstant(constant) == -1;
116}
117
118}  // namespace
119
120// Returns true if the code was simplified to use only one negation operation
121// after the binary operation instead of one on each of the inputs.
122bool InstructionSimplifierVisitor::TryMoveNegOnInputsAfterBinop(HBinaryOperation* binop) {
123  DCHECK(binop->IsAdd() || binop->IsSub());
124  DCHECK(binop->GetLeft()->IsNeg() && binop->GetRight()->IsNeg());
125  HNeg* left_neg = binop->GetLeft()->AsNeg();
126  HNeg* right_neg = binop->GetRight()->AsNeg();
127  if (!left_neg->HasOnlyOneNonEnvironmentUse() ||
128      !right_neg->HasOnlyOneNonEnvironmentUse()) {
129    return false;
130  }
131  // Replace code looking like
132  //    NEG tmp1, a
133  //    NEG tmp2, b
134  //    ADD dst, tmp1, tmp2
135  // with
136  //    ADD tmp, a, b
137  //    NEG dst, tmp
138  // Note that we cannot optimize `(-a) + (-b)` to `-(a + b)` for floating-point.
139  // When `a` is `-0.0` and `b` is `0.0`, the former expression yields `0.0`,
140  // while the later yields `-0.0`.
141  if (!Primitive::IsIntegralType(binop->GetType())) {
142    return false;
143  }
144  binop->ReplaceInput(left_neg->GetInput(), 0);
145  binop->ReplaceInput(right_neg->GetInput(), 1);
146  left_neg->GetBlock()->RemoveInstruction(left_neg);
147  right_neg->GetBlock()->RemoveInstruction(right_neg);
148  HNeg* neg = new (GetGraph()->GetArena()) HNeg(binop->GetType(), binop);
149  binop->GetBlock()->InsertInstructionBefore(neg, binop->GetNext());
150  binop->ReplaceWithExceptInReplacementAtIndex(neg, 0);
151  RecordSimplification();
152  return true;
153}
154
155void InstructionSimplifierVisitor::VisitShift(HBinaryOperation* instruction) {
156  DCHECK(instruction->IsShl() || instruction->IsShr() || instruction->IsUShr());
157  HConstant* input_cst = instruction->GetConstantRight();
158  HInstruction* input_other = instruction->GetLeastConstantLeft();
159
160  if (input_cst != nullptr) {
161    if (input_cst->IsZero()) {
162      // Replace code looking like
163      //    SHL dst, src, 0
164      // with
165      //    src
166      instruction->ReplaceWith(input_other);
167      instruction->GetBlock()->RemoveInstruction(instruction);
168    } else if (instruction->IsShl() && input_cst->IsOne()) {
169      // Replace Shl looking like
170      //    SHL dst, src, 1
171      // with
172      //    ADD dst, src, src
173      HAdd *add = new(GetGraph()->GetArena()) HAdd(instruction->GetType(),
174                                                   input_other,
175                                                   input_other);
176      instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, add);
177      RecordSimplification();
178    }
179  }
180}
181
182void InstructionSimplifierVisitor::VisitNullCheck(HNullCheck* null_check) {
183  HInstruction* obj = null_check->InputAt(0);
184  if (!obj->CanBeNull()) {
185    null_check->ReplaceWith(obj);
186    null_check->GetBlock()->RemoveInstruction(null_check);
187    if (stats_ != nullptr) {
188      stats_->RecordStat(MethodCompilationStat::kRemovedNullCheck);
189    }
190  }
191}
192
193bool InstructionSimplifierVisitor::CanEnsureNotNullAt(HInstruction* input, HInstruction* at) const {
194  if (!input->CanBeNull()) {
195    return true;
196  }
197
198  for (HUseIterator<HInstruction*> it(input->GetUses()); !it.Done(); it.Advance()) {
199    HInstruction* use = it.Current()->GetUser();
200    if (use->IsNullCheck() && use->StrictlyDominates(at)) {
201      return true;
202    }
203  }
204
205  return false;
206}
207
208// Returns whether doing a type test between the class of `object` against `klass` has
209// a statically known outcome. The result of the test is stored in `outcome`.
210static bool TypeCheckHasKnownOutcome(HLoadClass* klass, HInstruction* object, bool* outcome) {
211  DCHECK(!object->IsNullConstant()) << "Null constants should be special cased";
212  ReferenceTypeInfo obj_rti = object->GetReferenceTypeInfo();
213  ScopedObjectAccess soa(Thread::Current());
214  if (!obj_rti.IsValid()) {
215    // We run the simplifier before the reference type propagation so type info might not be
216    // available.
217    return false;
218  }
219
220  ReferenceTypeInfo class_rti = klass->GetLoadedClassRTI();
221  DCHECK(class_rti.IsValid() && class_rti.IsExact());
222  if (class_rti.IsSupertypeOf(obj_rti)) {
223    *outcome = true;
224    return true;
225  } else if (obj_rti.IsExact()) {
226    // The test failed at compile time so will also fail at runtime.
227    *outcome = false;
228    return true;
229  } else if (!class_rti.IsInterface()
230             && !obj_rti.IsInterface()
231             && !obj_rti.IsSupertypeOf(class_rti)) {
232    // Different type hierarchy. The test will fail.
233    *outcome = false;
234    return true;
235  }
236  return false;
237}
238
239void InstructionSimplifierVisitor::VisitCheckCast(HCheckCast* check_cast) {
240  HInstruction* object = check_cast->InputAt(0);
241  if (CanEnsureNotNullAt(object, check_cast)) {
242    check_cast->ClearMustDoNullCheck();
243  }
244
245  if (object->IsNullConstant()) {
246    check_cast->GetBlock()->RemoveInstruction(check_cast);
247    if (stats_ != nullptr) {
248      stats_->RecordStat(MethodCompilationStat::kRemovedCheckedCast);
249    }
250    return;
251  }
252
253  bool outcome;
254  HLoadClass* load_class = check_cast->InputAt(1)->AsLoadClass();
255  if (TypeCheckHasKnownOutcome(load_class, object, &outcome)) {
256    if (outcome) {
257      check_cast->GetBlock()->RemoveInstruction(check_cast);
258      if (stats_ != nullptr) {
259        stats_->RecordStat(MethodCompilationStat::kRemovedCheckedCast);
260      }
261      if (!load_class->HasUses()) {
262        // We cannot rely on DCE to remove the class because the `HLoadClass` thinks it can throw.
263        // However, here we know that it cannot because the checkcast was successfull, hence
264        // the class was already loaded.
265        load_class->GetBlock()->RemoveInstruction(load_class);
266      }
267    } else {
268      // Don't do anything for exceptional cases for now. Ideally we should remove
269      // all instructions and blocks this instruction dominates.
270    }
271  }
272}
273
274void InstructionSimplifierVisitor::VisitInstanceOf(HInstanceOf* instruction) {
275  HInstruction* object = instruction->InputAt(0);
276  bool can_be_null = true;
277  if (CanEnsureNotNullAt(object, instruction)) {
278    can_be_null = false;
279    instruction->ClearMustDoNullCheck();
280  }
281
282  HGraph* graph = GetGraph();
283  if (object->IsNullConstant()) {
284    instruction->ReplaceWith(graph->GetIntConstant(0));
285    instruction->GetBlock()->RemoveInstruction(instruction);
286    RecordSimplification();
287    return;
288  }
289
290  bool outcome;
291  HLoadClass* load_class = instruction->InputAt(1)->AsLoadClass();
292  if (TypeCheckHasKnownOutcome(load_class, object, &outcome)) {
293    if (outcome && can_be_null) {
294      // Type test will succeed, we just need a null test.
295      HNotEqual* test = new (graph->GetArena()) HNotEqual(graph->GetNullConstant(), object);
296      instruction->GetBlock()->InsertInstructionBefore(test, instruction);
297      instruction->ReplaceWith(test);
298    } else {
299      // We've statically determined the result of the instanceof.
300      instruction->ReplaceWith(graph->GetIntConstant(outcome));
301    }
302    RecordSimplification();
303    instruction->GetBlock()->RemoveInstruction(instruction);
304    if (outcome && !load_class->HasUses()) {
305      // We cannot rely on DCE to remove the class because the `HLoadClass` thinks it can throw.
306      // However, here we know that it cannot because the instanceof check was successfull, hence
307      // the class was already loaded.
308      load_class->GetBlock()->RemoveInstruction(load_class);
309    }
310  }
311}
312
313void InstructionSimplifierVisitor::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
314  if ((instruction->GetValue()->GetType() == Primitive::kPrimNot)
315      && CanEnsureNotNullAt(instruction->GetValue(), instruction)) {
316    instruction->ClearValueCanBeNull();
317  }
318}
319
320void InstructionSimplifierVisitor::VisitStaticFieldSet(HStaticFieldSet* instruction) {
321  if ((instruction->GetValue()->GetType() == Primitive::kPrimNot)
322      && CanEnsureNotNullAt(instruction->GetValue(), instruction)) {
323    instruction->ClearValueCanBeNull();
324  }
325}
326
327void InstructionSimplifierVisitor::VisitSuspendCheck(HSuspendCheck* check) {
328  HBasicBlock* block = check->GetBlock();
329  // Currently always keep the suspend check at entry.
330  if (block->IsEntryBlock()) return;
331
332  // Currently always keep suspend checks at loop entry.
333  if (block->IsLoopHeader() && block->GetFirstInstruction() == check) {
334    DCHECK(block->GetLoopInformation()->GetSuspendCheck() == check);
335    return;
336  }
337
338  // Remove the suspend check that was added at build time for the baseline
339  // compiler.
340  block->RemoveInstruction(check);
341}
342
343void InstructionSimplifierVisitor::VisitEqual(HEqual* equal) {
344  HInstruction* input_const = equal->GetConstantRight();
345  if (input_const != nullptr) {
346    HInstruction* input_value = equal->GetLeastConstantLeft();
347    if (input_value->GetType() == Primitive::kPrimBoolean && input_const->IsIntConstant()) {
348      HBasicBlock* block = equal->GetBlock();
349      // We are comparing the boolean to a constant which is of type int and can
350      // be any constant.
351      if (input_const->AsIntConstant()->IsOne()) {
352        // Replace (bool_value == true) with bool_value
353        equal->ReplaceWith(input_value);
354        block->RemoveInstruction(equal);
355        RecordSimplification();
356      } else if (input_const->AsIntConstant()->IsZero()) {
357        // Replace (bool_value == false) with !bool_value
358        block->ReplaceAndRemoveInstructionWith(
359            equal, new (block->GetGraph()->GetArena()) HBooleanNot(input_value));
360        RecordSimplification();
361      } else {
362        // Replace (bool_value == integer_not_zero_nor_one_constant) with false
363        equal->ReplaceWith(GetGraph()->GetIntConstant(0));
364        block->RemoveInstruction(equal);
365        RecordSimplification();
366      }
367    } else {
368      VisitCondition(equal);
369    }
370  } else {
371    VisitCondition(equal);
372  }
373}
374
375void InstructionSimplifierVisitor::VisitNotEqual(HNotEqual* not_equal) {
376  HInstruction* input_const = not_equal->GetConstantRight();
377  if (input_const != nullptr) {
378    HInstruction* input_value = not_equal->GetLeastConstantLeft();
379    if (input_value->GetType() == Primitive::kPrimBoolean && input_const->IsIntConstant()) {
380      HBasicBlock* block = not_equal->GetBlock();
381      // We are comparing the boolean to a constant which is of type int and can
382      // be any constant.
383      if (input_const->AsIntConstant()->IsOne()) {
384        // Replace (bool_value != true) with !bool_value
385        block->ReplaceAndRemoveInstructionWith(
386            not_equal, new (block->GetGraph()->GetArena()) HBooleanNot(input_value));
387        RecordSimplification();
388      } else if (input_const->AsIntConstant()->IsZero()) {
389        // Replace (bool_value != false) with bool_value
390        not_equal->ReplaceWith(input_value);
391        block->RemoveInstruction(not_equal);
392        RecordSimplification();
393      } else {
394        // Replace (bool_value != integer_not_zero_nor_one_constant) with true
395        not_equal->ReplaceWith(GetGraph()->GetIntConstant(1));
396        block->RemoveInstruction(not_equal);
397        RecordSimplification();
398      }
399    } else {
400      VisitCondition(not_equal);
401    }
402  } else {
403    VisitCondition(not_equal);
404  }
405}
406
407void InstructionSimplifierVisitor::VisitBooleanNot(HBooleanNot* bool_not) {
408  HInstruction* parent = bool_not->InputAt(0);
409  if (parent->IsBooleanNot()) {
410    HInstruction* value = parent->InputAt(0);
411    // Replace (!(!bool_value)) with bool_value
412    bool_not->ReplaceWith(value);
413    bool_not->GetBlock()->RemoveInstruction(bool_not);
414    // It is possible that `parent` is dead at this point but we leave
415    // its removal to DCE for simplicity.
416    RecordSimplification();
417  }
418}
419
420void InstructionSimplifierVisitor::VisitArrayLength(HArrayLength* instruction) {
421  HInstruction* input = instruction->InputAt(0);
422  // If the array is a NewArray with constant size, replace the array length
423  // with the constant instruction. This helps the bounds check elimination phase.
424  if (input->IsNewArray()) {
425    input = input->InputAt(0);
426    if (input->IsIntConstant()) {
427      instruction->ReplaceWith(input);
428    }
429  }
430}
431
432void InstructionSimplifierVisitor::VisitArraySet(HArraySet* instruction) {
433  HInstruction* value = instruction->GetValue();
434  if (value->GetType() != Primitive::kPrimNot) return;
435
436  if (CanEnsureNotNullAt(value, instruction)) {
437    instruction->ClearValueCanBeNull();
438  }
439
440  if (value->IsArrayGet()) {
441    if (value->AsArrayGet()->GetArray() == instruction->GetArray()) {
442      // If the code is just swapping elements in the array, no need for a type check.
443      instruction->ClearNeedsTypeCheck();
444      return;
445    }
446  }
447
448  if (value->IsNullConstant()) {
449    instruction->ClearNeedsTypeCheck();
450    return;
451  }
452
453  ScopedObjectAccess soa(Thread::Current());
454  ReferenceTypeInfo array_rti = instruction->GetArray()->GetReferenceTypeInfo();
455  ReferenceTypeInfo value_rti = value->GetReferenceTypeInfo();
456  if (!array_rti.IsValid()) {
457    return;
458  }
459
460  if (value_rti.IsValid() && array_rti.CanArrayHold(value_rti)) {
461    instruction->ClearNeedsTypeCheck();
462    return;
463  }
464
465  if (array_rti.IsObjectArray()) {
466    if (array_rti.IsExact()) {
467      instruction->ClearNeedsTypeCheck();
468      return;
469    }
470    instruction->SetStaticTypeOfArrayIsObjectArray();
471  }
472}
473
474void InstructionSimplifierVisitor::VisitTypeConversion(HTypeConversion* instruction) {
475  if (instruction->GetResultType() == instruction->GetInputType()) {
476    // Remove the instruction if it's converting to the same type.
477    instruction->ReplaceWith(instruction->GetInput());
478    instruction->GetBlock()->RemoveInstruction(instruction);
479  }
480}
481
482void InstructionSimplifierVisitor::VisitAdd(HAdd* instruction) {
483  HConstant* input_cst = instruction->GetConstantRight();
484  HInstruction* input_other = instruction->GetLeastConstantLeft();
485  if ((input_cst != nullptr) && input_cst->IsZero()) {
486    // Replace code looking like
487    //    ADD dst, src, 0
488    // with
489    //    src
490    // Note that we cannot optimize `x + 0.0` to `x` for floating-point. When
491    // `x` is `-0.0`, the former expression yields `0.0`, while the later
492    // yields `-0.0`.
493    if (Primitive::IsIntegralType(instruction->GetType())) {
494      instruction->ReplaceWith(input_other);
495      instruction->GetBlock()->RemoveInstruction(instruction);
496      return;
497    }
498  }
499
500  HInstruction* left = instruction->GetLeft();
501  HInstruction* right = instruction->GetRight();
502  bool left_is_neg = left->IsNeg();
503  bool right_is_neg = right->IsNeg();
504
505  if (left_is_neg && right_is_neg) {
506    if (TryMoveNegOnInputsAfterBinop(instruction)) {
507      return;
508    }
509  }
510
511  HNeg* neg = left_is_neg ? left->AsNeg() : right->AsNeg();
512  if ((left_is_neg ^ right_is_neg) && neg->HasOnlyOneNonEnvironmentUse()) {
513    // Replace code looking like
514    //    NEG tmp, b
515    //    ADD dst, a, tmp
516    // with
517    //    SUB dst, a, b
518    // We do not perform the optimization if the input negation has environment
519    // uses or multiple non-environment uses as it could lead to worse code. In
520    // particular, we do not want the live range of `b` to be extended if we are
521    // not sure the initial 'NEG' instruction can be removed.
522    HInstruction* other = left_is_neg ? right : left;
523    HSub* sub = new(GetGraph()->GetArena()) HSub(instruction->GetType(), other, neg->GetInput());
524    instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, sub);
525    RecordSimplification();
526    neg->GetBlock()->RemoveInstruction(neg);
527  }
528}
529
530void InstructionSimplifierVisitor::VisitAnd(HAnd* instruction) {
531  HConstant* input_cst = instruction->GetConstantRight();
532  HInstruction* input_other = instruction->GetLeastConstantLeft();
533
534  if (input_cst != nullptr) {
535    int64_t value = Int64FromConstant(input_cst);
536    if (value == -1) {
537      // Replace code looking like
538      //    AND dst, src, 0xFFF...FF
539      // with
540      //    src
541      instruction->ReplaceWith(input_other);
542      instruction->GetBlock()->RemoveInstruction(instruction);
543      RecordSimplification();
544      return;
545    }
546    // Eliminate And from UShr+And if the And-mask contains all the bits that
547    // can be non-zero after UShr. Transform Shr+And to UShr if the And-mask
548    // precisely clears the shifted-in sign bits.
549    if ((input_other->IsUShr() || input_other->IsShr()) && input_other->InputAt(1)->IsConstant()) {
550      size_t reg_bits = (instruction->GetResultType() == Primitive::kPrimLong) ? 64 : 32;
551      size_t shift = Int64FromConstant(input_other->InputAt(1)->AsConstant()) & (reg_bits - 1);
552      size_t num_tail_bits_set = CTZ(value + 1);
553      if ((num_tail_bits_set >= reg_bits - shift) && input_other->IsUShr()) {
554        // This AND clears only bits known to be clear, for example "(x >>> 24) & 0xff".
555        instruction->ReplaceWith(input_other);
556        instruction->GetBlock()->RemoveInstruction(instruction);
557        RecordSimplification();
558        return;
559      }  else if ((num_tail_bits_set == reg_bits - shift) && IsPowerOfTwo(value + 1) &&
560          input_other->HasOnlyOneNonEnvironmentUse()) {
561        DCHECK(input_other->IsShr());  // For UShr, we would have taken the branch above.
562        // Replace SHR+AND with USHR, for example "(x >> 24) & 0xff" -> "x >>> 24".
563        HUShr* ushr = new (GetGraph()->GetArena()) HUShr(instruction->GetType(),
564                                                         input_other->InputAt(0),
565                                                         input_other->InputAt(1),
566                                                         input_other->GetDexPc());
567        instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, ushr);
568        input_other->GetBlock()->RemoveInstruction(input_other);
569        RecordSimplification();
570        return;
571      }
572    }
573  }
574
575  // We assume that GVN has run before, so we only perform a pointer comparison.
576  // If for some reason the values are equal but the pointers are different, we
577  // are still correct and only miss an optimization opportunity.
578  if (instruction->GetLeft() == instruction->GetRight()) {
579    // Replace code looking like
580    //    AND dst, src, src
581    // with
582    //    src
583    instruction->ReplaceWith(instruction->GetLeft());
584    instruction->GetBlock()->RemoveInstruction(instruction);
585  }
586}
587
588void InstructionSimplifierVisitor::VisitGreaterThan(HGreaterThan* condition) {
589  VisitCondition(condition);
590}
591
592void InstructionSimplifierVisitor::VisitGreaterThanOrEqual(HGreaterThanOrEqual* condition) {
593  VisitCondition(condition);
594}
595
596void InstructionSimplifierVisitor::VisitLessThan(HLessThan* condition) {
597  VisitCondition(condition);
598}
599
600void InstructionSimplifierVisitor::VisitLessThanOrEqual(HLessThanOrEqual* condition) {
601  VisitCondition(condition);
602}
603
604void InstructionSimplifierVisitor::VisitCondition(HCondition* condition) {
605  // Try to fold an HCompare into this HCondition.
606
607  // This simplification is currently supported on x86, x86_64, ARM and ARM64.
608  // TODO: Implement it for MIPS64.
609  InstructionSet instruction_set = GetGraph()->GetInstructionSet();
610  if (instruction_set == kMips64) {
611    return;
612  }
613
614  HInstruction* left = condition->GetLeft();
615  HInstruction* right = condition->GetRight();
616  // We can only replace an HCondition which compares a Compare to 0.
617  // Both 'dx' and 'jack' generate a compare to 0 when compiling a
618  // condition with a long, float or double comparison as input.
619  if (!left->IsCompare() || !right->IsConstant() || right->AsIntConstant()->GetValue() != 0) {
620    // Conversion is not possible.
621    return;
622  }
623
624  // Is the Compare only used for this purpose?
625  if (!left->GetUses().HasOnlyOneUse()) {
626    // Someone else also wants the result of the compare.
627    return;
628  }
629
630  if (!left->GetEnvUses().IsEmpty()) {
631    // There is a reference to the compare result in an environment. Do we really need it?
632    if (GetGraph()->IsDebuggable()) {
633      return;
634    }
635
636    // We have to ensure that there are no deopt points in the sequence.
637    if (left->HasAnyEnvironmentUseBefore(condition)) {
638      return;
639    }
640  }
641
642  // Clean up any environment uses from the HCompare, if any.
643  left->RemoveEnvironmentUsers();
644
645  // We have decided to fold the HCompare into the HCondition. Transfer the information.
646  condition->SetBias(left->AsCompare()->GetBias());
647
648  // Replace the operands of the HCondition.
649  condition->ReplaceInput(left->InputAt(0), 0);
650  condition->ReplaceInput(left->InputAt(1), 1);
651
652  // Remove the HCompare.
653  left->GetBlock()->RemoveInstruction(left);
654
655  RecordSimplification();
656}
657
658void InstructionSimplifierVisitor::VisitDiv(HDiv* instruction) {
659  HConstant* input_cst = instruction->GetConstantRight();
660  HInstruction* input_other = instruction->GetLeastConstantLeft();
661  Primitive::Type type = instruction->GetType();
662
663  if ((input_cst != nullptr) && input_cst->IsOne()) {
664    // Replace code looking like
665    //    DIV dst, src, 1
666    // with
667    //    src
668    instruction->ReplaceWith(input_other);
669    instruction->GetBlock()->RemoveInstruction(instruction);
670    return;
671  }
672
673  if ((input_cst != nullptr) && input_cst->IsMinusOne()) {
674    // Replace code looking like
675    //    DIV dst, src, -1
676    // with
677    //    NEG dst, src
678    instruction->GetBlock()->ReplaceAndRemoveInstructionWith(
679        instruction, new (GetGraph()->GetArena()) HNeg(type, input_other));
680    RecordSimplification();
681    return;
682  }
683
684  if ((input_cst != nullptr) && Primitive::IsFloatingPointType(type)) {
685    // Try replacing code looking like
686    //    DIV dst, src, constant
687    // with
688    //    MUL dst, src, 1 / constant
689    HConstant* reciprocal = nullptr;
690    if (type == Primitive::Primitive::kPrimDouble) {
691      double value = input_cst->AsDoubleConstant()->GetValue();
692      if (CanDivideByReciprocalMultiplyDouble(bit_cast<int64_t, double>(value))) {
693        reciprocal = GetGraph()->GetDoubleConstant(1.0 / value);
694      }
695    } else {
696      DCHECK_EQ(type, Primitive::kPrimFloat);
697      float value = input_cst->AsFloatConstant()->GetValue();
698      if (CanDivideByReciprocalMultiplyFloat(bit_cast<int32_t, float>(value))) {
699        reciprocal = GetGraph()->GetFloatConstant(1.0f / value);
700      }
701    }
702
703    if (reciprocal != nullptr) {
704      instruction->GetBlock()->ReplaceAndRemoveInstructionWith(
705          instruction, new (GetGraph()->GetArena()) HMul(type, input_other, reciprocal));
706      RecordSimplification();
707      return;
708    }
709  }
710}
711
712void InstructionSimplifierVisitor::VisitMul(HMul* instruction) {
713  HConstant* input_cst = instruction->GetConstantRight();
714  HInstruction* input_other = instruction->GetLeastConstantLeft();
715  Primitive::Type type = instruction->GetType();
716  HBasicBlock* block = instruction->GetBlock();
717  ArenaAllocator* allocator = GetGraph()->GetArena();
718
719  if (input_cst == nullptr) {
720    return;
721  }
722
723  if (input_cst->IsOne()) {
724    // Replace code looking like
725    //    MUL dst, src, 1
726    // with
727    //    src
728    instruction->ReplaceWith(input_other);
729    instruction->GetBlock()->RemoveInstruction(instruction);
730    return;
731  }
732
733  if (input_cst->IsMinusOne() &&
734      (Primitive::IsFloatingPointType(type) || Primitive::IsIntOrLongType(type))) {
735    // Replace code looking like
736    //    MUL dst, src, -1
737    // with
738    //    NEG dst, src
739    HNeg* neg = new (allocator) HNeg(type, input_other);
740    block->ReplaceAndRemoveInstructionWith(instruction, neg);
741    RecordSimplification();
742    return;
743  }
744
745  if (Primitive::IsFloatingPointType(type) &&
746      ((input_cst->IsFloatConstant() && input_cst->AsFloatConstant()->GetValue() == 2.0f) ||
747       (input_cst->IsDoubleConstant() && input_cst->AsDoubleConstant()->GetValue() == 2.0))) {
748    // Replace code looking like
749    //    FP_MUL dst, src, 2.0
750    // with
751    //    FP_ADD dst, src, src
752    // The 'int' and 'long' cases are handled below.
753    block->ReplaceAndRemoveInstructionWith(instruction,
754                                           new (allocator) HAdd(type, input_other, input_other));
755    RecordSimplification();
756    return;
757  }
758
759  if (Primitive::IsIntOrLongType(type)) {
760    int64_t factor = Int64FromConstant(input_cst);
761    // Even though constant propagation also takes care of the zero case, other
762    // optimizations can lead to having a zero multiplication.
763    if (factor == 0) {
764      // Replace code looking like
765      //    MUL dst, src, 0
766      // with
767      //    0
768      instruction->ReplaceWith(input_cst);
769      instruction->GetBlock()->RemoveInstruction(instruction);
770    } else if (IsPowerOfTwo(factor)) {
771      // Replace code looking like
772      //    MUL dst, src, pow_of_2
773      // with
774      //    SHL dst, src, log2(pow_of_2)
775      HIntConstant* shift = GetGraph()->GetIntConstant(WhichPowerOf2(factor));
776      HShl* shl = new(allocator) HShl(type, input_other, shift);
777      block->ReplaceAndRemoveInstructionWith(instruction, shl);
778      RecordSimplification();
779    }
780  }
781}
782
783void InstructionSimplifierVisitor::VisitNeg(HNeg* instruction) {
784  HInstruction* input = instruction->GetInput();
785  if (input->IsNeg()) {
786    // Replace code looking like
787    //    NEG tmp, src
788    //    NEG dst, tmp
789    // with
790    //    src
791    HNeg* previous_neg = input->AsNeg();
792    instruction->ReplaceWith(previous_neg->GetInput());
793    instruction->GetBlock()->RemoveInstruction(instruction);
794    // We perform the optimization even if the input negation has environment
795    // uses since it allows removing the current instruction. But we only delete
796    // the input negation only if it is does not have any uses left.
797    if (!previous_neg->HasUses()) {
798      previous_neg->GetBlock()->RemoveInstruction(previous_neg);
799    }
800    RecordSimplification();
801    return;
802  }
803
804  if (input->IsSub() && input->HasOnlyOneNonEnvironmentUse() &&
805      !Primitive::IsFloatingPointType(input->GetType())) {
806    // Replace code looking like
807    //    SUB tmp, a, b
808    //    NEG dst, tmp
809    // with
810    //    SUB dst, b, a
811    // We do not perform the optimization if the input subtraction has
812    // environment uses or multiple non-environment uses as it could lead to
813    // worse code. In particular, we do not want the live ranges of `a` and `b`
814    // to be extended if we are not sure the initial 'SUB' instruction can be
815    // removed.
816    // We do not perform optimization for fp because we could lose the sign of zero.
817    HSub* sub = input->AsSub();
818    HSub* new_sub =
819        new (GetGraph()->GetArena()) HSub(instruction->GetType(), sub->GetRight(), sub->GetLeft());
820    instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, new_sub);
821    if (!sub->HasUses()) {
822      sub->GetBlock()->RemoveInstruction(sub);
823    }
824    RecordSimplification();
825  }
826}
827
828void InstructionSimplifierVisitor::VisitNot(HNot* instruction) {
829  HInstruction* input = instruction->GetInput();
830  if (input->IsNot()) {
831    // Replace code looking like
832    //    NOT tmp, src
833    //    NOT dst, tmp
834    // with
835    //    src
836    // We perform the optimization even if the input negation has environment
837    // uses since it allows removing the current instruction. But we only delete
838    // the input negation only if it is does not have any uses left.
839    HNot* previous_not = input->AsNot();
840    instruction->ReplaceWith(previous_not->GetInput());
841    instruction->GetBlock()->RemoveInstruction(instruction);
842    if (!previous_not->HasUses()) {
843      previous_not->GetBlock()->RemoveInstruction(previous_not);
844    }
845    RecordSimplification();
846  }
847}
848
849void InstructionSimplifierVisitor::VisitOr(HOr* instruction) {
850  HConstant* input_cst = instruction->GetConstantRight();
851  HInstruction* input_other = instruction->GetLeastConstantLeft();
852
853  if ((input_cst != nullptr) && input_cst->IsZero()) {
854    // Replace code looking like
855    //    OR dst, src, 0
856    // with
857    //    src
858    instruction->ReplaceWith(input_other);
859    instruction->GetBlock()->RemoveInstruction(instruction);
860    return;
861  }
862
863  // We assume that GVN has run before, so we only perform a pointer comparison.
864  // If for some reason the values are equal but the pointers are different, we
865  // are still correct and only miss an optimization opportunity.
866  if (instruction->GetLeft() == instruction->GetRight()) {
867    // Replace code looking like
868    //    OR dst, src, src
869    // with
870    //    src
871    instruction->ReplaceWith(instruction->GetLeft());
872    instruction->GetBlock()->RemoveInstruction(instruction);
873  }
874}
875
876void InstructionSimplifierVisitor::VisitShl(HShl* instruction) {
877  VisitShift(instruction);
878}
879
880void InstructionSimplifierVisitor::VisitShr(HShr* instruction) {
881  VisitShift(instruction);
882}
883
884void InstructionSimplifierVisitor::VisitSub(HSub* instruction) {
885  HConstant* input_cst = instruction->GetConstantRight();
886  HInstruction* input_other = instruction->GetLeastConstantLeft();
887
888  Primitive::Type type = instruction->GetType();
889  if (Primitive::IsFloatingPointType(type)) {
890    return;
891  }
892
893  if ((input_cst != nullptr) && input_cst->IsZero()) {
894    // Replace code looking like
895    //    SUB dst, src, 0
896    // with
897    //    src
898    // Note that we cannot optimize `x - 0.0` to `x` for floating-point. When
899    // `x` is `-0.0`, the former expression yields `0.0`, while the later
900    // yields `-0.0`.
901    instruction->ReplaceWith(input_other);
902    instruction->GetBlock()->RemoveInstruction(instruction);
903    return;
904  }
905
906  HBasicBlock* block = instruction->GetBlock();
907  ArenaAllocator* allocator = GetGraph()->GetArena();
908
909  HInstruction* left = instruction->GetLeft();
910  HInstruction* right = instruction->GetRight();
911  if (left->IsConstant()) {
912    if (Int64FromConstant(left->AsConstant()) == 0) {
913      // Replace code looking like
914      //    SUB dst, 0, src
915      // with
916      //    NEG dst, src
917      // Note that we cannot optimize `0.0 - x` to `-x` for floating-point. When
918      // `x` is `0.0`, the former expression yields `0.0`, while the later
919      // yields `-0.0`.
920      HNeg* neg = new (allocator) HNeg(type, right);
921      block->ReplaceAndRemoveInstructionWith(instruction, neg);
922      RecordSimplification();
923      return;
924    }
925  }
926
927  if (left->IsNeg() && right->IsNeg()) {
928    if (TryMoveNegOnInputsAfterBinop(instruction)) {
929      return;
930    }
931  }
932
933  if (right->IsNeg() && right->HasOnlyOneNonEnvironmentUse()) {
934    // Replace code looking like
935    //    NEG tmp, b
936    //    SUB dst, a, tmp
937    // with
938    //    ADD dst, a, b
939    HAdd* add = new(GetGraph()->GetArena()) HAdd(type, left, right->AsNeg()->GetInput());
940    instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, add);
941    RecordSimplification();
942    right->GetBlock()->RemoveInstruction(right);
943    return;
944  }
945
946  if (left->IsNeg() && left->HasOnlyOneNonEnvironmentUse()) {
947    // Replace code looking like
948    //    NEG tmp, a
949    //    SUB dst, tmp, b
950    // with
951    //    ADD tmp, a, b
952    //    NEG dst, tmp
953    // The second version is not intrinsically better, but enables more
954    // transformations.
955    HAdd* add = new(GetGraph()->GetArena()) HAdd(type, left->AsNeg()->GetInput(), right);
956    instruction->GetBlock()->InsertInstructionBefore(add, instruction);
957    HNeg* neg = new (GetGraph()->GetArena()) HNeg(instruction->GetType(), add);
958    instruction->GetBlock()->InsertInstructionBefore(neg, instruction);
959    instruction->ReplaceWith(neg);
960    instruction->GetBlock()->RemoveInstruction(instruction);
961    RecordSimplification();
962    left->GetBlock()->RemoveInstruction(left);
963  }
964}
965
966void InstructionSimplifierVisitor::VisitUShr(HUShr* instruction) {
967  VisitShift(instruction);
968}
969
970void InstructionSimplifierVisitor::VisitXor(HXor* instruction) {
971  HConstant* input_cst = instruction->GetConstantRight();
972  HInstruction* input_other = instruction->GetLeastConstantLeft();
973
974  if ((input_cst != nullptr) && input_cst->IsZero()) {
975    // Replace code looking like
976    //    XOR dst, src, 0
977    // with
978    //    src
979    instruction->ReplaceWith(input_other);
980    instruction->GetBlock()->RemoveInstruction(instruction);
981    return;
982  }
983
984  if ((input_cst != nullptr) && AreAllBitsSet(input_cst)) {
985    // Replace code looking like
986    //    XOR dst, src, 0xFFF...FF
987    // with
988    //    NOT dst, src
989    HNot* bitwise_not = new (GetGraph()->GetArena()) HNot(instruction->GetType(), input_other);
990    instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, bitwise_not);
991    RecordSimplification();
992    return;
993  }
994}
995
996void InstructionSimplifierVisitor::VisitFakeString(HFakeString* instruction) {
997  HInstruction* actual_string = nullptr;
998
999  // Find the string we need to replace this instruction with. The actual string is
1000  // the return value of a StringFactory call.
1001  for (HUseIterator<HInstruction*> it(instruction->GetUses()); !it.Done(); it.Advance()) {
1002    HInstruction* use = it.Current()->GetUser();
1003    if (use->IsInvokeStaticOrDirect()
1004        && use->AsInvokeStaticOrDirect()->IsStringFactoryFor(instruction)) {
1005      use->AsInvokeStaticOrDirect()->RemoveFakeStringArgumentAsLastInput();
1006      actual_string = use;
1007      break;
1008    }
1009  }
1010
1011  // Check that there is no other instruction that thinks it is the factory for that string.
1012  if (kIsDebugBuild) {
1013    CHECK(actual_string != nullptr);
1014    for (HUseIterator<HInstruction*> it(instruction->GetUses()); !it.Done(); it.Advance()) {
1015      HInstruction* use = it.Current()->GetUser();
1016      if (use->IsInvokeStaticOrDirect()) {
1017        CHECK(!use->AsInvokeStaticOrDirect()->IsStringFactoryFor(instruction));
1018      }
1019    }
1020  }
1021
1022  // We need to remove any environment uses of the fake string that are not dominated by
1023  // `actual_string` to null.
1024  for (HUseIterator<HEnvironment*> it(instruction->GetEnvUses()); !it.Done(); it.Advance()) {
1025    HEnvironment* environment = it.Current()->GetUser();
1026    if (!actual_string->StrictlyDominates(environment->GetHolder())) {
1027      environment->RemoveAsUserOfInput(it.Current()->GetIndex());
1028      environment->SetRawEnvAt(it.Current()->GetIndex(), nullptr);
1029    }
1030  }
1031
1032  // Only uses dominated by `actual_string` must remain. We can safely replace and remove
1033  // `instruction`.
1034  instruction->ReplaceWith(actual_string);
1035  instruction->GetBlock()->RemoveInstruction(instruction);
1036}
1037
1038void InstructionSimplifierVisitor::VisitInvoke(HInvoke* instruction) {
1039  if (instruction->GetIntrinsic() == Intrinsics::kStringEquals) {
1040    HInstruction* argument = instruction->InputAt(1);
1041    HInstruction* receiver = instruction->InputAt(0);
1042    if (receiver == argument) {
1043      // Because String.equals is an instance call, the receiver is
1044      // a null check if we don't know it's null. The argument however, will
1045      // be the actual object. So we cannot end up in a situation where both
1046      // are equal but could be null.
1047      DCHECK(CanEnsureNotNullAt(argument, instruction));
1048      instruction->ReplaceWith(GetGraph()->GetIntConstant(1));
1049      instruction->GetBlock()->RemoveInstruction(instruction);
1050    } else {
1051      StringEqualsOptimizations optimizations(instruction);
1052      if (CanEnsureNotNullAt(argument, instruction)) {
1053        optimizations.SetArgumentNotNull();
1054      }
1055      ScopedObjectAccess soa(Thread::Current());
1056      if (argument->GetReferenceTypeInfo().IsStringClass()) {
1057        optimizations.SetArgumentIsString();
1058      }
1059    }
1060  }
1061}
1062
1063}  // namespace art
1064