blob: 337177fa80ad303dc0a9cbdf0b7ea8d8583e738f [file] [log] [blame]
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001/*
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
Andreas Gampec6ea7d02017-02-01 16:46:28 -080019#include "art_method-inl.h"
20#include "class_linker-inl.h"
Aart Bik71bf7b42016-11-16 10:17:46 -080021#include "escape.h"
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +010022#include "intrinsics.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000023#include "mirror/class-inl.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070024#include "scoped_thread_state_change-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070025#include "sharpening.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000026
Nicolas Geoffray3c049742014-09-24 18:10:46 +010027namespace art {
28
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +010029class InstructionSimplifierVisitor : public HGraphDelegateVisitor {
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000030 public:
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +000031 InstructionSimplifierVisitor(HGraph* graph,
32 CodeGenerator* codegen,
Vladimir Marko65979462017-05-19 17:25:12 +010033 CompilerDriver* compiler_driver,
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +000034 OptimizingCompilerStats* stats)
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +010035 : HGraphDelegateVisitor(graph),
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +000036 codegen_(codegen),
Vladimir Marko65979462017-05-19 17:25:12 +010037 compiler_driver_(compiler_driver),
Alexandre Rames188d4312015-04-09 18:30:21 +010038 stats_(stats) {}
39
40 void Run();
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000041
42 private:
Alexandre Rames188d4312015-04-09 18:30:21 +010043 void RecordSimplification() {
44 simplification_occurred_ = true;
45 simplifications_at_current_position_++;
Igor Murashkin1e065a52017-08-09 13:20:34 -070046 MaybeRecordStat(stats_, kInstructionSimplifications);
Alexandre Rames188d4312015-04-09 18:30:21 +010047 }
48
Scott Wakeling40a04bf2015-12-11 09:50:36 +000049 bool ReplaceRotateWithRor(HBinaryOperation* op, HUShr* ushr, HShl* shl);
50 bool TryReplaceWithRotate(HBinaryOperation* instruction);
51 bool TryReplaceWithRotateConstantPattern(HBinaryOperation* op, HUShr* ushr, HShl* shl);
52 bool TryReplaceWithRotateRegisterNegPattern(HBinaryOperation* op, HUShr* ushr, HShl* shl);
53 bool TryReplaceWithRotateRegisterSubPattern(HBinaryOperation* op, HUShr* ushr, HShl* shl);
54
Alexandre Rames188d4312015-04-09 18:30:21 +010055 bool TryMoveNegOnInputsAfterBinop(HBinaryOperation* binop);
Alexandre Ramesca0e3a02016-02-03 10:54:07 +000056 // `op` should be either HOr or HAnd.
57 // De Morgan's laws:
58 // ~a & ~b = ~(a | b) and ~a | ~b = ~(a & b)
59 bool TryDeMorganNegationFactoring(HBinaryOperation* op);
Anton Kirilove14dc862016-05-13 17:56:15 +010060 bool TryHandleAssociativeAndCommutativeOperation(HBinaryOperation* instruction);
61 bool TrySubtractionChainSimplification(HBinaryOperation* instruction);
Lena Djokicbc5460b2017-07-20 16:07:36 +020062 bool TryCombineVecMultiplyAccumulate(HVecMul* mul);
Anton Kirilove14dc862016-05-13 17:56:15 +010063
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000064 void VisitShift(HBinaryOperation* shift);
65
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000066 void VisitEqual(HEqual* equal) OVERRIDE;
David Brazdil0d13fee2015-04-17 14:52:19 +010067 void VisitNotEqual(HNotEqual* equal) OVERRIDE;
68 void VisitBooleanNot(HBooleanNot* bool_not) OVERRIDE;
Nicolas Geoffray07276db2015-05-18 14:22:09 +010069 void VisitInstanceFieldSet(HInstanceFieldSet* equal) OVERRIDE;
70 void VisitStaticFieldSet(HStaticFieldSet* equal) OVERRIDE;
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000071 void VisitArraySet(HArraySet* equal) OVERRIDE;
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +000072 void VisitTypeConversion(HTypeConversion* instruction) OVERRIDE;
Calin Juravle10e244f2015-01-26 18:54:32 +000073 void VisitNullCheck(HNullCheck* instruction) OVERRIDE;
Mingyao Yang0304e182015-01-30 16:41:29 -080074 void VisitArrayLength(HArrayLength* instruction) OVERRIDE;
Calin Juravleacf735c2015-02-12 15:25:22 +000075 void VisitCheckCast(HCheckCast* instruction) OVERRIDE;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000076 void VisitAdd(HAdd* instruction) OVERRIDE;
77 void VisitAnd(HAnd* instruction) OVERRIDE;
Mark Mendellc4701932015-04-10 13:18:51 -040078 void VisitCondition(HCondition* instruction) OVERRIDE;
79 void VisitGreaterThan(HGreaterThan* condition) OVERRIDE;
80 void VisitGreaterThanOrEqual(HGreaterThanOrEqual* condition) OVERRIDE;
81 void VisitLessThan(HLessThan* condition) OVERRIDE;
82 void VisitLessThanOrEqual(HLessThanOrEqual* condition) OVERRIDE;
Anton Shaminbdd79352016-02-15 12:48:36 +060083 void VisitBelow(HBelow* condition) OVERRIDE;
84 void VisitBelowOrEqual(HBelowOrEqual* condition) OVERRIDE;
85 void VisitAbove(HAbove* condition) OVERRIDE;
86 void VisitAboveOrEqual(HAboveOrEqual* condition) OVERRIDE;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000087 void VisitDiv(HDiv* instruction) OVERRIDE;
88 void VisitMul(HMul* instruction) OVERRIDE;
Alexandre Rames188d4312015-04-09 18:30:21 +010089 void VisitNeg(HNeg* instruction) OVERRIDE;
90 void VisitNot(HNot* instruction) OVERRIDE;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000091 void VisitOr(HOr* instruction) OVERRIDE;
92 void VisitShl(HShl* instruction) OVERRIDE;
93 void VisitShr(HShr* instruction) OVERRIDE;
94 void VisitSub(HSub* instruction) OVERRIDE;
95 void VisitUShr(HUShr* instruction) OVERRIDE;
96 void VisitXor(HXor* instruction) OVERRIDE;
David Brazdil74eb1b22015-12-14 11:44:01 +000097 void VisitSelect(HSelect* select) OVERRIDE;
98 void VisitIf(HIf* instruction) OVERRIDE;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +010099 void VisitInstanceOf(HInstanceOf* instruction) OVERRIDE;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +0100100 void VisitInvoke(HInvoke* invoke) OVERRIDE;
Aart Bikbb245d12015-10-19 11:05:03 -0700101 void VisitDeoptimize(HDeoptimize* deoptimize) OVERRIDE;
Lena Djokicbc5460b2017-07-20 16:07:36 +0200102 void VisitVecMul(HVecMul* instruction) OVERRIDE;
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100103
104 bool CanEnsureNotNullAt(HInstruction* instr, HInstruction* at) const;
Calin Juravleacf735c2015-02-12 15:25:22 +0000105
Roland Levillain22c49222016-03-18 14:04:28 +0000106 void SimplifyRotate(HInvoke* invoke, bool is_left, Primitive::Type type);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100107 void SimplifySystemArrayCopy(HInvoke* invoke);
108 void SimplifyStringEquals(HInvoke* invoke);
Roland Levillaina5c4a402016-03-15 15:02:50 +0000109 void SimplifyCompare(HInvoke* invoke, bool is_signum, Primitive::Type type);
Aart Bik75a38b22016-02-17 10:41:50 -0800110 void SimplifyIsNaN(HInvoke* invoke);
Aart Bik2a6aad92016-02-25 11:32:32 -0800111 void SimplifyFP2Int(HInvoke* invoke);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100112 void SimplifyStringCharAt(HInvoke* invoke);
Vladimir Markodce016e2016-04-28 13:10:02 +0100113 void SimplifyStringIsEmptyOrLength(HInvoke* invoke);
Aart Bikff7d89c2016-11-07 08:49:28 -0800114 void SimplifyNPEOnArgN(HInvoke* invoke, size_t);
Aart Bik71bf7b42016-11-16 10:17:46 -0800115 void SimplifyReturnThis(HInvoke* invoke);
116 void SimplifyAllocationIntrinsic(HInvoke* invoke);
Aart Bik11932592016-03-08 12:42:25 -0800117 void SimplifyMemBarrier(HInvoke* invoke, MemBarrierKind barrier_kind);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100118
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +0000119 CodeGenerator* codegen_;
Vladimir Marko65979462017-05-19 17:25:12 +0100120 CompilerDriver* compiler_driver_;
Calin Juravleacf735c2015-02-12 15:25:22 +0000121 OptimizingCompilerStats* stats_;
Alexandre Rames188d4312015-04-09 18:30:21 +0100122 bool simplification_occurred_ = false;
123 int simplifications_at_current_position_ = 0;
Aart Bik2767f4b2016-10-28 15:03:53 -0700124 // We ensure we do not loop infinitely. The value should not be too high, since that
125 // would allow looping around the same basic block too many times. The value should
126 // not be too low either, however, since we want to allow revisiting a basic block
127 // with many statements and simplifications at least once.
128 static constexpr int kMaxSamePositionSimplifications = 50;
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000129};
130
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100131void InstructionSimplifier::Run() {
Vladimir Marko65979462017-05-19 17:25:12 +0100132 InstructionSimplifierVisitor visitor(graph_, codegen_, compiler_driver_, stats_);
Alexandre Rames188d4312015-04-09 18:30:21 +0100133 visitor.Run();
134}
135
136void InstructionSimplifierVisitor::Run() {
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100137 // Iterate in reverse post order to open up more simplifications to users
138 // of instructions that got simplified.
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100139 for (HBasicBlock* block : GetGraph()->GetReversePostOrder()) {
Alexandre Rames188d4312015-04-09 18:30:21 +0100140 // The simplification of an instruction to another instruction may yield
141 // possibilities for other simplifications. So although we perform a reverse
142 // post order visit, we sometimes need to revisit an instruction index.
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100143 do {
144 simplification_occurred_ = false;
145 VisitBasicBlock(block);
146 } while (simplification_occurred_ &&
147 (simplifications_at_current_position_ < kMaxSamePositionSimplifications));
Alexandre Rames188d4312015-04-09 18:30:21 +0100148 simplifications_at_current_position_ = 0;
Alexandre Rames188d4312015-04-09 18:30:21 +0100149 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100150}
151
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000152namespace {
153
154bool AreAllBitsSet(HConstant* constant) {
155 return Int64FromConstant(constant) == -1;
156}
157
158} // namespace
159
Alexandre Rames188d4312015-04-09 18:30:21 +0100160// Returns true if the code was simplified to use only one negation operation
161// after the binary operation instead of one on each of the inputs.
162bool InstructionSimplifierVisitor::TryMoveNegOnInputsAfterBinop(HBinaryOperation* binop) {
163 DCHECK(binop->IsAdd() || binop->IsSub());
164 DCHECK(binop->GetLeft()->IsNeg() && binop->GetRight()->IsNeg());
165 HNeg* left_neg = binop->GetLeft()->AsNeg();
166 HNeg* right_neg = binop->GetRight()->AsNeg();
167 if (!left_neg->HasOnlyOneNonEnvironmentUse() ||
168 !right_neg->HasOnlyOneNonEnvironmentUse()) {
169 return false;
170 }
171 // Replace code looking like
172 // NEG tmp1, a
173 // NEG tmp2, b
174 // ADD dst, tmp1, tmp2
175 // with
176 // ADD tmp, a, b
177 // NEG dst, tmp
Serdjuk, Nikolay Yaae9e662015-08-21 13:26:34 +0600178 // Note that we cannot optimize `(-a) + (-b)` to `-(a + b)` for floating-point.
179 // When `a` is `-0.0` and `b` is `0.0`, the former expression yields `0.0`,
180 // while the later yields `-0.0`.
181 if (!Primitive::IsIntegralType(binop->GetType())) {
182 return false;
183 }
Alexandre Rames188d4312015-04-09 18:30:21 +0100184 binop->ReplaceInput(left_neg->GetInput(), 0);
185 binop->ReplaceInput(right_neg->GetInput(), 1);
186 left_neg->GetBlock()->RemoveInstruction(left_neg);
187 right_neg->GetBlock()->RemoveInstruction(right_neg);
188 HNeg* neg = new (GetGraph()->GetArena()) HNeg(binop->GetType(), binop);
189 binop->GetBlock()->InsertInstructionBefore(neg, binop->GetNext());
190 binop->ReplaceWithExceptInReplacementAtIndex(neg, 0);
191 RecordSimplification();
192 return true;
193}
194
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000195bool InstructionSimplifierVisitor::TryDeMorganNegationFactoring(HBinaryOperation* op) {
196 DCHECK(op->IsAnd() || op->IsOr()) << op->DebugName();
197 Primitive::Type type = op->GetType();
198 HInstruction* left = op->GetLeft();
199 HInstruction* right = op->GetRight();
200
201 // We can apply De Morgan's laws if both inputs are Not's and are only used
202 // by `op`.
Alexandre Rames9f980252016-02-05 14:00:28 +0000203 if (((left->IsNot() && right->IsNot()) ||
204 (left->IsBooleanNot() && right->IsBooleanNot())) &&
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000205 left->HasOnlyOneNonEnvironmentUse() &&
206 right->HasOnlyOneNonEnvironmentUse()) {
207 // Replace code looking like
208 // NOT nota, a
209 // NOT notb, b
210 // AND dst, nota, notb (respectively OR)
211 // with
212 // OR or, a, b (respectively AND)
213 // NOT dest, or
Alexandre Rames9f980252016-02-05 14:00:28 +0000214 HInstruction* src_left = left->InputAt(0);
215 HInstruction* src_right = right->InputAt(0);
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000216 uint32_t dex_pc = op->GetDexPc();
217
218 // Remove the negations on the inputs.
219 left->ReplaceWith(src_left);
220 right->ReplaceWith(src_right);
221 left->GetBlock()->RemoveInstruction(left);
222 right->GetBlock()->RemoveInstruction(right);
223
224 // Replace the `HAnd` or `HOr`.
225 HBinaryOperation* hbin;
226 if (op->IsAnd()) {
227 hbin = new (GetGraph()->GetArena()) HOr(type, src_left, src_right, dex_pc);
228 } else {
229 hbin = new (GetGraph()->GetArena()) HAnd(type, src_left, src_right, dex_pc);
230 }
Alexandre Rames9f980252016-02-05 14:00:28 +0000231 HInstruction* hnot;
232 if (left->IsBooleanNot()) {
233 hnot = new (GetGraph()->GetArena()) HBooleanNot(hbin, dex_pc);
234 } else {
235 hnot = new (GetGraph()->GetArena()) HNot(type, hbin, dex_pc);
236 }
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000237
238 op->GetBlock()->InsertInstructionBefore(hbin, op);
239 op->GetBlock()->ReplaceAndRemoveInstructionWith(op, hnot);
240
241 RecordSimplification();
242 return true;
243 }
244
245 return false;
246}
247
Lena Djokicbc5460b2017-07-20 16:07:36 +0200248bool InstructionSimplifierVisitor::TryCombineVecMultiplyAccumulate(HVecMul* mul) {
249 Primitive::Type type = mul->GetPackedType();
250 InstructionSet isa = codegen_->GetInstructionSet();
251 switch (isa) {
252 case kArm64:
253 if (!(type == Primitive::kPrimByte ||
254 type == Primitive::kPrimChar ||
255 type == Primitive::kPrimShort ||
256 type == Primitive::kPrimInt)) {
257 return false;
258 }
259 break;
260 case kMips:
261 case kMips64:
262 if (!(type == Primitive::kPrimByte ||
263 type == Primitive::kPrimChar ||
264 type == Primitive::kPrimShort ||
265 type == Primitive::kPrimInt ||
266 type == Primitive::kPrimLong)) {
267 return false;
268 }
269 break;
270 default:
271 return false;
272 }
273
274 ArenaAllocator* arena = mul->GetBlock()->GetGraph()->GetArena();
275
276 if (mul->HasOnlyOneNonEnvironmentUse()) {
277 HInstruction* use = mul->GetUses().front().GetUser();
278 if (use->IsVecAdd() || use->IsVecSub()) {
279 // Replace code looking like
280 // VECMUL tmp, x, y
281 // VECADD/SUB dst, acc, tmp
282 // with
283 // VECMULACC dst, acc, x, y
284 // Note that we do not want to (unconditionally) perform the merge when the
285 // multiplication has multiple uses and it can be merged in all of them.
286 // Multiple uses could happen on the same control-flow path, and we would
287 // then increase the amount of work. In the future we could try to evaluate
288 // whether all uses are on different control-flow paths (using dominance and
289 // reverse-dominance information) and only perform the merge when they are.
290 HInstruction* accumulator = nullptr;
291 HVecBinaryOperation* binop = use->AsVecBinaryOperation();
292 HInstruction* binop_left = binop->GetLeft();
293 HInstruction* binop_right = binop->GetRight();
294 // This is always true since the `HVecMul` has only one use (which is checked above).
295 DCHECK_NE(binop_left, binop_right);
296 if (binop_right == mul) {
297 accumulator = binop_left;
298 } else if (use->IsVecAdd()) {
299 DCHECK_EQ(binop_left, mul);
300 accumulator = binop_right;
301 }
302
303 HInstruction::InstructionKind kind =
304 use->IsVecAdd() ? HInstruction::kAdd : HInstruction::kSub;
305 if (accumulator != nullptr) {
306 HVecMultiplyAccumulate* mulacc =
307 new (arena) HVecMultiplyAccumulate(arena,
308 kind,
309 accumulator,
310 mul->GetLeft(),
311 mul->GetRight(),
312 binop->GetPackedType(),
313 binop->GetVectorLength());
314
315 binop->GetBlock()->ReplaceAndRemoveInstructionWith(binop, mulacc);
316 DCHECK(!mul->HasUses());
317 mul->GetBlock()->RemoveInstruction(mul);
318 return true;
319 }
320 }
321 }
322
323 return false;
324}
325
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000326void InstructionSimplifierVisitor::VisitShift(HBinaryOperation* instruction) {
327 DCHECK(instruction->IsShl() || instruction->IsShr() || instruction->IsUShr());
Alexandre Rames50518442016-06-27 11:39:19 +0100328 HInstruction* shift_amount = instruction->GetRight();
329 HInstruction* value = instruction->GetLeft();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000330
Alexandre Rames50518442016-06-27 11:39:19 +0100331 int64_t implicit_mask = (value->GetType() == Primitive::kPrimLong)
332 ? kMaxLongShiftDistance
333 : kMaxIntShiftDistance;
334
335 if (shift_amount->IsConstant()) {
336 int64_t cst = Int64FromConstant(shift_amount->AsConstant());
Aart Bik50e20d52017-05-05 14:07:29 -0700337 int64_t masked_cst = cst & implicit_mask;
338 if (masked_cst == 0) {
Mark Mendellba56d062015-05-05 21:34:03 -0400339 // Replace code looking like
Alexandre Rames50518442016-06-27 11:39:19 +0100340 // SHL dst, value, 0
Mark Mendellba56d062015-05-05 21:34:03 -0400341 // with
Alexandre Rames50518442016-06-27 11:39:19 +0100342 // value
343 instruction->ReplaceWith(value);
Mark Mendellba56d062015-05-05 21:34:03 -0400344 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +0100345 RecordSimplification();
Alexandre Rames50518442016-06-27 11:39:19 +0100346 return;
Aart Bik50e20d52017-05-05 14:07:29 -0700347 } else if (masked_cst != cst) {
348 // Replace code looking like
349 // SHL dst, value, cst
350 // where cst exceeds maximum distance with the equivalent
351 // SHL dst, value, cst & implicit_mask
352 // (as defined by shift semantics). This ensures other
353 // optimizations do not need to special case for such situations.
354 DCHECK_EQ(shift_amount->GetType(), Primitive::kPrimInt);
355 instruction->ReplaceInput(GetGraph()->GetIntConstant(masked_cst), /* index */ 1);
356 RecordSimplification();
357 return;
Alexandre Rames50518442016-06-27 11:39:19 +0100358 }
359 }
360
361 // Shift operations implicitly mask the shift amount according to the type width. Get rid of
362 // unnecessary explicit masking operations on the shift amount.
363 // Replace code looking like
364 // AND masked_shift, shift, <superset of implicit mask>
365 // SHL dst, value, masked_shift
366 // with
367 // SHL dst, value, shift
368 if (shift_amount->IsAnd()) {
369 HAnd* and_insn = shift_amount->AsAnd();
370 HConstant* mask = and_insn->GetConstantRight();
371 if ((mask != nullptr) && ((Int64FromConstant(mask) & implicit_mask) == implicit_mask)) {
372 instruction->ReplaceInput(and_insn->GetLeastConstantLeft(), 1);
373 RecordSimplification();
Mark Mendellba56d062015-05-05 21:34:03 -0400374 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000375 }
376}
377
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000378static bool IsSubRegBitsMinusOther(HSub* sub, size_t reg_bits, HInstruction* other) {
379 return (sub->GetRight() == other &&
380 sub->GetLeft()->IsConstant() &&
381 (Int64FromConstant(sub->GetLeft()->AsConstant()) & (reg_bits - 1)) == 0);
382}
383
384bool InstructionSimplifierVisitor::ReplaceRotateWithRor(HBinaryOperation* op,
385 HUShr* ushr,
386 HShl* shl) {
Roland Levillain22c49222016-03-18 14:04:28 +0000387 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr()) << op->DebugName();
388 HRor* ror = new (GetGraph()->GetArena()) HRor(ushr->GetType(), ushr->GetLeft(), ushr->GetRight());
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000389 op->GetBlock()->ReplaceAndRemoveInstructionWith(op, ror);
390 if (!ushr->HasUses()) {
391 ushr->GetBlock()->RemoveInstruction(ushr);
392 }
393 if (!ushr->GetRight()->HasUses()) {
394 ushr->GetRight()->GetBlock()->RemoveInstruction(ushr->GetRight());
395 }
396 if (!shl->HasUses()) {
397 shl->GetBlock()->RemoveInstruction(shl);
398 }
399 if (!shl->GetRight()->HasUses()) {
400 shl->GetRight()->GetBlock()->RemoveInstruction(shl->GetRight());
401 }
Alexandre Ramesc5809c32016-05-25 15:01:06 +0100402 RecordSimplification();
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000403 return true;
404}
405
406// Try to replace a binary operation flanked by one UShr and one Shl with a bitfield rotation.
407bool InstructionSimplifierVisitor::TryReplaceWithRotate(HBinaryOperation* op) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000408 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
409 HInstruction* left = op->GetLeft();
410 HInstruction* right = op->GetRight();
411 // If we have an UShr and a Shl (in either order).
412 if ((left->IsUShr() && right->IsShl()) || (left->IsShl() && right->IsUShr())) {
413 HUShr* ushr = left->IsUShr() ? left->AsUShr() : right->AsUShr();
414 HShl* shl = left->IsShl() ? left->AsShl() : right->AsShl();
415 DCHECK(Primitive::IsIntOrLongType(ushr->GetType()));
416 if (ushr->GetType() == shl->GetType() &&
417 ushr->GetLeft() == shl->GetLeft()) {
418 if (ushr->GetRight()->IsConstant() && shl->GetRight()->IsConstant()) {
419 // Shift distances are both constant, try replacing with Ror if they
420 // add up to the register size.
421 return TryReplaceWithRotateConstantPattern(op, ushr, shl);
422 } else if (ushr->GetRight()->IsSub() || shl->GetRight()->IsSub()) {
423 // Shift distances are potentially of the form x and (reg_size - x).
424 return TryReplaceWithRotateRegisterSubPattern(op, ushr, shl);
425 } else if (ushr->GetRight()->IsNeg() || shl->GetRight()->IsNeg()) {
426 // Shift distances are potentially of the form d and -d.
427 return TryReplaceWithRotateRegisterNegPattern(op, ushr, shl);
428 }
429 }
430 }
431 return false;
432}
433
434// Try replacing code looking like (x >>> #rdist OP x << #ldist):
435// UShr dst, x, #rdist
436// Shl tmp, x, #ldist
437// OP dst, dst, tmp
438// or like (x >>> #rdist OP x << #-ldist):
439// UShr dst, x, #rdist
440// Shl tmp, x, #-ldist
441// OP dst, dst, tmp
442// with
443// Ror dst, x, #rdist
444bool InstructionSimplifierVisitor::TryReplaceWithRotateConstantPattern(HBinaryOperation* op,
445 HUShr* ushr,
446 HShl* shl) {
447 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
448 size_t reg_bits = Primitive::ComponentSize(ushr->GetType()) * kBitsPerByte;
449 size_t rdist = Int64FromConstant(ushr->GetRight()->AsConstant());
450 size_t ldist = Int64FromConstant(shl->GetRight()->AsConstant());
451 if (((ldist + rdist) & (reg_bits - 1)) == 0) {
452 ReplaceRotateWithRor(op, ushr, shl);
453 return true;
454 }
455 return false;
456}
457
458// Replace code looking like (x >>> -d OP x << d):
459// Neg neg, d
460// UShr dst, x, neg
461// Shl tmp, x, d
462// OP dst, dst, tmp
463// with
464// Neg neg, d
465// Ror dst, x, neg
466// *** OR ***
467// Replace code looking like (x >>> d OP x << -d):
468// UShr dst, x, d
469// Neg neg, d
470// Shl tmp, x, neg
471// OP dst, dst, tmp
472// with
473// Ror dst, x, d
474bool InstructionSimplifierVisitor::TryReplaceWithRotateRegisterNegPattern(HBinaryOperation* op,
475 HUShr* ushr,
476 HShl* shl) {
477 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
478 DCHECK(ushr->GetRight()->IsNeg() || shl->GetRight()->IsNeg());
479 bool neg_is_left = shl->GetRight()->IsNeg();
480 HNeg* neg = neg_is_left ? shl->GetRight()->AsNeg() : ushr->GetRight()->AsNeg();
481 // And the shift distance being negated is the distance being shifted the other way.
482 if (neg->InputAt(0) == (neg_is_left ? ushr->GetRight() : shl->GetRight())) {
483 ReplaceRotateWithRor(op, ushr, shl);
484 }
485 return false;
486}
487
488// Try replacing code looking like (x >>> d OP x << (#bits - d)):
489// UShr dst, x, d
490// Sub ld, #bits, d
491// Shl tmp, x, ld
492// OP dst, dst, tmp
493// with
494// Ror dst, x, d
495// *** OR ***
496// Replace code looking like (x >>> (#bits - d) OP x << d):
497// Sub rd, #bits, d
498// UShr dst, x, rd
499// Shl tmp, x, d
500// OP dst, dst, tmp
501// with
502// Neg neg, d
503// Ror dst, x, neg
504bool InstructionSimplifierVisitor::TryReplaceWithRotateRegisterSubPattern(HBinaryOperation* op,
505 HUShr* ushr,
506 HShl* shl) {
507 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
508 DCHECK(ushr->GetRight()->IsSub() || shl->GetRight()->IsSub());
509 size_t reg_bits = Primitive::ComponentSize(ushr->GetType()) * kBitsPerByte;
510 HInstruction* shl_shift = shl->GetRight();
511 HInstruction* ushr_shift = ushr->GetRight();
512 if ((shl_shift->IsSub() && IsSubRegBitsMinusOther(shl_shift->AsSub(), reg_bits, ushr_shift)) ||
513 (ushr_shift->IsSub() && IsSubRegBitsMinusOther(ushr_shift->AsSub(), reg_bits, shl_shift))) {
514 return ReplaceRotateWithRor(op, ushr, shl);
515 }
516 return false;
517}
518
Calin Juravle10e244f2015-01-26 18:54:32 +0000519void InstructionSimplifierVisitor::VisitNullCheck(HNullCheck* null_check) {
520 HInstruction* obj = null_check->InputAt(0);
521 if (!obj->CanBeNull()) {
522 null_check->ReplaceWith(obj);
523 null_check->GetBlock()->RemoveInstruction(null_check);
Calin Juravleacf735c2015-02-12 15:25:22 +0000524 if (stats_ != nullptr) {
525 stats_->RecordStat(MethodCompilationStat::kRemovedNullCheck);
526 }
527 }
528}
529
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100530bool InstructionSimplifierVisitor::CanEnsureNotNullAt(HInstruction* input, HInstruction* at) const {
531 if (!input->CanBeNull()) {
532 return true;
533 }
534
Vladimir Marko46817b82016-03-29 12:21:58 +0100535 for (const HUseListNode<HInstruction*>& use : input->GetUses()) {
536 HInstruction* user = use.GetUser();
537 if (user->IsNullCheck() && user->StrictlyDominates(at)) {
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +0100538 return true;
539 }
540 }
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100541
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +0100542 return false;
543}
544
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100545// Returns whether doing a type test between the class of `object` against `klass` has
546// a statically known outcome. The result of the test is stored in `outcome`.
547static bool TypeCheckHasKnownOutcome(HLoadClass* klass, HInstruction* object, bool* outcome) {
Calin Juravle2e768302015-07-28 14:41:11 +0000548 DCHECK(!object->IsNullConstant()) << "Null constants should be special cased";
549 ReferenceTypeInfo obj_rti = object->GetReferenceTypeInfo();
550 ScopedObjectAccess soa(Thread::Current());
551 if (!obj_rti.IsValid()) {
552 // We run the simplifier before the reference type propagation so type info might not be
553 // available.
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100554 return false;
Calin Juravleacf735c2015-02-12 15:25:22 +0000555 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100556
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100557 ReferenceTypeInfo class_rti = klass->GetLoadedClassRTI();
Calin Juravle98893e12015-10-02 21:05:03 +0100558 if (!class_rti.IsValid()) {
559 // Happens when the loaded class is unresolved.
560 return false;
561 }
562 DCHECK(class_rti.IsExact());
Calin Juravleacf735c2015-02-12 15:25:22 +0000563 if (class_rti.IsSupertypeOf(obj_rti)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100564 *outcome = true;
565 return true;
566 } else if (obj_rti.IsExact()) {
567 // The test failed at compile time so will also fail at runtime.
568 *outcome = false;
569 return true;
Nicolas Geoffray7cb499b2015-06-17 11:35:11 +0100570 } else if (!class_rti.IsInterface()
571 && !obj_rti.IsInterface()
572 && !obj_rti.IsSupertypeOf(class_rti)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100573 // Different type hierarchy. The test will fail.
574 *outcome = false;
575 return true;
576 }
577 return false;
578}
579
580void InstructionSimplifierVisitor::VisitCheckCast(HCheckCast* check_cast) {
581 HInstruction* object = check_cast->InputAt(0);
Calin Juravlee53fb552015-10-07 17:51:52 +0100582 HLoadClass* load_class = check_cast->InputAt(1)->AsLoadClass();
583 if (load_class->NeedsAccessCheck()) {
584 // If we need to perform an access check we cannot remove the instruction.
585 return;
586 }
587
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100588 if (CanEnsureNotNullAt(object, check_cast)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100589 check_cast->ClearMustDoNullCheck();
590 }
591
592 if (object->IsNullConstant()) {
Calin Juravleacf735c2015-02-12 15:25:22 +0000593 check_cast->GetBlock()->RemoveInstruction(check_cast);
Igor Murashkin1e065a52017-08-09 13:20:34 -0700594 MaybeRecordStat(stats_, MethodCompilationStat::kRemovedCheckedCast);
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100595 return;
596 }
597
Vladimir Markoa65ed302016-03-14 21:21:29 +0000598 // Note: The `outcome` is initialized to please valgrind - the compiler can reorder
599 // the return value check with the `outcome` check, b/27651442 .
600 bool outcome = false;
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700601 if (TypeCheckHasKnownOutcome(load_class, object, &outcome)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100602 if (outcome) {
603 check_cast->GetBlock()->RemoveInstruction(check_cast);
Igor Murashkin1e065a52017-08-09 13:20:34 -0700604 MaybeRecordStat(stats_, MethodCompilationStat::kRemovedCheckedCast);
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700605 if (!load_class->HasUses()) {
606 // We cannot rely on DCE to remove the class because the `HLoadClass` thinks it can throw.
607 // However, here we know that it cannot because the checkcast was successfull, hence
608 // the class was already loaded.
609 load_class->GetBlock()->RemoveInstruction(load_class);
610 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100611 } else {
612 // Don't do anything for exceptional cases for now. Ideally we should remove
613 // all instructions and blocks this instruction dominates.
614 }
Calin Juravle10e244f2015-01-26 18:54:32 +0000615 }
616}
617
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100618void InstructionSimplifierVisitor::VisitInstanceOf(HInstanceOf* instruction) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100619 HInstruction* object = instruction->InputAt(0);
Calin Juravlee53fb552015-10-07 17:51:52 +0100620 HLoadClass* load_class = instruction->InputAt(1)->AsLoadClass();
621 if (load_class->NeedsAccessCheck()) {
622 // If we need to perform an access check we cannot remove the instruction.
623 return;
624 }
625
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100626 bool can_be_null = true;
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100627 if (CanEnsureNotNullAt(object, instruction)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100628 can_be_null = false;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100629 instruction->ClearMustDoNullCheck();
630 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100631
632 HGraph* graph = GetGraph();
633 if (object->IsNullConstant()) {
Igor Murashkin1e065a52017-08-09 13:20:34 -0700634 MaybeRecordStat(stats_, kRemovedInstanceOf);
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100635 instruction->ReplaceWith(graph->GetIntConstant(0));
636 instruction->GetBlock()->RemoveInstruction(instruction);
637 RecordSimplification();
638 return;
639 }
640
Vladimir Marko24bd8952016-03-15 10:40:33 +0000641 // Note: The `outcome` is initialized to please valgrind - the compiler can reorder
642 // the return value check with the `outcome` check, b/27651442 .
643 bool outcome = false;
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700644 if (TypeCheckHasKnownOutcome(load_class, object, &outcome)) {
Igor Murashkin1e065a52017-08-09 13:20:34 -0700645 MaybeRecordStat(stats_, kRemovedInstanceOf);
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100646 if (outcome && can_be_null) {
647 // Type test will succeed, we just need a null test.
648 HNotEqual* test = new (graph->GetArena()) HNotEqual(graph->GetNullConstant(), object);
649 instruction->GetBlock()->InsertInstructionBefore(test, instruction);
650 instruction->ReplaceWith(test);
651 } else {
652 // We've statically determined the result of the instanceof.
653 instruction->ReplaceWith(graph->GetIntConstant(outcome));
654 }
655 RecordSimplification();
656 instruction->GetBlock()->RemoveInstruction(instruction);
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700657 if (outcome && !load_class->HasUses()) {
658 // We cannot rely on DCE to remove the class because the `HLoadClass` thinks it can throw.
659 // However, here we know that it cannot because the instanceof check was successfull, hence
660 // the class was already loaded.
661 load_class->GetBlock()->RemoveInstruction(load_class);
662 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100663 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100664}
665
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100666void InstructionSimplifierVisitor::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
667 if ((instruction->GetValue()->GetType() == Primitive::kPrimNot)
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100668 && CanEnsureNotNullAt(instruction->GetValue(), instruction)) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100669 instruction->ClearValueCanBeNull();
670 }
671}
672
673void InstructionSimplifierVisitor::VisitStaticFieldSet(HStaticFieldSet* instruction) {
674 if ((instruction->GetValue()->GetType() == Primitive::kPrimNot)
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100675 && CanEnsureNotNullAt(instruction->GetValue(), instruction)) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100676 instruction->ClearValueCanBeNull();
677 }
678}
679
Anton Shaminbdd79352016-02-15 12:48:36 +0600680static HCondition* GetOppositeConditionSwapOps(ArenaAllocator* arena, HInstruction* cond) {
681 HInstruction *lhs = cond->InputAt(0);
682 HInstruction *rhs = cond->InputAt(1);
683 switch (cond->GetKind()) {
684 case HInstruction::kEqual:
685 return new (arena) HEqual(rhs, lhs);
686 case HInstruction::kNotEqual:
687 return new (arena) HNotEqual(rhs, lhs);
688 case HInstruction::kLessThan:
689 return new (arena) HGreaterThan(rhs, lhs);
690 case HInstruction::kLessThanOrEqual:
691 return new (arena) HGreaterThanOrEqual(rhs, lhs);
692 case HInstruction::kGreaterThan:
693 return new (arena) HLessThan(rhs, lhs);
694 case HInstruction::kGreaterThanOrEqual:
695 return new (arena) HLessThanOrEqual(rhs, lhs);
696 case HInstruction::kBelow:
697 return new (arena) HAbove(rhs, lhs);
698 case HInstruction::kBelowOrEqual:
699 return new (arena) HAboveOrEqual(rhs, lhs);
700 case HInstruction::kAbove:
701 return new (arena) HBelow(rhs, lhs);
702 case HInstruction::kAboveOrEqual:
703 return new (arena) HBelowOrEqual(rhs, lhs);
704 default:
705 LOG(FATAL) << "Unknown ConditionType " << cond->GetKind();
706 }
707 return nullptr;
708}
709
Aart Bik2767f4b2016-10-28 15:03:53 -0700710static bool CmpHasBoolType(HInstruction* input, HInstruction* cmp) {
711 if (input->GetType() == Primitive::kPrimBoolean) {
712 return true; // input has direct boolean type
713 } else if (cmp->GetUses().HasExactlyOneElement()) {
714 // Comparison also has boolean type if both its input and the instruction
715 // itself feed into the same phi node.
716 HInstruction* user = cmp->GetUses().front().GetUser();
717 return user->IsPhi() && user->HasInput(input) && user->HasInput(cmp);
718 }
719 return false;
720}
721
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000722void InstructionSimplifierVisitor::VisitEqual(HEqual* equal) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100723 HInstruction* input_const = equal->GetConstantRight();
724 if (input_const != nullptr) {
725 HInstruction* input_value = equal->GetLeastConstantLeft();
Aart Bik2767f4b2016-10-28 15:03:53 -0700726 if (CmpHasBoolType(input_value, equal) && input_const->IsIntConstant()) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100727 HBasicBlock* block = equal->GetBlock();
Nicolas Geoffray3c4ab802015-06-19 11:42:07 +0100728 // We are comparing the boolean to a constant which is of type int and can
729 // be any constant.
Roland Levillain1a653882016-03-18 18:05:57 +0000730 if (input_const->AsIntConstant()->IsTrue()) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100731 // Replace (bool_value == true) with bool_value
732 equal->ReplaceWith(input_value);
733 block->RemoveInstruction(equal);
734 RecordSimplification();
Roland Levillain1a653882016-03-18 18:05:57 +0000735 } else if (input_const->AsIntConstant()->IsFalse()) {
Aart Bik2767f4b2016-10-28 15:03:53 -0700736 // Replace (bool_value == false) with !bool_value
Mark Mendellf6529172015-11-17 11:16:56 -0500737 equal->ReplaceWith(GetGraph()->InsertOppositeCondition(input_value, equal));
738 block->RemoveInstruction(equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100739 RecordSimplification();
David Brazdil1e9ec052015-06-22 10:26:45 +0100740 } else {
741 // Replace (bool_value == integer_not_zero_nor_one_constant) with false
742 equal->ReplaceWith(GetGraph()->GetIntConstant(0));
743 block->RemoveInstruction(equal);
744 RecordSimplification();
David Brazdil0d13fee2015-04-17 14:52:19 +0100745 }
Mark Mendellc4701932015-04-10 13:18:51 -0400746 } else {
747 VisitCondition(equal);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100748 }
Mark Mendellc4701932015-04-10 13:18:51 -0400749 } else {
750 VisitCondition(equal);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100751 }
752}
753
David Brazdil0d13fee2015-04-17 14:52:19 +0100754void InstructionSimplifierVisitor::VisitNotEqual(HNotEqual* not_equal) {
755 HInstruction* input_const = not_equal->GetConstantRight();
756 if (input_const != nullptr) {
757 HInstruction* input_value = not_equal->GetLeastConstantLeft();
Aart Bik2767f4b2016-10-28 15:03:53 -0700758 if (CmpHasBoolType(input_value, not_equal) && input_const->IsIntConstant()) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100759 HBasicBlock* block = not_equal->GetBlock();
Nicolas Geoffray3c4ab802015-06-19 11:42:07 +0100760 // We are comparing the boolean to a constant which is of type int and can
761 // be any constant.
Roland Levillain1a653882016-03-18 18:05:57 +0000762 if (input_const->AsIntConstant()->IsTrue()) {
Aart Bik2767f4b2016-10-28 15:03:53 -0700763 // Replace (bool_value != true) with !bool_value
Mark Mendellf6529172015-11-17 11:16:56 -0500764 not_equal->ReplaceWith(GetGraph()->InsertOppositeCondition(input_value, not_equal));
765 block->RemoveInstruction(not_equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100766 RecordSimplification();
Roland Levillain1a653882016-03-18 18:05:57 +0000767 } else if (input_const->AsIntConstant()->IsFalse()) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100768 // Replace (bool_value != false) with bool_value
David Brazdil0d13fee2015-04-17 14:52:19 +0100769 not_equal->ReplaceWith(input_value);
770 block->RemoveInstruction(not_equal);
771 RecordSimplification();
David Brazdil1e9ec052015-06-22 10:26:45 +0100772 } else {
773 // Replace (bool_value != integer_not_zero_nor_one_constant) with true
774 not_equal->ReplaceWith(GetGraph()->GetIntConstant(1));
775 block->RemoveInstruction(not_equal);
776 RecordSimplification();
David Brazdil0d13fee2015-04-17 14:52:19 +0100777 }
Mark Mendellc4701932015-04-10 13:18:51 -0400778 } else {
779 VisitCondition(not_equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100780 }
Mark Mendellc4701932015-04-10 13:18:51 -0400781 } else {
782 VisitCondition(not_equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100783 }
784}
785
786void InstructionSimplifierVisitor::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000787 HInstruction* input = bool_not->InputAt(0);
788 HInstruction* replace_with = nullptr;
789
790 if (input->IsIntConstant()) {
791 // Replace !(true/false) with false/true.
Roland Levillain1a653882016-03-18 18:05:57 +0000792 if (input->AsIntConstant()->IsTrue()) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000793 replace_with = GetGraph()->GetIntConstant(0);
794 } else {
Roland Levillain1a653882016-03-18 18:05:57 +0000795 DCHECK(input->AsIntConstant()->IsFalse()) << input->AsIntConstant()->GetValue();
David Brazdil74eb1b22015-12-14 11:44:01 +0000796 replace_with = GetGraph()->GetIntConstant(1);
797 }
798 } else if (input->IsBooleanNot()) {
799 // Replace (!(!bool_value)) with bool_value.
800 replace_with = input->InputAt(0);
801 } else if (input->IsCondition() &&
802 // Don't change FP compares. The definition of compares involving
803 // NaNs forces the compares to be done as written by the user.
804 !Primitive::IsFloatingPointType(input->InputAt(0)->GetType())) {
805 // Replace condition with its opposite.
806 replace_with = GetGraph()->InsertOppositeCondition(input->AsCondition(), bool_not);
807 }
808
809 if (replace_with != nullptr) {
810 bool_not->ReplaceWith(replace_with);
David Brazdil0d13fee2015-04-17 14:52:19 +0100811 bool_not->GetBlock()->RemoveInstruction(bool_not);
David Brazdil74eb1b22015-12-14 11:44:01 +0000812 RecordSimplification();
813 }
814}
815
Aart Bik4f7dd342017-09-12 13:12:57 -0700816// Constructs a new ABS(x) node in the HIR.
817static HInstruction* NewIntegralAbs(ArenaAllocator* arena, HInstruction* x, HInstruction* cursor) {
818 Primitive::Type type = x->GetType();
819 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
820 // Construct a fake intrinsic with as much context as is needed to allocate one.
821 // The intrinsic will always be lowered into code later anyway.
822 // TODO: b/65164101 : moving towards a real HAbs node makes more sense.
823 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
824 HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress,
825 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
826 0u
827 };
828 HInvokeStaticOrDirect* invoke = new (arena) HInvokeStaticOrDirect(
829 arena,
830 1,
831 type,
832 x->GetDexPc(),
833 /*method_idx*/ -1,
834 /*resolved_method*/ nullptr,
835 dispatch_info,
836 kStatic,
837 MethodReference(nullptr, dex::kDexNoIndex),
838 HInvokeStaticOrDirect::ClinitCheckRequirement::kNone);
839 invoke->SetArgumentAt(0, x);
840 invoke->SetIntrinsic(type == Primitive::kPrimInt ? Intrinsics::kMathAbsInt
841 : Intrinsics::kMathAbsLong,
842 kNoEnvironmentOrCache,
843 kNoSideEffects,
844 kNoThrow);
845 cursor->GetBlock()->InsertInstructionBefore(invoke, cursor);
846 return invoke;
847}
848
849// Returns true if operands a and b consists of widening type conversions
850// (either explicit or implicit) to the given to_type.
851static bool AreLowerPrecisionArgs(Primitive::Type to_type, HInstruction* a, HInstruction* b) {
852 if (a->IsTypeConversion() && a->GetType() == to_type) {
853 a = a->InputAt(0);
854 }
855 if (b->IsTypeConversion() && b->GetType() == to_type) {
856 b = b->InputAt(0);
857 }
858 Primitive::Type type1 = a->GetType();
859 Primitive::Type type2 = b->GetType();
860 return (type1 == Primitive::kPrimByte && type2 == Primitive::kPrimByte) ||
861 (type1 == Primitive::kPrimShort && type2 == Primitive::kPrimShort) ||
862 (type1 == Primitive::kPrimChar && type2 == Primitive::kPrimChar) ||
863 (type1 == Primitive::kPrimInt && type2 == Primitive::kPrimInt &&
864 to_type == Primitive::kPrimLong);
865}
866
David Brazdil74eb1b22015-12-14 11:44:01 +0000867void InstructionSimplifierVisitor::VisitSelect(HSelect* select) {
868 HInstruction* replace_with = nullptr;
869 HInstruction* condition = select->GetCondition();
870 HInstruction* true_value = select->GetTrueValue();
871 HInstruction* false_value = select->GetFalseValue();
872
873 if (condition->IsBooleanNot()) {
874 // Change ((!cond) ? x : y) to (cond ? y : x).
875 condition = condition->InputAt(0);
876 std::swap(true_value, false_value);
877 select->ReplaceInput(false_value, 0);
878 select->ReplaceInput(true_value, 1);
879 select->ReplaceInput(condition, 2);
880 RecordSimplification();
881 }
882
883 if (true_value == false_value) {
884 // Replace (cond ? x : x) with (x).
885 replace_with = true_value;
886 } else if (condition->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +0000887 if (condition->AsIntConstant()->IsTrue()) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000888 // Replace (true ? x : y) with (x).
889 replace_with = true_value;
890 } else {
891 // Replace (false ? x : y) with (y).
Roland Levillain1a653882016-03-18 18:05:57 +0000892 DCHECK(condition->AsIntConstant()->IsFalse()) << condition->AsIntConstant()->GetValue();
David Brazdil74eb1b22015-12-14 11:44:01 +0000893 replace_with = false_value;
894 }
895 } else if (true_value->IsIntConstant() && false_value->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +0000896 if (true_value->AsIntConstant()->IsTrue() && false_value->AsIntConstant()->IsFalse()) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000897 // Replace (cond ? true : false) with (cond).
898 replace_with = condition;
Roland Levillain1a653882016-03-18 18:05:57 +0000899 } else if (true_value->AsIntConstant()->IsFalse() && false_value->AsIntConstant()->IsTrue()) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000900 // Replace (cond ? false : true) with (!cond).
901 replace_with = GetGraph()->InsertOppositeCondition(condition, select);
902 }
Aart Bik4f7dd342017-09-12 13:12:57 -0700903 } else if (condition->IsCondition()) {
904 IfCondition cmp = condition->AsCondition()->GetCondition();
905 HInstruction* a = condition->InputAt(0);
906 HInstruction* b = condition->InputAt(1);
907 Primitive::Type t_type = true_value->GetType();
908 Primitive::Type f_type = false_value->GetType();
909 // Here we have a <cmp> b ? true_value : false_value.
910 // Test if both values are same-typed int or long.
911 if (t_type == f_type && (t_type == Primitive::kPrimInt || t_type == Primitive::kPrimLong)) {
912 // Try to replace typical integral ABS constructs.
913 if (true_value->IsNeg()) {
914 HInstruction* negated = true_value->InputAt(0);
915 if ((cmp == kCondLT || cmp == kCondLE) &&
916 (a == negated && a == false_value && IsInt64Value(b, 0))) {
917 // Found a < 0 ? -a : a which can be replaced by ABS(a).
918 replace_with = NewIntegralAbs(GetGraph()->GetArena(), false_value, select);
919 }
920 } else if (false_value->IsNeg()) {
921 HInstruction* negated = false_value->InputAt(0);
922 if ((cmp == kCondGT || cmp == kCondGE) &&
923 (a == true_value && a == negated && IsInt64Value(b, 0))) {
924 // Found a > 0 ? a : -a which can be replaced by ABS(a).
925 replace_with = NewIntegralAbs(GetGraph()->GetArena(), true_value, select);
926 }
927 } else if (true_value->IsSub() && false_value->IsSub()) {
928 HInstruction* true_sub1 = true_value->InputAt(0);
929 HInstruction* true_sub2 = true_value->InputAt(1);
930 HInstruction* false_sub1 = false_value->InputAt(0);
931 HInstruction* false_sub2 = false_value->InputAt(1);
932 if ((((cmp == kCondGT || cmp == kCondGE) &&
933 (a == true_sub1 && b == true_sub2 && a == false_sub2 && b == false_sub1)) ||
934 ((cmp == kCondLT || cmp == kCondLE) &&
935 (a == true_sub2 && b == true_sub1 && a == false_sub1 && b == false_sub2))) &&
936 AreLowerPrecisionArgs(t_type, a, b)) {
937 // Found a > b ? a - b : b - a or
938 // a < b ? b - a : a - b
939 // which can be replaced by ABS(a - b) for lower precision operands a, b.
940 replace_with = NewIntegralAbs(GetGraph()->GetArena(), true_value, select);
941 }
942 }
943 }
David Brazdil74eb1b22015-12-14 11:44:01 +0000944 }
945
946 if (replace_with != nullptr) {
947 select->ReplaceWith(replace_with);
948 select->GetBlock()->RemoveInstruction(select);
949 RecordSimplification();
950 }
951}
952
953void InstructionSimplifierVisitor::VisitIf(HIf* instruction) {
954 HInstruction* condition = instruction->InputAt(0);
955 if (condition->IsBooleanNot()) {
956 // Swap successors if input is negated.
957 instruction->ReplaceInput(condition->InputAt(0), 0);
958 instruction->GetBlock()->SwapSuccessors();
David Brazdil0d13fee2015-04-17 14:52:19 +0100959 RecordSimplification();
960 }
961}
962
Mingyao Yang0304e182015-01-30 16:41:29 -0800963void InstructionSimplifierVisitor::VisitArrayLength(HArrayLength* instruction) {
964 HInstruction* input = instruction->InputAt(0);
965 // If the array is a NewArray with constant size, replace the array length
966 // with the constant instruction. This helps the bounds check elimination phase.
967 if (input->IsNewArray()) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +0000968 input = input->AsNewArray()->GetLength();
Mingyao Yang0304e182015-01-30 16:41:29 -0800969 if (input->IsIntConstant()) {
970 instruction->ReplaceWith(input);
971 }
972 }
973}
974
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000975void InstructionSimplifierVisitor::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000976 HInstruction* value = instruction->GetValue();
977 if (value->GetType() != Primitive::kPrimNot) return;
978
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100979 if (CanEnsureNotNullAt(value, instruction)) {
980 instruction->ClearValueCanBeNull();
981 }
982
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000983 if (value->IsArrayGet()) {
984 if (value->AsArrayGet()->GetArray() == instruction->GetArray()) {
985 // If the code is just swapping elements in the array, no need for a type check.
986 instruction->ClearNeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100987 return;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000988 }
989 }
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100990
Nicolas Geoffray9fdb31e2015-07-01 12:56:46 +0100991 if (value->IsNullConstant()) {
992 instruction->ClearNeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100993 return;
Nicolas Geoffray9fdb31e2015-07-01 12:56:46 +0100994 }
995
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100996 ScopedObjectAccess soa(Thread::Current());
997 ReferenceTypeInfo array_rti = instruction->GetArray()->GetReferenceTypeInfo();
998 ReferenceTypeInfo value_rti = value->GetReferenceTypeInfo();
999 if (!array_rti.IsValid()) {
1000 return;
1001 }
1002
1003 if (value_rti.IsValid() && array_rti.CanArrayHold(value_rti)) {
1004 instruction->ClearNeedsTypeCheck();
1005 return;
1006 }
1007
1008 if (array_rti.IsObjectArray()) {
1009 if (array_rti.IsExact()) {
1010 instruction->ClearNeedsTypeCheck();
1011 return;
1012 }
1013 instruction->SetStaticTypeOfArrayIsObjectArray();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001014 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00001015}
1016
Vladimir Markob52bbde2016-02-12 12:06:05 +00001017static bool IsTypeConversionImplicit(Primitive::Type input_type, Primitive::Type result_type) {
Roland Levillainf355c3f2016-03-30 19:09:03 +01001018 // Invariant: We should never generate a conversion to a Boolean value.
1019 DCHECK_NE(Primitive::kPrimBoolean, result_type);
1020
Vladimir Markob52bbde2016-02-12 12:06:05 +00001021 // Besides conversion to the same type, widening integral conversions are implicit,
1022 // excluding conversions to long and the byte->char conversion where we need to
1023 // clear the high 16 bits of the 32-bit sign-extended representation of byte.
1024 return result_type == input_type ||
Roland Levillainf355c3f2016-03-30 19:09:03 +01001025 (result_type == Primitive::kPrimInt && (input_type == Primitive::kPrimBoolean ||
1026 input_type == Primitive::kPrimByte ||
1027 input_type == Primitive::kPrimShort ||
1028 input_type == Primitive::kPrimChar)) ||
1029 (result_type == Primitive::kPrimChar && input_type == Primitive::kPrimBoolean) ||
1030 (result_type == Primitive::kPrimShort && (input_type == Primitive::kPrimBoolean ||
1031 input_type == Primitive::kPrimByte)) ||
1032 (result_type == Primitive::kPrimByte && input_type == Primitive::kPrimBoolean);
Vladimir Markob52bbde2016-02-12 12:06:05 +00001033}
1034
1035static bool IsTypeConversionLossless(Primitive::Type input_type, Primitive::Type result_type) {
1036 // The conversion to a larger type is loss-less with the exception of two cases,
1037 // - conversion to char, the only unsigned type, where we may lose some bits, and
1038 // - conversion from float to long, the only FP to integral conversion with smaller FP type.
1039 // For integral to FP conversions this holds because the FP mantissa is large enough.
1040 DCHECK_NE(input_type, result_type);
1041 return Primitive::ComponentSize(result_type) > Primitive::ComponentSize(input_type) &&
1042 result_type != Primitive::kPrimChar &&
1043 !(result_type == Primitive::kPrimLong && input_type == Primitive::kPrimFloat);
1044}
1045
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001046void InstructionSimplifierVisitor::VisitTypeConversion(HTypeConversion* instruction) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00001047 HInstruction* input = instruction->GetInput();
1048 Primitive::Type input_type = input->GetType();
1049 Primitive::Type result_type = instruction->GetResultType();
1050 if (IsTypeConversionImplicit(input_type, result_type)) {
1051 // Remove the implicit conversion; this includes conversion to the same type.
1052 instruction->ReplaceWith(input);
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001053 instruction->GetBlock()->RemoveInstruction(instruction);
Vladimir Markob52bbde2016-02-12 12:06:05 +00001054 RecordSimplification();
1055 return;
1056 }
1057
1058 if (input->IsTypeConversion()) {
1059 HTypeConversion* input_conversion = input->AsTypeConversion();
1060 HInstruction* original_input = input_conversion->GetInput();
1061 Primitive::Type original_type = original_input->GetType();
1062
1063 // When the first conversion is lossless, a direct conversion from the original type
1064 // to the final type yields the same result, even for a lossy second conversion, for
1065 // example float->double->int or int->double->float.
1066 bool is_first_conversion_lossless = IsTypeConversionLossless(original_type, input_type);
1067
1068 // For integral conversions, see if the first conversion loses only bits that the second
1069 // doesn't need, i.e. the final type is no wider than the intermediate. If so, direct
1070 // conversion yields the same result, for example long->int->short or int->char->short.
1071 bool integral_conversions_with_non_widening_second =
1072 Primitive::IsIntegralType(input_type) &&
1073 Primitive::IsIntegralType(original_type) &&
1074 Primitive::IsIntegralType(result_type) &&
1075 Primitive::ComponentSize(result_type) <= Primitive::ComponentSize(input_type);
1076
1077 if (is_first_conversion_lossless || integral_conversions_with_non_widening_second) {
1078 // If the merged conversion is implicit, do the simplification unconditionally.
1079 if (IsTypeConversionImplicit(original_type, result_type)) {
1080 instruction->ReplaceWith(original_input);
1081 instruction->GetBlock()->RemoveInstruction(instruction);
1082 if (!input_conversion->HasUses()) {
1083 // Don't wait for DCE.
1084 input_conversion->GetBlock()->RemoveInstruction(input_conversion);
1085 }
1086 RecordSimplification();
1087 return;
1088 }
1089 // Otherwise simplify only if the first conversion has no other use.
1090 if (input_conversion->HasOnlyOneNonEnvironmentUse()) {
1091 input_conversion->ReplaceWith(original_input);
1092 input_conversion->GetBlock()->RemoveInstruction(input_conversion);
1093 RecordSimplification();
1094 return;
1095 }
1096 }
Vladimir Marko625090f2016-03-14 18:00:05 +00001097 } else if (input->IsAnd() && Primitive::IsIntegralType(result_type)) {
Vladimir Marko8428bd32016-02-12 16:53:57 +00001098 DCHECK(Primitive::IsIntegralType(input_type));
1099 HAnd* input_and = input->AsAnd();
1100 HConstant* constant = input_and->GetConstantRight();
1101 if (constant != nullptr) {
1102 int64_t value = Int64FromConstant(constant);
1103 DCHECK_NE(value, -1); // "& -1" would have been optimized away in VisitAnd().
1104 size_t trailing_ones = CTZ(~static_cast<uint64_t>(value));
1105 if (trailing_ones >= kBitsPerByte * Primitive::ComponentSize(result_type)) {
1106 // The `HAnd` is useless, for example in `(byte) (x & 0xff)`, get rid of it.
Vladimir Marko625090f2016-03-14 18:00:05 +00001107 HInstruction* original_input = input_and->GetLeastConstantLeft();
1108 if (IsTypeConversionImplicit(original_input->GetType(), result_type)) {
1109 instruction->ReplaceWith(original_input);
1110 instruction->GetBlock()->RemoveInstruction(instruction);
1111 RecordSimplification();
1112 return;
1113 } else if (input->HasOnlyOneNonEnvironmentUse()) {
1114 input_and->ReplaceWith(original_input);
1115 input_and->GetBlock()->RemoveInstruction(input_and);
1116 RecordSimplification();
1117 return;
1118 }
Vladimir Marko8428bd32016-02-12 16:53:57 +00001119 }
1120 }
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001121 }
1122}
1123
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001124void InstructionSimplifierVisitor::VisitAdd(HAdd* instruction) {
1125 HConstant* input_cst = instruction->GetConstantRight();
1126 HInstruction* input_other = instruction->GetLeastConstantLeft();
Maxim Kazantsevd3278bd2016-07-12 15:55:33 +06001127 bool integral_type = Primitive::IsIntegralType(instruction->GetType());
Roland Levillain1a653882016-03-18 18:05:57 +00001128 if ((input_cst != nullptr) && input_cst->IsArithmeticZero()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001129 // Replace code looking like
1130 // ADD dst, src, 0
1131 // with
1132 // src
Serguei Katkov115b53f2015-08-05 17:03:30 +06001133 // Note that we cannot optimize `x + 0.0` to `x` for floating-point. When
1134 // `x` is `-0.0`, the former expression yields `0.0`, while the later
1135 // yields `-0.0`.
Maxim Kazantsevd3278bd2016-07-12 15:55:33 +06001136 if (integral_type) {
Serguei Katkov115b53f2015-08-05 17:03:30 +06001137 instruction->ReplaceWith(input_other);
1138 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001139 RecordSimplification();
Serguei Katkov115b53f2015-08-05 17:03:30 +06001140 return;
1141 }
Alexandre Rames188d4312015-04-09 18:30:21 +01001142 }
1143
1144 HInstruction* left = instruction->GetLeft();
1145 HInstruction* right = instruction->GetRight();
1146 bool left_is_neg = left->IsNeg();
1147 bool right_is_neg = right->IsNeg();
1148
1149 if (left_is_neg && right_is_neg) {
1150 if (TryMoveNegOnInputsAfterBinop(instruction)) {
1151 return;
1152 }
1153 }
1154
1155 HNeg* neg = left_is_neg ? left->AsNeg() : right->AsNeg();
1156 if ((left_is_neg ^ right_is_neg) && neg->HasOnlyOneNonEnvironmentUse()) {
1157 // Replace code looking like
1158 // NEG tmp, b
1159 // ADD dst, a, tmp
1160 // with
1161 // SUB dst, a, b
1162 // We do not perform the optimization if the input negation has environment
1163 // uses or multiple non-environment uses as it could lead to worse code. In
1164 // particular, we do not want the live range of `b` to be extended if we are
1165 // not sure the initial 'NEG' instruction can be removed.
1166 HInstruction* other = left_is_neg ? right : left;
1167 HSub* sub = new(GetGraph()->GetArena()) HSub(instruction->GetType(), other, neg->GetInput());
1168 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, sub);
1169 RecordSimplification();
1170 neg->GetBlock()->RemoveInstruction(neg);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001171 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001172 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001173
Anton Kirilove14dc862016-05-13 17:56:15 +01001174 if (TryReplaceWithRotate(instruction)) {
1175 return;
1176 }
1177
1178 // TryHandleAssociativeAndCommutativeOperation() does not remove its input,
1179 // so no need to return.
1180 TryHandleAssociativeAndCommutativeOperation(instruction);
1181
Maxim Kazantsevd3278bd2016-07-12 15:55:33 +06001182 if ((left->IsSub() || right->IsSub()) &&
Anton Kirilove14dc862016-05-13 17:56:15 +01001183 TrySubtractionChainSimplification(instruction)) {
1184 return;
1185 }
Maxim Kazantsevd3278bd2016-07-12 15:55:33 +06001186
1187 if (integral_type) {
1188 // Replace code patterns looking like
1189 // SUB dst1, x, y SUB dst1, x, y
1190 // ADD dst2, dst1, y ADD dst2, y, dst1
1191 // with
1192 // SUB dst1, x, y
1193 // ADD instruction is not needed in this case, we may use
1194 // one of inputs of SUB instead.
1195 if (left->IsSub() && left->InputAt(1) == right) {
1196 instruction->ReplaceWith(left->InputAt(0));
1197 RecordSimplification();
1198 instruction->GetBlock()->RemoveInstruction(instruction);
1199 return;
1200 } else if (right->IsSub() && right->InputAt(1) == left) {
1201 instruction->ReplaceWith(right->InputAt(0));
1202 RecordSimplification();
1203 instruction->GetBlock()->RemoveInstruction(instruction);
1204 return;
1205 }
1206 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001207}
1208
1209void InstructionSimplifierVisitor::VisitAnd(HAnd* instruction) {
1210 HConstant* input_cst = instruction->GetConstantRight();
1211 HInstruction* input_other = instruction->GetLeastConstantLeft();
1212
Vladimir Marko452c1b62015-09-25 14:44:17 +01001213 if (input_cst != nullptr) {
1214 int64_t value = Int64FromConstant(input_cst);
1215 if (value == -1) {
1216 // Replace code looking like
1217 // AND dst, src, 0xFFF...FF
1218 // with
1219 // src
1220 instruction->ReplaceWith(input_other);
1221 instruction->GetBlock()->RemoveInstruction(instruction);
1222 RecordSimplification();
1223 return;
1224 }
1225 // Eliminate And from UShr+And if the And-mask contains all the bits that
1226 // can be non-zero after UShr. Transform Shr+And to UShr if the And-mask
1227 // precisely clears the shifted-in sign bits.
1228 if ((input_other->IsUShr() || input_other->IsShr()) && input_other->InputAt(1)->IsConstant()) {
1229 size_t reg_bits = (instruction->GetResultType() == Primitive::kPrimLong) ? 64 : 32;
1230 size_t shift = Int64FromConstant(input_other->InputAt(1)->AsConstant()) & (reg_bits - 1);
1231 size_t num_tail_bits_set = CTZ(value + 1);
1232 if ((num_tail_bits_set >= reg_bits - shift) && input_other->IsUShr()) {
1233 // This AND clears only bits known to be clear, for example "(x >>> 24) & 0xff".
1234 instruction->ReplaceWith(input_other);
1235 instruction->GetBlock()->RemoveInstruction(instruction);
1236 RecordSimplification();
1237 return;
1238 } else if ((num_tail_bits_set == reg_bits - shift) && IsPowerOfTwo(value + 1) &&
1239 input_other->HasOnlyOneNonEnvironmentUse()) {
1240 DCHECK(input_other->IsShr()); // For UShr, we would have taken the branch above.
1241 // Replace SHR+AND with USHR, for example "(x >> 24) & 0xff" -> "x >>> 24".
1242 HUShr* ushr = new (GetGraph()->GetArena()) HUShr(instruction->GetType(),
1243 input_other->InputAt(0),
1244 input_other->InputAt(1),
1245 input_other->GetDexPc());
1246 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, ushr);
1247 input_other->GetBlock()->RemoveInstruction(input_other);
1248 RecordSimplification();
1249 return;
1250 }
1251 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001252 }
1253
1254 // We assume that GVN has run before, so we only perform a pointer comparison.
1255 // If for some reason the values are equal but the pointers are different, we
Alexandre Rames188d4312015-04-09 18:30:21 +01001256 // are still correct and only miss an optimization opportunity.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001257 if (instruction->GetLeft() == instruction->GetRight()) {
1258 // Replace code looking like
1259 // AND dst, src, src
1260 // with
1261 // src
1262 instruction->ReplaceWith(instruction->GetLeft());
1263 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001264 RecordSimplification();
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001265 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001266 }
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001267
Anton Kirilove14dc862016-05-13 17:56:15 +01001268 if (TryDeMorganNegationFactoring(instruction)) {
1269 return;
1270 }
1271
1272 // TryHandleAssociativeAndCommutativeOperation() does not remove its input,
1273 // so no need to return.
1274 TryHandleAssociativeAndCommutativeOperation(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001275}
1276
Mark Mendellc4701932015-04-10 13:18:51 -04001277void InstructionSimplifierVisitor::VisitGreaterThan(HGreaterThan* condition) {
1278 VisitCondition(condition);
1279}
1280
1281void InstructionSimplifierVisitor::VisitGreaterThanOrEqual(HGreaterThanOrEqual* condition) {
1282 VisitCondition(condition);
1283}
1284
1285void InstructionSimplifierVisitor::VisitLessThan(HLessThan* condition) {
1286 VisitCondition(condition);
1287}
1288
1289void InstructionSimplifierVisitor::VisitLessThanOrEqual(HLessThanOrEqual* condition) {
1290 VisitCondition(condition);
1291}
1292
Anton Shaminbdd79352016-02-15 12:48:36 +06001293void InstructionSimplifierVisitor::VisitBelow(HBelow* condition) {
1294 VisitCondition(condition);
1295}
1296
1297void InstructionSimplifierVisitor::VisitBelowOrEqual(HBelowOrEqual* condition) {
1298 VisitCondition(condition);
1299}
1300
1301void InstructionSimplifierVisitor::VisitAbove(HAbove* condition) {
1302 VisitCondition(condition);
1303}
1304
1305void InstructionSimplifierVisitor::VisitAboveOrEqual(HAboveOrEqual* condition) {
1306 VisitCondition(condition);
1307}
Aart Bike9f37602015-10-09 11:15:55 -07001308
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001309// Recognize the following pattern:
1310// obj.getClass() ==/!= Foo.class
1311// And replace it with a constant value if the type of `obj` is statically known.
1312static bool RecognizeAndSimplifyClassCheck(HCondition* condition) {
1313 HInstruction* input_one = condition->InputAt(0);
1314 HInstruction* input_two = condition->InputAt(1);
1315 HLoadClass* load_class = input_one->IsLoadClass()
1316 ? input_one->AsLoadClass()
1317 : input_two->AsLoadClass();
1318 if (load_class == nullptr) {
1319 return false;
1320 }
1321
1322 ReferenceTypeInfo class_rti = load_class->GetLoadedClassRTI();
1323 if (!class_rti.IsValid()) {
1324 // Unresolved class.
1325 return false;
1326 }
1327
1328 HInstanceFieldGet* field_get = (load_class == input_one)
1329 ? input_two->AsInstanceFieldGet()
1330 : input_one->AsInstanceFieldGet();
1331 if (field_get == nullptr) {
1332 return false;
1333 }
1334
1335 HInstruction* receiver = field_get->InputAt(0);
1336 ReferenceTypeInfo receiver_type = receiver->GetReferenceTypeInfo();
1337 if (!receiver_type.IsExact()) {
1338 return false;
1339 }
1340
1341 {
1342 ScopedObjectAccess soa(Thread::Current());
1343 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1344 ArtField* field = class_linker->GetClassRoot(ClassLinker::kJavaLangObject)->GetInstanceField(0);
1345 DCHECK_EQ(std::string(field->GetName()), "shadow$_klass_");
1346 if (field_get->GetFieldInfo().GetField() != field) {
1347 return false;
1348 }
1349
1350 // We can replace the compare.
1351 int value = 0;
1352 if (receiver_type.IsEqual(class_rti)) {
1353 value = condition->IsEqual() ? 1 : 0;
1354 } else {
1355 value = condition->IsNotEqual() ? 1 : 0;
1356 }
1357 condition->ReplaceWith(condition->GetBlock()->GetGraph()->GetIntConstant(value));
1358 return true;
1359 }
1360}
1361
Mark Mendellc4701932015-04-10 13:18:51 -04001362void InstructionSimplifierVisitor::VisitCondition(HCondition* condition) {
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001363 if (condition->IsEqual() || condition->IsNotEqual()) {
1364 if (RecognizeAndSimplifyClassCheck(condition)) {
1365 return;
1366 }
1367 }
1368
Anton Shaminbdd79352016-02-15 12:48:36 +06001369 // Reverse condition if left is constant. Our code generators prefer constant
1370 // on the right hand side.
1371 if (condition->GetLeft()->IsConstant() && !condition->GetRight()->IsConstant()) {
1372 HBasicBlock* block = condition->GetBlock();
1373 HCondition* replacement = GetOppositeConditionSwapOps(block->GetGraph()->GetArena(), condition);
1374 // If it is a fp we must set the opposite bias.
1375 if (replacement != nullptr) {
1376 if (condition->IsLtBias()) {
1377 replacement->SetBias(ComparisonBias::kGtBias);
1378 } else if (condition->IsGtBias()) {
1379 replacement->SetBias(ComparisonBias::kLtBias);
1380 }
1381 block->ReplaceAndRemoveInstructionWith(condition, replacement);
1382 RecordSimplification();
1383
1384 condition = replacement;
1385 }
1386 }
Mark Mendellc4701932015-04-10 13:18:51 -04001387
Mark Mendellc4701932015-04-10 13:18:51 -04001388 HInstruction* left = condition->GetLeft();
1389 HInstruction* right = condition->GetRight();
Anton Shaminbdd79352016-02-15 12:48:36 +06001390
1391 // Try to fold an HCompare into this HCondition.
1392
Mark Mendellc4701932015-04-10 13:18:51 -04001393 // We can only replace an HCondition which compares a Compare to 0.
1394 // Both 'dx' and 'jack' generate a compare to 0 when compiling a
1395 // condition with a long, float or double comparison as input.
1396 if (!left->IsCompare() || !right->IsConstant() || right->AsIntConstant()->GetValue() != 0) {
1397 // Conversion is not possible.
1398 return;
1399 }
1400
1401 // Is the Compare only used for this purpose?
Vladimir Marko46817b82016-03-29 12:21:58 +01001402 if (!left->GetUses().HasExactlyOneElement()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001403 // Someone else also wants the result of the compare.
1404 return;
1405 }
1406
Vladimir Marko46817b82016-03-29 12:21:58 +01001407 if (!left->GetEnvUses().empty()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001408 // There is a reference to the compare result in an environment. Do we really need it?
1409 if (GetGraph()->IsDebuggable()) {
1410 return;
1411 }
1412
1413 // We have to ensure that there are no deopt points in the sequence.
1414 if (left->HasAnyEnvironmentUseBefore(condition)) {
1415 return;
1416 }
1417 }
1418
1419 // Clean up any environment uses from the HCompare, if any.
1420 left->RemoveEnvironmentUsers();
1421
1422 // We have decided to fold the HCompare into the HCondition. Transfer the information.
1423 condition->SetBias(left->AsCompare()->GetBias());
1424
1425 // Replace the operands of the HCondition.
1426 condition->ReplaceInput(left->InputAt(0), 0);
1427 condition->ReplaceInput(left->InputAt(1), 1);
1428
1429 // Remove the HCompare.
1430 left->GetBlock()->RemoveInstruction(left);
1431
1432 RecordSimplification();
1433}
1434
Andreas Gampe9186ced2016-12-12 14:28:21 -08001435// Return whether x / divisor == x * (1.0f / divisor), for every float x.
1436static constexpr bool CanDivideByReciprocalMultiplyFloat(int32_t divisor) {
1437 // True, if the most significant bits of divisor are 0.
1438 return ((divisor & 0x7fffff) == 0);
1439}
1440
1441// Return whether x / divisor == x * (1.0 / divisor), for every double x.
1442static constexpr bool CanDivideByReciprocalMultiplyDouble(int64_t divisor) {
1443 // True, if the most significant bits of divisor are 0.
1444 return ((divisor & ((UINT64_C(1) << 52) - 1)) == 0);
1445}
1446
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001447void InstructionSimplifierVisitor::VisitDiv(HDiv* instruction) {
1448 HConstant* input_cst = instruction->GetConstantRight();
1449 HInstruction* input_other = instruction->GetLeastConstantLeft();
1450 Primitive::Type type = instruction->GetType();
1451
1452 if ((input_cst != nullptr) && input_cst->IsOne()) {
1453 // Replace code looking like
1454 // DIV dst, src, 1
1455 // with
1456 // src
1457 instruction->ReplaceWith(input_other);
1458 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001459 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001460 return;
1461 }
1462
Nicolas Geoffray0d221842015-04-27 08:53:46 +00001463 if ((input_cst != nullptr) && input_cst->IsMinusOne()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001464 // Replace code looking like
1465 // DIV dst, src, -1
1466 // with
1467 // NEG dst, src
1468 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(
Nicolas Geoffray0d221842015-04-27 08:53:46 +00001469 instruction, new (GetGraph()->GetArena()) HNeg(type, input_other));
Alexandre Rames188d4312015-04-09 18:30:21 +01001470 RecordSimplification();
Nicolas Geoffray0d221842015-04-27 08:53:46 +00001471 return;
1472 }
1473
1474 if ((input_cst != nullptr) && Primitive::IsFloatingPointType(type)) {
1475 // Try replacing code looking like
1476 // DIV dst, src, constant
1477 // with
1478 // MUL dst, src, 1 / constant
1479 HConstant* reciprocal = nullptr;
1480 if (type == Primitive::Primitive::kPrimDouble) {
1481 double value = input_cst->AsDoubleConstant()->GetValue();
1482 if (CanDivideByReciprocalMultiplyDouble(bit_cast<int64_t, double>(value))) {
1483 reciprocal = GetGraph()->GetDoubleConstant(1.0 / value);
1484 }
1485 } else {
1486 DCHECK_EQ(type, Primitive::kPrimFloat);
1487 float value = input_cst->AsFloatConstant()->GetValue();
1488 if (CanDivideByReciprocalMultiplyFloat(bit_cast<int32_t, float>(value))) {
1489 reciprocal = GetGraph()->GetFloatConstant(1.0f / value);
1490 }
1491 }
1492
1493 if (reciprocal != nullptr) {
1494 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(
1495 instruction, new (GetGraph()->GetArena()) HMul(type, input_other, reciprocal));
1496 RecordSimplification();
1497 return;
1498 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001499 }
1500}
1501
1502void InstructionSimplifierVisitor::VisitMul(HMul* instruction) {
1503 HConstant* input_cst = instruction->GetConstantRight();
1504 HInstruction* input_other = instruction->GetLeastConstantLeft();
1505 Primitive::Type type = instruction->GetType();
1506 HBasicBlock* block = instruction->GetBlock();
1507 ArenaAllocator* allocator = GetGraph()->GetArena();
1508
1509 if (input_cst == nullptr) {
1510 return;
1511 }
1512
1513 if (input_cst->IsOne()) {
1514 // Replace code looking like
1515 // MUL dst, src, 1
1516 // with
1517 // src
1518 instruction->ReplaceWith(input_other);
1519 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001520 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001521 return;
1522 }
1523
1524 if (input_cst->IsMinusOne() &&
1525 (Primitive::IsFloatingPointType(type) || Primitive::IsIntOrLongType(type))) {
1526 // Replace code looking like
1527 // MUL dst, src, -1
1528 // with
1529 // NEG dst, src
1530 HNeg* neg = new (allocator) HNeg(type, input_other);
1531 block->ReplaceAndRemoveInstructionWith(instruction, neg);
Alexandre Rames188d4312015-04-09 18:30:21 +01001532 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001533 return;
1534 }
1535
1536 if (Primitive::IsFloatingPointType(type) &&
1537 ((input_cst->IsFloatConstant() && input_cst->AsFloatConstant()->GetValue() == 2.0f) ||
1538 (input_cst->IsDoubleConstant() && input_cst->AsDoubleConstant()->GetValue() == 2.0))) {
1539 // Replace code looking like
1540 // FP_MUL dst, src, 2.0
1541 // with
1542 // FP_ADD dst, src, src
1543 // The 'int' and 'long' cases are handled below.
1544 block->ReplaceAndRemoveInstructionWith(instruction,
1545 new (allocator) HAdd(type, input_other, input_other));
Alexandre Rames188d4312015-04-09 18:30:21 +01001546 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001547 return;
1548 }
1549
1550 if (Primitive::IsIntOrLongType(type)) {
1551 int64_t factor = Int64FromConstant(input_cst);
Serguei Katkov53849192015-04-20 14:22:27 +06001552 // Even though constant propagation also takes care of the zero case, other
1553 // optimizations can lead to having a zero multiplication.
1554 if (factor == 0) {
1555 // Replace code looking like
1556 // MUL dst, src, 0
1557 // with
1558 // 0
1559 instruction->ReplaceWith(input_cst);
1560 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001561 RecordSimplification();
Anton Kirilove14dc862016-05-13 17:56:15 +01001562 return;
Serguei Katkov53849192015-04-20 14:22:27 +06001563 } else if (IsPowerOfTwo(factor)) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001564 // Replace code looking like
1565 // MUL dst, src, pow_of_2
1566 // with
1567 // SHL dst, src, log2(pow_of_2)
David Brazdil8d5b8b22015-03-24 10:51:52 +00001568 HIntConstant* shift = GetGraph()->GetIntConstant(WhichPowerOf2(factor));
Roland Levillain22c49222016-03-18 14:04:28 +00001569 HShl* shl = new (allocator) HShl(type, input_other, shift);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001570 block->ReplaceAndRemoveInstructionWith(instruction, shl);
Alexandre Rames188d4312015-04-09 18:30:21 +01001571 RecordSimplification();
Anton Kirilove14dc862016-05-13 17:56:15 +01001572 return;
Alexandre Rames38db7852015-11-20 15:02:45 +00001573 } else if (IsPowerOfTwo(factor - 1)) {
1574 // Transform code looking like
1575 // MUL dst, src, (2^n + 1)
1576 // into
1577 // SHL tmp, src, n
1578 // ADD dst, src, tmp
1579 HShl* shl = new (allocator) HShl(type,
1580 input_other,
1581 GetGraph()->GetIntConstant(WhichPowerOf2(factor - 1)));
1582 HAdd* add = new (allocator) HAdd(type, input_other, shl);
1583
1584 block->InsertInstructionBefore(shl, instruction);
1585 block->ReplaceAndRemoveInstructionWith(instruction, add);
1586 RecordSimplification();
Anton Kirilove14dc862016-05-13 17:56:15 +01001587 return;
Alexandre Rames38db7852015-11-20 15:02:45 +00001588 } else if (IsPowerOfTwo(factor + 1)) {
1589 // Transform code looking like
1590 // MUL dst, src, (2^n - 1)
1591 // into
1592 // SHL tmp, src, n
1593 // SUB dst, tmp, src
1594 HShl* shl = new (allocator) HShl(type,
1595 input_other,
1596 GetGraph()->GetIntConstant(WhichPowerOf2(factor + 1)));
1597 HSub* sub = new (allocator) HSub(type, shl, input_other);
1598
1599 block->InsertInstructionBefore(shl, instruction);
1600 block->ReplaceAndRemoveInstructionWith(instruction, sub);
1601 RecordSimplification();
Anton Kirilove14dc862016-05-13 17:56:15 +01001602 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001603 }
1604 }
Anton Kirilove14dc862016-05-13 17:56:15 +01001605
1606 // TryHandleAssociativeAndCommutativeOperation() does not remove its input,
1607 // so no need to return.
1608 TryHandleAssociativeAndCommutativeOperation(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001609}
1610
Alexandre Rames188d4312015-04-09 18:30:21 +01001611void InstructionSimplifierVisitor::VisitNeg(HNeg* instruction) {
1612 HInstruction* input = instruction->GetInput();
1613 if (input->IsNeg()) {
1614 // Replace code looking like
1615 // NEG tmp, src
1616 // NEG dst, tmp
1617 // with
1618 // src
1619 HNeg* previous_neg = input->AsNeg();
1620 instruction->ReplaceWith(previous_neg->GetInput());
1621 instruction->GetBlock()->RemoveInstruction(instruction);
1622 // We perform the optimization even if the input negation has environment
1623 // uses since it allows removing the current instruction. But we only delete
1624 // the input negation only if it is does not have any uses left.
1625 if (!previous_neg->HasUses()) {
1626 previous_neg->GetBlock()->RemoveInstruction(previous_neg);
1627 }
1628 RecordSimplification();
1629 return;
1630 }
1631
Serguei Katkov339dfc22015-04-20 12:29:32 +06001632 if (input->IsSub() && input->HasOnlyOneNonEnvironmentUse() &&
1633 !Primitive::IsFloatingPointType(input->GetType())) {
Alexandre Rames188d4312015-04-09 18:30:21 +01001634 // Replace code looking like
1635 // SUB tmp, a, b
1636 // NEG dst, tmp
1637 // with
1638 // SUB dst, b, a
1639 // We do not perform the optimization if the input subtraction has
1640 // environment uses or multiple non-environment uses as it could lead to
1641 // worse code. In particular, we do not want the live ranges of `a` and `b`
1642 // to be extended if we are not sure the initial 'SUB' instruction can be
1643 // removed.
Serguei Katkov339dfc22015-04-20 12:29:32 +06001644 // We do not perform optimization for fp because we could lose the sign of zero.
Alexandre Rames188d4312015-04-09 18:30:21 +01001645 HSub* sub = input->AsSub();
1646 HSub* new_sub =
1647 new (GetGraph()->GetArena()) HSub(instruction->GetType(), sub->GetRight(), sub->GetLeft());
1648 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, new_sub);
1649 if (!sub->HasUses()) {
1650 sub->GetBlock()->RemoveInstruction(sub);
1651 }
1652 RecordSimplification();
1653 }
1654}
1655
1656void InstructionSimplifierVisitor::VisitNot(HNot* instruction) {
1657 HInstruction* input = instruction->GetInput();
1658 if (input->IsNot()) {
1659 // Replace code looking like
1660 // NOT tmp, src
1661 // NOT dst, tmp
1662 // with
1663 // src
1664 // We perform the optimization even if the input negation has environment
1665 // uses since it allows removing the current instruction. But we only delete
1666 // the input negation only if it is does not have any uses left.
1667 HNot* previous_not = input->AsNot();
1668 instruction->ReplaceWith(previous_not->GetInput());
1669 instruction->GetBlock()->RemoveInstruction(instruction);
1670 if (!previous_not->HasUses()) {
1671 previous_not->GetBlock()->RemoveInstruction(previous_not);
1672 }
1673 RecordSimplification();
1674 }
1675}
1676
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001677void InstructionSimplifierVisitor::VisitOr(HOr* instruction) {
1678 HConstant* input_cst = instruction->GetConstantRight();
1679 HInstruction* input_other = instruction->GetLeastConstantLeft();
1680
Roland Levillain1a653882016-03-18 18:05:57 +00001681 if ((input_cst != nullptr) && input_cst->IsZeroBitPattern()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001682 // Replace code looking like
1683 // OR dst, src, 0
1684 // with
1685 // src
1686 instruction->ReplaceWith(input_other);
1687 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001688 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001689 return;
1690 }
1691
1692 // We assume that GVN has run before, so we only perform a pointer comparison.
1693 // If for some reason the values are equal but the pointers are different, we
Alexandre Rames188d4312015-04-09 18:30:21 +01001694 // are still correct and only miss an optimization opportunity.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001695 if (instruction->GetLeft() == instruction->GetRight()) {
1696 // Replace code looking like
1697 // OR dst, src, src
1698 // with
1699 // src
1700 instruction->ReplaceWith(instruction->GetLeft());
1701 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001702 RecordSimplification();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001703 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001704 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001705
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001706 if (TryDeMorganNegationFactoring(instruction)) return;
1707
Anton Kirilove14dc862016-05-13 17:56:15 +01001708 if (TryReplaceWithRotate(instruction)) {
1709 return;
1710 }
1711
1712 // TryHandleAssociativeAndCommutativeOperation() does not remove its input,
1713 // so no need to return.
1714 TryHandleAssociativeAndCommutativeOperation(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001715}
1716
1717void InstructionSimplifierVisitor::VisitShl(HShl* instruction) {
1718 VisitShift(instruction);
1719}
1720
1721void InstructionSimplifierVisitor::VisitShr(HShr* instruction) {
1722 VisitShift(instruction);
1723}
1724
1725void InstructionSimplifierVisitor::VisitSub(HSub* instruction) {
1726 HConstant* input_cst = instruction->GetConstantRight();
1727 HInstruction* input_other = instruction->GetLeastConstantLeft();
1728
Serguei Katkov115b53f2015-08-05 17:03:30 +06001729 Primitive::Type type = instruction->GetType();
1730 if (Primitive::IsFloatingPointType(type)) {
1731 return;
1732 }
1733
Roland Levillain1a653882016-03-18 18:05:57 +00001734 if ((input_cst != nullptr) && input_cst->IsArithmeticZero()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001735 // Replace code looking like
1736 // SUB dst, src, 0
1737 // with
1738 // src
Serguei Katkov115b53f2015-08-05 17:03:30 +06001739 // Note that we cannot optimize `x - 0.0` to `x` for floating-point. When
1740 // `x` is `-0.0`, the former expression yields `0.0`, while the later
1741 // yields `-0.0`.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001742 instruction->ReplaceWith(input_other);
1743 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001744 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001745 return;
1746 }
1747
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001748 HBasicBlock* block = instruction->GetBlock();
1749 ArenaAllocator* allocator = GetGraph()->GetArena();
1750
Alexandre Rames188d4312015-04-09 18:30:21 +01001751 HInstruction* left = instruction->GetLeft();
1752 HInstruction* right = instruction->GetRight();
1753 if (left->IsConstant()) {
1754 if (Int64FromConstant(left->AsConstant()) == 0) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001755 // Replace code looking like
1756 // SUB dst, 0, src
1757 // with
1758 // NEG dst, src
Alexandre Rames188d4312015-04-09 18:30:21 +01001759 // Note that we cannot optimize `0.0 - x` to `-x` for floating-point. When
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001760 // `x` is `0.0`, the former expression yields `0.0`, while the later
1761 // yields `-0.0`.
Alexandre Rames188d4312015-04-09 18:30:21 +01001762 HNeg* neg = new (allocator) HNeg(type, right);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001763 block->ReplaceAndRemoveInstructionWith(instruction, neg);
Alexandre Rames188d4312015-04-09 18:30:21 +01001764 RecordSimplification();
1765 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001766 }
1767 }
Alexandre Rames188d4312015-04-09 18:30:21 +01001768
1769 if (left->IsNeg() && right->IsNeg()) {
1770 if (TryMoveNegOnInputsAfterBinop(instruction)) {
1771 return;
1772 }
1773 }
1774
1775 if (right->IsNeg() && right->HasOnlyOneNonEnvironmentUse()) {
1776 // Replace code looking like
1777 // NEG tmp, b
1778 // SUB dst, a, tmp
1779 // with
1780 // ADD dst, a, b
1781 HAdd* add = new(GetGraph()->GetArena()) HAdd(type, left, right->AsNeg()->GetInput());
1782 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, add);
1783 RecordSimplification();
1784 right->GetBlock()->RemoveInstruction(right);
1785 return;
1786 }
1787
1788 if (left->IsNeg() && left->HasOnlyOneNonEnvironmentUse()) {
1789 // Replace code looking like
1790 // NEG tmp, a
1791 // SUB dst, tmp, b
1792 // with
1793 // ADD tmp, a, b
1794 // NEG dst, tmp
1795 // The second version is not intrinsically better, but enables more
1796 // transformations.
1797 HAdd* add = new(GetGraph()->GetArena()) HAdd(type, left->AsNeg()->GetInput(), right);
1798 instruction->GetBlock()->InsertInstructionBefore(add, instruction);
1799 HNeg* neg = new (GetGraph()->GetArena()) HNeg(instruction->GetType(), add);
1800 instruction->GetBlock()->InsertInstructionBefore(neg, instruction);
1801 instruction->ReplaceWith(neg);
1802 instruction->GetBlock()->RemoveInstruction(instruction);
1803 RecordSimplification();
1804 left->GetBlock()->RemoveInstruction(left);
Anton Kirilove14dc862016-05-13 17:56:15 +01001805 return;
1806 }
1807
1808 if (TrySubtractionChainSimplification(instruction)) {
1809 return;
Alexandre Rames188d4312015-04-09 18:30:21 +01001810 }
Maxim Kazantsevd3278bd2016-07-12 15:55:33 +06001811
1812 if (left->IsAdd()) {
1813 // Replace code patterns looking like
1814 // ADD dst1, x, y ADD dst1, x, y
1815 // SUB dst2, dst1, y SUB dst2, dst1, x
1816 // with
1817 // ADD dst1, x, y
1818 // SUB instruction is not needed in this case, we may use
1819 // one of inputs of ADD instead.
1820 // It is applicable to integral types only.
1821 DCHECK(Primitive::IsIntegralType(type));
1822 if (left->InputAt(1) == right) {
1823 instruction->ReplaceWith(left->InputAt(0));
1824 RecordSimplification();
1825 instruction->GetBlock()->RemoveInstruction(instruction);
1826 return;
1827 } else if (left->InputAt(0) == right) {
1828 instruction->ReplaceWith(left->InputAt(1));
1829 RecordSimplification();
1830 instruction->GetBlock()->RemoveInstruction(instruction);
1831 return;
1832 }
1833 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001834}
1835
1836void InstructionSimplifierVisitor::VisitUShr(HUShr* instruction) {
1837 VisitShift(instruction);
1838}
1839
1840void InstructionSimplifierVisitor::VisitXor(HXor* instruction) {
1841 HConstant* input_cst = instruction->GetConstantRight();
1842 HInstruction* input_other = instruction->GetLeastConstantLeft();
1843
Roland Levillain1a653882016-03-18 18:05:57 +00001844 if ((input_cst != nullptr) && input_cst->IsZeroBitPattern()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001845 // Replace code looking like
1846 // XOR dst, src, 0
1847 // with
1848 // src
1849 instruction->ReplaceWith(input_other);
1850 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001851 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001852 return;
1853 }
1854
Sebastien Hertz9837caf2016-08-01 11:09:50 +02001855 if ((input_cst != nullptr) && input_cst->IsOne()
1856 && input_other->GetType() == Primitive::kPrimBoolean) {
1857 // Replace code looking like
1858 // XOR dst, src, 1
1859 // with
1860 // BOOLEAN_NOT dst, src
1861 HBooleanNot* boolean_not = new (GetGraph()->GetArena()) HBooleanNot(input_other);
1862 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, boolean_not);
1863 RecordSimplification();
1864 return;
1865 }
1866
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001867 if ((input_cst != nullptr) && AreAllBitsSet(input_cst)) {
1868 // Replace code looking like
1869 // XOR dst, src, 0xFFF...FF
1870 // with
1871 // NOT dst, src
1872 HNot* bitwise_not = new (GetGraph()->GetArena()) HNot(instruction->GetType(), input_other);
1873 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, bitwise_not);
Alexandre Rames188d4312015-04-09 18:30:21 +01001874 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001875 return;
1876 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001877
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001878 HInstruction* left = instruction->GetLeft();
1879 HInstruction* right = instruction->GetRight();
Alexandre Rames9f980252016-02-05 14:00:28 +00001880 if (((left->IsNot() && right->IsNot()) ||
1881 (left->IsBooleanNot() && right->IsBooleanNot())) &&
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001882 left->HasOnlyOneNonEnvironmentUse() &&
1883 right->HasOnlyOneNonEnvironmentUse()) {
1884 // Replace code looking like
1885 // NOT nota, a
1886 // NOT notb, b
1887 // XOR dst, nota, notb
1888 // with
1889 // XOR dst, a, b
Alexandre Rames9f980252016-02-05 14:00:28 +00001890 instruction->ReplaceInput(left->InputAt(0), 0);
1891 instruction->ReplaceInput(right->InputAt(0), 1);
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001892 left->GetBlock()->RemoveInstruction(left);
1893 right->GetBlock()->RemoveInstruction(right);
1894 RecordSimplification();
1895 return;
1896 }
1897
Anton Kirilove14dc862016-05-13 17:56:15 +01001898 if (TryReplaceWithRotate(instruction)) {
1899 return;
1900 }
1901
1902 // TryHandleAssociativeAndCommutativeOperation() does not remove its input,
1903 // so no need to return.
1904 TryHandleAssociativeAndCommutativeOperation(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001905}
1906
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001907void InstructionSimplifierVisitor::SimplifyStringEquals(HInvoke* instruction) {
1908 HInstruction* argument = instruction->InputAt(1);
1909 HInstruction* receiver = instruction->InputAt(0);
1910 if (receiver == argument) {
1911 // Because String.equals is an instance call, the receiver is
1912 // a null check if we don't know it's null. The argument however, will
1913 // be the actual object. So we cannot end up in a situation where both
1914 // are equal but could be null.
1915 DCHECK(CanEnsureNotNullAt(argument, instruction));
1916 instruction->ReplaceWith(GetGraph()->GetIntConstant(1));
1917 instruction->GetBlock()->RemoveInstruction(instruction);
1918 } else {
1919 StringEqualsOptimizations optimizations(instruction);
1920 if (CanEnsureNotNullAt(argument, instruction)) {
1921 optimizations.SetArgumentNotNull();
1922 }
1923 ScopedObjectAccess soa(Thread::Current());
1924 ReferenceTypeInfo argument_rti = argument->GetReferenceTypeInfo();
1925 if (argument_rti.IsValid() && argument_rti.IsStringClass()) {
1926 optimizations.SetArgumentIsString();
1927 }
1928 }
1929}
1930
Roland Levillain22c49222016-03-18 14:04:28 +00001931void InstructionSimplifierVisitor::SimplifyRotate(HInvoke* invoke,
1932 bool is_left,
1933 Primitive::Type type) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001934 DCHECK(invoke->IsInvokeStaticOrDirect());
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01001935 DCHECK_EQ(invoke->GetInvokeType(), InvokeType::kStatic);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001936 HInstruction* value = invoke->InputAt(0);
1937 HInstruction* distance = invoke->InputAt(1);
1938 // Replace the invoke with an HRor.
1939 if (is_left) {
Roland Levillain937e6cd2016-03-22 11:54:37 +00001940 // Unconditionally set the type of the negated distance to `int`,
1941 // as shift and rotate operations expect a 32-bit (or narrower)
1942 // value for their distance input.
1943 distance = new (GetGraph()->GetArena()) HNeg(Primitive::kPrimInt, distance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001944 invoke->GetBlock()->InsertInstructionBefore(distance, invoke);
1945 }
Roland Levillain22c49222016-03-18 14:04:28 +00001946 HRor* ror = new (GetGraph()->GetArena()) HRor(type, value, distance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001947 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, ror);
1948 // Remove ClinitCheck and LoadClass, if possible.
Vladimir Marko372f10e2016-05-17 16:30:10 +01001949 HInstruction* clinit = invoke->GetInputs().back();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001950 if (clinit->IsClinitCheck() && !clinit->HasUses()) {
1951 clinit->GetBlock()->RemoveInstruction(clinit);
1952 HInstruction* ldclass = clinit->InputAt(0);
1953 if (ldclass->IsLoadClass() && !ldclass->HasUses()) {
1954 ldclass->GetBlock()->RemoveInstruction(ldclass);
1955 }
1956 }
1957}
1958
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001959static bool IsArrayLengthOf(HInstruction* potential_length, HInstruction* potential_array) {
1960 if (potential_length->IsArrayLength()) {
1961 return potential_length->InputAt(0) == potential_array;
1962 }
1963
1964 if (potential_array->IsNewArray()) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00001965 return potential_array->AsNewArray()->GetLength() == potential_length;
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001966 }
1967
1968 return false;
1969}
1970
1971void InstructionSimplifierVisitor::SimplifySystemArrayCopy(HInvoke* instruction) {
1972 HInstruction* source = instruction->InputAt(0);
1973 HInstruction* destination = instruction->InputAt(2);
1974 HInstruction* count = instruction->InputAt(4);
1975 SystemArrayCopyOptimizations optimizations(instruction);
1976 if (CanEnsureNotNullAt(source, instruction)) {
1977 optimizations.SetSourceIsNotNull();
1978 }
1979 if (CanEnsureNotNullAt(destination, instruction)) {
1980 optimizations.SetDestinationIsNotNull();
1981 }
1982 if (destination == source) {
1983 optimizations.SetDestinationIsSource();
1984 }
1985
1986 if (IsArrayLengthOf(count, source)) {
1987 optimizations.SetCountIsSourceLength();
1988 }
1989
1990 if (IsArrayLengthOf(count, destination)) {
1991 optimizations.SetCountIsDestinationLength();
1992 }
1993
1994 {
1995 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00001996 Primitive::Type source_component_type = Primitive::kPrimVoid;
1997 Primitive::Type destination_component_type = Primitive::kPrimVoid;
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001998 ReferenceTypeInfo destination_rti = destination->GetReferenceTypeInfo();
1999 if (destination_rti.IsValid()) {
2000 if (destination_rti.IsObjectArray()) {
2001 if (destination_rti.IsExact()) {
2002 optimizations.SetDoesNotNeedTypeCheck();
2003 }
2004 optimizations.SetDestinationIsTypedObjectArray();
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01002005 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01002006 if (destination_rti.IsPrimitiveArrayClass()) {
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002007 destination_component_type =
2008 destination_rti.GetTypeHandle()->GetComponentType()->GetPrimitiveType();
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01002009 optimizations.SetDestinationIsPrimitiveArray();
2010 } else if (destination_rti.IsNonPrimitiveArrayClass()) {
2011 optimizations.SetDestinationIsNonPrimitiveArray();
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01002012 }
2013 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01002014 ReferenceTypeInfo source_rti = source->GetReferenceTypeInfo();
2015 if (source_rti.IsValid()) {
2016 if (destination_rti.IsValid() && destination_rti.CanArrayHoldValuesOf(source_rti)) {
2017 optimizations.SetDoesNotNeedTypeCheck();
2018 }
2019 if (source_rti.IsPrimitiveArrayClass()) {
2020 optimizations.SetSourceIsPrimitiveArray();
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002021 source_component_type = source_rti.GetTypeHandle()->GetComponentType()->GetPrimitiveType();
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01002022 } else if (source_rti.IsNonPrimitiveArrayClass()) {
2023 optimizations.SetSourceIsNonPrimitiveArray();
2024 }
2025 }
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002026 // For primitive arrays, use their optimized ArtMethod implementations.
2027 if ((source_component_type != Primitive::kPrimVoid) &&
2028 (source_component_type == destination_component_type)) {
2029 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
2030 PointerSize image_size = class_linker->GetImagePointerSize();
2031 HInvokeStaticOrDirect* invoke = instruction->AsInvokeStaticOrDirect();
2032 mirror::Class* system = invoke->GetResolvedMethod()->GetDeclaringClass();
2033 ArtMethod* method = nullptr;
2034 switch (source_component_type) {
2035 case Primitive::kPrimBoolean:
Vladimir Markoba118822017-06-12 15:41:56 +01002036 method = system->FindClassMethod("arraycopy", "([ZI[ZII)V", image_size);
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002037 break;
2038 case Primitive::kPrimByte:
Vladimir Markoba118822017-06-12 15:41:56 +01002039 method = system->FindClassMethod("arraycopy", "([BI[BII)V", image_size);
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002040 break;
2041 case Primitive::kPrimChar:
Vladimir Markoba118822017-06-12 15:41:56 +01002042 method = system->FindClassMethod("arraycopy", "([CI[CII)V", image_size);
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002043 break;
2044 case Primitive::kPrimShort:
Vladimir Markoba118822017-06-12 15:41:56 +01002045 method = system->FindClassMethod("arraycopy", "([SI[SII)V", image_size);
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002046 break;
2047 case Primitive::kPrimInt:
Vladimir Markoba118822017-06-12 15:41:56 +01002048 method = system->FindClassMethod("arraycopy", "([II[III)V", image_size);
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002049 break;
2050 case Primitive::kPrimFloat:
Vladimir Markoba118822017-06-12 15:41:56 +01002051 method = system->FindClassMethod("arraycopy", "([FI[FII)V", image_size);
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002052 break;
2053 case Primitive::kPrimLong:
Vladimir Markoba118822017-06-12 15:41:56 +01002054 method = system->FindClassMethod("arraycopy", "([JI[JII)V", image_size);
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002055 break;
2056 case Primitive::kPrimDouble:
Vladimir Markoba118822017-06-12 15:41:56 +01002057 method = system->FindClassMethod("arraycopy", "([DI[DII)V", image_size);
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002058 break;
2059 default:
2060 LOG(FATAL) << "Unreachable";
2061 }
2062 DCHECK(method != nullptr);
Vladimir Markoba118822017-06-12 15:41:56 +01002063 DCHECK(method->IsStatic());
2064 DCHECK(method->GetDeclaringClass() == system);
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002065 invoke->SetResolvedMethod(method);
2066 // Sharpen the new invoke. Note that we do not update the dex method index of
2067 // the invoke, as we would need to look it up in the current dex file, and it
2068 // is unlikely that it exists. The most usual situation for such typed
2069 // arraycopy methods is a direct pointer to the boot image.
Vladimir Marko65979462017-05-19 17:25:12 +01002070 HSharpening::SharpenInvokeStaticOrDirect(invoke, codegen_, compiler_driver_);
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002071 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01002072 }
2073}
2074
Roland Levillaina5c4a402016-03-15 15:02:50 +00002075void InstructionSimplifierVisitor::SimplifyCompare(HInvoke* invoke,
2076 bool is_signum,
2077 Primitive::Type type) {
Aart Bika19616e2016-02-01 18:57:58 -08002078 DCHECK(invoke->IsInvokeStaticOrDirect());
2079 uint32_t dex_pc = invoke->GetDexPc();
2080 HInstruction* left = invoke->InputAt(0);
2081 HInstruction* right;
Aart Bika19616e2016-02-01 18:57:58 -08002082 if (!is_signum) {
2083 right = invoke->InputAt(1);
2084 } else if (type == Primitive::kPrimLong) {
2085 right = GetGraph()->GetLongConstant(0);
2086 } else {
2087 right = GetGraph()->GetIntConstant(0);
2088 }
2089 HCompare* compare = new (GetGraph()->GetArena())
2090 HCompare(type, left, right, ComparisonBias::kNoBias, dex_pc);
2091 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, compare);
2092}
2093
Aart Bik75a38b22016-02-17 10:41:50 -08002094void InstructionSimplifierVisitor::SimplifyIsNaN(HInvoke* invoke) {
2095 DCHECK(invoke->IsInvokeStaticOrDirect());
2096 uint32_t dex_pc = invoke->GetDexPc();
2097 // IsNaN(x) is the same as x != x.
2098 HInstruction* x = invoke->InputAt(0);
2099 HCondition* condition = new (GetGraph()->GetArena()) HNotEqual(x, x, dex_pc);
Aart Bik8ffc1fa2016-02-17 15:13:56 -08002100 condition->SetBias(ComparisonBias::kLtBias);
Aart Bik75a38b22016-02-17 10:41:50 -08002101 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, condition);
2102}
2103
Aart Bik2a6aad92016-02-25 11:32:32 -08002104void InstructionSimplifierVisitor::SimplifyFP2Int(HInvoke* invoke) {
2105 DCHECK(invoke->IsInvokeStaticOrDirect());
2106 uint32_t dex_pc = invoke->GetDexPc();
2107 HInstruction* x = invoke->InputAt(0);
2108 Primitive::Type type = x->GetType();
2109 // Set proper bit pattern for NaN and replace intrinsic with raw version.
2110 HInstruction* nan;
2111 if (type == Primitive::kPrimDouble) {
2112 nan = GetGraph()->GetLongConstant(0x7ff8000000000000L);
2113 invoke->SetIntrinsic(Intrinsics::kDoubleDoubleToRawLongBits,
2114 kNeedsEnvironmentOrCache,
2115 kNoSideEffects,
2116 kNoThrow);
2117 } else {
2118 DCHECK_EQ(type, Primitive::kPrimFloat);
2119 nan = GetGraph()->GetIntConstant(0x7fc00000);
2120 invoke->SetIntrinsic(Intrinsics::kFloatFloatToRawIntBits,
2121 kNeedsEnvironmentOrCache,
2122 kNoSideEffects,
2123 kNoThrow);
2124 }
2125 // Test IsNaN(x), which is the same as x != x.
2126 HCondition* condition = new (GetGraph()->GetArena()) HNotEqual(x, x, dex_pc);
2127 condition->SetBias(ComparisonBias::kLtBias);
2128 invoke->GetBlock()->InsertInstructionBefore(condition, invoke->GetNext());
2129 // Select between the two.
2130 HInstruction* select = new (GetGraph()->GetArena()) HSelect(condition, nan, invoke, dex_pc);
2131 invoke->GetBlock()->InsertInstructionBefore(select, condition->GetNext());
2132 invoke->ReplaceWithExceptInReplacementAtIndex(select, 0); // false at index 0
2133}
2134
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002135void InstructionSimplifierVisitor::SimplifyStringCharAt(HInvoke* invoke) {
2136 HInstruction* str = invoke->InputAt(0);
2137 HInstruction* index = invoke->InputAt(1);
2138 uint32_t dex_pc = invoke->GetDexPc();
2139 ArenaAllocator* arena = GetGraph()->GetArena();
2140 // We treat String as an array to allow DCE and BCE to seamlessly work on strings,
2141 // so create the HArrayLength, HBoundsCheck and HArrayGet.
2142 HArrayLength* length = new (arena) HArrayLength(str, dex_pc, /* is_string_length */ true);
2143 invoke->GetBlock()->InsertInstructionBefore(length, invoke);
Nicolas Geoffray431121f2017-01-09 14:02:45 +00002144 HBoundsCheck* bounds_check = new (arena) HBoundsCheck(
2145 index, length, dex_pc, invoke->GetDexMethodIndex());
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002146 invoke->GetBlock()->InsertInstructionBefore(bounds_check, invoke);
Nicolas Geoffray431121f2017-01-09 14:02:45 +00002147 HArrayGet* array_get = new (arena) HArrayGet(
2148 str, bounds_check, Primitive::kPrimChar, dex_pc, /* is_string_char_at */ true);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002149 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, array_get);
2150 bounds_check->CopyEnvironmentFrom(invoke->GetEnvironment());
2151 GetGraph()->SetHasBoundsChecks(true);
2152}
2153
Vladimir Markodce016e2016-04-28 13:10:02 +01002154void InstructionSimplifierVisitor::SimplifyStringIsEmptyOrLength(HInvoke* invoke) {
2155 HInstruction* str = invoke->InputAt(0);
2156 uint32_t dex_pc = invoke->GetDexPc();
2157 // We treat String as an array to allow DCE and BCE to seamlessly work on strings,
2158 // so create the HArrayLength.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002159 HArrayLength* length =
2160 new (GetGraph()->GetArena()) HArrayLength(str, dex_pc, /* is_string_length */ true);
Vladimir Markodce016e2016-04-28 13:10:02 +01002161 HInstruction* replacement;
2162 if (invoke->GetIntrinsic() == Intrinsics::kStringIsEmpty) {
2163 // For String.isEmpty(), create the `HEqual` representing the `length == 0`.
2164 invoke->GetBlock()->InsertInstructionBefore(length, invoke);
2165 HIntConstant* zero = GetGraph()->GetIntConstant(0);
2166 HEqual* equal = new (GetGraph()->GetArena()) HEqual(length, zero, dex_pc);
2167 replacement = equal;
2168 } else {
2169 DCHECK_EQ(invoke->GetIntrinsic(), Intrinsics::kStringLength);
2170 replacement = length;
2171 }
2172 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, replacement);
2173}
2174
Aart Bikff7d89c2016-11-07 08:49:28 -08002175// This method should only be used on intrinsics whose sole way of throwing an
2176// exception is raising a NPE when the nth argument is null. If that argument
2177// is provably non-null, we can clear the flag.
2178void InstructionSimplifierVisitor::SimplifyNPEOnArgN(HInvoke* invoke, size_t n) {
2179 HInstruction* arg = invoke->InputAt(n);
Aart Bik71bf7b42016-11-16 10:17:46 -08002180 if (invoke->CanThrow() && !arg->CanBeNull()) {
Aart Bikff7d89c2016-11-07 08:49:28 -08002181 invoke->SetCanThrow(false);
2182 }
2183}
2184
Aart Bik71bf7b42016-11-16 10:17:46 -08002185// Methods that return "this" can replace the returned value with the receiver.
2186void InstructionSimplifierVisitor::SimplifyReturnThis(HInvoke* invoke) {
2187 if (invoke->HasUses()) {
2188 HInstruction* receiver = invoke->InputAt(0);
2189 invoke->ReplaceWith(receiver);
2190 RecordSimplification();
2191 }
2192}
2193
2194// Helper method for StringBuffer escape analysis.
2195static bool NoEscapeForStringBufferReference(HInstruction* reference, HInstruction* user) {
2196 if (user->IsInvokeStaticOrDirect()) {
2197 // Any constructor on StringBuffer is okay.
Aart Bikab2270f2016-12-15 09:36:31 -08002198 return user->AsInvokeStaticOrDirect()->GetResolvedMethod() != nullptr &&
2199 user->AsInvokeStaticOrDirect()->GetResolvedMethod()->IsConstructor() &&
Aart Bik71bf7b42016-11-16 10:17:46 -08002200 user->InputAt(0) == reference;
2201 } else if (user->IsInvokeVirtual()) {
2202 switch (user->AsInvokeVirtual()->GetIntrinsic()) {
2203 case Intrinsics::kStringBufferLength:
2204 case Intrinsics::kStringBufferToString:
2205 DCHECK_EQ(user->InputAt(0), reference);
2206 return true;
2207 case Intrinsics::kStringBufferAppend:
2208 // Returns "this", so only okay if no further uses.
2209 DCHECK_EQ(user->InputAt(0), reference);
2210 DCHECK_NE(user->InputAt(1), reference);
2211 return !user->HasUses();
2212 default:
2213 break;
2214 }
2215 }
2216 return false;
2217}
2218
2219// Certain allocation intrinsics are not removed by dead code elimination
2220// because of potentially throwing an OOM exception or other side effects.
2221// This method removes such intrinsics when special circumstances allow.
2222void InstructionSimplifierVisitor::SimplifyAllocationIntrinsic(HInvoke* invoke) {
2223 if (!invoke->HasUses()) {
2224 // Instruction has no uses. If unsynchronized, we can remove right away, safely ignoring
2225 // the potential OOM of course. Otherwise, we must ensure the receiver object of this
2226 // call does not escape since only thread-local synchronization may be removed.
2227 bool is_synchronized = invoke->GetIntrinsic() == Intrinsics::kStringBufferToString;
2228 HInstruction* receiver = invoke->InputAt(0);
2229 if (!is_synchronized || DoesNotEscape(receiver, NoEscapeForStringBufferReference)) {
2230 invoke->GetBlock()->RemoveInstruction(invoke);
2231 RecordSimplification();
2232 }
2233 }
2234}
2235
Aart Bik11932592016-03-08 12:42:25 -08002236void InstructionSimplifierVisitor::SimplifyMemBarrier(HInvoke* invoke, MemBarrierKind barrier_kind) {
2237 uint32_t dex_pc = invoke->GetDexPc();
2238 HMemoryBarrier* mem_barrier = new (GetGraph()->GetArena()) HMemoryBarrier(barrier_kind, dex_pc);
2239 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, mem_barrier);
2240}
2241
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01002242void InstructionSimplifierVisitor::VisitInvoke(HInvoke* instruction) {
Aart Bik2a6aad92016-02-25 11:32:32 -08002243 switch (instruction->GetIntrinsic()) {
2244 case Intrinsics::kStringEquals:
2245 SimplifyStringEquals(instruction);
2246 break;
2247 case Intrinsics::kSystemArrayCopy:
2248 SimplifySystemArrayCopy(instruction);
2249 break;
2250 case Intrinsics::kIntegerRotateRight:
Roland Levillain22c49222016-03-18 14:04:28 +00002251 SimplifyRotate(instruction, /* is_left */ false, Primitive::kPrimInt);
2252 break;
Aart Bik2a6aad92016-02-25 11:32:32 -08002253 case Intrinsics::kLongRotateRight:
Roland Levillain22c49222016-03-18 14:04:28 +00002254 SimplifyRotate(instruction, /* is_left */ false, Primitive::kPrimLong);
Aart Bik2a6aad92016-02-25 11:32:32 -08002255 break;
2256 case Intrinsics::kIntegerRotateLeft:
Roland Levillain22c49222016-03-18 14:04:28 +00002257 SimplifyRotate(instruction, /* is_left */ true, Primitive::kPrimInt);
2258 break;
Aart Bik2a6aad92016-02-25 11:32:32 -08002259 case Intrinsics::kLongRotateLeft:
Roland Levillain22c49222016-03-18 14:04:28 +00002260 SimplifyRotate(instruction, /* is_left */ true, Primitive::kPrimLong);
Aart Bik2a6aad92016-02-25 11:32:32 -08002261 break;
2262 case Intrinsics::kIntegerCompare:
Roland Levillaina5c4a402016-03-15 15:02:50 +00002263 SimplifyCompare(instruction, /* is_signum */ false, Primitive::kPrimInt);
2264 break;
Aart Bik2a6aad92016-02-25 11:32:32 -08002265 case Intrinsics::kLongCompare:
Roland Levillaina5c4a402016-03-15 15:02:50 +00002266 SimplifyCompare(instruction, /* is_signum */ false, Primitive::kPrimLong);
Aart Bik2a6aad92016-02-25 11:32:32 -08002267 break;
2268 case Intrinsics::kIntegerSignum:
Roland Levillaina5c4a402016-03-15 15:02:50 +00002269 SimplifyCompare(instruction, /* is_signum */ true, Primitive::kPrimInt);
2270 break;
Aart Bik2a6aad92016-02-25 11:32:32 -08002271 case Intrinsics::kLongSignum:
Roland Levillaina5c4a402016-03-15 15:02:50 +00002272 SimplifyCompare(instruction, /* is_signum */ true, Primitive::kPrimLong);
Aart Bik2a6aad92016-02-25 11:32:32 -08002273 break;
2274 case Intrinsics::kFloatIsNaN:
2275 case Intrinsics::kDoubleIsNaN:
2276 SimplifyIsNaN(instruction);
2277 break;
2278 case Intrinsics::kFloatFloatToIntBits:
2279 case Intrinsics::kDoubleDoubleToLongBits:
2280 SimplifyFP2Int(instruction);
2281 break;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002282 case Intrinsics::kStringCharAt:
2283 SimplifyStringCharAt(instruction);
2284 break;
Vladimir Markodce016e2016-04-28 13:10:02 +01002285 case Intrinsics::kStringIsEmpty:
2286 case Intrinsics::kStringLength:
2287 SimplifyStringIsEmptyOrLength(instruction);
2288 break;
Aart Bikff7d89c2016-11-07 08:49:28 -08002289 case Intrinsics::kStringStringIndexOf:
2290 case Intrinsics::kStringStringIndexOfAfter:
2291 SimplifyNPEOnArgN(instruction, 1); // 0th has own NullCheck
2292 break;
Aart Bik71bf7b42016-11-16 10:17:46 -08002293 case Intrinsics::kStringBufferAppend:
2294 case Intrinsics::kStringBuilderAppend:
2295 SimplifyReturnThis(instruction);
2296 break;
2297 case Intrinsics::kStringBufferToString:
2298 case Intrinsics::kStringBuilderToString:
2299 SimplifyAllocationIntrinsic(instruction);
2300 break;
Aart Bik11932592016-03-08 12:42:25 -08002301 case Intrinsics::kUnsafeLoadFence:
2302 SimplifyMemBarrier(instruction, MemBarrierKind::kLoadAny);
2303 break;
2304 case Intrinsics::kUnsafeStoreFence:
2305 SimplifyMemBarrier(instruction, MemBarrierKind::kAnyStore);
2306 break;
2307 case Intrinsics::kUnsafeFullFence:
2308 SimplifyMemBarrier(instruction, MemBarrierKind::kAnyAny);
2309 break;
Aart Bik2a6aad92016-02-25 11:32:32 -08002310 default:
2311 break;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01002312 }
2313}
2314
Aart Bikbb245d12015-10-19 11:05:03 -07002315void InstructionSimplifierVisitor::VisitDeoptimize(HDeoptimize* deoptimize) {
2316 HInstruction* cond = deoptimize->InputAt(0);
2317 if (cond->IsConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002318 if (cond->AsIntConstant()->IsFalse()) {
Aart Bikbb245d12015-10-19 11:05:03 -07002319 // Never deopt: instruction can be removed.
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002320 if (deoptimize->GuardsAnInput()) {
2321 deoptimize->ReplaceWith(deoptimize->GuardedInput());
2322 }
Aart Bikbb245d12015-10-19 11:05:03 -07002323 deoptimize->GetBlock()->RemoveInstruction(deoptimize);
2324 } else {
2325 // Always deopt.
2326 }
2327 }
2328}
2329
Anton Kirilove14dc862016-05-13 17:56:15 +01002330// Replace code looking like
2331// OP y, x, const1
2332// OP z, y, const2
2333// with
2334// OP z, x, const3
2335// where OP is both an associative and a commutative operation.
2336bool InstructionSimplifierVisitor::TryHandleAssociativeAndCommutativeOperation(
2337 HBinaryOperation* instruction) {
2338 DCHECK(instruction->IsCommutative());
2339
2340 if (!Primitive::IsIntegralType(instruction->GetType())) {
2341 return false;
2342 }
2343
2344 HInstruction* left = instruction->GetLeft();
2345 HInstruction* right = instruction->GetRight();
2346 // Variable names as described above.
2347 HConstant* const2;
2348 HBinaryOperation* y;
2349
2350 if (instruction->InstructionTypeEquals(left) && right->IsConstant()) {
2351 const2 = right->AsConstant();
2352 y = left->AsBinaryOperation();
2353 } else if (left->IsConstant() && instruction->InstructionTypeEquals(right)) {
2354 const2 = left->AsConstant();
2355 y = right->AsBinaryOperation();
2356 } else {
2357 // The node does not match the pattern.
2358 return false;
2359 }
2360
2361 // If `y` has more than one use, we do not perform the optimization
2362 // because it might increase code size (e.g. if the new constant is
2363 // no longer encodable as an immediate operand in the target ISA).
2364 if (!y->HasOnlyOneNonEnvironmentUse()) {
2365 return false;
2366 }
2367
2368 // GetConstantRight() can return both left and right constants
2369 // for commutative operations.
2370 HConstant* const1 = y->GetConstantRight();
2371 if (const1 == nullptr) {
2372 return false;
2373 }
2374
2375 instruction->ReplaceInput(const1, 0);
2376 instruction->ReplaceInput(const2, 1);
2377 HConstant* const3 = instruction->TryStaticEvaluation();
2378 DCHECK(const3 != nullptr);
2379 instruction->ReplaceInput(y->GetLeastConstantLeft(), 0);
2380 instruction->ReplaceInput(const3, 1);
2381 RecordSimplification();
2382 return true;
2383}
2384
2385static HBinaryOperation* AsAddOrSub(HInstruction* binop) {
2386 return (binop->IsAdd() || binop->IsSub()) ? binop->AsBinaryOperation() : nullptr;
2387}
2388
2389// Helper function that performs addition statically, considering the result type.
2390static int64_t ComputeAddition(Primitive::Type type, int64_t x, int64_t y) {
2391 // Use the Compute() method for consistency with TryStaticEvaluation().
2392 if (type == Primitive::kPrimInt) {
2393 return HAdd::Compute<int32_t>(x, y);
2394 } else {
2395 DCHECK_EQ(type, Primitive::kPrimLong);
2396 return HAdd::Compute<int64_t>(x, y);
2397 }
2398}
2399
2400// Helper function that handles the child classes of HConstant
2401// and returns an integer with the appropriate sign.
2402static int64_t GetValue(HConstant* constant, bool is_negated) {
2403 int64_t ret = Int64FromConstant(constant);
2404 return is_negated ? -ret : ret;
2405}
2406
2407// Replace code looking like
2408// OP1 y, x, const1
2409// OP2 z, y, const2
2410// with
2411// OP3 z, x, const3
2412// where OPx is either ADD or SUB, and at least one of OP{1,2} is SUB.
2413bool InstructionSimplifierVisitor::TrySubtractionChainSimplification(
2414 HBinaryOperation* instruction) {
2415 DCHECK(instruction->IsAdd() || instruction->IsSub()) << instruction->DebugName();
2416
2417 Primitive::Type type = instruction->GetType();
2418 if (!Primitive::IsIntegralType(type)) {
2419 return false;
2420 }
2421
2422 HInstruction* left = instruction->GetLeft();
2423 HInstruction* right = instruction->GetRight();
2424 // Variable names as described above.
2425 HConstant* const2 = right->IsConstant() ? right->AsConstant() : left->AsConstant();
2426 if (const2 == nullptr) {
2427 return false;
2428 }
2429
2430 HBinaryOperation* y = (AsAddOrSub(left) != nullptr)
2431 ? left->AsBinaryOperation()
2432 : AsAddOrSub(right);
2433 // If y has more than one use, we do not perform the optimization because
2434 // it might increase code size (e.g. if the new constant is no longer
2435 // encodable as an immediate operand in the target ISA).
2436 if ((y == nullptr) || !y->HasOnlyOneNonEnvironmentUse()) {
2437 return false;
2438 }
2439
2440 left = y->GetLeft();
2441 HConstant* const1 = left->IsConstant() ? left->AsConstant() : y->GetRight()->AsConstant();
2442 if (const1 == nullptr) {
2443 return false;
2444 }
2445
2446 HInstruction* x = (const1 == left) ? y->GetRight() : left;
2447 // If both inputs are constants, let the constant folding pass deal with it.
2448 if (x->IsConstant()) {
2449 return false;
2450 }
2451
2452 bool is_const2_negated = (const2 == right) && instruction->IsSub();
2453 int64_t const2_val = GetValue(const2, is_const2_negated);
2454 bool is_y_negated = (y == right) && instruction->IsSub();
2455 right = y->GetRight();
2456 bool is_const1_negated = is_y_negated ^ ((const1 == right) && y->IsSub());
2457 int64_t const1_val = GetValue(const1, is_const1_negated);
2458 bool is_x_negated = is_y_negated ^ ((x == right) && y->IsSub());
2459 int64_t const3_val = ComputeAddition(type, const1_val, const2_val);
2460 HBasicBlock* block = instruction->GetBlock();
2461 HConstant* const3 = block->GetGraph()->GetConstant(type, const3_val);
2462 ArenaAllocator* arena = instruction->GetArena();
2463 HInstruction* z;
2464
2465 if (is_x_negated) {
2466 z = new (arena) HSub(type, const3, x, instruction->GetDexPc());
2467 } else {
2468 z = new (arena) HAdd(type, x, const3, instruction->GetDexPc());
2469 }
2470
2471 block->ReplaceAndRemoveInstructionWith(instruction, z);
2472 RecordSimplification();
2473 return true;
2474}
2475
Lena Djokicbc5460b2017-07-20 16:07:36 +02002476void InstructionSimplifierVisitor::VisitVecMul(HVecMul* instruction) {
2477 if (TryCombineVecMultiplyAccumulate(instruction)) {
2478 RecordSimplification();
2479 }
2480}
2481
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002482} // namespace art