• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "instruction_simplifier.h"
18 
19 #include "art_method-inl.h"
20 #include "class_linker-inl.h"
21 #include "class_root-inl.h"
22 #include "data_type-inl.h"
23 #include "driver/compiler_options.h"
24 #include "escape.h"
25 #include "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 = 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 = WellKnownClasses::java_lang_Object_shadowKlass;
1804     if (field_get->GetFieldInfo().GetField() != field) {
1805       return false;
1806     }
1807 
1808     // We can replace the compare.
1809     int value = 0;
1810     if (receiver_type.IsEqual(class_rti)) {
1811       value = condition->IsEqual() ? 1 : 0;
1812     } else {
1813       value = condition->IsNotEqual() ? 1 : 0;
1814     }
1815     condition->ReplaceWith(condition->GetBlock()->GetGraph()->GetIntConstant(value));
1816     return true;
1817   }
1818 }
1819 
CreateUnsignedConditionReplacement(ArenaAllocator * allocator,HCondition * cond,HCompare * compare)1820 static HInstruction* CreateUnsignedConditionReplacement(ArenaAllocator* allocator,
1821                                                         HCondition* cond,
1822                                                         HCompare* compare) {
1823   DCHECK(cond->InputAt(1)->IsIntConstant());
1824   DCHECK_EQ(cond->InputAt(1)->AsIntConstant()->GetValue(), 0);
1825   DCHECK(cond->InputAt(0) == compare);
1826 
1827   HBasicBlock* block = cond->GetBlock();
1828   HInstruction* lhs = compare->InputAt(0);
1829   HInstruction* rhs = compare->InputAt(1);
1830 
1831   switch (cond->GetKind()) {
1832     case HInstruction::kLessThan:
1833       return new (allocator) HBelow(lhs, rhs, cond->GetDexPc());
1834     case HInstruction::kLessThanOrEqual:
1835       return new (allocator) HBelowOrEqual(lhs, rhs, cond->GetDexPc());
1836     case HInstruction::kGreaterThan:
1837       return new (allocator) HAbove(lhs, rhs, cond->GetDexPc());
1838     case HInstruction::kGreaterThanOrEqual:
1839       return new (allocator) HAboveOrEqual(lhs, rhs, cond->GetDexPc());
1840     case HInstruction::kBelow:
1841       // Below(Compare(x, y), 0) always False since
1842       //   unsigned(-1) < 0 -> False
1843       //   0 < 0 -> False
1844       //   1 < 0 -> False
1845       return block->GetGraph()->GetConstant(DataType::Type::kBool, 0);
1846     case HInstruction::kBelowOrEqual:
1847       // BelowOrEqual(Compare(x, y), 0) transforms into Equal(x, y)
1848       //    unsigned(-1) <= 0 -> False
1849       //    0 <= 0 -> True
1850       //    1 <= 0 -> False
1851       return new (allocator) HEqual(lhs, rhs, cond->GetDexPc());
1852     case HInstruction::kAbove:
1853       // Above(Compare(x, y), 0) transforms into NotEqual(x, y)
1854       //    unsigned(-1) > 0 -> True
1855       //    0 > 0 -> False
1856       //    1 > 0 -> True
1857       return new (allocator) HNotEqual(lhs, rhs, cond->GetDexPc());
1858     case HInstruction::kAboveOrEqual:
1859       // AboveOrEqual(Compare(x, y), 0) always True since
1860       //   unsigned(-1) >= 0 -> True
1861       //   0 >= 0 -> True
1862       //   1 >= 0 -> True
1863       return block->GetGraph()->GetConstant(DataType::Type::kBool, 1);
1864     default:
1865       LOG(FATAL) << "Unknown ConditionType " << cond->GetKind();
1866       UNREACHABLE();
1867   }
1868 }
1869 
VisitCondition(HCondition * condition)1870 void InstructionSimplifierVisitor::VisitCondition(HCondition* condition) {
1871   if (condition->IsEqual() || condition->IsNotEqual()) {
1872     if (RecognizeAndSimplifyClassCheck(condition)) {
1873       return;
1874     }
1875   }
1876 
1877   // Reverse condition if left is constant. Our code generators prefer constant
1878   // on the right hand side.
1879   HBasicBlock* block = condition->GetBlock();
1880   HInstruction* left = condition->GetLeft();
1881   HInstruction* right = condition->GetRight();
1882   if (left->IsConstant() && !right->IsConstant()) {
1883     IfCondition new_cond = GetOppositeConditionForOperandSwap(condition->GetCondition());
1884     HCondition* replacement = HCondition::Create(GetGraph(), new_cond, right, left);
1885     block->ReplaceAndRemoveInstructionWith(condition, replacement);
1886     // If it is a FP condition, we must set the opposite bias.
1887     if (condition->IsLtBias()) {
1888       replacement->SetBias(ComparisonBias::kGtBias);
1889     } else if (condition->IsGtBias()) {
1890       replacement->SetBias(ComparisonBias::kLtBias);
1891     }
1892     RecordSimplification();
1893     condition = replacement;
1894     std::swap(left, right);
1895   }
1896 
1897   // Try to fold an HCompare into this HCondition.
1898 
1899   // We can only replace an HCondition which compares a Compare to 0.
1900   // Both 'dx' and 'jack' generate a compare to 0 when compiling a
1901   // condition with a long, float or double comparison as input.
1902   if (!left->IsCompare() || !right->IsConstant() || right->AsIntConstant()->GetValue() != 0) {
1903     // Conversion is not possible.
1904     return;
1905   }
1906 
1907   // Is the Compare only used for this purpose?
1908   if (!left->GetUses().HasExactlyOneElement()) {
1909     // Someone else also wants the result of the compare.
1910     return;
1911   }
1912 
1913   if (!left->GetEnvUses().empty()) {
1914     // There is a reference to the compare result in an environment. Do we really need it?
1915     if (GetGraph()->IsDebuggable()) {
1916       return;
1917     }
1918 
1919     // We have to ensure that there are no deopt points in the sequence.
1920     if (left->HasAnyEnvironmentUseBefore(condition)) {
1921       return;
1922     }
1923   }
1924 
1925   // Clean up any environment uses from the HCompare, if any.
1926   left->RemoveEnvironmentUsers();
1927 
1928   // We have decided to fold the HCompare into the HCondition. Transfer the information.
1929   if (DataType::IsUnsignedType(left->AsCompare()->GetComparisonType()) &&
1930       !condition->IsEqual() &&
1931       !condition->IsNotEqual()) {
1932     DCHECK_EQ(condition->GetBias(), ComparisonBias::kNoBias);
1933     HInstruction* replacement = CreateUnsignedConditionReplacement(
1934         block->GetGraph()->GetAllocator(), condition, left->AsCompare());
1935 
1936     if (replacement->IsConstant()) {
1937       condition->ReplaceWith(replacement);
1938       block->RemoveInstruction(condition);
1939     } else {
1940       block->ReplaceAndRemoveInstructionWith(condition, replacement);
1941     }
1942   } else {
1943     condition->SetBias(left->AsCompare()->GetBias());
1944 
1945     // Replace the operands of the HCondition.
1946     condition->ReplaceInput(left->InputAt(0), 0);
1947     condition->ReplaceInput(left->InputAt(1), 1);
1948   }
1949 
1950   // Remove the HCompare.
1951   left->GetBlock()->RemoveInstruction(left);
1952 
1953   RecordSimplification();
1954 }
1955 
CheckSignedToUnsignedCompareConversion(HInstruction * operand,HCompare * compare)1956 static HInstruction* CheckSignedToUnsignedCompareConversion(HInstruction* operand,
1957                                                             HCompare* compare) {
1958   // Check if operand looks like `ADD op, MIN_INTEGRAL`
1959   if (operand->IsConstant()) {
1960     // CONSTANT #x -> CONSTANT #(x - MIN_INTEGRAL)
1961     HConstant* constant = operand->AsConstant();
1962     if (constant->IsIntConstant()) {
1963       HIntConstant* int_constant = constant->AsIntConstant();
1964       int32_t old_value = int_constant->GetValue();
1965       int32_t new_value = old_value - std::numeric_limits<int32_t>::min();
1966       return operand->GetBlock()->GetGraph()->GetIntConstant(new_value);
1967     } else if (constant->IsLongConstant()) {
1968       HLongConstant* long_constant = constant->AsLongConstant();
1969       int64_t old_value = long_constant->GetValue();
1970       int64_t new_value = old_value - std::numeric_limits<int64_t>::min();
1971       return operand->GetBlock()->GetGraph()->GetLongConstant(new_value);
1972     } else {
1973       return nullptr;
1974     }
1975   }
1976 
1977   if (!operand->IsAdd() && !operand->IsXor()) {
1978     return nullptr;
1979   }
1980 
1981   if (!operand->GetEnvUses().empty()) {
1982     // There is a reference to the compare result in an environment. Do we really need it?
1983     if (operand->GetBlock()->GetGraph()->IsDebuggable()) {
1984       return nullptr;
1985     }
1986 
1987     // We have to ensure that there are no deopt points in the sequence.
1988     if (operand->HasAnyEnvironmentUseBefore(compare)) {
1989       return nullptr;
1990     }
1991   }
1992 
1993   HBinaryOperation* additive_operand = operand->AsBinaryOperation();
1994 
1995   HInstruction* left = additive_operand->GetLeft();
1996   HInstruction* right = additive_operand->GetRight();
1997 
1998   HConstant* constant = nullptr;
1999   HInstruction* value = nullptr;
2000 
2001   if (left->IsConstant() && !right->IsConstant()) {
2002     constant = left->AsConstant();
2003     value = right;
2004   } else if (!left->IsConstant() && right->IsConstant()) {
2005     value = left;
2006     constant = right->AsConstant();
2007   } else {
2008     return nullptr;
2009   }
2010 
2011   if (constant->IsIntConstant()) {
2012     HIntConstant* int_constant = constant->AsIntConstant();
2013     if (int_constant->GetValue() != std::numeric_limits<int32_t>::min()) {
2014       return nullptr;
2015     }
2016   } else if (constant->IsLongConstant()) {
2017     HLongConstant* long_constant = constant->AsLongConstant();
2018     if (long_constant->GetValue() != std::numeric_limits<int64_t>::min()) {
2019       return nullptr;
2020     }
2021   } else {
2022     return nullptr;
2023   }
2024 
2025   return value;
2026 }
2027 
GetOpositeSignType(DataType::Type type)2028 static DataType::Type GetOpositeSignType(DataType::Type type) {
2029   return DataType::IsUnsignedType(type) ? DataType::ToSigned(type) : DataType::ToUnsigned(type);
2030 }
2031 
VisitCompare(HCompare * compare)2032 void InstructionSimplifierVisitor::VisitCompare(HCompare* compare) {
2033   // Transform signed compare into unsigned if possible
2034   // Replace code looking like
2035   //    ADD normalizedLeft, left, MIN_INTEGRAL
2036   //    ADD normalizedRight, right, MIN_INTEGRAL
2037   //    COMPARE normalizedLeft, normalizedRight, sign
2038   // with
2039   //    COMPARE left, right, !sign
2040 
2041   if (!DataType::IsIntegralType(compare->GetComparisonType())) {
2042     return;
2043   }
2044 
2045   HInstruction* compare_left = compare->GetLeft();
2046   HInstruction* compare_right = compare->GetRight();
2047 
2048   if (compare_left->IsConstant() && compare_right->IsConstant()) {
2049     // Do not simplify, let it be folded.
2050     return;
2051   }
2052 
2053   HInstruction* left = CheckSignedToUnsignedCompareConversion(compare_left, compare);
2054   if (left == nullptr) {
2055     return;
2056   }
2057 
2058   HInstruction* right = CheckSignedToUnsignedCompareConversion(compare_right, compare);
2059   if (right == nullptr) {
2060     return;
2061   }
2062 
2063   compare->SetComparisonType(GetOpositeSignType(compare->GetComparisonType()));
2064   compare->ReplaceInput(left, 0);
2065   compare->ReplaceInput(right, 1);
2066 
2067   RecordSimplification();
2068 
2069   if (compare_left->GetUses().empty()) {
2070     compare_left->RemoveEnvironmentUsers();
2071     compare_left->GetBlock()->RemoveInstruction(compare_left);
2072   }
2073 
2074   if (compare_right->GetUses().empty()) {
2075     compare_right->RemoveEnvironmentUsers();
2076     compare_right->GetBlock()->RemoveInstruction(compare_right);
2077   }
2078 }
2079 
2080 // Return whether x / divisor == x * (1.0f / divisor), for every float x.
CanDivideByReciprocalMultiplyFloat(int32_t divisor)2081 static constexpr bool CanDivideByReciprocalMultiplyFloat(int32_t divisor) {
2082   // True, if the most significant bits of divisor are 0.
2083   return ((divisor & 0x7fffff) == 0);
2084 }
2085 
2086 // Return whether x / divisor == x * (1.0 / divisor), for every double x.
CanDivideByReciprocalMultiplyDouble(int64_t divisor)2087 static constexpr bool CanDivideByReciprocalMultiplyDouble(int64_t divisor) {
2088   // True, if the most significant bits of divisor are 0.
2089   return ((divisor & ((UINT64_C(1) << 52) - 1)) == 0);
2090 }
2091 
VisitDiv(HDiv * instruction)2092 void InstructionSimplifierVisitor::VisitDiv(HDiv* instruction) {
2093   HConstant* input_cst = instruction->GetConstantRight();
2094   HInstruction* input_other = instruction->GetLeastConstantLeft();
2095   DataType::Type type = instruction->GetType();
2096 
2097   if ((input_cst != nullptr) && input_cst->IsOne()) {
2098     // Replace code looking like
2099     //    DIV dst, src, 1
2100     // with
2101     //    src
2102     instruction->ReplaceWith(input_other);
2103     instruction->GetBlock()->RemoveInstruction(instruction);
2104     RecordSimplification();
2105     return;
2106   }
2107 
2108   if ((input_cst != nullptr) && input_cst->IsMinusOne()) {
2109     // Replace code looking like
2110     //    DIV dst, src, -1
2111     // with
2112     //    NEG dst, src
2113     instruction->GetBlock()->ReplaceAndRemoveInstructionWith(
2114         instruction, new (GetGraph()->GetAllocator()) HNeg(type, input_other));
2115     RecordSimplification();
2116     return;
2117   }
2118 
2119   if ((input_cst != nullptr) && DataType::IsFloatingPointType(type)) {
2120     // Try replacing code looking like
2121     //    DIV dst, src, constant
2122     // with
2123     //    MUL dst, src, 1 / constant
2124     HConstant* reciprocal = nullptr;
2125     if (type == DataType::Type::kFloat64) {
2126       double value = input_cst->AsDoubleConstant()->GetValue();
2127       if (CanDivideByReciprocalMultiplyDouble(bit_cast<int64_t, double>(value))) {
2128         reciprocal = GetGraph()->GetDoubleConstant(1.0 / value);
2129       }
2130     } else {
2131       DCHECK_EQ(type, DataType::Type::kFloat32);
2132       float value = input_cst->AsFloatConstant()->GetValue();
2133       if (CanDivideByReciprocalMultiplyFloat(bit_cast<int32_t, float>(value))) {
2134         reciprocal = GetGraph()->GetFloatConstant(1.0f / value);
2135       }
2136     }
2137 
2138     if (reciprocal != nullptr) {
2139       instruction->GetBlock()->ReplaceAndRemoveInstructionWith(
2140           instruction, new (GetGraph()->GetAllocator()) HMul(type, input_other, reciprocal));
2141       RecordSimplification();
2142       return;
2143     }
2144   }
2145 }
2146 
2147 
2148 // Search HDiv having the specified dividend and divisor which is in the specified basic block.
2149 // Return nullptr if nothing has been found.
FindDivWithInputsInBasicBlock(HInstruction * dividend,HInstruction * divisor,HBasicBlock * basic_block)2150 static HDiv* FindDivWithInputsInBasicBlock(HInstruction* dividend,
2151                                            HInstruction* divisor,
2152                                            HBasicBlock* basic_block) {
2153   for (const HUseListNode<HInstruction*>& use : dividend->GetUses()) {
2154     HInstruction* user = use.GetUser();
2155     if (user->GetBlock() == basic_block &&
2156         user->IsDiv() &&
2157         user->InputAt(0) == dividend &&
2158         user->InputAt(1) == divisor) {
2159       return user->AsDiv();
2160     }
2161   }
2162   return nullptr;
2163 }
2164 
2165 // If there is Div with the same inputs as Rem and in the same basic block, it can be reused.
2166 // Rem is replaced with Mul+Sub which use the found Div.
TryToReuseDiv(HRem * rem)2167 void InstructionSimplifierVisitor::TryToReuseDiv(HRem* rem) {
2168   // As the optimization replaces Rem with Mul+Sub they prevent some loop optimizations
2169   // if the Rem is in a loop.
2170   // Check if it is allowed to optimize such Rems.
2171   if (rem->IsInLoop() && be_loop_friendly_) {
2172     return;
2173   }
2174   DataType::Type type = rem->GetResultType();
2175   if (!DataType::IsIntOrLongType(type)) {
2176     return;
2177   }
2178 
2179   HBasicBlock* basic_block = rem->GetBlock();
2180   HInstruction* dividend = rem->GetLeft();
2181   HInstruction* divisor = rem->GetRight();
2182 
2183   if (divisor->IsConstant()) {
2184     HConstant* input_cst = rem->GetConstantRight();
2185     DCHECK(input_cst->IsIntConstant() || input_cst->IsLongConstant());
2186     int64_t cst_value = Int64FromConstant(input_cst);
2187     if (cst_value == std::numeric_limits<int64_t>::min() || IsPowerOfTwo(std::abs(cst_value))) {
2188       // Such cases are usually handled in the code generator because they don't need Div at all.
2189       return;
2190     }
2191   }
2192 
2193   HDiv* quotient = FindDivWithInputsInBasicBlock(dividend, divisor, basic_block);
2194   if (quotient == nullptr) {
2195     return;
2196   }
2197   if (!quotient->StrictlyDominates(rem)) {
2198     quotient->MoveBefore(rem);
2199   }
2200 
2201   ArenaAllocator* allocator = GetGraph()->GetAllocator();
2202   HInstruction* mul = new (allocator) HMul(type, quotient, divisor);
2203   basic_block->InsertInstructionBefore(mul, rem);
2204   HInstruction* sub = new (allocator) HSub(type, dividend, mul);
2205   basic_block->InsertInstructionBefore(sub, rem);
2206   rem->ReplaceWith(sub);
2207   basic_block->RemoveInstruction(rem);
2208   RecordSimplification();
2209 }
2210 
VisitRem(HRem * rem)2211 void InstructionSimplifierVisitor::VisitRem(HRem* rem) {
2212   TryToReuseDiv(rem);
2213 }
2214 
VisitMul(HMul * instruction)2215 void InstructionSimplifierVisitor::VisitMul(HMul* instruction) {
2216   HConstant* input_cst = instruction->GetConstantRight();
2217   HInstruction* input_other = instruction->GetLeastConstantLeft();
2218   DataType::Type type = instruction->GetType();
2219   HBasicBlock* block = instruction->GetBlock();
2220   ArenaAllocator* allocator = GetGraph()->GetAllocator();
2221 
2222   if (input_cst == nullptr) {
2223     return;
2224   }
2225 
2226   if (input_cst->IsOne()) {
2227     // Replace code looking like
2228     //    MUL dst, src, 1
2229     // with
2230     //    src
2231     instruction->ReplaceWith(input_other);
2232     instruction->GetBlock()->RemoveInstruction(instruction);
2233     RecordSimplification();
2234     return;
2235   }
2236 
2237   if (input_cst->IsMinusOne() &&
2238       (DataType::IsFloatingPointType(type) || DataType::IsIntOrLongType(type))) {
2239     // Replace code looking like
2240     //    MUL dst, src, -1
2241     // with
2242     //    NEG dst, src
2243     HNeg* neg = new (allocator) HNeg(type, input_other);
2244     block->ReplaceAndRemoveInstructionWith(instruction, neg);
2245     RecordSimplification();
2246     return;
2247   }
2248 
2249   if (DataType::IsFloatingPointType(type) &&
2250       ((input_cst->IsFloatConstant() && input_cst->AsFloatConstant()->GetValue() == 2.0f) ||
2251        (input_cst->IsDoubleConstant() && input_cst->AsDoubleConstant()->GetValue() == 2.0))) {
2252     // Replace code looking like
2253     //    FP_MUL dst, src, 2.0
2254     // with
2255     //    FP_ADD dst, src, src
2256     // The 'int' and 'long' cases are handled below.
2257     block->ReplaceAndRemoveInstructionWith(instruction,
2258                                            new (allocator) HAdd(type, input_other, input_other));
2259     RecordSimplification();
2260     return;
2261   }
2262 
2263   if (DataType::IsIntOrLongType(type)) {
2264     int64_t factor = Int64FromConstant(input_cst);
2265     // Even though constant propagation also takes care of the zero case, other
2266     // optimizations can lead to having a zero multiplication.
2267     if (factor == 0) {
2268       // Replace code looking like
2269       //    MUL dst, src, 0
2270       // with
2271       //    0
2272       instruction->ReplaceWith(input_cst);
2273       instruction->GetBlock()->RemoveInstruction(instruction);
2274       RecordSimplification();
2275       return;
2276     } else if (IsPowerOfTwo(factor)) {
2277       // Replace code looking like
2278       //    MUL dst, src, pow_of_2
2279       // with
2280       //    SHL dst, src, log2(pow_of_2)
2281       HIntConstant* shift = GetGraph()->GetIntConstant(WhichPowerOf2(factor));
2282       HShl* shl = new (allocator) HShl(type, input_other, shift);
2283       block->ReplaceAndRemoveInstructionWith(instruction, shl);
2284       RecordSimplification();
2285       return;
2286     } else if (IsPowerOfTwo(factor - 1)) {
2287       // Transform code looking like
2288       //    MUL dst, src, (2^n + 1)
2289       // into
2290       //    SHL tmp, src, n
2291       //    ADD dst, src, tmp
2292       HShl* shl = new (allocator) HShl(type,
2293                                        input_other,
2294                                        GetGraph()->GetIntConstant(WhichPowerOf2(factor - 1)));
2295       HAdd* add = new (allocator) HAdd(type, input_other, shl);
2296 
2297       block->InsertInstructionBefore(shl, instruction);
2298       block->ReplaceAndRemoveInstructionWith(instruction, add);
2299       RecordSimplification();
2300       return;
2301     } else if (IsPowerOfTwo(factor + 1)) {
2302       // Transform code looking like
2303       //    MUL dst, src, (2^n - 1)
2304       // into
2305       //    SHL tmp, src, n
2306       //    SUB dst, tmp, src
2307       HShl* shl = new (allocator) HShl(type,
2308                                        input_other,
2309                                        GetGraph()->GetIntConstant(WhichPowerOf2(factor + 1)));
2310       HSub* sub = new (allocator) HSub(type, shl, input_other);
2311 
2312       block->InsertInstructionBefore(shl, instruction);
2313       block->ReplaceAndRemoveInstructionWith(instruction, sub);
2314       RecordSimplification();
2315       return;
2316     }
2317   }
2318 
2319   // TryHandleAssociativeAndCommutativeOperation() does not remove its input,
2320   // so no need to return.
2321   TryHandleAssociativeAndCommutativeOperation(instruction);
2322 }
2323 
VisitNeg(HNeg * instruction)2324 void InstructionSimplifierVisitor::VisitNeg(HNeg* instruction) {
2325   HInstruction* input = instruction->GetInput();
2326   if (input->IsNeg()) {
2327     // Replace code looking like
2328     //    NEG tmp, src
2329     //    NEG dst, tmp
2330     // with
2331     //    src
2332     HNeg* previous_neg = input->AsNeg();
2333     instruction->ReplaceWith(previous_neg->GetInput());
2334     instruction->GetBlock()->RemoveInstruction(instruction);
2335     // We perform the optimization even if the input negation has environment
2336     // uses since it allows removing the current instruction. But we only delete
2337     // the input negation only if it is does not have any uses left.
2338     if (!previous_neg->HasUses()) {
2339       previous_neg->GetBlock()->RemoveInstruction(previous_neg);
2340     }
2341     RecordSimplification();
2342     return;
2343   }
2344 
2345   if (input->IsSub() && input->HasOnlyOneNonEnvironmentUse() &&
2346       !DataType::IsFloatingPointType(input->GetType())) {
2347     // Replace code looking like
2348     //    SUB tmp, a, b
2349     //    NEG dst, tmp
2350     // with
2351     //    SUB dst, b, a
2352     // We do not perform the optimization if the input subtraction has
2353     // environment uses or multiple non-environment uses as it could lead to
2354     // worse code. In particular, we do not want the live ranges of `a` and `b`
2355     // to be extended if we are not sure the initial 'SUB' instruction can be
2356     // removed.
2357     // We do not perform optimization for fp because we could lose the sign of zero.
2358     HSub* sub = input->AsSub();
2359     HSub* new_sub = new (GetGraph()->GetAllocator()) HSub(
2360         instruction->GetType(), sub->GetRight(), sub->GetLeft());
2361     instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, new_sub);
2362     if (!sub->HasUses()) {
2363       sub->GetBlock()->RemoveInstruction(sub);
2364     }
2365     RecordSimplification();
2366   }
2367 }
2368 
VisitNot(HNot * instruction)2369 void InstructionSimplifierVisitor::VisitNot(HNot* instruction) {
2370   HInstruction* input = instruction->GetInput();
2371   if (input->IsNot()) {
2372     // Replace code looking like
2373     //    NOT tmp, src
2374     //    NOT dst, tmp
2375     // with
2376     //    src
2377     // We perform the optimization even if the input negation has environment
2378     // uses since it allows removing the current instruction. But we only delete
2379     // the input negation only if it is does not have any uses left.
2380     HNot* previous_not = input->AsNot();
2381     instruction->ReplaceWith(previous_not->GetInput());
2382     instruction->GetBlock()->RemoveInstruction(instruction);
2383     if (!previous_not->HasUses()) {
2384       previous_not->GetBlock()->RemoveInstruction(previous_not);
2385     }
2386     RecordSimplification();
2387   }
2388 }
2389 
VisitOr(HOr * instruction)2390 void InstructionSimplifierVisitor::VisitOr(HOr* instruction) {
2391   HConstant* input_cst = instruction->GetConstantRight();
2392   HInstruction* input_other = instruction->GetLeastConstantLeft();
2393 
2394   if ((input_cst != nullptr) && input_cst->IsZeroBitPattern()) {
2395     // Replace code looking like
2396     //    OR dst, src, 0
2397     // with
2398     //    src
2399     instruction->ReplaceWith(input_other);
2400     instruction->GetBlock()->RemoveInstruction(instruction);
2401     RecordSimplification();
2402     return;
2403   }
2404 
2405   // We assume that GVN has run before, so we only perform a pointer comparison.
2406   // If for some reason the values are equal but the pointers are different, we
2407   // are still correct and only miss an optimization opportunity.
2408   if (instruction->GetLeft() == instruction->GetRight()) {
2409     // Replace code looking like
2410     //    OR dst, src, src
2411     // with
2412     //    src
2413     instruction->ReplaceWith(instruction->GetLeft());
2414     instruction->GetBlock()->RemoveInstruction(instruction);
2415     RecordSimplification();
2416     return;
2417   }
2418 
2419   if (TryDeMorganNegationFactoring(instruction)) return;
2420 
2421   if (TryReplaceWithRotate(instruction)) {
2422     return;
2423   }
2424 
2425   // TryHandleAssociativeAndCommutativeOperation() does not remove its input,
2426   // so no need to return.
2427   TryHandleAssociativeAndCommutativeOperation(instruction);
2428 }
2429 
VisitShl(HShl * instruction)2430 void InstructionSimplifierVisitor::VisitShl(HShl* instruction) {
2431   VisitShift(instruction);
2432 }
2433 
VisitShr(HShr * instruction)2434 void InstructionSimplifierVisitor::VisitShr(HShr* instruction) {
2435   VisitShift(instruction);
2436 }
2437 
VisitSub(HSub * instruction)2438 void InstructionSimplifierVisitor::VisitSub(HSub* instruction) {
2439   HConstant* input_cst = instruction->GetConstantRight();
2440   HInstruction* input_other = instruction->GetLeastConstantLeft();
2441 
2442   DataType::Type type = instruction->GetType();
2443   if (DataType::IsFloatingPointType(type)) {
2444     return;
2445   }
2446 
2447   if ((input_cst != nullptr) && input_cst->IsArithmeticZero()) {
2448     // Replace code looking like
2449     //    SUB dst, src, 0
2450     // with
2451     //    src
2452     // Note that we cannot optimize `x - 0.0` to `x` for floating-point. When
2453     // `x` is `-0.0`, the former expression yields `0.0`, while the later
2454     // yields `-0.0`.
2455     instruction->ReplaceWith(input_other);
2456     instruction->GetBlock()->RemoveInstruction(instruction);
2457     RecordSimplification();
2458     return;
2459   }
2460 
2461   HBasicBlock* block = instruction->GetBlock();
2462   ArenaAllocator* allocator = GetGraph()->GetAllocator();
2463 
2464   HInstruction* left = instruction->GetLeft();
2465   HInstruction* right = instruction->GetRight();
2466   if (left->IsConstant()) {
2467     if (Int64FromConstant(left->AsConstant()) == 0) {
2468       // Replace code looking like
2469       //    SUB dst, 0, src
2470       // with
2471       //    NEG dst, src
2472       // Note that we cannot optimize `0.0 - x` to `-x` for floating-point. When
2473       // `x` is `0.0`, the former expression yields `0.0`, while the later
2474       // yields `-0.0`.
2475       HNeg* neg = new (allocator) HNeg(type, right);
2476       block->ReplaceAndRemoveInstructionWith(instruction, neg);
2477       RecordSimplification();
2478       return;
2479     }
2480   }
2481 
2482   if (left->IsNeg() && right->IsNeg()) {
2483     if (TryMoveNegOnInputsAfterBinop(instruction)) {
2484       return;
2485     }
2486   }
2487 
2488   if (right->IsNeg() && right->HasOnlyOneNonEnvironmentUse()) {
2489     // Replace code looking like
2490     //    NEG tmp, b
2491     //    SUB dst, a, tmp
2492     // with
2493     //    ADD dst, a, b
2494     HAdd* add = new(GetGraph()->GetAllocator()) HAdd(type, left, right->AsNeg()->GetInput());
2495     instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, add);
2496     RecordSimplification();
2497     right->GetBlock()->RemoveInstruction(right);
2498     return;
2499   }
2500 
2501   if (left->IsNeg() && left->HasOnlyOneNonEnvironmentUse()) {
2502     // Replace code looking like
2503     //    NEG tmp, a
2504     //    SUB dst, tmp, b
2505     // with
2506     //    ADD tmp, a, b
2507     //    NEG dst, tmp
2508     // The second version is not intrinsically better, but enables more
2509     // transformations.
2510     HAdd* add = new(GetGraph()->GetAllocator()) HAdd(type, left->AsNeg()->GetInput(), right);
2511     instruction->GetBlock()->InsertInstructionBefore(add, instruction);
2512     HNeg* neg = new (GetGraph()->GetAllocator()) HNeg(instruction->GetType(), add);
2513     instruction->GetBlock()->InsertInstructionBefore(neg, instruction);
2514     instruction->ReplaceWith(neg);
2515     instruction->GetBlock()->RemoveInstruction(instruction);
2516     RecordSimplification();
2517     left->GetBlock()->RemoveInstruction(left);
2518     return;
2519   }
2520 
2521   if (TrySubtractionChainSimplification(instruction)) {
2522     return;
2523   }
2524 
2525   if (left->IsAdd()) {
2526     // Cases (x + y) - y = x, and (x + y) - x = y.
2527     // Replace code patterns looking like
2528     //    ADD dst1, x, y        ADD dst1, x, y
2529     //    SUB dst2, dst1, y     SUB dst2, dst1, x
2530     // with
2531     //    ADD dst1, x, y
2532     // SUB instruction is not needed in this case, we may use
2533     // one of inputs of ADD instead.
2534     // It is applicable to integral types only.
2535     HAdd* add = left->AsAdd();
2536     DCHECK(DataType::IsIntegralType(type));
2537     if (add->GetRight() == right) {
2538       instruction->ReplaceWith(add->GetLeft());
2539       RecordSimplification();
2540       instruction->GetBlock()->RemoveInstruction(instruction);
2541       return;
2542     } else if (add->GetLeft() == right) {
2543       instruction->ReplaceWith(add->GetRight());
2544       RecordSimplification();
2545       instruction->GetBlock()->RemoveInstruction(instruction);
2546       return;
2547     }
2548   } else if (right->IsAdd()) {
2549     // Cases y - (x + y) = -x, and  x - (x + y) = -y.
2550     // Replace code patterns looking like
2551     //    ADD dst1, x, y        ADD dst1, x, y
2552     //    SUB dst2, y, dst1     SUB dst2, x, dst1
2553     // with
2554     //    ADD dst1, x, y        ADD dst1, x, y
2555     //    NEG x                 NEG y
2556     // SUB instruction is not needed in this case, we may use
2557     // one of inputs of ADD instead with a NEG.
2558     // It is applicable to integral types only.
2559     HAdd* add = right->AsAdd();
2560     DCHECK(DataType::IsIntegralType(type));
2561     if (add->GetRight() == left) {
2562       HNeg* neg = new (GetGraph()->GetAllocator()) HNeg(add->GetType(), add->GetLeft());
2563       instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, neg);
2564       RecordSimplification();
2565       return;
2566     } else if (add->GetLeft() == left) {
2567       HNeg* neg = new (GetGraph()->GetAllocator()) HNeg(add->GetType(), add->GetRight());
2568       instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, neg);
2569       RecordSimplification();
2570       return;
2571     }
2572   } else if (left->IsSub()) {
2573     // Case (x - y) - x = -y.
2574     // Replace code patterns looking like
2575     //    SUB dst1, x, y
2576     //    SUB dst2, dst1, x
2577     // with
2578     //    SUB dst1, x, y
2579     //    NEG y
2580     // The second SUB is not needed in this case, we may use the second input of the first SUB
2581     // instead with a NEG.
2582     // It is applicable to integral types only.
2583     HSub* sub = left->AsSub();
2584     DCHECK(DataType::IsIntegralType(type));
2585     if (sub->GetLeft() == right) {
2586       HNeg* neg = new (GetGraph()->GetAllocator()) HNeg(sub->GetType(), sub->GetRight());
2587       instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, neg);
2588       RecordSimplification();
2589       return;
2590     }
2591   } else if (right->IsSub()) {
2592     // Case x - (x - y) = y.
2593     // Replace code patterns looking like
2594     //    SUB dst1, x, y
2595     //    SUB dst2, x, dst1
2596     // with
2597     //    SUB dst1, x, y
2598     // The second SUB is not needed in this case, we may use the second input of the first SUB.
2599     // It is applicable to integral types only.
2600     HSub* sub = right->AsSub();
2601     DCHECK(DataType::IsIntegralType(type));
2602     if (sub->GetLeft() == left) {
2603       instruction->ReplaceWith(sub->GetRight());
2604       RecordSimplification();
2605       instruction->GetBlock()->RemoveInstruction(instruction);
2606       return;
2607     }
2608   }
2609 }
2610 
VisitUShr(HUShr * instruction)2611 void InstructionSimplifierVisitor::VisitUShr(HUShr* instruction) {
2612   VisitShift(instruction);
2613 }
2614 
VisitXor(HXor * instruction)2615 void InstructionSimplifierVisitor::VisitXor(HXor* instruction) {
2616   HConstant* input_cst = instruction->GetConstantRight();
2617   HInstruction* input_other = instruction->GetLeastConstantLeft();
2618 
2619   if ((input_cst != nullptr) && input_cst->IsZeroBitPattern()) {
2620     // Replace code looking like
2621     //    XOR dst, src, 0
2622     // with
2623     //    src
2624     instruction->ReplaceWith(input_other);
2625     instruction->GetBlock()->RemoveInstruction(instruction);
2626     RecordSimplification();
2627     return;
2628   }
2629 
2630   if ((input_cst != nullptr) && input_cst->IsOne()
2631       && input_other->GetType() == DataType::Type::kBool) {
2632     // Replace code looking like
2633     //    XOR dst, src, 1
2634     // with
2635     //    BOOLEAN_NOT dst, src
2636     HBooleanNot* boolean_not = new (GetGraph()->GetAllocator()) HBooleanNot(input_other);
2637     instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, boolean_not);
2638     RecordSimplification();
2639     return;
2640   }
2641 
2642   if ((input_cst != nullptr) && AreAllBitsSet(input_cst)) {
2643     // Replace code looking like
2644     //    XOR dst, src, 0xFFF...FF
2645     // with
2646     //    NOT dst, src
2647     HNot* bitwise_not = new (GetGraph()->GetAllocator()) HNot(instruction->GetType(), input_other);
2648     instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, bitwise_not);
2649     RecordSimplification();
2650     return;
2651   }
2652 
2653   HInstruction* left = instruction->GetLeft();
2654   HInstruction* right = instruction->GetRight();
2655   if (((left->IsNot() && right->IsNot()) ||
2656        (left->IsBooleanNot() && right->IsBooleanNot())) &&
2657       left->HasOnlyOneNonEnvironmentUse() &&
2658       right->HasOnlyOneNonEnvironmentUse()) {
2659     // Replace code looking like
2660     //    NOT nota, a
2661     //    NOT notb, b
2662     //    XOR dst, nota, notb
2663     // with
2664     //    XOR dst, a, b
2665     instruction->ReplaceInput(left->InputAt(0), 0);
2666     instruction->ReplaceInput(right->InputAt(0), 1);
2667     left->GetBlock()->RemoveInstruction(left);
2668     right->GetBlock()->RemoveInstruction(right);
2669     RecordSimplification();
2670     return;
2671   }
2672 
2673   if (TryReplaceWithRotate(instruction)) {
2674     return;
2675   }
2676 
2677   // TryHandleAssociativeAndCommutativeOperation() does not remove its input,
2678   // so no need to return.
2679   TryHandleAssociativeAndCommutativeOperation(instruction);
2680 }
2681 
SimplifyBoxUnbox(HInvoke * instruction,ArtField * field,DataType::Type type)2682 void InstructionSimplifierVisitor::SimplifyBoxUnbox(
2683     HInvoke* instruction, ArtField* field, DataType::Type type) {
2684   DCHECK(instruction->GetIntrinsic() == Intrinsics::kByteValueOf ||
2685          instruction->GetIntrinsic() == Intrinsics::kShortValueOf ||
2686          instruction->GetIntrinsic() == Intrinsics::kCharacterValueOf ||
2687          instruction->GetIntrinsic() == Intrinsics::kIntegerValueOf);
2688   const HUseList<HInstruction*>& uses = instruction->GetUses();
2689   for (auto it = uses.begin(), end = uses.end(); it != end;) {
2690     HInstruction* user = it->GetUser();
2691     ++it;  // Increment the iterator before we potentially remove the node from the list.
2692     if (user->IsInstanceFieldGet() &&
2693         user->AsInstanceFieldGet()->GetFieldInfo().GetField() == field &&
2694         // Note: Due to other simplifications, we may have an `HInstanceFieldGet` with
2695         // a different type (Int8 vs. Uint8, Int16 vs. Uint16) for the same field.
2696         // Do not optimize that case for now. (We would need to insert a `HTypeConversion`.)
2697         user->GetType() == type) {
2698       user->ReplaceWith(instruction->InputAt(0));
2699       RecordSimplification();
2700       // Do not remove `user` while we're iterating over the block's instructions. Let DCE do it.
2701     }
2702   }
2703 }
2704 
SimplifyStringEquals(HInvoke * instruction)2705 void InstructionSimplifierVisitor::SimplifyStringEquals(HInvoke* instruction) {
2706   HInstruction* argument = instruction->InputAt(1);
2707   HInstruction* receiver = instruction->InputAt(0);
2708   if (receiver == argument) {
2709     // Because String.equals is an instance call, the receiver is
2710     // a null check if we don't know it's null. The argument however, will
2711     // be the actual object. So we cannot end up in a situation where both
2712     // are equal but could be null.
2713     DCHECK(CanEnsureNotNullAt(argument, instruction));
2714     instruction->ReplaceWith(GetGraph()->GetIntConstant(1));
2715     instruction->GetBlock()->RemoveInstruction(instruction);
2716   } else {
2717     StringEqualsOptimizations optimizations(instruction);
2718     if (CanEnsureNotNullAt(argument, instruction)) {
2719       optimizations.SetArgumentNotNull();
2720     }
2721     ScopedObjectAccess soa(Thread::Current());
2722     ReferenceTypeInfo argument_rti = argument->GetReferenceTypeInfo();
2723     if (argument_rti.IsValid() && argument_rti.IsStringClass()) {
2724       optimizations.SetArgumentIsString();
2725     }
2726   }
2727 }
2728 
IsArrayLengthOf(HInstruction * potential_length,HInstruction * potential_array)2729 static bool IsArrayLengthOf(HInstruction* potential_length, HInstruction* potential_array) {
2730   if (potential_length->IsArrayLength()) {
2731     return potential_length->InputAt(0) == potential_array;
2732   }
2733 
2734   if (potential_array->IsNewArray()) {
2735     return potential_array->AsNewArray()->GetLength() == potential_length;
2736   }
2737 
2738   return false;
2739 }
2740 
SimplifySystemArrayCopy(HInvoke * instruction)2741 void InstructionSimplifierVisitor::SimplifySystemArrayCopy(HInvoke* instruction) {
2742   HInstruction* source = instruction->InputAt(0);
2743   HInstruction* source_pos = instruction->InputAt(1);
2744   HInstruction* destination = instruction->InputAt(2);
2745   HInstruction* destination_pos = instruction->InputAt(3);
2746   HInstruction* count = instruction->InputAt(4);
2747   SystemArrayCopyOptimizations optimizations(instruction);
2748   if (CanEnsureNotNullAt(source, instruction)) {
2749     optimizations.SetSourceIsNotNull();
2750   }
2751   if (CanEnsureNotNullAt(destination, instruction)) {
2752     optimizations.SetDestinationIsNotNull();
2753   }
2754   if (destination == source) {
2755     optimizations.SetDestinationIsSource();
2756   }
2757 
2758   if (source_pos == destination_pos) {
2759     optimizations.SetSourcePositionIsDestinationPosition();
2760   }
2761 
2762   if (IsArrayLengthOf(count, source)) {
2763     optimizations.SetCountIsSourceLength();
2764   }
2765 
2766   if (IsArrayLengthOf(count, destination)) {
2767     optimizations.SetCountIsDestinationLength();
2768   }
2769 
2770   {
2771     ScopedObjectAccess soa(Thread::Current());
2772     DataType::Type source_component_type = DataType::Type::kVoid;
2773     DataType::Type destination_component_type = DataType::Type::kVoid;
2774     ReferenceTypeInfo destination_rti = destination->GetReferenceTypeInfo();
2775     if (destination_rti.IsValid()) {
2776       if (destination_rti.IsObjectArray()) {
2777         if (destination_rti.IsExact()) {
2778           optimizations.SetDoesNotNeedTypeCheck();
2779         }
2780         optimizations.SetDestinationIsTypedObjectArray();
2781       }
2782       if (destination_rti.IsPrimitiveArrayClass()) {
2783         destination_component_type = DataTypeFromPrimitive(
2784             destination_rti.GetTypeHandle()->GetComponentType()->GetPrimitiveType());
2785         optimizations.SetDestinationIsPrimitiveArray();
2786       } else if (destination_rti.IsNonPrimitiveArrayClass()) {
2787         optimizations.SetDestinationIsNonPrimitiveArray();
2788       }
2789     }
2790     ReferenceTypeInfo source_rti = source->GetReferenceTypeInfo();
2791     if (source_rti.IsValid()) {
2792       if (destination_rti.IsValid() && destination_rti.CanArrayHoldValuesOf(source_rti)) {
2793         optimizations.SetDoesNotNeedTypeCheck();
2794       }
2795       if (source_rti.IsPrimitiveArrayClass()) {
2796         optimizations.SetSourceIsPrimitiveArray();
2797         source_component_type = DataTypeFromPrimitive(
2798             source_rti.GetTypeHandle()->GetComponentType()->GetPrimitiveType());
2799       } else if (source_rti.IsNonPrimitiveArrayClass()) {
2800         optimizations.SetSourceIsNonPrimitiveArray();
2801       }
2802     }
2803     // For primitive arrays, use their optimized ArtMethod implementations.
2804     if ((source_component_type != DataType::Type::kVoid) &&
2805         (source_component_type == destination_component_type)) {
2806       ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
2807       PointerSize image_size = class_linker->GetImagePointerSize();
2808       HInvokeStaticOrDirect* invoke = instruction->AsInvokeStaticOrDirect();
2809       ObjPtr<mirror::Class> system = invoke->GetResolvedMethod()->GetDeclaringClass();
2810       ArtMethod* method = nullptr;
2811       switch (source_component_type) {
2812         case DataType::Type::kBool:
2813           method = system->FindClassMethod("arraycopy", "([ZI[ZII)V", image_size);
2814           break;
2815         case DataType::Type::kInt8:
2816           method = system->FindClassMethod("arraycopy", "([BI[BII)V", image_size);
2817           break;
2818         case DataType::Type::kUint16:
2819           method = system->FindClassMethod("arraycopy", "([CI[CII)V", image_size);
2820           break;
2821         case DataType::Type::kInt16:
2822           method = system->FindClassMethod("arraycopy", "([SI[SII)V", image_size);
2823           break;
2824         case DataType::Type::kInt32:
2825           method = system->FindClassMethod("arraycopy", "([II[III)V", image_size);
2826           break;
2827         case DataType::Type::kFloat32:
2828           method = system->FindClassMethod("arraycopy", "([FI[FII)V", image_size);
2829           break;
2830         case DataType::Type::kInt64:
2831           method = system->FindClassMethod("arraycopy", "([JI[JII)V", image_size);
2832           break;
2833         case DataType::Type::kFloat64:
2834           method = system->FindClassMethod("arraycopy", "([DI[DII)V", image_size);
2835           break;
2836         default:
2837           LOG(FATAL) << "Unreachable";
2838       }
2839       DCHECK(method != nullptr);
2840       DCHECK(method->IsStatic());
2841       DCHECK(method->GetDeclaringClass() == system);
2842       invoke->SetResolvedMethod(method, !codegen_->GetGraph()->IsDebuggable());
2843       // Sharpen the new invoke. Note that we do not update the dex method index of
2844       // the invoke, as we would need to look it up in the current dex file, and it
2845       // is unlikely that it exists. The most usual situation for such typed
2846       // arraycopy methods is a direct pointer to the boot image.
2847       invoke->SetDispatchInfo(HSharpening::SharpenLoadMethod(
2848           method,
2849           /* has_method_id= */ true,
2850           /* for_interface_call= */ false,
2851           codegen_));
2852     }
2853   }
2854 }
2855 
SimplifyFP2Int(HInvoke * invoke)2856 void InstructionSimplifierVisitor::SimplifyFP2Int(HInvoke* invoke) {
2857   DCHECK(invoke->IsInvokeStaticOrDirect());
2858   uint32_t dex_pc = invoke->GetDexPc();
2859   HInstruction* x = invoke->InputAt(0);
2860   DataType::Type type = x->GetType();
2861   // Set proper bit pattern for NaN and replace intrinsic with raw version.
2862   HInstruction* nan;
2863   if (type == DataType::Type::kFloat64) {
2864     nan = GetGraph()->GetLongConstant(0x7ff8000000000000L);
2865     invoke->SetIntrinsic(Intrinsics::kDoubleDoubleToRawLongBits,
2866                          kNeedsEnvironment,
2867                          kNoSideEffects,
2868                          kNoThrow);
2869   } else {
2870     DCHECK_EQ(type, DataType::Type::kFloat32);
2871     nan = GetGraph()->GetIntConstant(0x7fc00000);
2872     invoke->SetIntrinsic(Intrinsics::kFloatFloatToRawIntBits,
2873                          kNeedsEnvironment,
2874                          kNoSideEffects,
2875                          kNoThrow);
2876   }
2877   // Test IsNaN(x), which is the same as x != x.
2878   HCondition* condition = new (GetGraph()->GetAllocator()) HNotEqual(x, x, dex_pc);
2879   condition->SetBias(ComparisonBias::kLtBias);
2880   invoke->GetBlock()->InsertInstructionBefore(condition, invoke->GetNext());
2881   // Select between the two.
2882   HInstruction* select = new (GetGraph()->GetAllocator()) HSelect(condition, nan, invoke, dex_pc);
2883   invoke->GetBlock()->InsertInstructionBefore(select, condition->GetNext());
2884   invoke->ReplaceWithExceptInReplacementAtIndex(select, 0);  // false at index 0
2885 }
2886 
SimplifyStringCharAt(HInvoke * invoke)2887 void InstructionSimplifierVisitor::SimplifyStringCharAt(HInvoke* invoke) {
2888   HInstruction* str = invoke->InputAt(0);
2889   HInstruction* index = invoke->InputAt(1);
2890   uint32_t dex_pc = invoke->GetDexPc();
2891   ArenaAllocator* allocator = GetGraph()->GetAllocator();
2892   // We treat String as an array to allow DCE and BCE to seamlessly work on strings,
2893   // so create the HArrayLength, HBoundsCheck and HArrayGet.
2894   HArrayLength* length = new (allocator) HArrayLength(str, dex_pc, /* is_string_length= */ true);
2895   invoke->GetBlock()->InsertInstructionBefore(length, invoke);
2896   HBoundsCheck* bounds_check = new (allocator) HBoundsCheck(
2897       index, length, dex_pc, /* is_string_char_at= */ true);
2898   invoke->GetBlock()->InsertInstructionBefore(bounds_check, invoke);
2899   HArrayGet* array_get = new (allocator) HArrayGet(str,
2900                                                    bounds_check,
2901                                                    DataType::Type::kUint16,
2902                                                    SideEffects::None(),  // Strings are immutable.
2903                                                    dex_pc,
2904                                                    /* is_string_char_at= */ true);
2905   invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, array_get);
2906   bounds_check->CopyEnvironmentFrom(invoke->GetEnvironment());
2907   GetGraph()->SetHasBoundsChecks(true);
2908 }
2909 
SimplifyStringLength(HInvoke * invoke)2910 void InstructionSimplifierVisitor::SimplifyStringLength(HInvoke* invoke) {
2911   HInstruction* str = invoke->InputAt(0);
2912   uint32_t dex_pc = invoke->GetDexPc();
2913   // We treat String as an array to allow DCE and BCE to seamlessly work on strings,
2914   // so create the HArrayLength.
2915   HArrayLength* length =
2916       new (GetGraph()->GetAllocator()) HArrayLength(str, dex_pc, /* is_string_length= */ true);
2917   invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, length);
2918 }
2919 
SimplifyStringIndexOf(HInvoke * invoke)2920 void InstructionSimplifierVisitor::SimplifyStringIndexOf(HInvoke* invoke) {
2921   DCHECK(invoke->GetIntrinsic() == Intrinsics::kStringIndexOf ||
2922          invoke->GetIntrinsic() == Intrinsics::kStringIndexOfAfter);
2923   if (invoke->InputAt(0)->IsLoadString()) {
2924     HLoadString* load_string = invoke->InputAt(0)->AsLoadString();
2925     const DexFile& dex_file = load_string->GetDexFile();
2926     uint32_t utf16_length;
2927     const char* data =
2928         dex_file.GetStringDataAndUtf16Length(load_string->GetStringIndex(), &utf16_length);
2929     if (utf16_length == 0) {
2930       invoke->ReplaceWith(GetGraph()->GetIntConstant(-1));
2931       invoke->GetBlock()->RemoveInstruction(invoke);
2932       RecordSimplification();
2933       return;
2934     }
2935     if (utf16_length == 1 && invoke->GetIntrinsic() == Intrinsics::kStringIndexOf) {
2936       // Simplify to HSelect(HEquals(., load_string.charAt(0)), 0, -1).
2937       // If the sought character is supplementary, this gives the correct result, i.e. -1.
2938       uint32_t c = GetUtf16FromUtf8(&data);
2939       DCHECK_EQ(GetTrailingUtf16Char(c), 0u);
2940       DCHECK_EQ(GetLeadingUtf16Char(c), c);
2941       uint32_t dex_pc = invoke->GetDexPc();
2942       ArenaAllocator* allocator = GetGraph()->GetAllocator();
2943       HEqual* equal =
2944           new (allocator) HEqual(invoke->InputAt(1), GetGraph()->GetIntConstant(c), dex_pc);
2945       invoke->GetBlock()->InsertInstructionBefore(equal, invoke);
2946       HSelect* result = new (allocator) HSelect(equal,
2947                                                 GetGraph()->GetIntConstant(0),
2948                                                 GetGraph()->GetIntConstant(-1),
2949                                                 dex_pc);
2950       invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, result);
2951       RecordSimplification();
2952       return;
2953     }
2954   }
2955 }
2956 
2957 // This method should only be used on intrinsics whose sole way of throwing an
2958 // exception is raising a NPE when the nth argument is null. If that argument
2959 // is provably non-null, we can clear the flag.
SimplifyNPEOnArgN(HInvoke * invoke,size_t n)2960 void InstructionSimplifierVisitor::SimplifyNPEOnArgN(HInvoke* invoke, size_t n) {
2961   HInstruction* arg = invoke->InputAt(n);
2962   if (invoke->CanThrow() && !arg->CanBeNull()) {
2963     invoke->SetCanThrow(false);
2964   }
2965 }
2966 
2967 // Methods that return "this" can replace the returned value with the receiver.
SimplifyReturnThis(HInvoke * invoke)2968 void InstructionSimplifierVisitor::SimplifyReturnThis(HInvoke* invoke) {
2969   if (invoke->HasUses()) {
2970     HInstruction* receiver = invoke->InputAt(0);
2971     invoke->ReplaceWith(receiver);
2972     RecordSimplification();
2973   }
2974 }
2975 
2976 // Helper method for StringBuffer escape analysis.
NoEscapeForStringBufferReference(HInstruction * reference,HInstruction * user)2977 static bool NoEscapeForStringBufferReference(HInstruction* reference, HInstruction* user) {
2978   if (user->IsInvoke()) {
2979     switch (user->AsInvoke()->GetIntrinsic()) {
2980       case Intrinsics::kStringBufferLength:
2981       case Intrinsics::kStringBufferToString:
2982         DCHECK_EQ(user->InputAt(0), reference);
2983         return true;
2984       case Intrinsics::kStringBufferAppend:
2985         // Returns "this", so only okay if no further uses.
2986         DCHECK_EQ(user->InputAt(0), reference);
2987         DCHECK_NE(user->InputAt(1), reference);
2988         return !user->HasUses();
2989       default:
2990         break;
2991     }
2992   }
2993 
2994   if (user->IsInvokeStaticOrDirect()) {
2995     // Any constructor on StringBuffer is okay.
2996     return user->AsInvokeStaticOrDirect()->GetResolvedMethod() != nullptr &&
2997            user->AsInvokeStaticOrDirect()->GetResolvedMethod()->IsConstructor() &&
2998            user->InputAt(0) == reference;
2999   }
3000 
3001   return false;
3002 }
3003 
TryReplaceStringBuilderAppend(CodeGenerator * codegen,HInvoke * invoke)3004 static bool TryReplaceStringBuilderAppend(CodeGenerator* codegen, HInvoke* invoke) {
3005   DCHECK_EQ(invoke->GetIntrinsic(), Intrinsics::kStringBuilderToString);
3006   if (invoke->CanThrowIntoCatchBlock()) {
3007     return false;
3008   }
3009 
3010   HBasicBlock* block = invoke->GetBlock();
3011   HInstruction* sb = invoke->InputAt(0);
3012 
3013   // We support only a new StringBuilder, otherwise we cannot ensure that
3014   // the StringBuilder data does not need to be populated for other users.
3015   if (!sb->IsNewInstance()) {
3016     return false;
3017   }
3018 
3019   // For now, we support only single-block recognition.
3020   // (Ternary operators feeding the append could be implemented.)
3021   for (const HUseListNode<HInstruction*>& use : sb->GetUses()) {
3022     if (use.GetUser()->GetBlock() != block) {
3023       return false;
3024     }
3025     // The append pattern uses the StringBuilder only as the first argument.
3026     if (use.GetIndex() != 0u) {
3027       return false;
3028     }
3029   }
3030 
3031   // Collect args and check for unexpected uses.
3032   // We expect one call to a constructor with no arguments, one constructor fence (unless
3033   // eliminated), some number of append calls and one call to StringBuilder.toString().
3034   bool seen_constructor = false;
3035   bool seen_constructor_fence = false;
3036   bool seen_to_string = false;
3037   uint32_t format = 0u;
3038   uint32_t num_args = 0u;
3039   bool has_fp_args = false;
3040   HInstruction* args[StringBuilderAppend::kMaxArgs];  // Added in reverse order.
3041   for (HBackwardInstructionIterator iter(block->GetInstructions()); !iter.Done(); iter.Advance()) {
3042     HInstruction* user = iter.Current();
3043     // Instructions of interest apply to `sb`, skip those that do not involve `sb`.
3044     if (user->InputCount() == 0u || user->InputAt(0u) != sb) {
3045       continue;
3046     }
3047     // We visit the uses in reverse order, so the StringBuilder.toString() must come first.
3048     if (!seen_to_string) {
3049       if (user == invoke) {
3050         seen_to_string = true;
3051         continue;
3052       } else {
3053         return false;
3054       }
3055     }
3056 
3057     // Pattern match seeing arguments, then constructor, then constructor fence.
3058     if (user->IsInvokeStaticOrDirect() &&
3059         user->AsInvokeStaticOrDirect()->GetResolvedMethod() != nullptr &&
3060         user->AsInvokeStaticOrDirect()->GetResolvedMethod()->IsConstructor() &&
3061         user->AsInvokeStaticOrDirect()->GetNumberOfArguments() == 1u) {
3062       // After arguments, we should see the constructor.
3063       // We accept only the constructor with no extra arguments.
3064       DCHECK(!seen_constructor);
3065       DCHECK(!seen_constructor_fence);
3066       seen_constructor = true;
3067     } else if (user->IsInvoke()) {
3068       // The arguments.
3069       HInvoke* as_invoke = user->AsInvoke();
3070       DCHECK(!seen_constructor);
3071       DCHECK(!seen_constructor_fence);
3072       StringBuilderAppend::Argument arg;
3073       switch (as_invoke->GetIntrinsic()) {
3074         case Intrinsics::kStringBuilderAppendObject:
3075           // TODO: Unimplemented, needs to call String.valueOf().
3076           return false;
3077         case Intrinsics::kStringBuilderAppendString:
3078           arg = StringBuilderAppend::Argument::kString;
3079           break;
3080         case Intrinsics::kStringBuilderAppendCharArray:
3081           // TODO: Unimplemented, StringBuilder.append(char[]) can throw NPE and we would
3082           // not have the correct stack trace for it.
3083           return false;
3084         case Intrinsics::kStringBuilderAppendBoolean:
3085           arg = StringBuilderAppend::Argument::kBoolean;
3086           break;
3087         case Intrinsics::kStringBuilderAppendChar:
3088           arg = StringBuilderAppend::Argument::kChar;
3089           break;
3090         case Intrinsics::kStringBuilderAppendInt:
3091           arg = StringBuilderAppend::Argument::kInt;
3092           break;
3093         case Intrinsics::kStringBuilderAppendLong:
3094           arg = StringBuilderAppend::Argument::kLong;
3095           break;
3096         case Intrinsics::kStringBuilderAppendFloat:
3097           arg = StringBuilderAppend::Argument::kFloat;
3098           has_fp_args = true;
3099           break;
3100         case Intrinsics::kStringBuilderAppendDouble:
3101           arg = StringBuilderAppend::Argument::kDouble;
3102           has_fp_args = true;
3103           break;
3104         case Intrinsics::kStringBuilderAppendCharSequence: {
3105           ReferenceTypeInfo rti = as_invoke->InputAt(1)->GetReferenceTypeInfo();
3106           if (!rti.IsValid()) {
3107             return false;
3108           }
3109           ScopedObjectAccess soa(Thread::Current());
3110           Handle<mirror::Class> input_type = rti.GetTypeHandle();
3111           DCHECK(input_type != nullptr);
3112           if (input_type.Get() == GetClassRoot<mirror::String>()) {
3113             arg = StringBuilderAppend::Argument::kString;
3114           } else {
3115             // TODO: Check and implement for StringBuilder. We could find the StringBuilder's
3116             // internal char[] inconsistent with the length, or the string compression
3117             // of the result could be compromised with a concurrent modification, and
3118             // we would need to throw appropriate exceptions.
3119             return false;
3120           }
3121           break;
3122         }
3123         default: {
3124           return false;
3125         }
3126       }
3127       // Uses of the append return value should have been replaced with the first input.
3128       DCHECK(!as_invoke->HasUses());
3129       DCHECK(!as_invoke->HasEnvironmentUses());
3130       if (num_args == StringBuilderAppend::kMaxArgs) {
3131         return false;
3132       }
3133       format = (format << StringBuilderAppend::kBitsPerArg) | static_cast<uint32_t>(arg);
3134       args[num_args] = as_invoke->InputAt(1u);
3135       ++num_args;
3136     } else if (user->IsConstructorFence()) {
3137       // The last use we see is the constructor fence.
3138       DCHECK(seen_constructor);
3139       DCHECK(!seen_constructor_fence);
3140       seen_constructor_fence = true;
3141     } else {
3142       return false;
3143     }
3144   }
3145 
3146   if (num_args == 0u) {
3147     return false;
3148   }
3149 
3150   // Check environment uses.
3151   for (const HUseListNode<HEnvironment*>& use : sb->GetEnvUses()) {
3152     HInstruction* holder = use.GetUser()->GetHolder();
3153     if (holder->GetBlock() != block) {
3154       return false;
3155     }
3156     // Accept only calls on the StringBuilder (which shall all be removed).
3157     // TODO: Carve-out for const-string? Or rely on environment pruning (to be implemented)?
3158     if (holder->InputCount() == 0 || holder->InputAt(0) != sb) {
3159       return false;
3160     }
3161   }
3162 
3163   // Calculate outgoing vregs, including padding for 64-bit arg alignment.
3164   const PointerSize pointer_size = InstructionSetPointerSize(codegen->GetInstructionSet());
3165   const size_t method_vregs = static_cast<size_t>(pointer_size) / kVRegSize;
3166   uint32_t number_of_out_vregs = method_vregs;  // For correct alignment padding; subtracted below.
3167   for (uint32_t f = format; f != 0u; f >>= StringBuilderAppend::kBitsPerArg) {
3168     auto a = enum_cast<StringBuilderAppend::Argument>(f & StringBuilderAppend::kArgMask);
3169     if (a == StringBuilderAppend::Argument::kLong || a == StringBuilderAppend::Argument::kDouble) {
3170       number_of_out_vregs += /* alignment */ ((number_of_out_vregs) & 1u) + /* vregs */ 2u;
3171     } else {
3172       number_of_out_vregs += /* vregs */ 1u;
3173     }
3174   }
3175   number_of_out_vregs -= method_vregs;
3176 
3177   // Create replacement instruction.
3178   HIntConstant* fmt = block->GetGraph()->GetIntConstant(static_cast<int32_t>(format));
3179   ArenaAllocator* allocator = block->GetGraph()->GetAllocator();
3180   HStringBuilderAppend* append = new (allocator) HStringBuilderAppend(
3181       fmt, num_args, number_of_out_vregs, has_fp_args, allocator, invoke->GetDexPc());
3182   append->SetReferenceTypeInfoIfValid(invoke->GetReferenceTypeInfo());
3183   for (size_t i = 0; i != num_args; ++i) {
3184     append->SetArgumentAt(i, args[num_args - 1u - i]);
3185   }
3186   block->InsertInstructionBefore(append, invoke);
3187   DCHECK(!invoke->CanBeNull());
3188   DCHECK(!append->CanBeNull());
3189   invoke->ReplaceWith(append);
3190   // Copy environment, except for the StringBuilder uses.
3191   for (HEnvironment* env = invoke->GetEnvironment(); env != nullptr; env = env->GetParent()) {
3192     for (size_t i = 0, size = env->Size(); i != size; ++i) {
3193       if (env->GetInstructionAt(i) == sb) {
3194         env->RemoveAsUserOfInput(i);
3195         env->SetRawEnvAt(i, /*instruction=*/ nullptr);
3196       }
3197     }
3198   }
3199   append->CopyEnvironmentFrom(invoke->GetEnvironment());
3200   // Remove the old instruction.
3201   block->RemoveInstruction(invoke);
3202   // Remove the StringBuilder's uses and StringBuilder.
3203   while (sb->HasNonEnvironmentUses()) {
3204     block->RemoveInstruction(sb->GetUses().front().GetUser());
3205   }
3206   DCHECK(!sb->HasEnvironmentUses());
3207   block->RemoveInstruction(sb);
3208   return true;
3209 }
3210 
3211 // Certain allocation intrinsics are not removed by dead code elimination
3212 // because of potentially throwing an OOM exception or other side effects.
3213 // This method removes such intrinsics when special circumstances allow.
SimplifyAllocationIntrinsic(HInvoke * invoke)3214 void InstructionSimplifierVisitor::SimplifyAllocationIntrinsic(HInvoke* invoke) {
3215   if (!invoke->HasUses()) {
3216     // Instruction has no uses. If unsynchronized, we can remove right away, safely ignoring
3217     // the potential OOM of course. Otherwise, we must ensure the receiver object of this
3218     // call does not escape since only thread-local synchronization may be removed.
3219     bool is_synchronized = invoke->GetIntrinsic() == Intrinsics::kStringBufferToString;
3220     HInstruction* receiver = invoke->InputAt(0);
3221     if (!is_synchronized || DoesNotEscape(receiver, NoEscapeForStringBufferReference)) {
3222       invoke->GetBlock()->RemoveInstruction(invoke);
3223       RecordSimplification();
3224     }
3225   } else if (invoke->GetIntrinsic() == Intrinsics::kStringBuilderToString &&
3226              TryReplaceStringBuilderAppend(codegen_, invoke)) {
3227     RecordSimplification();
3228   }
3229 }
3230 
SimplifyVarHandleIntrinsic(HInvoke * invoke)3231 void InstructionSimplifierVisitor::SimplifyVarHandleIntrinsic(HInvoke* invoke) {
3232   DCHECK(invoke->IsInvokePolymorphic());
3233   VarHandleOptimizations optimizations(invoke);
3234 
3235   if (optimizations.GetDoNotIntrinsify()) {
3236     // Preceding static checks disabled intrinsic, so no need to analyze further.
3237     return;
3238   }
3239 
3240   size_t expected_coordinates_count = GetExpectedVarHandleCoordinatesCount(invoke);
3241   if (expected_coordinates_count != 0u) {
3242     HInstruction* object = invoke->InputAt(1);
3243     // The following has been ensured by static checks in the instruction builder.
3244     DCHECK(object->GetType() == DataType::Type::kReference);
3245     // Re-check for null constant, as this might have changed after the inliner.
3246     if (object->IsNullConstant()) {
3247       optimizations.SetDoNotIntrinsify();
3248       return;
3249     }
3250     // Test whether we can avoid the null check on the object.
3251     if (CanEnsureNotNullAt(object, invoke)) {
3252       optimizations.SetSkipObjectNullCheck();
3253     }
3254   }
3255 
3256   if (CanUseKnownImageVarHandle(invoke)) {
3257     optimizations.SetUseKnownImageVarHandle();
3258   }
3259 }
3260 
CanUseKnownImageVarHandle(HInvoke * invoke)3261 bool InstructionSimplifierVisitor::CanUseKnownImageVarHandle(HInvoke* invoke) {
3262   // If the `VarHandle` comes from a static final field of an initialized class in an image
3263   // (boot image or app image), we can do the checks at compile time. We do this optimization
3264   // only for AOT and only for field handles when we can avoid all checks. This avoids the
3265   // possibility of the code concurrently messing with the `VarHandle` using reflection,
3266   // we simply perform the operation with the `VarHandle` as seen at compile time.
3267   // TODO: Extend this to arrays to support the `AtomicIntegerArray` class.
3268   const CompilerOptions& compiler_options = codegen_->GetCompilerOptions();
3269   if (!compiler_options.IsAotCompiler()) {
3270     return false;
3271   }
3272   size_t expected_coordinates_count = GetExpectedVarHandleCoordinatesCount(invoke);
3273   if (expected_coordinates_count == 2u) {
3274     return false;
3275   }
3276   HInstruction* var_handle_instruction = invoke->InputAt(0);
3277   if (var_handle_instruction->IsNullCheck()) {
3278     var_handle_instruction = var_handle_instruction->InputAt(0);
3279   }
3280   if (!var_handle_instruction->IsStaticFieldGet()) {
3281     return false;
3282   }
3283   ArtField* field = var_handle_instruction->AsStaticFieldGet()->GetFieldInfo().GetField();
3284   DCHECK(field->IsStatic());
3285   if (!field->IsFinal()) {
3286     return false;
3287   }
3288   ScopedObjectAccess soa(Thread::Current());
3289   ObjPtr<mirror::Class> declaring_class = field->GetDeclaringClass();
3290   if (!declaring_class->IsVisiblyInitialized()) {
3291     // During AOT compilation, dex2oat ensures that initialized classes are visibly initialized.
3292     DCHECK(!declaring_class->IsInitialized());
3293     return false;
3294   }
3295   HInstruction* load_class = var_handle_instruction->InputAt(0);
3296   if (kIsDebugBuild) {
3297     bool is_in_image = false;
3298     if (Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(declaring_class)) {
3299       is_in_image = true;
3300     } else if (compiler_options.IsGeneratingImage()) {
3301       std::string storage;
3302       const char* descriptor = declaring_class->GetDescriptor(&storage);
3303       is_in_image = compiler_options.IsImageClass(descriptor);
3304     }
3305     CHECK_EQ(is_in_image, load_class->IsLoadClass() && load_class->AsLoadClass()->IsInImage());
3306   }
3307   if (!load_class->IsLoadClass() || !load_class->AsLoadClass()->IsInImage()) {
3308     return false;
3309   }
3310 
3311   // Get the `VarHandle` object and check its class.
3312   ObjPtr<mirror::Class> expected_var_handle_class;
3313   switch (expected_coordinates_count) {
3314     case 0:
3315       expected_var_handle_class = GetClassRoot<mirror::StaticFieldVarHandle>();
3316       break;
3317     default:
3318       DCHECK_EQ(expected_coordinates_count, 1u);
3319       expected_var_handle_class = GetClassRoot<mirror::FieldVarHandle>();
3320       break;
3321   }
3322   ObjPtr<mirror::Object> var_handle_object = field->GetObject(declaring_class);
3323   if (var_handle_object == nullptr || var_handle_object->GetClass() != expected_var_handle_class) {
3324     return false;
3325   }
3326   ObjPtr<mirror::VarHandle> var_handle = ObjPtr<mirror::VarHandle>::DownCast(var_handle_object);
3327 
3328   // Check access mode.
3329   mirror::VarHandle::AccessMode access_mode =
3330       mirror::VarHandle::GetAccessModeByIntrinsic(invoke->GetIntrinsic());
3331   if (!var_handle->IsAccessModeSupported(access_mode)) {
3332     return false;
3333   }
3334 
3335   // Check argument types.
3336   ObjPtr<mirror::Class> var_type = var_handle->GetVarType();
3337   mirror::VarHandle::AccessModeTemplate access_mode_template =
3338       mirror::VarHandle::GetAccessModeTemplate(access_mode);
3339   // Note: The data type of input arguments does not need to match the type from shorty
3340   // due to implicit conversions or avoiding unnecessary conversions before narrow stores.
3341   DataType::Type type = (access_mode_template == mirror::VarHandle::AccessModeTemplate::kGet)
3342       ? invoke->GetType()
3343       : GetDataTypeFromShorty(invoke, invoke->GetNumberOfArguments() - 1u);
3344   if (type != DataTypeFromPrimitive(var_type->GetPrimitiveType())) {
3345     return false;
3346   }
3347   if (type == DataType::Type::kReference) {
3348     uint32_t arguments_start = /* VarHandle object */ 1u + expected_coordinates_count;
3349     uint32_t number_of_arguments = invoke->GetNumberOfArguments();
3350     for (size_t arg_index = arguments_start; arg_index != number_of_arguments; ++arg_index) {
3351       HInstruction* arg = invoke->InputAt(arg_index);
3352       DCHECK_EQ(arg->GetType(), DataType::Type::kReference);
3353       if (!arg->IsNullConstant()) {
3354         ReferenceTypeInfo arg_type_info = arg->GetReferenceTypeInfo();
3355         if (!arg_type_info.IsValid() ||
3356             !var_type->IsAssignableFrom(arg_type_info.GetTypeHandle().Get())) {
3357           return false;
3358         }
3359       }
3360     }
3361   }
3362 
3363   // Check the first coordinate.
3364   if (expected_coordinates_count != 0u) {
3365     ObjPtr<mirror::Class> coordinate0_type = var_handle->GetCoordinateType0();
3366     DCHECK(coordinate0_type != nullptr);
3367     ReferenceTypeInfo object_type_info = invoke->InputAt(1)->GetReferenceTypeInfo();
3368     if (!object_type_info.IsValid() ||
3369         !coordinate0_type->IsAssignableFrom(object_type_info.GetTypeHandle().Get())) {
3370       return false;
3371     }
3372   }
3373 
3374   // All required checks passed.
3375   return true;
3376 }
3377 
VisitInvoke(HInvoke * instruction)3378 void InstructionSimplifierVisitor::VisitInvoke(HInvoke* instruction) {
3379   switch (instruction->GetIntrinsic()) {
3380 #define SIMPLIFY_BOX_UNBOX(name, low, high, type, start_index) \
3381     case Intrinsics::k ## name ## ValueOf: \
3382       SimplifyBoxUnbox(instruction, WellKnownClasses::java_lang_##name##_value, type); \
3383       break;
3384     BOXED_TYPES(SIMPLIFY_BOX_UNBOX)
3385 #undef SIMPLIFY_BOX_UNBOX
3386     case Intrinsics::kStringEquals:
3387       SimplifyStringEquals(instruction);
3388       break;
3389     case Intrinsics::kSystemArrayCopy:
3390       SimplifySystemArrayCopy(instruction);
3391       break;
3392     case Intrinsics::kFloatFloatToIntBits:
3393     case Intrinsics::kDoubleDoubleToLongBits:
3394       SimplifyFP2Int(instruction);
3395       break;
3396     case Intrinsics::kStringCharAt:
3397       // Instruction builder creates intermediate representation directly
3398       // but the inliner can sharpen CharSequence.charAt() to String.charAt().
3399       SimplifyStringCharAt(instruction);
3400       break;
3401     case Intrinsics::kStringLength:
3402       // Instruction builder creates intermediate representation directly
3403       // but the inliner can sharpen CharSequence.length() to String.length().
3404       SimplifyStringLength(instruction);
3405       break;
3406     case Intrinsics::kStringIndexOf:
3407     case Intrinsics::kStringIndexOfAfter:
3408       SimplifyStringIndexOf(instruction);
3409       break;
3410     case Intrinsics::kStringStringIndexOf:
3411     case Intrinsics::kStringStringIndexOfAfter:
3412       SimplifyNPEOnArgN(instruction, 1);  // 0th has own NullCheck
3413       break;
3414     case Intrinsics::kStringBufferAppend:
3415     case Intrinsics::kStringBuilderAppendObject:
3416     case Intrinsics::kStringBuilderAppendString:
3417     case Intrinsics::kStringBuilderAppendCharSequence:
3418     case Intrinsics::kStringBuilderAppendCharArray:
3419     case Intrinsics::kStringBuilderAppendBoolean:
3420     case Intrinsics::kStringBuilderAppendChar:
3421     case Intrinsics::kStringBuilderAppendInt:
3422     case Intrinsics::kStringBuilderAppendLong:
3423     case Intrinsics::kStringBuilderAppendFloat:
3424     case Intrinsics::kStringBuilderAppendDouble:
3425       SimplifyReturnThis(instruction);
3426       break;
3427     case Intrinsics::kStringBufferToString:
3428     case Intrinsics::kStringBuilderToString:
3429       SimplifyAllocationIntrinsic(instruction);
3430       break;
3431     case Intrinsics::kVarHandleCompareAndExchange:
3432     case Intrinsics::kVarHandleCompareAndExchangeAcquire:
3433     case Intrinsics::kVarHandleCompareAndExchangeRelease:
3434     case Intrinsics::kVarHandleCompareAndSet:
3435     case Intrinsics::kVarHandleGet:
3436     case Intrinsics::kVarHandleGetAcquire:
3437     case Intrinsics::kVarHandleGetAndAdd:
3438     case Intrinsics::kVarHandleGetAndAddAcquire:
3439     case Intrinsics::kVarHandleGetAndAddRelease:
3440     case Intrinsics::kVarHandleGetAndBitwiseAnd:
3441     case Intrinsics::kVarHandleGetAndBitwiseAndAcquire:
3442     case Intrinsics::kVarHandleGetAndBitwiseAndRelease:
3443     case Intrinsics::kVarHandleGetAndBitwiseOr:
3444     case Intrinsics::kVarHandleGetAndBitwiseOrAcquire:
3445     case Intrinsics::kVarHandleGetAndBitwiseOrRelease:
3446     case Intrinsics::kVarHandleGetAndBitwiseXor:
3447     case Intrinsics::kVarHandleGetAndBitwiseXorAcquire:
3448     case Intrinsics::kVarHandleGetAndBitwiseXorRelease:
3449     case Intrinsics::kVarHandleGetAndSet:
3450     case Intrinsics::kVarHandleGetAndSetAcquire:
3451     case Intrinsics::kVarHandleGetAndSetRelease:
3452     case Intrinsics::kVarHandleGetOpaque:
3453     case Intrinsics::kVarHandleGetVolatile:
3454     case Intrinsics::kVarHandleSet:
3455     case Intrinsics::kVarHandleSetOpaque:
3456     case Intrinsics::kVarHandleSetRelease:
3457     case Intrinsics::kVarHandleSetVolatile:
3458     case Intrinsics::kVarHandleWeakCompareAndSet:
3459     case Intrinsics::kVarHandleWeakCompareAndSetAcquire:
3460     case Intrinsics::kVarHandleWeakCompareAndSetPlain:
3461     case Intrinsics::kVarHandleWeakCompareAndSetRelease:
3462       SimplifyVarHandleIntrinsic(instruction);
3463       break;
3464     case Intrinsics::kUnsafeArrayBaseOffset:
3465     case Intrinsics::kJdkUnsafeArrayBaseOffset:
3466       SimplifyArrayBaseOffset(instruction);
3467       break;
3468     default:
3469       break;
3470   }
3471 }
3472 
SimplifyArrayBaseOffset(HInvoke * invoke)3473 void InstructionSimplifierVisitor::SimplifyArrayBaseOffset(HInvoke* invoke) {
3474   if (!invoke->InputAt(1)->IsLoadClass()) {
3475     return;
3476   }
3477   HLoadClass* load_class = invoke->InputAt(1)->AsLoadClass();
3478   ReferenceTypeInfo info = load_class->GetLoadedClassRTI();
3479   if (!info.IsValid()) {
3480     return;
3481   }
3482   ScopedObjectAccess soa(Thread::Current());
3483   ObjPtr<mirror::Class> cls = info.GetTypeHandle()->GetComponentType();
3484   if (cls == nullptr) {
3485     return;
3486   }
3487   uint32_t base_offset =
3488       mirror::Array::DataOffset(Primitive::ComponentSize(cls->GetPrimitiveType())).Int32Value();
3489   invoke->ReplaceWith(GetGraph()->GetIntConstant(base_offset));
3490   RecordSimplification();
3491   return;
3492 }
3493 
VisitDeoptimize(HDeoptimize * deoptimize)3494 void InstructionSimplifierVisitor::VisitDeoptimize(HDeoptimize* deoptimize) {
3495   HInstruction* cond = deoptimize->InputAt(0);
3496   if (cond->IsConstant()) {
3497     if (cond->AsIntConstant()->IsFalse()) {
3498       // Never deopt: instruction can be removed.
3499       if (deoptimize->GuardsAnInput()) {
3500         deoptimize->ReplaceWith(deoptimize->GuardedInput());
3501       }
3502       deoptimize->GetBlock()->RemoveInstruction(deoptimize);
3503     } else {
3504       // Always deopt.
3505     }
3506   }
3507 }
3508 
3509 // Replace code looking like
3510 //    OP y, x, const1
3511 //    OP z, y, const2
3512 // with
3513 //    OP z, x, const3
3514 // where OP is both an associative and a commutative operation.
TryHandleAssociativeAndCommutativeOperation(HBinaryOperation * instruction)3515 bool InstructionSimplifierVisitor::TryHandleAssociativeAndCommutativeOperation(
3516     HBinaryOperation* instruction) {
3517   DCHECK(instruction->IsCommutative());
3518 
3519   if (!DataType::IsIntegralType(instruction->GetType())) {
3520     return false;
3521   }
3522 
3523   HInstruction* left = instruction->GetLeft();
3524   HInstruction* right = instruction->GetRight();
3525   // Variable names as described above.
3526   HConstant* const2;
3527   HBinaryOperation* y;
3528 
3529   if (instruction->GetKind() == left->GetKind() && right->IsConstant()) {
3530     const2 = right->AsConstant();
3531     y = left->AsBinaryOperation();
3532   } else if (left->IsConstant() && instruction->GetKind() == right->GetKind()) {
3533     const2 = left->AsConstant();
3534     y = right->AsBinaryOperation();
3535   } else {
3536     // The node does not match the pattern.
3537     return false;
3538   }
3539 
3540   // If `y` has more than one use, we do not perform the optimization
3541   // because it might increase code size (e.g. if the new constant is
3542   // no longer encodable as an immediate operand in the target ISA).
3543   if (!y->HasOnlyOneNonEnvironmentUse()) {
3544     return false;
3545   }
3546 
3547   // GetConstantRight() can return both left and right constants
3548   // for commutative operations.
3549   HConstant* const1 = y->GetConstantRight();
3550   if (const1 == nullptr) {
3551     return false;
3552   }
3553 
3554   instruction->ReplaceInput(const1, 0);
3555   instruction->ReplaceInput(const2, 1);
3556   HConstant* const3 = instruction->TryStaticEvaluation();
3557   DCHECK(const3 != nullptr);
3558   instruction->ReplaceInput(y->GetLeastConstantLeft(), 0);
3559   instruction->ReplaceInput(const3, 1);
3560   RecordSimplification();
3561   return true;
3562 }
3563 
AsAddOrSubOrNull(HInstruction * binop)3564 static HBinaryOperation* AsAddOrSubOrNull(HInstruction* binop) {
3565   return (binop->IsAdd() || binop->IsSub()) ? binop->AsBinaryOperation() : nullptr;
3566 }
3567 
3568 // Helper function that performs addition statically, considering the result type.
ComputeAddition(DataType::Type type,int64_t x,int64_t y)3569 static int64_t ComputeAddition(DataType::Type type, int64_t x, int64_t y) {
3570   // Use the Compute() method for consistency with TryStaticEvaluation().
3571   if (type == DataType::Type::kInt32) {
3572     return HAdd::Compute<int32_t>(x, y);
3573   } else {
3574     DCHECK_EQ(type, DataType::Type::kInt64);
3575     return HAdd::Compute<int64_t>(x, y);
3576   }
3577 }
3578 
3579 // Helper function that handles the child classes of HConstant
3580 // and returns an integer with the appropriate sign.
GetValue(HConstant * constant,bool is_negated)3581 static int64_t GetValue(HConstant* constant, bool is_negated) {
3582   int64_t ret = Int64FromConstant(constant);
3583   return is_negated ? -ret : ret;
3584 }
3585 
3586 // Replace code looking like
3587 //    OP1 y, x, const1
3588 //    OP2 z, y, const2
3589 // with
3590 //    OP3 z, x, const3
3591 // where OPx is either ADD or SUB, and at least one of OP{1,2} is SUB.
TrySubtractionChainSimplification(HBinaryOperation * instruction)3592 bool InstructionSimplifierVisitor::TrySubtractionChainSimplification(
3593     HBinaryOperation* instruction) {
3594   DCHECK(instruction->IsAdd() || instruction->IsSub()) << instruction->DebugName();
3595 
3596   DataType::Type type = instruction->GetType();
3597   if (!DataType::IsIntegralType(type)) {
3598     return false;
3599   }
3600 
3601   HInstruction* left = instruction->GetLeft();
3602   HInstruction* right = instruction->GetRight();
3603   // Variable names as described above.
3604   HConstant* const2 = right->IsConstant() ? right->AsConstant() : left->AsConstantOrNull();
3605   if (const2 == nullptr) {
3606     return false;
3607   }
3608 
3609   HBinaryOperation* y = (AsAddOrSubOrNull(left) != nullptr)
3610       ? left->AsBinaryOperation()
3611       : AsAddOrSubOrNull(right);
3612   // If y has more than one use, we do not perform the optimization because
3613   // it might increase code size (e.g. if the new constant is no longer
3614   // encodable as an immediate operand in the target ISA).
3615   if ((y == nullptr) || !y->HasOnlyOneNonEnvironmentUse()) {
3616     return false;
3617   }
3618 
3619   left = y->GetLeft();
3620   HConstant* const1 = left->IsConstant() ? left->AsConstant() : y->GetRight()->AsConstantOrNull();
3621   if (const1 == nullptr) {
3622     return false;
3623   }
3624 
3625   HInstruction* x = (const1 == left) ? y->GetRight() : left;
3626   // If both inputs are constants, let the constant folding pass deal with it.
3627   if (x->IsConstant()) {
3628     return false;
3629   }
3630 
3631   bool is_const2_negated = (const2 == right) && instruction->IsSub();
3632   int64_t const2_val = GetValue(const2, is_const2_negated);
3633   bool is_y_negated = (y == right) && instruction->IsSub();
3634   right = y->GetRight();
3635   bool is_const1_negated = is_y_negated ^ ((const1 == right) && y->IsSub());
3636   int64_t const1_val = GetValue(const1, is_const1_negated);
3637   bool is_x_negated = is_y_negated ^ ((x == right) && y->IsSub());
3638   int64_t const3_val = ComputeAddition(type, const1_val, const2_val);
3639   HBasicBlock* block = instruction->GetBlock();
3640   HConstant* const3 = GetGraph()->GetConstant(type, const3_val);
3641   ArenaAllocator* allocator = GetGraph()->GetAllocator();
3642   HInstruction* z;
3643 
3644   if (is_x_negated) {
3645     z = new (allocator) HSub(type, const3, x, instruction->GetDexPc());
3646   } else {
3647     z = new (allocator) HAdd(type, x, const3, instruction->GetDexPc());
3648   }
3649 
3650   block->ReplaceAndRemoveInstructionWith(instruction, z);
3651   RecordSimplification();
3652   return true;
3653 }
3654 
VisitVecMul(HVecMul * instruction)3655 void InstructionSimplifierVisitor::VisitVecMul(HVecMul* instruction) {
3656   if (TryCombineVecMultiplyAccumulate(instruction)) {
3657     RecordSimplification();
3658   }
3659 }
3660 
TryMergeNegatedInput(HBinaryOperation * op)3661 bool TryMergeNegatedInput(HBinaryOperation* op) {
3662   DCHECK(op->IsAnd() || op->IsOr() || op->IsXor()) << op->DebugName();
3663   HInstruction* left = op->GetLeft();
3664   HInstruction* right = op->GetRight();
3665 
3666   // Only consider the case where there is exactly one Not, with 2 Not's De
3667   // Morgan's laws should be applied instead.
3668   if (left->IsNot() ^ right->IsNot()) {
3669     HInstruction* hnot = (left->IsNot() ? left : right);
3670     HInstruction* hother = (left->IsNot() ? right : left);
3671 
3672     // Only do the simplification if the Not has only one use and can thus be
3673     // safely removed. Even though ARM64 negated bitwise operations do not have
3674     // an immediate variant (only register), we still do the simplification when
3675     // `hother` is a constant, because it removes an instruction if the constant
3676     // cannot be encoded as an immediate:
3677     //   mov r0, #large_constant
3678     //   neg r2, r1
3679     //   and r0, r0, r2
3680     // becomes:
3681     //   mov r0, #large_constant
3682     //   bic r0, r0, r1
3683     if (hnot->HasOnlyOneNonEnvironmentUse()) {
3684       // Replace code looking like
3685       //    NOT tmp, mask
3686       //    AND dst, src, tmp   (respectively ORR, EOR)
3687       // with
3688       //    BIC dst, src, mask  (respectively ORN, EON)
3689       HInstruction* src = hnot->AsNot()->GetInput();
3690 
3691       HBitwiseNegatedRight* neg_op = new (hnot->GetBlock()->GetGraph()->GetAllocator())
3692           HBitwiseNegatedRight(op->GetType(), op->GetKind(), hother, src, op->GetDexPc());
3693 
3694       op->GetBlock()->ReplaceAndRemoveInstructionWith(op, neg_op);
3695       hnot->GetBlock()->RemoveInstruction(hnot);
3696       return true;
3697     }
3698   }
3699 
3700   return false;
3701 }
3702 
TryMergeWithAnd(HSub * instruction)3703 bool TryMergeWithAnd(HSub* instruction) {
3704   HAnd* and_instr = instruction->GetRight()->AsAndOrNull();
3705   if (and_instr == nullptr) {
3706     return false;
3707   }
3708 
3709   HInstruction* value = instruction->GetLeft();
3710 
3711   HInstruction* left = and_instr->GetLeft();
3712   const bool left_is_equal = left == value;
3713   HInstruction* right = and_instr->GetRight();
3714   const bool right_is_equal = right == value;
3715   if (!left_is_equal && !right_is_equal) {
3716     return false;
3717   }
3718 
3719   HBitwiseNegatedRight* bnr = new (instruction->GetBlock()->GetGraph()->GetAllocator())
3720       HBitwiseNegatedRight(instruction->GetType(),
3721                            HInstruction::InstructionKind::kAnd,
3722                            value,
3723                            left_is_equal ? right : left,
3724                            instruction->GetDexPc());
3725   instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, bnr);
3726   // Since we don't run DCE after this phase, try to manually remove the And instruction.
3727   if (!and_instr->HasUses()) {
3728     and_instr->GetBlock()->RemoveInstruction(and_instr);
3729   }
3730   return true;
3731 }
3732 
3733 }  // namespace art
3734