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