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