• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "art_method-inl.h"
20 #include "class_linker-inl.h"
21 #include "class_root-inl.h"
22 #include "data_type-inl.h"
23 #include "driver/compiler_options.h"
24 #include "escape.h"
25 #include "intrinsics.h"
26 #include "intrinsics_utils.h"
27 #include "mirror/class-inl.h"
28 #include "optimizing/data_type.h"
29 #include "optimizing/nodes.h"
30 #include "scoped_thread_state_change-inl.h"
31 #include "sharpening.h"
32 #include "string_builder_append.h"
33 
34 namespace art HIDDEN {
35 
36 // Whether to run an exhaustive test of individual HInstructions cloning when each instruction
37 // is replaced with its copy if it is clonable.
38 static constexpr bool kTestInstructionClonerExhaustively = false;
39 
40 class InstructionSimplifierVisitor final : public HGraphDelegateVisitor {
41  public:
InstructionSimplifierVisitor(HGraph * graph,CodeGenerator * codegen,OptimizingCompilerStats * stats,bool be_loop_friendly)42   InstructionSimplifierVisitor(HGraph* graph,
43                                CodeGenerator* codegen,
44                                OptimizingCompilerStats* stats,
45                                bool be_loop_friendly)
46       : HGraphDelegateVisitor(graph),
47         codegen_(codegen),
48         stats_(stats),
49         be_loop_friendly_(be_loop_friendly) {}
50 
51   bool Run();
52 
53  private:
RecordSimplification()54   void RecordSimplification() {
55     simplification_occurred_ = true;
56     simplifications_at_current_position_++;
57     MaybeRecordStat(stats_, MethodCompilationStat::kInstructionSimplifications);
58   }
59 
60   bool ReplaceRotateWithRor(HBinaryOperation* op, HUShr* ushr, HShl* shl);
61   bool TryReplaceWithRotate(HBinaryOperation* instruction);
62   bool TryReplaceWithRotateConstantPattern(HBinaryOperation* op, HUShr* ushr, HShl* shl);
63   bool TryReplaceWithRotateRegisterNegPattern(HBinaryOperation* op, HUShr* ushr, HShl* shl);
64   bool TryReplaceWithRotateRegisterSubPattern(HBinaryOperation* op, HUShr* ushr, HShl* shl);
65 
66   bool TryMoveNegOnInputsAfterBinop(HBinaryOperation* binop);
67   // `op` should be either HOr or HAnd.
68   // De Morgan's laws:
69   // ~a & ~b = ~(a | b)  and  ~a | ~b = ~(a & b)
70   bool TryDeMorganNegationFactoring(HBinaryOperation* op);
71   bool TryHandleAssociativeAndCommutativeOperation(HBinaryOperation* instruction);
72   bool TrySubtractionChainSimplification(HBinaryOperation* instruction);
73   bool TryCombineVecMultiplyAccumulate(HVecMul* mul);
74   void TryToReuseDiv(HRem* rem);
75 
76   void VisitShift(HBinaryOperation* shift);
77   void VisitEqual(HEqual* equal) override;
78   void VisitNotEqual(HNotEqual* equal) override;
79   void VisitBooleanNot(HBooleanNot* bool_not) override;
80   void VisitInstanceFieldSet(HInstanceFieldSet* equal) override;
81   void VisitStaticFieldSet(HStaticFieldSet* equal) override;
82   void VisitArraySet(HArraySet* equal) override;
83   void VisitTypeConversion(HTypeConversion* instruction) override;
84   void VisitNullCheck(HNullCheck* instruction) override;
85   void VisitArrayLength(HArrayLength* instruction) override;
86   void VisitCheckCast(HCheckCast* instruction) override;
87   void VisitAbs(HAbs* instruction) override;
88   void VisitAdd(HAdd* instruction) override;
89   void VisitAnd(HAnd* instruction) override;
90   void VisitCondition(HCondition* instruction) override;
91   void VisitGreaterThan(HGreaterThan* condition) override;
92   void VisitGreaterThanOrEqual(HGreaterThanOrEqual* condition) override;
93   void VisitLessThan(HLessThan* condition) override;
94   void VisitLessThanOrEqual(HLessThanOrEqual* condition) override;
95   void VisitBelow(HBelow* condition) override;
96   void VisitBelowOrEqual(HBelowOrEqual* condition) override;
97   void VisitAbove(HAbove* condition) override;
98   void VisitAboveOrEqual(HAboveOrEqual* condition) override;
99   void VisitDiv(HDiv* instruction) override;
100   void VisitRem(HRem* instruction) override;
101   void VisitMul(HMul* instruction) override;
102   void VisitNeg(HNeg* instruction) override;
103   void VisitNot(HNot* instruction) override;
104   void VisitOr(HOr* instruction) override;
105   void VisitShl(HShl* instruction) override;
106   void VisitShr(HShr* instruction) override;
107   void VisitSub(HSub* instruction) override;
108   void VisitUShr(HUShr* instruction) override;
109   void VisitXor(HXor* instruction) override;
110   void VisitSelect(HSelect* select) override;
111   void VisitIf(HIf* instruction) override;
112   void VisitInstanceOf(HInstanceOf* instruction) override;
113   void VisitInvoke(HInvoke* invoke) override;
114   void VisitDeoptimize(HDeoptimize* deoptimize) override;
115   void VisitVecMul(HVecMul* instruction) override;
116   void VisitPredicatedInstanceFieldGet(HPredicatedInstanceFieldGet* instruction) override;
117   void SimplifySystemArrayCopy(HInvoke* invoke);
118   void SimplifyStringEquals(HInvoke* invoke);
119   void SimplifyFP2Int(HInvoke* invoke);
120   void SimplifyStringCharAt(HInvoke* invoke);
121   void SimplifyStringLength(HInvoke* invoke);
122   void SimplifyStringIndexOf(HInvoke* invoke);
123   void SimplifyNPEOnArgN(HInvoke* invoke, size_t);
124   void SimplifyReturnThis(HInvoke* invoke);
125   void SimplifyAllocationIntrinsic(HInvoke* invoke);
126   void SimplifyVarHandleIntrinsic(HInvoke* invoke);
127 
128   bool CanUseKnownBootImageVarHandle(HInvoke* invoke);
129   static bool CanEnsureNotNullAt(HInstruction* input, HInstruction* at);
130 
131   CodeGenerator* codegen_;
132   OptimizingCompilerStats* stats_;
133   bool simplification_occurred_ = false;
134   int simplifications_at_current_position_ = 0;
135   // Prohibit optimizations which can affect HInductionVarAnalysis/HLoopOptimization
136   // and prevent loop optimizations:
137   //   true - avoid such optimizations.
138   //   false - allow such optimizations.
139   // Checked by the following optimizations:
140   //   - TryToReuseDiv: simplification of Div+Rem into Div+Mul+Sub.
141   bool be_loop_friendly_;
142   // We ensure we do not loop infinitely. The value should not be too high, since that
143   // would allow looping around the same basic block too many times. The value should
144   // not be too low either, however, since we want to allow revisiting a basic block
145   // with many statements and simplifications at least once.
146   static constexpr int kMaxSamePositionSimplifications = 50;
147 };
148 
Run()149 bool InstructionSimplifier::Run() {
150   if (kTestInstructionClonerExhaustively) {
151     CloneAndReplaceInstructionVisitor visitor(graph_);
152     visitor.VisitReversePostOrder();
153   }
154 
155   bool be_loop_friendly = (use_all_optimizations_ == false);
156 
157   InstructionSimplifierVisitor visitor(graph_, codegen_, stats_, be_loop_friendly);
158   return visitor.Run();
159 }
160 
Run()161 bool InstructionSimplifierVisitor::Run() {
162   bool didSimplify = false;
163   // Iterate in reverse post order to open up more simplifications to users
164   // of instructions that got simplified.
165   for (HBasicBlock* block : GetGraph()->GetReversePostOrder()) {
166     // The simplification of an instruction to another instruction may yield
167     // possibilities for other simplifications. So although we perform a reverse
168     // post order visit, we sometimes need to revisit an instruction index.
169     do {
170       simplification_occurred_ = false;
171       VisitBasicBlock(block);
172       if (simplification_occurred_) {
173         didSimplify = true;
174       }
175     } while (simplification_occurred_ &&
176              (simplifications_at_current_position_ < kMaxSamePositionSimplifications));
177     simplifications_at_current_position_ = 0;
178   }
179   return didSimplify;
180 }
181 
182 namespace {
183 
AreAllBitsSet(HConstant * constant)184 bool AreAllBitsSet(HConstant* constant) {
185   return Int64FromConstant(constant) == -1;
186 }
187 
188 }  // namespace
189 
190 // Returns true if the code was simplified to use only one negation operation
191 // after the binary operation instead of one on each of the inputs.
TryMoveNegOnInputsAfterBinop(HBinaryOperation * binop)192 bool InstructionSimplifierVisitor::TryMoveNegOnInputsAfterBinop(HBinaryOperation* binop) {
193   DCHECK(binop->IsAdd() || binop->IsSub());
194   DCHECK(binop->GetLeft()->IsNeg() && binop->GetRight()->IsNeg());
195   HNeg* left_neg = binop->GetLeft()->AsNeg();
196   HNeg* right_neg = binop->GetRight()->AsNeg();
197   if (!left_neg->HasOnlyOneNonEnvironmentUse() ||
198       !right_neg->HasOnlyOneNonEnvironmentUse()) {
199     return false;
200   }
201   // Replace code looking like
202   //    NEG tmp1, a
203   //    NEG tmp2, b
204   //    ADD dst, tmp1, tmp2
205   // with
206   //    ADD tmp, a, b
207   //    NEG dst, tmp
208   // Note that we cannot optimize `(-a) + (-b)` to `-(a + b)` for floating-point.
209   // When `a` is `-0.0` and `b` is `0.0`, the former expression yields `0.0`,
210   // while the later yields `-0.0`.
211   if (!DataType::IsIntegralType(binop->GetType())) {
212     return false;
213   }
214   binop->ReplaceInput(left_neg->GetInput(), 0);
215   binop->ReplaceInput(right_neg->GetInput(), 1);
216   left_neg->GetBlock()->RemoveInstruction(left_neg);
217   right_neg->GetBlock()->RemoveInstruction(right_neg);
218   HNeg* neg = new (GetGraph()->GetAllocator()) HNeg(binop->GetType(), binop);
219   binop->GetBlock()->InsertInstructionBefore(neg, binop->GetNext());
220   binop->ReplaceWithExceptInReplacementAtIndex(neg, 0);
221   RecordSimplification();
222   return true;
223 }
224 
TryDeMorganNegationFactoring(HBinaryOperation * op)225 bool InstructionSimplifierVisitor::TryDeMorganNegationFactoring(HBinaryOperation* op) {
226   DCHECK(op->IsAnd() || op->IsOr()) << op->DebugName();
227   DataType::Type type = op->GetType();
228   HInstruction* left = op->GetLeft();
229   HInstruction* right = op->GetRight();
230 
231   // We can apply De Morgan's laws if both inputs are Not's and are only used
232   // by `op`.
233   if (((left->IsNot() && right->IsNot()) ||
234        (left->IsBooleanNot() && right->IsBooleanNot())) &&
235       left->HasOnlyOneNonEnvironmentUse() &&
236       right->HasOnlyOneNonEnvironmentUse()) {
237     // Replace code looking like
238     //    NOT nota, a
239     //    NOT notb, b
240     //    AND dst, nota, notb (respectively OR)
241     // with
242     //    OR or, a, b         (respectively AND)
243     //    NOT dest, or
244     HInstruction* src_left = left->InputAt(0);
245     HInstruction* src_right = right->InputAt(0);
246     uint32_t dex_pc = op->GetDexPc();
247 
248     // Remove the negations on the inputs.
249     left->ReplaceWith(src_left);
250     right->ReplaceWith(src_right);
251     left->GetBlock()->RemoveInstruction(left);
252     right->GetBlock()->RemoveInstruction(right);
253 
254     // Replace the `HAnd` or `HOr`.
255     HBinaryOperation* hbin;
256     if (op->IsAnd()) {
257       hbin = new (GetGraph()->GetAllocator()) HOr(type, src_left, src_right, dex_pc);
258     } else {
259       hbin = new (GetGraph()->GetAllocator()) HAnd(type, src_left, src_right, dex_pc);
260     }
261     HInstruction* hnot;
262     if (left->IsBooleanNot()) {
263       hnot = new (GetGraph()->GetAllocator()) HBooleanNot(hbin, dex_pc);
264     } else {
265       hnot = new (GetGraph()->GetAllocator()) HNot(type, hbin, dex_pc);
266     }
267 
268     op->GetBlock()->InsertInstructionBefore(hbin, op);
269     op->GetBlock()->ReplaceAndRemoveInstructionWith(op, hnot);
270 
271     RecordSimplification();
272     return true;
273   }
274 
275   return false;
276 }
277 
TryCombineVecMultiplyAccumulate(HVecMul * mul)278 bool InstructionSimplifierVisitor::TryCombineVecMultiplyAccumulate(HVecMul* mul) {
279   DataType::Type type = mul->GetPackedType();
280   InstructionSet isa = codegen_->GetInstructionSet();
281   switch (isa) {
282     case InstructionSet::kArm64:
283       if (!(type == DataType::Type::kUint8 ||
284             type == DataType::Type::kInt8 ||
285             type == DataType::Type::kUint16 ||
286             type == DataType::Type::kInt16 ||
287             type == DataType::Type::kInt32)) {
288         return false;
289       }
290       break;
291     default:
292       return false;
293   }
294 
295   ArenaAllocator* allocator = mul->GetBlock()->GetGraph()->GetAllocator();
296   if (!mul->HasOnlyOneNonEnvironmentUse()) {
297     return false;
298   }
299   HInstruction* binop = mul->GetUses().front().GetUser();
300   if (!binop->IsVecAdd() && !binop->IsVecSub()) {
301     return false;
302   }
303 
304   // Replace code looking like
305   //    VECMUL tmp, x, y
306   //    VECADD/SUB dst, acc, tmp
307   // with
308   //    VECMULACC dst, acc, x, y
309   // Note that we do not want to (unconditionally) perform the merge when the
310   // multiplication has multiple uses and it can be merged in all of them.
311   // Multiple uses could happen on the same control-flow path, and we would
312   // then increase the amount of work. In the future we could try to evaluate
313   // whether all uses are on different control-flow paths (using dominance and
314   // reverse-dominance information) and only perform the merge when they are.
315   HInstruction* accumulator = nullptr;
316   HVecBinaryOperation* vec_binop = binop->AsVecBinaryOperation();
317   HInstruction* binop_left = vec_binop->GetLeft();
318   HInstruction* binop_right = vec_binop->GetRight();
319   // This is always true since the `HVecMul` has only one use (which is checked above).
320   DCHECK_NE(binop_left, binop_right);
321   if (binop_right == mul) {
322     accumulator = binop_left;
323   } else {
324     DCHECK_EQ(binop_left, mul);
325     // Only addition is commutative.
326     if (!binop->IsVecAdd()) {
327       return false;
328     }
329     accumulator = binop_right;
330   }
331 
332   DCHECK(accumulator != nullptr);
333   HInstruction::InstructionKind kind =
334       binop->IsVecAdd() ? HInstruction::kAdd : HInstruction::kSub;
335 
336   bool predicated_simd = vec_binop->IsPredicated();
337   if (predicated_simd && !HVecOperation::HaveSamePredicate(vec_binop, mul)) {
338     return false;
339   }
340 
341   HVecMultiplyAccumulate* mulacc =
342       new (allocator) HVecMultiplyAccumulate(allocator,
343                                              kind,
344                                              accumulator,
345                                              mul->GetLeft(),
346                                              mul->GetRight(),
347                                              vec_binop->GetPackedType(),
348                                              vec_binop->GetVectorLength(),
349                                              vec_binop->GetDexPc());
350 
351 
352 
353   vec_binop->GetBlock()->ReplaceAndRemoveInstructionWith(vec_binop, mulacc);
354   if (predicated_simd) {
355     mulacc->SetGoverningPredicate(vec_binop->GetGoverningPredicate(),
356                                   vec_binop->GetPredicationKind());
357   }
358 
359   DCHECK(!mul->HasUses());
360   mul->GetBlock()->RemoveInstruction(mul);
361   return true;
362 }
363 
VisitShift(HBinaryOperation * instruction)364 void InstructionSimplifierVisitor::VisitShift(HBinaryOperation* instruction) {
365   DCHECK(instruction->IsShl() || instruction->IsShr() || instruction->IsUShr());
366   HInstruction* shift_amount = instruction->GetRight();
367   HInstruction* value = instruction->GetLeft();
368 
369   int64_t implicit_mask = (value->GetType() == DataType::Type::kInt64)
370       ? kMaxLongShiftDistance
371       : kMaxIntShiftDistance;
372 
373   if (shift_amount->IsConstant()) {
374     int64_t cst = Int64FromConstant(shift_amount->AsConstant());
375     int64_t masked_cst = cst & implicit_mask;
376     if (masked_cst == 0) {
377       // Replace code looking like
378       //    SHL dst, value, 0
379       // with
380       //    value
381       instruction->ReplaceWith(value);
382       instruction->GetBlock()->RemoveInstruction(instruction);
383       RecordSimplification();
384       return;
385     } else if (masked_cst != cst) {
386       // Replace code looking like
387       //    SHL dst, value, cst
388       // where cst exceeds maximum distance with the equivalent
389       //    SHL dst, value, cst & implicit_mask
390       // (as defined by shift semantics). This ensures other
391       // optimizations do not need to special case for such situations.
392       DCHECK_EQ(shift_amount->GetType(), DataType::Type::kInt32);
393       instruction->ReplaceInput(GetGraph()->GetIntConstant(masked_cst), /* index= */ 1);
394       RecordSimplification();
395       return;
396     }
397   }
398 
399   // Shift operations implicitly mask the shift amount according to the type width. Get rid of
400   // unnecessary And/Or/Xor/Add/Sub/TypeConversion operations on the shift amount that do not
401   // affect the relevant bits.
402   // Replace code looking like
403   //    AND adjusted_shift, shift, <superset of implicit mask>
404   //    [OR/XOR/ADD/SUB adjusted_shift, shift, <value not overlapping with implicit mask>]
405   //    [<conversion-from-integral-non-64-bit-type> adjusted_shift, shift]
406   //    SHL dst, value, adjusted_shift
407   // with
408   //    SHL dst, value, shift
409   if (shift_amount->IsAnd() ||
410       shift_amount->IsOr() ||
411       shift_amount->IsXor() ||
412       shift_amount->IsAdd() ||
413       shift_amount->IsSub()) {
414     int64_t required_result = shift_amount->IsAnd() ? implicit_mask : 0;
415     HBinaryOperation* bin_op = shift_amount->AsBinaryOperation();
416     HConstant* mask = bin_op->GetConstantRight();
417     if (mask != nullptr && (Int64FromConstant(mask) & implicit_mask) == required_result) {
418       instruction->ReplaceInput(bin_op->GetLeastConstantLeft(), 1);
419       RecordSimplification();
420       return;
421     }
422   } else if (shift_amount->IsTypeConversion()) {
423     DCHECK_NE(shift_amount->GetType(), DataType::Type::kBool);  // We never convert to bool.
424     DataType::Type source_type = shift_amount->InputAt(0)->GetType();
425     // Non-integral and 64-bit source types require an explicit type conversion.
426     if (DataType::IsIntegralType(source_type) && !DataType::Is64BitType(source_type)) {
427       instruction->ReplaceInput(shift_amount->AsTypeConversion()->GetInput(), 1);
428       RecordSimplification();
429       return;
430     }
431   }
432 }
433 
IsSubRegBitsMinusOther(HSub * sub,size_t reg_bits,HInstruction * other)434 static bool IsSubRegBitsMinusOther(HSub* sub, size_t reg_bits, HInstruction* other) {
435   return (sub->GetRight() == other &&
436           sub->GetLeft()->IsConstant() &&
437           (Int64FromConstant(sub->GetLeft()->AsConstant()) & (reg_bits - 1)) == 0);
438 }
439 
ReplaceRotateWithRor(HBinaryOperation * op,HUShr * ushr,HShl * shl)440 bool InstructionSimplifierVisitor::ReplaceRotateWithRor(HBinaryOperation* op,
441                                                         HUShr* ushr,
442                                                         HShl* shl) {
443   DCHECK(op->IsAdd() || op->IsXor() || op->IsOr()) << op->DebugName();
444   HRor* ror =
445       new (GetGraph()->GetAllocator()) HRor(ushr->GetType(), ushr->GetLeft(), ushr->GetRight());
446   op->GetBlock()->ReplaceAndRemoveInstructionWith(op, ror);
447   if (!ushr->HasUses()) {
448     ushr->GetBlock()->RemoveInstruction(ushr);
449   }
450   if (!ushr->GetRight()->HasUses()) {
451     ushr->GetRight()->GetBlock()->RemoveInstruction(ushr->GetRight());
452   }
453   if (!shl->HasUses()) {
454     shl->GetBlock()->RemoveInstruction(shl);
455   }
456   if (!shl->GetRight()->HasUses()) {
457     shl->GetRight()->GetBlock()->RemoveInstruction(shl->GetRight());
458   }
459   RecordSimplification();
460   return true;
461 }
462 
463 // Try to replace a binary operation flanked by one UShr and one Shl with a bitfield rotation.
TryReplaceWithRotate(HBinaryOperation * op)464 bool InstructionSimplifierVisitor::TryReplaceWithRotate(HBinaryOperation* op) {
465   DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
466   HInstruction* left = op->GetLeft();
467   HInstruction* right = op->GetRight();
468   // If we have an UShr and a Shl (in either order).
469   if ((left->IsUShr() && right->IsShl()) || (left->IsShl() && right->IsUShr())) {
470     HUShr* ushr = left->IsUShr() ? left->AsUShr() : right->AsUShr();
471     HShl* shl = left->IsShl() ? left->AsShl() : right->AsShl();
472     DCHECK(DataType::IsIntOrLongType(ushr->GetType()));
473     if (ushr->GetType() == shl->GetType() &&
474         ushr->GetLeft() == shl->GetLeft()) {
475       if (ushr->GetRight()->IsConstant() && shl->GetRight()->IsConstant()) {
476         // Shift distances are both constant, try replacing with Ror if they
477         // add up to the register size.
478         return TryReplaceWithRotateConstantPattern(op, ushr, shl);
479       } else if (ushr->GetRight()->IsSub() || shl->GetRight()->IsSub()) {
480         // Shift distances are potentially of the form x and (reg_size - x).
481         return TryReplaceWithRotateRegisterSubPattern(op, ushr, shl);
482       } else if (ushr->GetRight()->IsNeg() || shl->GetRight()->IsNeg()) {
483         // Shift distances are potentially of the form d and -d.
484         return TryReplaceWithRotateRegisterNegPattern(op, ushr, shl);
485       }
486     }
487   }
488   return false;
489 }
490 
491 // Try replacing code looking like (x >>> #rdist OP x << #ldist):
492 //    UShr dst, x,   #rdist
493 //    Shl  tmp, x,   #ldist
494 //    OP   dst, dst, tmp
495 // or like (x >>> #rdist OP x << #-ldist):
496 //    UShr dst, x,   #rdist
497 //    Shl  tmp, x,   #-ldist
498 //    OP   dst, dst, tmp
499 // with
500 //    Ror  dst, x,   #rdist
TryReplaceWithRotateConstantPattern(HBinaryOperation * op,HUShr * ushr,HShl * shl)501 bool InstructionSimplifierVisitor::TryReplaceWithRotateConstantPattern(HBinaryOperation* op,
502                                                                        HUShr* ushr,
503                                                                        HShl* shl) {
504   DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
505   size_t reg_bits = DataType::Size(ushr->GetType()) * kBitsPerByte;
506   size_t rdist = Int64FromConstant(ushr->GetRight()->AsConstant());
507   size_t ldist = Int64FromConstant(shl->GetRight()->AsConstant());
508   if (((ldist + rdist) & (reg_bits - 1)) == 0) {
509     ReplaceRotateWithRor(op, ushr, shl);
510     return true;
511   }
512   return false;
513 }
514 
515 // Replace code looking like (x >>> -d OP x << d):
516 //    Neg  neg, d
517 //    UShr dst, x,   neg
518 //    Shl  tmp, x,   d
519 //    OP   dst, dst, tmp
520 // with
521 //    Neg  neg, d
522 //    Ror  dst, x,   neg
523 // *** OR ***
524 // Replace code looking like (x >>> d OP x << -d):
525 //    UShr dst, x,   d
526 //    Neg  neg, d
527 //    Shl  tmp, x,   neg
528 //    OP   dst, dst, tmp
529 // with
530 //    Ror  dst, x,   d
TryReplaceWithRotateRegisterNegPattern(HBinaryOperation * op,HUShr * ushr,HShl * shl)531 bool InstructionSimplifierVisitor::TryReplaceWithRotateRegisterNegPattern(HBinaryOperation* op,
532                                                                           HUShr* ushr,
533                                                                           HShl* shl) {
534   DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
535   DCHECK(ushr->GetRight()->IsNeg() || shl->GetRight()->IsNeg());
536   bool neg_is_left = shl->GetRight()->IsNeg();
537   HNeg* neg = neg_is_left ? shl->GetRight()->AsNeg() : ushr->GetRight()->AsNeg();
538   // And the shift distance being negated is the distance being shifted the other way.
539   if (neg->InputAt(0) == (neg_is_left ? ushr->GetRight() : shl->GetRight())) {
540     ReplaceRotateWithRor(op, ushr, shl);
541   }
542   return false;
543 }
544 
545 // Try replacing code looking like (x >>> d OP x << (#bits - d)):
546 //    UShr dst, x,     d
547 //    Sub  ld,  #bits, d
548 //    Shl  tmp, x,     ld
549 //    OP   dst, dst,   tmp
550 // with
551 //    Ror  dst, x,     d
552 // *** OR ***
553 // Replace code looking like (x >>> (#bits - d) OP x << d):
554 //    Sub  rd,  #bits, d
555 //    UShr dst, x,     rd
556 //    Shl  tmp, x,     d
557 //    OP   dst, dst,   tmp
558 // with
559 //    Neg  neg, d
560 //    Ror  dst, x,     neg
TryReplaceWithRotateRegisterSubPattern(HBinaryOperation * op,HUShr * ushr,HShl * shl)561 bool InstructionSimplifierVisitor::TryReplaceWithRotateRegisterSubPattern(HBinaryOperation* op,
562                                                                           HUShr* ushr,
563                                                                           HShl* shl) {
564   DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
565   DCHECK(ushr->GetRight()->IsSub() || shl->GetRight()->IsSub());
566   size_t reg_bits = DataType::Size(ushr->GetType()) * kBitsPerByte;
567   HInstruction* shl_shift = shl->GetRight();
568   HInstruction* ushr_shift = ushr->GetRight();
569   if ((shl_shift->IsSub() && IsSubRegBitsMinusOther(shl_shift->AsSub(), reg_bits, ushr_shift)) ||
570       (ushr_shift->IsSub() && IsSubRegBitsMinusOther(ushr_shift->AsSub(), reg_bits, shl_shift))) {
571     return ReplaceRotateWithRor(op, ushr, shl);
572   }
573   return false;
574 }
575 
VisitNullCheck(HNullCheck * null_check)576 void InstructionSimplifierVisitor::VisitNullCheck(HNullCheck* null_check) {
577   HInstruction* obj = null_check->InputAt(0);
578   if (!obj->CanBeNull()) {
579     null_check->ReplaceWith(obj);
580     null_check->GetBlock()->RemoveInstruction(null_check);
581     if (stats_ != nullptr) {
582       stats_->RecordStat(MethodCompilationStat::kRemovedNullCheck);
583     }
584   }
585 }
586 
CanEnsureNotNullAt(HInstruction * input,HInstruction * at)587 bool InstructionSimplifierVisitor::CanEnsureNotNullAt(HInstruction* input, HInstruction* at) {
588   if (!input->CanBeNull()) {
589     return true;
590   }
591 
592   for (const HUseListNode<HInstruction*>& use : input->GetUses()) {
593     HInstruction* user = use.GetUser();
594     if (user->IsNullCheck() && user->StrictlyDominates(at)) {
595       return true;
596     }
597   }
598 
599   return false;
600 }
601 
602 // Returns whether doing a type test between the class of `object` against `klass` has
603 // a statically known outcome. The result of the test is stored in `outcome`.
TypeCheckHasKnownOutcome(ReferenceTypeInfo class_rti,HInstruction * object,bool * outcome)604 static bool TypeCheckHasKnownOutcome(ReferenceTypeInfo class_rti,
605                                      HInstruction* object,
606                                      /*out*/bool* outcome) {
607   DCHECK(!object->IsNullConstant()) << "Null constants should be special cased";
608   ReferenceTypeInfo obj_rti = object->GetReferenceTypeInfo();
609   ScopedObjectAccess soa(Thread::Current());
610   if (!obj_rti.IsValid()) {
611     // We run the simplifier before the reference type propagation so type info might not be
612     // available.
613     return false;
614   }
615 
616   if (!class_rti.IsValid()) {
617     // Happens when the loaded class is unresolved.
618     if (obj_rti.IsExact()) {
619       // outcome == 'true' && obj_rti is valid implies that class_rti is valid.
620       // Since that's a contradiction we must not pass this check.
621       *outcome = false;
622       return true;
623     } else {
624       // We aren't able to say anything in particular since we don't know the
625       // exact type of the object.
626       return false;
627     }
628   }
629   DCHECK(class_rti.IsExact());
630   if (class_rti.IsSupertypeOf(obj_rti)) {
631     *outcome = true;
632     return true;
633   } else if (obj_rti.IsExact()) {
634     // The test failed at compile time so will also fail at runtime.
635     *outcome = false;
636     return true;
637   } else if (!class_rti.IsInterface()
638              && !obj_rti.IsInterface()
639              && !obj_rti.IsSupertypeOf(class_rti)) {
640     // Different type hierarchy. The test will fail.
641     *outcome = false;
642     return true;
643   }
644   return false;
645 }
646 
VisitCheckCast(HCheckCast * check_cast)647 void InstructionSimplifierVisitor::VisitCheckCast(HCheckCast* check_cast) {
648   HInstruction* object = check_cast->InputAt(0);
649   if (CanEnsureNotNullAt(object, check_cast)) {
650     check_cast->ClearMustDoNullCheck();
651   }
652 
653   if (object->IsNullConstant()) {
654     check_cast->GetBlock()->RemoveInstruction(check_cast);
655     MaybeRecordStat(stats_, MethodCompilationStat::kRemovedCheckedCast);
656     return;
657   }
658 
659   // Minor correctness check.
660   DCHECK(check_cast->GetTargetClass()->StrictlyDominates(check_cast))
661       << "Illegal graph!\n"
662       << check_cast->DumpWithArgs();
663 
664   // Historical note: The `outcome` was initialized to please Valgrind - the compiler can reorder
665   // the return value check with the `outcome` check, b/27651442.
666   bool outcome = false;
667   if (TypeCheckHasKnownOutcome(check_cast->GetTargetClassRTI(), object, &outcome)) {
668     if (outcome) {
669       check_cast->GetBlock()->RemoveInstruction(check_cast);
670       MaybeRecordStat(stats_, MethodCompilationStat::kRemovedCheckedCast);
671       if (check_cast->GetTypeCheckKind() != TypeCheckKind::kBitstringCheck) {
672         HLoadClass* load_class = check_cast->GetTargetClass();
673         if (!load_class->HasUses() && !load_class->NeedsAccessCheck()) {
674           // We cannot rely on DCE to remove the class because the `HLoadClass` thinks it can throw.
675           // However, here we know that it cannot because the checkcast was successful, hence
676           // the class was already loaded.
677           load_class->GetBlock()->RemoveInstruction(load_class);
678         }
679       }
680     } else {
681       // TODO Don't do anything for exceptional cases for now. Ideally we should
682       // remove all instructions and blocks this instruction dominates and
683       // replace it with a manual throw.
684     }
685   }
686 }
687 
VisitInstanceOf(HInstanceOf * instruction)688 void InstructionSimplifierVisitor::VisitInstanceOf(HInstanceOf* instruction) {
689   HInstruction* object = instruction->InputAt(0);
690 
691   bool can_be_null = true;
692   if (CanEnsureNotNullAt(object, instruction)) {
693     can_be_null = false;
694     instruction->ClearMustDoNullCheck();
695   }
696 
697   HGraph* graph = GetGraph();
698   if (object->IsNullConstant()) {
699     MaybeRecordStat(stats_, MethodCompilationStat::kRemovedInstanceOf);
700     instruction->ReplaceWith(graph->GetIntConstant(0));
701     instruction->GetBlock()->RemoveInstruction(instruction);
702     RecordSimplification();
703     return;
704   }
705 
706   // Minor correctness check.
707   DCHECK(instruction->GetTargetClass()->StrictlyDominates(instruction))
708       << "Illegal graph!\n"
709       << instruction->DumpWithArgs();
710 
711   // Historical note: The `outcome` was initialized to please Valgrind - the compiler can reorder
712   // the return value check with the `outcome` check, b/27651442.
713   bool outcome = false;
714   if (TypeCheckHasKnownOutcome(instruction->GetTargetClassRTI(), object, &outcome)) {
715     MaybeRecordStat(stats_, MethodCompilationStat::kRemovedInstanceOf);
716     if (outcome && can_be_null) {
717       // Type test will succeed, we just need a null test.
718       HNotEqual* test = new (graph->GetAllocator()) HNotEqual(graph->GetNullConstant(), object);
719       instruction->GetBlock()->InsertInstructionBefore(test, instruction);
720       instruction->ReplaceWith(test);
721     } else {
722       // We've statically determined the result of the instanceof.
723       instruction->ReplaceWith(graph->GetIntConstant(outcome));
724     }
725     RecordSimplification();
726     instruction->GetBlock()->RemoveInstruction(instruction);
727     if (outcome && instruction->GetTypeCheckKind() != TypeCheckKind::kBitstringCheck) {
728       HLoadClass* load_class = instruction->GetTargetClass();
729       if (!load_class->HasUses() && !load_class->NeedsAccessCheck()) {
730         // We cannot rely on DCE to remove the class because the `HLoadClass`
731         // thinks it can throw. However, here we know that it cannot because the
732         // instanceof check was successful and we don't need to check the
733         // access, hence the class was already loaded.
734         load_class->GetBlock()->RemoveInstruction(load_class);
735       }
736     }
737   }
738 }
739 
VisitInstanceFieldSet(HInstanceFieldSet * instruction)740 void InstructionSimplifierVisitor::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
741   if ((instruction->GetValue()->GetType() == DataType::Type::kReference)
742       && CanEnsureNotNullAt(instruction->GetValue(), instruction)) {
743     instruction->ClearValueCanBeNull();
744   }
745 }
746 
VisitStaticFieldSet(HStaticFieldSet * instruction)747 void InstructionSimplifierVisitor::VisitStaticFieldSet(HStaticFieldSet* instruction) {
748   if ((instruction->GetValue()->GetType() == DataType::Type::kReference)
749       && CanEnsureNotNullAt(instruction->GetValue(), instruction)) {
750     instruction->ClearValueCanBeNull();
751   }
752 }
753 
GetOppositeConditionSwapOps(ArenaAllocator * allocator,HInstruction * cond)754 static HCondition* GetOppositeConditionSwapOps(ArenaAllocator* allocator, HInstruction* cond) {
755   HInstruction *lhs = cond->InputAt(0);
756   HInstruction *rhs = cond->InputAt(1);
757   switch (cond->GetKind()) {
758     case HInstruction::kEqual:
759       return new (allocator) HEqual(rhs, lhs);
760     case HInstruction::kNotEqual:
761       return new (allocator) HNotEqual(rhs, lhs);
762     case HInstruction::kLessThan:
763       return new (allocator) HGreaterThan(rhs, lhs);
764     case HInstruction::kLessThanOrEqual:
765       return new (allocator) HGreaterThanOrEqual(rhs, lhs);
766     case HInstruction::kGreaterThan:
767       return new (allocator) HLessThan(rhs, lhs);
768     case HInstruction::kGreaterThanOrEqual:
769       return new (allocator) HLessThanOrEqual(rhs, lhs);
770     case HInstruction::kBelow:
771       return new (allocator) HAbove(rhs, lhs);
772     case HInstruction::kBelowOrEqual:
773       return new (allocator) HAboveOrEqual(rhs, lhs);
774     case HInstruction::kAbove:
775       return new (allocator) HBelow(rhs, lhs);
776     case HInstruction::kAboveOrEqual:
777       return new (allocator) HBelowOrEqual(rhs, lhs);
778     default:
779       LOG(FATAL) << "Unknown ConditionType " << cond->GetKind();
780       UNREACHABLE();
781   }
782 }
783 
VisitEqual(HEqual * equal)784 void InstructionSimplifierVisitor::VisitEqual(HEqual* equal) {
785   HInstruction* input_const = equal->GetConstantRight();
786   if (input_const != nullptr) {
787     HInstruction* input_value = equal->GetLeastConstantLeft();
788     if ((input_value->GetType() == DataType::Type::kBool) && input_const->IsIntConstant()) {
789       HBasicBlock* block = equal->GetBlock();
790       // We are comparing the boolean to a constant which is of type int and can
791       // be any constant.
792       if (input_const->AsIntConstant()->IsTrue()) {
793         // Replace (bool_value == true) with bool_value
794         equal->ReplaceWith(input_value);
795         block->RemoveInstruction(equal);
796         RecordSimplification();
797       } else if (input_const->AsIntConstant()->IsFalse()) {
798         // Replace (bool_value == false) with !bool_value
799         equal->ReplaceWith(GetGraph()->InsertOppositeCondition(input_value, equal));
800         block->RemoveInstruction(equal);
801         RecordSimplification();
802       } else {
803         // Replace (bool_value == integer_not_zero_nor_one_constant) with false
804         equal->ReplaceWith(GetGraph()->GetIntConstant(0));
805         block->RemoveInstruction(equal);
806         RecordSimplification();
807       }
808     } else {
809       VisitCondition(equal);
810     }
811   } else {
812     VisitCondition(equal);
813   }
814 }
815 
VisitNotEqual(HNotEqual * not_equal)816 void InstructionSimplifierVisitor::VisitNotEqual(HNotEqual* not_equal) {
817   HInstruction* input_const = not_equal->GetConstantRight();
818   if (input_const != nullptr) {
819     HInstruction* input_value = not_equal->GetLeastConstantLeft();
820     if ((input_value->GetType() == DataType::Type::kBool) && input_const->IsIntConstant()) {
821       HBasicBlock* block = not_equal->GetBlock();
822       // We are comparing the boolean to a constant which is of type int and can
823       // be any constant.
824       if (input_const->AsIntConstant()->IsTrue()) {
825         // Replace (bool_value != true) with !bool_value
826         not_equal->ReplaceWith(GetGraph()->InsertOppositeCondition(input_value, not_equal));
827         block->RemoveInstruction(not_equal);
828         RecordSimplification();
829       } else if (input_const->AsIntConstant()->IsFalse()) {
830         // Replace (bool_value != false) with bool_value
831         not_equal->ReplaceWith(input_value);
832         block->RemoveInstruction(not_equal);
833         RecordSimplification();
834       } else {
835         // Replace (bool_value != integer_not_zero_nor_one_constant) with true
836         not_equal->ReplaceWith(GetGraph()->GetIntConstant(1));
837         block->RemoveInstruction(not_equal);
838         RecordSimplification();
839       }
840     } else {
841       VisitCondition(not_equal);
842     }
843   } else {
844     VisitCondition(not_equal);
845   }
846 }
847 
VisitBooleanNot(HBooleanNot * bool_not)848 void InstructionSimplifierVisitor::VisitBooleanNot(HBooleanNot* bool_not) {
849   HInstruction* input = bool_not->InputAt(0);
850   HInstruction* replace_with = nullptr;
851 
852   if (input->IsIntConstant()) {
853     // Replace !(true/false) with false/true.
854     if (input->AsIntConstant()->IsTrue()) {
855       replace_with = GetGraph()->GetIntConstant(0);
856     } else {
857       DCHECK(input->AsIntConstant()->IsFalse()) << input->AsIntConstant()->GetValue();
858       replace_with = GetGraph()->GetIntConstant(1);
859     }
860   } else if (input->IsBooleanNot()) {
861     // Replace (!(!bool_value)) with bool_value.
862     replace_with = input->InputAt(0);
863   } else if (input->IsCondition() &&
864              // Don't change FP compares. The definition of compares involving
865              // NaNs forces the compares to be done as written by the user.
866              !DataType::IsFloatingPointType(input->InputAt(0)->GetType())) {
867     // Replace condition with its opposite.
868     replace_with = GetGraph()->InsertOppositeCondition(input->AsCondition(), bool_not);
869   }
870 
871   if (replace_with != nullptr) {
872     bool_not->ReplaceWith(replace_with);
873     bool_not->GetBlock()->RemoveInstruction(bool_not);
874     RecordSimplification();
875   }
876 }
877 
878 // Constructs a new ABS(x) node in the HIR.
NewIntegralAbs(ArenaAllocator * allocator,HInstruction * x,HInstruction * cursor)879 static HInstruction* NewIntegralAbs(ArenaAllocator* allocator,
880                                     HInstruction* x,
881                                     HInstruction* cursor) {
882   DataType::Type type = DataType::Kind(x->GetType());
883   DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
884   HAbs* abs = new (allocator) HAbs(type, x, cursor->GetDexPc());
885   cursor->GetBlock()->InsertInstructionBefore(abs, cursor);
886   return abs;
887 }
888 
889 // Constructs a new MIN/MAX(x, y) node in the HIR.
NewIntegralMinMax(ArenaAllocator * allocator,HInstruction * x,HInstruction * y,HInstruction * cursor,bool is_min)890 static HInstruction* NewIntegralMinMax(ArenaAllocator* allocator,
891                                        HInstruction* x,
892                                        HInstruction* y,
893                                        HInstruction* cursor,
894                                        bool is_min) {
895   DataType::Type type = DataType::Kind(x->GetType());
896   DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
897   HBinaryOperation* minmax = nullptr;
898   if (is_min) {
899     minmax = new (allocator) HMin(type, x, y, cursor->GetDexPc());
900   } else {
901     minmax = new (allocator) HMax(type, x, y, cursor->GetDexPc());
902   }
903   cursor->GetBlock()->InsertInstructionBefore(minmax, cursor);
904   return minmax;
905 }
906 
907 // Returns true if operands a and b consists of widening type conversions
908 // (either explicit or implicit) to the given to_type.
AreLowerPrecisionArgs(DataType::Type to_type,HInstruction * a,HInstruction * b)909 static bool AreLowerPrecisionArgs(DataType::Type to_type, HInstruction* a, HInstruction* b) {
910   if (a->IsTypeConversion() && a->GetType() == to_type) {
911     a = a->InputAt(0);
912   }
913   if (b->IsTypeConversion() && b->GetType() == to_type) {
914     b = b->InputAt(0);
915   }
916   DataType::Type type1 = a->GetType();
917   DataType::Type type2 = b->GetType();
918   return (type1 == DataType::Type::kUint8  && type2 == DataType::Type::kUint8) ||
919          (type1 == DataType::Type::kInt8   && type2 == DataType::Type::kInt8) ||
920          (type1 == DataType::Type::kInt16  && type2 == DataType::Type::kInt16) ||
921          (type1 == DataType::Type::kUint16 && type2 == DataType::Type::kUint16) ||
922          (type1 == DataType::Type::kInt32  && type2 == DataType::Type::kInt32 &&
923           to_type == DataType::Type::kInt64);
924 }
925 
926 // Returns an acceptable substitution for "a" on the select
927 // construct "a <cmp> b ? c : .."  during MIN/MAX recognition.
AllowInMinMax(IfCondition cmp,HInstruction * a,HInstruction * b,HInstruction * c)928 static HInstruction* AllowInMinMax(IfCondition cmp,
929                                    HInstruction* a,
930                                    HInstruction* b,
931                                    HInstruction* c) {
932   int64_t value = 0;
933   if (IsInt64AndGet(b, /*out*/ &value) &&
934       (((cmp == kCondLT || cmp == kCondLE) && c->IsMax()) ||
935        ((cmp == kCondGT || cmp == kCondGE) && c->IsMin()))) {
936     HConstant* other = c->AsBinaryOperation()->GetConstantRight();
937     if (other != nullptr && a == c->AsBinaryOperation()->GetLeastConstantLeft()) {
938       int64_t other_value = Int64FromConstant(other);
939       bool is_max = (cmp == kCondLT || cmp == kCondLE);
940       // Allow the max for a <  100 ? max(a, -100) : ..
941       //    or the min for a > -100 ? min(a,  100) : ..
942       if (is_max ? (value >= other_value) : (value <= other_value)) {
943         return c;
944       }
945     }
946   }
947   return nullptr;
948 }
949 
950 // TODO This should really be done by LSE itself since there is significantly
951 // more information available there.
VisitPredicatedInstanceFieldGet(HPredicatedInstanceFieldGet * pred_get)952 void InstructionSimplifierVisitor::VisitPredicatedInstanceFieldGet(
953     HPredicatedInstanceFieldGet* pred_get) {
954   HInstruction* target = pred_get->GetTarget();
955   HInstruction* default_val = pred_get->GetDefaultValue();
956   if (target->IsNullConstant()) {
957     pred_get->ReplaceWith(default_val);
958     pred_get->GetBlock()->RemoveInstruction(pred_get);
959     RecordSimplification();
960     return;
961   } else if (!target->CanBeNull()) {
962     HInstruction* replace_with = new (GetGraph()->GetAllocator())
963         HInstanceFieldGet(pred_get->GetTarget(),
964                           pred_get->GetFieldInfo().GetField(),
965                           pred_get->GetFieldType(),
966                           pred_get->GetFieldOffset(),
967                           pred_get->IsVolatile(),
968                           pred_get->GetFieldInfo().GetFieldIndex(),
969                           pred_get->GetFieldInfo().GetDeclaringClassDefIndex(),
970                           pred_get->GetFieldInfo().GetDexFile(),
971                           pred_get->GetDexPc());
972     if (pred_get->GetType() == DataType::Type::kReference) {
973       replace_with->SetReferenceTypeInfoIfValid(pred_get->GetReferenceTypeInfo());
974     }
975     pred_get->GetBlock()->InsertInstructionBefore(replace_with, pred_get);
976     pred_get->ReplaceWith(replace_with);
977     pred_get->GetBlock()->RemoveInstruction(pred_get);
978     RecordSimplification();
979     return;
980   }
981   if (!target->IsPhi() || !default_val->IsPhi() || default_val->GetBlock() != target->GetBlock()) {
982     // The iget has already been reduced. We know the target or the phi
983     // selection will differ between the target and default.
984     return;
985   }
986   DCHECK_EQ(default_val->InputCount(), target->InputCount());
987   // In the same block both phis only one non-null we can remove the phi from default_val.
988   HInstruction* single_value = nullptr;
989   auto inputs = target->GetInputs();
990   for (auto [input, idx] : ZipCount(MakeIterationRange(inputs))) {
991     if (input->CanBeNull()) {
992       if (single_value == nullptr) {
993         single_value = default_val->InputAt(idx);
994       } else if (single_value != default_val->InputAt(idx) &&
995                  !single_value->Equals(default_val->InputAt(idx))) {
996         // Multiple values are associated with potential nulls, can't combine.
997         return;
998       }
999     }
1000   }
1001   DCHECK(single_value != nullptr) << "All target values are non-null but the phi as a whole still"
1002                                   << " can be null? This should not be possible." << std::endl
1003                                   << pred_get->DumpWithArgs();
1004   if (single_value->StrictlyDominates(pred_get)) {
1005     // Combine all the maybe null values into one.
1006     pred_get->ReplaceInput(single_value, 0);
1007     RecordSimplification();
1008   }
1009 }
1010 
VisitSelect(HSelect * select)1011 void InstructionSimplifierVisitor::VisitSelect(HSelect* select) {
1012   HInstruction* replace_with = nullptr;
1013   HInstruction* condition = select->GetCondition();
1014   HInstruction* true_value = select->GetTrueValue();
1015   HInstruction* false_value = select->GetFalseValue();
1016 
1017   if (condition->IsBooleanNot()) {
1018     // Change ((!cond) ? x : y) to (cond ? y : x).
1019     condition = condition->InputAt(0);
1020     std::swap(true_value, false_value);
1021     select->ReplaceInput(false_value, 0);
1022     select->ReplaceInput(true_value, 1);
1023     select->ReplaceInput(condition, 2);
1024     RecordSimplification();
1025   }
1026 
1027   if (true_value == false_value) {
1028     // Replace (cond ? x : x) with (x).
1029     replace_with = true_value;
1030   } else if (condition->IsIntConstant()) {
1031     if (condition->AsIntConstant()->IsTrue()) {
1032       // Replace (true ? x : y) with (x).
1033       replace_with = true_value;
1034     } else {
1035       // Replace (false ? x : y) with (y).
1036       DCHECK(condition->AsIntConstant()->IsFalse()) << condition->AsIntConstant()->GetValue();
1037       replace_with = false_value;
1038     }
1039   } else if (true_value->IsIntConstant() && false_value->IsIntConstant()) {
1040     if (true_value->AsIntConstant()->IsTrue() && false_value->AsIntConstant()->IsFalse()) {
1041       // Replace (cond ? true : false) with (cond).
1042       replace_with = condition;
1043     } else if (true_value->AsIntConstant()->IsFalse() && false_value->AsIntConstant()->IsTrue()) {
1044       // Replace (cond ? false : true) with (!cond).
1045       replace_with = GetGraph()->InsertOppositeCondition(condition, select);
1046     }
1047   } else if (condition->IsCondition()) {
1048     IfCondition cmp = condition->AsCondition()->GetCondition();
1049     HInstruction* a = condition->InputAt(0);
1050     HInstruction* b = condition->InputAt(1);
1051     DataType::Type t_type = true_value->GetType();
1052     DataType::Type f_type = false_value->GetType();
1053     // Here we have a <cmp> b ? true_value : false_value.
1054     // Test if both values are compatible integral types (resulting MIN/MAX/ABS
1055     // type will be int or long, like the condition). Replacements are general,
1056     // but assume conditions prefer constants on the right.
1057     if (DataType::IsIntegralType(t_type) && DataType::Kind(t_type) == DataType::Kind(f_type)) {
1058       // Allow a <  100 ? max(a, -100) : ..
1059       //    or a > -100 ? min(a,  100) : ..
1060       // to use min/max instead of a to detect nested min/max expressions.
1061       HInstruction* new_a = AllowInMinMax(cmp, a, b, true_value);
1062       if (new_a != nullptr) {
1063         a = new_a;
1064       }
1065       // Try to replace typical integral MIN/MAX/ABS constructs.
1066       if ((cmp == kCondLT || cmp == kCondLE || cmp == kCondGT || cmp == kCondGE) &&
1067           ((a == true_value && b == false_value) ||
1068            (b == true_value && a == false_value))) {
1069         // Found a < b ? a : b (MIN) or a < b ? b : a (MAX)
1070         //    or a > b ? a : b (MAX) or a > b ? b : a (MIN).
1071         bool is_min = (cmp == kCondLT || cmp == kCondLE) == (a == true_value);
1072         replace_with = NewIntegralMinMax(GetGraph()->GetAllocator(), a, b, select, is_min);
1073       } else if (((cmp == kCondLT || cmp == kCondLE) && true_value->IsNeg()) ||
1074                  ((cmp == kCondGT || cmp == kCondGE) && false_value->IsNeg())) {
1075         bool negLeft = (cmp == kCondLT || cmp == kCondLE);
1076         HInstruction* the_negated = negLeft ? true_value->InputAt(0) : false_value->InputAt(0);
1077         HInstruction* not_negated = negLeft ? false_value : true_value;
1078         if (a == the_negated && a == not_negated && IsInt64Value(b, 0)) {
1079           // Found a < 0 ? -a :  a
1080           //    or a > 0 ?  a : -a
1081           // which can be replaced by ABS(a).
1082           replace_with = NewIntegralAbs(GetGraph()->GetAllocator(), a, select);
1083         }
1084       } else if (true_value->IsSub() && false_value->IsSub()) {
1085         HInstruction* true_sub1 = true_value->InputAt(0);
1086         HInstruction* true_sub2 = true_value->InputAt(1);
1087         HInstruction* false_sub1 = false_value->InputAt(0);
1088         HInstruction* false_sub2 = false_value->InputAt(1);
1089         if ((((cmp == kCondGT || cmp == kCondGE) &&
1090               (a == true_sub1 && b == true_sub2 && a == false_sub2 && b == false_sub1)) ||
1091              ((cmp == kCondLT || cmp == kCondLE) &&
1092               (a == true_sub2 && b == true_sub1 && a == false_sub1 && b == false_sub2))) &&
1093             AreLowerPrecisionArgs(t_type, a, b)) {
1094           // Found a > b ? a - b  : b - a
1095           //    or a < b ? b - a  : a - b
1096           // which can be replaced by ABS(a - b) for lower precision operands a, b.
1097           replace_with = NewIntegralAbs(GetGraph()->GetAllocator(), true_value, select);
1098         }
1099       }
1100     }
1101   }
1102 
1103   if (replace_with != nullptr) {
1104     select->ReplaceWith(replace_with);
1105     select->GetBlock()->RemoveInstruction(select);
1106     RecordSimplification();
1107   }
1108 }
1109 
VisitIf(HIf * instruction)1110 void InstructionSimplifierVisitor::VisitIf(HIf* instruction) {
1111   HInstruction* condition = instruction->InputAt(0);
1112   if (condition->IsBooleanNot()) {
1113     // Swap successors if input is negated.
1114     instruction->ReplaceInput(condition->InputAt(0), 0);
1115     instruction->GetBlock()->SwapSuccessors();
1116     RecordSimplification();
1117   }
1118 }
1119 
1120 // TODO(solanes): This optimization should be in ConstantFolding since we are folding to a constant.
1121 // However, we get code size regressions when we do that since we sometimes have a NullCheck between
1122 // HArrayLength and IsNewArray, and said NullCheck is eliminated in InstructionSimplifier. If we run
1123 // ConstantFolding and InstructionSimplifier in lockstep this wouldn't be an issue.
VisitArrayLength(HArrayLength * instruction)1124 void InstructionSimplifierVisitor::VisitArrayLength(HArrayLength* instruction) {
1125   HInstruction* input = instruction->InputAt(0);
1126   // If the array is a NewArray with constant size, replace the array length
1127   // with the constant instruction. This helps the bounds check elimination phase.
1128   if (input->IsNewArray()) {
1129     input = input->AsNewArray()->GetLength();
1130     if (input->IsIntConstant()) {
1131       instruction->ReplaceWith(input);
1132     }
1133   }
1134 }
1135 
VisitArraySet(HArraySet * instruction)1136 void InstructionSimplifierVisitor::VisitArraySet(HArraySet* instruction) {
1137   HInstruction* value = instruction->GetValue();
1138   if (value->GetType() != DataType::Type::kReference) {
1139     return;
1140   }
1141 
1142   if (CanEnsureNotNullAt(value, instruction)) {
1143     instruction->ClearValueCanBeNull();
1144   }
1145 
1146   if (value->IsArrayGet()) {
1147     if (value->AsArrayGet()->GetArray() == instruction->GetArray()) {
1148       // If the code is just swapping elements in the array, no need for a type check.
1149       instruction->ClearTypeCheck();
1150       return;
1151     }
1152   }
1153 
1154   if (value->IsNullConstant()) {
1155     instruction->ClearTypeCheck();
1156     return;
1157   }
1158 
1159   ScopedObjectAccess soa(Thread::Current());
1160   ReferenceTypeInfo array_rti = instruction->GetArray()->GetReferenceTypeInfo();
1161   ReferenceTypeInfo value_rti = value->GetReferenceTypeInfo();
1162   if (!array_rti.IsValid()) {
1163     return;
1164   }
1165 
1166   if (value_rti.IsValid() && array_rti.CanArrayHold(value_rti)) {
1167     instruction->ClearTypeCheck();
1168     return;
1169   }
1170 
1171   if (array_rti.IsObjectArray()) {
1172     if (array_rti.IsExact()) {
1173       instruction->ClearTypeCheck();
1174       return;
1175     }
1176     instruction->SetStaticTypeOfArrayIsObjectArray();
1177   }
1178 }
1179 
IsTypeConversionLossless(DataType::Type input_type,DataType::Type result_type)1180 static bool IsTypeConversionLossless(DataType::Type input_type, DataType::Type result_type) {
1181   // Make sure all implicit conversions have been simplified and no new ones have been introduced.
1182   DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
1183       << input_type << "," << result_type;
1184   // The conversion to a larger type is loss-less with the exception of two cases,
1185   //   - conversion to the unsigned type Uint16, where we may lose some bits, and
1186   //   - conversion from float to long, the only FP to integral conversion with smaller FP type.
1187   // For integral to FP conversions this holds because the FP mantissa is large enough.
1188   // Note: The size check excludes Uint8 as the result type.
1189   return DataType::Size(result_type) > DataType::Size(input_type) &&
1190       result_type != DataType::Type::kUint16 &&
1191       !(result_type == DataType::Type::kInt64 && input_type == DataType::Type::kFloat32);
1192 }
1193 
CanRemoveRedundantAnd(HConstant * and_right,HConstant * shr_right,DataType::Type result_type)1194 static bool CanRemoveRedundantAnd(HConstant* and_right,
1195                                   HConstant* shr_right,
1196                                   DataType::Type result_type) {
1197   int64_t and_cst = Int64FromConstant(and_right);
1198   int64_t shr_cst = Int64FromConstant(shr_right);
1199 
1200   // In the following sequence A is the input value, D is the result:
1201   // B := A & x
1202   // C := B >> r
1203   // D := TypeConv(n-bit type) C
1204 
1205   // The value of D is entirely dependent on the bits [n-1:0] of C, which in turn are dependent
1206   // on bits [r+n-1:r] of B.
1207   // Therefore, if the AND does not change bits [r+n-1:r] of A then it will not affect D.
1208   // This can be checked by ensuring that bits [r+n-1:r] of the AND Constant are 1.
1209 
1210   // For example: return (byte) ((value & 0xff00) >> 8)
1211   //              return (byte) ((value & 0xff000000) >> 31)
1212 
1213   // The mask sets bits [r+n-1:r] to 1, and all others to 0.
1214   int64_t mask = DataType::MaxValueOfIntegralType(DataType::ToUnsigned(result_type)) << shr_cst;
1215 
1216   // If the result of a bitwise AND between the mask and the AND constant is the original mask, then
1217   // the AND does not change bits [r+n-1:r], meaning that it is redundant and can be removed.
1218   return ((and_cst & mask) == mask);
1219 }
1220 
TryReplaceFieldOrArrayGetType(HInstruction * maybe_get,DataType::Type new_type)1221 static inline bool TryReplaceFieldOrArrayGetType(HInstruction* maybe_get, DataType::Type new_type) {
1222   if (maybe_get->IsInstanceFieldGet()) {
1223     maybe_get->AsInstanceFieldGet()->SetType(new_type);
1224     return true;
1225   } else if (maybe_get->IsPredicatedInstanceFieldGet()) {
1226     maybe_get->AsPredicatedInstanceFieldGet()->SetType(new_type);
1227     return true;
1228   } else if (maybe_get->IsStaticFieldGet()) {
1229     maybe_get->AsStaticFieldGet()->SetType(new_type);
1230     return true;
1231   } else if (maybe_get->IsArrayGet() && !maybe_get->AsArrayGet()->IsStringCharAt()) {
1232     maybe_get->AsArrayGet()->SetType(new_type);
1233     return true;
1234   } else {
1235     return false;
1236   }
1237 }
1238 
1239 // The type conversion is only used for storing into a field/element of the
1240 // same/narrower size.
IsTypeConversionForStoringIntoNoWiderFieldOnly(HTypeConversion * type_conversion)1241 static bool IsTypeConversionForStoringIntoNoWiderFieldOnly(HTypeConversion* type_conversion) {
1242   if (type_conversion->HasEnvironmentUses()) {
1243     return false;
1244   }
1245   DataType::Type input_type = type_conversion->GetInputType();
1246   DataType::Type result_type = type_conversion->GetResultType();
1247   if (!DataType::IsIntegralType(input_type) ||
1248       !DataType::IsIntegralType(result_type) ||
1249       input_type == DataType::Type::kInt64 ||
1250       result_type == DataType::Type::kInt64) {
1251     // Type conversion is needed if non-integer types are involved, or 64-bit
1252     // types are involved, which may use different number of registers.
1253     return false;
1254   }
1255   if (DataType::Size(input_type) >= DataType::Size(result_type)) {
1256     // Type conversion is not necessary when storing to a field/element of the
1257     // same/smaller size.
1258   } else {
1259     // We do not handle this case here.
1260     return false;
1261   }
1262 
1263   // Check if the converted value is only used for storing into heap.
1264   for (const HUseListNode<HInstruction*>& use : type_conversion->GetUses()) {
1265     HInstruction* instruction = use.GetUser();
1266     if (instruction->IsInstanceFieldSet() &&
1267         instruction->AsInstanceFieldSet()->GetFieldType() == result_type) {
1268       DCHECK_EQ(instruction->AsInstanceFieldSet()->GetValue(), type_conversion);
1269       continue;
1270     }
1271     if (instruction->IsStaticFieldSet() &&
1272         instruction->AsStaticFieldSet()->GetFieldType() == result_type) {
1273       DCHECK_EQ(instruction->AsStaticFieldSet()->GetValue(), type_conversion);
1274       continue;
1275     }
1276     if (instruction->IsArraySet() &&
1277         instruction->AsArraySet()->GetComponentType() == result_type &&
1278         // not index use.
1279         instruction->AsArraySet()->GetIndex() != type_conversion) {
1280       DCHECK_EQ(instruction->AsArraySet()->GetValue(), type_conversion);
1281       continue;
1282     }
1283     // The use is not as a store value, or the field/element type is not the
1284     // same as the result_type, keep the type conversion.
1285     return false;
1286   }
1287   // Codegen automatically handles the type conversion during the store.
1288   return true;
1289 }
1290 
VisitTypeConversion(HTypeConversion * instruction)1291 void InstructionSimplifierVisitor::VisitTypeConversion(HTypeConversion* instruction) {
1292   HInstruction* input = instruction->GetInput();
1293   DataType::Type input_type = input->GetType();
1294   DataType::Type result_type = instruction->GetResultType();
1295   if (instruction->IsImplicitConversion()) {
1296     instruction->ReplaceWith(input);
1297     instruction->GetBlock()->RemoveInstruction(instruction);
1298     RecordSimplification();
1299     return;
1300   }
1301 
1302   if (input->IsTypeConversion()) {
1303     HTypeConversion* input_conversion = input->AsTypeConversion();
1304     HInstruction* original_input = input_conversion->GetInput();
1305     DataType::Type original_type = original_input->GetType();
1306 
1307     // When the first conversion is lossless, a direct conversion from the original type
1308     // to the final type yields the same result, even for a lossy second conversion, for
1309     // example float->double->int or int->double->float.
1310     bool is_first_conversion_lossless = IsTypeConversionLossless(original_type, input_type);
1311 
1312     // For integral conversions, see if the first conversion loses only bits that the second
1313     // doesn't need, i.e. the final type is no wider than the intermediate. If so, direct
1314     // conversion yields the same result, for example long->int->short or int->char->short.
1315     bool integral_conversions_with_non_widening_second =
1316         DataType::IsIntegralType(input_type) &&
1317         DataType::IsIntegralType(original_type) &&
1318         DataType::IsIntegralType(result_type) &&
1319         DataType::Size(result_type) <= DataType::Size(input_type);
1320 
1321     if (is_first_conversion_lossless || integral_conversions_with_non_widening_second) {
1322       // If the merged conversion is implicit, do the simplification unconditionally.
1323       if (DataType::IsTypeConversionImplicit(original_type, result_type)) {
1324         instruction->ReplaceWith(original_input);
1325         instruction->GetBlock()->RemoveInstruction(instruction);
1326         if (!input_conversion->HasUses()) {
1327           // Don't wait for DCE.
1328           input_conversion->GetBlock()->RemoveInstruction(input_conversion);
1329         }
1330         RecordSimplification();
1331         return;
1332       }
1333       // Otherwise simplify only if the first conversion has no other use.
1334       if (input_conversion->HasOnlyOneNonEnvironmentUse()) {
1335         input_conversion->ReplaceWith(original_input);
1336         input_conversion->GetBlock()->RemoveInstruction(input_conversion);
1337         RecordSimplification();
1338         return;
1339       }
1340     }
1341   } else if (input->IsShr() && DataType::IsIntegralType(result_type) &&
1342             // Optimization only applies to lossy Type Conversions.
1343             !IsTypeConversionLossless(input_type, result_type)) {
1344     DCHECK(DataType::IsIntegralType(input_type));
1345     HShr* shr_op = input->AsShr();
1346     HConstant* shr_right = shr_op->GetConstantRight();
1347     HInstruction* shr_left = shr_op->GetLeastConstantLeft();
1348     if (shr_right != nullptr && shr_left->IsAnd()) {
1349       // Optimization needs AND -> SHR -> TypeConversion pattern.
1350       HAnd* and_op = shr_left->AsAnd();
1351       HConstant* and_right = and_op->GetConstantRight();
1352       HInstruction* and_left = and_op->GetLeastConstantLeft();
1353       if (and_right != nullptr &&
1354           !DataType::IsUnsignedType(and_left->GetType()) &&
1355           !DataType::IsUnsignedType(result_type) &&
1356           !DataType::IsUnsignedType(and_right->GetType()) &&
1357           (DataType::Size(and_left->GetType()) < 8) &&
1358           (DataType::Size(result_type) == 1)) {
1359         // TODO: Support Unsigned Types.
1360         // TODO: Support Long Types.
1361         // TODO: Support result types other than byte.
1362         if (and_op->HasOnlyOneNonEnvironmentUse() &&
1363             CanRemoveRedundantAnd(and_right, shr_right, result_type)) {
1364           and_op->ReplaceWith(and_left);
1365           and_op->GetBlock()->RemoveInstruction(and_op);
1366           RecordSimplification();
1367           return;
1368         }
1369       }
1370     }
1371   } else if (input->IsAnd() && DataType::IsIntegralType(result_type)) {
1372     DCHECK(DataType::IsIntegralType(input_type));
1373     HAnd* input_and = input->AsAnd();
1374     HConstant* constant = input_and->GetConstantRight();
1375     if (constant != nullptr) {
1376       int64_t value = Int64FromConstant(constant);
1377       DCHECK_NE(value, -1);  // "& -1" would have been optimized away in VisitAnd().
1378       size_t trailing_ones = CTZ(~static_cast<uint64_t>(value));
1379       if (trailing_ones >= kBitsPerByte * DataType::Size(result_type)) {
1380         // The `HAnd` is useless, for example in `(byte) (x & 0xff)`, get rid of it.
1381         HInstruction* original_input = input_and->GetLeastConstantLeft();
1382         if (DataType::IsTypeConversionImplicit(original_input->GetType(), result_type)) {
1383           instruction->ReplaceWith(original_input);
1384           instruction->GetBlock()->RemoveInstruction(instruction);
1385           RecordSimplification();
1386           return;
1387         } else if (input->HasOnlyOneNonEnvironmentUse()) {
1388           input_and->ReplaceWith(original_input);
1389           input_and->GetBlock()->RemoveInstruction(input_and);
1390           RecordSimplification();
1391           return;
1392         }
1393       }
1394     }
1395   } else if (input->HasOnlyOneNonEnvironmentUse() &&
1396              ((input_type == DataType::Type::kInt8 && result_type == DataType::Type::kUint8) ||
1397               (input_type == DataType::Type::kUint8 && result_type == DataType::Type::kInt8) ||
1398               (input_type == DataType::Type::kInt16 && result_type == DataType::Type::kUint16) ||
1399               (input_type == DataType::Type::kUint16 && result_type == DataType::Type::kInt16))) {
1400     // Try to modify the type of the load to `result_type` and remove the explicit type conversion.
1401     if (TryReplaceFieldOrArrayGetType(input, result_type)) {
1402       instruction->ReplaceWith(input);
1403       instruction->GetBlock()->RemoveInstruction(instruction);
1404       RecordSimplification();
1405       return;
1406     }
1407   }
1408 
1409   if (IsTypeConversionForStoringIntoNoWiderFieldOnly(instruction)) {
1410     instruction->ReplaceWith(input);
1411     instruction->GetBlock()->RemoveInstruction(instruction);
1412     RecordSimplification();
1413     return;
1414   }
1415 }
1416 
VisitAbs(HAbs * instruction)1417 void InstructionSimplifierVisitor::VisitAbs(HAbs* instruction) {
1418   HInstruction* input = instruction->GetInput();
1419   if (DataType::IsZeroExtension(input->GetType(), instruction->GetResultType())) {
1420     // Zero extension from narrow to wide can never set sign bit in the wider
1421     // operand, making the subsequent Abs redundant (e.g., abs(b & 0xff) for byte b).
1422     instruction->ReplaceWith(input);
1423     instruction->GetBlock()->RemoveInstruction(instruction);
1424     RecordSimplification();
1425   }
1426 }
1427 
VisitAdd(HAdd * instruction)1428 void InstructionSimplifierVisitor::VisitAdd(HAdd* instruction) {
1429   HConstant* input_cst = instruction->GetConstantRight();
1430   HInstruction* input_other = instruction->GetLeastConstantLeft();
1431   bool integral_type = DataType::IsIntegralType(instruction->GetType());
1432   if ((input_cst != nullptr) && input_cst->IsArithmeticZero()) {
1433     // Replace code looking like
1434     //    ADD dst, src, 0
1435     // with
1436     //    src
1437     // Note that we cannot optimize `x + 0.0` to `x` for floating-point. When
1438     // `x` is `-0.0`, the former expression yields `0.0`, while the later
1439     // yields `-0.0`.
1440     if (integral_type) {
1441       instruction->ReplaceWith(input_other);
1442       instruction->GetBlock()->RemoveInstruction(instruction);
1443       RecordSimplification();
1444       return;
1445     }
1446   }
1447 
1448   HInstruction* left = instruction->GetLeft();
1449   HInstruction* right = instruction->GetRight();
1450   bool left_is_neg = left->IsNeg();
1451   bool right_is_neg = right->IsNeg();
1452 
1453   if (left_is_neg && right_is_neg) {
1454     if (TryMoveNegOnInputsAfterBinop(instruction)) {
1455       return;
1456     }
1457   }
1458 
1459   HNeg* neg = left_is_neg ? left->AsNeg() : right->AsNeg();
1460   if (left_is_neg != right_is_neg && neg->HasOnlyOneNonEnvironmentUse()) {
1461     // Replace code looking like
1462     //    NEG tmp, b
1463     //    ADD dst, a, tmp
1464     // with
1465     //    SUB dst, a, b
1466     // We do not perform the optimization if the input negation has environment
1467     // uses or multiple non-environment uses as it could lead to worse code. In
1468     // particular, we do not want the live range of `b` to be extended if we are
1469     // not sure the initial 'NEG' instruction can be removed.
1470     HInstruction* other = left_is_neg ? right : left;
1471     HSub* sub =
1472         new(GetGraph()->GetAllocator()) HSub(instruction->GetType(), other, neg->GetInput());
1473     instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, sub);
1474     RecordSimplification();
1475     neg->GetBlock()->RemoveInstruction(neg);
1476     return;
1477   }
1478 
1479   if (TryReplaceWithRotate(instruction)) {
1480     return;
1481   }
1482 
1483   // TryHandleAssociativeAndCommutativeOperation() does not remove its input,
1484   // so no need to return.
1485   TryHandleAssociativeAndCommutativeOperation(instruction);
1486 
1487   if ((left->IsSub() || right->IsSub()) &&
1488       TrySubtractionChainSimplification(instruction)) {
1489     return;
1490   }
1491 
1492   if (integral_type) {
1493     // Replace code patterns looking like
1494     //    SUB dst1, x, y        SUB dst1, x, y
1495     //    ADD dst2, dst1, y     ADD dst2, y, dst1
1496     // with
1497     //    SUB dst1, x, y
1498     // ADD instruction is not needed in this case, we may use
1499     // one of inputs of SUB instead.
1500     if (left->IsSub() && left->InputAt(1) == right) {
1501       instruction->ReplaceWith(left->InputAt(0));
1502       RecordSimplification();
1503       instruction->GetBlock()->RemoveInstruction(instruction);
1504       return;
1505     } else if (right->IsSub() && right->InputAt(1) == left) {
1506       instruction->ReplaceWith(right->InputAt(0));
1507       RecordSimplification();
1508       instruction->GetBlock()->RemoveInstruction(instruction);
1509       return;
1510     }
1511   }
1512 }
1513 
VisitAnd(HAnd * instruction)1514 void InstructionSimplifierVisitor::VisitAnd(HAnd* instruction) {
1515   DCHECK(DataType::IsIntegralType(instruction->GetType()));
1516   HConstant* input_cst = instruction->GetConstantRight();
1517   HInstruction* input_other = instruction->GetLeastConstantLeft();
1518 
1519   if (input_cst != nullptr) {
1520     int64_t value = Int64FromConstant(input_cst);
1521     if (value == -1 ||
1522         // Similar cases under zero extension.
1523         (DataType::IsUnsignedType(input_other->GetType()) &&
1524          ((DataType::MaxValueOfIntegralType(input_other->GetType()) & ~value) == 0))) {
1525       // Replace code looking like
1526       //    AND dst, src, 0xFFF...FF
1527       // with
1528       //    src
1529       instruction->ReplaceWith(input_other);
1530       instruction->GetBlock()->RemoveInstruction(instruction);
1531       RecordSimplification();
1532       return;
1533     }
1534     if (input_other->IsTypeConversion() &&
1535         input_other->GetType() == DataType::Type::kInt64 &&
1536         DataType::IsIntegralType(input_other->InputAt(0)->GetType()) &&
1537         IsInt<32>(value) &&
1538         input_other->HasOnlyOneNonEnvironmentUse()) {
1539       // The AND can be reordered before the TypeConversion. Replace
1540       //   LongConstant cst, <32-bit-constant-sign-extended-to-64-bits>
1541       //   TypeConversion<Int64> tmp, src
1542       //   AND dst, tmp, cst
1543       // with
1544       //   IntConstant cst, <32-bit-constant>
1545       //   AND tmp, src, cst
1546       //   TypeConversion<Int64> dst, tmp
1547       // This helps 32-bit targets and does not hurt 64-bit targets.
1548       // This also simplifies detection of other patterns, such as Uint8 loads.
1549       HInstruction* new_and_input = input_other->InputAt(0);
1550       // Implicit conversion Int64->Int64 would have been removed previously.
1551       DCHECK_NE(new_and_input->GetType(), DataType::Type::kInt64);
1552       HConstant* new_const = GetGraph()->GetConstant(DataType::Type::kInt32, value);
1553       HAnd* new_and =
1554           new (GetGraph()->GetAllocator()) HAnd(DataType::Type::kInt32, new_and_input, new_const);
1555       instruction->GetBlock()->InsertInstructionBefore(new_and, instruction);
1556       HTypeConversion* new_conversion =
1557           new (GetGraph()->GetAllocator()) HTypeConversion(DataType::Type::kInt64, new_and);
1558       instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, new_conversion);
1559       input_other->GetBlock()->RemoveInstruction(input_other);
1560       RecordSimplification();
1561       // Try to process the new And now, do not wait for the next round of simplifications.
1562       instruction = new_and;
1563       input_other = new_and_input;
1564     }
1565     // Eliminate And from UShr+And if the And-mask contains all the bits that
1566     // can be non-zero after UShr. Transform Shr+And to UShr if the And-mask
1567     // precisely clears the shifted-in sign bits.
1568     if ((input_other->IsUShr() || input_other->IsShr()) && input_other->InputAt(1)->IsConstant()) {
1569       size_t reg_bits = (instruction->GetResultType() == DataType::Type::kInt64) ? 64 : 32;
1570       size_t shift = Int64FromConstant(input_other->InputAt(1)->AsConstant()) & (reg_bits - 1);
1571       size_t num_tail_bits_set = CTZ(value + 1);
1572       if ((num_tail_bits_set >= reg_bits - shift) && input_other->IsUShr()) {
1573         // This AND clears only bits known to be clear, for example "(x >>> 24) & 0xff".
1574         instruction->ReplaceWith(input_other);
1575         instruction->GetBlock()->RemoveInstruction(instruction);
1576         RecordSimplification();
1577         return;
1578       }  else if ((num_tail_bits_set == reg_bits - shift) && IsPowerOfTwo(value + 1) &&
1579           input_other->HasOnlyOneNonEnvironmentUse()) {
1580         DCHECK(input_other->IsShr());  // For UShr, we would have taken the branch above.
1581         // Replace SHR+AND with USHR, for example "(x >> 24) & 0xff" -> "x >>> 24".
1582         HUShr* ushr = new (GetGraph()->GetAllocator()) HUShr(instruction->GetType(),
1583                                                              input_other->InputAt(0),
1584                                                              input_other->InputAt(1),
1585                                                              input_other->GetDexPc());
1586         instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, ushr);
1587         input_other->GetBlock()->RemoveInstruction(input_other);
1588         RecordSimplification();
1589         return;
1590       }
1591     }
1592     if ((value == 0xff || value == 0xffff) && instruction->GetType() != DataType::Type::kInt64) {
1593       // Transform AND to a type conversion to Uint8/Uint16. If `input_other` is a field
1594       // or array Get with only a single use, short-circuit the subsequent simplification
1595       // of the Get+TypeConversion and change the Get's type to `new_type` instead.
1596       DataType::Type new_type = (value == 0xff) ? DataType::Type::kUint8 : DataType::Type::kUint16;
1597       DataType::Type find_type = (value == 0xff) ? DataType::Type::kInt8 : DataType::Type::kInt16;
1598       if (input_other->GetType() == find_type &&
1599           input_other->HasOnlyOneNonEnvironmentUse() &&
1600           TryReplaceFieldOrArrayGetType(input_other, new_type)) {
1601         instruction->ReplaceWith(input_other);
1602         instruction->GetBlock()->RemoveInstruction(instruction);
1603       } else if (DataType::IsTypeConversionImplicit(input_other->GetType(), new_type)) {
1604         instruction->ReplaceWith(input_other);
1605         instruction->GetBlock()->RemoveInstruction(instruction);
1606       } else {
1607         HTypeConversion* type_conversion = new (GetGraph()->GetAllocator()) HTypeConversion(
1608             new_type, input_other, instruction->GetDexPc());
1609         instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, type_conversion);
1610       }
1611       RecordSimplification();
1612       return;
1613     }
1614   }
1615 
1616   // We assume that GVN has run before, so we only perform a pointer comparison.
1617   // If for some reason the values are equal but the pointers are different, we
1618   // are still correct and only miss an optimization opportunity.
1619   if (instruction->GetLeft() == instruction->GetRight()) {
1620     // Replace code looking like
1621     //    AND dst, src, src
1622     // with
1623     //    src
1624     instruction->ReplaceWith(instruction->GetLeft());
1625     instruction->GetBlock()->RemoveInstruction(instruction);
1626     RecordSimplification();
1627     return;
1628   }
1629 
1630   if (TryDeMorganNegationFactoring(instruction)) {
1631     return;
1632   }
1633 
1634   // TryHandleAssociativeAndCommutativeOperation() does not remove its input,
1635   // so no need to return.
1636   TryHandleAssociativeAndCommutativeOperation(instruction);
1637 }
1638 
VisitGreaterThan(HGreaterThan * condition)1639 void InstructionSimplifierVisitor::VisitGreaterThan(HGreaterThan* condition) {
1640   VisitCondition(condition);
1641 }
1642 
VisitGreaterThanOrEqual(HGreaterThanOrEqual * condition)1643 void InstructionSimplifierVisitor::VisitGreaterThanOrEqual(HGreaterThanOrEqual* condition) {
1644   VisitCondition(condition);
1645 }
1646 
VisitLessThan(HLessThan * condition)1647 void InstructionSimplifierVisitor::VisitLessThan(HLessThan* condition) {
1648   VisitCondition(condition);
1649 }
1650 
VisitLessThanOrEqual(HLessThanOrEqual * condition)1651 void InstructionSimplifierVisitor::VisitLessThanOrEqual(HLessThanOrEqual* condition) {
1652   VisitCondition(condition);
1653 }
1654 
VisitBelow(HBelow * condition)1655 void InstructionSimplifierVisitor::VisitBelow(HBelow* condition) {
1656   VisitCondition(condition);
1657 }
1658 
VisitBelowOrEqual(HBelowOrEqual * condition)1659 void InstructionSimplifierVisitor::VisitBelowOrEqual(HBelowOrEqual* condition) {
1660   VisitCondition(condition);
1661 }
1662 
VisitAbove(HAbove * condition)1663 void InstructionSimplifierVisitor::VisitAbove(HAbove* condition) {
1664   VisitCondition(condition);
1665 }
1666 
VisitAboveOrEqual(HAboveOrEqual * condition)1667 void InstructionSimplifierVisitor::VisitAboveOrEqual(HAboveOrEqual* condition) {
1668   VisitCondition(condition);
1669 }
1670 
1671 // Recognize the following pattern:
1672 // obj.getClass() ==/!= Foo.class
1673 // And replace it with a constant value if the type of `obj` is statically known.
RecognizeAndSimplifyClassCheck(HCondition * condition)1674 static bool RecognizeAndSimplifyClassCheck(HCondition* condition) {
1675   HInstruction* input_one = condition->InputAt(0);
1676   HInstruction* input_two = condition->InputAt(1);
1677   HLoadClass* load_class = input_one->IsLoadClass()
1678       ? input_one->AsLoadClass()
1679       : input_two->AsLoadClass();
1680   if (load_class == nullptr) {
1681     return false;
1682   }
1683 
1684   ReferenceTypeInfo class_rti = load_class->GetLoadedClassRTI();
1685   if (!class_rti.IsValid()) {
1686     // Unresolved class.
1687     return false;
1688   }
1689 
1690   HInstanceFieldGet* field_get = (load_class == input_one)
1691       ? input_two->AsInstanceFieldGet()
1692       : input_one->AsInstanceFieldGet();
1693   if (field_get == nullptr) {
1694     return false;
1695   }
1696 
1697   HInstruction* receiver = field_get->InputAt(0);
1698   ReferenceTypeInfo receiver_type = receiver->GetReferenceTypeInfo();
1699   if (!receiver_type.IsExact()) {
1700     return false;
1701   }
1702 
1703   {
1704     ScopedObjectAccess soa(Thread::Current());
1705     ArtField* field = GetClassRoot<mirror::Object>()->GetInstanceField(0);
1706     DCHECK_EQ(std::string(field->GetName()), "shadow$_klass_");
1707     if (field_get->GetFieldInfo().GetField() != field) {
1708       return false;
1709     }
1710 
1711     // We can replace the compare.
1712     int value = 0;
1713     if (receiver_type.IsEqual(class_rti)) {
1714       value = condition->IsEqual() ? 1 : 0;
1715     } else {
1716       value = condition->IsNotEqual() ? 1 : 0;
1717     }
1718     condition->ReplaceWith(condition->GetBlock()->GetGraph()->GetIntConstant(value));
1719     return true;
1720   }
1721 }
1722 
VisitCondition(HCondition * condition)1723 void InstructionSimplifierVisitor::VisitCondition(HCondition* condition) {
1724   if (condition->IsEqual() || condition->IsNotEqual()) {
1725     if (RecognizeAndSimplifyClassCheck(condition)) {
1726       return;
1727     }
1728   }
1729 
1730   // Reverse condition if left is constant. Our code generators prefer constant
1731   // on the right hand side.
1732   if (condition->GetLeft()->IsConstant() && !condition->GetRight()->IsConstant()) {
1733     HBasicBlock* block = condition->GetBlock();
1734     HCondition* replacement =
1735         GetOppositeConditionSwapOps(block->GetGraph()->GetAllocator(), condition);
1736     // If it is a fp we must set the opposite bias.
1737     if (replacement != nullptr) {
1738       if (condition->IsLtBias()) {
1739         replacement->SetBias(ComparisonBias::kGtBias);
1740       } else if (condition->IsGtBias()) {
1741         replacement->SetBias(ComparisonBias::kLtBias);
1742       }
1743       block->ReplaceAndRemoveInstructionWith(condition, replacement);
1744       RecordSimplification();
1745 
1746       condition = replacement;
1747     }
1748   }
1749 
1750   HInstruction* left = condition->GetLeft();
1751   HInstruction* right = condition->GetRight();
1752 
1753   // Try to fold an HCompare into this HCondition.
1754 
1755   // We can only replace an HCondition which compares a Compare to 0.
1756   // Both 'dx' and 'jack' generate a compare to 0 when compiling a
1757   // condition with a long, float or double comparison as input.
1758   if (!left->IsCompare() || !right->IsConstant() || right->AsIntConstant()->GetValue() != 0) {
1759     // Conversion is not possible.
1760     return;
1761   }
1762 
1763   // Is the Compare only used for this purpose?
1764   if (!left->GetUses().HasExactlyOneElement()) {
1765     // Someone else also wants the result of the compare.
1766     return;
1767   }
1768 
1769   if (!left->GetEnvUses().empty()) {
1770     // There is a reference to the compare result in an environment. Do we really need it?
1771     if (GetGraph()->IsDebuggable()) {
1772       return;
1773     }
1774 
1775     // We have to ensure that there are no deopt points in the sequence.
1776     if (left->HasAnyEnvironmentUseBefore(condition)) {
1777       return;
1778     }
1779   }
1780 
1781   // Clean up any environment uses from the HCompare, if any.
1782   left->RemoveEnvironmentUsers();
1783 
1784   // We have decided to fold the HCompare into the HCondition. Transfer the information.
1785   condition->SetBias(left->AsCompare()->GetBias());
1786 
1787   // Replace the operands of the HCondition.
1788   condition->ReplaceInput(left->InputAt(0), 0);
1789   condition->ReplaceInput(left->InputAt(1), 1);
1790 
1791   // Remove the HCompare.
1792   left->GetBlock()->RemoveInstruction(left);
1793 
1794   RecordSimplification();
1795 }
1796 
1797 // Return whether x / divisor == x * (1.0f / divisor), for every float x.
CanDivideByReciprocalMultiplyFloat(int32_t divisor)1798 static constexpr bool CanDivideByReciprocalMultiplyFloat(int32_t divisor) {
1799   // True, if the most significant bits of divisor are 0.
1800   return ((divisor & 0x7fffff) == 0);
1801 }
1802 
1803 // Return whether x / divisor == x * (1.0 / divisor), for every double x.
CanDivideByReciprocalMultiplyDouble(int64_t divisor)1804 static constexpr bool CanDivideByReciprocalMultiplyDouble(int64_t divisor) {
1805   // True, if the most significant bits of divisor are 0.
1806   return ((divisor & ((UINT64_C(1) << 52) - 1)) == 0);
1807 }
1808 
VisitDiv(HDiv * instruction)1809 void InstructionSimplifierVisitor::VisitDiv(HDiv* instruction) {
1810   HConstant* input_cst = instruction->GetConstantRight();
1811   HInstruction* input_other = instruction->GetLeastConstantLeft();
1812   DataType::Type type = instruction->GetType();
1813 
1814   if ((input_cst != nullptr) && input_cst->IsOne()) {
1815     // Replace code looking like
1816     //    DIV dst, src, 1
1817     // with
1818     //    src
1819     instruction->ReplaceWith(input_other);
1820     instruction->GetBlock()->RemoveInstruction(instruction);
1821     RecordSimplification();
1822     return;
1823   }
1824 
1825   if ((input_cst != nullptr) && input_cst->IsMinusOne()) {
1826     // Replace code looking like
1827     //    DIV dst, src, -1
1828     // with
1829     //    NEG dst, src
1830     instruction->GetBlock()->ReplaceAndRemoveInstructionWith(
1831         instruction, new (GetGraph()->GetAllocator()) HNeg(type, input_other));
1832     RecordSimplification();
1833     return;
1834   }
1835 
1836   if ((input_cst != nullptr) && DataType::IsFloatingPointType(type)) {
1837     // Try replacing code looking like
1838     //    DIV dst, src, constant
1839     // with
1840     //    MUL dst, src, 1 / constant
1841     HConstant* reciprocal = nullptr;
1842     if (type == DataType::Type::kFloat64) {
1843       double value = input_cst->AsDoubleConstant()->GetValue();
1844       if (CanDivideByReciprocalMultiplyDouble(bit_cast<int64_t, double>(value))) {
1845         reciprocal = GetGraph()->GetDoubleConstant(1.0 / value);
1846       }
1847     } else {
1848       DCHECK_EQ(type, DataType::Type::kFloat32);
1849       float value = input_cst->AsFloatConstant()->GetValue();
1850       if (CanDivideByReciprocalMultiplyFloat(bit_cast<int32_t, float>(value))) {
1851         reciprocal = GetGraph()->GetFloatConstant(1.0f / value);
1852       }
1853     }
1854 
1855     if (reciprocal != nullptr) {
1856       instruction->GetBlock()->ReplaceAndRemoveInstructionWith(
1857           instruction, new (GetGraph()->GetAllocator()) HMul(type, input_other, reciprocal));
1858       RecordSimplification();
1859       return;
1860     }
1861   }
1862 }
1863 
1864 
1865 // Search HDiv having the specified dividend and divisor which is in the specified basic block.
1866 // Return nullptr if nothing has been found.
FindDivWithInputsInBasicBlock(HInstruction * dividend,HInstruction * divisor,HBasicBlock * basic_block)1867 static HDiv* FindDivWithInputsInBasicBlock(HInstruction* dividend,
1868                                            HInstruction* divisor,
1869                                            HBasicBlock* basic_block) {
1870   for (const HUseListNode<HInstruction*>& use : dividend->GetUses()) {
1871     HInstruction* user = use.GetUser();
1872     if (user->GetBlock() == basic_block &&
1873         user->IsDiv() &&
1874         user->InputAt(0) == dividend &&
1875         user->InputAt(1) == divisor) {
1876       return user->AsDiv();
1877     }
1878   }
1879   return nullptr;
1880 }
1881 
1882 // If there is Div with the same inputs as Rem and in the same basic block, it can be reused.
1883 // Rem is replaced with Mul+Sub which use the found Div.
TryToReuseDiv(HRem * rem)1884 void InstructionSimplifierVisitor::TryToReuseDiv(HRem* rem) {
1885   // As the optimization replaces Rem with Mul+Sub they prevent some loop optimizations
1886   // if the Rem is in a loop.
1887   // Check if it is allowed to optimize such Rems.
1888   if (rem->IsInLoop() && be_loop_friendly_) {
1889     return;
1890   }
1891   DataType::Type type = rem->GetResultType();
1892   if (!DataType::IsIntOrLongType(type)) {
1893     return;
1894   }
1895 
1896   HBasicBlock* basic_block = rem->GetBlock();
1897   HInstruction* dividend = rem->GetLeft();
1898   HInstruction* divisor = rem->GetRight();
1899 
1900   if (divisor->IsConstant()) {
1901     HConstant* input_cst = rem->GetConstantRight();
1902     DCHECK(input_cst->IsIntConstant() || input_cst->IsLongConstant());
1903     int64_t cst_value = Int64FromConstant(input_cst);
1904     if (cst_value == std::numeric_limits<int64_t>::min() || IsPowerOfTwo(std::abs(cst_value))) {
1905       // Such cases are usually handled in the code generator because they don't need Div at all.
1906       return;
1907     }
1908   }
1909 
1910   HDiv* quotient = FindDivWithInputsInBasicBlock(dividend, divisor, basic_block);
1911   if (quotient == nullptr) {
1912     return;
1913   }
1914   if (!quotient->StrictlyDominates(rem)) {
1915     quotient->MoveBefore(rem);
1916   }
1917 
1918   ArenaAllocator* allocator = GetGraph()->GetAllocator();
1919   HInstruction* mul = new (allocator) HMul(type, quotient, divisor);
1920   basic_block->InsertInstructionBefore(mul, rem);
1921   HInstruction* sub = new (allocator) HSub(type, dividend, mul);
1922   basic_block->InsertInstructionBefore(sub, rem);
1923   rem->ReplaceWith(sub);
1924   basic_block->RemoveInstruction(rem);
1925   RecordSimplification();
1926 }
1927 
VisitRem(HRem * rem)1928 void InstructionSimplifierVisitor::VisitRem(HRem* rem) {
1929   TryToReuseDiv(rem);
1930 }
1931 
VisitMul(HMul * instruction)1932 void InstructionSimplifierVisitor::VisitMul(HMul* instruction) {
1933   HConstant* input_cst = instruction->GetConstantRight();
1934   HInstruction* input_other = instruction->GetLeastConstantLeft();
1935   DataType::Type type = instruction->GetType();
1936   HBasicBlock* block = instruction->GetBlock();
1937   ArenaAllocator* allocator = GetGraph()->GetAllocator();
1938 
1939   if (input_cst == nullptr) {
1940     return;
1941   }
1942 
1943   if (input_cst->IsOne()) {
1944     // Replace code looking like
1945     //    MUL dst, src, 1
1946     // with
1947     //    src
1948     instruction->ReplaceWith(input_other);
1949     instruction->GetBlock()->RemoveInstruction(instruction);
1950     RecordSimplification();
1951     return;
1952   }
1953 
1954   if (input_cst->IsMinusOne() &&
1955       (DataType::IsFloatingPointType(type) || DataType::IsIntOrLongType(type))) {
1956     // Replace code looking like
1957     //    MUL dst, src, -1
1958     // with
1959     //    NEG dst, src
1960     HNeg* neg = new (allocator) HNeg(type, input_other);
1961     block->ReplaceAndRemoveInstructionWith(instruction, neg);
1962     RecordSimplification();
1963     return;
1964   }
1965 
1966   if (DataType::IsFloatingPointType(type) &&
1967       ((input_cst->IsFloatConstant() && input_cst->AsFloatConstant()->GetValue() == 2.0f) ||
1968        (input_cst->IsDoubleConstant() && input_cst->AsDoubleConstant()->GetValue() == 2.0))) {
1969     // Replace code looking like
1970     //    FP_MUL dst, src, 2.0
1971     // with
1972     //    FP_ADD dst, src, src
1973     // The 'int' and 'long' cases are handled below.
1974     block->ReplaceAndRemoveInstructionWith(instruction,
1975                                            new (allocator) HAdd(type, input_other, input_other));
1976     RecordSimplification();
1977     return;
1978   }
1979 
1980   if (DataType::IsIntOrLongType(type)) {
1981     int64_t factor = Int64FromConstant(input_cst);
1982     // Even though constant propagation also takes care of the zero case, other
1983     // optimizations can lead to having a zero multiplication.
1984     if (factor == 0) {
1985       // Replace code looking like
1986       //    MUL dst, src, 0
1987       // with
1988       //    0
1989       instruction->ReplaceWith(input_cst);
1990       instruction->GetBlock()->RemoveInstruction(instruction);
1991       RecordSimplification();
1992       return;
1993     } else if (IsPowerOfTwo(factor)) {
1994       // Replace code looking like
1995       //    MUL dst, src, pow_of_2
1996       // with
1997       //    SHL dst, src, log2(pow_of_2)
1998       HIntConstant* shift = GetGraph()->GetIntConstant(WhichPowerOf2(factor));
1999       HShl* shl = new (allocator) HShl(type, input_other, shift);
2000       block->ReplaceAndRemoveInstructionWith(instruction, shl);
2001       RecordSimplification();
2002       return;
2003     } else if (IsPowerOfTwo(factor - 1)) {
2004       // Transform code looking like
2005       //    MUL dst, src, (2^n + 1)
2006       // into
2007       //    SHL tmp, src, n
2008       //    ADD dst, src, tmp
2009       HShl* shl = new (allocator) HShl(type,
2010                                        input_other,
2011                                        GetGraph()->GetIntConstant(WhichPowerOf2(factor - 1)));
2012       HAdd* add = new (allocator) HAdd(type, input_other, shl);
2013 
2014       block->InsertInstructionBefore(shl, instruction);
2015       block->ReplaceAndRemoveInstructionWith(instruction, add);
2016       RecordSimplification();
2017       return;
2018     } else if (IsPowerOfTwo(factor + 1)) {
2019       // Transform code looking like
2020       //    MUL dst, src, (2^n - 1)
2021       // into
2022       //    SHL tmp, src, n
2023       //    SUB dst, tmp, src
2024       HShl* shl = new (allocator) HShl(type,
2025                                        input_other,
2026                                        GetGraph()->GetIntConstant(WhichPowerOf2(factor + 1)));
2027       HSub* sub = new (allocator) HSub(type, shl, input_other);
2028 
2029       block->InsertInstructionBefore(shl, instruction);
2030       block->ReplaceAndRemoveInstructionWith(instruction, sub);
2031       RecordSimplification();
2032       return;
2033     }
2034   }
2035 
2036   // TryHandleAssociativeAndCommutativeOperation() does not remove its input,
2037   // so no need to return.
2038   TryHandleAssociativeAndCommutativeOperation(instruction);
2039 }
2040 
VisitNeg(HNeg * instruction)2041 void InstructionSimplifierVisitor::VisitNeg(HNeg* instruction) {
2042   HInstruction* input = instruction->GetInput();
2043   if (input->IsNeg()) {
2044     // Replace code looking like
2045     //    NEG tmp, src
2046     //    NEG dst, tmp
2047     // with
2048     //    src
2049     HNeg* previous_neg = input->AsNeg();
2050     instruction->ReplaceWith(previous_neg->GetInput());
2051     instruction->GetBlock()->RemoveInstruction(instruction);
2052     // We perform the optimization even if the input negation has environment
2053     // uses since it allows removing the current instruction. But we only delete
2054     // the input negation only if it is does not have any uses left.
2055     if (!previous_neg->HasUses()) {
2056       previous_neg->GetBlock()->RemoveInstruction(previous_neg);
2057     }
2058     RecordSimplification();
2059     return;
2060   }
2061 
2062   if (input->IsSub() && input->HasOnlyOneNonEnvironmentUse() &&
2063       !DataType::IsFloatingPointType(input->GetType())) {
2064     // Replace code looking like
2065     //    SUB tmp, a, b
2066     //    NEG dst, tmp
2067     // with
2068     //    SUB dst, b, a
2069     // We do not perform the optimization if the input subtraction has
2070     // environment uses or multiple non-environment uses as it could lead to
2071     // worse code. In particular, we do not want the live ranges of `a` and `b`
2072     // to be extended if we are not sure the initial 'SUB' instruction can be
2073     // removed.
2074     // We do not perform optimization for fp because we could lose the sign of zero.
2075     HSub* sub = input->AsSub();
2076     HSub* new_sub = new (GetGraph()->GetAllocator()) HSub(
2077         instruction->GetType(), sub->GetRight(), sub->GetLeft());
2078     instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, new_sub);
2079     if (!sub->HasUses()) {
2080       sub->GetBlock()->RemoveInstruction(sub);
2081     }
2082     RecordSimplification();
2083   }
2084 }
2085 
VisitNot(HNot * instruction)2086 void InstructionSimplifierVisitor::VisitNot(HNot* instruction) {
2087   HInstruction* input = instruction->GetInput();
2088   if (input->IsNot()) {
2089     // Replace code looking like
2090     //    NOT tmp, src
2091     //    NOT dst, tmp
2092     // with
2093     //    src
2094     // We perform the optimization even if the input negation has environment
2095     // uses since it allows removing the current instruction. But we only delete
2096     // the input negation only if it is does not have any uses left.
2097     HNot* previous_not = input->AsNot();
2098     instruction->ReplaceWith(previous_not->GetInput());
2099     instruction->GetBlock()->RemoveInstruction(instruction);
2100     if (!previous_not->HasUses()) {
2101       previous_not->GetBlock()->RemoveInstruction(previous_not);
2102     }
2103     RecordSimplification();
2104   }
2105 }
2106 
VisitOr(HOr * instruction)2107 void InstructionSimplifierVisitor::VisitOr(HOr* instruction) {
2108   HConstant* input_cst = instruction->GetConstantRight();
2109   HInstruction* input_other = instruction->GetLeastConstantLeft();
2110 
2111   if ((input_cst != nullptr) && input_cst->IsZeroBitPattern()) {
2112     // Replace code looking like
2113     //    OR dst, src, 0
2114     // with
2115     //    src
2116     instruction->ReplaceWith(input_other);
2117     instruction->GetBlock()->RemoveInstruction(instruction);
2118     RecordSimplification();
2119     return;
2120   }
2121 
2122   // We assume that GVN has run before, so we only perform a pointer comparison.
2123   // If for some reason the values are equal but the pointers are different, we
2124   // are still correct and only miss an optimization opportunity.
2125   if (instruction->GetLeft() == instruction->GetRight()) {
2126     // Replace code looking like
2127     //    OR dst, src, src
2128     // with
2129     //    src
2130     instruction->ReplaceWith(instruction->GetLeft());
2131     instruction->GetBlock()->RemoveInstruction(instruction);
2132     RecordSimplification();
2133     return;
2134   }
2135 
2136   if (TryDeMorganNegationFactoring(instruction)) return;
2137 
2138   if (TryReplaceWithRotate(instruction)) {
2139     return;
2140   }
2141 
2142   // TryHandleAssociativeAndCommutativeOperation() does not remove its input,
2143   // so no need to return.
2144   TryHandleAssociativeAndCommutativeOperation(instruction);
2145 }
2146 
VisitShl(HShl * instruction)2147 void InstructionSimplifierVisitor::VisitShl(HShl* instruction) {
2148   VisitShift(instruction);
2149 }
2150 
VisitShr(HShr * instruction)2151 void InstructionSimplifierVisitor::VisitShr(HShr* instruction) {
2152   VisitShift(instruction);
2153 }
2154 
VisitSub(HSub * instruction)2155 void InstructionSimplifierVisitor::VisitSub(HSub* instruction) {
2156   HConstant* input_cst = instruction->GetConstantRight();
2157   HInstruction* input_other = instruction->GetLeastConstantLeft();
2158 
2159   DataType::Type type = instruction->GetType();
2160   if (DataType::IsFloatingPointType(type)) {
2161     return;
2162   }
2163 
2164   if ((input_cst != nullptr) && input_cst->IsArithmeticZero()) {
2165     // Replace code looking like
2166     //    SUB dst, src, 0
2167     // with
2168     //    src
2169     // Note that we cannot optimize `x - 0.0` to `x` for floating-point. When
2170     // `x` is `-0.0`, the former expression yields `0.0`, while the later
2171     // yields `-0.0`.
2172     instruction->ReplaceWith(input_other);
2173     instruction->GetBlock()->RemoveInstruction(instruction);
2174     RecordSimplification();
2175     return;
2176   }
2177 
2178   HBasicBlock* block = instruction->GetBlock();
2179   ArenaAllocator* allocator = GetGraph()->GetAllocator();
2180 
2181   HInstruction* left = instruction->GetLeft();
2182   HInstruction* right = instruction->GetRight();
2183   if (left->IsConstant()) {
2184     if (Int64FromConstant(left->AsConstant()) == 0) {
2185       // Replace code looking like
2186       //    SUB dst, 0, src
2187       // with
2188       //    NEG dst, src
2189       // Note that we cannot optimize `0.0 - x` to `-x` for floating-point. When
2190       // `x` is `0.0`, the former expression yields `0.0`, while the later
2191       // yields `-0.0`.
2192       HNeg* neg = new (allocator) HNeg(type, right);
2193       block->ReplaceAndRemoveInstructionWith(instruction, neg);
2194       RecordSimplification();
2195       return;
2196     }
2197   }
2198 
2199   if (left->IsNeg() && right->IsNeg()) {
2200     if (TryMoveNegOnInputsAfterBinop(instruction)) {
2201       return;
2202     }
2203   }
2204 
2205   if (right->IsNeg() && right->HasOnlyOneNonEnvironmentUse()) {
2206     // Replace code looking like
2207     //    NEG tmp, b
2208     //    SUB dst, a, tmp
2209     // with
2210     //    ADD dst, a, b
2211     HAdd* add = new(GetGraph()->GetAllocator()) HAdd(type, left, right->AsNeg()->GetInput());
2212     instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, add);
2213     RecordSimplification();
2214     right->GetBlock()->RemoveInstruction(right);
2215     return;
2216   }
2217 
2218   if (left->IsNeg() && left->HasOnlyOneNonEnvironmentUse()) {
2219     // Replace code looking like
2220     //    NEG tmp, a
2221     //    SUB dst, tmp, b
2222     // with
2223     //    ADD tmp, a, b
2224     //    NEG dst, tmp
2225     // The second version is not intrinsically better, but enables more
2226     // transformations.
2227     HAdd* add = new(GetGraph()->GetAllocator()) HAdd(type, left->AsNeg()->GetInput(), right);
2228     instruction->GetBlock()->InsertInstructionBefore(add, instruction);
2229     HNeg* neg = new (GetGraph()->GetAllocator()) HNeg(instruction->GetType(), add);
2230     instruction->GetBlock()->InsertInstructionBefore(neg, instruction);
2231     instruction->ReplaceWith(neg);
2232     instruction->GetBlock()->RemoveInstruction(instruction);
2233     RecordSimplification();
2234     left->GetBlock()->RemoveInstruction(left);
2235     return;
2236   }
2237 
2238   if (TrySubtractionChainSimplification(instruction)) {
2239     return;
2240   }
2241 
2242   if (left->IsAdd()) {
2243     // Replace code patterns looking like
2244     //    ADD dst1, x, y        ADD dst1, x, y
2245     //    SUB dst2, dst1, y     SUB dst2, dst1, x
2246     // with
2247     //    ADD dst1, x, y
2248     // SUB instruction is not needed in this case, we may use
2249     // one of inputs of ADD instead.
2250     // It is applicable to integral types only.
2251     DCHECK(DataType::IsIntegralType(type));
2252     if (left->InputAt(1) == right) {
2253       instruction->ReplaceWith(left->InputAt(0));
2254       RecordSimplification();
2255       instruction->GetBlock()->RemoveInstruction(instruction);
2256       return;
2257     } else if (left->InputAt(0) == right) {
2258       instruction->ReplaceWith(left->InputAt(1));
2259       RecordSimplification();
2260       instruction->GetBlock()->RemoveInstruction(instruction);
2261       return;
2262     }
2263   }
2264 }
2265 
VisitUShr(HUShr * instruction)2266 void InstructionSimplifierVisitor::VisitUShr(HUShr* instruction) {
2267   VisitShift(instruction);
2268 }
2269 
VisitXor(HXor * instruction)2270 void InstructionSimplifierVisitor::VisitXor(HXor* instruction) {
2271   HConstant* input_cst = instruction->GetConstantRight();
2272   HInstruction* input_other = instruction->GetLeastConstantLeft();
2273 
2274   if ((input_cst != nullptr) && input_cst->IsZeroBitPattern()) {
2275     // Replace code looking like
2276     //    XOR dst, src, 0
2277     // with
2278     //    src
2279     instruction->ReplaceWith(input_other);
2280     instruction->GetBlock()->RemoveInstruction(instruction);
2281     RecordSimplification();
2282     return;
2283   }
2284 
2285   if ((input_cst != nullptr) && input_cst->IsOne()
2286       && input_other->GetType() == DataType::Type::kBool) {
2287     // Replace code looking like
2288     //    XOR dst, src, 1
2289     // with
2290     //    BOOLEAN_NOT dst, src
2291     HBooleanNot* boolean_not = new (GetGraph()->GetAllocator()) HBooleanNot(input_other);
2292     instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, boolean_not);
2293     RecordSimplification();
2294     return;
2295   }
2296 
2297   if ((input_cst != nullptr) && AreAllBitsSet(input_cst)) {
2298     // Replace code looking like
2299     //    XOR dst, src, 0xFFF...FF
2300     // with
2301     //    NOT dst, src
2302     HNot* bitwise_not = new (GetGraph()->GetAllocator()) HNot(instruction->GetType(), input_other);
2303     instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, bitwise_not);
2304     RecordSimplification();
2305     return;
2306   }
2307 
2308   HInstruction* left = instruction->GetLeft();
2309   HInstruction* right = instruction->GetRight();
2310   if (((left->IsNot() && right->IsNot()) ||
2311        (left->IsBooleanNot() && right->IsBooleanNot())) &&
2312       left->HasOnlyOneNonEnvironmentUse() &&
2313       right->HasOnlyOneNonEnvironmentUse()) {
2314     // Replace code looking like
2315     //    NOT nota, a
2316     //    NOT notb, b
2317     //    XOR dst, nota, notb
2318     // with
2319     //    XOR dst, a, b
2320     instruction->ReplaceInput(left->InputAt(0), 0);
2321     instruction->ReplaceInput(right->InputAt(0), 1);
2322     left->GetBlock()->RemoveInstruction(left);
2323     right->GetBlock()->RemoveInstruction(right);
2324     RecordSimplification();
2325     return;
2326   }
2327 
2328   if (TryReplaceWithRotate(instruction)) {
2329     return;
2330   }
2331 
2332   // TryHandleAssociativeAndCommutativeOperation() does not remove its input,
2333   // so no need to return.
2334   TryHandleAssociativeAndCommutativeOperation(instruction);
2335 }
2336 
SimplifyStringEquals(HInvoke * instruction)2337 void InstructionSimplifierVisitor::SimplifyStringEquals(HInvoke* instruction) {
2338   HInstruction* argument = instruction->InputAt(1);
2339   HInstruction* receiver = instruction->InputAt(0);
2340   if (receiver == argument) {
2341     // Because String.equals is an instance call, the receiver is
2342     // a null check if we don't know it's null. The argument however, will
2343     // be the actual object. So we cannot end up in a situation where both
2344     // are equal but could be null.
2345     DCHECK(CanEnsureNotNullAt(argument, instruction));
2346     instruction->ReplaceWith(GetGraph()->GetIntConstant(1));
2347     instruction->GetBlock()->RemoveInstruction(instruction);
2348   } else {
2349     StringEqualsOptimizations optimizations(instruction);
2350     if (CanEnsureNotNullAt(argument, instruction)) {
2351       optimizations.SetArgumentNotNull();
2352     }
2353     ScopedObjectAccess soa(Thread::Current());
2354     ReferenceTypeInfo argument_rti = argument->GetReferenceTypeInfo();
2355     if (argument_rti.IsValid() && argument_rti.IsStringClass()) {
2356       optimizations.SetArgumentIsString();
2357     }
2358   }
2359 }
2360 
IsArrayLengthOf(HInstruction * potential_length,HInstruction * potential_array)2361 static bool IsArrayLengthOf(HInstruction* potential_length, HInstruction* potential_array) {
2362   if (potential_length->IsArrayLength()) {
2363     return potential_length->InputAt(0) == potential_array;
2364   }
2365 
2366   if (potential_array->IsNewArray()) {
2367     return potential_array->AsNewArray()->GetLength() == potential_length;
2368   }
2369 
2370   return false;
2371 }
2372 
SimplifySystemArrayCopy(HInvoke * instruction)2373 void InstructionSimplifierVisitor::SimplifySystemArrayCopy(HInvoke* instruction) {
2374   HInstruction* source = instruction->InputAt(0);
2375   HInstruction* destination = instruction->InputAt(2);
2376   HInstruction* count = instruction->InputAt(4);
2377   SystemArrayCopyOptimizations optimizations(instruction);
2378   if (CanEnsureNotNullAt(source, instruction)) {
2379     optimizations.SetSourceIsNotNull();
2380   }
2381   if (CanEnsureNotNullAt(destination, instruction)) {
2382     optimizations.SetDestinationIsNotNull();
2383   }
2384   if (destination == source) {
2385     optimizations.SetDestinationIsSource();
2386   }
2387 
2388   if (IsArrayLengthOf(count, source)) {
2389     optimizations.SetCountIsSourceLength();
2390   }
2391 
2392   if (IsArrayLengthOf(count, destination)) {
2393     optimizations.SetCountIsDestinationLength();
2394   }
2395 
2396   {
2397     ScopedObjectAccess soa(Thread::Current());
2398     DataType::Type source_component_type = DataType::Type::kVoid;
2399     DataType::Type destination_component_type = DataType::Type::kVoid;
2400     ReferenceTypeInfo destination_rti = destination->GetReferenceTypeInfo();
2401     if (destination_rti.IsValid()) {
2402       if (destination_rti.IsObjectArray()) {
2403         if (destination_rti.IsExact()) {
2404           optimizations.SetDoesNotNeedTypeCheck();
2405         }
2406         optimizations.SetDestinationIsTypedObjectArray();
2407       }
2408       if (destination_rti.IsPrimitiveArrayClass()) {
2409         destination_component_type = DataTypeFromPrimitive(
2410             destination_rti.GetTypeHandle()->GetComponentType()->GetPrimitiveType());
2411         optimizations.SetDestinationIsPrimitiveArray();
2412       } else if (destination_rti.IsNonPrimitiveArrayClass()) {
2413         optimizations.SetDestinationIsNonPrimitiveArray();
2414       }
2415     }
2416     ReferenceTypeInfo source_rti = source->GetReferenceTypeInfo();
2417     if (source_rti.IsValid()) {
2418       if (destination_rti.IsValid() && destination_rti.CanArrayHoldValuesOf(source_rti)) {
2419         optimizations.SetDoesNotNeedTypeCheck();
2420       }
2421       if (source_rti.IsPrimitiveArrayClass()) {
2422         optimizations.SetSourceIsPrimitiveArray();
2423         source_component_type = DataTypeFromPrimitive(
2424             source_rti.GetTypeHandle()->GetComponentType()->GetPrimitiveType());
2425       } else if (source_rti.IsNonPrimitiveArrayClass()) {
2426         optimizations.SetSourceIsNonPrimitiveArray();
2427       }
2428     }
2429     // For primitive arrays, use their optimized ArtMethod implementations.
2430     if ((source_component_type != DataType::Type::kVoid) &&
2431         (source_component_type == destination_component_type)) {
2432       ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
2433       PointerSize image_size = class_linker->GetImagePointerSize();
2434       HInvokeStaticOrDirect* invoke = instruction->AsInvokeStaticOrDirect();
2435       ObjPtr<mirror::Class> system = invoke->GetResolvedMethod()->GetDeclaringClass();
2436       ArtMethod* method = nullptr;
2437       switch (source_component_type) {
2438         case DataType::Type::kBool:
2439           method = system->FindClassMethod("arraycopy", "([ZI[ZII)V", image_size);
2440           break;
2441         case DataType::Type::kInt8:
2442           method = system->FindClassMethod("arraycopy", "([BI[BII)V", image_size);
2443           break;
2444         case DataType::Type::kUint16:
2445           method = system->FindClassMethod("arraycopy", "([CI[CII)V", image_size);
2446           break;
2447         case DataType::Type::kInt16:
2448           method = system->FindClassMethod("arraycopy", "([SI[SII)V", image_size);
2449           break;
2450         case DataType::Type::kInt32:
2451           method = system->FindClassMethod("arraycopy", "([II[III)V", image_size);
2452           break;
2453         case DataType::Type::kFloat32:
2454           method = system->FindClassMethod("arraycopy", "([FI[FII)V", image_size);
2455           break;
2456         case DataType::Type::kInt64:
2457           method = system->FindClassMethod("arraycopy", "([JI[JII)V", image_size);
2458           break;
2459         case DataType::Type::kFloat64:
2460           method = system->FindClassMethod("arraycopy", "([DI[DII)V", image_size);
2461           break;
2462         default:
2463           LOG(FATAL) << "Unreachable";
2464       }
2465       DCHECK(method != nullptr);
2466       DCHECK(method->IsStatic());
2467       DCHECK(method->GetDeclaringClass() == system);
2468       invoke->SetResolvedMethod(method, !codegen_->GetGraph()->IsDebuggable());
2469       // Sharpen the new invoke. Note that we do not update the dex method index of
2470       // the invoke, as we would need to look it up in the current dex file, and it
2471       // is unlikely that it exists. The most usual situation for such typed
2472       // arraycopy methods is a direct pointer to the boot image.
2473       invoke->SetDispatchInfo(HSharpening::SharpenLoadMethod(
2474           method,
2475           /* has_method_id= */ true,
2476           /* for_interface_call= */ false,
2477           codegen_));
2478     }
2479   }
2480 }
2481 
SimplifyFP2Int(HInvoke * invoke)2482 void InstructionSimplifierVisitor::SimplifyFP2Int(HInvoke* invoke) {
2483   DCHECK(invoke->IsInvokeStaticOrDirect());
2484   uint32_t dex_pc = invoke->GetDexPc();
2485   HInstruction* x = invoke->InputAt(0);
2486   DataType::Type type = x->GetType();
2487   // Set proper bit pattern for NaN and replace intrinsic with raw version.
2488   HInstruction* nan;
2489   if (type == DataType::Type::kFloat64) {
2490     nan = GetGraph()->GetLongConstant(0x7ff8000000000000L);
2491     invoke->SetIntrinsic(Intrinsics::kDoubleDoubleToRawLongBits,
2492                          kNeedsEnvironment,
2493                          kNoSideEffects,
2494                          kNoThrow);
2495   } else {
2496     DCHECK_EQ(type, DataType::Type::kFloat32);
2497     nan = GetGraph()->GetIntConstant(0x7fc00000);
2498     invoke->SetIntrinsic(Intrinsics::kFloatFloatToRawIntBits,
2499                          kNeedsEnvironment,
2500                          kNoSideEffects,
2501                          kNoThrow);
2502   }
2503   // Test IsNaN(x), which is the same as x != x.
2504   HCondition* condition = new (GetGraph()->GetAllocator()) HNotEqual(x, x, dex_pc);
2505   condition->SetBias(ComparisonBias::kLtBias);
2506   invoke->GetBlock()->InsertInstructionBefore(condition, invoke->GetNext());
2507   // Select between the two.
2508   HInstruction* select = new (GetGraph()->GetAllocator()) HSelect(condition, nan, invoke, dex_pc);
2509   invoke->GetBlock()->InsertInstructionBefore(select, condition->GetNext());
2510   invoke->ReplaceWithExceptInReplacementAtIndex(select, 0);  // false at index 0
2511 }
2512 
SimplifyStringCharAt(HInvoke * invoke)2513 void InstructionSimplifierVisitor::SimplifyStringCharAt(HInvoke* invoke) {
2514   HInstruction* str = invoke->InputAt(0);
2515   HInstruction* index = invoke->InputAt(1);
2516   uint32_t dex_pc = invoke->GetDexPc();
2517   ArenaAllocator* allocator = GetGraph()->GetAllocator();
2518   // We treat String as an array to allow DCE and BCE to seamlessly work on strings,
2519   // so create the HArrayLength, HBoundsCheck and HArrayGet.
2520   HArrayLength* length = new (allocator) HArrayLength(str, dex_pc, /* is_string_length= */ true);
2521   invoke->GetBlock()->InsertInstructionBefore(length, invoke);
2522   HBoundsCheck* bounds_check = new (allocator) HBoundsCheck(
2523       index, length, dex_pc, /* is_string_char_at= */ true);
2524   invoke->GetBlock()->InsertInstructionBefore(bounds_check, invoke);
2525   HArrayGet* array_get = new (allocator) HArrayGet(str,
2526                                                    bounds_check,
2527                                                    DataType::Type::kUint16,
2528                                                    SideEffects::None(),  // Strings are immutable.
2529                                                    dex_pc,
2530                                                    /* is_string_char_at= */ true);
2531   invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, array_get);
2532   bounds_check->CopyEnvironmentFrom(invoke->GetEnvironment());
2533   GetGraph()->SetHasBoundsChecks(true);
2534 }
2535 
SimplifyStringLength(HInvoke * invoke)2536 void InstructionSimplifierVisitor::SimplifyStringLength(HInvoke* invoke) {
2537   HInstruction* str = invoke->InputAt(0);
2538   uint32_t dex_pc = invoke->GetDexPc();
2539   // We treat String as an array to allow DCE and BCE to seamlessly work on strings,
2540   // so create the HArrayLength.
2541   HArrayLength* length =
2542       new (GetGraph()->GetAllocator()) HArrayLength(str, dex_pc, /* is_string_length= */ true);
2543   invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, length);
2544 }
2545 
SimplifyStringIndexOf(HInvoke * invoke)2546 void InstructionSimplifierVisitor::SimplifyStringIndexOf(HInvoke* invoke) {
2547   DCHECK(invoke->GetIntrinsic() == Intrinsics::kStringIndexOf ||
2548          invoke->GetIntrinsic() == Intrinsics::kStringIndexOfAfter);
2549   if (invoke->InputAt(0)->IsLoadString()) {
2550     HLoadString* load_string = invoke->InputAt(0)->AsLoadString();
2551     const DexFile& dex_file = load_string->GetDexFile();
2552     uint32_t utf16_length;
2553     const char* data =
2554         dex_file.StringDataAndUtf16LengthByIdx(load_string->GetStringIndex(), &utf16_length);
2555     if (utf16_length == 0) {
2556       invoke->ReplaceWith(GetGraph()->GetIntConstant(-1));
2557       invoke->GetBlock()->RemoveInstruction(invoke);
2558       RecordSimplification();
2559       return;
2560     }
2561     if (utf16_length == 1 && invoke->GetIntrinsic() == Intrinsics::kStringIndexOf) {
2562       // Simplify to HSelect(HEquals(., load_string.charAt(0)), 0, -1).
2563       // If the sought character is supplementary, this gives the correct result, i.e. -1.
2564       uint32_t c = GetUtf16FromUtf8(&data);
2565       DCHECK_EQ(GetTrailingUtf16Char(c), 0u);
2566       DCHECK_EQ(GetLeadingUtf16Char(c), c);
2567       uint32_t dex_pc = invoke->GetDexPc();
2568       ArenaAllocator* allocator = GetGraph()->GetAllocator();
2569       HEqual* equal =
2570           new (allocator) HEqual(invoke->InputAt(1), GetGraph()->GetIntConstant(c), dex_pc);
2571       invoke->GetBlock()->InsertInstructionBefore(equal, invoke);
2572       HSelect* result = new (allocator) HSelect(equal,
2573                                                 GetGraph()->GetIntConstant(0),
2574                                                 GetGraph()->GetIntConstant(-1),
2575                                                 dex_pc);
2576       invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, result);
2577       RecordSimplification();
2578       return;
2579     }
2580   }
2581 }
2582 
2583 // This method should only be used on intrinsics whose sole way of throwing an
2584 // exception is raising a NPE when the nth argument is null. If that argument
2585 // is provably non-null, we can clear the flag.
SimplifyNPEOnArgN(HInvoke * invoke,size_t n)2586 void InstructionSimplifierVisitor::SimplifyNPEOnArgN(HInvoke* invoke, size_t n) {
2587   HInstruction* arg = invoke->InputAt(n);
2588   if (invoke->CanThrow() && !arg->CanBeNull()) {
2589     invoke->SetCanThrow(false);
2590   }
2591 }
2592 
2593 // Methods that return "this" can replace the returned value with the receiver.
SimplifyReturnThis(HInvoke * invoke)2594 void InstructionSimplifierVisitor::SimplifyReturnThis(HInvoke* invoke) {
2595   if (invoke->HasUses()) {
2596     HInstruction* receiver = invoke->InputAt(0);
2597     invoke->ReplaceWith(receiver);
2598     RecordSimplification();
2599   }
2600 }
2601 
2602 // Helper method for StringBuffer escape analysis.
NoEscapeForStringBufferReference(HInstruction * reference,HInstruction * user)2603 static bool NoEscapeForStringBufferReference(HInstruction* reference, HInstruction* user) {
2604   if (user->IsInvokeStaticOrDirect()) {
2605     // Any constructor on StringBuffer is okay.
2606     return user->AsInvokeStaticOrDirect()->GetResolvedMethod() != nullptr &&
2607            user->AsInvokeStaticOrDirect()->GetResolvedMethod()->IsConstructor() &&
2608            user->InputAt(0) == reference;
2609   } else if (user->IsInvokeVirtual()) {
2610     switch (user->AsInvokeVirtual()->GetIntrinsic()) {
2611       case Intrinsics::kStringBufferLength:
2612       case Intrinsics::kStringBufferToString:
2613         DCHECK_EQ(user->InputAt(0), reference);
2614         return true;
2615       case Intrinsics::kStringBufferAppend:
2616         // Returns "this", so only okay if no further uses.
2617         DCHECK_EQ(user->InputAt(0), reference);
2618         DCHECK_NE(user->InputAt(1), reference);
2619         return !user->HasUses();
2620       default:
2621         break;
2622     }
2623   }
2624   return false;
2625 }
2626 
TryReplaceStringBuilderAppend(HInvoke * invoke)2627 static bool TryReplaceStringBuilderAppend(HInvoke* invoke) {
2628   DCHECK_EQ(invoke->GetIntrinsic(), Intrinsics::kStringBuilderToString);
2629   if (invoke->CanThrowIntoCatchBlock()) {
2630     return false;
2631   }
2632 
2633   HBasicBlock* block = invoke->GetBlock();
2634   HInstruction* sb = invoke->InputAt(0);
2635 
2636   // We support only a new StringBuilder, otherwise we cannot ensure that
2637   // the StringBuilder data does not need to be populated for other users.
2638   if (!sb->IsNewInstance()) {
2639     return false;
2640   }
2641 
2642   // For now, we support only single-block recognition.
2643   // (Ternary operators feeding the append could be implemented.)
2644   for (const HUseListNode<HInstruction*>& use : sb->GetUses()) {
2645     if (use.GetUser()->GetBlock() != block) {
2646       return false;
2647     }
2648     // The append pattern uses the StringBuilder only as the first argument.
2649     if (use.GetIndex() != 0u) {
2650       return false;
2651     }
2652   }
2653 
2654   // Collect args and check for unexpected uses.
2655   // We expect one call to a constructor with no arguments, one constructor fence (unless
2656   // eliminated), some number of append calls and one call to StringBuilder.toString().
2657   bool seen_constructor = false;
2658   bool seen_constructor_fence = false;
2659   bool seen_to_string = false;
2660   uint32_t format = 0u;
2661   uint32_t num_args = 0u;
2662   bool has_fp_args = false;
2663   HInstruction* args[StringBuilderAppend::kMaxArgs];  // Added in reverse order.
2664   for (HBackwardInstructionIterator iter(block->GetInstructions()); !iter.Done(); iter.Advance()) {
2665     HInstruction* user = iter.Current();
2666     // Instructions of interest apply to `sb`, skip those that do not involve `sb`.
2667     if (user->InputCount() == 0u || user->InputAt(0u) != sb) {
2668       continue;
2669     }
2670     // We visit the uses in reverse order, so the StringBuilder.toString() must come first.
2671     if (!seen_to_string) {
2672       if (user == invoke) {
2673         seen_to_string = true;
2674         continue;
2675       } else {
2676         return false;
2677       }
2678     }
2679     // Then we should see the arguments.
2680     if (user->IsInvokeVirtual()) {
2681       HInvokeVirtual* as_invoke_virtual = user->AsInvokeVirtual();
2682       DCHECK(!seen_constructor);
2683       DCHECK(!seen_constructor_fence);
2684       StringBuilderAppend::Argument arg;
2685       switch (as_invoke_virtual->GetIntrinsic()) {
2686         case Intrinsics::kStringBuilderAppendObject:
2687           // TODO: Unimplemented, needs to call String.valueOf().
2688           return false;
2689         case Intrinsics::kStringBuilderAppendString:
2690           arg = StringBuilderAppend::Argument::kString;
2691           break;
2692         case Intrinsics::kStringBuilderAppendCharArray:
2693           // TODO: Unimplemented, StringBuilder.append(char[]) can throw NPE and we would
2694           // not have the correct stack trace for it.
2695           return false;
2696         case Intrinsics::kStringBuilderAppendBoolean:
2697           arg = StringBuilderAppend::Argument::kBoolean;
2698           break;
2699         case Intrinsics::kStringBuilderAppendChar:
2700           arg = StringBuilderAppend::Argument::kChar;
2701           break;
2702         case Intrinsics::kStringBuilderAppendInt:
2703           arg = StringBuilderAppend::Argument::kInt;
2704           break;
2705         case Intrinsics::kStringBuilderAppendLong:
2706           arg = StringBuilderAppend::Argument::kLong;
2707           break;
2708         case Intrinsics::kStringBuilderAppendFloat:
2709           arg = StringBuilderAppend::Argument::kFloat;
2710           has_fp_args = true;
2711           break;
2712         case Intrinsics::kStringBuilderAppendDouble:
2713           arg = StringBuilderAppend::Argument::kDouble;
2714           has_fp_args = true;
2715           break;
2716         case Intrinsics::kStringBuilderAppendCharSequence: {
2717           ReferenceTypeInfo rti = user->AsInvokeVirtual()->InputAt(1)->GetReferenceTypeInfo();
2718           if (!rti.IsValid()) {
2719             return false;
2720           }
2721           ScopedObjectAccess soa(Thread::Current());
2722           Handle<mirror::Class> input_type = rti.GetTypeHandle();
2723           DCHECK(input_type != nullptr);
2724           if (input_type.Get() == GetClassRoot<mirror::String>()) {
2725             arg = StringBuilderAppend::Argument::kString;
2726           } else {
2727             // TODO: Check and implement for StringBuilder. We could find the StringBuilder's
2728             // internal char[] inconsistent with the length, or the string compression
2729             // of the result could be compromised with a concurrent modification, and
2730             // we would need to throw appropriate exceptions.
2731             return false;
2732           }
2733           break;
2734         }
2735         default: {
2736           return false;
2737         }
2738       }
2739       // Uses of the append return value should have been replaced with the first input.
2740       DCHECK(!as_invoke_virtual->HasUses());
2741       DCHECK(!as_invoke_virtual->HasEnvironmentUses());
2742       if (num_args == StringBuilderAppend::kMaxArgs) {
2743         return false;
2744       }
2745       format = (format << StringBuilderAppend::kBitsPerArg) | static_cast<uint32_t>(arg);
2746       args[num_args] = as_invoke_virtual->InputAt(1u);
2747       ++num_args;
2748     } else if (user->IsInvokeStaticOrDirect() &&
2749                user->AsInvokeStaticOrDirect()->GetResolvedMethod() != nullptr &&
2750                user->AsInvokeStaticOrDirect()->GetResolvedMethod()->IsConstructor() &&
2751                user->AsInvokeStaticOrDirect()->GetNumberOfArguments() == 1u) {
2752       // After arguments, we should see the constructor.
2753       // We accept only the constructor with no extra arguments.
2754       DCHECK(!seen_constructor);
2755       DCHECK(!seen_constructor_fence);
2756       seen_constructor = true;
2757     } else if (user->IsConstructorFence()) {
2758       // The last use we see is the constructor fence.
2759       DCHECK(seen_constructor);
2760       DCHECK(!seen_constructor_fence);
2761       seen_constructor_fence = true;
2762     } else {
2763       return false;
2764     }
2765   }
2766 
2767   if (num_args == 0u) {
2768     return false;
2769   }
2770 
2771   // Check environment uses.
2772   for (const HUseListNode<HEnvironment*>& use : sb->GetEnvUses()) {
2773     HInstruction* holder = use.GetUser()->GetHolder();
2774     if (holder->GetBlock() != block) {
2775       return false;
2776     }
2777     // Accept only calls on the StringBuilder (which shall all be removed).
2778     // TODO: Carve-out for const-string? Or rely on environment pruning (to be implemented)?
2779     if (holder->InputCount() == 0 || holder->InputAt(0) != sb) {
2780       return false;
2781     }
2782   }
2783 
2784   // Create replacement instruction.
2785   HIntConstant* fmt = block->GetGraph()->GetIntConstant(static_cast<int32_t>(format));
2786   ArenaAllocator* allocator = block->GetGraph()->GetAllocator();
2787   HStringBuilderAppend* append = new (allocator) HStringBuilderAppend(
2788       fmt, num_args, has_fp_args, allocator, invoke->GetDexPc());
2789   append->SetReferenceTypeInfoIfValid(invoke->GetReferenceTypeInfo());
2790   for (size_t i = 0; i != num_args; ++i) {
2791     append->SetArgumentAt(i, args[num_args - 1u - i]);
2792   }
2793   block->InsertInstructionBefore(append, invoke);
2794   DCHECK(!invoke->CanBeNull());
2795   DCHECK(!append->CanBeNull());
2796   invoke->ReplaceWith(append);
2797   // Copy environment, except for the StringBuilder uses.
2798   for (HEnvironment* env = invoke->GetEnvironment(); env != nullptr; env = env->GetParent()) {
2799     for (size_t i = 0, size = env->Size(); i != size; ++i) {
2800       if (env->GetInstructionAt(i) == sb) {
2801         env->RemoveAsUserOfInput(i);
2802         env->SetRawEnvAt(i, /*instruction=*/ nullptr);
2803       }
2804     }
2805   }
2806   append->CopyEnvironmentFrom(invoke->GetEnvironment());
2807   // Remove the old instruction.
2808   block->RemoveInstruction(invoke);
2809   // Remove the StringBuilder's uses and StringBuilder.
2810   while (sb->HasNonEnvironmentUses()) {
2811     block->RemoveInstruction(sb->GetUses().front().GetUser());
2812   }
2813   DCHECK(!sb->HasEnvironmentUses());
2814   block->RemoveInstruction(sb);
2815   return true;
2816 }
2817 
2818 // Certain allocation intrinsics are not removed by dead code elimination
2819 // because of potentially throwing an OOM exception or other side effects.
2820 // This method removes such intrinsics when special circumstances allow.
SimplifyAllocationIntrinsic(HInvoke * invoke)2821 void InstructionSimplifierVisitor::SimplifyAllocationIntrinsic(HInvoke* invoke) {
2822   if (!invoke->HasUses()) {
2823     // Instruction has no uses. If unsynchronized, we can remove right away, safely ignoring
2824     // the potential OOM of course. Otherwise, we must ensure the receiver object of this
2825     // call does not escape since only thread-local synchronization may be removed.
2826     bool is_synchronized = invoke->GetIntrinsic() == Intrinsics::kStringBufferToString;
2827     HInstruction* receiver = invoke->InputAt(0);
2828     if (!is_synchronized || DoesNotEscape(receiver, NoEscapeForStringBufferReference)) {
2829       invoke->GetBlock()->RemoveInstruction(invoke);
2830       RecordSimplification();
2831     }
2832   } else if (invoke->GetIntrinsic() == Intrinsics::kStringBuilderToString &&
2833              TryReplaceStringBuilderAppend(invoke)) {
2834     RecordSimplification();
2835   }
2836 }
2837 
SimplifyVarHandleIntrinsic(HInvoke * invoke)2838 void InstructionSimplifierVisitor::SimplifyVarHandleIntrinsic(HInvoke* invoke) {
2839   DCHECK(invoke->IsInvokePolymorphic());
2840   VarHandleOptimizations optimizations(invoke);
2841 
2842   if (optimizations.GetDoNotIntrinsify()) {
2843     // Preceding static checks disabled intrinsic, so no need to analyze further.
2844     return;
2845   }
2846 
2847   size_t expected_coordinates_count = GetExpectedVarHandleCoordinatesCount(invoke);
2848   if (expected_coordinates_count != 0u) {
2849     HInstruction* object = invoke->InputAt(1);
2850     // The following has been ensured by static checks in the instruction builder.
2851     DCHECK(object->GetType() == DataType::Type::kReference);
2852     // Re-check for null constant, as this might have changed after the inliner.
2853     if (object->IsNullConstant()) {
2854       optimizations.SetDoNotIntrinsify();
2855       return;
2856     }
2857     // Test whether we can avoid the null check on the object.
2858     if (CanEnsureNotNullAt(object, invoke)) {
2859       optimizations.SetSkipObjectNullCheck();
2860     }
2861   }
2862 
2863   if (CanUseKnownBootImageVarHandle(invoke)) {
2864     optimizations.SetUseKnownBootImageVarHandle();
2865   }
2866 }
2867 
CanUseKnownBootImageVarHandle(HInvoke * invoke)2868 bool InstructionSimplifierVisitor::CanUseKnownBootImageVarHandle(HInvoke* invoke) {
2869   // If the `VarHandle` comes from a static final field of an initialized class in
2870   // the boot image, we can do the checks at compile time. We do this optimization only
2871   // for AOT and only for field handles when we can avoid all checks. This avoids the
2872   // possibility of the code concurrently messing with the `VarHandle` using reflection,
2873   // we simply perform the operation with the `VarHandle` as seen at compile time.
2874   // TODO: Extend this to arrays to support the `AtomicIntegerArray` class.
2875   const CompilerOptions& compiler_options = codegen_->GetCompilerOptions();
2876   if (!compiler_options.IsAotCompiler()) {
2877     return false;
2878   }
2879   size_t expected_coordinates_count = GetExpectedVarHandleCoordinatesCount(invoke);
2880   if (expected_coordinates_count == 2u) {
2881     return false;
2882   }
2883   HInstruction* var_handle_instruction = invoke->InputAt(0);
2884   if (var_handle_instruction->IsNullCheck()) {
2885     var_handle_instruction = var_handle_instruction->InputAt(0);
2886   }
2887   if (!var_handle_instruction->IsStaticFieldGet()) {
2888     return false;
2889   }
2890   ArtField* field = var_handle_instruction->AsStaticFieldGet()->GetFieldInfo().GetField();
2891   DCHECK(field->IsStatic());
2892   if (!field->IsFinal()) {
2893     return false;
2894   }
2895   ScopedObjectAccess soa(Thread::Current());
2896   ObjPtr<mirror::Class> declaring_class = field->GetDeclaringClass();
2897   if (!declaring_class->IsVisiblyInitialized()) {
2898     // During AOT compilation, dex2oat ensures that initialized classes are visibly initialized.
2899     DCHECK(!declaring_class->IsInitialized());
2900     return false;
2901   }
2902   HInstruction* load_class = var_handle_instruction->InputAt(0);
2903   if (kIsDebugBuild) {
2904     bool is_in_boot_image = false;
2905     if (Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(declaring_class)) {
2906       is_in_boot_image = true;
2907     } else if (compiler_options.IsBootImage() || compiler_options.IsBootImageExtension()) {
2908       std::string storage;
2909       const char* descriptor = declaring_class->GetDescriptor(&storage);
2910       is_in_boot_image = compiler_options.IsImageClass(descriptor);
2911     }
2912     CHECK_EQ(is_in_boot_image,
2913              load_class->IsLoadClass() && load_class->AsLoadClass()->IsInBootImage());
2914   }
2915   if (!load_class->IsLoadClass() || !load_class->AsLoadClass()->IsInBootImage()) {
2916     return false;
2917   }
2918 
2919   // Get the `VarHandle` object and check its class.
2920   ObjPtr<mirror::Class> expected_var_handle_class;
2921   switch (expected_coordinates_count) {
2922     case 0:
2923       expected_var_handle_class = GetClassRoot<mirror::StaticFieldVarHandle>();
2924       break;
2925     default:
2926       DCHECK_EQ(expected_coordinates_count, 1u);
2927       expected_var_handle_class = GetClassRoot<mirror::FieldVarHandle>();
2928       break;
2929   }
2930   ObjPtr<mirror::Object> var_handle_object = field->GetObject(declaring_class);
2931   if (var_handle_object == nullptr || var_handle_object->GetClass() != expected_var_handle_class) {
2932     return false;
2933   }
2934   ObjPtr<mirror::VarHandle> var_handle = ObjPtr<mirror::VarHandle>::DownCast(var_handle_object);
2935 
2936   // Check access mode.
2937   mirror::VarHandle::AccessMode access_mode =
2938       mirror::VarHandle::GetAccessModeByIntrinsic(invoke->GetIntrinsic());
2939   if (!var_handle->IsAccessModeSupported(access_mode)) {
2940     return false;
2941   }
2942 
2943   // Check argument types.
2944   ObjPtr<mirror::Class> var_type = var_handle->GetVarType();
2945   mirror::VarHandle::AccessModeTemplate access_mode_template =
2946       mirror::VarHandle::GetAccessModeTemplate(access_mode);
2947   // Note: The data type of input arguments does not need to match the type from shorty
2948   // due to implicit conversions or avoiding unnecessary conversions before narrow stores.
2949   DataType::Type type = (access_mode_template == mirror::VarHandle::AccessModeTemplate::kGet)
2950       ? invoke->GetType()
2951       : GetDataTypeFromShorty(invoke, invoke->GetNumberOfArguments() - 1u);
2952   if (type != DataTypeFromPrimitive(var_type->GetPrimitiveType())) {
2953     return false;
2954   }
2955   if (type == DataType::Type::kReference) {
2956     uint32_t arguments_start = /* VarHandle object */ 1u + expected_coordinates_count;
2957     uint32_t number_of_arguments = invoke->GetNumberOfArguments();
2958     for (size_t arg_index = arguments_start; arg_index != number_of_arguments; ++arg_index) {
2959       HInstruction* arg = invoke->InputAt(arg_index);
2960       DCHECK_EQ(arg->GetType(), DataType::Type::kReference);
2961       if (!arg->IsNullConstant()) {
2962         ReferenceTypeInfo arg_type_info = arg->GetReferenceTypeInfo();
2963         if (!arg_type_info.IsValid() ||
2964             !var_type->IsAssignableFrom(arg_type_info.GetTypeHandle().Get())) {
2965           return false;
2966         }
2967       }
2968     }
2969   }
2970 
2971   // Check the first coordinate.
2972   if (expected_coordinates_count != 0u) {
2973     ObjPtr<mirror::Class> coordinate0_type = var_handle->GetCoordinateType0();
2974     DCHECK(coordinate0_type != nullptr);
2975     ReferenceTypeInfo object_type_info = invoke->InputAt(1)->GetReferenceTypeInfo();
2976     if (!object_type_info.IsValid() ||
2977         !coordinate0_type->IsAssignableFrom(object_type_info.GetTypeHandle().Get())) {
2978       return false;
2979     }
2980   }
2981 
2982   // All required checks passed.
2983   return true;
2984 }
2985 
VisitInvoke(HInvoke * instruction)2986 void InstructionSimplifierVisitor::VisitInvoke(HInvoke* instruction) {
2987   switch (instruction->GetIntrinsic()) {
2988     case Intrinsics::kStringEquals:
2989       SimplifyStringEquals(instruction);
2990       break;
2991     case Intrinsics::kSystemArrayCopy:
2992       SimplifySystemArrayCopy(instruction);
2993       break;
2994     case Intrinsics::kFloatFloatToIntBits:
2995     case Intrinsics::kDoubleDoubleToLongBits:
2996       SimplifyFP2Int(instruction);
2997       break;
2998     case Intrinsics::kStringCharAt:
2999       // Instruction builder creates intermediate representation directly
3000       // but the inliner can sharpen CharSequence.charAt() to String.charAt().
3001       SimplifyStringCharAt(instruction);
3002       break;
3003     case Intrinsics::kStringLength:
3004       // Instruction builder creates intermediate representation directly
3005       // but the inliner can sharpen CharSequence.length() to String.length().
3006       SimplifyStringLength(instruction);
3007       break;
3008     case Intrinsics::kStringIndexOf:
3009     case Intrinsics::kStringIndexOfAfter:
3010       SimplifyStringIndexOf(instruction);
3011       break;
3012     case Intrinsics::kStringStringIndexOf:
3013     case Intrinsics::kStringStringIndexOfAfter:
3014       SimplifyNPEOnArgN(instruction, 1);  // 0th has own NullCheck
3015       break;
3016     case Intrinsics::kStringBufferAppend:
3017     case Intrinsics::kStringBuilderAppendObject:
3018     case Intrinsics::kStringBuilderAppendString:
3019     case Intrinsics::kStringBuilderAppendCharSequence:
3020     case Intrinsics::kStringBuilderAppendCharArray:
3021     case Intrinsics::kStringBuilderAppendBoolean:
3022     case Intrinsics::kStringBuilderAppendChar:
3023     case Intrinsics::kStringBuilderAppendInt:
3024     case Intrinsics::kStringBuilderAppendLong:
3025     case Intrinsics::kStringBuilderAppendFloat:
3026     case Intrinsics::kStringBuilderAppendDouble:
3027       SimplifyReturnThis(instruction);
3028       break;
3029     case Intrinsics::kStringBufferToString:
3030     case Intrinsics::kStringBuilderToString:
3031       SimplifyAllocationIntrinsic(instruction);
3032       break;
3033     case Intrinsics::kVarHandleCompareAndExchange:
3034     case Intrinsics::kVarHandleCompareAndExchangeAcquire:
3035     case Intrinsics::kVarHandleCompareAndExchangeRelease:
3036     case Intrinsics::kVarHandleCompareAndSet:
3037     case Intrinsics::kVarHandleGet:
3038     case Intrinsics::kVarHandleGetAcquire:
3039     case Intrinsics::kVarHandleGetAndAdd:
3040     case Intrinsics::kVarHandleGetAndAddAcquire:
3041     case Intrinsics::kVarHandleGetAndAddRelease:
3042     case Intrinsics::kVarHandleGetAndBitwiseAnd:
3043     case Intrinsics::kVarHandleGetAndBitwiseAndAcquire:
3044     case Intrinsics::kVarHandleGetAndBitwiseAndRelease:
3045     case Intrinsics::kVarHandleGetAndBitwiseOr:
3046     case Intrinsics::kVarHandleGetAndBitwiseOrAcquire:
3047     case Intrinsics::kVarHandleGetAndBitwiseOrRelease:
3048     case Intrinsics::kVarHandleGetAndBitwiseXor:
3049     case Intrinsics::kVarHandleGetAndBitwiseXorAcquire:
3050     case Intrinsics::kVarHandleGetAndBitwiseXorRelease:
3051     case Intrinsics::kVarHandleGetAndSet:
3052     case Intrinsics::kVarHandleGetAndSetAcquire:
3053     case Intrinsics::kVarHandleGetAndSetRelease:
3054     case Intrinsics::kVarHandleGetOpaque:
3055     case Intrinsics::kVarHandleGetVolatile:
3056     case Intrinsics::kVarHandleSet:
3057     case Intrinsics::kVarHandleSetOpaque:
3058     case Intrinsics::kVarHandleSetRelease:
3059     case Intrinsics::kVarHandleSetVolatile:
3060     case Intrinsics::kVarHandleWeakCompareAndSet:
3061     case Intrinsics::kVarHandleWeakCompareAndSetAcquire:
3062     case Intrinsics::kVarHandleWeakCompareAndSetPlain:
3063     case Intrinsics::kVarHandleWeakCompareAndSetRelease:
3064       SimplifyVarHandleIntrinsic(instruction);
3065       break;
3066     case Intrinsics::kIntegerRotateRight:
3067     case Intrinsics::kLongRotateRight:
3068     case Intrinsics::kIntegerRotateLeft:
3069     case Intrinsics::kLongRotateLeft:
3070     case Intrinsics::kIntegerCompare:
3071     case Intrinsics::kLongCompare:
3072     case Intrinsics::kIntegerSignum:
3073     case Intrinsics::kLongSignum:
3074     case Intrinsics::kFloatIsNaN:
3075     case Intrinsics::kDoubleIsNaN:
3076     case Intrinsics::kStringIsEmpty:
3077     case Intrinsics::kUnsafeLoadFence:
3078     case Intrinsics::kUnsafeStoreFence:
3079     case Intrinsics::kUnsafeFullFence:
3080     case Intrinsics::kJdkUnsafeLoadFence:
3081     case Intrinsics::kJdkUnsafeStoreFence:
3082     case Intrinsics::kJdkUnsafeFullFence:
3083     case Intrinsics::kVarHandleFullFence:
3084     case Intrinsics::kVarHandleAcquireFence:
3085     case Intrinsics::kVarHandleReleaseFence:
3086     case Intrinsics::kVarHandleLoadLoadFence:
3087     case Intrinsics::kVarHandleStoreStoreFence:
3088     case Intrinsics::kMathMinIntInt:
3089     case Intrinsics::kMathMinLongLong:
3090     case Intrinsics::kMathMinFloatFloat:
3091     case Intrinsics::kMathMinDoubleDouble:
3092     case Intrinsics::kMathMaxIntInt:
3093     case Intrinsics::kMathMaxLongLong:
3094     case Intrinsics::kMathMaxFloatFloat:
3095     case Intrinsics::kMathMaxDoubleDouble:
3096     case Intrinsics::kMathAbsInt:
3097     case Intrinsics::kMathAbsLong:
3098     case Intrinsics::kMathAbsFloat:
3099     case Intrinsics::kMathAbsDouble:
3100       // These are replaced by intermediate representation in the instruction builder.
3101       LOG(FATAL) << "Unexpected " << static_cast<Intrinsics>(instruction->GetIntrinsic());
3102       UNREACHABLE();
3103     default:
3104       break;
3105   }
3106 }
3107 
VisitDeoptimize(HDeoptimize * deoptimize)3108 void InstructionSimplifierVisitor::VisitDeoptimize(HDeoptimize* deoptimize) {
3109   HInstruction* cond = deoptimize->InputAt(0);
3110   if (cond->IsConstant()) {
3111     if (cond->AsIntConstant()->IsFalse()) {
3112       // Never deopt: instruction can be removed.
3113       if (deoptimize->GuardsAnInput()) {
3114         deoptimize->ReplaceWith(deoptimize->GuardedInput());
3115       }
3116       deoptimize->GetBlock()->RemoveInstruction(deoptimize);
3117     } else {
3118       // Always deopt.
3119     }
3120   }
3121 }
3122 
3123 // Replace code looking like
3124 //    OP y, x, const1
3125 //    OP z, y, const2
3126 // with
3127 //    OP z, x, const3
3128 // where OP is both an associative and a commutative operation.
TryHandleAssociativeAndCommutativeOperation(HBinaryOperation * instruction)3129 bool InstructionSimplifierVisitor::TryHandleAssociativeAndCommutativeOperation(
3130     HBinaryOperation* instruction) {
3131   DCHECK(instruction->IsCommutative());
3132 
3133   if (!DataType::IsIntegralType(instruction->GetType())) {
3134     return false;
3135   }
3136 
3137   HInstruction* left = instruction->GetLeft();
3138   HInstruction* right = instruction->GetRight();
3139   // Variable names as described above.
3140   HConstant* const2;
3141   HBinaryOperation* y;
3142 
3143   if (instruction->GetKind() == left->GetKind() && right->IsConstant()) {
3144     const2 = right->AsConstant();
3145     y = left->AsBinaryOperation();
3146   } else if (left->IsConstant() && instruction->GetKind() == right->GetKind()) {
3147     const2 = left->AsConstant();
3148     y = right->AsBinaryOperation();
3149   } else {
3150     // The node does not match the pattern.
3151     return false;
3152   }
3153 
3154   // If `y` has more than one use, we do not perform the optimization
3155   // because it might increase code size (e.g. if the new constant is
3156   // no longer encodable as an immediate operand in the target ISA).
3157   if (!y->HasOnlyOneNonEnvironmentUse()) {
3158     return false;
3159   }
3160 
3161   // GetConstantRight() can return both left and right constants
3162   // for commutative operations.
3163   HConstant* const1 = y->GetConstantRight();
3164   if (const1 == nullptr) {
3165     return false;
3166   }
3167 
3168   instruction->ReplaceInput(const1, 0);
3169   instruction->ReplaceInput(const2, 1);
3170   HConstant* const3 = instruction->TryStaticEvaluation();
3171   DCHECK(const3 != nullptr);
3172   instruction->ReplaceInput(y->GetLeastConstantLeft(), 0);
3173   instruction->ReplaceInput(const3, 1);
3174   RecordSimplification();
3175   return true;
3176 }
3177 
AsAddOrSub(HInstruction * binop)3178 static HBinaryOperation* AsAddOrSub(HInstruction* binop) {
3179   return (binop->IsAdd() || binop->IsSub()) ? binop->AsBinaryOperation() : nullptr;
3180 }
3181 
3182 // Helper function that performs addition statically, considering the result type.
ComputeAddition(DataType::Type type,int64_t x,int64_t y)3183 static int64_t ComputeAddition(DataType::Type type, int64_t x, int64_t y) {
3184   // Use the Compute() method for consistency with TryStaticEvaluation().
3185   if (type == DataType::Type::kInt32) {
3186     return HAdd::Compute<int32_t>(x, y);
3187   } else {
3188     DCHECK_EQ(type, DataType::Type::kInt64);
3189     return HAdd::Compute<int64_t>(x, y);
3190   }
3191 }
3192 
3193 // Helper function that handles the child classes of HConstant
3194 // and returns an integer with the appropriate sign.
GetValue(HConstant * constant,bool is_negated)3195 static int64_t GetValue(HConstant* constant, bool is_negated) {
3196   int64_t ret = Int64FromConstant(constant);
3197   return is_negated ? -ret : ret;
3198 }
3199 
3200 // Replace code looking like
3201 //    OP1 y, x, const1
3202 //    OP2 z, y, const2
3203 // with
3204 //    OP3 z, x, const3
3205 // where OPx is either ADD or SUB, and at least one of OP{1,2} is SUB.
TrySubtractionChainSimplification(HBinaryOperation * instruction)3206 bool InstructionSimplifierVisitor::TrySubtractionChainSimplification(
3207     HBinaryOperation* instruction) {
3208   DCHECK(instruction->IsAdd() || instruction->IsSub()) << instruction->DebugName();
3209 
3210   DataType::Type type = instruction->GetType();
3211   if (!DataType::IsIntegralType(type)) {
3212     return false;
3213   }
3214 
3215   HInstruction* left = instruction->GetLeft();
3216   HInstruction* right = instruction->GetRight();
3217   // Variable names as described above.
3218   HConstant* const2 = right->IsConstant() ? right->AsConstant() : left->AsConstant();
3219   if (const2 == nullptr) {
3220     return false;
3221   }
3222 
3223   HBinaryOperation* y = (AsAddOrSub(left) != nullptr)
3224       ? left->AsBinaryOperation()
3225       : AsAddOrSub(right);
3226   // If y has more than one use, we do not perform the optimization because
3227   // it might increase code size (e.g. if the new constant is no longer
3228   // encodable as an immediate operand in the target ISA).
3229   if ((y == nullptr) || !y->HasOnlyOneNonEnvironmentUse()) {
3230     return false;
3231   }
3232 
3233   left = y->GetLeft();
3234   HConstant* const1 = left->IsConstant() ? left->AsConstant() : y->GetRight()->AsConstant();
3235   if (const1 == nullptr) {
3236     return false;
3237   }
3238 
3239   HInstruction* x = (const1 == left) ? y->GetRight() : left;
3240   // If both inputs are constants, let the constant folding pass deal with it.
3241   if (x->IsConstant()) {
3242     return false;
3243   }
3244 
3245   bool is_const2_negated = (const2 == right) && instruction->IsSub();
3246   int64_t const2_val = GetValue(const2, is_const2_negated);
3247   bool is_y_negated = (y == right) && instruction->IsSub();
3248   right = y->GetRight();
3249   bool is_const1_negated = is_y_negated ^ ((const1 == right) && y->IsSub());
3250   int64_t const1_val = GetValue(const1, is_const1_negated);
3251   bool is_x_negated = is_y_negated ^ ((x == right) && y->IsSub());
3252   int64_t const3_val = ComputeAddition(type, const1_val, const2_val);
3253   HBasicBlock* block = instruction->GetBlock();
3254   HConstant* const3 = block->GetGraph()->GetConstant(type, const3_val);
3255   ArenaAllocator* allocator = instruction->GetAllocator();
3256   HInstruction* z;
3257 
3258   if (is_x_negated) {
3259     z = new (allocator) HSub(type, const3, x, instruction->GetDexPc());
3260   } else {
3261     z = new (allocator) HAdd(type, x, const3, instruction->GetDexPc());
3262   }
3263 
3264   block->ReplaceAndRemoveInstructionWith(instruction, z);
3265   RecordSimplification();
3266   return true;
3267 }
3268 
VisitVecMul(HVecMul * instruction)3269 void InstructionSimplifierVisitor::VisitVecMul(HVecMul* instruction) {
3270   if (TryCombineVecMultiplyAccumulate(instruction)) {
3271     RecordSimplification();
3272   }
3273 }
3274 
3275 }  // namespace art
3276