blob: 4ca0600dba89bff514eb5099206be4c2a6eab61c [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
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +010019#include "intrinsics.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000020#include "mirror/class-inl.h"
21#include "scoped_thread_state_change.h"
22
Nicolas Geoffray3c049742014-09-24 18:10:46 +010023namespace art {
24
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +010025class InstructionSimplifierVisitor : public HGraphDelegateVisitor {
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000026 public:
Calin Juravleacf735c2015-02-12 15:25:22 +000027 InstructionSimplifierVisitor(HGraph* graph, OptimizingCompilerStats* stats)
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +010028 : HGraphDelegateVisitor(graph),
Alexandre Rames188d4312015-04-09 18:30:21 +010029 stats_(stats) {}
30
31 void Run();
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000032
33 private:
Alexandre Rames188d4312015-04-09 18:30:21 +010034 void RecordSimplification() {
35 simplification_occurred_ = true;
36 simplifications_at_current_position_++;
Calin Juravle69158982016-03-16 11:53:41 +000037 MaybeRecordStat(kInstructionSimplifications);
38 }
39
40 void MaybeRecordStat(MethodCompilationStat stat) {
41 if (stats_ != nullptr) {
42 stats_->RecordStat(stat);
Alexandre Rames188d4312015-04-09 18:30:21 +010043 }
44 }
45
Scott Wakeling40a04bf2015-12-11 09:50:36 +000046 bool ReplaceRotateWithRor(HBinaryOperation* op, HUShr* ushr, HShl* shl);
47 bool TryReplaceWithRotate(HBinaryOperation* instruction);
48 bool TryReplaceWithRotateConstantPattern(HBinaryOperation* op, HUShr* ushr, HShl* shl);
49 bool TryReplaceWithRotateRegisterNegPattern(HBinaryOperation* op, HUShr* ushr, HShl* shl);
50 bool TryReplaceWithRotateRegisterSubPattern(HBinaryOperation* op, HUShr* ushr, HShl* shl);
51
Alexandre Rames188d4312015-04-09 18:30:21 +010052 bool TryMoveNegOnInputsAfterBinop(HBinaryOperation* binop);
Alexandre Ramesca0e3a02016-02-03 10:54:07 +000053 // `op` should be either HOr or HAnd.
54 // De Morgan's laws:
55 // ~a & ~b = ~(a | b) and ~a | ~b = ~(a & b)
56 bool TryDeMorganNegationFactoring(HBinaryOperation* op);
Anton Kirilove14dc862016-05-13 17:56:15 +010057 bool TryHandleAssociativeAndCommutativeOperation(HBinaryOperation* instruction);
58 bool TrySubtractionChainSimplification(HBinaryOperation* instruction);
59
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000060 void VisitShift(HBinaryOperation* shift);
61
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000062 void VisitEqual(HEqual* equal) OVERRIDE;
David Brazdil0d13fee2015-04-17 14:52:19 +010063 void VisitNotEqual(HNotEqual* equal) OVERRIDE;
64 void VisitBooleanNot(HBooleanNot* bool_not) OVERRIDE;
Nicolas Geoffray07276db2015-05-18 14:22:09 +010065 void VisitInstanceFieldSet(HInstanceFieldSet* equal) OVERRIDE;
66 void VisitStaticFieldSet(HStaticFieldSet* equal) OVERRIDE;
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000067 void VisitArraySet(HArraySet* equal) OVERRIDE;
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +000068 void VisitTypeConversion(HTypeConversion* instruction) OVERRIDE;
Calin Juravle10e244f2015-01-26 18:54:32 +000069 void VisitNullCheck(HNullCheck* instruction) OVERRIDE;
Mingyao Yang0304e182015-01-30 16:41:29 -080070 void VisitArrayLength(HArrayLength* instruction) OVERRIDE;
Calin Juravleacf735c2015-02-12 15:25:22 +000071 void VisitCheckCast(HCheckCast* instruction) OVERRIDE;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000072 void VisitAdd(HAdd* instruction) OVERRIDE;
73 void VisitAnd(HAnd* instruction) OVERRIDE;
Mark Mendellc4701932015-04-10 13:18:51 -040074 void VisitCondition(HCondition* instruction) OVERRIDE;
75 void VisitGreaterThan(HGreaterThan* condition) OVERRIDE;
76 void VisitGreaterThanOrEqual(HGreaterThanOrEqual* condition) OVERRIDE;
77 void VisitLessThan(HLessThan* condition) OVERRIDE;
78 void VisitLessThanOrEqual(HLessThanOrEqual* condition) OVERRIDE;
Anton Shaminbdd79352016-02-15 12:48:36 +060079 void VisitBelow(HBelow* condition) OVERRIDE;
80 void VisitBelowOrEqual(HBelowOrEqual* condition) OVERRIDE;
81 void VisitAbove(HAbove* condition) OVERRIDE;
82 void VisitAboveOrEqual(HAboveOrEqual* condition) OVERRIDE;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000083 void VisitDiv(HDiv* instruction) OVERRIDE;
84 void VisitMul(HMul* instruction) OVERRIDE;
Alexandre Rames188d4312015-04-09 18:30:21 +010085 void VisitNeg(HNeg* instruction) OVERRIDE;
86 void VisitNot(HNot* instruction) OVERRIDE;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000087 void VisitOr(HOr* instruction) OVERRIDE;
88 void VisitShl(HShl* instruction) OVERRIDE;
89 void VisitShr(HShr* instruction) OVERRIDE;
90 void VisitSub(HSub* instruction) OVERRIDE;
91 void VisitUShr(HUShr* instruction) OVERRIDE;
92 void VisitXor(HXor* instruction) OVERRIDE;
David Brazdil74eb1b22015-12-14 11:44:01 +000093 void VisitSelect(HSelect* select) OVERRIDE;
94 void VisitIf(HIf* instruction) OVERRIDE;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +010095 void VisitInstanceOf(HInstanceOf* instruction) OVERRIDE;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +010096 void VisitInvoke(HInvoke* invoke) OVERRIDE;
Aart Bikbb245d12015-10-19 11:05:03 -070097 void VisitDeoptimize(HDeoptimize* deoptimize) OVERRIDE;
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +010098
99 bool CanEnsureNotNullAt(HInstruction* instr, HInstruction* at) const;
Calin Juravleacf735c2015-02-12 15:25:22 +0000100
Roland Levillain22c49222016-03-18 14:04:28 +0000101 void SimplifyRotate(HInvoke* invoke, bool is_left, Primitive::Type type);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100102 void SimplifySystemArrayCopy(HInvoke* invoke);
103 void SimplifyStringEquals(HInvoke* invoke);
Roland Levillaina5c4a402016-03-15 15:02:50 +0000104 void SimplifyCompare(HInvoke* invoke, bool is_signum, Primitive::Type type);
Aart Bik75a38b22016-02-17 10:41:50 -0800105 void SimplifyIsNaN(HInvoke* invoke);
Aart Bik2a6aad92016-02-25 11:32:32 -0800106 void SimplifyFP2Int(HInvoke* invoke);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100107 void SimplifyStringCharAt(HInvoke* invoke);
Vladimir Markodce016e2016-04-28 13:10:02 +0100108 void SimplifyStringIsEmptyOrLength(HInvoke* invoke);
Aart Bik11932592016-03-08 12:42:25 -0800109 void SimplifyMemBarrier(HInvoke* invoke, MemBarrierKind barrier_kind);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100110
Calin Juravleacf735c2015-02-12 15:25:22 +0000111 OptimizingCompilerStats* stats_;
Alexandre Rames188d4312015-04-09 18:30:21 +0100112 bool simplification_occurred_ = false;
113 int simplifications_at_current_position_ = 0;
114 // We ensure we do not loop infinitely. The value is a finger in the air guess
115 // that should allow enough simplification.
116 static constexpr int kMaxSamePositionSimplifications = 10;
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000117};
118
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100119void InstructionSimplifier::Run() {
Calin Juravleacf735c2015-02-12 15:25:22 +0000120 InstructionSimplifierVisitor visitor(graph_, stats_);
Alexandre Rames188d4312015-04-09 18:30:21 +0100121 visitor.Run();
122}
123
124void InstructionSimplifierVisitor::Run() {
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100125 // Iterate in reverse post order to open up more simplifications to users
126 // of instructions that got simplified.
Alexandre Rames188d4312015-04-09 18:30:21 +0100127 for (HReversePostOrderIterator it(*GetGraph()); !it.Done();) {
128 // The simplification of an instruction to another instruction may yield
129 // possibilities for other simplifications. So although we perform a reverse
130 // post order visit, we sometimes need to revisit an instruction index.
131 simplification_occurred_ = false;
132 VisitBasicBlock(it.Current());
133 if (simplification_occurred_ &&
134 (simplifications_at_current_position_ < kMaxSamePositionSimplifications)) {
135 // New simplifications may be applicable to the instruction at the
136 // current index, so don't advance the iterator.
137 continue;
138 }
Alexandre Rames188d4312015-04-09 18:30:21 +0100139 simplifications_at_current_position_ = 0;
140 it.Advance();
141 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100142}
143
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000144namespace {
145
146bool AreAllBitsSet(HConstant* constant) {
147 return Int64FromConstant(constant) == -1;
148}
149
150} // namespace
151
Alexandre Rames188d4312015-04-09 18:30:21 +0100152// Returns true if the code was simplified to use only one negation operation
153// after the binary operation instead of one on each of the inputs.
154bool InstructionSimplifierVisitor::TryMoveNegOnInputsAfterBinop(HBinaryOperation* binop) {
155 DCHECK(binop->IsAdd() || binop->IsSub());
156 DCHECK(binop->GetLeft()->IsNeg() && binop->GetRight()->IsNeg());
157 HNeg* left_neg = binop->GetLeft()->AsNeg();
158 HNeg* right_neg = binop->GetRight()->AsNeg();
159 if (!left_neg->HasOnlyOneNonEnvironmentUse() ||
160 !right_neg->HasOnlyOneNonEnvironmentUse()) {
161 return false;
162 }
163 // Replace code looking like
164 // NEG tmp1, a
165 // NEG tmp2, b
166 // ADD dst, tmp1, tmp2
167 // with
168 // ADD tmp, a, b
169 // NEG dst, tmp
Serdjuk, Nikolay Yaae9e662015-08-21 13:26:34 +0600170 // Note that we cannot optimize `(-a) + (-b)` to `-(a + b)` for floating-point.
171 // When `a` is `-0.0` and `b` is `0.0`, the former expression yields `0.0`,
172 // while the later yields `-0.0`.
173 if (!Primitive::IsIntegralType(binop->GetType())) {
174 return false;
175 }
Alexandre Rames188d4312015-04-09 18:30:21 +0100176 binop->ReplaceInput(left_neg->GetInput(), 0);
177 binop->ReplaceInput(right_neg->GetInput(), 1);
178 left_neg->GetBlock()->RemoveInstruction(left_neg);
179 right_neg->GetBlock()->RemoveInstruction(right_neg);
180 HNeg* neg = new (GetGraph()->GetArena()) HNeg(binop->GetType(), binop);
181 binop->GetBlock()->InsertInstructionBefore(neg, binop->GetNext());
182 binop->ReplaceWithExceptInReplacementAtIndex(neg, 0);
183 RecordSimplification();
184 return true;
185}
186
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000187bool InstructionSimplifierVisitor::TryDeMorganNegationFactoring(HBinaryOperation* op) {
188 DCHECK(op->IsAnd() || op->IsOr()) << op->DebugName();
189 Primitive::Type type = op->GetType();
190 HInstruction* left = op->GetLeft();
191 HInstruction* right = op->GetRight();
192
193 // We can apply De Morgan's laws if both inputs are Not's and are only used
194 // by `op`.
Alexandre Rames9f980252016-02-05 14:00:28 +0000195 if (((left->IsNot() && right->IsNot()) ||
196 (left->IsBooleanNot() && right->IsBooleanNot())) &&
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000197 left->HasOnlyOneNonEnvironmentUse() &&
198 right->HasOnlyOneNonEnvironmentUse()) {
199 // Replace code looking like
200 // NOT nota, a
201 // NOT notb, b
202 // AND dst, nota, notb (respectively OR)
203 // with
204 // OR or, a, b (respectively AND)
205 // NOT dest, or
Alexandre Rames9f980252016-02-05 14:00:28 +0000206 HInstruction* src_left = left->InputAt(0);
207 HInstruction* src_right = right->InputAt(0);
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000208 uint32_t dex_pc = op->GetDexPc();
209
210 // Remove the negations on the inputs.
211 left->ReplaceWith(src_left);
212 right->ReplaceWith(src_right);
213 left->GetBlock()->RemoveInstruction(left);
214 right->GetBlock()->RemoveInstruction(right);
215
216 // Replace the `HAnd` or `HOr`.
217 HBinaryOperation* hbin;
218 if (op->IsAnd()) {
219 hbin = new (GetGraph()->GetArena()) HOr(type, src_left, src_right, dex_pc);
220 } else {
221 hbin = new (GetGraph()->GetArena()) HAnd(type, src_left, src_right, dex_pc);
222 }
Alexandre Rames9f980252016-02-05 14:00:28 +0000223 HInstruction* hnot;
224 if (left->IsBooleanNot()) {
225 hnot = new (GetGraph()->GetArena()) HBooleanNot(hbin, dex_pc);
226 } else {
227 hnot = new (GetGraph()->GetArena()) HNot(type, hbin, dex_pc);
228 }
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000229
230 op->GetBlock()->InsertInstructionBefore(hbin, op);
231 op->GetBlock()->ReplaceAndRemoveInstructionWith(op, hnot);
232
233 RecordSimplification();
234 return true;
235 }
236
237 return false;
238}
239
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000240void InstructionSimplifierVisitor::VisitShift(HBinaryOperation* instruction) {
241 DCHECK(instruction->IsShl() || instruction->IsShr() || instruction->IsUShr());
Alexandre Rames50518442016-06-27 11:39:19 +0100242 HInstruction* shift_amount = instruction->GetRight();
243 HInstruction* value = instruction->GetLeft();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000244
Alexandre Rames50518442016-06-27 11:39:19 +0100245 int64_t implicit_mask = (value->GetType() == Primitive::kPrimLong)
246 ? kMaxLongShiftDistance
247 : kMaxIntShiftDistance;
248
249 if (shift_amount->IsConstant()) {
250 int64_t cst = Int64FromConstant(shift_amount->AsConstant());
251 if ((cst & implicit_mask) == 0) {
Mark Mendellba56d062015-05-05 21:34:03 -0400252 // Replace code looking like
Alexandre Rames50518442016-06-27 11:39:19 +0100253 // SHL dst, value, 0
Mark Mendellba56d062015-05-05 21:34:03 -0400254 // with
Alexandre Rames50518442016-06-27 11:39:19 +0100255 // value
256 instruction->ReplaceWith(value);
Mark Mendellba56d062015-05-05 21:34:03 -0400257 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +0100258 RecordSimplification();
Alexandre Rames50518442016-06-27 11:39:19 +0100259 return;
260 }
261 }
262
263 // Shift operations implicitly mask the shift amount according to the type width. Get rid of
264 // unnecessary explicit masking operations on the shift amount.
265 // Replace code looking like
266 // AND masked_shift, shift, <superset of implicit mask>
267 // SHL dst, value, masked_shift
268 // with
269 // SHL dst, value, shift
270 if (shift_amount->IsAnd()) {
271 HAnd* and_insn = shift_amount->AsAnd();
272 HConstant* mask = and_insn->GetConstantRight();
273 if ((mask != nullptr) && ((Int64FromConstant(mask) & implicit_mask) == implicit_mask)) {
274 instruction->ReplaceInput(and_insn->GetLeastConstantLeft(), 1);
275 RecordSimplification();
Mark Mendellba56d062015-05-05 21:34:03 -0400276 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000277 }
278}
279
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000280static bool IsSubRegBitsMinusOther(HSub* sub, size_t reg_bits, HInstruction* other) {
281 return (sub->GetRight() == other &&
282 sub->GetLeft()->IsConstant() &&
283 (Int64FromConstant(sub->GetLeft()->AsConstant()) & (reg_bits - 1)) == 0);
284}
285
286bool InstructionSimplifierVisitor::ReplaceRotateWithRor(HBinaryOperation* op,
287 HUShr* ushr,
288 HShl* shl) {
Roland Levillain22c49222016-03-18 14:04:28 +0000289 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr()) << op->DebugName();
290 HRor* ror = new (GetGraph()->GetArena()) HRor(ushr->GetType(), ushr->GetLeft(), ushr->GetRight());
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000291 op->GetBlock()->ReplaceAndRemoveInstructionWith(op, ror);
292 if (!ushr->HasUses()) {
293 ushr->GetBlock()->RemoveInstruction(ushr);
294 }
295 if (!ushr->GetRight()->HasUses()) {
296 ushr->GetRight()->GetBlock()->RemoveInstruction(ushr->GetRight());
297 }
298 if (!shl->HasUses()) {
299 shl->GetBlock()->RemoveInstruction(shl);
300 }
301 if (!shl->GetRight()->HasUses()) {
302 shl->GetRight()->GetBlock()->RemoveInstruction(shl->GetRight());
303 }
Alexandre Ramesc5809c32016-05-25 15:01:06 +0100304 RecordSimplification();
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000305 return true;
306}
307
308// Try to replace a binary operation flanked by one UShr and one Shl with a bitfield rotation.
309bool InstructionSimplifierVisitor::TryReplaceWithRotate(HBinaryOperation* op) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000310 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
311 HInstruction* left = op->GetLeft();
312 HInstruction* right = op->GetRight();
313 // If we have an UShr and a Shl (in either order).
314 if ((left->IsUShr() && right->IsShl()) || (left->IsShl() && right->IsUShr())) {
315 HUShr* ushr = left->IsUShr() ? left->AsUShr() : right->AsUShr();
316 HShl* shl = left->IsShl() ? left->AsShl() : right->AsShl();
317 DCHECK(Primitive::IsIntOrLongType(ushr->GetType()));
318 if (ushr->GetType() == shl->GetType() &&
319 ushr->GetLeft() == shl->GetLeft()) {
320 if (ushr->GetRight()->IsConstant() && shl->GetRight()->IsConstant()) {
321 // Shift distances are both constant, try replacing with Ror if they
322 // add up to the register size.
323 return TryReplaceWithRotateConstantPattern(op, ushr, shl);
324 } else if (ushr->GetRight()->IsSub() || shl->GetRight()->IsSub()) {
325 // Shift distances are potentially of the form x and (reg_size - x).
326 return TryReplaceWithRotateRegisterSubPattern(op, ushr, shl);
327 } else if (ushr->GetRight()->IsNeg() || shl->GetRight()->IsNeg()) {
328 // Shift distances are potentially of the form d and -d.
329 return TryReplaceWithRotateRegisterNegPattern(op, ushr, shl);
330 }
331 }
332 }
333 return false;
334}
335
336// Try replacing code looking like (x >>> #rdist OP x << #ldist):
337// UShr dst, x, #rdist
338// Shl tmp, x, #ldist
339// OP dst, dst, tmp
340// or like (x >>> #rdist OP x << #-ldist):
341// UShr dst, x, #rdist
342// Shl tmp, x, #-ldist
343// OP dst, dst, tmp
344// with
345// Ror dst, x, #rdist
346bool InstructionSimplifierVisitor::TryReplaceWithRotateConstantPattern(HBinaryOperation* op,
347 HUShr* ushr,
348 HShl* shl) {
349 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
350 size_t reg_bits = Primitive::ComponentSize(ushr->GetType()) * kBitsPerByte;
351 size_t rdist = Int64FromConstant(ushr->GetRight()->AsConstant());
352 size_t ldist = Int64FromConstant(shl->GetRight()->AsConstant());
353 if (((ldist + rdist) & (reg_bits - 1)) == 0) {
354 ReplaceRotateWithRor(op, ushr, shl);
355 return true;
356 }
357 return false;
358}
359
360// Replace code looking like (x >>> -d OP x << d):
361// Neg neg, d
362// UShr dst, x, neg
363// Shl tmp, x, d
364// OP dst, dst, tmp
365// with
366// Neg neg, d
367// Ror dst, x, neg
368// *** OR ***
369// Replace code looking like (x >>> d OP x << -d):
370// UShr dst, x, d
371// Neg neg, d
372// Shl tmp, x, neg
373// OP dst, dst, tmp
374// with
375// Ror dst, x, d
376bool InstructionSimplifierVisitor::TryReplaceWithRotateRegisterNegPattern(HBinaryOperation* op,
377 HUShr* ushr,
378 HShl* shl) {
379 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
380 DCHECK(ushr->GetRight()->IsNeg() || shl->GetRight()->IsNeg());
381 bool neg_is_left = shl->GetRight()->IsNeg();
382 HNeg* neg = neg_is_left ? shl->GetRight()->AsNeg() : ushr->GetRight()->AsNeg();
383 // And the shift distance being negated is the distance being shifted the other way.
384 if (neg->InputAt(0) == (neg_is_left ? ushr->GetRight() : shl->GetRight())) {
385 ReplaceRotateWithRor(op, ushr, shl);
386 }
387 return false;
388}
389
390// Try replacing code looking like (x >>> d OP x << (#bits - d)):
391// UShr dst, x, d
392// Sub ld, #bits, d
393// Shl tmp, x, ld
394// OP dst, dst, tmp
395// with
396// Ror dst, x, d
397// *** OR ***
398// Replace code looking like (x >>> (#bits - d) OP x << d):
399// Sub rd, #bits, d
400// UShr dst, x, rd
401// Shl tmp, x, d
402// OP dst, dst, tmp
403// with
404// Neg neg, d
405// Ror dst, x, neg
406bool InstructionSimplifierVisitor::TryReplaceWithRotateRegisterSubPattern(HBinaryOperation* op,
407 HUShr* ushr,
408 HShl* shl) {
409 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
410 DCHECK(ushr->GetRight()->IsSub() || shl->GetRight()->IsSub());
411 size_t reg_bits = Primitive::ComponentSize(ushr->GetType()) * kBitsPerByte;
412 HInstruction* shl_shift = shl->GetRight();
413 HInstruction* ushr_shift = ushr->GetRight();
414 if ((shl_shift->IsSub() && IsSubRegBitsMinusOther(shl_shift->AsSub(), reg_bits, ushr_shift)) ||
415 (ushr_shift->IsSub() && IsSubRegBitsMinusOther(ushr_shift->AsSub(), reg_bits, shl_shift))) {
416 return ReplaceRotateWithRor(op, ushr, shl);
417 }
418 return false;
419}
420
Calin Juravle10e244f2015-01-26 18:54:32 +0000421void InstructionSimplifierVisitor::VisitNullCheck(HNullCheck* null_check) {
422 HInstruction* obj = null_check->InputAt(0);
423 if (!obj->CanBeNull()) {
424 null_check->ReplaceWith(obj);
425 null_check->GetBlock()->RemoveInstruction(null_check);
Calin Juravleacf735c2015-02-12 15:25:22 +0000426 if (stats_ != nullptr) {
427 stats_->RecordStat(MethodCompilationStat::kRemovedNullCheck);
428 }
429 }
430}
431
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100432bool InstructionSimplifierVisitor::CanEnsureNotNullAt(HInstruction* input, HInstruction* at) const {
433 if (!input->CanBeNull()) {
434 return true;
435 }
436
Vladimir Marko46817b82016-03-29 12:21:58 +0100437 for (const HUseListNode<HInstruction*>& use : input->GetUses()) {
438 HInstruction* user = use.GetUser();
439 if (user->IsNullCheck() && user->StrictlyDominates(at)) {
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +0100440 return true;
441 }
442 }
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100443
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +0100444 return false;
445}
446
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100447// Returns whether doing a type test between the class of `object` against `klass` has
448// a statically known outcome. The result of the test is stored in `outcome`.
449static bool TypeCheckHasKnownOutcome(HLoadClass* klass, HInstruction* object, bool* outcome) {
Calin Juravle2e768302015-07-28 14:41:11 +0000450 DCHECK(!object->IsNullConstant()) << "Null constants should be special cased";
451 ReferenceTypeInfo obj_rti = object->GetReferenceTypeInfo();
452 ScopedObjectAccess soa(Thread::Current());
453 if (!obj_rti.IsValid()) {
454 // We run the simplifier before the reference type propagation so type info might not be
455 // available.
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100456 return false;
Calin Juravleacf735c2015-02-12 15:25:22 +0000457 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100458
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100459 ReferenceTypeInfo class_rti = klass->GetLoadedClassRTI();
Calin Juravle98893e12015-10-02 21:05:03 +0100460 if (!class_rti.IsValid()) {
461 // Happens when the loaded class is unresolved.
462 return false;
463 }
464 DCHECK(class_rti.IsExact());
Calin Juravleacf735c2015-02-12 15:25:22 +0000465 if (class_rti.IsSupertypeOf(obj_rti)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100466 *outcome = true;
467 return true;
468 } else if (obj_rti.IsExact()) {
469 // The test failed at compile time so will also fail at runtime.
470 *outcome = false;
471 return true;
Nicolas Geoffray7cb499b2015-06-17 11:35:11 +0100472 } else if (!class_rti.IsInterface()
473 && !obj_rti.IsInterface()
474 && !obj_rti.IsSupertypeOf(class_rti)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100475 // Different type hierarchy. The test will fail.
476 *outcome = false;
477 return true;
478 }
479 return false;
480}
481
482void InstructionSimplifierVisitor::VisitCheckCast(HCheckCast* check_cast) {
483 HInstruction* object = check_cast->InputAt(0);
Calin Juravlee53fb552015-10-07 17:51:52 +0100484 HLoadClass* load_class = check_cast->InputAt(1)->AsLoadClass();
485 if (load_class->NeedsAccessCheck()) {
486 // If we need to perform an access check we cannot remove the instruction.
487 return;
488 }
489
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100490 if (CanEnsureNotNullAt(object, check_cast)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100491 check_cast->ClearMustDoNullCheck();
492 }
493
494 if (object->IsNullConstant()) {
Calin Juravleacf735c2015-02-12 15:25:22 +0000495 check_cast->GetBlock()->RemoveInstruction(check_cast);
Calin Juravle69158982016-03-16 11:53:41 +0000496 MaybeRecordStat(MethodCompilationStat::kRemovedCheckedCast);
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100497 return;
498 }
499
Vladimir Markoa65ed302016-03-14 21:21:29 +0000500 // Note: The `outcome` is initialized to please valgrind - the compiler can reorder
501 // the return value check with the `outcome` check, b/27651442 .
502 bool outcome = false;
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700503 if (TypeCheckHasKnownOutcome(load_class, object, &outcome)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100504 if (outcome) {
505 check_cast->GetBlock()->RemoveInstruction(check_cast);
Calin Juravle69158982016-03-16 11:53:41 +0000506 MaybeRecordStat(MethodCompilationStat::kRemovedCheckedCast);
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700507 if (!load_class->HasUses()) {
508 // We cannot rely on DCE to remove the class because the `HLoadClass` thinks it can throw.
509 // However, here we know that it cannot because the checkcast was successfull, hence
510 // the class was already loaded.
511 load_class->GetBlock()->RemoveInstruction(load_class);
512 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100513 } else {
514 // Don't do anything for exceptional cases for now. Ideally we should remove
515 // all instructions and blocks this instruction dominates.
516 }
Calin Juravle10e244f2015-01-26 18:54:32 +0000517 }
518}
519
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100520void InstructionSimplifierVisitor::VisitInstanceOf(HInstanceOf* instruction) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100521 HInstruction* object = instruction->InputAt(0);
Calin Juravlee53fb552015-10-07 17:51:52 +0100522 HLoadClass* load_class = instruction->InputAt(1)->AsLoadClass();
523 if (load_class->NeedsAccessCheck()) {
524 // If we need to perform an access check we cannot remove the instruction.
525 return;
526 }
527
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100528 bool can_be_null = true;
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100529 if (CanEnsureNotNullAt(object, instruction)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100530 can_be_null = false;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100531 instruction->ClearMustDoNullCheck();
532 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100533
534 HGraph* graph = GetGraph();
535 if (object->IsNullConstant()) {
Calin Juravle69158982016-03-16 11:53:41 +0000536 MaybeRecordStat(kRemovedInstanceOf);
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100537 instruction->ReplaceWith(graph->GetIntConstant(0));
538 instruction->GetBlock()->RemoveInstruction(instruction);
539 RecordSimplification();
540 return;
541 }
542
Vladimir Marko24bd8952016-03-15 10:40:33 +0000543 // Note: The `outcome` is initialized to please valgrind - the compiler can reorder
544 // the return value check with the `outcome` check, b/27651442 .
545 bool outcome = false;
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700546 if (TypeCheckHasKnownOutcome(load_class, object, &outcome)) {
Calin Juravle69158982016-03-16 11:53:41 +0000547 MaybeRecordStat(kRemovedInstanceOf);
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100548 if (outcome && can_be_null) {
549 // Type test will succeed, we just need a null test.
550 HNotEqual* test = new (graph->GetArena()) HNotEqual(graph->GetNullConstant(), object);
551 instruction->GetBlock()->InsertInstructionBefore(test, instruction);
552 instruction->ReplaceWith(test);
553 } else {
554 // We've statically determined the result of the instanceof.
555 instruction->ReplaceWith(graph->GetIntConstant(outcome));
556 }
557 RecordSimplification();
558 instruction->GetBlock()->RemoveInstruction(instruction);
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700559 if (outcome && !load_class->HasUses()) {
560 // We cannot rely on DCE to remove the class because the `HLoadClass` thinks it can throw.
561 // However, here we know that it cannot because the instanceof check was successfull, hence
562 // the class was already loaded.
563 load_class->GetBlock()->RemoveInstruction(load_class);
564 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100565 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100566}
567
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100568void InstructionSimplifierVisitor::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
569 if ((instruction->GetValue()->GetType() == Primitive::kPrimNot)
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100570 && CanEnsureNotNullAt(instruction->GetValue(), instruction)) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100571 instruction->ClearValueCanBeNull();
572 }
573}
574
575void InstructionSimplifierVisitor::VisitStaticFieldSet(HStaticFieldSet* instruction) {
576 if ((instruction->GetValue()->GetType() == Primitive::kPrimNot)
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100577 && CanEnsureNotNullAt(instruction->GetValue(), instruction)) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100578 instruction->ClearValueCanBeNull();
579 }
580}
581
Anton Shaminbdd79352016-02-15 12:48:36 +0600582static HCondition* GetOppositeConditionSwapOps(ArenaAllocator* arena, HInstruction* cond) {
583 HInstruction *lhs = cond->InputAt(0);
584 HInstruction *rhs = cond->InputAt(1);
585 switch (cond->GetKind()) {
586 case HInstruction::kEqual:
587 return new (arena) HEqual(rhs, lhs);
588 case HInstruction::kNotEqual:
589 return new (arena) HNotEqual(rhs, lhs);
590 case HInstruction::kLessThan:
591 return new (arena) HGreaterThan(rhs, lhs);
592 case HInstruction::kLessThanOrEqual:
593 return new (arena) HGreaterThanOrEqual(rhs, lhs);
594 case HInstruction::kGreaterThan:
595 return new (arena) HLessThan(rhs, lhs);
596 case HInstruction::kGreaterThanOrEqual:
597 return new (arena) HLessThanOrEqual(rhs, lhs);
598 case HInstruction::kBelow:
599 return new (arena) HAbove(rhs, lhs);
600 case HInstruction::kBelowOrEqual:
601 return new (arena) HAboveOrEqual(rhs, lhs);
602 case HInstruction::kAbove:
603 return new (arena) HBelow(rhs, lhs);
604 case HInstruction::kAboveOrEqual:
605 return new (arena) HBelowOrEqual(rhs, lhs);
606 default:
607 LOG(FATAL) << "Unknown ConditionType " << cond->GetKind();
608 }
609 return nullptr;
610}
611
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000612void InstructionSimplifierVisitor::VisitEqual(HEqual* equal) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100613 HInstruction* input_const = equal->GetConstantRight();
614 if (input_const != nullptr) {
615 HInstruction* input_value = equal->GetLeastConstantLeft();
616 if (input_value->GetType() == Primitive::kPrimBoolean && input_const->IsIntConstant()) {
617 HBasicBlock* block = equal->GetBlock();
Nicolas Geoffray3c4ab802015-06-19 11:42:07 +0100618 // We are comparing the boolean to a constant which is of type int and can
619 // be any constant.
Roland Levillain1a653882016-03-18 18:05:57 +0000620 if (input_const->AsIntConstant()->IsTrue()) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100621 // Replace (bool_value == true) with bool_value
622 equal->ReplaceWith(input_value);
623 block->RemoveInstruction(equal);
624 RecordSimplification();
Roland Levillain1a653882016-03-18 18:05:57 +0000625 } else if (input_const->AsIntConstant()->IsFalse()) {
Mark Mendellf6529172015-11-17 11:16:56 -0500626 equal->ReplaceWith(GetGraph()->InsertOppositeCondition(input_value, equal));
627 block->RemoveInstruction(equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100628 RecordSimplification();
David Brazdil1e9ec052015-06-22 10:26:45 +0100629 } else {
630 // Replace (bool_value == integer_not_zero_nor_one_constant) with false
631 equal->ReplaceWith(GetGraph()->GetIntConstant(0));
632 block->RemoveInstruction(equal);
633 RecordSimplification();
David Brazdil0d13fee2015-04-17 14:52:19 +0100634 }
Mark Mendellc4701932015-04-10 13:18:51 -0400635 } else {
636 VisitCondition(equal);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100637 }
Mark Mendellc4701932015-04-10 13:18:51 -0400638 } else {
639 VisitCondition(equal);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100640 }
641}
642
David Brazdil0d13fee2015-04-17 14:52:19 +0100643void InstructionSimplifierVisitor::VisitNotEqual(HNotEqual* not_equal) {
644 HInstruction* input_const = not_equal->GetConstantRight();
645 if (input_const != nullptr) {
646 HInstruction* input_value = not_equal->GetLeastConstantLeft();
647 if (input_value->GetType() == Primitive::kPrimBoolean && input_const->IsIntConstant()) {
648 HBasicBlock* block = not_equal->GetBlock();
Nicolas Geoffray3c4ab802015-06-19 11:42:07 +0100649 // We are comparing the boolean to a constant which is of type int and can
650 // be any constant.
Roland Levillain1a653882016-03-18 18:05:57 +0000651 if (input_const->AsIntConstant()->IsTrue()) {
Mark Mendellf6529172015-11-17 11:16:56 -0500652 not_equal->ReplaceWith(GetGraph()->InsertOppositeCondition(input_value, not_equal));
653 block->RemoveInstruction(not_equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100654 RecordSimplification();
Roland Levillain1a653882016-03-18 18:05:57 +0000655 } else if (input_const->AsIntConstant()->IsFalse()) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100656 // Replace (bool_value != false) with bool_value
David Brazdil0d13fee2015-04-17 14:52:19 +0100657 not_equal->ReplaceWith(input_value);
658 block->RemoveInstruction(not_equal);
659 RecordSimplification();
David Brazdil1e9ec052015-06-22 10:26:45 +0100660 } else {
661 // Replace (bool_value != integer_not_zero_nor_one_constant) with true
662 not_equal->ReplaceWith(GetGraph()->GetIntConstant(1));
663 block->RemoveInstruction(not_equal);
664 RecordSimplification();
David Brazdil0d13fee2015-04-17 14:52:19 +0100665 }
Mark Mendellc4701932015-04-10 13:18:51 -0400666 } else {
667 VisitCondition(not_equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100668 }
Mark Mendellc4701932015-04-10 13:18:51 -0400669 } else {
670 VisitCondition(not_equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100671 }
672}
673
674void InstructionSimplifierVisitor::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000675 HInstruction* input = bool_not->InputAt(0);
676 HInstruction* replace_with = nullptr;
677
678 if (input->IsIntConstant()) {
679 // Replace !(true/false) with false/true.
Roland Levillain1a653882016-03-18 18:05:57 +0000680 if (input->AsIntConstant()->IsTrue()) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000681 replace_with = GetGraph()->GetIntConstant(0);
682 } else {
Roland Levillain1a653882016-03-18 18:05:57 +0000683 DCHECK(input->AsIntConstant()->IsFalse()) << input->AsIntConstant()->GetValue();
David Brazdil74eb1b22015-12-14 11:44:01 +0000684 replace_with = GetGraph()->GetIntConstant(1);
685 }
686 } else if (input->IsBooleanNot()) {
687 // Replace (!(!bool_value)) with bool_value.
688 replace_with = input->InputAt(0);
689 } else if (input->IsCondition() &&
690 // Don't change FP compares. The definition of compares involving
691 // NaNs forces the compares to be done as written by the user.
692 !Primitive::IsFloatingPointType(input->InputAt(0)->GetType())) {
693 // Replace condition with its opposite.
694 replace_with = GetGraph()->InsertOppositeCondition(input->AsCondition(), bool_not);
695 }
696
697 if (replace_with != nullptr) {
698 bool_not->ReplaceWith(replace_with);
David Brazdil0d13fee2015-04-17 14:52:19 +0100699 bool_not->GetBlock()->RemoveInstruction(bool_not);
David Brazdil74eb1b22015-12-14 11:44:01 +0000700 RecordSimplification();
701 }
702}
703
704void InstructionSimplifierVisitor::VisitSelect(HSelect* select) {
705 HInstruction* replace_with = nullptr;
706 HInstruction* condition = select->GetCondition();
707 HInstruction* true_value = select->GetTrueValue();
708 HInstruction* false_value = select->GetFalseValue();
709
710 if (condition->IsBooleanNot()) {
711 // Change ((!cond) ? x : y) to (cond ? y : x).
712 condition = condition->InputAt(0);
713 std::swap(true_value, false_value);
714 select->ReplaceInput(false_value, 0);
715 select->ReplaceInput(true_value, 1);
716 select->ReplaceInput(condition, 2);
717 RecordSimplification();
718 }
719
720 if (true_value == false_value) {
721 // Replace (cond ? x : x) with (x).
722 replace_with = true_value;
723 } else if (condition->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +0000724 if (condition->AsIntConstant()->IsTrue()) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000725 // Replace (true ? x : y) with (x).
726 replace_with = true_value;
727 } else {
728 // Replace (false ? x : y) with (y).
Roland Levillain1a653882016-03-18 18:05:57 +0000729 DCHECK(condition->AsIntConstant()->IsFalse()) << condition->AsIntConstant()->GetValue();
David Brazdil74eb1b22015-12-14 11:44:01 +0000730 replace_with = false_value;
731 }
732 } else if (true_value->IsIntConstant() && false_value->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +0000733 if (true_value->AsIntConstant()->IsTrue() && false_value->AsIntConstant()->IsFalse()) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000734 // Replace (cond ? true : false) with (cond).
735 replace_with = condition;
Roland Levillain1a653882016-03-18 18:05:57 +0000736 } else if (true_value->AsIntConstant()->IsFalse() && false_value->AsIntConstant()->IsTrue()) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000737 // Replace (cond ? false : true) with (!cond).
738 replace_with = GetGraph()->InsertOppositeCondition(condition, select);
739 }
740 }
741
742 if (replace_with != nullptr) {
743 select->ReplaceWith(replace_with);
744 select->GetBlock()->RemoveInstruction(select);
745 RecordSimplification();
746 }
747}
748
749void InstructionSimplifierVisitor::VisitIf(HIf* instruction) {
750 HInstruction* condition = instruction->InputAt(0);
751 if (condition->IsBooleanNot()) {
752 // Swap successors if input is negated.
753 instruction->ReplaceInput(condition->InputAt(0), 0);
754 instruction->GetBlock()->SwapSuccessors();
David Brazdil0d13fee2015-04-17 14:52:19 +0100755 RecordSimplification();
756 }
757}
758
Mingyao Yang0304e182015-01-30 16:41:29 -0800759void InstructionSimplifierVisitor::VisitArrayLength(HArrayLength* instruction) {
760 HInstruction* input = instruction->InputAt(0);
761 // If the array is a NewArray with constant size, replace the array length
762 // with the constant instruction. This helps the bounds check elimination phase.
763 if (input->IsNewArray()) {
764 input = input->InputAt(0);
765 if (input->IsIntConstant()) {
766 instruction->ReplaceWith(input);
767 }
768 }
769}
770
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000771void InstructionSimplifierVisitor::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000772 HInstruction* value = instruction->GetValue();
773 if (value->GetType() != Primitive::kPrimNot) return;
774
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100775 if (CanEnsureNotNullAt(value, instruction)) {
776 instruction->ClearValueCanBeNull();
777 }
778
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000779 if (value->IsArrayGet()) {
780 if (value->AsArrayGet()->GetArray() == instruction->GetArray()) {
781 // If the code is just swapping elements in the array, no need for a type check.
782 instruction->ClearNeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100783 return;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000784 }
785 }
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100786
Nicolas Geoffray9fdb31e2015-07-01 12:56:46 +0100787 if (value->IsNullConstant()) {
788 instruction->ClearNeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100789 return;
Nicolas Geoffray9fdb31e2015-07-01 12:56:46 +0100790 }
791
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100792 ScopedObjectAccess soa(Thread::Current());
793 ReferenceTypeInfo array_rti = instruction->GetArray()->GetReferenceTypeInfo();
794 ReferenceTypeInfo value_rti = value->GetReferenceTypeInfo();
795 if (!array_rti.IsValid()) {
796 return;
797 }
798
799 if (value_rti.IsValid() && array_rti.CanArrayHold(value_rti)) {
800 instruction->ClearNeedsTypeCheck();
801 return;
802 }
803
804 if (array_rti.IsObjectArray()) {
805 if (array_rti.IsExact()) {
806 instruction->ClearNeedsTypeCheck();
807 return;
808 }
809 instruction->SetStaticTypeOfArrayIsObjectArray();
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100810 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000811}
812
Vladimir Markob52bbde2016-02-12 12:06:05 +0000813static bool IsTypeConversionImplicit(Primitive::Type input_type, Primitive::Type result_type) {
Roland Levillainf355c3f2016-03-30 19:09:03 +0100814 // Invariant: We should never generate a conversion to a Boolean value.
815 DCHECK_NE(Primitive::kPrimBoolean, result_type);
816
Vladimir Markob52bbde2016-02-12 12:06:05 +0000817 // Besides conversion to the same type, widening integral conversions are implicit,
818 // excluding conversions to long and the byte->char conversion where we need to
819 // clear the high 16 bits of the 32-bit sign-extended representation of byte.
820 return result_type == input_type ||
Roland Levillainf355c3f2016-03-30 19:09:03 +0100821 (result_type == Primitive::kPrimInt && (input_type == Primitive::kPrimBoolean ||
822 input_type == Primitive::kPrimByte ||
823 input_type == Primitive::kPrimShort ||
824 input_type == Primitive::kPrimChar)) ||
825 (result_type == Primitive::kPrimChar && input_type == Primitive::kPrimBoolean) ||
826 (result_type == Primitive::kPrimShort && (input_type == Primitive::kPrimBoolean ||
827 input_type == Primitive::kPrimByte)) ||
828 (result_type == Primitive::kPrimByte && input_type == Primitive::kPrimBoolean);
Vladimir Markob52bbde2016-02-12 12:06:05 +0000829}
830
831static bool IsTypeConversionLossless(Primitive::Type input_type, Primitive::Type result_type) {
832 // The conversion to a larger type is loss-less with the exception of two cases,
833 // - conversion to char, the only unsigned type, where we may lose some bits, and
834 // - conversion from float to long, the only FP to integral conversion with smaller FP type.
835 // For integral to FP conversions this holds because the FP mantissa is large enough.
836 DCHECK_NE(input_type, result_type);
837 return Primitive::ComponentSize(result_type) > Primitive::ComponentSize(input_type) &&
838 result_type != Primitive::kPrimChar &&
839 !(result_type == Primitive::kPrimLong && input_type == Primitive::kPrimFloat);
840}
841
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +0000842void InstructionSimplifierVisitor::VisitTypeConversion(HTypeConversion* instruction) {
Vladimir Markob52bbde2016-02-12 12:06:05 +0000843 HInstruction* input = instruction->GetInput();
844 Primitive::Type input_type = input->GetType();
845 Primitive::Type result_type = instruction->GetResultType();
846 if (IsTypeConversionImplicit(input_type, result_type)) {
847 // Remove the implicit conversion; this includes conversion to the same type.
848 instruction->ReplaceWith(input);
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +0000849 instruction->GetBlock()->RemoveInstruction(instruction);
Vladimir Markob52bbde2016-02-12 12:06:05 +0000850 RecordSimplification();
851 return;
852 }
853
854 if (input->IsTypeConversion()) {
855 HTypeConversion* input_conversion = input->AsTypeConversion();
856 HInstruction* original_input = input_conversion->GetInput();
857 Primitive::Type original_type = original_input->GetType();
858
859 // When the first conversion is lossless, a direct conversion from the original type
860 // to the final type yields the same result, even for a lossy second conversion, for
861 // example float->double->int or int->double->float.
862 bool is_first_conversion_lossless = IsTypeConversionLossless(original_type, input_type);
863
864 // For integral conversions, see if the first conversion loses only bits that the second
865 // doesn't need, i.e. the final type is no wider than the intermediate. If so, direct
866 // conversion yields the same result, for example long->int->short or int->char->short.
867 bool integral_conversions_with_non_widening_second =
868 Primitive::IsIntegralType(input_type) &&
869 Primitive::IsIntegralType(original_type) &&
870 Primitive::IsIntegralType(result_type) &&
871 Primitive::ComponentSize(result_type) <= Primitive::ComponentSize(input_type);
872
873 if (is_first_conversion_lossless || integral_conversions_with_non_widening_second) {
874 // If the merged conversion is implicit, do the simplification unconditionally.
875 if (IsTypeConversionImplicit(original_type, result_type)) {
876 instruction->ReplaceWith(original_input);
877 instruction->GetBlock()->RemoveInstruction(instruction);
878 if (!input_conversion->HasUses()) {
879 // Don't wait for DCE.
880 input_conversion->GetBlock()->RemoveInstruction(input_conversion);
881 }
882 RecordSimplification();
883 return;
884 }
885 // Otherwise simplify only if the first conversion has no other use.
886 if (input_conversion->HasOnlyOneNonEnvironmentUse()) {
887 input_conversion->ReplaceWith(original_input);
888 input_conversion->GetBlock()->RemoveInstruction(input_conversion);
889 RecordSimplification();
890 return;
891 }
892 }
Vladimir Marko625090f2016-03-14 18:00:05 +0000893 } else if (input->IsAnd() && Primitive::IsIntegralType(result_type)) {
Vladimir Marko8428bd32016-02-12 16:53:57 +0000894 DCHECK(Primitive::IsIntegralType(input_type));
895 HAnd* input_and = input->AsAnd();
896 HConstant* constant = input_and->GetConstantRight();
897 if (constant != nullptr) {
898 int64_t value = Int64FromConstant(constant);
899 DCHECK_NE(value, -1); // "& -1" would have been optimized away in VisitAnd().
900 size_t trailing_ones = CTZ(~static_cast<uint64_t>(value));
901 if (trailing_ones >= kBitsPerByte * Primitive::ComponentSize(result_type)) {
902 // The `HAnd` is useless, for example in `(byte) (x & 0xff)`, get rid of it.
Vladimir Marko625090f2016-03-14 18:00:05 +0000903 HInstruction* original_input = input_and->GetLeastConstantLeft();
904 if (IsTypeConversionImplicit(original_input->GetType(), result_type)) {
905 instruction->ReplaceWith(original_input);
906 instruction->GetBlock()->RemoveInstruction(instruction);
907 RecordSimplification();
908 return;
909 } else if (input->HasOnlyOneNonEnvironmentUse()) {
910 input_and->ReplaceWith(original_input);
911 input_and->GetBlock()->RemoveInstruction(input_and);
912 RecordSimplification();
913 return;
914 }
Vladimir Marko8428bd32016-02-12 16:53:57 +0000915 }
916 }
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +0000917 }
918}
919
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000920void InstructionSimplifierVisitor::VisitAdd(HAdd* instruction) {
921 HConstant* input_cst = instruction->GetConstantRight();
922 HInstruction* input_other = instruction->GetLeastConstantLeft();
Maxim Kazantsevd3278bd2016-07-12 15:55:33 +0600923 bool integral_type = Primitive::IsIntegralType(instruction->GetType());
Roland Levillain1a653882016-03-18 18:05:57 +0000924 if ((input_cst != nullptr) && input_cst->IsArithmeticZero()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000925 // Replace code looking like
926 // ADD dst, src, 0
927 // with
928 // src
Serguei Katkov115b53f2015-08-05 17:03:30 +0600929 // Note that we cannot optimize `x + 0.0` to `x` for floating-point. When
930 // `x` is `-0.0`, the former expression yields `0.0`, while the later
931 // yields `-0.0`.
Maxim Kazantsevd3278bd2016-07-12 15:55:33 +0600932 if (integral_type) {
Serguei Katkov115b53f2015-08-05 17:03:30 +0600933 instruction->ReplaceWith(input_other);
934 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +0100935 RecordSimplification();
Serguei Katkov115b53f2015-08-05 17:03:30 +0600936 return;
937 }
Alexandre Rames188d4312015-04-09 18:30:21 +0100938 }
939
940 HInstruction* left = instruction->GetLeft();
941 HInstruction* right = instruction->GetRight();
942 bool left_is_neg = left->IsNeg();
943 bool right_is_neg = right->IsNeg();
944
945 if (left_is_neg && right_is_neg) {
946 if (TryMoveNegOnInputsAfterBinop(instruction)) {
947 return;
948 }
949 }
950
951 HNeg* neg = left_is_neg ? left->AsNeg() : right->AsNeg();
952 if ((left_is_neg ^ right_is_neg) && neg->HasOnlyOneNonEnvironmentUse()) {
953 // Replace code looking like
954 // NEG tmp, b
955 // ADD dst, a, tmp
956 // with
957 // SUB dst, a, b
958 // We do not perform the optimization if the input negation has environment
959 // uses or multiple non-environment uses as it could lead to worse code. In
960 // particular, we do not want the live range of `b` to be extended if we are
961 // not sure the initial 'NEG' instruction can be removed.
962 HInstruction* other = left_is_neg ? right : left;
963 HSub* sub = new(GetGraph()->GetArena()) HSub(instruction->GetType(), other, neg->GetInput());
964 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, sub);
965 RecordSimplification();
966 neg->GetBlock()->RemoveInstruction(neg);
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000967 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000968 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000969
Anton Kirilove14dc862016-05-13 17:56:15 +0100970 if (TryReplaceWithRotate(instruction)) {
971 return;
972 }
973
974 // TryHandleAssociativeAndCommutativeOperation() does not remove its input,
975 // so no need to return.
976 TryHandleAssociativeAndCommutativeOperation(instruction);
977
Maxim Kazantsevd3278bd2016-07-12 15:55:33 +0600978 if ((left->IsSub() || right->IsSub()) &&
Anton Kirilove14dc862016-05-13 17:56:15 +0100979 TrySubtractionChainSimplification(instruction)) {
980 return;
981 }
Maxim Kazantsevd3278bd2016-07-12 15:55:33 +0600982
983 if (integral_type) {
984 // Replace code patterns looking like
985 // SUB dst1, x, y SUB dst1, x, y
986 // ADD dst2, dst1, y ADD dst2, y, dst1
987 // with
988 // SUB dst1, x, y
989 // ADD instruction is not needed in this case, we may use
990 // one of inputs of SUB instead.
991 if (left->IsSub() && left->InputAt(1) == right) {
992 instruction->ReplaceWith(left->InputAt(0));
993 RecordSimplification();
994 instruction->GetBlock()->RemoveInstruction(instruction);
995 return;
996 } else if (right->IsSub() && right->InputAt(1) == left) {
997 instruction->ReplaceWith(right->InputAt(0));
998 RecordSimplification();
999 instruction->GetBlock()->RemoveInstruction(instruction);
1000 return;
1001 }
1002 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001003}
1004
1005void InstructionSimplifierVisitor::VisitAnd(HAnd* instruction) {
1006 HConstant* input_cst = instruction->GetConstantRight();
1007 HInstruction* input_other = instruction->GetLeastConstantLeft();
1008
Vladimir Marko452c1b62015-09-25 14:44:17 +01001009 if (input_cst != nullptr) {
1010 int64_t value = Int64FromConstant(input_cst);
1011 if (value == -1) {
1012 // Replace code looking like
1013 // AND dst, src, 0xFFF...FF
1014 // with
1015 // src
1016 instruction->ReplaceWith(input_other);
1017 instruction->GetBlock()->RemoveInstruction(instruction);
1018 RecordSimplification();
1019 return;
1020 }
1021 // Eliminate And from UShr+And if the And-mask contains all the bits that
1022 // can be non-zero after UShr. Transform Shr+And to UShr if the And-mask
1023 // precisely clears the shifted-in sign bits.
1024 if ((input_other->IsUShr() || input_other->IsShr()) && input_other->InputAt(1)->IsConstant()) {
1025 size_t reg_bits = (instruction->GetResultType() == Primitive::kPrimLong) ? 64 : 32;
1026 size_t shift = Int64FromConstant(input_other->InputAt(1)->AsConstant()) & (reg_bits - 1);
1027 size_t num_tail_bits_set = CTZ(value + 1);
1028 if ((num_tail_bits_set >= reg_bits - shift) && input_other->IsUShr()) {
1029 // This AND clears only bits known to be clear, for example "(x >>> 24) & 0xff".
1030 instruction->ReplaceWith(input_other);
1031 instruction->GetBlock()->RemoveInstruction(instruction);
1032 RecordSimplification();
1033 return;
1034 } else if ((num_tail_bits_set == reg_bits - shift) && IsPowerOfTwo(value + 1) &&
1035 input_other->HasOnlyOneNonEnvironmentUse()) {
1036 DCHECK(input_other->IsShr()); // For UShr, we would have taken the branch above.
1037 // Replace SHR+AND with USHR, for example "(x >> 24) & 0xff" -> "x >>> 24".
1038 HUShr* ushr = new (GetGraph()->GetArena()) HUShr(instruction->GetType(),
1039 input_other->InputAt(0),
1040 input_other->InputAt(1),
1041 input_other->GetDexPc());
1042 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, ushr);
1043 input_other->GetBlock()->RemoveInstruction(input_other);
1044 RecordSimplification();
1045 return;
1046 }
1047 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001048 }
1049
1050 // We assume that GVN has run before, so we only perform a pointer comparison.
1051 // If for some reason the values are equal but the pointers are different, we
Alexandre Rames188d4312015-04-09 18:30:21 +01001052 // are still correct and only miss an optimization opportunity.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001053 if (instruction->GetLeft() == instruction->GetRight()) {
1054 // Replace code looking like
1055 // AND dst, src, src
1056 // with
1057 // src
1058 instruction->ReplaceWith(instruction->GetLeft());
1059 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001060 RecordSimplification();
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001061 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001062 }
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001063
Anton Kirilove14dc862016-05-13 17:56:15 +01001064 if (TryDeMorganNegationFactoring(instruction)) {
1065 return;
1066 }
1067
1068 // TryHandleAssociativeAndCommutativeOperation() does not remove its input,
1069 // so no need to return.
1070 TryHandleAssociativeAndCommutativeOperation(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001071}
1072
Mark Mendellc4701932015-04-10 13:18:51 -04001073void InstructionSimplifierVisitor::VisitGreaterThan(HGreaterThan* condition) {
1074 VisitCondition(condition);
1075}
1076
1077void InstructionSimplifierVisitor::VisitGreaterThanOrEqual(HGreaterThanOrEqual* condition) {
1078 VisitCondition(condition);
1079}
1080
1081void InstructionSimplifierVisitor::VisitLessThan(HLessThan* condition) {
1082 VisitCondition(condition);
1083}
1084
1085void InstructionSimplifierVisitor::VisitLessThanOrEqual(HLessThanOrEqual* condition) {
1086 VisitCondition(condition);
1087}
1088
Anton Shaminbdd79352016-02-15 12:48:36 +06001089void InstructionSimplifierVisitor::VisitBelow(HBelow* condition) {
1090 VisitCondition(condition);
1091}
1092
1093void InstructionSimplifierVisitor::VisitBelowOrEqual(HBelowOrEqual* condition) {
1094 VisitCondition(condition);
1095}
1096
1097void InstructionSimplifierVisitor::VisitAbove(HAbove* condition) {
1098 VisitCondition(condition);
1099}
1100
1101void InstructionSimplifierVisitor::VisitAboveOrEqual(HAboveOrEqual* condition) {
1102 VisitCondition(condition);
1103}
Aart Bike9f37602015-10-09 11:15:55 -07001104
Mark Mendellc4701932015-04-10 13:18:51 -04001105void InstructionSimplifierVisitor::VisitCondition(HCondition* condition) {
Anton Shaminbdd79352016-02-15 12:48:36 +06001106 // Reverse condition if left is constant. Our code generators prefer constant
1107 // on the right hand side.
1108 if (condition->GetLeft()->IsConstant() && !condition->GetRight()->IsConstant()) {
1109 HBasicBlock* block = condition->GetBlock();
1110 HCondition* replacement = GetOppositeConditionSwapOps(block->GetGraph()->GetArena(), condition);
1111 // If it is a fp we must set the opposite bias.
1112 if (replacement != nullptr) {
1113 if (condition->IsLtBias()) {
1114 replacement->SetBias(ComparisonBias::kGtBias);
1115 } else if (condition->IsGtBias()) {
1116 replacement->SetBias(ComparisonBias::kLtBias);
1117 }
1118 block->ReplaceAndRemoveInstructionWith(condition, replacement);
1119 RecordSimplification();
1120
1121 condition = replacement;
1122 }
1123 }
Mark Mendellc4701932015-04-10 13:18:51 -04001124
Mark Mendellc4701932015-04-10 13:18:51 -04001125 HInstruction* left = condition->GetLeft();
1126 HInstruction* right = condition->GetRight();
Anton Shaminbdd79352016-02-15 12:48:36 +06001127
1128 // Try to fold an HCompare into this HCondition.
1129
Mark Mendellc4701932015-04-10 13:18:51 -04001130 // We can only replace an HCondition which compares a Compare to 0.
1131 // Both 'dx' and 'jack' generate a compare to 0 when compiling a
1132 // condition with a long, float or double comparison as input.
1133 if (!left->IsCompare() || !right->IsConstant() || right->AsIntConstant()->GetValue() != 0) {
1134 // Conversion is not possible.
1135 return;
1136 }
1137
1138 // Is the Compare only used for this purpose?
Vladimir Marko46817b82016-03-29 12:21:58 +01001139 if (!left->GetUses().HasExactlyOneElement()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001140 // Someone else also wants the result of the compare.
1141 return;
1142 }
1143
Vladimir Marko46817b82016-03-29 12:21:58 +01001144 if (!left->GetEnvUses().empty()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001145 // There is a reference to the compare result in an environment. Do we really need it?
1146 if (GetGraph()->IsDebuggable()) {
1147 return;
1148 }
1149
1150 // We have to ensure that there are no deopt points in the sequence.
1151 if (left->HasAnyEnvironmentUseBefore(condition)) {
1152 return;
1153 }
1154 }
1155
1156 // Clean up any environment uses from the HCompare, if any.
1157 left->RemoveEnvironmentUsers();
1158
1159 // We have decided to fold the HCompare into the HCondition. Transfer the information.
1160 condition->SetBias(left->AsCompare()->GetBias());
1161
1162 // Replace the operands of the HCondition.
1163 condition->ReplaceInput(left->InputAt(0), 0);
1164 condition->ReplaceInput(left->InputAt(1), 1);
1165
1166 // Remove the HCompare.
1167 left->GetBlock()->RemoveInstruction(left);
1168
1169 RecordSimplification();
1170}
1171
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001172void InstructionSimplifierVisitor::VisitDiv(HDiv* instruction) {
1173 HConstant* input_cst = instruction->GetConstantRight();
1174 HInstruction* input_other = instruction->GetLeastConstantLeft();
1175 Primitive::Type type = instruction->GetType();
1176
1177 if ((input_cst != nullptr) && input_cst->IsOne()) {
1178 // Replace code looking like
1179 // DIV dst, src, 1
1180 // with
1181 // src
1182 instruction->ReplaceWith(input_other);
1183 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001184 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001185 return;
1186 }
1187
Nicolas Geoffray0d221842015-04-27 08:53:46 +00001188 if ((input_cst != nullptr) && input_cst->IsMinusOne()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001189 // Replace code looking like
1190 // DIV dst, src, -1
1191 // with
1192 // NEG dst, src
1193 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(
Nicolas Geoffray0d221842015-04-27 08:53:46 +00001194 instruction, new (GetGraph()->GetArena()) HNeg(type, input_other));
Alexandre Rames188d4312015-04-09 18:30:21 +01001195 RecordSimplification();
Nicolas Geoffray0d221842015-04-27 08:53:46 +00001196 return;
1197 }
1198
1199 if ((input_cst != nullptr) && Primitive::IsFloatingPointType(type)) {
1200 // Try replacing code looking like
1201 // DIV dst, src, constant
1202 // with
1203 // MUL dst, src, 1 / constant
1204 HConstant* reciprocal = nullptr;
1205 if (type == Primitive::Primitive::kPrimDouble) {
1206 double value = input_cst->AsDoubleConstant()->GetValue();
1207 if (CanDivideByReciprocalMultiplyDouble(bit_cast<int64_t, double>(value))) {
1208 reciprocal = GetGraph()->GetDoubleConstant(1.0 / value);
1209 }
1210 } else {
1211 DCHECK_EQ(type, Primitive::kPrimFloat);
1212 float value = input_cst->AsFloatConstant()->GetValue();
1213 if (CanDivideByReciprocalMultiplyFloat(bit_cast<int32_t, float>(value))) {
1214 reciprocal = GetGraph()->GetFloatConstant(1.0f / value);
1215 }
1216 }
1217
1218 if (reciprocal != nullptr) {
1219 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(
1220 instruction, new (GetGraph()->GetArena()) HMul(type, input_other, reciprocal));
1221 RecordSimplification();
1222 return;
1223 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001224 }
1225}
1226
1227void InstructionSimplifierVisitor::VisitMul(HMul* instruction) {
1228 HConstant* input_cst = instruction->GetConstantRight();
1229 HInstruction* input_other = instruction->GetLeastConstantLeft();
1230 Primitive::Type type = instruction->GetType();
1231 HBasicBlock* block = instruction->GetBlock();
1232 ArenaAllocator* allocator = GetGraph()->GetArena();
1233
1234 if (input_cst == nullptr) {
1235 return;
1236 }
1237
1238 if (input_cst->IsOne()) {
1239 // Replace code looking like
1240 // MUL dst, src, 1
1241 // with
1242 // src
1243 instruction->ReplaceWith(input_other);
1244 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001245 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001246 return;
1247 }
1248
1249 if (input_cst->IsMinusOne() &&
1250 (Primitive::IsFloatingPointType(type) || Primitive::IsIntOrLongType(type))) {
1251 // Replace code looking like
1252 // MUL dst, src, -1
1253 // with
1254 // NEG dst, src
1255 HNeg* neg = new (allocator) HNeg(type, input_other);
1256 block->ReplaceAndRemoveInstructionWith(instruction, neg);
Alexandre Rames188d4312015-04-09 18:30:21 +01001257 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001258 return;
1259 }
1260
1261 if (Primitive::IsFloatingPointType(type) &&
1262 ((input_cst->IsFloatConstant() && input_cst->AsFloatConstant()->GetValue() == 2.0f) ||
1263 (input_cst->IsDoubleConstant() && input_cst->AsDoubleConstant()->GetValue() == 2.0))) {
1264 // Replace code looking like
1265 // FP_MUL dst, src, 2.0
1266 // with
1267 // FP_ADD dst, src, src
1268 // The 'int' and 'long' cases are handled below.
1269 block->ReplaceAndRemoveInstructionWith(instruction,
1270 new (allocator) HAdd(type, input_other, input_other));
Alexandre Rames188d4312015-04-09 18:30:21 +01001271 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001272 return;
1273 }
1274
1275 if (Primitive::IsIntOrLongType(type)) {
1276 int64_t factor = Int64FromConstant(input_cst);
Serguei Katkov53849192015-04-20 14:22:27 +06001277 // Even though constant propagation also takes care of the zero case, other
1278 // optimizations can lead to having a zero multiplication.
1279 if (factor == 0) {
1280 // Replace code looking like
1281 // MUL dst, src, 0
1282 // with
1283 // 0
1284 instruction->ReplaceWith(input_cst);
1285 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001286 RecordSimplification();
Anton Kirilove14dc862016-05-13 17:56:15 +01001287 return;
Serguei Katkov53849192015-04-20 14:22:27 +06001288 } else if (IsPowerOfTwo(factor)) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001289 // Replace code looking like
1290 // MUL dst, src, pow_of_2
1291 // with
1292 // SHL dst, src, log2(pow_of_2)
David Brazdil8d5b8b22015-03-24 10:51:52 +00001293 HIntConstant* shift = GetGraph()->GetIntConstant(WhichPowerOf2(factor));
Roland Levillain22c49222016-03-18 14:04:28 +00001294 HShl* shl = new (allocator) HShl(type, input_other, shift);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001295 block->ReplaceAndRemoveInstructionWith(instruction, shl);
Alexandre Rames188d4312015-04-09 18:30:21 +01001296 RecordSimplification();
Anton Kirilove14dc862016-05-13 17:56:15 +01001297 return;
Alexandre Rames38db7852015-11-20 15:02:45 +00001298 } else if (IsPowerOfTwo(factor - 1)) {
1299 // Transform code looking like
1300 // MUL dst, src, (2^n + 1)
1301 // into
1302 // SHL tmp, src, n
1303 // ADD dst, src, tmp
1304 HShl* shl = new (allocator) HShl(type,
1305 input_other,
1306 GetGraph()->GetIntConstant(WhichPowerOf2(factor - 1)));
1307 HAdd* add = new (allocator) HAdd(type, input_other, shl);
1308
1309 block->InsertInstructionBefore(shl, instruction);
1310 block->ReplaceAndRemoveInstructionWith(instruction, add);
1311 RecordSimplification();
Anton Kirilove14dc862016-05-13 17:56:15 +01001312 return;
Alexandre Rames38db7852015-11-20 15:02:45 +00001313 } else if (IsPowerOfTwo(factor + 1)) {
1314 // Transform code looking like
1315 // MUL dst, src, (2^n - 1)
1316 // into
1317 // SHL tmp, src, n
1318 // SUB dst, tmp, src
1319 HShl* shl = new (allocator) HShl(type,
1320 input_other,
1321 GetGraph()->GetIntConstant(WhichPowerOf2(factor + 1)));
1322 HSub* sub = new (allocator) HSub(type, shl, input_other);
1323
1324 block->InsertInstructionBefore(shl, instruction);
1325 block->ReplaceAndRemoveInstructionWith(instruction, sub);
1326 RecordSimplification();
Anton Kirilove14dc862016-05-13 17:56:15 +01001327 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001328 }
1329 }
Anton Kirilove14dc862016-05-13 17:56:15 +01001330
1331 // TryHandleAssociativeAndCommutativeOperation() does not remove its input,
1332 // so no need to return.
1333 TryHandleAssociativeAndCommutativeOperation(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001334}
1335
Alexandre Rames188d4312015-04-09 18:30:21 +01001336void InstructionSimplifierVisitor::VisitNeg(HNeg* instruction) {
1337 HInstruction* input = instruction->GetInput();
1338 if (input->IsNeg()) {
1339 // Replace code looking like
1340 // NEG tmp, src
1341 // NEG dst, tmp
1342 // with
1343 // src
1344 HNeg* previous_neg = input->AsNeg();
1345 instruction->ReplaceWith(previous_neg->GetInput());
1346 instruction->GetBlock()->RemoveInstruction(instruction);
1347 // We perform the optimization even if the input negation has environment
1348 // uses since it allows removing the current instruction. But we only delete
1349 // the input negation only if it is does not have any uses left.
1350 if (!previous_neg->HasUses()) {
1351 previous_neg->GetBlock()->RemoveInstruction(previous_neg);
1352 }
1353 RecordSimplification();
1354 return;
1355 }
1356
Serguei Katkov339dfc22015-04-20 12:29:32 +06001357 if (input->IsSub() && input->HasOnlyOneNonEnvironmentUse() &&
1358 !Primitive::IsFloatingPointType(input->GetType())) {
Alexandre Rames188d4312015-04-09 18:30:21 +01001359 // Replace code looking like
1360 // SUB tmp, a, b
1361 // NEG dst, tmp
1362 // with
1363 // SUB dst, b, a
1364 // We do not perform the optimization if the input subtraction has
1365 // environment uses or multiple non-environment uses as it could lead to
1366 // worse code. In particular, we do not want the live ranges of `a` and `b`
1367 // to be extended if we are not sure the initial 'SUB' instruction can be
1368 // removed.
Serguei Katkov339dfc22015-04-20 12:29:32 +06001369 // We do not perform optimization for fp because we could lose the sign of zero.
Alexandre Rames188d4312015-04-09 18:30:21 +01001370 HSub* sub = input->AsSub();
1371 HSub* new_sub =
1372 new (GetGraph()->GetArena()) HSub(instruction->GetType(), sub->GetRight(), sub->GetLeft());
1373 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, new_sub);
1374 if (!sub->HasUses()) {
1375 sub->GetBlock()->RemoveInstruction(sub);
1376 }
1377 RecordSimplification();
1378 }
1379}
1380
1381void InstructionSimplifierVisitor::VisitNot(HNot* instruction) {
1382 HInstruction* input = instruction->GetInput();
1383 if (input->IsNot()) {
1384 // Replace code looking like
1385 // NOT tmp, src
1386 // NOT dst, tmp
1387 // with
1388 // src
1389 // We perform the optimization even if the input negation has environment
1390 // uses since it allows removing the current instruction. But we only delete
1391 // the input negation only if it is does not have any uses left.
1392 HNot* previous_not = input->AsNot();
1393 instruction->ReplaceWith(previous_not->GetInput());
1394 instruction->GetBlock()->RemoveInstruction(instruction);
1395 if (!previous_not->HasUses()) {
1396 previous_not->GetBlock()->RemoveInstruction(previous_not);
1397 }
1398 RecordSimplification();
1399 }
1400}
1401
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001402void InstructionSimplifierVisitor::VisitOr(HOr* instruction) {
1403 HConstant* input_cst = instruction->GetConstantRight();
1404 HInstruction* input_other = instruction->GetLeastConstantLeft();
1405
Roland Levillain1a653882016-03-18 18:05:57 +00001406 if ((input_cst != nullptr) && input_cst->IsZeroBitPattern()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001407 // Replace code looking like
1408 // OR dst, src, 0
1409 // with
1410 // src
1411 instruction->ReplaceWith(input_other);
1412 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001413 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001414 return;
1415 }
1416
1417 // We assume that GVN has run before, so we only perform a pointer comparison.
1418 // If for some reason the values are equal but the pointers are different, we
Alexandre Rames188d4312015-04-09 18:30:21 +01001419 // are still correct and only miss an optimization opportunity.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001420 if (instruction->GetLeft() == instruction->GetRight()) {
1421 // Replace code looking like
1422 // OR dst, src, src
1423 // with
1424 // src
1425 instruction->ReplaceWith(instruction->GetLeft());
1426 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001427 RecordSimplification();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001428 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001429 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001430
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001431 if (TryDeMorganNegationFactoring(instruction)) return;
1432
Anton Kirilove14dc862016-05-13 17:56:15 +01001433 if (TryReplaceWithRotate(instruction)) {
1434 return;
1435 }
1436
1437 // TryHandleAssociativeAndCommutativeOperation() does not remove its input,
1438 // so no need to return.
1439 TryHandleAssociativeAndCommutativeOperation(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001440}
1441
1442void InstructionSimplifierVisitor::VisitShl(HShl* instruction) {
1443 VisitShift(instruction);
1444}
1445
1446void InstructionSimplifierVisitor::VisitShr(HShr* instruction) {
1447 VisitShift(instruction);
1448}
1449
1450void InstructionSimplifierVisitor::VisitSub(HSub* instruction) {
1451 HConstant* input_cst = instruction->GetConstantRight();
1452 HInstruction* input_other = instruction->GetLeastConstantLeft();
1453
Serguei Katkov115b53f2015-08-05 17:03:30 +06001454 Primitive::Type type = instruction->GetType();
1455 if (Primitive::IsFloatingPointType(type)) {
1456 return;
1457 }
1458
Roland Levillain1a653882016-03-18 18:05:57 +00001459 if ((input_cst != nullptr) && input_cst->IsArithmeticZero()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001460 // Replace code looking like
1461 // SUB dst, src, 0
1462 // with
1463 // src
Serguei Katkov115b53f2015-08-05 17:03:30 +06001464 // Note that we cannot optimize `x - 0.0` to `x` for floating-point. When
1465 // `x` is `-0.0`, the former expression yields `0.0`, while the later
1466 // yields `-0.0`.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001467 instruction->ReplaceWith(input_other);
1468 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001469 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001470 return;
1471 }
1472
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001473 HBasicBlock* block = instruction->GetBlock();
1474 ArenaAllocator* allocator = GetGraph()->GetArena();
1475
Alexandre Rames188d4312015-04-09 18:30:21 +01001476 HInstruction* left = instruction->GetLeft();
1477 HInstruction* right = instruction->GetRight();
1478 if (left->IsConstant()) {
1479 if (Int64FromConstant(left->AsConstant()) == 0) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001480 // Replace code looking like
1481 // SUB dst, 0, src
1482 // with
1483 // NEG dst, src
Alexandre Rames188d4312015-04-09 18:30:21 +01001484 // Note that we cannot optimize `0.0 - x` to `-x` for floating-point. When
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001485 // `x` is `0.0`, the former expression yields `0.0`, while the later
1486 // yields `-0.0`.
Alexandre Rames188d4312015-04-09 18:30:21 +01001487 HNeg* neg = new (allocator) HNeg(type, right);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001488 block->ReplaceAndRemoveInstructionWith(instruction, neg);
Alexandre Rames188d4312015-04-09 18:30:21 +01001489 RecordSimplification();
1490 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001491 }
1492 }
Alexandre Rames188d4312015-04-09 18:30:21 +01001493
1494 if (left->IsNeg() && right->IsNeg()) {
1495 if (TryMoveNegOnInputsAfterBinop(instruction)) {
1496 return;
1497 }
1498 }
1499
1500 if (right->IsNeg() && right->HasOnlyOneNonEnvironmentUse()) {
1501 // Replace code looking like
1502 // NEG tmp, b
1503 // SUB dst, a, tmp
1504 // with
1505 // ADD dst, a, b
1506 HAdd* add = new(GetGraph()->GetArena()) HAdd(type, left, right->AsNeg()->GetInput());
1507 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, add);
1508 RecordSimplification();
1509 right->GetBlock()->RemoveInstruction(right);
1510 return;
1511 }
1512
1513 if (left->IsNeg() && left->HasOnlyOneNonEnvironmentUse()) {
1514 // Replace code looking like
1515 // NEG tmp, a
1516 // SUB dst, tmp, b
1517 // with
1518 // ADD tmp, a, b
1519 // NEG dst, tmp
1520 // The second version is not intrinsically better, but enables more
1521 // transformations.
1522 HAdd* add = new(GetGraph()->GetArena()) HAdd(type, left->AsNeg()->GetInput(), right);
1523 instruction->GetBlock()->InsertInstructionBefore(add, instruction);
1524 HNeg* neg = new (GetGraph()->GetArena()) HNeg(instruction->GetType(), add);
1525 instruction->GetBlock()->InsertInstructionBefore(neg, instruction);
1526 instruction->ReplaceWith(neg);
1527 instruction->GetBlock()->RemoveInstruction(instruction);
1528 RecordSimplification();
1529 left->GetBlock()->RemoveInstruction(left);
Anton Kirilove14dc862016-05-13 17:56:15 +01001530 return;
1531 }
1532
1533 if (TrySubtractionChainSimplification(instruction)) {
1534 return;
Alexandre Rames188d4312015-04-09 18:30:21 +01001535 }
Maxim Kazantsevd3278bd2016-07-12 15:55:33 +06001536
1537 if (left->IsAdd()) {
1538 // Replace code patterns looking like
1539 // ADD dst1, x, y ADD dst1, x, y
1540 // SUB dst2, dst1, y SUB dst2, dst1, x
1541 // with
1542 // ADD dst1, x, y
1543 // SUB instruction is not needed in this case, we may use
1544 // one of inputs of ADD instead.
1545 // It is applicable to integral types only.
1546 DCHECK(Primitive::IsIntegralType(type));
1547 if (left->InputAt(1) == right) {
1548 instruction->ReplaceWith(left->InputAt(0));
1549 RecordSimplification();
1550 instruction->GetBlock()->RemoveInstruction(instruction);
1551 return;
1552 } else if (left->InputAt(0) == right) {
1553 instruction->ReplaceWith(left->InputAt(1));
1554 RecordSimplification();
1555 instruction->GetBlock()->RemoveInstruction(instruction);
1556 return;
1557 }
1558 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001559}
1560
1561void InstructionSimplifierVisitor::VisitUShr(HUShr* instruction) {
1562 VisitShift(instruction);
1563}
1564
1565void InstructionSimplifierVisitor::VisitXor(HXor* instruction) {
1566 HConstant* input_cst = instruction->GetConstantRight();
1567 HInstruction* input_other = instruction->GetLeastConstantLeft();
1568
Roland Levillain1a653882016-03-18 18:05:57 +00001569 if ((input_cst != nullptr) && input_cst->IsZeroBitPattern()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001570 // Replace code looking like
1571 // XOR dst, src, 0
1572 // with
1573 // src
1574 instruction->ReplaceWith(input_other);
1575 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001576 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001577 return;
1578 }
1579
1580 if ((input_cst != nullptr) && AreAllBitsSet(input_cst)) {
1581 // Replace code looking like
1582 // XOR dst, src, 0xFFF...FF
1583 // with
1584 // NOT dst, src
1585 HNot* bitwise_not = new (GetGraph()->GetArena()) HNot(instruction->GetType(), input_other);
1586 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, bitwise_not);
Alexandre Rames188d4312015-04-09 18:30:21 +01001587 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001588 return;
1589 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001590
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001591 HInstruction* left = instruction->GetLeft();
1592 HInstruction* right = instruction->GetRight();
Alexandre Rames9f980252016-02-05 14:00:28 +00001593 if (((left->IsNot() && right->IsNot()) ||
1594 (left->IsBooleanNot() && right->IsBooleanNot())) &&
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001595 left->HasOnlyOneNonEnvironmentUse() &&
1596 right->HasOnlyOneNonEnvironmentUse()) {
1597 // Replace code looking like
1598 // NOT nota, a
1599 // NOT notb, b
1600 // XOR dst, nota, notb
1601 // with
1602 // XOR dst, a, b
Alexandre Rames9f980252016-02-05 14:00:28 +00001603 instruction->ReplaceInput(left->InputAt(0), 0);
1604 instruction->ReplaceInput(right->InputAt(0), 1);
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001605 left->GetBlock()->RemoveInstruction(left);
1606 right->GetBlock()->RemoveInstruction(right);
1607 RecordSimplification();
1608 return;
1609 }
1610
Anton Kirilove14dc862016-05-13 17:56:15 +01001611 if (TryReplaceWithRotate(instruction)) {
1612 return;
1613 }
1614
1615 // TryHandleAssociativeAndCommutativeOperation() does not remove its input,
1616 // so no need to return.
1617 TryHandleAssociativeAndCommutativeOperation(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001618}
1619
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001620void InstructionSimplifierVisitor::SimplifyStringEquals(HInvoke* instruction) {
1621 HInstruction* argument = instruction->InputAt(1);
1622 HInstruction* receiver = instruction->InputAt(0);
1623 if (receiver == argument) {
1624 // Because String.equals is an instance call, the receiver is
1625 // a null check if we don't know it's null. The argument however, will
1626 // be the actual object. So we cannot end up in a situation where both
1627 // are equal but could be null.
1628 DCHECK(CanEnsureNotNullAt(argument, instruction));
1629 instruction->ReplaceWith(GetGraph()->GetIntConstant(1));
1630 instruction->GetBlock()->RemoveInstruction(instruction);
1631 } else {
1632 StringEqualsOptimizations optimizations(instruction);
1633 if (CanEnsureNotNullAt(argument, instruction)) {
1634 optimizations.SetArgumentNotNull();
1635 }
1636 ScopedObjectAccess soa(Thread::Current());
1637 ReferenceTypeInfo argument_rti = argument->GetReferenceTypeInfo();
1638 if (argument_rti.IsValid() && argument_rti.IsStringClass()) {
1639 optimizations.SetArgumentIsString();
1640 }
1641 }
1642}
1643
Roland Levillain22c49222016-03-18 14:04:28 +00001644void InstructionSimplifierVisitor::SimplifyRotate(HInvoke* invoke,
1645 bool is_left,
1646 Primitive::Type type) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001647 DCHECK(invoke->IsInvokeStaticOrDirect());
1648 DCHECK_EQ(invoke->GetOriginalInvokeType(), InvokeType::kStatic);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001649 HInstruction* value = invoke->InputAt(0);
1650 HInstruction* distance = invoke->InputAt(1);
1651 // Replace the invoke with an HRor.
1652 if (is_left) {
Roland Levillain937e6cd2016-03-22 11:54:37 +00001653 // Unconditionally set the type of the negated distance to `int`,
1654 // as shift and rotate operations expect a 32-bit (or narrower)
1655 // value for their distance input.
1656 distance = new (GetGraph()->GetArena()) HNeg(Primitive::kPrimInt, distance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001657 invoke->GetBlock()->InsertInstructionBefore(distance, invoke);
1658 }
Roland Levillain22c49222016-03-18 14:04:28 +00001659 HRor* ror = new (GetGraph()->GetArena()) HRor(type, value, distance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001660 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, ror);
1661 // Remove ClinitCheck and LoadClass, if possible.
Vladimir Marko372f10e2016-05-17 16:30:10 +01001662 HInstruction* clinit = invoke->GetInputs().back();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001663 if (clinit->IsClinitCheck() && !clinit->HasUses()) {
1664 clinit->GetBlock()->RemoveInstruction(clinit);
1665 HInstruction* ldclass = clinit->InputAt(0);
1666 if (ldclass->IsLoadClass() && !ldclass->HasUses()) {
1667 ldclass->GetBlock()->RemoveInstruction(ldclass);
1668 }
1669 }
1670}
1671
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001672static bool IsArrayLengthOf(HInstruction* potential_length, HInstruction* potential_array) {
1673 if (potential_length->IsArrayLength()) {
1674 return potential_length->InputAt(0) == potential_array;
1675 }
1676
1677 if (potential_array->IsNewArray()) {
1678 return potential_array->InputAt(0) == potential_length;
1679 }
1680
1681 return false;
1682}
1683
1684void InstructionSimplifierVisitor::SimplifySystemArrayCopy(HInvoke* instruction) {
1685 HInstruction* source = instruction->InputAt(0);
1686 HInstruction* destination = instruction->InputAt(2);
1687 HInstruction* count = instruction->InputAt(4);
1688 SystemArrayCopyOptimizations optimizations(instruction);
1689 if (CanEnsureNotNullAt(source, instruction)) {
1690 optimizations.SetSourceIsNotNull();
1691 }
1692 if (CanEnsureNotNullAt(destination, instruction)) {
1693 optimizations.SetDestinationIsNotNull();
1694 }
1695 if (destination == source) {
1696 optimizations.SetDestinationIsSource();
1697 }
1698
1699 if (IsArrayLengthOf(count, source)) {
1700 optimizations.SetCountIsSourceLength();
1701 }
1702
1703 if (IsArrayLengthOf(count, destination)) {
1704 optimizations.SetCountIsDestinationLength();
1705 }
1706
1707 {
1708 ScopedObjectAccess soa(Thread::Current());
1709 ReferenceTypeInfo destination_rti = destination->GetReferenceTypeInfo();
1710 if (destination_rti.IsValid()) {
1711 if (destination_rti.IsObjectArray()) {
1712 if (destination_rti.IsExact()) {
1713 optimizations.SetDoesNotNeedTypeCheck();
1714 }
1715 optimizations.SetDestinationIsTypedObjectArray();
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001716 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001717 if (destination_rti.IsPrimitiveArrayClass()) {
1718 optimizations.SetDestinationIsPrimitiveArray();
1719 } else if (destination_rti.IsNonPrimitiveArrayClass()) {
1720 optimizations.SetDestinationIsNonPrimitiveArray();
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001721 }
1722 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001723 ReferenceTypeInfo source_rti = source->GetReferenceTypeInfo();
1724 if (source_rti.IsValid()) {
1725 if (destination_rti.IsValid() && destination_rti.CanArrayHoldValuesOf(source_rti)) {
1726 optimizations.SetDoesNotNeedTypeCheck();
1727 }
1728 if (source_rti.IsPrimitiveArrayClass()) {
1729 optimizations.SetSourceIsPrimitiveArray();
1730 } else if (source_rti.IsNonPrimitiveArrayClass()) {
1731 optimizations.SetSourceIsNonPrimitiveArray();
1732 }
1733 }
1734 }
1735}
1736
Roland Levillaina5c4a402016-03-15 15:02:50 +00001737void InstructionSimplifierVisitor::SimplifyCompare(HInvoke* invoke,
1738 bool is_signum,
1739 Primitive::Type type) {
Aart Bika19616e2016-02-01 18:57:58 -08001740 DCHECK(invoke->IsInvokeStaticOrDirect());
1741 uint32_t dex_pc = invoke->GetDexPc();
1742 HInstruction* left = invoke->InputAt(0);
1743 HInstruction* right;
Aart Bika19616e2016-02-01 18:57:58 -08001744 if (!is_signum) {
1745 right = invoke->InputAt(1);
1746 } else if (type == Primitive::kPrimLong) {
1747 right = GetGraph()->GetLongConstant(0);
1748 } else {
1749 right = GetGraph()->GetIntConstant(0);
1750 }
1751 HCompare* compare = new (GetGraph()->GetArena())
1752 HCompare(type, left, right, ComparisonBias::kNoBias, dex_pc);
1753 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, compare);
1754}
1755
Aart Bik75a38b22016-02-17 10:41:50 -08001756void InstructionSimplifierVisitor::SimplifyIsNaN(HInvoke* invoke) {
1757 DCHECK(invoke->IsInvokeStaticOrDirect());
1758 uint32_t dex_pc = invoke->GetDexPc();
1759 // IsNaN(x) is the same as x != x.
1760 HInstruction* x = invoke->InputAt(0);
1761 HCondition* condition = new (GetGraph()->GetArena()) HNotEqual(x, x, dex_pc);
Aart Bik8ffc1fa2016-02-17 15:13:56 -08001762 condition->SetBias(ComparisonBias::kLtBias);
Aart Bik75a38b22016-02-17 10:41:50 -08001763 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, condition);
1764}
1765
Aart Bik2a6aad92016-02-25 11:32:32 -08001766void InstructionSimplifierVisitor::SimplifyFP2Int(HInvoke* invoke) {
1767 DCHECK(invoke->IsInvokeStaticOrDirect());
1768 uint32_t dex_pc = invoke->GetDexPc();
1769 HInstruction* x = invoke->InputAt(0);
1770 Primitive::Type type = x->GetType();
1771 // Set proper bit pattern for NaN and replace intrinsic with raw version.
1772 HInstruction* nan;
1773 if (type == Primitive::kPrimDouble) {
1774 nan = GetGraph()->GetLongConstant(0x7ff8000000000000L);
1775 invoke->SetIntrinsic(Intrinsics::kDoubleDoubleToRawLongBits,
1776 kNeedsEnvironmentOrCache,
1777 kNoSideEffects,
1778 kNoThrow);
1779 } else {
1780 DCHECK_EQ(type, Primitive::kPrimFloat);
1781 nan = GetGraph()->GetIntConstant(0x7fc00000);
1782 invoke->SetIntrinsic(Intrinsics::kFloatFloatToRawIntBits,
1783 kNeedsEnvironmentOrCache,
1784 kNoSideEffects,
1785 kNoThrow);
1786 }
1787 // Test IsNaN(x), which is the same as x != x.
1788 HCondition* condition = new (GetGraph()->GetArena()) HNotEqual(x, x, dex_pc);
1789 condition->SetBias(ComparisonBias::kLtBias);
1790 invoke->GetBlock()->InsertInstructionBefore(condition, invoke->GetNext());
1791 // Select between the two.
1792 HInstruction* select = new (GetGraph()->GetArena()) HSelect(condition, nan, invoke, dex_pc);
1793 invoke->GetBlock()->InsertInstructionBefore(select, condition->GetNext());
1794 invoke->ReplaceWithExceptInReplacementAtIndex(select, 0); // false at index 0
1795}
1796
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001797void InstructionSimplifierVisitor::SimplifyStringCharAt(HInvoke* invoke) {
1798 HInstruction* str = invoke->InputAt(0);
1799 HInstruction* index = invoke->InputAt(1);
1800 uint32_t dex_pc = invoke->GetDexPc();
1801 ArenaAllocator* arena = GetGraph()->GetArena();
1802 // We treat String as an array to allow DCE and BCE to seamlessly work on strings,
1803 // so create the HArrayLength, HBoundsCheck and HArrayGet.
1804 HArrayLength* length = new (arena) HArrayLength(str, dex_pc, /* is_string_length */ true);
1805 invoke->GetBlock()->InsertInstructionBefore(length, invoke);
1806 HBoundsCheck* bounds_check =
1807 new (arena) HBoundsCheck(index, length, dex_pc, invoke->GetDexMethodIndex());
1808 invoke->GetBlock()->InsertInstructionBefore(bounds_check, invoke);
1809 HArrayGet* array_get =
1810 new (arena) HArrayGet(str, index, Primitive::kPrimChar, dex_pc, /* is_string_char_at */ true);
1811 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, array_get);
1812 bounds_check->CopyEnvironmentFrom(invoke->GetEnvironment());
1813 GetGraph()->SetHasBoundsChecks(true);
1814}
1815
Vladimir Markodce016e2016-04-28 13:10:02 +01001816void InstructionSimplifierVisitor::SimplifyStringIsEmptyOrLength(HInvoke* invoke) {
1817 HInstruction* str = invoke->InputAt(0);
1818 uint32_t dex_pc = invoke->GetDexPc();
1819 // We treat String as an array to allow DCE and BCE to seamlessly work on strings,
1820 // so create the HArrayLength.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001821 HArrayLength* length =
1822 new (GetGraph()->GetArena()) HArrayLength(str, dex_pc, /* is_string_length */ true);
Vladimir Markodce016e2016-04-28 13:10:02 +01001823 HInstruction* replacement;
1824 if (invoke->GetIntrinsic() == Intrinsics::kStringIsEmpty) {
1825 // For String.isEmpty(), create the `HEqual` representing the `length == 0`.
1826 invoke->GetBlock()->InsertInstructionBefore(length, invoke);
1827 HIntConstant* zero = GetGraph()->GetIntConstant(0);
1828 HEqual* equal = new (GetGraph()->GetArena()) HEqual(length, zero, dex_pc);
1829 replacement = equal;
1830 } else {
1831 DCHECK_EQ(invoke->GetIntrinsic(), Intrinsics::kStringLength);
1832 replacement = length;
1833 }
1834 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, replacement);
1835}
1836
Aart Bik11932592016-03-08 12:42:25 -08001837void InstructionSimplifierVisitor::SimplifyMemBarrier(HInvoke* invoke, MemBarrierKind barrier_kind) {
1838 uint32_t dex_pc = invoke->GetDexPc();
1839 HMemoryBarrier* mem_barrier = new (GetGraph()->GetArena()) HMemoryBarrier(barrier_kind, dex_pc);
1840 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, mem_barrier);
1841}
1842
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001843void InstructionSimplifierVisitor::VisitInvoke(HInvoke* instruction) {
Aart Bik2a6aad92016-02-25 11:32:32 -08001844 switch (instruction->GetIntrinsic()) {
1845 case Intrinsics::kStringEquals:
1846 SimplifyStringEquals(instruction);
1847 break;
1848 case Intrinsics::kSystemArrayCopy:
1849 SimplifySystemArrayCopy(instruction);
1850 break;
1851 case Intrinsics::kIntegerRotateRight:
Roland Levillain22c49222016-03-18 14:04:28 +00001852 SimplifyRotate(instruction, /* is_left */ false, Primitive::kPrimInt);
1853 break;
Aart Bik2a6aad92016-02-25 11:32:32 -08001854 case Intrinsics::kLongRotateRight:
Roland Levillain22c49222016-03-18 14:04:28 +00001855 SimplifyRotate(instruction, /* is_left */ false, Primitive::kPrimLong);
Aart Bik2a6aad92016-02-25 11:32:32 -08001856 break;
1857 case Intrinsics::kIntegerRotateLeft:
Roland Levillain22c49222016-03-18 14:04:28 +00001858 SimplifyRotate(instruction, /* is_left */ true, Primitive::kPrimInt);
1859 break;
Aart Bik2a6aad92016-02-25 11:32:32 -08001860 case Intrinsics::kLongRotateLeft:
Roland Levillain22c49222016-03-18 14:04:28 +00001861 SimplifyRotate(instruction, /* is_left */ true, Primitive::kPrimLong);
Aart Bik2a6aad92016-02-25 11:32:32 -08001862 break;
1863 case Intrinsics::kIntegerCompare:
Roland Levillaina5c4a402016-03-15 15:02:50 +00001864 SimplifyCompare(instruction, /* is_signum */ false, Primitive::kPrimInt);
1865 break;
Aart Bik2a6aad92016-02-25 11:32:32 -08001866 case Intrinsics::kLongCompare:
Roland Levillaina5c4a402016-03-15 15:02:50 +00001867 SimplifyCompare(instruction, /* is_signum */ false, Primitive::kPrimLong);
Aart Bik2a6aad92016-02-25 11:32:32 -08001868 break;
1869 case Intrinsics::kIntegerSignum:
Roland Levillaina5c4a402016-03-15 15:02:50 +00001870 SimplifyCompare(instruction, /* is_signum */ true, Primitive::kPrimInt);
1871 break;
Aart Bik2a6aad92016-02-25 11:32:32 -08001872 case Intrinsics::kLongSignum:
Roland Levillaina5c4a402016-03-15 15:02:50 +00001873 SimplifyCompare(instruction, /* is_signum */ true, Primitive::kPrimLong);
Aart Bik2a6aad92016-02-25 11:32:32 -08001874 break;
1875 case Intrinsics::kFloatIsNaN:
1876 case Intrinsics::kDoubleIsNaN:
1877 SimplifyIsNaN(instruction);
1878 break;
1879 case Intrinsics::kFloatFloatToIntBits:
1880 case Intrinsics::kDoubleDoubleToLongBits:
1881 SimplifyFP2Int(instruction);
1882 break;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001883 case Intrinsics::kStringCharAt:
1884 SimplifyStringCharAt(instruction);
1885 break;
Vladimir Markodce016e2016-04-28 13:10:02 +01001886 case Intrinsics::kStringIsEmpty:
1887 case Intrinsics::kStringLength:
1888 SimplifyStringIsEmptyOrLength(instruction);
1889 break;
Aart Bik11932592016-03-08 12:42:25 -08001890 case Intrinsics::kUnsafeLoadFence:
1891 SimplifyMemBarrier(instruction, MemBarrierKind::kLoadAny);
1892 break;
1893 case Intrinsics::kUnsafeStoreFence:
1894 SimplifyMemBarrier(instruction, MemBarrierKind::kAnyStore);
1895 break;
1896 case Intrinsics::kUnsafeFullFence:
1897 SimplifyMemBarrier(instruction, MemBarrierKind::kAnyAny);
1898 break;
Aart Bik2a6aad92016-02-25 11:32:32 -08001899 default:
1900 break;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001901 }
1902}
1903
Aart Bikbb245d12015-10-19 11:05:03 -07001904void InstructionSimplifierVisitor::VisitDeoptimize(HDeoptimize* deoptimize) {
1905 HInstruction* cond = deoptimize->InputAt(0);
1906 if (cond->IsConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001907 if (cond->AsIntConstant()->IsFalse()) {
Aart Bikbb245d12015-10-19 11:05:03 -07001908 // Never deopt: instruction can be removed.
1909 deoptimize->GetBlock()->RemoveInstruction(deoptimize);
1910 } else {
1911 // Always deopt.
1912 }
1913 }
1914}
1915
Anton Kirilove14dc862016-05-13 17:56:15 +01001916// Replace code looking like
1917// OP y, x, const1
1918// OP z, y, const2
1919// with
1920// OP z, x, const3
1921// where OP is both an associative and a commutative operation.
1922bool InstructionSimplifierVisitor::TryHandleAssociativeAndCommutativeOperation(
1923 HBinaryOperation* instruction) {
1924 DCHECK(instruction->IsCommutative());
1925
1926 if (!Primitive::IsIntegralType(instruction->GetType())) {
1927 return false;
1928 }
1929
1930 HInstruction* left = instruction->GetLeft();
1931 HInstruction* right = instruction->GetRight();
1932 // Variable names as described above.
1933 HConstant* const2;
1934 HBinaryOperation* y;
1935
1936 if (instruction->InstructionTypeEquals(left) && right->IsConstant()) {
1937 const2 = right->AsConstant();
1938 y = left->AsBinaryOperation();
1939 } else if (left->IsConstant() && instruction->InstructionTypeEquals(right)) {
1940 const2 = left->AsConstant();
1941 y = right->AsBinaryOperation();
1942 } else {
1943 // The node does not match the pattern.
1944 return false;
1945 }
1946
1947 // If `y` has more than one use, we do not perform the optimization
1948 // because it might increase code size (e.g. if the new constant is
1949 // no longer encodable as an immediate operand in the target ISA).
1950 if (!y->HasOnlyOneNonEnvironmentUse()) {
1951 return false;
1952 }
1953
1954 // GetConstantRight() can return both left and right constants
1955 // for commutative operations.
1956 HConstant* const1 = y->GetConstantRight();
1957 if (const1 == nullptr) {
1958 return false;
1959 }
1960
1961 instruction->ReplaceInput(const1, 0);
1962 instruction->ReplaceInput(const2, 1);
1963 HConstant* const3 = instruction->TryStaticEvaluation();
1964 DCHECK(const3 != nullptr);
1965 instruction->ReplaceInput(y->GetLeastConstantLeft(), 0);
1966 instruction->ReplaceInput(const3, 1);
1967 RecordSimplification();
1968 return true;
1969}
1970
1971static HBinaryOperation* AsAddOrSub(HInstruction* binop) {
1972 return (binop->IsAdd() || binop->IsSub()) ? binop->AsBinaryOperation() : nullptr;
1973}
1974
1975// Helper function that performs addition statically, considering the result type.
1976static int64_t ComputeAddition(Primitive::Type type, int64_t x, int64_t y) {
1977 // Use the Compute() method for consistency with TryStaticEvaluation().
1978 if (type == Primitive::kPrimInt) {
1979 return HAdd::Compute<int32_t>(x, y);
1980 } else {
1981 DCHECK_EQ(type, Primitive::kPrimLong);
1982 return HAdd::Compute<int64_t>(x, y);
1983 }
1984}
1985
1986// Helper function that handles the child classes of HConstant
1987// and returns an integer with the appropriate sign.
1988static int64_t GetValue(HConstant* constant, bool is_negated) {
1989 int64_t ret = Int64FromConstant(constant);
1990 return is_negated ? -ret : ret;
1991}
1992
1993// Replace code looking like
1994// OP1 y, x, const1
1995// OP2 z, y, const2
1996// with
1997// OP3 z, x, const3
1998// where OPx is either ADD or SUB, and at least one of OP{1,2} is SUB.
1999bool InstructionSimplifierVisitor::TrySubtractionChainSimplification(
2000 HBinaryOperation* instruction) {
2001 DCHECK(instruction->IsAdd() || instruction->IsSub()) << instruction->DebugName();
2002
2003 Primitive::Type type = instruction->GetType();
2004 if (!Primitive::IsIntegralType(type)) {
2005 return false;
2006 }
2007
2008 HInstruction* left = instruction->GetLeft();
2009 HInstruction* right = instruction->GetRight();
2010 // Variable names as described above.
2011 HConstant* const2 = right->IsConstant() ? right->AsConstant() : left->AsConstant();
2012 if (const2 == nullptr) {
2013 return false;
2014 }
2015
2016 HBinaryOperation* y = (AsAddOrSub(left) != nullptr)
2017 ? left->AsBinaryOperation()
2018 : AsAddOrSub(right);
2019 // If y has more than one use, we do not perform the optimization because
2020 // it might increase code size (e.g. if the new constant is no longer
2021 // encodable as an immediate operand in the target ISA).
2022 if ((y == nullptr) || !y->HasOnlyOneNonEnvironmentUse()) {
2023 return false;
2024 }
2025
2026 left = y->GetLeft();
2027 HConstant* const1 = left->IsConstant() ? left->AsConstant() : y->GetRight()->AsConstant();
2028 if (const1 == nullptr) {
2029 return false;
2030 }
2031
2032 HInstruction* x = (const1 == left) ? y->GetRight() : left;
2033 // If both inputs are constants, let the constant folding pass deal with it.
2034 if (x->IsConstant()) {
2035 return false;
2036 }
2037
2038 bool is_const2_negated = (const2 == right) && instruction->IsSub();
2039 int64_t const2_val = GetValue(const2, is_const2_negated);
2040 bool is_y_negated = (y == right) && instruction->IsSub();
2041 right = y->GetRight();
2042 bool is_const1_negated = is_y_negated ^ ((const1 == right) && y->IsSub());
2043 int64_t const1_val = GetValue(const1, is_const1_negated);
2044 bool is_x_negated = is_y_negated ^ ((x == right) && y->IsSub());
2045 int64_t const3_val = ComputeAddition(type, const1_val, const2_val);
2046 HBasicBlock* block = instruction->GetBlock();
2047 HConstant* const3 = block->GetGraph()->GetConstant(type, const3_val);
2048 ArenaAllocator* arena = instruction->GetArena();
2049 HInstruction* z;
2050
2051 if (is_x_negated) {
2052 z = new (arena) HSub(type, const3, x, instruction->GetDexPc());
2053 } else {
2054 z = new (arena) HAdd(type, x, const3, instruction->GetDexPc());
2055 }
2056
2057 block->ReplaceAndRemoveInstructionWith(instruction, z);
2058 RecordSimplification();
2059 return true;
2060}
2061
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002062} // namespace art